From 4265d84ff9bd45e0e4e42377375e5b4437b9ec15 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Tue, 1 Nov 2022 16:48:37 -0500 Subject: [PATCH] Some performance optimizations. to reduce # of calls to GetAudioSamples() --- src/AudioWaveformer.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/AudioWaveformer.cpp b/src/AudioWaveformer.cpp index 2c064941..2b3761c5 100644 --- a/src/AudioWaveformer.cpp +++ b/src/AudioWaveformer.cpp @@ -67,12 +67,19 @@ AudioWaveformData AudioWaveformer::ExtractSamples(int channel, int num_per_secon // Get next frame shared_ptr frame = reader->GetFrame(f); + // Cache channels for this frame, to reduce # of calls to frame->GetAudioSamples + float* channels[channel_count]; + for (auto channel_index = 0; channel_index < reader->info.channels; channel_index++) { + if (channel == channel_index || channel == -1) { + channels[channel_index] = frame->GetAudioSamples(channel_index); + } + } + // Get sample value from a specific channel (or all channels) for (auto s = 0; s < frame->GetAudioSamplesCount(); s++) { - for (auto channel_index = 0; channel_index < reader->info.channels; channel_index++) { if (channel == channel_index || channel == -1) { - float *samples = frame->GetAudioSamples(channel_index); + float *samples = channels[channel_index]; float rms_sample_value = std::sqrt(samples[s] * samples[s]); // Accumulate sample averages