api.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>
  5. The libsndfile API
  6. </TITLE>
  7. <META NAME="Author" CONTENT="Erik de Castro Lopo (erikd AT mega-nerd DOT com)">
  8. <META NAME="Description" CONTENT="The libsndfile API.">
  9. <META NAME="Keywords" CONTENT="WAV AIFF AU libsndfile sound audio dsp Linux">
  10. <LINK REL="stylesheet" HREF="libsndfile.css" TYPE="text/css" MEDIA="all">
  11. <LINK REL="stylesheet" HREF="print.css" TYPE="text/css" MEDIA="print">
  12. </HEAD>
  13. <BODY>
  14. <BR>
  15. <H1><B>libsndfile</B></H1>
  16. <P>
  17. Libsndfile is a library designed to allow the reading and writing of many
  18. different sampled sound file formats (such as MS Windows WAV and the Apple/SGI
  19. AIFF format) through one standard library interface.
  20. </P>
  21. <!-- pepper -->
  22. <P>
  23. During read and write operations, formats are seamlessly converted between the
  24. format the application program has requested or supplied and the file's data
  25. format. The application programmer can remain blissfully unaware of issues
  26. such as file endian-ness and data format. See <A HREF="#note1">Note 1</A> and
  27. <A HREF="#note2">Note 2</A>.
  28. </P>
  29. <!-- pepper -->
  30. <P>
  31. Every effort is made to keep these documents up-to-date, error free and
  32. unambiguous.
  33. However, since maintaining the documentation is the least fun part of working
  34. on libsndfile, these docs can and do fall behind the behaviour of library.
  35. If any errors, omissions or ambiguities are found, please notify me (erikd)
  36. at mega-nerd dot com.
  37. </P>
  38. <!-- pepper -->
  39. <P>
  40. To supplement this reference documentation, there are simple example programs
  41. included in the source code tarball.
  42. The test suite which is also part of the source code tarball is also a good
  43. place to look for the correct usage of the library functions.
  44. </P>
  45. <!-- pepper -->
  46. <P>
  47. <B> Finally, if you think there is some feature missing from libsndfile, check that
  48. it isn't already implemented (and documented)
  49. <A HREF="command.html">here</A>.
  50. </B>
  51. </P>
  52. <H2><B>Synopsis</B></H2>
  53. <P>
  54. The functions of libsndfile are defined as follows:
  55. </P>
  56. <!-- pepper -->
  57. <PRE>
  58. #include &lt;stdio.h&gt;
  59. #include &lt;sndfile.h&gt;
  60. SNDFILE* <A HREF="#open">sf_open</A> (const char *path, int mode, SF_INFO *sfinfo) ;
  61. SNDFILE* <A HREF="#open_fd">sf_open_fd</A> (int fd, int mode, SF_INFO *sfinfo, int close_desc) ;
  62. SNDFILE* <A HREF="#open_virtual">sf_open_virtual</A> (SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo, void *user_data) ;
  63. int <A HREF="#check">sf_format_check</A> (const SF_INFO *info) ;
  64. sf_count_t <A HREF="#seek">sf_seek</A> (SNDFILE *sndfile, sf_count_t frames, int whence) ;
  65. int <A HREF="command.html">sf_command</A> (SNDFILE *sndfile, int cmd, void *data, int datasize) ;
  66. int <A HREF="#error">sf_error</A> (SNDFILE *sndfile) ;
  67. const char* <A HREF="#error">sf_strerror</A> (SNDFILE *sndfile) ;
  68. const char* <A HREF="#error">sf_error_number</A> (int errnum) ;
  69. int <A HREF="#error">sf_perror</A> (SNDFILE *sndfile) ;
  70. int <A HREF="#error">sf_error_str</A> (SNDFILE *sndfile, char* str, size_t len) ;
  71. int <A HREF="#close">sf_close</A> (SNDFILE *sndfile) ;
  72. void <A HREF="#write_sync">sf_write_sync</A> (SNDFILE *sndfile) ;
  73. sf_count_t <A HREF="#read">sf_read_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
  74. sf_count_t <A HREF="#read">sf_read_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
  75. sf_count_t <A HREF="#read">sf_read_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
  76. sf_count_t <A HREF="#read">sf_read_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
  77. sf_count_t <A HREF="#readf">sf_readf_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
  78. sf_count_t <A HREF="#readf">sf_readf_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
  79. sf_count_t <A HREF="#readf">sf_readf_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
  80. sf_count_t <A HREF="#readf">sf_readf_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
  81. sf_count_t <A HREF="#write">sf_write_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
  82. sf_count_t <A HREF="#write">sf_write_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
  83. sf_count_t <A HREF="#write">sf_write_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
  84. sf_count_t <A HREF="#write">sf_write_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
  85. sf_count_t <A HREF="#writef">sf_writef_short</A> (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
  86. sf_count_t <A HREF="#writef">sf_writef_int</A> (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
  87. sf_count_t <A HREF="#writef">sf_writef_float</A> (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
  88. sf_count_t <A HREF="#writef">sf_writef_double</A> (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
  89. sf_count_t <A HREF="#raw">sf_read_raw</A> (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
  90. sf_count_t <A HREF="#raw">sf_write_raw</A> (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
  91. const char* <A HREF="#string">sf_get_string</A> (SNDFILE *sndfile, int str_type) ;
  92. int <A HREF="#string">sf_set_string</A> (SNDFILE *sndfile, int str_type, const char* str) ;
  93. </PRE>
  94. <!-- pepper -->
  95. <P>
  96. SNDFILE* is an anonymous pointer to data which is private to the library.
  97. </P>
  98. <A NAME="open"></A>
  99. <H2><B>File Open Function</B></H2>
  100. <PRE>
  101. SNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo) ;
  102. </PRE>
  103. <P>
  104. The SF_INFO structure is for passing data between the calling function and the library
  105. when opening a file for reading or writing. It is defined in sndfile.h as follows:
  106. </P>
  107. <!-- pepper -->
  108. <PRE>
  109. typedef struct
  110. { sf_count_t frames ; /* Used to be called samples. */
  111. int samplerate ;
  112. int channels ;
  113. int format ;
  114. int sections ;
  115. int seekable ;
  116. } SF_INFO ;
  117. </PRE>
  118. <P>
  119. The mode parameter for this function can be any one of the following three values:
  120. </P>
  121. <!-- pepper -->
  122. <PRE>
  123. SFM_READ - read only mode
  124. SFM_WRITE - write only mode
  125. SFM_RDWR - read/write mode
  126. </PRE>
  127. <P>
  128. When opening a file for read, the <b>format</B> field should be set to zero before
  129. calling sf_open().
  130. The only exception to this is the case of RAW files where the caller has to set
  131. the samplerate, channels and format fields to valid values.
  132. All other fields of the structure are filled in by the library.
  133. </P>
  134. <!-- pepper -->
  135. <P>
  136. When opening a file for write, the caller must fill in structure members samplerate,
  137. channels, and format.
  138. </P>
  139. <!-- pepper -->
  140. <P>
  141. The format field in the above SF_INFO structure is made up of the bit-wise OR of a
  142. major format type (values between 0x10000 and 0x08000000), a minor format type
  143. (with values less than 0x10000) and an optional endian-ness value.
  144. The currently understood formats are listed in sndfile.h as follows and also include
  145. bitmasks for separating major and minor file types.
  146. Not all combinations of endian-ness and major and minor file types are valid.
  147. </P>
  148. <!-- pepper -->
  149. <PRE>
  150. enum
  151. { /* Major formats. */
  152. SF_FORMAT_WAV = 0x010000, /* Microsoft WAV format (little endian). */
  153. SF_FORMAT_AIFF = 0x020000, /* Apple/SGI AIFF format (big endian). */
  154. SF_FORMAT_AU = 0x030000, /* Sun/NeXT AU format (big endian). */
  155. SF_FORMAT_RAW = 0x040000, /* RAW PCM data. */
  156. SF_FORMAT_PAF = 0x050000, /* Ensoniq PARIS file format. */
  157. SF_FORMAT_SVX = 0x060000, /* Amiga IFF / SVX8 / SV16 format. */
  158. SF_FORMAT_NIST = 0x070000, /* Sphere NIST format. */
  159. SF_FORMAT_VOC = 0x080000, /* VOC files. */
  160. SF_FORMAT_IRCAM = 0x0A0000, /* Berkeley/IRCAM/CARL */
  161. SF_FORMAT_W64 = 0x0B0000, /* Sonic Foundry's 64 bit RIFF/WAV */
  162. SF_FORMAT_MAT4 = 0x0C0000, /* Matlab (tm) V4.2 / GNU Octave 2.0 */
  163. SF_FORMAT_MAT5 = 0x0D0000, /* Matlab (tm) V5.0 / GNU Octave 2.1 */
  164. SF_FORMAT_PVF = 0x0E0000, /* Portable Voice Format */
  165. SF_FORMAT_XI = 0x0F0000, /* Fasttracker 2 Extended Instrument */
  166. SF_FORMAT_HTK = 0x100000, /* HMM Tool Kit format */
  167. SF_FORMAT_SDS = 0x110000, /* Midi Sample Dump Standard */
  168. SF_FORMAT_AVR = 0x120000, /* Audio Visual Research */
  169. SF_FORMAT_WAVEX = 0x130000, /* MS WAVE with WAVEFORMATEX */
  170. SF_FORMAT_SD2 = 0x160000, /* Sound Designer 2 */
  171. SF_FORMAT_FLAC = 0x170000, /* FLAC lossless file format */
  172. SF_FORMAT_CAF = 0x180000, /* Core Audio File format */
  173. SF_FORMAT_WVE = 0x190000, /* Psion WVE format */
  174. SF_FORMAT_OGG = 0x200000, /* Xiph OGG container */
  175. SF_FORMAT_MPC2K = 0x210000, /* Akai MPC 2000 sampler */
  176. SF_FORMAT_RF64 = 0x220000, /* RF64 WAV file */
  177. /* Subtypes from here on. */
  178. SF_FORMAT_PCM_S8 = 0x0001, /* Signed 8 bit data */
  179. SF_FORMAT_PCM_16 = 0x0002, /* Signed 16 bit data */
  180. SF_FORMAT_PCM_24 = 0x0003, /* Signed 24 bit data */
  181. SF_FORMAT_PCM_32 = 0x0004, /* Signed 32 bit data */
  182. SF_FORMAT_PCM_U8 = 0x0005, /* Unsigned 8 bit data (WAV and RAW only) */
  183. SF_FORMAT_FLOAT = 0x0006, /* 32 bit float data */
  184. SF_FORMAT_DOUBLE = 0x0007, /* 64 bit float data */
  185. SF_FORMAT_ULAW = 0x0010, /* U-Law encoded. */
  186. SF_FORMAT_ALAW = 0x0011, /* A-Law encoded. */
  187. SF_FORMAT_IMA_ADPCM = 0x0012, /* IMA ADPCM. */
  188. SF_FORMAT_MS_ADPCM = 0x0013, /* Microsoft ADPCM. */
  189. SF_FORMAT_GSM610 = 0x0020, /* GSM 6.10 encoding. */
  190. SF_FORMAT_VOX_ADPCM = 0x0021, /* Oki Dialogic ADPCM encoding. */
  191. SF_FORMAT_G721_32 = 0x0030, /* 32kbs G721 ADPCM encoding. */
  192. SF_FORMAT_G723_24 = 0x0031, /* 24kbs G723 ADPCM encoding. */
  193. SF_FORMAT_G723_40 = 0x0032, /* 40kbs G723 ADPCM encoding. */
  194. SF_FORMAT_DWVW_12 = 0x0040, /* 12 bit Delta Width Variable Word encoding. */
  195. SF_FORMAT_DWVW_16 = 0x0041, /* 16 bit Delta Width Variable Word encoding. */
  196. SF_FORMAT_DWVW_24 = 0x0042, /* 24 bit Delta Width Variable Word encoding. */
  197. SF_FORMAT_DWVW_N = 0x0043, /* N bit Delta Width Variable Word encoding. */
  198. SF_FORMAT_DPCM_8 = 0x0050, /* 8 bit differential PCM (XI only) */
  199. SF_FORMAT_DPCM_16 = 0x0051, /* 16 bit differential PCM (XI only) */
  200. SF_FORMAT_VORBIS = 0x0060, /* Xiph Vorbis encoding. */
  201. /* Endian-ness options. */
  202. SF_ENDIAN_FILE = 0x00000000, /* Default file endian-ness. */
  203. SF_ENDIAN_LITTLE = 0x10000000, /* Force little endian-ness. */
  204. SF_ENDIAN_BIG = 0x20000000, /* Force big endian-ness. */
  205. SF_ENDIAN_CPU = 0x30000000, /* Force CPU endian-ness. */
  206. SF_FORMAT_SUBMASK = 0x0000FFFF,
  207. SF_FORMAT_TYPEMASK = 0x0FFF0000,
  208. SF_FORMAT_ENDMASK = 0x30000000
  209. } ;
  210. </PRE>
  211. <!-- pepper -->
  212. <P>
  213. Every call to sf_open() should be matched with a call to sf_close() to free up
  214. memory allocated during the call to sf_open().
  215. </P>
  216. <!-- pepper -->
  217. <P>
  218. On success, the sf_open function returns a non-NULL pointer which should be
  219. passed as the first parameter to all subsequent libsndfile calls dealing with
  220. that audio file.
  221. On fail, the sf_open function returns a NULL pointer.
  222. An explanation of the error can obtained by passing NULL to
  223. <A HREF="#error">sf_strerror</A>.
  224. </P>
  225. <A NAME="open_fd"></A>
  226. <H3><B>File Descriptor Open</B></H3>
  227. <PRE>
  228. SNDFILE* sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc) ;
  229. </PRE>
  230. <P>
  231. <b>Note:</b> On Microsoft Windows, this function does not work if the
  232. application and the libsndfile DLL are linked to different versions of the
  233. Microsoft C runtime DLL.
  234. </P>
  235. <P>
  236. The second open function takes a file descriptor of a file that has already been
  237. opened.
  238. Care should be taken to ensure that the mode of the file represented by the
  239. descriptor matches the mode argument.
  240. This function is useful in the following circumstances:
  241. </P>
  242. <UL>
  243. <LI>Opening temporary files securely (ie use the tmpfile() to return a
  244. FILE* pointer and then using fileno() to retrieve the file descriptor
  245. which is then passed to libsndfile).
  246. <LI>Opening files with file names using OS specific character encodings
  247. and then passing the file descriptor to sf_open_fd().
  248. <LI>Opening sound files embedded within larger files.
  249. <A HREF="embedded_files.html">More info</A>.
  250. </UL>
  251. <P>
  252. Every call to sf_open_fd() should be matched with a call to sf_close() to free up
  253. memory allocated during the call to sf_open().
  254. </P>
  255. <P>
  256. When sf_close() is called, the file descriptor is only closed if the <B>close_desc</B>
  257. parameter was TRUE when the sf_open_fd() function was called.
  258. </P>
  259. <P>
  260. On success, the sf_open_fd function returns a non-NULL pointer which should be
  261. passed as the first parameter to all subsequent libsndfile calls dealing with
  262. that audio file.
  263. On fail, the sf_open_fd function returns a NULL pointer.
  264. </P>
  265. <A NAME="open_virtual"></A>
  266. <h3><b>Virtual File Open Function</b></h3>
  267. <pre>
  268. SNDFILE* sf_open_virtual (SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo, void *user_data) ;
  269. </pre>
  270. <p>
  271. Opens a soundfile from a virtual file I/O context which is provided
  272. by the caller. This is usually used to interface libsndfile to a stream or buffer
  273. based system. Apart from the sfvirtual and the user_data parameters this function behaves
  274. like <a href="#open">sf_open</a>.
  275. </p>
  276. <pre>
  277. typedef struct
  278. { sf_vio_get_filelen get_filelen ;
  279. sf_vio_seek seek ;
  280. sf_vio_read read ;
  281. sf_vio_write write ;
  282. sf_vio_tell tell ;
  283. } SF_VIRTUAL_IO ;
  284. </pre>
  285. <p>
  286. Libsndfile calls the callbacks provided by the SF_VIRTUAL_IO structure when opening, reading
  287. and writing to the virtual file context. The user_data pointer is a user defined context which
  288. will be available in the callbacks.
  289. </p>
  290. <pre>
  291. typedef sf_count_t (*sf_vio_get_filelen) (void *user_data) ;
  292. typedef sf_count_t (*sf_vio_seek) (sf_count_t offset, int whence, void *user_data) ;
  293. typedef sf_count_t (*sf_vio_read) (void *ptr, sf_count_t count, void *user_data) ;
  294. typedef sf_count_t (*sf_vio_write) (const void *ptr, sf_count_t count, void *user_data) ;
  295. typedef sf_count_t (*sf_vio_tell) (void *user_data) ;
  296. </pre>
  297. <h4>sf_vio_get_filelen</h4>
  298. <pre>
  299. typedef sf_count_t (*sf_vio_get_filelen) (void *user_data) ;
  300. </pre>
  301. <p>
  302. The virtual file contex must return the length of the virtual file in bytes.<br>
  303. </p>
  304. <h4>sf_vio_seek</h4>
  305. <pre>
  306. typedef sf_count_t (*sf_vio_seek) (sf_count_t offset, int whence, void *user_data) ;
  307. </pre>
  308. <p>
  309. The virtual file context must seek to offset using the seek mode provided by whence which is one of<br>
  310. </p>
  311. <pre>
  312. SEEK_CUR
  313. SEEK_SET
  314. SEEK_END
  315. </pre>
  316. <p>
  317. The return value must contain the new offset in the file.
  318. </p>
  319. <h4>sf_vio_read</h4>
  320. <pre>
  321. typedef sf_count_t (*sf_vio_read) (void *ptr, sf_count_t count, void *user_data) ;
  322. </pre>
  323. <p>
  324. The virtual file context must copy ("read") "count" bytes into the
  325. buffer provided by ptr and return the count of actually copied bytes.
  326. </p>
  327. <h4>sf_vio_write</h4>
  328. <pre>
  329. typedef sf_count_t (*sf_vio_write) (const void *ptr, sf_count_t count, void *user_data) ;
  330. </pre>
  331. <p>
  332. The virtual file context must process "count" bytes stored in the
  333. buffer passed with ptr and return the count of actually processed bytes.<br>
  334. </p>
  335. <h4>sf_vio_tell</h4>
  336. <pre>
  337. typedef sf_count_t (*sf_vio_tell) (void *user_data) ;
  338. </pre>
  339. <p>
  340. Return the current position of the virtual file context.<br>
  341. </p>
  342. <A NAME="check"></A>
  343. <BR><H2><B>Format Check Function</B></H2>
  344. <PRE>
  345. int sf_format_check (const SF_INFO *info) ;
  346. </PRE>
  347. <!-- pepper -->
  348. <P>
  349. This function allows the caller to check if a set of parameters in the SF_INFO struct
  350. is valid before calling sf_open (SFM_WRITE).
  351. </P>
  352. <P>
  353. sf_format_check returns TRUE if the parameters are valid and FALSE otherwise.
  354. </P>
  355. <A NAME="seek"></A>
  356. <BR><H2><B>File Seek Functions</B></H2>
  357. <PRE>
  358. sf_count_t sf_seek (SNDFILE *sndfile, sf_count_t frames, int whence) ;
  359. </PRE>
  360. <P>
  361. The file seek functions work much like lseek in unistd.h with the exception that
  362. the non-audio data is ignored and the seek only moves within the audio data section of
  363. the file.
  364. In addition, seeks are defined in number of (multichannel) frames.
  365. Therefore, a seek in a stereo file from the current position forward with an offset
  366. of 1 would skip forward by one sample of both channels.
  367. </P>
  368. <P>
  369. like lseek(), the whence parameter can be any one of the following three values:
  370. </P>
  371. <PRE>
  372. SEEK_SET - The offset is set to the start of the audio data plus offset (multichannel) frames.
  373. SEEK_CUR - The offset is set to its current location plus offset (multichannel) frames.
  374. SEEK_END - The offset is set to the end of the data plus offset (multichannel) frames.
  375. </PRE>
  376. <!-- pepper -->
  377. <P>
  378. Internally, libsndfile keeps track of the read and write locations using separate
  379. read and write pointers.
  380. If a file has been opened with a mode of SFM_RDWR, bitwise OR-ing the standard whence
  381. values above with either SFM_READ or SFM_WRITE allows the read and write pointers to
  382. be modified separately.
  383. If the SEEK_* values are used on their own, the read and write pointers are
  384. both modified.
  385. </P>
  386. <P>
  387. Note that the frames offset can be negative and in fact should be when SEEK_END is used for the
  388. whence parameter.
  389. </P>
  390. <P>
  391. sf_seek will return the offset in (multichannel) frames from the start of the audio data
  392. or -1 if an error occured (ie an attempt is made to seek beyond the start or end of the file).
  393. </P>
  394. <A NAME="error"></A>
  395. <H2><BR><B>Error Reporting Functions</B></H2>
  396. <PRE>
  397. int sf_error (SNDFILE *sndfile) ;
  398. </PRE>
  399. <P>
  400. This function returns the current error number for the given SNDFILE.
  401. The error number may be one of the following:
  402. </P>
  403. <PRE>
  404. enum
  405. { SF_ERR_NO_ERROR = 0,
  406. SF_ERR_UNRECOGNISED_FORMAT = 1,
  407. SF_ERR_SYSTEM = 2,
  408. SF_ERR_MALFORMED_FILE = 3,
  409. SF_ERR_UNSUPPORTED_ENCODING = 4
  410. } ;
  411. </PRE>
  412. <!-- pepper -->
  413. <P>
  414. or any one of many other internal error values.
  415. Applications should only test the return value against error values defined in
  416. &lt;sndfile.h&gt; as the internal error values are subject to change at any
  417. time.
  418. For errors not in the above list, the function sf_error_number() can be used to
  419. convert it to an error string.
  420. </P>
  421. <PRE>
  422. const char* sf_strerror (SNDFILE *sndfile) ;
  423. const char* sf_error_number (int errnum) ;
  424. </PRE>
  425. <P>
  426. The error functions sf_strerror() and sf_error_number() convert the library's internal
  427. error enumerations into text strings.
  428. </P>
  429. <PRE>
  430. int sf_perror (SNDFILE *sndfile) ;
  431. int sf_error_str (SNDFILE *sndfile, char* str, size_t len) ;
  432. </PRE>
  433. <P>
  434. The functions sf_perror() and sf_error_str() are deprecated and will be dropped
  435. from the library at some later date.
  436. </P>
  437. <A NAME="close"></A>
  438. <H2><BR><B>File Close Function</B></H2>
  439. <PRE>
  440. int sf_close (SNDFILE *sndfile) ;
  441. </PRE>
  442. <!-- pepper -->
  443. <P>
  444. The close function closes the file, deallocates its internal buffers and returns
  445. 0 on success or an error value otherwise.
  446. </P>
  447. <BR>
  448. <A NAME="write_sync"></A>
  449. <H2><BR><B>Write Sync Function</B></H2>
  450. <PRE>
  451. void sf_write_sync (SNDFILE *sndfile) ;
  452. </PRE>
  453. <!-- pepper -->
  454. <P>
  455. If the file is opened SFM_WRITE or SFM_RDWR, call the operating system's function
  456. to force the writing of all file cache buffers to disk. If the file is opened
  457. SFM_READ no action is taken.
  458. </P>
  459. <BR>
  460. <A NAME="read"></A>
  461. <H2><BR><B>File Read Functions (Items)</B></H2>
  462. <PRE>
  463. sf_count_t sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
  464. sf_count_t sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
  465. sf_count_t sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
  466. sf_count_t sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
  467. </PRE>
  468. <P>
  469. The file read items functions fill the array pointed to by ptr with the requested
  470. number of items. The items parameter must be an integer product of the number
  471. of channels or an error will occur.
  472. </P>
  473. <!-- pepper -->
  474. <P>
  475. It is important to note that the data type used by the calling program and the data
  476. format of the file do not need to be the same. For instance, it is possible to open
  477. a 16 bit PCM encoded WAV file and read the data using sf_read_float(). The library
  478. seamlessly converts between the two formats on-the-fly. See
  479. <A HREF="#note1">Note 1</A>.
  480. </P>
  481. <!-- pepper -->
  482. <P>
  483. The sf_read_XXXX functions return the number of items read.
  484. Unless the end of the file was reached during the read, the return value should
  485. equal the number of items requested.
  486. Attempts to read beyond the end of the file will not result in an error but will
  487. cause the sf_read_XXXX functions to return less than the number of items requested
  488. or 0 if already at the end of the file.
  489. </P>
  490. <A NAME="readf"></A>
  491. <H2><BR><B>File Read Functions (Frames)</B></H2>
  492. <PRE>
  493. sf_count_t sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
  494. sf_count_t sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
  495. sf_count_t sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
  496. sf_count_t sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
  497. </PRE>
  498. <!-- pepper -->
  499. <P>
  500. The file read frames functions fill the array pointed to by ptr with the requested
  501. number of frames of data. The array must be large enough to hold the product of
  502. frames and the number of channels.
  503. </P>
  504. <P><B>
  505. Care must be taken to ensure that there is enough space in the array pointed to by
  506. ptr, to take (frames * channels) number of items (shorts, ints, floats or doubles).
  507. </B></P>
  508. <P>
  509. The sf_readf_XXXX functions return the number of frames read.
  510. Unless the end of the file was reached during the read, the return value should equal
  511. the number of frames requested.
  512. Attempts to read beyond the end of the file will not result in an error but will cause
  513. the sf_readf_XXXX functions to return less than the number of frames requested or 0 if
  514. already at the end of the file.
  515. </P>
  516. <A NAME="write"></A>
  517. <H2><BR><B>File Write Functions (Items)</B></H2>
  518. <PRE>
  519. sf_count_t sf_write_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ;
  520. sf_count_t sf_write_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ;
  521. sf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ;
  522. sf_count_t sf_write_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ;
  523. </PRE>
  524. <P>
  525. The file write items functions write the data in the array pointed to by ptr to the file.
  526. The items parameter must be an integer product of the number of channels or an error
  527. will occur.
  528. </P>
  529. <!-- pepper -->
  530. <P>
  531. It is important to note that the data type used by the calling program and the data
  532. format of the file do not need to be the same. For instance, it is possible to open
  533. a 16 bit PCM encoded WAV file and write the data using sf_write_float(). The library
  534. seamlessly converts between the two formats on-the-fly. See
  535. <A HREF="#note1">Note 1</A>.
  536. </P>
  537. <P>
  538. The sf_write_XXXX functions return the number of items written (which should be the
  539. same as the items parameter).
  540. </P>
  541. <A NAME="writef"></A>
  542. <H2><BR><B>File Write Functions (Frames)</B></H2>
  543. <PRE>
  544. sf_count_t sf_writef_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ;
  545. sf_count_t sf_writef_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ;
  546. sf_count_t sf_writef_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ;
  547. sf_count_t sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ;
  548. </PRE>
  549. <P>
  550. The file write frames functions write the data in the array pointed to by ptr to the file.
  551. The array must be large enough to hold the product of frames and the number of channels.
  552. </P>
  553. <P>
  554. The sf_writef_XXXX functions return the number of frames written (which should be the
  555. same as the frames parameter).
  556. </P>
  557. <A NAME="raw"></A>
  558. <H2><BR><B>Raw File Read and Write Functions</B></H2>
  559. <!-- pepper -->
  560. <PRE>
  561. sf_count_t sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
  562. sf_count_t sf_write_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ;
  563. </PRE>
  564. <P>
  565. <b>Note:</b> Unless you are writing an external decoder/encode that uses
  566. libsndfile to handle the file headers, you should not be using these
  567. functions.
  568. </P>
  569. <P>
  570. The raw read and write functions read raw audio data from the audio file (not to be
  571. confused with reading RAW header-less PCM files). The number of bytes read or written
  572. must always be an integer multiple of the number of channels multiplied by the number
  573. of bytes required to represent one sample from one channel.
  574. </P>
  575. <!-- pepper -->
  576. <P>
  577. The raw read and write functions return the number of bytes read or written (which
  578. should be the same as the bytes parameter).
  579. </P>
  580. <P>
  581. <B>
  582. Note : The result of using of both regular reads/writes and raw reads/writes on
  583. compressed file formats other than SF_FORMAT_ALAW and SF_FORMAT_ULAW is undefined.
  584. </B>
  585. </P>
  586. <p>
  587. See also : <a href="command.html#SFC_RAW_NEEDS_ENDSWAP">SFC_RAW_NEEDS_ENDSWAP</a>
  588. </p>
  589. <A NAME="string"></A>
  590. <H2><BR><B>Functions for Reading and Writing String Data</B></H2>
  591. <PRE>
  592. const char* sf_get_string (SNDFILE *sndfile, int str_type) ;
  593. int sf_set_string (SNDFILE *sndfile, int str_type, const char* str) ;
  594. </PRE>
  595. <P>
  596. These functions allow strings to be set on files opened for write and to be
  597. retrieved from files opened for read where supported by the given file type.
  598. The <B>str_type</B> parameter can be any one of the following string types:
  599. </P>
  600. <PRE>
  601. enum
  602. { SF_STR_TITLE,
  603. SF_STR_COPYRIGHT,
  604. SF_STR_SOFTWARE,
  605. SF_STR_ARTIST,
  606. SF_STR_COMMENT,
  607. SF_STR_DATE,
  608. SF_STR_ALBUM,
  609. SF_STR_LICENSE,
  610. SF_STR_TRACKNUMBER,
  611. SF_STR_GENRE
  612. } ;
  613. </PRE>
  614. <P>
  615. The sf_get_string() function returns the specified string if it exists and a
  616. NULL pointer otherwise.
  617. In addition to the string ids above, SF_STR_FIRST (== SF_STR_TITLE) and
  618. SF_STR_LAST (always the same as the highest numbers string id) are also
  619. available to allow iteration over all the available string ids.
  620. </P>
  621. <P>
  622. The sf_set_string() function sets the string data.
  623. It returns zero on success and non-zero on error.
  624. The error code can be converted to a string using sf_error_number().
  625. </P>
  626. <P>
  627. </P>
  628. <HR>
  629. <A NAME="note1"></A>
  630. <H2><BR><B>Note 1</B></H2>
  631. <!-- pepper -->
  632. <P>
  633. When converting between integer PCM formats of differing size (ie using sf_read_int()
  634. to read a 16 bit PCM encoded WAV file) libsndfile obeys one simple rule:
  635. </P>
  636. <P CLASS=indent_block>
  637. Whenever integer data is moved from one sized container to another sized container,
  638. the most significant bit in the source container will become the most significant bit
  639. in the destination container.
  640. </P>
  641. <P>
  642. When converting between integer data and floating point data, different rules apply.
  643. The default behaviour when reading floating point data (sf_read_float() or
  644. sf_read_double ()) from a file with integer data is normalisation. Regardless of
  645. whether data in the file is 8, 16, 24 or 32 bit wide, the data will be read as
  646. floating point data in the range [-1.0, 1.0]. Similarly, data in the range [-1.0, 1.0]
  647. will be written to an integer PCM file so that a data value of 1.0 will be the largest
  648. allowable integer for the given bit width. This normalisation can be turned on or off
  649. using the <A HREF="command.html">sf_command</A> interface.
  650. </P>
  651. <A NAME="note2"></A>
  652. <H2><BR><B>Note 2</B></H2>
  653. <P>
  654. Reading a file containg floating point data (allowable with WAV, AIFF, AU and other
  655. file formats) using integer read methods (sf_read_short() or sf_read_int()) can
  656. produce unexpected results.
  657. For instance the data in the file may have a maximum absolute value &lt; 1.0 which
  658. would mean that all sample values read from the file will be zero.
  659. In order to read these files correctly using integer read methods, it is recommended
  660. that you use the
  661. <A HREF="command.html">sf_command</A>
  662. interface, a command of
  663. <A HREF="command.html#SFC_SET_SCALE_FLOAT_INT_READ">SFC_SET_SCALE_FLOAT_INT_READ</A>
  664. and a parameter of SF_TRUE to force correct scaling.
  665. </P>
  666. <!-- pepper -->
  667. <HR>
  668. <!-- pepper -->
  669. <P>
  670. The libsndfile home page is
  671. <A HREF="http://www.mega-nerd.com/libsndfile/">here</A>.
  672. </P>
  673. <P>
  674. Version : 1.0.25
  675. </P>
  676. <!-- pepper -->
  677. <!-- pepper -->
  678. <!-- pepper -->
  679. <!-- pepper -->
  680. </BODY>
  681. </HTML>