Skip to content

Commit 4265d84

Browse files
committed
Some performance optimizations. to reduce # of calls to GetAudioSamples()
1 parent c838c12 commit 4265d84

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/AudioWaveformer.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,19 @@ AudioWaveformData AudioWaveformer::ExtractSamples(int channel, int num_per_secon
6767
// Get next frame
6868
shared_ptr<openshot::Frame> frame = reader->GetFrame(f);
6969

70+
// Cache channels for this frame, to reduce # of calls to frame->GetAudioSamples
71+
float* channels[channel_count];
72+
for (auto channel_index = 0; channel_index < reader->info.channels; channel_index++) {
73+
if (channel == channel_index || channel == -1) {
74+
channels[channel_index] = frame->GetAudioSamples(channel_index);
75+
}
76+
}
77+
7078
// Get sample value from a specific channel (or all channels)
7179
for (auto s = 0; s < frame->GetAudioSamplesCount(); s++) {
72-
7380
for (auto channel_index = 0; channel_index < reader->info.channels; channel_index++) {
7481
if (channel == channel_index || channel == -1) {
75-
float *samples = frame->GetAudioSamples(channel_index);
82+
float *samples = channels[channel_index];
7683
float rms_sample_value = std::sqrt(samples[s] * samples[s]);
7784

7885
// Accumulate sample averages

0 commit comments

Comments
 (0)