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.
34 #define BUFFER_SIZE (1<<12)
36 static void lrintf_test (void) ;
38 static void pcm_test_bits_8 (const char *filename
, int filetype
, uint64_t hash
) ;
39 static void pcm_test_bits_16 (const char *filename
, int filetype
, uint64_t hash
) ;
40 static void pcm_test_bits_24 (const char *filename
, int filetype
, uint64_t hash
) ;
41 static void pcm_test_bits_32 (const char *filename
, int filetype
, uint64_t hash
) ;
43 static void pcm_test_float (const char *filename
, int filetype
, uint64_t hash
, int replace_float
) ;
44 static void pcm_test_double (const char *filename
, int filetype
, uint64_t hash
, int replace_float
) ;
47 { double d
[BUFFER_SIZE
+ 1] ;
48 float f
[BUFFER_SIZE
+ 1] ;
49 int i
[BUFFER_SIZE
+ 1] ;
50 short s
[BUFFER_SIZE
+ 1] ;
53 /* Data written to the file. */
54 static BUFFER data_out
;
56 /* Data read back from the file. */
57 static BUFFER data_in
;
64 pcm_test_bits_8 ("pcm-s8.raw", SF_FORMAT_RAW
| SF_FORMAT_PCM_S8
, 0x1cda335091249dbfLL
) ;
65 pcm_test_bits_8 ("pcm-u8.raw", SF_FORMAT_RAW
| SF_FORMAT_PCM_U8
, 0x7f748c433d695f3fLL
) ;
67 pcm_test_bits_16 ("le-pcm16.raw", SF_ENDIAN_LITTLE
| SF_FORMAT_RAW
| SF_FORMAT_PCM_16
, 0x3a2b956c881ebf08LL
) ;
68 pcm_test_bits_16 ("be-pcm16.raw", SF_ENDIAN_BIG
| SF_FORMAT_RAW
| SF_FORMAT_PCM_16
, 0xd9e2f840c55750f8LL
) ;
70 pcm_test_bits_24 ("le-pcm24.raw", SF_ENDIAN_LITTLE
| SF_FORMAT_RAW
| SF_FORMAT_PCM_24
, 0x933b6a759ab496f8LL
) ;
71 pcm_test_bits_24 ("be-pcm24.raw", SF_ENDIAN_BIG
| SF_FORMAT_RAW
| SF_FORMAT_PCM_24
, 0xbb1f3eaf9c30b6f8LL
) ;
73 pcm_test_bits_32 ("le-pcm32.raw", SF_ENDIAN_LITTLE
| SF_FORMAT_RAW
| SF_FORMAT_PCM_32
, 0xa77aece1c1c17f08LL
) ;
74 pcm_test_bits_32 ("be-pcm32.raw", SF_ENDIAN_BIG
| SF_FORMAT_RAW
| SF_FORMAT_PCM_32
, 0x3099ddf142d0b0f8LL
) ;
76 /* Lite remove start */
77 pcm_test_float ("le-float.raw", SF_ENDIAN_LITTLE
| SF_FORMAT_RAW
| SF_FORMAT_FLOAT
, 0x3c2ad04f7554267aLL
, SF_FALSE
) ;
78 pcm_test_float ("be-float.raw", SF_ENDIAN_BIG
| SF_FORMAT_RAW
| SF_FORMAT_FLOAT
, 0x074de3e248fa9186LL
, SF_FALSE
) ;
80 pcm_test_double ("le-double.raw", SF_ENDIAN_LITTLE
| SF_FORMAT_RAW
| SF_FORMAT_DOUBLE
, 0xc682726f958f669cLL
, SF_FALSE
) ;
81 pcm_test_double ("be-double.raw", SF_ENDIAN_BIG
| SF_FORMAT_RAW
| SF_FORMAT_DOUBLE
, 0xd9a3583f8ee51164LL
, SF_FALSE
) ;
83 pcm_test_float ("le-float.raw", SF_ENDIAN_LITTLE
| SF_FORMAT_RAW
| SF_FORMAT_FLOAT
, 0x3c2ad04f7554267aLL
, SF_TRUE
) ;
84 pcm_test_float ("be-float.raw", SF_ENDIAN_BIG
| SF_FORMAT_RAW
| SF_FORMAT_FLOAT
, 0x074de3e248fa9186LL
, SF_TRUE
) ;
86 pcm_test_double ("le-double.raw", SF_ENDIAN_LITTLE
| SF_FORMAT_RAW
| SF_FORMAT_DOUBLE
, 0xc682726f958f669cLL
, SF_TRUE
) ;
87 pcm_test_double ("be-double.raw", SF_ENDIAN_BIG
| SF_FORMAT_RAW
| SF_FORMAT_DOUBLE
, 0xd9a3583f8ee51164LL
, SF_TRUE
) ;
93 /*============================================================================================
94 ** Here are the test functions.
103 print_test_name ("lrintf_test", "") ;
107 float_data
= data_out
.f
;
108 int_data
= data_in
.i
;
110 for (k
= 0 ; k
< items
; k
++)
111 float_data
[k
] = (k
* ((k
% 2) ? 333333.0 : -333333.0)) ;
113 for (k
= 0 ; k
< items
; k
++)
114 int_data
[k
] = lrintf (float_data
[k
]) ;
116 for (k
= 0 ; k
< items
; k
++)
117 if (fabs (int_data
[k
] - float_data
[k
]) > 1.0)
118 { printf ("\n\nLine %d: float : Incorrect sample (#%d : %f => %d).\n", __LINE__
, k
, float_data
[k
], int_data
[k
]) ;
126 pcm_test_bits_8 (const char *filename
, int filetype
, uint64_t hash
)
129 int k
, items
, zero_count
;
130 short *short_out
, *short_in
;
131 int *int_out
, *int_in
;
132 /* Lite remove start */
133 float *float_out
, *float_in
;
134 double *double_out
, *double_in
;
135 /* Lite remove end */
137 print_test_name ("pcm_test_bits_8", filename
) ;
141 short_out
= data_out
.s
;
142 short_in
= data_in
.s
;
145 for (k
= 0 ; k
< items
; k
++)
146 { short_out
[k
] = ((k
* ((k
% 2) ? 1 : -1)) << 8) ;
147 zero_count
= short_out
[k
] ? zero_count
: zero_count
+ 1 ;
150 if (zero_count
> items
/ 4)
151 { printf ("\n\nLine %d: too many zeros.\n", __LINE__
) ;
155 sfinfo
.samplerate
= 44100 ;
156 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
157 sfinfo
.channels
= 1 ;
158 sfinfo
.format
= filetype
;
160 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
162 test_write_short_or_die (file
, 0, short_out
, items
, __LINE__
) ;
166 memset (short_in
, 0, items
* sizeof (short)) ;
168 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
170 if (sfinfo
.format
!= filetype
)
171 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
175 if (sfinfo
.frames
!= items
)
176 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
180 if (sfinfo
.channels
!= 1)
181 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
185 check_log_buffer_or_die (file
, __LINE__
) ;
187 test_read_short_or_die (file
, 0, short_in
, items
, __LINE__
) ;
189 for (k
= 0 ; k
< items
; k
++)
190 if (short_out
[k
] != short_in
[k
])
191 { printf ("\n\nLine %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__
, k
, short_out
[k
], short_in
[k
]) ;
197 /* Finally, check the file hash. */
198 check_file_hash_or_die (filename
, hash
, __LINE__
) ;
200 /*--------------------------------------------------------------------------
201 ** Test sf_read/write_int ()
205 int_out
= data_out
.i
;
207 for (k
= 0 ; k
< items
; k
++)
208 { int_out
[k
] = ((k
* ((k
% 2) ? 1 : -1)) << 24) ;
209 zero_count
= int_out
[k
] ? zero_count
: zero_count
+ 1 ;
212 if (zero_count
> items
/ 4)
213 { printf ("\n\nLine %d: too many zeros.\n", __LINE__
) ;
217 sfinfo
.samplerate
= 44100 ;
218 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
219 sfinfo
.channels
= 1 ;
220 sfinfo
.format
= filetype
;
222 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
224 test_write_int_or_die (file
, 0, int_out
, items
, __LINE__
) ;
228 memset (int_in
, 0, items
* sizeof (int)) ;
230 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
232 if (sfinfo
.format
!= filetype
)
233 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
237 if (sfinfo
.frames
!= items
)
238 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
242 if (sfinfo
.channels
!= 1)
243 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
247 check_log_buffer_or_die (file
, __LINE__
) ;
249 test_read_int_or_die (file
, 0, int_in
, items
, __LINE__
) ;
251 for (k
= 0 ; k
< items
; k
++)
252 if (int_out
[k
] != int_in
[k
])
253 { printf ("\n\nLine %d: int : Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__
, k
, int_out
[k
], int_in
[k
]) ;
259 /* Lite remove start */
260 /*--------------------------------------------------------------------------
261 ** Test sf_read/write_float ()
265 float_out
= data_out
.f
;
266 float_in
= data_in
.f
;
267 for (k
= 0 ; k
< items
; k
++)
268 { float_out
[k
] = (k
* ((k
% 2) ? 1 : -1)) ;
269 zero_count
= (fabs (float_out
[k
]) > 1e-10) ? zero_count
: zero_count
+ 1 ;
272 if (zero_count
> items
/ 4)
273 { printf ("\n\nLine %d: too many zeros (%d/%d).\n", __LINE__
, zero_count
, items
) ;
277 sfinfo
.samplerate
= 44100 ;
278 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
279 sfinfo
.channels
= 1 ;
280 sfinfo
.format
= filetype
;
282 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
284 sf_command (file
, SFC_SET_NORM_FLOAT
, NULL
, SF_FALSE
) ;
286 test_write_float_or_die (file
, 0, float_out
, items
, __LINE__
) ;
290 memset (float_in
, 0, items
* sizeof (float)) ;
292 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
294 if (sfinfo
.format
!= filetype
)
295 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
299 if (sfinfo
.frames
!= items
)
300 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
304 if (sfinfo
.channels
!= 1)
305 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
309 check_log_buffer_or_die (file
, __LINE__
) ;
311 sf_command (file
, SFC_SET_NORM_FLOAT
, NULL
, SF_FALSE
) ;
313 test_read_float_or_die (file
, 0, float_in
, items
, __LINE__
) ;
315 for (k
= 0 ; k
< items
; k
++)
316 if (fabs (float_out
[k
] - float_in
[k
]) > 1e-10)
317 { printf ("\n\nLine %d: float : Incorrect sample (#%d : %f => %f).\n", __LINE__
, k
, (double) float_out
[k
], (double) float_in
[k
]) ;
323 /*--------------------------------------------------------------------------
324 ** Test sf_read/write_double ()
328 double_out
= data_out
.d
;
329 double_in
= data_in
.d
;
330 for (k
= 0 ; k
< items
; k
++)
331 { double_out
[k
] = (k
* ((k
% 2) ? 1 : -1)) ;
332 zero_count
= (fabs (double_out
[k
]) > 1e-10) ? zero_count
: zero_count
+ 1 ;
335 if (zero_count
> items
/ 4)
336 { printf ("\n\nLine %d: too many zeros (%d/%d).\n", __LINE__
, zero_count
, items
) ;
340 sfinfo
.samplerate
= 44100 ;
341 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
342 sfinfo
.channels
= 1 ;
343 sfinfo
.format
= filetype
;
345 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
347 sf_command (file
, SFC_SET_NORM_DOUBLE
, NULL
, SF_FALSE
) ;
349 test_write_double_or_die (file
, 0, double_out
, items
, __LINE__
) ;
353 memset (double_in
, 0, items
* sizeof (double)) ;
355 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
357 if (sfinfo
.format
!= filetype
)
358 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
362 if (sfinfo
.frames
!= items
)
363 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
367 if (sfinfo
.channels
!= 1)
368 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
372 check_log_buffer_or_die (file
, __LINE__
) ;
374 sf_command (file
, SFC_SET_NORM_DOUBLE
, NULL
, SF_FALSE
) ;
376 test_read_double_or_die (file
, 0, double_in
, items
, __LINE__
) ;
378 for (k
= 0 ; k
< items
; k
++)
379 if (fabs (double_out
[k
] - double_in
[k
]) > 1e-10)
380 { printf ("\n\nLine %d: double : Incorrect sample (#%d : %f => %f).\n", __LINE__
, k
, double_out
[k
], double_in
[k
]) ;
385 /* Lite remove end */
389 } /* pcm_test_bits_8 */
392 pcm_test_bits_16 (const char *filename
, int filetype
, uint64_t hash
)
395 int k
, items
, zero_count
;
396 short *short_out
, *short_in
;
397 int *int_out
, *int_in
;
398 /* Lite remove start */
399 float *float_out
, *float_in
;
400 double *double_out
, *double_in
;
401 /* Lite remove end */
403 print_test_name ("pcm_test_bits_16", filename
) ;
407 short_out
= data_out
.s
;
408 short_in
= data_in
.s
;
411 for (k
= 0 ; k
< items
; k
++)
412 { short_out
[k
] = (k
* ((k
% 2) ? 3 : -3)) ;
413 zero_count
= short_out
[k
] ? zero_count
: zero_count
+ 1 ;
416 if (zero_count
> items
/ 4)
417 { printf ("\n\nLine %d: too many zeros.\n", __LINE__
) ;
421 sfinfo
.samplerate
= 44100 ;
422 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
423 sfinfo
.channels
= 1 ;
424 sfinfo
.format
= filetype
;
426 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
428 test_write_short_or_die (file
, 0, short_out
, items
, __LINE__
) ;
432 memset (short_in
, 0, items
* sizeof (short)) ;
434 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
436 if (sfinfo
.format
!= filetype
)
437 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
441 if (sfinfo
.frames
!= items
)
442 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
446 if (sfinfo
.channels
!= 1)
447 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
451 check_log_buffer_or_die (file
, __LINE__
) ;
453 test_read_short_or_die (file
, 0, short_in
, items
, __LINE__
) ;
455 for (k
= 0 ; k
< items
; k
++)
456 if (short_out
[k
] != short_in
[k
])
457 { printf ("\n\nLine %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__
, k
, short_out
[k
], short_in
[k
]) ;
463 /* Finally, check the file hash. */
464 check_file_hash_or_die (filename
, hash
, __LINE__
) ;
466 /*--------------------------------------------------------------------------
467 ** Test sf_read/write_int ()
471 int_out
= data_out
.i
;
473 for (k
= 0 ; k
< items
; k
++)
474 { int_out
[k
] = ((k
* ((k
% 2) ? 3 : -3)) << 16) ;
475 zero_count
= int_out
[k
] ? zero_count
: zero_count
+ 1 ;
478 if (zero_count
> items
/ 4)
479 { printf ("\n\nLine %d: too many zeros.\n", __LINE__
) ;
483 sfinfo
.samplerate
= 44100 ;
484 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
485 sfinfo
.channels
= 1 ;
486 sfinfo
.format
= filetype
;
488 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
490 test_write_int_or_die (file
, 0, int_out
, items
, __LINE__
) ;
494 memset (int_in
, 0, items
* sizeof (int)) ;
496 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
498 if (sfinfo
.format
!= filetype
)
499 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
503 if (sfinfo
.frames
!= items
)
504 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
508 if (sfinfo
.channels
!= 1)
509 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
513 check_log_buffer_or_die (file
, __LINE__
) ;
515 test_read_int_or_die (file
, 0, int_in
, items
, __LINE__
) ;
517 for (k
= 0 ; k
< items
; k
++)
518 if (int_out
[k
] != int_in
[k
])
519 { printf ("\n\nLine %d: int : Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__
, k
, int_out
[k
], int_in
[k
]) ;
525 /* Lite remove start */
526 /*--------------------------------------------------------------------------
527 ** Test sf_read/write_float ()
531 float_out
= data_out
.f
;
532 float_in
= data_in
.f
;
533 for (k
= 0 ; k
< items
; k
++)
534 { float_out
[k
] = (k
* ((k
% 2) ? 3 : -3)) ;
535 zero_count
= (fabs (float_out
[k
]) > 1e-10) ? zero_count
: zero_count
+ 1 ;
538 if (zero_count
> items
/ 4)
539 { printf ("\n\nLine %d: too many zeros (%d/%d).\n", __LINE__
, zero_count
, items
) ;
543 sfinfo
.samplerate
= 44100 ;
544 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
545 sfinfo
.channels
= 1 ;
546 sfinfo
.format
= filetype
;
548 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
550 sf_command (file
, SFC_SET_NORM_FLOAT
, NULL
, SF_FALSE
) ;
552 test_write_float_or_die (file
, 0, float_out
, items
, __LINE__
) ;
556 memset (float_in
, 0, items
* sizeof (float)) ;
558 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
560 if (sfinfo
.format
!= filetype
)
561 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
565 if (sfinfo
.frames
!= items
)
566 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
570 if (sfinfo
.channels
!= 1)
571 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
575 check_log_buffer_or_die (file
, __LINE__
) ;
577 sf_command (file
, SFC_SET_NORM_FLOAT
, NULL
, SF_FALSE
) ;
579 test_read_float_or_die (file
, 0, float_in
, items
, __LINE__
) ;
581 for (k
= 0 ; k
< items
; k
++)
582 if (fabs (float_out
[k
] - float_in
[k
]) > 1e-10)
583 { printf ("\n\nLine %d: float : Incorrect sample (#%d : %f => %f).\n", __LINE__
, k
, (double) float_out
[k
], (double) float_in
[k
]) ;
589 /*--------------------------------------------------------------------------
590 ** Test sf_read/write_double ()
594 double_out
= data_out
.d
;
595 double_in
= data_in
.d
;
596 for (k
= 0 ; k
< items
; k
++)
597 { double_out
[k
] = (k
* ((k
% 2) ? 3 : -3)) ;
598 zero_count
= (fabs (double_out
[k
]) > 1e-10) ? zero_count
: zero_count
+ 1 ;
601 if (zero_count
> items
/ 4)
602 { printf ("\n\nLine %d: too many zeros (%d/%d).\n", __LINE__
, zero_count
, items
) ;
606 sfinfo
.samplerate
= 44100 ;
607 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
608 sfinfo
.channels
= 1 ;
609 sfinfo
.format
= filetype
;
611 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
613 sf_command (file
, SFC_SET_NORM_DOUBLE
, NULL
, SF_FALSE
) ;
615 test_write_double_or_die (file
, 0, double_out
, items
, __LINE__
) ;
619 memset (double_in
, 0, items
* sizeof (double)) ;
621 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
623 if (sfinfo
.format
!= filetype
)
624 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
628 if (sfinfo
.frames
!= items
)
629 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
633 if (sfinfo
.channels
!= 1)
634 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
638 check_log_buffer_or_die (file
, __LINE__
) ;
640 sf_command (file
, SFC_SET_NORM_DOUBLE
, NULL
, SF_FALSE
) ;
642 test_read_double_or_die (file
, 0, double_in
, items
, __LINE__
) ;
644 for (k
= 0 ; k
< items
; k
++)
645 if (fabs (double_out
[k
] - double_in
[k
]) > 1e-10)
646 { printf ("\n\nLine %d: double : Incorrect sample (#%d : %f => %f).\n", __LINE__
, k
, double_out
[k
], double_in
[k
]) ;
651 /* Lite remove end */
655 } /* pcm_test_bits_16 */
658 pcm_test_bits_24 (const char *filename
, int filetype
, uint64_t hash
)
661 int k
, items
, zero_count
;
662 short *short_out
, *short_in
;
663 int *int_out
, *int_in
;
664 /* Lite remove start */
665 float *float_out
, *float_in
;
666 double *double_out
, *double_in
;
667 /* Lite remove end */
669 print_test_name ("pcm_test_bits_24", filename
) ;
673 short_out
= data_out
.s
;
674 short_in
= data_in
.s
;
677 for (k
= 0 ; k
< items
; k
++)
678 { short_out
[k
] = (k
* ((k
% 2) ? 3 : -3)) ;
679 zero_count
= short_out
[k
] ? zero_count
: zero_count
+ 1 ;
682 if (zero_count
> items
/ 4)
683 { printf ("\n\nLine %d: too many zeros.\n", __LINE__
) ;
687 sfinfo
.samplerate
= 44100 ;
688 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
689 sfinfo
.channels
= 1 ;
690 sfinfo
.format
= filetype
;
692 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
694 test_write_short_or_die (file
, 0, short_out
, items
, __LINE__
) ;
698 memset (short_in
, 0, items
* sizeof (short)) ;
700 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
702 if (sfinfo
.format
!= filetype
)
703 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
707 if (sfinfo
.frames
!= items
)
708 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
712 if (sfinfo
.channels
!= 1)
713 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
717 check_log_buffer_or_die (file
, __LINE__
) ;
719 test_read_short_or_die (file
, 0, short_in
, items
, __LINE__
) ;
721 for (k
= 0 ; k
< items
; k
++)
722 if (short_out
[k
] != short_in
[k
])
723 { printf ("\n\nLine %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__
, k
, short_out
[k
], short_in
[k
]) ;
729 /* Finally, check the file hash. */
730 check_file_hash_or_die (filename
, hash
, __LINE__
) ;
732 /*--------------------------------------------------------------------------
733 ** Test sf_read/write_int ()
737 int_out
= data_out
.i
;
739 for (k
= 0 ; k
< items
; k
++)
740 { int_out
[k
] = ((k
* ((k
% 2) ? 3333 : -3333)) << 8) ;
741 zero_count
= int_out
[k
] ? zero_count
: zero_count
+ 1 ;
744 if (zero_count
> items
/ 4)
745 { printf ("\n\nLine %d: too many zeros.\n", __LINE__
) ;
749 sfinfo
.samplerate
= 44100 ;
750 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
751 sfinfo
.channels
= 1 ;
752 sfinfo
.format
= filetype
;
754 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
756 test_write_int_or_die (file
, 0, int_out
, items
, __LINE__
) ;
760 memset (int_in
, 0, items
* sizeof (int)) ;
762 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
764 if (sfinfo
.format
!= filetype
)
765 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
769 if (sfinfo
.frames
!= items
)
770 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
774 if (sfinfo
.channels
!= 1)
775 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
779 check_log_buffer_or_die (file
, __LINE__
) ;
781 test_read_int_or_die (file
, 0, int_in
, items
, __LINE__
) ;
783 for (k
= 0 ; k
< items
; k
++)
784 if (int_out
[k
] != int_in
[k
])
785 { printf ("\n\nLine %d: int : Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__
, k
, int_out
[k
], int_in
[k
]) ;
791 /* Lite remove start */
792 /*--------------------------------------------------------------------------
793 ** Test sf_read/write_float ()
797 float_out
= data_out
.f
;
798 float_in
= data_in
.f
;
799 for (k
= 0 ; k
< items
; k
++)
800 { float_out
[k
] = (k
* ((k
% 2) ? 3333 : -3333)) ;
801 zero_count
= (fabs (float_out
[k
]) > 1e-10) ? zero_count
: zero_count
+ 1 ;
804 if (zero_count
> items
/ 4)
805 { printf ("\n\nLine %d: too many zeros (%d/%d).\n", __LINE__
, zero_count
, items
) ;
809 sfinfo
.samplerate
= 44100 ;
810 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
811 sfinfo
.channels
= 1 ;
812 sfinfo
.format
= filetype
;
814 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
816 sf_command (file
, SFC_SET_NORM_FLOAT
, NULL
, SF_FALSE
) ;
818 test_write_float_or_die (file
, 0, float_out
, items
, __LINE__
) ;
822 memset (float_in
, 0, items
* sizeof (float)) ;
824 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
826 if (sfinfo
.format
!= filetype
)
827 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
831 if (sfinfo
.frames
!= items
)
832 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
836 if (sfinfo
.channels
!= 1)
837 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
841 check_log_buffer_or_die (file
, __LINE__
) ;
843 sf_command (file
, SFC_SET_NORM_FLOAT
, NULL
, SF_FALSE
) ;
845 test_read_float_or_die (file
, 0, float_in
, items
, __LINE__
) ;
847 for (k
= 0 ; k
< items
; k
++)
848 if (fabs (float_out
[k
] - float_in
[k
]) > 1e-10)
849 { printf ("\n\nLine %d: float : Incorrect sample (#%d : %f => %f).\n", __LINE__
, k
, (double) float_out
[k
], (double) float_in
[k
]) ;
855 /*--------------------------------------------------------------------------
856 ** Test sf_read/write_double ()
860 double_out
= data_out
.d
;
861 double_in
= data_in
.d
;
862 for (k
= 0 ; k
< items
; k
++)
863 { double_out
[k
] = (k
* ((k
% 2) ? 3333 : -3333)) ;
864 zero_count
= (fabs (double_out
[k
]) > 1e-10) ? zero_count
: zero_count
+ 1 ;
867 if (zero_count
> items
/ 4)
868 { printf ("\n\nLine %d: too many zeros (%d/%d).\n", __LINE__
, zero_count
, items
) ;
872 sfinfo
.samplerate
= 44100 ;
873 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
874 sfinfo
.channels
= 1 ;
875 sfinfo
.format
= filetype
;
877 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
879 sf_command (file
, SFC_SET_NORM_DOUBLE
, NULL
, SF_FALSE
) ;
881 test_write_double_or_die (file
, 0, double_out
, items
, __LINE__
) ;
885 memset (double_in
, 0, items
* sizeof (double)) ;
887 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
889 if (sfinfo
.format
!= filetype
)
890 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
894 if (sfinfo
.frames
!= items
)
895 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
899 if (sfinfo
.channels
!= 1)
900 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
904 check_log_buffer_or_die (file
, __LINE__
) ;
906 sf_command (file
, SFC_SET_NORM_DOUBLE
, NULL
, SF_FALSE
) ;
908 test_read_double_or_die (file
, 0, double_in
, items
, __LINE__
) ;
910 for (k
= 0 ; k
< items
; k
++)
911 if (fabs (double_out
[k
] - double_in
[k
]) > 1e-10)
912 { printf ("\n\nLine %d: double : Incorrect sample (#%d : %f => %f).\n", __LINE__
, k
, double_out
[k
], double_in
[k
]) ;
917 /* Lite remove end */
921 } /* pcm_test_bits_24 */
924 pcm_test_bits_32 (const char *filename
, int filetype
, uint64_t hash
)
927 int k
, items
, zero_count
;
928 short *short_out
, *short_in
;
929 int *int_out
, *int_in
;
930 /* Lite remove start */
931 float *float_out
, *float_in
;
932 double *double_out
, *double_in
;
933 /* Lite remove end */
935 print_test_name ("pcm_test_bits_32", filename
) ;
939 short_out
= data_out
.s
;
940 short_in
= data_in
.s
;
943 for (k
= 0 ; k
< items
; k
++)
944 { short_out
[k
] = (k
* ((k
% 2) ? 3 : -3)) ;
945 zero_count
= short_out
[k
] ? zero_count
: zero_count
+ 1 ;
948 if (zero_count
> items
/ 4)
949 { printf ("\n\nLine %d: too many zeros.\n", __LINE__
) ;
953 sfinfo
.samplerate
= 44100 ;
954 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
955 sfinfo
.channels
= 1 ;
956 sfinfo
.format
= filetype
;
958 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
960 test_write_short_or_die (file
, 0, short_out
, items
, __LINE__
) ;
964 memset (short_in
, 0, items
* sizeof (short)) ;
966 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
968 if (sfinfo
.format
!= filetype
)
969 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
973 if (sfinfo
.frames
!= items
)
974 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
978 if (sfinfo
.channels
!= 1)
979 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
983 check_log_buffer_or_die (file
, __LINE__
) ;
985 test_read_short_or_die (file
, 0, short_in
, items
, __LINE__
) ;
987 for (k
= 0 ; k
< items
; k
++)
988 if (short_out
[k
] != short_in
[k
])
989 { printf ("\n\nLine %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__
, k
, short_out
[k
], short_in
[k
]) ;
995 /* Finally, check the file hash. */
996 check_file_hash_or_die (filename
, hash
, __LINE__
) ;
998 /*--------------------------------------------------------------------------
999 ** Test sf_read/write_int ()
1003 int_out
= data_out
.i
;
1004 int_in
= data_in
.i
;
1005 for (k
= 0 ; k
< items
; k
++)
1006 { int_out
[k
] = (k
* ((k
% 2) ? 333333 : -333333)) ;
1007 zero_count
= int_out
[k
] ? zero_count
: zero_count
+ 1 ;
1010 if (zero_count
> items
/ 4)
1011 { printf ("\n\nLine %d: too many zeros.\n", __LINE__
) ;
1015 sfinfo
.samplerate
= 44100 ;
1016 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1017 sfinfo
.channels
= 1 ;
1018 sfinfo
.format
= filetype
;
1020 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1022 test_write_int_or_die (file
, 0, int_out
, items
, __LINE__
) ;
1026 memset (int_in
, 0, items
* sizeof (int)) ;
1028 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1030 if (sfinfo
.format
!= filetype
)
1031 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
1035 if (sfinfo
.frames
!= items
)
1036 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
1040 if (sfinfo
.channels
!= 1)
1041 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
1045 check_log_buffer_or_die (file
, __LINE__
) ;
1047 test_read_int_or_die (file
, 0, int_in
, items
, __LINE__
) ;
1049 for (k
= 0 ; k
< items
; k
++)
1050 if (int_out
[k
] != int_in
[k
])
1051 { printf ("\n\nLine %d: int : Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__
, k
, int_out
[k
], int_in
[k
]) ;
1057 /* Lite remove start */
1058 /*--------------------------------------------------------------------------
1059 ** Test sf_read/write_float ()
1063 float_out
= data_out
.f
;
1064 float_in
= data_in
.f
;
1065 for (k
= 0 ; k
< items
; k
++)
1066 { float_out
[k
] = (k
* ((k
% 2) ? 333333 : -333333)) ;
1067 zero_count
= (fabs (float_out
[k
]) > 1e-10) ? zero_count
: zero_count
+ 1 ;
1070 if (zero_count
> items
/ 4)
1071 { printf ("\n\nLine %d: too many zeros (%d/%d).\n", __LINE__
, zero_count
, items
) ;
1075 sfinfo
.samplerate
= 44100 ;
1076 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1077 sfinfo
.channels
= 1 ;
1078 sfinfo
.format
= filetype
;
1080 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1082 sf_command (file
, SFC_SET_NORM_FLOAT
, NULL
, SF_FALSE
) ;
1084 test_write_float_or_die (file
, 0, float_out
, items
, __LINE__
) ;
1088 memset (float_in
, 0, items
* sizeof (float)) ;
1090 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1092 if (sfinfo
.format
!= filetype
)
1093 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
1097 if (sfinfo
.frames
!= items
)
1098 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
1102 if (sfinfo
.channels
!= 1)
1103 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
1107 check_log_buffer_or_die (file
, __LINE__
) ;
1109 sf_command (file
, SFC_SET_NORM_FLOAT
, NULL
, SF_FALSE
) ;
1111 test_read_float_or_die (file
, 0, float_in
, items
, __LINE__
) ;
1113 for (k
= 0 ; k
< items
; k
++)
1114 if (fabs (float_out
[k
] - float_in
[k
]) > 1e-10)
1115 { printf ("\n\nLine %d: float : Incorrect sample (#%d : %f => %f).\n", __LINE__
, k
, (double) float_out
[k
], (double) float_in
[k
]) ;
1121 /*--------------------------------------------------------------------------
1122 ** Test sf_read/write_double ()
1126 double_out
= data_out
.d
;
1127 double_in
= data_in
.d
;
1128 for (k
= 0 ; k
< items
; k
++)
1129 { double_out
[k
] = (k
* ((k
% 2) ? 333333 : -333333)) ;
1130 zero_count
= (fabs (double_out
[k
]) > 1e-10) ? zero_count
: zero_count
+ 1 ;
1133 if (zero_count
> items
/ 4)
1134 { printf ("\n\nLine %d: too many zeros (%d/%d).\n", __LINE__
, zero_count
, items
) ;
1138 sfinfo
.samplerate
= 44100 ;
1139 sfinfo
.frames
= 123456789 ; /* Wrong length. Library should correct this on sf_close. */
1140 sfinfo
.channels
= 1 ;
1141 sfinfo
.format
= filetype
;
1143 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1145 sf_command (file
, SFC_SET_NORM_DOUBLE
, NULL
, SF_FALSE
) ;
1147 test_write_double_or_die (file
, 0, double_out
, items
, __LINE__
) ;
1151 memset (double_in
, 0, items
* sizeof (double)) ;
1153 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1155 if (sfinfo
.format
!= filetype
)
1156 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__
, filetype
, sfinfo
.format
) ;
1160 if (sfinfo
.frames
!= items
)
1161 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
1165 if (sfinfo
.channels
!= 1)
1166 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__
) ;
1170 check_log_buffer_or_die (file
, __LINE__
) ;
1172 sf_command (file
, SFC_SET_NORM_DOUBLE
, NULL
, SF_FALSE
) ;
1174 test_read_double_or_die (file
, 0, double_in
, items
, __LINE__
) ;
1176 for (k
= 0 ; k
< items
; k
++)
1177 if (fabs (double_out
[k
] - double_in
[k
]) > 1e-10)
1178 { printf ("\n\nLine %d: double : Incorrect sample (#%d : %f => %f).\n", __LINE__
, k
, double_out
[k
], double_in
[k
]) ;
1183 /* Lite remove end */
1187 } /* pcm_test_bits_32 */
1191 /*==============================================================================
1195 pcm_test_float (const char *filename
, int filetype
, uint64_t hash
, int replace_float
)
1198 int k
, items
, frames
;
1200 double *data
, error
;
1202 print_test_name (replace_float
? "pcm_test_float (replace)" : "pcm_test_float", filename
) ;
1204 items
= BUFFER_SIZE
;
1207 for (sign
= 1, k
= 0 ; k
< items
; k
++)
1208 { data
[k
] = ((double) (k
* sign
)) / 100.0 ;
1209 sign
= (sign
> 0) ? -1 : 1 ;
1212 sfinfo
.samplerate
= 44100 ;
1213 sfinfo
.frames
= items
;
1214 sfinfo
.channels
= 1 ;
1215 sfinfo
.format
= filetype
;
1217 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1218 sf_command (file
, SFC_TEST_IEEE_FLOAT_REPLACE
, NULL
, replace_float
) ;
1219 if (replace_float
&& string_in_log_buffer (file
, "Using IEEE replacement") == 0)
1220 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__
) ;
1221 dump_log_buffer (file
) ;
1225 test_write_double_or_die (file
, 0, data
, items
, __LINE__
) ;
1229 check_file_hash_or_die (filename
, hash
, __LINE__
) ;
1231 memset (data
, 0, items
* sizeof (double)) ;
1233 if ((filetype
& SF_FORMAT_TYPEMASK
) != SF_FORMAT_RAW
)
1234 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
1236 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1237 sf_command (file
, SFC_TEST_IEEE_FLOAT_REPLACE
, NULL
, replace_float
) ;
1238 if (replace_float
&& string_in_log_buffer (file
, "Using IEEE replacement") == 0)
1239 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__
) ;
1240 dump_log_buffer (file
) ;
1244 if (sfinfo
.format
!= filetype
)
1245 { printf ("\n\nError (%s:%d) Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __FILE__
, __LINE__
, filetype
, sfinfo
.format
) ;
1249 if (sfinfo
.frames
!= items
)
1250 { printf ("\n\nError (%s:%d) Mono : Incorrect number of frames in file. (%d => %ld)\n", __FILE__
, __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
1254 if (sfinfo
.channels
!= 1)
1255 { printf ("\n\nError (%s:%d) Mono : Incorrect number of channels in file.\n", __FILE__
, __LINE__
) ;
1259 check_log_buffer_or_die (file
, __LINE__
) ;
1261 test_read_double_or_die (file
, 0, data
, items
, __LINE__
) ;
1263 for (sign
= -1, k
= 0 ; k
< items
; k
++)
1264 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1265 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1266 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1271 /* Seek to start of file. */
1272 test_seek_or_die (file
, 0, SEEK_SET
, 0, sfinfo
.channels
, __LINE__
) ;
1274 test_read_double_or_die (file
, 0, data
, 4, __LINE__
) ;
1275 for (k
= 0 ; k
< 4 ; k
++)
1276 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1277 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1278 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1283 /* Seek to offset from start of file. */
1284 test_seek_or_die (file
, 10, SEEK_SET
, 10, sfinfo
.channels
, __LINE__
) ;
1286 test_read_double_or_die (file
, 0, data
+ 10, 4, __LINE__
) ;
1287 for (k
= 10 ; k
< 14 ; k
++)
1288 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1289 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1290 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1295 /* Seek to offset from current position. */
1296 test_seek_or_die (file
, 6, SEEK_CUR
, 20, sfinfo
.channels
, __LINE__
) ;
1298 test_read_double_or_die (file
, 0, data
+ 20, 4, __LINE__
) ;
1299 for (k
= 20 ; k
< 24 ; k
++)
1300 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1301 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1302 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1307 /* Seek to offset from end of file. */
1308 test_seek_or_die (file
, -1 * (sfinfo
.frames
- 10), SEEK_END
, 10, sfinfo
.channels
, __LINE__
) ;
1310 test_read_double_or_die (file
, 0, data
+ 10, 4, __LINE__
) ;
1311 for (k
= 10 ; k
< 14 ; k
++)
1312 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1313 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1314 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1321 /* Now test Stereo. */
1323 if ((filetype
& SF_FORMAT_TYPEMASK
) == SF_FORMAT_SVX
) /* SVX is mono only */
1328 items
= BUFFER_SIZE
;
1331 for (sign
= -1, k
= 0 ; k
< items
; k
++)
1332 data
[k
] = ((double) k
) / 100.0 * (sign
*= -1) ;
1334 sfinfo
.samplerate
= 44100 ;
1335 sfinfo
.frames
= items
;
1336 sfinfo
.channels
= 2 ;
1337 sfinfo
.format
= filetype
;
1339 frames
= items
/ sfinfo
.channels
;
1341 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1342 sf_command (file
, SFC_TEST_IEEE_FLOAT_REPLACE
, NULL
, replace_float
) ;
1343 if (replace_float
&& string_in_log_buffer (file
, "Using IEEE replacement") == 0)
1344 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__
) ;
1345 dump_log_buffer (file
) ;
1349 test_writef_double_or_die (file
, 0, data
, frames
, __LINE__
) ;
1353 check_file_hash_or_die (filename
, hash
, __LINE__
) ;
1355 memset (data
, 0, items
* sizeof (double)) ;
1357 if ((filetype
& SF_FORMAT_TYPEMASK
) != SF_FORMAT_RAW
)
1358 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
1360 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1361 sf_command (file
, SFC_TEST_IEEE_FLOAT_REPLACE
, NULL
, replace_float
) ;
1362 if (replace_float
&& string_in_log_buffer (file
, "Using IEEE replacement") == 0)
1363 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__
) ;
1364 dump_log_buffer (file
) ;
1368 if (sfinfo
.format
!= filetype
)
1369 { printf ("\n\nError (%s:%d) Stereo : Returned format incorrect (0x%08X => 0x%08X).\n", __FILE__
, __LINE__
, filetype
, sfinfo
.format
) ;
1373 if (sfinfo
.frames
!= frames
)
1374 { printf ("\n\nError (%s:%d) Stereo : Incorrect number of frames in file. (%d => %ld)\n", __FILE__
, __LINE__
, frames
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
1378 if (sfinfo
.channels
!= 2)
1379 { printf ("\n\nError (%s:%d) Stereo : Incorrect number of channels in file.\n", __FILE__
, __LINE__
) ;
1383 check_log_buffer_or_die (file
, __LINE__
) ;
1385 test_readf_double_or_die (file
, 0, data
, frames
, __LINE__
) ;
1386 for (sign
= -1, k
= 0 ; k
< items
; k
++)
1387 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1388 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1389 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1394 /* Seek to start of file. */
1395 test_seek_or_die (file
, 0, SEEK_SET
, 0, sfinfo
.channels
, __LINE__
) ;
1397 test_readf_double_or_die (file
, 0, data
, 4, __LINE__
) ;
1398 for (k
= 0 ; k
< 4 ; k
++)
1399 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1400 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1401 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1406 /* Seek to offset from start of file. */
1407 test_seek_or_die (file
, 10, SEEK_SET
, 10, sfinfo
.channels
, __LINE__
) ;
1409 test_readf_double_or_die (file
, 0, data
+ 20, 2, __LINE__
) ;
1410 for (k
= 20 ; k
< 24 ; k
++)
1411 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1412 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1413 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1418 /* Seek to offset from current position. */
1419 test_seek_or_die (file
, 8, SEEK_CUR
, 20, sfinfo
.channels
, __LINE__
) ;
1421 test_readf_double_or_die (file
, 0, data
+ 40, 2, __LINE__
) ;
1422 for (k
= 40 ; k
< 44 ; k
++)
1423 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1424 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1425 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1430 /* Seek to offset from end of file. */
1431 test_seek_or_die (file
, -1 * (sfinfo
.frames
- 10), SEEK_END
, 10, sfinfo
.channels
, __LINE__
) ;
1433 test_readf_double_or_die (file
, 0, data
+ 20, 2, __LINE__
) ;
1434 for (k
= 20 ; k
< 24 ; k
++)
1435 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1436 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1437 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1446 } /* pcm_test_float */
1449 pcm_test_double (const char *filename
, int filetype
, uint64_t hash
, int replace_float
)
1452 int k
, items
, frames
;
1454 double *data
, error
;
1456 /* This is the best test routine. Other should be brought up to this standard. */
1458 print_test_name (replace_float
? "pcm_test_double (replace)" : "pcm_test_double", filename
) ;
1460 items
= BUFFER_SIZE
;
1463 for (sign
= 1, k
= 0 ; k
< items
; k
++)
1464 { data
[k
] = ((double) (k
* sign
)) / 100.0 ;
1465 sign
= (sign
> 0) ? -1 : 1 ;
1468 sfinfo
.samplerate
= 44100 ;
1469 sfinfo
.frames
= items
;
1470 sfinfo
.channels
= 1 ;
1471 sfinfo
.format
= filetype
;
1473 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1474 sf_command (file
, SFC_TEST_IEEE_FLOAT_REPLACE
, NULL
, replace_float
) ;
1475 if (replace_float
&& string_in_log_buffer (file
, "Using IEEE replacement") == 0)
1476 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__
) ;
1477 dump_log_buffer (file
) ;
1481 test_write_double_or_die (file
, 0, data
, items
, __LINE__
) ;
1485 #if (defined (WIN32) || defined (_WIN32))
1486 /* File hashing on Win32 fails due to slighty different
1487 ** calculated values of the sin() function.
1489 hash
= hash
; /* Avoid compiler warning. */
1491 check_file_hash_or_die (filename
, hash
, __LINE__
) ;
1494 memset (data
, 0, items
* sizeof (double)) ;
1496 if ((filetype
& SF_FORMAT_TYPEMASK
) != SF_FORMAT_RAW
)
1497 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
1499 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1500 sf_command (file
, SFC_TEST_IEEE_FLOAT_REPLACE
, NULL
, replace_float
) ;
1501 if (replace_float
&& string_in_log_buffer (file
, "Using IEEE replacement") == 0)
1502 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__
) ;
1503 dump_log_buffer (file
) ;
1507 if (sfinfo
.format
!= filetype
)
1508 { printf ("\n\nError (%s:%d) Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __FILE__
, __LINE__
, filetype
, sfinfo
.format
) ;
1512 if (sfinfo
.frames
!= items
)
1513 { printf ("\n\nError (%s:%d) Mono : Incorrect number of frames in file. (%d => %ld)\n", __FILE__
, __LINE__
, items
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
1517 if (sfinfo
.channels
!= 1)
1518 { printf ("\n\nError (%s:%d) Mono : Incorrect number of channels in file.\n", __FILE__
, __LINE__
) ;
1522 check_log_buffer_or_die (file
, __LINE__
) ;
1524 test_read_double_or_die (file
, 0, data
, items
, __LINE__
) ;
1526 for (sign
= -1, k
= 0 ; k
< items
; k
++)
1527 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1528 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1529 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1534 /* Seek to start of file. */
1535 test_seek_or_die (file
, 0, SEEK_SET
, 0, sfinfo
.channels
, __LINE__
) ;
1537 test_read_double_or_die (file
, 0, data
, 4, __LINE__
) ;
1538 for (k
= 0 ; k
< 4 ; k
++)
1539 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1540 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1541 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1546 /* Seek to offset from start of file. */
1547 test_seek_or_die (file
, 10, SEEK_SET
, 10, sfinfo
.channels
, __LINE__
) ;
1549 test_read_double_or_die (file
, 0, data
+ 10, 4, __LINE__
) ;
1551 test_seek_or_die (file
, 0, SEEK_CUR
, 14, sfinfo
.channels
, __LINE__
) ;
1553 for (k
= 10 ; k
< 14 ; k
++)
1554 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1555 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1556 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1561 /* Seek to offset from current position. */
1562 test_seek_or_die (file
, 6, SEEK_CUR
, 20, sfinfo
.channels
, __LINE__
) ;
1564 test_read_double_or_die (file
, 0, data
+ 20, 4, __LINE__
) ;
1565 for (k
= 20 ; k
< 24 ; k
++)
1566 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1567 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1568 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1573 /* Seek to offset from end of file. */
1574 test_seek_or_die (file
, -1 * (sfinfo
.frames
- 10), SEEK_END
, 10, sfinfo
.channels
, __LINE__
) ;
1576 test_read_double_or_die (file
, 0, data
+ 10, 4, __LINE__
) ;
1577 for (k
= 10 ; k
< 14 ; k
++)
1578 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1579 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1580 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1587 /* Now test Stereo. */
1589 if ((filetype
& SF_FORMAT_TYPEMASK
) == SF_FORMAT_SVX
) /* SVX is mono only */
1594 items
= BUFFER_SIZE
;
1597 for (sign
= -1, k
= 0 ; k
< items
; k
++)
1598 data
[k
] = ((double) k
) / 100.0 * (sign
*= -1) ;
1600 sfinfo
.samplerate
= 44100 ;
1601 sfinfo
.frames
= items
;
1602 sfinfo
.channels
= 2 ;
1603 sfinfo
.format
= filetype
;
1605 frames
= items
/ sfinfo
.channels
;
1607 file
= test_open_file_or_die (filename
, SFM_WRITE
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1608 sf_command (file
, SFC_TEST_IEEE_FLOAT_REPLACE
, NULL
, replace_float
) ;
1609 if (replace_float
&& string_in_log_buffer (file
, "Using IEEE replacement") == 0)
1610 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__
) ;
1611 dump_log_buffer (file
) ;
1615 test_writef_double_or_die (file
, 0, data
, frames
, __LINE__
) ;
1619 #if (defined (WIN32) || defined (_WIN32))
1620 /* File hashing on Win32 fails due to slighty different
1621 ** calculated values.
1623 hash
= hash
; /* Avoid compiler warning. */
1625 check_file_hash_or_die (filename
, hash
, __LINE__
) ;
1628 memset (data
, 0, items
* sizeof (double)) ;
1630 if ((filetype
& SF_FORMAT_TYPEMASK
) != SF_FORMAT_RAW
)
1631 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
1633 file
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
1634 sf_command (file
, SFC_TEST_IEEE_FLOAT_REPLACE
, NULL
, replace_float
) ;
1635 if (replace_float
&& string_in_log_buffer (file
, "Using IEEE replacement") == 0)
1636 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__
) ;
1637 dump_log_buffer (file
) ;
1641 if (sfinfo
.format
!= filetype
)
1642 { printf ("\n\nError (%s:%d) Stereo : Returned format incorrect (0x%08X => 0x%08X).\n", __FILE__
, __LINE__
, filetype
, sfinfo
.format
) ;
1646 if (sfinfo
.frames
!= frames
)
1647 { printf ("\n\nError (%s:%d) Stereo : Incorrect number of frames in file. (%d => %ld)\n", __FILE__
, __LINE__
, frames
, SF_COUNT_TO_LONG (sfinfo
.frames
)) ;
1651 if (sfinfo
.channels
!= 2)
1652 { printf ("\n\nError (%s:%d) Stereo : Incorrect number of channels in file.\n", __FILE__
, __LINE__
) ;
1656 check_log_buffer_or_die (file
, __LINE__
) ;
1658 test_readf_double_or_die (file
, 0, data
, frames
, __LINE__
) ;
1660 for (sign
= -1, k
= 0 ; k
< items
; k
++)
1661 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1662 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1663 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1668 /* Seek to start of file. */
1669 test_seek_or_die (file
, 0, SEEK_SET
, 0, sfinfo
.channels
, __LINE__
) ;
1671 test_read_double_or_die (file
, 0, data
, 4, __LINE__
) ;
1672 for (k
= 0 ; k
< 4 ; k
++)
1673 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1674 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1675 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1680 /* Seek to offset from start of file. */
1681 test_seek_or_die (file
, 10, SEEK_SET
, 10, sfinfo
.channels
, __LINE__
) ;
1683 test_read_double_or_die (file
, 0, data
+ 10, 4, __LINE__
) ;
1684 for (k
= 20 ; k
< 24 ; k
++)
1685 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1686 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1687 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1692 /* Seek to offset from current position. */
1693 test_seek_or_die (file
, 8, SEEK_CUR
, 20, sfinfo
.channels
, __LINE__
) ;
1695 test_readf_double_or_die (file
, 0, data
+ 40, 4, __LINE__
) ;
1696 for (k
= 40 ; k
< 44 ; k
++)
1697 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1698 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1699 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1704 /* Seek to offset from end of file. */
1705 test_seek_or_die (file
, -1 * (sfinfo
.frames
-10), SEEK_END
, 10, sfinfo
.channels
, __LINE__
) ;
1707 test_readf_double_or_die (file
, 0, data
+ 20, 4, __LINE__
) ;
1708 for (k
= 20 ; k
< 24 ; k
++)
1709 { error
= fabs (data
[k
] - ((double) k
) / 100.0 * (sign
*= -1)) ;
1710 if (fabs (data
[k
]) > 1e-100 && fabs (error
/ data
[k
]) > 1e-5)
1711 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__
, __LINE__
, k
, ((double) k
) / 100.0, data
[k
]) ;
1720 } /* pcm_test_double */
1722 /*==============================================================================