rodio/examples/basic.rs
2024-11-10 02:34:49 +04:00

47 lines
1.3 KiB
Rust

use rodio::source::SineWave;
use rodio::Source;
use std::io::BufReader;
use std::thread;
use std::time::Duration;
fn main() {
let stream_handle = rodio::OutputStreamBuilder::try_default_stream()
.expect("open default audio stream");
let mixer = stream_handle.mixer();
let beep1 = {
let file = std::fs::File::open("assets/beep.wav").unwrap();
let sink = rodio::play(&mixer, BufReader::new(file)).unwrap();
sink.set_volume(0.2);
sink
};
println!("Started beep1");
thread::sleep(Duration::from_millis(1500));
{
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 = {
let file = std::fs::File::open("assets/beep3.ogg").unwrap();
let sink = rodio::play(&mixer, BufReader::new(file)).unwrap();
sink.set_volume(0.2);
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");
thread::sleep(Duration::from_millis(1500));
}