mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-01-07 22:57:48 +00:00
Refactor to update audio stats when audio output is muted.
This commit is contained in:
parent
665f54b410
commit
e51db769e6
@ -194,6 +194,29 @@ class AudioStatsCollector {
|
|||||||
public:
|
public:
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
void feed(buffer_s16_t src, Callback callback) {
|
void feed(buffer_s16_t src, Callback callback) {
|
||||||
|
consume_audio_buffer(src);
|
||||||
|
|
||||||
|
if( update_stats(src.count, src.sampling_rate) ) {
|
||||||
|
callback(statistics);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
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;
|
auto src_p = src.p;
|
||||||
const auto src_end = &src.p[src.count];
|
const auto src_end = &src.p[src.count];
|
||||||
while(src_p < src_end) {
|
while(src_p < src_end) {
|
||||||
@ -204,34 +227,30 @@ public:
|
|||||||
max_squared = sample_squared;
|
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 ) {
|
if( count >= samples_per_update ) {
|
||||||
const float squared_sum_f = squared_sum;
|
const float squared_sum_f = squared_sum;
|
||||||
const float max_squared_f = max_squared;
|
const float max_squared_f = max_squared;
|
||||||
const float squared_avg_f = squared_sum_f / count;
|
const float squared_avg_f = squared_sum_f / count;
|
||||||
const int32_t rms_db = complex16_mag_squared_to_dbv_norm(squared_avg_f);
|
statistics.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);
|
statistics.max_db = complex16_mag_squared_to_dbv_norm(max_squared_f);
|
||||||
const AudioStatistics statistics {
|
statistics.count = count;
|
||||||
.rms_db = rms_db,
|
|
||||||
.max_db = max_db,
|
|
||||||
.count = count,
|
|
||||||
};
|
|
||||||
callback(statistics);
|
|
||||||
|
|
||||||
squared_sum = 0;
|
squared_sum = 0;
|
||||||
max_squared = 0;
|
max_squared = 0;
|
||||||
count = 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 {
|
class ChannelDecimator {
|
||||||
@ -405,16 +424,20 @@ protected:
|
|||||||
for(size_t i=0; i<audio_buffer.count; i++) {
|
for(size_t i=0; i<audio_buffer.count; i++) {
|
||||||
audio_buffer.p[i].left = audio_buffer.p[i].right = audio.p[i];
|
audio_buffer.p[i].left = audio_buffer.p[i].right = audio.p[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
i2s::i2s0::tx_unmute();
|
i2s::i2s0::tx_unmute();
|
||||||
|
|
||||||
feed_audio_stats(audio);
|
feed_audio_stats(audio);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mute_audio() {
|
void mute_audio_buffer() {
|
||||||
// TODO: Feed audio stats? What if baseband never produces audio?
|
auto audio_buffer = audio::dma::tx_empty_buffer();;
|
||||||
// TODO: How should audio stats behave if I *sometimes* mute audio?
|
for(size_t i=0; i<audio_buffer.count; i++) {
|
||||||
i2s::i2s0::tx_mute();
|
audio_buffer.p[i].left = audio_buffer.p[i].right = 0;
|
||||||
|
}
|
||||||
|
i2s::i2s0::tx_unmute();
|
||||||
|
|
||||||
|
// TODO: No hard-coded audio sampling rate!
|
||||||
|
mute_audio_stats(audio_buffer.count, 48000);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -451,6 +474,16 @@ private:
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mute_audio_stats(const size_t count, const size_t sampling_rate) {
|
||||||
|
audio_stats.mute(
|
||||||
|
count,
|
||||||
|
sampling_rate,
|
||||||
|
[this](const AudioStatistics statistics) {
|
||||||
|
this->post_audio_stats_message(statistics);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
void post_audio_stats_message(const AudioStatistics statistics) {
|
void post_audio_stats_message(const AudioStatistics statistics) {
|
||||||
if( audio_stats_message.is_free() ) {
|
if( audio_stats_message.is_free() ) {
|
||||||
audio_stats_message.statistics = statistics;
|
audio_stats_message.statistics = statistics;
|
||||||
@ -546,7 +579,7 @@ public:
|
|||||||
audio_hpf.execute_in_place(audio);
|
audio_hpf.execute_in_place(audio);
|
||||||
fill_audio_buffer(audio);
|
fill_audio_buffer(audio);
|
||||||
} else {
|
} else {
|
||||||
mute_audio();
|
mute_audio_buffer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -694,7 +727,7 @@ public:
|
|||||||
|
|
||||||
auto demodulated = demod.execute(channel, work_demod_buffer);
|
auto demodulated = demod.execute(channel, work_demod_buffer);
|
||||||
|
|
||||||
mute_audio();
|
mute_audio_buffer();
|
||||||
|
|
||||||
for(size_t i=0; i<demodulated.count; i++) {
|
for(size_t i=0; i<demodulated.count; i++) {
|
||||||
clock_recovery.execute(demodulated.p[i], symbol_handler_fn);
|
clock_recovery.execute(demodulated.p[i], symbol_handler_fn);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user