Bugs fixed for stdin and stdout.
[Faustine.git] / interpreter / lib / src / libsndfile-1.0.25 / tests / scale_clip_test.tpl
1 [+ AutoGen5 template c +]
2 /*
3 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #include "sfconfig.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #include <math.h>
31
32 #include <sndfile.h>
33
34 #include "utils.h"
35
36 #ifndef M_PI
37 #define M_PI 3.14159265358979323846264338
38 #endif
39
40 #define HALF_BUFFER_SIZE (1 << 12)
41 #define BUFFER_SIZE (2 * HALF_BUFFER_SIZE)
42
43 #define SINE_AMP 1.1
44 #define MAX_ERROR 0.0202
45
46 [+ FOR float_type +]
47 [+ FOR data_type
48 +]static void [+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +] (const char *filename, int filetype, float maxval) ;
49 [+ ENDFOR data_type
50 +][+ ENDFOR float_type +]
51
52 [+ FOR float_type +]
53 [+ FOR int_type
54 +]static void [+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test (const char *filename, int filetype) ;
55 [+ ENDFOR int_type
56 +][+ ENDFOR float_type +]
57
58 [+ FOR int_type +]
59 [+ FOR float_type
60 +]static void [+ (get "int_type_name") +]_[+ (get "float_short_name") +]_scale_write_test (const char *filename, int filetype) ;
61 [+ ENDFOR float_type
62 +][+ ENDFOR int_type +]
63
64 typedef union
65 { double dbl [BUFFER_SIZE] ;
66 float flt [BUFFER_SIZE] ;
67 int i [BUFFER_SIZE] ;
68 short s [BUFFER_SIZE] ;
69 } BUFFER ;
70
71 /* Data buffer. */
72 static BUFFER buffer_out ;
73 static BUFFER buffer_in ;
74
75 int
76 main (void)
77 {
78 flt_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
79 flt_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
80
81 dbl_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
82 dbl_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
83
84 /*
85 ** Now use SF_FORMAT_AU where possible because it allows both
86 ** big and little endian files.
87 */
88
89 flt_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
90 flt_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
91 flt_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
92 flt_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
93 flt_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
94 flt_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
95
96 dbl_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
97 dbl_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
98 dbl_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
99 dbl_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
100 dbl_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
101 dbl_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
102
103 flt_short_clip_read_test ("flt_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
104 flt_int_clip_read_test ("flt_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
105 dbl_short_clip_read_test ("dbl_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
106 dbl_int_clip_read_test ("dbl_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
107
108 short_flt_scale_write_test ("short_flt.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
109 int_flt_scale_write_test ("int_flt.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
110 short_dbl_scale_write_test ("short_dbl.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
111 int_dbl_scale_write_test ("int_dbl.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
112
113 return 0 ;
114 } /* main */
115
116 /*============================================================================================
117 ** Here are the test functions.
118 */
119
120 [+ FOR float_type +]
121 [+ FOR data_type
122 +]static void
123 [+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +] (const char *filename, int filetype, float maxval)
124 { SNDFILE *file ;
125 SF_INFO sfinfo ;
126 int k ;
127 [+ (get "float_type_name") +] *data_out, *data_in ;
128 double diff, clip_max_diff ;
129
130 print_test_name ("[+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +]", filename) ;
131
132 data_out = buffer_out.[+ (get "float_short_name") +] ;
133 data_in = buffer_in.[+ (get "float_short_name") +] ;
134
135 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
136 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
137 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
138 } ;
139
140 sfinfo.samplerate = 44100 ;
141 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
142 sfinfo.channels = 1 ;
143 sfinfo.format = filetype ;
144
145 /*
146 ** Write two versions of the data:
147 ** normalized and clipped
148 ** un-normalized and clipped.
149 */
150
151 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
152 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
153 test_write_[+ (get "float_type_name") +]_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
154 sf_command (file, SFC_SET_NORM_[+ (get "float_upper_name") +], NULL, SF_FALSE) ;
155 test_write_[+ (get "float_type_name") +]_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
156 sf_close (file) ;
157
158 memset (&buffer_in, 0, sizeof (buffer_in)) ;
159
160 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
161
162 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
163
164 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
165 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
166 exit (1) ;
167 } ;
168
169 if (sfinfo.frames != BUFFER_SIZE)
170 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
171 exit (1) ;
172 } ;
173
174 if (sfinfo.channels != 1)
175 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
176 exit (1) ;
177 } ;
178
179 check_log_buffer_or_die (file, __LINE__) ;
180
181 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
182 sf_command (file, SFC_SET_NORM_[+ (get "float_upper_name") +], NULL, SF_FALSE) ;
183 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
184 sf_close (file) ;
185
186 /* Check normalized version. */
187 clip_max_diff = 0.0 ;
188 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
189 { if (fabs (data_in [k]) > 1.0)
190 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
191 exit (1) ;
192 } ;
193
194 if (data_out [k] * data_in [k] < 0.0)
195 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
196 exit (1) ;
197 } ;
198
199 if (fabs (data_out [k]) > 1.0)
200 continue ;
201
202 diff = fabs (data_out [k] - data_in [k]) ;
203 if (diff > clip_max_diff)
204 clip_max_diff = diff ;
205 } ;
206
207 if (clip_max_diff < 1e-20)
208 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
209 exit (1) ;
210 } ;
211
212 if (clip_max_diff > [+ (get "error_val") +])
213 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
214 exit (1) ;
215 } ;
216
217 /* Check the un-normalized data. */
218 clip_max_diff = 0.0 ;
219 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
220 { if (fabs (data_in [k]) > maxval)
221 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
222 exit (1) ;
223 } ;
224
225 if (data_out [k] * data_in [k] < 0.0)
226 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
227 exit (1) ;
228 } ;
229
230 if (fabs (data_out [k]) > maxval)
231 continue ;
232
233 diff = fabs (data_out [k] - data_in [k]) ;
234 if (diff > clip_max_diff)
235 clip_max_diff = diff ;
236 } ;
237
238 if (clip_max_diff < 1e-20)
239 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
240 exit (1) ;
241 } ;
242
243 if (clip_max_diff > 1.0)
244 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
245 exit (1) ;
246 } ;
247
248 printf ("ok\n") ;
249 unlink (filename) ;
250 } /* [+ (get "float_short_name") +]_scale_clip_test_[+ (get "name") +] */
251
252 [+ ENDFOR data_type
253 +]
254 [+ ENDFOR float_type +]
255
256 /*==============================================================================
257 */
258
259 [+ FOR float_type +]
260 [+ FOR int_type
261 +]static void [+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test (const char *filename, int filetype)
262 { SNDFILE *file ;
263 SF_INFO sfinfo ;
264 [+ (get "float_type_name") +] *data_out ;
265 [+ (get "int_type_name") +] *data_in, max_value ;
266 int k ;
267
268 print_test_name ("[+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test", filename) ;
269
270 data_out = buffer_out.[+ (get "float_short_name") +] ;
271 data_in = buffer_in.[+ (get "int_short_name") +] ;
272
273 for (k = 0 ; k < BUFFER_SIZE ; k++)
274 data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
275 data_out [BUFFER_SIZE / 8] = 1.0 ;
276 data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
277 data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
278 data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
279
280 memset (&sfinfo, 0, sizeof (sfinfo)) ;
281 sfinfo.samplerate = 44100 ;
282 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
283 sfinfo.channels = 1 ;
284 sfinfo.format = filetype ;
285
286 /* Save unclipped data to the file. */
287 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
288 test_write_[+ (get "float_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
289 sf_close (file) ;
290
291 memset (&sfinfo, 0, sizeof (sfinfo)) ;
292
293 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
294 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
295
296 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
297
298 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
299 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
300 exit (1) ;
301 } ;
302
303 if (sfinfo.frames != BUFFER_SIZE)
304 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
305 exit (1) ;
306 } ;
307
308 if (sfinfo.channels != 1)
309 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
310 exit (1) ;
311 } ;
312
313 check_log_buffer_or_die (file, __LINE__) ;
314
315 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
316 test_read_[+ (get "int_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
317 /*-sf_command (file, SFC_SET_NORM_[+ (get "float_upper_name") +], NULL, SF_FALSE) ;-*/
318 sf_close (file) ;
319
320 /* Check the first half. */
321 max_value = 0 ;
322 for (k = 0 ; k < sfinfo.frames ; k++)
323 { /* Check if data_out has different sign from data_in. */
324 if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
325 { printf ("\n\nLine %d: Data wrap around at index %d/%d (%f -> %d).\n\n", __LINE__, k, BUFFER_SIZE, data_out [k], data_in [k]) ;
326 exit (1) ;
327 } ;
328 max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
329 } ;
330
331 unlink (filename) ;
332 puts ("ok") ;
333 } /* [+ (get "float_short_name") +]_[+ (get "int_type_name") +]_clip_read_test */
334 [+ ENDFOR int_type
335 +][+ ENDFOR float_type +]
336
337 /*==============================================================================
338 */
339
340 [+ FOR int_type +]
341 [+ FOR float_type
342 +]static void [+ (get "int_type_name") +]_[+ (get "float_short_name") +]_scale_write_test (const char *filename, int filetype)
343 { SNDFILE *file ;
344 SF_INFO sfinfo ;
345 [+ (get "int_type_name") +] *data_out ;
346 [+ (get "float_type_name") +] *data_in, max_value ;
347 int k ;
348
349 print_test_name ("[+ (get "int_type_name") +]_[+ (get "float_short_name") +]_clip_write_test", filename) ;
350
351 data_out = buffer_out.[+ (get "int_short_name") +] ;
352 data_in = buffer_in.[+ (get "float_short_name") +] ;
353
354 for (k = 0 ; k < BUFFER_SIZE ; k++)
355 data_out [k] = [+ (get "float_to_int") +] ([+ (get "int_max_value") +] * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
356
357 memset (&sfinfo, 0, sizeof (sfinfo)) ;
358 sfinfo.samplerate = 44100 ;
359 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
360 sfinfo.channels = 1 ;
361 sfinfo.format = filetype ;
362
363 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
364 test_write_[+ (get "int_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
365 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
366 test_write_[+ (get "int_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
367 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
368 test_write_[+ (get "int_type_name") +]_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
369 sf_close (file) ;
370
371 memset (&sfinfo, 0, sizeof (sfinfo)) ;
372
373 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
374
375 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
376
377 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
378 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
379 exit (1) ;
380 } ;
381
382 if (sfinfo.frames != 3 * BUFFER_SIZE)
383 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, 3 * BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
384 exit (1) ;
385 } ;
386
387 if (sfinfo.channels != 1)
388 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
389 exit (1) ;
390 } ;
391
392 check_log_buffer_or_die (file, __LINE__) ;
393
394 /* Check the first section. */
395 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
396
397 max_value = 0.0 ;
398 for (k = 0 ; k < BUFFER_SIZE ; k++)
399 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
400
401 if (max_value < 1000.0)
402 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
403 exit (1) ;
404 } ;
405
406 /* Check the second section. */
407 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
408
409 max_value = 0.0 ;
410 for (k = 0 ; k < BUFFER_SIZE ; k++)
411 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
412
413 if (max_value > 1.0)
414 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
415 exit (1) ;
416 } ;
417
418 /* Check the third section. */
419 test_read_[+ (get "float_type_name") +]_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
420
421 max_value = 0.0 ;
422 for (k = 0 ; k < BUFFER_SIZE ; k++)
423 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
424
425 if (max_value < 1000.0)
426 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
427 exit (1) ;
428 } ;
429
430 sf_close (file) ;
431
432 unlink (filename) ;
433 puts ("ok") ;
434 } /* [+ (get "int_type_name") +]_[+ (get "float_short_name") +]_scale_write_test */
435 [+ ENDFOR float_type
436 +][+ ENDFOR int_type +]
437
438