2 ** Copyright (C) 1999-2011 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 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.
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.
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.
30 #define SNDFILE_MAGICK 0x1234C0DE
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.
38 #if (CPU_IS_LITTLE_ENDIAN == 1)
39 #error "Universal binary compile detected. See http://www.mega-nerd.com/libsndfile/FAQ.html#Q018"
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"
57 ErrorStruct SndfileErrors
[] =
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." },
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)." },
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." },
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." },
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." },
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." },
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." },
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." },
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." },
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." },
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." },
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." },
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." },
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." },
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."
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." },
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." },
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." },
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." },
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." },
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." },
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." },
212 { SFE_MAT4_BAD_NAME
, "Error in MAT4 file. No variable name." },
213 { SFE_MAT4_NO_SAMPLERATE
, "Error in MAT4 file. No sample rate." },
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." },
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." },
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." },
227 { SFE_HTK_NO_PIPE
, "Error : not able to operate on HTK files over a pipe." },
229 { SFE_SDS_NOT_SDS
, "Error : not an SDS file." },
230 { SFE_SDS_BAD_BIT_WIDTH
, "Error : bad bit width for SDS file." },
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." },
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." },
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." },
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." },
253 { SFE_VORBIS_ENCODER_BUG
, "Error : Sample rate chosen is known to trigger a Vorbis encoder bug on this CPU." },
255 { SFE_RF64_NOT_RF64
, "Error : Not an RF64 file." },
257 { SFE_MAX_ERROR
, "Maximum error number." },
258 { SFE_MAX_ERROR
+ 1 , NULL
}
261 /*------------------------------------------------------------------------------
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
) ;
272 static int try_resource_fork (SF_PRIVATE
* psf
) ;
274 /*------------------------------------------------------------------------------
275 ** Private (static) variables.
279 static char sf_logbuffer
[SF_BUFFER_LEN
] = { 0 } ;
280 static char sf_syserr
[SF_SYSERR_LEN
] = { 0 } ;
282 /*------------------------------------------------------------------------------
285 #define VALIDATE_SNDFILE_AND_ASSIGN_PSF(a,b,c) \
287 { sf_errno = SFE_BAD_SNDFILE_PTR ; \
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 ; \
296 if ((b)->Magick != SNDFILE_MAGICK) \
297 { (b)->error = SFE_BAD_SNDFILE_PTR ; \
300 if (c) (b)->error = 0 ; \
303 /*------------------------------------------------------------------------------
308 sf_open (const char *path
, int mode
, SF_INFO
*sfinfo
)
311 /* Ultimate sanity check. */
312 assert (sizeof (sf_count_t
) == 8) ;
314 if ((psf
= calloc (1, sizeof (SF_PRIVATE
))) == NULL
)
315 { sf_errno
= SFE_MALLOC_FAILED
;
319 memset (psf
, 0, sizeof (SF_PRIVATE
)) ;
320 psf_init_files (psf
) ;
322 psf_log_printf (psf
, "File : %s\n", path
) ;
324 copy_filename (psf
, path
) ;
326 psf
->file
.mode
= mode
;
327 if (strcmp (path
, "-") == 0)
328 psf
->error
= psf_set_stdio (psf
) ;
330 psf
->error
= psf_fopen (psf
) ;
332 return psf_open_file (psf
, sfinfo
) ;
336 sf_open_fd (int fd
, int mode
, SF_INFO
*sfinfo
, int close_desc
)
339 if ((SF_CONTAINER (sfinfo
->format
)) == SF_FORMAT_SD2
)
340 { sf_errno
= SFE_SD2_FD_DISALLOWED
;
344 if ((psf
= calloc (1, sizeof (SF_PRIVATE
))) == NULL
)
345 { sf_errno
= SFE_MALLOC_FAILED
;
349 psf_init_files (psf
) ;
350 copy_filename (psf
, "") ;
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
) ;
358 psf
->file
.do_not_close_descriptor
= SF_TRUE
;
360 return psf_open_file (psf
, sfinfo
) ;
364 sf_open_virtual (SF_VIRTUAL_IO
*sfvirtual
, int mode
, SF_INFO
*sfinfo
, void *user_data
)
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") ;
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") ;
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") ;
386 if ((psf
= calloc (1, sizeof (SF_PRIVATE
))) == NULL
)
387 { sf_errno
= SFE_MALLOC_FAILED
;
391 psf_init_files (psf
) ;
393 psf
->virtual_io
= SF_TRUE
;
394 psf
->vio
= *sfvirtual
;
395 psf
->vio_user_data
= user_data
;
397 psf
->file
.mode
= mode
;
399 return psf_open_file (psf
, sfinfo
) ;
400 } /* sf_open_virtual */
403 sf_close (SNDFILE
*sndfile
)
406 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
408 return psf_close (psf
) ;
412 sf_write_sync (SNDFILE
*sndfile
)
415 if ((psf
= (SF_PRIVATE
*) sndfile
) == NULL
)
421 } /* sf_write_sync */
423 /*==============================================================================
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." ;
432 if (errnum
== SFE_MAX_ERROR
)
433 return SndfileErrors
[0].str
;
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
) ;
441 for (k
= 0 ; SndfileErrors
[k
].str
; k
++)
442 if (errnum
== SndfileErrors
[k
].error
)
443 return SndfileErrors
[k
].str
;
446 } /* sf_error_number */
449 sf_strerror (SNDFILE
*sndfile
)
450 { SF_PRIVATE
*psf
= NULL
;
454 { errnum
= sf_errno
;
455 if (errnum
== SFE_SYSTEM
&& sf_syserr
[0])
459 { psf
= (SF_PRIVATE
*) sndfile
;
461 if (psf
->Magick
!= SNDFILE_MAGICK
)
462 return "sf_strerror : Bad magic number." ;
464 errnum
= psf
->error
;
466 if (errnum
== SFE_SYSTEM
&& psf
->syserr
[0])
470 return sf_error_number (errnum
) ;
473 /*------------------------------------------------------------------------------
477 sf_error (SNDFILE
*sndfile
)
483 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 0) ;
491 /*------------------------------------------------------------------------------
495 sf_perror (SNDFILE
*sndfile
)
500 { errnum
= sf_errno
;
503 { VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 0) ;
504 errnum
= psf
->error
;
507 fprintf (stderr
, "%s\n", sf_error_number (errnum
)) ;
508 return SFE_NO_ERROR
;
512 /*------------------------------------------------------------------------------
516 sf_error_str (SNDFILE
*sndfile
, char *str
, size_t maxlen
)
521 return SFE_INTERNAL
;
526 { VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 0) ;
527 errnum
= psf
->error
;
530 snprintf (str
, maxlen
, "%s", sf_error_number (errnum
)) ;
532 return SFE_NO_ERROR
;
535 /*==============================================================================
539 sf_format_check (const SF_INFO
*info
)
540 { int subformat
, endian
;
542 subformat
= SF_CODEC (info
->format
) ;
543 endian
= SF_ENDIAN (info
->format
) ;
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.
550 if (info
->channels
< 1 || info
->channels
> SF_MAX_CHANNELS
)
553 if (info
->samplerate
< 0)
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
)
561 if (subformat
== SF_FORMAT_PCM_24
|| subformat
== SF_FORMAT_PCM_32
)
563 if ((subformat
== SF_FORMAT_IMA_ADPCM
|| subformat
== SF_FORMAT_MS_ADPCM
) && info
->channels
<= 2)
565 if (subformat
== SF_FORMAT_GSM610
&& info
->channels
== 1)
567 if (subformat
== SF_FORMAT_G721_32
&& info
->channels
== 1)
569 if (subformat
== SF_FORMAT_ULAW
|| subformat
== SF_FORMAT_ALAW
)
571 if (subformat
== SF_FORMAT_FLOAT
|| subformat
== SF_FORMAT_DOUBLE
)
575 case SF_FORMAT_WAVEX
:
576 if (endian
== SF_ENDIAN_BIG
|| endian
== SF_ENDIAN_CPU
)
578 if (subformat
== SF_FORMAT_PCM_U8
|| subformat
== SF_FORMAT_PCM_16
)
580 if (subformat
== SF_FORMAT_PCM_24
|| subformat
== SF_FORMAT_PCM_32
)
582 if (subformat
== SF_FORMAT_ULAW
|| subformat
== SF_FORMAT_ALAW
)
584 if (subformat
== SF_FORMAT_FLOAT
|| subformat
== SF_FORMAT_DOUBLE
)
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
)
592 /* Other encodings. Check for endian-ness. */
593 if (endian
== SF_ENDIAN_LITTLE
|| endian
== SF_ENDIAN_CPU
)
595 if (subformat
== SF_FORMAT_PCM_U8
|| subformat
== SF_FORMAT_PCM_S8
)
597 if (subformat
== SF_FORMAT_FLOAT
|| subformat
== SF_FORMAT_DOUBLE
)
599 if (subformat
== SF_FORMAT_ULAW
|| subformat
== SF_FORMAT_ALAW
)
601 if ((subformat
== SF_FORMAT_DWVW_12
|| subformat
== SF_FORMAT_DWVW_16
||
602 subformat
== SF_FORMAT_DWVW_24
) && info
-> channels
== 1)
604 if (subformat
== SF_FORMAT_GSM610
&& info
->channels
== 1)
606 if (subformat
== SF_FORMAT_IMA_ADPCM
&& (info
->channels
== 1 || info
->channels
== 2))
611 if (subformat
== SF_FORMAT_PCM_S8
|| subformat
== SF_FORMAT_PCM_16
)
613 if (subformat
== SF_FORMAT_PCM_24
|| subformat
== SF_FORMAT_PCM_32
)
615 if (subformat
== SF_FORMAT_ULAW
|| subformat
== SF_FORMAT_ALAW
)
617 if (subformat
== SF_FORMAT_FLOAT
|| subformat
== SF_FORMAT_DOUBLE
)
619 if (subformat
== SF_FORMAT_G721_32
&& info
->channels
== 1)
621 if (subformat
== SF_FORMAT_G723_24
&& info
->channels
== 1)
623 if (subformat
== SF_FORMAT_G723_40
&& info
->channels
== 1)
628 if (subformat
== SF_FORMAT_PCM_S8
|| subformat
== SF_FORMAT_PCM_16
)
630 if (subformat
== SF_FORMAT_PCM_24
|| subformat
== SF_FORMAT_PCM_32
)
632 if (subformat
== SF_FORMAT_ULAW
|| subformat
== SF_FORMAT_ALAW
)
634 if (subformat
== SF_FORMAT_FLOAT
|| subformat
== SF_FORMAT_DOUBLE
)
639 if (subformat
== SF_FORMAT_PCM_U8
|| subformat
== SF_FORMAT_PCM_S8
|| subformat
== SF_FORMAT_PCM_16
)
641 if (subformat
== SF_FORMAT_PCM_24
|| subformat
== SF_FORMAT_PCM_32
)
643 if (subformat
== SF_FORMAT_FLOAT
|| subformat
== SF_FORMAT_DOUBLE
)
645 if (subformat
== SF_FORMAT_ALAW
|| subformat
== SF_FORMAT_ULAW
)
647 if ((subformat
== SF_FORMAT_DWVW_12
|| subformat
== SF_FORMAT_DWVW_16
||
648 subformat
== SF_FORMAT_DWVW_24
) && info
-> channels
== 1)
650 if (subformat
== SF_FORMAT_GSM610
&& info
->channels
== 1)
652 if (subformat
== SF_FORMAT_VOX_ADPCM
&& info
->channels
== 1)
657 if (subformat
== SF_FORMAT_PCM_S8
|| subformat
== SF_FORMAT_PCM_16
|| subformat
== SF_FORMAT_PCM_24
)
662 /* SVX only supports writing mono SVX files. */
663 if (info
->channels
> 1)
665 /* Always big endian. */
666 if (endian
== SF_ENDIAN_LITTLE
|| endian
== SF_ENDIAN_CPU
)
669 if (subformat
== SF_FORMAT_PCM_S8
|| subformat
== SF_FORMAT_PCM_16
)
673 case SF_FORMAT_NIST
:
674 if (subformat
== SF_FORMAT_PCM_S8
|| subformat
== SF_FORMAT_PCM_16
)
676 if (subformat
== SF_FORMAT_PCM_24
|| subformat
== SF_FORMAT_PCM_32
)
678 if (subformat
== SF_FORMAT_ULAW
|| subformat
== SF_FORMAT_ALAW
)
682 case SF_FORMAT_IRCAM
:
683 if (subformat
== SF_FORMAT_PCM_16
|| subformat
== SF_FORMAT_PCM_32
)
685 if (subformat
== SF_FORMAT_ULAW
|| subformat
== SF_FORMAT_ALAW
|| subformat
== SF_FORMAT_FLOAT
)
690 /* VOC is strictly little endian. */
691 if (endian
== SF_ENDIAN_BIG
|| endian
== SF_ENDIAN_CPU
)
693 if (subformat
== SF_FORMAT_PCM_U8
|| subformat
== SF_FORMAT_PCM_16
)
695 if (subformat
== SF_FORMAT_ULAW
|| subformat
== SF_FORMAT_ALAW
)
700 /* W64 is strictly little endian. */
701 if (endian
== SF_ENDIAN_BIG
|| endian
== SF_ENDIAN_CPU
)
703 if (subformat
== SF_FORMAT_PCM_U8
|| subformat
== SF_FORMAT_PCM_16
)
705 if (subformat
== SF_FORMAT_PCM_24
|| subformat
== SF_FORMAT_PCM_32
)
707 if ((subformat
== SF_FORMAT_IMA_ADPCM
|| subformat
== SF_FORMAT_MS_ADPCM
) && info
->channels
<= 2)
709 if (subformat
== SF_FORMAT_GSM610
&& info
->channels
== 1)
711 if (subformat
== SF_FORMAT_ULAW
|| subformat
== SF_FORMAT_ALAW
)
713 if (subformat
== SF_FORMAT_FLOAT
|| subformat
== SF_FORMAT_DOUBLE
)
717 case SF_FORMAT_MAT4
:
718 if (subformat
== SF_FORMAT_PCM_16
|| subformat
== SF_FORMAT_PCM_32
)
720 if (subformat
== SF_FORMAT_FLOAT
|| subformat
== SF_FORMAT_DOUBLE
)
724 case SF_FORMAT_MAT5
:
725 if (subformat
== SF_FORMAT_PCM_U8
|| subformat
== SF_FORMAT_PCM_16
|| subformat
== SF_FORMAT_PCM_32
)
727 if (subformat
== SF_FORMAT_FLOAT
|| subformat
== SF_FORMAT_DOUBLE
)
732 if (subformat
== SF_FORMAT_PCM_S8
|| subformat
== SF_FORMAT_PCM_16
|| subformat
== SF_FORMAT_PCM_32
)
737 if (info
->channels
!= 1)
739 if (subformat
== SF_FORMAT_DPCM_8
|| subformat
== SF_FORMAT_DPCM_16
)
744 /* HTK is strictly big endian. */
745 if (endian
== SF_ENDIAN_LITTLE
|| endian
== SF_ENDIAN_CPU
)
747 if (info
->channels
!= 1)
749 if (subformat
== SF_FORMAT_PCM_16
)
754 /* SDS is strictly big endian. */
755 if (endian
== SF_ENDIAN_LITTLE
|| endian
== SF_ENDIAN_CPU
)
757 if (info
->channels
!= 1)
759 if (subformat
== SF_FORMAT_PCM_S8
|| subformat
== SF_FORMAT_PCM_16
|| subformat
== SF_FORMAT_PCM_24
)
764 /* SDS is strictly big endian. */
765 if (endian
== SF_ENDIAN_LITTLE
|| endian
== SF_ENDIAN_CPU
)
767 if (info
->channels
> 2)
769 if (subformat
== SF_FORMAT_PCM_U8
|| subformat
== SF_FORMAT_PCM_S8
|| subformat
== SF_FORMAT_PCM_16
)
773 case SF_FORMAT_FLAC
:
774 /* FLAC can't do more than 8 channels. */
775 if (info
->channels
> 8)
777 if (subformat
== SF_FORMAT_PCM_S8
|| subformat
== SF_FORMAT_PCM_16
|| subformat
== SF_FORMAT_PCM_24
)
782 /* SD2 is strictly big endian. */
783 if (endian
== SF_ENDIAN_LITTLE
|| endian
== SF_ENDIAN_CPU
)
785 if (subformat
== SF_FORMAT_PCM_S8
|| subformat
== SF_FORMAT_PCM_16
|| subformat
== SF_FORMAT_PCM_24
)
790 /* WVE is strictly big endian. */
791 if (endian
== SF_ENDIAN_BIG
|| endian
== SF_ENDIAN_CPU
)
793 if (info
->channels
> 1)
795 if (subformat
== SF_FORMAT_ALAW
)
800 if (subformat
== SF_FORMAT_VORBIS
)
804 case SF_FORMAT_MPC2K
:
805 /* MPC2000 is strictly little endian. */
806 if (endian
== SF_ENDIAN_BIG
|| endian
== SF_ENDIAN_CPU
)
808 if (info
->channels
> 2)
810 if (subformat
== SF_FORMAT_PCM_16
)
814 case SF_FORMAT_RF64
:
815 if (endian
== SF_ENDIAN_BIG
|| endian
== SF_ENDIAN_CPU
)
817 if (subformat
== SF_FORMAT_PCM_U8
|| subformat
== SF_FORMAT_PCM_16
)
819 if (subformat
== SF_FORMAT_PCM_24
|| subformat
== SF_FORMAT_PCM_32
)
821 if (subformat
== SF_FORMAT_ULAW
|| subformat
== SF_FORMAT_ALAW
)
823 if (subformat
== SF_FORMAT_FLOAT
|| subformat
== SF_FORMAT_DOUBLE
)
830 } /* sf_format_check */
832 /*------------------------------------------------------------------------------
836 sf_version_string (void)
838 #if ENABLE_EXPERIMENTAL_CODE
839 return PACKAGE_NAME
"-" PACKAGE_VERSION
"-exp" ;
841 return PACKAGE_NAME
"-" PACKAGE_VERSION
;
846 /*------------------------------------------------------------------------------
850 sf_command (SNDFILE
*sndfile
, int command
, void *data
, int datasize
)
851 { SF_PRIVATE
*psf
= (SF_PRIVATE
*) sndfile
;
854 /* This set of commands do not need the sndfile parameter. */
856 { case SFC_GET_LIB_VERSION
:
859 psf
->error
= SFE_BAD_COMMAND_PARAM
;
860 return SFE_BAD_COMMAND_PARAM
;
862 snprintf (data
, datasize
, "%s", sf_version_string ()) ;
863 return strlen (data
) ;
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 () ;
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
) ;
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 () ;
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
) ;
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 () ;
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
) ;
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
) ;
904 if (sndfile
== NULL
&& command
== SFC_GET_LOG_INFO
)
906 return (sf_errno
= SFE_BAD_COMMAND_PARAM
) ;
907 snprintf (data
, datasize
, "%s", sf_logbuffer
) ;
908 return strlen (data
) ;
911 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
914 { case SFC_SET_NORM_FLOAT
:
915 old_value
= psf
->norm_float
;
916 psf
->norm_float
= (datasize
) ? SF_TRUE
: SF_FALSE
;
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
)) ;
925 case SFC_SET_NORM_DOUBLE
:
926 old_value
= psf
->norm_double
;
927 psf
->norm_double
= (datasize
) ? SF_TRUE
: SF_FALSE
;
930 case SFC_GET_NORM_FLOAT
:
931 return psf
->norm_float
;
933 case SFC_GET_NORM_DOUBLE
:
934 return psf
->norm_double
;
936 case SFC_SET_SCALE_FLOAT_INT_READ
:
937 old_value
= psf
->float_int_mult
;
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
) ;
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
;
949 case SFC_SET_ADD_PEAK_CHUNK
:
950 { int format
= SF_CONTAINER (psf
->sf
.format
) ;
952 /* Only WAV and AIFF support the PEAK chunk. */
954 { case SF_FORMAT_AIFF
:
957 case SF_FORMAT_WAVEX
:
964 format
= SF_CODEC (psf
->sf
.format
) ;
966 /* Only files containg the following data types support the PEAK chunk. */
967 if (format
!= SF_FORMAT_FLOAT
&& format
!= SF_FORMAT_DOUBLE
)
971 /* Can only do this is in SFM_WRITE mode. */
972 if (psf
->file
.mode
!= SFM_WRITE
&& psf
->file
.mode
!= SFM_RDWR
)
974 /* If data has already been written this must fail. */
975 if (psf
->have_written
)
976 { psf
->error
= SFE_CMD_HAS_DATA
;
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
;
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
;
990 if (psf
->write_header
)
991 psf
->write_header (psf
, SF_TRUE
) ;
994 case SFC_SET_ADD_HEADER_PAD_CHUNK
:
997 case SFC_GET_LOG_INFO
:
999 return SFE_BAD_COMMAND_PARAM
;
1000 snprintf (data
, datasize
, "%s", psf
->logbuffer
) ;
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
) ;
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
) ;
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
) ;
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
) ;
1025 case SFC_GET_SIGNAL_MAX
:
1026 if (data
== NULL
|| datasize
!= sizeof (double))
1027 { psf
->error
= SFE_BAD_COMMAND_PARAM
;
1030 return psf_get_signal_max (psf
, (double *) data
) ;
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
;
1037 return psf_get_max_all_channels (psf
, (double*) data
) ;
1039 case SFC_UPDATE_HEADER_NOW
:
1040 if (psf
->write_header
)
1041 psf
->write_header (psf
, SF_TRUE
) ;
1044 case SFC_SET_UPDATE_HEADER_AUTO
:
1045 psf
->auto_header
= datasize
? SF_TRUE
: SF_FALSE
;
1046 return psf
->auto_header
;
1049 case SFC_SET_ADD_DITHER_ON_WRITE
:
1050 case SFC_SET_ADD_DITHER_ON_READ
:
1053 ** These are obsolete. Just return.
1054 ** Remove some time after version 1.0.8.
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
) ;
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
) ;
1074 case SFC_FILE_TRUNCATE
:
1075 if (psf
->file
.mode
!= SFM_WRITE
&& psf
->file
.mode
!= SFM_RDWR
)
1077 if (datasize
!= sizeof (sf_count_t
))
1079 if (data
== NULL
|| datasize
!= sizeof (sf_count_t
))
1080 { psf
->error
= SFE_BAD_COMMAND_PARAM
;
1084 { sf_count_t position
;
1086 position
= *((sf_count_t
*) data
) ;
1088 if (sf_seek (sndfile
, position
, SEEK_SET
) != position
)
1091 psf
->sf
.frames
= position
;
1093 position
= psf_fseek (psf
, 0, SEEK_CUR
) ;
1095 return psf_ftruncate (psf
, position
) ;
1099 case SFC_SET_RAW_START_OFFSET
:
1100 if (data
== NULL
|| datasize
!= sizeof (sf_count_t
))
1101 return (psf
->error
= SFE_BAD_COMMAND_PARAM
) ;
1103 if ((SF_CONTAINER (psf
->sf
.format
)) != SF_FORMAT_RAW
)
1104 return (psf
->error
= SFE_BAD_COMMAND_PARAM
) ;
1106 psf
->dataoffset
= *((sf_count_t
*) data
) ;
1107 sf_seek (sndfile
, 0, SEEK_CUR
) ;
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
) ;
1114 ((SF_EMBED_FILE_INFO
*) data
)->offset
= psf
->fileoffset
;
1115 ((SF_EMBED_FILE_INFO
*) data
)->length
= psf
->filelength
;
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
) ;
1126 return (psf
->error
= SFE_BAD_COMMAND_PARAM
) ;
1128 /* Lite remove end */
1130 case SFC_SET_CLIPPING
:
1131 psf
->add_clipping
= (datasize
) ? SF_TRUE
: SF_FALSE
;
1132 return psf
->add_clipping
;
1134 case SFC_GET_CLIPPING
:
1135 return psf
->add_clipping
;
1137 case SFC_GET_LOOP_INFO
:
1138 if (datasize
!= sizeof (SF_LOOP_INFO
) || data
== NULL
)
1139 { psf
->error
= SFE_BAD_COMMAND_PARAM
;
1142 if (psf
->loop_info
== NULL
)
1144 memcpy (data
, psf
->loop_info
, sizeof (SF_LOOP_INFO
)) ;
1147 case SFC_SET_BROADCAST_INFO
:
1148 { int format
= SF_CONTAINER (psf
->sf
.format
) ;
1150 /* Only WAV and RF64 supports the BEXT (Broadcast) chunk. */
1151 if (format
!= SF_FORMAT_WAV
&& format
!= SF_FORMAT_WAVEX
&& format
!= SF_FORMAT_RF64
)
1155 /* Only makes sense in SFM_WRITE or SFM_RDWR mode. */
1156 if ((psf
->file
.mode
!= SFM_WRITE
) && (psf
->file
.mode
!= SFM_RDWR
))
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
;
1165 if (psf
->broadcast_info
== NULL
)
1166 psf
->broadcast_info
= broadcast_info_alloc () ;
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
) ;
1171 broadcast_var_set (psf
, data
, datasize
) ;
1174 if (psf
->write_header
)
1175 psf
->write_header (psf
, SF_TRUE
) ;
1178 case SFC_GET_BROADCAST_INFO
:
1180 { psf
->error
= SFE_BAD_COMMAND_PARAM
;
1184 if (psf
->broadcast_info
== NULL
)
1186 return broadcast_info_copy (data
, psf
->broadcast_info
) ;
1188 return broadcast_var_get (psf
, data
, datasize
) ;
1191 case SFC_GET_INSTRUMENT
:
1192 if (datasize
!= sizeof (SF_INSTRUMENT
) || data
== NULL
)
1193 { psf
->error
= SFE_BAD_COMMAND_PARAM
;
1196 if (psf
->instrument
== NULL
)
1198 memcpy (data
, psf
->instrument
, sizeof (SF_INSTRUMENT
)) ;
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
;
1207 if (datasize
!= sizeof (SF_INSTRUMENT
) || data
== NULL
)
1208 { psf
->error
= SFE_BAD_COMMAND_PARAM
;
1212 if (psf
->instrument
== NULL
&& (psf
->instrument
= psf_instrument_alloc ()) == NULL
)
1213 { psf
->error
= SFE_MALLOC_FAILED
;
1216 memcpy (psf
->instrument
, data
, sizeof (SF_INSTRUMENT
)) ;
1219 case SFC_RAW_DATA_NEEDS_ENDSWAP
:
1220 return psf
->data_endswap
;
1222 case SFC_GET_CHANNEL_MAP_INFO
:
1223 if (psf
->channel_map
== NULL
)
1226 if (data
== NULL
|| datasize
!= SIGNED_SIZEOF (psf
->channel_map
[0]) * psf
->sf
.channels
)
1227 { psf
->error
= SFE_BAD_COMMAND_PARAM
;
1231 memcpy (data
, psf
->channel_map
, datasize
) ;
1234 case SFC_SET_CHANNEL_MAP_INFO
:
1235 if (psf
->have_written
)
1236 { psf
->error
= SFE_CMD_HAS_DATA
;
1239 if (data
== NULL
|| datasize
!= SIGNED_SIZEOF (psf
->channel_map
[0]) * psf
->sf
.channels
)
1240 { psf
->error
= SFE_BAD_COMMAND_PARAM
;
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
;
1254 free (psf
->channel_map
) ;
1255 if ((psf
->channel_map
= malloc (datasize
)) == NULL
)
1256 { psf
->error
= SFE_MALLOC_FAILED
;
1260 memcpy (psf
->channel_map
, data
, datasize
) ;
1263 ** Pass the command down to the container's command handler.
1264 ** Don't pass user data, use validated psf->channel_map data instead.
1267 return psf
->command (psf
, command
, NULL
, 0) ;
1271 /* Must be a file specific command. Pass it on. */
1273 return psf
->command (psf
, command
, data
, datasize
) ;
1275 psf_log_printf (psf
, "*** sf_command : cmd = 0x%X\n", command
) ;
1276 return (psf
->error
= SFE_BAD_COMMAND_PARAM
) ;
1282 /*------------------------------------------------------------------------------
1286 sf_seek (SNDFILE
*sndfile
, sf_count_t offset
, int whence
)
1288 sf_count_t seek_from_start
= 0, retval
;
1290 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1292 if (! psf
->sf
.seekable
)
1293 { psf
->error
= SFE_NOT_SEEKABLE
;
1294 return PSF_SEEK_ERROR
;
1297 /* If the whence parameter has a mode ORed in, check to see that
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
;
1306 /* Convert all SEEK_CUR and SEEK_END into seek_from_start to be
1307 ** used with SEEK_SET.
1310 { /* The SEEK_SET behaviour is independant of mode. */
1312 case SEEK_SET
| SFM_READ
:
1313 case SEEK_SET
| SFM_WRITE
:
1314 case SEEK_SET
| SFM_RDWR
:
1315 seek_from_start
= offset
;
1318 /* The SEEK_CUR is a little more tricky. */
1321 { if (psf
->file
.mode
== SFM_READ
)
1322 return psf
->read_current
;
1323 if (psf
->file
.mode
== SFM_WRITE
)
1324 return psf
->write_current
;
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
;
1331 psf
->error
= SFE_AMBIGUOUS_SEEK
;
1334 case SEEK_CUR
| SFM_READ
:
1336 return psf
->read_current
;
1337 seek_from_start
= psf
->read_current
+ offset
;
1340 case SEEK_CUR
| SFM_WRITE
:
1342 return psf
->write_current
;
1343 seek_from_start
= psf
->write_current
+ offset
;
1348 case SEEK_END
| SFM_READ
:
1349 case SEEK_END
| SFM_WRITE
:
1350 seek_from_start
= psf
->sf
.frames
+ offset
;
1354 psf
->error
= SFE_BAD_SEEK
;
1359 return PSF_SEEK_ERROR
;
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
;
1367 else if (seek_from_start
< 0 || seek_from_start
> psf
->sf
.frames
)
1368 { psf
->error
= SFE_BAD_SEEK
;
1369 return PSF_SEEK_ERROR
;
1373 { int new_mode
= (whence
& SFM_MASK
) ? (whence
& SFM_MASK
) : psf
->file
.mode
;
1375 retval
= psf
->seek (psf
, new_mode
, seek_from_start
) ;
1379 psf
->read_current
= retval
;
1382 psf
->write_current
= retval
;
1385 psf
->read_current
= retval
;
1386 psf
->write_current
= retval
;
1387 new_mode
= SFM_READ
;
1391 psf
->last_op
= new_mode
;
1396 psf
->error
= SFE_AMBIGUOUS_SEEK
;
1397 return PSF_SEEK_ERROR
;
1400 /*------------------------------------------------------------------------------
1404 sf_get_string (SNDFILE
*sndfile
, int str_type
)
1407 if ((psf
= (SF_PRIVATE
*) sndfile
) == NULL
)
1409 if (psf
->Magick
!= SNDFILE_MAGICK
)
1412 return psf_get_string (psf
, str_type
) ;
1413 } /* sf_get_string */
1416 sf_set_string (SNDFILE
*sndfile
, int str_type
, const char* str
)
1419 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1421 return psf_set_string (psf
, str_type
, str
) ;
1422 } /* sf_get_string */
1424 /*==============================================================================
1428 sf_read_raw (SNDFILE
*sndfile
, void *ptr
, sf_count_t bytes
)
1430 sf_count_t count
, extra
;
1431 int bytewidth
, blockwidth
;
1433 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1435 bytewidth
= (psf
->bytewidth
> 0) ? psf
->bytewidth
: 1 ;
1436 blockwidth
= (psf
->blockwidth
> 0) ? psf
->blockwidth
: 1 ;
1438 if (psf
->file
.mode
== SFM_WRITE
)
1439 { psf
->error
= SFE_NOT_READMODE
;
1443 if (bytes
< 0 || psf
->read_current
>= psf
->sf
.frames
)
1444 { psf_memset (ptr
, 0, bytes
) ;
1448 if (bytes
% (psf
->sf
.channels
* bytewidth
))
1449 { psf
->error
= SFE_BAD_READ_ALIGN
;
1453 if (psf
->last_op
!= SFM_READ
)
1454 if (psf
->seek (psf
, SFM_READ
, psf
->read_current
) < 0)
1457 count
= psf_fread (ptr
, 1, bytes
, psf
) ;
1459 if (psf
->read_current
+ count
/ blockwidth
<= psf
->sf
.frames
)
1460 psf
->read_current
+= count
/ blockwidth
;
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
;
1468 psf
->last_op
= SFM_READ
;
1473 /*------------------------------------------------------------------------------
1477 sf_read_short (SNDFILE
*sndfile
, short *ptr
, sf_count_t len
)
1479 sf_count_t count
, extra
;
1481 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1483 if (psf
->file
.mode
== SFM_WRITE
)
1484 { psf
->error
= SFE_NOT_READMODE
;
1488 if (len
% psf
->sf
.channels
)
1489 { psf
->error
= SFE_BAD_READ_ALIGN
;
1493 if (len
<= 0 || psf
->read_current
>= psf
->sf
.frames
)
1494 { psf_memset (ptr
, 0, len
* sizeof (short)) ;
1495 return 0 ; /* End of file. */
1498 if (psf
->read_short
== NULL
|| psf
->seek
== NULL
)
1499 { psf
->error
= SFE_UNIMPLEMENTED
;
1503 if (psf
->last_op
!= SFM_READ
)
1504 if (psf
->seek (psf
, SFM_READ
, psf
->read_current
) < 0)
1507 count
= psf
->read_short (psf
, ptr
, len
) ;
1509 if (psf
->read_current
+ count
/ psf
->sf
.channels
<= psf
->sf
.frames
)
1510 psf
->read_current
+= count
/ psf
->sf
.channels
;
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
;
1518 psf
->last_op
= SFM_READ
;
1521 } /* sf_read_short */
1524 sf_readf_short (SNDFILE
*sndfile
, short *ptr
, sf_count_t frames
)
1526 sf_count_t count
, extra
;
1528 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1530 if (psf
->file
.mode
== SFM_WRITE
)
1531 { psf
->error
= SFE_NOT_READMODE
;
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. */
1540 if (psf
->read_short
== NULL
|| psf
->seek
== NULL
)
1541 { psf
->error
= SFE_UNIMPLEMENTED
;
1545 if (psf
->last_op
!= SFM_READ
)
1546 if (psf
->seek (psf
, SFM_READ
, psf
->read_current
) < 0)
1549 count
= psf
->read_short (psf
, ptr
, frames
* psf
->sf
.channels
) ;
1551 if (psf
->read_current
+ count
/ psf
->sf
.channels
<= psf
->sf
.frames
)
1552 psf
->read_current
+= count
/ psf
->sf
.channels
;
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
;
1560 psf
->last_op
= SFM_READ
;
1562 return count
/ psf
->sf
.channels
;
1563 } /* sf_readf_short */
1565 /*------------------------------------------------------------------------------
1569 sf_read_int (SNDFILE
*sndfile
, int *ptr
, sf_count_t len
)
1571 sf_count_t count
, extra
;
1573 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1575 if (psf
->file
.mode
== SFM_WRITE
)
1576 { psf
->error
= SFE_NOT_READMODE
;
1580 if (len
% psf
->sf
.channels
)
1581 { psf
->error
= SFE_BAD_READ_ALIGN
;
1585 if (len
<= 0 || psf
->read_current
>= psf
->sf
.frames
)
1586 { psf_memset (ptr
, 0, len
* sizeof (int)) ;
1590 if (psf
->read_int
== NULL
|| psf
->seek
== NULL
)
1591 { psf
->error
= SFE_UNIMPLEMENTED
;
1595 if (psf
->last_op
!= SFM_READ
)
1596 if (psf
->seek (psf
, SFM_READ
, psf
->read_current
) < 0)
1599 count
= psf
->read_int (psf
, ptr
, len
) ;
1601 if (psf
->read_current
+ count
/ psf
->sf
.channels
<= psf
->sf
.frames
)
1602 psf
->read_current
+= count
/ psf
->sf
.channels
;
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
;
1610 psf
->last_op
= SFM_READ
;
1616 sf_readf_int (SNDFILE
*sndfile
, int *ptr
, sf_count_t frames
)
1618 sf_count_t count
, extra
;
1620 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1622 if (psf
->file
.mode
== SFM_WRITE
)
1623 { psf
->error
= SFE_NOT_READMODE
;
1627 if (frames
<= 0 || psf
->read_current
>= psf
->sf
.frames
)
1628 { psf_memset (ptr
, 0, frames
* psf
->sf
.channels
* sizeof (int)) ;
1632 if (psf
->read_int
== NULL
|| psf
->seek
== NULL
)
1633 { psf
->error
= SFE_UNIMPLEMENTED
;
1637 if (psf
->last_op
!= SFM_READ
)
1638 if (psf
->seek (psf
, SFM_READ
, psf
->read_current
) < 0)
1641 count
= psf
->read_int (psf
, ptr
, frames
* psf
->sf
.channels
) ;
1643 if (psf
->read_current
+ count
/ psf
->sf
.channels
<= psf
->sf
.frames
)
1644 psf
->read_current
+= count
/ psf
->sf
.channels
;
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
;
1652 psf
->last_op
= SFM_READ
;
1654 return count
/ psf
->sf
.channels
;
1655 } /* sf_readf_int */
1657 /*------------------------------------------------------------------------------
1661 sf_read_float (SNDFILE
*sndfile
, float *ptr
, sf_count_t len
)
1663 sf_count_t count
, extra
;
1665 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1667 if (psf
->file
.mode
== SFM_WRITE
)
1668 { psf
->error
= SFE_NOT_READMODE
;
1672 if (len
% psf
->sf
.channels
)
1673 { psf
->error
= SFE_BAD_READ_ALIGN
;
1677 if (len
<= 0 || psf
->read_current
>= psf
->sf
.frames
)
1678 { psf_memset (ptr
, 0, len
* sizeof (float)) ;
1682 if (psf
->read_float
== NULL
|| psf
->seek
== NULL
)
1683 { psf
->error
= SFE_UNIMPLEMENTED
;
1687 if (psf
->last_op
!= SFM_READ
)
1688 if (psf
->seek (psf
, SFM_READ
, psf
->read_current
) < 0)
1691 count
= psf
->read_float (psf
, ptr
, len
) ;
1693 if (psf
->read_current
+ count
/ psf
->sf
.channels
<= psf
->sf
.frames
)
1694 psf
->read_current
+= count
/ psf
->sf
.channels
;
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
;
1702 psf
->last_op
= SFM_READ
;
1705 } /* sf_read_float */
1708 sf_readf_float (SNDFILE
*sndfile
, float *ptr
, sf_count_t frames
)
1710 sf_count_t count
, extra
;
1712 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1714 if (psf
->file
.mode
== SFM_WRITE
)
1715 { psf
->error
= SFE_NOT_READMODE
;
1719 if (frames
<= 0 || psf
->read_current
>= psf
->sf
.frames
)
1720 { psf_memset (ptr
, 0, frames
* psf
->sf
.channels
* sizeof (float)) ;
1724 if (psf
->read_float
== NULL
|| psf
->seek
== NULL
)
1725 { psf
->error
= SFE_UNIMPLEMENTED
;
1729 if (psf
->last_op
!= SFM_READ
)
1730 if (psf
->seek (psf
, SFM_READ
, psf
->read_current
) < 0)
1733 count
= psf
->read_float (psf
, ptr
, frames
* psf
->sf
.channels
) ;
1735 if (psf
->read_current
+ count
/ psf
->sf
.channels
<= psf
->sf
.frames
)
1736 psf
->read_current
+= count
/ psf
->sf
.channels
;
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
;
1744 psf
->last_op
= SFM_READ
;
1746 return count
/ psf
->sf
.channels
;
1747 } /* sf_readf_float */
1749 /*------------------------------------------------------------------------------
1753 sf_read_double (SNDFILE
*sndfile
, double *ptr
, sf_count_t len
)
1755 sf_count_t count
, extra
;
1757 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1759 if (psf
->file
.mode
== SFM_WRITE
)
1760 { psf
->error
= SFE_NOT_READMODE
;
1764 if (len
% psf
->sf
.channels
)
1765 { psf
->error
= SFE_BAD_READ_ALIGN
;
1769 if (len
<= 0 || psf
->read_current
>= psf
->sf
.frames
)
1770 { psf_memset (ptr
, 0, len
* sizeof (double)) ;
1774 if (psf
->read_double
== NULL
|| psf
->seek
== NULL
)
1775 { psf
->error
= SFE_UNIMPLEMENTED
;
1779 if (psf
->last_op
!= SFM_READ
)
1780 if (psf
->seek (psf
, SFM_READ
, psf
->read_current
) < 0)
1783 count
= psf
->read_double (psf
, ptr
, len
) ;
1785 if (psf
->read_current
+ count
/ psf
->sf
.channels
<= psf
->sf
.frames
)
1786 psf
->read_current
+= count
/ psf
->sf
.channels
;
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
;
1794 psf
->last_op
= SFM_READ
;
1797 } /* sf_read_double */
1800 sf_readf_double (SNDFILE
*sndfile
, double *ptr
, sf_count_t frames
)
1802 sf_count_t count
, extra
;
1804 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1806 if (psf
->file
.mode
== SFM_WRITE
)
1807 { psf
->error
= SFE_NOT_READMODE
;
1811 if (frames
<= 0 || psf
->read_current
>= psf
->sf
.frames
)
1812 { psf_memset (ptr
, 0, frames
* psf
->sf
.channels
* sizeof (double)) ;
1816 if (psf
->read_double
== NULL
|| psf
->seek
== NULL
)
1817 { psf
->error
= SFE_UNIMPLEMENTED
;
1821 if (psf
->last_op
!= SFM_READ
)
1822 if (psf
->seek (psf
, SFM_READ
, psf
->read_current
) < 0)
1825 count
= psf
->read_double (psf
, ptr
, frames
* psf
->sf
.channels
) ;
1827 if (psf
->read_current
+ count
/ psf
->sf
.channels
<= psf
->sf
.frames
)
1828 psf
->read_current
+= count
/ psf
->sf
.channels
;
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
;
1836 psf
->last_op
= SFM_READ
;
1838 return count
/ psf
->sf
.channels
;
1839 } /* sf_readf_double */
1841 /*------------------------------------------------------------------------------
1845 sf_write_raw (SNDFILE
*sndfile
, const void *ptr
, sf_count_t len
)
1848 int bytewidth
, blockwidth
;
1850 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1852 bytewidth
= (psf
->bytewidth
> 0) ? psf
->bytewidth
: 1 ;
1853 blockwidth
= (psf
->blockwidth
> 0) ? psf
->blockwidth
: 1 ;
1855 if (psf
->file
.mode
== SFM_READ
)
1856 { psf
->error
= SFE_NOT_WRITEMODE
;
1860 if (len
% (psf
->sf
.channels
* bytewidth
))
1861 { psf
->error
= SFE_BAD_WRITE_ALIGN
;
1865 if (psf
->last_op
!= SFM_WRITE
)
1866 if (psf
->seek (psf
, SFM_WRITE
, psf
->write_current
) < 0)
1869 if (psf
->have_written
== SF_FALSE
&& psf
->write_header
!= NULL
)
1870 psf
->write_header (psf
, SF_FALSE
) ;
1871 psf
->have_written
= SF_TRUE
;
1873 count
= psf_fwrite (ptr
, 1, len
, psf
) ;
1875 psf
->write_current
+= count
/ blockwidth
;
1877 psf
->last_op
= SFM_WRITE
;
1879 if (psf
->auto_header
&& psf
->write_header
!= NULL
)
1880 psf
->write_header (psf
, SF_TRUE
) ;
1882 if (psf
->write_current
> psf
->sf
.frames
)
1883 psf
->sf
.frames
= psf
->write_current
;
1886 } /* sf_write_raw */
1888 /*------------------------------------------------------------------------------
1892 sf_write_short (SNDFILE
*sndfile
, const short *ptr
, sf_count_t len
)
1896 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1898 if (psf
->file
.mode
== SFM_READ
)
1899 { psf
->error
= SFE_NOT_WRITEMODE
;
1903 if (len
% psf
->sf
.channels
)
1904 { psf
->error
= SFE_BAD_WRITE_ALIGN
;
1908 if (psf
->write_short
== NULL
|| psf
->seek
== NULL
)
1909 { psf
->error
= SFE_UNIMPLEMENTED
;
1913 if (psf
->last_op
!= SFM_WRITE
)
1914 if (psf
->seek (psf
, SFM_WRITE
, psf
->write_current
) < 0)
1917 if (psf
->have_written
== SF_FALSE
&& psf
->write_header
!= NULL
)
1918 psf
->write_header (psf
, SF_FALSE
) ;
1919 psf
->have_written
= SF_TRUE
;
1921 count
= psf
->write_short (psf
, ptr
, len
) ;
1923 psf
->write_current
+= count
/ psf
->sf
.channels
;
1925 psf
->last_op
= SFM_WRITE
;
1927 if (psf
->auto_header
&& psf
->write_header
!= NULL
)
1928 psf
->write_header (psf
, SF_TRUE
) ;
1930 if (psf
->write_current
> psf
->sf
.frames
)
1931 psf
->sf
.frames
= psf
->write_current
;
1934 } /* sf_write_short */
1937 sf_writef_short (SNDFILE
*sndfile
, const short *ptr
, sf_count_t frames
)
1941 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1943 if (psf
->file
.mode
== SFM_READ
)
1944 { psf
->error
= SFE_NOT_WRITEMODE
;
1948 if (psf
->write_short
== NULL
|| psf
->seek
== NULL
)
1949 { psf
->error
= SFE_UNIMPLEMENTED
;
1953 if (psf
->last_op
!= SFM_WRITE
)
1954 if (psf
->seek (psf
, SFM_WRITE
, psf
->write_current
) < 0)
1957 if (psf
->have_written
== SF_FALSE
&& psf
->write_header
!= NULL
)
1958 psf
->write_header (psf
, SF_FALSE
) ;
1959 psf
->have_written
= SF_TRUE
;
1961 count
= psf
->write_short (psf
, ptr
, frames
* psf
->sf
.channels
) ;
1963 psf
->write_current
+= count
/ psf
->sf
.channels
;
1965 psf
->last_op
= SFM_WRITE
;
1967 if (psf
->auto_header
&& psf
->write_header
!= NULL
)
1968 psf
->write_header (psf
, SF_TRUE
) ;
1970 if (psf
->write_current
> psf
->sf
.frames
)
1971 psf
->sf
.frames
= psf
->write_current
;
1973 return count
/ psf
->sf
.channels
;
1974 } /* sf_writef_short */
1976 /*------------------------------------------------------------------------------
1980 sf_write_int (SNDFILE
*sndfile
, const int *ptr
, sf_count_t len
)
1984 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
1986 if (psf
->file
.mode
== SFM_READ
)
1987 { psf
->error
= SFE_NOT_WRITEMODE
;
1991 if (len
% psf
->sf
.channels
)
1992 { psf
->error
= SFE_BAD_WRITE_ALIGN
;
1996 if (psf
->write_int
== NULL
|| psf
->seek
== NULL
)
1997 { psf
->error
= SFE_UNIMPLEMENTED
;
2001 if (psf
->last_op
!= SFM_WRITE
)
2002 if (psf
->seek (psf
, SFM_WRITE
, psf
->write_current
) < 0)
2005 if (psf
->have_written
== SF_FALSE
&& psf
->write_header
!= NULL
)
2006 psf
->write_header (psf
, SF_FALSE
) ;
2007 psf
->have_written
= SF_TRUE
;
2009 count
= psf
->write_int (psf
, ptr
, len
) ;
2011 psf
->write_current
+= count
/ psf
->sf
.channels
;
2013 psf
->last_op
= SFM_WRITE
;
2015 if (psf
->auto_header
&& psf
->write_header
!= NULL
)
2016 psf
->write_header (psf
, SF_TRUE
) ;
2018 if (psf
->write_current
> psf
->sf
.frames
)
2019 psf
->sf
.frames
= psf
->write_current
;
2022 } /* sf_write_int */
2025 sf_writef_int (SNDFILE
*sndfile
, const int *ptr
, sf_count_t frames
)
2029 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
2031 if (psf
->file
.mode
== SFM_READ
)
2032 { psf
->error
= SFE_NOT_WRITEMODE
;
2036 if (psf
->write_int
== NULL
|| psf
->seek
== NULL
)
2037 { psf
->error
= SFE_UNIMPLEMENTED
;
2041 if (psf
->last_op
!= SFM_WRITE
)
2042 if (psf
->seek (psf
, SFM_WRITE
, psf
->write_current
) < 0)
2045 if (psf
->have_written
== SF_FALSE
&& psf
->write_header
!= NULL
)
2046 psf
->write_header (psf
, SF_FALSE
) ;
2047 psf
->have_written
= SF_TRUE
;
2049 count
= psf
->write_int (psf
, ptr
, frames
* psf
->sf
.channels
) ;
2051 psf
->write_current
+= count
/ psf
->sf
.channels
;
2053 psf
->last_op
= SFM_WRITE
;
2055 if (psf
->auto_header
&& psf
->write_header
!= NULL
)
2056 psf
->write_header (psf
, SF_TRUE
) ;
2058 if (psf
->write_current
> psf
->sf
.frames
)
2059 psf
->sf
.frames
= psf
->write_current
;
2061 return count
/ psf
->sf
.channels
;
2062 } /* sf_writef_int */
2064 /*------------------------------------------------------------------------------
2068 sf_write_float (SNDFILE
*sndfile
, const float *ptr
, sf_count_t len
)
2072 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
2074 if (psf
->file
.mode
== SFM_READ
)
2075 { psf
->error
= SFE_NOT_WRITEMODE
;
2079 if (len
% psf
->sf
.channels
)
2080 { psf
->error
= SFE_BAD_WRITE_ALIGN
;
2084 if (psf
->write_float
== NULL
|| psf
->seek
== NULL
)
2085 { psf
->error
= SFE_UNIMPLEMENTED
;
2089 if (psf
->last_op
!= SFM_WRITE
)
2090 if (psf
->seek (psf
, SFM_WRITE
, psf
->write_current
) < 0)
2093 if (psf
->have_written
== SF_FALSE
&& psf
->write_header
!= NULL
)
2094 psf
->write_header (psf
, SF_FALSE
) ;
2095 psf
->have_written
= SF_TRUE
;
2097 count
= psf
->write_float (psf
, ptr
, len
) ;
2099 psf
->write_current
+= count
/ psf
->sf
.channels
;
2101 psf
->last_op
= SFM_WRITE
;
2103 if (psf
->auto_header
&& psf
->write_header
!= NULL
)
2104 psf
->write_header (psf
, SF_TRUE
) ;
2106 if (psf
->write_current
> psf
->sf
.frames
)
2107 psf
->sf
.frames
= psf
->write_current
;
2110 } /* sf_write_float */
2113 sf_writef_float (SNDFILE
*sndfile
, const float *ptr
, sf_count_t frames
)
2117 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
2119 if (psf
->file
.mode
== SFM_READ
)
2120 { psf
->error
= SFE_NOT_WRITEMODE
;
2124 if (psf
->write_float
== NULL
|| psf
->seek
== NULL
)
2125 { psf
->error
= SFE_UNIMPLEMENTED
;
2129 if (psf
->last_op
!= SFM_WRITE
)
2130 if (psf
->seek (psf
, SFM_WRITE
, psf
->write_current
) < 0)
2133 if (psf
->have_written
== SF_FALSE
&& psf
->write_header
!= NULL
)
2134 psf
->write_header (psf
, SF_FALSE
) ;
2135 psf
->have_written
= SF_TRUE
;
2137 count
= psf
->write_float (psf
, ptr
, frames
* psf
->sf
.channels
) ;
2139 psf
->write_current
+= count
/ psf
->sf
.channels
;
2141 psf
->last_op
= SFM_WRITE
;
2143 if (psf
->auto_header
&& psf
->write_header
!= NULL
)
2144 psf
->write_header (psf
, SF_TRUE
) ;
2146 if (psf
->write_current
> psf
->sf
.frames
)
2147 psf
->sf
.frames
= psf
->write_current
;
2149 return count
/ psf
->sf
.channels
;
2150 } /* sf_writef_float */
2152 /*------------------------------------------------------------------------------
2156 sf_write_double (SNDFILE
*sndfile
, const double *ptr
, sf_count_t len
)
2160 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
2162 if (psf
->file
.mode
== SFM_READ
)
2163 { psf
->error
= SFE_NOT_WRITEMODE
;
2167 if (len
% psf
->sf
.channels
)
2168 { psf
->error
= SFE_BAD_WRITE_ALIGN
;
2172 if (psf
->write_double
== NULL
|| psf
->seek
== NULL
)
2173 { psf
->error
= SFE_UNIMPLEMENTED
;
2177 if (psf
->last_op
!= SFM_WRITE
)
2178 if (psf
->seek (psf
, SFM_WRITE
, psf
->write_current
) < 0)
2181 if (psf
->have_written
== SF_FALSE
&& psf
->write_header
!= NULL
)
2182 psf
->write_header (psf
, SF_FALSE
) ;
2183 psf
->have_written
= SF_TRUE
;
2185 count
= psf
->write_double (psf
, ptr
, len
) ;
2187 psf
->write_current
+= count
/ psf
->sf
.channels
;
2189 psf
->last_op
= SFM_WRITE
;
2191 if (psf
->auto_header
&& psf
->write_header
!= NULL
)
2192 psf
->write_header (psf
, SF_TRUE
) ;
2194 if (psf
->write_current
> psf
->sf
.frames
)
2195 psf
->sf
.frames
= psf
->write_current
;
2198 } /* sf_write_double */
2201 sf_writef_double (SNDFILE
*sndfile
, const double *ptr
, sf_count_t frames
)
2205 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile
, psf
, 1) ;
2207 if (psf
->file
.mode
== SFM_READ
)
2208 { psf
->error
= SFE_NOT_WRITEMODE
;
2212 if (psf
->write_double
== NULL
|| psf
->seek
== NULL
)
2213 { psf
->error
= SFE_UNIMPLEMENTED
;
2217 if (psf
->last_op
!= SFM_WRITE
)
2218 if (psf
->seek (psf
, SFM_WRITE
, psf
->write_current
) < 0)
2221 if (psf
->have_written
== SF_FALSE
&& psf
->write_header
!= NULL
)
2222 psf
->write_header (psf
, SF_FALSE
) ;
2223 psf
->have_written
= SF_TRUE
;
2225 count
= psf
->write_double (psf
, ptr
, frames
* psf
->sf
.channels
) ;
2227 psf
->write_current
+= count
/ psf
->sf
.channels
;
2229 psf
->last_op
= SFM_WRITE
;
2231 if (psf
->auto_header
&& psf
->write_header
!= NULL
)
2232 psf
->write_header (psf
, SF_TRUE
) ;
2234 if (psf
->write_current
> psf
->sf
.frames
)
2235 psf
->sf
.frames
= psf
->write_current
;
2237 return count
/ psf
->sf
.channels
;
2238 } /* sf_writef_double */
2240 /*=========================================================================
2241 ** Private functions.
2245 try_resource_fork (SF_PRIVATE
* psf
)
2246 { int old_error
= psf
->error
;
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
;
2255 /* More checking here. */
2256 psf_log_printf (psf
, "Resource fork : %s\n", psf
->rsrc
.path
.c
) ;
2258 return SF_FORMAT_SD2
;
2259 } /* try_resource_fork */
2262 format_from_extension (SF_PRIVATE
*psf
)
2267 if ((cptr
= strrchr (psf
->file
.name
.c
, '.')) == NULL
)
2271 if (strlen (cptr
) > sizeof (buffer
) - 1)
2274 psf_strlcpy (buffer
, sizeof (buffer
), cptr
) ;
2275 buffer
[sizeof (buffer
) - 1] = 0 ;
2277 /* Convert everything in the buffer to lower case. */
2280 { *cptr
= tolower (*cptr
) ;
2286 if (strcmp (cptr
, "au") == 0)
2287 { psf
->sf
.channels
= 1 ;
2288 psf
->sf
.samplerate
= 8000 ;
2289 format
= SF_FORMAT_RAW
| SF_FORMAT_ULAW
;
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
;
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
;
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
;
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
;
2313 /* For RAW files, make sure the dataoffset if set correctly. */
2314 if ((SF_CONTAINER (format
)) == SF_FORMAT_RAW
)
2315 psf
->dataoffset
= 0 ;
2318 } /* format_from_extension */
2321 guess_file_type (SF_PRIVATE
*psf
)
2322 { int buffer
[3], format
;
2324 if (psf_binheader_readf (psf
, "b", &buffer
, SIGNED_SIZEOF (buffer
)) != SIGNED_SIZEOF (buffer
))
2325 { psf
->error
= SFE_BAD_FILE_READ
;
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
;
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
;
2341 if (buffer
[0] == MAKE_MARKER ('.', 's', 'n', 'd') || buffer
[0] == MAKE_MARKER ('d', 'n', 's', '.'))
2342 return SF_FORMAT_AU
;
2344 if ((buffer
[0] == MAKE_MARKER ('f', 'a', 'p', ' ') || buffer
[0] == MAKE_MARKER (' ', 'p', 'a', 'f')))
2345 return SF_FORMAT_PAF
;
2347 if (buffer
[0] == MAKE_MARKER ('N', 'I', 'S', 'T'))
2348 return SF_FORMAT_NIST
;
2350 if (buffer
[0] == MAKE_MARKER ('C', 'r', 'e', 'a') && buffer
[1] == MAKE_MARKER ('t', 'i', 'v', 'e'))
2351 return SF_FORMAT_VOC
;
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
;
2357 if (buffer
[0] == MAKE_MARKER ('r', 'i', 'f', 'f'))
2358 return SF_FORMAT_W64
;
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
;
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
;
2368 if (buffer
[0] == MAKE_MARKER ('M', 'A', 'T', 'L') && buffer
[1] == MAKE_MARKER ('A', 'B', ' ', '5'))
2369 return SF_FORMAT_MAT5
;
2371 if (buffer
[0] == MAKE_MARKER ('P', 'V', 'F', '1'))
2372 return SF_FORMAT_PVF
;
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
;
2378 if (buffer
[0] == MAKE_MARKER ('c', 'a', 'f', 'f') && buffer
[2] == MAKE_MARKER ('d', 'e', 's', 'c'))
2379 return SF_FORMAT_CAF
;
2381 if (buffer
[0] == MAKE_MARKER ('O', 'g', 'g', 'S'))
2382 return SF_FORMAT_OGG
;
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
;
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
;
2392 if (buffer
[0] == MAKE_MARKER ('L', 'M', '8', '9') || buffer
[0] == MAKE_MARKER ('5', '3', 0, 0))
2393 return SF_FORMAT_TXW
;
2395 if ((buffer
[0] & MAKE_MARKER (0xFF, 0xFF, 0x80, 0xFF)) == MAKE_MARKER (0xF0, 0x7E, 0, 0x01))
2396 return SF_FORMAT_SDS
;
2398 if ((buffer
[0] & MAKE_MARKER (0xFF, 0xFF, 0, 0)) == MAKE_MARKER (1, 4, 0, 0))
2399 return SF_FORMAT_MPC2K
;
2401 if (buffer
[0] == MAKE_MARKER ('C', 'A', 'T', ' ') && buffer
[2] == MAKE_MARKER ('R', 'E', 'X', '2'))
2402 return SF_FORMAT_REX2
;
2404 if (buffer
[0] == MAKE_MARKER (0x30, 0x26, 0xB2, 0x75) && buffer
[1] == MAKE_MARKER (0x8E, 0x66, 0xCF, 0x11))
2405 return 0 /*-SF_FORMAT_WMA-*/ ;
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
;
2411 if (buffer
[0] == MAKE_MARKER ('f', 'L', 'a', 'C'))
2412 return SF_FORMAT_FLAC
;
2414 if (buffer
[0] == MAKE_MARKER ('2', 'B', 'I', 'T'))
2415 return SF_FORMAT_AVR
;
2417 if (buffer
[0] == MAKE_MARKER ('R', 'F', '6', '4') && buffer
[2] == MAKE_MARKER ('W', 'A', 'V', 'E'))
2418 return SF_FORMAT_RF64
;
2420 if (buffer
[0] == MAKE_MARKER ('I', 'D', '3', 3))
2421 { psf_log_printf (psf
, "Found 'ID3' marker.\n") ;
2423 return guess_file_type (psf
) ;
2427 /* Turtle Beach SMP 16-bit */
2428 if (buffer
[0] == MAKE_MARKER ('S', 'O', 'U', 'N') && buffer
[1] == MAKE_MARKER ('D', ' ', 'S', 'A'))
2431 /* Yamaha sampler format. */
2432 if (buffer
[0] == MAKE_MARKER ('S', 'Y', '8', '0') || buffer
[0] == MAKE_MARKER ('S', 'Y', '8', '5'))
2435 if (buffer
[0] == MAKE_MARKER ('a', 'j', 'k', 'g'))
2436 return 0 /*-SF_FORMAT_SHN-*/ ;
2438 /* This must be the last one. */
2439 if (psf
->filelength
> 0 && (format
= try_resource_fork (psf
)) != 0)
2443 } /* guess_file_type */
2447 validate_sfinfo (SF_INFO
*sfinfo
)
2448 { if (sfinfo
->samplerate
< 1)
2450 if (sfinfo
->frames
< 0)
2452 if (sfinfo
->channels
< 1)
2454 if ((SF_CONTAINER (sfinfo
->format
)) == 0)
2456 if ((SF_CODEC (sfinfo
->format
)) == 0)
2458 if (sfinfo
->sections
< 1)
2461 } /* validate_sfinfo */
2464 validate_psf (SF_PRIVATE
*psf
)
2466 if (psf
->datalength
< 0)
2467 { psf_log_printf (psf
, "Invalid SF_PRIVATE field : datalength == %D.\n", psf
->datalength
) ;
2470 if (psf
->dataoffset
< 0)
2471 { psf_log_printf (psf
, "Invalid SF_PRIVATE field : dataoffset == %D.\n", psf
->dataoffset
) ;
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
) ;
2480 } /* validate_psf */
2483 save_header_info (SF_PRIVATE
*psf
)
2484 { snprintf (sf_logbuffer
, sizeof (sf_logbuffer
), "%s", psf
->logbuffer
) ;
2485 } /* save_header_info */
2488 copy_filename (SF_PRIVATE
*psf
, const char *path
)
2489 { const char *ccptr
;
2492 snprintf (psf
->file
.path
.c
, sizeof (psf
->file
.path
.c
), "%s", path
) ;
2493 if ((ccptr
= strrchr (path
, '/')) || (ccptr
= strrchr (path
, '\\')))
2498 snprintf (psf
->file
.name
.c
, sizeof (psf
->file
.name
.c
), "%s", ccptr
) ;
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
, '\\')))
2505 psf
->file
.dir
.c
[0] = 0 ;
2508 } /* copy_filename */
2510 /*==============================================================================
2514 psf_close (SF_PRIVATE
*psf
)
2517 if (psf
->codec_close
)
2518 error
= psf
->codec_close (psf
) ;
2519 if (psf
->container_close
)
2520 error
= psf
->container_close (psf
) ;
2522 error
= psf_fclose (psf
) ;
2523 psf_close_rsrc (psf
) ;
2525 if (psf
->container_data
)
2526 free (psf
->container_data
) ;
2528 if (psf
->codec_data
)
2529 free (psf
->codec_data
) ;
2531 if (psf
->interleave
)
2532 free (psf
->interleave
) ;
2535 free (psf
->dither
) ;
2538 free (psf
->peak_info
) ;
2540 if (psf
->broadcast_16k
)
2541 free (psf
->broadcast_16k
) ;
2544 free (psf
->loop_info
) ;
2546 if (psf
->instrument
)
2547 free (psf
->instrument
) ;
2549 if (psf
->channel_map
)
2550 free (psf
->channel_map
) ;
2552 if (psf
->format_desc
)
2553 { psf
->format_desc
[0] = 0 ;
2554 free (psf
->format_desc
) ;
2557 memset (psf
, 0, sizeof (SF_PRIVATE
)) ;
2564 psf_open_file (SF_PRIVATE
*psf
, SF_INFO
*sfinfo
)
2565 { int error
, format
;
2567 sf_errno
= error
= 0 ;
2568 sf_logbuffer
[0] = 0 ;
2571 { error
= psf
->error
;
2575 if (psf
->file
.mode
!= SFM_READ
&& psf
->file
.mode
!= SFM_WRITE
&& psf
->file
.mode
!= SFM_RDWR
)
2576 { error
= SFE_BAD_OPEN_MODE
;
2581 { error
= SFE_BAD_SF_INFO_PTR
;
2585 /* Zero out these fields. */
2586 sfinfo
->frames
= 0 ;
2587 sfinfo
->sections
= 0 ;
2588 sfinfo
->seekable
= 0 ;
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
;
2598 memset (sfinfo
, 0, sizeof (SF_INFO
)) ;
2601 memcpy (&(psf
->sf
), sfinfo
, sizeof (SF_INFO
)) ;
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 ;
2616 /* An attempt at a per SF_PRIVATE unique id. */
2617 psf
->unique_id
= psf_rand_int32 () ;
2619 psf
->sf
.sections
= 1 ;
2621 psf
->is_pipe
= psf_is_pipe (psf
) ;
2624 { psf
->sf
.seekable
= SF_FALSE
;
2625 psf
->filelength
= SF_COUNT_MAX
;
2628 { psf
->sf
.seekable
= SF_TRUE
;
2630 /* File is open, so get the length. */
2631 psf
->filelength
= psf_get_filelen (psf
) ;
2634 if (psf
->fileoffset
> 0)
2635 { switch (psf
->file
.mode
)
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
;
2645 psf
->fileoffset
= 0 ;
2646 psf_fseek (psf
, 0, SEEK_END
) ;
2647 psf
->fileoffset
= psf_ftell (psf
) ;
2651 error
= SFE_NO_EMBEDDED_RDWR
;
2655 psf_log_printf (psf
, "Embedded file offset : %D\n", psf
->fileoffset
) ;
2658 if (psf
->filelength
== SF_COUNT_MAX
)
2659 psf_log_printf (psf
, "Length : unknown\n") ;
2661 psf_log_printf (psf
, "Length : %D\n", psf
->filelength
) ;
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.
2667 if ((SF_CONTAINER (psf
->sf
.format
)) == 0)
2668 { error
= SFE_ZERO_MAJOR_FORMAT
;
2671 if ((SF_CODEC (psf
->sf
.format
)) == 0)
2672 { error
= SFE_ZERO_MINOR_FORMAT
;
2676 if (sf_format_check (&(psf
->sf
)) == 0)
2677 { error
= SFE_BAD_OPEN_FORMAT
;
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
) ;
2685 if (psf
->sf
.format
== 0)
2686 psf
->sf
.format
= format_from_extension (psf
) ;
2689 /* Prevent unnecessary seeks */
2690 psf
->last_op
= psf
->file
.mode
;
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 ;
2702 case SF_FORMAT_PCM_16
:
2703 case SF_FORMAT_DPCM_16
:
2704 psf
->bytewidth
= 2 ;
2707 case SF_FORMAT_PCM_24
:
2708 psf
->bytewidth
= 3 ;
2711 case SF_FORMAT_PCM_32
:
2712 case SF_FORMAT_FLOAT
:
2713 psf
->bytewidth
= 4 ;
2716 case SF_FORMAT_DOUBLE
:
2717 psf
->bytewidth
= 8 ;
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
) ;
2728 case SF_FORMAT_AIFF
:
2729 error
= aiff_open (psf
) ;
2733 error
= au_open (psf
) ;
2736 case SF_FORMAT_RAW
:
2737 error
= raw_open (psf
) ;
2740 case SF_FORMAT_W64
:
2741 error
= w64_open (psf
) ;
2744 case SF_FORMAT_RF64
:
2745 error
= rf64_open (psf
) ;
2748 /* Lite remove start */
2749 case SF_FORMAT_PAF
:
2750 error
= paf_open (psf
) ;
2753 case SF_FORMAT_SVX
:
2754 error
= svx_open (psf
) ;
2757 case SF_FORMAT_NIST
:
2758 error
= nist_open (psf
) ;
2761 case SF_FORMAT_IRCAM
:
2762 error
= ircam_open (psf
) ;
2765 case SF_FORMAT_VOC
:
2766 error
= voc_open (psf
) ;
2769 case SF_FORMAT_SDS
:
2770 error
= sds_open (psf
) ;
2773 case SF_FORMAT_OGG
:
2774 error
= ogg_open (psf
) ;
2777 case SF_FORMAT_TXW
:
2778 error
= txw_open (psf
) ;
2781 case SF_FORMAT_WVE
:
2782 error
= wve_open (psf
) ;
2785 case SF_FORMAT_DWD
:
2786 error
= dwd_open (psf
) ;
2789 case SF_FORMAT_MAT4
:
2790 error
= mat4_open (psf
) ;
2793 case SF_FORMAT_MAT5
:
2794 error
= mat5_open (psf
) ;
2797 case SF_FORMAT_PVF
:
2798 error
= pvf_open (psf
) ;
2802 error
= xi_open (psf
) ;
2805 case SF_FORMAT_HTK
:
2806 error
= htk_open (psf
) ;
2809 case SF_FORMAT_SD2
:
2810 error
= sd2_open (psf
) ;
2813 case SF_FORMAT_REX2
:
2814 error
= rx2_open (psf
) ;
2817 case SF_FORMAT_AVR
:
2818 error
= avr_open (psf
) ;
2821 case SF_FORMAT_FLAC
:
2822 error
= flac_open (psf
) ;
2825 case SF_FORMAT_CAF
:
2826 error
= caf_open (psf
) ;
2829 case SF_FORMAT_MPC2K
:
2830 error
= mpc2k_open (psf
) ;
2833 /* Lite remove end */
2836 error
= SFE_UNKNOWN_FORMAT
;
2842 /* For now, check whether embedding is supported. */
2843 format
= SF_CONTAINER (psf
->sf
.format
) ;
2844 if (psf
->fileoffset
> 0)
2846 { case SF_FORMAT_WAV
:
2847 case SF_FORMAT_WAVEX
:
2848 case SF_FORMAT_AIFF
:
2850 /* Actual embedded files. */
2853 case SF_FORMAT_FLAC
:
2854 /* Flac with an ID3v2 header? */
2858 error
= SFE_NO_EMBED_SUPPORT
;
2863 if (psf
->fileoffset
> 0)
2864 psf_log_printf (psf
, "Embedded file length : %D\n", psf
->filelength
) ;
2866 if (psf
->file
.mode
== SFM_RDWR
&& sf_format_check (&(psf
->sf
)) == 0)
2867 { error
= SFE_BAD_MODE_RW
;
2871 if (validate_sfinfo (&(psf
->sf
)) == 0)
2872 { psf_log_SF_INFO (psf
) ;
2873 save_header_info (psf
) ;
2874 error
= SFE_BAD_SF_INFO
;
2878 if (validate_psf (psf
) == 0)
2879 { save_header_info (psf
) ;
2880 error
= SFE_INTERNAL
;
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
;
2891 memcpy (sfinfo
, &(psf
->sf
), sizeof (SF_INFO
)) ;
2893 memcpy (sfinfo
, &(psf
->sf
), sizeof (SF_INFO
)) ;
2895 return (SNDFILE
*) psf
;
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
) ;
2905 { case SF_ERR_SYSTEM
:
2906 case SF_ERR_UNSUPPORTED_ENCODING
:
2907 case SFE_UNIMPLEMENTED
:
2910 case SFE_RAW_BAD_FORMAT
:
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
;
2922 } /* psf_open_file */