chore: Reimplement Timer::beep()

This commit is contained in:
Rekai Nyangadzayi Musuka 2020-10-08 19:04:37 -05:00
parent 7b6413f0cf
commit cf85feb70c
1 changed files with 7 additions and 17 deletions

View File

@ -1,4 +1,4 @@
use rodio::{source::SineWave, Sink}; use rodio::{source::SineWave, Sink, Source};
use std::sync::atomic::{AtomicU8, Ordering}; use std::sync::atomic::{AtomicU8, Ordering};
use std::thread; use std::thread;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -68,23 +68,13 @@ impl Timer {
} }
fn beep() { fn beep() {
let maybe_device = rodio::default_output_device(); const BEEP_FREQ: u32 = 440; // Hz (Middle A)
let beep = SineWave::new(440); const BEEP_LENGTH: u64 = 150; // ms
let beep = SineWave::new(BEEP_FREQ)
.take_duration(Duration::from_millis(BEEP_LENGTH));
// TODO: Fix Pop when sound ends? if let Some(device) = rodio::default_output_device() {
if let Some(device) = maybe_device { rodio::play_raw(&device, beep);
std::thread::spawn(move || {
let sink = Sink::new(&device);
let duration = Duration::from_millis(100);
let start = Instant::now();
sink.append(beep);
loop {
if Instant::now().duration_since(start) > duration {
break;
}
}
});
} }
} }
} }