comparison src/libsndfile-1.0.27/tests/header_test.tpl @ 40:1df64224f5ac

Current libsndfile source
author Chris Cannam
date Tue, 18 Oct 2016 13:22:47 +0100
parents
children
comparison
equal deleted inserted replaced
39:7ddb4fc30dac 40:1df64224f5ac
1 [+ AutoGen5 template c +]
2 /*
3 ** Copyright (C) 2001-2012 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 #include <inttypes.h>
26 #include <math.h>
27
28 #include <sys/stat.h>
29
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #if (HAVE_DECL_S_IRGRP == 0)
35 #include <sf_unistd.h>
36 #endif
37
38 #if (defined (WIN32) || defined (_WIN32))
39 #include <io.h>
40 #include <direct.h>
41 #endif
42
43 #include <sndfile.h>
44
45 #include "utils.h"
46
47 #define BUFFER_LEN (1 << 10)
48 #define LOG_BUFFER_SIZE 1024
49
50 static void update_header_test (const char *filename, int typemajor) ;
51
52 [+ FOR data_type
53 +]static void update_seek_[+ (get "name") +]_test (const char *filename, int filetype) ;
54 [+ ENDFOR data_type
55 +]
56
57 static void extra_header_test (const char *filename, int filetype) ;
58
59 static void header_shrink_test (const char *filename, int filetype) ;
60
61 /* Force the start of this buffer to be double aligned. Sparc-solaris will
62 ** choke if its not.
63 */
64 static int data_out [BUFFER_LEN] ;
65 static int data_in [BUFFER_LEN] ;
66
67 int
68 main (int argc, char *argv [])
69 { int do_all = 0 ;
70 int test_count = 0 ;
71
72 if (argc != 2)
73 { printf ("Usage : %s <test>\n", argv [0]) ;
74 printf (" Where <test> is one of the following:\n") ;
75 printf (" wav - test WAV file peak chunk\n") ;
76 printf (" aiff - test AIFF file PEAK chunk\n") ;
77 printf (" all - perform all tests\n") ;
78 exit (1) ;
79 } ;
80
81 do_all= !strcmp (argv [1], "all") ;
82
83 if (do_all || ! strcmp (argv [1], "wav"))
84 { update_header_test ("header.wav", SF_FORMAT_WAV) ;
85 update_seek_short_test ("header_short.wav", SF_FORMAT_WAV) ;
86 update_seek_int_test ("header_int.wav", SF_FORMAT_WAV) ;
87 update_seek_float_test ("header_float.wav", SF_FORMAT_WAV) ;
88 update_seek_double_test ("header_double.wav", SF_FORMAT_WAV) ;
89 header_shrink_test ("header_shrink.wav", SF_FORMAT_WAV) ;
90 extra_header_test ("extra.wav", SF_FORMAT_WAV) ;
91
92 update_header_test ("header.wavex", SF_FORMAT_WAVEX) ;
93 update_seek_short_test ("header_short.wavex", SF_FORMAT_WAVEX) ;
94 update_seek_int_test ("header_int.wavex", SF_FORMAT_WAVEX) ;
95 update_seek_float_test ("header_float.wavex", SF_FORMAT_WAVEX) ;
96 update_seek_double_test ("header_double.wavex", SF_FORMAT_WAVEX) ;
97 header_shrink_test ("header_shrink.wavex", SF_FORMAT_WAVEX) ;
98 extra_header_test ("extra.wavex", SF_FORMAT_WAVEX) ;
99 test_count++ ;
100 } ;
101
102 if (do_all || ! strcmp (argv [1], "aiff"))
103 { update_header_test ("header.aiff", SF_FORMAT_AIFF) ;
104 update_seek_short_test ("header_short.aiff", SF_FORMAT_AIFF) ;
105 update_seek_int_test ("header_int.aiff", SF_FORMAT_AIFF) ;
106 update_seek_float_test ("header_float.aiff", SF_FORMAT_AIFF) ;
107 update_seek_double_test ("header_double.aiff", SF_FORMAT_AIFF) ;
108 header_shrink_test ("header_shrink.wav", SF_FORMAT_AIFF) ;
109 extra_header_test ("extra.aiff", SF_FORMAT_AIFF) ;
110 test_count++ ;
111 } ;
112
113 if (do_all || ! strcmp (argv [1], "au"))
114 { update_header_test ("header.au", SF_FORMAT_AU) ;
115 update_seek_short_test ("header_short.au", SF_FORMAT_AU) ;
116 update_seek_int_test ("header_int.au", SF_FORMAT_AU) ;
117 update_seek_float_test ("header_float.au", SF_FORMAT_AU) ;
118 update_seek_double_test ("header_double.au", SF_FORMAT_AU) ;
119 test_count++ ;
120 } ;
121
122 if (do_all || ! strcmp (argv [1], "caf"))
123 { update_header_test ("header.caf", SF_FORMAT_CAF) ;
124 update_seek_short_test ("header_short.caf", SF_FORMAT_CAF) ;
125 update_seek_int_test ("header_int.caf", SF_FORMAT_CAF) ;
126 update_seek_float_test ("header_float.caf", SF_FORMAT_CAF) ;
127 update_seek_double_test ("header_double.caf", SF_FORMAT_CAF) ;
128 /* extra_header_test ("extra.caf", SF_FORMAT_CAF) ; */
129 test_count++ ;
130 } ;
131
132 if (do_all || ! strcmp (argv [1], "nist"))
133 { update_header_test ("header.nist", SF_FORMAT_NIST) ;
134 update_seek_short_test ("header_short.nist", SF_FORMAT_NIST) ;
135 update_seek_int_test ("header_int.nist", SF_FORMAT_NIST) ;
136 test_count++ ;
137 } ;
138
139 if (do_all || ! strcmp (argv [1], "paf"))
140 { update_header_test ("header.paf", SF_FORMAT_PAF) ;
141 update_seek_short_test ("header_short.paf", SF_FORMAT_PAF) ;
142 test_count++ ;
143 } ;
144
145 if (do_all || ! strcmp (argv [1], "ircam"))
146 { update_header_test ("header.ircam", SF_FORMAT_IRCAM) ;
147 update_seek_short_test ("header_short.ircam", SF_FORMAT_IRCAM) ;
148 test_count++ ;
149 } ;
150
151 if (do_all || ! strcmp (argv [1], "w64"))
152 { update_header_test ("header.w64", SF_FORMAT_W64) ;
153 update_seek_short_test ("header_short.w64", SF_FORMAT_W64) ;
154 update_seek_int_test ("header_int.w64", SF_FORMAT_W64) ;
155 update_seek_float_test ("header_float.w64", SF_FORMAT_W64) ;
156 update_seek_double_test ("header_double.w64", SF_FORMAT_W64) ;
157 test_count++ ;
158 } ;
159
160 if (do_all || ! strcmp (argv [1], "rf64"))
161 { update_header_test ("header.rf64", SF_FORMAT_RF64) ;
162 update_seek_short_test ("header_short.rf64", SF_FORMAT_RF64) ;
163 update_seek_int_test ("header_int.rf64", SF_FORMAT_RF64) ;
164 update_seek_float_test ("header_float.rf64", SF_FORMAT_RF64) ;
165 update_seek_double_test ("header_double.rf64", SF_FORMAT_RF64) ;
166 test_count++ ;
167 } ;
168
169 if (do_all || ! strcmp (argv [1], "mat4"))
170 { update_header_test ("header.mat4", SF_FORMAT_MAT4) ;
171 update_seek_short_test ("header_short.mat4", SF_FORMAT_MAT4) ;
172 update_seek_int_test ("header_int.mat4", SF_FORMAT_MAT4) ;
173 update_seek_float_test ("header_float.mat4", SF_FORMAT_MAT4) ;
174 update_seek_double_test ("header_double.mat4", SF_FORMAT_MAT4) ;
175 test_count++ ;
176 } ;
177
178 if (do_all || ! strcmp (argv [1], "mat5"))
179 { update_header_test ("header.mat5", SF_FORMAT_MAT5) ;
180 update_seek_short_test ("header_short.mat5", SF_FORMAT_MAT5) ;
181 update_seek_int_test ("header_int.mat5", SF_FORMAT_MAT5) ;
182 update_seek_float_test ("header_float.mat5", SF_FORMAT_MAT5) ;
183 update_seek_double_test ("header_double.mat5", SF_FORMAT_MAT5) ;
184 test_count++ ;
185 } ;
186
187 if (do_all || ! strcmp (argv [1], "pvf"))
188 { update_header_test ("header.pvf", SF_FORMAT_PVF) ;
189 update_seek_short_test ("header_short.pvf", SF_FORMAT_PVF) ;
190 test_count++ ;
191 } ;
192
193 if (do_all || ! strcmp (argv [1], "avr"))
194 { update_header_test ("header.avr", SF_FORMAT_AVR) ;
195 update_seek_short_test ("header_short.avr", SF_FORMAT_AVR) ;
196 test_count++ ;
197 } ;
198
199 if (do_all || ! strcmp (argv [1], "htk"))
200 { update_header_test ("header.htk", SF_FORMAT_HTK) ;
201 update_seek_short_test ("header_short.htk", SF_FORMAT_HTK) ;
202 test_count++ ;
203 } ;
204
205 if (do_all || ! strcmp (argv [1], "svx"))
206 { update_header_test ("header.svx", SF_FORMAT_SVX) ;
207 update_seek_short_test ("header_short.svx", SF_FORMAT_SVX) ;
208 test_count++ ;
209 } ;
210
211 if (do_all || ! strcmp (argv [1], "voc"))
212 { update_header_test ("header.voc", SF_FORMAT_VOC) ;
213 /*-update_seek_short_test ("header_short.voc", SF_FORMAT_VOC) ;-*/
214 test_count++ ;
215 } ;
216
217 if (do_all || ! strcmp (argv [1], "sds"))
218 { update_header_test ("header.sds", SF_FORMAT_SDS) ;
219 /*-update_seek_short_test ("header_short.sds", SF_FORMAT_SDS) ;-*/
220 test_count++ ;
221 } ;
222
223 if (do_all || ! strcmp (argv [1], "mpc2k"))
224 { update_header_test ("header.mpc", SF_FORMAT_MPC2K) ;
225 update_seek_short_test ("header_short.mpc", SF_FORMAT_MPC2K) ;
226 test_count++ ;
227 } ;
228
229 if (test_count == 0)
230 { printf ("Mono : ************************************\n") ;
231 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
232 printf ("Mono : ************************************\n") ;
233 return 1 ;
234 } ;
235
236 return 0 ;
237 } /* main */
238
239
240 /*============================================================================================
241 ** Here are the test functions.
242 */
243
244 static void
245 update_header_sub (const char *filename, int typemajor, int write_mode)
246 { SNDFILE *outfile, *infile ;
247 SF_INFO sfinfo ;
248 int k ;
249
250 sfinfo.samplerate = 44100 ;
251 sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
252 sfinfo.channels = 1 ;
253 sfinfo.frames = 0 ;
254
255 outfile = test_open_file_or_die (filename, write_mode, &sfinfo, SF_TRUE, __LINE__) ;
256
257 for (k = 0 ; k < BUFFER_LEN ; k++)
258 data_out [k] = k + 1 ;
259 test_write_int_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ;
260
261 if (typemajor != SF_FORMAT_HTK)
262 { /* The HTK header is not correct when the file is first written. */
263 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
264 sf_close (infile) ;
265 } ;
266
267 sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, 0) ;
268
269 /*
270 ** Open file and check log buffer for an error. If header update failed
271 ** the the log buffer will contain errors.
272 */
273 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
274 check_log_buffer_or_die (infile, __LINE__) ;
275
276 if (sfinfo.frames < BUFFER_LEN || sfinfo.frames > BUFFER_LEN + 50)
277 { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, BUFFER_LEN) ;
278 dump_log_buffer (infile) ;
279 exit (1) ;
280 } ;
281
282 test_read_int_or_die (infile, 0, data_in, BUFFER_LEN, __LINE__) ;
283 for (k = 0 ; k < BUFFER_LEN ; k++)
284 if (data_out [k] != k + 1)
285 printf ("Error : line %d\n", __LINE__) ;
286
287 sf_close (infile) ;
288
289 /* Set auto update on. */
290 sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
291
292 /* Write more data_out. */
293 for (k = 0 ; k < BUFFER_LEN ; k++)
294 data_out [k] = k + 2 ;
295 test_write_int_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ;
296
297 /* Open file again and make sure no errors in log buffer. */
298 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
299 check_log_buffer_or_die (infile, __LINE__) ;
300
301 if (sfinfo.frames < 2 * BUFFER_LEN || sfinfo.frames > 2 * BUFFER_LEN + 50)
302 { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, 2 * BUFFER_LEN) ;
303 dump_log_buffer (infile) ;
304 exit (1) ;
305 } ;
306
307 sf_close (infile) ;
308
309 sf_close (outfile) ;
310
311 unlink (filename) ;
312 } /* update_header_sub */
313
314 static void
315 update_header_test (const char *filename, int typemajor)
316 {
317 print_test_name ("update_header_test", filename) ;
318
319 update_header_sub (filename, typemajor, SFM_WRITE) ;
320 update_header_sub (filename, typemajor, SFM_RDWR) ;
321
322 unlink (filename) ;
323 puts ("ok") ;
324 } /* update_header_test */
325
326 /*==============================================================================
327 */
328
329 [+ FOR data_type
330 +]static void
331 update_seek_[+ (get "name") +]_test (const char *filename, int filetype)
332 { SNDFILE *outfile, *infile ;
333 SF_INFO sfinfo ;
334 sf_count_t frames ;
335 [+ (get "name") +] buffer [8] ;
336 int k ;
337
338 print_test_name ("update_seek_[+ (get "name") +]_test", filename) ;
339
340 memset (buffer, 0, sizeof (buffer)) ;
341
342 /* Create sound outfile with no data. */
343 sfinfo.format = filetype | [+ (get "format") +] ;
344 sfinfo.samplerate = 48000 ;
345 sfinfo.channels = 2 ;
346
347 if (sf_format_check (&sfinfo) == SF_FALSE)
348 sfinfo.channels = 1 ;
349
350 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
351 sf_close (outfile) ;
352
353 /* Open again for read/write. */
354 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
355
356 /*
357 ** In auto header update mode, seeking to the end of the file with
358 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
359 ** will seek to 0 anyway
360 */
361 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
362 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
363 exit (1) ;
364 } ;
365
366 /* Now write some frames. */
367 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
368
369 for (k = 0 ; k < 6 ; k++)
370 { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
371 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
372
373 /* Open file again and make sure no errors in log buffer. */
374 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
375 check_log_buffer_or_die (infile, __LINE__) ;
376 sf_close (infile) ;
377
378 if (sfinfo.frames != k * frames)
379 { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %" PRId64 ")\n", __LINE__, sfinfo.frames, k + frames) ;
380 dump_log_buffer (infile) ;
381 exit (1) ;
382 } ;
383
384 if ((k & 1) == 0)
385 test_write_[+ (get "name") +]_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
386 else
387 test_writef_[+ (get "name") +]_or_die (outfile, k, buffer, frames, __LINE__) ;
388 } ;
389
390 sf_close (outfile) ;
391 unlink (filename) ;
392
393 puts ("ok") ;
394 return ;
395 } /* update_seek_[+ (get "name") +]_test */
396
397 [+ ENDFOR data_type
398 +]
399
400 static void
401 header_shrink_test (const char *filename, int filetype)
402 { SNDFILE *outfile, *infile ;
403 SF_INFO sfinfo ;
404 sf_count_t frames ;
405 float buffer [8], bufferin [8] ;
406
407 print_test_name ("header_shrink_test", filename) ;
408
409 memset (&sfinfo, 0, sizeof (sfinfo)) ;
410 sfinfo.samplerate = 44100 ;
411 sfinfo.format = filetype | SF_FORMAT_FLOAT ;
412 sfinfo.channels = 1 ;
413
414 memset (buffer, 0xA0, sizeof (buffer)) ;
415
416 /* Now write some frames. */
417 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
418
419 /* Test the file with extra header data. */
420 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
421
422 sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
423 sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, SF_FALSE) ;
424 sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
425
426 test_writef_float_or_die (outfile, 0, buffer, frames, __LINE__) ;
427 sf_close (outfile) ;
428
429 /* Open again for read. */
430 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
431
432 test_readf_float_or_die (infile, 0, bufferin, frames, __LINE__) ;
433 sf_close (infile) ;
434
435 compare_float_or_die (buffer, bufferin, frames, __LINE__) ;
436
437 unlink (filename) ;
438 puts ("ok") ;
439 return ;
440 } /* header_shrink_test */
441
442
443 static void
444 extra_header_test (const char *filename, int filetype)
445 { SNDFILE *outfile, *infile ;
446 SF_INFO sfinfo ;
447 sf_count_t frames ;
448 short buffer [8] ;
449 int k = 0 ;
450
451 print_test_name ("extra_header_test", filename) ;
452
453 sfinfo.samplerate = 44100 ;
454 sfinfo.format = (filetype | SF_FORMAT_PCM_16) ;
455 sfinfo.channels = 1 ;
456
457 memset (buffer, 0xA0, sizeof (buffer)) ;
458
459 /* Now write some frames. */
460 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
461
462 /* Test the file with extra header data. */
463 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, [+ (tpl-file-line "%2$d") +]) ;
464 sf_set_string (outfile, SF_STR_TITLE, filename) ;
465 test_writef_short_or_die (outfile, k, buffer, frames, [+ (tpl-file-line "%2$d") +]) ;
466 sf_set_string (outfile, SF_STR_COPYRIGHT, "(c) 1980 Erik") ;
467 sf_close (outfile) ;
468
469 #if 1
470 /*
471 ** Erik de Castro Lopo <erikd@mega-nerd.com> May 23 2004.
472 **
473 ** This file has extra string data in the header and therefore cannot
474 ** currently be opened in SFM_RDWR mode. This is fixable, but its in
475 ** a part of the code I don't want to fiddle with until the Ogg/Vorbis
476 ** integration is done.
477 */
478
479 if ((infile = sf_open (filename, SFM_RDWR, &sfinfo)) != NULL)
480 { printf ("\n\nError : should not be able to open this file in SFM_RDWR.\n\n") ;
481 exit (1) ;
482 } ;
483
484 unlink (filename) ;
485 puts ("ok") ;
486 return ;
487 #else
488
489 hexdump_file (filename, 0, 100000) ;
490
491 /* Open again for read/write. */
492 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, [+ (tpl-file-line "%2$d") +]) ;
493
494 /*
495 ** In auto header update mode, seeking to the end of the file with
496 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
497 ** will seek to 0 anyway
498 */
499 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
500 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
501 exit (1) ;
502 } ;
503
504 /* Now write some frames. */
505 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
506
507 for (k = 1 ; k < 6 ; k++)
508 {
509 printf ("\n*** pass %d\n", k) ;
510 memset (buffer, 0xA0 + k, sizeof (buffer)) ;
511
512
513 test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, [+ (tpl-file-line "%2$d") +]) ;
514 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, [+ (tpl-file-line "%2$d") +]) ;
515
516 /* Open file again and make sure no errors in log buffer. */
517 if (0)
518 { infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, [+ (tpl-file-line "%2$d") +]) ;
519 check_log_buffer_or_die (infile, [+ (tpl-file-line "%2$d") +]) ;
520 sf_close (infile) ;
521 } ;
522
523 if (sfinfo.frames != k * frames)
524 { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %" PRId64 ")\n", [+ (tpl-file-line "%2$d") +], sfinfo.frames, k + frames) ;
525 dump_log_buffer (infile) ;
526 exit (1) ;
527 } ;
528
529 if ((k & 1) == 0)
530 test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, [+ (tpl-file-line "%2$d") +]) ;
531 else
532 test_writef_short_or_die (outfile, k, buffer, frames, [+ (tpl-file-line "%2$d") +]) ;
533 hexdump_file (filename, 0, 100000) ;
534 } ;
535
536 sf_close (outfile) ;
537 unlink (filename) ;
538
539 puts ("ok") ;
540 return ;
541 #endif
542 } /* extra_header_test */
543