Skip to content

Commit

Permalink
Some clippy and style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
iluvcapra committed Aug 11, 2024
1 parent b6c8a7e commit 1eabb21
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use self::from_factory::{from_factory, FromFactoryIter};
pub use self::from_iter::{from_iter, FromIter};
pub use self::linear_ramp::LinearGainRamp;
pub use self::mix::Mix;
pub use self::noise::{pink, white, PinkNoise, WhiteNoise};
pub use self::pausable::Pausable;
pub use self::periodic::PeriodicAccess;
pub use self::position::TrackPosition;
Expand Down
38 changes: 30 additions & 8 deletions src/source/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ use crate::Source;

use rand::prelude::*;

/// Create a new white noise source.
#[inline]
pub fn white(sample_rate: cpal::SampleRate) -> WhiteNoise {
WhiteNoise::new(sample_rate)
}

/// Create a new pink noise source.
#[inline]
pub fn pink(sample_rate: cpal::SampleRate) -> PinkNoise {
PinkNoise::new(sample_rate)
}

/// Generates an infinite stream of random samples in [=1.0, 1.0]
#[derive(Clone, Debug)]
pub struct WhiteNoise {
Expand Down Expand Up @@ -32,18 +44,22 @@ impl Iterator for WhiteNoise {
}

impl Source for WhiteNoise {
#[inline]
fn current_frame_len(&self) -> Option<usize> {
None
}

#[inline]
fn channels(&self) -> u16 {
1
}

#[inline]
fn sample_rate(&self) -> u32 {
self.sample_rate.0
}

#[inline]
fn total_duration(&self) -> Option<std::time::Duration> {
None
}
Expand All @@ -52,7 +68,7 @@ impl Source for WhiteNoise {
// https://www.musicdsp.org/en/latest/Filters/76-pink-noise-filter.html
//
/// Generate an infinite stream of pink noise samples in [-1.0, 1.0].
struct PinkNoise {
pub struct PinkNoise {
noise: WhiteNoise,
b: [f32; 7],
}
Expand All @@ -73,13 +89,19 @@ impl Iterator for PinkNoise {
let white = self.noise.next().unwrap();
self.b[0] = 0.99886 * self.b[0] + white * 0.0555179;
self.b[1] = 0.99332 * self.b[1] + white * 0.0750759;
self.b[2] = 0.96900 * self.b[2] + white * 0.1538520;
self.b[3] = 0.86650 * self.b[3] + white * 0.3104856;
self.b[4] = 0.55000 * self.b[4] + white * 0.5329522;
self.b[5] = -0.7616 * self.b[5] - white * 0.0168980;

let pink = self.b[0] + self.b[1] + self.b[2] + self.b[3] + self.b[4] + self.b[5] +
self.b[6] + white * 0.5362;
self.b[2] = 0.969 * self.b[2] + white * 0.153852;
self.b[3] = 0.8665 * self.b[3] + white * 0.3104856;
self.b[4] = 0.550 * self.b[4] + white * 0.5329522;
self.b[5] = -0.7616 * self.b[5] - white * 0.016898;

let pink = self.b[0]
+ self.b[1]
+ self.b[2]
+ self.b[3]
+ self.b[4]
+ self.b[5]
+ self.b[6]
+ white * 0.5362;

self.b[6] = white * 0.115926;

Expand Down

0 comments on commit 1eabb21

Please sign in to comment.