Skip to content

Commit 377fbb9

Browse files
committed
refactor: rename and rework noise generators API, update tests
- Rename noise generators to PascalCase types (e.g. WhiteUniform, Pink) - Move all noise generators under `source::noise` module - Deprecate `white()` and `pink()` in favor of `noise::WhiteUniform::new()` and `noise::Pink::new()` - Remove legacy noise generator functions/types from prelude - Update `rstest`, `rstest_reuse` to fix templates in unit tests - Refactor and expand noise generator documentation and examples - Move and consolidate noise generator tests into `src/source/noise.rs` - Update `examples/noise_generator.rs` to use new API and add descriptions
1 parent b90ad08 commit 377fbb9

File tree

7 files changed

+642
-757
lines changed

7 files changed

+642
-757
lines changed

CHANGELOG.md

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

3939
### Changed
4040
- Breaking: `OutputStreamBuilder` should now be used to initialize an audio output stream.
@@ -57,7 +57,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5757
- Breaking: As optional features are now available: CAF and MKV containers, MP1/MP2 and ADPCM
5858
decoders. Previously, the ADPCM decoder was enabled when `symphonia-wav` was.
5959
- docs.rs will now document all features, including those that are not enabled by default.
60-
- `PinkNoise` is not deterministically seekable, so will now return `Err` when seeking.
60+
- Breaking: `WhiteNoise` and `PinkNoise` have been renamed to `noise::WhiteUniform` and
61+
`noise::Pink`.
62+
- `noise::Pink` is not deterministically seekable, so will now return `Err` when seeking.
6163

6264
### Fixed
6365
- `ChannelVolume` no longer clips/overflows when converting from many channels to
@@ -71,11 +73,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7173
- `LoopedDecoder::size_hint` now correctly indicates an infinite stream.
7274
- Symphonia decoder `total_duration` for Vorbis now return the correct value (#696).
7375
- Symphonia decoder for MP4 now seeks correctly (#577).
74-
- White noise was not correctly uniformly distributed
75-
- Pink noise was not correctly distributed on sampling rates other than 44100 Hz
76+
- White noise was not correctly uniformly distributed.
77+
- Pink noise was not correctly distributed on sampling rates other than 44100 Hz.
7678

7779
### Deprecated
78-
- Deprecated `Sample::zero_value()` function in favor of `Sample::ZERO_VALUE` constant
80+
- Deprecated `Sample::zero_value()` function in favor of `Sample::ZERO_VALUE` constant.
81+
- Deprecated `white()` and `pink()` methods in favor of `noise::WhiteUniform::new()` and `noise::Pink::new()`.
7982

8083
### Removed
8184
- Breaking: Removed `Mp4Type` enum in favor of using MIME type string "audio/mp4" for MP4 format detection with `Decoder::new_mp4` (#612).

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ num-rational = "0.4.2"
107107

108108
[dev-dependencies]
109109
quickcheck = "1"
110-
rstest = "0.18.2"
111-
rstest_reuse = "0.6.0"
110+
rstest = "0.24"
111+
rstest_reuse = "0.7"
112112
approx = "0.5.1"
113113
dasp_sample = "0.11.0"
114114
divan = "0.1.14"

examples/noise_generator.rs

Lines changed: 70 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,89 @@
1-
//! Noise generator example. Use the "noise" feature to enable the noise generator sources.
1+
//! Noise generator example demonstrating practical applications like dithering.
2+
//! Use the "noise" feature to enable the noise generator sources.
23
3-
use std::error::Error;
4+
use std::{error::Error, thread::sleep, time::Duration};
45

5-
fn main() -> Result<(), Box<dyn Error>> {
6-
use rodio::source::{
7-
blue, brownian, gaussian_white, pink, triangular_white, velvet, violet, white, Source,
8-
};
9-
use std::thread;
10-
use std::time::Duration;
6+
use rodio::source::{
7+
noise::{Blue, Brownian, Pink, Velvet, Violet, WhiteGaussian, WhiteTriangular, WhiteUniform},
8+
Source,
9+
};
1110

11+
fn main() -> Result<(), Box<dyn Error>> {
1212
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
13+
let sample_rate = stream_handle.config().sample_rate();
1314

14-
let noise_duration = Duration::from_millis(1000);
15-
let interval_duration = Duration::from_millis(1500);
16-
17-
stream_handle
18-
.mixer()
19-
.add(white(48000).amplify(0.1).take_duration(noise_duration));
20-
println!("Playing white noise");
21-
22-
thread::sleep(interval_duration);
23-
24-
stream_handle.mixer().add(
25-
gaussian_white(48000)
26-
.amplify(0.1)
27-
.take_duration(noise_duration),
15+
play_noise(
16+
&stream_handle,
17+
WhiteUniform::new(sample_rate),
18+
"White Uniform",
19+
"Testing equipment linearly, masking sounds",
2820
);
29-
println!("Playing Gaussian white noise");
3021

31-
thread::sleep(interval_duration);
32-
33-
stream_handle.mixer().add(
34-
triangular_white(48000)
35-
.amplify(0.1)
36-
.take_duration(noise_duration),
22+
play_noise(
23+
&stream_handle,
24+
WhiteGaussian::new(sample_rate),
25+
"White Gaussian",
26+
"Scientific modeling, natural processes",
3727
);
38-
println!("Playing triangular white noise");
39-
40-
thread::sleep(interval_duration);
4128

42-
stream_handle
43-
.mixer()
44-
.add(pink(48000).amplify(0.1).take_duration(noise_duration));
45-
println!("Playing pink noise");
46-
47-
thread::sleep(interval_duration);
29+
play_noise(
30+
&stream_handle,
31+
WhiteTriangular::new(sample_rate),
32+
"White Triangular",
33+
"High-quality audio dithering (TPDF)",
34+
);
4835

49-
stream_handle
50-
.mixer()
51-
.add(blue(48000).amplify(0.1).take_duration(noise_duration));
52-
println!("Playing blue noise");
36+
play_noise(
37+
&stream_handle,
38+
Pink::new(sample_rate),
39+
"Pink",
40+
"Speaker testing, pleasant background sounds",
41+
);
5342

54-
thread::sleep(interval_duration);
43+
play_noise(
44+
&stream_handle,
45+
Blue::new(sample_rate),
46+
"Blue",
47+
"High-frequency emphasis, bright effects",
48+
);
5549

56-
stream_handle
57-
.mixer()
58-
.add(violet(48000).amplify(0.1).take_duration(noise_duration));
59-
println!("Playing violet noise");
50+
play_noise(
51+
&stream_handle,
52+
Violet::new(sample_rate),
53+
"Violet",
54+
"Very bright, sharp, high-frequency testing",
55+
);
6056

61-
thread::sleep(interval_duration);
57+
play_noise(
58+
&stream_handle,
59+
Brownian::new(sample_rate),
60+
"Brownian",
61+
"Muffled/distant effects, deep rumbles",
62+
);
6263

63-
stream_handle
64-
.mixer()
65-
.add(brownian(48000).amplify(0.1).take_duration(noise_duration));
66-
println!("Playing brownian noise");
64+
play_noise(
65+
&stream_handle,
66+
Velvet::new(sample_rate),
67+
"Velvet",
68+
"Sparse impulse generation for audio processing",
69+
);
6770

68-
thread::sleep(interval_duration);
71+
Ok(())
72+
}
6973

70-
stream_handle
71-
.mixer()
72-
.add(velvet(48000).amplify(0.1).take_duration(noise_duration));
73-
println!("Playing velvet noise");
74+
/// Helper function to play a noise type with description
75+
fn play_noise<S>(stream_handle: &rodio::OutputStream, source: S, name: &str, description: &str)
76+
where
77+
S: Source<Item = f32> + Send + 'static,
78+
{
79+
println!("{} Noise", name);
80+
println!(" Application: {}", description);
7481

75-
thread::sleep(interval_duration);
82+
stream_handle.mixer().add(
83+
source
84+
.amplify(0.12)
85+
.take_duration(Duration::from_millis(1500)),
86+
);
7687

77-
Ok(())
88+
sleep(Duration::from_millis(2000));
7889
}

src/source/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,9 @@ mod uniform;
8585
mod zero;
8686

8787
#[cfg(feature = "noise")]
88-
mod noise;
88+
pub mod noise;
8989
#[cfg(feature = "noise")]
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-
};
90+
pub use self::noise::{Pink, WhiteUniform};
9591

9692
/// A source of samples.
9793
///

0 commit comments

Comments
 (0)