This commit is contained in:
Theron 2019-12-19 18:35:14 -06:00
parent 444636f621
commit 1e11c22493
3 changed files with 17 additions and 6 deletions

View File

@ -81,7 +81,7 @@ impl Apu {
}
}
pub fn step(&mut self) -> Option<f32> {
pub fn clock(&mut self) -> Option<f32> {
let mut sample = None;
if (self.frame_counter == 4 && FRAME_COUNTER_STEPS[..4].contains(&self.cycle))

View File

@ -3,7 +3,7 @@ extern crate sdl2;
use sdl2::audio::{AudioCallback, AudioSpecDesired};
pub struct Speaker {
buffer: Vec<f32>,
buffer: [f32; 4096*4],
head: usize,
}
@ -13,7 +13,14 @@ impl AudioCallback for Speaker {
for (i, x) in out.iter_mut().enumerate() {
*x = self.buffer[i+self.head]; // get data from apu
}
self.buffer = self.buffer[4096..].to_vec();
self.head = (self.head + 4096) % (4096*4)
}
}
impl Speaker {
pub fn append(&mut self, sample: f32) {
self.buffer[self.head] = sample;
self.head = (self.head + 1) % (4096*4);
}
}
@ -31,6 +38,6 @@ pub fn initialize(context: &sdl2::Sdl) -> Result<sdl2::audio::AudioDevice<Speake
println!("{:?}", spec);
// initialize the audio callback
Speaker{buffer: vec![], head: 0}
Speaker{buffer: [0_f32; 4096*4], head: 0}
})
}

View File

@ -35,8 +35,9 @@ fn main() -> Result<(), String> {
let mut screen_buffer = vec![0; byte_width * byte_height]; // contains raw RGB data for the screen
// Set up audio
let mut speaker = audio::initialize(&sdl_context).expect("Could not create audio device");
let mut audio_device = audio::initialize(&sdl_context).expect("Could not create audio device");
let mut half_cycle = false;
audio_device.resume();
// Initialize hardware components
let cart = Cartridge::new();
@ -64,7 +65,10 @@ fn main() -> Result<(), String> {
}
}
for _ in 0..apu_cycles {
cpu.apu.step();
match cpu.apu.clock() {
Some(sample) => audio_device.speaker.append(sample),
None => (),
}
}
// clock PPU three times for every CPU cycle
for _ in 0..cpu_cycles * 3 {