]>
iEval git - audio-libsamplerate.git/blob - termination_test.c
caffec407eb1abc4eee81ba369e7fd2563e7aac1
2 ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** All rights reserved.
5 ** This code is released under 2-clause BSD license. Please see the
6 ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
13 #include <samplerate.h>
17 #define SHORT_BUFFER_LEN 2048
18 #define LONG_BUFFER_LEN ((1 << 16) - 20)
20 static void simple_test (int converter
) ;
21 static void stream_test (int converter
, double ratio
) ;
22 static void init_term_test (int converter
, double ratio
) ;
24 static int next_block_length (int reset
) ;
28 { static double src_ratios
[] =
29 { 0.999900, 1.000100, 0.789012, 1.200000, 0.333333, 3.100000,
30 0.125000, 8.000000, 0.099900, 9.990000, 0.100000, 10.00000
35 puts ("\n Zero Order Hold interpolator:") ;
37 for (k
= 0 ; k
< ARRAY_LEN (src_ratios
) ; k
++)
38 init_term_test (SRC_ZERO_ORDER_HOLD
, src_ratios
[k
]) ;
40 for (k
= 0 ; k
< ARRAY_LEN (src_ratios
) ; k
++)
41 stream_test (SRC_ZERO_ORDER_HOLD
, src_ratios
[k
]) ;
44 puts ("\n Linear interpolator:") ;
45 for (k
= 0 ; k
< ARRAY_LEN (src_ratios
) ; k
++)
46 init_term_test (SRC_LINEAR
, src_ratios
[k
]) ;
48 for (k
= 0 ; k
< ARRAY_LEN (src_ratios
) ; k
++)
49 stream_test (SRC_LINEAR
, src_ratios
[k
]) ;
52 puts ("\n Sinc interpolator:") ;
53 for (k
= 0 ; k
< ARRAY_LEN (src_ratios
) ; k
++)
54 init_term_test (SRC_SINC_FASTEST
, src_ratios
[k
]) ;
56 for (k
= 0 ; k
< ARRAY_LEN (src_ratios
) ; k
++)
57 stream_test (SRC_SINC_FASTEST
, src_ratios
[k
]) ;
61 simple_test (SRC_SINC_FASTEST
) ;
67 simple_test (int converter
)
69 int ilen
= 199030, olen
= 1000, error
;
74 double ratio
= (1.0 * olen
) / ilen
;
82 error
= src_simple (&src_data
, converter
, 1) ;
84 { printf ("\n\nLine %d : %s\n\n", __LINE__
, src_strerror (error
)) ;
93 init_term_test (int converter
, double src_ratio
)
94 { static float input
[SHORT_BUFFER_LEN
], output
[SHORT_BUFFER_LEN
] ;
98 int k
, input_len
, output_len
, error
, terminate
;
100 printf ("\tinit_term_test (SRC ratio = %7.4f) .......... ", src_ratio
) ;
103 /* Calculate maximun input and output lengths. */
104 if (src_ratio
>= 1.0)
105 { output_len
= SHORT_BUFFER_LEN
;
106 input_len
= (int) floor (SHORT_BUFFER_LEN
/ src_ratio
) ;
109 { input_len
= SHORT_BUFFER_LEN
;
110 output_len
= (int) floor (SHORT_BUFFER_LEN
* src_ratio
) ;
113 /* Reduce input_len by 10 so output is longer than necessary. */
116 for (k
= 0 ; k
< ARRAY_LEN (input
) ; k
++)
119 if (output_len
> SHORT_BUFFER_LEN
)
120 { printf ("\n\nLine %d : output_len > SHORT_BUFFER_LEN\n\n", __LINE__
) ;
124 src_data
.data_in
= input
;
125 src_data
.input_frames
= input_len
;
127 src_data
.src_ratio
= src_ratio
;
129 src_data
.data_out
= output
;
130 src_data
.output_frames
= SHORT_BUFFER_LEN
;
132 if ((error
= src_simple (&src_data
, converter
, 1)))
133 { printf ("\n\nLine %d : %s\n\n", __LINE__
, src_strerror (error
)) ;
137 terminate
= (int) ceil ((src_ratio
>= 1.0) ? 1 : 1.0 / src_ratio
) ;
139 if (fabs (src_ratio
* input_len
- src_data
.output_frames_gen
) > terminate
)
140 { printf ("\n\nLine %d : Bad output frame count.\n\n", __LINE__
) ;
141 printf ("\tterminate : %d\n", terminate
) ;
142 printf ("\tsrc_ratio : %.4f\n", src_ratio
) ;
143 printf ("\tinput_len : %d\n"
144 "\tinput_len * src_ratio : %f\n", input_len
, input_len
* src_ratio
) ;
145 printf ("\toutput_frames_gen : %ld\n\n", src_data
.output_frames_gen
) ;
149 if (labs (src_data
.input_frames_used
- input_len
) > 1)
150 { printf ("\n\nLine %d : input_frames_used should be %d, is %ld.\n\n",
151 __LINE__
, input_len
, src_data
.input_frames_used
) ;
152 printf ("\tsrc_ratio : %.4f\n", src_ratio
) ;
153 printf ("\tinput_len : %d\n\tinput_used : %ld\n\n", input_len
, src_data
.input_frames_used
) ;
157 if (fabs (output
[0]) < 0.1)
158 { printf ("\n\nLine %d : First output sample is bad.\n\n", __LINE__
) ;
159 printf ("\toutput [0] == %f\n\n", output
[0]) ;
166 } /* init_term_test */
169 stream_test (int converter
, double src_ratio
)
170 { static float input
[LONG_BUFFER_LEN
], output
[LONG_BUFFER_LEN
] ;
172 SRC_STATE
*src_state
;
175 int input_len
, output_len
, current_in
, current_out
;
176 int k
, error
, terminate
;
178 printf ("\tstream_test (SRC ratio = %7.4f) .......... ", src_ratio
) ;
182 for (k
= 0 ; k
< LONG_BUFFER_LEN
; k
++) input
[k
] = k
* 1.0 ;
184 /* Calculate maximun input and output lengths. */
185 if (src_ratio
>= 1.0)
186 { output_len
= LONG_BUFFER_LEN
;
187 input_len
= (int) floor (LONG_BUFFER_LEN
/ src_ratio
) ;
190 { input_len
= LONG_BUFFER_LEN
;
191 output_len
= (int) floor (LONG_BUFFER_LEN
* src_ratio
) ;
194 /* Reduce input_len by 10 so output is longer than necessary. */
197 if (output_len
> LONG_BUFFER_LEN
)
198 { printf ("\n\nLine %d : output_len > LONG_BUFFER_LEN\n\n", __LINE__
) ;
202 current_in
= current_out
= 0 ;
204 /* Perform sample rate conversion. */
205 if ((src_state
= src_new (converter
, 1, &error
)) == NULL
)
206 { printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__
, src_strerror (error
)) ;
210 src_data
.end_of_input
= 0 ; /* Set this later. */
212 src_data
.data_in
= input
;
214 src_data
.src_ratio
= src_ratio
;
216 src_data
.data_out
= output
;
217 src_data
.output_frames
= ARRAY_LEN (output
) / 10 ;
219 terminate
= 1 + (int) ceil ((src_ratio
>= 1.0) ? src_ratio
: 1.0 / src_ratio
) ;
223 src_data
.input_frames
= next_block_length (0) ;
224 src_data
.input_frames
= MIN (src_data
.input_frames
, input_len
- current_in
) ;
226 src_data
.output_frames
= ARRAY_LEN (output
) - current_out
;
227 /*-Erik MIN (src_data.output_frames, output_len - current_out) ;-*/
229 src_data
.end_of_input
= (current_in
>= input_len
) ? 1 : 0 ;
231 if ((error
= src_process (src_state
, &src_data
)))
232 { printf ("\n\nLine %d : %s\n\n", __LINE__
, src_strerror (error
)) ;
233 printf (" src_data.input_frames : %ld\n", src_data
.input_frames
) ;
234 printf (" src_data.output_frames : %ld\n\n", src_data
.output_frames
) ;
238 if (src_data
.end_of_input
&& src_data
.output_frames_gen
== 0)
241 if (src_data
.input_frames_used
> src_data
.input_frames
)
242 { printf ("\n\nLine %d : input_frames_used > input_frames\n\n", __LINE__
) ;
243 printf (" src_data.input_frames : %ld\n", src_data
.input_frames
) ;
244 printf (" src_data.input_frames_used : %ld\n", src_data
.input_frames_used
) ;
245 printf (" src_data.output_frames : %ld\n", src_data
.output_frames
) ;
246 printf (" src_data.output_frames_gen : %ld\n\n", src_data
.output_frames_gen
) ;
250 if (src_data
.input_frames_used
< 0)
251 { printf ("\n\nLine %d : input_frames_used (%ld) < 0\n\n", __LINE__
, src_data
.input_frames_used
) ;
255 if (src_data
.output_frames_gen
< 0)
256 { printf ("\n\nLine %d : output_frames_gen (%ld) < 0\n\n", __LINE__
, src_data
.output_frames_gen
) ;
260 current_in
+= src_data
.input_frames_used
;
261 current_out
+= src_data
.output_frames_gen
;
263 if (current_in
> input_len
+ terminate
)
264 { printf ("\n\nLine %d : current_in (%d) > input_len (%d + %d)\n\n", __LINE__
, current_in
, input_len
, terminate
) ;
268 if (current_out
> output_len
)
269 { printf ("\n\nLine %d : current_out (%d) > output_len (%d)\n\n", __LINE__
, current_out
, output_len
) ;
273 if (src_data
.input_frames_used
> input_len
)
274 { printf ("\n\nLine %d : input_frames_used (%ld) > %d\n\n", __LINE__
, src_data
.input_frames_used
, input_len
) ;
278 if (src_data
.output_frames_gen
> output_len
)
279 { printf ("\n\nLine %d : output_frames_gen (%ld) > %d\n\n", __LINE__
, src_data
.output_frames_gen
, output_len
) ;
283 if (src_data
.data_in
== NULL
&& src_data
.output_frames_gen
== 0)
287 src_data
.data_in
+= src_data
.input_frames_used
;
288 src_data
.data_out
+= src_data
.output_frames_gen
;
291 src_state
= src_delete (src_state
) ;
293 if (fabs (current_out
- src_ratio
* input_len
) > terminate
)
294 { printf ("\n\nLine %d : bad output data length %d should be %2.1f +/- %d.\n", __LINE__
,
295 current_out
, src_ratio
* input_len
, terminate
) ;
296 printf ("\tsrc_ratio : %.4f\n", src_ratio
) ;
297 printf ("\tinput_len : %d\n\tinput_used : %d\n", input_len
, current_in
) ;
298 printf ("\toutput_len : %d\n\toutput_gen : %d\n\n", output_len
, current_out
) ;
302 if (current_in
!= input_len
)
303 { printf ("\n\nLine %d : unused input.\n", __LINE__
) ;
304 printf ("\tinput_len : %d\n", input_len
) ;
305 printf ("\tinput_frames_used : %d\n\n", current_in
) ;
315 next_block_length (int reset
)
316 { static int block_lengths
[] = /* Should be an odd length. */
317 { /*-2, 500, 5, 400, 10, 300, 20, 200, 50, 100, 70 -*/
318 5, 400, 10, 300, 20, 200, 50, 100, 70
320 static int block_len_index
= 0 ;
323 block_len_index
= 0 ;
325 block_len_index
= (block_len_index
+ 1) % ARRAY_LEN (block_lengths
) ;
327 return block_lengths
[block_len_index
] ;
328 } /* next_block_length */
This page took 0.073204 seconds and 3 git commands to generate.