vst makes audio!!

This commit is contained in:
Alex Janka 2023-03-07 09:53:56 +11:00
parent fbd3d51693
commit e80b961eca
3 changed files with 29 additions and 29 deletions

View file

@ -70,34 +70,20 @@ impl Plugin for GameboyEmu {
_: &mut AuxiliaryBuffers, _: &mut AuxiliaryBuffers,
_: &mut impl ProcessContext<Self>, _: &mut impl ProcessContext<Self>,
) -> ProcessStatus { ) -> ProcessStatus {
nih_log!("processing");
if let Some(ref mut vars) = self.vars { if let Some(ref mut vars) = self.vars {
vars.emulator_core.run_until_buffer_full();
if buffer.channels() != 2 { if buffer.channels() != 2 {
panic!() panic!()
} }
let mut got_any = false;
let mut got_nonzero = false;
for sample in buffer.iter_samples() { for sample in buffer.iter_samples() {
if let Some(a) = executor::block_on(vars.rx.pop()) { if vars.rx.is_empty() {
got_any = true; vars.emulator_core.run_until_buffer_full();
if (a[0] != 0.) || (a[1] != 0.) {
got_nonzero = true;
} }
if let Some(a) = executor::block_on(vars.rx.pop()) {
for (source, dest) in a.iter().zip(sample) { for (source, dest) in a.iter().zip(sample) {
*dest = *source; *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 ProcessStatus::KeepAlive
} }
@ -118,7 +104,7 @@ impl Plugin for GameboyEmu {
no_save: true, no_save: true,
bootrom_path: None, bootrom_path: None,
connect_serial: false, connect_serial: false,
verbose: true, verbose: false,
cycle_count: false, cycle_count: false,
}; };
let (sender, receiver) = channel::<EmulatorMessage>(); let (sender, receiver) = channel::<EmulatorMessage>();

View file

@ -154,13 +154,10 @@ impl EmulatorCore {
} }
pub fn run_until_buffer_full(&mut self) { pub fn run_until_buffer_full(&mut self) {
println!("running until buffer full!!");
while !self.cpu.memory.is_audio_buffer_full() { while !self.cpu.memory.is_audio_buffer_full() {
loop {
self.run(); self.run();
} }
} }
}
fn run_cycle(&mut self) { fn run_cycle(&mut self) {
let will_pause = unsafe { PAUSE_QUEUED }; let will_pause = unsafe { PAUSE_QUEUED };

View file

@ -42,6 +42,7 @@ pub struct Apu {
mixer: Mixer, mixer: Mixer,
div_apu: u8, div_apu: u8,
buffer: Vec<DacSample>, buffer: Vec<DacSample>,
out_buffer: Vec<[f32; 2]>,
converter: Downsampler, converter: Downsampler,
output: AudioOutput, output: AudioOutput,
} }
@ -57,6 +58,7 @@ impl Apu {
mixer: Mixer::default(), mixer: Mixer::default(),
div_apu: 0, div_apu: 0,
buffer: vec![], buffer: vec![],
out_buffer: vec![],
converter: Downsampler::new(output.sample_rate), converter: Downsampler::new(output.sample_rate),
output, output,
} }
@ -101,18 +103,33 @@ impl Apu {
); );
if self.buffer.len() >= CYCLES_PER_FRAME { if self.buffer.len() >= CYCLES_PER_FRAME {
self.next_audio(); self.next_audio();
} else if !self.out_buffer.is_empty() {
self.push_audio();
} }
} }
fn next_audio(&mut self) { fn next_audio(&mut self) {
let converted = self.converter.process( self.out_buffer.append(
&mut self.converter.process(
self.buffer self.buffer
.drain(..) .drain(..)
.map(|v| v.mixed(&self.mixer)) .map(|v| v.mixed(&self.mixer))
.collect::<Vec<[f32; 2]>>(), .collect::<Vec<[f32; 2]>>(),
),
); );
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::<Vec<[f32; 2]>>()),
)
.unwrap();
} }
pub fn is_buffer_full(&self) -> bool { pub fn is_buffer_full(&self) -> bool {