-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathnoise_generator.rs
42 lines (32 loc) · 1.16 KB
/
noise_generator.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! Noise generator example. Use the "noise" feature to enable the noise generator sources.
use rodio::constants::DEFAULT_SAMPLE_RATE;
use std::error::Error;
#[cfg(feature = "noise")]
fn main() -> Result<(), Box<dyn Error>> {
use rodio::source::{pink, white, Source};
use std::thread;
use std::time::Duration;
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
let noise_duration = Duration::from_millis(1000);
let interval_duration = Duration::from_millis(1500);
stream_handle.mixer().add(
white(cpal::SampleRate(DEFAULT_SAMPLE_RATE))
.amplify(0.1)
.take_duration(noise_duration),
);
println!("Playing white noise");
thread::sleep(interval_duration);
stream_handle.mixer().add(
pink(cpal::SampleRate(DEFAULT_SAMPLE_RATE))
.amplify(0.1)
.take_duration(noise_duration),
);
println!("Playing pink noise");
thread::sleep(interval_duration);
Ok(())
}
#[cfg(not(feature = "noise"))]
fn main() {
println!("rodio has not been compiled with noise sources, use `--features noise` to enable this feature.");
println!("Exiting...");
}