11
11
from : ChannelCount ,
12
12
to : ChannelCount ,
13
13
sample_repeat : Option < Sample > ,
14
- next_output_sample_pos : ChannelCount ,
14
+ next_output_sample_pos : u16 ,
15
15
}
16
16
17
17
impl < I > ChannelCountConverter < I >
26
26
///
27
27
#[ inline]
28
28
pub fn new ( input : I , from : ChannelCount , to : ChannelCount ) -> ChannelCountConverter < I > {
29
- assert ! ( from >= 1 ) ;
30
- assert ! ( to >= 1 ) ;
31
-
32
29
ChannelCountConverter {
33
30
input,
34
31
from,
65
62
self . sample_repeat = value;
66
63
value
67
64
}
68
- x if x < self . from => self . input . next ( ) ,
65
+ x if x < self . from . get ( ) => self . input . next ( ) ,
69
66
1 => self . sample_repeat ,
70
67
_ => Some ( 0.0 ) ,
71
68
} ;
@@ -74,11 +71,11 @@ where
74
71
self . next_output_sample_pos += 1 ;
75
72
}
76
73
77
- if self . next_output_sample_pos == self . to {
74
+ if self . next_output_sample_pos == self . to . get ( ) {
78
75
self . next_output_sample_pos = 0 ;
79
76
80
77
if self . from > self . to {
81
- for _ in self . to .. self . from {
78
+ for _ in self . to . get ( ) .. self . from . get ( ) {
82
79
self . input . next ( ) ; // discarding extra input
83
80
}
84
81
}
91
88
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
92
89
let ( min, max) = self . input . size_hint ( ) ;
93
90
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 ;
95
92
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
97
94
- self . next_output_sample_pos as usize
98
95
} ;
99
96
@@ -110,38 +107,45 @@ impl<I> ExactSizeIterator for ChannelCountConverter<I> where I: ExactSizeIterato
110
107
mod test {
111
108
use super :: ChannelCountConverter ;
112
109
use crate :: common:: ChannelCount ;
110
+ use crate :: math:: ch;
113
111
use crate :: Sample ;
114
112
115
113
#[ test]
116
114
fn remove_channels ( ) {
117
115
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 < _ > > ( ) ;
119
118
assert_eq ! ( output, [ 1.0 , 2.0 , 4.0 , 5.0 ] ) ;
120
119
121
120
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 < _ > > ( ) ;
123
123
assert_eq ! ( output, [ 1.0 , 5.0 ] ) ;
124
124
}
125
125
126
126
#[ test]
127
127
fn add_channels ( ) {
128
128
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 < _ > > ( ) ;
130
131
assert_eq ! ( output, [ 1.0 , 1.0 , 2.0 , 2.0 , 3.0 , 3.0 , 4.0 , 4.0 ] ) ;
131
132
132
133
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 < _ > > ( ) ;
134
136
assert_eq ! ( output, [ 1.0 , 1.0 , 0.0 , 0.0 , 2.0 , 2.0 , 0.0 , 0.0 ] ) ;
135
137
136
138
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 < _ > > ( ) ;
138
141
assert_eq ! ( output, [ 1.0 , 2.0 , 0.0 , 0.0 , 3.0 , 4.0 , 0.0 , 0.0 ] ) ;
139
142
}
140
143
141
144
#[ test]
142
145
fn size_hint ( ) {
143
146
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) ;
145
149
let count = converter. clone ( ) . count ( ) ;
146
150
for left_in_iter in ( 0 ..=count) . rev ( ) {
147
151
println ! ( "left_in_iter = {left_in_iter}" ) ;
@@ -151,24 +155,24 @@ mod test {
151
155
assert_eq ! ( converter. size_hint( ) , ( 0 , Some ( 0 ) ) ) ;
152
156
}
153
157
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 ) ) ;
159
163
}
160
164
161
165
#[ test]
162
166
fn len_more ( ) {
163
167
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 ) ) ;
165
169
assert_eq ! ( output. len( ) , 6 ) ;
166
170
}
167
171
168
172
#[ test]
169
173
fn len_less ( ) {
170
174
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 ) ) ;
172
176
assert_eq ! ( output. len( ) , 2 ) ;
173
177
}
174
178
}
0 commit comments