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

« back to all changes in this revision

Viewing changes to lib-src/libsndfile/tests/command_test.tpl

  • 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
 
[+ AutoGen5 template c +]
2
 
/*
3
 
** Copyright (C) 2001-2002 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 "config.h"
21
 
 
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include <string.h>
25
 
 
26
 
#ifdef HAVE_UNISTD_H
27
 
#include <unistd.h>
28
 
#endif
29
 
 
30
 
#include <math.h>
31
 
 
32
 
#include <sndfile.h>
33
 
 
34
 
#include "utils.h"
35
 
 
36
 
#define BUFFER_LEN              (1<<10)
37
 
#define LOG_BUFFER_SIZE 1024
38
 
 
39
 
static  void    float_norm_test         (const char *filename) ;
40
 
static  void    double_norm_test        (const char *filename) ;
41
 
static  void    format_tests            (void) ;
42
 
static  void    calc_peak_test          (int filetype, const char *filename) ;
43
 
static  void    truncate_test           (const char *filename, int filetype) ;
44
 
 
45
 
/* Force the start of this buffer to be double aligned. Sparc-solaris will
46
 
** choke if its not.
47
 
*/
48
 
 
49
 
static  float   float_data      [BUFFER_LEN] ;
50
 
static  double  double_data     [BUFFER_LEN] ;
51
 
 
52
 
int
53
 
main (int argc, char *argv [])
54
 
{       /*-char *filename ;-*/
55
 
        int             do_all = 0 ;
56
 
        int             test_count = 0 ;
57
 
 
58
 
        if (argc != 2)
59
 
        {       printf ("Usage : %s <test>\n", argv [0]) ;
60
 
                printf ("    Where <test> is one of the following:\n") ;
61
 
                printf ("           ver    - test sf_command (SFC_GETLIB_VERSION)\n") ;
62
 
                /*-printf ("           text  - test adding of text strings\n") ;-*/
63
 
                printf ("           norm  - test floating point normalisation\n") ;
64
 
                printf ("           format - test format string commands\n") ;
65
 
                printf ("           peak   - test peak calculation\n") ;
66
 
                printf ("           trunc  - test file truncation\n") ;
67
 
                printf ("           all   - perform all tests\n") ;
68
 
                exit (1) ;
69
 
                } ;
70
 
 
71
 
        do_all =! strcmp (argv [1], "all") ;
72
 
 
73
 
        if (do_all || ! strcmp (argv [1], "ver"))
74
 
        {       char buffer [128] ;
75
 
                buffer [0] = 0 ;
76
 
                sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ;
77
 
                if (strlen (buffer) < 1)
78
 
                {       printf ("Line %d: could not retrieve lib version.\n", __LINE__) ;
79
 
                        exit (1) ;
80
 
                        } ;
81
 
                test_count++ ;
82
 
                } ;
83
 
 
84
 
        if (do_all || ! strcmp (argv [1], "norm"))
85
 
        {       /*      Preliminary float/double normalisation tests. More testing
86
 
                **      is done in the program 'floating_point_test'.
87
 
                */
88
 
                float_norm_test         ("float.wav") ;
89
 
                double_norm_test        ("double.wav") ;
90
 
                test_count++ ;
91
 
                } ;
92
 
 
93
 
        if (do_all || ! strcmp (argv [1], "peak"))
94
 
        {       calc_peak_test (SF_ENDIAN_BIG           | SF_FORMAT_RAW, "be-peak.raw") ;
95
 
                calc_peak_test (SF_ENDIAN_LITTLE        | SF_FORMAT_RAW, "le-peak.raw") ;
96
 
                test_count++ ;
97
 
                } ;
98
 
 
99
 
        if (do_all || ! strcmp (argv [1], "format"))
100
 
        {       format_tests () ;
101
 
                test_count++ ;
102
 
                } ;
103
 
 
104
 
        if (do_all || ! strcmp (argv [1], "trunc"))
105
 
        {       truncate_test ("truncate.raw", SF_FORMAT_RAW | SF_FORMAT_PCM_32) ;
106
 
                truncate_test ("truncate.au" , SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
107
 
                test_count++ ;
108
 
                } ;
109
 
 
110
 
        if (test_count == 0)
111
 
        {       printf ("Mono : ************************************\n") ;
112
 
                printf ("Mono : *  No '%s' test defined.\n", argv [1]) ;
113
 
                printf ("Mono : ************************************\n") ;
114
 
                return 1 ;
115
 
                } ;
116
 
 
117
 
        return 0 ;
118
 
} /* main */
119
 
 
120
 
/*============================================================================================
121
 
**      Here are the test functions.
122
 
*/
123
 
 
124
 
static void
125
 
float_norm_test (const char *filename)
126
 
{       SNDFILE                 *file ;
127
 
        SF_INFO                 sfinfo ;
128
 
        unsigned int    k ;
129
 
 
130
 
        print_test_name ("float_norm_test", filename) ;
131
 
 
132
 
        sfinfo.samplerate       = 44100 ;
133
 
        sfinfo.format           = (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;
134
 
        sfinfo.channels         = 1 ;
135
 
        sfinfo.frames           = BUFFER_LEN ;
136
 
 
137
 
        /* Create float_data with all values being less than 1.0. */
138
 
        for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
139
 
                float_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;
140
 
        for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)
141
 
                float_data [k] = (k + 5) ;
142
 
 
143
 
        if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
144
 
        {       printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
145
 
                fflush (stdout) ;
146
 
                puts (sf_strerror (NULL)) ;
147
 
                exit (1) ;
148
 
                } ;
149
 
 
150
 
        /* Normalisation is on by default so no need to do anything here. */
151
 
 
152
 
        if ((k = sf_write_float (file, float_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
153
 
        {       printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
154
 
                exit (1) ;
155
 
                } ;
156
 
 
157
 
        /* Turn normalisation off. */
158
 
        sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
159
 
 
160
 
        if ((k = sf_write_float (file, float_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
161
 
        {       printf ("Line %d: sf_write_float failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
162
 
                exit (1) ;
163
 
                } ;
164
 
 
165
 
        sf_close (file) ;
166
 
 
167
 
        /* sfinfo struct should still contain correct data. */
168
 
        if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
169
 
        {       printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
170
 
                fflush (stdout) ;
171
 
                puts (sf_strerror (NULL)) ;
172
 
                exit (1) ;
173
 
                } ;
174
 
 
175
 
        if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))
176
 
        {       printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;
177
 
                exit (1) ;
178
 
                } ;
179
 
 
180
 
        if (sfinfo.frames != BUFFER_LEN)
181
 
        {       printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
182
 
                exit (1) ;
183
 
                } ;
184
 
 
185
 
        if (sfinfo.channels != 1)
186
 
        {       printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
187
 
                exit (1) ;
188
 
                } ;
189
 
 
190
 
        /* Read float_data and check that it is normalised (ie default). */
191
 
        if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
192
 
        {       printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
193
 
                exit (1) ;
194
 
                } ;
195
 
 
196
 
        for (k = 0 ; k < BUFFER_LEN ; k++)
197
 
                if (float_data [k] >= 1.0)
198
 
                {       printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;
199
 
                        exit (1) ;
200
 
                        } ;
201
 
 
202
 
        /* Seek to start of file, turn normalisation off, read float_data and check again. */
203
 
        sf_seek (file, 0, SEEK_SET) ;
204
 
        sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_FALSE) ;
205
 
 
206
 
        if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
207
 
        {       printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
208
 
                exit (1) ;
209
 
                } ;
210
 
 
211
 
        for (k = 0 ; k < BUFFER_LEN ; k++)
212
 
                if (float_data [k] < 1.0)
213
 
                {       printf ("\n\nLine %d: float_data [%d] == %f which is less than 1.0\n", __LINE__, k, float_data [k]) ;
214
 
                        exit (1) ;
215
 
                        } ;
216
 
 
217
 
        /* Seek to start of file, turn normalisation on, read float_data and do final check. */
218
 
        sf_seek (file, 0, SEEK_SET) ;
219
 
        sf_command (file, SFC_SET_NORM_FLOAT, NULL, SF_TRUE) ;
220
 
 
221
 
        if ((k = sf_read_float (file, float_data, BUFFER_LEN)) != BUFFER_LEN)
222
 
        {       printf ("\n\nLine %d: sf_read_float failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
223
 
                exit (1) ;
224
 
                } ;
225
 
 
226
 
        for (k = 0 ; k < BUFFER_LEN ; k++)
227
 
                if (float_data [k] > 1.0)
228
 
                {       printf ("\n\nLine %d: float_data [%d] == %f which is greater than 1.0\n", __LINE__, k, float_data [k]) ;
229
 
                        exit (1) ;
230
 
                        } ;
231
 
 
232
 
 
233
 
        sf_close (file) ;
234
 
 
235
 
        unlink (filename) ;
236
 
 
237
 
        printf ("ok\n") ;
238
 
} /* float_norm_test */
239
 
 
240
 
static void
241
 
double_norm_test (const char *filename)
242
 
{       SNDFILE                 *file ;
243
 
        SF_INFO                 sfinfo ;
244
 
        unsigned int    k ;
245
 
 
246
 
        print_test_name ("double_norm_test", filename) ;
247
 
 
248
 
        sfinfo.samplerate       = 44100 ;
249
 
        sfinfo.format           = (SF_FORMAT_RAW | SF_FORMAT_PCM_16) ;
250
 
        sfinfo.channels         = 1 ;
251
 
        sfinfo.frames           = BUFFER_LEN ;
252
 
 
253
 
        /* Create double_data with all values being less than 1.0. */
254
 
        for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
255
 
                double_data [k] = (k + 5) / (2.0 * BUFFER_LEN) ;
256
 
        for (k = BUFFER_LEN / 2 ; k < BUFFER_LEN ; k++)
257
 
                double_data [k] = (k + 5) ;
258
 
 
259
 
        if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
260
 
        {       printf ("Line %d: sf_open_write failed with error : ", __LINE__) ;
261
 
                fflush (stdout) ;
262
 
                puts (sf_strerror (NULL)) ;
263
 
                exit (1) ;
264
 
                } ;
265
 
 
266
 
        /* Normailsation is on by default so no need to do anything here. */
267
 
        /*-sf_command (file, "set-norm-double", "true", 0) ;-*/
268
 
 
269
 
        if ((k = sf_write_double (file, double_data, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
270
 
        {       printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
271
 
                exit (1) ;
272
 
                } ;
273
 
 
274
 
        /* Turn normalisation off. */
275
 
        sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
276
 
 
277
 
        if ((k = sf_write_double (file, double_data + BUFFER_LEN / 2, BUFFER_LEN / 2)) != BUFFER_LEN / 2)
278
 
        {       printf ("Line %d: sf_write_double failed with short write (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
279
 
                exit (1) ;
280
 
                } ;
281
 
 
282
 
        sf_close (file) ;
283
 
 
284
 
        if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
285
 
        {       printf ("Line %d: sf_open_read failed with error : ", __LINE__) ;
286
 
                fflush (stdout) ;
287
 
                puts (sf_strerror (NULL)) ;
288
 
                exit (1) ;
289
 
                } ;
290
 
 
291
 
        if (sfinfo.format != (SF_FORMAT_RAW | SF_FORMAT_PCM_16))
292
 
        {       printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, (SF_FORMAT_RAW | SF_FORMAT_PCM_16), sfinfo.format) ;
293
 
                exit (1) ;
294
 
                } ;
295
 
 
296
 
        if (sfinfo.frames != BUFFER_LEN)
297
 
        {       printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
298
 
                exit (1) ;
299
 
                } ;
300
 
 
301
 
        if (sfinfo.channels != 1)
302
 
        {       printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
303
 
                exit (1) ;
304
 
                } ;
305
 
 
306
 
        /* Read double_data and check that it is normalised (ie default). */
307
 
        if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
308
 
        {       printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
309
 
                exit (1) ;
310
 
                } ;
311
 
 
312
 
        for (k = 0 ; k < BUFFER_LEN ; k++)
313
 
                if (double_data [k] >= 1.0)
314
 
                {       printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ;
315
 
                        exit (1) ;
316
 
                        } ;
317
 
 
318
 
        /* Seek to start of file, turn normalisation off, read double_data and check again. */
319
 
        sf_seek (file, 0, SEEK_SET) ;
320
 
        sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_FALSE) ;
321
 
 
322
 
        if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
323
 
        {       printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
324
 
                exit (1) ;
325
 
                } ;
326
 
 
327
 
        for (k = 0 ; k < BUFFER_LEN ; k++)
328
 
                if (double_data [k] < 1.0)
329
 
                {       printf ("\n\nLine %d: double_data [%d] == %f which is less than 1.0\n", __LINE__, k, double_data [k]) ;
330
 
                        exit (1) ;
331
 
                        } ;
332
 
 
333
 
        /* Seek to start of file, turn normalisation on, read double_data and do final check. */
334
 
        sf_seek (file, 0, SEEK_SET) ;
335
 
        sf_command (file, SFC_SET_NORM_DOUBLE, NULL, SF_TRUE) ;
336
 
 
337
 
        if ((k = sf_read_double (file, double_data, BUFFER_LEN)) != BUFFER_LEN)
338
 
        {       printf ("\n\nLine %d: sf_read_double failed with short read (%d ->%d)\n", __LINE__, BUFFER_LEN, k) ;
339
 
                exit (1) ;
340
 
                } ;
341
 
 
342
 
        for (k = 0 ; k < BUFFER_LEN ; k++)
343
 
                if (double_data [k] > 1.0)
344
 
                {       printf ("\n\nLine %d: double_data [%d] == %f which is greater than 1.0\n", __LINE__, k, double_data [k]) ;
345
 
                        exit (1) ;
346
 
                        } ;
347
 
 
348
 
 
349
 
        sf_close (file) ;
350
 
 
351
 
        unlink (filename) ;
352
 
 
353
 
        printf ("ok\n") ;
354
 
} /* double_norm_test */
355
 
 
356
 
static  void
357
 
format_tests    (void)
358
 
{       SF_FORMAT_INFO format_info ;
359
 
        SF_INFO         sfinfo ;
360
 
        const char      *last_name ;
361
 
        int             k, count ;
362
 
 
363
 
        print_test_name ("format_tests", "(null)") ;
364
 
 
365
 
        /* Clear out SF_INFO struct and set channels > 0. */
366
 
        memset (&sfinfo, 0, sizeof (sfinfo)) ;
367
 
        sfinfo.channels = 1 ;
368
 
 
369
 
        /* First test simple formats. */
370
 
 
371
 
        sf_command (NULL, SFC_GET_SIMPLE_FORMAT_COUNT, &count, sizeof (int)) ;
372
 
 
373
 
        if (count < 0 || count > 30)
374
 
        {       printf ("Line %d: Weird count.\n", __LINE__) ;
375
 
                exit (1) ;
376
 
                } ;
377
 
 
378
 
        format_info.format = 0 ;
379
 
        sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
380
 
 
381
 
        last_name = format_info.name ;
382
 
        for (k = 1 ; k < count ; k ++)
383
 
        {       format_info.format = k ;
384
 
                sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
385
 
                if (strcmp (last_name, format_info.name) >= 0)
386
 
                {       printf ("\n\nLine %d: format names out of sequence `%s' < `%s'.\n", __LINE__, last_name, format_info.name) ;
387
 
                        exit (1) ;
388
 
                        } ;
389
 
                sfinfo.format = format_info.format ;
390
 
 
391
 
                if (! sf_format_check (&sfinfo))
392
 
                {       printf ("\n\nLine %d: sf_format_check failed.\n", __LINE__) ;
393
 
                        printf ("        Name : %s\n", format_info.name) ;
394
 
                        printf ("        Format      : 0x%X\n", sfinfo.format) ;
395
 
                        printf ("        Channels    : 0x%X\n", sfinfo.channels) ;
396
 
                        printf ("        Sample Rate : 0x%X\n", sfinfo.samplerate) ;
397
 
                        exit (1) ;
398
 
                        } ;
399
 
                last_name = format_info.name ;
400
 
                } ;
401
 
        format_info.format = 666 ;
402
 
        sf_command (NULL, SFC_GET_SIMPLE_FORMAT, &format_info, sizeof (format_info)) ;
403
 
 
404
 
        /* Now test major formats. */
405
 
        sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int)) ;
406
 
 
407
 
        if (count < 0 || count > 30)
408
 
        {       printf ("Line %d: Weird count.\n", __LINE__) ;
409
 
                exit (1) ;
410
 
                } ;
411
 
 
412
 
        format_info.format = 0 ;
413
 
        sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
414
 
 
415
 
        last_name = format_info.name ;
416
 
        for (k = 1 ; k < count ; k ++)
417
 
        {       format_info.format = k ;
418
 
                sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
419
 
                if (strcmp (last_name, format_info.name) >= 0)
420
 
                {       printf ("\n\nLine %d: format names out of sequence (%d) `%s' < `%s'.\n", __LINE__, k, last_name, format_info.name) ;
421
 
                        exit (1) ;
422
 
                        } ;
423
 
 
424
 
                last_name = format_info.name ;
425
 
                } ;
426
 
        format_info.format = 666 ;
427
 
        sf_command (NULL, SFC_GET_FORMAT_MAJOR, &format_info, sizeof (format_info)) ;
428
 
 
429
 
        /* Now test subtype formats. */
430
 
        sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int)) ;
431
 
 
432
 
        if (count < 0 || count > 30)
433
 
        {       printf ("Line %d: Weird count.\n", __LINE__) ;
434
 
                exit (1) ;
435
 
                } ;
436
 
 
437
 
        format_info.format = 0 ;
438
 
        sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
439
 
 
440
 
        last_name = format_info.name ;
441
 
        for (k = 1 ; k < count ; k ++)
442
 
        {       format_info.format = k ;
443
 
                sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
444
 
                } ;
445
 
        format_info.format = 666 ;
446
 
        sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &format_info, sizeof (format_info)) ;
447
 
 
448
 
 
449
 
        printf ("ok\n") ;
450
 
} /* format_tests */
451
 
 
452
 
static  void
453
 
calc_peak_test (int filetype, const char *filename)
454
 
{       SNDFILE         *file ;
455
 
        SF_INFO         sfinfo ;
456
 
        int                     k, format ;
457
 
        double          peak ;
458
 
 
459
 
        print_test_name ("calc_peak_test", filename) ;
460
 
 
461
 
        format = (filetype | SF_FORMAT_PCM_16) ;
462
 
 
463
 
        sfinfo.samplerate       = 44100 ;
464
 
        sfinfo.format           = format ;
465
 
        sfinfo.channels         = 1 ;
466
 
        sfinfo.frames           = BUFFER_LEN ;
467
 
 
468
 
        /* Create double_data with max value of 0.5. */
469
 
        for (k = 0 ; k < BUFFER_LEN ; k++)
470
 
                double_data [k] = (k + 1) / (2.0 * BUFFER_LEN) ;
471
 
 
472
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
473
 
 
474
 
        test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
475
 
 
476
 
        sf_close (file) ;
477
 
 
478
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
479
 
 
480
 
        if (sfinfo.format != format)
481
 
        {       printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
482
 
                exit (1) ;
483
 
                } ;
484
 
 
485
 
        if (sfinfo.frames != BUFFER_LEN)
486
 
        {       printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
487
 
                exit (1) ;
488
 
                } ;
489
 
 
490
 
        if (sfinfo.channels != 1)
491
 
        {       printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
492
 
                exit (1) ;
493
 
                } ;
494
 
 
495
 
        sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ;
496
 
        if (fabs (peak - (1 << 14)) > 1.0)
497
 
        {       printf ("Line %d : Peak value should be %d (is %f).\n", __LINE__, (1 << 14), peak) ;
498
 
                exit (1) ;
499
 
                } ;
500
 
 
501
 
        sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ;
502
 
        if (fabs (peak - 0.5) > 4e-5)
503
 
        {       printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
504
 
                exit (1) ;
505
 
                } ;
506
 
 
507
 
        sf_close (file) ;
508
 
 
509
 
        format = (filetype | SF_FORMAT_FLOAT) ;
510
 
        sfinfo.samplerate       = 44100 ;
511
 
        sfinfo.format           = format ;
512
 
        sfinfo.channels         = 1 ;
513
 
        sfinfo.frames           = BUFFER_LEN ;
514
 
 
515
 
        /* Create double_data with max value of 0.5. */
516
 
        for (k = 0 ; k < BUFFER_LEN ; k++)
517
 
                double_data [k] = (k + 1) / (2.0 * BUFFER_LEN) ;
518
 
 
519
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
520
 
 
521
 
        test_write_double_or_die (file, 0, double_data, BUFFER_LEN, __LINE__) ;
522
 
 
523
 
        sf_close (file) ;
524
 
 
525
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
526
 
 
527
 
        if (sfinfo.format != format)
528
 
        {       printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
529
 
                exit (1) ;
530
 
                } ;
531
 
 
532
 
        if (sfinfo.frames != BUFFER_LEN)
533
 
        {       printf ("\n\nLine %d: Incorrect number of.frames in file. (%d => %ld)\n", __LINE__, BUFFER_LEN, SF_COUNT_TO_LONG (sfinfo.frames)) ;
534
 
                exit (1) ;
535
 
                } ;
536
 
 
537
 
        if (sfinfo.channels != 1)
538
 
        {       printf ("Line %d: Incorrect number of channels in file.\n", __LINE__) ;
539
 
                exit (1) ;
540
 
                } ;
541
 
 
542
 
        sf_command (file, SFC_CALC_SIGNAL_MAX, &peak, sizeof (peak)) ;
543
 
        if (fabs (peak - 0.5) > 1e-5)
544
 
        {       printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
545
 
                exit (1) ;
546
 
                } ;
547
 
 
548
 
        sf_command (file, SFC_CALC_NORM_SIGNAL_MAX, &peak, sizeof (peak)) ;
549
 
        if (fabs (peak - 0.5) > 1e-5)
550
 
        {       printf ("Line %d : Peak value should be %f (is %f).\n", __LINE__, 0.5, peak) ;
551
 
                exit (1) ;
552
 
                } ;
553
 
 
554
 
        sf_close (file) ;
555
 
 
556
 
        unlink (filename) ;
557
 
 
558
 
        printf ("ok\n") ;
559
 
} /* calc_peak_test */
560
 
 
561
 
static void
562
 
truncate_test (const char *filename, int filetype)
563
 
{       SNDFILE         *file ;
564
 
        SF_INFO         sfinfo ;
565
 
        sf_count_t      len ;
566
 
        int                     *int_data ;
567
 
 
568
 
        print_test_name ("truncate_test", filename) ;
569
 
 
570
 
        sfinfo.samplerate       = 11025 ;
571
 
        sfinfo.format           = filetype ;
572
 
        sfinfo.channels         = 2 ;
573
 
 
574
 
        int_data = (int*) double_data ;
575
 
 
576
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
577
 
 
578
 
        test_write_int_or_die (file, 0, int_data, BUFFER_LEN, __LINE__) ;
579
 
 
580
 
        len = 100 ;
581
 
        if (sf_command (file, SFC_FILE_TRUNCATE, &len, sizeof (len)))
582
 
        {       printf ("Line %d: sf_command (SFC_FILE_TRUNCATE) returned error.\n", __LINE__) ;
583
 
                exit (1) ;
584
 
                } ;
585
 
 
586
 
        test_seek_or_die (file, 0, SEEK_CUR, len, 2, __LINE__) ;
587
 
        test_seek_or_die (file, 0, SEEK_END, len, 2, __LINE__) ;
588
 
 
589
 
        sf_close (file) ;
590
 
 
591
 
        unlink (filename) ;
592
 
        puts ("ok") ;
593
 
} /* truncate_test */
594
 
 
595
 
[+ COMMENT
596
 
 
597
 
 Do not edit or modify anything in this comment block.
598
 
 The following line is a file identity tag for the GNU Arch 
599
 
 revision control system.
600
 
 
601
 
 arch-tag: 59e5d452-8dae-45aa-99aa-b78dc0deba1c
602
 
 
603
 
+]