-
Notifications
You must be signed in to change notification settings - Fork 247
/
Copy pathsignal_generator.rs
71 lines (54 loc) · 1.99 KB
/
signal_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Test signal generator example.
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
use rodio::source::{chirp, Function, SignalGenerator, Source};
use std::thread;
use std::time::Duration;
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
let test_signal_duration = Duration::from_millis(1000);
let interval_duration = Duration::from_millis(1500);
let sample_rate = 48000;
println!("Playing 1000 Hz tone");
stream_handle.mixer().add(
SignalGenerator::new(sample_rate, 1000.0, Function::Sine)
.amplify(0.1)
.take_duration(test_signal_duration),
);
thread::sleep(interval_duration);
println!("Playing 10,000 Hz tone");
stream_handle.mixer().add(
SignalGenerator::new(sample_rate, 10000.0, Function::Sine)
.amplify(0.1)
.take_duration(test_signal_duration),
);
thread::sleep(interval_duration);
println!("Playing 440 Hz Triangle Wave");
stream_handle.mixer().add(
SignalGenerator::new(sample_rate, 440.0, Function::Triangle)
.amplify(0.1)
.take_duration(test_signal_duration),
);
thread::sleep(interval_duration);
println!("Playing 440 Hz Sawtooth Wave");
stream_handle.mixer().add(
SignalGenerator::new(sample_rate, 440.0, Function::Sawtooth)
.amplify(0.1)
.take_duration(test_signal_duration),
);
thread::sleep(interval_duration);
println!("Playing 440 Hz Square Wave");
stream_handle.mixer().add(
SignalGenerator::new(sample_rate, 440.0, Function::Square)
.amplify(0.1)
.take_duration(test_signal_duration),
);
thread::sleep(interval_duration);
println!("Playing 20-10000 Hz Sweep");
stream_handle.mixer().add(
chirp(sample_rate, 20.0, 10000.0, Duration::from_secs(1))
.amplify(0.1)
.take_duration(test_signal_duration),
);
thread::sleep(interval_duration);
Ok(())
}