From a3056713c8c49e702ef062379fb13c2689e6b97d Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Fri, 18 Nov 2022 19:32:52 +0100 Subject: [PATCH] Don't normalize empty buffers in Buffr Glitch --- plugins/buffr_glitch/src/buffer.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/plugins/buffr_glitch/src/buffer.rs b/plugins/buffr_glitch/src/buffer.rs index 80b62f37..f918cad6 100644 --- a/plugins/buffr_glitch/src/buffer.rs +++ b/plugins/buffr_glitch/src/buffer.rs @@ -128,12 +128,17 @@ impl RingBuffer { match normalization_mode { NormalizationMode::None => (), NormalizationMode::Auto => { - let normalization_factor = - calculate_rms(&self.recording_buffers) / calculate_rms(&self.playback_buffers); + // Prevent this from causing divisions by zero or making very loud clicks when audio + // playback has just started + let playback_rms = calculate_rms(&self.playback_buffers); + if playback_rms > 0.001 { + let recording_rms = calculate_rms(&self.recording_buffers); + let normalization_factor = recording_rms / playback_rms; - for buffer in self.playback_buffers.iter_mut() { - for sample in buffer.iter_mut() { - *sample *= normalization_factor; + for buffer in self.playback_buffers.iter_mut() { + for sample in buffer.iter_mut() { + *sample *= normalization_factor; + } } } }