third_party_libsnd/doc/api.html

680 lines
26 KiB
HTML
Raw Normal View History

2007-05-14 09:55:24 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>
The libsndfile API.
</TITLE>
<META NAME="Author" CONTENT="Erik de Castro Lopo (erikd AT mega-nerd DOT com)">
2007-05-14 09:55:24 +00:00
<META NAME="Description" CONTENT="The libsndfile API.">
<META NAME="Keywords" CONTENT="WAV AIFF AU libsndfile sound audio dsp Linux">
<LINK REL="stylesheet" HREF="libsndfile.css" TYPE="text/css" MEDIA="all">
<LINK REL="stylesheet" HREF="print.css" TYPE="text/css" MEDIA="print">
2007-05-14 09:55:24 +00:00
</HEAD>
2004-04-25 13:09:52 +00:00
<BODY>
2007-05-14 09:55:24 +00:00
<BR>
<H1><B>libsndfile</B></H1>
<P>
2004-06-21 22:49:08 +00:00
Libsndfile is a library designed to allow the reading and writing of many
different sampled sound file formats (such as MS Windows WAV and the Apple/SGI
AIFF format) through one standard library interface.
2007-05-14 09:55:24 +00:00
</P>
<!-- pepper -->
<P>
2004-02-14 03:02:56 +00:00
During read and write operations, formats are seamlessly converted between the
2007-05-14 09:55:24 +00:00
format the application program has requested or supplied and the file's data
format. The application programmer can remain blissfully unaware of issues
such as file endian-ness and data format. See <A HREF="#note1">Note 1</A> and
2004-09-22 08:03:38 +00:00
<A HREF="#note2">Note 2</A>.
2007-05-14 09:55:24 +00:00
</P>
<!-- pepper -->
<P>
2004-06-21 22:49:08 +00:00
Every effort is made to keep these documents up-to-date, error free and
unambiguous.
However, since maintaining the documentation is the least fun part of working
2007-05-14 09:55:24 +00:00
on libsndfile, these docs can and do fall behind the behaviour of library.
If any errors omissions or ambiguities are found, please notify
<A HREF="m&#97;ilt&#111;:&#101;rikd&#64;z&#105;p.&#99;om.au">
Erik de Castro Lopo</a>.
</P>
<!-- pepper -->
<P>
To supplement this reference documentation, there are simple example programs
included in the source code tarball.
The test suite which is also part of the source code tarball is also a good
place to look for the correct usage of the library functions.
</P>
<!-- pepper -->
2007-05-14 09:55:24 +00:00
<P>
<B> Finally, if you think there is some feature missing from libsndfile, check that
2004-06-21 22:49:08 +00:00
it isn't already implemented (and documented)
2007-05-14 09:55:24 +00:00
<A HREF="command.html">here</A>.
</B>
</P>
<H2><B>SYNOPSIS</B></H2>
<P>
2005-11-13 11:07:52 +00:00
The functions of libsndfile are defined as follows:
2007-05-14 09:55:24 +00:00
</P>
<!-- pepper -->
<PRE>
#include &lt;stdio.h&gt;
#include &lt;sndfile.h&gt;
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
SNDFILE* <A HREF="#open">sf_open</A> (const char *path, int mode, SF_INFO *sfinfo) ;
SNDFILE* <A HREF="#open_fd">sf_open_fd</A> (int fd, int mode, SF_INFO *sfinfo, int close_desc) ;
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
int <A HREF="#check">sf_format_check</A> (const SF_INFO *info) ;
sf_count_t <A HREF="#seek">sf_seek</A> (SNDFILE *sndfile, sf_count_t frames, int whence) ;
2008-05-10 09:19:06 +00:00
int <A HREF="command.html">sf_command</A> (SNDFILE *sndfile, int cmd, void *data, int datasize) ;
2007-05-14 09:55:24 +00:00
int <A HREF="#error">sf_error</A> (SNDFILE *sndfile) ;
const char* <A HREF="#error">sf_strerror</A> (SNDFILE *sndfile) ;
const char* <A HREF="#error">sf_error_number</A> (int errnum) ;
int <A HREF="#error">sf_perror</A> (SNDFILE *sndfile) ;
int <A HREF="#error">sf_error_str</A> (SNDFILE *sndfile, char* str, size_t len) ;
int <A HREF="#close">sf_close</A> (SNDFILE *sndfile) ;
void <A HREF="#write_sync">sf_write_sync</A> (SNDFILE *sndfile) ;
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
sf_count_t <A HREF="#read">sf_read_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
sf_count_t <A HREF="#read">sf_read_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
sf_count_t <A HREF="#read">sf_read_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
sf_count_t <A HREF="#read">sf_read_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
sf_count_t <A HREF="#readf">sf_readf_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
sf_count_t <A HREF="#readf">sf_readf_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
sf_count_t <A HREF="#readf">sf_readf_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
sf_count_t <A HREF="#readf">sf_readf_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
sf_count_t <A HREF="#write">sf_write_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
sf_count_t <A HREF="#write">sf_write_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
sf_count_t <A HREF="#write">sf_write_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
sf_count_t <A HREF="#write">sf_write_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
sf_count_t <A HREF="#writef">sf_writef_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
sf_count_t <A HREF="#writef">sf_writef_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
sf_count_t <A HREF="#writef">sf_writef_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
sf_count_t <A HREF="#writef">sf_writef_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
sf_count_t <A HREF="#raw">sf_read_raw</A> (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
sf_count_t <A HREF="#raw">sf_write_raw</A> (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
const char* <A HREF="#string">sf_get_string</A> (SNDFILE *sndfile, int str_type) ;
int <A HREF="#string">sf_set_string</A> (SNDFILE *sndfile, int str_type, const char* str) ;
</PRE>
<!-- pepper -->
<P>
SNDFILE* is an anonymous pointer to data which is private to the library.
</P>
<A NAME="open"></A>
<H2><B>File Open Function</B></H2>
<PRE>
SNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo) ;
</PRE>
<P>
The SF_INFO structure is for passing data between the calling function and the library
2004-02-14 03:02:56 +00:00
when opening a file for reading or writing. It is defined in sndfile.h as follows:
2007-05-14 09:55:24 +00:00
</P>
<!-- pepper -->
<PRE>
typedef struct
{ sf_count_t frames ; /* Used to be called samples. */
int samplerate ;
int channels ;
int format ;
int sections ;
int seekable ;
} SF_INFO ;
</PRE>
<P>
The mode parameter for this function can be any one of the following three values:
</P>
<!-- pepper -->
<PRE>
SFM_READ - read only mode
SFM_WRITE - write only mode
SFM_RDWR - read/write mode
</PRE>
<P>
When opening a file for read, the <b>format</B> field should be set to zero before
calling sf_open().
2004-06-21 22:49:08 +00:00
The only exception to this is the case of RAW files where the caller has to set
2005-11-13 11:07:52 +00:00
the samplerate, channels and format fields to valid values.
2007-05-14 09:55:24 +00:00
All other fields of the structure are filled in by the library.
</P>
<!-- pepper -->
<P>
When opening a file for write, the caller must fill in structure members samplerate,
2004-06-21 22:49:08 +00:00
channels, and format.
2007-05-14 09:55:24 +00:00
</P>
<!-- pepper -->
<P>
2004-06-21 22:49:08 +00:00
The format field in the above SF_INFO structure is made up of the bit-wise OR of a
major format type (values between 0x10000 and 0x08000000), a minor format type
(with values less than 0x10000) and an optional endian-ness value.
The currently understood formats are listed in sndfile.h as follows and also include
2007-05-14 09:55:24 +00:00
bitmasks for separating major and minor file types.
2004-06-21 22:49:08 +00:00
Not all combinations of endian-ness and major and minor file types are valid.
2007-05-14 09:55:24 +00:00
</P>
<!-- pepper -->
<PRE>
enum
{ /* Major formats. */
SF_FORMAT_WAV = 0x010000, /* Microsoft WAV format (little endian). */
SF_FORMAT_AIFF = 0x020000, /* Apple/SGI AIFF format (big endian). */
SF_FORMAT_AU = 0x030000, /* Sun/NeXT AU format (big endian). */
SF_FORMAT_RAW = 0x040000, /* RAW PCM data. */
SF_FORMAT_PAF = 0x050000, /* Ensoniq PARIS file format. */
SF_FORMAT_SVX = 0x060000, /* Amiga IFF / SVX8 / SV16 format. */
SF_FORMAT_NIST = 0x070000, /* Sphere NIST format. */
SF_FORMAT_VOC = 0x080000, /* VOC files. */
SF_FORMAT_IRCAM = 0x0A0000, /* Berkeley/IRCAM/CARL */
SF_FORMAT_W64 = 0x0B0000, /* Sonic Foundry's 64 bit RIFF/WAV */
SF_FORMAT_MAT4 = 0x0C0000, /* Matlab (tm) V4.2 / GNU Octave 2.0 */
SF_FORMAT_MAT5 = 0x0D0000, /* Matlab (tm) V5.0 / GNU Octave 2.1 */
2005-07-13 02:48:52 +00:00
SF_FORMAT_PVF = 0x0E0000, /* Portable Voice Format */
SF_FORMAT_XI = 0x0F0000, /* Fasttracker 2 Extended Instrument */
SF_FORMAT_HTK = 0x100000, /* HMM Tool Kit format */
SF_FORMAT_SDS = 0x110000, /* Midi Sample Dump Standard */
SF_FORMAT_AVR = 0x120000, /* Audio Visual Research */
SF_FORMAT_WAVEX = 0x130000, /* MS WAVE with WAVEFORMATEX */
SF_FORMAT_SD2 = 0x160000, /* Sound Designer 2 */
SF_FORMAT_FLAC = 0x170000, /* FLAC lossless file format */
SF_FORMAT_CAF = 0x180000, /* Core Audio File format */
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
/* Subtypes from here on. */
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
SF_FORMAT_PCM_S8 = 0x0001, /* Signed 8 bit data */
SF_FORMAT_PCM_16 = 0x0002, /* Signed 16 bit data */
SF_FORMAT_PCM_24 = 0x0003, /* Signed 24 bit data */
SF_FORMAT_PCM_32 = 0x0004, /* Signed 32 bit data */
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
SF_FORMAT_PCM_U8 = 0x0005, /* Unsigned 8 bit data (WAV and RAW only) */
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
SF_FORMAT_FLOAT = 0x0006, /* 32 bit float data */
SF_FORMAT_DOUBLE = 0x0007, /* 64 bit float data */
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
SF_FORMAT_ULAW = 0x0010, /* U-Law encoded. */
SF_FORMAT_ALAW = 0x0011, /* A-Law encoded. */
SF_FORMAT_IMA_ADPCM = 0x0012, /* IMA ADPCM. */
SF_FORMAT_MS_ADPCM = 0x0013, /* Microsoft ADPCM. */
SF_FORMAT_GSM610 = 0x0020, /* GSM 6.10 encoding. */
SF_FORMAT_VOX_ADPCM = 0x0021, /* Oki Dialogic ADPCM encoding. */
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
SF_FORMAT_G721_32 = 0x0030, /* 32kbs G721 ADPCM encoding. */
SF_FORMAT_G723_24 = 0x0031, /* 24kbs G723 ADPCM encoding. */
SF_FORMAT_G723_40 = 0x0032, /* 40kbs G723 ADPCM encoding. */
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
SF_FORMAT_DWVW_12 = 0x0040, /* 12 bit Delta Width Variable Word encoding. */
SF_FORMAT_DWVW_16 = 0x0041, /* 16 bit Delta Width Variable Word encoding. */
SF_FORMAT_DWVW_24 = 0x0042, /* 24 bit Delta Width Variable Word encoding. */
SF_FORMAT_DWVW_N = 0x0043, /* N bit Delta Width Variable Word encoding. */
2004-06-21 22:49:08 +00:00
2005-07-13 02:48:52 +00:00
SF_FORMAT_DPCM_8 = 0x0050, /* 8 bit differential PCM (XI only) */
SF_FORMAT_DPCM_16 = 0x0051, /* 16 bit differential PCM (XI only) */
2007-05-14 09:55:24 +00:00
/* Endian-ness options. */
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
SF_ENDIAN_FILE = 0x00000000, /* Default file endian-ness. */
SF_ENDIAN_LITTLE = 0x10000000, /* Force little endian-ness. */
SF_ENDIAN_BIG = 0x20000000, /* Force big endian-ness. */
SF_ENDIAN_CPU = 0x30000000, /* Force CPU endian-ness. */
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
SF_FORMAT_SUBMASK = 0x0000FFFF,
SF_FORMAT_TYPEMASK = 0x0FFF0000,
SF_FORMAT_ENDMASK = 0x30000000
} ;
</PRE>
<!-- pepper -->
<P>
Every call to sf_open() should be matched with a call to sf_close() to free up
2004-02-14 03:02:56 +00:00
memory allocated during the call to sf_open().
2007-05-14 09:55:24 +00:00
</P>
<!-- pepper -->
<P>
2004-06-21 22:49:08 +00:00
On success, the sf_open function returns a non NULL pointer which should be
passed as the first parameter to all subsequent libsndfile calls dealing with
2007-05-14 09:55:24 +00:00
that audio file.
2004-02-14 03:02:56 +00:00
On fail, the sf_open function returns a NULL pointer.
2007-05-14 09:55:24 +00:00
</P>
<A NAME="open_fd"></A>
<H3><B>File Descriptor Open</B></H3>
<PRE>
SNDFILE* sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc) ;
</PRE>
<P>
2004-06-21 22:49:08 +00:00
The second open function takes a file descriptor of a file that has already been
opened.
Care should be taken to ensure that the mode of the file represented by the
2007-05-14 09:55:24 +00:00
descriptor matches the mode argument.
This function is useful in the following circumstances:
</P>
<UL>
<LI>Opening temporary files securely (ie use the tmpfile() to return a
FILE* pointer and then using fileno() to retrieve the file descriptor
which is then passed to libsndfile).
<LI>Opening files with file names using OS specific character encodings
and then passing the file descriptor to sf_open_fd().
2004-06-21 22:49:08 +00:00
<LI>Opening sound files embedded within larger files.
2007-05-14 09:55:24 +00:00
<A HREF="embedded_files.html">More info</A>.
</UL>
<P>
Every call to sf_open_fd() should be matched with a call to sf_close() to free up
2004-02-14 03:02:56 +00:00
memory allocated during the call to sf_open().
</P>
<P>
When sf_close() is called, the file descriptor is only closed if the <B>close_desc</B>
parameter was TRUE when the sf_open_fd() function was called.
2007-05-14 09:55:24 +00:00
</P>
<P>
2004-06-21 22:49:08 +00:00
On success, the sf_open_fd function returns a non NULL pointer which should be
passed as the first parameter to all subsequent libsndfile calls dealing with
2007-05-14 09:55:24 +00:00
that audio file.
2004-02-14 03:02:56 +00:00
On fail, the sf_open_fd function returns a NULL pointer.
2007-05-14 09:55:24 +00:00
</P>
<A NAME="check"></A>
<BR><H2><B>Format Check Function</B></H2>
<PRE>
int sf_format_check (const SF_INFO *info) ;
</PRE>
<!-- pepper -->
<P>
This function allows the caller to check if a set of parameters in the SF_INFO struct
is valid before calling sf_open (SFM_WRITE).
</P>
<P>
sf_format_check returns TRUE if the parameters are valid and FALSE otherwise.
</P>
<A NAME="seek"></A>
<BR><H2><B>File Seek Functions</B></H2>
<PRE>
sf_count_t sf_seek (SNDFILE *sndfile, sf_count_t frames, int whence) ;
</PRE>
<P>
The file seek functions work much like lseek in unistd.h with the exception that
2004-06-21 22:49:08 +00:00
the non-audio data is ignored and the seek only moves within the audio data section of
the file.
2007-05-14 09:55:24 +00:00
In addition, seeks are defined in number of (multichannel) frames.
2004-06-21 22:49:08 +00:00
Therefore, a seek in a stereo file from the current position forward with an offset
2007-05-14 09:55:24 +00:00
of 1 would skip forward by one sample of both channels.
</P>
<P>
like lseek(), the whence parameter can be any one of the following three values:
</P>
<PRE>
SEEK_SET - The offset is set to the start of the audio data plus offset (multichannel) frames.
SEEK_CUR - The offset is set to its current location plus offset (multichannel) frames.
SEEK_END - The offset is set to the end of the data plus offset (multichannel) frames.
</PRE>
<!-- pepper -->
<P>
2004-06-21 22:49:08 +00:00
Internally, libsndfile keeps track of the read and write locations using separate
read and write pointers.
If a file has been opened with a mode of SFM_RDWR, bitwise OR-ing the standard whence
values above with either SFM_READ or SFM_WRITE allows the read and write pointers to
2007-05-14 09:55:24 +00:00
be modified separately.
If the SEEK_* values are used on their own, the read and write pointers are
both modified.
</P>
<P>
2004-06-21 22:49:08 +00:00
Note that the frames offset can be negative and in fact should be when SEEK_END is used for the
whence parameter.
2007-05-14 09:55:24 +00:00
</P>
<P>
sf_seek will return the offset in (multichannel) frames from the start of the audio data
2004-02-14 03:02:56 +00:00
or -1 if an error occured (ie an attempt is made to seek beyond the start or end of the file).
2007-05-14 09:55:24 +00:00
</P>
<A NAME="error"></A>
<H2><BR><B>Error Reporting Functions</B></H2>
<PRE>
int sf_error (SNDFILE *sndfile) ;
</PRE>
<P>
This function returns the current error number for the given SNDFILE.
The error number may be one of the following:
</P>
<PRE>
enum
{ SF_ERR_NO_ERROR = 0,
2005-07-13 02:48:52 +00:00
SF_ERR_UNRECOGNISED_FORMAT = 1,
SF_ERR_SYSTEM = 2,
SF_ERR_MALFORMED_FILE = 3,
SF_ERR_UNSUPPORTED_ENCODING = 4
2007-05-14 09:55:24 +00:00
} ;
</PRE>
<!-- pepper -->
<P>
or any one of many other internal error values.
Applications should only test the return value against error values defined in
2004-06-21 22:49:08 +00:00
&lt;sndfile.h&gt; as the internal error values are subject to change at any
2007-05-14 09:55:24 +00:00
time.
For errors not in the above list, the function sf_error_number() can be used to
convert it to an error string.
</P>
<PRE>
const char* sf_strerror (SNDFILE *sndfile) ;
const char* sf_error_number (int errnum) ;
</PRE>
<P>
2004-06-21 22:49:08 +00:00
The error functions sf_strerror() and sf_error_number() convert the library's internal
error enumerations into text strings.
2007-05-14 09:55:24 +00:00
</P>
<PRE>
int sf_perror (SNDFILE *sndfile) ;
int sf_error_str (SNDFILE *sndfile, char* str, size_t len) ;
</PRE>
<P>
2004-02-14 03:02:56 +00:00
The functions sf_perror() and sf_error_str() are deprecated and will be dropped
2007-05-14 09:55:24 +00:00
from the library at some later date.
</P>
<A NAME="close"></A>
<H2><BR><B>File Close Function</B></H2>
<PRE>
int sf_close (SNDFILE *sndfile) ;
</PRE>
<!-- pepper -->
<P>
2004-02-14 03:02:56 +00:00
The close function closes the file, deallocates its internal buffers and returns
0 on success or an error value otherwise.
2007-05-14 09:55:24 +00:00
</P>
<BR>
<A NAME="write_sync"></A>
<H2><BR><B>Write Sync Function</B></H2>
<PRE>
void sf_write_sync (SNDFILE *sndfile) ;
</PRE>
<!-- pepper -->
<P>
If the file is opened SFM_WRITE or SFM_RDWR, call the operating system's function
to force the writing of all file cache buffers to disk. If the file is opened
SFM_READ no action is taken.
</P>
<BR>
2007-05-14 09:55:24 +00:00
<A NAME="read"></A>
<H2><BR><B>File Read Functions (Items)</B></H2>
<PRE>
sf_count_t sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
sf_count_t sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
sf_count_t sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
sf_count_t sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
</PRE>
<P>
The file read items functions fill the array pointed to by ptr with the requested
2004-06-21 22:49:08 +00:00
number of items. The items parameter must be an integer product of the number
of channels or an error will occur.
2007-05-14 09:55:24 +00:00
</P>
<!-- pepper -->
<P>
2004-02-14 03:02:56 +00:00
It is important to note that the data type used by the calling program and the data
2007-05-14 09:55:24 +00:00
format of the file do not need to be the same. For instance, it is possible to open
a 16 bit PCM encoded WAV file and read the data using sf_read_float(). The library
2004-02-14 03:02:56 +00:00
seamlessly converts between the two formats on-the-fly. See
2007-05-14 09:55:24 +00:00
<A HREF="#note1">Note 1</A>.
</P>
<!-- pepper -->
<P>
2004-06-21 22:49:08 +00:00
The sf_read_XXXX functions return the number of items read.
Unless the end of the file was reached during the read, the return value should
equal the number of items requested.
Attempts to read beyond the end of the file will not result in an error but will
cause the sf_read_XXXX functions to return less than the number of items requested
or 0 if already at the end of the file.
2007-05-14 09:55:24 +00:00
</P>
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
<A NAME="readf"></A>
<H2><BR><B>File Read Functions (Frames)</B></H2>
<PRE>
sf_count_t sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
sf_count_t sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
sf_count_t sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
sf_count_t sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
</PRE>
<!-- pepper -->
<P>
The file read frames functions fill the array pointed to by ptr with the requested
number of frames of data. The array must be large enough to hold the product of
frames and the number of channels.
</P>
<P><B>
2004-02-14 03:02:56 +00:00
Care must be taken to ensure that there is enough space in the array pointed to by
2007-05-14 09:55:24 +00:00
ptr, to take (frames * channels) number of items (shorts, ints, floats or doubles).
</B></P>
<P>
2004-06-21 22:49:08 +00:00
The sf_readf_XXXX functions return the number of frames read.
Unless the end of the file was reached during the read, the return value should equal
the number of frames requested.
Attempts to read beyond the end of the file will not result in an error but will cause
the sf_readf_XXXX functions to return less than the number of frames requested or 0 if
already at the end of the file.
2007-05-14 09:55:24 +00:00
</P>
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
<A NAME="write"></A>
<H2><BR><B>File Write Functions (Items)</B></H2>
<PRE>
sf_count_t sf_write_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
sf_count_t sf_write_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
sf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
sf_count_t sf_write_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
</PRE>
<P>
The file write items functions write the data in the array pointed to by ptr to the file.
2004-06-21 22:49:08 +00:00
The items parameter must be an integer product of the number of channels or an error
2007-05-14 09:55:24 +00:00
will occur.
</P>
<!-- pepper -->
<P>
2004-02-14 03:02:56 +00:00
It is important to note that the data type used by the calling program and the data
2007-05-14 09:55:24 +00:00
format of the file do not need to be the same. For instance, it is possible to open
a 16 bit PCM encoded WAV file and write the data using sf_write_float(). The library
2004-02-14 03:02:56 +00:00
seamlessly converts between the two formats on-the-fly. See
2007-05-14 09:55:24 +00:00
<A HREF="#note1">Note 1</A>.
</P>
<P>
The sf_write_XXXX functions return the number of items written (which should be the
same as the items parameter).
</P>
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
<A NAME="writef"></A>
<H2><BR><B>File Write Functions (Frames)</B></H2>
<PRE>
sf_count_t sf_writef_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
sf_count_t sf_writef_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
sf_count_t sf_writef_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
sf_count_t sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
</PRE>
<P>
The file write frames functions write the data in the array pointed to by ptr to the file.
The array must be large enough to hold the product of frames and the number of channels.
</P>
<P>
The sf_writef_XXXX functions return the number of frames written (which should be the
2004-06-21 22:49:08 +00:00
same as the frames parameter).
2007-05-14 09:55:24 +00:00
</P>
2004-06-21 22:49:08 +00:00
2007-05-14 09:55:24 +00:00
<A NAME="raw"></A>
<H2><BR><B>Raw File Read and Write Functions</B></H2>
<!-- pepper -->
<PRE>
sf_count_t sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
sf_count_t sf_write_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
</PRE>
<P>
2004-02-14 03:02:56 +00:00
The raw read and write functions read raw audio data from the audio file (not to be
2004-06-21 22:49:08 +00:00
confused with reading RAW header-less PCM files). The number of bytes read or written
must always be an integer multiple of the number of channels multiplied by the number
2007-05-14 09:55:24 +00:00
of bytes required to represent one sample from one channel.
</P>
<!-- pepper -->
<P>
2004-06-21 22:49:08 +00:00
The raw read and write functions return the number of bytes read or written (which
should be the same as the bytes parameter).
2007-05-14 09:55:24 +00:00
</P>
<P>
<B>
2004-06-21 22:49:08 +00:00
Note : The result of using of both regular reads/writes and raw reads/writes on
2007-05-14 09:55:24 +00:00
compressed file formats other than SF_FORMAT_ALAW and SF_FORMAT_ULAW is undefined.
</B>
</P>
2008-05-10 09:19:06 +00:00
<p>
See also : <a href="command.html#SFC_RAW_NEEDS_ENDSWAP">SFC_RAW_NEEDS_ENDSWAP</a>
</p>
2007-05-14 09:55:24 +00:00
<A NAME="string"></A>
<H2><BR><B>Functions for Reading and Writing String Data</B></H2>
<PRE>
const char* sf_get_string (SNDFILE *sndfile, int str_type) ;
int sf_set_string (SNDFILE *sndfile, int str_type, const char* str) ;
</PRE>
<P>
2004-06-21 22:49:08 +00:00
These functions allow strings to be set on files opened for write and to be
2004-02-14 03:02:56 +00:00
retrieved from files opened for read where supported by the given file type.
2007-05-14 09:55:24 +00:00
The <B>str_type</B> parameter can be any one of the following string types:
</P>
<PRE>
enum
{ SF_STR_TITLE,
SF_STR_COPYRIGHT,
SF_STR_SOFTWARE,
SF_STR_ARTIST,
SF_STR_COMMENT,
SF_STR_DATE
} ;
</PRE>
<P>
2004-06-21 22:49:08 +00:00
The sf_get_string() function returns the specificed string if it exists and a
2007-05-14 09:55:24 +00:00
NULL pointer otherwise.
2004-06-21 22:49:08 +00:00
In addition to the string ids above, SF_STR_FIRST (== SF_STR_TITLE) and
SF_STR_LAST (always the same as the highest numbers string id) are also
available to allow iteration over all the available string ids.
2007-05-14 09:55:24 +00:00
</P>
<P>
The sf_set_string() function sets the string data.
2004-06-21 22:49:08 +00:00
It returns zero on success and non-zero on error.
2007-05-14 09:55:24 +00:00
The error code can be converted to a string using sf_error_number().
</P>
<P>
</P>
<HR>
<A NAME="note1"></A>
<H2><BR><B>Note 1</B></H2>
<!-- pepper -->
<P>
2004-06-21 22:49:08 +00:00
When converting between integer PCM formats of differing size (ie using sf_read_int()
to read a 16 bit PCM encoded WAV file) libsndfile obeys one simple rule:
2007-05-14 09:55:24 +00:00
</P>
<P CLASS=indent_block>
2004-06-21 22:49:08 +00:00
Whenever integer data is moved from one sized container to another sized container,
the most significant bit in the source container will become the most significant bit
in the destination container.
2007-05-14 09:55:24 +00:00
</P>
<P>
When converting between integer data and floating point data, different rules apply.
2004-06-21 22:49:08 +00:00
The default behaviour when reading floating point data (sf_read_float() or
sf_read_double ()) from a file with integer data is normalisation. Regardless of
whether data in the file is 8, 16, 24 or 32 bit wide, the data will be read as
2007-05-14 09:55:24 +00:00
floating point data in the range [-1.0, 1.0]. Similarly, data in the range [-1.0, 1.0]
will be written to an integer PCM file so that a data value of 1.0 will be the largest
allowable integer for the given bit width. This normalisation can be turned on or off
using the <A HREF="command.html">sf_command</A> interface.
</P>
<A NAME="note2"></A>
<H2><BR><B>Note 2</B></H2>
<P>
2004-06-21 22:49:08 +00:00
Reading a file containg floating point data (allowable with WAV, AIFF, AU and other
file formats) using integer read methods (sf_read_short() or sf_read_int()) can
produce unexpected results.
For instance the data in the file may have a maximum absolute value &lt; 1.0 which
would mean that all sample values read from the file will be zero.
In order to read these files correctly using integer read methods, it is recommended
that you use the
<A HREF="command.html">sf_command</A>
interface a command of
2006-02-20 02:13:10 +00:00
<A HREF="command.html#SFC_SET_SCALE_FLOAT_INT_READ">SFC_SET_SCALE_FLOAT_INT_READ</A>
and a parameter of SF_TRUE to force correct scaling.
2007-05-14 09:55:24 +00:00
</P>
<!-- pepper -->
<HR>
<!-- pepper -->
<P>
2004-06-21 22:49:08 +00:00
The libsndfile home page is
<A HREF="http://www.mega-nerd.com/libsndfile/">here</A>.
2007-05-14 09:55:24 +00:00
</P>
<P>
2009-05-14 10:20:01 +00:00
Version : 1.0.20
2007-05-14 09:55:24 +00:00
</P>
<!-- pepper -->
<!-- pepper -->
<!-- pepper -->
<!-- pepper -->
</BODY>
</HTML>