Chris@40: Chris@40: Chris@40: Chris@40: Chris@40: Chris@40: The libsndfile API Chris@40: Chris@40: Chris@40: Chris@40: Chris@40: Chris@40: Chris@40: Chris@40: Chris@40: Chris@40: Chris@40:
Chris@40:

libsndfile

Chris@40:

Chris@40: Libsndfile is a library designed to allow the reading and writing of many Chris@40: different sampled sound file formats (such as MS Windows WAV and the Apple/SGI Chris@40: AIFF format) through one standard library interface. Chris@40:

Chris@40: Chris@40:

Chris@40: During read and write operations, formats are seamlessly converted between the Chris@40: format the application program has requested or supplied and the file's data Chris@40: format. The application programmer can remain blissfully unaware of issues Chris@40: such as file endian-ness and data format. See Note 1 and Chris@40: Note 2. Chris@40:

Chris@40: Chris@40:

Chris@40: Every effort is made to keep these documents up-to-date, error free and Chris@40: unambiguous. Chris@40: However, since maintaining the documentation is the least fun part of working Chris@40: on libsndfile, these docs can and do fall behind the behaviour of the library. Chris@40: If any errors, omissions or ambiguities are found, please notify me (erikd) Chris@40: at mega-nerd dot com. Chris@40:

Chris@40: Chris@40:

Chris@40: To supplement this reference documentation, there are simple example programs Chris@40: included in the source code tarball. Chris@40: The test suite which is also part of the source code tarball is also a good Chris@40: place to look for the correct usage of the library functions. Chris@40:

Chris@40: Chris@40:

Chris@40: Finally, if you think there is some feature missing from libsndfile, check that Chris@40: it isn't already implemented (and documented) Chris@40: here. Chris@40: Chris@40:

Chris@40: Chris@40:

Synopsis

Chris@40:

Chris@40: The functions of libsndfile are defined as follows: Chris@40:

Chris@40: Chris@40:
Chris@40:       #include <stdio.h>
Chris@40:       #include <sndfile.h>
Chris@40: 
Chris@40:       SNDFILE*    sf_open          (const char *path, int mode, SF_INFO *sfinfo) ;
Chris@40:       SNDFILE*    sf_wchar_open    (LPCWSTR wpath, int mode, SF_INFO *sfinfo) ;
Chris@40:       SNDFILE*    sf_open_fd       (int fd, int mode, SF_INFO *sfinfo, int close_desc) ;
Chris@40:       SNDFILE* 	  sf_open_virtual  (SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo, void *user_data) ;
Chris@40:       int         sf_format_check  (const SF_INFO *info) ;
Chris@40: 
Chris@40:       sf_count_t  sf_seek          (SNDFILE *sndfile, sf_count_t frames, int whence) ;
Chris@40: 
Chris@40:       int         sf_command       (SNDFILE *sndfile, int cmd, void *data, int datasize) ;
Chris@40: 
Chris@40:       int         sf_error         (SNDFILE *sndfile) ;
Chris@40:       const char* sf_strerror      (SNDFILE *sndfile) ;
Chris@40:       const char* sf_error_number  (int errnum) ;
Chris@40: 
Chris@40:       int         sf_perror        (SNDFILE *sndfile) ;
Chris@40:       int         sf_error_str     (SNDFILE *sndfile, char* str, size_t len) ;
Chris@40: 
Chris@40:       int         sf_close         (SNDFILE *sndfile) ;
Chris@40:       void        sf_write_sync    (SNDFILE *sndfile) ;
Chris@40: 
Chris@40:       sf_count_t  sf_read_short    (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_read_int      (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_read_float    (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_read_double   (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
Chris@40: 
Chris@40:       sf_count_t  sf_readf_short   (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_readf_int     (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_readf_float   (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_readf_double  (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
Chris@40: 
Chris@40:       sf_count_t  sf_write_short   (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_write_int     (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_write_float   (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_write_double  (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
Chris@40: 
Chris@40:       sf_count_t  sf_writef_short  (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_writef_int    (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_writef_float  (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
Chris@40: 
Chris@40:       sf_count_t  sf_read_raw      (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
Chris@40:       sf_count_t  sf_write_raw     (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
Chris@40: 
Chris@40:       const char* sf_get_string    (SNDFILE *sndfile, int str_type) ;
Chris@40:       int         sf_set_string    (SNDFILE *sndfile, int str_type, const char* str) ;
Chris@40: 
Chris@40: 
Chris@40: Chris@40:

Chris@40: SNDFILE* is an anonymous pointer to data which is private to the library. Chris@40:

Chris@40: Chris@40: Chris@40: Chris@40:

File Open Function

Chris@40: Chris@40:
Chris@40:       SNDFILE*  sf_open    (const char *path, int mode, SF_INFO *sfinfo) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: The sf_open() function opens the sound file at the specified path. Chris@40: The filename is byte encoded, but may be utf-8 on Linux, while on Mac OS X it Chris@40: will use the filesystem character set. Chris@40: On Windows, there is also a Windows specific sf_wchar_open() that takes a Chris@40: UTF16_BE encoded filename. Chris@40:

Chris@40: Chris@40:
Chris@40:       SNDFILE*  sf_wchar_open (LPCWSTR wpath, int mode, SF_INFO *sfinfo) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: The SF_INFO structure is for passing data between the calling function and the library Chris@40: when opening a file for reading or writing. It is defined in sndfile.h as follows: Chris@40:

Chris@40: Chris@40:
Chris@40:       typedef struct
Chris@40:       {    sf_count_t  frames ;     /* Used to be called samples. */
Chris@40:            int         samplerate ;
Chris@40:            int         channels ;
Chris@40:            int         format ;
Chris@40:            int         sections ;
Chris@40:            int         seekable ;
Chris@40:        } SF_INFO ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: The mode parameter for this function can be any one of the following three values: Chris@40:

Chris@40: Chris@40:
Chris@40:       SFM_READ    - read only mode
Chris@40:       SFM_WRITE   - write only mode
Chris@40:       SFM_RDWR    - read/write mode
Chris@40: 
Chris@40: Chris@40:

Chris@40: When opening a file for read, the format field should be set to zero before Chris@40: calling sf_open(). Chris@40: The only exception to this is the case of RAW files where the caller has to set Chris@40: the samplerate, channels and format fields to valid values. Chris@40: All other fields of the structure are filled in by the library. Chris@40:

Chris@40: Chris@40:

Chris@40: When opening a file for write, the caller must fill in structure members samplerate, Chris@40: channels, and format. Chris@40:

Chris@40: Chris@40:

Chris@40: The format field in the above SF_INFO structure is made up of the bit-wise OR of a Chris@40: major format type (values between 0x10000 and 0x08000000), a minor format type Chris@40: (with values less than 0x10000) and an optional endian-ness value. Chris@40: The currently understood formats are listed in sndfile.h as follows and also include Chris@40: bitmasks for separating major and minor file types. Chris@40: Not all combinations of endian-ness and major and minor file types are valid. Chris@40:

Chris@40: Chris@40:
Chris@40:       enum
Chris@40:       {   /* Major formats. */
Chris@40:           SF_FORMAT_WAV          = 0x010000,     /* Microsoft WAV format (little endian). */
Chris@40:           SF_FORMAT_AIFF         = 0x020000,     /* Apple/SGI AIFF format (big endian). */
Chris@40:           SF_FORMAT_AU           = 0x030000,     /* Sun/NeXT AU format (big endian). */
Chris@40:           SF_FORMAT_RAW          = 0x040000,     /* RAW PCM data. */
Chris@40:           SF_FORMAT_PAF          = 0x050000,     /* Ensoniq PARIS file format. */
Chris@40:           SF_FORMAT_SVX          = 0x060000,     /* Amiga IFF / SVX8 / SV16 format. */
Chris@40:           SF_FORMAT_NIST         = 0x070000,     /* Sphere NIST format. */
Chris@40:           SF_FORMAT_VOC          = 0x080000,     /* VOC files. */
Chris@40:           SF_FORMAT_IRCAM        = 0x0A0000,     /* Berkeley/IRCAM/CARL */
Chris@40:           SF_FORMAT_W64          = 0x0B0000,     /* Sonic Foundry's 64 bit RIFF/WAV */
Chris@40:           SF_FORMAT_MAT4         = 0x0C0000,     /* Matlab (tm) V4.2 / GNU Octave 2.0 */
Chris@40:           SF_FORMAT_MAT5         = 0x0D0000,     /* Matlab (tm) V5.0 / GNU Octave 2.1 */
Chris@40:           SF_FORMAT_PVF          = 0x0E0000,     /* Portable Voice Format */
Chris@40:           SF_FORMAT_XI           = 0x0F0000,     /* Fasttracker 2 Extended Instrument */
Chris@40:           SF_FORMAT_HTK          = 0x100000,     /* HMM Tool Kit format */
Chris@40:           SF_FORMAT_SDS          = 0x110000,     /* Midi Sample Dump Standard */
Chris@40:           SF_FORMAT_AVR          = 0x120000,     /* Audio Visual Research */
Chris@40:           SF_FORMAT_WAVEX        = 0x130000,     /* MS WAVE with WAVEFORMATEX */
Chris@40:           SF_FORMAT_SD2          = 0x160000,     /* Sound Designer 2 */
Chris@40:           SF_FORMAT_FLAC         = 0x170000,     /* FLAC lossless file format */
Chris@40:           SF_FORMAT_CAF          = 0x180000,     /* Core Audio File format */
Chris@40:           SF_FORMAT_WVE          = 0x190000,     /* Psion WVE format */
Chris@40:           SF_FORMAT_OGG          = 0x200000,     /* Xiph OGG container */
Chris@40:           SF_FORMAT_MPC2K        = 0x210000,     /* Akai MPC 2000 sampler */
Chris@40:           SF_FORMAT_RF64         = 0x220000,     /* RF64 WAV file */
Chris@40: 
Chris@40:           /* Subtypes from here on. */
Chris@40: 
Chris@40:           SF_FORMAT_PCM_S8       = 0x0001,       /* Signed 8 bit data */
Chris@40:           SF_FORMAT_PCM_16       = 0x0002,       /* Signed 16 bit data */
Chris@40:           SF_FORMAT_PCM_24       = 0x0003,       /* Signed 24 bit data */
Chris@40:           SF_FORMAT_PCM_32       = 0x0004,       /* Signed 32 bit data */
Chris@40: 
Chris@40:           SF_FORMAT_PCM_U8       = 0x0005,       /* Unsigned 8 bit data (WAV and RAW only) */
Chris@40: 
Chris@40:           SF_FORMAT_FLOAT        = 0x0006,       /* 32 bit float data */
Chris@40:           SF_FORMAT_DOUBLE       = 0x0007,       /* 64 bit float data */
Chris@40: 
Chris@40:           SF_FORMAT_ULAW         = 0x0010,       /* U-Law encoded. */
Chris@40:           SF_FORMAT_ALAW         = 0x0011,       /* A-Law encoded. */
Chris@40:           SF_FORMAT_IMA_ADPCM    = 0x0012,       /* IMA ADPCM. */
Chris@40:           SF_FORMAT_MS_ADPCM     = 0x0013,       /* Microsoft ADPCM. */
Chris@40: 
Chris@40:           SF_FORMAT_GSM610       = 0x0020,       /* GSM 6.10 encoding. */
Chris@40:           SF_FORMAT_VOX_ADPCM    = 0x0021,       /* Oki Dialogic ADPCM encoding. */
Chris@40: 
Chris@40:           SF_FORMAT_G721_32      = 0x0030,       /* 32kbs G721 ADPCM encoding. */
Chris@40:           SF_FORMAT_G723_24      = 0x0031,       /* 24kbs G723 ADPCM encoding. */
Chris@40:           SF_FORMAT_G723_40      = 0x0032,       /* 40kbs G723 ADPCM encoding. */
Chris@40: 
Chris@40:           SF_FORMAT_DWVW_12      = 0x0040,       /* 12 bit Delta Width Variable Word encoding. */
Chris@40:           SF_FORMAT_DWVW_16      = 0x0041,       /* 16 bit Delta Width Variable Word encoding. */
Chris@40:           SF_FORMAT_DWVW_24      = 0x0042,       /* 24 bit Delta Width Variable Word encoding. */
Chris@40:           SF_FORMAT_DWVW_N       = 0x0043,       /* N bit Delta Width Variable Word encoding. */
Chris@40: 
Chris@40:           SF_FORMAT_DPCM_8       = 0x0050,       /* 8 bit differential PCM (XI only) */
Chris@40:           SF_FORMAT_DPCM_16      = 0x0051,       /* 16 bit differential PCM (XI only) */
Chris@40: 
Chris@40:           SF_FORMAT_VORBIS       = 0x0060,       /* Xiph Vorbis encoding. */
Chris@40: 
Chris@40:           /* Endian-ness options. */
Chris@40: 
Chris@40:           SF_ENDIAN_FILE         = 0x00000000,   /* Default file endian-ness. */
Chris@40:           SF_ENDIAN_LITTLE       = 0x10000000,   /* Force little endian-ness. */
Chris@40:           SF_ENDIAN_BIG          = 0x20000000,   /* Force big endian-ness. */
Chris@40:           SF_ENDIAN_CPU          = 0x30000000,   /* Force CPU endian-ness. */
Chris@40: 
Chris@40:           SF_FORMAT_SUBMASK      = 0x0000FFFF,
Chris@40:           SF_FORMAT_TYPEMASK     = 0x0FFF0000,
Chris@40:           SF_FORMAT_ENDMASK      = 0x30000000
Chris@40:       } ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: Every call to sf_open() should be matched with a call to sf_close() to free up Chris@40: memory allocated during the call to sf_open(). Chris@40:

Chris@40: Chris@40:

Chris@40: On success, the sf_open function returns a non-NULL pointer which should be Chris@40: passed as the first parameter to all subsequent libsndfile calls dealing with Chris@40: that audio file. Chris@40: On fail, the sf_open function returns a NULL pointer. Chris@40: An explanation of the error can obtained by passing NULL to Chris@40: sf_strerror. Chris@40:

Chris@40: Chris@40: Chris@40:

File Descriptor Open

Chris@40: Chris@40:
Chris@40:       SNDFILE*  sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: Note: On Microsoft Windows, this function does not work if the Chris@40: application and the libsndfile DLL are linked to different versions of the Chris@40: Microsoft C runtime DLL. Chris@40:

Chris@40:

Chris@40: The second open function takes a file descriptor of a file that has already been Chris@40: opened. Chris@40: Care should be taken to ensure that the mode of the file represented by the Chris@40: descriptor matches the mode argument. Chris@40: This function is useful in the following circumstances: Chris@40:

Chris@40: Chris@40: Chris@40: Chris@40:

Chris@40: Every call to sf_open_fd() should be matched with a call to sf_close() to free up Chris@40: memory allocated during the call to sf_open(). Chris@40:

Chris@40: Chris@40:

Chris@40: When sf_close() is called, the file descriptor is only closed if the close_desc Chris@40: parameter was TRUE when the sf_open_fd() function was called. Chris@40:

Chris@40: Chris@40:

Chris@40: On success, the sf_open_fd function returns a non-NULL pointer which should be Chris@40: passed as the first parameter to all subsequent libsndfile calls dealing with Chris@40: that audio file. Chris@40: On fail, the sf_open_fd function returns a NULL pointer. Chris@40:

Chris@40: Chris@40: Chris@40:

Virtual File Open Function

Chris@40:
Chris@40:       SNDFILE* 	sf_open_virtual	(SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo, void *user_data) ;
Chris@40: 
Chris@40:

Chris@40: Opens a soundfile from a virtual file I/O context which is provided Chris@40: by the caller. This is usually used to interface libsndfile to a stream or buffer Chris@40: based system. Apart from the sfvirtual and the user_data parameters this function behaves Chris@40: like sf_open. Chris@40:

Chris@40: Chris@40:
Chris@40:       typedef struct
Chris@40:       {    sf_vio_get_filelen  get_filelen ;
Chris@40:            sf_vio_seek         seek ;
Chris@40:            sf_vio_read         read ;
Chris@40:            sf_vio_write        write ;
Chris@40:            sf_vio_tell         tell ;
Chris@40:       } SF_VIRTUAL_IO ;
Chris@40: 
Chris@40:

Chris@40: Libsndfile calls the callbacks provided by the SF_VIRTUAL_IO structure when opening, reading Chris@40: and writing to the virtual file context. The user_data pointer is a user defined context which Chris@40: will be available in the callbacks. Chris@40:

Chris@40:
Chris@40:       typedef sf_count_t  (*sf_vio_get_filelen) (void *user_data) ;
Chris@40:       typedef sf_count_t  (*sf_vio_seek)        (sf_count_t offset, int whence, void *user_data) ;
Chris@40:       typedef sf_count_t  (*sf_vio_read)        (void *ptr, sf_count_t count, void *user_data) ;
Chris@40:       typedef sf_count_t  (*sf_vio_write)       (const void *ptr, sf_count_t count, void *user_data) ;
Chris@40:       typedef sf_count_t  (*sf_vio_tell)        (void *user_data) ;
Chris@40: 
Chris@40:

sf_vio_get_filelen

Chris@40:
Chris@40:       typedef sf_count_t  (*sf_vio_get_filelen) (void *user_data) ;
Chris@40: 
Chris@40:

Chris@40: The virtual file contex must return the length of the virtual file in bytes.
Chris@40:

Chris@40:

sf_vio_seek

Chris@40:
Chris@40:       typedef sf_count_t  (*sf_vio_seek)        (sf_count_t offset, int whence, void *user_data) ;
Chris@40: 
Chris@40:

Chris@40: The virtual file context must seek to offset using the seek mode provided by whence which is one of
Chris@40:

Chris@40:
Chris@40:       SEEK_CUR
Chris@40:       SEEK_SET
Chris@40:       SEEK_END
Chris@40: 
Chris@40:

Chris@40: The return value must contain the new offset in the file. Chris@40:

Chris@40:

sf_vio_read

Chris@40:
Chris@40:       typedef sf_count_t  (*sf_vio_read)        (void *ptr, sf_count_t count, void *user_data) ;
Chris@40: 
Chris@40:

Chris@40: The virtual file context must copy ("read") "count" bytes into the Chris@40: buffer provided by ptr and return the count of actually copied bytes. Chris@40:

Chris@40:

sf_vio_write

Chris@40:
Chris@40:       typedef sf_count_t  (*sf_vio_write)       (const void *ptr, sf_count_t count, void *user_data) ;
Chris@40: 
Chris@40:

Chris@40: The virtual file context must process "count" bytes stored in the Chris@40: buffer passed with ptr and return the count of actually processed bytes.
Chris@40:

Chris@40:

sf_vio_tell

Chris@40:
Chris@40:       typedef sf_count_t  (*sf_vio_tell)        (void *user_data) ;
Chris@40: 
Chris@40:

Chris@40: Return the current position of the virtual file context.
Chris@40:

Chris@40: Chris@40: Chris@40: Chris@40:

Format Check Function

Chris@40: Chris@40:
Chris@40:       int  sf_format_check (const SF_INFO *info) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: This function allows the caller to check if a set of parameters in the SF_INFO struct Chris@40: is valid before calling sf_open (SFM_WRITE). Chris@40:

Chris@40:

Chris@40: sf_format_check returns TRUE if the parameters are valid and FALSE otherwise. Chris@40:

Chris@40: Chris@40: Chris@40:

File Seek Functions

Chris@40: Chris@40:
Chris@40:       sf_count_t  sf_seek  (SNDFILE *sndfile, sf_count_t frames, int whence) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: The file seek functions work much like lseek in unistd.h with the exception that Chris@40: the non-audio data is ignored and the seek only moves within the audio data section of Chris@40: the file. Chris@40: In addition, seeks are defined in number of (multichannel) frames. Chris@40: Therefore, a seek in a stereo file from the current position forward with an offset Chris@40: of 1 would skip forward by one sample of both channels. Chris@40:

Chris@40: Chris@40:

Chris@40: like lseek(), the whence parameter can be any one of the following three values: Chris@40:

Chris@40: Chris@40:
Chris@40:       SEEK_SET  - The offset is set to the start of the audio data plus offset (multichannel) frames.
Chris@40:       SEEK_CUR  - The offset is set to its current location plus offset (multichannel) frames.
Chris@40:       SEEK_END  - The offset is set to the end of the data plus offset (multichannel) frames.
Chris@40: 
Chris@40: Chris@40:

Chris@40: Internally, libsndfile keeps track of the read and write locations using separate Chris@40: read and write pointers. Chris@40: If a file has been opened with a mode of SFM_RDWR, bitwise OR-ing the standard whence Chris@40: values above with either SFM_READ or SFM_WRITE allows the read and write pointers to Chris@40: be modified separately. Chris@40: If the SEEK_* values are used on their own, the read and write pointers are Chris@40: both modified. Chris@40:

Chris@40: Chris@40:

Chris@40: Note that the frames offset can be negative and in fact should be when SEEK_END is used for the Chris@40: whence parameter. Chris@40:

Chris@40:

Chris@40: sf_seek will return the offset in (multichannel) frames from the start of the audio data Chris@40: or -1 if an error occured (ie an attempt is made to seek beyond the start or end of the file). Chris@40:

Chris@40: Chris@40: Chris@40:


Error Reporting Functions

Chris@40: Chris@40: Chris@40:
Chris@40:       int         sf_error        (SNDFILE *sndfile) ;
Chris@40: 
Chris@40:

Chris@40: This function returns the current error number for the given SNDFILE. Chris@40: The error number may be one of the following: Chris@40:

Chris@40:
Chris@40:         enum
Chris@40:         {   SF_ERR_NO_ERROR             = 0,
Chris@40:             SF_ERR_UNRECOGNISED_FORMAT  = 1,
Chris@40:             SF_ERR_SYSTEM               = 2,
Chris@40:             SF_ERR_MALFORMED_FILE       = 3,
Chris@40:             SF_ERR_UNSUPPORTED_ENCODING = 4
Chris@40:         } ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: or any one of many other internal error values. Chris@40: Applications should only test the return value against error values defined in Chris@40: <sndfile.h> as the internal error values are subject to change at any Chris@40: time. Chris@40: For errors not in the above list, the function sf_error_number() can be used to Chris@40: convert it to an error string. Chris@40:

Chris@40: Chris@40:
Chris@40:       const char* sf_strerror     (SNDFILE *sndfile) ;
Chris@40:       const char* sf_error_number (int errnum) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: The error functions sf_strerror() and sf_error_number() convert the library's internal Chris@40: error enumerations into text strings. Chris@40:

Chris@40:
Chris@40:       int         sf_perror     (SNDFILE *sndfile) ;
Chris@40:       int         sf_error_str  (SNDFILE *sndfile, char* str, size_t len) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: The functions sf_perror() and sf_error_str() are deprecated and will be dropped Chris@40: from the library at some later date. Chris@40:

Chris@40: Chris@40: Chris@40:


File Close Function

Chris@40: Chris@40:
Chris@40:       int  sf_close  (SNDFILE *sndfile) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: The close function closes the file, deallocates its internal buffers and returns Chris@40: 0 on success or an error value otherwise. Chris@40:

Chris@40:
Chris@40: Chris@40: Chris@40:


Write Sync Function

Chris@40: Chris@40:
Chris@40:       void  sf_write_sync  (SNDFILE *sndfile) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: If the file is opened SFM_WRITE or SFM_RDWR, call the operating system's function Chris@40: to force the writing of all file cache buffers to disk. If the file is opened Chris@40: SFM_READ no action is taken. Chris@40:

Chris@40:
Chris@40: Chris@40: Chris@40: Chris@40:


File Read Functions

Chris@40: Chris@40:
Chris@40:       sf_count_t  sf_read_short   (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_read_int     (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_read_float   (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_read_double  (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
Chris@40: 
Chris@40: Chris@40: Chris@40:
Chris@40:       sf_count_t  sf_readf_short   (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_readf_int     (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_readf_float   (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_readf_double  (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: The file read functions fill the array pointed to by ptr with the Chris@40: requested number of items or frames. Chris@40:

Chris@40: Chris@40:

Chris@40: For the frames-count functions, the frames parameter specifies the number Chris@40: of frames. A frame is just a block of samples, one for each Chris@40: channel. Care must be taken to ensure that there is enough space Chris@40: in the array pointed to by ptr, to take (frames * channels) number of Chris@40: items (shorts, ints, floats or doubles). Chris@40:

Chris@40: Chris@40:

Chris@40: For the items-count functions, the items parameter must be an integer product Chris@40: of the number of channels or an error will occur. Here, an item is just a Chris@40: sample. Chris@40:

Chris@40: Chris@40:

Chris@40: Note: The only difference between the "items" and "frames" versions of Chris@40: each read function is the units in which the object count is specified Chris@40: - calling sf_readf_short with a count argument of N, on a SNDFILE with Chris@40: C channels, is the same as calling sf_read_short with a count argument Chris@40: of N*C. The buffer pointed to by "ptr" should be the same number of Chris@40: bytes in each case. Chris@40:

Chris@40: Chris@40: Chris@40:

Chris@40: Note: The data type used by the calling program and the data format of Chris@40: the file do not need to be the same. For instance, it is possible to Chris@40: open a 16 bit PCM encoded WAV file and read the data using Chris@40: sf_read_float(). The library seamlessly converts between the two Chris@40: formats on-the-fly. See Chris@40: Note 1. Chris@40:

Chris@40: Chris@40:

Chris@40: The sf_read_XXXX and sf_readf_XXXX functions return the number of Chris@40: items or frames read, respectively. Unless the end of the file was Chris@40: reached during the read, the return value should equal the number of Chris@40: objects requested. Attempts to read beyond the end of the file will Chris@40: not result in an error but will cause the read functions to return Chris@40: less than the number of objects requested or 0 if already at the end Chris@40: of the file. Chris@40:

Chris@40: Chris@40: Chris@40:


File Write Functions

Chris@40: Chris@40:
Chris@40:       sf_count_t  sf_write_short   (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_write_int     (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_write_float   (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
Chris@40:       sf_count_t  sf_write_double  (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
Chris@40: 
Chris@40: Chris@40: Chris@40:
Chris@40:       sf_count_t  sf_writef_short  (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_writef_int    (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_writef_float  (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
Chris@40:       sf_count_t  sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: The file write functions write the data in the array pointed to by ptr to the file. Chris@40:

Chris@40: Chris@40:

Chris@40: For items-count functions, the items parameter specifies the size of Chris@40: the array and must be an integer product of the number of channels or Chris@40: an error will occur. Chris@40:

Chris@40: Chris@40:

Chris@40: For the frames-count functions, the array is expected to be large enough Chris@40: to hold a number of items equal to the product of frames and the Chris@40: number of channels. Chris@40:

Chris@40: Chris@40:

As with the read functions above, the only Chris@40: difference in the items and frames version of each write function is Chris@40: the units in which the buffer size is specified. Again, the data type Chris@40: used by the calling program and the data format of the file do not Chris@40: need to be the same (Note 1). Chris@40:

Chris@40: Chris@40:

Chris@40: The sf_write_XXXX and sf_writef_XXXX functions respectively return the Chris@40: number of items or frames written (which should be the same as the Chris@40: items or frames parameter). Chris@40:

Chris@40: Chris@40: Chris@40: Chris@40:


Raw File Read and Write Functions

Chris@40: Chris@40:
Chris@40:       sf_count_t  sf_read_raw     (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
Chris@40:       sf_count_t  sf_write_raw    (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: Note: Unless you are writing an external decoder/encode that uses Chris@40: libsndfile to handle the file headers, you should not be using these Chris@40: functions. Chris@40:

Chris@40: Chris@40:

Chris@40: The raw read and write functions read raw audio data from the audio file (not to be Chris@40: confused with reading RAW header-less PCM files). The number of bytes read or written Chris@40: must always be an integer multiple of the number of channels multiplied by the number Chris@40: of bytes required to represent one sample from one channel. Chris@40:

Chris@40: Chris@40:

Chris@40: The raw read and write functions return the number of bytes read or written (which Chris@40: should be the same as the bytes parameter). Chris@40:

Chris@40: Chris@40:

Chris@40: Chris@40: Note : The result of using of both regular reads/writes and raw reads/writes on Chris@40: compressed file formats other than SF_FORMAT_ALAW and SF_FORMAT_ULAW is undefined. Chris@40: Chris@40:

Chris@40: Chris@40:

Chris@40: See also : SFC_RAW_NEEDS_ENDSWAP Chris@40:

Chris@40: Chris@40: Chris@40:


Functions for Reading and Writing String Data

Chris@40: Chris@40: Chris@40:
Chris@40:       const char* sf_get_string   (SNDFILE *sndfile, int str_type) ;
Chris@40:       int         sf_set_string   (SNDFILE *sndfile, int str_type, const char* str) ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: These functions allow strings to be set on files opened for write and to be Chris@40: retrieved from files opened for read where supported by the given file type. Chris@40: The str_type parameter can be any one of the following string types: Chris@40:

Chris@40: Chris@40:
Chris@40:           enum
Chris@40:           {   SF_STR_TITLE,
Chris@40:               SF_STR_COPYRIGHT,
Chris@40:               SF_STR_SOFTWARE,
Chris@40:               SF_STR_ARTIST,
Chris@40:               SF_STR_COMMENT,
Chris@40:               SF_STR_DATE,
Chris@40:               SF_STR_ALBUM,
Chris@40:               SF_STR_LICENSE,
Chris@40:               SF_STR_TRACKNUMBER,
Chris@40:               SF_STR_GENRE
Chris@40:           } ;
Chris@40: 
Chris@40: Chris@40:

Chris@40: The sf_get_string() function returns the specified string if it exists and a Chris@40: NULL pointer otherwise. Chris@40: In addition to the string ids above, SF_STR_FIRST (== SF_STR_TITLE) and Chris@40: SF_STR_LAST (always the same as the highest numbers string id) are also Chris@40: available to allow iteration over all the available string ids. Chris@40:

Chris@40: Chris@40:

Chris@40: The sf_set_string() function sets the string data. Chris@40: It returns zero on success and non-zero on error. Chris@40: The error code can be converted to a string using sf_error_number(). Chris@40:

Chris@40: Chris@40:

Chris@40: Strings passed to and retrieved from these two functions are assumed to be Chris@40: utf-8. Chris@40: However, while formats like Ogg/Vorbis and FLAC fully support utf-8, others Chris@40: like WAV and AIFF officially only support ASCII. Chris@40: Writing utf-8 strings to WAV and AIF files with libsndfile will work when read Chris@40: back with libsndfile, but may not work with other programs. Chris@40:

Chris@40: Chris@40:

Chris@40: The suggested method of dealing with tags retrived using sf_get_string() is to Chris@40: assume they are utf-8. Chris@40: Similarly if you have a string in some exotic format like utf-16, it should be Chris@40: encoded to utf-8 before being written using libsndfile. Chris@40:

Chris@40: Chris@40:
Chris@40: Chris@40: Chris@40:


Note 1

Chris@40: Chris@40:

Chris@40: When converting between integer PCM formats of differing size Chris@40: (e.g. using sf_read_int() to read a 16 bit PCM encoded WAV file) Chris@40: libsndfile obeys one simple rule: Chris@40:

Chris@40: Chris@40:

Chris@40: Whenever integer data is moved from one sized container to another sized container, Chris@40: the most significant bit in the source container will become the most significant bit Chris@40: in the destination container. Chris@40:

Chris@40: Chris@40:

Chris@40: When converting between integer data and floating point data, different rules apply. Chris@40: The default behaviour when reading floating point data (sf_read_float() or Chris@40: sf_read_double ()) from a file with integer data is normalisation. Regardless of Chris@40: whether data in the file is 8, 16, 24 or 32 bit wide, the data will be read as Chris@40: floating point data in the range [-1.0, 1.0]. Similarly, data in the range [-1.0, 1.0] Chris@40: will be written to an integer PCM file so that a data value of 1.0 will be the largest Chris@40: allowable integer for the given bit width. This normalisation can be turned on or off Chris@40: using the sf_command interface. Chris@40:

Chris@40: Chris@40: Chris@40:


Note 2

Chris@40: Chris@40:

Chris@40: Reading a file containg floating point data (allowable with WAV, AIFF, AU and other Chris@40: file formats) using integer read methods (sf_read_short() or sf_read_int()) can Chris@40: produce unexpected results. Chris@40: For instance the data in the file may have a maximum absolute value < 1.0 which Chris@40: would mean that all sample values read from the file will be zero. Chris@40: In order to read these files correctly using integer read methods, it is recommended Chris@40: that you use the Chris@40: sf_command Chris@40: interface, a command of Chris@40: SFC_SET_SCALE_FLOAT_INT_READ Chris@40: and a parameter of SF_TRUE to force correct scaling. Chris@40:

Chris@40: Chris@40:
Chris@40: Chris@40:

Chris@40: The libsndfile home page is Chris@40: here. Chris@40:

Chris@40:

Chris@40: Version : 1.0.27 Chris@40:

Chris@40: Chris@40: Chris@40: Chris@40: Chris@40: Chris@40: Chris@40: