This commit is contained in:
Theron Spiegl 2019-12-20 00:44:25 -06:00
parent 1e11c22493
commit 69dda36534
2 changed files with 26 additions and 30 deletions

View File

@ -2,29 +2,29 @@ extern crate sdl2;
use sdl2::audio::{AudioCallback, AudioSpecDesired}; use sdl2::audio::{AudioCallback, AudioSpecDesired};
pub struct Speaker { // pub struct Speaker {
buffer: [f32; 4096*4], // buffer: [f32; 4096*4],
head: usize, // head: usize,
} // }
impl AudioCallback for Speaker { // impl AudioCallback for Speaker {
type Channel = f32; // type Channel = f32;
fn callback(&mut self, out: &mut [f32]) { // fn callback(&mut self, out: &mut [f32]) {
for (i, x) in out.iter_mut().enumerate() { // for (i, x) in out.iter_mut().enumerate() {
*x = self.buffer[i+self.head]; // get data from apu // *x = self.buffer[i+self.head]; // get data from apu
} // }
self.head = (self.head + 4096) % (4096*4) // self.head = (self.head + 4096) % (4096*4)
} // }
} // }
impl Speaker { // impl Speaker {
pub fn append(&mut self, sample: f32) { // pub fn append(&mut self, sample: f32) {
self.buffer[self.head] = sample; // self.buffer[self.head] = sample;
self.head = (self.head + 1) % (4096*4); // self.head = (self.head + 1) % (4096*4);
} // }
} // }
pub fn initialize(context: &sdl2::Sdl) -> Result<sdl2::audio::AudioDevice<Speaker>, String> { pub fn initialize(context: &sdl2::Sdl) -> Result<sdl2::audio::AudioQueue<f32>, String> {
let audio_subsystem = context.audio()?; let audio_subsystem = context.audio()?;
let desired_spec = AudioSpecDesired { let desired_spec = AudioSpecDesired {
@ -33,11 +33,7 @@ pub fn initialize(context: &sdl2::Sdl) -> Result<sdl2::audio::AudioDevice<Speake
samples: Some(4096), // default sample size samples: Some(4096), // default sample size
}; };
audio_subsystem.open_playback(None, &desired_spec, |spec| { audio_subsystem.open_queue(None, &desired_spec)
// Show obtained AudioSpec }
println!("{:?}", spec);
// initialize the audio callback // problem is: how to get data into callback from outside? can't change its signature. can't sneak it in through struct as struct is consumed by the audio device
Speaker{buffer: [0_f32; 4096*4], head: 0}
})
}

View File

@ -66,9 +66,9 @@ fn main() -> Result<(), String> {
} }
for _ in 0..apu_cycles { for _ in 0..apu_cycles {
match cpu.apu.clock() { match cpu.apu.clock() {
Some(sample) => audio_device.speaker.append(sample), Some(sample) => audio_device.queue(&wav),
None => (), None => false,
} };
} }
// clock PPU three times for every CPU cycle // clock PPU three times for every CPU cycle
for _ in 0..cpu_cycles * 3 { for _ in 0..cpu_cycles * 3 {