Implement CHIP-8 Beep
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
use rodio::{source::SineWave, Sink};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Display {
|
||||
pub buf: [u8; Self::WIDTH as usize * Self::HEIGHT as usize],
|
||||
@@ -85,9 +88,32 @@ impl Timer {
|
||||
|
||||
if self.remaining == 0 {
|
||||
self.enabled = false;
|
||||
println!("Beep!");
|
||||
Self::beep();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn beep() {
|
||||
let maybe_device = rodio::default_output_device();
|
||||
let beep = SineWave::new(440);
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
|
Reference in New Issue
Block a user