diff --git a/firmware/baseband/main.cpp b/firmware/baseband/main.cpp index 1d1ba109..5b3d9436 100755 --- a/firmware/baseband/main.cpp +++ b/firmware/baseband/main.cpp @@ -194,6 +194,29 @@ class AudioStatsCollector { public: template void feed(buffer_s16_t src, Callback callback) { + consume_audio_buffer(src); + + if( update_stats(src.count, src.sampling_rate) ) { + callback(statistics); + } + } + + template + void mute(const size_t sample_count, const size_t sampling_rate, Callback callback) { + if( update_stats(sample_count, sampling_rate) ) { + callback(statistics); + } + } + +private: + static constexpr float update_interval { 0.1f }; + uint64_t squared_sum { 0 }; + uint32_t max_squared { 0 }; + size_t count { 0 }; + + AudioStatistics statistics; + + void consume_audio_buffer(buffer_s16_t src) { auto src_p = src.p; const auto src_end = &src.p[src.count]; while(src_p < src_end) { @@ -204,34 +227,30 @@ public: max_squared = sample_squared; } } - count += src.count; + } - const size_t samples_per_update = src.sampling_rate * update_interval; + bool update_stats(const size_t sample_count, const size_t sampling_rate) { + count += sample_count; + + const size_t samples_per_update = sampling_rate * update_interval; if( count >= samples_per_update ) { const float squared_sum_f = squared_sum; const float max_squared_f = max_squared; const float squared_avg_f = squared_sum_f / count; - const int32_t rms_db = complex16_mag_squared_to_dbv_norm(squared_avg_f); - const int32_t max_db = complex16_mag_squared_to_dbv_norm(max_squared_f); - const AudioStatistics statistics { - .rms_db = rms_db, - .max_db = max_db, - .count = count, - }; - callback(statistics); + statistics.rms_db = complex16_mag_squared_to_dbv_norm(squared_avg_f); + statistics.max_db = complex16_mag_squared_to_dbv_norm(max_squared_f); + statistics.count = count; squared_sum = 0; max_squared = 0; count = 0; + + return true; + } else { + return false; } } - -private: - static constexpr float update_interval { 0.1f }; - uint64_t squared_sum { 0 }; - uint32_t max_squared { 0 }; - size_t count { 0 }; }; class ChannelDecimator { @@ -405,16 +424,20 @@ protected: for(size_t i=0; ipost_audio_stats_message(statistics); + } + ); + } + void post_audio_stats_message(const AudioStatistics statistics) { if( audio_stats_message.is_free() ) { audio_stats_message.statistics = statistics; @@ -546,7 +579,7 @@ public: audio_hpf.execute_in_place(audio); fill_audio_buffer(audio); } else { - mute_audio(); + mute_audio_buffer(); } } @@ -694,7 +727,7 @@ public: auto demodulated = demod.execute(channel, work_demod_buffer); - mute_audio(); + mute_audio_buffer(); for(size_t i=0; i