| 1 | use std::collections::VecDeque; |
| 2 | use std::sync::{Arc, Mutex}; |
| 3 | |
| 4 | use crate::audio::{FRAME_SAMPLES, SAMPLE_RATE}; |
| 5 | |
| 6 | const MAX_QUEUED_SAMPLES: usize = SAMPLE_RATE as usize / 5; |
| 7 | |
| 8 | #[derive(Clone, Default)] |
| 9 | pub struct SampleQueue { |
| 10 | samples: Arc<Mutex<VecDeque<i16>>>, |
| 11 | } |
| 12 | |
| 13 | impl SampleQueue { |
| 14 | pub fn push(&self, samples: &[i16]) { |
| 15 | let mut queued = self |
| 16 | .samples |
| 17 | .lock() |
| 18 | .unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 19 | queued.extend(samples.iter().copied()); |
| 20 | let excess = queued.len().saturating_sub(MAX_QUEUED_SAMPLES); |
| 21 | queued.drain(..excess); |
| 22 | } |
| 23 | |
| 24 | /// Returns a full 10 ms frame when enough samples are queued. Partial frames stay queued |
| 25 | /// so AEC and the encoder never see zero-padded speech. |
| 26 | pub fn next_frame(&self) -> Option<Vec<i16>> { |
| 27 | let mut queued = self |
| 28 | .samples |
| 29 | .lock() |
| 30 | .unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 31 | if queued.len() < FRAME_SAMPLES { |
| 32 | return None; |
| 33 | } |
| 34 | Some(queued.drain(..FRAME_SAMPLES).collect()) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// Downmixes interleaved floats to mono and converts them to 48 kHz with linear interpolation. |
| 39 | pub struct MonoResampler { |
| 40 | step: f64, |
| 41 | position: f64, |
| 42 | previous: f32, |
| 43 | } |
| 44 | |
| 45 | impl MonoResampler { |
| 46 | pub fn new(source_rate: u32) -> Self { |
| 47 | MonoResampler { |
| 48 | step: f64::from(source_rate) / f64::from(SAMPLE_RATE), |
| 49 | position: 0.0, |
| 50 | previous: 0.0, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn convert(&mut self, interleaved: &[f32], channels: usize, output: &mut Vec<i16>) { |
| 55 | let channels = channels.max(1); |
| 56 | for frame in interleaved.chunks_exact(channels) { |
| 57 | let current = frame.iter().sum::<f32>() / channels as f32; |
| 58 | while self.position < 1.0 { |
| 59 | let value = self.previous + (current - self.previous) * self.position as f32; |
| 60 | output.push((value.clamp(-1.0, 1.0) * f32::from(i16::MAX)) as i16); |
| 61 | self.position += self.step; |
| 62 | } |
| 63 | self.position -= 1.0; |
| 64 | self.previous = current; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn i16_to_f32(samples: &[i16]) -> Vec<f32> { |
| 70 | samples |
| 71 | .iter() |
| 72 | .map(|&sample| f32::from(sample) / f32::from(i16::MAX)) |
| 73 | .collect() |
| 74 | } |
| 75 | |
| 76 | #[cfg(test)] |
| 77 | mod tests { |
| 78 | use super::*; |
| 79 | |
| 80 | #[test] |
| 81 | fn partial_frames_stay_queued_until_a_full_frame_is_ready() { |
| 82 | let queue = SampleQueue::default(); |
| 83 | queue.push(&[1, 2, 3]); |
| 84 | assert!(queue.next_frame().is_none()); |
| 85 | queue.push(&vec![4; FRAME_SAMPLES - 3]); |
| 86 | let frame = queue.next_frame().expect("full frame"); |
| 87 | assert_eq!(frame.len(), FRAME_SAMPLES); |
| 88 | assert_eq!(&frame[..4], &[1, 2, 3, 4]); |
| 89 | assert!(queue.next_frame().is_none()); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn the_queue_drops_the_oldest_samples_when_the_reader_stalls() { |
| 94 | let queue = SampleQueue::default(); |
| 95 | queue.push(&vec![1; MAX_QUEUED_SAMPLES]); |
| 96 | queue.push(&vec![2; FRAME_SAMPLES]); |
| 97 | let frame = queue.next_frame().expect("full frame"); |
| 98 | assert_eq!(frame[0], 1); |
| 99 | let mut last = frame; |
| 100 | for _ in 1..MAX_QUEUED_SAMPLES / FRAME_SAMPLES { |
| 101 | last = queue.next_frame().expect("full frame"); |
| 102 | } |
| 103 | assert_eq!(last[FRAME_SAMPLES - 1], 2); |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn stereo_at_24_khz_becomes_mono_at_48_khz() { |
| 108 | let mut resampler = MonoResampler::new(24_000); |
| 109 | let mut output = Vec::new(); |
| 110 | resampler.convert(&[0.5, 0.5].repeat(240), 2, &mut output); |
| 111 | assert_eq!(output.len(), 480); |
| 112 | assert!( |
| 113 | output[10..] |
| 114 | .iter() |
| 115 | .all(|&sample| (sample - (0.5 * f32::from(i16::MAX)) as i16).abs() < 2) |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn the_native_rate_passes_through() { |
| 121 | let mut resampler = MonoResampler::new(48_000); |
| 122 | let mut output = Vec::new(); |
| 123 | resampler.convert(&[0.25; 480], 1, &mut output); |
| 124 | assert_eq!(output.len(), 480); |
| 125 | } |
| 126 | } |