rodio/examples/basic.rs

55 lines
1.4 KiB
Rust
Raw Normal View History

2024-11-10 02:34:49 +04:00
use rodio::source::SineWave;
use rodio::Source;
use std::error::Error;
2015-09-10 16:38:16 +02:00
use std::io::BufReader;
2016-10-04 16:36:11 +02:00
use std::thread;
use std::time::Duration;
2024-11-10 20:23:18 +04:00
#[cfg(feature = "tracing")]
use tracing;
2015-09-10 16:38:16 +02:00
fn main() -> Result<(), Box<dyn Error>> {
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
2024-11-10 00:10:36 +04:00
let mixer = stream_handle.mixer();
2024-11-10 02:34:49 +04:00
let beep1 = {
2024-11-10 20:23:18 +04:00
// Play a WAV file.
let file = std::fs::File::open("assets/beep.wav")?;
let sink = rodio::play(&mixer, BufReader::new(file))?;
2024-11-10 00:10:36 +04:00
sink.set_volume(0.2);
2024-11-10 02:34:49 +04:00
sink
};
println!("Started beep1");
thread::sleep(Duration::from_millis(1500));
2024-11-10 00:10:36 +04:00
{
2024-11-10 20:23:18 +04:00
// Generate sine wave.
2024-11-10 02:34:49 +04:00
let wave = SineWave::new(740.0)
.amplify(0.2)
.take_duration(Duration::from_secs(3));
mixer.add(wave);
}
println!("Started beep2");
thread::sleep(Duration::from_millis(1500));
let beep3 = {
2024-11-10 20:23:18 +04:00
// Play an OGG file.
let file = std::fs::File::open("assets/beep3.ogg")?;
let sink = rodio::play(&mixer, BufReader::new(file))?;
2024-11-10 00:10:36 +04:00
sink.set_volume(0.2);
2024-11-10 02:34:49 +04:00
sink
};
println!("Started beep3");
thread::sleep(Duration::from_millis(1500));
drop(beep1);
println!("Stopped beep1");
thread::sleep(Duration::from_millis(1500));
drop(beep3);
println!("Stopped beep3");
2024-11-10 00:10:36 +04:00
2024-11-10 02:34:49 +04:00
thread::sleep(Duration::from_millis(1500));
Ok(())
2015-07-22 12:14:11 +02:00
}