|  | 
|  | 1 | +use std::time::Duration; | 
|  | 2 | + | 
|  | 3 | +use crate::{Sample, Source}; | 
|  | 4 | + | 
|  | 5 | +use super::SeekError; | 
|  | 6 | + | 
|  | 7 | +/// Internal function that builds a `TrackPosition` object. | 
|  | 8 | +pub fn track_position<I>(source: I) -> TrackPosition<I> { | 
|  | 9 | +    TrackPosition { | 
|  | 10 | +        input: source, | 
|  | 11 | +        samples_counted: 0, | 
|  | 12 | +        offset_duration: 0.0, | 
|  | 13 | +        current_frame_sample_rate: 0, | 
|  | 14 | +        current_frame_channels: 0, | 
|  | 15 | +        current_frame_len: None, | 
|  | 16 | +    } | 
|  | 17 | +} | 
|  | 18 | + | 
|  | 19 | +#[derive(Debug)] | 
|  | 20 | +pub struct TrackPosition<I> { | 
|  | 21 | +    input: I, | 
|  | 22 | +    samples_counted: usize, | 
|  | 23 | +    offset_duration: f64, | 
|  | 24 | +    current_frame_sample_rate: u32, | 
|  | 25 | +    current_frame_channels: u16, | 
|  | 26 | +    current_frame_len: Option<usize>, | 
|  | 27 | +} | 
|  | 28 | + | 
|  | 29 | +impl<I> TrackPosition<I> { | 
|  | 30 | +    /// Returns a reference to the inner source. | 
|  | 31 | +    #[inline] | 
|  | 32 | +    pub fn inner(&self) -> &I { | 
|  | 33 | +        &self.input | 
|  | 34 | +    } | 
|  | 35 | + | 
|  | 36 | +    /// Returns a mutable reference to the inner source. | 
|  | 37 | +    #[inline] | 
|  | 38 | +    pub fn inner_mut(&mut self) -> &mut I { | 
|  | 39 | +        &mut self.input | 
|  | 40 | +    } | 
|  | 41 | + | 
|  | 42 | +    /// Returns the inner source. | 
|  | 43 | +    #[inline] | 
|  | 44 | +    pub fn into_inner(self) -> I { | 
|  | 45 | +        self.input | 
|  | 46 | +    } | 
|  | 47 | +} | 
|  | 48 | + | 
|  | 49 | +impl<I> TrackPosition<I> | 
|  | 50 | +where | 
|  | 51 | +    I: Source, | 
|  | 52 | +    I::Item: Sample, | 
|  | 53 | +{ | 
|  | 54 | +    /// Returns the position of the source. | 
|  | 55 | +    #[inline] | 
|  | 56 | +    pub fn get_pos(&self) -> f64 { | 
|  | 57 | +        self.samples_counted as f64 / self.input.sample_rate() as f64 / self.input.channels() as f64 | 
|  | 58 | +            + self.offset_duration | 
|  | 59 | +    } | 
|  | 60 | + | 
|  | 61 | +    #[inline] | 
|  | 62 | +    fn set_current_frame(&mut self) { | 
|  | 63 | +        self.current_frame_len = self.current_frame_len(); | 
|  | 64 | +        self.current_frame_sample_rate = self.sample_rate(); | 
|  | 65 | +        self.current_frame_channels = self.channels(); | 
|  | 66 | +    } | 
|  | 67 | +} | 
|  | 68 | + | 
|  | 69 | +impl<I> Iterator for TrackPosition<I> | 
|  | 70 | +where | 
|  | 71 | +    I: Source, | 
|  | 72 | +    I::Item: Sample, | 
|  | 73 | +{ | 
|  | 74 | +    type Item = I::Item; | 
|  | 75 | + | 
|  | 76 | +    #[inline] | 
|  | 77 | +    fn next(&mut self) -> Option<I::Item> { | 
|  | 78 | +        // This should only be executed once at the first call to next. | 
|  | 79 | +        if self.current_frame_len.is_none() { | 
|  | 80 | +            self.set_current_frame(); | 
|  | 81 | +        } | 
|  | 82 | + | 
|  | 83 | +        let item = self.input.next(); | 
|  | 84 | +        if item.is_some() { | 
|  | 85 | +            self.samples_counted += 1; | 
|  | 86 | + | 
|  | 87 | +            // At the end of a frame add the duration of this frame to | 
|  | 88 | +            // offset_duration and start collecting samples again. | 
|  | 89 | +            if Some(self.samples_counted) == self.current_frame_len() { | 
|  | 90 | +                self.offset_duration += self.samples_counted as f64 | 
|  | 91 | +                    / self.current_frame_sample_rate as f64 | 
|  | 92 | +                    / self.current_frame_channels as f64; | 
|  | 93 | + | 
|  | 94 | +                // Reset. | 
|  | 95 | +                self.samples_counted = 0; | 
|  | 96 | +                self.set_current_frame(); | 
|  | 97 | +            }; | 
|  | 98 | +        }; | 
|  | 99 | +        item | 
|  | 100 | +    } | 
|  | 101 | + | 
|  | 102 | +    #[inline] | 
|  | 103 | +    fn size_hint(&self) -> (usize, Option<usize>) { | 
|  | 104 | +        self.input.size_hint() | 
|  | 105 | +    } | 
|  | 106 | +} | 
|  | 107 | + | 
|  | 108 | +impl<I> Source for TrackPosition<I> | 
|  | 109 | +where | 
|  | 110 | +    I: Source, | 
|  | 111 | +    I::Item: Sample, | 
|  | 112 | +{ | 
|  | 113 | +    #[inline] | 
|  | 114 | +    fn current_frame_len(&self) -> Option<usize> { | 
|  | 115 | +        self.input.current_frame_len() | 
|  | 116 | +    } | 
|  | 117 | + | 
|  | 118 | +    #[inline] | 
|  | 119 | +    fn channels(&self) -> u16 { | 
|  | 120 | +        self.input.channels() | 
|  | 121 | +    } | 
|  | 122 | + | 
|  | 123 | +    #[inline] | 
|  | 124 | +    fn sample_rate(&self) -> u32 { | 
|  | 125 | +        self.input.sample_rate() | 
|  | 126 | +    } | 
|  | 127 | + | 
|  | 128 | +    #[inline] | 
|  | 129 | +    fn total_duration(&self) -> Option<Duration> { | 
|  | 130 | +        self.input.total_duration() | 
|  | 131 | +    } | 
|  | 132 | + | 
|  | 133 | +    #[inline] | 
|  | 134 | +    fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> { | 
|  | 135 | +        let result = self.input.try_seek(pos); | 
|  | 136 | +        if result.is_ok() { | 
|  | 137 | +            self.offset_duration = pos.as_secs_f64(); | 
|  | 138 | +            // This assumes that the seek implementation of the codec always | 
|  | 139 | +            // starts again at the beginning of a frame. Which is the case with | 
|  | 140 | +            // symphonia. | 
|  | 141 | +            self.samples_counted = 0; | 
|  | 142 | +        } | 
|  | 143 | +        result | 
|  | 144 | +    } | 
|  | 145 | +} | 
|  | 146 | + | 
|  | 147 | +#[cfg(test)] | 
|  | 148 | +mod tests { | 
|  | 149 | +    use std::time::Duration; | 
|  | 150 | + | 
|  | 151 | +    use crate::buffer::SamplesBuffer; | 
|  | 152 | +    use crate::source::Source; | 
|  | 153 | + | 
|  | 154 | +    #[test] | 
|  | 155 | +    fn test_position() { | 
|  | 156 | +        let inner = SamplesBuffer::new(1, 1, vec![10i16, -10, 10, -10, 20, -20]); | 
|  | 157 | +        let mut source = inner.track_position(); | 
|  | 158 | + | 
|  | 159 | +        assert_eq!(source.get_pos(), 0.0); | 
|  | 160 | +        source.next(); | 
|  | 161 | +        assert_eq!(source.get_pos(), 1.0); | 
|  | 162 | + | 
|  | 163 | +        source.next(); | 
|  | 164 | +        assert_eq!(source.get_pos(), 2.0); | 
|  | 165 | + | 
|  | 166 | +        assert_eq!(source.try_seek(Duration::new(1, 0)).is_ok(), true); | 
|  | 167 | +        assert_eq!(source.get_pos(), 1.0); | 
|  | 168 | +    } | 
|  | 169 | +} | 
0 commit comments