Libsndfile is a library designed to allow the reading and writing of many different sampled sound file formats (such as MS Windows WAV and the Apple/SGI AIFF format) through one standard library interface.
During read and write operations, formats are seamlessly converted between the format the application program has requested or supplied and the file's data format. The application programmer can remain blissfully unaware of issues such as file endian-ness and data format. See Note 1 and Note 2.
Every effort is made to keep these documents up-to-date, error free and unambiguous. However, since maintaining the documentation is the least fun part of working on libsndfile, these docs can and do fall behind the behaviour of library. If any errors, omissions or ambiguities are found, please notify Erik de Castro Lopo.
To supplement this reference documentation, there are simple example programs included in the source code tarball. The test suite which is also part of the source code tarball is also a good place to look for the correct usage of the library functions.
Finally, if you think there is some feature missing from libsndfile, check that it isn't already implemented (and documented) here.
The functions of libsndfile are defined as follows:
#include <stdio.h>
#include <sndfile.h>
SNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo) ;
SNDFILE* sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc) ;
int sf_format_check (const SF_INFO *info) ;
sf_count_t sf_seek (SNDFILE *sndfile, sf_count_t frames, int whence) ;
int sf_command (SNDFILE *sndfile, int cmd, void *data, int datasize) ;
int sf_error (SNDFILE *sndfile) ;
const char* sf_strerror (SNDFILE *sndfile) ;
const char* sf_error_number (int errnum) ;
int sf_perror (SNDFILE *sndfile) ;
int sf_error_str (SNDFILE *sndfile, char* str, size_t len) ;
int sf_close (SNDFILE *sndfile) ;
void sf_write_sync (SNDFILE *sndfile) ;
sf_count_t sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
sf_count_t sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
sf_count_t sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
sf_count_t sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
sf_count_t sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
sf_count_t sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
sf_count_t sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
sf_count_t sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
sf_count_t sf_write_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
sf_count_t sf_write_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
sf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
sf_count_t sf_write_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
sf_count_t sf_writef_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
sf_count_t sf_writef_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
sf_count_t sf_writef_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
sf_count_t sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
sf_count_t sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
sf_count_t sf_write_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
const char* sf_get_string (SNDFILE *sndfile, int str_type) ;
int sf_set_string (SNDFILE *sndfile, int str_type, const char* str) ;
SNDFILE* is an anonymous pointer to data which is private to the library.
SNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo) ;
The SF_INFO structure is for passing data between the calling function and the library when opening a file for reading or writing. It is defined in sndfile.h as follows:
typedef struct
{ sf_count_t frames ; /* Used to be called samples. */
int samplerate ;
int channels ;
int format ;
int sections ;
int seekable ;
} SF_INFO ;
The mode parameter for this function can be any one of the following three values:
SFM_READ - read only mode
SFM_WRITE - write only mode
SFM_RDWR - read/write mode
When opening a file for read, the format field should be set to zero before calling sf_open(). The only exception to this is the case of RAW files where the caller has to set the samplerate, channels and format fields to valid values. All other fields of the structure are filled in by the library.
When opening a file for write, the caller must fill in structure members samplerate, channels, and format.
The format field in the above SF_INFO structure is made up of the bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and / bit-wise OR of a major format type (values between 0x10000 and /