Skip to content

Commit d8df57a

Browse files
committed
Add noise generators and improve their distribution
The commit improves and expands the noise generation capabilities by adding new types and fixing distribution issues in existing ones. Key changes: - Add more generators: Gaussian, triangular, blue, brownian, violet, velvet - Fix white noise uniform distribution - Fix pink noise sampling rate issues - Make noise generators properly deterministic with seeds - Add comprehensive tests for noise generator properties - Improve documentation with detailed usage guidance This provides a complete suite of high-quality noise generators for audio synthesis, testing, and dithering applications.
1 parent 071db6d commit d8df57a

File tree

7 files changed

+1147
-95
lines changed

7 files changed

+1147
-95
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232
`OutputStreamConfig::buffer_size()` and `OutputStreamConfig::sample_format()` getters to access
3333
an `OutputStreamConfig`'s channel count, sample rate, buffer size and sample format values.
3434
- Added `Source::limit()` method for limiting the maximum amplitude of a source.
35+
- Added more noise generators: `gaussian_white`, `triangular_white`, `blue`, `brownian`, `violet`,
36+
and `velvet`.
3537

3638
### Changed
3739
- Breaking: `OutputStreamBuilder` should now be used to initialize an audio output stream.
@@ -48,6 +50,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4850
functions from `dasp_sample` crate. For example `DaspSample::from_sample(sample)`.
4951
- `OutputStreamConfig` is now public.
5052
- Update `cpal` to [0.16](https://github.com/RustAudio/cpal/blob/master/CHANGELOG.md#version-0160-2025-06-07).
53+
- docs.rs will now document all features, including those that are not enabled by default.
54+
- `PinkNoise` is not deterministically seekable, so will now return `Err` when seeking.
5155

5256
### Fixed
5357
- `ChannelVolume` no longer clips/overflows when converting from many channels to
@@ -61,6 +65,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6165
- `LoopedDecoder::size_hint` now correctly indicates an infinite stream.
6266
- Symphonia decoder `total_duration` for Vorbis now return the correct value (#696).
6367
- Symphonia decoder for MP4 now seeks correctly (#577).
68+
- White noise was not correctly uniformly distributed
69+
- Pink noise was not correctly distributed on sampling rates other than 44100 Hz
6470

6571
### Deprecated
6672
- Deprecated `Sample::zero_value()` function in favor of `Sample::ZERO_VALUE` constant

Cargo.lock

Lines changed: 24 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ minimp3_fixed = { version = "0.5.4", optional = true }
1919
symphonia = { version = "0.5.4", optional = true, default-features = false }
2020
crossbeam-channel = { version = "0.5.15", optional = true }
2121

22-
rand = { version = "0.9.0", features = [
23-
"small_rng",
24-
"os_rng",
25-
], optional = true }
22+
rand = { version = "0.9", features = ["small_rng"], optional = true }
23+
rand_distr = { version = "0.5", optional = true }
2624
tracing = { version = "0.1.40", optional = true }
2725

2826
atomic_float = { version = "1.1.0", optional = true }
@@ -40,7 +38,7 @@ wav = ["hound"]
4038
mp3 = ["symphonia-mp3"]
4139
minimp3 = ["dep:minimp3_fixed"]
4240

43-
noise = ["rand"]
41+
noise = ["rand", "rand_distr"]
4442

4543
wasm-bindgen = ["cpal/wasm-bindgen"]
4644
cpal-shared-stdcxx = ["cpal/oboe-shared-stdcxx"]
@@ -64,6 +62,9 @@ symphonia-wav = ["symphonia/wav", "symphonia/pcm", "symphonia/adpcm"]
6462
symphonia-alac = ["symphonia/isomp4", "symphonia/alac"]
6563
symphonia-aiff = ["symphonia/aiff", "symphonia/pcm"]
6664

65+
[package.metadata.docs.rs]
66+
all-features = true
67+
6768
[dev-dependencies]
6869
quickcheck = "1"
6970
rstest = "0.18.2"

examples/noise_generator.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
use std::error::Error;
44

55
fn main() -> Result<(), Box<dyn Error>> {
6-
use rodio::source::{pink, white, Source};
6+
use rodio::source::{
7+
blue, brownian, gaussian_white, pink, triangular_white, velvet, violet, white, Source,
8+
};
79
use std::thread;
810
use std::time::Duration;
911

@@ -19,12 +21,58 @@ fn main() -> Result<(), Box<dyn Error>> {
1921

2022
thread::sleep(interval_duration);
2123

24+
stream_handle.mixer().add(
25+
gaussian_white(48000)
26+
.amplify(0.1)
27+
.take_duration(noise_duration),
28+
);
29+
println!("Playing Gaussian white noise");
30+
31+
thread::sleep(interval_duration);
32+
33+
stream_handle.mixer().add(
34+
triangular_white(48000)
35+
.amplify(0.1)
36+
.take_duration(noise_duration),
37+
);
38+
println!("Playing triangular white noise");
39+
40+
thread::sleep(interval_duration);
41+
2242
stream_handle
2343
.mixer()
2444
.add(pink(48000).amplify(0.1).take_duration(noise_duration));
2545
println!("Playing pink noise");
2646

2747
thread::sleep(interval_duration);
2848

49+
stream_handle
50+
.mixer()
51+
.add(blue(48000).amplify(0.1).take_duration(noise_duration));
52+
println!("Playing blue noise");
53+
54+
thread::sleep(interval_duration);
55+
56+
stream_handle
57+
.mixer()
58+
.add(violet(48000).amplify(0.1).take_duration(noise_duration));
59+
println!("Playing violet noise");
60+
61+
thread::sleep(interval_duration);
62+
63+
stream_handle
64+
.mixer()
65+
.add(brownian(48000).amplify(0.1).take_duration(noise_duration));
66+
println!("Playing brownian noise");
67+
68+
thread::sleep(interval_duration);
69+
70+
stream_handle
71+
.mixer()
72+
.add(velvet(48000).amplify(0.1).take_duration(noise_duration));
73+
println!("Playing velvet noise");
74+
75+
thread::sleep(interval_duration);
76+
2977
Ok(())
3078
}

src/source/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ mod zero;
8787
#[cfg(feature = "noise")]
8888
mod noise;
8989
#[cfg(feature = "noise")]
90-
pub use self::noise::{pink, white, PinkNoise, WhiteNoise};
90+
pub use self::noise::{
91+
blue, brownian, gaussian_white, pink, triangular_white, velvet, violet, white, BlueNoise,
92+
BrownianNoise, GaussianWhiteNoise, NoiseGenerator, PinkNoise, TriangularWhiteNoise,
93+
VelvetNoise, VioletNoise, WhiteNoise,
94+
};
9195

9296
/// A source of samples.
9397
///

0 commit comments

Comments
 (0)