@@ -134,7 +134,7 @@ pub fn process_wav(input_path: &Path, output_path: &Path) -> Result<(), Box<dyn
134134fn resample ( input : & [ f32 ] , input_rate : f32 , output_rate : f32 ) -> Result < Vec < f32 > , Box < dyn Error > > {
135135 println ! ( "Resampling from {} Hz to {} Hz" , input_rate, output_rate) ;
136136
137- let ratio = output_rate / input_rate;
137+ let ratio = ( output_rate / input_rate) as f64 ;
138138 let sinc_params = SincInterpolationParameters {
139139 sinc_len : 256 ,
140140 f_cutoff : 0.95 ,
@@ -145,7 +145,7 @@ fn resample(input: &[f32], input_rate: f32, output_rate: f32) -> Result<Vec<f32>
145145
146146 let chunk_size = 1024 ; // Matches constructor argument for consistency.
147147 let mut resampler = SincFixedIn :: < f64 > :: new (
148- ratio as f64 ,
148+ ratio,
149149 1.0 , // nb_channels as f64
150150 sinc_params,
151151 chunk_size,
@@ -180,6 +180,12 @@ fn resample(input: &[f32], input_rate: f32, output_rate: f32) -> Result<Vec<f32>
180180 output. extend_from_slice ( & waves_out[ 0 ] ) ;
181181 }
182182
183+ // Truncate to the expected length to account for padding
184+ let expected_len = ( input. len ( ) as f64 * ratio) . round ( ) as usize ;
185+ if output. len ( ) > expected_len {
186+ output. truncate ( expected_len) ;
187+ }
188+
183189 Ok ( output. into_iter ( ) . map ( |s| s as f32 ) . collect ( ) )
184190}
185191
@@ -188,7 +194,6 @@ mod tests {
188194 use super :: * ;
189195
190196 use hound:: WavSpec as InputSpec ;
191- use std:: fs:: File ;
192197
193198 /// Generates a simple sine wave WAV file for testing.
194199 fn create_test_wav (
@@ -197,25 +202,23 @@ mod tests {
197202 rate : u32 ,
198203 freq : f32 ,
199204 duration : f32 ,
200- ) -> WavWriter < File > {
205+ ) {
201206 let spec = InputSpec {
202207 channels,
203208 sample_rate : rate,
204209 bits_per_sample : 16 ,
205210 sample_format : SampleFormat :: Int ,
206211 } ;
207212 let mut writer = WavWriter :: create ( path, spec) . unwrap ( ) ;
208- let num_samples = ( rate as f32 * duration) as usize / channels as usize ;
209- for i in 0 ..num_samples {
213+ let num_frames = ( rate as f32 * duration) as usize ;
214+ for i in 0 ..num_frames {
210215 let t = i as f32 / rate as f32 ;
211216 let sample = ( 2.0 * std:: f32:: consts:: PI * freq * t) . sin ( ) ;
212217 for _ in 0 ..channels as usize {
213218 writer. write_sample ( ( sample * 32767.0 ) as i16 ) . unwrap ( ) ;
214219 }
215220 }
216221 writer. finalize ( ) . unwrap ( ) ;
217-
218- WavWriter :: new ( File :: create ( path) . unwrap ( ) , spec) . unwrap ( ) // Reopen for test consistency, but actually we use the file
219222 }
220223
221224 #[ test]
@@ -264,11 +267,16 @@ mod tests {
264267 let output_file = dir. path ( ) . join ( "output.wav" ) ;
265268 process_wav ( & input_file, & output_file) . unwrap ( ) ;
266269
267- let mut reader = WavReader :: open ( & output_file) . unwrap ( ) ;
268- let samples_out: Vec < i16 > = reader . samples ( ) . collect :: < Result < _ , _ > > ( ) . unwrap ( ) ;
270+ let mut reader_out = WavReader :: open ( & output_file) . unwrap ( ) ;
271+ let samples_out: Vec < i16 > = reader_out . samples ( ) . collect :: < Result < _ , _ > > ( ) . unwrap ( ) ;
269272
270273 let mut reader_in = WavReader :: open ( & input_file) . unwrap ( ) ;
271274 let samples_in: Vec < i16 > = reader_in. samples ( ) . collect :: < Result < _ , _ > > ( ) . unwrap ( ) ;
272- assert_eq ! ( samples_out, samples_in) ; // No change beyond potential normalization
275+
276+ assert_eq ! ( samples_out. len( ) , samples_in. len( ) ) ;
277+ assert ! ( samples_out
278+ . iter( )
279+ . zip( & samples_in)
280+ . all( |( & a, & b) | ( a as i32 - b as i32 ) . abs( ) <= 1 ) ) ;
273281 }
274282}
0 commit comments