use std::time::Duration; use rand::Rng; use rodio::{ source::{SineWave, Zero}, OutputStream, Sink, Source, }; fn main() { let (_stream, stream_handle) = OutputStream::try_default().unwrap(); let sink = Sink::try_new(&stream_handle).unwrap(); loop { let mut rng = rand::thread_rng(); let freq = rng.gen_range((440 / 4)..(440 * 4)); let wave = SineWave::new(freq); let zero: Zero = Zero::new(1, 48_000); let length = rng.gen_range(0.1..0.3); let source = wave .take_crossfade_with(zero, Duration::from_secs_f32(length)) .amplify(0.20); sink.append(source); println!("8{}D", "=".repeat((length * 50.0) as usize)); // The sound plays in a separate thread. This call will block the current thread until the sink // has finished playing all its queued sounds. sink.sleep_until_end(); } }