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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use std::cmp;
use std::mem;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use crate::{Sample, Source};

use super::SeekError;

/// Internal function that builds a `Buffered` object.
#[inline]
pub fn buffered<I>(input: I) -> Buffered<I>
where
    I: Source,
    I::Item: Sample,
{
    let total_duration = input.total_duration();
    let first_frame = extract(input);

    Buffered {
        current_frame: first_frame,
        position_in_frame: 0,
        total_duration,
    }
}

/// Iterator that at the same time extracts data from the iterator and stores it in a buffer.
pub struct Buffered<I>
where
    I: Source,
    I::Item: Sample,
{
    /// Immutable reference to the next frame of data. Cannot be `Frame::Input`.
    current_frame: Arc<Frame<I>>,

    /// The position in number of samples of this iterator inside `current_frame`.
    position_in_frame: usize,

    /// Obtained once at creation and never modified again.
    total_duration: Option<Duration>,
}

enum Frame<I>
where
    I: Source,
    I::Item: Sample,
{
    /// Data that has already been extracted from the iterator. Also contains a pointer to the
    /// next frame.
    Data(FrameData<I>),

    /// No more data.
    End,

    /// Unextracted data. The `Option` should never be `None` and is only here for easier data
    /// processing.
    Input(Mutex<Option<I>>),
}

struct FrameData<I>
where
    I: Source,
    I::Item: Sample,
{
    data: Vec<I::Item>,
    channels: u16,
    rate: u32,
    next: Mutex<Arc<Frame<I>>>,
}

impl<I> Drop for FrameData<I>
where
    I: Source,
    I::Item: Sample,
{
    fn drop(&mut self) {
        // This is necessary to prevent stack overflows deallocating long chains of the mutually
        // recursive `Frame` and `FrameData` types. This iteratively traverses as much of the
        // chain as needs to be deallocated, and repeatedly "pops" the head off the list. This
        // solves the problem, as when the time comes to actually deallocate the `FrameData`,
        // the `next` field will contain a `Frame::End`, or an `Arc` with additional references,
        // so the depth of recursive drops will be bounded.
        while let Ok(arc_next) = self.next.get_mut() {
            if let Some(next_ref) = Arc::get_mut(arc_next) {
                // This allows us to own the next Frame.
                let next = mem::replace(next_ref, Frame::End);
                if let Frame::Data(next_data) = next {
                    // Swap the current FrameData with the next one, allowing the current one
                    // to go out of scope.
                    *self = next_data;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }
}

/// Builds a frame from the input iterator.
fn extract<I>(mut input: I) -> Arc<Frame<I>>
where
    I: Source,
    I::Item: Sample,
{
    let frame_len = input.current_frame_len();

    if frame_len == Some(0) {
        return Arc::new(Frame::End);
    }

    let channels = input.channels();
    let rate = input.sample_rate();
    let data: Vec<I::Item> = input
        .by_ref()
        .take(cmp::min(frame_len.unwrap_or(32768), 32768))
        .collect();

    if data.is_empty() {
        return Arc::new(Frame::End);
    }

    Arc::new(Frame::Data(FrameData {
        data,
        channels,
        rate,
        next: Mutex::new(Arc::new(Frame::Input(Mutex::new(Some(input))))),
    }))
}

impl<I> Buffered<I>
where
    I: Source,
    I::Item: Sample,
{
    /// Advances to the next frame.
    fn next_frame(&mut self) {
        let next_frame = {
            let mut next_frame_ptr = match &*self.current_frame {
                Frame::Data(FrameData { next, .. }) => next.lock().unwrap(),
                _ => unreachable!(),
            };

            let next_frame = match &**next_frame_ptr {
                Frame::Data(_) => next_frame_ptr.clone(),
                Frame::End => next_frame_ptr.clone(),
                Frame::Input(input) => {
                    let input = input.lock().unwrap().take().unwrap();
                    extract(input)
                }
            };

            *next_frame_ptr = next_frame.clone();
            next_frame
        };

        self.current_frame = next_frame;
        self.position_in_frame = 0;
    }
}

impl<I> Iterator for Buffered<I>
where
    I: Source,
    I::Item: Sample,
{
    type Item = I::Item;

    #[inline]
    fn next(&mut self) -> Option<I::Item> {
        let current_sample;
        let advance_frame;

        match &*self.current_frame {
            Frame::Data(FrameData { data, .. }) => {
                current_sample = Some(data[self.position_in_frame]);
                self.position_in_frame += 1;
                advance_frame = self.position_in_frame >= data.len();
            }

            Frame::End => {
                current_sample = None;
                advance_frame = false;
            }

            Frame::Input(_) => unreachable!(),
        };

        if advance_frame {
            self.next_frame();
        }

        current_sample
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        // TODO:
        (0, None)
    }
}

// TODO: uncomment when `size_hint` is fixed
/*impl<I> ExactSizeIterator for Amplify<I> where I: Source + ExactSizeIterator, I::Item: Sample {
}*/

impl<I> Source for Buffered<I>
where
    I: Source,
    I::Item: Sample,
{
    #[inline]
    fn current_frame_len(&self) -> Option<usize> {
        match &*self.current_frame {
            Frame::Data(FrameData { data, .. }) => Some(data.len() - self.position_in_frame),
            Frame::End => Some(0),
            Frame::Input(_) => unreachable!(),
        }
    }

    #[inline]
    fn channels(&self) -> u16 {
        match *self.current_frame {
            Frame::Data(FrameData { channels, .. }) => channels,
            Frame::End => 1,
            Frame::Input(_) => unreachable!(),
        }
    }

    #[inline]
    fn sample_rate(&self) -> u32 {
        match *self.current_frame {
            Frame::Data(FrameData { rate, .. }) => rate,
            Frame::End => 44100,
            Frame::Input(_) => unreachable!(),
        }
    }

    #[inline]
    fn total_duration(&self) -> Option<Duration> {
        self.total_duration
    }

    /// Can not support seek, in the end state we lose the underlying source
    /// which makes seeking back impossible.
    #[inline]
    fn try_seek(&mut self, _: Duration) -> Result<(), SeekError> {
        Err(SeekError::NotSupported {
            underlying_source: std::any::type_name::<Self>(),
        })
    }
}

impl<I> Clone for Buffered<I>
where
    I: Source,
    I::Item: Sample,
{
    #[inline]
    fn clone(&self) -> Buffered<I> {
        Buffered {
            current_frame: self.current_frame.clone(),
            position_in_frame: self.position_in_frame,
            total_duration: self.total_duration,
        }
    }
}