1+ /// A iterator that stores extracted data in memory while allowing
2+ /// concurrent reading in real time.
13use std:: mem;
24use std:: sync:: { Arc , Mutex } ;
35use std:: time:: Duration ;
46
57use super :: SeekError ;
68use crate :: common:: { ChannelCount , SampleRate } ;
9+ use crate :: math:: PrevMultipleOf ;
710use crate :: Source ;
811
9- /// Iterator that at the same time extracts data from the iterator and stores it in a buffer.
12+ /// Iterator that at the same time extracts data from the iterator and
13+ /// stores it in a buffer.
1014pub struct Buffered < I >
1115where
1216 I : Source ,
1317{
1418 /// Immutable reference to the next span of data. Cannot be `Span::Input`.
1519 current_span : Arc < Span < I > > ,
1620
21+ parameters_changed : bool ,
22+
1723 /// The position in number of samples of this iterator inside `current_span`.
1824 position_in_span : usize ,
1925
@@ -30,6 +36,7 @@ impl<I: Source> Buffered<I> {
3036 current_span : first_span,
3137 position_in_span : 0 ,
3238 total_duration,
39+ parameters_changed : false ,
3340 }
3441 }
3542
@@ -46,6 +53,7 @@ impl<I: Source> Buffered<I> {
4653 Span :: End => next_span_ptr. clone ( ) ,
4754 Span :: Input ( input) => {
4855 let input = input. lock ( ) . unwrap ( ) . take ( ) . unwrap ( ) ;
56+ dbg ! ( ) ;
4957 extract ( input)
5058 }
5159 } ;
7078 let current_sample;
7179 let advance_span;
7280
73- match & * self . current_span {
81+ match dbg ! ( & * self . current_span) {
7482 Span :: Data ( SpanData { data, .. } ) => {
7583 current_sample = Some ( data[ self . position_in_span ] ) ;
7684 self . position_in_span += 1 ;
@@ -86,39 +94,31 @@ where
8694 } ;
8795
8896 if advance_span {
97+ dbg ! ( ) ;
98+ self . parameters_changed = true ;
8999 self . next_span ( ) ;
100+ } else {
101+ self . parameters_changed = false ;
90102 }
91103
92104 current_sample
93105 }
94-
95- #[ inline]
96- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
97- // TODO:
98- ( 0 , None )
99- }
100106}
101107
102- // TODO: implement exactsize iterator when size_hint is done
103-
104108impl < I > Source for Buffered < I >
105109where
106110 I : Source ,
107111{
108112 #[ inline]
109113 fn parameters_changed ( & self ) -> bool {
110- match & * self . current_span {
111- Span :: Data ( _) => false ,
112- Span :: End => true ,
113- Span :: Input ( _) => unreachable ! ( ) ,
114- }
114+ self . parameters_changed
115115 }
116116
117117 #[ inline]
118118 fn channels ( & self ) -> ChannelCount {
119119 match * self . current_span {
120120 Span :: Data ( SpanData { channels, .. } ) => channels,
121- Span :: End => 1 ,
121+ Span :: End => 0 ,
122122 Span :: Input ( _) => unreachable ! ( ) ,
123123 }
124124 }
@@ -127,7 +127,7 @@ where
127127 fn sample_rate ( & self ) -> SampleRate {
128128 match * self . current_span {
129129 Span :: Data ( SpanData { rate, .. } ) => rate,
130- Span :: End => 44100 ,
130+ Span :: End => 0 ,
131131 Span :: Input ( _) => unreachable ! ( ) ,
132132 }
133133 }
@@ -157,6 +157,7 @@ where
157157 current_span : self . current_span . clone ( ) ,
158158 position_in_span : self . position_in_span ,
159159 total_duration : self . total_duration ,
160+ parameters_changed : self . parameters_changed ,
160161 }
161162 }
162163}
@@ -165,8 +166,8 @@ enum Span<I>
165166where
166167 I : Source ,
167168{
168- /// Data that has already been extracted from the iterator. Also contains a pointer to the
169- /// next span.
169+ /// Data that has already been extracted from the iterator.
170+ /// Also contains a pointer to the next span.
170171 Data ( SpanData < I > ) ,
171172
172173 /// No more data.
@@ -177,6 +178,16 @@ where
177178 Input ( Mutex < Option < I > > ) ,
178179}
179180
181+ impl < I : Source > std:: fmt:: Debug for Span < I > {
182+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
183+ match self {
184+ Span :: Data ( _) => f. write_str ( "Span::Data" ) ,
185+ Span :: End => f. write_str ( "Span::End" ) ,
186+ Span :: Input ( _) => f. write_str ( "Span::Input" ) ,
187+ }
188+ }
189+ }
190+
180191struct SpanData < I >
181192where
182193 I : Source ,
@@ -221,26 +232,23 @@ fn extract<I>(mut input: I) -> Arc<Span<I>>
221232where
222233 I : Source ,
223234{
224- if input. parameters_changed ( ) {
225- return Arc :: new ( Span :: End ) ;
226- }
227-
228235 let channels = input. channels ( ) ;
229236 let rate = input. sample_rate ( ) ;
230237
231238 let mut data = Vec :: new ( ) ;
232239 loop {
233- let Some ( element) = input. next ( ) else { break } ;
234- data. push ( element) ;
240+ let Some ( sample) = input. next ( ) else { break } ;
241+ data. push ( sample) ;
242+ dbg ! ( sample) ;
235243 if input. parameters_changed ( ) {
236244 break ;
237245 }
238- if data. len ( ) > 32768 {
246+ if data. len ( ) > 32768 . prev_multiple_of ( channels ) {
239247 break ;
240248 }
241249 }
242250
243- if data. is_empty ( ) {
251+ if dbg ! ( data. is_empty( ) ) {
244252 return Arc :: new ( Span :: End ) ;
245253 }
246254
0 commit comments