use std::cmp; use std::time::Duration; use cpal::FromSample; use crate::conversions::{ChannelCountConverter, DataConverter, SampleRateConverter}; use crate::{Sample, Source}; use super::SeekError; /// An iterator that reads from a `Source` and converts the samples to a specific rate and /// channels count. /// /// It implements `Source` as well, but all the data is guaranteed to be in a single frame whose /// channels and samples rate have been passed to `new`. #[derive(Clone)] pub struct UniformSourceIterator where I: Source, I::Item: Sample, D: Sample, { inner: Option>>, D>>, target_channels: u16, target_sample_rate: u32, total_duration: Option, } impl UniformSourceIterator where I: Source, I::Item: Sample, D: Sample, { #[inline] pub fn new( input: I, target_channels: u16, target_sample_rate: u32, ) -> UniformSourceIterator { let total_duration = input.total_duration(); let input = UniformSourceIterator::bootstrap(input, target_channels, target_sample_rate); UniformSourceIterator { inner: Some(input), target_channels, target_sample_rate, total_duration, } } #[inline] fn bootstrap( input: I, target_channels: u16, target_sample_rate: u32, ) -> DataConverter>>, D> { // Limit the frame length to something reasonable let frame_len = input.current_frame_len().map(|x| x.min(32768)); let from_channels = input.channels(); let from_sample_rate = input.sample_rate(); let input = Take { iter: input, n: frame_len, }; let input = SampleRateConverter::new( input, cpal::SampleRate(from_sample_rate), cpal::SampleRate(target_sample_rate), from_channels, ); let input = ChannelCountConverter::new(input, from_channels, target_channels); DataConverter::new(input) } } impl Iterator for UniformSourceIterator where I: Source, I::Item: Sample, D: FromSample + Sample, { type Item = D; #[inline] fn next(&mut self) -> Option { if let Some(value) = self.inner.as_mut().unwrap().next() { return Some(value); } let input = self .inner .take() .unwrap() .into_inner() .into_inner() .into_inner() .iter; let mut input = UniformSourceIterator::bootstrap(input, self.target_channels, self.target_sample_rate); let value = input.next(); self.inner = Some(input); value } #[inline] fn size_hint(&self) -> (usize, Option) { (self.inner.as_ref().unwrap().size_hint().0, None) } } impl Source for UniformSourceIterator where I: Iterator + Source, I::Item: Sample, D: FromSample + Sample, { #[inline] fn current_frame_len(&self) -> Option { None } #[inline] fn channels(&self) -> u16 { self.target_channels } #[inline] fn sample_rate(&self) -> u32 { self.target_sample_rate } #[inline] fn total_duration(&self) -> Option { self.total_duration } #[inline] fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> { if let Some(input) = self.inner.as_mut() { input .inner_mut() .inner_mut() .inner_mut() .inner_mut() .try_seek(pos) } else { Ok(()) } } } #[derive(Clone, Debug)] struct Take { iter: I, n: Option, } impl Take { #[inline] pub fn inner_mut(&mut self) -> &mut I { &mut self.iter } } impl Iterator for Take where I: Iterator, { type Item = ::Item; #[inline] fn next(&mut self) -> Option<::Item> { if let Some(n) = &mut self.n { if *n != 0 { *n -= 1; self.iter.next() } else { None } } else { self.iter.next() } } #[inline] fn size_hint(&self) -> (usize, Option) { if let Some(n) = self.n { let (lower, upper) = self.iter.size_hint(); let lower = cmp::min(lower, n); let upper = match upper { Some(x) if x < n => Some(x), _ => Some(n), }; (lower, upper) } else { self.iter.size_hint() } } } impl ExactSizeIterator for Take where I: ExactSizeIterator {}