1 [+ AutoGen5 template c +]
3 ** Copyright (C) 2001-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.
33 #if (HAVE_DECL_S_IRGRP == 0)
34 #include <sf_unistd.h>
37 #if (defined (WIN32) || defined (_WIN32))
46 #define BUFFER_LEN (1<<10)
47 #define LOG_BUFFER_SIZE 1024
49 static void update_header_test (const char *filename, int typemajor) ;
52 +]static void update_seek_[+ (get "name") +]_test (const char *filename, int filetype) ;
56 static void extra_header_test (const char *filename, int filetype) ;
58 static void header_shrink_test (const char *filename, int filetype) ;
60 /* Force the start of this buffer to be double aligned. Sparc-solaris will
63 static int data_out [BUFFER_LEN] ;
64 static int data_in [BUFFER_LEN] ;
67 main (int argc, char *argv [])
72 { printf ("Usage : %s <test>\n", argv [0]) ;
73 printf (" Where <test> is one of the following:\n") ;
74 printf (" wav - test WAV file peak chunk\n") ;
75 printf (" aiff - test AIFF file PEAK chunk\n") ;
76 printf (" all - perform all tests\n") ;
80 do_all=!strcmp (argv [1], "all") ;
82 if (do_all || ! strcmp (argv [1], "wav"))
83 { update_header_test ("header.wav", SF_FORMAT_WAV) ;
84 update_seek_short_test ("header_short.wav", SF_FORMAT_WAV) ;
85 update_seek_int_test ("header_int.wav", SF_FORMAT_WAV) ;
86 update_seek_float_test ("header_float.wav", SF_FORMAT_WAV) ;
87 update_seek_double_test ("header_double.wav", SF_FORMAT_WAV) ;
88 header_shrink_test ("header_shrink.wav", SF_FORMAT_WAV) ;
89 extra_header_test ("extra.wav", SF_FORMAT_WAV) ;
91 update_header_test ("header.wavex", SF_FORMAT_WAVEX) ;
92 update_seek_short_test ("header_short.wavex", SF_FORMAT_WAVEX) ;
93 update_seek_int_test ("header_int.wavex", SF_FORMAT_WAVEX) ;
94 update_seek_float_test ("header_float.wavex", SF_FORMAT_WAVEX) ;
95 update_seek_double_test ("header_double.wavex", SF_FORMAT_WAVEX) ;
96 header_shrink_test ("header_shrink.wavex", SF_FORMAT_WAVEX) ;
97 extra_header_test ("extra.wavex", SF_FORMAT_WAVEX) ;
101 if (do_all || ! strcmp (argv [1], "aiff"))
102 { update_header_test ("header.aiff", SF_FORMAT_AIFF) ;
103 update_seek_short_test ("header_short.aiff", SF_FORMAT_AIFF) ;
104 update_seek_int_test ("header_int.aiff", SF_FORMAT_AIFF) ;
105 update_seek_float_test ("header_float.aiff", SF_FORMAT_AIFF) ;
106 update_seek_double_test ("header_double.aiff", SF_FORMAT_AIFF) ;
107 header_shrink_test ("header_shrink.wav", SF_FORMAT_AIFF) ;
108 extra_header_test ("extra.aiff", SF_FORMAT_AIFF) ;
112 if (do_all || ! strcmp (argv [1], "au"))
113 { update_header_test ("header.au", SF_FORMAT_AU) ;
114 update_seek_short_test ("header_short.au", SF_FORMAT_AU) ;
115 update_seek_int_test ("header_int.au", SF_FORMAT_AU) ;
116 update_seek_float_test ("header_float.au", SF_FORMAT_AU) ;
117 update_seek_double_test ("header_double.au", SF_FORMAT_AU) ;
121 if (do_all || ! strcmp (argv [1], "caf"))
122 { update_header_test ("header.caf", SF_FORMAT_CAF) ;
123 update_seek_short_test ("header_short.caf", SF_FORMAT_CAF) ;
124 update_seek_int_test ("header_int.caf", SF_FORMAT_CAF) ;
125 update_seek_float_test ("header_float.caf", SF_FORMAT_CAF) ;
126 update_seek_double_test ("header_double.caf", SF_FORMAT_CAF) ;
127 /* extra_header_test ("extra.caf", SF_FORMAT_CAF) ; */
131 if (do_all || ! strcmp (argv [1], "nist"))
132 { update_header_test ("header.nist", SF_FORMAT_NIST) ;
133 update_seek_short_test ("header_short.nist", SF_FORMAT_NIST) ;
134 update_seek_int_test ("header_int.nist", SF_FORMAT_NIST) ;
138 if (do_all || ! strcmp (argv [1], "paf"))
139 { update_header_test ("header.paf", SF_FORMAT_PAF) ;
140 update_seek_short_test ("header_short.paf", SF_FORMAT_PAF) ;
144 if (do_all || ! strcmp (argv [1], "ircam"))
145 { update_header_test ("header.ircam", SF_FORMAT_IRCAM) ;
146 update_seek_short_test ("header_short.ircam", SF_FORMAT_IRCAM) ;
150 if (do_all || ! strcmp (argv [1], "w64"))
151 { update_header_test ("header.w64", SF_FORMAT_W64) ;
152 update_seek_short_test ("header_short.w64", SF_FORMAT_W64) ;
153 update_seek_int_test ("header_int.w64", SF_FORMAT_W64) ;
154 update_seek_float_test ("header_float.w64", SF_FORMAT_W64) ;
155 update_seek_double_test ("header_double.w64", SF_FORMAT_W64) ;
159 if (do_all || ! strcmp (argv [1], "rf64"))
160 { update_header_test ("header.rf64", SF_FORMAT_RF64) ;
161 update_seek_short_test ("header_short.rf64", SF_FORMAT_RF64) ;
162 update_seek_int_test ("header_int.rf64", SF_FORMAT_RF64) ;
163 update_seek_float_test ("header_float.rf64", SF_FORMAT_RF64) ;
164 update_seek_double_test ("header_double.rf64", SF_FORMAT_RF64) ;
168 if (do_all || ! strcmp (argv [1], "mat4"))
169 { update_header_test ("header.mat4", SF_FORMAT_MAT4) ;
170 update_seek_short_test ("header_short.mat4", SF_FORMAT_MAT4) ;
171 update_seek_int_test ("header_int.mat4", SF_FORMAT_MAT4) ;
172 update_seek_float_test ("header_float.mat4", SF_FORMAT_MAT4) ;
173 update_seek_double_test ("header_double.mat4", SF_FORMAT_MAT4) ;
177 if (do_all || ! strcmp (argv [1], "mat5"))
178 { update_header_test ("header.mat5", SF_FORMAT_MAT5) ;
179 update_seek_short_test ("header_short.mat5", SF_FORMAT_MAT5) ;
180 update_seek_int_test ("header_int.mat5", SF_FORMAT_MAT5) ;
181 update_seek_float_test ("header_float.mat5", SF_FORMAT_MAT5) ;
182 update_seek_double_test ("header_double.mat5", SF_FORMAT_MAT5) ;
186 if (do_all || ! strcmp (argv [1], "pvf"))
187 { update_header_test ("header.pvf", SF_FORMAT_PVF) ;
188 update_seek_short_test ("header_short.pvf", SF_FORMAT_PVF) ;
192 if (do_all || ! strcmp (argv [1], "avr"))
193 { update_header_test ("header.avr", SF_FORMAT_AVR) ;
194 update_seek_short_test ("header_short.avr", SF_FORMAT_AVR) ;
198 if (do_all || ! strcmp (argv [1], "htk"))
199 { update_header_test ("header.htk", SF_FORMAT_HTK) ;
200 update_seek_short_test ("header_short.htk", SF_FORMAT_HTK) ;
204 if (do_all || ! strcmp (argv [1], "svx"))
205 { update_header_test ("header.svx", SF_FORMAT_SVX) ;
206 update_seek_short_test ("header_short.svx", SF_FORMAT_SVX) ;
210 if (do_all || ! strcmp (argv [1], "voc"))
211 { update_header_test ("header.voc", SF_FORMAT_VOC) ;
212 /*-update_seek_short_test ("header_short.voc", SF_FORMAT_VOC) ;-*/
216 if (do_all || ! strcmp (argv [1], "sds"))
217 { update_header_test ("header.sds", SF_FORMAT_SDS) ;
218 /*-update_seek_short_test ("header_short.sds", SF_FORMAT_SDS) ;-*/
222 if (do_all || ! strcmp (argv [1], "mpc2k"))
223 { update_header_test ("header.mpc", SF_FORMAT_MPC2K) ;
224 update_seek_short_test ("header_short.mpc", SF_FORMAT_MPC2K) ;
229 { printf ("Mono : ************************************\n") ;
230 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
231 printf ("Mono : ************************************\n") ;
239 /*============================================================================================
240 ** Here are the test functions.
244 update_header_sub (const char *filename, int typemajor, int write_mode)
245 { SNDFILE *outfile, *infile ;
249 sfinfo.samplerate = 44100 ;
250 sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
251 sfinfo.channels = 1 ;
254 outfile = test_open_file_or_die (filename, write_mode, &sfinfo, SF_TRUE, __LINE__) ;
256 for (k = 0 ; k < BUFFER_LEN ; k++)
257 data_out [k] = k + 1 ;
258 test_write_int_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ;
260 if (typemajor != SF_FORMAT_HTK)
261 { /* The HTK header is not correct when the file is first written. */
262 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
266 sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, 0) ;
269 ** Open file and check log buffer for an error. If header update failed
270 ** the the log buffer will contain errors.
272 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
273 check_log_buffer_or_die (infile, __LINE__) ;
275 if (sfinfo.frames < BUFFER_LEN || sfinfo.frames > BUFFER_LEN + 50)
276 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), BUFFER_LEN) ;
277 dump_log_buffer (infile) ;
281 test_read_int_or_die (infile, 0, data_in, BUFFER_LEN, __LINE__) ;
282 for (k = 0 ; k < BUFFER_LEN ; k++)
283 if (data_out [k] != k + 1)
284 printf ("Error : line %d\n", __LINE__) ;
288 /* Set auto update on. */
289 sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
291 /* Write more data_out. */
292 for (k = 0 ; k < BUFFER_LEN ; k++)
293 data_out [k] = k + 2 ;
294 test_write_int_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ;
296 /* Open file again and make sure no errors in log buffer. */
297 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
298 check_log_buffer_or_die (infile, __LINE__) ;
300 if (sfinfo.frames < 2 * BUFFER_LEN || sfinfo.frames > 2 * BUFFER_LEN + 50)
301 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * BUFFER_LEN) ;
302 dump_log_buffer (infile) ;
311 } /* update_header_sub */
314 update_header_test (const char *filename, int typemajor)
316 print_test_name ("update_header_test", filename) ;
318 update_header_sub (filename, typemajor, SFM_WRITE) ;
319 update_header_sub (filename, typemajor, SFM_RDWR) ;
323 } /* update_header_test */
325 /*==============================================================================
330 update_seek_[+ (get "name") +]_test (const char *filename, int filetype)
331 { SNDFILE *outfile, *infile ;
334 [+ (get "name") +] buffer [8] ;
337 print_test_name ("update_seek_[+ (get "name") +]_test", filename) ;
339 memset (buffer, 0, sizeof (buffer)) ;
341 /* Create sound outfile with no data. */
342 sfinfo.format = filetype | [+ (get "format") +] ;
343 sfinfo.samplerate = 48000 ;
344 sfinfo.channels = 2 ;
346 if (sf_format_check (&sfinfo) == SF_FALSE)
347 sfinfo.channels = 1 ;
349 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
352 /* Open again for read/write. */
353 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
356 ** In auto header update mode, seeking to the end of the file with
357 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
358 ** will seek to 0 anyway
360 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
361 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
365 /* Now write some frames. */
366 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
368 for (k = 0 ; k < 6 ; k++)
369 { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
370 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
372 /* Open file again and make sure no errors in log buffer. */
373 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
374 check_log_buffer_or_die (infile, __LINE__) ;
377 if (sfinfo.frames != k * frames)
378 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %ld)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), SF_COUNT_TO_LONG (k + frames)) ;
379 dump_log_buffer (infile) ;
384 test_write_[+ (get "name") +]_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
386 test_writef_[+ (get "name") +]_or_die (outfile, k, buffer, frames, __LINE__) ;
394 } /* update_seek_[+ (get "name") +]_test */
400 header_shrink_test (const char *filename, int filetype)
401 { SNDFILE *outfile, *infile ;
404 float buffer [8], bufferin [8] ;
406 print_test_name ("header_shrink_test", filename) ;
408 memset (&sfinfo, 0, sizeof (sfinfo)) ;
409 sfinfo.samplerate = 44100 ;
410 sfinfo.format = filetype | SF_FORMAT_FLOAT ;
411 sfinfo.channels = 1 ;
413 memset (buffer, 0xA0, sizeof (buffer)) ;
415 /* Now write some frames. */
416 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
418 /* Test the file with extra header data. */
419 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
421 sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
422 sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, SF_FALSE) ;
423 sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
425 test_writef_float_or_die (outfile, 0, buffer, frames, __LINE__) ;
428 /* Open again for read. */
429 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
431 test_readf_float_or_die (infile, 0, bufferin, frames, __LINE__) ;
434 compare_float_or_die (buffer, bufferin, frames, __LINE__) ;
439 } /* header_shrink_test */
443 extra_header_test (const char *filename, int filetype)
444 { SNDFILE *outfile, *infile ;
450 print_test_name ("extra_header_test", filename) ;
452 sfinfo.samplerate = 44100 ;
453 sfinfo.format = (filetype | SF_FORMAT_PCM_16) ;
454 sfinfo.channels = 1 ;
456 memset (buffer, 0xA0, sizeof (buffer)) ;
458 /* Now write some frames. */
459 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
461 /* Test the file with extra header data. */
462 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, [+ (tpl-file-line "%2$d") +]) ;
463 sf_set_string (outfile, SF_STR_TITLE, filename) ;
464 test_writef_short_or_die (outfile, k, buffer, frames, [+ (tpl-file-line "%2$d") +]) ;
465 sf_set_string (outfile, SF_STR_COPYRIGHT, "(c) 1980 Erik") ;
470 ** Erik de Castro Lopo <erikd@mega-nerd.com> May 23 2004.
472 ** This file has extra string data in the header and therefore cannot
473 ** currently be opened in SFM_RDWR mode. This is fixable, but its in
474 ** a part of the code I don't want to fiddle with until the Ogg/Vorbis
475 ** integration is done.
478 if ((infile = sf_open (filename, SFM_RDWR, &sfinfo)) != NULL)
479 { printf ("\n\nError : should not be able to open this file in SFM_RDWR.\n\n") ;
488 hexdump_file (filename, 0, 100000) ;
490 /* Open again for read/write. */
491 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, [+ (tpl-file-line "%2$d") +]) ;
494 ** In auto header update mode, seeking to the end of the file with
495 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
496 ** will seek to 0 anyway
498 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
499 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
503 /* Now write some frames. */
504 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
506 for (k = 1 ; k < 6 ; k++)
508 printf ("\n*** pass %d\n", k) ;
509 memset (buffer, 0xA0 + k, sizeof (buffer)) ;
512 test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, [+ (tpl-file-line "%2$d") +]) ;
513 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, [+ (tpl-file-line "%2$d") +]) ;
515 /* Open file again and make sure no errors in log buffer. */
517 { infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, [+ (tpl-file-line "%2$d") +]) ;
518 check_log_buffer_or_die (infile, [+ (tpl-file-line "%2$d") +]) ;
522 if (sfinfo.frames != k * frames)
523 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %ld)\n", [+ (tpl-file-line "%2$d") +], SF_COUNT_TO_LONG (sfinfo.frames), SF_COUNT_TO_LONG (k + frames)) ;
524 dump_log_buffer (infile) ;
529 test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, [+ (tpl-file-line "%2$d") +]) ;
531 test_writef_short_or_die (outfile, k, buffer, frames, [+ (tpl-file-line "%2$d") +]) ;
532 hexdump_file (filename, 0, 100000) ;
541 } /* extra_header_test */