Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: The libsndfile API Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: Chris@0:
Chris@0:

libsndfile

Chris@0:

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

Chris@0: Chris@0:

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

Chris@0: Chris@0:

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

Chris@0: Chris@0:

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

Chris@0: Chris@0:

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

Chris@0: Chris@0:

Synopsis

Chris@0:

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

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

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

Chris@0: Chris@0: Chris@0: Chris@0:

File Open Function

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

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

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

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

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

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

Chris@0: Chris@0:

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

Chris@0: Chris@0:

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

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

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

Chris@0: Chris@0:

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

Chris@0: Chris@0: Chris@0:

File Descriptor Open

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

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

Chris@0:

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

Chris@0: Chris@0: Chris@0: Chris@0:

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

Chris@0: Chris@0:

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

Chris@0: Chris@0:

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

Chris@0: Chris@0: Chris@0:

Virtual File Open Function

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

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

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

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

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

sf_vio_get_filelen

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

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

Chris@0:

sf_vio_seek

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

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

Chris@0:
Chris@0:       SEEK_CUR
Chris@0:       SEEK_SET
Chris@0:       SEEK_END
Chris@0: 
Chris@0:

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

Chris@0:

sf_vio_read

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

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

Chris@0:

sf_vio_write

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

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

Chris@0:

sf_vio_tell

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

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

Chris@0: Chris@0: Chris@0: Chris@0:

Format Check Function

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

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

Chris@0:

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

Chris@0: Chris@0: Chris@0:

File Seek Functions

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

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

Chris@0: Chris@0:

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

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

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

Chris@0: Chris@0:

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

Chris@0:

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

Chris@0: Chris@0: Chris@0:


Error Reporting Functions

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

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

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

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

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

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

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

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

Chris@0: Chris@0: Chris@0:


File Close Function

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

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

Chris@0:
Chris@0: Chris@0: Chris@0:


Write Sync Function

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

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

Chris@0:
Chris@0: Chris@0: Chris@0: Chris@0:


File Read Functions (Items)

Chris@0: Chris@0:
Chris@0:       sf_count_t  sf_read_short   (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
Chris@0:       sf_count_t  sf_read_int     (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
Chris@0:       sf_count_t  sf_read_float   (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
Chris@0:       sf_count_t  sf_read_double  (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
Chris@0: 
Chris@0: Chris@0:

Chris@0: The file read items functions fill the array pointed to by ptr with the requested Chris@0: number of items. The items parameter must be an integer product of the number Chris@0: of channels or an error will occur. Chris@0:

Chris@0: Chris@0:

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

Chris@0: Chris@0:

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

Chris@0: Chris@0: Chris@0:


File Read Functions (Frames)

Chris@0: Chris@0:
Chris@0:       sf_count_t  sf_readf_short   (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
Chris@0:       sf_count_t  sf_readf_int     (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
Chris@0:       sf_count_t  sf_readf_float   (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
Chris@0:       sf_count_t  sf_readf_double  (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
Chris@0: 
Chris@0: Chris@0:

Chris@0: The file read frames functions fill the array pointed to by ptr with the requested Chris@0: number of frames of data. The array must be large enough to hold the product of Chris@0: frames and the number of channels. Chris@0:

Chris@0: Chris@0:

Chris@0: Care must be taken to ensure that there is enough space in the array pointed to by Chris@0: ptr, to take (frames * channels) number of items (shorts, ints, floats or doubles). Chris@0:

Chris@0: Chris@0:

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

Chris@0: Chris@0: Chris@0:


File Write Functions (Items)

Chris@0: Chris@0:
Chris@0:       sf_count_t  sf_write_short   (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
Chris@0:       sf_count_t  sf_write_int     (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
Chris@0:       sf_count_t  sf_write_float   (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
Chris@0:       sf_count_t  sf_write_double  (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
Chris@0: 
Chris@0: Chris@0:

Chris@0: The file write items functions write the data in the array pointed to by ptr to the file. Chris@0: The items parameter must be an integer product of the number of channels or an error Chris@0: will occur. Chris@0:

Chris@0: Chris@0:

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

Chris@0:

Chris@0: The sf_write_XXXX functions return the number of items written (which should be the Chris@0: same as the items parameter). Chris@0:

Chris@0: Chris@0: Chris@0:


File Write Functions (Frames)

Chris@0: Chris@0:
Chris@0:       sf_count_t  sf_writef_short  (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
Chris@0:       sf_count_t  sf_writef_int    (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
Chris@0:       sf_count_t  sf_writef_float  (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
Chris@0:       sf_count_t  sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
Chris@0: 
Chris@0: Chris@0:

Chris@0: The file write frames functions write the data in the array pointed to by ptr to the file. Chris@0: The array must be large enough to hold the product of frames and the number of channels. Chris@0:

Chris@0:

Chris@0: The sf_writef_XXXX functions return the number of frames written (which should be the Chris@0: same as the frames parameter). Chris@0:

Chris@0: Chris@0: Chris@0:


Raw File Read and Write Functions

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

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

Chris@0: Chris@0:

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

Chris@0: Chris@0:

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

Chris@0: Chris@0:

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

Chris@0: Chris@0:

Chris@0: See also : SFC_RAW_NEEDS_ENDSWAP Chris@0:

Chris@0: Chris@0: Chris@0:


Functions for Reading and Writing String Data

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

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

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

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

Chris@0: Chris@0:

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

Chris@0: Chris@0: Chris@0:

Chris@0: Chris@0:

Chris@0: Chris@0:
Chris@0: Chris@0: Chris@0:


Note 1

Chris@0: Chris@0:

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

Chris@0: Chris@0:

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

Chris@0: Chris@0:

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

Chris@0: Chris@0: Chris@0:


Note 2

Chris@0: Chris@0:

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

Chris@0: Chris@0:
Chris@0: Chris@0:

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

Chris@0:

Chris@0: Version : 1.0.25 Chris@0:

Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: Chris@0: