Implement CHIP-8 Beep

This commit is contained in:
Rekai Musuka
2020-07-15 15:00:50 -05:00
parent f10ad7f8dd
commit a6f1dd0f11
3 changed files with 336 additions and 57 deletions

View File

@@ -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)]