comparison src/libsndfile-1.0.27/tests/header_test.c @ 125:cd6cdf86811e

Current libsndfile source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:22:47 +0100
parents
children
comparison
equal deleted inserted replaced
124:e3d5853d5918 125:cd6cdf86811e
1 /*
2 ** Copyright (C) 2001-2012 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 #include <inttypes.h>
25 #include <math.h>
26
27 #include <sys/stat.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 static void update_seek_short_test (const char *filename, int filetype) ;
52 static void update_seek_int_test (const char *filename, int filetype) ;
53 static void update_seek_float_test (const char *filename, int filetype) ;
54 static void update_seek_double_test (const char *filename, int filetype) ;
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 static void
330 update_seek_short_test (const char *filename, int filetype)
331 { SNDFILE *outfile, *infile ;
332 SF_INFO sfinfo ;
333 sf_count_t frames ;
334 short buffer [8] ;
335 int k ;
336
337 print_test_name ("update_seek_short_test", filename) ;
338
339 memset (buffer, 0, sizeof (buffer)) ;
340
341 /* Create sound outfile with no data. */
342 sfinfo.format = filetype | SF_FORMAT_PCM_16 ;
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 (%" PRId64 " should be %" PRId64 ")\n", __LINE__, sfinfo.frames, k + frames) ;
379 dump_log_buffer (infile) ;
380 exit (1) ;
381 } ;
382
383 if ((k & 1) == 0)
384 test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
385 else
386 test_writef_short_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_short_test */
395
396 static void
397 update_seek_int_test (const char *filename, int filetype)
398 { SNDFILE *outfile, *infile ;
399 SF_INFO sfinfo ;
400 sf_count_t frames ;
401 int buffer [8] ;
402 int k ;
403
404 print_test_name ("update_seek_int_test", filename) ;
405
406 memset (buffer, 0, sizeof (buffer)) ;
407
408 /* Create sound outfile with no data. */
409 sfinfo.format = filetype | SF_FORMAT_PCM_32 ;
410 sfinfo.samplerate = 48000 ;
411 sfinfo.channels = 2 ;
412
413 if (sf_format_check (&sfinfo) == SF_FALSE)
414 sfinfo.channels = 1 ;
415
416 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
417 sf_close (outfile) ;
418
419 /* Open again for read/write. */
420 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
421
422 /*
423 ** In auto header update mode, seeking to the end of the file with
424 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
425 ** will seek to 0 anyway
426 */
427 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
428 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
429 exit (1) ;
430 } ;
431
432 /* Now write some frames. */
433 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
434
435 for (k = 0 ; k < 6 ; k++)
436 { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
437 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
438
439 /* Open file again and make sure no errors in log buffer. */
440 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
441 check_log_buffer_or_die (infile, __LINE__) ;
442 sf_close (infile) ;
443
444 if (sfinfo.frames != k * frames)
445 { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %" PRId64 ")\n", __LINE__, sfinfo.frames, k + frames) ;
446 dump_log_buffer (infile) ;
447 exit (1) ;
448 } ;
449
450 if ((k & 1) == 0)
451 test_write_int_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
452 else
453 test_writef_int_or_die (outfile, k, buffer, frames, __LINE__) ;
454 } ;
455
456 sf_close (outfile) ;
457 unlink (filename) ;
458
459 puts ("ok") ;
460 return ;
461 } /* update_seek_int_test */
462
463 static void
464 update_seek_float_test (const char *filename, int filetype)
465 { SNDFILE *outfile, *infile ;
466 SF_INFO sfinfo ;
467 sf_count_t frames ;
468 float buffer [8] ;
469 int k ;
470
471 print_test_name ("update_seek_float_test", filename) ;
472
473 memset (buffer, 0, sizeof (buffer)) ;
474
475 /* Create sound outfile with no data. */
476 sfinfo.format = filetype | SF_FORMAT_FLOAT ;
477 sfinfo.samplerate = 48000 ;
478 sfinfo.channels = 2 ;
479
480 if (sf_format_check (&sfinfo) == SF_FALSE)
481 sfinfo.channels = 1 ;
482
483 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
484 sf_close (outfile) ;
485
486 /* Open again for read/write. */
487 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
488
489 /*
490 ** In auto header update mode, seeking to the end of the file with
491 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
492 ** will seek to 0 anyway
493 */
494 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
495 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
496 exit (1) ;
497 } ;
498
499 /* Now write some frames. */
500 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
501
502 for (k = 0 ; k < 6 ; k++)
503 { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
504 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
505
506 /* Open file again and make sure no errors in log buffer. */
507 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
508 check_log_buffer_or_die (infile, __LINE__) ;
509 sf_close (infile) ;
510
511 if (sfinfo.frames != k * frames)
512 { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %" PRId64 ")\n", __LINE__, sfinfo.frames, k + frames) ;
513 dump_log_buffer (infile) ;
514 exit (1) ;
515 } ;
516
517 if ((k & 1) == 0)
518 test_write_float_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
519 else
520 test_writef_float_or_die (outfile, k, buffer, frames, __LINE__) ;
521 } ;
522
523 sf_close (outfile) ;
524 unlink (filename) ;
525
526 puts ("ok") ;
527 return ;
528 } /* update_seek_float_test */
529
530 static void
531 update_seek_double_test (const char *filename, int filetype)
532 { SNDFILE *outfile, *infile ;
533 SF_INFO sfinfo ;
534 sf_count_t frames ;
535 double buffer [8] ;
536 int k ;
537
538 print_test_name ("update_seek_double_test", filename) ;
539
540 memset (buffer, 0, sizeof (buffer)) ;
541
542 /* Create sound outfile with no data. */
543 sfinfo.format = filetype | SF_FORMAT_DOUBLE ;
544 sfinfo.samplerate = 48000 ;
545 sfinfo.channels = 2 ;
546
547 if (sf_format_check (&sfinfo) == SF_FALSE)
548 sfinfo.channels = 1 ;
549
550 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
551 sf_close (outfile) ;
552
553 /* Open again for read/write. */
554 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
555
556 /*
557 ** In auto header update mode, seeking to the end of the file with
558 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
559 ** will seek to 0 anyway
560 */
561 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
562 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
563 exit (1) ;
564 } ;
565
566 /* Now write some frames. */
567 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
568
569 for (k = 0 ; k < 6 ; k++)
570 { test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
571 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
572
573 /* Open file again and make sure no errors in log buffer. */
574 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
575 check_log_buffer_or_die (infile, __LINE__) ;
576 sf_close (infile) ;
577
578 if (sfinfo.frames != k * frames)
579 { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %" PRId64 ")\n", __LINE__, sfinfo.frames, k + frames) ;
580 dump_log_buffer (infile) ;
581 exit (1) ;
582 } ;
583
584 if ((k & 1) == 0)
585 test_write_double_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
586 else
587 test_writef_double_or_die (outfile, k, buffer, frames, __LINE__) ;
588 } ;
589
590 sf_close (outfile) ;
591 unlink (filename) ;
592
593 puts ("ok") ;
594 return ;
595 } /* update_seek_double_test */
596
597
598
599 static void
600 header_shrink_test (const char *filename, int filetype)
601 { SNDFILE *outfile, *infile ;
602 SF_INFO sfinfo ;
603 sf_count_t frames ;
604 float buffer [8], bufferin [8] ;
605
606 print_test_name ("header_shrink_test", filename) ;
607
608 memset (&sfinfo, 0, sizeof (sfinfo)) ;
609 sfinfo.samplerate = 44100 ;
610 sfinfo.format = filetype | SF_FORMAT_FLOAT ;
611 sfinfo.channels = 1 ;
612
613 memset (buffer, 0xA0, sizeof (buffer)) ;
614
615 /* Now write some frames. */
616 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
617
618 /* Test the file with extra header data. */
619 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
620
621 sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_TRUE) ;
622 sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, SF_FALSE) ;
623 sf_command (outfile, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
624
625 test_writef_float_or_die (outfile, 0, buffer, frames, __LINE__) ;
626 sf_close (outfile) ;
627
628 /* Open again for read. */
629 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
630
631 test_readf_float_or_die (infile, 0, bufferin, frames, __LINE__) ;
632 sf_close (infile) ;
633
634 compare_float_or_die (buffer, bufferin, frames, __LINE__) ;
635
636 unlink (filename) ;
637 puts ("ok") ;
638 return ;
639 } /* header_shrink_test */
640
641
642 static void
643 extra_header_test (const char *filename, int filetype)
644 { SNDFILE *outfile, *infile ;
645 SF_INFO sfinfo ;
646 sf_count_t frames ;
647 short buffer [8] ;
648 int k = 0 ;
649
650 print_test_name ("extra_header_test", filename) ;
651
652 sfinfo.samplerate = 44100 ;
653 sfinfo.format = (filetype | SF_FORMAT_PCM_16) ;
654 sfinfo.channels = 1 ;
655
656 memset (buffer, 0xA0, sizeof (buffer)) ;
657
658 /* Now write some frames. */
659 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
660
661 /* Test the file with extra header data. */
662 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, 463) ;
663 sf_set_string (outfile, SF_STR_TITLE, filename) ;
664 test_writef_short_or_die (outfile, k, buffer, frames, 465) ;
665 sf_set_string (outfile, SF_STR_COPYRIGHT, "(c) 1980 Erik") ;
666 sf_close (outfile) ;
667
668 #if 1
669 /*
670 ** Erik de Castro Lopo <erikd@mega-nerd.com> May 23 2004.
671 **
672 ** This file has extra string data in the header and therefore cannot
673 ** currently be opened in SFM_RDWR mode. This is fixable, but its in
674 ** a part of the code I don't want to fiddle with until the Ogg/Vorbis
675 ** integration is done.
676 */
677
678 if ((infile = sf_open (filename, SFM_RDWR, &sfinfo)) != NULL)
679 { printf ("\n\nError : should not be able to open this file in SFM_RDWR.\n\n") ;
680 exit (1) ;
681 } ;
682
683 unlink (filename) ;
684 puts ("ok") ;
685 return ;
686 #else
687
688 hexdump_file (filename, 0, 100000) ;
689
690 /* Open again for read/write. */
691 outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, 492) ;
692
693 /*
694 ** In auto header update mode, seeking to the end of the file with
695 ** SEEK_SET will fail from the 2nd seek on. seeking to 0, SEEK_END
696 ** will seek to 0 anyway
697 */
698 if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
699 { printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
700 exit (1) ;
701 } ;
702
703 /* Now write some frames. */
704 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
705
706 for (k = 1 ; k < 6 ; k++)
707 {
708 printf ("\n*** pass %d\n", k) ;
709 memset (buffer, 0xA0 + k, sizeof (buffer)) ;
710
711
712 test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, 513) ;
713 test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, 514) ;
714
715 /* Open file again and make sure no errors in log buffer. */
716 if (0)
717 { infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, 518) ;
718 check_log_buffer_or_die (infile, 519) ;
719 sf_close (infile) ;
720 } ;
721
722 if (sfinfo.frames != k * frames)
723 { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %" PRId64 ")\n", 524, sfinfo.frames, k + frames) ;
724 dump_log_buffer (infile) ;
725 exit (1) ;
726 } ;
727
728 if ((k & 1) == 0)
729 test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, 530) ;
730 else
731 test_writef_short_or_die (outfile, k, buffer, frames, 532) ;
732 hexdump_file (filename, 0, 100000) ;
733 } ;
734
735 sf_close (outfile) ;
736 unlink (filename) ;
737
738 puts ("ok") ;
739 return ;
740 #endif
741 } /* extra_header_test */
742