1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
8 <META NAME="Author" CONTENT="Erik de Castro Lopo (erikd AT mega-nerd DOT com)">
9 <META NAME="Description" CONTENT="The libsndfile API.">
10 <META NAME="Keywords" CONTENT="WAV AIFF AU libsndfile sound audio dsp Linux">
11 <LINK REL="stylesheet" HREF="libsndfile.css" TYPE="text/css" MEDIA="all">
12 <LINK REL="stylesheet" HREF="print.css" TYPE="text/css" MEDIA="print">
18 <H1><B>libsndfile</B></H1>
20 Libsndfile is a library designed to allow the reading and writing of many
21 different sampled sound file formats (such as MS Windows WAV and the Apple/SGI
22 AIFF format) through one standard library interface.
26 During read and write operations, formats are seamlessly converted between the
27 format the application program has requested or supplied and the file's data
28 format. The application programmer can remain blissfully unaware of issues
29 such as file endian-ness and data format. See <A HREF="#note1">Note 1</A> and
30 <A HREF="#note2">Note 2</A>.
34 Every effort is made to keep these documents up-to-date, error free and
36 However, since maintaining the documentation is the least fun part of working
37 on libsndfile, these docs can and do fall behind the behaviour of library.
38 If any errors, omissions or ambiguities are found, please notify
39 <A HREF="mailto:erikd@zip.com.au">
40 Erik de Castro Lopo</a>.
44 To supplement this reference documentation, there are simple example programs
45 included in the source code tarball.
46 The test suite which is also part of the source code tarball is also a good
47 place to look for the correct usage of the library functions.
51 <B> Finally, if you think there is some feature missing from libsndfile, check that
52 it isn't already implemented (and documented)
53 <A HREF="command.html">here</A>.
57 <H2><B>Synopsis</B></H2>
59 The functions of libsndfile are defined as follows:
63 #include <stdio.h>
64 #include <sndfile.h>
66 SNDFILE* <A HREF="#open">sf_open</A> (const char *path, int mode, SF_INFO *sfinfo) ;
67 SNDFILE* <A HREF="#open_fd">sf_open_fd</A> (int fd, int mode, SF_INFO *sfinfo, int close_desc) ;
69 int <A HREF="#check">sf_format_check</A> (const SF_INFO *info) ;
71 sf_count_t <A HREF="#seek">sf_seek</A> (SNDFILE *sndfile, sf_count_t frames, int whence) ;
73 int <A HREF="command.html">sf_command</A> (SNDFILE *sndfile, int cmd, void *data, int datasize) ;
75 int <A HREF="#error">sf_error</A> (SNDFILE *sndfile) ;
76 const char* <A HREF="#error">sf_strerror</A> (SNDFILE *sndfile) ;
77 const char* <A HREF="#error">sf_error_number</A> (int errnum) ;
79 int <A HREF="#error">sf_perror</A> (SNDFILE *sndfile) ;
80 int <A HREF="#error">sf_error_str</A> (SNDFILE *sndfile, char* str, size_t len) ;
82 int <A HREF="#close">sf_close</A> (SNDFILE *sndfile) ;
83 void <A HREF="#write_sync">sf_write_sync</A> (SNDFILE *sndfile) ;
85 sf_count_t <A HREF="#read">sf_read_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
86 sf_count_t <A HREF="#read">sf_read_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
87 sf_count_t <A HREF="#read">sf_read_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
88 sf_count_t <A HREF="#read">sf_read_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
90 sf_count_t <A HREF="#readf">sf_readf_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
91 sf_count_t <A HREF="#readf">sf_readf_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
92 sf_count_t <A HREF="#readf">sf_readf_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
93 sf_count_t <A HREF="#readf">sf_readf_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
95 sf_count_t <A HREF="#write">sf_write_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
96 sf_count_t <A HREF="#write">sf_write_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
97 sf_count_t <A HREF="#write">sf_write_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
98 sf_count_t <A HREF="#write">sf_write_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
100 sf_count_t <A HREF="#writef">sf_writef_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
101 sf_count_t <A HREF="#writef">sf_writef_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
102 sf_count_t <A HREF="#writef">sf_writef_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
103 sf_count_t <A HREF="#writef">sf_writef_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
105 sf_count_t <A HREF="#raw">sf_read_raw</A> (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
106 sf_count_t <A HREF="#raw">sf_write_raw</A> (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
108 const char* <A HREF="#string">sf_get_string</A> (SNDFILE *sndfile, int str_type) ;
109 int <A HREF="#string">sf_set_string</A> (SNDFILE *sndfile, int str_type, const char* str) ;
114 SNDFILE* is an anonymous pointer to data which is private to the library.
119 <H2><B>File Open Function</B></H2>
122 SNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo) ;
126 The SF_INFO structure is for passing data between the calling function and the library
127 when opening a file for reading or writing. It is defined in sndfile.h as follows:
132 { sf_count_t frames ; /* Used to be called samples. */
142 The mode parameter for this function can be any one of the following three values:
146 SFM_READ - read only mode
147 SFM_WRITE - write only mode
148 SFM_RDWR - read/write mode
152 When opening a file for read, the <b>format</B> field should be set to zero before
154 The only exception to this is the case of RAW files where the caller has to set
155 the samplerate, channels and format fields to valid values.
156 All other fields of the structure are filled in by the library.
160 When opening a file for write, the caller must fill in structure members samplerate,
161 channels, and format.
165 The format field in the above SF_INFO structure is made up of the bit-wise OR of a
166 major format type (values between 0x10000 and 0x08000000), a minor format type
167 (with values less than 0x10000) and an optional endian-ness value.
168 The currently understood formats are listed in sndfile.h as follows and also include
169 bitmasks for separating major and minor file types.
170 Not all combinations of endian-ness and major and minor file types are valid.
175 { /* Major formats. */
176 SF_FORMAT_WAV = 0x010000, /* Microsoft WAV format (little endian). */
177 SF_FORMAT_AIFF = 0x020000, /* Apple/SGI AIFF format (big endian). */
178 SF_FORMAT_AU = 0x030000, /* Sun/NeXT AU format (big endian). */
179 SF_FORMAT_RAW = 0x040000, /* RAW PCM data. */
180 SF_FORMAT_PAF = 0x050000, /* Ensoniq PARIS file format. */
181 SF_FORMAT_SVX = 0x060000, /* Amiga IFF / SVX8 / SV16 format. */
182 SF_FORMAT_NIST = 0x070000, /* Sphere NIST format. */
183 SF_FORMAT_VOC = 0x080000, /* VOC files. */
184 SF_FORMAT_IRCAM = 0x0A0000, /* Berkeley/IRCAM/CARL */
185 SF_FORMAT_W64 = 0x0B0000, /* Sonic Foundry's 64 bit RIFF/WAV */
186 SF_FORMAT_MAT4 = 0x0C0000, /* Matlab (tm) V4.2 / GNU Octave 2.0 */
187 SF_FORMAT_MAT5 = 0x0D0000, /* Matlab (tm) V5.0 / GNU Octave 2.1 */
188 SF_FORMAT_PVF = 0x0E0000, /* Portable Voice Format */
189 SF_FORMAT_XI = 0x0F0000, /* Fasttracker 2 Extended Instrument */
190 SF_FORMAT_HTK = 0x100000, /* HMM Tool Kit format */
191 SF_FORMAT_SDS = 0x110000, /* Midi Sample Dump Standard */
192 SF_FORMAT_AVR = 0x120000, /* Audio Visual Research */
193 SF_FORMAT_WAVEX = 0x130000, /* MS WAVE with WAVEFORMATEX */
194 SF_FORMAT_SD2 = 0x160000, /* Sound Designer 2 */
195 SF_FORMAT_FLAC = 0x170000, /* FLAC lossless file format */
196 SF_FORMAT_CAF = 0x180000, /* Core Audio File format */
198 /* Subtypes from here on. */
200 SF_FORMAT_PCM_S8 = 0x0001, /* Signed 8 bit data */
201 SF_FORMAT_PCM_16 = 0x0002, /* Signed 16 bit data */
202 SF_FORMAT_PCM_24 = 0x0003, /* Signed 24 bit data */
203 SF_FORMAT_PCM_32 = 0x0004, /* Signed 32 bit data */
205 SF_FORMAT_PCM_U8 = 0x0005, /* Unsigned 8 bit data (WAV and RAW only) */
207 SF_FORMAT_FLOAT = 0x0006, /* 32 bit float data */
208 SF_FORMAT_DOUBLE = 0x0007, /* 64 bit float data */
210 SF_FORMAT_ULAW = 0x0010, /* U-Law encoded. */
211 SF_FORMAT_ALAW = 0x0011, /* A-Law encoded. */
212 SF_FORMAT_IMA_ADPCM = 0x0012, /* IMA ADPCM. */
213 SF_FORMAT_MS_ADPCM = 0x0013, /* Microsoft ADPCM. */
215 SF_FORMAT_GSM610 = 0x0020, /* GSM 6.10 encoding. */
216 SF_FORMAT_VOX_ADPCM = 0x0021, /* Oki Dialogic ADPCM encoding. */
218 SF_FORMAT_G721_32 = 0x0030, /* 32kbs G721 ADPCM encoding. */
219 SF_FORMAT_G723_24 = 0x0031, /* 24kbs G723 ADPCM encoding. */
220 SF_FORMAT_G723_40 = 0x0032, /* 40kbs G723 ADPCM encoding. */
222 SF_FORMAT_DWVW_12 = 0x0040, /* 12 bit Delta Width Variable Word encoding. */
223 SF_FORMAT_DWVW_16 = 0x0041, /* 16 bit Delta Width Variable Word encoding. */
224 SF_FORMAT_DWVW_24 = 0x0042, /* 24 bit Delta Width Variable Word encoding. */
225 SF_FORMAT_DWVW_N = 0x0043, /* N bit Delta Width Variable Word encoding. */
227 SF_FORMAT_DPCM_8 = 0x0050, /* 8 bit differential PCM (XI only) */
228 SF_FORMAT_DPCM_16 = 0x0051, /* 16 bit differential PCM (XI only) */
230 /* Endian-ness options. */
232 SF_ENDIAN_FILE = 0x00000000, /* Default file endian-ness. */
233 SF_ENDIAN_LITTLE = 0x10000000, /* Force little endian-ness. */
234 SF_ENDIAN_BIG = 0x20000000, /* Force big endian-ness. */
235 SF_ENDIAN_CPU = 0x30000000, /* Force CPU endian-ness. */
237 SF_FORMAT_SUBMASK = 0x0000FFFF,
238 SF_FORMAT_TYPEMASK = 0x0FFF0000,
239 SF_FORMAT_ENDMASK = 0x30000000
244 Every call to sf_open() should be matched with a call to sf_close() to free up
245 memory allocated during the call to sf_open().
249 On success, the sf_open function returns a non-NULL pointer which should be
250 passed as the first parameter to all subsequent libsndfile calls dealing with
252 On fail, the sf_open function returns a NULL pointer.
255 <A NAME="open_fd"></A>
256 <H3><B>File Descriptor Open</B></H3>
259 SNDFILE* sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc) ;
263 The second open function takes a file descriptor of a file that has already been
265 Care should be taken to ensure that the mode of the file represented by the
266 descriptor matches the mode argument.
267 This function is useful in the following circumstances:
271 <LI>Opening temporary files securely (ie use the tmpfile() to return a
272 FILE* pointer and then using fileno() to retrieve the file descriptor
273 which is then passed to libsndfile).
274 <LI>Opening files with file names using OS specific character encodings
275 and then passing the file descriptor to sf_open_fd().
276 <LI>Opening sound files embedded within larger files.
277 <A HREF="embedded_files.html">More info</A>.
281 Every call to sf_open_fd() should be matched with a call to sf_close() to free up
282 memory allocated during the call to sf_open().
286 When sf_close() is called, the file descriptor is only closed if the <B>close_desc</B>
287 parameter was TRUE when the sf_open_fd() function was called.
291 On success, the sf_open_fd function returns a non-NULL pointer which should be
292 passed as the first parameter to all subsequent libsndfile calls dealing with
294 On fail, the sf_open_fd function returns a NULL pointer.
298 <BR><H2><B>Format Check Function</B></H2>
301 int sf_format_check (const SF_INFO *info) ;
305 This function allows the caller to check if a set of parameters in the SF_INFO struct
306 is valid before calling sf_open (SFM_WRITE).
309 sf_format_check returns TRUE if the parameters are valid and FALSE otherwise.
313 <BR><H2><B>File Seek Functions</B></H2>
316 sf_count_t sf_seek (SNDFILE *sndfile, sf_count_t frames, int whence) ;
320 The file seek functions work much like lseek in unistd.h with the exception that
321 the non-audio data is ignored and the seek only moves within the audio data section of
323 In addition, seeks are defined in number of (multichannel) frames.
324 Therefore, a seek in a stereo file from the current position forward with an offset
325 of 1 would skip forward by one sample of both channels.
329 like lseek(), the whence parameter can be any one of the following three values:
333 SEEK_SET - The offset is set to the start of the audio data plus offset (multichannel) frames.
334 SEEK_CUR - The offset is set to its current location plus offset (multichannel) frames.
335 SEEK_END - The offset is set to the end of the data plus offset (multichannel) frames.
339 Internally, libsndfile keeps track of the read and write locations using separate
340 read and write pointers.
341 If a file has been opened with a mode of SFM_RDWR, bitwise OR-ing the standard whence
342 values above with either SFM_READ or SFM_WRITE allows the read and write pointers to
343 be modified separately.
344 If the SEEK_* values are used on their own, the read and write pointers are
349 Note that the frames offset can be negative and in fact should be when SEEK_END is used for the
353 sf_seek will return the offset in (multichannel) frames from the start of the audio data
354 or -1 if an error occured (ie an attempt is made to seek beyond the start or end of the file).
358 <H2><BR><B>Error Reporting Functions</B></H2>
362 int sf_error (SNDFILE *sndfile) ;
365 This function returns the current error number for the given SNDFILE.
366 The error number may be one of the following:
370 { SF_ERR_NO_ERROR = 0,
371 SF_ERR_UNRECOGNISED_FORMAT = 1,
373 SF_ERR_MALFORMED_FILE = 3,
374 SF_ERR_UNSUPPORTED_ENCODING = 4
379 or any one of many other internal error values.
380 Applications should only test the return value against error values defined in
381 <sndfile.h> as the internal error values are subject to change at any
383 For errors not in the above list, the function sf_error_number() can be used to
384 convert it to an error string.
388 const char* sf_strerror (SNDFILE *sndfile) ;
389 const char* sf_error_number (int errnum) ;
393 The error functions sf_strerror() and sf_error_number() convert the library's internal
394 error enumerations into text strings.
397 int sf_perror (SNDFILE *sndfile) ;
398 int sf_error_str (SNDFILE *sndfile, char* str, size_t len) ;
402 The functions sf_perror() and sf_error_str() are deprecated and will be dropped
403 from the library at some later date.
407 <H2><BR><B>File Close Function</B></H2>
410 int sf_close (SNDFILE *sndfile) ;
414 The close function closes the file, deallocates its internal buffers and returns
415 0 on success or an error value otherwise.
419 <A NAME="write_sync"></A>
420 <H2><BR><B>Write Sync Function</B></H2>
423 void sf_write_sync (SNDFILE *sndfile) ;
427 If the file is opened SFM_WRITE or SFM_RDWR, call the operating system's function
428 to force the writing of all file cache buffers to disk. If the file is opened
429 SFM_READ no action is taken.
435 <H2><BR><B>File Read Functions (Items)</B></H2>
438 sf_count_t sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
439 sf_count_t sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
440 sf_count_t sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
441 sf_count_t sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
445 The file read items functions fill the array pointed to by ptr with the requested
446 number of items. The items parameter must be an integer product of the number
447 of channels or an error will occur.
451 It is important to note that the data type used by the calling program and the data
452 format of the file do not need to be the same. For instance, it is possible to open
453 a 16 bit PCM encoded WAV file and read the data using sf_read_float(). The library
454 seamlessly converts between the two formats on-the-fly. See
455 <A HREF="#note1">Note 1</A>.
459 The sf_read_XXXX functions return the number of items read.
460 Unless the end of the file was reached during the read, the return value should
461 equal the number of items requested.
462 Attempts to read beyond the end of the file will not result in an error but will
463 cause the sf_read_XXXX functions to return less than the number of items requested
464 or 0 if already at the end of the file.
468 <H2><BR><B>File Read Functions (Frames)</B></H2>
471 sf_count_t sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
472 sf_count_t sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
473 sf_count_t sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
474 sf_count_t sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
478 The file read frames functions fill the array pointed to by ptr with the requested
479 number of frames of data. The array must be large enough to hold the product of
480 frames and the number of channels.
484 Care must be taken to ensure that there is enough space in the array pointed to by
485 ptr, to take (frames * channels) number of items (shorts, ints, floats or doubles).
489 The sf_readf_XXXX functions return the number of frames read.
490 Unless the end of the file was reached during the read, the return value should equal
491 the number of frames requested.
492 Attempts to read beyond the end of the file will not result in an error but will cause
493 the sf_readf_XXXX functions to return less than the number of frames requested or 0 if
494 already at the end of the file.
498 <H2><BR><B>File Write Functions (Items)</B></H2>
501 sf_count_t sf_write_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
502 sf_count_t sf_write_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
503 sf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
504 sf_count_t sf_write_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
508 The file write items functions write the data in the array pointed to by ptr to the file.
509 The items parameter must be an integer product of the number of channels or an error
514 It is important to note that the data type used by the calling program and the data
515 format of the file do not need to be the same. For instance, it is possible to open
516 a 16 bit PCM encoded WAV file and write the data using sf_write_float(). The library
517 seamlessly converts between the two formats on-the-fly. See
518 <A HREF="#note1">Note 1</A>.
521 The sf_write_XXXX functions return the number of items written (which should be the
522 same as the items parameter).
525 <A NAME="writef"></A>
526 <H2><BR><B>File Write Functions (Frames)</B></H2>
529 sf_count_t sf_writef_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
530 sf_count_t sf_writef_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
531 sf_count_t sf_writef_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
532 sf_count_t sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
536 The file write frames functions write the data in the array pointed to by ptr to the file.
537 The array must be large enough to hold the product of frames and the number of channels.
540 The sf_writef_XXXX functions return the number of frames written (which should be the
541 same as the frames parameter).
545 <H2><BR><B>Raw File Read and Write Functions</B></H2>
548 sf_count_t sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
549 sf_count_t sf_write_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
553 The raw read and write functions read raw audio data from the audio file (not to be
554 confused with reading RAW header-less PCM files). The number of bytes read or written
555 must always be an integer multiple of the number of channels multiplied by the number
556 of bytes required to represent one sample from one channel.
560 The raw read and write functions return the number of bytes read or written (which
561 should be the same as the bytes parameter).
566 Note : The result of using of both regular reads/writes and raw reads/writes on
567 compressed file formats other than SF_FORMAT_ALAW and SF_FORMAT_ULAW is undefined.
572 See also : <a href="command.html#SFC_RAW_NEEDS_ENDSWAP">SFC_RAW_NEEDS_ENDSWAP</a>
575 <A NAME="string"></A>
576 <H2><BR><B>Functions for Reading and Writing String Data</B></H2>
580 const char* sf_get_string (SNDFILE *sndfile, int str_type) ;
581 int sf_set_string (SNDFILE *sndfile, int str_type, const char* str) ;
585 These functions allow strings to be set on files opened for write and to be
586 retrieved from files opened for read where supported by the given file type.
587 The <B>str_type</B> parameter can be any one of the following string types:
602 The sf_get_string() function returns the specified string if it exists and a
603 NULL pointer otherwise.
604 In addition to the string ids above, SF_STR_FIRST (== SF_STR_TITLE) and
605 SF_STR_LAST (always the same as the highest numbers string id) are also
606 available to allow iteration over all the available string ids.
610 The sf_set_string() function sets the string data.
611 It returns zero on success and non-zero on error.
612 The error code can be converted to a string using sf_error_number().
623 <H2><BR><B>Note 1</B></H2>
626 When converting between integer PCM formats of differing size (ie using sf_read_int()
627 to read a 16 bit PCM encoded WAV file) libsndfile obeys one simple rule:
630 <P CLASS=indent_block>
631 Whenever integer data is moved from one sized container to another sized container,
632 the most significant bit in the source container will become the most significant bit
633 in the destination container.
637 When converting between integer data and floating point data, different rules apply.
638 The default behaviour when reading floating point data (sf_read_float() or
639 sf_read_double ()) from a file with integer data is normalisation. Regardless of
640 whether data in the file is 8, 16, 24 or 32 bit wide, the data will be read as
641 floating point data in the range [-1.0, 1.0]. Similarly, data in the range [-1.0, 1.0]
642 will be written to an integer PCM file so that a data value of 1.0 will be the largest
643 allowable integer for the given bit width. This normalisation can be turned on or off
644 using the <A HREF="command.html">sf_command</A> interface.
648 <H2><BR><B>Note 2</B></H2>
651 Reading a file containg floating point data (allowable with WAV, AIFF, AU and other
652 file formats) using integer read methods (sf_read_short() or sf_read_int()) can
653 produce unexpected results.
654 For instance the data in the file may have a maximum absolute value < 1.0 which
655 would mean that all sample values read from the file will be zero.
656 In order to read these files correctly using integer read methods, it is recommended
658 <A HREF="command.html">sf_command</A>
659 interface, a command of
660 <A HREF="command.html#SFC_SET_SCALE_FLOAT_INT_READ">SFC_SET_SCALE_FLOAT_INT_READ</A>
661 and a parameter of SF_TRUE to force correct scaling.
667 The libsndfile home page is
668 <A HREF="http://www.mega-nerd.com/libsndfile/">here</A>.