From 4440e61d020c32bc2377144aae3b40796fe37a55 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 8 Mar 2022 19:02:02 +0800 Subject: [PATCH] fix short sample problems, add pause/abort API pause API waits till the buffer drains to stop playback abort causes playback to stop immediately, causing pending frames to go empty. --- services/codec/src/api.rs | 4 +++- services/codec/src/backend/tlv320aic3100.rs | 12 +++++++++++- services/codec/src/lib.rs | 20 +++++++++++++++++++- services/codec/src/main.rs | 11 +++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/services/codec/src/api.rs b/services/codec/src/api.rs index 36f3773d6..3cefec0f5 100644 --- a/services/codec/src/api.rs +++ b/services/codec/src/api.rs @@ -24,8 +24,10 @@ pub(crate) enum Opcode { /// Powers on the CODEC, sets up 8k stereo streaming; puts audio in "paused" state Setup8kStereo, - /// Pause the stream without powering anything off + /// Pause the stream without powering anything off. Will wait until the current playback frames in process are finished. PauseStream, + /// Pause the stream without powering anything off. Clears the buffer immediately, losing any frames in playback. + AbortStream, /// Resumes the stream withouth re-initializing anything ResumeStream, diff --git a/services/codec/src/backend/tlv320aic3100.rs b/services/codec/src/backend/tlv320aic3100.rs index a420f495f..5f98a6c85 100644 --- a/services/codec/src/backend/tlv320aic3100.rs +++ b/services/codec/src/backend/tlv320aic3100.rs @@ -27,6 +27,7 @@ pub struct Codec { initialized: bool, live: bool, conn: xous::CID, + drain: bool, } static SILENCE: [u32; FIFO_DEPTH] = [ZERO_PCM as u32 | (ZERO_PCM as u32) << 16; FIFO_DEPTH]; @@ -91,7 +92,7 @@ fn audio_handler(_irq_no: usize, arg: *mut usize) { } // if the buffer is low, let the audio handler know we used up another frame! - if codec.play_buffer.readable_count() < 6 { + if codec.play_buffer.readable_count() < 6 && !codec.drain { xous::try_send_message(codec.conn, xous::Message::new_scalar(Opcode::AnotherFrame.to_usize().unwrap(), rx_rdcount, rx_wrcount, 0, 0)).unwrap(); } @@ -136,6 +137,7 @@ impl Codec { conn, tx_stat_errors: 0, rx_stat_errors: 0, + drain: false, }; xous::claim_interrupt( @@ -202,6 +204,12 @@ impl Codec { pub fn free_play_frames(&self) -> usize { self.play_buffer.writeable_count() } + pub fn can_play(&self) -> bool { + !self.play_buffer.is_empty() + } + pub fn drain(&mut self) { + self.drain = true; + } pub fn available_rec_frames(&self) -> usize { self.rec_buffer.readable_count() } @@ -497,6 +505,7 @@ impl Codec { // this sets everything running self.csr.wfo(utra::audio::RX_CTL_ENABLE, 1); self.csr.wfo(utra::audio::TX_CTL_ENABLE, 1); + self.drain = false; self.live = true; } @@ -517,6 +526,7 @@ impl Codec { self.csr.wfo(utra::audio::RX_CTL_RESET, 1); self.csr.wfo(utra::audio::TX_CTL_RESET, 1); self.live = false; + self.drain = true; self.play_buffer.clear(); self.rec_buffer.clear(); } diff --git a/services/codec/src/lib.rs b/services/codec/src/lib.rs index 75850ef5a..28a7f429e 100644 --- a/services/codec/src/lib.rs +++ b/services/codec/src/lib.rs @@ -82,6 +82,11 @@ impl Codec { Message::new_scalar(Opcode::PauseStream.to_usize().unwrap(), 0, 0, 0, 0) ).map(|_| ()) } + pub fn abort(&mut self) -> Result<(), xous::Error> { + send_message(self.conn, + Message::new_scalar(Opcode::AbortStream.to_usize().unwrap(), 0, 0, 0, 0) + ).map(|_| ()) + } /// gain goes from 0dB down to -80dB pub fn set_speaker_volume(&self, op: VolumeOps, gain: Option) -> Result<(), xous::Error> { @@ -123,7 +128,20 @@ impl Codec { ) ).map(|_| ()) } - + pub fn is_running(&self) -> Result { + match send_message(self.conn, + Message::new_blocking_scalar(Opcode::IsLive.to_usize().unwrap(), 0, 0, 0, 0) + ) { + Ok(xous::Result::Scalar1(is_live)) => { + if is_live != 0 { + Ok(true) + } else { + Ok(false) + } + } + _ => Err(xous::Error::InternalError) + } + } } use core::sync::atomic::{AtomicU32, Ordering}; diff --git a/services/codec/src/main.rs b/services/codec/src/main.rs index e433fcaa5..dfbf6dd2e 100644 --- a/services/codec/src/main.rs +++ b/services/codec/src/main.rs @@ -80,11 +80,22 @@ fn xmain() -> ! { }), Some(api::Opcode::PauseStream) => xous::msg_scalar_unpack!(msg, _, _, _, _, { if codec.is_on() && codec.is_init() { + codec.drain(); // this will suppress any future callbacks from firing + while codec.can_play() { + xous::yield_slice(); + } codec.audio_i2s_stop(); } else { log::error!("attempted to pause a stream on an uninitialized codec, ignoring!") } }), + Some(api::Opcode::AbortStream) => xous::msg_scalar_unpack!(msg, _, _, _, _, { + if codec.is_on() && codec.is_init() { + codec.audio_i2s_stop(); + } else { + log::error!("attempted to abort a stream on an uninitialized codec, ignoring!") + } + }), Some(api::Opcode::IsLive) => xous::msg_blocking_scalar_unpack!(msg, _, _, _, _, { let ret = if codec.is_live() { 1