rodio/examples/signal_generator.rs

84 lines
2.3 KiB
Rust
Raw Normal View History

2024-09-29 23:45:29 +00:00
//! Test signal generator example.
2024-09-29 23:15:41 +00:00
fn main() {
2024-10-01 05:02:46 +00:00
use rodio::source::{chirp, Function, SignalGenerator, Source};
2024-09-29 23:15:41 +00:00
use std::thread;
use std::time::Duration;
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
2024-09-29 23:45:29 +00:00
let test_signal_duration = Duration::from_millis(1000);
2024-09-29 23:15:41 +00:00
let interval_duration = Duration::from_millis(1500);
println!("Playing 1000 Hz tone");
stream_handle
.play_raw(
SignalGenerator::new(cpal::SampleRate(48000), 1000.0, Function::Sine)
2024-09-29 23:15:41 +00:00
.amplify(0.1)
2024-09-29 23:45:29 +00:00
.take_duration(test_signal_duration),
2024-09-29 23:15:41 +00:00
)
.unwrap();
thread::sleep(interval_duration);
println!("Playing 10,000 Hz tone");
stream_handle
.play_raw(
SignalGenerator::new(cpal::SampleRate(48000), 10000.0, Function::Sine)
2024-09-29 23:15:41 +00:00
.amplify(0.1)
2024-09-29 23:45:29 +00:00
.take_duration(test_signal_duration),
2024-09-29 23:15:41 +00:00
)
.unwrap();
thread::sleep(interval_duration);
println!("Playing 440 Hz Triangle Wave");
stream_handle
.play_raw(
SignalGenerator::new(cpal::SampleRate(48000), 440.0, Function::Triangle)
2024-09-29 23:15:41 +00:00
.amplify(0.1)
2024-09-29 23:45:29 +00:00
.take_duration(test_signal_duration),
2024-09-29 23:15:41 +00:00
)
.unwrap();
thread::sleep(interval_duration);
println!("Playing 440 Hz Sawtooth Wave");
stream_handle
.play_raw(
SignalGenerator::new(cpal::SampleRate(48000), 440.0, Function::Sawtooth)
2024-09-29 23:15:41 +00:00
.amplify(0.1)
2024-09-29 23:45:29 +00:00
.take_duration(test_signal_duration),
2024-09-29 23:15:41 +00:00
)
.unwrap();
thread::sleep(interval_duration);
println!("Playing 440 Hz Square Wave");
stream_handle
.play_raw(
SignalGenerator::new(cpal::SampleRate(48000), 440.0, Function::Square)
2024-09-29 23:15:41 +00:00
.amplify(0.1)
2024-09-29 23:45:29 +00:00
.take_duration(test_signal_duration),
2024-09-29 23:15:41 +00:00
)
.unwrap();
thread::sleep(interval_duration);
println!("Playing 20-10000 Hz Sweep");
stream_handle
.play_raw(
chirp(
cpal::SampleRate(48000),
20.0,
10000.0,
Duration::from_secs(1),
)
.amplify(0.1)
2024-09-29 23:45:29 +00:00
.take_duration(test_signal_duration),
2024-09-29 23:15:41 +00:00
)
.unwrap();
thread::sleep(interval_duration);
}