diff --git a/services/codec/src/backend/tlv320aic3100.rs b/services/codec/src/backend/tlv320aic3100.rs index d2ac212a2..9f85cb6dc 100644 --- a/services/codec/src/backend/tlv320aic3100.rs +++ b/services/codec/src/backend/tlv320aic3100.rs @@ -57,6 +57,8 @@ fn audio_handler(_irq_no: usize, arg: *mut usize) { } let rx_rdcount = codec.csr.rf(utra::audio::RX_STAT_RDCOUNT) as usize; let rx_wrcount = codec.csr.rf(utra::audio::RX_STAT_WRCOUNT) as usize; + + /* // this version saves on a useless initialization step if codec.rec_buffer.is_full() { codec.rec_frames_dropped += 1; let mut _dummy = 0; @@ -68,17 +70,18 @@ fn audio_handler(_irq_no: usize, arg: *mut usize) { codec.rec_buffer.rec_sample(unsafe{volatile_audio.read_volatile()}); } codec.rec_buffer.rec_advance(); - } + }*/ - /* let mut rec_buf: [u32; FIFO_DEPTH] = [ZERO_PCM as u32 | (ZERO_PCM as u32) << 16; FIFO_DEPTH]; for stereo_sample in rec_buf.iter_mut() { unsafe{ *stereo_sample = volatile_audio.read_volatile(); } } match codec.rec_buffer.nq_frame(rec_buf) { Ok(()) => {}, - Err(_buff) => codec.rec_frames_dropped += 1, - }*/ + Err(_buff) => { + codec.rec_frames_dropped += 1 + }, + } // if the buffer is low, let the audio handler know we used up another frame! if codec.play_buffer.readable_count() < 6 { @@ -141,14 +144,14 @@ impl Codec { codec } - pub fn trace(&mut self) { + fn trace(&mut self) { if self.tx_stat_errors > 0 || self.rx_stat_errors > 0 { log::trace!("drop p:{} r:{} | staterr tx:{} rx:{}", self.play_frames_dropped, self.rec_frames_dropped, self.tx_stat_errors, self.rx_stat_errors); self.tx_stat_errors = 0; self.rx_stat_errors = 0; } } - pub fn trace_rx(&self) { + fn trace_rx(&self) { log::trace!("T rd {} wr {}", self.csr.rf(utra::audio::RX_STAT_RDCOUNT), self.csr.rf(utra::audio::RX_STAT_WRCOUNT)); } @@ -462,6 +465,13 @@ impl Codec { self.csr.wfo(utra::audio::RX_CTL_ENABLE, 0); self.csr.wfo(utra::audio::TX_CTL_ENABLE, 0); + log::info!("playback stopped. frames dropped: p{} r{} / errors: tx{} rx{}", + self.play_frames_dropped, self.rec_frames_dropped, self.tx_stat_errors, self.rx_stat_errors); + self.play_frames_dropped = 0; + self.rec_frames_dropped = 0; + self.tx_stat_errors = 0; + self.rx_stat_errors = 0; + self.csr.wfo(utra::audio::RX_CTL_RESET, 1); self.csr.wfo(utra::audio::TX_CTL_RESET, 1); self.live = false; diff --git a/services/codec/src/main.rs b/services/codec/src/main.rs index 265e8eaf8..401965b0d 100644 --- a/services/codec/src/main.rs +++ b/services/codec/src/main.rs @@ -60,11 +60,8 @@ fn xmain() -> ! { ticktimer.sleep_ms(2).unwrap(); log::trace!("initializing codec"); codec.init(); - codec.trace_rx(); }), Some(api::Opcode::ResumeStream) => xous::msg_scalar_unpack!(msg, _, _, _, _, { - codec.trace_rx(); - codec.trace(); if codec.is_on() && codec.is_init() { codec.audio_i2s_start(); } else { @@ -72,8 +69,6 @@ fn xmain() -> ! { } }), Some(api::Opcode::PauseStream) => xous::msg_scalar_unpack!(msg, _, _, _, _, { - codec.trace_rx(); - codec.trace(); if codec.is_on() && codec.is_init() { codec.audio_i2s_stop(); } else { @@ -99,6 +94,18 @@ fn xmain() -> ! { loop { if let Some(frame) = framering.dq_frame() { + let mut printed = false; + while codec.free_play_frames() == 0 { + if !printed { + log::info!("swap overrun"); + printed = true; + } + xous::yield_slice(); + if !codec.is_live() { + // handle the case that play stopped while we're trying to run the swap + break; + } + } if codec.free_play_frames() > 0 { codec.nq_play_frame(frame).unwrap(); // throw away the result because we know this must succeed } else { @@ -133,7 +140,6 @@ fn xmain() -> ! { log::trace!("hook done, {:?}", audio_cb_conns); } Some(api::Opcode::AnotherFrame) => xous::msg_scalar_unpack!(msg, _rdcount, _wrcount, _, _, { - codec.trace(); //log::trace!("A rd {} wr {}", rdcount, wrcount); send_event(&audio_cb_conns, codec.free_play_frames(), codec.available_rec_frames()); }), diff --git a/services/shellchat/src/cmds/audio.rs b/services/shellchat/src/cmds/audio.rs index 31229355d..baa844d0b 100644 --- a/services/shellchat/src/cmds/audio.rs +++ b/services/shellchat/src/cmds/audio.rs @@ -161,7 +161,7 @@ impl<'a> ShellCmdApi<'a> for Audio { free_play }; self.framecount += frames_to_push as u32; - log::debug!("frame {}", self.framecount); + log::debug!("f{} p{}", self.framecount, frames_to_push); for _ in 0..frames_to_push { let mut frame: [u32; codec::FIFO_DEPTH] = [ZERO_PCM as u32 | (ZERO_PCM as u32) << 16; codec::FIFO_DEPTH]; for i in 0..codec::FIFO_DEPTH { @@ -177,9 +177,9 @@ impl<'a> ShellCmdApi<'a> for Audio { log::debug!("stopping playback"); if self.framecount != 0 { self.codec.pause().unwrap(); // this should stop callbacks from occurring too. + write!(ret, "Playback of {} frames finished", self.framecount).unwrap(); self.framecount = 0; self.play_ptr_bytes = 0; - write!(ret, "{}", "Playback finished").unwrap(); } else { // we will get extra callbacks as the pipe clears return Ok(None)