@@ -641,7 +641,6 @@ mod tests {
641641 } ;
642642 use crate :: { Async , FixedAsync } ;
643643 use audioadapter_buffers:: direct:: SequentialSliceOfVecs ;
644- use rand:: Rng ;
645644 use test_case:: test_matrix;
646645
647646 fn basic_params ( ) -> SincInterpolationParameters {
@@ -815,4 +814,91 @@ mod tests {
815814 resampler. set_chunk_size ( 600 ) . unwrap ( ) ;
816815 check_output ! ( resampler, f64 ) ;
817816 }
817+
818+ fn process_and_get_frame_counts ( resampler : & mut Async < f64 > ) -> ( usize , usize ) {
819+ let input_frames = resampler. input_frames_next ( ) ;
820+ let output_frames_max = resampler. output_frames_max ( ) ;
821+ let input_data = vec ! [ vec![ 0.25 ; input_frames] ; 2 ] ;
822+ let input = SequentialSliceOfVecs :: new ( & input_data, 2 , input_frames) . unwrap ( ) ;
823+
824+ let mut output_data = vec ! [ vec![ 0.0 ; output_frames_max] ; 2 ] ;
825+ let mut output =
826+ SequentialSliceOfVecs :: new_mut ( & mut output_data, 2 , output_frames_max) . unwrap ( ) ;
827+
828+ let ( consumed_frames, produced_frames) = resampler
829+ . process_into_buffer ( & input, & mut output, None )
830+ . unwrap ( ) ;
831+ ( consumed_frames, produced_frames)
832+ }
833+
834+ fn process_chunks_and_get_total_frame_counts (
835+ resampler : & mut Async < f64 > ,
836+ nbr_chunks : usize ,
837+ ) -> ( usize , usize ) {
838+ ( 0 ..nbr_chunks) . fold ( ( 0 , 0 ) , |( sum_in, sum_out) , _| {
839+ let ( frames_in, frames_out) = process_and_get_frame_counts ( resampler) ;
840+ ( sum_in + frames_in, sum_out + frames_out)
841+ } )
842+ }
843+
844+ fn assert_ratio_within_tolerance (
845+ total_in : usize ,
846+ total_out : usize ,
847+ expected_ratio : f64 ,
848+ tolerance : f64 ,
849+ label : & str ,
850+ ) {
851+ let measured_ratio = total_out as f64 / total_in as f64 ;
852+ let lower = expected_ratio - tolerance;
853+ let upper = expected_ratio + tolerance;
854+ assert ! (
855+ measured_ratio >= lower && measured_ratio <= upper,
856+ "{} ratio out of range: got {}, expected {} +/- {} (out {}, in {})" ,
857+ label,
858+ measured_ratio,
859+ expected_ratio,
860+ tolerance,
861+ total_out,
862+ total_in,
863+ ) ;
864+ }
865+
866+ fn check_relative_ratio_changes_frame_ratio ( mut resampler : Async < f64 > ) {
867+ let nbr_chunks = 8 ;
868+ let tolerance = 0.01 ;
869+
870+ let ( baseline_in, baseline_out) =
871+ process_chunks_and_get_total_frame_counts ( & mut resampler, nbr_chunks) ;
872+ assert_ratio_within_tolerance ( baseline_in, baseline_out, 1.0 , tolerance, "Baseline" ) ;
873+
874+ // Lower ratio speeds up output.
875+ resampler. set_resample_ratio_relative ( 0.75 , false ) . unwrap ( ) ;
876+ let ( lower_in, lower_out) =
877+ process_chunks_and_get_total_frame_counts ( & mut resampler, nbr_chunks) ;
878+ assert_ratio_within_tolerance ( lower_in, lower_out, 0.75 , tolerance, "0.75x" ) ;
879+
880+ // Higher ratio slows down output.
881+ resampler. set_resample_ratio_relative ( 1.25 , false ) . unwrap ( ) ;
882+ let ( higher_in, higher_out) =
883+ process_chunks_and_get_total_frame_counts ( & mut resampler, nbr_chunks) ;
884+ assert_ratio_within_tolerance ( higher_in, higher_out, 1.25 , tolerance, "1.25x" ) ;
885+ }
886+
887+ #[ test_log:: test( test_matrix(
888+ [ FixedAsync :: Input , FixedAsync :: Output ]
889+ ) ) ]
890+ fn poly_relative_ratio_changes_frame_ratio ( fixed : FixedAsync ) {
891+ let resampler =
892+ Async :: < f64 > :: new_poly ( 1.0 , 4.0 , PolynomialDegree :: Cubic , 1024 , 2 , fixed) . unwrap ( ) ;
893+ check_relative_ratio_changes_frame_ratio ( resampler) ;
894+ }
895+
896+ #[ test_log:: test( test_matrix(
897+ [ FixedAsync :: Input , FixedAsync :: Output ]
898+ ) ) ]
899+ fn sinc_relative_ratio_changes_frame_ratio ( fixed : FixedAsync ) {
900+ let params = basic_params ( ) ;
901+ let resampler = Async :: < f64 > :: new_sinc ( 1.0 , 4.0 , & params, 1024 , 2 , fixed) . unwrap ( ) ;
902+ check_relative_ratio_changes_frame_ratio ( resampler) ;
903+ }
818904}
0 commit comments