libsndfile compiling.
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / header_test.c
1 /*
2 ** Copyright (C) 2001-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software ; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation ; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY ; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program ; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <sys/stat.h>
26 #include <math.h>
27
28 #if HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 #if (HAVE_DECL_S_IRGRP == 0)
33 #include <sf_unistd.h>
34 #endif
35
36 #if (defined (WIN32) || defined (_WIN32))
37 #include <io.h>
38 #include <direct.h>
39 #endif
40
41 #include <sndfile.h>
42
43 #include "utils.h"
44
45 #define BUFFER_LEN (1<<10)
46 #define LOG_BUFFER_SIZE 1024
47
48 static void update_header_test (const char *filename, int typemajor) ;
49
50 static void update_seek_short_test (const char *filename, int filetype) ;
51 static void update_seek_int_test (const char *filename, int filetype) ;
52 static void update_seek_float_test (const char *filename, int filetype) ;
53 static void update_seek_double_test (const char *filename, int filetype) ;
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 static void
329 update_seek_short_test (const char *filename, int filetype)
330 { SNDFILE *outfile, *infile ;
331 SF_INFO sfinfo ;
332 sf_count_t frames ;
333 short buffer [8] ;
334 int k ;
335
336 print_test_name ("update_seek_short_test", filename) ;
337
338 memset (buffer, 0, sizeof (buffer)) ;
339
340 /* Create sound outfile with no data. */
341 sfinfo.format = filetype | SF_FORMAT_PCM_16 ;
342 sfinfo.samplerate = 48000 ;
343 sfinfo.channels = 2 ;
344
345 if (sf_format_check (&sfinfo) == SF_FALSE)
346 sfinfo.channels = 1 ;
347
348 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
349 sf_close (outfile) ;
350
351 /* Open again for read/write. */
352 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
353
354 /*
355 ** In auto header update mode, seeking to the end of the file with
356 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
357 ** will seek to 0 anyway
358 */
359 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
360 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
361 exit (1) ;
362 } ;
363
364 /* Now write some frames. */
365 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
366
367 for (k = 0 ; k < 6 ; k++)
368 { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
369 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
370
371 /* Open file again and make sure no errors in log buffer. */
372 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
373 check_log_buffer_or_die (infile, __LINE__) ;
374 sf_close (infile) ;
375
376 if (sfinfo.frames != k * frames)
377 { 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)) ;
378 dump_log_buffer (infile) ;
379 exit (1) ;
380 } ;
381
382 if ((k & 1) == 0)
383 test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
384 else
385 test_writef_short_or_die (outfile, k, buffer, frames, __LINE__) ;
386 } ;
387
388 sf_close (outfile) ;
389 unlink (filename) ;
390
391 puts ("ok") ;
392 return ;
393 } /* update_seek_short_test */
394
395 static void
396 update_seek_int_test (const char *filename, int filetype)
397 { SNDFILE *outfile, *infile ;
398 SF_INFO sfinfo ;
399 sf_count_t frames ;
400 int buffer [8] ;
401 int k ;
402
403 print_test_name ("update_seek_int_test", filename) ;
404
405 memset (buffer, 0, sizeof (buffer)) ;
406
407 /* Create sound outfile with no data. */
408 sfinfo.format = filetype | SF_FORMAT_PCM_32 ;
409 sfinfo.samplerate = 48000 ;
410 sfinfo.channels = 2 ;
411
412 if (sf_format_check (&sfinfo) == SF_FALSE)
413 sfinfo.channels = 1 ;
414
415 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
416 sf_close (outfile) ;
417
418 /* Open again for read/write. */
419 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
420
421 /*
422 ** In auto header update mode, seeking to the end of the file with
423 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
424 ** will seek to 0 anyway
425 */
426 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
427 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
428 exit (1) ;
429 } ;
430
431 /* Now write some frames. */
432 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
433
434 for (k = 0 ; k < 6 ; k++)
435 { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
436 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
437
438 /* Open file again and make sure no errors in log buffer. */
439 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
440 check_log_buffer_or_die (infile, __LINE__) ;
441 sf_close (infile) ;
442
443 if (sfinfo.frames != k * frames)
444 { 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)) ;
445 dump_log_buffer (infile) ;
446 exit (1) ;
447 } ;
448
449 if ((k & 1) == 0)
450 test_write_int_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
451 else
452 test_writef_int_or_die (outfile, k, buffer, frames, __LINE__) ;
453 } ;
454
455 sf_close (outfile) ;
456 unlink (filename) ;
457
458 puts ("ok") ;
459 return ;
460 } /* update_seek_int_test */
461
462 static void
463 update_seek_float_test (const char *filename, int filetype)
464 { SNDFILE *outfile, *infile ;
465 SF_INFO sfinfo ;
466 sf_count_t frames ;
467 float buffer [8] ;
468 int k ;
469
470 print_test_name ("update_seek_float_test", filename) ;
471
472 memset (buffer, 0, sizeof (buffer)) ;
473
474 /* Create sound outfile with no data. */
475 sfinfo.format = filetype | SF_FORMAT_FLOAT ;
476 sfinfo.samplerate = 48000 ;
477 sfinfo.channels = 2 ;
478
479 if (sf_format_check (&sfinfo) == SF_FALSE)
480 sfinfo.channels = 1 ;
481
482 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
483 sf_close (outfile) ;
484
485 /* Open again for read/write. */
486 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
487
488 /*
489 ** In auto header update mode, seeking to the end of the file with
490 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
491 ** will seek to 0 anyway
492 */
493 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
494 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
495 exit (1) ;
496 } ;
497
498 /* Now write some frames. */
499 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
500
501 for (k = 0 ; k < 6 ; k++)
502 { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
503 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
504
505 /* Open file again and make sure no errors in log buffer. */
506 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
507 check_log_buffer_or_die (infile, __LINE__) ;
508 sf_close (infile) ;
509
510 if (sfinfo.frames != k * frames)
511 { 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)) ;
512 dump_log_buffer (infile) ;
513 exit (1) ;
514 } ;
515
516 if ((k & 1) == 0)
517 test_write_float_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
518 else
519 test_writef_float_or_die (outfile, k, buffer, frames, __LINE__) ;
520 } ;
521
522 sf_close (outfile) ;
523 unlink (filename) ;
524
525 puts ("ok") ;
526 return ;
527 } /* update_seek_float_test */
528
529 static void
530 update_seek_double_test (const char *filename, int filetype)
531 { SNDFILE *outfile, *infile ;
532 SF_INFO sfinfo ;
533 sf_count_t frames ;
534 double buffer [8] ;
535 int k ;
536
537 print_test_name ("update_seek_double_test", filename) ;
538
539 memset (buffer, 0, sizeof (buffer)) ;
540
541 /* Create sound outfile with no data. */
542 sfinfo.format = filetype | SF_FORMAT_DOUBLE ;
543 sfinfo.samplerate = 48000 ;
544 sfinfo.channels = 2 ;
545
546 if (sf_format_check (&sfinfo) == SF_FALSE)
547 sfinfo.channels = 1 ;
548
549 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
550 sf_close (outfile) ;
551
552 /* Open again for read/write. */
553 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
554
555 /*
556 ** In auto header update mode, seeking to the end of the file with
557 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
558 ** will seek to 0 anyway
559 */
560 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
561 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
562 exit (1) ;
563 } ;
564
565 /* Now write some frames. */
566 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
567
568 for (k = 0 ; k < 6 ; k++)
569 { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
570 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
571
572 /* Open file again and make sure no errors in log buffer. */
573 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
574 check_log_buffer_or_die (infile, __LINE__) ;
575 sf_close (infile) ;
576
577 if (sfinfo.frames != k * frames)
578 { 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)) ;
579 dump_log_buffer (infile) ;
580 exit (1) ;
581 } ;
582
583 if ((k & 1) == 0)
584 test_write_double_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
585 else
586 test_writef_double_or_die (outfile, k, buffer, frames, __LINE__) ;
587 } ;
588
589 sf_close (outfile) ;
590 unlink (filename) ;
591
592 puts ("ok") ;
593 return ;
594 } /* update_seek_double_test */
595
596
597
598 static void
599 header_shrink_test (const char *filename, int filetype)
600 { SNDFILE *outfile, *infile ;
601 SF_INFO sfinfo ;
602 sf_count_t frames ;
603 float buffer [8], bufferin [8] ;
604
605 print_test_name ("header_shrink_test", filename) ;
606
607 memset (&sfinfo, 0, sizeof (sfinfo)) ;
608 sfinfo.samplerate = 44100 ;
609 sfinfo.format = filetype | SF_FORMAT_FLOAT ;
610 sfinfo.channels = 1 ;
611
612 memset (buffer, 0xA0, sizeof (buffer)) ;
613
614 /* Now write some frames. */
615 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
616
617 /* Test the file with extra header data. */
618 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
619
620 sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
621 sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, SF_FALSE) ;
622 sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
623
624 test_writef_float_or_die (outfile, 0, buffer, frames, __LINE__) ;
625 sf_close (outfile) ;
626
627 /* Open again for read. */
628 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
629
630 test_readf_float_or_die (infile, 0, bufferin, frames, __LINE__) ;
631 sf_close (infile) ;
632
633 compare_float_or_die (buffer, bufferin, frames, __LINE__) ;
634
635 unlink (filename) ;
636 puts ("ok") ;
637 return ;
638 } /* header_shrink_test */
639
640
641 static void
642 extra_header_test (const char *filename, int filetype)
643 { SNDFILE *outfile, *infile ;
644 SF_INFO sfinfo ;
645 sf_count_t frames ;
646 short buffer [8] ;
647 int k = 0 ;
648
649 print_test_name ("extra_header_test", filename) ;
650
651 sfinfo.samplerate = 44100 ;
652 sfinfo.format = (filetype | SF_FORMAT_PCM_16) ;
653 sfinfo.channels = 1 ;
654
655 memset (buffer, 0xA0, sizeof (buffer)) ;
656
657 /* Now write some frames. */
658 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
659
660 /* Test the file with extra header data. */
661 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, 462) ;
662 sf_set_string (outfile, SF_STR_TITLE, filename) ;
663 test_writef_short_or_die (outfile, k, buffer, frames, 464) ;
664 sf_set_string (outfile, SF_STR_COPYRIGHT, "(c) 1980 Erik") ;
665 sf_close (outfile) ;
666
667 #if 1
668 /*
669 ** Erik de Castro Lopo <erikd@mega-nerd.com> May 23 2004.
670 **
671 ** This file has extra string data in the header and therefore cannot
672 ** currently be opened in SFM_RDWR mode. This is fixable, but its in
673 ** a part of the code I don't want to fiddle with until the Ogg/Vorbis
674 ** integration is done.
675 */
676
677 if ((infile = sf_open (filename, SFM_RDWR, &sfinfo)) != NULL)
678 { printf ("\n\nError : should not be able to open this file in SFM_RDWR.\n\n") ;
679 exit (1) ;
680 } ;
681
682 unlink (filename) ;
683 puts ("ok") ;
684 return ;
685 #else
686
687 hexdump_file (filename, 0, 100000) ;
688
689 /* Open again for read/write. */
690 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, 491) ;
691
692 /*
693 ** In auto header update mode, seeking to the end of the file with
694 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
695 ** will seek to 0 anyway
696 */
697 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
698 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
699 exit (1) ;
700 } ;
701
702 /* Now write some frames. */
703 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
704
705 for (k = 1 ; k < 6 ; k++)
706 {
707 printf ("\n*** pass %d\n", k) ;
708 memset (buffer, 0xA0 + k, sizeof (buffer)) ;
709
710
711 test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, 512) ;
712 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, 513) ;
713
714 /* Open file again and make sure no errors in log buffer. */
715 if (0)
716 { infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, 517) ;
717 check_log_buffer_or_die (infile, 518) ;
718 sf_close (infile) ;
719 } ;
720
721 if (sfinfo.frames != k * frames)
722 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %ld)\n", 523, SF_COUNT_TO_LONG (sfinfo.frames), SF_COUNT_TO_LONG (k + frames)) ;
723 dump_log_buffer (infile) ;
724 exit (1) ;
725 } ;
726
727 if ((k & 1) == 0)
728 test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, 529) ;
729 else
730 test_writef_short_or_die (outfile, k, buffer, frames, 531) ;
731 hexdump_file (filename, 0, 100000) ;
732 } ;
733
734 sf_close (outfile) ;
735 unlink (filename) ;
736
737 puts ("ok") ;
738 return ;
739 #endif
740 } /* extra_header_test */
741