From 78fcce82676bc64a7d523e369204bb7f79802357 Mon Sep 17 00:00:00 2001 From: Michiel Roos Date: Mon, 30 Sep 2024 12:45:28 +0200 Subject: [PATCH 1/2] Update noise.rs Add brown noise generator --- knyst/src/gen/noise.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/knyst/src/gen/noise.rs b/knyst/src/gen/noise.rs index bacab38..33ca368 100644 --- a/knyst/src/gen/noise.rs +++ b/knyst/src/gen/noise.rs @@ -6,7 +6,7 @@ use super::GenState; use crate as knyst; use crate::Sample; -/// Whate noise (fastrand RNG based on wyrand) +/// White noise (fastrand RNG based on wyrand) pub struct WhiteNoise { rng: fastrand::Rng, } @@ -98,3 +98,43 @@ impl PinkNoise { GenState::Continue } } + +/// Brown noise (also known as red noise) +/// +/// Brown noise is generated by integrating white noise. +/// This implementation uses a simple integration of white noise samples with a small step size, +/// and clamps the output to prevent it from exceeding the [-1.0, 1.0] range. +pub struct BrownNoise { + rng: fastrand::Rng, + last_output: Sample, +} + +#[impl_gen(range = normal)] +impl BrownNoise { + #[allow(missing_docs)] + pub fn new() -> Self { + let mut rng = fastrand::Rng::new(); + rng.seed(next_randomness_seed()); + Self { + rng, + last_output: 0.0, + } + } + + #[allow(missing_docs)] + pub fn process(&mut self, output: &mut [Sample]) -> GenState { + for out in output.iter_mut() { + let white = self.rng.f32() as Sample * 2.0 - 1.0; + // Adjust the coefficient to control the step size + self.last_output += white * 0.1; + // Clamp to [-1.0, 1.0] to prevent output from exceeding the range + if self.last_output > 1.0 { + self.last_output = 1.0; + } else if self.last_output < -1.0 { + self.last_output = -1.0; + } + *out = self.last_output; + } + GenState::Continue + } +} From 2c51f2664a08c566e4aed544d29b21f0c35b49cd Mon Sep 17 00:00:00 2001 From: Michiel Roos Date: Mon, 30 Sep 2024 21:53:28 +0200 Subject: [PATCH 2/2] Use more readable clamp code --- knyst/src/gen/noise.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/knyst/src/gen/noise.rs b/knyst/src/gen/noise.rs index 33ca368..2d94fa6 100644 --- a/knyst/src/gen/noise.rs +++ b/knyst/src/gen/noise.rs @@ -128,11 +128,7 @@ impl BrownNoise { // Adjust the coefficient to control the step size self.last_output += white * 0.1; // Clamp to [-1.0, 1.0] to prevent output from exceeding the range - if self.last_output > 1.0 { - self.last_output = 1.0; - } else if self.last_output < -1.0 { - self.last_output = -1.0; - } + self.last_output = self.last_output.clamp(-1.0, 1.0); *out = self.last_output; } GenState::Continue