From cf85feb70c4ea92e15ac53d401fc3ddee8f392d6 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 8 Oct 2020 19:04:37 -0500 Subject: [PATCH] chore: Reimplement Timer::beep() --- src/timer.rs | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/timer.rs b/src/timer.rs index aff2dab..41f5528 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -1,4 +1,4 @@ -use rodio::{source::SineWave, Sink}; +use rodio::{source::SineWave, Sink, Source}; use std::sync::atomic::{AtomicU8, Ordering}; use std::thread; use std::time::{Duration, Instant}; @@ -68,23 +68,13 @@ impl Timer { } fn beep() { - let maybe_device = rodio::default_output_device(); - let beep = SineWave::new(440); + const BEEP_FREQ: u32 = 440; // Hz (Middle A) + 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) = maybe_device { - 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; - } - } - }); + if let Some(device) = rodio::default_output_device() { + rodio::play_raw(&device, beep); } } }