|
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. |
2 | 3 |
|
3 |
| -use std::error::Error; |
| 4 | +use std::{error::Error, thread::sleep, time::Duration}; |
4 | 5 |
|
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 | +}; |
11 | 10 |
|
| 11 | +fn main() -> Result<(), Box<dyn Error>> { |
12 | 12 | let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
|
| 13 | + let sample_rate = stream_handle.config().sample_rate(); |
13 | 14 |
|
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", |
28 | 20 | );
|
29 |
| - println!("Playing Gaussian white noise"); |
30 | 21 |
|
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", |
37 | 27 | );
|
38 |
| - println!("Playing triangular white noise"); |
39 |
| - |
40 |
| - thread::sleep(interval_duration); |
41 | 28 |
|
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 | + ); |
48 | 35 |
|
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 | + ); |
53 | 42 |
|
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 | + ); |
55 | 49 |
|
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 | + ); |
60 | 56 |
|
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 | + ); |
62 | 63 |
|
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 | + ); |
67 | 70 |
|
68 |
| - thread::sleep(interval_duration); |
| 71 | + Ok(()) |
| 72 | +} |
69 | 73 |
|
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); |
74 | 81 |
|
75 |
| - thread::sleep(interval_duration); |
| 82 | + stream_handle.mixer().add( |
| 83 | + source |
| 84 | + .amplify(0.12) |
| 85 | + .take_duration(Duration::from_millis(1500)), |
| 86 | + ); |
76 | 87 |
|
77 |
| - Ok(()) |
| 88 | + sleep(Duration::from_millis(2000)); |
78 | 89 | }
|
0 commit comments