fix audio playback of 8Khz samples, further cut down on clones/copies

This commit is contained in:
2026-06-22 16:18:52 +02:00
parent 2d95d0c6aa
commit bcf37182fe
2 changed files with 42 additions and 13 deletions
+5 -7
View File
@@ -156,18 +156,16 @@ impl AudioSink {
}
fn process(&mut self, scope: &ProcessScope) -> Result<(), AudioError> {
let mut next_outbuf = match self.sample_src.try_recv() {
Ok(buf) => buf,
Err(tokio::sync::mpsc::error::TryRecvError::Empty) => return Ok(()),
Err(err) => return Err(err.into())
};
self.output_buf.append(&mut next_outbuf);
if let Ok(buf) = self.sample_src.try_recv() {
self.output_buf.extend(buf);
}
if self.port.connected_count()? > 0 && !self.output_buf.is_empty() {
let outbuf = self.port.as_mut_slice(scope);
let mut next_segment: Vec<f32> = self.output_buf.drain(0..(outbuf.len()).min(self.output_buf.len())).collect();
let mut next_segment: Vec<f32> = self.output_buf.drain(..(outbuf.len()).min(self.output_buf.len())).collect();
let underrun = outbuf.len() - next_segment.len();
if underrun > 0 {
log::warn!("Audio stream underrun: {} samples", underrun);
next_segment.extend(std::iter::repeat_n(0., underrun));
}