From 2b5926e533f15e80c773bfee84a8726aebbe735e Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Wed, 14 Jul 2021 22:34:57 -0500 Subject: [PATCH] chore(snd): Make AudioReceiver and AudioSender structs generic --- src/bus.rs | 2 +- src/cpu.rs | 2 +- src/sound.rs | 29 ++++++++++++++++------------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/bus.rs b/src/bus.rs index 4e64a1c..c99f11b 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -66,7 +66,7 @@ impl Bus { self.cartridge.as_ref()?.title() } - pub(crate) fn pass_audio_src(&mut self, sender: AudioSender) { + pub(crate) fn pass_audio_src(&mut self, sender: AudioSender) { self.snd.set_audio_src(sender) } diff --git a/src/cpu.rs b/src/cpu.rs index 2e0db76..4b9bafa 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -45,7 +45,7 @@ impl Cpu { }) } - pub fn set_audio_src(&mut self, sender: AudioSender) { + pub fn set_audio_src(&mut self, sender: AudioSender) { self.bus.pass_audio_src(sender) } diff --git a/src/sound.rs b/src/sound.rs index 6b973b6..87c812e 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -27,7 +27,7 @@ pub(crate) struct Sound { frame_seq_state: FrameSequencerState, div_prev: Option, - sender: Option, + sender: Option>, sample_counter: u64, is_mpsc_full: bool, @@ -104,7 +104,7 @@ impl Sound { } } - pub(crate) fn set_audio_src(&mut self, sender: AudioSender) { + pub(crate) fn set_audio_src(&mut self, sender: AudioSender) { self.sender = Some(sender); } @@ -1175,20 +1175,23 @@ impl From for u8 { pub struct AudioMPSC; impl AudioMPSC { - pub fn new() -> (AudioSender, AudioReceiver) { - let (send, recv) = crossbeam_channel::bounded(AUDIO_BUFFER_LEN * 2); + pub fn new() -> (AudioSender, AudioReceiver) { + // TODO: Can we provide an upper limit for this? + // The larger this channel is, the more lag there is between the Audio and + // Emulator + let (send, recv) = crossbeam_channel::unbounded(); (AudioSender { inner: send }, AudioReceiver { inner: recv }) } } #[derive(Debug, Clone)] -pub struct AudioSender { - inner: Sender, +pub struct AudioSender { + inner: Sender, } -impl AudioSender { - fn send_samples(&self, left: f32, right: f32) -> Result<(), TrySendError> { +impl AudioSender { + fn send_samples(&self, left: T, right: T) -> Result<(), TrySendError> { self.inner.try_send(left).and(self.inner.try_send(right))?; Ok(()) } @@ -1198,12 +1201,12 @@ impl AudioSender { } } -pub struct AudioReceiver { - inner: Receiver, +pub struct AudioReceiver { + inner: Receiver, } -impl Iterator for AudioReceiver { - type Item = f32; +impl Iterator for AudioReceiver { + type Item = T; fn next(&mut self) -> Option { // TODO: Should this never return none? @@ -1211,7 +1214,7 @@ impl Iterator for AudioReceiver { } } -impl Source for AudioReceiver { +impl Source for AudioReceiver { fn current_frame_len(&self) -> Option { // A frame changes when the samples rate or // number of channels change. This will never happen, so