2
** Copyright (C) 1999-2005 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
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.
31
#if (defined (WIN32) || defined (_WIN32))
33
static int truncate (const char *filename, int ignored) ;
40
#define SAMPLE_RATE 11025
41
#define DATA_LENGTH (1<<12)
43
#define SILLY_WRITE_COUNT (234)
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) ;
52
static void empty_file_test (const char *filename, int format) ;
54
static double orig_data [DATA_LENGTH] ;
55
static double test_data [DATA_LENGTH] ;
58
main (int argc, char **argv)
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") ;
84
do_all = !strcmp (argv [1], "all") ;
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) ;
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) ;
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) ;
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) ;
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) ;
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) ;
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 */
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) ;
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) ;
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) ;
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) ;
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) ;
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 */
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 */
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 */
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 */
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 */
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) ;
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) ;
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) ;
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 */
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) ;
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) ;
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) ;
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) ;
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) ;
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) ;
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) ;
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) ;
270
if (do_all || ! strcmp (argv [1], "mat5"))
271
{ increment_open_file_count () ;
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) ;
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) ;
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) ;
297
if (do_all || ! strcmp (argv [1], "htk"))
298
{ pcm_test_short ("short.htk", SF_FORMAT_HTK | SF_FORMAT_PCM_16, SF_FALSE) ;
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) ;
308
/* Lite remove end */
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) ;
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 */
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) ;
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) ;
340
if (do_all || ! strcmp (argv [1], "flac"))
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) ;
347
printf (" **** flac not supported in this binary. ****\n") ;
353
{ printf ("Mono : ************************************\n") ;
354
printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
355
printf ("Mono : ************************************\n") ;
359
/* Only open file descriptors should be stdin, stdout and stderr. */
360
check_open_file_count_or_die (__LINE__) ;
365
/*============================================================================================
366
** Helper functions and macros.
369
static void create_short_file (const char *filename) ;
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)
376
#define CONVERT_DATA(k,len,new,orig) \
377
{ for ((k) = 0 ; (k) < (len) ; (k) ++) \
378
(new) [k] = (orig) [k] ; \
382
/*======================================================================================
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) ;
391
pcm_test_char (const char *filename, int format, int long_file_ok)
394
int k, items, allow_fd ;
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 ;
399
print_test_name ("pcm_test_char", filename) ;
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 ;
406
gen_windowed_sine_double (orig_data, DATA_LENGTH, 32000.0) ;
408
orig = (short*) orig_data ;
409
test = (short*) test_data ;
411
/* Make this a macro so gdb steps over it in one go. */
412
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
414
items = DATA_LENGTH ;
416
/* Some test broken out here. */
418
mono_char_test (filename, format, long_file_ok, allow_fd) ;
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") ;
428
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
429
mono_rdwr_char_test (filename, format, long_file_ok, allow_fd) ;
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") ;
439
stereo_char_test (filename, format, long_file_ok, allow_fd) ;
441
/* New read/write test. Not sure if this is needed yet. */
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) ;
448
delete_file (format, filename) ;
452
} /* pcm_test_char */
455
mono_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
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 ;
467
orig = (short*) orig_data ;
468
test = (short*) test_data ;
470
items = DATA_LENGTH ;
472
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
474
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
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) ;
481
/* Add non-audio data after the audio. */
482
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
486
memset (test, 0, items * sizeof (short)) ;
488
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
489
memset (&sfinfo, 0, sizeof (sfinfo)) ;
491
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
493
if (sfinfo.format != format)
494
{ printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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) ;
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) ;
508
if (sfinfo.channels != 1)
509
{ printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
513
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
522
/* Seek to start of file. */
523
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
532
if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
533
(format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
536
printf ("no seek : ") ;
540
/* Seek to offset from start of file. */
541
test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
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]) ;
550
/* Seek to offset from current position. */
551
test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
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]) ;
560
/* Seek to offset from end of file. */
561
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
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__) ;
574
while (count < sfinfo.frames)
575
count += sf_read_short (file, test, 311) ;
577
/* Check that no error has occurred. */
579
{ printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
580
puts (sf_strerror (file)) ;
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) ;
590
test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
594
} /* mono_char_test */
597
stereo_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
601
int k, items, frames ;
603
orig = (short*) orig_data ;
604
test = (short*) test_data ;
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 ;
611
gen_windowed_sine_double (orig_data, DATA_LENGTH, 32000.0) ;
613
orig = (short*) orig_data ;
614
test = (short*) test_data ;
616
/* Make this a macro so gdb steps over it in one go. */
617
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
619
items = DATA_LENGTH ;
620
frames = items / sfinfo.channels ;
622
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
624
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
626
test_writef_short_or_die (file, 0, orig, frames, __LINE__) ;
628
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
632
memset (test, 0, items * sizeof (short)) ;
634
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
635
memset (&sfinfo, 0, sizeof (sfinfo)) ;
637
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
639
if (sfinfo.format != format)
640
{ printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
641
__LINE__, format, sfinfo.format) ;
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) ;
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) ;
657
if (sfinfo.channels != 2)
658
{ printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
662
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
671
/* Seek to start of file. */
672
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
681
/* Seek to offset from start of file. */
682
test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
684
/* Check for errors here. */
686
{ printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
687
puts (sf_strerror (file)) ;
691
if (sf_read_short (file, test, 1) > 0)
692
{ printf ("Line %d: Should return 0.\n", __LINE__) ;
696
if (! sf_error (file))
697
{ printf ("Line %d: Should return an error.\n", __LINE__) ;
700
/*-----------------------*/
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]) ;
709
/* Seek to offset from current position. */
710
test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
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]) ;
719
/* Seek to offset from end of file. */
720
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
730
} /* stereo_char_test */
733
mono_rdwr_char_test (const char *filename, int format, int long_file_ok, int allow_fd)
739
orig = (short*) orig_data ;
740
test = (short*) test_data ;
742
sfinfo.samplerate = SAMPLE_RATE ;
743
sfinfo.frames = DATA_LENGTH ;
744
sfinfo.channels = 1 ;
745
sfinfo.format = format ;
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)
752
{ /* Create a short file. */
753
create_short_file (filename) ;
755
/* Opening a already existing short file (ie invalid header) RDWR is disallowed.
756
** If this returns a valif pointer sf_open() screwed up.
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__) ;
763
/* Truncate the file to zero bytes. */
764
if (truncate (filename, 0) < 0)
765
{ printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
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.
774
sfinfo.samplerate = SAMPLE_RATE ;
775
sfinfo.frames = DATA_LENGTH ;
776
sfinfo.channels = 1 ;
777
sfinfo.format = format ;
779
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
781
/* Do 3 writes followed by reads. After each, check the data and the current
782
** read and write offsets.
784
for (pass = 1 ; pass <= 3 ; pass ++)
785
{ orig [20] = pass * 2 ;
787
/* Write some data. */
788
test_write_short_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
790
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
792
/* Read what we just wrote. */
793
test_read_short_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
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) ;
803
test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
804
} ; /* for (pass ...) */
808
/* Open the file again to check the data. */
809
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
811
if (sfinfo.format != format)
812
{ printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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 ) ;
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 ) ;
826
if (sfinfo.channels != 1)
827
{ printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
832
test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
834
test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
836
for (pass = 1 ; pass <= 3 ; pass ++)
837
{ orig [20] = pass * 2 ;
839
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
841
/* Read what we just wrote. */
842
test_read_short_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
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) ;
852
} ; /* for (pass ...) */
855
} /* mono_rdwr_short_test */
858
new_rdwr_char_test (const char *filename, int format, int allow_fd)
859
{ SNDFILE *wfile, *rwfile ;
864
orig = (short*) orig_data ;
865
test = (short*) test_data ;
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 ;
872
items = DATA_LENGTH ;
873
frames = items / sfinfo.channels ;
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) ;
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) ;
888
test_writef_short_or_die (wfile, 3, orig, frames, __LINE__) ;
890
test_readf_short_or_die (rwfile, 1, test, frames, __LINE__) ;
891
test_readf_short_or_die (rwfile, 2, test, frames, __LINE__) ;
895
} /* new_rdwr_char_test */
898
/*======================================================================================
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) ;
907
pcm_test_short (const char *filename, int format, int long_file_ok)
910
int k, items, allow_fd ;
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 ;
915
print_test_name ("pcm_test_short", filename) ;
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 ;
922
gen_windowed_sine_double (orig_data, DATA_LENGTH, 32000.0) ;
924
orig = (short*) orig_data ;
925
test = (short*) test_data ;
927
/* Make this a macro so gdb steps over it in one go. */
928
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
930
items = DATA_LENGTH ;
932
/* Some test broken out here. */
934
mono_short_test (filename, format, long_file_ok, allow_fd) ;
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") ;
944
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
945
mono_rdwr_short_test (filename, format, long_file_ok, allow_fd) ;
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") ;
955
stereo_short_test (filename, format, long_file_ok, allow_fd) ;
957
/* New read/write test. Not sure if this is needed yet. */
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) ;
964
delete_file (format, filename) ;
968
} /* pcm_test_short */
971
mono_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
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 ;
983
orig = (short*) orig_data ;
984
test = (short*) test_data ;
986
items = DATA_LENGTH ;
988
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
990
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
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) ;
997
/* Add non-audio data after the audio. */
998
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
1002
memset (test, 0, items * sizeof (short)) ;
1004
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
1005
memset (&sfinfo, 0, sizeof (sfinfo)) ;
1007
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
1009
if (sfinfo.format != format)
1010
{ printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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) ;
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) ;
1024
if (sfinfo.channels != 1)
1025
{ printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
1029
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
1038
/* Seek to start of file. */
1039
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
1048
if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
1049
(format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
1052
printf ("no seek : ") ;
1056
/* Seek to offset from start of file. */
1057
test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
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]) ;
1066
/* Seek to offset from current position. */
1067
test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
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]) ;
1076
/* Seek to offset from end of file. */
1077
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
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__) ;
1090
while (count < sfinfo.frames)
1091
count += sf_read_short (file, test, 311) ;
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)) ;
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) ;
1106
test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
1110
} /* mono_short_test */
1113
stereo_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
1116
short *orig, *test ;
1117
int k, items, frames ;
1119
orig = (short*) orig_data ;
1120
test = (short*) test_data ;
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 ;
1127
gen_windowed_sine_double (orig_data, DATA_LENGTH, 32000.0) ;
1129
orig = (short*) orig_data ;
1130
test = (short*) test_data ;
1132
/* Make this a macro so gdb steps over it in one go. */
1133
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
1135
items = DATA_LENGTH ;
1136
frames = items / sfinfo.channels ;
1138
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
1140
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
1142
test_writef_short_or_die (file, 0, orig, frames, __LINE__) ;
1144
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
1148
memset (test, 0, items * sizeof (short)) ;
1150
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
1151
memset (&sfinfo, 0, sizeof (sfinfo)) ;
1153
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
1155
if (sfinfo.format != format)
1156
{ printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
1157
__LINE__, format, sfinfo.format) ;
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) ;
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) ;
1173
if (sfinfo.channels != 2)
1174
{ printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
1178
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
1187
/* Seek to start of file. */
1188
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
1197
/* Seek to offset from start of file. */
1198
test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
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)) ;
1207
if (sf_read_short (file, test, 1) > 0)
1208
{ printf ("Line %d: Should return 0.\n", __LINE__) ;
1212
if (! sf_error (file))
1213
{ printf ("Line %d: Should return an error.\n", __LINE__) ;
1216
/*-----------------------*/
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]) ;
1225
/* Seek to offset from current position. */
1226
test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
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]) ;
1235
/* Seek to offset from end of file. */
1236
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
1246
} /* stereo_short_test */
1249
mono_rdwr_short_test (const char *filename, int format, int long_file_ok, int allow_fd)
1252
short *orig, *test ;
1255
orig = (short*) orig_data ;
1256
test = (short*) test_data ;
1258
sfinfo.samplerate = SAMPLE_RATE ;
1259
sfinfo.frames = DATA_LENGTH ;
1260
sfinfo.channels = 1 ;
1261
sfinfo.format = format ;
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)
1268
{ /* Create a short file. */
1269
create_short_file (filename) ;
1271
/* Opening a already existing short file (ie invalid header) RDWR is disallowed.
1272
** If this returns a valif pointer sf_open() screwed up.
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__) ;
1279
/* Truncate the file to zero bytes. */
1280
if (truncate (filename, 0) < 0)
1281
{ printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
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.
1290
sfinfo.samplerate = SAMPLE_RATE ;
1291
sfinfo.frames = DATA_LENGTH ;
1292
sfinfo.channels = 1 ;
1293
sfinfo.format = format ;
1295
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
1297
/* Do 3 writes followed by reads. After each, check the data and the current
1298
** read and write offsets.
1300
for (pass = 1 ; pass <= 3 ; pass ++)
1301
{ orig [20] = pass * 2 ;
1303
/* Write some data. */
1304
test_write_short_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
1306
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
1308
/* Read what we just wrote. */
1309
test_read_short_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
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) ;
1319
test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
1320
} ; /* for (pass ...) */
1324
/* Open the file again to check the data. */
1325
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
1327
if (sfinfo.format != format)
1328
{ printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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 ) ;
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 ) ;
1342
if (sfinfo.channels != 1)
1343
{ printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
1348
test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
1350
test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
1352
for (pass = 1 ; pass <= 3 ; pass ++)
1353
{ orig [20] = pass * 2 ;
1355
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
1357
/* Read what we just wrote. */
1358
test_read_short_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
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) ;
1368
} ; /* for (pass ...) */
1371
} /* mono_rdwr_short_test */
1374
new_rdwr_short_test (const char *filename, int format, int allow_fd)
1375
{ SNDFILE *wfile, *rwfile ;
1377
short *orig, *test ;
1380
orig = (short*) orig_data ;
1381
test = (short*) test_data ;
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 ;
1388
items = DATA_LENGTH ;
1389
frames = items / sfinfo.channels ;
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) ;
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) ;
1404
test_writef_short_or_die (wfile, 3, orig, frames, __LINE__) ;
1406
test_readf_short_or_die (rwfile, 1, test, frames, __LINE__) ;
1407
test_readf_short_or_die (rwfile, 2, test, frames, __LINE__) ;
1411
} /* new_rdwr_short_test */
1414
/*======================================================================================
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) ;
1423
pcm_test_24bit (const char *filename, int format, int long_file_ok)
1426
int k, items, allow_fd ;
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 ;
1431
print_test_name ("pcm_test_24bit", filename) ;
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 ;
1438
gen_windowed_sine_double (orig_data, DATA_LENGTH, (1.0 * 0x7F000000)) ;
1440
orig = (int*) orig_data ;
1441
test = (int*) test_data ;
1443
/* Make this a macro so gdb steps over it in one go. */
1444
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
1446
items = DATA_LENGTH ;
1448
/* Some test broken out here. */
1450
mono_24bit_test (filename, format, long_file_ok, allow_fd) ;
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") ;
1460
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
1461
mono_rdwr_24bit_test (filename, format, long_file_ok, allow_fd) ;
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") ;
1471
stereo_24bit_test (filename, format, long_file_ok, allow_fd) ;
1473
/* New read/write test. Not sure if this is needed yet. */
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) ;
1480
delete_file (format, filename) ;
1484
} /* pcm_test_24bit */
1487
mono_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
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 ;
1499
orig = (int*) orig_data ;
1500
test = (int*) test_data ;
1502
items = DATA_LENGTH ;
1504
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
1506
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
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) ;
1513
/* Add non-audio data after the audio. */
1514
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
1518
memset (test, 0, items * sizeof (int)) ;
1520
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
1521
memset (&sfinfo, 0, sizeof (sfinfo)) ;
1523
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
1525
if (sfinfo.format != format)
1526
{ printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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) ;
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) ;
1540
if (sfinfo.channels != 1)
1541
{ printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
1545
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
1554
/* Seek to start of file. */
1555
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
1564
if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
1565
(format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
1568
printf ("no seek : ") ;
1572
/* Seek to offset from start of file. */
1573
test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
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]) ;
1582
/* Seek to offset from current position. */
1583
test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
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]) ;
1592
/* Seek to offset from end of file. */
1593
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
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__) ;
1606
while (count < sfinfo.frames)
1607
count += sf_read_int (file, test, 311) ;
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)) ;
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) ;
1622
test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
1626
} /* mono_24bit_test */
1629
stereo_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
1633
int k, items, frames ;
1635
orig = (int*) orig_data ;
1636
test = (int*) test_data ;
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 ;
1643
gen_windowed_sine_double (orig_data, DATA_LENGTH, (1.0 * 0x7F000000)) ;
1645
orig = (int*) orig_data ;
1646
test = (int*) test_data ;
1648
/* Make this a macro so gdb steps over it in one go. */
1649
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
1651
items = DATA_LENGTH ;
1652
frames = items / sfinfo.channels ;
1654
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
1656
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
1658
test_writef_int_or_die (file, 0, orig, frames, __LINE__) ;
1660
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
1664
memset (test, 0, items * sizeof (int)) ;
1666
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
1667
memset (&sfinfo, 0, sizeof (sfinfo)) ;
1669
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
1671
if (sfinfo.format != format)
1672
{ printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
1673
__LINE__, format, sfinfo.format) ;
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) ;
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) ;
1689
if (sfinfo.channels != 2)
1690
{ printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
1694
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
1703
/* Seek to start of file. */
1704
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
1713
/* Seek to offset from start of file. */
1714
test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
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)) ;
1723
if (sf_read_int (file, test, 1) > 0)
1724
{ printf ("Line %d: Should return 0.\n", __LINE__) ;
1728
if (! sf_error (file))
1729
{ printf ("Line %d: Should return an error.\n", __LINE__) ;
1732
/*-----------------------*/
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]) ;
1741
/* Seek to offset from current position. */
1742
test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
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]) ;
1751
/* Seek to offset from end of file. */
1752
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
1762
} /* stereo_24bit_test */
1765
mono_rdwr_24bit_test (const char *filename, int format, int long_file_ok, int allow_fd)
1771
orig = (int*) orig_data ;
1772
test = (int*) test_data ;
1774
sfinfo.samplerate = SAMPLE_RATE ;
1775
sfinfo.frames = DATA_LENGTH ;
1776
sfinfo.channels = 1 ;
1777
sfinfo.format = format ;
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)
1784
{ /* Create a short file. */
1785
create_short_file (filename) ;
1787
/* Opening a already existing short file (ie invalid header) RDWR is disallowed.
1788
** If this returns a valif pointer sf_open() screwed up.
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__) ;
1795
/* Truncate the file to zero bytes. */
1796
if (truncate (filename, 0) < 0)
1797
{ printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
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.
1806
sfinfo.samplerate = SAMPLE_RATE ;
1807
sfinfo.frames = DATA_LENGTH ;
1808
sfinfo.channels = 1 ;
1809
sfinfo.format = format ;
1811
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
1813
/* Do 3 writes followed by reads. After each, check the data and the current
1814
** read and write offsets.
1816
for (pass = 1 ; pass <= 3 ; pass ++)
1817
{ orig [20] = pass * 2 ;
1819
/* Write some data. */
1820
test_write_int_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
1822
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
1824
/* Read what we just wrote. */
1825
test_read_int_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
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) ;
1835
test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
1836
} ; /* for (pass ...) */
1840
/* Open the file again to check the data. */
1841
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
1843
if (sfinfo.format != format)
1844
{ printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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 ) ;
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 ) ;
1858
if (sfinfo.channels != 1)
1859
{ printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
1864
test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
1866
test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
1868
for (pass = 1 ; pass <= 3 ; pass ++)
1869
{ orig [20] = pass * 2 ;
1871
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
1873
/* Read what we just wrote. */
1874
test_read_int_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
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) ;
1884
} ; /* for (pass ...) */
1887
} /* mono_rdwr_int_test */
1890
new_rdwr_24bit_test (const char *filename, int format, int allow_fd)
1891
{ SNDFILE *wfile, *rwfile ;
1896
orig = (int*) orig_data ;
1897
test = (int*) test_data ;
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 ;
1904
items = DATA_LENGTH ;
1905
frames = items / sfinfo.channels ;
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) ;
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) ;
1920
test_writef_int_or_die (wfile, 3, orig, frames, __LINE__) ;
1922
test_readf_int_or_die (rwfile, 1, test, frames, __LINE__) ;
1923
test_readf_int_or_die (rwfile, 2, test, frames, __LINE__) ;
1927
} /* new_rdwr_24bit_test */
1930
/*======================================================================================
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) ;
1939
pcm_test_int (const char *filename, int format, int long_file_ok)
1942
int k, items, allow_fd ;
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 ;
1947
print_test_name ("pcm_test_int", filename) ;
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 ;
1954
gen_windowed_sine_double (orig_data, DATA_LENGTH, (1.0 * 0x7F000000)) ;
1956
orig = (int*) orig_data ;
1957
test = (int*) test_data ;
1959
/* Make this a macro so gdb steps over it in one go. */
1960
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
1962
items = DATA_LENGTH ;
1964
/* Some test broken out here. */
1966
mono_int_test (filename, format, long_file_ok, allow_fd) ;
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") ;
1976
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
1977
mono_rdwr_int_test (filename, format, long_file_ok, allow_fd) ;
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") ;
1987
stereo_int_test (filename, format, long_file_ok, allow_fd) ;
1989
/* New read/write test. Not sure if this is needed yet. */
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) ;
1996
delete_file (format, filename) ;
2000
} /* pcm_test_int */
2003
mono_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
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 ;
2015
orig = (int*) orig_data ;
2016
test = (int*) test_data ;
2018
items = DATA_LENGTH ;
2020
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
2022
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
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) ;
2029
/* Add non-audio data after the audio. */
2030
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
2034
memset (test, 0, items * sizeof (int)) ;
2036
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
2037
memset (&sfinfo, 0, sizeof (sfinfo)) ;
2039
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
2041
if (sfinfo.format != format)
2042
{ printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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) ;
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) ;
2056
if (sfinfo.channels != 1)
2057
{ printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
2061
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
2070
/* Seek to start of file. */
2071
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
2080
if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
2081
(format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
2084
printf ("no seek : ") ;
2088
/* Seek to offset from start of file. */
2089
test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
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]) ;
2098
/* Seek to offset from current position. */
2099
test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
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]) ;
2108
/* Seek to offset from end of file. */
2109
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
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__) ;
2122
while (count < sfinfo.frames)
2123
count += sf_read_int (file, test, 311) ;
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)) ;
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) ;
2138
test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
2142
} /* mono_int_test */
2145
stereo_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
2149
int k, items, frames ;
2151
orig = (int*) orig_data ;
2152
test = (int*) test_data ;
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 ;
2159
gen_windowed_sine_double (orig_data, DATA_LENGTH, (1.0 * 0x7F000000)) ;
2161
orig = (int*) orig_data ;
2162
test = (int*) test_data ;
2164
/* Make this a macro so gdb steps over it in one go. */
2165
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
2167
items = DATA_LENGTH ;
2168
frames = items / sfinfo.channels ;
2170
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
2172
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
2174
test_writef_int_or_die (file, 0, orig, frames, __LINE__) ;
2176
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
2180
memset (test, 0, items * sizeof (int)) ;
2182
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
2183
memset (&sfinfo, 0, sizeof (sfinfo)) ;
2185
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
2187
if (sfinfo.format != format)
2188
{ printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
2189
__LINE__, format, sfinfo.format) ;
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) ;
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) ;
2205
if (sfinfo.channels != 2)
2206
{ printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
2210
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
2219
/* Seek to start of file. */
2220
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
2229
/* Seek to offset from start of file. */
2230
test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
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)) ;
2239
if (sf_read_int (file, test, 1) > 0)
2240
{ printf ("Line %d: Should return 0.\n", __LINE__) ;
2244
if (! sf_error (file))
2245
{ printf ("Line %d: Should return an error.\n", __LINE__) ;
2248
/*-----------------------*/
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]) ;
2257
/* Seek to offset from current position. */
2258
test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
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]) ;
2267
/* Seek to offset from end of file. */
2268
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
2278
} /* stereo_int_test */
2281
mono_rdwr_int_test (const char *filename, int format, int long_file_ok, int allow_fd)
2287
orig = (int*) orig_data ;
2288
test = (int*) test_data ;
2290
sfinfo.samplerate = SAMPLE_RATE ;
2291
sfinfo.frames = DATA_LENGTH ;
2292
sfinfo.channels = 1 ;
2293
sfinfo.format = format ;
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)
2300
{ /* Create a short file. */
2301
create_short_file (filename) ;
2303
/* Opening a already existing short file (ie invalid header) RDWR is disallowed.
2304
** If this returns a valif pointer sf_open() screwed up.
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__) ;
2311
/* Truncate the file to zero bytes. */
2312
if (truncate (filename, 0) < 0)
2313
{ printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
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.
2322
sfinfo.samplerate = SAMPLE_RATE ;
2323
sfinfo.frames = DATA_LENGTH ;
2324
sfinfo.channels = 1 ;
2325
sfinfo.format = format ;
2327
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
2329
/* Do 3 writes followed by reads. After each, check the data and the current
2330
** read and write offsets.
2332
for (pass = 1 ; pass <= 3 ; pass ++)
2333
{ orig [20] = pass * 2 ;
2335
/* Write some data. */
2336
test_write_int_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
2338
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
2340
/* Read what we just wrote. */
2341
test_read_int_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
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) ;
2351
test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
2352
} ; /* for (pass ...) */
2356
/* Open the file again to check the data. */
2357
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
2359
if (sfinfo.format != format)
2360
{ printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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 ) ;
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 ) ;
2374
if (sfinfo.channels != 1)
2375
{ printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
2380
test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
2382
test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
2384
for (pass = 1 ; pass <= 3 ; pass ++)
2385
{ orig [20] = pass * 2 ;
2387
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
2389
/* Read what we just wrote. */
2390
test_read_int_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
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) ;
2400
} ; /* for (pass ...) */
2403
} /* mono_rdwr_int_test */
2406
new_rdwr_int_test (const char *filename, int format, int allow_fd)
2407
{ SNDFILE *wfile, *rwfile ;
2412
orig = (int*) orig_data ;
2413
test = (int*) test_data ;
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 ;
2420
items = DATA_LENGTH ;
2421
frames = items / sfinfo.channels ;
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) ;
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) ;
2436
test_writef_int_or_die (wfile, 3, orig, frames, __LINE__) ;
2438
test_readf_int_or_die (rwfile, 1, test, frames, __LINE__) ;
2439
test_readf_int_or_die (rwfile, 2, test, frames, __LINE__) ;
2443
} /* new_rdwr_int_test */
2446
/*======================================================================================
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) ;
2455
pcm_test_float (const char *filename, int format, int long_file_ok)
2457
float *orig, *test ;
2458
int k, items, allow_fd ;
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 ;
2463
print_test_name ("pcm_test_float", filename) ;
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 ;
2470
gen_windowed_sine_double (orig_data, DATA_LENGTH, 1.0) ;
2472
orig = (float*) orig_data ;
2473
test = (float*) test_data ;
2475
/* Make this a macro so gdb steps over it in one go. */
2476
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
2478
items = DATA_LENGTH ;
2480
/* Some test broken out here. */
2482
mono_float_test (filename, format, long_file_ok, allow_fd) ;
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") ;
2492
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
2493
mono_rdwr_float_test (filename, format, long_file_ok, allow_fd) ;
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") ;
2503
stereo_float_test (filename, format, long_file_ok, allow_fd) ;
2505
/* New read/write test. Not sure if this is needed yet. */
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) ;
2512
delete_file (format, filename) ;
2516
} /* pcm_test_float */
2519
mono_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
2522
float *orig, *test ;
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 ;
2531
orig = (float*) orig_data ;
2532
test = (float*) test_data ;
2534
items = DATA_LENGTH ;
2536
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
2538
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
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) ;
2545
/* Add non-audio data after the audio. */
2546
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
2550
memset (test, 0, items * sizeof (float)) ;
2552
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
2553
memset (&sfinfo, 0, sizeof (sfinfo)) ;
2555
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
2557
if (sfinfo.format != format)
2558
{ printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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) ;
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) ;
2572
if (sfinfo.channels != 1)
2573
{ printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
2577
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
2586
/* Seek to start of file. */
2587
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
2596
if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
2597
(format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
2600
printf ("no seek : ") ;
2604
/* Seek to offset from start of file. */
2605
test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
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]) ;
2614
/* Seek to offset from current position. */
2615
test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
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]) ;
2624
/* Seek to offset from end of file. */
2625
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
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__) ;
2638
while (count < sfinfo.frames)
2639
count += sf_read_float (file, test, 311) ;
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)) ;
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) ;
2654
test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
2658
} /* mono_float_test */
2661
stereo_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
2664
float *orig, *test ;
2665
int k, items, frames ;
2667
orig = (float*) orig_data ;
2668
test = (float*) test_data ;
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 ;
2675
gen_windowed_sine_double (orig_data, DATA_LENGTH, 1.0) ;
2677
orig = (float*) orig_data ;
2678
test = (float*) test_data ;
2680
/* Make this a macro so gdb steps over it in one go. */
2681
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
2683
items = DATA_LENGTH ;
2684
frames = items / sfinfo.channels ;
2686
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
2688
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
2690
test_writef_float_or_die (file, 0, orig, frames, __LINE__) ;
2692
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
2696
memset (test, 0, items * sizeof (float)) ;
2698
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
2699
memset (&sfinfo, 0, sizeof (sfinfo)) ;
2701
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
2703
if (sfinfo.format != format)
2704
{ printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
2705
__LINE__, format, sfinfo.format) ;
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) ;
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) ;
2721
if (sfinfo.channels != 2)
2722
{ printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
2726
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
2735
/* Seek to start of file. */
2736
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
2745
/* Seek to offset from start of file. */
2746
test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
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)) ;
2755
if (sf_read_float (file, test, 1) > 0)
2756
{ printf ("Line %d: Should return 0.\n", __LINE__) ;
2760
if (! sf_error (file))
2761
{ printf ("Line %d: Should return an error.\n", __LINE__) ;
2764
/*-----------------------*/
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]) ;
2773
/* Seek to offset from current position. */
2774
test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
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]) ;
2783
/* Seek to offset from end of file. */
2784
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
2794
} /* stereo_float_test */
2797
mono_rdwr_float_test (const char *filename, int format, int long_file_ok, int allow_fd)
2800
float *orig, *test ;
2803
orig = (float*) orig_data ;
2804
test = (float*) test_data ;
2806
sfinfo.samplerate = SAMPLE_RATE ;
2807
sfinfo.frames = DATA_LENGTH ;
2808
sfinfo.channels = 1 ;
2809
sfinfo.format = format ;
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)
2816
{ /* Create a short file. */
2817
create_short_file (filename) ;
2819
/* Opening a already existing short file (ie invalid header) RDWR is disallowed.
2820
** If this returns a valif pointer sf_open() screwed up.
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__) ;
2827
/* Truncate the file to zero bytes. */
2828
if (truncate (filename, 0) < 0)
2829
{ printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
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.
2838
sfinfo.samplerate = SAMPLE_RATE ;
2839
sfinfo.frames = DATA_LENGTH ;
2840
sfinfo.channels = 1 ;
2841
sfinfo.format = format ;
2843
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
2845
/* Do 3 writes followed by reads. After each, check the data and the current
2846
** read and write offsets.
2848
for (pass = 1 ; pass <= 3 ; pass ++)
2849
{ orig [20] = pass * 2 ;
2851
/* Write some data. */
2852
test_write_float_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
2854
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
2856
/* Read what we just wrote. */
2857
test_read_float_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
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) ;
2867
test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
2868
} ; /* for (pass ...) */
2872
/* Open the file again to check the data. */
2873
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
2875
if (sfinfo.format != format)
2876
{ printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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 ) ;
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 ) ;
2890
if (sfinfo.channels != 1)
2891
{ printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
2896
test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
2898
test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
2900
for (pass = 1 ; pass <= 3 ; pass ++)
2901
{ orig [20] = pass * 2 ;
2903
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
2905
/* Read what we just wrote. */
2906
test_read_float_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
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) ;
2916
} ; /* for (pass ...) */
2919
} /* mono_rdwr_float_test */
2922
new_rdwr_float_test (const char *filename, int format, int allow_fd)
2923
{ SNDFILE *wfile, *rwfile ;
2925
float *orig, *test ;
2928
orig = (float*) orig_data ;
2929
test = (float*) test_data ;
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 ;
2936
items = DATA_LENGTH ;
2937
frames = items / sfinfo.channels ;
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) ;
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) ;
2952
test_writef_float_or_die (wfile, 3, orig, frames, __LINE__) ;
2954
test_readf_float_or_die (rwfile, 1, test, frames, __LINE__) ;
2955
test_readf_float_or_die (rwfile, 2, test, frames, __LINE__) ;
2959
} /* new_rdwr_float_test */
2962
/*======================================================================================
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) ;
2971
pcm_test_double (const char *filename, int format, int long_file_ok)
2973
double *orig, *test ;
2974
int k, items, allow_fd ;
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 ;
2979
print_test_name ("pcm_test_double", filename) ;
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 ;
2986
gen_windowed_sine_double (orig_data, DATA_LENGTH, 1.0) ;
2988
orig = (double*) orig_data ;
2989
test = (double*) test_data ;
2991
/* Make this a macro so gdb steps over it in one go. */
2992
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
2994
items = DATA_LENGTH ;
2996
/* Some test broken out here. */
2998
mono_double_test (filename, format, long_file_ok, allow_fd) ;
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") ;
3008
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
3009
mono_rdwr_double_test (filename, format, long_file_ok, allow_fd) ;
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") ;
3019
stereo_double_test (filename, format, long_file_ok, allow_fd) ;
3021
/* New read/write test. Not sure if this is needed yet. */
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) ;
3028
delete_file (format, filename) ;
3032
} /* pcm_test_double */
3035
mono_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
3038
double *orig, *test ;
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 ;
3047
orig = (double*) orig_data ;
3048
test = (double*) test_data ;
3050
items = DATA_LENGTH ;
3052
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
3054
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
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) ;
3061
/* Add non-audio data after the audio. */
3062
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
3066
memset (test, 0, items * sizeof (double)) ;
3068
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
3069
memset (&sfinfo, 0, sizeof (sfinfo)) ;
3071
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
3073
if (sfinfo.format != format)
3074
{ printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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) ;
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) ;
3088
if (sfinfo.channels != 1)
3089
{ printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
3093
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
3102
/* Seek to start of file. */
3103
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
3112
if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
3113
(format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
3116
printf ("no seek : ") ;
3120
/* Seek to offset from start of file. */
3121
test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
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]) ;
3130
/* Seek to offset from current position. */
3131
test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
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]) ;
3140
/* Seek to offset from end of file. */
3141
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
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__) ;
3154
while (count < sfinfo.frames)
3155
count += sf_read_double (file, test, 311) ;
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)) ;
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) ;
3170
test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
3174
} /* mono_double_test */
3177
stereo_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
3180
double *orig, *test ;
3181
int k, items, frames ;
3183
orig = (double*) orig_data ;
3184
test = (double*) test_data ;
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 ;
3191
gen_windowed_sine_double (orig_data, DATA_LENGTH, 1.0) ;
3193
orig = (double*) orig_data ;
3194
test = (double*) test_data ;
3196
/* Make this a macro so gdb steps over it in one go. */
3197
CONVERT_DATA (k, DATA_LENGTH, orig, orig_data) ;
3199
items = DATA_LENGTH ;
3200
frames = items / sfinfo.channels ;
3202
file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
3204
sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
3206
test_writef_double_or_die (file, 0, orig, frames, __LINE__) ;
3208
sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
3212
memset (test, 0, items * sizeof (double)) ;
3214
if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
3215
memset (&sfinfo, 0, sizeof (sfinfo)) ;
3217
file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
3219
if (sfinfo.format != format)
3220
{ printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
3221
__LINE__, format, sfinfo.format) ;
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) ;
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) ;
3237
if (sfinfo.channels != 2)
3238
{ printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
3242
check_log_buffer_or_die (file, __LINE__) ;
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]) ;
3251
/* Seek to start of file. */
3252
test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
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]) ;
3261
/* Seek to offset from start of file. */
3262
test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
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)) ;
3271
if (sf_read_double (file, test, 1) > 0)
3272
{ printf ("Line %d: Should return 0.\n", __LINE__) ;
3276
if (! sf_error (file))
3277
{ printf ("Line %d: Should return an error.\n", __LINE__) ;
3280
/*-----------------------*/
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]) ;
3289
/* Seek to offset from current position. */
3290
test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
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]) ;
3299
/* Seek to offset from end of file. */
3300
test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
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]) ;
3310
} /* stereo_double_test */
3313
mono_rdwr_double_test (const char *filename, int format, int long_file_ok, int allow_fd)
3316
double *orig, *test ;
3319
orig = (double*) orig_data ;
3320
test = (double*) test_data ;
3322
sfinfo.samplerate = SAMPLE_RATE ;
3323
sfinfo.frames = DATA_LENGTH ;
3324
sfinfo.channels = 1 ;
3325
sfinfo.format = format ;
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)
3332
{ /* Create a short file. */
3333
create_short_file (filename) ;
3335
/* Opening a already existing short file (ie invalid header) RDWR is disallowed.
3336
** If this returns a valif pointer sf_open() screwed up.
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__) ;
3343
/* Truncate the file to zero bytes. */
3344
if (truncate (filename, 0) < 0)
3345
{ printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
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.
3354
sfinfo.samplerate = SAMPLE_RATE ;
3355
sfinfo.frames = DATA_LENGTH ;
3356
sfinfo.channels = 1 ;
3357
sfinfo.format = format ;
3359
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
3361
/* Do 3 writes followed by reads. After each, check the data and the current
3362
** read and write offsets.
3364
for (pass = 1 ; pass <= 3 ; pass ++)
3365
{ orig [20] = pass * 2 ;
3367
/* Write some data. */
3368
test_write_double_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
3370
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
3372
/* Read what we just wrote. */
3373
test_read_double_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
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) ;
3383
test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
3384
} ; /* for (pass ...) */
3388
/* Open the file again to check the data. */
3389
file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
3391
if (sfinfo.format != format)
3392
{ printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
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 ) ;
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 ) ;
3406
if (sfinfo.channels != 1)
3407
{ printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
3412
test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
3414
test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
3416
for (pass = 1 ; pass <= 3 ; pass ++)
3417
{ orig [20] = pass * 2 ;
3419
test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
3421
/* Read what we just wrote. */
3422
test_read_double_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
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) ;
3432
} ; /* for (pass ...) */
3435
} /* mono_rdwr_double_test */
3438
new_rdwr_double_test (const char *filename, int format, int allow_fd)
3439
{ SNDFILE *wfile, *rwfile ;
3441
double *orig, *test ;
3444
orig = (double*) orig_data ;
3445
test = (double*) test_data ;
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 ;
3452
items = DATA_LENGTH ;
3453
frames = items / sfinfo.channels ;
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) ;
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) ;
3468
test_writef_double_or_die (wfile, 3, orig, frames, __LINE__) ;
3470
test_readf_double_or_die (rwfile, 1, test, frames, __LINE__) ;
3471
test_readf_double_or_die (rwfile, 2, test, frames, __LINE__) ;
3475
} /* new_rdwr_double_test */
3479
/*----------------------------------------------------------------------------------------
3483
empty_file_test (const char *filename, int format)
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 ;
3491
print_test_name ("empty_file_test", filename) ;
3495
info.samplerate = 48000 ;
3497
info.format = format ;
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") ;
3507
/* Create an empty file. */
3508
file = test_open_file_or_die (filename, SFM_WRITE, &info, allow_fd, __LINE__) ;
3511
/* Open for read and check the length. */
3512
file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
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)) ;
3521
/* Open for read/write and check the length. */
3522
file = test_open_file_or_die (filename, SFM_RDWR, &info, allow_fd, __LINE__) ;
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)) ;
3531
/* Open for read and check the length. */
3532
file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
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)) ;
3541
check_open_file_count_or_die (__LINE__) ;
3547
} /* empty_file_test */
3550
/*----------------------------------------------------------------------------------------
3554
create_short_file (const char *filename)
3557
if (! (file = fopen (filename, "w")))
3558
{ printf ("create_short_file : fopen (%s, \"w\") failed.", filename) ;
3564
fprintf (file, "This is the file data.\n") ;
3567
} /* create_short_file */
3569
#if (defined (WIN32) || defined (__WIN32))
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
3577
truncate (const char *filename, int ignored)
3582
if ((fd = open (filename, O_RDWR | O_TRUNC | O_BINARY)) < 0)