diff --git a/src/audio/FreqPlayer.cpp b/src/audio/FreqPlayer.cpp index 292675d..39d535c 100644 --- a/src/audio/FreqPlayer.cpp +++ b/src/audio/FreqPlayer.cpp @@ -21,6 +21,14 @@ #include "FreqPlayer.hpp" +#ifndef min +#define min(a,b) (ab?a:b) +#endif + template FreqPlayer::FreqPlayer(int _rate): rate(_rate), n_frame(0) @@ -28,6 +36,7 @@ template FreqPlayer::FreqPlayer(int _rate): k = K(); k_update = -1; // invalid: don't update max_harmonic_freq = rate / 2; + freq2volume(); } template void FreqPlayer::Reset() @@ -37,6 +46,7 @@ template void FreqPlayer::Reset() if (k_update != -1) { k = k_update; k_update = -1; + freq2volume(); } } @@ -62,8 +72,11 @@ template void FreqPlayer::SetWaveform(WAVEFORM form waveform = form; } -template<> int16_t FreqPlayer::max() { return INT16_MAX; } -template<> double FreqPlayer::max() { return 1; } +template<> int16_t FreqPlayer::sample_min() { return INT16_MIN; } +template<> double FreqPlayer::sample_min() { return -1; } + +template<> int16_t FreqPlayer::sample_max() { return INT16_MAX; } +template<> double FreqPlayer::sample_max() { return 1; } template double FreqPlayer::radius() { @@ -102,7 +115,13 @@ template sample_t FreqPlayer::AudioFrame() break; } - return v * max() * volume; + return max(min(v * sample_max() * volume, sample_max()), sample_min()); +} + +template void FreqPlayer::freq2volume() +{ + if (volume_adaptative) volume = min(volume_max, max(volume_min, (freq_volume_min - freq) / (freq_volume_min - freq_volume_max) * (volume_max - volume_min) + volume_min)); + std::cerr << "volume " << volume << std::endl; } template void FreqPlayer::WriteAudio(sample_t *out, int nb_frame, bool stop) @@ -119,6 +138,7 @@ template void FreqPlayer::WriteAudio(sample_t *out, n_frame = 0; k = k_update; k_update = -1; + freq2volume(); v = AudioFrame(); } else if (stop) { diff --git a/src/audio/FreqPlayer.hpp b/src/audio/FreqPlayer.hpp index 571b1e3..8bc3c9a 100644 --- a/src/audio/FreqPlayer.hpp +++ b/src/audio/FreqPlayer.hpp @@ -30,10 +30,19 @@ template class FreqPlayer { enum WAVEFORM { W_SINUS, W_TRIANGLE, W_HARMONIC }; private: + /// frequencies for min and max volumes + static constexpr double freq_volume_min = 1200; + static constexpr double freq_volume_max = 120; + /// current frequency double freq = 440; /// current volume (linear) double volume = 0.5; + /// volume min and max + double volume_min = 0.5; + double volume_max = 1; + /// if volume is adaptative to freq (lower -> volume higher) + bool volume_adaptative = true; /// audio rate int rate; /// current frame nb @@ -52,9 +61,13 @@ template class FreqPlayer { /// return k computed double K() const; /// return the max sample_t - sample_t max(); + sample_t sample_min(); + /// return the max sample_t + sample_t sample_max(); /// return the current radius double radius(); + /// return volume if adaptative + void freq2volume(); public: FreqPlayer(int rate);