chore(snd): Make AudioReceiver and AudioSender structs generic

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-07-14 22:34:57 -05:00
parent 1e27875dc3
commit 2b5926e533
3 changed files with 18 additions and 15 deletions

View File

@ -66,7 +66,7 @@ impl Bus {
self.cartridge.as_ref()?.title() 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<f32>) {
self.snd.set_audio_src(sender) self.snd.set_audio_src(sender)
} }

View File

@ -45,7 +45,7 @@ impl Cpu {
}) })
} }
pub fn set_audio_src(&mut self, sender: AudioSender) { pub fn set_audio_src(&mut self, sender: AudioSender<f32>) {
self.bus.pass_audio_src(sender) self.bus.pass_audio_src(sender)
} }

View File

@ -27,7 +27,7 @@ pub(crate) struct Sound {
frame_seq_state: FrameSequencerState, frame_seq_state: FrameSequencerState,
div_prev: Option<u8>, div_prev: Option<u8>,
sender: Option<AudioSender>, sender: Option<AudioSender<f32>>,
sample_counter: u64, sample_counter: u64,
is_mpsc_full: bool, 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<f32>) {
self.sender = Some(sender); self.sender = Some(sender);
} }
@ -1175,20 +1175,23 @@ impl From<ChannelControl> for u8 {
pub struct AudioMPSC; pub struct AudioMPSC;
impl AudioMPSC { impl AudioMPSC {
pub fn new() -> (AudioSender, AudioReceiver) { pub fn new() -> (AudioSender<f32>, AudioReceiver<f32>) {
let (send, recv) = crossbeam_channel::bounded(AUDIO_BUFFER_LEN * 2); // 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 }) (AudioSender { inner: send }, AudioReceiver { inner: recv })
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct AudioSender { pub struct AudioSender<T> {
inner: Sender<f32>, inner: Sender<T>,
} }
impl AudioSender { impl<T> AudioSender<T> {
fn send_samples(&self, left: f32, right: f32) -> Result<(), TrySendError<f32>> { fn send_samples(&self, left: T, right: T) -> Result<(), TrySendError<T>> {
self.inner.try_send(left).and(self.inner.try_send(right))?; self.inner.try_send(left).and(self.inner.try_send(right))?;
Ok(()) Ok(())
} }
@ -1198,12 +1201,12 @@ impl AudioSender {
} }
} }
pub struct AudioReceiver { pub struct AudioReceiver<T> {
inner: Receiver<f32>, inner: Receiver<T>,
} }
impl Iterator for AudioReceiver { impl<T> Iterator for AudioReceiver<T> {
type Item = f32; type Item = T;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
// TODO: Should this never return none? // TODO: Should this never return none?
@ -1211,7 +1214,7 @@ impl Iterator for AudioReceiver {
} }
} }
impl Source for AudioReceiver { impl<T: rodio::Sample> Source for AudioReceiver<T> {
fn current_frame_len(&self) -> Option<usize> { fn current_frame_len(&self) -> Option<usize> {
// A frame changes when the samples rate or // A frame changes when the samples rate or
// number of channels change. This will never happen, so // number of channels change. This will never happen, so