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};
pub struct Speaker {
buffer: [f32; 4096*4],
head: usize,
}
// pub struct Speaker {
// buffer: [f32; 4096*4],
// head: usize,
// }
impl AudioCallback for Speaker {
type Channel = f32;
fn callback(&mut self, out: &mut [f32]) {
for (i, x) in out.iter_mut().enumerate() {
*x = self.buffer[i+self.head]; // get data from apu
}
self.head = (self.head + 4096) % (4096*4)
}
}
// impl AudioCallback for Speaker {
// type Channel = f32;
// fn callback(&mut self, out: &mut [f32]) {
// for (i, x) in out.iter_mut().enumerate() {
// *x = self.buffer[i+self.head]; // get data from apu
// }
// 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);
}
}
// impl Speaker {
// pub fn append(&mut self, sample: f32) {
// self.buffer[self.head] = sample;
// 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 desired_spec = AudioSpecDesired {
@ -33,11 +33,7 @@ pub fn initialize(context: &sdl2::Sdl) -> Result<sdl2::audio::AudioDevice<Speake
samples: Some(4096), // default sample size
};
audio_subsystem.open_playback(None, &desired_spec, |spec| {
// Show obtained AudioSpec
println!("{:?}", spec);
audio_subsystem.open_queue(None, &desired_spec)
}
// initialize the audio callback
Speaker{buffer: [0_f32; 4096*4], head: 0}
})
}
// 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

View File

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