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
use std::time::Duration;
use Sample;
use Source;
pub fn from_iter<I>(iterator: I) -> FromIter<I::IntoIter>
where
I: IntoIterator,
{
let mut iterator = iterator.into_iter();
let first_source = iterator.next();
FromIter {
iterator: iterator,
current_source: first_source,
}
}
#[derive(Clone)]
pub struct FromIter<I>
where
I: Iterator,
{
iterator: I,
current_source: Option<I::Item>,
}
impl<I> Iterator for FromIter<I>
where
I: Iterator,
I::Item: Iterator + Source,
<I::Item as Iterator>::Item: Sample,
{
type Item = <I::Item as Iterator>::Item;
#[inline]
fn next(&mut self) -> Option<<I::Item as Iterator>::Item> {
loop {
if let Some(ref mut src) = self.current_source {
if let Some(value) = src.next() {
return Some(value);
}
}
if let Some(src) = self.iterator.next() {
self.current_source = Some(src);
} else {
return None;
}
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
if let Some(ref cur) = self.current_source {
(cur.size_hint().0, None)
} else {
(0, None)
}
}
}
impl<I> Source for FromIter<I>
where
I: Iterator,
I::Item: Iterator + Source,
<I::Item as Iterator>::Item: Sample,
{
#[inline]
fn current_frame_len(&self) -> Option<usize> {
const THRESHOLD: usize = 10240;
if let Some(ref src) = self.current_source {
if let Some(val) = src.current_frame_len() {
if val != 0 {
return Some(val);
}
}
}
if let Some(ref src) = self.current_source {
if let Some(val) = src.size_hint().1 {
if val < THRESHOLD && val != 0 {
return Some(val);
}
}
}
Some(THRESHOLD)
}
#[inline]
fn channels(&self) -> u16 {
if let Some(ref src) = self.current_source {
src.channels()
} else {
2
}
}
#[inline]
fn sample_rate(&self) -> u32 {
if let Some(ref src) = self.current_source {
src.sample_rate()
} else {
44100
}
}
#[inline]
fn total_duration(&self) -> Option<Duration> {
None
}
}
#[cfg(test)]
mod tests {
use buffer::SamplesBuffer;
use source::from_iter;
use source::Source;
#[test]
fn basic() {
let mut rx = from_iter((0 .. 2).map(|n| {
if n == 0 {
SamplesBuffer::new(1, 48000, vec![10i16, -10, 10, -10])
} else if n == 1 {
SamplesBuffer::new(2, 96000, vec![5i16, 5, 5, 5])
} else {
unreachable!()
}
}));
assert_eq!(rx.channels(), 1);
assert_eq!(rx.sample_rate(), 48000);
assert_eq!(rx.next(), Some(10));
assert_eq!(rx.next(), Some(-10));
assert_eq!(rx.next(), Some(10));
assert_eq!(rx.next(), Some(-10));
assert_eq!(rx.next(), Some(5));
assert_eq!(rx.next(), Some(5));
assert_eq!(rx.next(), Some(5));
assert_eq!(rx.next(), Some(5));
assert_eq!(rx.next(), None);
}
}