Examples Makefile.
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / scale_clip_test.c
1 /*
2 ** Copyright (C) 1999-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 "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #include <math.h>
30
31 #include <sndfile.h>
32
33 #include "utils.h"
34
35 #ifndef M_PI
36 #define M_PI 3.14159265358979323846264338
37 #endif
38
39 #define HALF_BUFFER_SIZE (1 << 12)
40 #define BUFFER_SIZE (2 * HALF_BUFFER_SIZE)
41
42 #define SINE_AMP 1.1
43 #define MAX_ERROR 0.0202
44
45
46 static void flt_scale_clip_test_16 (const char *filename, int filetype, float maxval) ;
47 static void flt_scale_clip_test_24 (const char *filename, int filetype, float maxval) ;
48 static void flt_scale_clip_test_32 (const char *filename, int filetype, float maxval) ;
49 static void flt_scale_clip_test_08 (const char *filename, int filetype, float maxval) ;
50
51 static void dbl_scale_clip_test_16 (const char *filename, int filetype, float maxval) ;
52 static void dbl_scale_clip_test_24 (const char *filename, int filetype, float maxval) ;
53 static void dbl_scale_clip_test_32 (const char *filename, int filetype, float maxval) ;
54 static void dbl_scale_clip_test_08 (const char *filename, int filetype, float maxval) ;
55
56
57
58 static void flt_short_clip_read_test (const char *filename, int filetype) ;
59 static void flt_int_clip_read_test (const char *filename, int filetype) ;
60
61 static void dbl_short_clip_read_test (const char *filename, int filetype) ;
62 static void dbl_int_clip_read_test (const char *filename, int filetype) ;
63
64
65
66 static void short_flt_scale_write_test (const char *filename, int filetype) ;
67 static void short_dbl_scale_write_test (const char *filename, int filetype) ;
68
69 static void int_flt_scale_write_test (const char *filename, int filetype) ;
70 static void int_dbl_scale_write_test (const char *filename, int filetype) ;
71
72
73 typedef union
74 { double dbl [BUFFER_SIZE] ;
75 float flt [BUFFER_SIZE] ;
76 int i [BUFFER_SIZE] ;
77 short s [BUFFER_SIZE] ;
78 } BUFFER ;
79
80 /* Data buffer. */
81 static BUFFER buffer_out ;
82 static BUFFER buffer_in ;
83
84 int
85 main (void)
86 {
87 flt_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
88 flt_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
89
90 dbl_scale_clip_test_08 ("scale_clip_s8.au", SF_FORMAT_AU | SF_FORMAT_PCM_S8, 1.0 * 0x80) ;
91 dbl_scale_clip_test_08 ("scale_clip_u8.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8, 1.0 * 0x80) ;
92
93 /*
94 ** Now use SF_FORMAT_AU where possible because it allows both
95 ** big and little endian files.
96 */
97
98 flt_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
99 flt_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
100 flt_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
101 flt_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
102 flt_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
103 flt_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
104
105 dbl_scale_clip_test_16 ("scale_clip_be16.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
106 dbl_scale_clip_test_16 ("scale_clip_le16.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, 1.0 * 0x8000) ;
107 dbl_scale_clip_test_24 ("scale_clip_be24.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
108 dbl_scale_clip_test_24 ("scale_clip_le24.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, 1.0 * 0x800000) ;
109 dbl_scale_clip_test_32 ("scale_clip_be32.au", SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
110 dbl_scale_clip_test_32 ("scale_clip_le32.au", SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, 1.0 * 0x80000000) ;
111
112 flt_short_clip_read_test ("flt_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
113 flt_int_clip_read_test ("flt_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
114 dbl_short_clip_read_test ("dbl_short.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
115 dbl_int_clip_read_test ("dbl_int.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
116
117 short_flt_scale_write_test ("short_flt.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
118 int_flt_scale_write_test ("int_flt.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
119 short_dbl_scale_write_test ("short_dbl.au" , SF_ENDIAN_BIG | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
120 int_dbl_scale_write_test ("int_dbl.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE) ;
121
122 return 0 ;
123 } /* main */
124
125 /*============================================================================================
126 ** Here are the test functions.
127 */
128
129
130 static void
131 flt_scale_clip_test_16 (const char *filename, int filetype, float maxval)
132 { SNDFILE *file ;
133 SF_INFO sfinfo ;
134 int k ;
135 float *data_out, *data_in ;
136 double diff, clip_max_diff ;
137
138 print_test_name ("flt_scale_clip_test_16", filename) ;
139
140 data_out = buffer_out.flt ;
141 data_in = buffer_in.flt ;
142
143 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
144 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
145 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
146 } ;
147
148 sfinfo.samplerate = 44100 ;
149 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
150 sfinfo.channels = 1 ;
151 sfinfo.format = filetype ;
152
153 /*
154 ** Write two versions of the data:
155 ** normalized and clipped
156 ** un-normalized and clipped.
157 */
158
159 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
160 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
161 test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
162 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
163 test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
164 sf_close (file) ;
165
166 memset (&buffer_in, 0, sizeof (buffer_in)) ;
167
168 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
169
170 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
171
172 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
173 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
174 exit (1) ;
175 } ;
176
177 if (sfinfo.frames != BUFFER_SIZE)
178 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
179 exit (1) ;
180 } ;
181
182 if (sfinfo.channels != 1)
183 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
184 exit (1) ;
185 } ;
186
187 check_log_buffer_or_die (file, __LINE__) ;
188
189 test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
190 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
191 test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
192 sf_close (file) ;
193
194 /* Check normalized version. */
195 clip_max_diff = 0.0 ;
196 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
197 { if (fabs (data_in [k]) > 1.0)
198 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
199 exit (1) ;
200 } ;
201
202 if (data_out [k] * data_in [k] < 0.0)
203 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
204 exit (1) ;
205 } ;
206
207 if (fabs (data_out [k]) > 1.0)
208 continue ;
209
210 diff = fabs (data_out [k] - data_in [k]) ;
211 if (diff > clip_max_diff)
212 clip_max_diff = diff ;
213 } ;
214
215 if (clip_max_diff < 1e-20)
216 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
217 exit (1) ;
218 } ;
219
220 if (clip_max_diff > 1.0 / 0x8000)
221 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
222 exit (1) ;
223 } ;
224
225 /* Check the un-normalized data. */
226 clip_max_diff = 0.0 ;
227 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
228 { if (fabs (data_in [k]) > maxval)
229 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
230 exit (1) ;
231 } ;
232
233 if (data_out [k] * data_in [k] < 0.0)
234 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
235 exit (1) ;
236 } ;
237
238 if (fabs (data_out [k]) > maxval)
239 continue ;
240
241 diff = fabs (data_out [k] - data_in [k]) ;
242 if (diff > clip_max_diff)
243 clip_max_diff = diff ;
244 } ;
245
246 if (clip_max_diff < 1e-20)
247 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
248 exit (1) ;
249 } ;
250
251 if (clip_max_diff > 1.0)
252 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
253 exit (1) ;
254 } ;
255
256 printf ("ok\n") ;
257 unlink (filename) ;
258 } /* flt_scale_clip_test_16 */
259
260 static void
261 flt_scale_clip_test_24 (const char *filename, int filetype, float maxval)
262 { SNDFILE *file ;
263 SF_INFO sfinfo ;
264 int k ;
265 float *data_out, *data_in ;
266 double diff, clip_max_diff ;
267
268 print_test_name ("flt_scale_clip_test_24", filename) ;
269
270 data_out = buffer_out.flt ;
271 data_in = buffer_in.flt ;
272
273 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
274 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
275 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
276 } ;
277
278 sfinfo.samplerate = 44100 ;
279 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
280 sfinfo.channels = 1 ;
281 sfinfo.format = filetype ;
282
283 /*
284 ** Write two versions of the data:
285 ** normalized and clipped
286 ** un-normalized and clipped.
287 */
288
289 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
290 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
291 test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
292 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
293 test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
294 sf_close (file) ;
295
296 memset (&buffer_in, 0, sizeof (buffer_in)) ;
297
298 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
299
300 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
301
302 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
303 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
304 exit (1) ;
305 } ;
306
307 if (sfinfo.frames != BUFFER_SIZE)
308 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
309 exit (1) ;
310 } ;
311
312 if (sfinfo.channels != 1)
313 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
314 exit (1) ;
315 } ;
316
317 check_log_buffer_or_die (file, __LINE__) ;
318
319 test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
320 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
321 test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
322 sf_close (file) ;
323
324 /* Check normalized version. */
325 clip_max_diff = 0.0 ;
326 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
327 { if (fabs (data_in [k]) > 1.0)
328 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
329 exit (1) ;
330 } ;
331
332 if (data_out [k] * data_in [k] < 0.0)
333 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
334 exit (1) ;
335 } ;
336
337 if (fabs (data_out [k]) > 1.0)
338 continue ;
339
340 diff = fabs (data_out [k] - data_in [k]) ;
341 if (diff > clip_max_diff)
342 clip_max_diff = diff ;
343 } ;
344
345 if (clip_max_diff < 1e-20)
346 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
347 exit (1) ;
348 } ;
349
350 if (clip_max_diff > 1.0 / 0x800000)
351 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
352 exit (1) ;
353 } ;
354
355 /* Check the un-normalized data. */
356 clip_max_diff = 0.0 ;
357 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
358 { if (fabs (data_in [k]) > maxval)
359 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
360 exit (1) ;
361 } ;
362
363 if (data_out [k] * data_in [k] < 0.0)
364 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
365 exit (1) ;
366 } ;
367
368 if (fabs (data_out [k]) > maxval)
369 continue ;
370
371 diff = fabs (data_out [k] - data_in [k]) ;
372 if (diff > clip_max_diff)
373 clip_max_diff = diff ;
374 } ;
375
376 if (clip_max_diff < 1e-20)
377 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
378 exit (1) ;
379 } ;
380
381 if (clip_max_diff > 1.0)
382 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
383 exit (1) ;
384 } ;
385
386 printf ("ok\n") ;
387 unlink (filename) ;
388 } /* flt_scale_clip_test_24 */
389
390 static void
391 flt_scale_clip_test_32 (const char *filename, int filetype, float maxval)
392 { SNDFILE *file ;
393 SF_INFO sfinfo ;
394 int k ;
395 float *data_out, *data_in ;
396 double diff, clip_max_diff ;
397
398 print_test_name ("flt_scale_clip_test_32", filename) ;
399
400 data_out = buffer_out.flt ;
401 data_in = buffer_in.flt ;
402
403 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
404 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
405 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
406 } ;
407
408 sfinfo.samplerate = 44100 ;
409 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
410 sfinfo.channels = 1 ;
411 sfinfo.format = filetype ;
412
413 /*
414 ** Write two versions of the data:
415 ** normalized and clipped
416 ** un-normalized and clipped.
417 */
418
419 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
420 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
421 test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
422 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
423 test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
424 sf_close (file) ;
425
426 memset (&buffer_in, 0, sizeof (buffer_in)) ;
427
428 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
429
430 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
431
432 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
433 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
434 exit (1) ;
435 } ;
436
437 if (sfinfo.frames != BUFFER_SIZE)
438 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
439 exit (1) ;
440 } ;
441
442 if (sfinfo.channels != 1)
443 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
444 exit (1) ;
445 } ;
446
447 check_log_buffer_or_die (file, __LINE__) ;
448
449 test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
450 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
451 test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
452 sf_close (file) ;
453
454 /* Check normalized version. */
455 clip_max_diff = 0.0 ;
456 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
457 { if (fabs (data_in [k]) > 1.0)
458 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
459 exit (1) ;
460 } ;
461
462 if (data_out [k] * data_in [k] < 0.0)
463 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
464 exit (1) ;
465 } ;
466
467 if (fabs (data_out [k]) > 1.0)
468 continue ;
469
470 diff = fabs (data_out [k] - data_in [k]) ;
471 if (diff > clip_max_diff)
472 clip_max_diff = diff ;
473 } ;
474
475 if (clip_max_diff < 1e-20)
476 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
477 exit (1) ;
478 } ;
479
480 if (clip_max_diff > 1.0 / 0x80000000)
481 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
482 exit (1) ;
483 } ;
484
485 /* Check the un-normalized data. */
486 clip_max_diff = 0.0 ;
487 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
488 { if (fabs (data_in [k]) > maxval)
489 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
490 exit (1) ;
491 } ;
492
493 if (data_out [k] * data_in [k] < 0.0)
494 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
495 exit (1) ;
496 } ;
497
498 if (fabs (data_out [k]) > maxval)
499 continue ;
500
501 diff = fabs (data_out [k] - data_in [k]) ;
502 if (diff > clip_max_diff)
503 clip_max_diff = diff ;
504 } ;
505
506 if (clip_max_diff < 1e-20)
507 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
508 exit (1) ;
509 } ;
510
511 if (clip_max_diff > 1.0)
512 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
513 exit (1) ;
514 } ;
515
516 printf ("ok\n") ;
517 unlink (filename) ;
518 } /* flt_scale_clip_test_32 */
519
520 static void
521 flt_scale_clip_test_08 (const char *filename, int filetype, float maxval)
522 { SNDFILE *file ;
523 SF_INFO sfinfo ;
524 int k ;
525 float *data_out, *data_in ;
526 double diff, clip_max_diff ;
527
528 print_test_name ("flt_scale_clip_test_08", filename) ;
529
530 data_out = buffer_out.flt ;
531 data_in = buffer_in.flt ;
532
533 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
534 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
535 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
536 } ;
537
538 sfinfo.samplerate = 44100 ;
539 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
540 sfinfo.channels = 1 ;
541 sfinfo.format = filetype ;
542
543 /*
544 ** Write two versions of the data:
545 ** normalized and clipped
546 ** un-normalized and clipped.
547 */
548
549 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
550 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
551 test_write_float_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
552 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
553 test_write_float_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
554 sf_close (file) ;
555
556 memset (&buffer_in, 0, sizeof (buffer_in)) ;
557
558 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
559
560 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
561
562 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
563 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
564 exit (1) ;
565 } ;
566
567 if (sfinfo.frames != BUFFER_SIZE)
568 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
569 exit (1) ;
570 } ;
571
572 if (sfinfo.channels != 1)
573 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
574 exit (1) ;
575 } ;
576
577 check_log_buffer_or_die (file, __LINE__) ;
578
579 test_read_float_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
580 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
581 test_read_float_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
582 sf_close (file) ;
583
584 /* Check normalized version. */
585 clip_max_diff = 0.0 ;
586 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
587 { if (fabs (data_in [k]) > 1.0)
588 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
589 exit (1) ;
590 } ;
591
592 if (data_out [k] * data_in [k] < 0.0)
593 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
594 exit (1) ;
595 } ;
596
597 if (fabs (data_out [k]) > 1.0)
598 continue ;
599
600 diff = fabs (data_out [k] - data_in [k]) ;
601 if (diff > clip_max_diff)
602 clip_max_diff = diff ;
603 } ;
604
605 if (clip_max_diff < 1e-20)
606 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
607 exit (1) ;
608 } ;
609
610 if (clip_max_diff > 1.0 / 0x80)
611 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
612 exit (1) ;
613 } ;
614
615 /* Check the un-normalized data. */
616 clip_max_diff = 0.0 ;
617 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
618 { if (fabs (data_in [k]) > maxval)
619 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
620 exit (1) ;
621 } ;
622
623 if (data_out [k] * data_in [k] < 0.0)
624 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
625 exit (1) ;
626 } ;
627
628 if (fabs (data_out [k]) > maxval)
629 continue ;
630
631 diff = fabs (data_out [k] - data_in [k]) ;
632 if (diff > clip_max_diff)
633 clip_max_diff = diff ;
634 } ;
635
636 if (clip_max_diff < 1e-20)
637 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
638 exit (1) ;
639 } ;
640
641 if (clip_max_diff > 1.0)
642 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
643 exit (1) ;
644 } ;
645
646 printf ("ok\n") ;
647 unlink (filename) ;
648 } /* flt_scale_clip_test_08 */
649
650
651
652 static void
653 dbl_scale_clip_test_16 (const char *filename, int filetype, float maxval)
654 { SNDFILE *file ;
655 SF_INFO sfinfo ;
656 int k ;
657 double *data_out, *data_in ;
658 double diff, clip_max_diff ;
659
660 print_test_name ("dbl_scale_clip_test_16", filename) ;
661
662 data_out = buffer_out.dbl ;
663 data_in = buffer_in.dbl ;
664
665 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
666 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
667 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
668 } ;
669
670 sfinfo.samplerate = 44100 ;
671 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
672 sfinfo.channels = 1 ;
673 sfinfo.format = filetype ;
674
675 /*
676 ** Write two versions of the data:
677 ** normalized and clipped
678 ** un-normalized and clipped.
679 */
680
681 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
682 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
683 test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
684 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
685 test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
686 sf_close (file) ;
687
688 memset (&buffer_in, 0, sizeof (buffer_in)) ;
689
690 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
691
692 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
693
694 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
695 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
696 exit (1) ;
697 } ;
698
699 if (sfinfo.frames != BUFFER_SIZE)
700 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
701 exit (1) ;
702 } ;
703
704 if (sfinfo.channels != 1)
705 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
706 exit (1) ;
707 } ;
708
709 check_log_buffer_or_die (file, __LINE__) ;
710
711 test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
712 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
713 test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
714 sf_close (file) ;
715
716 /* Check normalized version. */
717 clip_max_diff = 0.0 ;
718 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
719 { if (fabs (data_in [k]) > 1.0)
720 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
721 exit (1) ;
722 } ;
723
724 if (data_out [k] * data_in [k] < 0.0)
725 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
726 exit (1) ;
727 } ;
728
729 if (fabs (data_out [k]) > 1.0)
730 continue ;
731
732 diff = fabs (data_out [k] - data_in [k]) ;
733 if (diff > clip_max_diff)
734 clip_max_diff = diff ;
735 } ;
736
737 if (clip_max_diff < 1e-20)
738 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
739 exit (1) ;
740 } ;
741
742 if (clip_max_diff > 1.0 / 0x8000)
743 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
744 exit (1) ;
745 } ;
746
747 /* Check the un-normalized data. */
748 clip_max_diff = 0.0 ;
749 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
750 { if (fabs (data_in [k]) > maxval)
751 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
752 exit (1) ;
753 } ;
754
755 if (data_out [k] * data_in [k] < 0.0)
756 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
757 exit (1) ;
758 } ;
759
760 if (fabs (data_out [k]) > maxval)
761 continue ;
762
763 diff = fabs (data_out [k] - data_in [k]) ;
764 if (diff > clip_max_diff)
765 clip_max_diff = diff ;
766 } ;
767
768 if (clip_max_diff < 1e-20)
769 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
770 exit (1) ;
771 } ;
772
773 if (clip_max_diff > 1.0)
774 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
775 exit (1) ;
776 } ;
777
778 printf ("ok\n") ;
779 unlink (filename) ;
780 } /* dbl_scale_clip_test_16 */
781
782 static void
783 dbl_scale_clip_test_24 (const char *filename, int filetype, float maxval)
784 { SNDFILE *file ;
785 SF_INFO sfinfo ;
786 int k ;
787 double *data_out, *data_in ;
788 double diff, clip_max_diff ;
789
790 print_test_name ("dbl_scale_clip_test_24", filename) ;
791
792 data_out = buffer_out.dbl ;
793 data_in = buffer_in.dbl ;
794
795 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
796 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
797 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
798 } ;
799
800 sfinfo.samplerate = 44100 ;
801 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
802 sfinfo.channels = 1 ;
803 sfinfo.format = filetype ;
804
805 /*
806 ** Write two versions of the data:
807 ** normalized and clipped
808 ** un-normalized and clipped.
809 */
810
811 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
812 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
813 test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
814 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
815 test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
816 sf_close (file) ;
817
818 memset (&buffer_in, 0, sizeof (buffer_in)) ;
819
820 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
821
822 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
823
824 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
825 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
826 exit (1) ;
827 } ;
828
829 if (sfinfo.frames != BUFFER_SIZE)
830 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
831 exit (1) ;
832 } ;
833
834 if (sfinfo.channels != 1)
835 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
836 exit (1) ;
837 } ;
838
839 check_log_buffer_or_die (file, __LINE__) ;
840
841 test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
842 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
843 test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
844 sf_close (file) ;
845
846 /* Check normalized version. */
847 clip_max_diff = 0.0 ;
848 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
849 { if (fabs (data_in [k]) > 1.0)
850 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
851 exit (1) ;
852 } ;
853
854 if (data_out [k] * data_in [k] < 0.0)
855 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
856 exit (1) ;
857 } ;
858
859 if (fabs (data_out [k]) > 1.0)
860 continue ;
861
862 diff = fabs (data_out [k] - data_in [k]) ;
863 if (diff > clip_max_diff)
864 clip_max_diff = diff ;
865 } ;
866
867 if (clip_max_diff < 1e-20)
868 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
869 exit (1) ;
870 } ;
871
872 if (clip_max_diff > 1.0 / 0x800000)
873 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
874 exit (1) ;
875 } ;
876
877 /* Check the un-normalized data. */
878 clip_max_diff = 0.0 ;
879 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
880 { if (fabs (data_in [k]) > maxval)
881 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
882 exit (1) ;
883 } ;
884
885 if (data_out [k] * data_in [k] < 0.0)
886 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
887 exit (1) ;
888 } ;
889
890 if (fabs (data_out [k]) > maxval)
891 continue ;
892
893 diff = fabs (data_out [k] - data_in [k]) ;
894 if (diff > clip_max_diff)
895 clip_max_diff = diff ;
896 } ;
897
898 if (clip_max_diff < 1e-20)
899 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
900 exit (1) ;
901 } ;
902
903 if (clip_max_diff > 1.0)
904 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
905 exit (1) ;
906 } ;
907
908 printf ("ok\n") ;
909 unlink (filename) ;
910 } /* dbl_scale_clip_test_24 */
911
912 static void
913 dbl_scale_clip_test_32 (const char *filename, int filetype, float maxval)
914 { SNDFILE *file ;
915 SF_INFO sfinfo ;
916 int k ;
917 double *data_out, *data_in ;
918 double diff, clip_max_diff ;
919
920 print_test_name ("dbl_scale_clip_test_32", filename) ;
921
922 data_out = buffer_out.dbl ;
923 data_in = buffer_in.dbl ;
924
925 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
926 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
927 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
928 } ;
929
930 sfinfo.samplerate = 44100 ;
931 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
932 sfinfo.channels = 1 ;
933 sfinfo.format = filetype ;
934
935 /*
936 ** Write two versions of the data:
937 ** normalized and clipped
938 ** un-normalized and clipped.
939 */
940
941 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
942 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
943 test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
944 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
945 test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
946 sf_close (file) ;
947
948 memset (&buffer_in, 0, sizeof (buffer_in)) ;
949
950 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
951
952 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
953
954 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
955 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
956 exit (1) ;
957 } ;
958
959 if (sfinfo.frames != BUFFER_SIZE)
960 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
961 exit (1) ;
962 } ;
963
964 if (sfinfo.channels != 1)
965 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
966 exit (1) ;
967 } ;
968
969 check_log_buffer_or_die (file, __LINE__) ;
970
971 test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
972 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
973 test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
974 sf_close (file) ;
975
976 /* Check normalized version. */
977 clip_max_diff = 0.0 ;
978 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
979 { if (fabs (data_in [k]) > 1.0)
980 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
981 exit (1) ;
982 } ;
983
984 if (data_out [k] * data_in [k] < 0.0)
985 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
986 exit (1) ;
987 } ;
988
989 if (fabs (data_out [k]) > 1.0)
990 continue ;
991
992 diff = fabs (data_out [k] - data_in [k]) ;
993 if (diff > clip_max_diff)
994 clip_max_diff = diff ;
995 } ;
996
997 if (clip_max_diff < 1e-20)
998 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
999 exit (1) ;
1000 } ;
1001
1002 if (clip_max_diff > 1.0 / 0x80000000)
1003 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
1004 exit (1) ;
1005 } ;
1006
1007 /* Check the un-normalized data. */
1008 clip_max_diff = 0.0 ;
1009 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
1010 { if (fabs (data_in [k]) > maxval)
1011 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
1012 exit (1) ;
1013 } ;
1014
1015 if (data_out [k] * data_in [k] < 0.0)
1016 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
1017 exit (1) ;
1018 } ;
1019
1020 if (fabs (data_out [k]) > maxval)
1021 continue ;
1022
1023 diff = fabs (data_out [k] - data_in [k]) ;
1024 if (diff > clip_max_diff)
1025 clip_max_diff = diff ;
1026 } ;
1027
1028 if (clip_max_diff < 1e-20)
1029 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
1030 exit (1) ;
1031 } ;
1032
1033 if (clip_max_diff > 1.0)
1034 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
1035 exit (1) ;
1036 } ;
1037
1038 printf ("ok\n") ;
1039 unlink (filename) ;
1040 } /* dbl_scale_clip_test_32 */
1041
1042 static void
1043 dbl_scale_clip_test_08 (const char *filename, int filetype, float maxval)
1044 { SNDFILE *file ;
1045 SF_INFO sfinfo ;
1046 int k ;
1047 double *data_out, *data_in ;
1048 double diff, clip_max_diff ;
1049
1050 print_test_name ("dbl_scale_clip_test_08", filename) ;
1051
1052 data_out = buffer_out.dbl ;
1053 data_in = buffer_in.dbl ;
1054
1055 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
1056 { data_out [k] = 1.2 * sin (2 * M_PI * k / HALF_BUFFER_SIZE) ;
1057 data_out [k + HALF_BUFFER_SIZE] = data_out [k] * maxval ;
1058 } ;
1059
1060 sfinfo.samplerate = 44100 ;
1061 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1062 sfinfo.channels = 1 ;
1063 sfinfo.format = filetype ;
1064
1065 /*
1066 ** Write two versions of the data:
1067 ** normalized and clipped
1068 ** un-normalized and clipped.
1069 */
1070
1071 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
1072 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
1073 test_write_double_or_die (file, 0, data_out, HALF_BUFFER_SIZE, __LINE__) ;
1074 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
1075 test_write_double_or_die (file, 0, data_out + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
1076 sf_close (file) ;
1077
1078 memset (&buffer_in, 0, sizeof (buffer_in)) ;
1079
1080 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
1081
1082 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
1083
1084 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
1085 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
1086 exit (1) ;
1087 } ;
1088
1089 if (sfinfo.frames != BUFFER_SIZE)
1090 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
1091 exit (1) ;
1092 } ;
1093
1094 if (sfinfo.channels != 1)
1095 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
1096 exit (1) ;
1097 } ;
1098
1099 check_log_buffer_or_die (file, __LINE__) ;
1100
1101 test_read_double_or_die (file, 0, data_in, HALF_BUFFER_SIZE, __LINE__) ;
1102 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
1103 test_read_double_or_die (file, 0, data_in + HALF_BUFFER_SIZE, HALF_BUFFER_SIZE, __LINE__) ;
1104 sf_close (file) ;
1105
1106 /* Check normalized version. */
1107 clip_max_diff = 0.0 ;
1108 for (k = 0 ; k < HALF_BUFFER_SIZE ; k++)
1109 { if (fabs (data_in [k]) > 1.0)
1110 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
1111 exit (1) ;
1112 } ;
1113
1114 if (data_out [k] * data_in [k] < 0.0)
1115 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
1116 exit (1) ;
1117 } ;
1118
1119 if (fabs (data_out [k]) > 1.0)
1120 continue ;
1121
1122 diff = fabs (data_out [k] - data_in [k]) ;
1123 if (diff > clip_max_diff)
1124 clip_max_diff = diff ;
1125 } ;
1126
1127 if (clip_max_diff < 1e-20)
1128 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__, clip_max_diff) ;
1129 exit (1) ;
1130 } ;
1131
1132 if (clip_max_diff > 1.0 / 0x80)
1133 { printf ("\n\nLine %d: Clipping difference (%e) too large (normalized).\n\n", __LINE__, clip_max_diff) ;
1134 exit (1) ;
1135 } ;
1136
1137 /* Check the un-normalized data. */
1138 clip_max_diff = 0.0 ;
1139 for (k = HALF_BUFFER_SIZE ; k < BUFFER_SIZE ; k++)
1140 { if (fabs (data_in [k]) > maxval)
1141 { printf ("\n\nLine %d: Input sample %d/%d (%f) has not been clipped.\n\n", __LINE__, k, BUFFER_SIZE, data_in [k]) ;
1142 exit (1) ;
1143 } ;
1144
1145 if (data_out [k] * data_in [k] < 0.0)
1146 { printf ("\n\nLine %d: Data wrap around at index %d/%d.\n\n", __LINE__, k, BUFFER_SIZE) ;
1147 exit (1) ;
1148 } ;
1149
1150 if (fabs (data_out [k]) > maxval)
1151 continue ;
1152
1153 diff = fabs (data_out [k] - data_in [k]) ;
1154 if (diff > clip_max_diff)
1155 clip_max_diff = diff ;
1156 } ;
1157
1158 if (clip_max_diff < 1e-20)
1159 { printf ("\n\nLine %d: Clipping difference (%e) too small (un-normalized).\n\n", __LINE__, clip_max_diff) ;
1160 exit (1) ;
1161 } ;
1162
1163 if (clip_max_diff > 1.0)
1164 { printf ("\n\nLine %d: Clipping difference (%e) too large (un-normalised).\n\n", __LINE__, clip_max_diff) ;
1165 exit (1) ;
1166 } ;
1167
1168 printf ("ok\n") ;
1169 unlink (filename) ;
1170 } /* dbl_scale_clip_test_08 */
1171
1172
1173
1174
1175 /*==============================================================================
1176 */
1177
1178
1179 static void flt_short_clip_read_test (const char *filename, int filetype)
1180 { SNDFILE *file ;
1181 SF_INFO sfinfo ;
1182 float *data_out ;
1183 short *data_in, max_value ;
1184 int k ;
1185
1186 print_test_name ("flt_short_clip_read_test", filename) ;
1187
1188 data_out = buffer_out.flt ;
1189 data_in = buffer_in.s ;
1190
1191 for (k = 0 ; k < BUFFER_SIZE ; k++)
1192 data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
1193 data_out [BUFFER_SIZE / 8] = 1.0 ;
1194 data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
1195 data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
1196 data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
1197
1198 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1199 sfinfo.samplerate = 44100 ;
1200 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1201 sfinfo.channels = 1 ;
1202 sfinfo.format = filetype ;
1203
1204 /* Save unclipped data to the file. */
1205 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
1206 test_write_float_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1207 sf_close (file) ;
1208
1209 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1210
1211 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
1212 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
1213
1214 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
1215
1216 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
1217 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
1218 exit (1) ;
1219 } ;
1220
1221 if (sfinfo.frames != BUFFER_SIZE)
1222 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
1223 exit (1) ;
1224 } ;
1225
1226 if (sfinfo.channels != 1)
1227 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
1228 exit (1) ;
1229 } ;
1230
1231 check_log_buffer_or_die (file, __LINE__) ;
1232
1233 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
1234 test_read_short_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1235 /*-sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;-*/
1236 sf_close (file) ;
1237
1238 /* Check the first half. */
1239 max_value = 0 ;
1240 for (k = 0 ; k < sfinfo.frames ; k++)
1241 { /* Check if data_out has different sign from data_in. */
1242 if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
1243 { 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]) ;
1244 exit (1) ;
1245 } ;
1246 max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
1247 } ;
1248
1249 unlink (filename) ;
1250 puts ("ok") ;
1251 } /* flt_short_clip_read_test */
1252 static void flt_int_clip_read_test (const char *filename, int filetype)
1253 { SNDFILE *file ;
1254 SF_INFO sfinfo ;
1255 float *data_out ;
1256 int *data_in, max_value ;
1257 int k ;
1258
1259 print_test_name ("flt_int_clip_read_test", filename) ;
1260
1261 data_out = buffer_out.flt ;
1262 data_in = buffer_in.i ;
1263
1264 for (k = 0 ; k < BUFFER_SIZE ; k++)
1265 data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
1266 data_out [BUFFER_SIZE / 8] = 1.0 ;
1267 data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
1268 data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
1269 data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
1270
1271 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1272 sfinfo.samplerate = 44100 ;
1273 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1274 sfinfo.channels = 1 ;
1275 sfinfo.format = filetype ;
1276
1277 /* Save unclipped data to the file. */
1278 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
1279 test_write_float_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1280 sf_close (file) ;
1281
1282 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1283
1284 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
1285 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
1286
1287 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
1288
1289 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
1290 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
1291 exit (1) ;
1292 } ;
1293
1294 if (sfinfo.frames != BUFFER_SIZE)
1295 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
1296 exit (1) ;
1297 } ;
1298
1299 if (sfinfo.channels != 1)
1300 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
1301 exit (1) ;
1302 } ;
1303
1304 check_log_buffer_or_die (file, __LINE__) ;
1305
1306 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
1307 test_read_int_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1308 /*-sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;-*/
1309 sf_close (file) ;
1310
1311 /* Check the first half. */
1312 max_value = 0 ;
1313 for (k = 0 ; k < sfinfo.frames ; k++)
1314 { /* Check if data_out has different sign from data_in. */
1315 if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
1316 { 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]) ;
1317 exit (1) ;
1318 } ;
1319 max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
1320 } ;
1321
1322 unlink (filename) ;
1323 puts ("ok") ;
1324 } /* flt_int_clip_read_test */
1325
1326 static void dbl_short_clip_read_test (const char *filename, int filetype)
1327 { SNDFILE *file ;
1328 SF_INFO sfinfo ;
1329 double *data_out ;
1330 short *data_in, max_value ;
1331 int k ;
1332
1333 print_test_name ("dbl_short_clip_read_test", filename) ;
1334
1335 data_out = buffer_out.dbl ;
1336 data_in = buffer_in.s ;
1337
1338 for (k = 0 ; k < BUFFER_SIZE ; k++)
1339 data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
1340 data_out [BUFFER_SIZE / 8] = 1.0 ;
1341 data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
1342 data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
1343 data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
1344
1345 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1346 sfinfo.samplerate = 44100 ;
1347 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1348 sfinfo.channels = 1 ;
1349 sfinfo.format = filetype ;
1350
1351 /* Save unclipped data to the file. */
1352 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
1353 test_write_double_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1354 sf_close (file) ;
1355
1356 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1357
1358 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
1359 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
1360
1361 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
1362
1363 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
1364 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
1365 exit (1) ;
1366 } ;
1367
1368 if (sfinfo.frames != BUFFER_SIZE)
1369 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
1370 exit (1) ;
1371 } ;
1372
1373 if (sfinfo.channels != 1)
1374 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
1375 exit (1) ;
1376 } ;
1377
1378 check_log_buffer_or_die (file, __LINE__) ;
1379
1380 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
1381 test_read_short_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1382 /*-sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;-*/
1383 sf_close (file) ;
1384
1385 /* Check the first half. */
1386 max_value = 0 ;
1387 for (k = 0 ; k < sfinfo.frames ; k++)
1388 { /* Check if data_out has different sign from data_in. */
1389 if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
1390 { 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]) ;
1391 exit (1) ;
1392 } ;
1393 max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
1394 } ;
1395
1396 unlink (filename) ;
1397 puts ("ok") ;
1398 } /* dbl_short_clip_read_test */
1399 static void dbl_int_clip_read_test (const char *filename, int filetype)
1400 { SNDFILE *file ;
1401 SF_INFO sfinfo ;
1402 double *data_out ;
1403 int *data_in, max_value ;
1404 int k ;
1405
1406 print_test_name ("dbl_int_clip_read_test", filename) ;
1407
1408 data_out = buffer_out.dbl ;
1409 data_in = buffer_in.i ;
1410
1411 for (k = 0 ; k < BUFFER_SIZE ; k++)
1412 data_out [k] = 0.995 * sin (4 * M_PI * k / BUFFER_SIZE) ;
1413 data_out [BUFFER_SIZE / 8] = 1.0 ;
1414 data_out [3 * BUFFER_SIZE / 8] = -1.000000001 ;
1415 data_out [5 * BUFFER_SIZE / 8] = 1.0 ;
1416 data_out [7 * BUFFER_SIZE / 8] = -1.000000001 ;
1417
1418 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1419 sfinfo.samplerate = 44100 ;
1420 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1421 sfinfo.channels = 1 ;
1422 sfinfo.format = filetype ;
1423
1424 /* Save unclipped data to the file. */
1425 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
1426 test_write_double_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1427 sf_close (file) ;
1428
1429 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1430
1431 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
1432 sf_command (file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE) ;
1433
1434 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
1435
1436 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
1437 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
1438 exit (1) ;
1439 } ;
1440
1441 if (sfinfo.frames != BUFFER_SIZE)
1442 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
1443 exit (1) ;
1444 } ;
1445
1446 if (sfinfo.channels != 1)
1447 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
1448 exit (1) ;
1449 } ;
1450
1451 check_log_buffer_or_die (file, __LINE__) ;
1452
1453 sf_command (file, SFC_SET_CLIPPING, NULL, SF_TRUE) ;
1454 test_read_int_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1455 /*-sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;-*/
1456 sf_close (file) ;
1457
1458 /* Check the first half. */
1459 max_value = 0 ;
1460 for (k = 0 ; k < sfinfo.frames ; k++)
1461 { /* Check if data_out has different sign from data_in. */
1462 if ((data_out [k] < 0.0 && data_in [k] > 0) || (data_out [k] > 0.0 && data_in [k] < 0))
1463 { 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]) ;
1464 exit (1) ;
1465 } ;
1466 max_value = (max_value > abs (data_in [k])) ? max_value : abs (data_in [k]) ;
1467 } ;
1468
1469 unlink (filename) ;
1470 puts ("ok") ;
1471 } /* dbl_int_clip_read_test */
1472
1473
1474 /*==============================================================================
1475 */
1476
1477
1478 static void short_flt_scale_write_test (const char *filename, int filetype)
1479 { SNDFILE *file ;
1480 SF_INFO sfinfo ;
1481 short *data_out ;
1482 float *data_in, max_value ;
1483 int k ;
1484
1485 print_test_name ("short_flt_clip_write_test", filename) ;
1486
1487 data_out = buffer_out.s ;
1488 data_in = buffer_in.flt ;
1489
1490 for (k = 0 ; k < BUFFER_SIZE ; k++)
1491 data_out [k] = lrintf (0x7FFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
1492
1493 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1494 sfinfo.samplerate = 44100 ;
1495 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1496 sfinfo.channels = 1 ;
1497 sfinfo.format = filetype ;
1498
1499 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
1500 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1501 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
1502 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1503 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
1504 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1505 sf_close (file) ;
1506
1507 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1508
1509 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
1510
1511 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
1512
1513 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
1514 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
1515 exit (1) ;
1516 } ;
1517
1518 if (sfinfo.frames != 3 * BUFFER_SIZE)
1519 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, 3 * BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
1520 exit (1) ;
1521 } ;
1522
1523 if (sfinfo.channels != 1)
1524 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
1525 exit (1) ;
1526 } ;
1527
1528 check_log_buffer_or_die (file, __LINE__) ;
1529
1530 /* Check the first section. */
1531 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1532
1533 max_value = 0.0 ;
1534 for (k = 0 ; k < BUFFER_SIZE ; k++)
1535 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1536
1537 if (max_value < 1000.0)
1538 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
1539 exit (1) ;
1540 } ;
1541
1542 /* Check the second section. */
1543 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1544
1545 max_value = 0.0 ;
1546 for (k = 0 ; k < BUFFER_SIZE ; k++)
1547 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1548
1549 if (max_value > 1.0)
1550 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
1551 exit (1) ;
1552 } ;
1553
1554 /* Check the third section. */
1555 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1556
1557 max_value = 0.0 ;
1558 for (k = 0 ; k < BUFFER_SIZE ; k++)
1559 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1560
1561 if (max_value < 1000.0)
1562 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
1563 exit (1) ;
1564 } ;
1565
1566 sf_close (file) ;
1567
1568 unlink (filename) ;
1569 puts ("ok") ;
1570 } /* short_flt_scale_write_test */
1571 static void short_dbl_scale_write_test (const char *filename, int filetype)
1572 { SNDFILE *file ;
1573 SF_INFO sfinfo ;
1574 short *data_out ;
1575 double *data_in, max_value ;
1576 int k ;
1577
1578 print_test_name ("short_dbl_clip_write_test", filename) ;
1579
1580 data_out = buffer_out.s ;
1581 data_in = buffer_in.dbl ;
1582
1583 for (k = 0 ; k < BUFFER_SIZE ; k++)
1584 data_out [k] = lrint (0x7FFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
1585
1586 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1587 sfinfo.samplerate = 44100 ;
1588 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1589 sfinfo.channels = 1 ;
1590 sfinfo.format = filetype ;
1591
1592 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
1593 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1594 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
1595 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1596 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
1597 test_write_short_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1598 sf_close (file) ;
1599
1600 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1601
1602 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
1603
1604 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
1605
1606 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
1607 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
1608 exit (1) ;
1609 } ;
1610
1611 if (sfinfo.frames != 3 * BUFFER_SIZE)
1612 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, 3 * BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
1613 exit (1) ;
1614 } ;
1615
1616 if (sfinfo.channels != 1)
1617 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
1618 exit (1) ;
1619 } ;
1620
1621 check_log_buffer_or_die (file, __LINE__) ;
1622
1623 /* Check the first section. */
1624 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1625
1626 max_value = 0.0 ;
1627 for (k = 0 ; k < BUFFER_SIZE ; k++)
1628 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1629
1630 if (max_value < 1000.0)
1631 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
1632 exit (1) ;
1633 } ;
1634
1635 /* Check the second section. */
1636 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1637
1638 max_value = 0.0 ;
1639 for (k = 0 ; k < BUFFER_SIZE ; k++)
1640 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1641
1642 if (max_value > 1.0)
1643 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
1644 exit (1) ;
1645 } ;
1646
1647 /* Check the third section. */
1648 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1649
1650 max_value = 0.0 ;
1651 for (k = 0 ; k < BUFFER_SIZE ; k++)
1652 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1653
1654 if (max_value < 1000.0)
1655 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
1656 exit (1) ;
1657 } ;
1658
1659 sf_close (file) ;
1660
1661 unlink (filename) ;
1662 puts ("ok") ;
1663 } /* short_dbl_scale_write_test */
1664
1665 static void int_flt_scale_write_test (const char *filename, int filetype)
1666 { SNDFILE *file ;
1667 SF_INFO sfinfo ;
1668 int *data_out ;
1669 float *data_in, max_value ;
1670 int k ;
1671
1672 print_test_name ("int_flt_clip_write_test", filename) ;
1673
1674 data_out = buffer_out.i ;
1675 data_in = buffer_in.flt ;
1676
1677 for (k = 0 ; k < BUFFER_SIZE ; k++)
1678 data_out [k] = lrintf (0x7FFFFFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
1679
1680 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1681 sfinfo.samplerate = 44100 ;
1682 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1683 sfinfo.channels = 1 ;
1684 sfinfo.format = filetype ;
1685
1686 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
1687 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1688 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
1689 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1690 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
1691 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1692 sf_close (file) ;
1693
1694 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1695
1696 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
1697
1698 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
1699
1700 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
1701 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
1702 exit (1) ;
1703 } ;
1704
1705 if (sfinfo.frames != 3 * BUFFER_SIZE)
1706 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, 3 * BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
1707 exit (1) ;
1708 } ;
1709
1710 if (sfinfo.channels != 1)
1711 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
1712 exit (1) ;
1713 } ;
1714
1715 check_log_buffer_or_die (file, __LINE__) ;
1716
1717 /* Check the first section. */
1718 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1719
1720 max_value = 0.0 ;
1721 for (k = 0 ; k < BUFFER_SIZE ; k++)
1722 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1723
1724 if (max_value < 1000.0)
1725 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
1726 exit (1) ;
1727 } ;
1728
1729 /* Check the second section. */
1730 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1731
1732 max_value = 0.0 ;
1733 for (k = 0 ; k < BUFFER_SIZE ; k++)
1734 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1735
1736 if (max_value > 1.0)
1737 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
1738 exit (1) ;
1739 } ;
1740
1741 /* Check the third section. */
1742 test_read_float_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1743
1744 max_value = 0.0 ;
1745 for (k = 0 ; k < BUFFER_SIZE ; k++)
1746 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1747
1748 if (max_value < 1000.0)
1749 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
1750 exit (1) ;
1751 } ;
1752
1753 sf_close (file) ;
1754
1755 unlink (filename) ;
1756 puts ("ok") ;
1757 } /* int_flt_scale_write_test */
1758 static void int_dbl_scale_write_test (const char *filename, int filetype)
1759 { SNDFILE *file ;
1760 SF_INFO sfinfo ;
1761 int *data_out ;
1762 double *data_in, max_value ;
1763 int k ;
1764
1765 print_test_name ("int_dbl_clip_write_test", filename) ;
1766
1767 data_out = buffer_out.i ;
1768 data_in = buffer_in.dbl ;
1769
1770 for (k = 0 ; k < BUFFER_SIZE ; k++)
1771 data_out [k] = lrint (0x7FFFFFFF * 0.995 * sin (4 * M_PI * k / BUFFER_SIZE)) ;
1772
1773 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1774 sfinfo.samplerate = 44100 ;
1775 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1776 sfinfo.channels = 1 ;
1777 sfinfo.format = filetype ;
1778
1779 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
1780 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1781 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE) ;
1782 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1783 sf_command (file, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_FALSE) ;
1784 test_write_int_or_die (file, 0, data_out, BUFFER_SIZE, __LINE__) ;
1785 sf_close (file) ;
1786
1787 memset (&sfinfo, 0, sizeof (sfinfo)) ;
1788
1789 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
1790
1791 sfinfo.format &= (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK) ;
1792
1793 if (sfinfo.format != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)))
1794 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n\n", __LINE__, filetype, sfinfo.format) ;
1795 exit (1) ;
1796 } ;
1797
1798 if (sfinfo.frames != 3 * BUFFER_SIZE)
1799 { printf ("\n\nLine %d: Incorrect number of frames in file (%d => %ld).\n\n", __LINE__, 3 * BUFFER_SIZE, SF_COUNT_TO_LONG (sfinfo.frames)) ;
1800 exit (1) ;
1801 } ;
1802
1803 if (sfinfo.channels != 1)
1804 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__) ;
1805 exit (1) ;
1806 } ;
1807
1808 check_log_buffer_or_die (file, __LINE__) ;
1809
1810 /* Check the first section. */
1811 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1812
1813 max_value = 0.0 ;
1814 for (k = 0 ; k < BUFFER_SIZE ; k++)
1815 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1816
1817 if (max_value < 1000.0)
1818 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
1819 exit (1) ;
1820 } ;
1821
1822 /* Check the second section. */
1823 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1824
1825 max_value = 0.0 ;
1826 for (k = 0 ; k < BUFFER_SIZE ; k++)
1827 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1828
1829 if (max_value > 1.0)
1830 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__, max_value) ;
1831 exit (1) ;
1832 } ;
1833
1834 /* Check the third section. */
1835 test_read_double_or_die (file, 0, data_in, BUFFER_SIZE, __LINE__) ;
1836
1837 max_value = 0.0 ;
1838 for (k = 0 ; k < BUFFER_SIZE ; k++)
1839 max_value = (max_value > fabs (data_in [k])) ? max_value : fabs (data_in [k]) ;
1840
1841 if (max_value < 1000.0)
1842 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__, max_value) ;
1843 exit (1) ;
1844 } ;
1845
1846 sf_close (file) ;
1847
1848 unlink (filename) ;
1849 puts ("ok") ;
1850 } /* int_dbl_scale_write_test */
1851
1852
1853