|
The libsndfile API.
libsndfileLibsndfile 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 seamless 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. This document covers version 1 of libsndfile only. The changes between libsndfile version 0 and version 1 can be viewed here. 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. Finally, if you think there is some feature missing from libsndfile, check that it isn't already implemented (and documented) here. SYNOPSISThe functions of linbsndfile are defined as follows: #include <stdio.h> #include <sndfile.h> SNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo) ; 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) ; 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) ; SNDFILE* is an anonymous pointer to data which is private to the library. File Open FunctionSNDFILE* 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 read 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(). All other fields of the structure are filled in by the library. The only exception to this is the case of RAW files where the caller has to set in the channels and format fields to valid values. 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 0x08000000), a minor format type with (values less than 0x10000) and an optional endian-ness value. The currently understood formats are listed in sndfile.h as follows and also includes bitmasks for separating major and minor file types. Not all combinations of endian-ness and major and minor file types are valid. enum { /* Major formats. */ SF_FORMAT_WAV = 0x010000, /* Microsoft WAV format (little endian). */ SF_FORMAT_AIFF = 0x020000, /* Apple/SGI AIFF format (big endian). */ SF_FORMAT_AU = 0x030000, /* Sun/NeXT AU format (big endian). */ SF_FORMAT_RAW = 0x040000, /* RAW PCM data. */ SF_FORMAT_PAF = 0x050000, /* Ensoniq PARIS file format. */ SF_FORMAT_SVX = 0x060000, /* Amiga IFF / SVX8 / SV16 format. */ SF_FORMAT_NIST = 0x070000, /* Sphere NIST format. */ SF_FORMAT_VOC = 0x080000, /* VOC files. */ SF_FORMAT_IRCAM = 0x0A0000, /* Berkeley/IRCAM/CARL */ SF_FORMAT_W64 = 0x0B0000, /* Sonic Foundry's 64 bit RIFF/WAV */ SF_FORMAT_MAT4 = 0x0C0000, /* Matlab (tm) V4.2 / GNU Octave 2.0 */ SF_FORMAT_MAT5 = 0x0D0000, /* Matlab (tm) V5.0 / GNU Octave 2.1 */ /* Subtypes from here on. */ SF_FORMAT_PCM_S8 = 0x0001, /* Signed 8 bit data */ SF_FORMAT_PCM_16 = 0x0002, /* Signed 16 bit data */ SF_FORMAT_PCM_24 = 0x0003, /* Signed 24 bit data */ SF_FORMAT_PCM_32 = 0x0004, /* Signed 32 bit data */ SF_FORMAT_PCM_U8 = 0x0005, /* Unsigned 8 bit data (WAV and RAW only) */ SF_FORMAT_FLOAT = 0x0006, /* 32 bit float data */ SF_FORMAT_DOUBLE = 0x0007, /* 64 bit float data */ SF_FORMAT_ULAW = 0x0010, /* U-Law encoded. */ SF_FORMAT_ALAW = 0x0011, /* A-Law encoded. */ SF_FORMAT_IMA_ADPCM = 0x0012, /* IMA ADPCM. */ SF_FORMAT_MS_ADPCM = 0x0013, /* Microsoft ADPCM. */ SF_FORMAT_GSM610 = 0x0020, /* GSM 6.10 encoding. */ SF_FORMAT_VOX_ADPCM = 0x0021, /* Oki Dialogic ADPCM encoding. */ SF_FORMAT_G721_32 = 0x0030, /* 32kbs G721 ADPCM encoding. */ SF_FORMAT_G723_24 = 0x0031, /* 24kbs G723 ADPCM encoding. */ SF_FORMAT_G723_40 = 0x0032, /* 40kbs G723 ADPCM encoding. */ SF_FORMAT_DWVW_12 = 0x0040, /* 12 bit Delta Width Variable Word encoding. */ SF_FORMAT_DWVW_16 = 0x0041, /* 16 bit Delta Width Variable Word encoding. */ SF_FORMAT_DWVW_24 = 0x0042, /* 24 bit Delta Width Variable Word encoding. */ SF_FORMAT_DWVW_N = 0x0043, /* N bit Delta Width Variable Word encoding. */ /* Endian-ness options. */ SF_ENDIAN_FILE = 0x00000000, /* Default file endian-ness. */ SF_ENDIAN_LITTLE = 0x10000000, /* Force little endian-ness. */ SF_ENDIAN_BIG = 0x20000000, /* Force big endian-ness. */ SF_ENDIAN_CPU = 0x30000000, /* Force CPU endian-ness. */ SF_FORMAT_SUBMASK = 0x0000FFFF, SF_FORMAT_TYPEMASK = 0x0FFF0000, SF_FORMAT_ENDMASK = 0x30000000 } ; Every call to sf_open() should be matched with a call to sf_close() to free up memory allocated by during the call to sf_open(). On success, the sf_open functions return a non NULL pointer which should be passed as the first parameter to all subsequent libsndfile calls dealing with that audio file. On fail, the sf_open functions return a NULL pointer. Format Check Functionint sf_format_check (const SF_INFO *info) ; This function allows the caller to check if a set of parameters in the SF_INFO struct is valid before calling sf_open (SFM_WRITE). sf_format_check returns TRUE if the parameters are valid and FALSE otherwise. File Seek Functionssf_count_t sf_seek (SNDFILE *sndfile, sf_count_t frames, int whence) ; The file seek functions work much like lseek in unistd.h with the exception that the non-audio data is ignored and the seek only moves within the audio data section of the file. In addition, seeks are defined in number of (multichannel) frames. Therefore, for a seek in a stereo file from the current position forward with an offset of 1 would skip forward by one sample of both channels. like lseek(), the whence parameter can be any one of the following three values: SEEK_SET - The offset is set to the start of the audio data plus offset (multichannel) frames. SEEK_CUR - The offset is set to its current location plus offset (multichannel) frames. SEEK_END - The offset is set to the end of the data plus offset (multichannel) frames. Internally, libsndfile keeps track of the read and write locations using separate read and write pointers. If a file has been opened with a mode of SFM_RDWR, bitwise OR-ing the standard whence values above with either SFM_READ or SFM_WRITE allows the read and write pointers to be modified separately. If the SEEK_* values are used on their own, the read and write pointers are both modified. Note that frames offset can be negative and in fact should be when SEEK_END is used for the whence parameter. sf_seek will return the offset in (multichannel) frames from the start of the audio data or -1 if an error occurs (ie an attempt is made to seek beyond the start or end of the file).
|