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.
This commit is contained in:
bunnie 2022-03-08 19:02:02 +08:00
parent 19f09b9ed3
commit 4440e61d02
4 changed files with 44 additions and 3 deletions

View file

@ -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,

View file

@ -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();
}

View file

@ -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<f32>) -> Result<(), xous::Error> {
@ -123,7 +128,20 @@ impl Codec {
)
).map(|_| ())
}
pub fn is_running(&self) -> Result<bool, xous::Error> {
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};

View file

@ -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