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