1111 from : ChannelCount ,
1212 to : ChannelCount ,
1313 sample_repeat : Option < Sample > ,
14- next_output_sample_pos : ChannelCount ,
14+ next_output_sample_pos : u16 ,
1515}
1616
1717impl < I > ChannelCountConverter < I >
2626 ///
2727 #[ inline]
2828 pub fn new ( input : I , from : ChannelCount , to : ChannelCount ) -> ChannelCountConverter < I > {
29- assert ! ( from >= 1 ) ;
30- assert ! ( to >= 1 ) ;
31-
3229 ChannelCountConverter {
3330 input,
3431 from,
6562 self . sample_repeat = value;
6663 value
6764 }
68- x if x < self . from => self . input . next ( ) ,
65+ x if x < self . from . get ( ) => self . input . next ( ) ,
6966 1 => self . sample_repeat ,
7067 _ => Some ( 0.0 ) ,
7168 } ;
@@ -74,11 +71,11 @@ where
7471 self . next_output_sample_pos += 1 ;
7572 }
7673
77- if self . next_output_sample_pos == self . to {
74+ if self . next_output_sample_pos == self . to . get ( ) {
7875 self . next_output_sample_pos = 0 ;
7976
8077 if self . from > self . to {
81- for _ in self . to .. self . from {
78+ for _ in self . to . get ( ) .. self . from . get ( ) {
8279 self . input . next ( ) ; // discarding extra input
8380 }
8481 }
9188 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
9289 let ( min, max) = self . input . size_hint ( ) ;
9390
94- let consumed = std:: cmp:: min ( self . from , self . next_output_sample_pos ) as usize ;
91+ let consumed = std:: cmp:: min ( self . from . get ( ) , self . next_output_sample_pos ) as usize ;
9592 let calculate = |size| {
96- ( size + consumed) / self . from as usize * self . to as usize
93+ ( size + consumed) / self . from . get ( ) as usize * self . to . get ( ) as usize
9794 - self . next_output_sample_pos as usize
9895 } ;
9996
@@ -110,38 +107,45 @@ impl<I> ExactSizeIterator for ChannelCountConverter<I> where I: ExactSizeIterato
110107mod test {
111108 use super :: ChannelCountConverter ;
112109 use crate :: common:: ChannelCount ;
110+ use crate :: math:: ch;
113111 use crate :: Sample ;
114112
115113 #[ test]
116114 fn remove_channels ( ) {
117115 let input = vec ! [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ] ;
118- let output = ChannelCountConverter :: new ( input. into_iter ( ) , 3 , 2 ) . collect :: < Vec < _ > > ( ) ;
116+ let output =
117+ ChannelCountConverter :: new ( input. into_iter ( ) , ch ! ( 3 ) , ch ! ( 2 ) ) . collect :: < Vec < _ > > ( ) ;
119118 assert_eq ! ( output, [ 1.0 , 2.0 , 4.0 , 5.0 ] ) ;
120119
121120 let input = vec ! [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 ] ;
122- let output = ChannelCountConverter :: new ( input. into_iter ( ) , 4 , 1 ) . collect :: < Vec < _ > > ( ) ;
121+ let output =
122+ ChannelCountConverter :: new ( input. into_iter ( ) , ch ! ( 4 ) , ch ! ( 1 ) ) . collect :: < Vec < _ > > ( ) ;
123123 assert_eq ! ( output, [ 1.0 , 5.0 ] ) ;
124124 }
125125
126126 #[ test]
127127 fn add_channels ( ) {
128128 let input = vec ! [ 1.0 , 2.0 , 3.0 , 4.0 ] ;
129- let output = ChannelCountConverter :: new ( input. into_iter ( ) , 1 , 2 ) . collect :: < Vec < _ > > ( ) ;
129+ let output =
130+ ChannelCountConverter :: new ( input. into_iter ( ) , ch ! ( 1 ) , ch ! ( 2 ) ) . collect :: < Vec < _ > > ( ) ;
130131 assert_eq ! ( output, [ 1.0 , 1.0 , 2.0 , 2.0 , 3.0 , 3.0 , 4.0 , 4.0 ] ) ;
131132
132133 let input = vec ! [ 1.0 , 2.0 ] ;
133- let output = ChannelCountConverter :: new ( input. into_iter ( ) , 1 , 4 ) . collect :: < Vec < _ > > ( ) ;
134+ let output =
135+ ChannelCountConverter :: new ( input. into_iter ( ) , ch ! ( 1 ) , ch ! ( 4 ) ) . collect :: < Vec < _ > > ( ) ;
134136 assert_eq ! ( output, [ 1.0 , 1.0 , 0.0 , 0.0 , 2.0 , 2.0 , 0.0 , 0.0 ] ) ;
135137
136138 let input = vec ! [ 1.0 , 2.0 , 3.0 , 4.0 ] ;
137- let output = ChannelCountConverter :: new ( input. into_iter ( ) , 2 , 4 ) . collect :: < Vec < _ > > ( ) ;
139+ let output =
140+ ChannelCountConverter :: new ( input. into_iter ( ) , ch ! ( 2 ) , ch ! ( 4 ) ) . collect :: < Vec < _ > > ( ) ;
138141 assert_eq ! ( output, [ 1.0 , 2.0 , 0.0 , 0.0 , 3.0 , 4.0 , 0.0 , 0.0 ] ) ;
139142 }
140143
141144 #[ test]
142145 fn size_hint ( ) {
143146 fn test ( input : & [ Sample ] , from : ChannelCount , to : ChannelCount ) {
144- let mut converter = ChannelCountConverter :: new ( input. iter ( ) . copied ( ) , from, to) ;
147+ let mut converter =
148+ ChannelCountConverter :: new ( input. iter ( ) . copied ( ) , from, to) ;
145149 let count = converter. clone ( ) . count ( ) ;
146150 for left_in_iter in ( 0 ..=count) . rev ( ) {
147151 println ! ( "left_in_iter = {left_in_iter}" ) ;
@@ -151,24 +155,24 @@ mod test {
151155 assert_eq ! ( converter. size_hint( ) , ( 0 , Some ( 0 ) ) ) ;
152156 }
153157
154- test ( & [ 1.0 , 2.0 , 3.0 ] , 1 , 2 ) ;
155- test ( & [ 1.0 , 2.0 , 3.0 , 4.0 ] , 2 , 4 ) ;
156- test ( & [ 1.0 , 2.0 , 3.0 , 4.0 ] , 4 , 2 ) ;
157- test ( & [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ] , 3 , 8 ) ;
158- test ( & [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 ] , 4 , 1 ) ;
158+ test ( & [ 1.0 , 2.0 , 3.0 ] , ch ! ( 1 ) , ch ! ( 2 ) ) ;
159+ test ( & [ 1.0 , 2.0 , 3.0 , 4.0 ] , ch ! ( 2 ) , ch ! ( 4 ) ) ;
160+ test ( & [ 1.0 , 2.0 , 3.0 , 4.0 ] , ch ! ( 4 ) , ch ! ( 2 ) ) ;
161+ test ( & [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ] , ch ! ( 3 ) , ch ! ( 8 ) ) ;
162+ test ( & [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 ] , ch ! ( 4 ) , ch ! ( 1 ) ) ;
159163 }
160164
161165 #[ test]
162166 fn len_more ( ) {
163167 let input = vec ! [ 1.0 , 2.0 , 3.0 , 4.0 ] ;
164- let output = ChannelCountConverter :: new ( input. into_iter ( ) , 2 , 3 ) ;
168+ let output = ChannelCountConverter :: new ( input. into_iter ( ) , ch ! ( 2 ) , ch ! ( 3 ) ) ;
165169 assert_eq ! ( output. len( ) , 6 ) ;
166170 }
167171
168172 #[ test]
169173 fn len_less ( ) {
170174 let input = vec ! [ 1.0 , 2.0 , 3.0 , 4.0 ] ;
171- let output = ChannelCountConverter :: new ( input. into_iter ( ) , 2 , 1 ) ;
175+ let output = ChannelCountConverter :: new ( input. into_iter ( ) , ch ! ( 2 ) , ch ! ( 1 ) ) ;
172176 assert_eq ! ( output. len( ) , 2 ) ;
173177 }
174178}
0 commit comments