From f4bb729235259d185d68b79d35c850f854d6a230 Mon Sep 17 00:00:00 2001 From: UnknownSuperficialNight <88142731+UnknownSuperficialNight@users.noreply.github.com> Date: Sun, 29 Sep 2024 15:03:58 +1300 Subject: [PATCH] Added benchmark for agc and inlines --- benches/effects.rs | 16 ++++++++++++++++ src/source/agc.rs | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/benches/effects.rs b/benches/effects.rs index 5f10011..eba38fa 100644 --- a/benches/effects.rs +++ b/benches/effects.rs @@ -46,3 +46,19 @@ fn amplify(bencher: Bencher) { .with_inputs(|| TestSource::music_wav().to_f32s()) .bench_values(|source| source.amplify(0.8).for_each(divan::black_box_drop)) } + +#[divan::bench] +fn agc(bencher: Bencher) { + bencher + .with_inputs(|| TestSource::music_wav().to_f32s()) + .bench_values(|source| { + source + .automatic_gain_control( + 1.0, // target_level + 2.0, // attack_time (in seconds) + 0.01, // release_time (in seconds) + 5.0, // absolute_max_gain + ) + .for_each(divan::black_box_drop) + }) +} diff --git a/src/source/agc.rs b/src/source/agc.rs index 02777dd..495c1b3 100644 --- a/src/source/agc.rs +++ b/src/source/agc.rs @@ -59,6 +59,7 @@ impl CircularBuffer { /// Creates a new CircularBuffer with a fixed size determined at compile time. /// /// The `_size` parameter is ignored as the buffer size is set by `RMS_WINDOW_SIZE`. + #[inline] fn new(_size: usize) -> Self { CircularBuffer { buffer: [0.0; RMS_WINDOW_SIZE], @@ -70,6 +71,7 @@ impl CircularBuffer { /// Pushes a new value into the buffer and returns the old value. /// /// This method maintains a running sum for efficient mean calculation. + #[inline] fn push(&mut self, value: f32) -> f32 { let old_value = self.buffer[self.index]; self.buffer[self.index] = value; @@ -81,6 +83,7 @@ impl CircularBuffer { /// Calculates the mean of all values in the buffer. /// /// This operation is O(1) due to the maintained running sum. + #[inline] fn mean(&self) -> f32 { self.sum / self.buffer.len() as f32 } @@ -95,6 +98,7 @@ impl CircularBuffer { /// * `attack_time` - Time constant for gain increase /// * `release_time` - Time constant for gain decrease /// * `absolute_max_gain` - Maximum allowable gain +#[inline] pub fn automatic_gain_control( input: I, target_level: f32,