1
[+ AutoGen5 template c +]
3
** Copyright (C) 2001-2002 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.
36
#define BUFFER_LEN (1<<10)
37
#define LOG_BUFFER_SIZE 1024
39
static void float_norm_test (const char *filename) ;
40
static void double_norm_test (const char *filename) ;
41
static void format_tests (void) ;
42
static void calc_peak_test (int filetype, const char *filename) ;
43
static void truncate_test (const char *filename, int filetype) ;
45
/* Force the start of this buffer to be double aligned. Sparc-solaris will
49
static float float_data [BUFFER_LEN] ;
50
static double double_data [BUFFER_LEN] ;
53
main (int argc, char *argv [])
54
{ /*-char *filename ;-*/
59
{ printf ("Usage : %s <test>\n", argv [0]) ;
60
printf (" Where <test> is one of the following:\n") ;
61
printf (" ver - test sf_command (SFC_GETLIB_VERSION)\n") ;
62
/*-printf (" text - test adding of text strings\n") ;-*/
63
printf (" norm - test floating point normalisation\n") ;
64
printf (" format - test format string commands\n") ;
65
printf (" peak - test peak calculation\n") ;
66
printf (" trunc - test file truncation\n") ;
67
printf (" all - perform all tests\n") ;
71
do_all =! strcmp (argv [1], "all") ;
73
if (do_all || ! strcmp (argv [1], "ver"))
76
sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ;
77
if (strlen (buffer) < 1)
78
{ printf ("Line %d: could not retrieve lib version.\n", __LINE__) ;
84
if (do_all || ! strcmp (argv [1], "norm"))
85
{ /* Preliminary float/double normalisation tests. More testing
86
** is done in the program 'floating_point_test'.
88
float_norm_test ("float.wav") ;
89
double_norm_test ("double.wav") ;
93
if (do_all || ! strcmp (argv [1], "peak"))
94
{ calc_peak_test (SF_ENDIAN_BIG | SF_FORMAT_RAW, "be-peak.raw") ;
95
calc_peak_test (SF_ENDIAN_LITTLE | SF_FORMAT_RAW, "le-peak.raw") ;
99
if (do_all || ! strcmp (argv [1], "format"))
104
if (do_all || ! strcmp (argv [1], "trunc"))
105
{ truncate_test ("truncate.raw", SF_FORMAT_RAW | SF_FORMAT_PCM_32) ;
106
truncate_test ("truncate.au" , SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
111
{ printf ("Mono : ************************************\n") ;
112
printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
113
printf ("Mono : ************************************\n") ;
120
/*============================================================================================
121
** Here are the test functions.
125
float_norm_test (const char *filename)
130
print_test_name ("float_norm_test", filename) ;
132
sfinfo.samplerate = 44100 ;
133
sfinfo.format = (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;
134
sfinfo.channels = 1 ;
135
sfinfo.frames = BUFFER_LEN ;
137
/* Create float_data with all values being less than 1.0. */
138
for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
139
float_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;
140
for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)
141
float_data [k] = (k + 5) ;
143
if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
144
{ printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
146
puts (sf_strerror (NULL)) ;
150
/* Normalisation is on by default so no need to do anything here. */
152
if ((k = sf_write_float (file, float_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
153
{ printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
157
/* Turn normalisation off. */
158
sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
160
if ((k = sf_write_float (file, float_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
161
{ printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
167
/* sfinfo struct should still contain correct data. */
168
if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
169
{ printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
171
puts (sf_strerror (NULL)) ;
175
if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))
176
{ printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;
180
if (sfinfo.frames != BUFFER_LEN)
181
{ printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
185
if (sfinfo.channels != 1)
186
{ printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
190
/* Read float_data and check that it is normalised (ie default). */
191
if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
192
{ printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
196
for (k = 0 ; k < BUFFER_LEN ; k++)
197
if (float_data [k] >= 1.0)
198
{ printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;
202
/* Seek to start of file, turn normalisation off, read float_data and check again. */
203
sf_seek (file, 0, SEEK_SET) ;
204
sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
206
if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
207
{ printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
211
for (k = 0 ; k < BUFFER_LEN ; k++)
212
if (float_data [k] < 1.0)
213
{ printf ("\n\nLine %d: float_data [%d] == %f which is less than 1.0\n", __LINE__, k, float_data [k]) ;
217
/* Seek to start of file, turn normalisation on, read float_data and do final check. */
218
sf_seek (file, 0, SEEK_SET) ;
219
sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_TRUE) ;
221
if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
222
{ printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
226
for (k = 0 ; k < BUFFER_LEN ; k++)
227
if (float_data [k] > 1.0)
228
{ printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;
238
} /* float_norm_test */
241
double_norm_test (const char *filename)
246
print_test_name ("double_norm_test", filename) ;
248
sfinfo.samplerate = 44100 ;
249
sfinfo.format = (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;
250
sfinfo.channels = 1 ;
251
sfinfo.frames = BUFFER_LEN ;
253
/* Create double_data with all values being less than 1.0. */
254
for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
255
double_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;
256
for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)
257
double_data [k] = (k + 5) ;
259
if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
260
{ printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
262
puts (sf_strerror (NULL)) ;
266
/* Normailsation is on by default so no need to do anything here. */
267
/*-sf_command (file, "set-norm-double", "true", 0) ;-*/
269
if ((k = sf_write_double (file, double_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
270
{ printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
274
/* Turn normalisation off. */
275
sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
277
if ((k = sf_write_double (file, double_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
278
{ printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
284
if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
285
{ printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
287
puts (sf_strerror (NULL)) ;
291
if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))
292
{ printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;
296
if (sfinfo.frames != BUFFER_LEN)
297
{ printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
301
if (sfinfo.channels != 1)
302
{ printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
306
/* Read double_data and check that it is normalised (ie default). */
307
if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
308
{ printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
312
for (k = 0 ; k < BUFFER_LEN ; k++)
313
if (double_data [k] >= 1.0)
314
{ printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ;
318
/* Seek to start of file, turn normalisation off, read double_data and check again. */
319
sf_seek (file, 0, SEEK_SET) ;
320
sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
322
if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
323
{ printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
327
for (k = 0 ; k < BUFFER_LEN ; k++)
328
if (double_data [k] < 1.0)
329
{ printf ("\n\nLine %d: double_data [%d] == %f which is less than 1.0\n", __LINE__, k, double_data [k]) ;
333
/* Seek to start of file, turn normalisation on, read double_data and do final check. */
334
sf_seek (file, 0, SEEK_SET) ;
335
sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_TRUE) ;
337
if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
338
{ printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
342
for (k = 0 ; k < BUFFER_LEN ; k++)
343
if (double_data [k] > 1.0)
344
{ printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ;
354
} /* double_norm_test */
358
{ SF_FORMAT_INFO format_info ;
360
const char *last_name ;
363
print_test_name ("format_tests", "(null)") ;
365
/* Clear out SF_INFO struct and set channels > 0. */
366
memset (&sfinfo, 0, sizeof (sfinfo)) ;
367
sfinfo.channels = 1 ;
369
/* First test simple formats. */
371
sf_command (NULL, SFC_GET_SIMPLE_FORMAT_COUNT, &count, sizeof (int)) ;
373
if (count < 0 || count > 30)
374
{ printf ("Line %d: Weird count.\n", __LINE__) ;
378
format_info.format = 0 ;
379
sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
381
last_name = format_info.name ;
382
for (k = 1 ; k < count ; k ++)
383
{ format_info.format = k ;
384
sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
385
if (strcmp (last_name, format_info.name) >= 0)
386
{ printf ("\n\nLine %d: format names out of sequence `%s' < `%s'.\n", __LINE__, last_name, format_info.name) ;
389
sfinfo.format = format_info.format ;
391
if (! sf_format_check (&sfinfo))
392
{ printf ("\n\nLine %d: sf_format_check failed.\n", __LINE__) ;
393
printf (" Name : %s\n", format_info.name) ;
394
printf (" Format : 0x%X\n", sfinfo.format) ;
395
printf (" Channels : 0x%X\n", sfinfo.channels) ;
396
printf (" Sample Rate : 0x%X\n", sfinfo.samplerate) ;
399
last_name = format_info.name ;
401
format_info.format = 666 ;
402
sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
404
/* Now test major formats. */
405
sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int)) ;
407
if (count < 0 || count > 30)
408
{ printf ("Line %d: Weird count.\n", __LINE__) ;
412
format_info.format = 0 ;
413
sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
415
last_name = format_info.name ;
416
for (k = 1 ; k < count ; k ++)
417
{ format_info.format = k ;
418
sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
419
if (strcmp (last_name, format_info.name) >= 0)
420
{ printf ("\n\nLine %d: format names out of sequence (%d) `%s' < `%s'.\n", __LINE__, k, last_name, format_info.name) ;
424
last_name = format_info.name ;
426
format_info.format = 666 ;
427
sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
429
/* Now test subtype formats. */
430
sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int)) ;
432
if (count < 0 || count > 30)
433
{ printf ("Line %d: Weird count.\n", __LINE__) ;
437
format_info.format = 0 ;
438
sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
440
last_name = format_info.name ;
441
for (k = 1 ; k < count ; k ++)
442
{ format_info.format = k ;
443
sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
445
format_info.format = 666 ;
446
sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
453
calc_peak_test (int filetype, const char *filename)
459
print_test_name ("calc_peak_test", filename) ;
461
format = (filetype | SF_FORMAT_PCM_16) ;
463
sfinfo.samplerate = 44100 ;
464
sfinfo.format = format ;
465
sfinfo.channels = 1 ;
466
sfinfo.frames = BUFFER_LEN ;
468
/* Create double_data with max value of 0.5. */
469
for (k = 0 ; k < BUFFER_LEN ; k++)
470
double_data [k] = (k + 1) / (2.0 * BUFFER_LEN) ;
472
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
474
test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
478
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
480
if (sfinfo.format != format)
481
{ printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
485
if (sfinfo.frames != BUFFER_LEN)
486
{ printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
490
if (sfinfo.channels != 1)
491
{ printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
495
sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ;
496
if (fabs (peak - (1 << 14)) > 1.0)
497
{ printf ("Line %d : Peak value should be %d (is %f).\n", __LINE__, (1 << 14), peak) ;
501
sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ;
502
if (fabs (peak - 0.5) > 4e-5)
503
{ printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
509
format = (filetype | SF_FORMAT_FLOAT) ;
510
sfinfo.samplerate = 44100 ;
511
sfinfo.format = format ;
512
sfinfo.channels = 1 ;
513
sfinfo.frames = BUFFER_LEN ;
515
/* Create double_data with max value of 0.5. */
516
for (k = 0 ; k < BUFFER_LEN ; k++)
517
double_data [k] = (k + 1) / (2.0 * BUFFER_LEN) ;
519
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
521
test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
525
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
527
if (sfinfo.format != format)
528
{ printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
532
if (sfinfo.frames != BUFFER_LEN)
533
{ printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
537
if (sfinfo.channels != 1)
538
{ printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
542
sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ;
543
if (fabs (peak - 0.5) > 1e-5)
544
{ printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
548
sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ;
549
if (fabs (peak - 0.5) > 1e-5)
550
{ printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
559
} /* calc_peak_test */
562
truncate_test (const char *filename, int filetype)
568
print_test_name ("truncate_test", filename) ;
570
sfinfo.samplerate = 11025 ;
571
sfinfo.format = filetype ;
572
sfinfo.channels = 2 ;
574
int_data = (int*) double_data ;
576
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
578
test_write_int_or_die (file, 0, int_data, BUFFER_LEN, __LINE__) ;
581
if (sf_command (file, SFC_FILE_TRUNCATE, &len, sizeof (len)))
582
{ printf ("Line %d: sf_command (SFC_FILE_TRUNCATE) returned error.\n", __LINE__) ;
586
test_seek_or_die (file, 0, SEEK_CUR, len, 2, __LINE__) ;
587
test_seek_or_die (file, 0, SEEK_END, len, 2, __LINE__) ;
593
} /* truncate_test */
597
Do not edit or modify anything in this comment block.
598
The following line is a file identity tag for the GNU Arch
599
revision control system.
601
arch-tag: 59e5d452-8dae-45aa-99aa-b78dc0deba1c