cannam@85: cannam@85: cannam@85: cannam@85:
cannam@85:cannam@85: Libsndfile is a library designed to allow the reading and writing of many cannam@85: different sampled sound file formats (such as MS Windows WAV and the Apple/SGI cannam@85: AIFF format) through one standard library interface. cannam@85:
cannam@85: cannam@85:cannam@85: During read and write operations, formats are seamlessly converted between the cannam@85: format the application program has requested or supplied and the file's data cannam@85: format. The application programmer can remain blissfully unaware of issues cannam@85: such as file endian-ness and data format. See Note 1 and cannam@85: Note 2. cannam@85:
cannam@85: cannam@85:cannam@85: Every effort is made to keep these documents up-to-date, error free and cannam@85: unambiguous. cannam@85: However, since maintaining the documentation is the least fun part of working cannam@85: on libsndfile, these docs can and do fall behind the behaviour of library. cannam@85: If any errors, omissions or ambiguities are found, please notify me (erikd) cannam@85: at mega-nerd dot com. cannam@85:
cannam@85: cannam@85:cannam@85: To supplement this reference documentation, there are simple example programs cannam@85: included in the source code tarball. cannam@85: The test suite which is also part of the source code tarball is also a good cannam@85: place to look for the correct usage of the library functions. cannam@85:
cannam@85: cannam@85:cannam@85: Finally, if you think there is some feature missing from libsndfile, check that cannam@85: it isn't already implemented (and documented) cannam@85: here. cannam@85: cannam@85:
cannam@85: cannam@85:cannam@85: The functions of libsndfile are defined as follows: cannam@85:
cannam@85: cannam@85:cannam@85: #include <stdio.h> cannam@85: #include <sndfile.h> cannam@85: cannam@85: SNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo) ; cannam@85: SNDFILE* sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc) ; cannam@85: SNDFILE* sf_open_virtual (SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo, void *user_data) ; cannam@85: int sf_format_check (const SF_INFO *info) ; cannam@85: cannam@85: sf_count_t sf_seek (SNDFILE *sndfile, sf_count_t frames, int whence) ; cannam@85: cannam@85: int sf_command (SNDFILE *sndfile, int cmd, void *data, int datasize) ; cannam@85: cannam@85: int sf_error (SNDFILE *sndfile) ; cannam@85: const char* sf_strerror (SNDFILE *sndfile) ; cannam@85: const char* sf_error_number (int errnum) ; cannam@85: cannam@85: int sf_perror (SNDFILE *sndfile) ; cannam@85: int sf_error_str (SNDFILE *sndfile, char* str, size_t len) ; cannam@85: cannam@85: int sf_close (SNDFILE *sndfile) ; cannam@85: void sf_write_sync (SNDFILE *sndfile) ; cannam@85: cannam@85: sf_count_t sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ; cannam@85: cannam@85: sf_count_t sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ; cannam@85: cannam@85: sf_count_t sf_write_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_write_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_write_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ; cannam@85: cannam@85: sf_count_t sf_writef_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_writef_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_writef_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ; cannam@85: cannam@85: sf_count_t sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ; cannam@85: sf_count_t sf_write_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ; cannam@85: cannam@85: const char* sf_get_string (SNDFILE *sndfile, int str_type) ; cannam@85: int sf_set_string (SNDFILE *sndfile, int str_type, const char* str) ; cannam@85: cannam@85:cannam@85: cannam@85:
cannam@85: SNDFILE* is an anonymous pointer to data which is private to the library. cannam@85:
cannam@85: cannam@85: cannam@85: cannam@85:cannam@85: SNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo) ; cannam@85:cannam@85: cannam@85:
cannam@85: The SF_INFO structure is for passing data between the calling function and the library cannam@85: when opening a file for reading or writing. It is defined in sndfile.h as follows: cannam@85:
cannam@85: cannam@85:cannam@85: typedef struct cannam@85: { sf_count_t frames ; /* Used to be called samples. */ cannam@85: int samplerate ; cannam@85: int channels ; cannam@85: int format ; cannam@85: int sections ; cannam@85: int seekable ; cannam@85: } SF_INFO ; cannam@85:cannam@85: cannam@85:
cannam@85: The mode parameter for this function can be any one of the following three values: cannam@85:
cannam@85: cannam@85:cannam@85: SFM_READ - read only mode cannam@85: SFM_WRITE - write only mode cannam@85: SFM_RDWR - read/write mode cannam@85:cannam@85: cannam@85:
cannam@85: When opening a file for read, the format field should be set to zero before cannam@85: calling sf_open(). cannam@85: The only exception to this is the case of RAW files where the caller has to set cannam@85: the samplerate, channels and format fields to valid values. cannam@85: All other fields of the structure are filled in by the library. cannam@85:
cannam@85: cannam@85:cannam@85: When opening a file for write, the caller must fill in structure members samplerate, cannam@85: channels, and format. cannam@85:
cannam@85: cannam@85:cannam@85: The format field in the above SF_INFO structure is made up of the bit-wise OR of a cannam@85: major format type (values between 0x10000 and 0x08000000), a minor format type cannam@85: (with values less than 0x10000) and an optional endian-ness value. cannam@85: The currently understood formats are listed in sndfile.h as follows and also include cannam@85: bitmasks for separating major and minor file types. cannam@85: Not all combinations of endian-ness and major and minor file types are valid. cannam@85:
cannam@85: cannam@85:cannam@85: enum cannam@85: { /* Major formats. */ cannam@85: SF_FORMAT_WAV = 0x010000, /* Microsoft WAV format (little endian). */ cannam@85: SF_FORMAT_AIFF = 0x020000, /* Apple/SGI AIFF format (big endian). */ cannam@85: SF_FORMAT_AU = 0x030000, /* Sun/NeXT AU format (big endian). */ cannam@85: SF_FORMAT_RAW = 0x040000, /* RAW PCM data. */ cannam@85: SF_FORMAT_PAF = 0x050000, /* Ensoniq PARIS file format. */ cannam@85: SF_FORMAT_SVX = 0x060000, /* Amiga IFF / SVX8 / SV16 format. */ cannam@85: SF_FORMAT_NIST = 0x070000, /* Sphere NIST format. */ cannam@85: SF_FORMAT_VOC = 0x080000, /* VOC files. */ cannam@85: SF_FORMAT_IRCAM = 0x0A0000, /* Berkeley/IRCAM/CARL */ cannam@85: SF_FORMAT_W64 = 0x0B0000, /* Sonic Foundry's 64 bit RIFF/WAV */ cannam@85: SF_FORMAT_MAT4 = 0x0C0000, /* Matlab (tm) V4.2 / GNU Octave 2.0 */ cannam@85: SF_FORMAT_MAT5 = 0x0D0000, /* Matlab (tm) V5.0 / GNU Octave 2.1 */ cannam@85: SF_FORMAT_PVF = 0x0E0000, /* Portable Voice Format */ cannam@85: SF_FORMAT_XI = 0x0F0000, /* Fasttracker 2 Extended Instrument */ cannam@85: SF_FORMAT_HTK = 0x100000, /* HMM Tool Kit format */ cannam@85: SF_FORMAT_SDS = 0x110000, /* Midi Sample Dump Standard */ cannam@85: SF_FORMAT_AVR = 0x120000, /* Audio Visual Research */ cannam@85: SF_FORMAT_WAVEX = 0x130000, /* MS WAVE with WAVEFORMATEX */ cannam@85: SF_FORMAT_SD2 = 0x160000, /* Sound Designer 2 */ cannam@85: SF_FORMAT_FLAC = 0x170000, /* FLAC lossless file format */ cannam@85: SF_FORMAT_CAF = 0x180000, /* Core Audio File format */ cannam@85: SF_FORMAT_WVE = 0x190000, /* Psion WVE format */ cannam@85: SF_FORMAT_OGG = 0x200000, /* Xiph OGG container */ cannam@85: SF_FORMAT_MPC2K = 0x210000, /* Akai MPC 2000 sampler */ cannam@85: SF_FORMAT_RF64 = 0x220000, /* RF64 WAV file */ cannam@85: cannam@85: /* Subtypes from here on. */ cannam@85: cannam@85: SF_FORMAT_PCM_S8 = 0x0001, /* Signed 8 bit data */ cannam@85: SF_FORMAT_PCM_16 = 0x0002, /* Signed 16 bit data */ cannam@85: SF_FORMAT_PCM_24 = 0x0003, /* Signed 24 bit data */ cannam@85: SF_FORMAT_PCM_32 = 0x0004, /* Signed 32 bit data */ cannam@85: cannam@85: SF_FORMAT_PCM_U8 = 0x0005, /* Unsigned 8 bit data (WAV and RAW only) */ cannam@85: cannam@85: SF_FORMAT_FLOAT = 0x0006, /* 32 bit float data */ cannam@85: SF_FORMAT_DOUBLE = 0x0007, /* 64 bit float data */ cannam@85: cannam@85: SF_FORMAT_ULAW = 0x0010, /* U-Law encoded. */ cannam@85: SF_FORMAT_ALAW = 0x0011, /* A-Law encoded. */ cannam@85: SF_FORMAT_IMA_ADPCM = 0x0012, /* IMA ADPCM. */ cannam@85: SF_FORMAT_MS_ADPCM = 0x0013, /* Microsoft ADPCM. */ cannam@85: cannam@85: SF_FORMAT_GSM610 = 0x0020, /* GSM 6.10 encoding. */ cannam@85: SF_FORMAT_VOX_ADPCM = 0x0021, /* Oki Dialogic ADPCM encoding. */ cannam@85: cannam@85: SF_FORMAT_G721_32 = 0x0030, /* 32kbs G721 ADPCM encoding. */ cannam@85: SF_FORMAT_G723_24 = 0x0031, /* 24kbs G723 ADPCM encoding. */ cannam@85: SF_FORMAT_G723_40 = 0x0032, /* 40kbs G723 ADPCM encoding. */ cannam@85: cannam@85: SF_FORMAT_DWVW_12 = 0x0040, /* 12 bit Delta Width Variable Word encoding. */ cannam@85: SF_FORMAT_DWVW_16 = 0x0041, /* 16 bit Delta Width Variable Word encoding. */ cannam@85: SF_FORMAT_DWVW_24 = 0x0042, /* 24 bit Delta Width Variable Word encoding. */ cannam@85: SF_FORMAT_DWVW_N = 0x0043, /* N bit Delta Width Variable Word encoding. */ cannam@85: cannam@85: SF_FORMAT_DPCM_8 = 0x0050, /* 8 bit differential PCM (XI only) */ cannam@85: SF_FORMAT_DPCM_16 = 0x0051, /* 16 bit differential PCM (XI only) */ cannam@85: cannam@85: SF_FORMAT_VORBIS = 0x0060, /* Xiph Vorbis encoding. */ cannam@85: cannam@85: /* Endian-ness options. */ cannam@85: cannam@85: SF_ENDIAN_FILE = 0x00000000, /* Default file endian-ness. */ cannam@85: SF_ENDIAN_LITTLE = 0x10000000, /* Force little endian-ness. */ cannam@85: SF_ENDIAN_BIG = 0x20000000, /* Force big endian-ness. */ cannam@85: SF_ENDIAN_CPU = 0x30000000, /* Force CPU endian-ness. */ cannam@85: cannam@85: SF_FORMAT_SUBMASK = 0x0000FFFF, cannam@85: SF_FORMAT_TYPEMASK = 0x0FFF0000, cannam@85: SF_FORMAT_ENDMASK = 0x30000000 cannam@85: } ; cannam@85:cannam@85: cannam@85:
cannam@85: Every call to sf_open() should be matched with a call to sf_close() to free up cannam@85: memory allocated during the call to sf_open(). cannam@85:
cannam@85: cannam@85:cannam@85: On success, the sf_open function returns a non-NULL pointer which should be cannam@85: passed as the first parameter to all subsequent libsndfile calls dealing with cannam@85: that audio file. cannam@85: On fail, the sf_open function returns a NULL pointer. cannam@85: An explanation of the error can obtained by passing NULL to cannam@85: sf_strerror. cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: SNDFILE* sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc) ; cannam@85:cannam@85: cannam@85:
cannam@85: Note: On Microsoft Windows, this function does not work if the cannam@85: application and the libsndfile DLL are linked to different versions of the cannam@85: Microsoft C runtime DLL. cannam@85:
cannam@85:cannam@85: The second open function takes a file descriptor of a file that has already been cannam@85: opened. cannam@85: Care should be taken to ensure that the mode of the file represented by the cannam@85: descriptor matches the mode argument. cannam@85: This function is useful in the following circumstances: cannam@85:
cannam@85: cannam@85:cannam@85: Every call to sf_open_fd() should be matched with a call to sf_close() to free up cannam@85: memory allocated during the call to sf_open(). cannam@85:
cannam@85: cannam@85:cannam@85: When sf_close() is called, the file descriptor is only closed if the close_desc cannam@85: parameter was TRUE when the sf_open_fd() function was called. cannam@85:
cannam@85: cannam@85:cannam@85: On success, the sf_open_fd function returns a non-NULL pointer which should be cannam@85: passed as the first parameter to all subsequent libsndfile calls dealing with cannam@85: that audio file. cannam@85: On fail, the sf_open_fd function returns a NULL pointer. cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: SNDFILE* sf_open_virtual (SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo, void *user_data) ; cannam@85:cannam@85:
cannam@85: Opens a soundfile from a virtual file I/O context which is provided cannam@85: by the caller. This is usually used to interface libsndfile to a stream or buffer cannam@85: based system. Apart from the sfvirtual and the user_data parameters this function behaves cannam@85: like sf_open. cannam@85:
cannam@85: cannam@85:cannam@85: typedef struct cannam@85: { sf_vio_get_filelen get_filelen ; cannam@85: sf_vio_seek seek ; cannam@85: sf_vio_read read ; cannam@85: sf_vio_write write ; cannam@85: sf_vio_tell tell ; cannam@85: } SF_VIRTUAL_IO ; cannam@85:cannam@85:
cannam@85: Libsndfile calls the callbacks provided by the SF_VIRTUAL_IO structure when opening, reading cannam@85: and writing to the virtual file context. The user_data pointer is a user defined context which cannam@85: will be available in the callbacks. cannam@85:
cannam@85:cannam@85: typedef sf_count_t (*sf_vio_get_filelen) (void *user_data) ; cannam@85: typedef sf_count_t (*sf_vio_seek) (sf_count_t offset, int whence, void *user_data) ; cannam@85: typedef sf_count_t (*sf_vio_read) (void *ptr, sf_count_t count, void *user_data) ; cannam@85: typedef sf_count_t (*sf_vio_write) (const void *ptr, sf_count_t count, void *user_data) ; cannam@85: typedef sf_count_t (*sf_vio_tell) (void *user_data) ; cannam@85:cannam@85:
cannam@85: typedef sf_count_t (*sf_vio_get_filelen) (void *user_data) ; cannam@85:cannam@85:
cannam@85: The virtual file contex must return the length of the virtual file in bytes.
cannam@85:
cannam@85: typedef sf_count_t (*sf_vio_seek) (sf_count_t offset, int whence, void *user_data) ; cannam@85:cannam@85:
cannam@85: The virtual file context must seek to offset using the seek mode provided by whence which is one of
cannam@85:
cannam@85: SEEK_CUR cannam@85: SEEK_SET cannam@85: SEEK_END cannam@85:cannam@85:
cannam@85: The return value must contain the new offset in the file. cannam@85:
cannam@85:cannam@85: typedef sf_count_t (*sf_vio_read) (void *ptr, sf_count_t count, void *user_data) ; cannam@85:cannam@85:
cannam@85: The virtual file context must copy ("read") "count" bytes into the cannam@85: buffer provided by ptr and return the count of actually copied bytes. cannam@85:
cannam@85:cannam@85: typedef sf_count_t (*sf_vio_write) (const void *ptr, sf_count_t count, void *user_data) ; cannam@85:cannam@85:
cannam@85: The virtual file context must process "count" bytes stored in the
cannam@85: buffer passed with ptr and return the count of actually processed bytes.
cannam@85:
cannam@85: typedef sf_count_t (*sf_vio_tell) (void *user_data) ; cannam@85:cannam@85:
cannam@85: Return the current position of the virtual file context.
cannam@85:
cannam@85: int sf_format_check (const SF_INFO *info) ; cannam@85:cannam@85: cannam@85:
cannam@85: This function allows the caller to check if a set of parameters in the SF_INFO struct cannam@85: is valid before calling sf_open (SFM_WRITE). cannam@85:
cannam@85:cannam@85: sf_format_check returns TRUE if the parameters are valid and FALSE otherwise. cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: sf_count_t sf_seek (SNDFILE *sndfile, sf_count_t frames, int whence) ; cannam@85:cannam@85: cannam@85:
cannam@85: The file seek functions work much like lseek in unistd.h with the exception that cannam@85: the non-audio data is ignored and the seek only moves within the audio data section of cannam@85: the file. cannam@85: In addition, seeks are defined in number of (multichannel) frames. cannam@85: Therefore, a seek in a stereo file from the current position forward with an offset cannam@85: of 1 would skip forward by one sample of both channels. cannam@85:
cannam@85: cannam@85:cannam@85: like lseek(), the whence parameter can be any one of the following three values: cannam@85:
cannam@85: cannam@85:cannam@85: SEEK_SET - The offset is set to the start of the audio data plus offset (multichannel) frames. cannam@85: SEEK_CUR - The offset is set to its current location plus offset (multichannel) frames. cannam@85: SEEK_END - The offset is set to the end of the data plus offset (multichannel) frames. cannam@85:cannam@85: cannam@85:
cannam@85: Internally, libsndfile keeps track of the read and write locations using separate cannam@85: read and write pointers. cannam@85: If a file has been opened with a mode of SFM_RDWR, bitwise OR-ing the standard whence cannam@85: values above with either SFM_READ or SFM_WRITE allows the read and write pointers to cannam@85: be modified separately. cannam@85: If the SEEK_* values are used on their own, the read and write pointers are cannam@85: both modified. cannam@85:
cannam@85: cannam@85:cannam@85: Note that the frames offset can be negative and in fact should be when SEEK_END is used for the cannam@85: whence parameter. cannam@85:
cannam@85:cannam@85: sf_seek will return the offset in (multichannel) frames from the start of the audio data cannam@85: or -1 if an error occured (ie an attempt is made to seek beyond the start or end of the file). cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: int sf_error (SNDFILE *sndfile) ; cannam@85:cannam@85:
cannam@85: This function returns the current error number for the given SNDFILE. cannam@85: The error number may be one of the following: cannam@85:
cannam@85:cannam@85: enum cannam@85: { SF_ERR_NO_ERROR = 0, cannam@85: SF_ERR_UNRECOGNISED_FORMAT = 1, cannam@85: SF_ERR_SYSTEM = 2, cannam@85: SF_ERR_MALFORMED_FILE = 3, cannam@85: SF_ERR_UNSUPPORTED_ENCODING = 4 cannam@85: } ; cannam@85:cannam@85: cannam@85:
cannam@85: or any one of many other internal error values. cannam@85: Applications should only test the return value against error values defined in cannam@85: <sndfile.h> as the internal error values are subject to change at any cannam@85: time. cannam@85: For errors not in the above list, the function sf_error_number() can be used to cannam@85: convert it to an error string. cannam@85:
cannam@85: cannam@85:cannam@85: const char* sf_strerror (SNDFILE *sndfile) ; cannam@85: const char* sf_error_number (int errnum) ; cannam@85:cannam@85: cannam@85:
cannam@85: The error functions sf_strerror() and sf_error_number() convert the library's internal cannam@85: error enumerations into text strings. cannam@85:
cannam@85:cannam@85: int sf_perror (SNDFILE *sndfile) ; cannam@85: int sf_error_str (SNDFILE *sndfile, char* str, size_t len) ; cannam@85:cannam@85: cannam@85:
cannam@85: The functions sf_perror() and sf_error_str() are deprecated and will be dropped cannam@85: from the library at some later date. cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: int sf_close (SNDFILE *sndfile) ; cannam@85:cannam@85: cannam@85:
cannam@85: The close function closes the file, deallocates its internal buffers and returns cannam@85: 0 on success or an error value otherwise. cannam@85:
cannam@85:cannam@85: void sf_write_sync (SNDFILE *sndfile) ; cannam@85:cannam@85: cannam@85:
cannam@85: If the file is opened SFM_WRITE or SFM_RDWR, call the operating system's function cannam@85: to force the writing of all file cache buffers to disk. If the file is opened cannam@85: SFM_READ no action is taken. cannam@85:
cannam@85:cannam@85: sf_count_t sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ; cannam@85:cannam@85: cannam@85:
cannam@85: The file read items functions fill the array pointed to by ptr with the requested cannam@85: number of items. The items parameter must be an integer product of the number cannam@85: of channels or an error will occur. cannam@85:
cannam@85: cannam@85:cannam@85: It is important to note that the data type used by the calling program and the data cannam@85: format of the file do not need to be the same. For instance, it is possible to open cannam@85: a 16 bit PCM encoded WAV file and read the data using sf_read_float(). The library cannam@85: seamlessly converts between the two formats on-the-fly. See cannam@85: Note 1. cannam@85:
cannam@85: cannam@85:cannam@85: The sf_read_XXXX functions return the number of items read. cannam@85: Unless the end of the file was reached during the read, the return value should cannam@85: equal the number of items requested. cannam@85: Attempts to read beyond the end of the file will not result in an error but will cannam@85: cause the sf_read_XXXX functions to return less than the number of items requested cannam@85: or 0 if already at the end of the file. cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: sf_count_t sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ; cannam@85:cannam@85: cannam@85:
cannam@85: The file read frames functions fill the array pointed to by ptr with the requested cannam@85: number of frames of data. The array must be large enough to hold the product of cannam@85: frames and the number of channels. cannam@85:
cannam@85: cannam@85:cannam@85: Care must be taken to ensure that there is enough space in the array pointed to by cannam@85: ptr, to take (frames * channels) number of items (shorts, ints, floats or doubles). cannam@85:
cannam@85: cannam@85:cannam@85: The sf_readf_XXXX functions return the number of frames read. cannam@85: Unless the end of the file was reached during the read, the return value should equal cannam@85: the number of frames requested. cannam@85: Attempts to read beyond the end of the file will not result in an error but will cause cannam@85: the sf_readf_XXXX functions to return less than the number of frames requested or 0 if cannam@85: already at the end of the file. cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: sf_count_t sf_write_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_write_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ; cannam@85: sf_count_t sf_write_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ; cannam@85:cannam@85: cannam@85:
cannam@85: The file write items functions write the data in the array pointed to by ptr to the file. cannam@85: The items parameter must be an integer product of the number of channels or an error cannam@85: will occur. cannam@85:
cannam@85: cannam@85:cannam@85: It is important to note that the data type used by the calling program and the data cannam@85: format of the file do not need to be the same. For instance, it is possible to open cannam@85: a 16 bit PCM encoded WAV file and write the data using sf_write_float(). The library cannam@85: seamlessly converts between the two formats on-the-fly. See cannam@85: Note 1. cannam@85:
cannam@85:cannam@85: The sf_write_XXXX functions return the number of items written (which should be the cannam@85: same as the items parameter). cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: sf_count_t sf_writef_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_writef_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_writef_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ; cannam@85: sf_count_t sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ; cannam@85:cannam@85: cannam@85:
cannam@85: The file write frames functions write the data in the array pointed to by ptr to the file. cannam@85: The array must be large enough to hold the product of frames and the number of channels. cannam@85:
cannam@85:cannam@85: The sf_writef_XXXX functions return the number of frames written (which should be the cannam@85: same as the frames parameter). cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: sf_count_t sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ; cannam@85: sf_count_t sf_write_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ; cannam@85:cannam@85: cannam@85:
cannam@85: Note: Unless you are writing an external decoder/encode that uses cannam@85: libsndfile to handle the file headers, you should not be using these cannam@85: functions. cannam@85:
cannam@85: cannam@85:cannam@85: The raw read and write functions read raw audio data from the audio file (not to be cannam@85: confused with reading RAW header-less PCM files). The number of bytes read or written cannam@85: must always be an integer multiple of the number of channels multiplied by the number cannam@85: of bytes required to represent one sample from one channel. cannam@85:
cannam@85: cannam@85:cannam@85: The raw read and write functions return the number of bytes read or written (which cannam@85: should be the same as the bytes parameter). cannam@85:
cannam@85: cannam@85:cannam@85: cannam@85: Note : The result of using of both regular reads/writes and raw reads/writes on cannam@85: compressed file formats other than SF_FORMAT_ALAW and SF_FORMAT_ULAW is undefined. cannam@85: cannam@85:
cannam@85: cannam@85:cannam@85: See also : SFC_RAW_NEEDS_ENDSWAP cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: const char* sf_get_string (SNDFILE *sndfile, int str_type) ; cannam@85: int sf_set_string (SNDFILE *sndfile, int str_type, const char* str) ; cannam@85:cannam@85: cannam@85:
cannam@85: These functions allow strings to be set on files opened for write and to be cannam@85: retrieved from files opened for read where supported by the given file type. cannam@85: The str_type parameter can be any one of the following string types: cannam@85:
cannam@85: cannam@85:cannam@85: enum cannam@85: { SF_STR_TITLE, cannam@85: SF_STR_COPYRIGHT, cannam@85: SF_STR_SOFTWARE, cannam@85: SF_STR_ARTIST, cannam@85: SF_STR_COMMENT, cannam@85: SF_STR_DATE, cannam@85: SF_STR_ALBUM, cannam@85: SF_STR_LICENSE, cannam@85: SF_STR_TRACKNUMBER, cannam@85: SF_STR_GENRE cannam@85: } ; cannam@85:cannam@85: cannam@85:
cannam@85: The sf_get_string() function returns the specified string if it exists and a cannam@85: NULL pointer otherwise. cannam@85: In addition to the string ids above, SF_STR_FIRST (== SF_STR_TITLE) and cannam@85: SF_STR_LAST (always the same as the highest numbers string id) are also cannam@85: available to allow iteration over all the available string ids. cannam@85:
cannam@85: cannam@85:cannam@85: The sf_set_string() function sets the string data. cannam@85: It returns zero on success and non-zero on error. cannam@85: The error code can be converted to a string using sf_error_number(). cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: cannam@85:
cannam@85: cannam@85:cannam@85: When converting between integer PCM formats of differing size (ie using sf_read_int() cannam@85: to read a 16 bit PCM encoded WAV file) libsndfile obeys one simple rule: cannam@85:
cannam@85: cannam@85:cannam@85: Whenever integer data is moved from one sized container to another sized container, cannam@85: the most significant bit in the source container will become the most significant bit cannam@85: in the destination container. cannam@85:
cannam@85: cannam@85:cannam@85: When converting between integer data and floating point data, different rules apply. cannam@85: The default behaviour when reading floating point data (sf_read_float() or cannam@85: sf_read_double ()) from a file with integer data is normalisation. Regardless of cannam@85: whether data in the file is 8, 16, 24 or 32 bit wide, the data will be read as cannam@85: floating point data in the range [-1.0, 1.0]. Similarly, data in the range [-1.0, 1.0] cannam@85: will be written to an integer PCM file so that a data value of 1.0 will be the largest cannam@85: allowable integer for the given bit width. This normalisation can be turned on or off cannam@85: using the sf_command interface. cannam@85:
cannam@85: cannam@85: cannam@85:cannam@85: Reading a file containg floating point data (allowable with WAV, AIFF, AU and other cannam@85: file formats) using integer read methods (sf_read_short() or sf_read_int()) can cannam@85: produce unexpected results. cannam@85: For instance the data in the file may have a maximum absolute value < 1.0 which cannam@85: would mean that all sample values read from the file will be zero. cannam@85: In order to read these files correctly using integer read methods, it is recommended cannam@85: that you use the cannam@85: sf_command cannam@85: interface, a command of cannam@85: SFC_SET_SCALE_FLOAT_INT_READ cannam@85: and a parameter of SF_TRUE to force correct scaling. cannam@85:
cannam@85: cannam@85:cannam@85: The libsndfile home page is cannam@85: here. cannam@85:
cannam@85:cannam@85: Version : 1.0.25 cannam@85:
cannam@85: cannam@85: cannam@85: cannam@85: cannam@85: cannam@85: cannam@85: