rodio/examples/signal_generator.rs

72 lines
2 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
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
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_handle = rodio::OutputStreamBuilder::try_default_stream()?;
2024-09-29 23:15:41 +00:00
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);
2024-11-10 16:23:18 +00:00
let sample_rate = cpal::SampleRate(48000);
2024-09-29 23:15:41 +00:00
println!("Playing 1000 Hz tone");
2024-11-10 16:23:18 +00:00
stream_handle.mixer().add(
SignalGenerator::new(sample_rate, 1000.0, Function::Sine)
.amplify(0.1)
.take_duration(test_signal_duration),
);
2024-09-29 23:15:41 +00:00
thread::sleep(interval_duration);
println!("Playing 10,000 Hz tone");
2024-11-10 16:23:18 +00:00
stream_handle.mixer().add(
SignalGenerator::new(sample_rate, 10000.0, Function::Sine)
.amplify(0.1)
.take_duration(test_signal_duration),
);
2024-09-29 23:15:41 +00:00
thread::sleep(interval_duration);
println!("Playing 440 Hz Triangle Wave");
2024-11-10 16:23:18 +00:00
stream_handle.mixer().add(
SignalGenerator::new(sample_rate, 440.0, Function::Triangle)
.amplify(0.1)
.take_duration(test_signal_duration),
);
2024-09-29 23:15:41 +00:00
thread::sleep(interval_duration);
println!("Playing 440 Hz Sawtooth Wave");
2024-11-10 16:23:18 +00:00
stream_handle.mixer().add(
SignalGenerator::new(sample_rate, 440.0, Function::Sawtooth)
.amplify(0.1)
.take_duration(test_signal_duration),
);
2024-09-29 23:15:41 +00:00
thread::sleep(interval_duration);
println!("Playing 440 Hz Square Wave");
2024-11-10 16:23:18 +00:00
stream_handle.mixer().add(
SignalGenerator::new(sample_rate, 440.0, Function::Square)
.amplify(0.1)
.take_duration(test_signal_duration),
);
2024-09-29 23:15:41 +00:00
thread::sleep(interval_duration);
println!("Playing 20-10000 Hz Sweep");
2024-11-10 16:23:18 +00:00
stream_handle.mixer().add(
2024-11-13 23:08:50 +00:00
chirp(sample_rate, 20.0, 10000.0, Duration::from_secs(1))
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-11-10 16:23:18 +00:00
);
2024-09-29 23:15:41 +00:00
thread::sleep(interval_duration);
Ok(())
2024-09-29 23:15:41 +00:00
}