1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::backend::display::est_num_frames_to_str;
use crate::backend::units::frames_to_samples;
use crate::backend::Signal;
use crate::backend::Source;

/// [`Source::prepend_zeros()`]
pub struct PrependZeros<S: Source> {
    iter: S,
    num_samples_remaining: usize,
}

impl<S: Source> std::fmt::Debug for PrependZeros<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "PrependZeros {{ {} frames,  {} channels,  {} hz,  {} }}",
            est_num_frames_to_str(self.num_frames_estimate()),
            self.num_channels(),
            self.frame_rate_hz(),
            self.duration_estimate_to_str(),
        )
    }
}

impl<S: Source> PrependZeros<S> {
    #[inline]
    pub fn new(iter: S, num_frames: usize) -> Self {
        let num_samples_remaining = frames_to_samples(num_frames, iter.num_channels());
        Self {
            iter,
            num_samples_remaining,
        }
    }
}

impl<S: Source> Source for PrependZeros<S> {}

impl<S: Source> Signal for PrependZeros<S> {
    #[inline]
    fn frame_rate_hz(&self) -> u32 {
        self.iter.frame_rate_hz()
    }

    #[inline]
    fn num_channels(&self) -> u16 {
        self.iter.num_channels()
    }

    #[inline]
    fn num_frames_estimate(&self) -> Option<usize> {
        self.iter.num_frames_estimate()
    }
}

impl<S: Source> Iterator for PrependZeros<S> {
    type Item = f32;

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let (lower, upper) = self.iter.size_hint();
        let lower = lower + self.num_samples_remaining;
        let upper = upper.map(|u| u + self.num_samples_remaining);
        (lower, upper)
    }

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.num_samples_remaining > 0 {
            self.num_samples_remaining -= 1;
            return Some(0.0_f32);
        }
        self.iter.next()
    }
}