diff --git a/gb-vst/src/lib.rs b/gb-vst/src/lib.rs index 5008c03..143c7e1 100644 --- a/gb-vst/src/lib.rs +++ b/gb-vst/src/lib.rs @@ -112,24 +112,32 @@ impl Plugin for GameboyEmu { verbose: false, cycle_count: false, }; - let (sender, receiver) = channel::(); - let (output, rx) = AudioOutput::new_unfilled(buffer_config.sample_rate, false); + if let Some(ref mut vars) = self.vars { + let (output, rx) = AudioOutput::new_unfilled(buffer_config.sample_rate, false); - let (renderer, frame_receiver, key_handler) = EmulatorRenderer::new(); + vars.emulator_core.replace_output(output); + vars.rx = rx; + } else { + let (sender, receiver) = channel::(); - *self.frame_receiver.lock().unwrap() = Some(frame_receiver); - *self.key_handler.lock().unwrap() = Some(key_handler); + let (output, rx) = AudioOutput::new_unfilled(buffer_config.sample_rate, false); - let mut emulator_core = - EmulatorCore::init(receiver, options, Box::new(renderer), output, None); - emulator_core.run_until_buffer_full(); + let (renderer, frame_receiver, key_handler) = EmulatorRenderer::new(); - self.vars = Some(EmuVars { - rx, - sender, - emulator_core, - }); + *self.frame_receiver.lock().unwrap() = Some(frame_receiver); + *self.key_handler.lock().unwrap() = Some(key_handler); + + let mut emulator_core = + EmulatorCore::init(receiver, options, Box::new(renderer), output, None); + emulator_core.run_until_buffer_full(); + + self.vars = Some(EmuVars { + rx, + sender, + emulator_core, + }); + } true } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 56012c4..ce21e06 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -124,6 +124,10 @@ impl + Clone> EmulatorCore { } } + pub fn replace_output(&mut self, new: AudioOutput) { + self.cpu.memory.replace_output(new); + } + pub fn run(&mut self) { self.process_messages(); self.cycle_num += 1; diff --git a/lib/src/processor/memory.rs b/lib/src/processor/memory.rs index 2e8ff56..f8e6331 100644 --- a/lib/src/processor/memory.rs +++ b/lib/src/processor/memory.rs @@ -226,6 +226,10 @@ impl + Clone> Memory { pub fn is_audio_buffer_full(&self) -> bool { self.apu.is_buffer_full() } + + pub fn replace_output(&mut self, new: AudioOutput) { + self.apu.replace_output(new); + } } impl + Clone> Cpu { diff --git a/lib/src/processor/memory/mmio/apu.rs b/lib/src/processor/memory/mmio/apu.rs index 12a4608..7749ebf 100644 --- a/lib/src/processor/memory/mmio/apu.rs +++ b/lib/src/processor/memory/mmio/apu.rs @@ -64,6 +64,11 @@ impl Apu { } } + pub fn replace_output(&mut self, new: AudioOutput) { + self.converter = Downsampler::new(new.sample_rate); + self.output = new; + } + pub fn div_apu_tick(&mut self) { self.div_apu = self.div_apu.wrapping_add(1); if self.div_apu % 8 == 0 {