libsndfile-ocaml source files.
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / src / sndfile.c
1 /*
2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "sfconfig.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <assert.h>
25
26 #include "sndfile.h"
27 #include "sfendian.h"
28 #include "common.h"
29
30 #define SNDFILE_MAGICK 0x1234C0DE
31
32 #ifdef __APPLE__
33 /*
34 ** Detect if a compile for a universal binary is being attempted and barf if it is.
35 ** See the URL below for the rationale.
36 */
37 #ifdef __BIG_ENDIAN__
38 #if (CPU_IS_LITTLE_ENDIAN == 1)
39 #error "Universal binary compile detected. See http://www.mega-nerd.com/libsndfile/FAQ.html#Q018"
40 #endif
41 #endif
42
43 #ifdef __LITTLE_ENDIAN__
44 #if (CPU_IS_BIG_ENDIAN == 1)
45 #error "Universal binary compile detected. See http://www.mega-nerd.com/libsndfile/FAQ.html#Q018"
46 #endif
47 #endif
48 #endif
49
50
51 typedef struct
52 { int error ;
53 const char *str ;
54 } ErrorStruct ;
55
56 static
57 ErrorStruct SndfileErrors [] =
58 {
59 /* Public error values and their associated strings. */
60 { SF_ERR_NO_ERROR , "No Error." },
61 { SF_ERR_UNRECOGNISED_FORMAT , "Format not recognised." },
62 { SF_ERR_SYSTEM , "System error." /* Often replaced. */ },
63 { SF_ERR_MALFORMED_FILE , "Supported file format but file is malformed." },
64 { SF_ERR_UNSUPPORTED_ENCODING , "Supported file format but unsupported encoding." },
65
66 /* Private error values and their associated strings. */
67 { SFE_ZERO_MAJOR_FORMAT , "Error : major format is 0." },
68 { SFE_ZERO_MINOR_FORMAT , "Error : minor format is 0." },
69 { SFE_BAD_FILE , "File does not exist or is not a regular file (possibly a pipe?)." },
70 { SFE_BAD_FILE_READ , "File exists but no data could be read." },
71 { SFE_OPEN_FAILED , "Could not open file." },
72 { SFE_BAD_SNDFILE_PTR , "Not a valid SNDFILE* pointer." },
73 { SFE_BAD_SF_INFO_PTR , "NULL SF_INFO pointer passed to libsndfile." },
74 { SFE_BAD_SF_INCOMPLETE , "SF_PRIVATE struct incomplete and end of header parsing." },
75 { SFE_BAD_FILE_PTR , "Bad FILE pointer." },
76 { SFE_BAD_INT_PTR , "Internal error, Bad pointer." },
77 { SFE_BAD_STAT_SIZE , "Error : software was misconfigured at compile time (sizeof statbuf.st_size)." },
78
79 { SFE_MALLOC_FAILED , "Internal malloc () failed." },
80 { SFE_UNIMPLEMENTED , "File contains data in an unimplemented format." },
81 { SFE_BAD_READ_ALIGN , "Attempt to read a non-integer number of channels." },
82 { SFE_BAD_WRITE_ALIGN , "Attempt to write a non-integer number of channels." },
83 { SFE_UNKNOWN_FORMAT , "File contains data in an unknown format." },
84 { SFE_NOT_READMODE , "Read attempted on file currently open for write." },
85 { SFE_NOT_WRITEMODE , "Write attempted on file currently open for read." },
86 { SFE_BAD_MODE_RW , "Error : This file format does not support read/write mode." },
87 { SFE_BAD_SF_INFO , "Internal error : SF_INFO struct incomplete." },
88 { SFE_BAD_OFFSET , "Error : supplied offset beyond end of file." },
89 { SFE_NO_EMBED_SUPPORT , "Error : embedding not supported for this file format." },
90 { SFE_NO_EMBEDDED_RDWR , "Error : cannot open embedded file read/write." },
91 { SFE_NO_PIPE_WRITE , "Error : this file format does not support pipe write." },
92 { SFE_BAD_VIRTUAL_IO , "Error : bad pointer on SF_VIRTUAL_IO struct." },
93 { SFE_BAD_BROADCAST_INFO_SIZE
94 , "Error : bad size in SF_BROADCAST_INFO struct." },
95 { SFE_BAD_BROADCAST_INFO_TOO_BIG
96 , "Error : SF_BROADCAST_INFO struct too large." },
97
98 { SFE_INTERLEAVE_MODE , "Attempt to write to file with non-interleaved data." },
99 { SFE_INTERLEAVE_SEEK , "Bad karma in seek during interleave read operation." },
100 { SFE_INTERLEAVE_READ , "Bad karma in read during interleave read operation." },
101
102 { SFE_INTERNAL , "Unspecified internal error." },
103 { SFE_BAD_COMMAND_PARAM , "Bad parameter passed to function sf_command." },
104 { SFE_BAD_ENDIAN , "Bad endian-ness. Try default endian-ness" },
105 { SFE_CHANNEL_COUNT_ZERO , "Channel count is zero." },
106 { SFE_CHANNEL_COUNT , "Too many channels specified." },
107
108 { SFE_BAD_SEEK , "Internal psf_fseek() failed." },
109 { SFE_NOT_SEEKABLE , "Seek attempted on unseekable file type." },
110 { SFE_AMBIGUOUS_SEEK , "Error : combination of file open mode and seek command is ambiguous." },
111 { SFE_WRONG_SEEK , "Error : invalid seek parameters." },
112 { SFE_SEEK_FAILED , "Error : parameters OK, but psf_seek() failed." },
113
114 { SFE_BAD_OPEN_MODE , "Error : bad mode parameter for file open." },
115 { SFE_OPEN_PIPE_RDWR , "Error : attempt to open a pipe in read/write mode." },
116 { SFE_RDWR_POSITION , "Error on RDWR position (cryptic)." },
117 { SFE_RDWR_BAD_HEADER , "Error : Cannot open file in read/write mode due to string data in header." },
118 { SFE_CMD_HAS_DATA , "Error : Command fails because file already has audio data." },
119
120 { SFE_STR_NO_SUPPORT , "Error : File type does not support string data." },
121 { SFE_STR_NOT_WRITE , "Error : Trying to set a string when file is not in write mode." },
122 { SFE_STR_MAX_DATA , "Error : Maximum string data storage reached." },
123 { SFE_STR_MAX_COUNT , "Error : Maximum string data count reached." },
124 { SFE_STR_BAD_TYPE , "Error : Bad string data type." },
125 { SFE_STR_NO_ADD_END , "Error : file type does not support strings added at end of file." },
126 { SFE_STR_BAD_STRING , "Error : bad string." },
127 { SFE_STR_WEIRD , "Error : Weird string error." },
128
129 { SFE_WAV_NO_RIFF , "Error in WAV file. No 'RIFF' chunk marker." },
130 { SFE_WAV_NO_WAVE , "Error in WAV file. No 'WAVE' chunk marker." },
131 { SFE_WAV_NO_FMT , "Error in WAV/W64/RF64 file. No 'fmt ' chunk marker." },
132 { SFE_WAV_BAD_FMT , "Error in WAV/W64/RF64 file. Malformed 'fmt ' chunk." },
133 { SFE_WAV_FMT_SHORT , "Error in WAV/W64/RF64 file. Short 'fmt ' chunk." },
134
135 { SFE_WAV_BAD_FACT , "Error in WAV file. 'fact' chunk out of place." },
136 { SFE_WAV_BAD_PEAK , "Error in WAV file. Bad 'PEAK' chunk." },
137 { SFE_WAV_PEAK_B4_FMT , "Error in WAV file. 'PEAK' chunk found before 'fmt ' chunk." },
138
139 { SFE_WAV_BAD_FORMAT , "Error in WAV file. Errors in 'fmt ' chunk." },
140 { SFE_WAV_BAD_BLOCKALIGN , "Error in WAV file. Block alignment in 'fmt ' chunk is incorrect." },
141 { SFE_WAV_NO_DATA , "Error in WAV file. No 'data' chunk marker." },
142 { SFE_WAV_BAD_LIST , "Error in WAV file. Malformed LIST chunk." },
143 { SFE_WAV_UNKNOWN_CHUNK , "Error in WAV file. File contains an unknown chunk marker." },
144 { SFE_WAV_WVPK_DATA , "Error in WAV file. Data is in WAVPACK format." },
145
146 { SFE_WAV_ADPCM_NOT4BIT , "Error in ADPCM WAV file. Invalid bit width." },
147 { SFE_WAV_ADPCM_CHANNELS , "Error in ADPCM WAV file. Invalid number of channels." },
148 { SFE_WAV_GSM610_FORMAT , "Error in GSM610 WAV file. Invalid format chunk." },
149
150 { SFE_AIFF_NO_FORM , "Error in AIFF file, bad 'FORM' marker." },
151 { SFE_AIFF_AIFF_NO_FORM , "Error in AIFF file, 'AIFF' marker without 'FORM'." },
152 { SFE_AIFF_COMM_NO_FORM , "Error in AIFF file, 'COMM' marker without 'FORM'." },
153 { SFE_AIFF_SSND_NO_COMM , "Error in AIFF file, 'SSND' marker without 'COMM'." },
154 { SFE_AIFF_UNKNOWN_CHUNK , "Error in AIFF file, unknown chunk." },
155 { SFE_AIFF_COMM_CHUNK_SIZE, "Error in AIFF file, bad 'COMM' chunk size." },
156 { SFE_AIFF_BAD_COMM_CHUNK , "Error in AIFF file, bad 'COMM' chunk." },
157 { SFE_AIFF_PEAK_B4_COMM , "Error in AIFF file. 'PEAK' chunk found before 'COMM' chunk." },
158 { SFE_AIFF_BAD_PEAK , "Error in AIFF file. Bad 'PEAK' chunk." },
159 { SFE_AIFF_NO_SSND , "Error in AIFF file, bad 'SSND' chunk." },
160 { SFE_AIFF_NO_DATA , "Error in AIFF file, no sound data." },
161 { SFE_AIFF_RW_SSND_NOT_LAST, "Error in AIFF file, RDWR only possible if SSND chunk at end of file." },
162
163 { SFE_AU_UNKNOWN_FORMAT , "Error in AU file, unknown format." },
164 { SFE_AU_NO_DOTSND , "Error in AU file, missing '.snd' or 'dns.' marker." },
165 { SFE_AU_EMBED_BAD_LEN , "Embedded AU file with unknown length." },
166
167 { SFE_RAW_READ_BAD_SPEC , "Error while opening RAW file for read. Must specify format and channels.\n"
168 "Possibly trying to open unsupported format."
169 },
170 { SFE_RAW_BAD_BITWIDTH , "Error. RAW file bitwidth must be a multiple of 8." },
171 { SFE_RAW_BAD_FORMAT , "Error. Bad format field in SF_INFO struct when openning a RAW file for read." },
172
173 { SFE_PAF_NO_MARKER , "Error in PAF file, no marker." },
174 { SFE_PAF_VERSION , "Error in PAF file, bad version." },
175 { SFE_PAF_UNKNOWN_FORMAT , "Error in PAF file, unknown format." },
176 { SFE_PAF_SHORT_HEADER , "Error in PAF file. File shorter than minimal header." },
177 { SFE_PAF_BAD_CHANNELS , "Error in PAF file. Bad channel count." },
178
179 { SFE_SVX_NO_FORM , "Error in 8SVX / 16SV file, no 'FORM' marker." },
180 { SFE_SVX_NO_BODY , "Error in 8SVX / 16SV file, no 'BODY' marker." },
181 { SFE_SVX_NO_DATA , "Error in 8SVX / 16SV file, no sound data." },
182 { SFE_SVX_BAD_COMP , "Error in 8SVX / 16SV file, unsupported compression format." },
183 { SFE_SVX_BAD_NAME_LENGTH , "Error in 8SVX / 16SV file, NAME chunk too long." },
184
185 { SFE_NIST_BAD_HEADER , "Error in NIST file, bad header." },
186 { SFE_NIST_CRLF_CONVERISON, "Error : NIST file damaged by Windows CR -> CRLF conversion process." },
187 { SFE_NIST_BAD_ENCODING , "Error in NIST file, unsupported compression format." },
188
189 { SFE_VOC_NO_CREATIVE , "Error in VOC file, no 'Creative Voice File' marker." },
190 { SFE_VOC_BAD_FORMAT , "Error in VOC file, bad format." },
191 { SFE_VOC_BAD_VERSION , "Error in VOC file, bad version number." },
192 { SFE_VOC_BAD_MARKER , "Error in VOC file, bad marker in file." },
193 { SFE_VOC_BAD_SECTIONS , "Error in VOC file, incompatible VOC sections." },
194 { SFE_VOC_MULTI_SAMPLERATE, "Error in VOC file, more than one sample rate defined." },
195 { SFE_VOC_MULTI_SECTION , "Unimplemented VOC file feature, file contains multiple sound sections." },
196 { SFE_VOC_MULTI_PARAM , "Error in VOC file, file contains multiple bit or channel widths." },
197 { SFE_VOC_SECTION_COUNT , "Error in VOC file, too many sections." },
198 { SFE_VOC_NO_PIPE , "Error : not able to operate on VOC files over a pipe." },
199
200 { SFE_IRCAM_NO_MARKER , "Error in IRCAM file, bad IRCAM marker." },
201 { SFE_IRCAM_BAD_CHANNELS , "Error in IRCAM file, bad channel count." },
202 { SFE_IRCAM_UNKNOWN_FORMAT, "Error in IRCAM file, unknow encoding format." },
203
204 { SFE_W64_64_BIT , "Error in W64 file, file contains 64 bit offset." },
205 { SFE_W64_NO_RIFF , "Error in W64 file. No 'riff' chunk marker." },
206 { SFE_W64_NO_WAVE , "Error in W64 file. No 'wave' chunk marker." },
207 { SFE_W64_NO_DATA , "Error in W64 file. No 'data' chunk marker." },
208 { SFE_W64_ADPCM_NOT4BIT , "Error in ADPCM W64 file. Invalid bit width." },
209 { SFE_W64_ADPCM_CHANNELS , "Error in ADPCM W64 file. Invalid number of channels." },
210 { SFE_W64_GSM610_FORMAT , "Error in GSM610 W64 file. Invalid format chunk." },
211
212 { SFE_MAT4_BAD_NAME , "Error in MAT4 file. No variable name." },
213 { SFE_MAT4_NO_SAMPLERATE , "Error in MAT4 file. No sample rate." },
214
215 { SFE_MAT5_BAD_ENDIAN , "Error in MAT5 file. Not able to determine endian-ness." },
216 { SFE_MAT5_NO_BLOCK , "Error in MAT5 file. Bad block structure." },
217 { SFE_MAT5_SAMPLE_RATE , "Error in MAT5 file. Not able to determine sample rate." },
218
219 { SFE_PVF_NO_PVF1 , "Error in PVF file. No PVF1 marker." },
220 { SFE_PVF_BAD_HEADER , "Error in PVF file. Bad header." },
221 { SFE_PVF_BAD_BITWIDTH , "Error in PVF file. Bad bit width." },
222
223 { SFE_XI_BAD_HEADER , "Error in XI file. Bad header." },
224 { SFE_XI_EXCESS_SAMPLES , "Error in XI file. Excess samples in file." },
225 { SFE_XI_NO_PIPE , "Error : not able to operate on XI files over a pipe." },
226
227 { SFE_HTK_NO_PIPE , "Error : not able to operate on HTK files over a pipe." },
228
229 { SFE_SDS_NOT_SDS , "Error : not an SDS file." },
230 { SFE_SDS_BAD_BIT_WIDTH , "Error : bad bit width for SDS file." },
231
232 { SFE_SD2_FD_DISALLOWED , "Error : cannot open SD2 file without a file name." },
233 { SFE_SD2_BAD_DATA_OFFSET , "Error : bad data offset." },
234 { SFE_SD2_BAD_MAP_OFFSET , "Error : bad map offset." },
235 { SFE_SD2_BAD_DATA_LENGTH , "Error : bad data length." },
236 { SFE_SD2_BAD_MAP_LENGTH , "Error : bad map length." },
237 { SFE_SD2_BAD_RSRC , "Error : bad resource fork." },
238 { SFE_SD2_BAD_SAMPLE_SIZE , "Error : bad sample size." },
239
240 { SFE_FLAC_BAD_HEADER , "Error : bad flac header." },
241 { SFE_FLAC_NEW_DECODER , "Error : problem while creating flac decoder." },
242 { SFE_FLAC_INIT_DECODER , "Error : problem while initialization of the flac decoder." },
243 { SFE_FLAC_LOST_SYNC , "Error : flac decoder lost sync." },
244 { SFE_FLAC_BAD_SAMPLE_RATE, "Error : flac does not support this sample rate." },
245 { SFE_FLAC_UNKOWN_ERROR , "Error : unknown error in flac decoder." },
246
247 { SFE_WVE_NOT_WVE , "Error : not a WVE file." },
248 { SFE_WVE_NO_PIPE , "Error : not able to operate on WVE files over a pipe." },
249
250 { SFE_DWVW_BAD_BITWIDTH , "Error : Bad bit width for DWVW encoding. Must be 12, 16 or 24." },
251 { SFE_G72X_NOT_MONO , "Error : G72x encoding does not support more than 1 channel." },
252
253 { SFE_VORBIS_ENCODER_BUG , "Error : Sample rate chosen is known to trigger a Vorbis encoder bug on this CPU." },
254
255 { SFE_RF64_NOT_RF64 , "Error : Not an RF64 file." },
256
257 { SFE_MAX_ERROR , "Maximum error number." },
258 { SFE_MAX_ERROR + 1 , NULL }
259 } ;
260
261 /*------------------------------------------------------------------------------
262 */
263
264 static int format_from_extension (SF_PRIVATE *psf) ;
265 static int guess_file_type (SF_PRIVATE *psf) ;
266 static int validate_sfinfo (SF_INFO *sfinfo) ;
267 static int validate_psf (SF_PRIVATE *psf) ;
268 static void save_header_info (SF_PRIVATE *psf) ;
269 static void copy_filename (SF_PRIVATE *psf, const char *path) ;
270 static int psf_close (SF_PRIVATE *psf) ;
271
272 static int try_resource_fork (SF_PRIVATE * psf) ;
273
274 /*------------------------------------------------------------------------------
275 ** Private (static) variables.
276 */
277
278 int sf_errno = 0 ;
279 static char sf_logbuffer [SF_BUFFER_LEN] = { 0 } ;
280 static char sf_syserr [SF_SYSERR_LEN] = { 0 } ;
281
282 /*------------------------------------------------------------------------------
283 */
284
285 #define VALIDATE_SNDFILE_AND_ASSIGN_PSF(a,b,c) \
286 { if ((a) == NULL) \
287 { sf_errno = SFE_BAD_SNDFILE_PTR ; \
288 return 0 ; \
289 } ; \
290 (b) = (SF_PRIVATE*) (a) ; \
291 if ((b)->virtual_io == SF_FALSE && \
292 psf_file_valid (b) == 0) \
293 { (b)->error = SFE_BAD_FILE_PTR ; \
294 return 0 ; \
295 } ; \
296 if ((b)->Magick != SNDFILE_MAGICK) \
297 { (b)->error = SFE_BAD_SNDFILE_PTR ; \
298 return 0 ; \
299 } ; \
300 if (c) (b)->error = 0 ; \
301 }
302
303 /*------------------------------------------------------------------------------
304 ** Public functions.
305 */
306
307 SNDFILE*
308 sf_open (const char *path, int mode, SF_INFO *sfinfo)
309 { SF_PRIVATE *psf ;
310
311 /* Ultimate sanity check. */
312 assert (sizeof (sf_count_t) == 8) ;
313
314 if ((psf = calloc (1, sizeof (SF_PRIVATE))) == NULL)
315 { sf_errno = SFE_MALLOC_FAILED ;
316 return NULL ;
317 } ;
318
319 memset (psf, 0, sizeof (SF_PRIVATE)) ;
320 psf_init_files (psf) ;
321
322 psf_log_printf (psf, "File : %s\n", path) ;
323
324 copy_filename (psf, path) ;
325
326 psf->file.mode = mode ;
327 if (strcmp (path, "-") == 0)
328 psf->error = psf_set_stdio (psf) ;
329 else
330 psf->error = psf_fopen (psf) ;
331
332 return psf_open_file (psf, sfinfo) ;
333 } /* sf_open */
334
335 SNDFILE*
336 sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc)
337 { SF_PRIVATE *psf ;
338
339 if ((SF_CONTAINER (sfinfo->format)) == SF_FORMAT_SD2)
340 { sf_errno = SFE_SD2_FD_DISALLOWED ;
341 return NULL ;
342 } ;
343
344 if ((psf = calloc (1, sizeof (SF_PRIVATE))) == NULL)
345 { sf_errno = SFE_MALLOC_FAILED ;
346 return NULL ;
347 } ;
348
349 psf_init_files (psf) ;
350 copy_filename (psf, "") ;
351
352 psf->file.mode = mode ;
353 psf_set_file (psf, fd) ;
354 psf->is_pipe = psf_is_pipe (psf) ;
355 psf->fileoffset = psf_ftell (psf) ;
356
357 if (! close_desc)
358 psf->file.do_not_close_descriptor = SF_TRUE ;
359
360 return psf_open_file (psf, sfinfo) ;
361 } /* sf_open_fd */
362
363 SNDFILE*
364 sf_open_virtual (SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo, void *user_data)
365 { SF_PRIVATE *psf ;
366
367 /* Make sure we have a valid set ot virtual pointers. */
368 if (sfvirtual->get_filelen == NULL || sfvirtual->seek == NULL || sfvirtual->tell == NULL)
369 { sf_errno = SFE_BAD_VIRTUAL_IO ;
370 snprintf (sf_logbuffer, sizeof (sf_logbuffer), "Bad vio_get_filelen / vio_seek / vio_tell in SF_VIRTUAL_IO struct.\n") ;
371 return NULL ;
372 } ;
373
374 if ((mode == SFM_READ || mode == SFM_RDWR) && sfvirtual->read == NULL)
375 { sf_errno = SFE_BAD_VIRTUAL_IO ;
376 snprintf (sf_logbuffer, sizeof (sf_logbuffer), "Bad vio_read in SF_VIRTUAL_IO struct.\n") ;
377 return NULL ;
378 } ;
379
380 if ((mode == SFM_WRITE || mode == SFM_RDWR) && sfvirtual->write == NULL)
381 { sf_errno = SFE_BAD_VIRTUAL_IO ;
382 snprintf (sf_logbuffer, sizeof (sf_logbuffer), "Bad vio_write in SF_VIRTUAL_IO struct.\n") ;
383 return NULL ;
384 } ;
385
386 if ((psf = calloc (1, sizeof (SF_PRIVATE))) == NULL)
387 { sf_errno = SFE_MALLOC_FAILED ;
388 return NULL ;
389 } ;
390
391 psf_init_files (psf) ;
392
393 psf->virtual_io = SF_TRUE ;
394 psf->vio = *sfvirtual ;
395 psf->vio_user_data = user_data ;
396
397 psf->file.mode = mode ;
398
399 return psf_open_file (psf, sfinfo) ;
400 } /* sf_open_virtual */
401
402 int
403 sf_close (SNDFILE *sndfile)
404 { SF_PRIVATE *psf ;
405
406 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
407
408 return psf_close (psf) ;
409 } /* sf_close */
410
411 void
412 sf_write_sync (SNDFILE *sndfile)
413 { SF_PRIVATE *psf ;
414
415 if ((psf = (SF_PRIVATE *) sndfile) == NULL)
416 return ;
417
418 psf_fsync (psf) ;
419
420 return ;
421 } /* sf_write_sync */
422
423 /*==============================================================================
424 */
425
426 const char*
427 sf_error_number (int errnum)
428 { static const char *bad_errnum =
429 "No error defined for this error number. This is a bug in libsndfile." ;
430 int k ;
431
432 if (errnum == SFE_MAX_ERROR)
433 return SndfileErrors [0].str ;
434
435 if (errnum < 0 || errnum > SFE_MAX_ERROR)
436 { /* This really shouldn't happen in release versions. */
437 printf ("Not a valid error number (%d).\n", errnum) ;
438 return bad_errnum ;
439 } ;
440
441 for (k = 0 ; SndfileErrors [k].str ; k++)
442 if (errnum == SndfileErrors [k].error)
443 return SndfileErrors [k].str ;
444
445 return bad_errnum ;
446 } /* sf_error_number */
447
448 const char*
449 sf_strerror (SNDFILE *sndfile)
450 { SF_PRIVATE *psf = NULL ;
451 int errnum ;
452
453 if (sndfile == NULL)
454 { errnum = sf_errno ;
455 if (errnum == SFE_SYSTEM && sf_syserr [0])
456 return sf_syserr ;
457 }
458 else
459 { psf = (SF_PRIVATE *) sndfile ;
460
461 if (psf->Magick != SNDFILE_MAGICK)
462 return "sf_strerror : Bad magic number." ;
463
464 errnum = psf->error ;
465
466 if (errnum == SFE_SYSTEM && psf->syserr [0])
467 return psf->syserr ;
468 } ;
469
470 return sf_error_number (errnum) ;
471 } /* sf_strerror */
472
473 /*------------------------------------------------------------------------------
474 */
475
476 int
477 sf_error (SNDFILE *sndfile)
478 { SF_PRIVATE *psf ;
479
480 if (sndfile == NULL)
481 return sf_errno ;
482
483 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0) ;
484
485 if (psf->error)
486 return psf->error ;
487
488 return 0 ;
489 } /* sf_error */
490
491 /*------------------------------------------------------------------------------
492 */
493
494 int
495 sf_perror (SNDFILE *sndfile)
496 { SF_PRIVATE *psf ;
497 int errnum ;
498
499 if (sndfile == NULL)
500 { errnum = sf_errno ;
501 }
502 else
503 { VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0) ;
504 errnum = psf->error ;
505 } ;
506
507 fprintf (stderr, "%s\n", sf_error_number (errnum)) ;
508 return SFE_NO_ERROR ;
509 } /* sf_perror */
510
511
512 /*------------------------------------------------------------------------------
513 */
514
515 int
516 sf_error_str (SNDFILE *sndfile, char *str, size_t maxlen)
517 { SF_PRIVATE *psf ;
518 int errnum ;
519
520 if (str == NULL)
521 return SFE_INTERNAL ;
522
523 if (sndfile == NULL)
524 errnum = sf_errno ;
525 else
526 { VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0) ;
527 errnum = psf->error ;
528 } ;
529
530 snprintf (str, maxlen, "%s", sf_error_number (errnum)) ;
531
532 return SFE_NO_ERROR ;
533 } /* sf_error_str */
534
535 /*==============================================================================
536 */
537
538 int
539 sf_format_check (const SF_INFO *info)
540 { int subformat, endian ;
541
542 subformat = SF_CODEC (info->format) ;
543 endian = SF_ENDIAN (info->format) ;
544
545 /* This is the place where each file format can check if the suppiled
546 ** SF_INFO struct is valid.
547 ** Return 0 on failure, 1 ons success.
548 */
549
550 if (info->channels < 1 || info->channels > SF_MAX_CHANNELS)
551 return 0 ;
552
553 if (info->samplerate < 0)
554 return 0 ;
555
556 switch (SF_CONTAINER (info->format))
557 { case SF_FORMAT_WAV :
558 /* WAV now allows both endian, RIFF or RIFX (little or big respectively) */
559 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
560 return 1 ;
561 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
562 return 1 ;
563 if ((subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM) && info->channels <= 2)
564 return 1 ;
565 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
566 return 1 ;
567 if (subformat == SF_FORMAT_G721_32 && info->channels == 1)
568 return 1 ;
569 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
570 return 1 ;
571 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
572 return 1 ;
573 break ;
574
575 case SF_FORMAT_WAVEX :
576 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
577 return 0 ;
578 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
579 return 1 ;
580 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
581 return 1 ;
582 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
583 return 1 ;
584 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
585 return 1 ;
586 break ;
587
588 case SF_FORMAT_AIFF :
589 /* AIFF does allow both endian-nesses for PCM data.*/
590 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
591 return 1 ;
592 /* Other encodings. Check for endian-ness. */
593 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
594 return 0 ;
595 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8)
596 return 1 ;
597 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
598 return 1 ;
599 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
600 return 1 ;
601 if ((subformat == SF_FORMAT_DWVW_12 || subformat == SF_FORMAT_DWVW_16 ||
602 subformat == SF_FORMAT_DWVW_24) && info-> channels == 1)
603 return 1 ;
604 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
605 return 1 ;
606 if (subformat == SF_FORMAT_IMA_ADPCM && (info->channels == 1 || info->channels == 2))
607 return 1 ;
608 break ;
609
610 case SF_FORMAT_AU :
611 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
612 return 1 ;
613 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
614 return 1 ;
615 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
616 return 1 ;
617 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
618 return 1 ;
619 if (subformat == SF_FORMAT_G721_32 && info->channels == 1)
620 return 1 ;
621 if (subformat == SF_FORMAT_G723_24 && info->channels == 1)
622 return 1 ;
623 if (subformat == SF_FORMAT_G723_40 && info->channels == 1)
624 return 1 ;
625 break ;
626
627 case SF_FORMAT_CAF :
628 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
629 return 1 ;
630 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
631 return 1 ;
632 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
633 return 1 ;
634 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
635 return 1 ;
636 break ;
637
638 case SF_FORMAT_RAW :
639 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
640 return 1 ;
641 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
642 return 1 ;
643 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
644 return 1 ;
645 if (subformat == SF_FORMAT_ALAW || subformat == SF_FORMAT_ULAW)
646 return 1 ;
647 if ((subformat == SF_FORMAT_DWVW_12 || subformat == SF_FORMAT_DWVW_16 ||
648 subformat == SF_FORMAT_DWVW_24) && info-> channels == 1)
649 return 1 ;
650 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
651 return 1 ;
652 if (subformat == SF_FORMAT_VOX_ADPCM && info->channels == 1)
653 return 1 ;
654 break ;
655
656 case SF_FORMAT_PAF :
657 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24)
658 return 1 ;
659 break ;
660
661 case SF_FORMAT_SVX :
662 /* SVX only supports writing mono SVX files. */
663 if (info->channels > 1)
664 return 0 ;
665 /* Always big endian. */
666 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
667 return 0 ;
668
669 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
670 return 1 ;
671 break ;
672
673 case SF_FORMAT_NIST :
674 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
675 return 1 ;
676 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
677 return 1 ;
678 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
679 return 1 ;
680 break ;
681
682 case SF_FORMAT_IRCAM :
683 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
684 return 1 ;
685 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW || subformat == SF_FORMAT_FLOAT)
686 return 1 ;
687 break ;
688
689 case SF_FORMAT_VOC :
690 /* VOC is strictly little endian. */
691 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
692 return 0 ;
693 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
694 return 1 ;
695 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
696 return 1 ;
697 break ;
698
699 case SF_FORMAT_W64 :
700 /* W64 is strictly little endian. */
701 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
702 return 0 ;
703 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
704 return 1 ;
705 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
706 return 1 ;
707 if ((subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM) && info->channels <= 2)
708 return 1 ;
709 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
710 return 1 ;
711 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
712 return 1 ;
713 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
714 return 1 ;
715 break ;
716
717 case SF_FORMAT_MAT4 :
718 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
719 return 1 ;
720 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
721 return 1 ;
722 break ;
723
724 case SF_FORMAT_MAT5 :
725 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
726 return 1 ;
727 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
728 return 1 ;
729 break ;
730
731 case SF_FORMAT_PVF :
732 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
733 return 1 ;
734 break ;
735
736 case SF_FORMAT_XI :
737 if (info->channels != 1)
738 return 0 ;
739 if (subformat == SF_FORMAT_DPCM_8 || subformat == SF_FORMAT_DPCM_16)
740 return 1 ;
741 break ;
742
743 case SF_FORMAT_HTK :
744 /* HTK is strictly big endian. */
745 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
746 return 0 ;
747 if (info->channels != 1)
748 return 0 ;
749 if (subformat == SF_FORMAT_PCM_16)
750 return 1 ;
751 break ;
752
753 case SF_FORMAT_SDS :
754 /* SDS is strictly big endian. */
755 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
756 return 0 ;
757 if (info->channels != 1)
758 return 0 ;
759 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24)
760 return 1 ;
761 break ;
762
763 case SF_FORMAT_AVR :
764 /* SDS is strictly big endian. */
765 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
766 return 0 ;
767 if (info->channels > 2)
768 return 0 ;
769 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
770 return 1 ;
771 break ;
772
773 case SF_FORMAT_FLAC :
774 /* FLAC can't do more than 8 channels. */
775 if (info->channels > 8)
776 return 0 ;
777 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24)
778 return 1 ;
779 break ;
780
781 case SF_FORMAT_SD2 :
782 /* SD2 is strictly big endian. */
783 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
784 return 0 ;
785 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24)
786 return 1 ;
787 break ;
788
789 case SF_FORMAT_WVE :
790 /* WVE is strictly big endian. */
791 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
792 return 0 ;
793 if (info->channels > 1)
794 return 0 ;
795 if (subformat == SF_FORMAT_ALAW)
796 return 1 ;
797 break ;
798
799 case SF_FORMAT_OGG :
800 if (subformat == SF_FORMAT_VORBIS)
801 return 1 ;
802 break ;
803
804 case SF_FORMAT_MPC2K :
805 /* MPC2000 is strictly little endian. */
806 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
807 return 0 ;
808 if (info->channels > 2)
809 return 0 ;
810 if (subformat == SF_FORMAT_PCM_16)
811 return 1 ;
812 break ;
813
814 case SF_FORMAT_RF64 :
815 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
816 return 0 ;
817 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
818 return 1 ;
819 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
820 return 1 ;
821 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
822 return 1 ;
823 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
824 return 1 ;
825 break ;
826 default : break ;
827 } ;
828
829 return 0 ;
830 } /* sf_format_check */
831
832 /*------------------------------------------------------------------------------
833 */
834
835 const char *
836 sf_version_string (void)
837 {
838 #if ENABLE_EXPERIMENTAL_CODE
839 return PACKAGE_NAME "-" PACKAGE_VERSION "-exp" ;
840 #else
841 return PACKAGE_NAME "-" PACKAGE_VERSION ;
842 #endif
843 }
844
845
846 /*------------------------------------------------------------------------------
847 */
848
849 int
850 sf_command (SNDFILE *sndfile, int command, void *data, int datasize)
851 { SF_PRIVATE *psf = (SF_PRIVATE *) sndfile ;
852 int old_value ;
853
854 /* This set of commands do not need the sndfile parameter. */
855 switch (command)
856 { case SFC_GET_LIB_VERSION :
857 if (data == NULL)
858 { if (psf)
859 psf->error = SFE_BAD_COMMAND_PARAM ;
860 return SFE_BAD_COMMAND_PARAM ;
861 } ;
862 snprintf (data, datasize, "%s", sf_version_string ()) ;
863 return strlen (data) ;
864
865 case SFC_GET_SIMPLE_FORMAT_COUNT :
866 if (data == NULL || datasize != SIGNED_SIZEOF (int))
867 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
868 *((int*) data) = psf_get_format_simple_count () ;
869 return 0 ;
870
871 case SFC_GET_SIMPLE_FORMAT :
872 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
873 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
874 return psf_get_format_simple (data) ;
875
876 case SFC_GET_FORMAT_MAJOR_COUNT :
877 if (data == NULL || datasize != SIGNED_SIZEOF (int))
878 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
879 *((int*) data) = psf_get_format_major_count () ;
880 return 0 ;
881
882 case SFC_GET_FORMAT_MAJOR :
883 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
884 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
885 return psf_get_format_major (data) ;
886
887 case SFC_GET_FORMAT_SUBTYPE_COUNT :
888 if (data == NULL || datasize != SIGNED_SIZEOF (int))
889 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
890 *((int*) data) = psf_get_format_subtype_count () ;
891 return 0 ;
892
893 case SFC_GET_FORMAT_SUBTYPE :
894 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
895 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
896 return psf_get_format_subtype (data) ;
897
898 case SFC_GET_FORMAT_INFO :
899 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
900 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
901 return psf_get_format_info (data) ;
902 } ;
903
904 if (sndfile == NULL && command == SFC_GET_LOG_INFO)
905 { if (data == NULL)
906 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
907 snprintf (data, datasize, "%s", sf_logbuffer) ;
908 return strlen (data) ;
909 } ;
910
911 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
912
913 switch (command)
914 { case SFC_SET_NORM_FLOAT :
915 old_value = psf->norm_float ;
916 psf->norm_float = (datasize) ? SF_TRUE : SF_FALSE ;
917 return old_value ;
918
919 case SFC_GET_CURRENT_SF_INFO :
920 if (data == NULL || datasize != SIGNED_SIZEOF (SF_INFO))
921 return (sf_errno = SFE_BAD_COMMAND_PARAM) ;
922 memcpy (data, &psf->sf, sizeof (SF_INFO)) ;
923 break ;
924
925 case SFC_SET_NORM_DOUBLE :
926 old_value = psf->norm_double ;
927 psf->norm_double = (datasize) ? SF_TRUE : SF_FALSE ;
928 return old_value ;
929
930 case SFC_GET_NORM_FLOAT :
931 return psf->norm_float ;
932
933 case SFC_GET_NORM_DOUBLE :
934 return psf->norm_double ;
935
936 case SFC_SET_SCALE_FLOAT_INT_READ :
937 old_value = psf->float_int_mult ;
938
939 psf->float_int_mult = (datasize != 0) ? SF_TRUE : SF_FALSE ;
940 if (psf->float_int_mult && psf->float_max < 0.0)
941 psf->float_max = psf_calc_signal_max (psf, SF_FALSE) ;
942 return old_value ;
943
944 case SFC_SET_SCALE_INT_FLOAT_WRITE :
945 old_value = psf->scale_int_float ;
946 psf->scale_int_float = (datasize != 0) ? SF_TRUE : SF_FALSE ;
947 return old_value ;
948
949 case SFC_SET_ADD_PEAK_CHUNK :
950 { int format = SF_CONTAINER (psf->sf.format) ;
951
952 /* Only WAV and AIFF support the PEAK chunk. */
953 switch (format)
954 { case SF_FORMAT_AIFF :
955 case SF_FORMAT_CAF :
956 case SF_FORMAT_WAV :
957 case SF_FORMAT_WAVEX :
958 break ;
959
960 default :
961 return SF_FALSE ;
962 } ;
963
964 format = SF_CODEC (psf->sf.format) ;
965
966 /* Only files containg the following data types support the PEAK chunk. */
967 if (format != SF_FORMAT_FLOAT && format != SF_FORMAT_DOUBLE)
968 return SF_FALSE ;
969
970 } ;
971 /* Can only do this is in SFM_WRITE mode. */
972 if (psf->file.mode != SFM_WRITE && psf->file.mode != SFM_RDWR)
973 return SF_FALSE ;
974 /* If data has already been written this must fail. */
975 if (psf->have_written)
976 { psf->error = SFE_CMD_HAS_DATA ;
977 return SF_FALSE ;
978 } ;
979 /* Everything seems OK, so set psf->has_peak and re-write header. */
980 if (datasize == SF_FALSE && psf->peak_info != NULL)
981 { free (psf->peak_info) ;
982 psf->peak_info = NULL ;
983 }
984 else if (psf->peak_info == NULL)
985 { psf->peak_info = peak_info_calloc (psf->sf.channels) ;
986 if (psf->peak_info != NULL)
987 psf->peak_info->peak_loc = SF_PEAK_START ;
988 } ;
989
990 if (psf->write_header)
991 psf->write_header (psf, SF_TRUE) ;
992 return datasize ;
993
994 case SFC_SET_ADD_HEADER_PAD_CHUNK :
995 return SF_FALSE ;
996
997 case SFC_GET_LOG_INFO :
998 if (data == NULL)
999 return SFE_BAD_COMMAND_PARAM ;
1000 snprintf (data, datasize, "%s", psf->logbuffer) ;
1001 break ;
1002
1003 case SFC_CALC_SIGNAL_MAX :
1004 if (data == NULL || datasize != sizeof (double))
1005 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1006 *((double*) data) = psf_calc_signal_max (psf, SF_FALSE) ;
1007 break ;
1008
1009 case SFC_CALC_NORM_SIGNAL_MAX :
1010 if (data == NULL || datasize != sizeof (double))
1011 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1012 *((double*) data) = psf_calc_signal_max (psf, SF_TRUE) ;
1013 break ;
1014
1015 case SFC_CALC_MAX_ALL_CHANNELS :
1016 if (data == NULL || datasize != SIGNED_SIZEOF (double) * psf->sf.channels)
1017 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1018 return psf_calc_max_all_channels (psf, (double*) data, SF_FALSE) ;
1019
1020 case SFC_CALC_NORM_MAX_ALL_CHANNELS :
1021 if (data == NULL || datasize != SIGNED_SIZEOF (double) * psf->sf.channels)
1022 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1023 return psf_calc_max_all_channels (psf, (double*) data, SF_TRUE) ;
1024
1025 case SFC_GET_SIGNAL_MAX :
1026 if (data == NULL || datasize != sizeof (double))
1027 { psf->error = SFE_BAD_COMMAND_PARAM ;
1028 return SF_FALSE ;
1029 } ;
1030 return psf_get_signal_max (psf, (double *) data) ;
1031
1032 case SFC_GET_MAX_ALL_CHANNELS :
1033 if (data == NULL || datasize != SIGNED_SIZEOF (double) * psf->sf.channels)
1034 { psf->error = SFE_BAD_COMMAND_PARAM ;
1035 return SF_FALSE ;
1036 } ;
1037 return psf_get_max_all_channels (psf, (double*) data) ;
1038
1039 case SFC_UPDATE_HEADER_NOW :
1040 if (psf->write_header)
1041 psf->write_header (psf, SF_TRUE) ;
1042 break ;
1043
1044 case SFC_SET_UPDATE_HEADER_AUTO :
1045 psf->auto_header = datasize ? SF_TRUE : SF_FALSE ;
1046 return psf->auto_header ;
1047 break ;
1048
1049 case SFC_SET_ADD_DITHER_ON_WRITE :
1050 case SFC_SET_ADD_DITHER_ON_READ :
1051 /*
1052 ** FIXME !
1053 ** These are obsolete. Just return.
1054 ** Remove some time after version 1.0.8.
1055 */
1056 break ;
1057
1058 case SFC_SET_DITHER_ON_WRITE :
1059 if (data == NULL || datasize != SIGNED_SIZEOF (SF_DITHER_INFO))
1060 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1061 memcpy (&psf->write_dither, data, sizeof (psf->write_dither)) ;
1062 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
1063 dither_init (psf, SFM_WRITE) ;
1064 break ;
1065
1066 case SFC_SET_DITHER_ON_READ :
1067 if (data == NULL || datasize != SIGNED_SIZEOF (SF_DITHER_INFO))
1068 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1069 memcpy (&psf->read_dither, data, sizeof (psf->read_dither)) ;
1070 if (psf->file.mode == SFM_READ || psf->file.mode == SFM_RDWR)
1071 dither_init (psf, SFM_READ) ;
1072 break ;
1073
1074 case SFC_FILE_TRUNCATE :
1075 if (psf->file.mode != SFM_WRITE && psf->file.mode != SFM_RDWR)
1076 return SF_TRUE ;
1077 if (datasize != sizeof (sf_count_t))
1078 return SF_TRUE ;
1079 if (data == NULL || datasize != sizeof (sf_count_t))
1080 { psf->error = SFE_BAD_COMMAND_PARAM ;
1081 return SF_FALSE ;
1082 }
1083 else
1084 { sf_count_t position ;
1085
1086 position = *((sf_count_t*) data) ;
1087
1088 if (sf_seek (sndfile, position, SEEK_SET) != position)
1089 return SF_TRUE ;
1090
1091 psf->sf.frames = position ;
1092
1093 position = psf_fseek (psf, 0, SEEK_CUR) ;
1094
1095 return psf_ftruncate (psf, position) ;
1096 } ;
1097 break ;
1098
1099 case SFC_SET_RAW_START_OFFSET :
1100 if (data == NULL || datasize != sizeof (sf_count_t))
1101 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1102
1103 if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_RAW)
1104 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1105
1106 psf->dataoffset = *((sf_count_t*) data) ;
1107 sf_seek (sndfile, 0, SEEK_CUR) ;
1108 break ;
1109
1110 case SFC_GET_EMBED_FILE_INFO :
1111 if (data == NULL || datasize != sizeof (SF_EMBED_FILE_INFO))
1112 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1113
1114 ((SF_EMBED_FILE_INFO*) data)->offset = psf->fileoffset ;
1115 ((SF_EMBED_FILE_INFO*) data)->length = psf->filelength ;
1116 break ;
1117
1118 /* Lite remove start */
1119 case SFC_TEST_IEEE_FLOAT_REPLACE :
1120 psf->ieee_replace = (datasize) ? SF_TRUE : SF_FALSE ;
1121 if ((SF_CODEC (psf->sf.format)) == SF_FORMAT_FLOAT)
1122 float32_init (psf) ;
1123 else if ((SF_CODEC (psf->sf.format)) == SF_FORMAT_DOUBLE)
1124 double64_init (psf) ;
1125 else
1126 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1127 break ;
1128 /* Lite remove end */
1129
1130 case SFC_SET_CLIPPING :
1131 psf->add_clipping = (datasize) ? SF_TRUE : SF_FALSE ;
1132 return psf->add_clipping ;
1133
1134 case SFC_GET_CLIPPING :
1135 return psf->add_clipping ;
1136
1137 case SFC_GET_LOOP_INFO :
1138 if (datasize != sizeof (SF_LOOP_INFO) || data == NULL)
1139 { psf->error = SFE_BAD_COMMAND_PARAM ;
1140 return SF_FALSE ;
1141 } ;
1142 if (psf->loop_info == NULL)
1143 return SF_FALSE ;
1144 memcpy (data, psf->loop_info, sizeof (SF_LOOP_INFO)) ;
1145 return SF_TRUE ;
1146
1147 case SFC_SET_BROADCAST_INFO :
1148 { int format = SF_CONTAINER (psf->sf.format) ;
1149
1150 /* Only WAV and RF64 supports the BEXT (Broadcast) chunk. */
1151 if (format != SF_FORMAT_WAV && format != SF_FORMAT_WAVEX && format != SF_FORMAT_RF64)
1152 return SF_FALSE ;
1153 } ;
1154
1155 /* Only makes sense in SFM_WRITE or SFM_RDWR mode. */
1156 if ((psf->file.mode != SFM_WRITE) && (psf->file.mode != SFM_RDWR))
1157 return SF_FALSE ;
1158 /* If data has already been written this must fail. */
1159 if (psf->broadcast_16k == NULL && psf->have_written)
1160 { psf->error = SFE_CMD_HAS_DATA ;
1161 return SF_FALSE ;
1162 } ;
1163
1164 #if 0
1165 if (psf->broadcast_info == NULL)
1166 psf->broadcast_info = broadcast_info_alloc () ;
1167
1168 broadcast_info_copy (psf->broadcast_info, data) ;
1169 broadcast_add_coding_history (psf->broadcast_info, psf->sf.channels, psf->sf.samplerate, psf->sf.format) ;
1170 #else
1171 broadcast_var_set (psf, data, datasize) ;
1172 #endif
1173
1174 if (psf->write_header)
1175 psf->write_header (psf, SF_TRUE) ;
1176 return SF_TRUE ;
1177
1178 case SFC_GET_BROADCAST_INFO :
1179 if (data == NULL)
1180 { psf->error = SFE_BAD_COMMAND_PARAM ;
1181 return SF_FALSE ;
1182 } ;
1183 #if 0
1184 if (psf->broadcast_info == NULL)
1185 return SF_FALSE ;
1186 return broadcast_info_copy (data, psf->broadcast_info) ;
1187 #else
1188 return broadcast_var_get (psf, data, datasize) ;
1189 #endif
1190
1191 case SFC_GET_INSTRUMENT :
1192 if (datasize != sizeof (SF_INSTRUMENT) || data == NULL)
1193 { psf->error = SFE_BAD_COMMAND_PARAM ;
1194 return SF_FALSE ;
1195 } ;
1196 if (psf->instrument == NULL)
1197 return SF_FALSE ;
1198 memcpy (data, psf->instrument, sizeof (SF_INSTRUMENT)) ;
1199 return SF_TRUE ;
1200
1201 case SFC_SET_INSTRUMENT :
1202 /* If data has already been written this must fail. */
1203 if (psf->have_written)
1204 { psf->error = SFE_CMD_HAS_DATA ;
1205 return SF_FALSE ;
1206 } ;
1207 if (datasize != sizeof (SF_INSTRUMENT) || data == NULL)
1208 { psf->error = SFE_BAD_COMMAND_PARAM ;
1209 return SF_FALSE ;
1210 } ;
1211
1212 if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
1213 { psf->error = SFE_MALLOC_FAILED ;
1214 return SF_FALSE ;
1215 } ;
1216 memcpy (psf->instrument, data, sizeof (SF_INSTRUMENT)) ;
1217 return SF_TRUE ;
1218
1219 case SFC_RAW_DATA_NEEDS_ENDSWAP :
1220 return psf->data_endswap ;
1221
1222 case SFC_GET_CHANNEL_MAP_INFO :
1223 if (psf->channel_map == NULL)
1224 return SF_FALSE ;
1225
1226 if (data == NULL || datasize != SIGNED_SIZEOF (psf->channel_map [0]) * psf->sf.channels)
1227 { psf->error = SFE_BAD_COMMAND_PARAM ;
1228 return SF_FALSE ;
1229 } ;
1230
1231 memcpy (data, psf->channel_map, datasize) ;
1232 return SF_TRUE ;
1233
1234 case SFC_SET_CHANNEL_MAP_INFO :
1235 if (psf->have_written)
1236 { psf->error = SFE_CMD_HAS_DATA ;
1237 return SF_FALSE ;
1238 } ;
1239 if (data == NULL || datasize != SIGNED_SIZEOF (psf->channel_map [0]) * psf->sf.channels)
1240 { psf->error = SFE_BAD_COMMAND_PARAM ;
1241 return SF_FALSE ;
1242 } ;
1243
1244 { int *iptr ;
1245
1246 for (iptr = data ; iptr < (int*) data + psf->sf.channels ; iptr++)
1247 { if (*iptr <= SF_CHANNEL_MAP_INVALID || *iptr >= SF_CHANNEL_MAP_MAX)
1248 { psf->error = SFE_BAD_COMMAND_PARAM ;
1249 return SF_FALSE ;
1250 } ;
1251 } ;
1252 } ;
1253
1254 free (psf->channel_map) ;
1255 if ((psf->channel_map = malloc (datasize)) == NULL)
1256 { psf->error = SFE_MALLOC_FAILED ;
1257 return SF_FALSE ;
1258 } ;
1259
1260 memcpy (psf->channel_map, data, datasize) ;
1261
1262 /*
1263 ** Pass the command down to the container's command handler.
1264 ** Don't pass user data, use validated psf->channel_map data instead.
1265 */
1266 if (psf->command)
1267 return psf->command (psf, command, NULL, 0) ;
1268 return SF_FALSE ;
1269
1270 default :
1271 /* Must be a file specific command. Pass it on. */
1272 if (psf->command)
1273 return psf->command (psf, command, data, datasize) ;
1274
1275 psf_log_printf (psf, "*** sf_command : cmd = 0x%X\n", command) ;
1276 return (psf->error = SFE_BAD_COMMAND_PARAM) ;
1277 } ;
1278
1279 return 0 ;
1280 } /* sf_command */
1281
1282 /*------------------------------------------------------------------------------
1283 */
1284
1285 sf_count_t
1286 sf_seek (SNDFILE *sndfile, sf_count_t offset, int whence)
1287 { SF_PRIVATE *psf ;
1288 sf_count_t seek_from_start = 0, retval ;
1289
1290 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1291
1292 if (! psf->sf.seekable)
1293 { psf->error = SFE_NOT_SEEKABLE ;
1294 return PSF_SEEK_ERROR ;
1295 } ;
1296
1297 /* If the whence parameter has a mode ORed in, check to see that
1298 ** it makes sense.
1299 */
1300 if (((whence & SFM_MASK) == SFM_WRITE && psf->file.mode == SFM_READ) ||
1301 ((whence & SFM_MASK) == SFM_READ && psf->file.mode == SFM_WRITE))
1302 { psf->error = SFE_WRONG_SEEK ;
1303 return PSF_SEEK_ERROR ;
1304 } ;
1305
1306 /* Convert all SEEK_CUR and SEEK_END into seek_from_start to be
1307 ** used with SEEK_SET.
1308 */
1309 switch (whence)
1310 { /* The SEEK_SET behaviour is independant of mode. */
1311 case SEEK_SET :
1312 case SEEK_SET | SFM_READ :
1313 case SEEK_SET | SFM_WRITE :
1314 case SEEK_SET | SFM_RDWR :
1315 seek_from_start = offset ;
1316 break ;
1317
1318 /* The SEEK_CUR is a little more tricky. */
1319 case SEEK_CUR :
1320 if (offset == 0)
1321 { if (psf->file.mode == SFM_READ)
1322 return psf->read_current ;
1323 if (psf->file.mode == SFM_WRITE)
1324 return psf->write_current ;
1325 } ;
1326 if (psf->file.mode == SFM_READ)
1327 seek_from_start = psf->read_current + offset ;
1328 else if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
1329 seek_from_start = psf->write_current + offset ;
1330 else
1331 psf->error = SFE_AMBIGUOUS_SEEK ;
1332 break ;
1333
1334 case SEEK_CUR | SFM_READ :
1335 if (offset == 0)
1336 return psf->read_current ;
1337 seek_from_start = psf->read_current + offset ;
1338 break ;
1339
1340 case SEEK_CUR | SFM_WRITE :
1341 if (offset == 0)
1342 return psf->write_current ;
1343 seek_from_start = psf->write_current + offset ;
1344 break ;
1345
1346 /* The SEEK_END */
1347 case SEEK_END :
1348 case SEEK_END | SFM_READ :
1349 case SEEK_END | SFM_WRITE :
1350 seek_from_start = psf->sf.frames + offset ;
1351 break ;
1352
1353 default :
1354 psf->error = SFE_BAD_SEEK ;
1355 break ;
1356 } ;
1357
1358 if (psf->error)
1359 return PSF_SEEK_ERROR ;
1360
1361 if (psf->file.mode == SFM_RDWR || psf->file.mode == SFM_WRITE)
1362 { if (seek_from_start < 0)
1363 { psf->error = SFE_BAD_SEEK ;
1364 return PSF_SEEK_ERROR ;
1365 } ;
1366 }
1367 else if (seek_from_start < 0 || seek_from_start > psf->sf.frames)
1368 { psf->error = SFE_BAD_SEEK ;
1369 return PSF_SEEK_ERROR ;
1370 } ;
1371
1372 if (psf->seek)
1373 { int new_mode = (whence & SFM_MASK) ? (whence & SFM_MASK) : psf->file.mode ;
1374
1375 retval = psf->seek (psf, new_mode, seek_from_start) ;
1376
1377 switch (new_mode)
1378 { case SFM_READ :
1379 psf->read_current = retval ;
1380 break ;
1381 case SFM_WRITE :
1382 psf->write_current = retval ;
1383 break ;
1384 case SFM_RDWR :
1385 psf->read_current = retval ;
1386 psf->write_current = retval ;
1387 new_mode = SFM_READ ;
1388 break ;
1389 } ;
1390
1391 psf->last_op = new_mode ;
1392
1393 return retval ;
1394 } ;
1395
1396 psf->error = SFE_AMBIGUOUS_SEEK ;
1397 return PSF_SEEK_ERROR ;
1398 } /* sf_seek */
1399
1400 /*------------------------------------------------------------------------------
1401 */
1402
1403 const char*
1404 sf_get_string (SNDFILE *sndfile, int str_type)
1405 { SF_PRIVATE *psf ;
1406
1407 if ((psf = (SF_PRIVATE*) sndfile) == NULL)
1408 return NULL ;
1409 if (psf->Magick != SNDFILE_MAGICK)
1410 return NULL ;
1411
1412 return psf_get_string (psf, str_type) ;
1413 } /* sf_get_string */
1414
1415 int
1416 sf_set_string (SNDFILE *sndfile, int str_type, const char* str)
1417 { SF_PRIVATE *psf ;
1418
1419 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1420
1421 return psf_set_string (psf, str_type, str) ;
1422 } /* sf_get_string */
1423
1424 /*==============================================================================
1425 */
1426
1427 sf_count_t
1428 sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes)
1429 { SF_PRIVATE *psf ;
1430 sf_count_t count, extra ;
1431 int bytewidth, blockwidth ;
1432
1433 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1434
1435 bytewidth = (psf->bytewidth > 0) ? psf->bytewidth : 1 ;
1436 blockwidth = (psf->blockwidth > 0) ? psf->blockwidth : 1 ;
1437
1438 if (psf->file.mode == SFM_WRITE)
1439 { psf->error = SFE_NOT_READMODE ;
1440 return 0 ;
1441 } ;
1442
1443 if (bytes < 0 || psf->read_current >= psf->sf.frames)
1444 { psf_memset (ptr, 0, bytes) ;
1445 return 0 ;
1446 } ;
1447
1448 if (bytes % (psf->sf.channels * bytewidth))
1449 { psf->error = SFE_BAD_READ_ALIGN ;
1450 return 0 ;
1451 } ;
1452
1453 if (psf->last_op != SFM_READ)
1454 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1455 return 0 ;
1456
1457 count = psf_fread (ptr, 1, bytes, psf) ;
1458
1459 if (psf->read_current + count / blockwidth <= psf->sf.frames)
1460 psf->read_current += count / blockwidth ;
1461 else
1462 { count = (psf->sf.frames - psf->read_current) * blockwidth ;
1463 extra = bytes - count ;
1464 psf_memset (((char *) ptr) + count, 0, extra) ;
1465 psf->read_current = psf->sf.frames ;
1466 } ;
1467
1468 psf->last_op = SFM_READ ;
1469
1470 return count ;
1471 } /* sf_read_raw */
1472
1473 /*------------------------------------------------------------------------------
1474 */
1475
1476 sf_count_t
1477 sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t len)
1478 { SF_PRIVATE *psf ;
1479 sf_count_t count, extra ;
1480
1481 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1482
1483 if (psf->file.mode == SFM_WRITE)
1484 { psf->error = SFE_NOT_READMODE ;
1485 return 0 ;
1486 } ;
1487
1488 if (len % psf->sf.channels)
1489 { psf->error = SFE_BAD_READ_ALIGN ;
1490 return 0 ;
1491 } ;
1492
1493 if (len <= 0 || psf->read_current >= psf->sf.frames)
1494 { psf_memset (ptr, 0, len * sizeof (short)) ;
1495 return 0 ; /* End of file. */
1496 } ;
1497
1498 if (psf->read_short == NULL || psf->seek == NULL)
1499 { psf->error = SFE_UNIMPLEMENTED ;
1500 return 0 ;
1501 } ;
1502
1503 if (psf->last_op != SFM_READ)
1504 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1505 return 0 ;
1506
1507 count = psf->read_short (psf, ptr, len) ;
1508
1509 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1510 psf->read_current += count / psf->sf.channels ;
1511 else
1512 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1513 extra = len - count ;
1514 psf_memset (ptr + count, 0, extra * sizeof (short)) ;
1515 psf->read_current = psf->sf.frames ;
1516 } ;
1517
1518 psf->last_op = SFM_READ ;
1519
1520 return count ;
1521 } /* sf_read_short */
1522
1523 sf_count_t
1524 sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames)
1525 { SF_PRIVATE *psf ;
1526 sf_count_t count, extra ;
1527
1528 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1529
1530 if (psf->file.mode == SFM_WRITE)
1531 { psf->error = SFE_NOT_READMODE ;
1532 return 0 ;
1533 } ;
1534
1535 if (frames <= 0 || psf->read_current >= psf->sf.frames)
1536 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (short)) ;
1537 return 0 ; /* End of file. */
1538 } ;
1539
1540 if (psf->read_short == NULL || psf->seek == NULL)
1541 { psf->error = SFE_UNIMPLEMENTED ;
1542 return 0 ;
1543 } ;
1544
1545 if (psf->last_op != SFM_READ)
1546 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1547 return 0 ;
1548
1549 count = psf->read_short (psf, ptr, frames * psf->sf.channels) ;
1550
1551 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1552 psf->read_current += count / psf->sf.channels ;
1553 else
1554 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1555 extra = frames * psf->sf.channels - count ;
1556 psf_memset (ptr + count, 0, extra * sizeof (short)) ;
1557 psf->read_current = psf->sf.frames ;
1558 } ;
1559
1560 psf->last_op = SFM_READ ;
1561
1562 return count / psf->sf.channels ;
1563 } /* sf_readf_short */
1564
1565 /*------------------------------------------------------------------------------
1566 */
1567
1568 sf_count_t
1569 sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t len)
1570 { SF_PRIVATE *psf ;
1571 sf_count_t count, extra ;
1572
1573 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1574
1575 if (psf->file.mode == SFM_WRITE)
1576 { psf->error = SFE_NOT_READMODE ;
1577 return 0 ;
1578 } ;
1579
1580 if (len % psf->sf.channels)
1581 { psf->error = SFE_BAD_READ_ALIGN ;
1582 return 0 ;
1583 } ;
1584
1585 if (len <= 0 || psf->read_current >= psf->sf.frames)
1586 { psf_memset (ptr, 0, len * sizeof (int)) ;
1587 return 0 ;
1588 } ;
1589
1590 if (psf->read_int == NULL || psf->seek == NULL)
1591 { psf->error = SFE_UNIMPLEMENTED ;
1592 return 0 ;
1593 } ;
1594
1595 if (psf->last_op != SFM_READ)
1596 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1597 return 0 ;
1598
1599 count = psf->read_int (psf, ptr, len) ;
1600
1601 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1602 psf->read_current += count / psf->sf.channels ;
1603 else
1604 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1605 extra = len - count ;
1606 psf_memset (ptr + count, 0, extra * sizeof (int)) ;
1607 psf->read_current = psf->sf.frames ;
1608 } ;
1609
1610 psf->last_op = SFM_READ ;
1611
1612 return count ;
1613 } /* sf_read_int */
1614
1615 sf_count_t
1616 sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames)
1617 { SF_PRIVATE *psf ;
1618 sf_count_t count, extra ;
1619
1620 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1621
1622 if (psf->file.mode == SFM_WRITE)
1623 { psf->error = SFE_NOT_READMODE ;
1624 return 0 ;
1625 } ;
1626
1627 if (frames <= 0 || psf->read_current >= psf->sf.frames)
1628 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (int)) ;
1629 return 0 ;
1630 } ;
1631
1632 if (psf->read_int == NULL || psf->seek == NULL)
1633 { psf->error = SFE_UNIMPLEMENTED ;
1634 return 0 ;
1635 } ;
1636
1637 if (psf->last_op != SFM_READ)
1638 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1639 return 0 ;
1640
1641 count = psf->read_int (psf, ptr, frames * psf->sf.channels) ;
1642
1643 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1644 psf->read_current += count / psf->sf.channels ;
1645 else
1646 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1647 extra = frames * psf->sf.channels - count ;
1648 psf_memset (ptr + count, 0, extra * sizeof (int)) ;
1649 psf->read_current = psf->sf.frames ;
1650 } ;
1651
1652 psf->last_op = SFM_READ ;
1653
1654 return count / psf->sf.channels ;
1655 } /* sf_readf_int */
1656
1657 /*------------------------------------------------------------------------------
1658 */
1659
1660 sf_count_t
1661 sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t len)
1662 { SF_PRIVATE *psf ;
1663 sf_count_t count, extra ;
1664
1665 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1666
1667 if (psf->file.mode == SFM_WRITE)
1668 { psf->error = SFE_NOT_READMODE ;
1669 return 0 ;
1670 } ;
1671
1672 if (len % psf->sf.channels)
1673 { psf->error = SFE_BAD_READ_ALIGN ;
1674 return 0 ;
1675 } ;
1676
1677 if (len <= 0 || psf->read_current >= psf->sf.frames)
1678 { psf_memset (ptr, 0, len * sizeof (float)) ;
1679 return 0 ;
1680 } ;
1681
1682 if (psf->read_float == NULL || psf->seek == NULL)
1683 { psf->error = SFE_UNIMPLEMENTED ;
1684 return 0 ;
1685 } ;
1686
1687 if (psf->last_op != SFM_READ)
1688 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1689 return 0 ;
1690
1691 count = psf->read_float (psf, ptr, len) ;
1692
1693 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1694 psf->read_current += count / psf->sf.channels ;
1695 else
1696 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1697 extra = len - count ;
1698 psf_memset (ptr + count, 0, extra * sizeof (float)) ;
1699 psf->read_current = psf->sf.frames ;
1700 } ;
1701
1702 psf->last_op = SFM_READ ;
1703
1704 return count ;
1705 } /* sf_read_float */
1706
1707 sf_count_t
1708 sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames)
1709 { SF_PRIVATE *psf ;
1710 sf_count_t count, extra ;
1711
1712 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1713
1714 if (psf->file.mode == SFM_WRITE)
1715 { psf->error = SFE_NOT_READMODE ;
1716 return 0 ;
1717 } ;
1718
1719 if (frames <= 0 || psf->read_current >= psf->sf.frames)
1720 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (float)) ;
1721 return 0 ;
1722 } ;
1723
1724 if (psf->read_float == NULL || psf->seek == NULL)
1725 { psf->error = SFE_UNIMPLEMENTED ;
1726 return 0 ;
1727 } ;
1728
1729 if (psf->last_op != SFM_READ)
1730 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1731 return 0 ;
1732
1733 count = psf->read_float (psf, ptr, frames * psf->sf.channels) ;
1734
1735 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1736 psf->read_current += count / psf->sf.channels ;
1737 else
1738 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1739 extra = frames * psf->sf.channels - count ;
1740 psf_memset (ptr + count, 0, extra * sizeof (float)) ;
1741 psf->read_current = psf->sf.frames ;
1742 } ;
1743
1744 psf->last_op = SFM_READ ;
1745
1746 return count / psf->sf.channels ;
1747 } /* sf_readf_float */
1748
1749 /*------------------------------------------------------------------------------
1750 */
1751
1752 sf_count_t
1753 sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t len)
1754 { SF_PRIVATE *psf ;
1755 sf_count_t count, extra ;
1756
1757 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1758
1759 if (psf->file.mode == SFM_WRITE)
1760 { psf->error = SFE_NOT_READMODE ;
1761 return 0 ;
1762 } ;
1763
1764 if (len % psf->sf.channels)
1765 { psf->error = SFE_BAD_READ_ALIGN ;
1766 return 0 ;
1767 } ;
1768
1769 if (len <= 0 || psf->read_current >= psf->sf.frames)
1770 { psf_memset (ptr, 0, len * sizeof (double)) ;
1771 return 0 ;
1772 } ;
1773
1774 if (psf->read_double == NULL || psf->seek == NULL)
1775 { psf->error = SFE_UNIMPLEMENTED ;
1776 return 0 ;
1777 } ;
1778
1779 if (psf->last_op != SFM_READ)
1780 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1781 return 0 ;
1782
1783 count = psf->read_double (psf, ptr, len) ;
1784
1785 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1786 psf->read_current += count / psf->sf.channels ;
1787 else
1788 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1789 extra = len - count ;
1790 psf_memset (ptr + count, 0, extra * sizeof (double)) ;
1791 psf->read_current = psf->sf.frames ;
1792 } ;
1793
1794 psf->last_op = SFM_READ ;
1795
1796 return count ;
1797 } /* sf_read_double */
1798
1799 sf_count_t
1800 sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames)
1801 { SF_PRIVATE *psf ;
1802 sf_count_t count, extra ;
1803
1804 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1805
1806 if (psf->file.mode == SFM_WRITE)
1807 { psf->error = SFE_NOT_READMODE ;
1808 return 0 ;
1809 } ;
1810
1811 if (frames <= 0 || psf->read_current >= psf->sf.frames)
1812 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (double)) ;
1813 return 0 ;
1814 } ;
1815
1816 if (psf->read_double == NULL || psf->seek == NULL)
1817 { psf->error = SFE_UNIMPLEMENTED ;
1818 return 0 ;
1819 } ;
1820
1821 if (psf->last_op != SFM_READ)
1822 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
1823 return 0 ;
1824
1825 count = psf->read_double (psf, ptr, frames * psf->sf.channels) ;
1826
1827 if (psf->read_current + count / psf->sf.channels <= psf->sf.frames)
1828 psf->read_current += count / psf->sf.channels ;
1829 else
1830 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
1831 extra = frames * psf->sf.channels - count ;
1832 psf_memset (ptr + count, 0, extra * sizeof (double)) ;
1833 psf->read_current = psf->sf.frames ;
1834 } ;
1835
1836 psf->last_op = SFM_READ ;
1837
1838 return count / psf->sf.channels ;
1839 } /* sf_readf_double */
1840
1841 /*------------------------------------------------------------------------------
1842 */
1843
1844 sf_count_t
1845 sf_write_raw (SNDFILE *sndfile, const void *ptr, sf_count_t len)
1846 { SF_PRIVATE *psf ;
1847 sf_count_t count ;
1848 int bytewidth, blockwidth ;
1849
1850 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1851
1852 bytewidth = (psf->bytewidth > 0) ? psf->bytewidth : 1 ;
1853 blockwidth = (psf->blockwidth > 0) ? psf->blockwidth : 1 ;
1854
1855 if (psf->file.mode == SFM_READ)
1856 { psf->error = SFE_NOT_WRITEMODE ;
1857 return 0 ;
1858 } ;
1859
1860 if (len % (psf->sf.channels * bytewidth))
1861 { psf->error = SFE_BAD_WRITE_ALIGN ;
1862 return 0 ;
1863 } ;
1864
1865 if (psf->last_op != SFM_WRITE)
1866 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
1867 return 0 ;
1868
1869 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
1870 psf->write_header (psf, SF_FALSE) ;
1871 psf->have_written = SF_TRUE ;
1872
1873 count = psf_fwrite (ptr, 1, len, psf) ;
1874
1875 psf->write_current += count / blockwidth ;
1876
1877 psf->last_op = SFM_WRITE ;
1878
1879 if (psf->auto_header && psf->write_header != NULL)
1880 psf->write_header (psf, SF_TRUE) ;
1881
1882 if (psf->write_current > psf->sf.frames)
1883 psf->sf.frames = psf->write_current ;
1884
1885 return count ;
1886 } /* sf_write_raw */
1887
1888 /*------------------------------------------------------------------------------
1889 */
1890
1891 sf_count_t
1892 sf_write_short (SNDFILE *sndfile, const short *ptr, sf_count_t len)
1893 { SF_PRIVATE *psf ;
1894 sf_count_t count ;
1895
1896 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1897
1898 if (psf->file.mode == SFM_READ)
1899 { psf->error = SFE_NOT_WRITEMODE ;
1900 return 0 ;
1901 } ;
1902
1903 if (len % psf->sf.channels)
1904 { psf->error = SFE_BAD_WRITE_ALIGN ;
1905 return 0 ;
1906 } ;
1907
1908 if (psf->write_short == NULL || psf->seek == NULL)
1909 { psf->error = SFE_UNIMPLEMENTED ;
1910 return 0 ;
1911 } ;
1912
1913 if (psf->last_op != SFM_WRITE)
1914 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
1915 return 0 ;
1916
1917 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
1918 psf->write_header (psf, SF_FALSE) ;
1919 psf->have_written = SF_TRUE ;
1920
1921 count = psf->write_short (psf, ptr, len) ;
1922
1923 psf->write_current += count / psf->sf.channels ;
1924
1925 psf->last_op = SFM_WRITE ;
1926
1927 if (psf->auto_header && psf->write_header != NULL)
1928 psf->write_header (psf, SF_TRUE) ;
1929
1930 if (psf->write_current > psf->sf.frames)
1931 psf->sf.frames = psf->write_current ;
1932
1933 return count ;
1934 } /* sf_write_short */
1935
1936 sf_count_t
1937 sf_writef_short (SNDFILE *sndfile, const short *ptr, sf_count_t frames)
1938 { SF_PRIVATE *psf ;
1939 sf_count_t count ;
1940
1941 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1942
1943 if (psf->file.mode == SFM_READ)
1944 { psf->error = SFE_NOT_WRITEMODE ;
1945 return 0 ;
1946 } ;
1947
1948 if (psf->write_short == NULL || psf->seek == NULL)
1949 { psf->error = SFE_UNIMPLEMENTED ;
1950 return 0 ;
1951 } ;
1952
1953 if (psf->last_op != SFM_WRITE)
1954 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
1955 return 0 ;
1956
1957 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
1958 psf->write_header (psf, SF_FALSE) ;
1959 psf->have_written = SF_TRUE ;
1960
1961 count = psf->write_short (psf, ptr, frames * psf->sf.channels) ;
1962
1963 psf->write_current += count / psf->sf.channels ;
1964
1965 psf->last_op = SFM_WRITE ;
1966
1967 if (psf->auto_header && psf->write_header != NULL)
1968 psf->write_header (psf, SF_TRUE) ;
1969
1970 if (psf->write_current > psf->sf.frames)
1971 psf->sf.frames = psf->write_current ;
1972
1973 return count / psf->sf.channels ;
1974 } /* sf_writef_short */
1975
1976 /*------------------------------------------------------------------------------
1977 */
1978
1979 sf_count_t
1980 sf_write_int (SNDFILE *sndfile, const int *ptr, sf_count_t len)
1981 { SF_PRIVATE *psf ;
1982 sf_count_t count ;
1983
1984 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
1985
1986 if (psf->file.mode == SFM_READ)
1987 { psf->error = SFE_NOT_WRITEMODE ;
1988 return 0 ;
1989 } ;
1990
1991 if (len % psf->sf.channels)
1992 { psf->error = SFE_BAD_WRITE_ALIGN ;
1993 return 0 ;
1994 } ;
1995
1996 if (psf->write_int == NULL || psf->seek == NULL)
1997 { psf->error = SFE_UNIMPLEMENTED ;
1998 return 0 ;
1999 } ;
2000
2001 if (psf->last_op != SFM_WRITE)
2002 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2003 return 0 ;
2004
2005 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
2006 psf->write_header (psf, SF_FALSE) ;
2007 psf->have_written = SF_TRUE ;
2008
2009 count = psf->write_int (psf, ptr, len) ;
2010
2011 psf->write_current += count / psf->sf.channels ;
2012
2013 psf->last_op = SFM_WRITE ;
2014
2015 if (psf->auto_header && psf->write_header != NULL)
2016 psf->write_header (psf, SF_TRUE) ;
2017
2018 if (psf->write_current > psf->sf.frames)
2019 psf->sf.frames = psf->write_current ;
2020
2021 return count ;
2022 } /* sf_write_int */
2023
2024 sf_count_t
2025 sf_writef_int (SNDFILE *sndfile, const int *ptr, sf_count_t frames)
2026 { SF_PRIVATE *psf ;
2027 sf_count_t count ;
2028
2029 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
2030
2031 if (psf->file.mode == SFM_READ)
2032 { psf->error = SFE_NOT_WRITEMODE ;
2033 return 0 ;
2034 } ;
2035
2036 if (psf->write_int == NULL || psf->seek == NULL)
2037 { psf->error = SFE_UNIMPLEMENTED ;
2038 return 0 ;
2039 } ;
2040
2041 if (psf->last_op != SFM_WRITE)
2042 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2043 return 0 ;
2044
2045 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
2046 psf->write_header (psf, SF_FALSE) ;
2047 psf->have_written = SF_TRUE ;
2048
2049 count = psf->write_int (psf, ptr, frames * psf->sf.channels) ;
2050
2051 psf->write_current += count / psf->sf.channels ;
2052
2053 psf->last_op = SFM_WRITE ;
2054
2055 if (psf->auto_header && psf->write_header != NULL)
2056 psf->write_header (psf, SF_TRUE) ;
2057
2058 if (psf->write_current > psf->sf.frames)
2059 psf->sf.frames = psf->write_current ;
2060
2061 return count / psf->sf.channels ;
2062 } /* sf_writef_int */
2063
2064 /*------------------------------------------------------------------------------
2065 */
2066
2067 sf_count_t
2068 sf_write_float (SNDFILE *sndfile, const float *ptr, sf_count_t len)
2069 { SF_PRIVATE *psf ;
2070 sf_count_t count ;
2071
2072 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
2073
2074 if (psf->file.mode == SFM_READ)
2075 { psf->error = SFE_NOT_WRITEMODE ;
2076 return 0 ;
2077 } ;
2078
2079 if (len % psf->sf.channels)
2080 { psf->error = SFE_BAD_WRITE_ALIGN ;
2081 return 0 ;
2082 } ;
2083
2084 if (psf->write_float == NULL || psf->seek == NULL)
2085 { psf->error = SFE_UNIMPLEMENTED ;
2086 return 0 ;
2087 } ;
2088
2089 if (psf->last_op != SFM_WRITE)
2090 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2091 return 0 ;
2092
2093 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
2094 psf->write_header (psf, SF_FALSE) ;
2095 psf->have_written = SF_TRUE ;
2096
2097 count = psf->write_float (psf, ptr, len) ;
2098
2099 psf->write_current += count / psf->sf.channels ;
2100
2101 psf->last_op = SFM_WRITE ;
2102
2103 if (psf->auto_header && psf->write_header != NULL)
2104 psf->write_header (psf, SF_TRUE) ;
2105
2106 if (psf->write_current > psf->sf.frames)
2107 psf->sf.frames = psf->write_current ;
2108
2109 return count ;
2110 } /* sf_write_float */
2111
2112 sf_count_t
2113 sf_writef_float (SNDFILE *sndfile, const float *ptr, sf_count_t frames)
2114 { SF_PRIVATE *psf ;
2115 sf_count_t count ;
2116
2117 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
2118
2119 if (psf->file.mode == SFM_READ)
2120 { psf->error = SFE_NOT_WRITEMODE ;
2121 return 0 ;
2122 } ;
2123
2124 if (psf->write_float == NULL || psf->seek == NULL)
2125 { psf->error = SFE_UNIMPLEMENTED ;
2126 return 0 ;
2127 } ;
2128
2129 if (psf->last_op != SFM_WRITE)
2130 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2131 return 0 ;
2132
2133 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
2134 psf->write_header (psf, SF_FALSE) ;
2135 psf->have_written = SF_TRUE ;
2136
2137 count = psf->write_float (psf, ptr, frames * psf->sf.channels) ;
2138
2139 psf->write_current += count / psf->sf.channels ;
2140
2141 psf->last_op = SFM_WRITE ;
2142
2143 if (psf->auto_header && psf->write_header != NULL)
2144 psf->write_header (psf, SF_TRUE) ;
2145
2146 if (psf->write_current > psf->sf.frames)
2147 psf->sf.frames = psf->write_current ;
2148
2149 return count / psf->sf.channels ;
2150 } /* sf_writef_float */
2151
2152 /*------------------------------------------------------------------------------
2153 */
2154
2155 sf_count_t
2156 sf_write_double (SNDFILE *sndfile, const double *ptr, sf_count_t len)
2157 { SF_PRIVATE *psf ;
2158 sf_count_t count ;
2159
2160 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
2161
2162 if (psf->file.mode == SFM_READ)
2163 { psf->error = SFE_NOT_WRITEMODE ;
2164 return 0 ;
2165 } ;
2166
2167 if (len % psf->sf.channels)
2168 { psf->error = SFE_BAD_WRITE_ALIGN ;
2169 return 0 ;
2170 } ;
2171
2172 if (psf->write_double == NULL || psf->seek == NULL)
2173 { psf->error = SFE_UNIMPLEMENTED ;
2174 return 0 ;
2175 } ;
2176
2177 if (psf->last_op != SFM_WRITE)
2178 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2179 return 0 ;
2180
2181 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
2182 psf->write_header (psf, SF_FALSE) ;
2183 psf->have_written = SF_TRUE ;
2184
2185 count = psf->write_double (psf, ptr, len) ;
2186
2187 psf->write_current += count / psf->sf.channels ;
2188
2189 psf->last_op = SFM_WRITE ;
2190
2191 if (psf->auto_header && psf->write_header != NULL)
2192 psf->write_header (psf, SF_TRUE) ;
2193
2194 if (psf->write_current > psf->sf.frames)
2195 psf->sf.frames = psf->write_current ;
2196
2197 return count ;
2198 } /* sf_write_double */
2199
2200 sf_count_t
2201 sf_writef_double (SNDFILE *sndfile, const double *ptr, sf_count_t frames)
2202 { SF_PRIVATE *psf ;
2203 sf_count_t count ;
2204
2205 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
2206
2207 if (psf->file.mode == SFM_READ)
2208 { psf->error = SFE_NOT_WRITEMODE ;
2209 return 0 ;
2210 } ;
2211
2212 if (psf->write_double == NULL || psf->seek == NULL)
2213 { psf->error = SFE_UNIMPLEMENTED ;
2214 return 0 ;
2215 } ;
2216
2217 if (psf->last_op != SFM_WRITE)
2218 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
2219 return 0 ;
2220
2221 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
2222 psf->write_header (psf, SF_FALSE) ;
2223 psf->have_written = SF_TRUE ;
2224
2225 count = psf->write_double (psf, ptr, frames * psf->sf.channels) ;
2226
2227 psf->write_current += count / psf->sf.channels ;
2228
2229 psf->last_op = SFM_WRITE ;
2230
2231 if (psf->auto_header && psf->write_header != NULL)
2232 psf->write_header (psf, SF_TRUE) ;
2233
2234 if (psf->write_current > psf->sf.frames)
2235 psf->sf.frames = psf->write_current ;
2236
2237 return count / psf->sf.channels ;
2238 } /* sf_writef_double */
2239
2240 /*=========================================================================
2241 ** Private functions.
2242 */
2243
2244 static int
2245 try_resource_fork (SF_PRIVATE * psf)
2246 { int old_error = psf->error ;
2247
2248 /* Set READ mode now, to see if resource fork exists. */
2249 psf->rsrc.mode = SFM_READ ;
2250 if (psf_open_rsrc (psf) != 0)
2251 { psf->error = old_error ;
2252 return 0 ;
2253 } ;
2254
2255 /* More checking here. */
2256 psf_log_printf (psf, "Resource fork : %s\n", psf->rsrc.path.c) ;
2257
2258 return SF_FORMAT_SD2 ;
2259 } /* try_resource_fork */
2260
2261 static int
2262 format_from_extension (SF_PRIVATE *psf)
2263 { char *cptr ;
2264 char buffer [16] ;
2265 int format = 0 ;
2266
2267 if ((cptr = strrchr (psf->file.name.c, '.')) == NULL)
2268 return 0 ;
2269
2270 cptr ++ ;
2271 if (strlen (cptr) > sizeof (buffer) - 1)
2272 return 0 ;
2273
2274 psf_strlcpy (buffer, sizeof (buffer), cptr) ;
2275 buffer [sizeof (buffer) - 1] = 0 ;
2276
2277 /* Convert everything in the buffer to lower case. */
2278 cptr = buffer ;
2279 while (*cptr)
2280 { *cptr = tolower (*cptr) ;
2281 cptr ++ ;
2282 } ;
2283
2284 cptr = buffer ;
2285
2286 if (strcmp (cptr, "au") == 0)
2287 { psf->sf.channels = 1 ;
2288 psf->sf.samplerate = 8000 ;
2289 format = SF_FORMAT_RAW | SF_FORMAT_ULAW ;
2290 }
2291 else if (strcmp (cptr, "snd") == 0)
2292 { psf->sf.channels = 1 ;
2293 psf->sf.samplerate = 8000 ;
2294 format = SF_FORMAT_RAW | SF_FORMAT_ULAW ;
2295 }
2296
2297 else if (strcmp (cptr, "vox") == 0 || strcmp (cptr, "vox8") == 0)
2298 { psf->sf.channels = 1 ;
2299 psf->sf.samplerate = 8000 ;
2300 format = SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM ;
2301 }
2302 else if (strcmp (cptr, "vox6") == 0)
2303 { psf->sf.channels = 1 ;
2304 psf->sf.samplerate = 6000 ;
2305 format = SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM ;
2306 }
2307 else if (strcmp (cptr, "gsm") == 0)
2308 { psf->sf.channels = 1 ;
2309 psf->sf.samplerate = 8000 ;
2310 format = SF_FORMAT_RAW | SF_FORMAT_GSM610 ;
2311 }
2312
2313 /* For RAW files, make sure the dataoffset if set correctly. */
2314 if ((SF_CONTAINER (format)) == SF_FORMAT_RAW)
2315 psf->dataoffset = 0 ;
2316
2317 return format ;
2318 } /* format_from_extension */
2319
2320 static int
2321 guess_file_type (SF_PRIVATE *psf)
2322 { int buffer [3], format ;
2323
2324 if (psf_binheader_readf (psf, "b", &buffer, SIGNED_SIZEOF (buffer)) != SIGNED_SIZEOF (buffer))
2325 { psf->error = SFE_BAD_FILE_READ ;
2326 return 0 ;
2327 } ;
2328
2329 if ((buffer [0] == MAKE_MARKER ('R', 'I', 'F', 'F') || buffer [0] == MAKE_MARKER ('R', 'I', 'F', 'X'))
2330 && buffer [2] == MAKE_MARKER ('W', 'A', 'V', 'E'))
2331 return SF_FORMAT_WAV ;
2332
2333 if (buffer [0] == MAKE_MARKER ('F', 'O', 'R', 'M'))
2334 { if (buffer [2] == MAKE_MARKER ('A', 'I', 'F', 'F') || buffer [2] == MAKE_MARKER ('A', 'I', 'F', 'C'))
2335 return SF_FORMAT_AIFF ;
2336 if (buffer [2] == MAKE_MARKER ('8', 'S', 'V', 'X') || buffer [2] == MAKE_MARKER ('1', '6', 'S', 'V'))
2337 return SF_FORMAT_SVX ;
2338 return 0 ;
2339 } ;
2340
2341 if (buffer [0] == MAKE_MARKER ('.', 's', 'n', 'd') || buffer [0] == MAKE_MARKER ('d', 'n', 's', '.'))
2342 return SF_FORMAT_AU ;
2343
2344 if ((buffer [0] == MAKE_MARKER ('f', 'a', 'p', ' ') || buffer [0] == MAKE_MARKER (' ', 'p', 'a', 'f')))
2345 return SF_FORMAT_PAF ;
2346
2347 if (buffer [0] == MAKE_MARKER ('N', 'I', 'S', 'T'))
2348 return SF_FORMAT_NIST ;
2349
2350 if (buffer [0] == MAKE_MARKER ('C', 'r', 'e', 'a') && buffer [1] == MAKE_MARKER ('t', 'i', 'v', 'e'))
2351 return SF_FORMAT_VOC ;
2352
2353 if ((buffer [0] & MAKE_MARKER (0xFF, 0xFF, 0xF8, 0xFF)) == MAKE_MARKER (0x64, 0xA3, 0x00, 0x00) ||
2354 (buffer [0] & MAKE_MARKER (0xFF, 0xF8, 0xFF, 0xFF)) == MAKE_MARKER (0x00, 0x00, 0xA3, 0x64))
2355 return SF_FORMAT_IRCAM ;
2356
2357 if (buffer [0] == MAKE_MARKER ('r', 'i', 'f', 'f'))
2358 return SF_FORMAT_W64 ;
2359
2360 if (buffer [0] == MAKE_MARKER (0, 0, 0x03, 0xE8) && buffer [1] == MAKE_MARKER (0, 0, 0, 1) &&
2361 buffer [2] == MAKE_MARKER (0, 0, 0, 1))
2362 return SF_FORMAT_MAT4 ;
2363
2364 if (buffer [0] == MAKE_MARKER (0, 0, 0, 0) && buffer [1] == MAKE_MARKER (1, 0, 0, 0) &&
2365 buffer [2] == MAKE_MARKER (1, 0, 0, 0))
2366 return SF_FORMAT_MAT4 ;
2367
2368 if (buffer [0] == MAKE_MARKER ('M', 'A', 'T', 'L') && buffer [1] == MAKE_MARKER ('A', 'B', ' ', '5'))
2369 return SF_FORMAT_MAT5 ;
2370
2371 if (buffer [0] == MAKE_MARKER ('P', 'V', 'F', '1'))
2372 return SF_FORMAT_PVF ;
2373
2374 if (buffer [0] == MAKE_MARKER ('E', 'x', 't', 'e') && buffer [1] == MAKE_MARKER ('n', 'd', 'e', 'd') &&
2375 buffer [2] == MAKE_MARKER (' ', 'I', 'n', 's'))
2376 return SF_FORMAT_XI ;
2377
2378 if (buffer [0] == MAKE_MARKER ('c', 'a', 'f', 'f') && buffer [2] == MAKE_MARKER ('d', 'e', 's', 'c'))
2379 return SF_FORMAT_CAF ;
2380
2381 if (buffer [0] == MAKE_MARKER ('O', 'g', 'g', 'S'))
2382 return SF_FORMAT_OGG ;
2383
2384 if (buffer [0] == MAKE_MARKER ('A', 'L', 'a', 'w') && buffer [1] == MAKE_MARKER ('S', 'o', 'u', 'n')
2385 && buffer [2] == MAKE_MARKER ('d', 'F', 'i', 'l'))
2386 return SF_FORMAT_WVE ;
2387
2388 if (buffer [0] == MAKE_MARKER ('D', 'i', 'a', 'm') && buffer [1] == MAKE_MARKER ('o', 'n', 'd', 'W')
2389 && buffer [2] == MAKE_MARKER ('a', 'r', 'e', ' '))
2390 return SF_FORMAT_DWD ;
2391
2392 if (buffer [0] == MAKE_MARKER ('L', 'M', '8', '9') || buffer [0] == MAKE_MARKER ('5', '3', 0, 0))
2393 return SF_FORMAT_TXW ;
2394
2395 if ((buffer [0] & MAKE_MARKER (0xFF, 0xFF, 0x80, 0xFF)) == MAKE_MARKER (0xF0, 0x7E, 0, 0x01))
2396 return SF_FORMAT_SDS ;
2397
2398 if ((buffer [0] & MAKE_MARKER (0xFF, 0xFF, 0, 0)) == MAKE_MARKER (1, 4, 0, 0))
2399 return SF_FORMAT_MPC2K ;
2400
2401 if (buffer [0] == MAKE_MARKER ('C', 'A', 'T', ' ') && buffer [2] == MAKE_MARKER ('R', 'E', 'X', '2'))
2402 return SF_FORMAT_REX2 ;
2403
2404 if (buffer [0] == MAKE_MARKER (0x30, 0x26, 0xB2, 0x75) && buffer [1] == MAKE_MARKER (0x8E, 0x66, 0xCF, 0x11))
2405 return 0 /*-SF_FORMAT_WMA-*/ ;
2406
2407 /* HMM (Hidden Markov Model) Tool Kit. */
2408 if (2 * BEI2H_INT (buffer [0]) + 12 == psf->filelength && buffer [2] == MAKE_MARKER (0, 2, 0, 0))
2409 return SF_FORMAT_HTK ;
2410
2411 if (buffer [0] == MAKE_MARKER ('f', 'L', 'a', 'C'))
2412 return SF_FORMAT_FLAC ;
2413
2414 if (buffer [0] == MAKE_MARKER ('2', 'B', 'I', 'T'))
2415 return SF_FORMAT_AVR ;
2416
2417 if (buffer [0] == MAKE_MARKER ('R', 'F', '6', '4') && buffer [2] == MAKE_MARKER ('W', 'A', 'V', 'E'))
2418 return SF_FORMAT_RF64 ;
2419
2420 if (buffer [0] == MAKE_MARKER ('I', 'D', '3', 3))
2421 { psf_log_printf (psf, "Found 'ID3' marker.\n") ;
2422 if (id3_skip (psf))
2423 return guess_file_type (psf) ;
2424 return 0 ;
2425 } ;
2426
2427 /* Turtle Beach SMP 16-bit */
2428 if (buffer [0] == MAKE_MARKER ('S', 'O', 'U', 'N') && buffer [1] == MAKE_MARKER ('D', ' ', 'S', 'A'))
2429 return 0 ;
2430
2431 /* Yamaha sampler format. */
2432 if (buffer [0] == MAKE_MARKER ('S', 'Y', '8', '0') || buffer [0] == MAKE_MARKER ('S', 'Y', '8', '5'))
2433 return 0 ;
2434
2435 if (buffer [0] == MAKE_MARKER ('a', 'j', 'k', 'g'))
2436 return 0 /*-SF_FORMAT_SHN-*/ ;
2437
2438 /* This must be the last one. */
2439 if (psf->filelength > 0 && (format = try_resource_fork (psf)) != 0)
2440 return format ;
2441
2442 return 0 ;
2443 } /* guess_file_type */
2444
2445
2446 static int
2447 validate_sfinfo (SF_INFO *sfinfo)
2448 { if (sfinfo->samplerate < 1)
2449 return 0 ;
2450 if (sfinfo->frames < 0)
2451 return 0 ;
2452 if (sfinfo->channels < 1)
2453 return 0 ;
2454 if ((SF_CONTAINER (sfinfo->format)) == 0)
2455 return 0 ;
2456 if ((SF_CODEC (sfinfo->format)) == 0)
2457 return 0 ;
2458 if (sfinfo->sections < 1)
2459 return 0 ;
2460 return 1 ;
2461 } /* validate_sfinfo */
2462
2463 static int
2464 validate_psf (SF_PRIVATE *psf)
2465 {
2466 if (psf->datalength < 0)
2467 { psf_log_printf (psf, "Invalid SF_PRIVATE field : datalength == %D.\n", psf->datalength) ;
2468 return 0 ;
2469 } ;
2470 if (psf->dataoffset < 0)
2471 { psf_log_printf (psf, "Invalid SF_PRIVATE field : dataoffset == %D.\n", psf->dataoffset) ;
2472 return 0 ;
2473 } ;
2474 if (psf->blockwidth && psf->blockwidth != psf->sf.channels * psf->bytewidth)
2475 { psf_log_printf (psf, "Invalid SF_PRIVATE field : channels * bytewidth == %d.\n",
2476 psf->sf.channels * psf->bytewidth) ;
2477 return 0 ;
2478 } ;
2479 return 1 ;
2480 } /* validate_psf */
2481
2482 static void
2483 save_header_info (SF_PRIVATE *psf)
2484 { snprintf (sf_logbuffer, sizeof (sf_logbuffer), "%s", psf->logbuffer) ;
2485 } /* save_header_info */
2486
2487 static void
2488 copy_filename (SF_PRIVATE *psf, const char *path)
2489 { const char *ccptr ;
2490 char *cptr ;
2491
2492 snprintf (psf->file.path.c, sizeof (psf->file.path.c), "%s", path) ;
2493 if ((ccptr = strrchr (path, '/')) || (ccptr = strrchr (path, '\\')))
2494 ccptr ++ ;
2495 else
2496 ccptr = path ;
2497
2498 snprintf (psf->file.name.c, sizeof (psf->file.name.c), "%s", ccptr) ;
2499
2500 /* Now grab the directory. */
2501 snprintf (psf->file.dir.c, sizeof (psf->file.dir.c), "%s", path) ;
2502 if ((cptr = strrchr (psf->file.dir.c, '/')) || (cptr = strrchr (psf->file.dir.c, '\\')))
2503 cptr [1] = 0 ;
2504 else
2505 psf->file.dir.c [0] = 0 ;
2506
2507 return ;
2508 } /* copy_filename */
2509
2510 /*==============================================================================
2511 */
2512
2513 static int
2514 psf_close (SF_PRIVATE *psf)
2515 { int error = 0 ;
2516
2517 if (psf->codec_close)
2518 error = psf->codec_close (psf) ;
2519 if (psf->container_close)
2520 error = psf->container_close (psf) ;
2521
2522 error = psf_fclose (psf) ;
2523 psf_close_rsrc (psf) ;
2524
2525 if (psf->container_data)
2526 free (psf->container_data) ;
2527
2528 if (psf->codec_data)
2529 free (psf->codec_data) ;
2530
2531 if (psf->interleave)
2532 free (psf->interleave) ;
2533
2534 if (psf->dither)
2535 free (psf->dither) ;
2536
2537 if (psf->peak_info)
2538 free (psf->peak_info) ;
2539
2540 if (psf->broadcast_16k)
2541 free (psf->broadcast_16k) ;
2542
2543 if (psf->loop_info)
2544 free (psf->loop_info) ;
2545
2546 if (psf->instrument)
2547 free (psf->instrument) ;
2548
2549 if (psf->channel_map)
2550 free (psf->channel_map) ;
2551
2552 if (psf->format_desc)
2553 { psf->format_desc [0] = 0 ;
2554 free (psf->format_desc) ;
2555 } ;
2556
2557 memset (psf, 0, sizeof (SF_PRIVATE)) ;
2558 free (psf) ;
2559
2560 return error ;
2561 } /* psf_close */
2562
2563 SNDFILE *
2564 psf_open_file (SF_PRIVATE *psf, SF_INFO *sfinfo)
2565 { int error, format ;
2566
2567 sf_errno = error = 0 ;
2568 sf_logbuffer [0] = 0 ;
2569
2570 if (psf->error)
2571 { error = psf->error ;
2572 goto error_exit ;
2573 } ;
2574
2575 if (psf->file.mode != SFM_READ && psf->file.mode != SFM_WRITE && psf->file.mode != SFM_RDWR)
2576 { error = SFE_BAD_OPEN_MODE ;
2577 goto error_exit ;
2578 } ;
2579
2580 if (sfinfo == NULL)
2581 { error = SFE_BAD_SF_INFO_PTR ;
2582 goto error_exit ;
2583 } ;
2584
2585 /* Zero out these fields. */
2586 sfinfo->frames = 0 ;
2587 sfinfo->sections = 0 ;
2588 sfinfo->seekable = 0 ;
2589
2590 if (psf->file.mode == SFM_READ)
2591 { if ((SF_CONTAINER (sfinfo->format)) == SF_FORMAT_RAW)
2592 { if (sf_format_check (sfinfo) == 0)
2593 { error = SFE_RAW_BAD_FORMAT ;
2594 goto error_exit ;
2595 } ;
2596 }
2597 else
2598 memset (sfinfo, 0, sizeof (SF_INFO)) ;
2599 } ;
2600
2601 memcpy (&(psf->sf), sfinfo, sizeof (SF_INFO)) ;
2602
2603 psf->Magick = SNDFILE_MAGICK ;
2604 psf->norm_float = SF_TRUE ;
2605 psf->norm_double = SF_TRUE ;
2606 psf->dataoffset = -1 ;
2607 psf->datalength = -1 ;
2608 psf->read_current = -1 ;
2609 psf->write_current = -1 ;
2610 psf->auto_header = SF_FALSE ;
2611 psf->rwf_endian = SF_ENDIAN_LITTLE ;
2612 psf->seek = psf_default_seek ;
2613 psf->float_int_mult = 0 ;
2614 psf->float_max = -1.0 ;
2615
2616 /* An attempt at a per SF_PRIVATE unique id. */
2617 psf->unique_id = psf_rand_int32 () ;
2618
2619 psf->sf.sections = 1 ;
2620
2621 psf->is_pipe = psf_is_pipe (psf) ;
2622
2623 if (psf->is_pipe)
2624 { psf->sf.seekable = SF_FALSE ;
2625 psf->filelength = SF_COUNT_MAX ;
2626 }
2627 else
2628 { psf->sf.seekable = SF_TRUE ;
2629
2630 /* File is open, so get the length. */
2631 psf->filelength = psf_get_filelen (psf) ;
2632 } ;
2633
2634 if (psf->fileoffset > 0)
2635 { switch (psf->file.mode)
2636 { case SFM_READ :
2637 if (psf->filelength < 44)
2638 { psf_log_printf (psf, "Short filelength: %D (fileoffset: %D)\n", psf->filelength, psf->fileoffset) ;
2639 error = SFE_BAD_OFFSET ;
2640 goto error_exit ;
2641 } ;
2642 break ;
2643
2644 case SFM_WRITE :
2645 psf->fileoffset = 0 ;
2646 psf_fseek (psf, 0, SEEK_END) ;
2647 psf->fileoffset = psf_ftell (psf) ;
2648 break ;
2649
2650 case SFM_RDWR :
2651 error = SFE_NO_EMBEDDED_RDWR ;
2652 goto error_exit ;
2653 } ;
2654
2655 psf_log_printf (psf, "Embedded file offset : %D\n", psf->fileoffset) ;
2656 } ;
2657
2658 if (psf->filelength == SF_COUNT_MAX)
2659 psf_log_printf (psf, "Length : unknown\n") ;
2660 else
2661 psf_log_printf (psf, "Length : %D\n", psf->filelength) ;
2662
2663 if (psf->file.mode == SFM_WRITE || (psf->file.mode == SFM_RDWR && psf->filelength == 0))
2664 { /* If the file is being opened for write or RDWR and the file is currently
2665 ** empty, then the SF_INFO struct must contain valid data.
2666 */
2667 if ((SF_CONTAINER (psf->sf.format)) == 0)
2668 { error = SFE_ZERO_MAJOR_FORMAT ;
2669 goto error_exit ;
2670 } ;
2671 if ((SF_CODEC (psf->sf.format)) == 0)
2672 { error = SFE_ZERO_MINOR_FORMAT ;
2673 goto error_exit ;
2674 } ;
2675
2676 if (sf_format_check (&(psf->sf)) == 0)
2677 { error = SFE_BAD_OPEN_FORMAT ;
2678 goto error_exit ;
2679 } ;
2680 }
2681 else if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_RAW)
2682 { /* If type RAW has not been specified then need to figure out file type. */
2683 psf->sf.format = guess_file_type (psf) ;
2684
2685 if (psf->sf.format == 0)
2686 psf->sf.format = format_from_extension (psf) ;
2687 } ;
2688
2689 /* Prevent unnecessary seeks */
2690 psf->last_op = psf->file.mode ;
2691
2692 /* Set bytewidth if known. */
2693 switch (SF_CODEC (psf->sf.format))
2694 { case SF_FORMAT_PCM_S8 :
2695 case SF_FORMAT_PCM_U8 :
2696 case SF_FORMAT_ULAW :
2697 case SF_FORMAT_ALAW :
2698 case SF_FORMAT_DPCM_8 :
2699 psf->bytewidth = 1 ;
2700 break ;
2701
2702 case SF_FORMAT_PCM_16 :
2703 case SF_FORMAT_DPCM_16 :
2704 psf->bytewidth = 2 ;
2705 break ;
2706
2707 case SF_FORMAT_PCM_24 :
2708 psf->bytewidth = 3 ;
2709 break ;
2710
2711 case SF_FORMAT_PCM_32 :
2712 case SF_FORMAT_FLOAT :
2713 psf->bytewidth = 4 ;
2714 break ;
2715
2716 case SF_FORMAT_DOUBLE :
2717 psf->bytewidth = 8 ;
2718 break ;
2719 } ;
2720
2721 /* Call the initialisation function for the relevant file type. */
2722 switch (SF_CONTAINER (psf->sf.format))
2723 { case SF_FORMAT_WAV :
2724 case SF_FORMAT_WAVEX :
2725 error = wav_open (psf) ;
2726 break ;
2727
2728 case SF_FORMAT_AIFF :
2729 error = aiff_open (psf) ;
2730 break ;
2731
2732 case SF_FORMAT_AU :
2733 error = au_open (psf) ;
2734 break ;
2735
2736 case SF_FORMAT_RAW :
2737 error = raw_open (psf) ;
2738 break ;
2739
2740 case SF_FORMAT_W64 :
2741 error = w64_open (psf) ;
2742 break ;
2743
2744 case SF_FORMAT_RF64 :
2745 error = rf64_open (psf) ;
2746 break ;
2747
2748 /* Lite remove start */
2749 case SF_FORMAT_PAF :
2750 error = paf_open (psf) ;
2751 break ;
2752
2753 case SF_FORMAT_SVX :
2754 error = svx_open (psf) ;
2755 break ;
2756
2757 case SF_FORMAT_NIST :
2758 error = nist_open (psf) ;
2759 break ;
2760
2761 case SF_FORMAT_IRCAM :
2762 error = ircam_open (psf) ;
2763 break ;
2764
2765 case SF_FORMAT_VOC :
2766 error = voc_open (psf) ;
2767 break ;
2768
2769 case SF_FORMAT_SDS :
2770 error = sds_open (psf) ;
2771 break ;
2772
2773 case SF_FORMAT_OGG :
2774 error = ogg_open (psf) ;
2775 break ;
2776
2777 case SF_FORMAT_TXW :
2778 error = txw_open (psf) ;
2779 break ;
2780
2781 case SF_FORMAT_WVE :
2782 error = wve_open (psf) ;
2783 break ;
2784
2785 case SF_FORMAT_DWD :
2786 error = dwd_open (psf) ;
2787 break ;
2788
2789 case SF_FORMAT_MAT4 :
2790 error = mat4_open (psf) ;
2791 break ;
2792
2793 case SF_FORMAT_MAT5 :
2794 error = mat5_open (psf) ;
2795 break ;
2796
2797 case SF_FORMAT_PVF :
2798 error = pvf_open (psf) ;
2799 break ;
2800
2801 case SF_FORMAT_XI :
2802 error = xi_open (psf) ;
2803 break ;
2804
2805 case SF_FORMAT_HTK :
2806 error = htk_open (psf) ;
2807 break ;
2808
2809 case SF_FORMAT_SD2 :
2810 error = sd2_open (psf) ;
2811 break ;
2812
2813 case SF_FORMAT_REX2 :
2814 error = rx2_open (psf) ;
2815 break ;
2816
2817 case SF_FORMAT_AVR :
2818 error = avr_open (psf) ;
2819 break ;
2820
2821 case SF_FORMAT_FLAC :
2822 error = flac_open (psf) ;
2823 break ;
2824
2825 case SF_FORMAT_CAF :
2826 error = caf_open (psf) ;
2827 break ;
2828
2829 case SF_FORMAT_MPC2K :
2830 error = mpc2k_open (psf) ;
2831 break ;
2832
2833 /* Lite remove end */
2834
2835 default :
2836 error = SFE_UNKNOWN_FORMAT ;
2837 } ;
2838
2839 if (error)
2840 goto error_exit ;
2841
2842 /* For now, check whether embedding is supported. */
2843 format = SF_CONTAINER (psf->sf.format) ;
2844 if (psf->fileoffset > 0)
2845 { switch (format)
2846 { case SF_FORMAT_WAV :
2847 case SF_FORMAT_WAVEX :
2848 case SF_FORMAT_AIFF :
2849 case SF_FORMAT_AU :
2850 /* Actual embedded files. */
2851 break ;
2852
2853 case SF_FORMAT_FLAC :
2854 /* Flac with an ID3v2 header? */
2855 break ;
2856
2857 default :
2858 error = SFE_NO_EMBED_SUPPORT ;
2859 goto error_exit ;
2860 } ;
2861 } ;
2862
2863 if (psf->fileoffset > 0)
2864 psf_log_printf (psf, "Embedded file length : %D\n", psf->filelength) ;
2865
2866 if (psf->file.mode == SFM_RDWR && sf_format_check (&(psf->sf)) == 0)
2867 { error = SFE_BAD_MODE_RW ;
2868 goto error_exit ;
2869 } ;
2870
2871 if (validate_sfinfo (&(psf->sf)) == 0)
2872 { psf_log_SF_INFO (psf) ;
2873 save_header_info (psf) ;
2874 error = SFE_BAD_SF_INFO ;
2875 goto error_exit ;
2876 } ;
2877
2878 if (validate_psf (psf) == 0)
2879 { save_header_info (psf) ;
2880 error = SFE_INTERNAL ;
2881 goto error_exit ;
2882 } ;
2883
2884 psf->read_current = 0 ;
2885 psf->write_current = 0 ;
2886 if (psf->file.mode == SFM_RDWR)
2887 { psf->write_current = psf->sf.frames ;
2888 psf->have_written = psf->sf.frames > 0 ? SF_TRUE : SF_FALSE ;
2889 } ;
2890
2891 memcpy (sfinfo, &(psf->sf), sizeof (SF_INFO)) ;
2892
2893 memcpy (sfinfo, &(psf->sf), sizeof (SF_INFO)) ;
2894
2895 return (SNDFILE *) psf ;
2896
2897 error_exit :
2898 sf_errno = error ;
2899
2900 if (error == SFE_SYSTEM)
2901 snprintf (sf_syserr, sizeof (sf_syserr), "%s", psf->syserr) ;
2902 snprintf (sf_logbuffer, sizeof (sf_logbuffer), "%s", psf->logbuffer) ;
2903
2904 switch (error)
2905 { case SF_ERR_SYSTEM :
2906 case SF_ERR_UNSUPPORTED_ENCODING :
2907 case SFE_UNIMPLEMENTED :
2908 break ;
2909
2910 case SFE_RAW_BAD_FORMAT :
2911 break ;
2912
2913 default :
2914 if (psf->file.mode == SFM_READ)
2915 { psf_log_printf (psf, "Parse error : %s\n", sf_error_number (error)) ;
2916 error = SF_ERR_MALFORMED_FILE ;
2917 } ;
2918 } ;
2919
2920 psf_close (psf) ;
2921 return NULL ;
2922 } /* psf_open_file */
2923