Debian Codegraph @

tests/termination_test.c

raw
1 /*
2 ** Copyright (c) 2002-2016, Erik de Castro Lopo <[email protected]>
3 ** All rights reserved.
4 **
5 ** This code is released under 2-clause BSD license. Please see the
6 ** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <math.h>
16
17 #include <samplerate.h>
18
19 #include "util.h"
20
21 #define SHORT_BUFFER_LEN 2048
22 #define LONG_BUFFER_LEN ((1 << 16) - 20)
23
24 #ifdef ENABLE_SINC_FAST_CONVERTER
25 static void simple_test (int converter) ;
26 #endif
27 static void stream_test (int converter, double ratio) ;
28 static void init_term_test (int converter, double ratio) ;
29
30 static int next_block_length (int reset) ;
31
32 int
33 main (void)
34 { static double src_ratios [] =
35 { 0.999900, 1.000100, 0.789012, 1.200000, 0.333333, 3.100000,
36 0.125000, 8.000000, 0.099900, 9.990000, 0.100000, 10.00000
37 } ;
38
39 int k ;
40
41 puts ("\n Zero Order Hold interpolator:") ;
42
43 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
44 init_term_test (SRC_ZERO_ORDER_HOLD, src_ratios [k]) ;
45 puts ("") ;
46 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
47 stream_test (SRC_ZERO_ORDER_HOLD, src_ratios [k]) ;
48
49
50 puts ("\n Linear interpolator:") ;
51 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
52 init_term_test (SRC_LINEAR, src_ratios [k]) ;
53 puts ("") ;
54 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
55 stream_test (SRC_LINEAR, src_ratios [k]) ;
56
57
58 #ifdef ENABLE_SINC_FAST_CONVERTER
59 puts ("\n Sinc interpolator:") ;
60 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
61 init_term_test (SRC_SINC_FASTEST, src_ratios [k]) ;
62 puts ("") ;
63 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
64 stream_test (SRC_SINC_FASTEST, src_ratios [k]) ;
65
66 puts ("") ;
67
68 simple_test (SRC_SINC_FASTEST) ;
69 #endif
70
71 return 0 ;
72 } /* main */
73
74 #ifdef ENABLE_SINC_FAST_CONVERTER
75 static void
76 simple_test (int converter)
77 {
78 // MSVC doesn't support variable-length arrays
79 #define ilen 199030
80 #define olen 1000
81 int error ;
82
83 {
84 float in [ilen] ;
85 float out [olen] ;
86 double ratio = (1.0 * olen) / ilen ;
87 SRC_DATA src_data =
88 { in, out,
89 ilen, olen,
90 0, 0, 0,
91 ratio
92 } ;
93
94 error = src_simple (&src_data, converter, 1) ;
95 if (error)
96 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
97 exit (1) ;
98 } ;
99 } ;
100
101 return ;
102 } /* simple_test */
103 #endif
104
105 static void
106 init_term_test (int converter, double src_ratio)
107 { static float input [SHORT_BUFFER_LEN], output [SHORT_BUFFER_LEN] ;
108
109 SRC_DATA src_data ;
110
111 int k, input_len, output_len, error, terminate ;
112
113 printf ("\tinit_term_test (SRC ratio = %7.4f) .......... ", src_ratio) ;
114 fflush (stdout) ;
115
116 /* Calculate maximun input and output lengths. */
117 if (src_ratio >= 1.0)
118 { output_len = SHORT_BUFFER_LEN ;
119 input_len = (int) floor (SHORT_BUFFER_LEN / src_ratio) ;
120 }
121 else
122 { input_len = SHORT_BUFFER_LEN ;
123 output_len = (int) floor (SHORT_BUFFER_LEN * src_ratio) ;
124 } ;
125
126 /* Reduce input_len by 10 so output is longer than necessary. */
127 input_len -= 10 ;
128
129 for (k = 0 ; k < ARRAY_LEN (input) ; k++)
130 input [k] = 1.0 ;
131
132 if (output_len > SHORT_BUFFER_LEN)
133 { printf ("\n\nLine %d : output_len > SHORT_BUFFER_LEN\n\n", __LINE__) ;
134 exit (1) ;
135 } ;
136
137 src_data.data_in = input ;
138 src_data.input_frames = input_len ;
139
140 src_data.src_ratio = src_ratio ;
141
142 src_data.data_out = output ;
143 src_data.output_frames = SHORT_BUFFER_LEN ;
144
145 if ((error = src_simple (&src_data, converter, 1)))
146 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
147 exit (1) ;
148 } ;
149
150 terminate = (int) ceil ((src_ratio >= 1.0) ? 1 : 1.0 / src_ratio) ;
151
152 if (fabs (src_ratio * input_len - src_data.output_frames_gen) > terminate)
153 { printf ("\n\nLine %d : Bad output frame count.\n\n", __LINE__) ;
154 printf ("\tterminate : %d\n", terminate) ;
155 printf ("\tsrc_ratio : %.4f\n", src_ratio) ;
156 printf ("\tinput_len : %d\n"
157 "\tinput_len * src_ratio : %f\n", input_len, input_len * src_ratio) ;
158 printf ("\toutput_frames_gen : %ld\n\n", src_data.output_frames_gen) ;
159 exit (1) ;
160 } ;
161
162 if (labs (src_data.input_frames_used - input_len) > 1)
163 { printf ("\n\nLine %d : input_frames_used should be %d, is %ld.\n\n",
164 __LINE__, input_len, src_data.input_frames_used) ;
165 printf ("\tsrc_ratio : %.4f\n", src_ratio) ;
166 printf ("\tinput_len : %d\n\tinput_used : %ld\n\n", input_len, src_data.input_frames_used) ;
167 exit (1) ;
168 } ;
169
170 if (fabs (output [0]) < 0.1)
171 { printf ("\n\nLine %d : First output sample is bad.\n\n", __LINE__) ;
172 printf ("\toutput [0] == %f\n\n", output [0]) ;
173 exit (1) ;
174 }
175
176 puts ("ok") ;
177
178 return ;
179 } /* init_term_test */
180
181 static void
182 stream_test (int converter, double src_ratio)
183 { static float input [LONG_BUFFER_LEN], output [LONG_BUFFER_LEN] ;
184
185 SRC_STATE *src_state ;
186 SRC_DATA src_data ;
187
188 int input_len, output_len, current_in, current_out ;
189 int k, error, terminate ;
190
191 printf ("\tstream_test (SRC ratio = %7.4f) .......... ", src_ratio) ;
192 fflush (stdout) ;
193
194 /* Erik */
195 for (k = 0 ; k < LONG_BUFFER_LEN ; k++) input [k] = k * 1.0f ;
196
197 /* Calculate maximun input and output lengths. */
198 if (src_ratio >= 1.0)
199 { output_len = LONG_BUFFER_LEN ;
200 input_len = (int) floor (LONG_BUFFER_LEN / src_ratio) ;
201 }
202 else
203 { input_len = LONG_BUFFER_LEN ;
204 output_len = (int) floor (LONG_BUFFER_LEN * src_ratio) ;
205 } ;
206
207 /* Reduce input_len by 10 so output is longer than necessary. */
208 input_len -= 20 ;
209
210 if (output_len > LONG_BUFFER_LEN)
211 { printf ("\n\nLine %d : output_len > LONG_BUFFER_LEN\n\n", __LINE__) ;
212 exit (1) ;
213 } ;
214
215 current_in = current_out = 0 ;
216
217 /* Perform sample rate conversion. */
218 if ((src_state = src_new (converter, 1, &error)) == NULL)
219 { printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__, src_strerror (error)) ;
220 exit (1) ;
221 } ;
222
223 src_data.end_of_input = 0 ; /* Set this later. */
224
225 src_data.data_in = input ;
226
227 src_data.src_ratio = src_ratio ;
228
229 src_data.data_out = output ;
230 src_data.output_frames = ARRAY_LEN (output) / 10 ;
231
232 terminate = 1 + (int) ceil ((src_ratio >= 1.0) ? src_ratio : 1.0 / src_ratio) ;
233
234 while (1)
235 {
236 src_data.input_frames = next_block_length (0) ;
237 src_data.input_frames = MIN (src_data.input_frames, input_len - current_in) ;
238
239 src_data.output_frames = ARRAY_LEN (output) - current_out ;
240 /*-Erik MIN (src_data.output_frames, output_len - current_out) ;-*/
241
242 src_data.end_of_input = (current_in >= input_len) ? 1 : 0 ;
243
244 if ((error = src_process (src_state, &src_data)))
245 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
246 printf (" src_data.input_frames : %ld\n", src_data.input_frames) ;
247 printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ;
248 exit (1) ;
249 } ;
250
251 if (src_data.end_of_input && src_data.output_frames_gen == 0)
252 break ;
253
254 if (src_data.input_frames_used > src_data.input_frames)
255 { printf ("\n\nLine %d : input_frames_used > input_frames\n\n", __LINE__) ;
256 printf (" src_data.input_frames : %ld\n", src_data.input_frames) ;
257 printf (" src_data.input_frames_used : %ld\n", src_data.input_frames_used) ;
258 printf (" src_data.output_frames : %ld\n", src_data.output_frames) ;
259 printf (" src_data.output_frames_gen : %ld\n\n", src_data.output_frames_gen) ;
260 exit (1) ;
261 } ;
262
263 if (src_data.input_frames_used < 0)
264 { printf ("\n\nLine %d : input_frames_used (%ld) < 0\n\n", __LINE__, src_data.input_frames_used) ;
265 exit (1) ;
266 } ;
267
268 if (src_data.output_frames_gen < 0)
269 { printf ("\n\nLine %d : output_frames_gen (%ld) < 0\n\n", __LINE__, src_data.output_frames_gen) ;
270 exit (1) ;
271 } ;
272
273 current_in += src_data.input_frames_used ;
274 current_out += src_data.output_frames_gen ;
275
276 if (current_in > input_len + terminate)
277 { printf ("\n\nLine %d : current_in (%d) > input_len (%d + %d)\n\n", __LINE__, current_in, input_len, terminate) ;
278 exit (1) ;
279 } ;
280
281 if (current_out > output_len)
282 { printf ("\n\nLine %d : current_out (%d) > output_len (%d)\n\n", __LINE__, current_out, output_len) ;
283 exit (1) ;
284 } ;
285
286 if (src_data.input_frames_used > input_len)
287 { printf ("\n\nLine %d : input_frames_used (%ld) > %d\n\n", __LINE__, src_data.input_frames_used, input_len) ;
288 exit (1) ;
289 } ;
290
291 if (src_data.output_frames_gen > output_len)
292 { printf ("\n\nLine %d : output_frames_gen (%ld) > %d\n\n", __LINE__, src_data.output_frames_gen, output_len) ;
293 exit (1) ;
294 } ;
295
296 if (src_data.data_in == NULL && src_data.output_frames_gen == 0)
297 break ;
298
299
300 src_data.data_in += src_data.input_frames_used ;
301 src_data.data_out += src_data.output_frames_gen ;
302 } ;
303
304 src_state = src_delete (src_state) ;
305
306 if (fabs (current_out - src_ratio * input_len) > terminate)
307 { printf ("\n\nLine %d : bad output data length %d should be %2.1f +/- %d.\n", __LINE__,
308 current_out, src_ratio * input_len, terminate) ;
309 printf ("\tsrc_ratio : %.4f\n", src_ratio) ;
310 printf ("\tinput_len : %d\n\tinput_used : %d\n", input_len, current_in) ;
311 printf ("\toutput_len : %d\n\toutput_gen : %d\n\n", output_len, current_out) ;
312 exit (1) ;
313 } ;
314
315 if (current_in != input_len)
316 { printf ("\n\nLine %d : unused input.\n", __LINE__) ;
317 printf ("\tinput_len : %d\n", input_len) ;
318 printf ("\tinput_frames_used : %d\n\n", current_in) ;
319 exit (1) ;
320 } ;
321
322 puts ("ok") ;
323
324 return ;
325 } /* stream_test */
326
327 static int
328 next_block_length (int reset)
329 { static int block_lengths [] = /* Should be an odd length. */
330 { /*-2, 500, 5, 400, 10, 300, 20, 200, 50, 100, 70 -*/
331 5, 400, 10, 300, 20, 200, 50, 100, 70
332 } ;
333 static int block_len_index = 0 ;
334
335 if (reset)
336 block_len_index = 0 ;
337 else
338 block_len_index = (block_len_index + 1) % ARRAY_LEN (block_lengths) ;
339
340 return block_lengths [block_len_index] ;
341 } /* next_block_length */
342