From e80b961eca8a1ded25b2c774a2089284029ecb09 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Tue, 7 Mar 2023 09:53:56 +1100 Subject: [PATCH] vst makes audio!! --- gb-vst/src/lib.rs | 22 ++++---------------- lib/src/lib.rs | 5 +---- lib/src/processor/memory/mmio/apu.rs | 31 +++++++++++++++++++++------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/gb-vst/src/lib.rs b/gb-vst/src/lib.rs index 3ed8cda..d64f412 100644 --- a/gb-vst/src/lib.rs +++ b/gb-vst/src/lib.rs @@ -70,34 +70,20 @@ impl Plugin for GameboyEmu { _: &mut AuxiliaryBuffers, _: &mut impl ProcessContext, ) -> ProcessStatus { - nih_log!("processing"); if let Some(ref mut vars) = self.vars { - vars.emulator_core.run_until_buffer_full(); if buffer.channels() != 2 { panic!() } - let mut got_any = false; - let mut got_nonzero = false; for sample in buffer.iter_samples() { + if vars.rx.is_empty() { + vars.emulator_core.run_until_buffer_full(); + } if let Some(a) = executor::block_on(vars.rx.pop()) { - got_any = true; - if (a[0] != 0.) || (a[1] != 0.) { - got_nonzero = true; - } for (source, dest) in a.iter().zip(sample) { *dest = *source; } } } - if got_any { - if got_nonzero { - nih_log!("got some real data"); - } else { - nih_log!("got some 0's"); - } - } - } else { - nih_log!("vars not initialised"); } ProcessStatus::KeepAlive } @@ -118,7 +104,7 @@ impl Plugin for GameboyEmu { no_save: true, bootrom_path: None, connect_serial: false, - verbose: true, + verbose: false, cycle_count: false, }; let (sender, receiver) = channel::(); diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 580f739..743d5fe 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -154,11 +154,8 @@ impl EmulatorCore { } pub fn run_until_buffer_full(&mut self) { - println!("running until buffer full!!"); while !self.cpu.memory.is_audio_buffer_full() { - loop { - self.run(); - } + self.run(); } } diff --git a/lib/src/processor/memory/mmio/apu.rs b/lib/src/processor/memory/mmio/apu.rs index a089c95..83a8b86 100644 --- a/lib/src/processor/memory/mmio/apu.rs +++ b/lib/src/processor/memory/mmio/apu.rs @@ -42,6 +42,7 @@ pub struct Apu { mixer: Mixer, div_apu: u8, buffer: Vec, + out_buffer: Vec<[f32; 2]>, converter: Downsampler, output: AudioOutput, } @@ -57,6 +58,7 @@ impl Apu { mixer: Mixer::default(), div_apu: 0, buffer: vec![], + out_buffer: vec![], converter: Downsampler::new(output.sample_rate), output, } @@ -101,18 +103,33 @@ impl Apu { ); if self.buffer.len() >= CYCLES_PER_FRAME { self.next_audio(); + } else if !self.out_buffer.is_empty() { + self.push_audio(); } } fn next_audio(&mut self) { - let converted = self.converter.process( - self.buffer - .drain(..) - .map(|v| v.mixed(&self.mixer)) - .collect::>(), + self.out_buffer.append( + &mut self.converter.process( + self.buffer + .drain(..) + .map(|v| v.mixed(&self.mixer)) + .collect::>(), + ), ); - println!("pushing {} samples to queue", converted.len()); - executor::block_on(self.output.send_rb.push_slice(&converted)).unwrap(); + + self.push_audio(); + } + + fn push_audio(&mut self) { + let length = self.out_buffer.len().min(self.output.send_rb.free_len()); + + executor::block_on( + self.output + .send_rb + .push_slice(&self.out_buffer.drain(..length).collect::>()), + ) + .unwrap(); } pub fn is_buffer_full(&self) -> bool {