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