| 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.samples.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 16 | queued.extend(samples.iter().copied()); |
| 17 | let excess = queued.len().saturating_sub(MAX_QUEUED_SAMPLES); |
| 18 | queued.drain(..excess); |
| 19 | } |
| 20 | |
| 21 | pub fn next_frame(&self) -> (Vec<i16>, bool) { |
| 22 | let mut queued = self.samples.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 23 | let available = queued.len().min(FRAME_SAMPLES); |
| 24 | let mut frame: Vec<i16> = queued.drain(..available).collect(); |
| 25 | frame.resize(FRAME_SAMPLES, 0); |
| 26 | (frame, available > 0) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /// Downmixes interleaved floats to mono and converts them to 48 kHz with linear interpolation. |
| 31 | pub struct MonoResampler { |
| 32 | step: f64, |
| 33 | position: f64, |
| 34 | previous: f32, |
| 35 | } |
| 36 | |
| 37 | impl MonoResampler { |
| 38 | pub fn new(source_rate: u32) -> Self { |
| 39 | MonoResampler { |
| 40 | step: f64::from(source_rate) / f64::from(SAMPLE_RATE), |
| 41 | position: 0.0, |
| 42 | previous: 0.0, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub fn convert(&mut self, interleaved: &[f32], channels: usize, output: &mut Vec<i16>) { |
| 47 | let channels = channels.max(1); |
| 48 | for frame in interleaved.chunks_exact(channels) { |
| 49 | let current = frame.iter().sum::<f32>() / channels as f32; |
| 50 | while self.position < 1.0 { |
| 51 | let value = self.previous + (current - self.previous) * self.position as f32; |
| 52 | output.push((value.clamp(-1.0, 1.0) * f32::from(i16::MAX)) as i16); |
| 53 | self.position += self.step; |
| 54 | } |
| 55 | self.position -= 1.0; |
| 56 | self.previous = current; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | pub fn i16_to_f32(samples: &[i16]) -> Vec<f32> { |
| 62 | samples.iter().map(|&sample| f32::from(sample) / f32::from(i16::MAX)).collect() |
| 63 | } |
| 64 | |
| 65 | #[cfg(test)] |
| 66 | mod tests { |
| 67 | use super::*; |
| 68 | |
| 69 | #[test] |
| 70 | fn frames_come_out_in_order_and_pad_with_silence() { |
| 71 | let queue = SampleQueue::default(); |
| 72 | queue.push(&[1, 2, 3]); |
| 73 | let (frame, had_data) = queue.next_frame(); |
| 74 | assert!(had_data); |
| 75 | assert_eq!(frame.len(), FRAME_SAMPLES); |
| 76 | assert_eq!(&frame[..4], &[1, 2, 3, 0]); |
| 77 | assert!(!queue.next_frame().1); |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn the_queue_drops_the_oldest_samples_when_the_reader_stalls() { |
| 82 | let queue = SampleQueue::default(); |
| 83 | queue.push(&vec![1; MAX_QUEUED_SAMPLES]); |
| 84 | queue.push(&vec![2; FRAME_SAMPLES]); |
| 85 | let (frame, _) = queue.next_frame(); |
| 86 | assert_eq!(frame[0], 1); |
| 87 | let mut last = frame; |
| 88 | for _ in 1..MAX_QUEUED_SAMPLES / FRAME_SAMPLES { |
| 89 | last = queue.next_frame().0; |
| 90 | } |
| 91 | assert_eq!(last[FRAME_SAMPLES - 1], 2); |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn stereo_at_24_khz_becomes_mono_at_48_khz() { |
| 96 | let mut resampler = MonoResampler::new(24_000); |
| 97 | let mut output = Vec::new(); |
| 98 | resampler.convert(&[0.5, 0.5].repeat(240), 2, &mut output); |
| 99 | assert_eq!(output.len(), 480); |
| 100 | assert!(output[10..].iter().all(|&sample| (sample - (0.5 * f32::from(i16::MAX)) as i16).abs() < 2)); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn the_native_rate_passes_through() { |
| 105 | let mut resampler = MonoResampler::new(48_000); |
| 106 | let mut output = Vec::new(); |
| 107 | resampler.convert(&[0.25; 480], 1, &mut output); |
| 108 | assert_eq!(output.len(), 480); |
| 109 | } |
| 110 | } |