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

« back to all changes in this revision

Viewing changes to lib-src/libsndfile/tests/write_read_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) 1999-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
 
#if HAVE_UNISTD_H
26
 
#include <unistd.h>
27
 
#endif
28
 
 
29
 
#include        <math.h>
30
 
 
31
 
#if (defined (WIN32) || defined (_WIN32))
32
 
#include        <fcntl.h>
33
 
static int truncate (const char *filename, int ignored) ;
34
 
#endif
35
 
 
36
 
#include        <sndfile.h>
37
 
 
38
 
#include        "utils.h"
39
 
 
40
 
#define SAMPLE_RATE                     11025
41
 
#define DATA_LENGTH                     (1<<12)
42
 
 
43
 
#define SILLY_WRITE_COUNT       (234)
44
 
 
45
 
static void     pcm_test_char (const char *str, int format, int long_file_okz) ;
46
 
static void     pcm_test_short (const char *str, int format, int long_file_okz) ;
47
 
static void     pcm_test_24bit (const char *str, int format, int long_file_okz) ;
48
 
static void     pcm_test_int (const char *str, int format, int long_file_okz) ;
49
 
static void     pcm_test_float (const char *str, int format, int long_file_okz) ;
50
 
static void     pcm_test_double (const char *str, int format, int long_file_okz) ;
51
 
 
52
 
static void empty_file_test (const char *filename, int format) ;
53
 
 
54
 
static  double  orig_data [DATA_LENGTH] ;
55
 
static  double  test_data [DATA_LENGTH] ;
56
 
 
57
 
int
58
 
main (int argc, char **argv)
59
 
{       int             do_all = 0 ;
60
 
        int             test_count = 0 ;
61
 
 
62
 
        count_open_files () ;
63
 
 
64
 
        if (argc != 2)
65
 
        {       printf ("Usage : %s <test>\n", argv [0]) ;
66
 
                printf ("    Where <test> is one of the following:\n") ;
67
 
                printf ("           wav   - test WAV file functions (little endian)\n") ;
68
 
                printf ("           aiff  - test AIFF file functions (big endian)\n") ;
69
 
                printf ("           au    - test AU file functions\n") ;
70
 
                printf ("           avr   - test AVR file functions\n") ;
71
 
                printf ("           caf   - test CAF file functions\n") ;
72
 
                printf ("           raw   - test RAW header-less PCM file functions\n") ;
73
 
                printf ("           paf   - test PAF file functions\n") ;
74
 
                printf ("           svx   - test 8SVX/16SV file functions\n") ;
75
 
                printf ("           nist  - test NIST Sphere file functions\n") ;
76
 
                printf ("           ircam - test IRCAM file functions\n") ;
77
 
                printf ("           voc   - Create Voice file functions\n") ;
78
 
                printf ("           w64   - Sonic Foundry's W64 file functions\n") ;
79
 
                printf ("           flac  - test FLAC file functions\n") ;
80
 
                printf ("           all   - perform all tests\n") ;
81
 
                exit (1) ;
82
 
                } ;
83
 
 
84
 
        do_all = !strcmp (argv [1], "all") ;
85
 
 
86
 
        if (do_all || ! strcmp (argv [1], "wav"))
87
 
        {       empty_file_test ("empty_char.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
88
 
                empty_file_test ("empty_short.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
89
 
                empty_file_test ("empty_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
90
 
 
91
 
                pcm_test_char   ("char.wav"             , SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
92
 
                pcm_test_short  ("short.wav"    , SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
93
 
                pcm_test_24bit  ("24bit.wav"    , SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
94
 
                pcm_test_int    ("int.wav"              , SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
95
 
 
96
 
                pcm_test_char   ("char.rifx"    , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
97
 
                pcm_test_short  ("short.rifx"   , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
98
 
                pcm_test_24bit  ("24bit.rifx"   , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
99
 
                pcm_test_int    ("int.rifx"             , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
100
 
 
101
 
                pcm_test_24bit  ("24bit.wavex"  , SF_FORMAT_WAVEX | SF_FORMAT_PCM_24, SF_FALSE) ;
102
 
                pcm_test_int    ("int.wavex"    , SF_FORMAT_WAVEX | SF_FORMAT_PCM_32, SF_FALSE) ;
103
 
 
104
 
                /* Lite remove start */
105
 
                pcm_test_float  ("float.wav"    , SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
106
 
                pcm_test_double ("double.wav"   , SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
107
 
 
108
 
                pcm_test_float  ("float.rifx"   , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
109
 
                pcm_test_double ("double.rifx"  , SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
110
 
 
111
 
                pcm_test_float  ("float.wavex"  , SF_FORMAT_WAVEX | SF_FORMAT_FLOAT , SF_FALSE) ;
112
 
                pcm_test_double ("double.wavex" , SF_FORMAT_WAVEX | SF_FORMAT_DOUBLE, SF_FALSE) ;
113
 
                /* Lite remove end */
114
 
                test_count++ ;
115
 
                } ;
116
 
 
117
 
        if (do_all || ! strcmp (argv [1], "aiff"))
118
 
        {       empty_file_test ("empty_char.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
119
 
                empty_file_test ("empty_short.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
120
 
                empty_file_test ("empty_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
121
 
 
122
 
                pcm_test_char   ("char_u8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_U8, SF_FALSE) ;
123
 
                pcm_test_char   ("char_s8.aiff" , SF_FORMAT_AIFF | SF_FORMAT_PCM_S8, SF_FALSE) ;
124
 
                pcm_test_short  ("short.aiff"   , SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
125
 
                pcm_test_24bit  ("24bit.aiff"   , SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
126
 
                pcm_test_int    ("int.aiff"             , SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
127
 
 
128
 
                pcm_test_short  ("short_sowt.aifc"      , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
129
 
                pcm_test_24bit  ("24bit_sowt.aifc"      , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
130
 
                pcm_test_int    ("int_sowt.aifc"        , SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
131
 
 
132
 
                pcm_test_short  ("short_twos.aifc"      , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
133
 
                pcm_test_24bit  ("24bit_twos.aifc"      , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
134
 
                pcm_test_int    ("int_twos.aifc"        , SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
135
 
 
136
 
                /* Lite remove start */
137
 
                pcm_test_short  ("dwvw16.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_16, SF_TRUE) ;
138
 
                pcm_test_24bit  ("dwvw24.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_24, SF_TRUE) ;
139
 
 
140
 
                pcm_test_float  ("float.aifc"   , SF_FORMAT_AIFF | SF_FORMAT_FLOAT , SF_FALSE) ;
141
 
                pcm_test_double ("double.aifc"  , SF_FORMAT_AIFF | SF_FORMAT_DOUBLE, SF_FALSE) ;
142
 
                /* Lite remove end */
143
 
                test_count++ ;
144
 
                } ;
145
 
 
146
 
        if (do_all || ! strcmp (argv [1], "au"))
147
 
        {       pcm_test_char   ("char.au"      , SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
148
 
                pcm_test_short  ("short.au"     , SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
149
 
                pcm_test_24bit  ("24bit.au"     , SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
150
 
                pcm_test_int    ("int.au"       , SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
151
 
                /* Lite remove start */
152
 
                pcm_test_float  ("float.au"     , SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
153
 
                pcm_test_double ("double.au", SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
154
 
                /* Lite remove end */
155
 
 
156
 
                pcm_test_char   ("char_le.au"   , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
157
 
                pcm_test_short  ("short_le.au"  , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
158
 
                pcm_test_24bit  ("24bit_le.au"  , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
159
 
                pcm_test_int    ("int_le.au"    , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
160
 
                /* Lite remove start */
161
 
                pcm_test_float  ("float_le.au"  , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
162
 
                pcm_test_double ("double_le.au" , SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
163
 
                /* Lite remove end */
164
 
                test_count++ ;
165
 
                } ;
166
 
 
167
 
        if (do_all || ! strcmp (argv [1], "caf"))
168
 
        {       pcm_test_char   ("char.caf"             , SF_FORMAT_CAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
169
 
                pcm_test_short  ("short.caf"    , SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
170
 
                pcm_test_24bit  ("24bit.caf"    , SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
171
 
                pcm_test_int    ("int.caf"              , SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
172
 
                /* Lite remove start */
173
 
                pcm_test_float  ("float.caf"    , SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
174
 
                pcm_test_double ("double.caf"   , SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ;
175
 
                /* Lite remove end */
176
 
 
177
 
                pcm_test_short  ("short_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
178
 
                pcm_test_24bit  ("24bit_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
179
 
                pcm_test_int    ("int_le.caf"   , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
180
 
                /* Lite remove start */
181
 
                pcm_test_float  ("float_le.caf" , SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
182
 
                pcm_test_double ("double_le.caf", SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ;
183
 
                /* Lite remove end */
184
 
                test_count++ ;
185
 
                } ;
186
 
 
187
 
        if (do_all || ! strcmp (argv [1], "raw"))
188
 
        {       pcm_test_char   ("char_s8.raw"  , SF_FORMAT_RAW | SF_FORMAT_PCM_S8, SF_FALSE) ;
189
 
                pcm_test_char   ("char_u8.raw"  , SF_FORMAT_RAW | SF_FORMAT_PCM_U8, SF_FALSE) ;
190
 
 
191
 
                pcm_test_short  ("short_le.raw" , SF_ENDIAN_LITTLE      | SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ;
192
 
                pcm_test_short  ("short_be.raw" , SF_ENDIAN_BIG         | SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ;
193
 
                pcm_test_24bit  ("24bit_le.raw" , SF_ENDIAN_LITTLE      | SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ;
194
 
                pcm_test_24bit  ("24bit_be.raw" , SF_ENDIAN_BIG         | SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ;
195
 
                pcm_test_int    ("int_le.raw"   , SF_ENDIAN_LITTLE      | SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ;
196
 
                pcm_test_int    ("int_be.raw"   , SF_ENDIAN_BIG         | SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ;
197
 
 
198
 
                /* Lite remove start */
199
 
                pcm_test_float  ("float_le.raw" , SF_ENDIAN_LITTLE      | SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ;
200
 
                pcm_test_float  ("float_be.raw" , SF_ENDIAN_BIG         | SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ;
201
 
 
202
 
                pcm_test_double ("double_le.raw", SF_ENDIAN_LITTLE      | SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ;
203
 
                pcm_test_double ("double_be.raw", SF_ENDIAN_BIG         | SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ;
204
 
                /* Lite remove end */
205
 
                test_count++ ;
206
 
                } ;
207
 
 
208
 
        /* Lite remove start */
209
 
        if (do_all || ! strcmp (argv [1], "paf"))
210
 
        {       pcm_test_char   ("char_le.paf", SF_ENDIAN_LITTLE        | SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
211
 
                pcm_test_char   ("char_be.paf", SF_ENDIAN_BIG           | SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
212
 
                pcm_test_short  ("short_le.paf", SF_ENDIAN_LITTLE       | SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ;
213
 
                pcm_test_short  ("short_be.paf", SF_ENDIAN_BIG          | SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ;
214
 
                pcm_test_24bit  ("24bit_le.paf", SF_ENDIAN_LITTLE       | SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ;
215
 
                pcm_test_24bit  ("24bit_be.paf", SF_ENDIAN_BIG          | SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ;
216
 
                test_count++ ;
217
 
                } ;
218
 
 
219
 
        if (do_all || ! strcmp (argv [1], "svx"))
220
 
        {       empty_file_test ("empty_char.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_S8) ;
221
 
                empty_file_test ("empty_short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
222
 
 
223
 
                pcm_test_char   ("char.svx" , SF_FORMAT_SVX | SF_FORMAT_PCM_S8, SF_FALSE) ;
224
 
                pcm_test_short  ("short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16, SF_FALSE) ;
225
 
                test_count++ ;
226
 
                } ;
227
 
 
228
 
        if (do_all || ! strcmp (argv [1], "nist"))
229
 
        {       pcm_test_short  ("short_le.nist", SF_ENDIAN_LITTLE      | SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ;
230
 
                pcm_test_short  ("short_be.nist", SF_ENDIAN_BIG         | SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ;
231
 
                pcm_test_24bit  ("24bit_le.nist", SF_ENDIAN_LITTLE      | SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ;
232
 
                pcm_test_24bit  ("24bit_be.nist", SF_ENDIAN_BIG         | SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ;
233
 
                pcm_test_int    ("int_le.nist"  , SF_ENDIAN_LITTLE      | SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ;
234
 
                pcm_test_int    ("int_be.nist"  , SF_ENDIAN_BIG         | SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ;
235
 
                test_count++ ;
236
 
                } ;
237
 
 
238
 
        if (do_all || ! strcmp (argv [1], "ircam"))
239
 
        {       pcm_test_short  ("short_be.ircam"       , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ;
240
 
                pcm_test_short  ("short_le.ircam"       , SF_ENDIAN_LITTLE      | SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ;
241
 
                pcm_test_int    ("int_be.ircam"         , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ;
242
 
                pcm_test_int    ("int_le.ircam"         , SF_ENDIAN_LITTLE      | SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ;
243
 
                pcm_test_float  ("float_be.ircam"       , SF_ENDIAN_BIG | SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ;
244
 
                pcm_test_float  ("float_le.ircam"       , SF_ENDIAN_LITTLE      | SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ;
245
 
                test_count++ ;
246
 
                } ;
247
 
 
248
 
        if (do_all || ! strcmp (argv [1], "voc"))
249
 
        {       pcm_test_char   ("char.voc" , SF_FORMAT_VOC | SF_FORMAT_PCM_U8, SF_FALSE) ;
250
 
                pcm_test_short  ("short.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_16, SF_FALSE) ;
251
 
 
252
 
                test_count++ ;
253
 
                } ;
254
 
 
255
 
        if (do_all || ! strcmp (argv [1], "mat4"))
256
 
        {       empty_file_test ("empty_short.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
257
 
                empty_file_test ("empty_float.mat4", SF_FORMAT_MAT4 | SF_FORMAT_FLOAT) ;
258
 
 
259
 
                pcm_test_short  ("short_be.mat4"        , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ;
260
 
                pcm_test_short  ("short_le.mat4"        , SF_ENDIAN_LITTLE      | SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ;
261
 
                pcm_test_int    ("int_be.mat4"          , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ;
262
 
                pcm_test_int    ("int_le.mat4"          , SF_ENDIAN_LITTLE      | SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ;
263
 
                pcm_test_float  ("float_be.mat4"        , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ;
264
 
                pcm_test_float  ("float_le.mat4"        , SF_ENDIAN_LITTLE      | SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ;
265
 
                pcm_test_double ("double_be.mat4"       , SF_ENDIAN_BIG | SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ;
266
 
                pcm_test_double ("double_le.mat4"       , SF_ENDIAN_LITTLE      | SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ;
267
 
                test_count++ ;
268
 
                } ;
269
 
 
270
 
        if (do_all || ! strcmp (argv [1], "mat5"))
271
 
        {       increment_open_file_count () ;
272
 
 
273
 
                empty_file_test ("empty_char.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8) ;
274
 
                empty_file_test ("empty_short.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
275
 
                empty_file_test ("empty_float.mat5", SF_FORMAT_MAT5 | SF_FORMAT_FLOAT) ;
276
 
 
277
 
                pcm_test_char   ("char_be.mat5"         , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ;
278
 
                pcm_test_char   ("char_le.mat5"         , SF_ENDIAN_LITTLE      | SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ;
279
 
                pcm_test_short  ("short_be.mat5"        , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ;
280
 
                pcm_test_short  ("short_le.mat5"        , SF_ENDIAN_LITTLE      | SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ;
281
 
                pcm_test_int    ("int_be.mat5"          , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ;
282
 
                pcm_test_int    ("int_le.mat5"          , SF_ENDIAN_LITTLE      | SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ;
283
 
                pcm_test_float  ("float_be.mat5"        , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ;
284
 
                pcm_test_float  ("float_le.mat5"        , SF_ENDIAN_LITTLE      | SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ;
285
 
                pcm_test_double ("double_be.mat5"       , SF_ENDIAN_BIG | SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ;
286
 
                pcm_test_double ("double_le.mat5"       , SF_ENDIAN_LITTLE      | SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ;
287
 
                test_count++ ;
288
 
                } ;
289
 
 
290
 
        if (do_all || ! strcmp (argv [1], "pvf"))
291
 
        {       pcm_test_char   ("char.pvf"     , SF_FORMAT_PVF | SF_FORMAT_PCM_S8, SF_FALSE) ;
292
 
                pcm_test_short  ("short.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_16, SF_FALSE) ;
293
 
                pcm_test_int    ("int.pvf"      , SF_FORMAT_PVF | SF_FORMAT_PCM_32, SF_FALSE) ;
294
 
                test_count++ ;
295
 
                } ;
296
 
 
297
 
        if (do_all || ! strcmp (argv [1], "htk"))
298
 
        {       pcm_test_short  ("short.htk", SF_FORMAT_HTK | SF_FORMAT_PCM_16, SF_FALSE) ;
299
 
                test_count++ ;
300
 
                } ;
301
 
 
302
 
        if (do_all || ! strcmp (argv [1], "avr"))
303
 
        {       pcm_test_char   ("char_u8.avr"  , SF_FORMAT_AVR | SF_FORMAT_PCM_U8, SF_FALSE) ;
304
 
                pcm_test_char   ("char_s8.avr"  , SF_FORMAT_AVR | SF_FORMAT_PCM_S8, SF_FALSE) ;
305
 
                pcm_test_short  ("short.avr"    , SF_FORMAT_AVR | SF_FORMAT_PCM_16, SF_FALSE) ;
306
 
                test_count++ ;
307
 
                } ;
308
 
        /* Lite remove end */
309
 
 
310
 
        if (do_all || ! strcmp (argv [1], "w64"))
311
 
        {       empty_file_test ("empty_char.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ;
312
 
                empty_file_test ("empty_short.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
313
 
                empty_file_test ("empty_float.w64", SF_FORMAT_W64 | SF_FORMAT_FLOAT) ;
314
 
 
315
 
                pcm_test_char   ("char.w64"             , SF_FORMAT_W64 | SF_FORMAT_PCM_U8, SF_FALSE) ;
316
 
                pcm_test_short  ("short.w64"    , SF_FORMAT_W64 | SF_FORMAT_PCM_16, SF_FALSE) ;
317
 
                pcm_test_24bit  ("24bit.w64"    , SF_FORMAT_W64 | SF_FORMAT_PCM_24, SF_FALSE) ;
318
 
                pcm_test_int    ("int.w64"              , SF_FORMAT_W64 | SF_FORMAT_PCM_32, SF_FALSE) ;
319
 
                /* Lite remove start */
320
 
                pcm_test_float  ("float.w64"    , SF_FORMAT_W64 | SF_FORMAT_FLOAT , SF_FALSE) ;
321
 
                pcm_test_double ("double.w64"   , SF_FORMAT_W64 | SF_FORMAT_DOUBLE, SF_FALSE) ;
322
 
                /* Lite remove end */
323
 
                test_count++ ;
324
 
                } ;
325
 
 
326
 
        if (do_all || ! strcmp (argv [1], "sds"))
327
 
        {       pcm_test_char   ("char.sds"             , SF_FORMAT_SDS | SF_FORMAT_PCM_S8, SF_TRUE) ;
328
 
                pcm_test_short  ("short.sds"    , SF_FORMAT_SDS | SF_FORMAT_PCM_16, SF_TRUE) ;
329
 
                pcm_test_24bit  ("24bit.sds"    , SF_FORMAT_SDS | SF_FORMAT_PCM_24, SF_TRUE) ;
330
 
                test_count++ ;
331
 
                } ;
332
 
 
333
 
        if (do_all || ! strcmp (argv [1], "sd2"))
334
 
        {       pcm_test_char   ("char.sd2"             , SF_FORMAT_SD2 | SF_FORMAT_PCM_S8, SF_TRUE) ;
335
 
                pcm_test_short  ("short.sd2"    , SF_FORMAT_SD2 | SF_FORMAT_PCM_16, SF_TRUE) ;
336
 
                pcm_test_24bit  ("24bit.sd2"    , SF_FORMAT_SD2 | SF_FORMAT_PCM_24, SF_TRUE) ;
337
 
                test_count++ ;
338
 
                } ;
339
 
 
340
 
        if (do_all || ! strcmp (argv [1], "flac"))
341
 
        {
342
 
#ifdef HAVE_FLAC_ALL_H
343
 
                pcm_test_char   ("char.flac"    , SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, SF_TRUE) ;
344
 
                pcm_test_short  ("short.flac"   , SF_FORMAT_FLAC | SF_FORMAT_PCM_16, SF_TRUE) ;
345
 
                pcm_test_24bit  ("24bit.flac"   , SF_FORMAT_FLAC | SF_FORMAT_PCM_24, SF_TRUE) ;
346
 
#else
347
 
                printf ("    **** flac not supported in this binary. ****\n") ;
348
 
#endif
349
 
                test_count++ ;
350
 
                } ;
351
 
 
352
 
        if (test_count == 0)
353
 
        {       printf ("Mono : ************************************\n") ;
354
 
                printf ("Mono : *  No '%s' test defined.\n", argv [1]) ;
355
 
                printf ("Mono : ************************************\n") ;
356
 
                return 1 ;
357
 
                } ;
358
 
 
359
 
        /* Only open file descriptors should be stdin, stdout and stderr. */
360
 
        check_open_file_count_or_die (__LINE__) ;
361
 
 
362
 
        return 0 ;
363
 
} /* main */
364
 
 
365
 
/*============================================================================================
366
 
**      Helper functions and macros.
367
 
*/
368
 
 
369
 
static void     create_short_file (const char *filename) ;
370
 
 
371
 
#define CHAR_ERROR(x,y)         (abs ((x) - (y)) > 255)
372
 
#define INT_ERROR(x,y)          (((x) - (y)) != 0)
373
 
#define TRIBYTE_ERROR(x,y)      (abs ((x) - (y)) > 255)
374
 
#define FLOAT_ERROR(x,y)        (fabs ((x) - (y)) > 1e-5)
375
 
 
376
 
#define CONVERT_DATA(k,len,new,orig)                                    \
377
 
                        {       for ((k) = 0 ; (k) < (len) ; (k) ++)    \
378
 
                                        (new) [k] = (orig) [k] ;                        \
379
 
                                }
380
 
 
381
 
 
382
 
/*======================================================================================
383
 
*/
384
 
 
385
 
static void mono_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
386
 
static void stereo_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
387
 
static void mono_rdwr_char_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
388
 
static void new_rdwr_char_test (const char *filename, int format, int allow_fd) ;
389
 
 
390
 
static void
391
 
pcm_test_char (const char *filename, int format, int long_file_ok)
392
 
{       SF_INFO         sfinfo ;
393
 
        short           *orig, *test ;
394
 
        int                     k, items, allow_fd ;
395
 
 
396
 
        /* Sd2 files cannot be opened from an existing file descriptor. */
397
 
        allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
398
 
 
399
 
        print_test_name ("pcm_test_char", filename) ;
400
 
 
401
 
        sfinfo.samplerate       = 44100 ;
402
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
403
 
        sfinfo.channels         = 1 ;
404
 
        sfinfo.format           = format ;
405
 
 
406
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, 32000.0) ;
407
 
 
408
 
        orig = (short*) orig_data ;
409
 
        test = (short*) test_data ;
410
 
 
411
 
        /* Make this a macro so gdb steps over it in one go. */
412
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
413
 
 
414
 
        items = DATA_LENGTH ;
415
 
 
416
 
        /* Some test broken out here. */
417
 
 
418
 
        mono_char_test (filename, format, long_file_ok, allow_fd) ;
419
 
 
420
 
        /* Sub format DWVW does not allow seeking. */
421
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
422
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
423
 
        {       unlink (filename) ;
424
 
                printf ("no seek : ok\n") ;
425
 
                return ;
426
 
                } ;
427
 
 
428
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
429
 
                mono_rdwr_char_test (filename, format, long_file_ok, allow_fd) ;
430
 
 
431
 
        /* If the format doesn't support stereo we're done. */
432
 
        sfinfo.channels = 2 ;
433
 
        if (sf_format_check (&sfinfo) == 0)
434
 
        {       unlink (filename) ;
435
 
                puts ("no stereo : ok") ;
436
 
                return ;
437
 
                } ;
438
 
 
439
 
        stereo_char_test (filename, format, long_file_ok, allow_fd) ;
440
 
 
441
 
        /* New read/write test. Not sure if this is needed yet. */
442
 
 
443
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
444
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
445
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
446
 
                new_rdwr_char_test (filename, format, allow_fd) ;
447
 
 
448
 
        delete_file (format, filename) ;
449
 
 
450
 
        puts ("ok") ;
451
 
        return ;
452
 
} /* pcm_test_char */
453
 
 
454
 
static void
455
 
mono_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
456
 
{       SNDFILE         *file ;
457
 
        SF_INFO         sfinfo ;
458
 
        short           *orig, *test ;
459
 
        sf_count_t      count ;
460
 
        int                     k, items ;
461
 
 
462
 
        sfinfo.samplerate       = 44100 ;
463
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
464
 
        sfinfo.channels         = 1 ;
465
 
        sfinfo.format           = format ;
466
 
 
467
 
        orig = (short*) orig_data ;
468
 
        test = (short*) test_data ;
469
 
 
470
 
        items = DATA_LENGTH ;
471
 
 
472
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
473
 
 
474
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
475
 
 
476
 
        test_write_short_or_die (file, 0, orig, items, __LINE__) ;
477
 
        sf_write_sync (file) ;
478
 
        test_write_short_or_die (file, 0, orig, items, __LINE__) ;
479
 
        sf_write_sync (file) ;
480
 
 
481
 
        /* Add non-audio data after the audio. */
482
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
483
 
 
484
 
        sf_close (file) ;
485
 
 
486
 
        memset (test, 0, items * sizeof (short)) ;
487
 
 
488
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
489
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
490
 
 
491
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
492
 
 
493
 
        if (sfinfo.format != format)
494
 
        {       printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
495
 
                exit (1) ;
496
 
                } ;
497
 
 
498
 
        if (sfinfo.frames < 2 * items)
499
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
500
 
                exit (1) ;
501
 
                } ;
502
 
 
503
 
        if (! long_file_ok && sfinfo.frames > 2 * items)
504
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
505
 
                exit (1) ;
506
 
                } ;
507
 
 
508
 
        if (sfinfo.channels != 1)
509
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
510
 
                exit (1) ;
511
 
                } ;
512
 
 
513
 
        check_log_buffer_or_die (file, __LINE__) ;
514
 
 
515
 
        test_read_short_or_die (file, 0, test, items, __LINE__) ;
516
 
        for (k = 0 ; k < items ; k++)
517
 
                if (CHAR_ERROR (orig [k], test [k]))
518
 
                {       printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
519
 
                        exit (1) ;
520
 
                        } ;
521
 
 
522
 
        /* Seek to start of file. */
523
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
524
 
 
525
 
        test_read_short_or_die (file, 0, test, 4, __LINE__) ;
526
 
        for (k = 0 ; k < 4 ; k++)
527
 
                if (CHAR_ERROR (orig [k], test [k]))
528
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
529
 
                        exit (1) ;
530
 
                        } ;
531
 
 
532
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
533
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
534
 
        {       sf_close (file) ;
535
 
                unlink (filename) ;
536
 
                printf ("no seek : ") ;
537
 
                return ;
538
 
                } ;
539
 
 
540
 
        /* Seek to offset from start of file. */
541
 
        test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
542
 
 
543
 
        test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
544
 
        for (k = 10 ; k < 14 ; k++)
545
 
                if (CHAR_ERROR (orig [k], test [k]))
546
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
547
 
                        exit (1) ;
548
 
                        } ;
549
 
 
550
 
        /* Seek to offset from current position. */
551
 
        test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
552
 
 
553
 
        test_read_short_or_die (file, 0, test + 20, 4, __LINE__) ;
554
 
        for (k = 20 ; k < 24 ; k++)
555
 
                if (CHAR_ERROR (orig [k], test [k]))
556
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
557
 
                        exit (1) ;
558
 
                        } ;
559
 
 
560
 
        /* Seek to offset from end of file. */
561
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
562
 
 
563
 
        test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
564
 
        for (k = 10 ; k < 14 ; k++)
565
 
                if (CHAR_ERROR (orig [k], test [k]))
566
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
567
 
                        exit (1) ;
568
 
                        } ;
569
 
 
570
 
        /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
571
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
572
 
 
573
 
        count = 0 ;
574
 
        while (count < sfinfo.frames)
575
 
                count += sf_read_short (file, test, 311) ;
576
 
 
577
 
        /* Check that no error has occurred. */
578
 
        if (sf_error (file))
579
 
        {       printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
580
 
                puts (sf_strerror (file)) ;
581
 
                exit (1) ;
582
 
                } ;
583
 
 
584
 
        /* Check that we haven't read beyond EOF. */
585
 
        if (count > sfinfo.frames)
586
 
        {       printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
587
 
                exit (1) ;
588
 
                } ;
589
 
 
590
 
        test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
591
 
 
592
 
        sf_close (file) ;
593
 
 
594
 
} /* mono_char_test */
595
 
 
596
 
static void
597
 
stereo_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
598
 
{       SNDFILE         *file ;
599
 
        SF_INFO         sfinfo ;
600
 
        short           *orig, *test ;
601
 
        int                     k, items, frames ;
602
 
 
603
 
        orig = (short*) orig_data ;
604
 
        test = (short*) test_data ;
605
 
 
606
 
        sfinfo.samplerate       = 44100 ;
607
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
608
 
        sfinfo.channels         = 2 ;
609
 
        sfinfo.format           = format ;
610
 
 
611
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, 32000.0) ;
612
 
 
613
 
        orig = (short*) orig_data ;
614
 
        test = (short*) test_data ;
615
 
 
616
 
        /* Make this a macro so gdb steps over it in one go. */
617
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
618
 
 
619
 
        items = DATA_LENGTH ;
620
 
        frames = items / sfinfo.channels ;
621
 
 
622
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
623
 
 
624
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
625
 
 
626
 
        test_writef_short_or_die (file, 0, orig, frames, __LINE__) ;
627
 
 
628
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
629
 
 
630
 
        sf_close (file) ;
631
 
 
632
 
        memset (test, 0, items * sizeof (short)) ;
633
 
 
634
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
635
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
636
 
 
637
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
638
 
 
639
 
        if (sfinfo.format != format)
640
 
        {       printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
641
 
                                __LINE__, format, sfinfo.format) ;
642
 
                exit (1) ;
643
 
                } ;
644
 
 
645
 
        if (sfinfo.frames < frames)
646
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
647
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
648
 
                exit (1) ;
649
 
                } ;
650
 
 
651
 
        if (! long_file_ok && sfinfo.frames > frames)
652
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
653
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
654
 
                exit (1) ;
655
 
                } ;
656
 
 
657
 
        if (sfinfo.channels != 2)
658
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
659
 
                exit (1) ;
660
 
                } ;
661
 
 
662
 
        check_log_buffer_or_die (file, __LINE__) ;
663
 
 
664
 
        test_readf_short_or_die (file, 0, test, frames, __LINE__) ;
665
 
        for (k = 0 ; k < items ; k++)
666
 
                if (CHAR_ERROR (test [k], orig [k]))
667
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
668
 
                        exit (1) ;
669
 
                        } ;
670
 
 
671
 
        /* Seek to start of file. */
672
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
673
 
 
674
 
        test_readf_short_or_die (file, 0, test, 2, __LINE__) ;
675
 
        for (k = 0 ; k < 4 ; k++)
676
 
                if (CHAR_ERROR (test [k], orig [k]))
677
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
678
 
                        exit (1) ;
679
 
                        } ;
680
 
 
681
 
        /* Seek to offset from start of file. */
682
 
        test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
683
 
 
684
 
        /* Check for errors here. */
685
 
        if (sf_error (file))
686
 
        {       printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
687
 
                puts (sf_strerror (file)) ;
688
 
                exit (1) ;
689
 
                } ;
690
 
 
691
 
        if (sf_read_short (file, test, 1) > 0)
692
 
        {       printf ("Line %d: Should return 0.\n", __LINE__) ;
693
 
                exit (1) ;
694
 
                } ;
695
 
 
696
 
        if (! sf_error (file))
697
 
        {       printf ("Line %d: Should return an error.\n", __LINE__) ;
698
 
                exit (1) ;
699
 
                } ;
700
 
        /*-----------------------*/
701
 
 
702
 
        test_readf_short_or_die (file, 0, test + 10, 2, __LINE__) ;
703
 
        for (k = 20 ; k < 24 ; k++)
704
 
                if (CHAR_ERROR (test [k], orig [k]))
705
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
706
 
                        exit (1) ;
707
 
                        } ;
708
 
 
709
 
        /* Seek to offset from current position. */
710
 
        test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
711
 
 
712
 
        test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
713
 
        for (k = 40 ; k < 44 ; k++)
714
 
                if (CHAR_ERROR (test [k], orig [k]))
715
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
716
 
                        exit (1) ;
717
 
                        } ;
718
 
 
719
 
        /* Seek to offset from end of file. */
720
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
721
 
 
722
 
        test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
723
 
        for (k = 20 ; k < 24 ; k++)
724
 
                if (CHAR_ERROR (test [k], orig [k]))
725
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
726
 
                        exit (1) ;
727
 
                        } ;
728
 
 
729
 
        sf_close (file) ;
730
 
} /* stereo_char_test */
731
 
 
732
 
static void
733
 
mono_rdwr_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
734
 
{       SNDFILE         *file ;
735
 
        SF_INFO         sfinfo ;
736
 
        short           *orig, *test ;
737
 
        int                     k, pass ;
738
 
 
739
 
        orig = (short*) orig_data ;
740
 
        test = (short*) test_data ;
741
 
 
742
 
        sfinfo.samplerate       = SAMPLE_RATE ;
743
 
        sfinfo.frames           = DATA_LENGTH ;
744
 
        sfinfo.channels         = 1 ;
745
 
        sfinfo.format           = format ;
746
 
 
747
 
        if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
748
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
749
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
750
 
                unlink (filename) ;
751
 
        else
752
 
        {       /* Create a short file. */
753
 
                create_short_file (filename) ;
754
 
 
755
 
                /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
756
 
                ** If this returns a valif pointer sf_open() screwed up.
757
 
                */
758
 
                if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
759
 
                {       printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
760
 
                        exit (1) ;
761
 
                        } ;
762
 
 
763
 
                /* Truncate the file to zero bytes. */
764
 
                if (truncate (filename, 0) < 0)
765
 
                {       printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
766
 
                        perror (NULL) ;
767
 
                        exit (1) ;
768
 
                        } ;
769
 
                } ;
770
 
 
771
 
        /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
772
 
        ** all the usual data required when opening the file in WRITE mode.
773
 
        */
774
 
        sfinfo.samplerate       = SAMPLE_RATE ;
775
 
        sfinfo.frames           = DATA_LENGTH ;
776
 
        sfinfo.channels         = 1 ;
777
 
        sfinfo.format           = format ;
778
 
 
779
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
780
 
 
781
 
        /* Do 3 writes followed by reads. After each, check the data and the current
782
 
        ** read and write offsets.
783
 
        */
784
 
        for (pass = 1 ; pass <= 3 ; pass ++)
785
 
        {       orig [20] = pass * 2 ;
786
 
 
787
 
                /* Write some data. */
788
 
                test_write_short_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
789
 
 
790
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
791
 
 
792
 
                /* Read what we just wrote. */
793
 
                test_read_short_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
794
 
 
795
 
                /* Check the data. */
796
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
797
 
                        if (CHAR_ERROR (orig [k], test [k]))
798
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
799
 
                                oct_save_short (orig, test, DATA_LENGTH) ;
800
 
                                exit (1) ;
801
 
                                } ;
802
 
 
803
 
                test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
804
 
                } ; /* for (pass ...) */
805
 
 
806
 
        sf_close (file) ;
807
 
 
808
 
        /* Open the file again to check the data. */
809
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
810
 
 
811
 
        if (sfinfo.format != format)
812
 
        {       printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
813
 
                exit (1) ;
814
 
                } ;
815
 
 
816
 
        if (sfinfo.frames < 3 * DATA_LENGTH)
817
 
        {       printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
818
 
                exit (1) ;
819
 
                }
820
 
 
821
 
        if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
822
 
        {       printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
823
 
                exit (1) ;
824
 
                } ;
825
 
 
826
 
        if (sfinfo.channels != 1)
827
 
        {       printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
828
 
                exit (1) ;
829
 
                } ;
830
 
 
831
 
        if (! long_file_ok)
832
 
                test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
833
 
        else
834
 
                test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
835
 
 
836
 
        for (pass = 1 ; pass <= 3 ; pass ++)
837
 
        {       orig [20] = pass * 2 ;
838
 
 
839
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
840
 
 
841
 
                /* Read what we just wrote. */
842
 
                test_read_short_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
843
 
 
844
 
                /* Check the data. */
845
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
846
 
                        if (CHAR_ERROR (orig [k], test [k]))
847
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
848
 
                                oct_save_short (orig, test, DATA_LENGTH) ;
849
 
                                exit (1) ;
850
 
                                } ;
851
 
 
852
 
                } ; /* for (pass ...) */
853
 
 
854
 
        sf_close (file) ;
855
 
} /* mono_rdwr_short_test */
856
 
 
857
 
static void
858
 
new_rdwr_char_test (const char *filename, int format, int allow_fd)
859
 
{       SNDFILE *wfile, *rwfile ;
860
 
        SF_INFO sfinfo ;
861
 
        short           *orig, *test ;
862
 
        int             items, frames ;
863
 
 
864
 
        orig = (short*) orig_data ;
865
 
        test = (short*) test_data ;
866
 
 
867
 
        sfinfo.samplerate       = 44100 ;
868
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
869
 
        sfinfo.channels         = 2 ;
870
 
        sfinfo.format           = format ;
871
 
 
872
 
        items = DATA_LENGTH ;
873
 
        frames = items / sfinfo.channels ;
874
 
 
875
 
        wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
876
 
        sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
877
 
        test_writef_short_or_die (wfile, 1, orig, frames, __LINE__) ;
878
 
        sf_write_sync (wfile) ;
879
 
        test_writef_short_or_die (wfile, 2, orig, frames, __LINE__) ;
880
 
        sf_write_sync (wfile) ;
881
 
 
882
 
        rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
883
 
        if (sfinfo.frames != 2 * frames)
884
 
        {       printf ("\n\nLine %d : incorrect number of frames in file (%ld shold be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
885
 
                exit (1) ;
886
 
                } ;
887
 
 
888
 
        test_writef_short_or_die (wfile, 3, orig, frames, __LINE__) ;
889
 
 
890
 
        test_readf_short_or_die (rwfile, 1, test, frames, __LINE__) ;
891
 
        test_readf_short_or_die (rwfile, 2, test, frames, __LINE__) ;
892
 
 
893
 
        sf_close (wfile) ;
894
 
        sf_close (rwfile) ;
895
 
} /* new_rdwr_char_test */
896
 
 
897
 
 
898
 
/*======================================================================================
899
 
*/
900
 
 
901
 
static void mono_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
902
 
static void stereo_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
903
 
static void mono_rdwr_short_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
904
 
static void new_rdwr_short_test (const char *filename, int format, int allow_fd) ;
905
 
 
906
 
static void
907
 
pcm_test_short (const char *filename, int format, int long_file_ok)
908
 
{       SF_INFO         sfinfo ;
909
 
        short           *orig, *test ;
910
 
        int                     k, items, allow_fd ;
911
 
 
912
 
        /* Sd2 files cannot be opened from an existing file descriptor. */
913
 
        allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
914
 
 
915
 
        print_test_name ("pcm_test_short", filename) ;
916
 
 
917
 
        sfinfo.samplerate       = 44100 ;
918
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
919
 
        sfinfo.channels         = 1 ;
920
 
        sfinfo.format           = format ;
921
 
 
922
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, 32000.0) ;
923
 
 
924
 
        orig = (short*) orig_data ;
925
 
        test = (short*) test_data ;
926
 
 
927
 
        /* Make this a macro so gdb steps over it in one go. */
928
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
929
 
 
930
 
        items = DATA_LENGTH ;
931
 
 
932
 
        /* Some test broken out here. */
933
 
 
934
 
        mono_short_test (filename, format, long_file_ok, allow_fd) ;
935
 
 
936
 
        /* Sub format DWVW does not allow seeking. */
937
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
938
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
939
 
        {       unlink (filename) ;
940
 
                printf ("no seek : ok\n") ;
941
 
                return ;
942
 
                } ;
943
 
 
944
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
945
 
                mono_rdwr_short_test (filename, format, long_file_ok, allow_fd) ;
946
 
 
947
 
        /* If the format doesn't support stereo we're done. */
948
 
        sfinfo.channels = 2 ;
949
 
        if (sf_format_check (&sfinfo) == 0)
950
 
        {       unlink (filename) ;
951
 
                puts ("no stereo : ok") ;
952
 
                return ;
953
 
                } ;
954
 
 
955
 
        stereo_short_test (filename, format, long_file_ok, allow_fd) ;
956
 
 
957
 
        /* New read/write test. Not sure if this is needed yet. */
958
 
 
959
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
960
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
961
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
962
 
                new_rdwr_short_test (filename, format, allow_fd) ;
963
 
 
964
 
        delete_file (format, filename) ;
965
 
 
966
 
        puts ("ok") ;
967
 
        return ;
968
 
} /* pcm_test_short */
969
 
 
970
 
static void
971
 
mono_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
972
 
{       SNDFILE         *file ;
973
 
        SF_INFO         sfinfo ;
974
 
        short           *orig, *test ;
975
 
        sf_count_t      count ;
976
 
        int                     k, items ;
977
 
 
978
 
        sfinfo.samplerate       = 44100 ;
979
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
980
 
        sfinfo.channels         = 1 ;
981
 
        sfinfo.format           = format ;
982
 
 
983
 
        orig = (short*) orig_data ;
984
 
        test = (short*) test_data ;
985
 
 
986
 
        items = DATA_LENGTH ;
987
 
 
988
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
989
 
 
990
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
991
 
 
992
 
        test_write_short_or_die (file, 0, orig, items, __LINE__) ;
993
 
        sf_write_sync (file) ;
994
 
        test_write_short_or_die (file, 0, orig, items, __LINE__) ;
995
 
        sf_write_sync (file) ;
996
 
 
997
 
        /* Add non-audio data after the audio. */
998
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
999
 
 
1000
 
        sf_close (file) ;
1001
 
 
1002
 
        memset (test, 0, items * sizeof (short)) ;
1003
 
 
1004
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
1005
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
1006
 
 
1007
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
1008
 
 
1009
 
        if (sfinfo.format != format)
1010
 
        {       printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
1011
 
                exit (1) ;
1012
 
                } ;
1013
 
 
1014
 
        if (sfinfo.frames < 2 * items)
1015
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
1016
 
                exit (1) ;
1017
 
                } ;
1018
 
 
1019
 
        if (! long_file_ok && sfinfo.frames > 2 * items)
1020
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
1021
 
                exit (1) ;
1022
 
                } ;
1023
 
 
1024
 
        if (sfinfo.channels != 1)
1025
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
1026
 
                exit (1) ;
1027
 
                } ;
1028
 
 
1029
 
        check_log_buffer_or_die (file, __LINE__) ;
1030
 
 
1031
 
        test_read_short_or_die (file, 0, test, items, __LINE__) ;
1032
 
        for (k = 0 ; k < items ; k++)
1033
 
                if (INT_ERROR (orig [k], test [k]))
1034
 
                {       printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1035
 
                        exit (1) ;
1036
 
                        } ;
1037
 
 
1038
 
        /* Seek to start of file. */
1039
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
1040
 
 
1041
 
        test_read_short_or_die (file, 0, test, 4, __LINE__) ;
1042
 
        for (k = 0 ; k < 4 ; k++)
1043
 
                if (INT_ERROR (orig [k], test [k]))
1044
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1045
 
                        exit (1) ;
1046
 
                        } ;
1047
 
 
1048
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
1049
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
1050
 
        {       sf_close (file) ;
1051
 
                unlink (filename) ;
1052
 
                printf ("no seek : ") ;
1053
 
                return ;
1054
 
                } ;
1055
 
 
1056
 
        /* Seek to offset from start of file. */
1057
 
        test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
1058
 
 
1059
 
        test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
1060
 
        for (k = 10 ; k < 14 ; k++)
1061
 
                if (INT_ERROR (orig [k], test [k]))
1062
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
1063
 
                        exit (1) ;
1064
 
                        } ;
1065
 
 
1066
 
        /* Seek to offset from current position. */
1067
 
        test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
1068
 
 
1069
 
        test_read_short_or_die (file, 0, test + 20, 4, __LINE__) ;
1070
 
        for (k = 20 ; k < 24 ; k++)
1071
 
                if (INT_ERROR (orig [k], test [k]))
1072
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
1073
 
                        exit (1) ;
1074
 
                        } ;
1075
 
 
1076
 
        /* Seek to offset from end of file. */
1077
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
1078
 
 
1079
 
        test_read_short_or_die (file, 0, test + 10, 4, __LINE__) ;
1080
 
        for (k = 10 ; k < 14 ; k++)
1081
 
                if (INT_ERROR (orig [k], test [k]))
1082
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
1083
 
                        exit (1) ;
1084
 
                        } ;
1085
 
 
1086
 
        /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
1087
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
1088
 
 
1089
 
        count = 0 ;
1090
 
        while (count < sfinfo.frames)
1091
 
                count += sf_read_short (file, test, 311) ;
1092
 
 
1093
 
        /* Check that no error has occurred. */
1094
 
        if (sf_error (file))
1095
 
        {       printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
1096
 
                puts (sf_strerror (file)) ;
1097
 
                exit (1) ;
1098
 
                } ;
1099
 
 
1100
 
        /* Check that we haven't read beyond EOF. */
1101
 
        if (count > sfinfo.frames)
1102
 
        {       printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
1103
 
                exit (1) ;
1104
 
                } ;
1105
 
 
1106
 
        test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
1107
 
 
1108
 
        sf_close (file) ;
1109
 
 
1110
 
} /* mono_short_test */
1111
 
 
1112
 
static void
1113
 
stereo_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
1114
 
{       SNDFILE         *file ;
1115
 
        SF_INFO         sfinfo ;
1116
 
        short           *orig, *test ;
1117
 
        int                     k, items, frames ;
1118
 
 
1119
 
        orig = (short*) orig_data ;
1120
 
        test = (short*) test_data ;
1121
 
 
1122
 
        sfinfo.samplerate       = 44100 ;
1123
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
1124
 
        sfinfo.channels         = 2 ;
1125
 
        sfinfo.format           = format ;
1126
 
 
1127
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, 32000.0) ;
1128
 
 
1129
 
        orig = (short*) orig_data ;
1130
 
        test = (short*) test_data ;
1131
 
 
1132
 
        /* Make this a macro so gdb steps over it in one go. */
1133
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
1134
 
 
1135
 
        items = DATA_LENGTH ;
1136
 
        frames = items / sfinfo.channels ;
1137
 
 
1138
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
1139
 
 
1140
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
1141
 
 
1142
 
        test_writef_short_or_die (file, 0, orig, frames, __LINE__) ;
1143
 
 
1144
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
1145
 
 
1146
 
        sf_close (file) ;
1147
 
 
1148
 
        memset (test, 0, items * sizeof (short)) ;
1149
 
 
1150
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
1151
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
1152
 
 
1153
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
1154
 
 
1155
 
        if (sfinfo.format != format)
1156
 
        {       printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
1157
 
                                __LINE__, format, sfinfo.format) ;
1158
 
                exit (1) ;
1159
 
                } ;
1160
 
 
1161
 
        if (sfinfo.frames < frames)
1162
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
1163
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
1164
 
                exit (1) ;
1165
 
                } ;
1166
 
 
1167
 
        if (! long_file_ok && sfinfo.frames > frames)
1168
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
1169
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
1170
 
                exit (1) ;
1171
 
                } ;
1172
 
 
1173
 
        if (sfinfo.channels != 2)
1174
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
1175
 
                exit (1) ;
1176
 
                } ;
1177
 
 
1178
 
        check_log_buffer_or_die (file, __LINE__) ;
1179
 
 
1180
 
        test_readf_short_or_die (file, 0, test, frames, __LINE__) ;
1181
 
        for (k = 0 ; k < items ; k++)
1182
 
                if (INT_ERROR (test [k], orig [k]))
1183
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1184
 
                        exit (1) ;
1185
 
                        } ;
1186
 
 
1187
 
        /* Seek to start of file. */
1188
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
1189
 
 
1190
 
        test_readf_short_or_die (file, 0, test, 2, __LINE__) ;
1191
 
        for (k = 0 ; k < 4 ; k++)
1192
 
                if (INT_ERROR (test [k], orig [k]))
1193
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1194
 
                        exit (1) ;
1195
 
                        } ;
1196
 
 
1197
 
        /* Seek to offset from start of file. */
1198
 
        test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
1199
 
 
1200
 
        /* Check for errors here. */
1201
 
        if (sf_error (file))
1202
 
        {       printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
1203
 
                puts (sf_strerror (file)) ;
1204
 
                exit (1) ;
1205
 
                } ;
1206
 
 
1207
 
        if (sf_read_short (file, test, 1) > 0)
1208
 
        {       printf ("Line %d: Should return 0.\n", __LINE__) ;
1209
 
                exit (1) ;
1210
 
                } ;
1211
 
 
1212
 
        if (! sf_error (file))
1213
 
        {       printf ("Line %d: Should return an error.\n", __LINE__) ;
1214
 
                exit (1) ;
1215
 
                } ;
1216
 
        /*-----------------------*/
1217
 
 
1218
 
        test_readf_short_or_die (file, 0, test + 10, 2, __LINE__) ;
1219
 
        for (k = 20 ; k < 24 ; k++)
1220
 
                if (INT_ERROR (test [k], orig [k]))
1221
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1222
 
                        exit (1) ;
1223
 
                        } ;
1224
 
 
1225
 
        /* Seek to offset from current position. */
1226
 
        test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
1227
 
 
1228
 
        test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
1229
 
        for (k = 40 ; k < 44 ; k++)
1230
 
                if (INT_ERROR (test [k], orig [k]))
1231
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1232
 
                        exit (1) ;
1233
 
                        } ;
1234
 
 
1235
 
        /* Seek to offset from end of file. */
1236
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
1237
 
 
1238
 
        test_readf_short_or_die (file, 0, test + 20, 2, __LINE__) ;
1239
 
        for (k = 20 ; k < 24 ; k++)
1240
 
                if (INT_ERROR (test [k], orig [k]))
1241
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1242
 
                        exit (1) ;
1243
 
                        } ;
1244
 
 
1245
 
        sf_close (file) ;
1246
 
} /* stereo_short_test */
1247
 
 
1248
 
static void
1249
 
mono_rdwr_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
1250
 
{       SNDFILE         *file ;
1251
 
        SF_INFO         sfinfo ;
1252
 
        short           *orig, *test ;
1253
 
        int                     k, pass ;
1254
 
 
1255
 
        orig = (short*) orig_data ;
1256
 
        test = (short*) test_data ;
1257
 
 
1258
 
        sfinfo.samplerate       = SAMPLE_RATE ;
1259
 
        sfinfo.frames           = DATA_LENGTH ;
1260
 
        sfinfo.channels         = 1 ;
1261
 
        sfinfo.format           = format ;
1262
 
 
1263
 
        if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
1264
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
1265
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
1266
 
                unlink (filename) ;
1267
 
        else
1268
 
        {       /* Create a short file. */
1269
 
                create_short_file (filename) ;
1270
 
 
1271
 
                /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
1272
 
                ** If this returns a valif pointer sf_open() screwed up.
1273
 
                */
1274
 
                if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
1275
 
                {       printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
1276
 
                        exit (1) ;
1277
 
                        } ;
1278
 
 
1279
 
                /* Truncate the file to zero bytes. */
1280
 
                if (truncate (filename, 0) < 0)
1281
 
                {       printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
1282
 
                        perror (NULL) ;
1283
 
                        exit (1) ;
1284
 
                        } ;
1285
 
                } ;
1286
 
 
1287
 
        /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
1288
 
        ** all the usual data required when opening the file in WRITE mode.
1289
 
        */
1290
 
        sfinfo.samplerate       = SAMPLE_RATE ;
1291
 
        sfinfo.frames           = DATA_LENGTH ;
1292
 
        sfinfo.channels         = 1 ;
1293
 
        sfinfo.format           = format ;
1294
 
 
1295
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
1296
 
 
1297
 
        /* Do 3 writes followed by reads. After each, check the data and the current
1298
 
        ** read and write offsets.
1299
 
        */
1300
 
        for (pass = 1 ; pass <= 3 ; pass ++)
1301
 
        {       orig [20] = pass * 2 ;
1302
 
 
1303
 
                /* Write some data. */
1304
 
                test_write_short_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
1305
 
 
1306
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
1307
 
 
1308
 
                /* Read what we just wrote. */
1309
 
                test_read_short_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
1310
 
 
1311
 
                /* Check the data. */
1312
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
1313
 
                        if (INT_ERROR (orig [k], test [k]))
1314
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
1315
 
                                oct_save_short (orig, test, DATA_LENGTH) ;
1316
 
                                exit (1) ;
1317
 
                                } ;
1318
 
 
1319
 
                test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
1320
 
                } ; /* for (pass ...) */
1321
 
 
1322
 
        sf_close (file) ;
1323
 
 
1324
 
        /* Open the file again to check the data. */
1325
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
1326
 
 
1327
 
        if (sfinfo.format != format)
1328
 
        {       printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
1329
 
                exit (1) ;
1330
 
                } ;
1331
 
 
1332
 
        if (sfinfo.frames < 3 * DATA_LENGTH)
1333
 
        {       printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
1334
 
                exit (1) ;
1335
 
                }
1336
 
 
1337
 
        if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
1338
 
        {       printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
1339
 
                exit (1) ;
1340
 
                } ;
1341
 
 
1342
 
        if (sfinfo.channels != 1)
1343
 
        {       printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
1344
 
                exit (1) ;
1345
 
                } ;
1346
 
 
1347
 
        if (! long_file_ok)
1348
 
                test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
1349
 
        else
1350
 
                test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
1351
 
 
1352
 
        for (pass = 1 ; pass <= 3 ; pass ++)
1353
 
        {       orig [20] = pass * 2 ;
1354
 
 
1355
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
1356
 
 
1357
 
                /* Read what we just wrote. */
1358
 
                test_read_short_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
1359
 
 
1360
 
                /* Check the data. */
1361
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
1362
 
                        if (INT_ERROR (orig [k], test [k]))
1363
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
1364
 
                                oct_save_short (orig, test, DATA_LENGTH) ;
1365
 
                                exit (1) ;
1366
 
                                } ;
1367
 
 
1368
 
                } ; /* for (pass ...) */
1369
 
 
1370
 
        sf_close (file) ;
1371
 
} /* mono_rdwr_short_test */
1372
 
 
1373
 
static void
1374
 
new_rdwr_short_test (const char *filename, int format, int allow_fd)
1375
 
{       SNDFILE *wfile, *rwfile ;
1376
 
        SF_INFO sfinfo ;
1377
 
        short           *orig, *test ;
1378
 
        int             items, frames ;
1379
 
 
1380
 
        orig = (short*) orig_data ;
1381
 
        test = (short*) test_data ;
1382
 
 
1383
 
        sfinfo.samplerate       = 44100 ;
1384
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
1385
 
        sfinfo.channels         = 2 ;
1386
 
        sfinfo.format           = format ;
1387
 
 
1388
 
        items = DATA_LENGTH ;
1389
 
        frames = items / sfinfo.channels ;
1390
 
 
1391
 
        wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
1392
 
        sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
1393
 
        test_writef_short_or_die (wfile, 1, orig, frames, __LINE__) ;
1394
 
        sf_write_sync (wfile) ;
1395
 
        test_writef_short_or_die (wfile, 2, orig, frames, __LINE__) ;
1396
 
        sf_write_sync (wfile) ;
1397
 
 
1398
 
        rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
1399
 
        if (sfinfo.frames != 2 * frames)
1400
 
        {       printf ("\n\nLine %d : incorrect number of frames in file (%ld shold be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
1401
 
                exit (1) ;
1402
 
                } ;
1403
 
 
1404
 
        test_writef_short_or_die (wfile, 3, orig, frames, __LINE__) ;
1405
 
 
1406
 
        test_readf_short_or_die (rwfile, 1, test, frames, __LINE__) ;
1407
 
        test_readf_short_or_die (rwfile, 2, test, frames, __LINE__) ;
1408
 
 
1409
 
        sf_close (wfile) ;
1410
 
        sf_close (rwfile) ;
1411
 
} /* new_rdwr_short_test */
1412
 
 
1413
 
 
1414
 
/*======================================================================================
1415
 
*/
1416
 
 
1417
 
static void mono_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
1418
 
static void stereo_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
1419
 
static void mono_rdwr_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
1420
 
static void new_rdwr_24bit_test (const char *filename, int format, int allow_fd) ;
1421
 
 
1422
 
static void
1423
 
pcm_test_24bit (const char *filename, int format, int long_file_ok)
1424
 
{       SF_INFO         sfinfo ;
1425
 
        int             *orig, *test ;
1426
 
        int                     k, items, allow_fd ;
1427
 
 
1428
 
        /* Sd2 files cannot be opened from an existing file descriptor. */
1429
 
        allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
1430
 
 
1431
 
        print_test_name ("pcm_test_24bit", filename) ;
1432
 
 
1433
 
        sfinfo.samplerate       = 44100 ;
1434
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
1435
 
        sfinfo.channels         = 1 ;
1436
 
        sfinfo.format           = format ;
1437
 
 
1438
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, (1.0 * 0x7F000000)) ;
1439
 
 
1440
 
        orig = (int*) orig_data ;
1441
 
        test = (int*) test_data ;
1442
 
 
1443
 
        /* Make this a macro so gdb steps over it in one go. */
1444
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
1445
 
 
1446
 
        items = DATA_LENGTH ;
1447
 
 
1448
 
        /* Some test broken out here. */
1449
 
 
1450
 
        mono_24bit_test (filename, format, long_file_ok, allow_fd) ;
1451
 
 
1452
 
        /* Sub format DWVW does not allow seeking. */
1453
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
1454
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
1455
 
        {       unlink (filename) ;
1456
 
                printf ("no seek : ok\n") ;
1457
 
                return ;
1458
 
                } ;
1459
 
 
1460
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
1461
 
                mono_rdwr_24bit_test (filename, format, long_file_ok, allow_fd) ;
1462
 
 
1463
 
        /* If the format doesn't support stereo we're done. */
1464
 
        sfinfo.channels = 2 ;
1465
 
        if (sf_format_check (&sfinfo) == 0)
1466
 
        {       unlink (filename) ;
1467
 
                puts ("no stereo : ok") ;
1468
 
                return ;
1469
 
                } ;
1470
 
 
1471
 
        stereo_24bit_test (filename, format, long_file_ok, allow_fd) ;
1472
 
 
1473
 
        /* New read/write test. Not sure if this is needed yet. */
1474
 
 
1475
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
1476
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
1477
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
1478
 
                new_rdwr_24bit_test (filename, format, allow_fd) ;
1479
 
 
1480
 
        delete_file (format, filename) ;
1481
 
 
1482
 
        puts ("ok") ;
1483
 
        return ;
1484
 
} /* pcm_test_24bit */
1485
 
 
1486
 
static void
1487
 
mono_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
1488
 
{       SNDFILE         *file ;
1489
 
        SF_INFO         sfinfo ;
1490
 
        int             *orig, *test ;
1491
 
        sf_count_t      count ;
1492
 
        int                     k, items ;
1493
 
 
1494
 
        sfinfo.samplerate       = 44100 ;
1495
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
1496
 
        sfinfo.channels         = 1 ;
1497
 
        sfinfo.format           = format ;
1498
 
 
1499
 
        orig = (int*) orig_data ;
1500
 
        test = (int*) test_data ;
1501
 
 
1502
 
        items = DATA_LENGTH ;
1503
 
 
1504
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
1505
 
 
1506
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
1507
 
 
1508
 
        test_write_int_or_die (file, 0, orig, items, __LINE__) ;
1509
 
        sf_write_sync (file) ;
1510
 
        test_write_int_or_die (file, 0, orig, items, __LINE__) ;
1511
 
        sf_write_sync (file) ;
1512
 
 
1513
 
        /* Add non-audio data after the audio. */
1514
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
1515
 
 
1516
 
        sf_close (file) ;
1517
 
 
1518
 
        memset (test, 0, items * sizeof (int)) ;
1519
 
 
1520
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
1521
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
1522
 
 
1523
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
1524
 
 
1525
 
        if (sfinfo.format != format)
1526
 
        {       printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
1527
 
                exit (1) ;
1528
 
                } ;
1529
 
 
1530
 
        if (sfinfo.frames < 2 * items)
1531
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
1532
 
                exit (1) ;
1533
 
                } ;
1534
 
 
1535
 
        if (! long_file_ok && sfinfo.frames > 2 * items)
1536
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
1537
 
                exit (1) ;
1538
 
                } ;
1539
 
 
1540
 
        if (sfinfo.channels != 1)
1541
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
1542
 
                exit (1) ;
1543
 
                } ;
1544
 
 
1545
 
        check_log_buffer_or_die (file, __LINE__) ;
1546
 
 
1547
 
        test_read_int_or_die (file, 0, test, items, __LINE__) ;
1548
 
        for (k = 0 ; k < items ; k++)
1549
 
                if (TRIBYTE_ERROR (orig [k], test [k]))
1550
 
                {       printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1551
 
                        exit (1) ;
1552
 
                        } ;
1553
 
 
1554
 
        /* Seek to start of file. */
1555
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
1556
 
 
1557
 
        test_read_int_or_die (file, 0, test, 4, __LINE__) ;
1558
 
        for (k = 0 ; k < 4 ; k++)
1559
 
                if (TRIBYTE_ERROR (orig [k], test [k]))
1560
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1561
 
                        exit (1) ;
1562
 
                        } ;
1563
 
 
1564
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
1565
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
1566
 
        {       sf_close (file) ;
1567
 
                unlink (filename) ;
1568
 
                printf ("no seek : ") ;
1569
 
                return ;
1570
 
                } ;
1571
 
 
1572
 
        /* Seek to offset from start of file. */
1573
 
        test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
1574
 
 
1575
 
        test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
1576
 
        for (k = 10 ; k < 14 ; k++)
1577
 
                if (TRIBYTE_ERROR (orig [k], test [k]))
1578
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
1579
 
                        exit (1) ;
1580
 
                        } ;
1581
 
 
1582
 
        /* Seek to offset from current position. */
1583
 
        test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
1584
 
 
1585
 
        test_read_int_or_die (file, 0, test + 20, 4, __LINE__) ;
1586
 
        for (k = 20 ; k < 24 ; k++)
1587
 
                if (TRIBYTE_ERROR (orig [k], test [k]))
1588
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
1589
 
                        exit (1) ;
1590
 
                        } ;
1591
 
 
1592
 
        /* Seek to offset from end of file. */
1593
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
1594
 
 
1595
 
        test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
1596
 
        for (k = 10 ; k < 14 ; k++)
1597
 
                if (TRIBYTE_ERROR (orig [k], test [k]))
1598
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
1599
 
                        exit (1) ;
1600
 
                        } ;
1601
 
 
1602
 
        /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
1603
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
1604
 
 
1605
 
        count = 0 ;
1606
 
        while (count < sfinfo.frames)
1607
 
                count += sf_read_int (file, test, 311) ;
1608
 
 
1609
 
        /* Check that no error has occurred. */
1610
 
        if (sf_error (file))
1611
 
        {       printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
1612
 
                puts (sf_strerror (file)) ;
1613
 
                exit (1) ;
1614
 
                } ;
1615
 
 
1616
 
        /* Check that we haven't read beyond EOF. */
1617
 
        if (count > sfinfo.frames)
1618
 
        {       printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
1619
 
                exit (1) ;
1620
 
                } ;
1621
 
 
1622
 
        test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
1623
 
 
1624
 
        sf_close (file) ;
1625
 
 
1626
 
} /* mono_24bit_test */
1627
 
 
1628
 
static void
1629
 
stereo_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
1630
 
{       SNDFILE         *file ;
1631
 
        SF_INFO         sfinfo ;
1632
 
        int             *orig, *test ;
1633
 
        int                     k, items, frames ;
1634
 
 
1635
 
        orig = (int*) orig_data ;
1636
 
        test = (int*) test_data ;
1637
 
 
1638
 
        sfinfo.samplerate       = 44100 ;
1639
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
1640
 
        sfinfo.channels         = 2 ;
1641
 
        sfinfo.format           = format ;
1642
 
 
1643
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, (1.0 * 0x7F000000)) ;
1644
 
 
1645
 
        orig = (int*) orig_data ;
1646
 
        test = (int*) test_data ;
1647
 
 
1648
 
        /* Make this a macro so gdb steps over it in one go. */
1649
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
1650
 
 
1651
 
        items = DATA_LENGTH ;
1652
 
        frames = items / sfinfo.channels ;
1653
 
 
1654
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
1655
 
 
1656
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
1657
 
 
1658
 
        test_writef_int_or_die (file, 0, orig, frames, __LINE__) ;
1659
 
 
1660
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
1661
 
 
1662
 
        sf_close (file) ;
1663
 
 
1664
 
        memset (test, 0, items * sizeof (int)) ;
1665
 
 
1666
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
1667
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
1668
 
 
1669
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
1670
 
 
1671
 
        if (sfinfo.format != format)
1672
 
        {       printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
1673
 
                                __LINE__, format, sfinfo.format) ;
1674
 
                exit (1) ;
1675
 
                } ;
1676
 
 
1677
 
        if (sfinfo.frames < frames)
1678
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
1679
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
1680
 
                exit (1) ;
1681
 
                } ;
1682
 
 
1683
 
        if (! long_file_ok && sfinfo.frames > frames)
1684
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
1685
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
1686
 
                exit (1) ;
1687
 
                } ;
1688
 
 
1689
 
        if (sfinfo.channels != 2)
1690
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
1691
 
                exit (1) ;
1692
 
                } ;
1693
 
 
1694
 
        check_log_buffer_or_die (file, __LINE__) ;
1695
 
 
1696
 
        test_readf_int_or_die (file, 0, test, frames, __LINE__) ;
1697
 
        for (k = 0 ; k < items ; k++)
1698
 
                if (TRIBYTE_ERROR (test [k], orig [k]))
1699
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1700
 
                        exit (1) ;
1701
 
                        } ;
1702
 
 
1703
 
        /* Seek to start of file. */
1704
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
1705
 
 
1706
 
        test_readf_int_or_die (file, 0, test, 2, __LINE__) ;
1707
 
        for (k = 0 ; k < 4 ; k++)
1708
 
                if (TRIBYTE_ERROR (test [k], orig [k]))
1709
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1710
 
                        exit (1) ;
1711
 
                        } ;
1712
 
 
1713
 
        /* Seek to offset from start of file. */
1714
 
        test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
1715
 
 
1716
 
        /* Check for errors here. */
1717
 
        if (sf_error (file))
1718
 
        {       printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
1719
 
                puts (sf_strerror (file)) ;
1720
 
                exit (1) ;
1721
 
                } ;
1722
 
 
1723
 
        if (sf_read_int (file, test, 1) > 0)
1724
 
        {       printf ("Line %d: Should return 0.\n", __LINE__) ;
1725
 
                exit (1) ;
1726
 
                } ;
1727
 
 
1728
 
        if (! sf_error (file))
1729
 
        {       printf ("Line %d: Should return an error.\n", __LINE__) ;
1730
 
                exit (1) ;
1731
 
                } ;
1732
 
        /*-----------------------*/
1733
 
 
1734
 
        test_readf_int_or_die (file, 0, test + 10, 2, __LINE__) ;
1735
 
        for (k = 20 ; k < 24 ; k++)
1736
 
                if (TRIBYTE_ERROR (test [k], orig [k]))
1737
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1738
 
                        exit (1) ;
1739
 
                        } ;
1740
 
 
1741
 
        /* Seek to offset from current position. */
1742
 
        test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
1743
 
 
1744
 
        test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
1745
 
        for (k = 40 ; k < 44 ; k++)
1746
 
                if (TRIBYTE_ERROR (test [k], orig [k]))
1747
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1748
 
                        exit (1) ;
1749
 
                        } ;
1750
 
 
1751
 
        /* Seek to offset from end of file. */
1752
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
1753
 
 
1754
 
        test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
1755
 
        for (k = 20 ; k < 24 ; k++)
1756
 
                if (TRIBYTE_ERROR (test [k], orig [k]))
1757
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
1758
 
                        exit (1) ;
1759
 
                        } ;
1760
 
 
1761
 
        sf_close (file) ;
1762
 
} /* stereo_24bit_test */
1763
 
 
1764
 
static void
1765
 
mono_rdwr_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
1766
 
{       SNDFILE         *file ;
1767
 
        SF_INFO         sfinfo ;
1768
 
        int             *orig, *test ;
1769
 
        int                     k, pass ;
1770
 
 
1771
 
        orig = (int*) orig_data ;
1772
 
        test = (int*) test_data ;
1773
 
 
1774
 
        sfinfo.samplerate       = SAMPLE_RATE ;
1775
 
        sfinfo.frames           = DATA_LENGTH ;
1776
 
        sfinfo.channels         = 1 ;
1777
 
        sfinfo.format           = format ;
1778
 
 
1779
 
        if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
1780
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
1781
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
1782
 
                unlink (filename) ;
1783
 
        else
1784
 
        {       /* Create a short file. */
1785
 
                create_short_file (filename) ;
1786
 
 
1787
 
                /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
1788
 
                ** If this returns a valif pointer sf_open() screwed up.
1789
 
                */
1790
 
                if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
1791
 
                {       printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
1792
 
                        exit (1) ;
1793
 
                        } ;
1794
 
 
1795
 
                /* Truncate the file to zero bytes. */
1796
 
                if (truncate (filename, 0) < 0)
1797
 
                {       printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
1798
 
                        perror (NULL) ;
1799
 
                        exit (1) ;
1800
 
                        } ;
1801
 
                } ;
1802
 
 
1803
 
        /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
1804
 
        ** all the usual data required when opening the file in WRITE mode.
1805
 
        */
1806
 
        sfinfo.samplerate       = SAMPLE_RATE ;
1807
 
        sfinfo.frames           = DATA_LENGTH ;
1808
 
        sfinfo.channels         = 1 ;
1809
 
        sfinfo.format           = format ;
1810
 
 
1811
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
1812
 
 
1813
 
        /* Do 3 writes followed by reads. After each, check the data and the current
1814
 
        ** read and write offsets.
1815
 
        */
1816
 
        for (pass = 1 ; pass <= 3 ; pass ++)
1817
 
        {       orig [20] = pass * 2 ;
1818
 
 
1819
 
                /* Write some data. */
1820
 
                test_write_int_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
1821
 
 
1822
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
1823
 
 
1824
 
                /* Read what we just wrote. */
1825
 
                test_read_int_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
1826
 
 
1827
 
                /* Check the data. */
1828
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
1829
 
                        if (TRIBYTE_ERROR (orig [k], test [k]))
1830
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
1831
 
                                oct_save_int (orig, test, DATA_LENGTH) ;
1832
 
                                exit (1) ;
1833
 
                                } ;
1834
 
 
1835
 
                test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
1836
 
                } ; /* for (pass ...) */
1837
 
 
1838
 
        sf_close (file) ;
1839
 
 
1840
 
        /* Open the file again to check the data. */
1841
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
1842
 
 
1843
 
        if (sfinfo.format != format)
1844
 
        {       printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
1845
 
                exit (1) ;
1846
 
                } ;
1847
 
 
1848
 
        if (sfinfo.frames < 3 * DATA_LENGTH)
1849
 
        {       printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
1850
 
                exit (1) ;
1851
 
                }
1852
 
 
1853
 
        if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
1854
 
        {       printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
1855
 
                exit (1) ;
1856
 
                } ;
1857
 
 
1858
 
        if (sfinfo.channels != 1)
1859
 
        {       printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
1860
 
                exit (1) ;
1861
 
                } ;
1862
 
 
1863
 
        if (! long_file_ok)
1864
 
                test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
1865
 
        else
1866
 
                test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
1867
 
 
1868
 
        for (pass = 1 ; pass <= 3 ; pass ++)
1869
 
        {       orig [20] = pass * 2 ;
1870
 
 
1871
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
1872
 
 
1873
 
                /* Read what we just wrote. */
1874
 
                test_read_int_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
1875
 
 
1876
 
                /* Check the data. */
1877
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
1878
 
                        if (TRIBYTE_ERROR (orig [k], test [k]))
1879
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
1880
 
                                oct_save_int (orig, test, DATA_LENGTH) ;
1881
 
                                exit (1) ;
1882
 
                                } ;
1883
 
 
1884
 
                } ; /* for (pass ...) */
1885
 
 
1886
 
        sf_close (file) ;
1887
 
} /* mono_rdwr_int_test */
1888
 
 
1889
 
static void
1890
 
new_rdwr_24bit_test (const char *filename, int format, int allow_fd)
1891
 
{       SNDFILE *wfile, *rwfile ;
1892
 
        SF_INFO sfinfo ;
1893
 
        int             *orig, *test ;
1894
 
        int             items, frames ;
1895
 
 
1896
 
        orig = (int*) orig_data ;
1897
 
        test = (int*) test_data ;
1898
 
 
1899
 
        sfinfo.samplerate       = 44100 ;
1900
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
1901
 
        sfinfo.channels         = 2 ;
1902
 
        sfinfo.format           = format ;
1903
 
 
1904
 
        items = DATA_LENGTH ;
1905
 
        frames = items / sfinfo.channels ;
1906
 
 
1907
 
        wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
1908
 
        sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
1909
 
        test_writef_int_or_die (wfile, 1, orig, frames, __LINE__) ;
1910
 
        sf_write_sync (wfile) ;
1911
 
        test_writef_int_or_die (wfile, 2, orig, frames, __LINE__) ;
1912
 
        sf_write_sync (wfile) ;
1913
 
 
1914
 
        rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
1915
 
        if (sfinfo.frames != 2 * frames)
1916
 
        {       printf ("\n\nLine %d : incorrect number of frames in file (%ld shold be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
1917
 
                exit (1) ;
1918
 
                } ;
1919
 
 
1920
 
        test_writef_int_or_die (wfile, 3, orig, frames, __LINE__) ;
1921
 
 
1922
 
        test_readf_int_or_die (rwfile, 1, test, frames, __LINE__) ;
1923
 
        test_readf_int_or_die (rwfile, 2, test, frames, __LINE__) ;
1924
 
 
1925
 
        sf_close (wfile) ;
1926
 
        sf_close (rwfile) ;
1927
 
} /* new_rdwr_24bit_test */
1928
 
 
1929
 
 
1930
 
/*======================================================================================
1931
 
*/
1932
 
 
1933
 
static void mono_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
1934
 
static void stereo_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
1935
 
static void mono_rdwr_int_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
1936
 
static void new_rdwr_int_test (const char *filename, int format, int allow_fd) ;
1937
 
 
1938
 
static void
1939
 
pcm_test_int (const char *filename, int format, int long_file_ok)
1940
 
{       SF_INFO         sfinfo ;
1941
 
        int             *orig, *test ;
1942
 
        int                     k, items, allow_fd ;
1943
 
 
1944
 
        /* Sd2 files cannot be opened from an existing file descriptor. */
1945
 
        allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
1946
 
 
1947
 
        print_test_name ("pcm_test_int", filename) ;
1948
 
 
1949
 
        sfinfo.samplerate       = 44100 ;
1950
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
1951
 
        sfinfo.channels         = 1 ;
1952
 
        sfinfo.format           = format ;
1953
 
 
1954
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, (1.0 * 0x7F000000)) ;
1955
 
 
1956
 
        orig = (int*) orig_data ;
1957
 
        test = (int*) test_data ;
1958
 
 
1959
 
        /* Make this a macro so gdb steps over it in one go. */
1960
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
1961
 
 
1962
 
        items = DATA_LENGTH ;
1963
 
 
1964
 
        /* Some test broken out here. */
1965
 
 
1966
 
        mono_int_test (filename, format, long_file_ok, allow_fd) ;
1967
 
 
1968
 
        /* Sub format DWVW does not allow seeking. */
1969
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
1970
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
1971
 
        {       unlink (filename) ;
1972
 
                printf ("no seek : ok\n") ;
1973
 
                return ;
1974
 
                } ;
1975
 
 
1976
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
1977
 
                mono_rdwr_int_test (filename, format, long_file_ok, allow_fd) ;
1978
 
 
1979
 
        /* If the format doesn't support stereo we're done. */
1980
 
        sfinfo.channels = 2 ;
1981
 
        if (sf_format_check (&sfinfo) == 0)
1982
 
        {       unlink (filename) ;
1983
 
                puts ("no stereo : ok") ;
1984
 
                return ;
1985
 
                } ;
1986
 
 
1987
 
        stereo_int_test (filename, format, long_file_ok, allow_fd) ;
1988
 
 
1989
 
        /* New read/write test. Not sure if this is needed yet. */
1990
 
 
1991
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
1992
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
1993
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
1994
 
                new_rdwr_int_test (filename, format, allow_fd) ;
1995
 
 
1996
 
        delete_file (format, filename) ;
1997
 
 
1998
 
        puts ("ok") ;
1999
 
        return ;
2000
 
} /* pcm_test_int */
2001
 
 
2002
 
static void
2003
 
mono_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
2004
 
{       SNDFILE         *file ;
2005
 
        SF_INFO         sfinfo ;
2006
 
        int             *orig, *test ;
2007
 
        sf_count_t      count ;
2008
 
        int                     k, items ;
2009
 
 
2010
 
        sfinfo.samplerate       = 44100 ;
2011
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
2012
 
        sfinfo.channels         = 1 ;
2013
 
        sfinfo.format           = format ;
2014
 
 
2015
 
        orig = (int*) orig_data ;
2016
 
        test = (int*) test_data ;
2017
 
 
2018
 
        items = DATA_LENGTH ;
2019
 
 
2020
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
2021
 
 
2022
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
2023
 
 
2024
 
        test_write_int_or_die (file, 0, orig, items, __LINE__) ;
2025
 
        sf_write_sync (file) ;
2026
 
        test_write_int_or_die (file, 0, orig, items, __LINE__) ;
2027
 
        sf_write_sync (file) ;
2028
 
 
2029
 
        /* Add non-audio data after the audio. */
2030
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
2031
 
 
2032
 
        sf_close (file) ;
2033
 
 
2034
 
        memset (test, 0, items * sizeof (int)) ;
2035
 
 
2036
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
2037
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
2038
 
 
2039
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
2040
 
 
2041
 
        if (sfinfo.format != format)
2042
 
        {       printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
2043
 
                exit (1) ;
2044
 
                } ;
2045
 
 
2046
 
        if (sfinfo.frames < 2 * items)
2047
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
2048
 
                exit (1) ;
2049
 
                } ;
2050
 
 
2051
 
        if (! long_file_ok && sfinfo.frames > 2 * items)
2052
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
2053
 
                exit (1) ;
2054
 
                } ;
2055
 
 
2056
 
        if (sfinfo.channels != 1)
2057
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
2058
 
                exit (1) ;
2059
 
                } ;
2060
 
 
2061
 
        check_log_buffer_or_die (file, __LINE__) ;
2062
 
 
2063
 
        test_read_int_or_die (file, 0, test, items, __LINE__) ;
2064
 
        for (k = 0 ; k < items ; k++)
2065
 
                if (INT_ERROR (orig [k], test [k]))
2066
 
                {       printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
2067
 
                        exit (1) ;
2068
 
                        } ;
2069
 
 
2070
 
        /* Seek to start of file. */
2071
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
2072
 
 
2073
 
        test_read_int_or_die (file, 0, test, 4, __LINE__) ;
2074
 
        for (k = 0 ; k < 4 ; k++)
2075
 
                if (INT_ERROR (orig [k], test [k]))
2076
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
2077
 
                        exit (1) ;
2078
 
                        } ;
2079
 
 
2080
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
2081
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
2082
 
        {       sf_close (file) ;
2083
 
                unlink (filename) ;
2084
 
                printf ("no seek : ") ;
2085
 
                return ;
2086
 
                } ;
2087
 
 
2088
 
        /* Seek to offset from start of file. */
2089
 
        test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
2090
 
 
2091
 
        test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
2092
 
        for (k = 10 ; k < 14 ; k++)
2093
 
                if (INT_ERROR (orig [k], test [k]))
2094
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
2095
 
                        exit (1) ;
2096
 
                        } ;
2097
 
 
2098
 
        /* Seek to offset from current position. */
2099
 
        test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
2100
 
 
2101
 
        test_read_int_or_die (file, 0, test + 20, 4, __LINE__) ;
2102
 
        for (k = 20 ; k < 24 ; k++)
2103
 
                if (INT_ERROR (orig [k], test [k]))
2104
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
2105
 
                        exit (1) ;
2106
 
                        } ;
2107
 
 
2108
 
        /* Seek to offset from end of file. */
2109
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
2110
 
 
2111
 
        test_read_int_or_die (file, 0, test + 10, 4, __LINE__) ;
2112
 
        for (k = 10 ; k < 14 ; k++)
2113
 
                if (INT_ERROR (orig [k], test [k]))
2114
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : 0x%X => 0x%X).\n", __LINE__, k, test [k], orig [k]) ;
2115
 
                        exit (1) ;
2116
 
                        } ;
2117
 
 
2118
 
        /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
2119
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
2120
 
 
2121
 
        count = 0 ;
2122
 
        while (count < sfinfo.frames)
2123
 
                count += sf_read_int (file, test, 311) ;
2124
 
 
2125
 
        /* Check that no error has occurred. */
2126
 
        if (sf_error (file))
2127
 
        {       printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
2128
 
                puts (sf_strerror (file)) ;
2129
 
                exit (1) ;
2130
 
                } ;
2131
 
 
2132
 
        /* Check that we haven't read beyond EOF. */
2133
 
        if (count > sfinfo.frames)
2134
 
        {       printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
2135
 
                exit (1) ;
2136
 
                } ;
2137
 
 
2138
 
        test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
2139
 
 
2140
 
        sf_close (file) ;
2141
 
 
2142
 
} /* mono_int_test */
2143
 
 
2144
 
static void
2145
 
stereo_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
2146
 
{       SNDFILE         *file ;
2147
 
        SF_INFO         sfinfo ;
2148
 
        int             *orig, *test ;
2149
 
        int                     k, items, frames ;
2150
 
 
2151
 
        orig = (int*) orig_data ;
2152
 
        test = (int*) test_data ;
2153
 
 
2154
 
        sfinfo.samplerate       = 44100 ;
2155
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
2156
 
        sfinfo.channels         = 2 ;
2157
 
        sfinfo.format           = format ;
2158
 
 
2159
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, (1.0 * 0x7F000000)) ;
2160
 
 
2161
 
        orig = (int*) orig_data ;
2162
 
        test = (int*) test_data ;
2163
 
 
2164
 
        /* Make this a macro so gdb steps over it in one go. */
2165
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
2166
 
 
2167
 
        items = DATA_LENGTH ;
2168
 
        frames = items / sfinfo.channels ;
2169
 
 
2170
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
2171
 
 
2172
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
2173
 
 
2174
 
        test_writef_int_or_die (file, 0, orig, frames, __LINE__) ;
2175
 
 
2176
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
2177
 
 
2178
 
        sf_close (file) ;
2179
 
 
2180
 
        memset (test, 0, items * sizeof (int)) ;
2181
 
 
2182
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
2183
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
2184
 
 
2185
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
2186
 
 
2187
 
        if (sfinfo.format != format)
2188
 
        {       printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
2189
 
                                __LINE__, format, sfinfo.format) ;
2190
 
                exit (1) ;
2191
 
                } ;
2192
 
 
2193
 
        if (sfinfo.frames < frames)
2194
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
2195
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
2196
 
                exit (1) ;
2197
 
                } ;
2198
 
 
2199
 
        if (! long_file_ok && sfinfo.frames > frames)
2200
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
2201
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
2202
 
                exit (1) ;
2203
 
                } ;
2204
 
 
2205
 
        if (sfinfo.channels != 2)
2206
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
2207
 
                exit (1) ;
2208
 
                } ;
2209
 
 
2210
 
        check_log_buffer_or_die (file, __LINE__) ;
2211
 
 
2212
 
        test_readf_int_or_die (file, 0, test, frames, __LINE__) ;
2213
 
        for (k = 0 ; k < items ; k++)
2214
 
                if (INT_ERROR (test [k], orig [k]))
2215
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
2216
 
                        exit (1) ;
2217
 
                        } ;
2218
 
 
2219
 
        /* Seek to start of file. */
2220
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
2221
 
 
2222
 
        test_readf_int_or_die (file, 0, test, 2, __LINE__) ;
2223
 
        for (k = 0 ; k < 4 ; k++)
2224
 
                if (INT_ERROR (test [k], orig [k]))
2225
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
2226
 
                        exit (1) ;
2227
 
                        } ;
2228
 
 
2229
 
        /* Seek to offset from start of file. */
2230
 
        test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
2231
 
 
2232
 
        /* Check for errors here. */
2233
 
        if (sf_error (file))
2234
 
        {       printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
2235
 
                puts (sf_strerror (file)) ;
2236
 
                exit (1) ;
2237
 
                } ;
2238
 
 
2239
 
        if (sf_read_int (file, test, 1) > 0)
2240
 
        {       printf ("Line %d: Should return 0.\n", __LINE__) ;
2241
 
                exit (1) ;
2242
 
                } ;
2243
 
 
2244
 
        if (! sf_error (file))
2245
 
        {       printf ("Line %d: Should return an error.\n", __LINE__) ;
2246
 
                exit (1) ;
2247
 
                } ;
2248
 
        /*-----------------------*/
2249
 
 
2250
 
        test_readf_int_or_die (file, 0, test + 10, 2, __LINE__) ;
2251
 
        for (k = 20 ; k < 24 ; k++)
2252
 
                if (INT_ERROR (test [k], orig [k]))
2253
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
2254
 
                        exit (1) ;
2255
 
                        } ;
2256
 
 
2257
 
        /* Seek to offset from current position. */
2258
 
        test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
2259
 
 
2260
 
        test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
2261
 
        for (k = 40 ; k < 44 ; k++)
2262
 
                if (INT_ERROR (test [k], orig [k]))
2263
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
2264
 
                        exit (1) ;
2265
 
                        } ;
2266
 
 
2267
 
        /* Seek to offset from end of file. */
2268
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
2269
 
 
2270
 
        test_readf_int_or_die (file, 0, test + 20, 2, __LINE__) ;
2271
 
        for (k = 20 ; k < 24 ; k++)
2272
 
                if (INT_ERROR (test [k], orig [k]))
2273
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : 0x%X => 0x%X).\n", __LINE__, k, orig [k], test [k]) ;
2274
 
                        exit (1) ;
2275
 
                        } ;
2276
 
 
2277
 
        sf_close (file) ;
2278
 
} /* stereo_int_test */
2279
 
 
2280
 
static void
2281
 
mono_rdwr_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
2282
 
{       SNDFILE         *file ;
2283
 
        SF_INFO         sfinfo ;
2284
 
        int             *orig, *test ;
2285
 
        int                     k, pass ;
2286
 
 
2287
 
        orig = (int*) orig_data ;
2288
 
        test = (int*) test_data ;
2289
 
 
2290
 
        sfinfo.samplerate       = SAMPLE_RATE ;
2291
 
        sfinfo.frames           = DATA_LENGTH ;
2292
 
        sfinfo.channels         = 1 ;
2293
 
        sfinfo.format           = format ;
2294
 
 
2295
 
        if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
2296
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
2297
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
2298
 
                unlink (filename) ;
2299
 
        else
2300
 
        {       /* Create a short file. */
2301
 
                create_short_file (filename) ;
2302
 
 
2303
 
                /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
2304
 
                ** If this returns a valif pointer sf_open() screwed up.
2305
 
                */
2306
 
                if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
2307
 
                {       printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
2308
 
                        exit (1) ;
2309
 
                        } ;
2310
 
 
2311
 
                /* Truncate the file to zero bytes. */
2312
 
                if (truncate (filename, 0) < 0)
2313
 
                {       printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
2314
 
                        perror (NULL) ;
2315
 
                        exit (1) ;
2316
 
                        } ;
2317
 
                } ;
2318
 
 
2319
 
        /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
2320
 
        ** all the usual data required when opening the file in WRITE mode.
2321
 
        */
2322
 
        sfinfo.samplerate       = SAMPLE_RATE ;
2323
 
        sfinfo.frames           = DATA_LENGTH ;
2324
 
        sfinfo.channels         = 1 ;
2325
 
        sfinfo.format           = format ;
2326
 
 
2327
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
2328
 
 
2329
 
        /* Do 3 writes followed by reads. After each, check the data and the current
2330
 
        ** read and write offsets.
2331
 
        */
2332
 
        for (pass = 1 ; pass <= 3 ; pass ++)
2333
 
        {       orig [20] = pass * 2 ;
2334
 
 
2335
 
                /* Write some data. */
2336
 
                test_write_int_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
2337
 
 
2338
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
2339
 
 
2340
 
                /* Read what we just wrote. */
2341
 
                test_read_int_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
2342
 
 
2343
 
                /* Check the data. */
2344
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
2345
 
                        if (INT_ERROR (orig [k], test [k]))
2346
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
2347
 
                                oct_save_int (orig, test, DATA_LENGTH) ;
2348
 
                                exit (1) ;
2349
 
                                } ;
2350
 
 
2351
 
                test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
2352
 
                } ; /* for (pass ...) */
2353
 
 
2354
 
        sf_close (file) ;
2355
 
 
2356
 
        /* Open the file again to check the data. */
2357
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
2358
 
 
2359
 
        if (sfinfo.format != format)
2360
 
        {       printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
2361
 
                exit (1) ;
2362
 
                } ;
2363
 
 
2364
 
        if (sfinfo.frames < 3 * DATA_LENGTH)
2365
 
        {       printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
2366
 
                exit (1) ;
2367
 
                }
2368
 
 
2369
 
        if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
2370
 
        {       printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
2371
 
                exit (1) ;
2372
 
                } ;
2373
 
 
2374
 
        if (sfinfo.channels != 1)
2375
 
        {       printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
2376
 
                exit (1) ;
2377
 
                } ;
2378
 
 
2379
 
        if (! long_file_ok)
2380
 
                test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
2381
 
        else
2382
 
                test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
2383
 
 
2384
 
        for (pass = 1 ; pass <= 3 ; pass ++)
2385
 
        {       orig [20] = pass * 2 ;
2386
 
 
2387
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
2388
 
 
2389
 
                /* Read what we just wrote. */
2390
 
                test_read_int_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
2391
 
 
2392
 
                /* Check the data. */
2393
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
2394
 
                        if (INT_ERROR (orig [k], test [k]))
2395
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (0x%X => 0x%X).\n", __LINE__, pass, k, orig [k], test [k]) ;
2396
 
                                oct_save_int (orig, test, DATA_LENGTH) ;
2397
 
                                exit (1) ;
2398
 
                                } ;
2399
 
 
2400
 
                } ; /* for (pass ...) */
2401
 
 
2402
 
        sf_close (file) ;
2403
 
} /* mono_rdwr_int_test */
2404
 
 
2405
 
static void
2406
 
new_rdwr_int_test (const char *filename, int format, int allow_fd)
2407
 
{       SNDFILE *wfile, *rwfile ;
2408
 
        SF_INFO sfinfo ;
2409
 
        int             *orig, *test ;
2410
 
        int             items, frames ;
2411
 
 
2412
 
        orig = (int*) orig_data ;
2413
 
        test = (int*) test_data ;
2414
 
 
2415
 
        sfinfo.samplerate       = 44100 ;
2416
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
2417
 
        sfinfo.channels         = 2 ;
2418
 
        sfinfo.format           = format ;
2419
 
 
2420
 
        items = DATA_LENGTH ;
2421
 
        frames = items / sfinfo.channels ;
2422
 
 
2423
 
        wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
2424
 
        sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
2425
 
        test_writef_int_or_die (wfile, 1, orig, frames, __LINE__) ;
2426
 
        sf_write_sync (wfile) ;
2427
 
        test_writef_int_or_die (wfile, 2, orig, frames, __LINE__) ;
2428
 
        sf_write_sync (wfile) ;
2429
 
 
2430
 
        rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
2431
 
        if (sfinfo.frames != 2 * frames)
2432
 
        {       printf ("\n\nLine %d : incorrect number of frames in file (%ld shold be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
2433
 
                exit (1) ;
2434
 
                } ;
2435
 
 
2436
 
        test_writef_int_or_die (wfile, 3, orig, frames, __LINE__) ;
2437
 
 
2438
 
        test_readf_int_or_die (rwfile, 1, test, frames, __LINE__) ;
2439
 
        test_readf_int_or_die (rwfile, 2, test, frames, __LINE__) ;
2440
 
 
2441
 
        sf_close (wfile) ;
2442
 
        sf_close (rwfile) ;
2443
 
} /* new_rdwr_int_test */
2444
 
 
2445
 
 
2446
 
/*======================================================================================
2447
 
*/
2448
 
 
2449
 
static void mono_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
2450
 
static void stereo_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
2451
 
static void mono_rdwr_float_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
2452
 
static void new_rdwr_float_test (const char *filename, int format, int allow_fd) ;
2453
 
 
2454
 
static void
2455
 
pcm_test_float (const char *filename, int format, int long_file_ok)
2456
 
{       SF_INFO         sfinfo ;
2457
 
        float           *orig, *test ;
2458
 
        int                     k, items, allow_fd ;
2459
 
 
2460
 
        /* Sd2 files cannot be opened from an existing file descriptor. */
2461
 
        allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
2462
 
 
2463
 
        print_test_name ("pcm_test_float", filename) ;
2464
 
 
2465
 
        sfinfo.samplerate       = 44100 ;
2466
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
2467
 
        sfinfo.channels         = 1 ;
2468
 
        sfinfo.format           = format ;
2469
 
 
2470
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, 1.0) ;
2471
 
 
2472
 
        orig = (float*) orig_data ;
2473
 
        test = (float*) test_data ;
2474
 
 
2475
 
        /* Make this a macro so gdb steps over it in one go. */
2476
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
2477
 
 
2478
 
        items = DATA_LENGTH ;
2479
 
 
2480
 
        /* Some test broken out here. */
2481
 
 
2482
 
        mono_float_test (filename, format, long_file_ok, allow_fd) ;
2483
 
 
2484
 
        /* Sub format DWVW does not allow seeking. */
2485
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
2486
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
2487
 
        {       unlink (filename) ;
2488
 
                printf ("no seek : ok\n") ;
2489
 
                return ;
2490
 
                } ;
2491
 
 
2492
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
2493
 
                mono_rdwr_float_test (filename, format, long_file_ok, allow_fd) ;
2494
 
 
2495
 
        /* If the format doesn't support stereo we're done. */
2496
 
        sfinfo.channels = 2 ;
2497
 
        if (sf_format_check (&sfinfo) == 0)
2498
 
        {       unlink (filename) ;
2499
 
                puts ("no stereo : ok") ;
2500
 
                return ;
2501
 
                } ;
2502
 
 
2503
 
        stereo_float_test (filename, format, long_file_ok, allow_fd) ;
2504
 
 
2505
 
        /* New read/write test. Not sure if this is needed yet. */
2506
 
 
2507
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
2508
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
2509
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
2510
 
                new_rdwr_float_test (filename, format, allow_fd) ;
2511
 
 
2512
 
        delete_file (format, filename) ;
2513
 
 
2514
 
        puts ("ok") ;
2515
 
        return ;
2516
 
} /* pcm_test_float */
2517
 
 
2518
 
static void
2519
 
mono_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
2520
 
{       SNDFILE         *file ;
2521
 
        SF_INFO         sfinfo ;
2522
 
        float           *orig, *test ;
2523
 
        sf_count_t      count ;
2524
 
        int                     k, items ;
2525
 
 
2526
 
        sfinfo.samplerate       = 44100 ;
2527
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
2528
 
        sfinfo.channels         = 1 ;
2529
 
        sfinfo.format           = format ;
2530
 
 
2531
 
        orig = (float*) orig_data ;
2532
 
        test = (float*) test_data ;
2533
 
 
2534
 
        items = DATA_LENGTH ;
2535
 
 
2536
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
2537
 
 
2538
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
2539
 
 
2540
 
        test_write_float_or_die (file, 0, orig, items, __LINE__) ;
2541
 
        sf_write_sync (file) ;
2542
 
        test_write_float_or_die (file, 0, orig, items, __LINE__) ;
2543
 
        sf_write_sync (file) ;
2544
 
 
2545
 
        /* Add non-audio data after the audio. */
2546
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
2547
 
 
2548
 
        sf_close (file) ;
2549
 
 
2550
 
        memset (test, 0, items * sizeof (float)) ;
2551
 
 
2552
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
2553
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
2554
 
 
2555
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
2556
 
 
2557
 
        if (sfinfo.format != format)
2558
 
        {       printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
2559
 
                exit (1) ;
2560
 
                } ;
2561
 
 
2562
 
        if (sfinfo.frames < 2 * items)
2563
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
2564
 
                exit (1) ;
2565
 
                } ;
2566
 
 
2567
 
        if (! long_file_ok && sfinfo.frames > 2 * items)
2568
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
2569
 
                exit (1) ;
2570
 
                } ;
2571
 
 
2572
 
        if (sfinfo.channels != 1)
2573
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
2574
 
                exit (1) ;
2575
 
                } ;
2576
 
 
2577
 
        check_log_buffer_or_die (file, __LINE__) ;
2578
 
 
2579
 
        test_read_float_or_die (file, 0, test, items, __LINE__) ;
2580
 
        for (k = 0 ; k < items ; k++)
2581
 
                if (FLOAT_ERROR (orig [k], test [k]))
2582
 
                {       printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
2583
 
                        exit (1) ;
2584
 
                        } ;
2585
 
 
2586
 
        /* Seek to start of file. */
2587
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
2588
 
 
2589
 
        test_read_float_or_die (file, 0, test, 4, __LINE__) ;
2590
 
        for (k = 0 ; k < 4 ; k++)
2591
 
                if (FLOAT_ERROR (orig [k], test [k]))
2592
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
2593
 
                        exit (1) ;
2594
 
                        } ;
2595
 
 
2596
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
2597
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
2598
 
        {       sf_close (file) ;
2599
 
                unlink (filename) ;
2600
 
                printf ("no seek : ") ;
2601
 
                return ;
2602
 
                } ;
2603
 
 
2604
 
        /* Seek to offset from start of file. */
2605
 
        test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
2606
 
 
2607
 
        test_read_float_or_die (file, 0, test + 10, 4, __LINE__) ;
2608
 
        for (k = 10 ; k < 14 ; k++)
2609
 
                if (FLOAT_ERROR (orig [k], test [k]))
2610
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
2611
 
                        exit (1) ;
2612
 
                        } ;
2613
 
 
2614
 
        /* Seek to offset from current position. */
2615
 
        test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
2616
 
 
2617
 
        test_read_float_or_die (file, 0, test + 20, 4, __LINE__) ;
2618
 
        for (k = 20 ; k < 24 ; k++)
2619
 
                if (FLOAT_ERROR (orig [k], test [k]))
2620
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
2621
 
                        exit (1) ;
2622
 
                        } ;
2623
 
 
2624
 
        /* Seek to offset from end of file. */
2625
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
2626
 
 
2627
 
        test_read_float_or_die (file, 0, test + 10, 4, __LINE__) ;
2628
 
        for (k = 10 ; k < 14 ; k++)
2629
 
                if (FLOAT_ERROR (orig [k], test [k]))
2630
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
2631
 
                        exit (1) ;
2632
 
                        } ;
2633
 
 
2634
 
        /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
2635
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
2636
 
 
2637
 
        count = 0 ;
2638
 
        while (count < sfinfo.frames)
2639
 
                count += sf_read_float (file, test, 311) ;
2640
 
 
2641
 
        /* Check that no error has occurred. */
2642
 
        if (sf_error (file))
2643
 
        {       printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
2644
 
                puts (sf_strerror (file)) ;
2645
 
                exit (1) ;
2646
 
                } ;
2647
 
 
2648
 
        /* Check that we haven't read beyond EOF. */
2649
 
        if (count > sfinfo.frames)
2650
 
        {       printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
2651
 
                exit (1) ;
2652
 
                } ;
2653
 
 
2654
 
        test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
2655
 
 
2656
 
        sf_close (file) ;
2657
 
 
2658
 
} /* mono_float_test */
2659
 
 
2660
 
static void
2661
 
stereo_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
2662
 
{       SNDFILE         *file ;
2663
 
        SF_INFO         sfinfo ;
2664
 
        float           *orig, *test ;
2665
 
        int                     k, items, frames ;
2666
 
 
2667
 
        orig = (float*) orig_data ;
2668
 
        test = (float*) test_data ;
2669
 
 
2670
 
        sfinfo.samplerate       = 44100 ;
2671
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
2672
 
        sfinfo.channels         = 2 ;
2673
 
        sfinfo.format           = format ;
2674
 
 
2675
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, 1.0) ;
2676
 
 
2677
 
        orig = (float*) orig_data ;
2678
 
        test = (float*) test_data ;
2679
 
 
2680
 
        /* Make this a macro so gdb steps over it in one go. */
2681
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
2682
 
 
2683
 
        items = DATA_LENGTH ;
2684
 
        frames = items / sfinfo.channels ;
2685
 
 
2686
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
2687
 
 
2688
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
2689
 
 
2690
 
        test_writef_float_or_die (file, 0, orig, frames, __LINE__) ;
2691
 
 
2692
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
2693
 
 
2694
 
        sf_close (file) ;
2695
 
 
2696
 
        memset (test, 0, items * sizeof (float)) ;
2697
 
 
2698
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
2699
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
2700
 
 
2701
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
2702
 
 
2703
 
        if (sfinfo.format != format)
2704
 
        {       printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
2705
 
                                __LINE__, format, sfinfo.format) ;
2706
 
                exit (1) ;
2707
 
                } ;
2708
 
 
2709
 
        if (sfinfo.frames < frames)
2710
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
2711
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
2712
 
                exit (1) ;
2713
 
                } ;
2714
 
 
2715
 
        if (! long_file_ok && sfinfo.frames > frames)
2716
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
2717
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
2718
 
                exit (1) ;
2719
 
                } ;
2720
 
 
2721
 
        if (sfinfo.channels != 2)
2722
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
2723
 
                exit (1) ;
2724
 
                } ;
2725
 
 
2726
 
        check_log_buffer_or_die (file, __LINE__) ;
2727
 
 
2728
 
        test_readf_float_or_die (file, 0, test, frames, __LINE__) ;
2729
 
        for (k = 0 ; k < items ; k++)
2730
 
                if (FLOAT_ERROR (test [k], orig [k]))
2731
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
2732
 
                        exit (1) ;
2733
 
                        } ;
2734
 
 
2735
 
        /* Seek to start of file. */
2736
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
2737
 
 
2738
 
        test_readf_float_or_die (file, 0, test, 2, __LINE__) ;
2739
 
        for (k = 0 ; k < 4 ; k++)
2740
 
                if (FLOAT_ERROR (test [k], orig [k]))
2741
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
2742
 
                        exit (1) ;
2743
 
                        } ;
2744
 
 
2745
 
        /* Seek to offset from start of file. */
2746
 
        test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
2747
 
 
2748
 
        /* Check for errors here. */
2749
 
        if (sf_error (file))
2750
 
        {       printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
2751
 
                puts (sf_strerror (file)) ;
2752
 
                exit (1) ;
2753
 
                } ;
2754
 
 
2755
 
        if (sf_read_float (file, test, 1) > 0)
2756
 
        {       printf ("Line %d: Should return 0.\n", __LINE__) ;
2757
 
                exit (1) ;
2758
 
                } ;
2759
 
 
2760
 
        if (! sf_error (file))
2761
 
        {       printf ("Line %d: Should return an error.\n", __LINE__) ;
2762
 
                exit (1) ;
2763
 
                } ;
2764
 
        /*-----------------------*/
2765
 
 
2766
 
        test_readf_float_or_die (file, 0, test + 10, 2, __LINE__) ;
2767
 
        for (k = 20 ; k < 24 ; k++)
2768
 
                if (FLOAT_ERROR (test [k], orig [k]))
2769
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
2770
 
                        exit (1) ;
2771
 
                        } ;
2772
 
 
2773
 
        /* Seek to offset from current position. */
2774
 
        test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
2775
 
 
2776
 
        test_readf_float_or_die (file, 0, test + 20, 2, __LINE__) ;
2777
 
        for (k = 40 ; k < 44 ; k++)
2778
 
                if (FLOAT_ERROR (test [k], orig [k]))
2779
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
2780
 
                        exit (1) ;
2781
 
                        } ;
2782
 
 
2783
 
        /* Seek to offset from end of file. */
2784
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
2785
 
 
2786
 
        test_readf_float_or_die (file, 0, test + 20, 2, __LINE__) ;
2787
 
        for (k = 20 ; k < 24 ; k++)
2788
 
                if (FLOAT_ERROR (test [k], orig [k]))
2789
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
2790
 
                        exit (1) ;
2791
 
                        } ;
2792
 
 
2793
 
        sf_close (file) ;
2794
 
} /* stereo_float_test */
2795
 
 
2796
 
static void
2797
 
mono_rdwr_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
2798
 
{       SNDFILE         *file ;
2799
 
        SF_INFO         sfinfo ;
2800
 
        float           *orig, *test ;
2801
 
        int                     k, pass ;
2802
 
 
2803
 
        orig = (float*) orig_data ;
2804
 
        test = (float*) test_data ;
2805
 
 
2806
 
        sfinfo.samplerate       = SAMPLE_RATE ;
2807
 
        sfinfo.frames           = DATA_LENGTH ;
2808
 
        sfinfo.channels         = 1 ;
2809
 
        sfinfo.format           = format ;
2810
 
 
2811
 
        if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
2812
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
2813
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
2814
 
                unlink (filename) ;
2815
 
        else
2816
 
        {       /* Create a short file. */
2817
 
                create_short_file (filename) ;
2818
 
 
2819
 
                /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
2820
 
                ** If this returns a valif pointer sf_open() screwed up.
2821
 
                */
2822
 
                if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
2823
 
                {       printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
2824
 
                        exit (1) ;
2825
 
                        } ;
2826
 
 
2827
 
                /* Truncate the file to zero bytes. */
2828
 
                if (truncate (filename, 0) < 0)
2829
 
                {       printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
2830
 
                        perror (NULL) ;
2831
 
                        exit (1) ;
2832
 
                        } ;
2833
 
                } ;
2834
 
 
2835
 
        /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
2836
 
        ** all the usual data required when opening the file in WRITE mode.
2837
 
        */
2838
 
        sfinfo.samplerate       = SAMPLE_RATE ;
2839
 
        sfinfo.frames           = DATA_LENGTH ;
2840
 
        sfinfo.channels         = 1 ;
2841
 
        sfinfo.format           = format ;
2842
 
 
2843
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
2844
 
 
2845
 
        /* Do 3 writes followed by reads. After each, check the data and the current
2846
 
        ** read and write offsets.
2847
 
        */
2848
 
        for (pass = 1 ; pass <= 3 ; pass ++)
2849
 
        {       orig [20] = pass * 2 ;
2850
 
 
2851
 
                /* Write some data. */
2852
 
                test_write_float_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
2853
 
 
2854
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
2855
 
 
2856
 
                /* Read what we just wrote. */
2857
 
                test_read_float_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
2858
 
 
2859
 
                /* Check the data. */
2860
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
2861
 
                        if (FLOAT_ERROR (orig [k], test [k]))
2862
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
2863
 
                                oct_save_float (orig, test, DATA_LENGTH) ;
2864
 
                                exit (1) ;
2865
 
                                } ;
2866
 
 
2867
 
                test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
2868
 
                } ; /* for (pass ...) */
2869
 
 
2870
 
        sf_close (file) ;
2871
 
 
2872
 
        /* Open the file again to check the data. */
2873
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
2874
 
 
2875
 
        if (sfinfo.format != format)
2876
 
        {       printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
2877
 
                exit (1) ;
2878
 
                } ;
2879
 
 
2880
 
        if (sfinfo.frames < 3 * DATA_LENGTH)
2881
 
        {       printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
2882
 
                exit (1) ;
2883
 
                }
2884
 
 
2885
 
        if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
2886
 
        {       printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
2887
 
                exit (1) ;
2888
 
                } ;
2889
 
 
2890
 
        if (sfinfo.channels != 1)
2891
 
        {       printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
2892
 
                exit (1) ;
2893
 
                } ;
2894
 
 
2895
 
        if (! long_file_ok)
2896
 
                test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
2897
 
        else
2898
 
                test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
2899
 
 
2900
 
        for (pass = 1 ; pass <= 3 ; pass ++)
2901
 
        {       orig [20] = pass * 2 ;
2902
 
 
2903
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
2904
 
 
2905
 
                /* Read what we just wrote. */
2906
 
                test_read_float_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
2907
 
 
2908
 
                /* Check the data. */
2909
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
2910
 
                        if (FLOAT_ERROR (orig [k], test [k]))
2911
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
2912
 
                                oct_save_float (orig, test, DATA_LENGTH) ;
2913
 
                                exit (1) ;
2914
 
                                } ;
2915
 
 
2916
 
                } ; /* for (pass ...) */
2917
 
 
2918
 
        sf_close (file) ;
2919
 
} /* mono_rdwr_float_test */
2920
 
 
2921
 
static void
2922
 
new_rdwr_float_test (const char *filename, int format, int allow_fd)
2923
 
{       SNDFILE *wfile, *rwfile ;
2924
 
        SF_INFO sfinfo ;
2925
 
        float           *orig, *test ;
2926
 
        int             items, frames ;
2927
 
 
2928
 
        orig = (float*) orig_data ;
2929
 
        test = (float*) test_data ;
2930
 
 
2931
 
        sfinfo.samplerate       = 44100 ;
2932
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
2933
 
        sfinfo.channels         = 2 ;
2934
 
        sfinfo.format           = format ;
2935
 
 
2936
 
        items = DATA_LENGTH ;
2937
 
        frames = items / sfinfo.channels ;
2938
 
 
2939
 
        wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
2940
 
        sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
2941
 
        test_writef_float_or_die (wfile, 1, orig, frames, __LINE__) ;
2942
 
        sf_write_sync (wfile) ;
2943
 
        test_writef_float_or_die (wfile, 2, orig, frames, __LINE__) ;
2944
 
        sf_write_sync (wfile) ;
2945
 
 
2946
 
        rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
2947
 
        if (sfinfo.frames != 2 * frames)
2948
 
        {       printf ("\n\nLine %d : incorrect number of frames in file (%ld shold be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
2949
 
                exit (1) ;
2950
 
                } ;
2951
 
 
2952
 
        test_writef_float_or_die (wfile, 3, orig, frames, __LINE__) ;
2953
 
 
2954
 
        test_readf_float_or_die (rwfile, 1, test, frames, __LINE__) ;
2955
 
        test_readf_float_or_die (rwfile, 2, test, frames, __LINE__) ;
2956
 
 
2957
 
        sf_close (wfile) ;
2958
 
        sf_close (rwfile) ;
2959
 
} /* new_rdwr_float_test */
2960
 
 
2961
 
 
2962
 
/*======================================================================================
2963
 
*/
2964
 
 
2965
 
static void mono_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
2966
 
static void stereo_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
2967
 
static void mono_rdwr_double_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
2968
 
static void new_rdwr_double_test (const char *filename, int format, int allow_fd) ;
2969
 
 
2970
 
static void
2971
 
pcm_test_double (const char *filename, int format, int long_file_ok)
2972
 
{       SF_INFO         sfinfo ;
2973
 
        double          *orig, *test ;
2974
 
        int                     k, items, allow_fd ;
2975
 
 
2976
 
        /* Sd2 files cannot be opened from an existing file descriptor. */
2977
 
        allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
2978
 
 
2979
 
        print_test_name ("pcm_test_double", filename) ;
2980
 
 
2981
 
        sfinfo.samplerate       = 44100 ;
2982
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
2983
 
        sfinfo.channels         = 1 ;
2984
 
        sfinfo.format           = format ;
2985
 
 
2986
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, 1.0) ;
2987
 
 
2988
 
        orig = (double*) orig_data ;
2989
 
        test = (double*) test_data ;
2990
 
 
2991
 
        /* Make this a macro so gdb steps over it in one go. */
2992
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
2993
 
 
2994
 
        items = DATA_LENGTH ;
2995
 
 
2996
 
        /* Some test broken out here. */
2997
 
 
2998
 
        mono_double_test (filename, format, long_file_ok, allow_fd) ;
2999
 
 
3000
 
        /* Sub format DWVW does not allow seeking. */
3001
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
3002
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
3003
 
        {       unlink (filename) ;
3004
 
                printf ("no seek : ok\n") ;
3005
 
                return ;
3006
 
                } ;
3007
 
 
3008
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
3009
 
                mono_rdwr_double_test (filename, format, long_file_ok, allow_fd) ;
3010
 
 
3011
 
        /* If the format doesn't support stereo we're done. */
3012
 
        sfinfo.channels = 2 ;
3013
 
        if (sf_format_check (&sfinfo) == 0)
3014
 
        {       unlink (filename) ;
3015
 
                puts ("no stereo : ok") ;
3016
 
                return ;
3017
 
                } ;
3018
 
 
3019
 
        stereo_double_test (filename, format, long_file_ok, allow_fd) ;
3020
 
 
3021
 
        /* New read/write test. Not sure if this is needed yet. */
3022
 
 
3023
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
3024
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
3025
 
                        (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
3026
 
                new_rdwr_double_test (filename, format, allow_fd) ;
3027
 
 
3028
 
        delete_file (format, filename) ;
3029
 
 
3030
 
        puts ("ok") ;
3031
 
        return ;
3032
 
} /* pcm_test_double */
3033
 
 
3034
 
static void
3035
 
mono_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
3036
 
{       SNDFILE         *file ;
3037
 
        SF_INFO         sfinfo ;
3038
 
        double          *orig, *test ;
3039
 
        sf_count_t      count ;
3040
 
        int                     k, items ;
3041
 
 
3042
 
        sfinfo.samplerate       = 44100 ;
3043
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
3044
 
        sfinfo.channels         = 1 ;
3045
 
        sfinfo.format           = format ;
3046
 
 
3047
 
        orig = (double*) orig_data ;
3048
 
        test = (double*) test_data ;
3049
 
 
3050
 
        items = DATA_LENGTH ;
3051
 
 
3052
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
3053
 
 
3054
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
3055
 
 
3056
 
        test_write_double_or_die (file, 0, orig, items, __LINE__) ;
3057
 
        sf_write_sync (file) ;
3058
 
        test_write_double_or_die (file, 0, orig, items, __LINE__) ;
3059
 
        sf_write_sync (file) ;
3060
 
 
3061
 
        /* Add non-audio data after the audio. */
3062
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
3063
 
 
3064
 
        sf_close (file) ;
3065
 
 
3066
 
        memset (test, 0, items * sizeof (double)) ;
3067
 
 
3068
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
3069
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
3070
 
 
3071
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
3072
 
 
3073
 
        if (sfinfo.format != format)
3074
 
        {       printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
3075
 
                exit (1) ;
3076
 
                } ;
3077
 
 
3078
 
        if (sfinfo.frames < 2 * items)
3079
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
3080
 
                exit (1) ;
3081
 
                } ;
3082
 
 
3083
 
        if (! long_file_ok && sfinfo.frames > 2 * items)
3084
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), items) ;
3085
 
                exit (1) ;
3086
 
                } ;
3087
 
 
3088
 
        if (sfinfo.channels != 1)
3089
 
        {       printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
3090
 
                exit (1) ;
3091
 
                } ;
3092
 
 
3093
 
        check_log_buffer_or_die (file, __LINE__) ;
3094
 
 
3095
 
        test_read_double_or_die (file, 0, test, items, __LINE__) ;
3096
 
        for (k = 0 ; k < items ; k++)
3097
 
                if (FLOAT_ERROR (orig [k], test [k]))
3098
 
                {       printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
3099
 
                        exit (1) ;
3100
 
                        } ;
3101
 
 
3102
 
        /* Seek to start of file. */
3103
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
3104
 
 
3105
 
        test_read_double_or_die (file, 0, test, 4, __LINE__) ;
3106
 
        for (k = 0 ; k < 4 ; k++)
3107
 
                if (FLOAT_ERROR (orig [k], test [k]))
3108
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
3109
 
                        exit (1) ;
3110
 
                        } ;
3111
 
 
3112
 
        if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
3113
 
                        (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
3114
 
        {       sf_close (file) ;
3115
 
                unlink (filename) ;
3116
 
                printf ("no seek : ") ;
3117
 
                return ;
3118
 
                } ;
3119
 
 
3120
 
        /* Seek to offset from start of file. */
3121
 
        test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
3122
 
 
3123
 
        test_read_double_or_die (file, 0, test + 10, 4, __LINE__) ;
3124
 
        for (k = 10 ; k < 14 ; k++)
3125
 
                if (FLOAT_ERROR (orig [k], test [k]))
3126
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
3127
 
                        exit (1) ;
3128
 
                        } ;
3129
 
 
3130
 
        /* Seek to offset from current position. */
3131
 
        test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
3132
 
 
3133
 
        test_read_double_or_die (file, 0, test + 20, 4, __LINE__) ;
3134
 
        for (k = 20 ; k < 24 ; k++)
3135
 
                if (FLOAT_ERROR (orig [k], test [k]))
3136
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
3137
 
                        exit (1) ;
3138
 
                        } ;
3139
 
 
3140
 
        /* Seek to offset from end of file. */
3141
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
3142
 
 
3143
 
        test_read_double_or_die (file, 0, test + 10, 4, __LINE__) ;
3144
 
        for (k = 10 ; k < 14 ; k++)
3145
 
                if (FLOAT_ERROR (orig [k], test [k]))
3146
 
                {       printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : %g => %g).\n", __LINE__, k, test [k], orig [k]) ;
3147
 
                        exit (1) ;
3148
 
                        } ;
3149
 
 
3150
 
        /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
3151
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
3152
 
 
3153
 
        count = 0 ;
3154
 
        while (count < sfinfo.frames)
3155
 
                count += sf_read_double (file, test, 311) ;
3156
 
 
3157
 
        /* Check that no error has occurred. */
3158
 
        if (sf_error (file))
3159
 
        {       printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
3160
 
                puts (sf_strerror (file)) ;
3161
 
                exit (1) ;
3162
 
                } ;
3163
 
 
3164
 
        /* Check that we haven't read beyond EOF. */
3165
 
        if (count > sfinfo.frames)
3166
 
        {       printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
3167
 
                exit (1) ;
3168
 
                } ;
3169
 
 
3170
 
        test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
3171
 
 
3172
 
        sf_close (file) ;
3173
 
 
3174
 
} /* mono_double_test */
3175
 
 
3176
 
static void
3177
 
stereo_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
3178
 
{       SNDFILE         *file ;
3179
 
        SF_INFO         sfinfo ;
3180
 
        double          *orig, *test ;
3181
 
        int                     k, items, frames ;
3182
 
 
3183
 
        orig = (double*) orig_data ;
3184
 
        test = (double*) test_data ;
3185
 
 
3186
 
        sfinfo.samplerate       = 44100 ;
3187
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
3188
 
        sfinfo.channels         = 2 ;
3189
 
        sfinfo.format           = format ;
3190
 
 
3191
 
        gen_windowed_sine_double (orig_data, DATA_LENGTH, 1.0) ;
3192
 
 
3193
 
        orig = (double*) orig_data ;
3194
 
        test = (double*) test_data ;
3195
 
 
3196
 
        /* Make this a macro so gdb steps over it in one go. */
3197
 
        CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
3198
 
 
3199
 
        items = DATA_LENGTH ;
3200
 
        frames = items / sfinfo.channels ;
3201
 
 
3202
 
        file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
3203
 
 
3204
 
        sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
3205
 
 
3206
 
        test_writef_double_or_die (file, 0, orig, frames, __LINE__) ;
3207
 
 
3208
 
        sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
3209
 
 
3210
 
        sf_close (file) ;
3211
 
 
3212
 
        memset (test, 0, items * sizeof (double)) ;
3213
 
 
3214
 
        if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
3215
 
                memset (&sfinfo, 0, sizeof (sfinfo)) ;
3216
 
 
3217
 
        file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
3218
 
 
3219
 
        if (sfinfo.format != format)
3220
 
        {       printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
3221
 
                                __LINE__, format, sfinfo.format) ;
3222
 
                exit (1) ;
3223
 
                } ;
3224
 
 
3225
 
        if (sfinfo.frames < frames)
3226
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
3227
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
3228
 
                exit (1) ;
3229
 
                } ;
3230
 
 
3231
 
        if (! long_file_ok && sfinfo.frames > frames)
3232
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
3233
 
                                __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
3234
 
                exit (1) ;
3235
 
                } ;
3236
 
 
3237
 
        if (sfinfo.channels != 2)
3238
 
        {       printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
3239
 
                exit (1) ;
3240
 
                } ;
3241
 
 
3242
 
        check_log_buffer_or_die (file, __LINE__) ;
3243
 
 
3244
 
        test_readf_double_or_die (file, 0, test, frames, __LINE__) ;
3245
 
        for (k = 0 ; k < items ; k++)
3246
 
                if (FLOAT_ERROR (test [k], orig [k]))
3247
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
3248
 
                        exit (1) ;
3249
 
                        } ;
3250
 
 
3251
 
        /* Seek to start of file. */
3252
 
        test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
3253
 
 
3254
 
        test_readf_double_or_die (file, 0, test, 2, __LINE__) ;
3255
 
        for (k = 0 ; k < 4 ; k++)
3256
 
                if (FLOAT_ERROR (test [k], orig [k]))
3257
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
3258
 
                        exit (1) ;
3259
 
                        } ;
3260
 
 
3261
 
        /* Seek to offset from start of file. */
3262
 
        test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
3263
 
 
3264
 
        /* Check for errors here. */
3265
 
        if (sf_error (file))
3266
 
        {       printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
3267
 
                puts (sf_strerror (file)) ;
3268
 
                exit (1) ;
3269
 
                } ;
3270
 
 
3271
 
        if (sf_read_double (file, test, 1) > 0)
3272
 
        {       printf ("Line %d: Should return 0.\n", __LINE__) ;
3273
 
                exit (1) ;
3274
 
                } ;
3275
 
 
3276
 
        if (! sf_error (file))
3277
 
        {       printf ("Line %d: Should return an error.\n", __LINE__) ;
3278
 
                exit (1) ;
3279
 
                } ;
3280
 
        /*-----------------------*/
3281
 
 
3282
 
        test_readf_double_or_die (file, 0, test + 10, 2, __LINE__) ;
3283
 
        for (k = 20 ; k < 24 ; k++)
3284
 
                if (FLOAT_ERROR (test [k], orig [k]))
3285
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
3286
 
                        exit (1) ;
3287
 
                        } ;
3288
 
 
3289
 
        /* Seek to offset from current position. */
3290
 
        test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
3291
 
 
3292
 
        test_readf_double_or_die (file, 0, test + 20, 2, __LINE__) ;
3293
 
        for (k = 40 ; k < 44 ; k++)
3294
 
                if (FLOAT_ERROR (test [k], orig [k]))
3295
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
3296
 
                        exit (1) ;
3297
 
                        } ;
3298
 
 
3299
 
        /* Seek to offset from end of file. */
3300
 
        test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
3301
 
 
3302
 
        test_readf_double_or_die (file, 0, test + 20, 2, __LINE__) ;
3303
 
        for (k = 20 ; k < 24 ; k++)
3304
 
                if (FLOAT_ERROR (test [k], orig [k]))
3305
 
                {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : %g => %g).\n", __LINE__, k, orig [k], test [k]) ;
3306
 
                        exit (1) ;
3307
 
                        } ;
3308
 
 
3309
 
        sf_close (file) ;
3310
 
} /* stereo_double_test */
3311
 
 
3312
 
static void
3313
 
mono_rdwr_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
3314
 
{       SNDFILE         *file ;
3315
 
        SF_INFO         sfinfo ;
3316
 
        double          *orig, *test ;
3317
 
        int                     k, pass ;
3318
 
 
3319
 
        orig = (double*) orig_data ;
3320
 
        test = (double*) test_data ;
3321
 
 
3322
 
        sfinfo.samplerate       = SAMPLE_RATE ;
3323
 
        sfinfo.frames           = DATA_LENGTH ;
3324
 
        sfinfo.channels         = 1 ;
3325
 
        sfinfo.format           = format ;
3326
 
 
3327
 
        if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
3328
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
3329
 
                || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
3330
 
                unlink (filename) ;
3331
 
        else
3332
 
        {       /* Create a short file. */
3333
 
                create_short_file (filename) ;
3334
 
 
3335
 
                /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
3336
 
                ** If this returns a valif pointer sf_open() screwed up.
3337
 
                */
3338
 
                if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
3339
 
                {       printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
3340
 
                        exit (1) ;
3341
 
                        } ;
3342
 
 
3343
 
                /* Truncate the file to zero bytes. */
3344
 
                if (truncate (filename, 0) < 0)
3345
 
                {       printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
3346
 
                        perror (NULL) ;
3347
 
                        exit (1) ;
3348
 
                        } ;
3349
 
                } ;
3350
 
 
3351
 
        /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
3352
 
        ** all the usual data required when opening the file in WRITE mode.
3353
 
        */
3354
 
        sfinfo.samplerate       = SAMPLE_RATE ;
3355
 
        sfinfo.frames           = DATA_LENGTH ;
3356
 
        sfinfo.channels         = 1 ;
3357
 
        sfinfo.format           = format ;
3358
 
 
3359
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
3360
 
 
3361
 
        /* Do 3 writes followed by reads. After each, check the data and the current
3362
 
        ** read and write offsets.
3363
 
        */
3364
 
        for (pass = 1 ; pass <= 3 ; pass ++)
3365
 
        {       orig [20] = pass * 2 ;
3366
 
 
3367
 
                /* Write some data. */
3368
 
                test_write_double_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
3369
 
 
3370
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
3371
 
 
3372
 
                /* Read what we just wrote. */
3373
 
                test_read_double_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
3374
 
 
3375
 
                /* Check the data. */
3376
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
3377
 
                        if (FLOAT_ERROR (orig [k], test [k]))
3378
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
3379
 
                                oct_save_double (orig, test, DATA_LENGTH) ;
3380
 
                                exit (1) ;
3381
 
                                } ;
3382
 
 
3383
 
                test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
3384
 
                } ; /* for (pass ...) */
3385
 
 
3386
 
        sf_close (file) ;
3387
 
 
3388
 
        /* Open the file again to check the data. */
3389
 
        file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
3390
 
 
3391
 
        if (sfinfo.format != format)
3392
 
        {       printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
3393
 
                exit (1) ;
3394
 
                } ;
3395
 
 
3396
 
        if (sfinfo.frames < 3 * DATA_LENGTH)
3397
 
        {       printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
3398
 
                exit (1) ;
3399
 
                }
3400
 
 
3401
 
        if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
3402
 
        {       printf ("\n\nLine %d : Incorrect number of frames in file. (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
3403
 
                exit (1) ;
3404
 
                } ;
3405
 
 
3406
 
        if (sfinfo.channels != 1)
3407
 
        {       printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
3408
 
                exit (1) ;
3409
 
                } ;
3410
 
 
3411
 
        if (! long_file_ok)
3412
 
                test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
3413
 
        else
3414
 
                test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
3415
 
 
3416
 
        for (pass = 1 ; pass <= 3 ; pass ++)
3417
 
        {       orig [20] = pass * 2 ;
3418
 
 
3419
 
                test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
3420
 
 
3421
 
                /* Read what we just wrote. */
3422
 
                test_read_double_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
3423
 
 
3424
 
                /* Check the data. */
3425
 
                for (k = 0 ; k < DATA_LENGTH ; k++)
3426
 
                        if (FLOAT_ERROR (orig [k], test [k]))
3427
 
                        {       printf ("\n\nLine %d (pass %d): Error at sample %d (%g => %g).\n", __LINE__, pass, k, orig [k], test [k]) ;
3428
 
                                oct_save_double (orig, test, DATA_LENGTH) ;
3429
 
                                exit (1) ;
3430
 
                                } ;
3431
 
 
3432
 
                } ; /* for (pass ...) */
3433
 
 
3434
 
        sf_close (file) ;
3435
 
} /* mono_rdwr_double_test */
3436
 
 
3437
 
static void
3438
 
new_rdwr_double_test (const char *filename, int format, int allow_fd)
3439
 
{       SNDFILE *wfile, *rwfile ;
3440
 
        SF_INFO sfinfo ;
3441
 
        double          *orig, *test ;
3442
 
        int             items, frames ;
3443
 
 
3444
 
        orig = (double*) orig_data ;
3445
 
        test = (double*) test_data ;
3446
 
 
3447
 
        sfinfo.samplerate       = 44100 ;
3448
 
        sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
3449
 
        sfinfo.channels         = 2 ;
3450
 
        sfinfo.format           = format ;
3451
 
 
3452
 
        items = DATA_LENGTH ;
3453
 
        frames = items / sfinfo.channels ;
3454
 
 
3455
 
        wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
3456
 
        sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
3457
 
        test_writef_double_or_die (wfile, 1, orig, frames, __LINE__) ;
3458
 
        sf_write_sync (wfile) ;
3459
 
        test_writef_double_or_die (wfile, 2, orig, frames, __LINE__) ;
3460
 
        sf_write_sync (wfile) ;
3461
 
 
3462
 
        rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
3463
 
        if (sfinfo.frames != 2 * frames)
3464
 
        {       printf ("\n\nLine %d : incorrect number of frames in file (%ld shold be %d)\n\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
3465
 
                exit (1) ;
3466
 
                } ;
3467
 
 
3468
 
        test_writef_double_or_die (wfile, 3, orig, frames, __LINE__) ;
3469
 
 
3470
 
        test_readf_double_or_die (rwfile, 1, test, frames, __LINE__) ;
3471
 
        test_readf_double_or_die (rwfile, 2, test, frames, __LINE__) ;
3472
 
 
3473
 
        sf_close (wfile) ;
3474
 
        sf_close (rwfile) ;
3475
 
} /* new_rdwr_double_test */
3476
 
 
3477
 
 
3478
 
 
3479
 
/*----------------------------------------------------------------------------------------
3480
 
*/
3481
 
 
3482
 
static void
3483
 
empty_file_test (const char *filename, int format)
3484
 
{       SNDFILE         *file ;
3485
 
        SF_INFO info ;
3486
 
        int allow_fd ;
3487
 
 
3488
 
        /* Sd2 files cannot be opened from an existing file descriptor. */
3489
 
        allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
3490
 
 
3491
 
        print_test_name ("empty_file_test", filename) ;
3492
 
 
3493
 
        unlink (filename) ;
3494
 
 
3495
 
        info.samplerate = 48000 ;
3496
 
        info.channels = 2 ;
3497
 
        info.format = format ;
3498
 
 
3499
 
        if (sf_format_check (&info) == SF_FALSE)
3500
 
        {       info.channels = 1 ;
3501
 
                if (sf_format_check (&info) == SF_FALSE)
3502
 
                {       puts ("invalid file format") ;
3503
 
                        return ;
3504
 
                        } ;
3505
 
                } ;
3506
 
 
3507
 
        /* Create an empty file. */
3508
 
        file = test_open_file_or_die (filename, SFM_WRITE, &info, allow_fd, __LINE__) ;
3509
 
        sf_close (file) ;
3510
 
 
3511
 
        /* Open for read and check the length. */
3512
 
        file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
3513
 
 
3514
 
        if (SF_COUNT_TO_LONG (info.frames) != 0)
3515
 
        {       printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
3516
 
                        exit (1) ;
3517
 
                        } ;
3518
 
 
3519
 
        sf_close (file) ;
3520
 
 
3521
 
        /* Open for read/write and check the length. */
3522
 
        file = test_open_file_or_die (filename, SFM_RDWR, &info, allow_fd, __LINE__) ;
3523
 
 
3524
 
        if (SF_COUNT_TO_LONG (info.frames) != 0)
3525
 
        {       printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
3526
 
                exit (1) ;
3527
 
                } ;
3528
 
 
3529
 
        sf_close (file) ;
3530
 
 
3531
 
        /* Open for read and check the length. */
3532
 
        file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
3533
 
 
3534
 
        if (SF_COUNT_TO_LONG (info.frames) != 0)
3535
 
        {       printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
3536
 
                exit (1) ;
3537
 
                } ;
3538
 
 
3539
 
        sf_close (file) ;
3540
 
 
3541
 
        check_open_file_count_or_die (__LINE__) ;
3542
 
 
3543
 
        unlink (filename) ;
3544
 
        puts ("ok") ;
3545
 
 
3546
 
        return ;
3547
 
} /* empty_file_test */
3548
 
 
3549
 
 
3550
 
/*----------------------------------------------------------------------------------------
3551
 
*/
3552
 
 
3553
 
static void
3554
 
create_short_file (const char *filename)
3555
 
{       FILE *file ;
3556
 
 
3557
 
        if (! (file = fopen (filename, "w")))
3558
 
        {       printf ("create_short_file : fopen (%s, \"w\") failed.", filename) ;
3559
 
                fflush (stdout) ;
3560
 
                perror (NULL) ;
3561
 
                exit (1) ;
3562
 
                } ;
3563
 
 
3564
 
        fprintf (file, "This is the file data.\n") ;
3565
 
 
3566
 
        fclose (file) ;
3567
 
} /* create_short_file */
3568
 
 
3569
 
#if (defined (WIN32) || defined (__WIN32))
3570
 
 
3571
 
/* Win32 does not have truncate (nor does it have the POSIX function ftruncate).
3572
 
** Hack somethng up here to over come this. This function can only truncate to a
3573
 
** length of zero.
3574
 
*/
3575
 
 
3576
 
static int
3577
 
truncate (const char *filename, int ignored)
3578
 
{       int fd ;
3579
 
 
3580
 
        ignored = 0 ;
3581
 
 
3582
 
        if ((fd = open (filename, O_RDWR | O_TRUNC | O_BINARY)) < 0)
3583
 
                return 0 ;
3584
 
 
3585
 
        close (fd) ;
3586
 
 
3587
 
        return 0 ;
3588
 
} /* truncate */
3589
 
 
3590
 
#endif
3591
 
 
3592