~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/libsndfile/tests/header_test.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
** Copyright (C) 2001-2005 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
 
/* Force the start of this buffer to be double aligned. Sparc-solaris will
59
 
** choke if its not.
60
 
*/
61
 
static  int     data_out [BUFFER_LEN] ;
62
 
static  int     data_in [BUFFER_LEN] ;
63
 
 
64
 
int
65
 
main (int argc, char *argv [])
66
 
{       int             do_all = 0 ;
67
 
        int             test_count = 0 ;
68
 
 
69
 
        if (argc != 2)
70
 
        {       printf ("Usage : %s <test>\n", argv [0]) ;
71
 
                printf ("    Where <test> is one of the following:\n") ;
72
 
                printf ("           wav  - test WAV file peak chunk\n") ;
73
 
                printf ("           aiff - test AIFF file PEAK chunk\n") ;
74
 
                printf ("           all  - perform all tests\n") ;
75
 
                exit (1) ;
76
 
                } ;
77
 
 
78
 
        do_all=!strcmp (argv [1], "all") ;
79
 
 
80
 
        if (do_all || ! strcmp (argv [1], "wav"))
81
 
        {       update_header_test ("header.wav", SF_FORMAT_WAV) ;
82
 
                update_seek_short_test ("header_short.wav", SF_FORMAT_WAV) ;
83
 
                update_seek_int_test ("header_int.wav", SF_FORMAT_WAV) ;
84
 
                update_seek_float_test ("header_float.wav", SF_FORMAT_WAV) ;
85
 
                update_seek_double_test ("header_double.wav", SF_FORMAT_WAV) ;
86
 
                extra_header_test ("extra.wav", SF_FORMAT_WAV) ;
87
 
                test_count++ ;
88
 
                } ;
89
 
 
90
 
        if (do_all || ! strcmp (argv [1], "aiff"))
91
 
        {       update_header_test ("header.aiff", SF_FORMAT_AIFF) ;
92
 
                update_seek_short_test ("header_short.aiff", SF_FORMAT_AIFF) ;
93
 
                update_seek_int_test ("header_int.aiff", SF_FORMAT_AIFF) ;
94
 
                update_seek_float_test ("header_float.aiff", SF_FORMAT_AIFF) ;
95
 
                update_seek_double_test ("header_double.aiff", SF_FORMAT_AIFF) ;
96
 
                extra_header_test ("extra.aiff", SF_FORMAT_AIFF) ;
97
 
                test_count++ ;
98
 
                } ;
99
 
 
100
 
        if (do_all || ! strcmp (argv [1], "au"))
101
 
        {       update_header_test ("header.au", SF_FORMAT_AU) ;
102
 
                update_seek_short_test ("header_short.au", SF_FORMAT_AU) ;
103
 
                update_seek_int_test ("header_int.au", SF_FORMAT_AU) ;
104
 
                update_seek_float_test ("header_float.au", SF_FORMAT_AU) ;
105
 
                update_seek_double_test ("header_double.au", SF_FORMAT_AU) ;
106
 
                test_count++ ;
107
 
                } ;
108
 
 
109
 
        if (do_all || ! strcmp (argv [1], "caf"))
110
 
        {       update_header_test ("header.caf", SF_FORMAT_CAF) ;
111
 
                update_seek_short_test ("header_short.caf", SF_FORMAT_CAF) ;
112
 
                update_seek_int_test ("header_int.caf", SF_FORMAT_CAF) ;
113
 
                update_seek_float_test ("header_float.caf", SF_FORMAT_CAF) ;
114
 
                update_seek_double_test ("header_double.caf", SF_FORMAT_CAF) ;
115
 
                /* extra_header_test ("extra.caf", SF_FORMAT_CAF) ; */
116
 
                test_count++ ;
117
 
                } ;
118
 
 
119
 
        if (do_all || ! strcmp (argv [1], "nist"))
120
 
        {       update_header_test ("header.nist", SF_FORMAT_NIST) ;
121
 
                update_seek_short_test ("header_short.nist", SF_FORMAT_NIST) ;
122
 
                update_seek_int_test ("header_int.nist", SF_FORMAT_NIST) ;
123
 
                test_count++ ;
124
 
                } ;
125
 
 
126
 
        if (do_all || ! strcmp (argv [1], "paf"))
127
 
        {       update_header_test ("header.paf", SF_FORMAT_PAF) ;
128
 
                update_seek_short_test ("header_short.paf", SF_FORMAT_PAF) ;
129
 
                test_count++ ;
130
 
                } ;
131
 
 
132
 
        if (do_all || ! strcmp (argv [1], "ircam"))
133
 
        {       update_header_test ("header.ircam", SF_FORMAT_IRCAM) ;
134
 
                update_seek_short_test ("header_short.ircam", SF_FORMAT_IRCAM) ;
135
 
                test_count++ ;
136
 
                } ;
137
 
 
138
 
        if (do_all || ! strcmp (argv [1], "w64"))
139
 
        {       update_header_test ("header.w64", SF_FORMAT_W64) ;
140
 
                update_seek_short_test ("header_short.w64", SF_FORMAT_W64) ;
141
 
                update_seek_int_test ("header_int.w64", SF_FORMAT_W64) ;
142
 
                update_seek_float_test ("header_float.w64", SF_FORMAT_W64) ;
143
 
                update_seek_double_test ("header_double.w64", SF_FORMAT_W64) ;
144
 
                test_count++ ;
145
 
                } ;
146
 
 
147
 
        if (do_all || ! strcmp (argv [1], "mat4"))
148
 
        {       update_header_test ("header.mat4", SF_FORMAT_MAT4) ;
149
 
                update_seek_short_test ("header_short.mat4", SF_FORMAT_MAT4) ;
150
 
                update_seek_int_test ("header_int.mat4", SF_FORMAT_MAT4) ;
151
 
                update_seek_float_test ("header_float.mat4", SF_FORMAT_MAT4) ;
152
 
                update_seek_double_test ("header_double.mat4", SF_FORMAT_MAT4) ;
153
 
                test_count++ ;
154
 
                } ;
155
 
 
156
 
        if (do_all || ! strcmp (argv [1], "mat5"))
157
 
        {       update_header_test ("header.mat5", SF_FORMAT_MAT5) ;
158
 
                update_seek_short_test ("header_short.mat5", SF_FORMAT_MAT5) ;
159
 
                update_seek_int_test ("header_int.mat5", SF_FORMAT_MAT5) ;
160
 
                update_seek_float_test ("header_float.mat5", SF_FORMAT_MAT5) ;
161
 
                update_seek_double_test ("header_double.mat5", SF_FORMAT_MAT5) ;
162
 
                test_count++ ;
163
 
                } ;
164
 
 
165
 
        if (do_all || ! strcmp (argv [1], "pvf"))
166
 
        {       update_header_test ("header.pvf", SF_FORMAT_PVF) ;
167
 
                update_seek_short_test ("header_short.pvf", SF_FORMAT_PVF) ;
168
 
                test_count++ ;
169
 
                } ;
170
 
 
171
 
        if (do_all || ! strcmp (argv [1], "avr"))
172
 
        {       update_header_test ("header.avr", SF_FORMAT_AVR) ;
173
 
                update_seek_short_test ("header_short.avr", SF_FORMAT_AVR) ;
174
 
                test_count++ ;
175
 
                } ;
176
 
 
177
 
        if (do_all || ! strcmp (argv [1], "htk"))
178
 
        {       update_header_test ("header.htk", SF_FORMAT_HTK) ;
179
 
                update_seek_short_test ("header_short.htk", SF_FORMAT_HTK) ;
180
 
                test_count++ ;
181
 
                } ;
182
 
 
183
 
        if (do_all || ! strcmp (argv [1], "svx"))
184
 
        {       update_header_test ("header.svx", SF_FORMAT_SVX) ;
185
 
                update_seek_short_test ("header_short.svx", SF_FORMAT_SVX) ;
186
 
                test_count++ ;
187
 
                } ;
188
 
 
189
 
        if (do_all || ! strcmp (argv [1], "voc"))
190
 
        {       update_header_test ("header.voc", SF_FORMAT_VOC) ;
191
 
                /*-update_seek_short_test ("header_short.voc", SF_FORMAT_VOC) ;-*/
192
 
                test_count++ ;
193
 
                } ;
194
 
 
195
 
        if (do_all || ! strcmp (argv [1], "sds"))
196
 
        {       update_header_test ("header.sds", SF_FORMAT_SDS) ;
197
 
                /*-update_seek_short_test ("header_short.sds", SF_FORMAT_SDS) ;-*/
198
 
                test_count++ ;
199
 
                } ;
200
 
 
201
 
        if (test_count == 0)
202
 
        {       printf ("Mono : ************************************\n") ;
203
 
                printf ("Mono : *  No '%s' test defined.\n", argv [1]) ;
204
 
                printf ("Mono : ************************************\n") ;
205
 
                return 1 ;
206
 
                } ;
207
 
 
208
 
        return 0 ;
209
 
} /* main */
210
 
 
211
 
 
212
 
/*============================================================================================
213
 
**      Here are the test functions.
214
 
*/
215
 
 
216
 
static void
217
 
update_header_sub (const char *filename, int typemajor, int write_mode)
218
 
{       SNDFILE         *outfile, *infile ;
219
 
        SF_INFO         sfinfo ;
220
 
        int                     k, frames ;
221
 
 
222
 
        sfinfo.samplerate = 44100 ;
223
 
        sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
224
 
        sfinfo.channels = 1 ;
225
 
        sfinfo.frames = 0 ;
226
 
 
227
 
        frames = BUFFER_LEN / sfinfo.channels ;
228
 
 
229
 
        outfile = test_open_file_or_die (filename, write_mode, &sfinfo, SF_TRUE, __LINE__) ;
230
 
 
231
 
        for (k = 0 ; k < BUFFER_LEN ; k++)
232
 
                data_out [k] = k + 1 ;
233
 
        test_write_int_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ;
234
 
 
235
 
        if (typemajor != SF_FORMAT_HTK)
236
 
        {       /* The HTK header is not correct when the file is first written. */
237
 
                infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
238
 
                sf_close (infile) ;
239
 
                } ;
240
 
 
241
 
        sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, 0) ;
242
 
 
243
 
        /*
244
 
        ** Open file and check log buffer for an error. If header update failed
245
 
        ** the the log buffer will contain errors.
246
 
        */
247
 
        infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
248
 
        check_log_buffer_or_die (infile, __LINE__) ;
249
 
 
250
 
        if (sfinfo.frames < BUFFER_LEN || sfinfo.frames > BUFFER_LEN + 50)
251
 
        {       printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), BUFFER_LEN) ;
252
 
                dump_log_buffer (infile) ;
253
 
                exit (1) ;
254
 
                } ;
255
 
 
256
 
        test_read_int_or_die (infile, 0, data_in, BUFFER_LEN, __LINE__) ;
257
 
        for (k = 0 ; k < BUFFER_LEN ; k++)
258
 
                if (data_out [k] != k + 1)
259
 
                        printf ("Error : line %d\n", __LINE__) ;
260
 
 
261
 
        sf_close (infile) ;
262
 
 
263
 
        /* Set auto update on. */
264
 
        sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
265
 
 
266
 
        /* Write more data_out. */
267
 
        for (k = 0 ; k < BUFFER_LEN ; k++)
268
 
                data_out [k] = k + 2 ;
269
 
        test_write_int_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ;
270
 
 
271
 
        /* Open file again and make sure no errors in log buffer. */
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 < 2 * BUFFER_LEN || sfinfo.frames > 2 * BUFFER_LEN + 50)
276
 
        {       printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * BUFFER_LEN) ;
277
 
                dump_log_buffer (infile) ;
278
 
                exit (1) ;
279
 
                } ;
280
 
 
281
 
        sf_close (infile) ;
282
 
 
283
 
        sf_close (outfile) ;
284
 
 
285
 
        unlink (filename) ;
286
 
} /* update_header_sub */
287
 
 
288
 
static void
289
 
update_header_test (const char *filename, int typemajor)
290
 
{
291
 
        print_test_name ("update_header_test", filename) ;
292
 
 
293
 
#if 0 /*-(OS_IS_WIN32 == 0)-*/
294
 
        if (typemajor == SF_FORMAT_PAF)
295
 
        {       /*
296
 
                ** I think this is a bug in the win32 file I/O code in src/file_io.c.
297
 
                ** I didn't write that code and I don't have the time to debug and
298
 
                ** fix it. Patches will gladly be accepted. Erik
299
 
                */
300
 
                puts ("doesn't work on win32") ;
301
 
                return ;
302
 
                } ;
303
 
#endif
304
 
 
305
 
        update_header_sub (filename, typemajor, SFM_WRITE) ;
306
 
        update_header_sub (filename, typemajor, SFM_RDWR) ;
307
 
 
308
 
        unlink (filename) ;
309
 
        puts ("ok") ;
310
 
} /* update_header_test */
311
 
 
312
 
/*==============================================================================
313
 
*/
314
 
 
315
 
static void
316
 
update_seek_short_test  (const char *filename, int filetype)
317
 
{       SNDFILE *outfile, *infile ;
318
 
        SF_INFO sfinfo ;
319
 
    sf_count_t frames ;
320
 
    short buffer [8] ;
321
 
        int k ;
322
 
 
323
 
        print_test_name ("update_seek_short_test", filename) ;
324
 
 
325
 
        memset (buffer, 0, sizeof (buffer)) ;
326
 
 
327
 
        /* Create sound outfile with no data. */
328
 
        sfinfo.format = filetype | SF_FORMAT_PCM_16 ;
329
 
        sfinfo.samplerate = 48000 ;
330
 
        sfinfo.channels = 2 ;
331
 
 
332
 
        if (sf_format_check (&sfinfo) == SF_FALSE)
333
 
                sfinfo.channels = 1 ;
334
 
 
335
 
        outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
336
 
        sf_close (outfile) ;
337
 
 
338
 
        /* Open again for read/write. */
339
 
        outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
340
 
 
341
 
        /*
342
 
        ** In auto header update mode, seeking to the end of the file with
343
 
    ** SEEK_SET will fail from the 2nd seek on.  seeking to 0, SEEK_END
344
 
        ** will seek to 0 anyway
345
 
        */
346
 
        if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
347
 
    {   printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
348
 
                exit (1) ;
349
 
                } ;
350
 
 
351
 
        /* Now write some frames. */
352
 
        frames = ARRAY_LEN (buffer) / sfinfo.channels ;
353
 
 
354
 
        for (k = 0 ; k < 6 ; k++)
355
 
        {       test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
356
 
                test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
357
 
 
358
 
                /* Open file again and make sure no errors in log buffer. */
359
 
                infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
360
 
                check_log_buffer_or_die (infile, __LINE__) ;
361
 
                sf_close (infile) ;
362
 
 
363
 
                if (sfinfo.frames != k * frames)
364
 
                {       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)) ;
365
 
                        dump_log_buffer (infile) ;
366
 
                        exit (1) ;
367
 
                        } ;
368
 
 
369
 
                if ((k & 1) == 0)
370
 
                        test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
371
 
                else
372
 
                        test_writef_short_or_die (outfile, k, buffer, frames, __LINE__) ;
373
 
                } ;
374
 
 
375
 
        sf_close (outfile) ;
376
 
        unlink (filename) ;
377
 
 
378
 
        puts ("ok") ;
379
 
        return ;
380
 
} /* update_seek_short_test */
381
 
static void
382
 
update_seek_int_test    (const char *filename, int filetype)
383
 
{       SNDFILE *outfile, *infile ;
384
 
        SF_INFO sfinfo ;
385
 
    sf_count_t frames ;
386
 
    int buffer [8] ;
387
 
        int k ;
388
 
 
389
 
        print_test_name ("update_seek_int_test", filename) ;
390
 
 
391
 
        memset (buffer, 0, sizeof (buffer)) ;
392
 
 
393
 
        /* Create sound outfile with no data. */
394
 
        sfinfo.format = filetype | SF_FORMAT_PCM_32 ;
395
 
        sfinfo.samplerate = 48000 ;
396
 
        sfinfo.channels = 2 ;
397
 
 
398
 
        if (sf_format_check (&sfinfo) == SF_FALSE)
399
 
                sfinfo.channels = 1 ;
400
 
 
401
 
        outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
402
 
        sf_close (outfile) ;
403
 
 
404
 
        /* Open again for read/write. */
405
 
        outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
406
 
 
407
 
        /*
408
 
        ** In auto header update mode, seeking to the end of the file with
409
 
    ** SEEK_SET will fail from the 2nd seek on.  seeking to 0, SEEK_END
410
 
        ** will seek to 0 anyway
411
 
        */
412
 
        if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
413
 
    {   printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
414
 
                exit (1) ;
415
 
                } ;
416
 
 
417
 
        /* Now write some frames. */
418
 
        frames = ARRAY_LEN (buffer) / sfinfo.channels ;
419
 
 
420
 
        for (k = 0 ; k < 6 ; k++)
421
 
        {       test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
422
 
                test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
423
 
 
424
 
                /* Open file again and make sure no errors in log buffer. */
425
 
                infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
426
 
                check_log_buffer_or_die (infile, __LINE__) ;
427
 
                sf_close (infile) ;
428
 
 
429
 
                if (sfinfo.frames != k * frames)
430
 
                {       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)) ;
431
 
                        dump_log_buffer (infile) ;
432
 
                        exit (1) ;
433
 
                        } ;
434
 
 
435
 
                if ((k & 1) == 0)
436
 
                        test_write_int_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
437
 
                else
438
 
                        test_writef_int_or_die (outfile, k, buffer, frames, __LINE__) ;
439
 
                } ;
440
 
 
441
 
        sf_close (outfile) ;
442
 
        unlink (filename) ;
443
 
 
444
 
        puts ("ok") ;
445
 
        return ;
446
 
} /* update_seek_int_test */
447
 
static void
448
 
update_seek_float_test  (const char *filename, int filetype)
449
 
{       SNDFILE *outfile, *infile ;
450
 
        SF_INFO sfinfo ;
451
 
    sf_count_t frames ;
452
 
    float buffer [8] ;
453
 
        int k ;
454
 
 
455
 
        print_test_name ("update_seek_float_test", filename) ;
456
 
 
457
 
        memset (buffer, 0, sizeof (buffer)) ;
458
 
 
459
 
        /* Create sound outfile with no data. */
460
 
        sfinfo.format = filetype | SF_FORMAT_FLOAT ;
461
 
        sfinfo.samplerate = 48000 ;
462
 
        sfinfo.channels = 2 ;
463
 
 
464
 
        if (sf_format_check (&sfinfo) == SF_FALSE)
465
 
                sfinfo.channels = 1 ;
466
 
 
467
 
        outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
468
 
        sf_close (outfile) ;
469
 
 
470
 
        /* Open again for read/write. */
471
 
        outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
472
 
 
473
 
        /*
474
 
        ** In auto header update mode, seeking to the end of the file with
475
 
    ** SEEK_SET will fail from the 2nd seek on.  seeking to 0, SEEK_END
476
 
        ** will seek to 0 anyway
477
 
        */
478
 
        if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
479
 
    {   printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
480
 
                exit (1) ;
481
 
                } ;
482
 
 
483
 
        /* Now write some frames. */
484
 
        frames = ARRAY_LEN (buffer) / sfinfo.channels ;
485
 
 
486
 
        for (k = 0 ; k < 6 ; k++)
487
 
        {       test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
488
 
                test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
489
 
 
490
 
                /* Open file again and make sure no errors in log buffer. */
491
 
                infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
492
 
                check_log_buffer_or_die (infile, __LINE__) ;
493
 
                sf_close (infile) ;
494
 
 
495
 
                if (sfinfo.frames != k * frames)
496
 
                {       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)) ;
497
 
                        dump_log_buffer (infile) ;
498
 
                        exit (1) ;
499
 
                        } ;
500
 
 
501
 
                if ((k & 1) == 0)
502
 
                        test_write_float_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
503
 
                else
504
 
                        test_writef_float_or_die (outfile, k, buffer, frames, __LINE__) ;
505
 
                } ;
506
 
 
507
 
        sf_close (outfile) ;
508
 
        unlink (filename) ;
509
 
 
510
 
        puts ("ok") ;
511
 
        return ;
512
 
} /* update_seek_float_test */
513
 
static void
514
 
update_seek_double_test (const char *filename, int filetype)
515
 
{       SNDFILE *outfile, *infile ;
516
 
        SF_INFO sfinfo ;
517
 
    sf_count_t frames ;
518
 
    double buffer [8] ;
519
 
        int k ;
520
 
 
521
 
        print_test_name ("update_seek_double_test", filename) ;
522
 
 
523
 
        memset (buffer, 0, sizeof (buffer)) ;
524
 
 
525
 
        /* Create sound outfile with no data. */
526
 
        sfinfo.format = filetype | SF_FORMAT_DOUBLE ;
527
 
        sfinfo.samplerate = 48000 ;
528
 
        sfinfo.channels = 2 ;
529
 
 
530
 
        if (sf_format_check (&sfinfo) == SF_FALSE)
531
 
                sfinfo.channels = 1 ;
532
 
 
533
 
        outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
534
 
        sf_close (outfile) ;
535
 
 
536
 
        /* Open again for read/write. */
537
 
        outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
538
 
 
539
 
        /*
540
 
        ** In auto header update mode, seeking to the end of the file with
541
 
    ** SEEK_SET will fail from the 2nd seek on.  seeking to 0, SEEK_END
542
 
        ** will seek to 0 anyway
543
 
        */
544
 
        if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
545
 
    {   printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
546
 
                exit (1) ;
547
 
                } ;
548
 
 
549
 
        /* Now write some frames. */
550
 
        frames = ARRAY_LEN (buffer) / sfinfo.channels ;
551
 
 
552
 
        for (k = 0 ; k < 6 ; k++)
553
 
        {       test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, __LINE__) ;
554
 
                test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, __LINE__) ;
555
 
 
556
 
                /* Open file again and make sure no errors in log buffer. */
557
 
                infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
558
 
                check_log_buffer_or_die (infile, __LINE__) ;
559
 
                sf_close (infile) ;
560
 
 
561
 
                if (sfinfo.frames != k * frames)
562
 
                {       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)) ;
563
 
                        dump_log_buffer (infile) ;
564
 
                        exit (1) ;
565
 
                        } ;
566
 
 
567
 
                if ((k & 1) == 0)
568
 
                        test_write_double_or_die (outfile, k, buffer, sfinfo.channels * frames, __LINE__) ;
569
 
                else
570
 
                        test_writef_double_or_die (outfile, k, buffer, frames, __LINE__) ;
571
 
                } ;
572
 
 
573
 
        sf_close (outfile) ;
574
 
        unlink (filename) ;
575
 
 
576
 
        puts ("ok") ;
577
 
        return ;
578
 
} /* update_seek_double_test */
579
 
 
580
 
 
581
 
 
582
 
 
583
 
 
584
 
 
585
 
static void
586
 
extra_header_test (const char *filename, int filetype)
587
 
{       SNDFILE *outfile, *infile ;
588
 
        SF_INFO sfinfo ;
589
 
    sf_count_t frames ;
590
 
    short buffer [8] ;
591
 
        int k = 0 ;
592
 
 
593
 
        print_test_name ("extra_header_test", filename) ;
594
 
 
595
 
        sfinfo.samplerate = 44100 ;
596
 
        sfinfo.format = (filetype | SF_FORMAT_PCM_16) ;
597
 
        sfinfo.channels = 1 ;
598
 
 
599
 
        memset (buffer, 0xA0, sizeof (buffer)) ;
600
 
 
601
 
        /* Now write some frames. */
602
 
        frames = ARRAY_LEN (buffer) / sfinfo.channels ;
603
 
 
604
 
        /* Test the file with extra header data. */
605
 
        outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, 409) ;
606
 
        sf_set_string (outfile, SF_STR_TITLE, filename) ;
607
 
        test_writef_short_or_die (outfile, k, buffer, frames, 411) ;
608
 
        sf_set_string (outfile, SF_STR_COPYRIGHT, "(c) 1980 Erik") ;
609
 
        sf_close (outfile) ;
610
 
 
611
 
#if 1
612
 
        /*
613
 
        **  Erik de Castro Lopo <erikd@mega-nerd.com> May 23 2004.
614
 
        **
615
 
        ** This file has extra string data in the header and therefore cannot
616
 
        ** currently be opened in SFM_RDWR mode. This is fixable, but its in
617
 
        ** a part of the code I don't want to fiddle with until the Ogg/Vorbis
618
 
        ** integration is done.
619
 
        */
620
 
 
621
 
        if ((infile = sf_open (filename, SFM_RDWR, &sfinfo)) != NULL)
622
 
        {       printf ("\n\nError : should not be able to open this file in SFM_RDWR.\n\n") ;
623
 
                exit (1) ;
624
 
                } ;
625
 
 
626
 
        unlink (filename) ;
627
 
        puts ("ok") ;
628
 
        return ;
629
 
#else
630
 
 
631
 
        hexdump_file (filename, 0, 100000) ;
632
 
 
633
 
        /* Open again for read/write. */
634
 
        outfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, 438) ;
635
 
 
636
 
        /*
637
 
        ** In auto header update mode, seeking to the end of the file with
638
 
    ** SEEK_SET will fail from the 2nd seek on.  seeking to 0, SEEK_END
639
 
        ** will seek to 0 anyway
640
 
        */
641
 
        if (sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) == 0)
642
 
    {   printf ("\n\nError : sf_command (SFC_SET_UPDATE_HEADER_AUTO) return error : %s\n\n", sf_strerror (outfile)) ;
643
 
                exit (1) ;
644
 
                } ;
645
 
 
646
 
        /* Now write some frames. */
647
 
        frames = ARRAY_LEN (buffer) / sfinfo.channels ;
648
 
 
649
 
        for (k = 1 ; k < 6 ; k++)
650
 
        {
651
 
                printf ("\n*** pass %d\n", k) ;
652
 
                memset (buffer, 0xA0 + k, sizeof (buffer)) ;
653
 
 
654
 
 
655
 
                test_seek_or_die (outfile, k * frames, SEEK_SET, k * frames, sfinfo.channels, 459) ;
656
 
                test_seek_or_die (outfile, 0, SEEK_END, k * frames, sfinfo.channels, 460) ;
657
 
 
658
 
                /* Open file again and make sure no errors in log buffer. */
659
 
                if (0)
660
 
                {       infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, 464) ;
661
 
                        check_log_buffer_or_die (infile, 465) ;
662
 
                        sf_close (infile) ;
663
 
                        } ;
664
 
 
665
 
                if (sfinfo.frames != k * frames)
666
 
                {       printf ("\n\nLine %d : Incorrect sample count (%ld should be %ld)\n", 470, SF_COUNT_TO_LONG (sfinfo.frames), SF_COUNT_TO_LONG (k + frames)) ;
667
 
                        dump_log_buffer (infile) ;
668
 
                        exit (1) ;
669
 
                        } ;
670
 
 
671
 
                if ((k & 1) == 0)
672
 
                        test_write_short_or_die (outfile, k, buffer, sfinfo.channels * frames, 476) ;
673
 
                else
674
 
                        test_writef_short_or_die (outfile, k, buffer, frames, 478) ;
675
 
                hexdump_file (filename, 0, 100000) ;
676
 
                } ;
677
 
 
678
 
        sf_close (outfile) ;
679
 
        unlink (filename) ;
680
 
 
681
 
        puts ("ok") ;
682
 
        return ;
683
 
#endif
684
 
} /* extra_header_test */
685
 
 
686