User Manual first draft.
[Faustine.git] / interpreter / lib / src / libsndfile-1.0.25 / tests / header_test.tpl
1 [+ AutoGen5 template c +]
2 /*
3 ** Copyright (C) 2001-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
4 **
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.
9 **
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.
14 **
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.
18 */
19
20 #include "sfconfig.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <sys/stat.h>
27 #include <math.h>
28
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #if (HAVE_DECL_S_IRGRP == 0)
34 #include <sf_unistd.h>
35 #endif
36
37 #if (defined (WIN32) || defined (_WIN32))
38 #include <io.h>
39 #include <direct.h>
40 #endif
41
42 #include <sndfile.h>
43
44 #include "utils.h"
45
46 #define BUFFER_LEN (1<<10)
47 #define LOG_BUFFER_SIZE 1024
48
49 static void update_header_test (const char *filename, int typemajor) ;
50
51 [+ FOR data_type
52 +]static void update_seek_[+ (get "name") +]_test (const char *filename, int filetype) ;
53 [+ ENDFOR data_type
54 +]
55
56 static void extra_header_test (const char *filename, int filetype) ;
57
58 static void header_shrink_test (const char *filename, int filetype) ;
59
60 /* Force the start of this buffer to be double aligned. Sparc-solaris will
61 ** choke if its not.
62 */
63 static int data_out [BUFFER_LEN] ;
64 static int data_in [BUFFER_LEN] ;
65
66 int
67 main (int argc, char *argv [])
68 { int do_all = 0 ;
69 int test_count = 0 ;
70
71 if (argc != 2)
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") ;
77 exit (1) ;
78 } ;
79
80 do_all=!strcmp (argv [1], "all") ;
81
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) ;
90
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) ;
98 test_count++ ;
99 } ;
100
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) ;
109 test_count++ ;
110 } ;
111
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) ;
118 test_count++ ;
119 } ;
120
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) ; */
128 test_count++ ;
129 } ;
130
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) ;
135 test_count++ ;
136 } ;
137
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) ;
141 test_count++ ;
142 } ;
143
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) ;
147 test_count++ ;
148 } ;
149
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) ;
156 test_count++ ;
157 } ;
158
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) ;
165 test_count++ ;
166 } ;
167
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) ;
174 test_count++ ;
175 } ;
176
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) ;
183 test_count++ ;
184 } ;
185
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) ;
189 test_count++ ;
190 } ;
191
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) ;
195 test_count++ ;
196 } ;
197
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) ;
201 test_count++ ;
202 } ;
203
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) ;
207 test_count++ ;
208 } ;
209
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) ;-*/
213 test_count++ ;
214 } ;
215
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) ;-*/
219 test_count++ ;
220 } ;
221
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) ;
225 test_count++ ;
226 } ;
227
228 if (test_count == 0)
229 { printf ("Mono : ************************************\n") ;
230 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
231 printf ("Mono : ************************************\n") ;
232 return 1 ;
233 } ;
234
235 return 0 ;
236 } /* main */
237
238
239 /*============================================================================================
240 ** Here are the test functions.
241 */
242
243 static void
244 update_header_sub (const char *filename, int typemajor, int write_mode)
245 { SNDFILE *outfile, *infile ;
246 SF_INFO sfinfo ;
247 int k ;
248
249 sfinfo.samplerate = 44100 ;
250 sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
251 sfinfo.channels = 1 ;
252 sfinfo.frames = 0 ;
253
254 outfile = test_open_file_or_die (filename, write_mode, &sfinfo, SF_TRUE, __LINE__) ;
255
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__) ;
259
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__) ;
263 sf_close (infile) ;
264 } ;
265
266 sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, 0) ;
267
268 /*
269 ** Open file and check log buffer for an error. If header update failed
270 ** the the log buffer will contain errors.
271 */
272 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
273 check_log_buffer_or_die (infile, __LINE__) ;
274
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) ;
278 exit (1) ;
279 } ;
280
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__) ;
285
286 sf_close (infile) ;
287
288 /* Set auto update on. */
289 sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
290
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__) ;
295
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__) ;
299
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) ;
303 exit (1) ;
304 } ;
305
306 sf_close (infile) ;
307
308 sf_close (outfile) ;
309
310 unlink (filename) ;
311 } /* update_header_sub */
312
313 static void
314 update_header_test (const char *filename, int typemajor)
315 {
316 print_test_name ("update_header_test", filename) ;
317
318 update_header_sub (filename, typemajor, SFM_WRITE) ;
319 update_header_sub (filename, typemajor, SFM_RDWR) ;
320
321 unlink (filename) ;
322 puts ("ok") ;
323 } /* update_header_test */
324
325 /*==============================================================================
326 */
327
328 [+ FOR data_type
329 +]static void
330 update_seek_[+ (get "name") +]_test (const char *filename, int filetype)
331 { SNDFILE *outfile, *infile ;
332 SF_INFO sfinfo ;
333 sf_count_t frames ;
334 [+ (get "name") +] buffer [8] ;
335 int k ;
336
337 print_test_name ("update_seek_[+ (get "name") +]_test", filename) ;
338
339 memset (buffer, 0, sizeof (buffer)) ;
340
341 /* Create sound outfile with no data. */
342 sfinfo.format = filetype | [+ (get "format") +] ;
343 sfinfo.samplerate = 48000 ;
344 sfinfo.channels = 2 ;
345
346 if (sf_format_check (&sfinfo) == SF_FALSE)
347 sfinfo.channels = 1 ;
348
349 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
350 sf_close (outfile) ;
351
352 /* Open again for read/write. */
353 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
354
355 /*
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
359 */
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)) ;
362 exit (1) ;
363 } ;
364
365 /* Now write some frames. */
366 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
367
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__) ;
371
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__) ;
375 sf_close (infile) ;
376
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) ;
380 exit (1) ;
381 } ;
382
383 if ((k & 1) == 0)
384 test_write_[+ (get "name") +]_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
385 else
386 test_writef_[+ (get "name") +]_or_die (outfile, k, buffer, frames, __LINE__) ;
387 } ;
388
389 sf_close (outfile) ;
390 unlink (filename) ;
391
392 puts ("ok") ;
393 return ;
394 } /* update_seek_[+ (get "name") +]_test */
395
396 [+ ENDFOR data_type
397 +]
398
399 static void
400 header_shrink_test (const char *filename, int filetype)
401 { SNDFILE *outfile, *infile ;
402 SF_INFO sfinfo ;
403 sf_count_t frames ;
404 float buffer [8], bufferin [8] ;
405
406 print_test_name ("header_shrink_test", filename) ;
407
408 memset (&sfinfo, 0, sizeof (sfinfo)) ;
409 sfinfo.samplerate = 44100 ;
410 sfinfo.format = filetype | SF_FORMAT_FLOAT ;
411 sfinfo.channels = 1 ;
412
413 memset (buffer, 0xA0, sizeof (buffer)) ;
414
415 /* Now write some frames. */
416 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
417
418 /* Test the file with extra header data. */
419 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
420
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) ;
424
425 test_writef_float_or_die (outfile, 0, buffer, frames, __LINE__) ;
426 sf_close (outfile) ;
427
428 /* Open again for read. */
429 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
430
431 test_readf_float_or_die (infile, 0, bufferin, frames, __LINE__) ;
432 sf_close (infile) ;
433
434 compare_float_or_die (buffer, bufferin, frames, __LINE__) ;
435
436 unlink (filename) ;
437 puts ("ok") ;
438 return ;
439 } /* header_shrink_test */
440
441
442 static void
443 extra_header_test (const char *filename, int filetype)
444 { SNDFILE *outfile, *infile ;
445 SF_INFO sfinfo ;
446 sf_count_t frames ;
447 short buffer [8] ;
448 int k = 0 ;
449
450 print_test_name ("extra_header_test", filename) ;
451
452 sfinfo.samplerate = 44100 ;
453 sfinfo.format = (filetype | SF_FORMAT_PCM_16) ;
454 sfinfo.channels = 1 ;
455
456 memset (buffer, 0xA0, sizeof (buffer)) ;
457
458 /* Now write some frames. */
459 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
460
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") ;
466 sf_close (outfile) ;
467
468 #if 1
469 /*
470 ** Erik de Castro Lopo <erikd@mega-nerd.com> May 23 2004.
471 **
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.
476 */
477
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") ;
480 exit (1) ;
481 } ;
482
483 unlink (filename) ;
484 puts ("ok") ;
485 return ;
486 #else
487
488 hexdump_file (filename, 0, 100000) ;
489
490 /* Open again for read/write. */
491 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, [+ (tpl-file-line "%2$d") +]) ;
492
493 /*
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
497 */
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)) ;
500 exit (1) ;
501 } ;
502
503 /* Now write some frames. */
504 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
505
506 for (k = 1 ; k < 6 ; k++)
507 {
508 printf ("\n*** pass %d\n", k) ;
509 memset (buffer, 0xA0 + k, sizeof (buffer)) ;
510
511
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") +]) ;
514
515 /* Open file again and make sure no errors in log buffer. */
516 if (0)
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") +]) ;
519 sf_close (infile) ;
520 } ;
521
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) ;
525 exit (1) ;
526 } ;
527
528 if ((k & 1) == 0)
529 test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, [+ (tpl-file-line "%2$d") +]) ;
530 else
531 test_writef_short_or_die (outfile, k, buffer, frames, [+ (tpl-file-line "%2$d") +]) ;
532 hexdump_file (filename, 0, 100000) ;
533 } ;
534
535 sf_close (outfile) ;
536 unlink (filename) ;
537
538 puts ("ok") ;
539 return ;
540 #endif
541 } /* extra_header_test */
542