1 [+ AutoGen5 template c +]
3 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #define BUFFER_SIZE (1<<12)
37 static void lrintf_test (void) ;
40 +]static void pcm_test_[+ (get "name") +] (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]) ;
127 pcm_test_[+ (get "name") +] (const char *filename, int filetype, uint64_t hash)
130 int k, items, zero_count ;
131 short *short_out, *short_in ;
132 int *int_out, *int_in ;
133 /* Lite remove start */
134 float *float_out, *float_in ;
135 double *double_out, *double_in ;
136 /* Lite remove end */
138 print_test_name ("pcm_test_[+ (get "name") +]", filename) ;
140 items = [+ (get "item_count") +] ;
142 short_out = data_out.s ;
143 short_in = data_in.s ;
146 for (k = 0 ; k < items ; k++)
147 { short_out [k] = [+ (get "short_func") +] ;
148 zero_count = short_out [k] ? zero_count : zero_count + 1 ;
151 if (zero_count > items / 4)
152 { printf ("\n\nLine %d: too many zeros.\n", __LINE__) ;
156 sfinfo.samplerate = 44100 ;
157 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
158 sfinfo.channels = 1 ;
159 sfinfo.format = filetype ;
161 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
163 test_write_short_or_die (file, 0, short_out, items, __LINE__) ;
167 memset (short_in, 0, items * sizeof (short)) ;
169 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
171 if (sfinfo.format != filetype)
172 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
176 if (sfinfo.frames != items)
177 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__, items, SF_COUNT_TO_LONG (sfinfo.frames)) ;
181 if (sfinfo.channels != 1)
182 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
186 check_log_buffer_or_die (file, __LINE__) ;
188 test_read_short_or_die (file, 0, short_in, items, __LINE__) ;
190 for (k = 0 ; k < items ; k++)
191 if (short_out [k] != short_in [k])
192 { printf ("\n\nLine %d: Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, short_out [k], short_in [k]) ;
198 /* Finally, check the file hash. */
199 check_file_hash_or_die (filename, hash, __LINE__) ;
201 /*--------------------------------------------------------------------------
202 ** Test sf_read/write_int ()
206 int_out = data_out.i ;
208 for (k = 0 ; k < items ; k++)
209 { int_out [k] = [+ (get "int_func") +] ;
210 zero_count = int_out [k] ? zero_count : zero_count + 1 ;
213 if (zero_count > items / 4)
214 { printf ("\n\nLine %d: too many zeros.\n", __LINE__) ;
218 sfinfo.samplerate = 44100 ;
219 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
220 sfinfo.channels = 1 ;
221 sfinfo.format = filetype ;
223 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
225 test_write_int_or_die (file, 0, int_out, items, __LINE__) ;
229 memset (int_in, 0, items * sizeof (int)) ;
231 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
233 if (sfinfo.format != filetype)
234 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
238 if (sfinfo.frames != items)
239 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__, items, SF_COUNT_TO_LONG (sfinfo.frames)) ;
243 if (sfinfo.channels != 1)
244 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
248 check_log_buffer_or_die (file, __LINE__) ;
250 test_read_int_or_die (file, 0, int_in, items, __LINE__) ;
252 for (k = 0 ; k < items ; k++)
253 if (int_out [k] != int_in [k])
254 { printf ("\n\nLine %d: int : Incorrect sample (#%d : 0x%x => 0x%x).\n", __LINE__, k, int_out [k], int_in [k]) ;
260 /* Lite remove start */
261 /*--------------------------------------------------------------------------
262 ** Test sf_read/write_float ()
266 float_out = data_out.f ;
267 float_in = data_in.f ;
268 for (k = 0 ; k < items ; k++)
269 { float_out [k] = [+ (get "float_func") +] ;
270 zero_count = (fabs (float_out [k]) > 1e-10) ? zero_count : zero_count + 1 ;
273 if (zero_count > items / 4)
274 { printf ("\n\nLine %d: too many zeros (%d/%d).\n", __LINE__, zero_count, items) ;
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 ;
283 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
285 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
287 test_write_float_or_die (file, 0, float_out, items, __LINE__) ;
291 memset (float_in, 0, items * sizeof (float)) ;
293 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
295 if (sfinfo.format != filetype)
296 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
300 if (sfinfo.frames != items)
301 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__, items, SF_COUNT_TO_LONG (sfinfo.frames)) ;
305 if (sfinfo.channels != 1)
306 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
310 check_log_buffer_or_die (file, __LINE__) ;
312 sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
314 test_read_float_or_die (file, 0, float_in, items, __LINE__) ;
316 for (k = 0 ; k < items ; k++)
317 if (fabs (float_out [k] - float_in [k]) > 1e-10)
318 { printf ("\n\nLine %d: float : Incorrect sample (#%d : %f => %f).\n", __LINE__, k, (double) float_out [k], (double) float_in [k]) ;
324 /*--------------------------------------------------------------------------
325 ** Test sf_read/write_double ()
329 double_out = data_out.d ;
330 double_in = data_in.d ;
331 for (k = 0 ; k < items ; k++)
332 { double_out [k] = [+ (get "float_func") +] ;
333 zero_count = (fabs (double_out [k]) > 1e-10) ? zero_count : zero_count + 1 ;
336 if (zero_count > items / 4)
337 { printf ("\n\nLine %d: too many zeros (%d/%d).\n", __LINE__, zero_count, items) ;
341 sfinfo.samplerate = 44100 ;
342 sfinfo.frames = 123456789 ; /* Wrong length. Library should correct this on sf_close. */
343 sfinfo.channels = 1 ;
344 sfinfo.format = filetype ;
346 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
348 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
350 test_write_double_or_die (file, 0, double_out, items, __LINE__) ;
354 memset (double_in, 0, items * sizeof (double)) ;
356 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
358 if (sfinfo.format != filetype)
359 { printf ("\n\nLine %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ;
363 if (sfinfo.frames != items)
364 { printf ("\n\nLine %d: Incorrect number of frames in file. (%d => %ld)\n", __LINE__, items, SF_COUNT_TO_LONG (sfinfo.frames)) ;
368 if (sfinfo.channels != 1)
369 { printf ("\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
373 check_log_buffer_or_die (file, __LINE__) ;
375 sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
377 test_read_double_or_die (file, 0, double_in, items, __LINE__) ;
379 for (k = 0 ; k < items ; k++)
380 if (fabs (double_out [k] - double_in [k]) > 1e-10)
381 { printf ("\n\nLine %d: double : Incorrect sample (#%d : %f => %f).\n", __LINE__, k, double_out [k], double_in [k]) ;
386 /* Lite remove end */
390 } /* pcm_test_[+ (get "name") +] */
395 /*==============================================================================
399 pcm_test_float (const char *filename, int filetype, uint64_t hash, int replace_float)
402 int k, items, frames ;
404 double *data, error ;
406 print_test_name (replace_float ? "pcm_test_float (replace)" : "pcm_test_float", filename) ;
408 items = BUFFER_SIZE ;
411 for (sign = 1, k = 0 ; k < items ; k++)
412 { data [k] = ((double) (k * sign)) / 100.0 ;
413 sign = (sign > 0) ? -1 : 1 ;
416 sfinfo.samplerate = 44100 ;
417 sfinfo.frames = items ;
418 sfinfo.channels = 1 ;
419 sfinfo.format = filetype ;
421 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
422 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
423 if (replace_float && string_in_log_buffer (file, "Using IEEE replacement") == 0)
424 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__) ;
425 dump_log_buffer (file) ;
429 test_write_double_or_die (file, 0, data, items, __LINE__) ;
433 check_file_hash_or_die (filename, hash, __LINE__) ;
435 memset (data, 0, items * sizeof (double)) ;
437 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
438 memset (&sfinfo, 0, sizeof (sfinfo)) ;
440 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
441 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
442 if (replace_float && string_in_log_buffer (file, "Using IEEE replacement") == 0)
443 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__) ;
444 dump_log_buffer (file) ;
448 if (sfinfo.format != filetype)
449 { printf ("\n\nError (%s:%d) Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __FILE__, __LINE__, filetype, sfinfo.format) ;
453 if (sfinfo.frames != items)
454 { printf ("\n\nError (%s:%d) Mono : Incorrect number of frames in file. (%d => %ld)\n", __FILE__, __LINE__, items, SF_COUNT_TO_LONG (sfinfo.frames)) ;
458 if (sfinfo.channels != 1)
459 { printf ("\n\nError (%s:%d) Mono : Incorrect number of channels in file.\n", __FILE__, __LINE__) ;
463 check_log_buffer_or_die (file, __LINE__) ;
465 test_read_double_or_die (file, 0, data, items, __LINE__) ;
467 for (sign = -1, k = 0 ; k < items ; k++)
468 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
469 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
470 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
475 /* Seek to start of file. */
476 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
478 test_read_double_or_die (file, 0, data, 4, __LINE__) ;
479 for (k = 0 ; k < 4 ; k++)
480 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
481 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
482 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
487 /* Seek to offset from start of file. */
488 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
490 test_read_double_or_die (file, 0, data + 10, 4, __LINE__) ;
491 for (k = 10 ; k < 14 ; k++)
492 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
493 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
494 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
499 /* Seek to offset from current position. */
500 test_seek_or_die (file, 6, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
502 test_read_double_or_die (file, 0, data + 20, 4, __LINE__) ;
503 for (k = 20 ; k < 24 ; k++)
504 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
505 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
506 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
511 /* Seek to offset from end of file. */
512 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
514 test_read_double_or_die (file, 0, data + 10, 4, __LINE__) ;
515 for (k = 10 ; k < 14 ; k++)
516 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
517 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
518 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
525 /* Now test Stereo. */
527 if ((filetype & SF_FORMAT_TYPEMASK) == SF_FORMAT_SVX) /* SVX is mono only */
532 items = BUFFER_SIZE ;
535 for (sign = -1, k = 0 ; k < items ; k++)
536 data [k] = ((double) k) / 100.0 * (sign *= -1) ;
538 sfinfo.samplerate = 44100 ;
539 sfinfo.frames = items ;
540 sfinfo.channels = 2 ;
541 sfinfo.format = filetype ;
543 frames = items / sfinfo.channels ;
545 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
546 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
547 if (replace_float && string_in_log_buffer (file, "Using IEEE replacement") == 0)
548 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__) ;
549 dump_log_buffer (file) ;
553 test_writef_double_or_die (file, 0, data, frames, __LINE__) ;
557 check_file_hash_or_die (filename, hash, __LINE__) ;
559 memset (data, 0, items * sizeof (double)) ;
561 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
562 memset (&sfinfo, 0, sizeof (sfinfo)) ;
564 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
565 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
566 if (replace_float && string_in_log_buffer (file, "Using IEEE replacement") == 0)
567 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__) ;
568 dump_log_buffer (file) ;
572 if (sfinfo.format != filetype)
573 { printf ("\n\nError (%s:%d) Stereo : Returned format incorrect (0x%08X => 0x%08X).\n", __FILE__, __LINE__, filetype, sfinfo.format) ;
577 if (sfinfo.frames != frames)
578 { printf ("\n\nError (%s:%d) Stereo : Incorrect number of frames in file. (%d => %ld)\n", __FILE__, __LINE__, frames, SF_COUNT_TO_LONG (sfinfo.frames)) ;
582 if (sfinfo.channels != 2)
583 { printf ("\n\nError (%s:%d) Stereo : Incorrect number of channels in file.\n", __FILE__, __LINE__) ;
587 check_log_buffer_or_die (file, __LINE__) ;
589 test_readf_double_or_die (file, 0, data, frames, __LINE__) ;
590 for (sign = -1, k = 0 ; k < items ; k++)
591 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
592 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
593 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
598 /* Seek to start of file. */
599 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
601 test_readf_double_or_die (file, 0, data, 4, __LINE__) ;
602 for (k = 0 ; k < 4 ; k++)
603 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
604 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
605 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
610 /* Seek to offset from start of file. */
611 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
613 test_readf_double_or_die (file, 0, data + 20, 2, __LINE__) ;
614 for (k = 20 ; k < 24 ; k++)
615 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
616 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
617 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
622 /* Seek to offset from current position. */
623 test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
625 test_readf_double_or_die (file, 0, data + 40, 2, __LINE__) ;
626 for (k = 40 ; k < 44 ; k++)
627 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
628 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
629 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
634 /* Seek to offset from end of file. */
635 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
637 test_readf_double_or_die (file, 0, data + 20, 2, __LINE__) ;
638 for (k = 20 ; k < 24 ; k++)
639 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
640 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
641 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
650 } /* pcm_test_float */
653 pcm_test_double (const char *filename, int filetype, uint64_t hash, int replace_float)
656 int k, items, frames ;
658 double *data, error ;
660 /* This is the best test routine. Other should be brought up to this standard. */
662 print_test_name (replace_float ? "pcm_test_double (replace)" : "pcm_test_double", filename) ;
664 items = BUFFER_SIZE ;
667 for (sign = 1, k = 0 ; k < items ; k++)
668 { data [k] = ((double) (k * sign)) / 100.0 ;
669 sign = (sign > 0) ? -1 : 1 ;
672 sfinfo.samplerate = 44100 ;
673 sfinfo.frames = items ;
674 sfinfo.channels = 1 ;
675 sfinfo.format = filetype ;
677 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
678 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
679 if (replace_float && string_in_log_buffer (file, "Using IEEE replacement") == 0)
680 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__) ;
681 dump_log_buffer (file) ;
685 test_write_double_or_die (file, 0, data, items, __LINE__) ;
689 #if (defined (WIN32) || defined (_WIN32))
690 /* File hashing on Win32 fails due to slighty different
691 ** calculated values of the sin() function.
693 hash = hash ; /* Avoid compiler warning. */
695 check_file_hash_or_die (filename, hash, __LINE__) ;
698 memset (data, 0, items * sizeof (double)) ;
700 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
701 memset (&sfinfo, 0, sizeof (sfinfo)) ;
703 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
704 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
705 if (replace_float && string_in_log_buffer (file, "Using IEEE replacement") == 0)
706 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__) ;
707 dump_log_buffer (file) ;
711 if (sfinfo.format != filetype)
712 { printf ("\n\nError (%s:%d) Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __FILE__, __LINE__, filetype, sfinfo.format) ;
716 if (sfinfo.frames != items)
717 { printf ("\n\nError (%s:%d) Mono : Incorrect number of frames in file. (%d => %ld)\n", __FILE__, __LINE__, items, SF_COUNT_TO_LONG (sfinfo.frames)) ;
721 if (sfinfo.channels != 1)
722 { printf ("\n\nError (%s:%d) Mono : Incorrect number of channels in file.\n", __FILE__, __LINE__) ;
726 check_log_buffer_or_die (file, __LINE__) ;
728 test_read_double_or_die (file, 0, data, items, __LINE__) ;
730 for (sign = -1, k = 0 ; k < items ; k++)
731 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
732 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
733 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
738 /* Seek to start of file. */
739 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
741 test_read_double_or_die (file, 0, data, 4, __LINE__) ;
742 for (k = 0 ; k < 4 ; k++)
743 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
744 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
745 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
750 /* Seek to offset from start of file. */
751 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
753 test_read_double_or_die (file, 0, data + 10, 4, __LINE__) ;
755 test_seek_or_die (file, 0, SEEK_CUR, 14, sfinfo.channels, __LINE__) ;
757 for (k = 10 ; k < 14 ; k++)
758 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
759 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
760 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
765 /* Seek to offset from current position. */
766 test_seek_or_die (file, 6, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
768 test_read_double_or_die (file, 0, data + 20, 4, __LINE__) ;
769 for (k = 20 ; k < 24 ; k++)
770 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
771 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
772 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
777 /* Seek to offset from end of file. */
778 test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
780 test_read_double_or_die (file, 0, data + 10, 4, __LINE__) ;
781 for (k = 10 ; k < 14 ; k++)
782 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
783 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
784 { printf ("\n\nError (%s:%d) Mono : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
791 /* Now test Stereo. */
793 if ((filetype & SF_FORMAT_TYPEMASK) == SF_FORMAT_SVX) /* SVX is mono only */
798 items = BUFFER_SIZE ;
801 for (sign = -1, k = 0 ; k < items ; k++)
802 data [k] = ((double) k) / 100.0 * (sign *= -1) ;
804 sfinfo.samplerate = 44100 ;
805 sfinfo.frames = items ;
806 sfinfo.channels = 2 ;
807 sfinfo.format = filetype ;
809 frames = items / sfinfo.channels ;
811 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
812 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
813 if (replace_float && string_in_log_buffer (file, "Using IEEE replacement") == 0)
814 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__) ;
815 dump_log_buffer (file) ;
819 test_writef_double_or_die (file, 0, data, frames, __LINE__) ;
823 #if (defined (WIN32) || defined (_WIN32))
824 /* File hashing on Win32 fails due to slighty different
825 ** calculated values.
827 hash = hash ; /* Avoid compiler warning. */
829 check_file_hash_or_die (filename, hash, __LINE__) ;
832 memset (data, 0, items * sizeof (double)) ;
834 if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
835 memset (&sfinfo, 0, sizeof (sfinfo)) ;
837 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
838 sf_command (file, SFC_TEST_IEEE_FLOAT_REPLACE, NULL, replace_float) ;
839 if (replace_float && string_in_log_buffer (file, "Using IEEE replacement") == 0)
840 { printf ("\n\nLine %d : Float replacement code not working.\n\n", __LINE__) ;
841 dump_log_buffer (file) ;
845 if (sfinfo.format != filetype)
846 { printf ("\n\nError (%s:%d) Stereo : Returned format incorrect (0x%08X => 0x%08X).\n", __FILE__, __LINE__, filetype, sfinfo.format) ;
850 if (sfinfo.frames != frames)
851 { printf ("\n\nError (%s:%d) Stereo : Incorrect number of frames in file. (%d => %ld)\n", __FILE__, __LINE__, frames, SF_COUNT_TO_LONG (sfinfo.frames)) ;
855 if (sfinfo.channels != 2)
856 { printf ("\n\nError (%s:%d) Stereo : Incorrect number of channels in file.\n", __FILE__, __LINE__) ;
860 check_log_buffer_or_die (file, __LINE__) ;
862 test_readf_double_or_die (file, 0, data, frames, __LINE__) ;
864 for (sign = -1, k = 0 ; k < items ; k++)
865 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
866 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
867 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
872 /* Seek to start of file. */
873 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
875 test_read_double_or_die (file, 0, data, 4, __LINE__) ;
876 for (k = 0 ; k < 4 ; k++)
877 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
878 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
879 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
884 /* Seek to offset from start of file. */
885 test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
887 test_read_double_or_die (file, 0, data + 10, 4, __LINE__) ;
888 for (k = 20 ; k < 24 ; k++)
889 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
890 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
891 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
896 /* Seek to offset from current position. */
897 test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
899 test_readf_double_or_die (file, 0, data + 40, 4, __LINE__) ;
900 for (k = 40 ; k < 44 ; k++)
901 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
902 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
903 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
908 /* Seek to offset from end of file. */
909 test_seek_or_die (file, -1 * (sfinfo.frames -10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
911 test_readf_double_or_die (file, 0, data + 20, 4, __LINE__) ;
912 for (k = 20 ; k < 24 ; k++)
913 { error = fabs (data [k] - ((double) k) / 100.0 * (sign *= -1)) ;
914 if (fabs (data [k]) > 1e-100 && fabs (error / data [k]) > 1e-5)
915 { printf ("\n\nError (%s:%d) Stereo : Incorrect sample (#%d : %f => %f).\n", __FILE__, __LINE__, k, ((double) k) / 100.0, data [k]) ;
924 } /* pcm_test_double */
926 /*==============================================================================