2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
36 #define M_PI 3.14159265358979323846264338
39 #define HALF_BUFFER_SIZE (1 << 12)
40 #define BUFFER_SIZE (2 * HALF_BUFFER_SIZE)
43 #define MAX_ERROR 0.0202
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
) ;
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
) ;
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
) ;
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
) ;
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
) ;
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
) ;
74 { double dbl
[BUFFER_SIZE
] ;
75 float flt
[BUFFER_SIZE
] ;
77 short s
[BUFFER_SIZE
] ;
81 static BUFFER buffer_out
;
82 static BUFFER buffer_in
;
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) ;
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) ;
94 ** Now use SF_FORMAT_AU where possible because it allows both
95 ** big and little endian files.
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) ;
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) ;
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
) ;
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
) ;
125 /*============================================================================================
126 ** Here are the test functions.
131 flt_scale_clip_test_16 (const char *filename
, int filetype
, float maxval
)
135 float *data_out
, *data_in
;
136 double diff
, clip_max_diff
;
138 print_test_name ("flt_scale_clip_test_16", filename
) ;
140 data_out
= buffer_out
.flt
;
141 data_in
= buffer_in
.flt
;
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
;
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
;
154 ** Write two versions of the data:
155 ** normalized and clipped
156 ** un-normalized and clipped.
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__
) ;
166 memset (&buffer_in
, 0, sizeof (buffer_in
)) ;
168 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
170 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
182 if (sfinfo
.channels
!= 1)
183 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
187 check_log_buffer_or_die (file
, __LINE__
) ;
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__
) ;
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
]) ;
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
) ;
207 if (fabs (data_out
[k
]) > 1.0)
210 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
211 if (diff
> clip_max_diff
)
212 clip_max_diff
= diff
;
215 if (clip_max_diff
< 1e-20)
216 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__
, clip_max_diff
) ;
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
) ;
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
]) ;
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
) ;
238 if (fabs (data_out
[k
]) > maxval
)
241 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
242 if (diff
> clip_max_diff
)
243 clip_max_diff
= diff
;
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
) ;
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
) ;
258 } /* flt_scale_clip_test_16 */
261 flt_scale_clip_test_24 (const char *filename
, int filetype
, float maxval
)
265 float *data_out
, *data_in
;
266 double diff
, clip_max_diff
;
268 print_test_name ("flt_scale_clip_test_24", filename
) ;
270 data_out
= buffer_out
.flt
;
271 data_in
= buffer_in
.flt
;
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
;
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
;
284 ** Write two versions of the data:
285 ** normalized and clipped
286 ** un-normalized and clipped.
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__
) ;
296 memset (&buffer_in
, 0, sizeof (buffer_in
)) ;
298 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
300 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
312 if (sfinfo
.channels
!= 1)
313 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
317 check_log_buffer_or_die (file
, __LINE__
) ;
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__
) ;
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
]) ;
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
) ;
337 if (fabs (data_out
[k
]) > 1.0)
340 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
341 if (diff
> clip_max_diff
)
342 clip_max_diff
= diff
;
345 if (clip_max_diff
< 1e-20)
346 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__
, clip_max_diff
) ;
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
) ;
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
]) ;
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
) ;
368 if (fabs (data_out
[k
]) > maxval
)
371 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
372 if (diff
> clip_max_diff
)
373 clip_max_diff
= diff
;
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
) ;
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
) ;
388 } /* flt_scale_clip_test_24 */
391 flt_scale_clip_test_32 (const char *filename
, int filetype
, float maxval
)
395 float *data_out
, *data_in
;
396 double diff
, clip_max_diff
;
398 print_test_name ("flt_scale_clip_test_32", filename
) ;
400 data_out
= buffer_out
.flt
;
401 data_in
= buffer_in
.flt
;
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
;
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
;
414 ** Write two versions of the data:
415 ** normalized and clipped
416 ** un-normalized and clipped.
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__
) ;
426 memset (&buffer_in
, 0, sizeof (buffer_in
)) ;
428 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
430 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
442 if (sfinfo
.channels
!= 1)
443 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
447 check_log_buffer_or_die (file
, __LINE__
) ;
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__
) ;
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
]) ;
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
) ;
467 if (fabs (data_out
[k
]) > 1.0)
470 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
471 if (diff
> clip_max_diff
)
472 clip_max_diff
= diff
;
475 if (clip_max_diff
< 1e-20)
476 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__
, clip_max_diff
) ;
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
) ;
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
]) ;
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
) ;
498 if (fabs (data_out
[k
]) > maxval
)
501 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
502 if (diff
> clip_max_diff
)
503 clip_max_diff
= diff
;
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
) ;
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
) ;
518 } /* flt_scale_clip_test_32 */
521 flt_scale_clip_test_08 (const char *filename
, int filetype
, float maxval
)
525 float *data_out
, *data_in
;
526 double diff
, clip_max_diff
;
528 print_test_name ("flt_scale_clip_test_08", filename
) ;
530 data_out
= buffer_out
.flt
;
531 data_in
= buffer_in
.flt
;
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
;
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
;
544 ** Write two versions of the data:
545 ** normalized and clipped
546 ** un-normalized and clipped.
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__
) ;
556 memset (&buffer_in
, 0, sizeof (buffer_in
)) ;
558 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
560 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
572 if (sfinfo
.channels
!= 1)
573 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
577 check_log_buffer_or_die (file
, __LINE__
) ;
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__
) ;
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
]) ;
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
) ;
597 if (fabs (data_out
[k
]) > 1.0)
600 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
601 if (diff
> clip_max_diff
)
602 clip_max_diff
= diff
;
605 if (clip_max_diff
< 1e-20)
606 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__
, clip_max_diff
) ;
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
) ;
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
]) ;
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
) ;
628 if (fabs (data_out
[k
]) > maxval
)
631 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
632 if (diff
> clip_max_diff
)
633 clip_max_diff
= diff
;
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
) ;
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
) ;
648 } /* flt_scale_clip_test_08 */
653 dbl_scale_clip_test_16 (const char *filename
, int filetype
, float maxval
)
657 double *data_out
, *data_in
;
658 double diff
, clip_max_diff
;
660 print_test_name ("dbl_scale_clip_test_16", filename
) ;
662 data_out
= buffer_out
.dbl
;
663 data_in
= buffer_in
.dbl
;
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
;
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
;
676 ** Write two versions of the data:
677 ** normalized and clipped
678 ** un-normalized and clipped.
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__
) ;
688 memset (&buffer_in
, 0, sizeof (buffer_in
)) ;
690 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
692 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
704 if (sfinfo
.channels
!= 1)
705 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
709 check_log_buffer_or_die (file
, __LINE__
) ;
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__
) ;
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
]) ;
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
) ;
729 if (fabs (data_out
[k
]) > 1.0)
732 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
733 if (diff
> clip_max_diff
)
734 clip_max_diff
= diff
;
737 if (clip_max_diff
< 1e-20)
738 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__
, clip_max_diff
) ;
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
) ;
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
]) ;
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
) ;
760 if (fabs (data_out
[k
]) > maxval
)
763 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
764 if (diff
> clip_max_diff
)
765 clip_max_diff
= diff
;
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
) ;
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
) ;
780 } /* dbl_scale_clip_test_16 */
783 dbl_scale_clip_test_24 (const char *filename
, int filetype
, float maxval
)
787 double *data_out
, *data_in
;
788 double diff
, clip_max_diff
;
790 print_test_name ("dbl_scale_clip_test_24", filename
) ;
792 data_out
= buffer_out
.dbl
;
793 data_in
= buffer_in
.dbl
;
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
;
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
;
806 ** Write two versions of the data:
807 ** normalized and clipped
808 ** un-normalized and clipped.
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__
) ;
818 memset (&buffer_in
, 0, sizeof (buffer_in
)) ;
820 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
822 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
834 if (sfinfo
.channels
!= 1)
835 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
839 check_log_buffer_or_die (file
, __LINE__
) ;
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__
) ;
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
]) ;
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
) ;
859 if (fabs (data_out
[k
]) > 1.0)
862 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
863 if (diff
> clip_max_diff
)
864 clip_max_diff
= diff
;
867 if (clip_max_diff
< 1e-20)
868 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__
, clip_max_diff
) ;
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
) ;
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
]) ;
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
) ;
890 if (fabs (data_out
[k
]) > maxval
)
893 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
894 if (diff
> clip_max_diff
)
895 clip_max_diff
= diff
;
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
) ;
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
) ;
910 } /* dbl_scale_clip_test_24 */
913 dbl_scale_clip_test_32 (const char *filename
, int filetype
, float maxval
)
917 double *data_out
, *data_in
;
918 double diff
, clip_max_diff
;
920 print_test_name ("dbl_scale_clip_test_32", filename
) ;
922 data_out
= buffer_out
.dbl
;
923 data_in
= buffer_in
.dbl
;
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
;
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
;
936 ** Write two versions of the data:
937 ** normalized and clipped
938 ** un-normalized and clipped.
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__
) ;
948 memset (&buffer_in
, 0, sizeof (buffer_in
)) ;
950 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
952 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
964 if (sfinfo
.channels
!= 1)
965 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
969 check_log_buffer_or_die (file
, __LINE__
) ;
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__
) ;
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
]) ;
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
) ;
989 if (fabs (data_out
[k
]) > 1.0)
992 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
993 if (diff
> clip_max_diff
)
994 clip_max_diff
= diff
;
997 if (clip_max_diff
< 1e-20)
998 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__
, clip_max_diff
) ;
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
) ;
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
]) ;
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
) ;
1020 if (fabs (data_out
[k
]) > maxval
)
1023 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
1024 if (diff
> clip_max_diff
)
1025 clip_max_diff
= diff
;
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
) ;
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
) ;
1040 } /* dbl_scale_clip_test_32 */
1043 dbl_scale_clip_test_08 (const char *filename
, int filetype
, float maxval
)
1047 double *data_out
, *data_in
;
1048 double diff
, clip_max_diff
;
1050 print_test_name ("dbl_scale_clip_test_08", filename
) ;
1052 data_out
= buffer_out
.dbl
;
1053 data_in
= buffer_in
.dbl
;
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
;
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
;
1066 ** Write two versions of the data:
1067 ** normalized and clipped
1068 ** un-normalized and clipped.
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__
) ;
1078 memset (&buffer_in
, 0, sizeof (buffer_in
)) ;
1080 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1082 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
1094 if (sfinfo
.channels
!= 1)
1095 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
1099 check_log_buffer_or_die (file
, __LINE__
) ;
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__
) ;
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
]) ;
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
) ;
1119 if (fabs (data_out
[k
]) > 1.0)
1122 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
1123 if (diff
> clip_max_diff
)
1124 clip_max_diff
= diff
;
1127 if (clip_max_diff
< 1e-20)
1128 { printf ("\n\nLine %d: Clipping difference (%e) too small (normalized).\n\n", __LINE__
, clip_max_diff
) ;
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
) ;
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
]) ;
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
) ;
1150 if (fabs (data_out
[k
]) > maxval
)
1153 diff
= fabs (data_out
[k
] - data_in
[k
]) ;
1154 if (diff
> clip_max_diff
)
1155 clip_max_diff
= diff
;
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
) ;
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
) ;
1170 } /* dbl_scale_clip_test_08 */
1175 /*==============================================================================
1179 static void flt_short_clip_read_test (const char *filename
, int filetype
)
1183 short *data_in
, max_value
;
1186 print_test_name ("flt_short_clip_read_test", filename
) ;
1188 data_out
= buffer_out
.flt
;
1189 data_in
= buffer_in
.s
;
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 ;
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
;
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__
) ;
1209 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
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
) ;
1214 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
1226 if (sfinfo
.channels
!= 1)
1227 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
1231 check_log_buffer_or_die (file
, __LINE__
) ;
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) ;-*/
1238 /* Check the first half. */
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
]) ;
1246 max_value
= (max_value
> abs (data_in
[k
])) ? max_value
: abs (data_in
[k
]) ;
1251 } /* flt_short_clip_read_test */
1252 static void flt_int_clip_read_test (const char *filename
, int filetype
)
1256 int *data_in
, max_value
;
1259 print_test_name ("flt_int_clip_read_test", filename
) ;
1261 data_out
= buffer_out
.flt
;
1262 data_in
= buffer_in
.i
;
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 ;
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
;
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__
) ;
1282 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
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
) ;
1287 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
1299 if (sfinfo
.channels
!= 1)
1300 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
1304 check_log_buffer_or_die (file
, __LINE__
) ;
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) ;-*/
1311 /* Check the first half. */
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
]) ;
1319 max_value
= (max_value
> abs (data_in
[k
])) ? max_value
: abs (data_in
[k
]) ;
1324 } /* flt_int_clip_read_test */
1326 static void dbl_short_clip_read_test (const char *filename
, int filetype
)
1330 short *data_in
, max_value
;
1333 print_test_name ("dbl_short_clip_read_test", filename
) ;
1335 data_out
= buffer_out
.dbl
;
1336 data_in
= buffer_in
.s
;
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 ;
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
;
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__
) ;
1356 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
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
) ;
1361 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
1373 if (sfinfo
.channels
!= 1)
1374 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
1378 check_log_buffer_or_die (file
, __LINE__
) ;
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) ;-*/
1385 /* Check the first half. */
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
]) ;
1393 max_value
= (max_value
> abs (data_in
[k
])) ? max_value
: abs (data_in
[k
]) ;
1398 } /* dbl_short_clip_read_test */
1399 static void dbl_int_clip_read_test (const char *filename
, int filetype
)
1403 int *data_in
, max_value
;
1406 print_test_name ("dbl_int_clip_read_test", filename
) ;
1408 data_out
= buffer_out
.dbl
;
1409 data_in
= buffer_in
.i
;
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 ;
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
;
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__
) ;
1429 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
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
) ;
1434 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
1446 if (sfinfo
.channels
!= 1)
1447 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
1451 check_log_buffer_or_die (file
, __LINE__
) ;
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) ;-*/
1458 /* Check the first half. */
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
]) ;
1466 max_value
= (max_value
> abs (data_in
[k
])) ? max_value
: abs (data_in
[k
]) ;
1471 } /* dbl_int_clip_read_test */
1474 /*==============================================================================
1478 static void short_flt_scale_write_test (const char *filename
, int filetype
)
1482 float *data_in
, max_value
;
1485 print_test_name ("short_flt_clip_write_test", filename
) ;
1487 data_out
= buffer_out
.s
;
1488 data_in
= buffer_in
.flt
;
1490 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1491 data_out
[k
] = lrintf (0x7FFFF * 0.995 * sin (4 * M_PI
* k
/ BUFFER_SIZE
)) ;
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
;
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__
) ;
1507 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
1509 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1511 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
1523 if (sfinfo
.channels
!= 1)
1524 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
1528 check_log_buffer_or_die (file
, __LINE__
) ;
1530 /* Check the first section. */
1531 test_read_float_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1534 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1535 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1537 if (max_value
< 1000.0)
1538 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__
, max_value
) ;
1542 /* Check the second section. */
1543 test_read_float_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1546 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1547 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1549 if (max_value
> 1.0)
1550 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__
, max_value
) ;
1554 /* Check the third section. */
1555 test_read_float_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1558 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1559 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1561 if (max_value
< 1000.0)
1562 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__
, max_value
) ;
1570 } /* short_flt_scale_write_test */
1571 static void short_dbl_scale_write_test (const char *filename
, int filetype
)
1575 double *data_in
, max_value
;
1578 print_test_name ("short_dbl_clip_write_test", filename
) ;
1580 data_out
= buffer_out
.s
;
1581 data_in
= buffer_in
.dbl
;
1583 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1584 data_out
[k
] = lrint (0x7FFFF * 0.995 * sin (4 * M_PI
* k
/ BUFFER_SIZE
)) ;
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
;
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__
) ;
1600 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
1602 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1604 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
1616 if (sfinfo
.channels
!= 1)
1617 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
1621 check_log_buffer_or_die (file
, __LINE__
) ;
1623 /* Check the first section. */
1624 test_read_double_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1627 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1628 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1630 if (max_value
< 1000.0)
1631 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__
, max_value
) ;
1635 /* Check the second section. */
1636 test_read_double_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1639 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1640 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1642 if (max_value
> 1.0)
1643 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__
, max_value
) ;
1647 /* Check the third section. */
1648 test_read_double_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1651 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1652 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1654 if (max_value
< 1000.0)
1655 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__
, max_value
) ;
1663 } /* short_dbl_scale_write_test */
1665 static void int_flt_scale_write_test (const char *filename
, int filetype
)
1669 float *data_in
, max_value
;
1672 print_test_name ("int_flt_clip_write_test", filename
) ;
1674 data_out
= buffer_out
.i
;
1675 data_in
= buffer_in
.flt
;
1677 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1678 data_out
[k
] = lrintf (0x7FFFFFFF * 0.995 * sin (4 * M_PI
* k
/ BUFFER_SIZE
)) ;
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
;
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__
) ;
1694 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
1696 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1698 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
1710 if (sfinfo
.channels
!= 1)
1711 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
1715 check_log_buffer_or_die (file
, __LINE__
) ;
1717 /* Check the first section. */
1718 test_read_float_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1721 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1722 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1724 if (max_value
< 1000.0)
1725 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__
, max_value
) ;
1729 /* Check the second section. */
1730 test_read_float_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1733 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1734 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1736 if (max_value
> 1.0)
1737 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__
, max_value
) ;
1741 /* Check the third section. */
1742 test_read_float_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1745 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1746 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1748 if (max_value
< 1000.0)
1749 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__
, max_value
) ;
1757 } /* int_flt_scale_write_test */
1758 static void int_dbl_scale_write_test (const char *filename
, int filetype
)
1762 double *data_in
, max_value
;
1765 print_test_name ("int_dbl_clip_write_test", filename
) ;
1767 data_out
= buffer_out
.i
;
1768 data_in
= buffer_in
.dbl
;
1770 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1771 data_out
[k
] = lrint (0x7FFFFFFF * 0.995 * sin (4 * M_PI
* k
/ BUFFER_SIZE
)) ;
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
;
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__
) ;
1787 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
1789 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1791 sfinfo
.format
&= (SF_FORMAT_TYPEMASK
| SF_FORMAT_SUBMASK
) ;
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
) ;
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
)) ;
1803 if (sfinfo
.channels
!= 1)
1804 { printf ("\n\nLine %d: Incorrect number of channels in file.\n\n", __LINE__
) ;
1808 check_log_buffer_or_die (file
, __LINE__
) ;
1810 /* Check the first section. */
1811 test_read_double_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1814 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1815 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1817 if (max_value
< 1000.0)
1818 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__
, max_value
) ;
1822 /* Check the second section. */
1823 test_read_double_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1826 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1827 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1829 if (max_value
> 1.0)
1830 { printf ("\n\nLine %d: Max value (%f) > 1.0.\n\n", __LINE__
, max_value
) ;
1834 /* Check the third section. */
1835 test_read_double_or_die (file
, 0, data_in
, BUFFER_SIZE
, __LINE__
) ;
1838 for (k
= 0 ; k
< BUFFER_SIZE
; k
++)
1839 max_value
= (max_value
> fabs (data_in
[k
])) ? max_value
: fabs (data_in
[k
]) ;
1841 if (max_value
< 1000.0)
1842 { printf ("\n\nLine %d: Max value (%f) < 1000.0.\n\n", __LINE__
, max_value
) ;
1850 } /* int_dbl_scale_write_test */