chore: Reimplement Timer::beep()
This commit is contained in:
parent
7b6413f0cf
commit
cf85feb70c
24
src/timer.rs
24
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue