1b815c7f3Sopenharmony_ci--- 2b815c7f3Sopenharmony_cilayout: page 3b815c7f3Sopenharmony_cititle: The libsndfile API 4b815c7f3Sopenharmony_ci--- 5b815c7f3Sopenharmony_ci 6b815c7f3Sopenharmony_ci# libsndfile 7b815c7f3Sopenharmony_ci 8b815c7f3Sopenharmony_ciLibsndfile is a library designed to allow the reading and writing of many different sampled sound file formats (such as 9b815c7f3Sopenharmony_ciMS Windows WAV and the Apple/SGI AIFF format) through one standard library interface. 10b815c7f3Sopenharmony_ci 11b815c7f3Sopenharmony_ciDuring read and write operations, formats are seamlessly converted between the format the application program has 12b815c7f3Sopenharmony_cirequested or supplied and the file's data format. The application programmer can remain blissfully unaware of issues 13b815c7f3Sopenharmony_cisuch as file endian-ness and data format. See [Note 1](#note-1) and [Note 2](#note-2). 14b815c7f3Sopenharmony_ci 15b815c7f3Sopenharmony_ciEvery effort is made to keep these documents up-to-date, error free and unambiguous. However, since maintaining the 16b815c7f3Sopenharmony_cidocumentation is the least fun part of working on libsndfile, these docs can and do fall behind the behaviour of the 17b815c7f3Sopenharmony_cilibrary. If any errors, omissions or ambiguities are found, please notify me (erikd) at mega-nerd dot com. 18b815c7f3Sopenharmony_ci 19b815c7f3Sopenharmony_ciTo supplement this reference documentation, there are simple example programs included in the source code tarball. The 20b815c7f3Sopenharmony_citest suite which is also part of the source code tarball is also a good place to look for the correct usage of the 21b815c7f3Sopenharmony_cilibrary functions. 22b815c7f3Sopenharmony_ci 23b815c7f3Sopenharmony_ci**Finally, if you think there is some feature missing from libsndfile, check that it isn't already implemented (and 24b815c7f3Sopenharmony_cidocumented) [here](command.md).** 25b815c7f3Sopenharmony_ci 26b815c7f3Sopenharmony_ci## Synopsis 27b815c7f3Sopenharmony_ci 28b815c7f3Sopenharmony_ci```c 29b815c7f3Sopenharmony_ci#include <stdio.h>; 30b815c7f3Sopenharmony_ci#include <sndfile.h>; 31b815c7f3Sopenharmony_ci``` 32b815c7f3Sopenharmony_ci 33b815c7f3Sopenharmony_ci| Name | Description | 34b815c7f3Sopenharmony_ci|:------------------------------------------------------------------------------------------------------------|:--------------------------------------- | 35b815c7f3Sopenharmony_ci| [sf_open, sf_wchar_open](#open) | File open functions. | 36b815c7f3Sopenharmony_ci| [sf_open_fd](#open_fd) | Open sound file using file descriptor. | 37b815c7f3Sopenharmony_ci| [sf_open_virtual](#open_virtual) | Open sound file using virtual API. | 38b815c7f3Sopenharmony_ci| [sf_format_check](#check) | Validate sound file info. | 39b815c7f3Sopenharmony_ci| [sf_seek](#seek) | Seek position in sound file. | 40b815c7f3Sopenharmony_ci| [sf_command](command.md) | Command interface. | 41b815c7f3Sopenharmony_ci| [sf_error, sf_strerror, sf_error_number, sf_perror, sf_error_str](#error) | Error functions. | 42b815c7f3Sopenharmony_ci| [sf_close](#close) | File close function. | 43b815c7f3Sopenharmony_ci| [sf_write_sync](#write_sync) | Write sync function. | 44b815c7f3Sopenharmony_ci| [sf_read_short, sf_read_int, sf_read_float, sf_read_double](#read) | File items read functions. | 45b815c7f3Sopenharmony_ci| [sf_readf_short, sf_readf_int, sf_readf_float, sf_readf_double](#readf) | File frames read functions. | 46b815c7f3Sopenharmony_ci| [sf_write_short, sf_write_int, sf_write_float, sf_write_double](#write) | File items write functions. | 47b815c7f3Sopenharmony_ci| [sf_writef_short, sf_writef_int, sf_writef_float, sf_writef_double](#writef) | File frames write functions. | 48b815c7f3Sopenharmony_ci| [sf_read_raw, sf_write_raw](#raw) | Raw read/write functions. | 49b815c7f3Sopenharmony_ci| [sf_get_string, sf_set_string](#string) | Functions for reading and writing string data. | 50b815c7f3Sopenharmony_ci| [sf_version_string](#version_string) | Retrive library version string. | 51b815c7f3Sopenharmony_ci| [sf_current_byterate](#current_byterate) | Retrieve current byterate. | 52b815c7f3Sopenharmony_ci| [sf_set_chunk, sf_get_chunk_iterator, sf_next_chunk_iterator, sf_get_chunk_size, sf_get_chunk_data](#chunk) | RIFF chunks API. | 53b815c7f3Sopenharmony_ci 54b815c7f3Sopenharmony_ciSNDFILE* is an anonymous pointer to data which is private to the library. 55b815c7f3Sopenharmony_ci 56b815c7f3Sopenharmony_ci## File Open Function {#open} 57b815c7f3Sopenharmony_ci 58b815c7f3Sopenharmony_ci```c 59b815c7f3Sopenharmony_ciSNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo) ; 60b815c7f3Sopenharmony_ci``` 61b815c7f3Sopenharmony_ci 62b815c7f3Sopenharmony_ciThe sf_open() function opens the sound file at the specified path. The filename is byte encoded, but may be utf-8 on 63b815c7f3Sopenharmony_ciLinux, while on Mac OS X it will use the filesystem character set. On Windows, there is also a Windows specific 64b815c7f3Sopenharmony_cisf_wchar_open() that takes a UTF16_BE encoded filename. 65b815c7f3Sopenharmony_ci 66b815c7f3Sopenharmony_ci```c 67b815c7f3Sopenharmony_ciSNDFILE* sf_wchar_open (LPCWSTR wpath, int mode, SF_INFO *sfinfo) ; 68b815c7f3Sopenharmony_ci``` 69b815c7f3Sopenharmony_ci 70b815c7f3Sopenharmony_ciThe SF_INFO structure is for passing data between the calling function and the library when opening a file for reading 71b815c7f3Sopenharmony_cior writing. It is defined in sndfile.h as follows: 72b815c7f3Sopenharmony_ci 73b815c7f3Sopenharmony_ci```c 74b815c7f3Sopenharmony_citypedef struct 75b815c7f3Sopenharmony_ci{ sf_count_t frames ; /* Used to be called samples. */ 76b815c7f3Sopenharmony_ci int samplerate ; 77b815c7f3Sopenharmony_ci int channels ; 78b815c7f3Sopenharmony_ci int format ; 79b815c7f3Sopenharmony_ci int sections ; 80b815c7f3Sopenharmony_ci int seekable ; 81b815c7f3Sopenharmony_ci } SF_INFO ; 82b815c7f3Sopenharmony_ci``` 83b815c7f3Sopenharmony_ci 84b815c7f3Sopenharmony_ciThe mode parameter for this function can be any one of the following three values: 85b815c7f3Sopenharmony_ci 86b815c7f3Sopenharmony_ciSFM_READ 87b815c7f3Sopenharmony_ci: read only mode 88b815c7f3Sopenharmony_ci 89b815c7f3Sopenharmony_ciSFM_WRITE 90b815c7f3Sopenharmony_ci: write only mode 91b815c7f3Sopenharmony_ci 92b815c7f3Sopenharmony_ciSFM_RDWR 93b815c7f3Sopenharmony_ci: read/write mode 94b815c7f3Sopenharmony_ci 95b815c7f3Sopenharmony_ciWhen opening a file for read, the **format** field should be set to zero before 96b815c7f3Sopenharmony_cicalling **sf_open**(). The only exception to this is the case of RAW files where 97b815c7f3Sopenharmony_cithe caller has to set the **samplerate**, **channels** and **format** fields to 98b815c7f3Sopenharmony_civalid values. All other fields of the structure are filled in by the library. 99b815c7f3Sopenharmony_ci 100b815c7f3Sopenharmony_ci**Note:** The libsndfile library will reject values for field **channels** that 101b815c7f3Sopenharmony_ciare greater than `1024`. These value represent the maximum theoretical limit 102b815c7f3Sopenharmony_ciand may be less for specific formats. 103b815c7f3Sopenharmony_ci 104b815c7f3Sopenharmony_ciWhen opening a file for write, the caller must fill in structure members 105b815c7f3Sopenharmony_ci**samplerate**, **channels**, and **format**. 106b815c7f3Sopenharmony_ci 107b815c7f3Sopenharmony_ciThe **format** field in the above **SF_INFO** structure is made up of the 108b815c7f3Sopenharmony_cibit-wise OR of a major format type (values between 0x10000 and 0x08000000), a 109b815c7f3Sopenharmony_ciminor format type (with values less than 0x10000) and an optional endian-ness 110b815c7f3Sopenharmony_civalue. The currently understood formats are listed in *sndfile.h* as follows and 111b815c7f3Sopenharmony_cialso include bitmasks for separating major and minor file types. Not all 112b815c7f3Sopenharmony_cicombinations of endian-ness and major and minor file types are valid. 113b815c7f3Sopenharmony_ci 114b815c7f3Sopenharmony_ci| Name | Value | Description | 115b815c7f3Sopenharmony_ci|:-------------------------|:-----------|:-------------------------------------------| 116b815c7f3Sopenharmony_ci| **Major formats.** | 117b815c7f3Sopenharmony_ci| SF_FORMAT_WAV | 0x010000 | Microsoft WAV format (little endian). | 118b815c7f3Sopenharmony_ci| SF_FORMAT_AIFF | 0x020000 | Apple/SGI AIFF format (big endian). | 119b815c7f3Sopenharmony_ci| SF_FORMAT_AU | 0x030000 | Sun/NeXT AU format (big endian). | 120b815c7f3Sopenharmony_ci| SF_FORMAT_RAW | 0x040000 | RAW PCM data. | 121b815c7f3Sopenharmony_ci| SF_FORMAT_PAF | 0x050000 | Ensoniq PARIS file format. | 122b815c7f3Sopenharmony_ci| SF_FORMAT_SVX | 0x060000 | Amiga IFF / SVX8 / SV16 format. | 123b815c7f3Sopenharmony_ci| SF_FORMAT_NIST | 0x070000 | Sphere NIST format. | 124b815c7f3Sopenharmony_ci| SF_FORMAT_VOC | 0x080000 | VOC files. | 125b815c7f3Sopenharmony_ci| SF_FORMAT_IRCAM | 0x0A0000 | Berkeley/IRCAM/CARL | 126b815c7f3Sopenharmony_ci| SF_FORMAT_W64 | 0x0B0000 | Sonic Foundry's 64 bit RIFF/WAV | 127b815c7f3Sopenharmony_ci| SF_FORMAT_MAT4 | 0x0C0000 | Matlab (tm) V4.2 / GNU Octave 2.0 | 128b815c7f3Sopenharmony_ci| SF_FORMAT_MAT5 | 0x0D0000 | Matlab (tm) V5.0 / GNU Octave 2.1 | 129b815c7f3Sopenharmony_ci| SF_FORMAT_PVF | 0x0E0000 | Portable Voice Format | 130b815c7f3Sopenharmony_ci| SF_FORMAT_XI | 0x0F0000 | Fasttracker 2 Extended Instrument | 131b815c7f3Sopenharmony_ci| SF_FORMAT_HTK | 0x100000 | HMM Tool Kit format | 132b815c7f3Sopenharmony_ci| SF_FORMAT_SDS | 0x110000 | Midi Sample Dump Standard | 133b815c7f3Sopenharmony_ci| SF_FORMAT_AVR | 0x120000 | Audio Visual Research | 134b815c7f3Sopenharmony_ci| SF_FORMAT_WAVEX | 0x130000 | MS WAVE with WAVEFORMATEX | 135b815c7f3Sopenharmony_ci| SF_FORMAT_SD2 | 0x160000 | Sound Designer 2 | 136b815c7f3Sopenharmony_ci| SF_FORMAT_FLAC | 0x170000 | FLAC lossless file format | 137b815c7f3Sopenharmony_ci| SF_FORMAT_CAF | 0x180000 | Core Audio File format | 138b815c7f3Sopenharmony_ci| SF_FORMAT_WVE | 0x190000 | Psion WVE format | 139b815c7f3Sopenharmony_ci| SF_FORMAT_OGG | 0x200000 | Xiph OGG container | 140b815c7f3Sopenharmony_ci| SF_FORMAT_MPC2K | 0x210000 | Akai MPC 2000 sampler | 141b815c7f3Sopenharmony_ci| SF_FORMAT_RF64 | 0x220000 | RF64 WAV file | 142b815c7f3Sopenharmony_ci| SF_FORMAT_MPEG | 0x230000 | MPEG-1/2 audio stream | 143b815c7f3Sopenharmony_ci| **Subtypes.** | 144b815c7f3Sopenharmony_ci| SF_FORMAT_PCM_S8 | 0x0001 | Signed 8 bit data | 145b815c7f3Sopenharmony_ci| SF_FORMAT_PCM_16 | 0x0002 | Signed 16 bit data | 146b815c7f3Sopenharmony_ci| SF_FORMAT_PCM_24 | 0x0003 | Signed 24 bit data | 147b815c7f3Sopenharmony_ci| SF_FORMAT_PCM_32 | 0x0004 | Signed 32 bit data | 148b815c7f3Sopenharmony_ci| SF_FORMAT_PCM_U8 | 0x0005 | Unsigned 8 bit data (WAV and RAW only) | 149b815c7f3Sopenharmony_ci| SF_FORMAT_FLOAT | 0x0006 | 32 bit float data | 150b815c7f3Sopenharmony_ci| SF_FORMAT_DOUBLE | 0x0007 | 64 bit float data | 151b815c7f3Sopenharmony_ci| SF_FORMAT_ULAW | 0x0010 | U-Law encoded. | 152b815c7f3Sopenharmony_ci| SF_FORMAT_ALAW | 0x0011 | A-Law encoded. | 153b815c7f3Sopenharmony_ci| SF_FORMAT_IMA_ADPCM | 0x0012 | IMA ADPCM. | 154b815c7f3Sopenharmony_ci| SF_FORMAT_MS_ADPCM | 0x0013 | Microsoft ADPCM. | 155b815c7f3Sopenharmony_ci| SF_FORMAT_GSM610 | 0x0020 | GSM 6.10 encoding. | 156b815c7f3Sopenharmony_ci| SF_FORMAT_VOX_ADPCM | 0x0021 | OKI / Dialogix ADPCM | 157b815c7f3Sopenharmony_ci| SF_FORMAT_NMS_ADPCM_16 | 0x0022 | 16kbs NMS G721-variant encoding. | 158b815c7f3Sopenharmony_ci| SF_FORMAT_NMS_ADPCM_24 | 0x0023 | 24kbs NMS G721-variant encoding. | 159b815c7f3Sopenharmony_ci| SF_FORMAT_NMS_ADPCM_32 | 0x0024 | 32kbs NMS G721-variant encoding. | 160b815c7f3Sopenharmony_ci| SF_FORMAT_G721_32 | 0x0030 | 32kbs G721 ADPCM encoding. | 161b815c7f3Sopenharmony_ci| SF_FORMAT_G723_24 | 0x0031 | 24kbs G723 ADPCM encoding. | 162b815c7f3Sopenharmony_ci| SF_FORMAT_G723_40 | 0x0032 | 40kbs G723 ADPCM encoding. | 163b815c7f3Sopenharmony_ci| SF_FORMAT_DWVW_12 | 0x0040 | 12 bit Delta Width Variable Word encoding. | 164b815c7f3Sopenharmony_ci| SF_FORMAT_DWVW_16 | 0x0041 | 16 bit Delta Width Variable Word encoding. | 165b815c7f3Sopenharmony_ci| SF_FORMAT_DWVW_24 | 0x0042 | 24 bit Delta Width Variable Word encoding. | 166b815c7f3Sopenharmony_ci| SF_FORMAT_DWVW_N | 0x0043 | N bit Delta Width Variable Word encoding. | 167b815c7f3Sopenharmony_ci| SF_FORMAT_DPCM_8 | 0x0050 | 8 bit differential PCM (XI only) | 168b815c7f3Sopenharmony_ci| SF_FORMAT_DPCM_16 | 0x0051 | 16 bit differential PCM (XI only) | 169b815c7f3Sopenharmony_ci| SF_FORMAT_VORBIS | 0x0060 | Xiph Vorbis encoding. | 170b815c7f3Sopenharmony_ci| SF_FORMAT_OPUS | 0x0064 | Xiph/Skype Opus encoding. | 171b815c7f3Sopenharmony_ci| SF_FORMAT_ALAC_16 | 0x0070 | Apple Lossless Audio Codec (16 bit). | 172b815c7f3Sopenharmony_ci| SF_FORMAT_ALAC_20 | 0x0071 | Apple Lossless Audio Codec (20 bit). | 173b815c7f3Sopenharmony_ci| SF_FORMAT_ALAC_24 | 0x0072 | Apple Lossless Audio Codec (24 bit). | 174b815c7f3Sopenharmony_ci| SF_FORMAT_ALAC_32 | 0x0073 | Apple Lossless Audio Codec (32 bit). | 175b815c7f3Sopenharmony_ci| SF_FORMAT_MPEG_LAYER_I | 0x0080 | MPEG-1 Audio Layer I. | 176b815c7f3Sopenharmony_ci| SF_FORMAT_MPEG_LAYER_II | 0x0081 | MPEG-1 Audio Layer II. | 177b815c7f3Sopenharmony_ci| SF_FORMAT_MPEG_LAYER_III | 0x0082 | MPEG-2 Audio Layer III. | 178b815c7f3Sopenharmony_ci| **Endian-ness options.** | 179b815c7f3Sopenharmony_ci| SF_ENDIAN_FILE | 0x00000000 | Default file endian-ness. | 180b815c7f3Sopenharmony_ci| SF_ENDIAN_LITTLE | 0x10000000 | Force little endian-ness. | 181b815c7f3Sopenharmony_ci| SF_ENDIAN_BIG | 0x20000000 | Force big endian-ness. | 182b815c7f3Sopenharmony_ci| SF_ENDIAN_CPU | 0x30000000 | Force CPU endian-ness. | 183b815c7f3Sopenharmony_ci| SF_FORMAT_SUBMASK | 0x0000FFFF | | 184b815c7f3Sopenharmony_ci| SF_FORMAT_TYPEMASK | 0x0FFF0000 | | 185b815c7f3Sopenharmony_ci| SF_FORMAT_ENDMASK | 0x30000000 | | 186b815c7f3Sopenharmony_ci 187b815c7f3Sopenharmony_ciEvery call to **sf_open**() should be matched with a call to 188b815c7f3Sopenharmony_ci[**sf_close**()](#close) to free up memory allocated during the call to **sf_open**(). 189b815c7f3Sopenharmony_ci 190b815c7f3Sopenharmony_ciOn success, the sf_open function returns a non-NULL pointer which should be passed as the first parameter to all 191b815c7f3Sopenharmony_cisubsequent libsndfile calls dealing with that audio file. On fail, the sf_open function returns a NULL pointer. An 192b815c7f3Sopenharmony_ciexplanation of the error can obtained by passing NULL to [**sf_strerror**()](#error). 193b815c7f3Sopenharmony_ci 194b815c7f3Sopenharmony_ci### File Descriptor Open {#open_fd} 195b815c7f3Sopenharmony_ci 196b815c7f3Sopenharmony_ci```c 197b815c7f3Sopenharmony_ciSNDFILE* sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc) ; 198b815c7f3Sopenharmony_ci``` 199b815c7f3Sopenharmony_ci 200b815c7f3Sopenharmony_ci**Note:** On Microsoft Windows, this function does not work if the application 201b815c7f3Sopenharmony_ciand the libsndfile DLL are linked to different versions of the Microsoft C 202b815c7f3Sopenharmony_ciruntime DLL. 203b815c7f3Sopenharmony_ci 204b815c7f3Sopenharmony_ciThe second open function takes a file descriptor of a file that has already been 205b815c7f3Sopenharmony_ciopened. Care should be taken to ensure that the mode of the file represented by 206b815c7f3Sopenharmony_cithe descriptor matches the mode argument. This function is useful in the 207b815c7f3Sopenharmony_cifollowing circumstances: 208b815c7f3Sopenharmony_ci 209b815c7f3Sopenharmony_ci* Opening temporary files securely (ie use the **tmpfile**() to return a FILE* 210b815c7f3Sopenharmony_ci pointer and then using fileno() to retrieve the file descriptor which is then 211b815c7f3Sopenharmony_ci passed to libsndfile). 212b815c7f3Sopenharmony_ci* Opening files with file names using OS specific character encodings and then 213b815c7f3Sopenharmony_ci passing the file descriptor to **sf_open_fd**(). 214b815c7f3Sopenharmony_ci* Opening sound files embedded within larger files. [More info](embedded_files.md). 215b815c7f3Sopenharmony_ci 216b815c7f3Sopenharmony_ciEvery call to `sf_open_fd`() should be matched with a call to sf_close() to free 217b815c7f3Sopenharmony_ciup memory allocated during the call to sf_open_fd(). 218b815c7f3Sopenharmony_ci 219b815c7f3Sopenharmony_ciWhen sf_close() is called, the file descriptor is only closed if the 220b815c7f3Sopenharmony_ci**close_desc** parameter was TRUE when the sf_open_fd() function was called. 221b815c7f3Sopenharmony_ci 222b815c7f3Sopenharmony_ciOn success, the sf_open_fd() function returns a non-NULL pointer which should be 223b815c7f3Sopenharmony_cipassed as the first parameter to all subsequent libsndfile calls dealing with 224b815c7f3Sopenharmony_cithat audio file. On fail, the sf_open_fd() function returns a NULL pointer. 225b815c7f3Sopenharmony_ci 226b815c7f3Sopenharmony_ci### Virtual File Open Function {#open_virtual} 227b815c7f3Sopenharmony_ci 228b815c7f3Sopenharmony_ci```c 229b815c7f3Sopenharmony_ciSNDFILE* sf_open_virtual (SF_VIRTUAL_IO *sfvirtual, int mode, SF_INFO *sfinfo, void *user_data) ; 230b815c7f3Sopenharmony_ci``` 231b815c7f3Sopenharmony_ci 232b815c7f3Sopenharmony_ciOpens a soundfile from a virtual file I/O context which is provided by the 233b815c7f3Sopenharmony_cicaller. This is usually used to interface libsndfile to write/read from memory 234b815c7f3Sopenharmony_ciwith a stream or buffer based system. Apart from the sfvirtual and the user_data 235b815c7f3Sopenharmony_ciparameters this function behaves like [sf_open()](#open). 236b815c7f3Sopenharmony_ci 237b815c7f3Sopenharmony_ci```c 238b815c7f3Sopenharmony_ci typedef struct 239b815c7f3Sopenharmony_ci { sf_vio_get_filelen get_filelen ; 240b815c7f3Sopenharmony_ci sf_vio_seek seek ; 241b815c7f3Sopenharmony_ci sf_vio_read read ; 242b815c7f3Sopenharmony_ci sf_vio_write write ; 243b815c7f3Sopenharmony_ci sf_vio_tell tell ; 244b815c7f3Sopenharmony_ci } SF_VIRTUAL_IO ; 245b815c7f3Sopenharmony_ci``` 246b815c7f3Sopenharmony_ci 247b815c7f3Sopenharmony_ciLibsndfile calls the callbacks provided by the SF_VIRTUAL_IO structure when 248b815c7f3Sopenharmony_ciopening, reading and writing to the virtual file context. The user_data pointer 249b815c7f3Sopenharmony_ciis a user defined context which will be available in the callbacks. 250b815c7f3Sopenharmony_ci 251b815c7f3Sopenharmony_ci```c 252b815c7f3Sopenharmony_citypedef sf_count_t (*sf_vio_get_filelen) (void *user_data) ; 253b815c7f3Sopenharmony_citypedef sf_count_t (*sf_vio_seek) (sf_count_t offset, int whence, void *user_data) ; 254b815c7f3Sopenharmony_citypedef sf_count_t (*sf_vio_read) (void *ptr, sf_count_t count, void *user_data) ; 255b815c7f3Sopenharmony_citypedef sf_count_t (*sf_vio_write) (const void *ptr, sf_count_t count, void *user_data) ; 256b815c7f3Sopenharmony_citypedef sf_count_t (*sf_vio_tell) (void *user_data) ; 257b815c7f3Sopenharmony_ci``` 258b815c7f3Sopenharmony_ci 259b815c7f3Sopenharmony_ci#### sf_vio_get_filelen 260b815c7f3Sopenharmony_ci 261b815c7f3Sopenharmony_ci```c 262b815c7f3Sopenharmony_citypedef sf_count_t (*sf_vio_get_filelen) (void *user_data) ; 263b815c7f3Sopenharmony_ci``` 264b815c7f3Sopenharmony_ci 265b815c7f3Sopenharmony_ciThe virtual file contex must return the length of the virtual file in bytes. 266b815c7f3Sopenharmony_ci 267b815c7f3Sopenharmony_ci#### sf_vio_seek 268b815c7f3Sopenharmony_ci 269b815c7f3Sopenharmony_ci```c 270b815c7f3Sopenharmony_citypedef sf_count_t (*sf_vio_seek) (sf_count_t offset, int whence, void *user_data) ; 271b815c7f3Sopenharmony_ci``` 272b815c7f3Sopenharmony_ci 273b815c7f3Sopenharmony_ciThe virtual file context must seek to offset using the seek mode provided by 274b815c7f3Sopenharmony_ciwhence which is one of SEEK_CUR, SEEK_SET, SEEK_END. 275b815c7f3Sopenharmony_ci 276b815c7f3Sopenharmony_ciThe return value must contain the new offset in the file. 277b815c7f3Sopenharmony_ci 278b815c7f3Sopenharmony_ci#### sf_vio_read 279b815c7f3Sopenharmony_ci 280b815c7f3Sopenharmony_ci```c 281b815c7f3Sopenharmony_citypedef sf_count_t (*sf_vio_read) (void *ptr, sf_count_t count, void *user_data) ; 282b815c7f3Sopenharmony_ci``` 283b815c7f3Sopenharmony_ci 284b815c7f3Sopenharmony_ciThe virtual file context must copy ("read") "count" bytes into the buffer 285b815c7f3Sopenharmony_ciprovided by ptr and return the count of actually copied bytes. 286b815c7f3Sopenharmony_ci 287b815c7f3Sopenharmony_ci#### sf_vio_write 288b815c7f3Sopenharmony_ci 289b815c7f3Sopenharmony_ci```c 290b815c7f3Sopenharmony_citypedef sf_count_t (*sf_vio_write) (const void *ptr, sf_count_t count, void *user_data) ; 291b815c7f3Sopenharmony_ci``` 292b815c7f3Sopenharmony_ci 293b815c7f3Sopenharmony_ciThe virtual file context must process "count" bytes stored in the buffer passed 294b815c7f3Sopenharmony_ciwith ptr and return the count of actually processed bytes. 295b815c7f3Sopenharmony_ci 296b815c7f3Sopenharmony_ci#### sf_vio_tell 297b815c7f3Sopenharmony_ci 298b815c7f3Sopenharmony_ci```c 299b815c7f3Sopenharmony_citypedef sf_count_t (*sf_vio_tell) (void *user_data) ; 300b815c7f3Sopenharmony_ci``` 301b815c7f3Sopenharmony_ci 302b815c7f3Sopenharmony_ciReturn the current position of the virtual file context. 303b815c7f3Sopenharmony_ci 304b815c7f3Sopenharmony_ci## Format Check Function {#chek} 305b815c7f3Sopenharmony_ci 306b815c7f3Sopenharmony_ci```c 307b815c7f3Sopenharmony_ciint sf_format_check (const SF_INFO *info) ; 308b815c7f3Sopenharmony_ci``` 309b815c7f3Sopenharmony_ci 310b815c7f3Sopenharmony_ciThis function allows the caller to check if a set of parameters in the SF_INFO 311b815c7f3Sopenharmony_cistruct is valid before calling [sf_open](#open) (SFM_WRITE). 312b815c7f3Sopenharmony_ci 313b815c7f3Sopenharmony_cisf_format_check() returns TRUE if the parameters are valid and FALSE otherwise. 314b815c7f3Sopenharmony_ci 315b815c7f3Sopenharmony_ci## File Seek Functions 316b815c7f3Sopenharmony_ci 317b815c7f3Sopenharmony_ci```c 318b815c7f3Sopenharmony_cisf_count_t sf_seek (SNDFILE *sndfile, sf_count_t frames, int whence) ; 319b815c7f3Sopenharmony_ci``` 320b815c7f3Sopenharmony_ci 321b815c7f3Sopenharmony_ciThe file seek functions work much like lseek in unistd.h with the exception that 322b815c7f3Sopenharmony_cithe non-audio data is ignored and the seek only moves within the audio data 323b815c7f3Sopenharmony_cisection of the file. In addition, seeks are defined in number of (multichannel) 324b815c7f3Sopenharmony_ciframes. Therefore, a seek in a stereo file from the current position forward 325b815c7f3Sopenharmony_ciwith an offset of 1 would skip forward by one sample of both channels. 326b815c7f3Sopenharmony_ci 327b815c7f3Sopenharmony_cilike lseek(), the whence parameter can be any one of the following three values: 328b815c7f3Sopenharmony_ci 329b815c7f3Sopenharmony_ciSEEK_SET 330b815c7f3Sopenharmony_ci: The offset is set to the start of the audio data plus offset (multichannel) 331b815c7f3Sopenharmony_ciframes. 332b815c7f3Sopenharmony_ci 333b815c7f3Sopenharmony_ciSEEK_CUR 334b815c7f3Sopenharmony_ci: The offset is set to its current location plus offset (multichannel) frames. 335b815c7f3Sopenharmony_ci 336b815c7f3Sopenharmony_ciSEEK_END 337b815c7f3Sopenharmony_ci: The offset is set to the end of the data plus offset (multichannel) frames. 338b815c7f3Sopenharmony_ci 339b815c7f3Sopenharmony_ciInternally, libsndfile keeps track of the read and write locations using 340b815c7f3Sopenharmony_ciseparate read and write pointers. If a file has been opened with a mode of 341b815c7f3Sopenharmony_ciSFM_RDWR, bitwise OR-ing the standard whence values above with either SFM_READ 342b815c7f3Sopenharmony_cior SFM_WRITE allows the read and write pointers to be modified separately. 343b815c7f3Sopenharmony_ciIf the SEEK_* values are used on their own, the read and write pointers are 344b815c7f3Sopenharmony_ciboth modified. 345b815c7f3Sopenharmony_ci 346b815c7f3Sopenharmony_ciNote that the frames offset can be negative and in fact should be when SEEK_END 347b815c7f3Sopenharmony_ciis used for the whence parameter. 348b815c7f3Sopenharmony_ci 349b815c7f3Sopenharmony_cisf_seek will return the offset in (multichannel) frames from the start of the 350b815c7f3Sopenharmony_ciaudio data or -1 if an error occured (ie an attempt is made to seek beyond the 351b815c7f3Sopenharmony_cistart or end of the file). 352b815c7f3Sopenharmony_ci 353b815c7f3Sopenharmony_ci## Error Reporting Functions {#error} 354b815c7f3Sopenharmony_ci 355b815c7f3Sopenharmony_ci```c 356b815c7f3Sopenharmony_ciint sf_error (SNDFILE *sndfile) ; 357b815c7f3Sopenharmony_ci``` 358b815c7f3Sopenharmony_ci 359b815c7f3Sopenharmony_ciThis function returns the current error number for the given SNDFILE. 360b815c7f3Sopenharmony_ci 361b815c7f3Sopenharmony_ciThe error number may be one of the following: 362b815c7f3Sopenharmony_ci 363b815c7f3Sopenharmony_ci| Name | Value | 364b815c7f3Sopenharmony_ci|:----------------------------|:------| 365b815c7f3Sopenharmony_ci| SF_ERR_NO_ERROR | 0 | 366b815c7f3Sopenharmony_ci| SF_ERR_UNRECOGNISED_FORMAT | 1 | 367b815c7f3Sopenharmony_ci| SF_ERR_SYSTEM | 2 | 368b815c7f3Sopenharmony_ci| SF_ERR_MALFORMED_FILE | 3 | 369b815c7f3Sopenharmony_ci| SF_ERR_UNSUPPORTED_ENCODING | 4 | 370b815c7f3Sopenharmony_ci 371b815c7f3Sopenharmony_cior any one of many other internal error values. 372b815c7f3Sopenharmony_ciApplications should only test the return value against error values defined in 373b815c7f3Sopenharmony_ci\<sndfile.h\>; as the internal error values are subject to change at any time. 374b815c7f3Sopenharmony_ciFor errors not in the above list, the function sf_error_number() can be used to 375b815c7f3Sopenharmony_ciconvert it to an error string. 376b815c7f3Sopenharmony_ci 377b815c7f3Sopenharmony_ci```c 378b815c7f3Sopenharmony_ciconst char* sf_strerror (SNDFILE *sndfile) ; 379b815c7f3Sopenharmony_ciconst char* sf_error_number (int errnum) ; 380b815c7f3Sopenharmony_ci``` 381b815c7f3Sopenharmony_ci 382b815c7f3Sopenharmony_ciThe error functions sf_strerror () and sf_error_number () convert the library's 383b815c7f3Sopenharmony_ciinternal error enumerations into text strings. 384b815c7f3Sopenharmony_ci 385b815c7f3Sopenharmony_ci```c 386b815c7f3Sopenharmony_ciint sf_perror (SNDFILE *sndfile) ; 387b815c7f3Sopenharmony_ciint sf_error_str (SNDFILE *sndfile, char* str, size_t len) ; 388b815c7f3Sopenharmony_ci``` 389b815c7f3Sopenharmony_ci 390b815c7f3Sopenharmony_ciThe functions sf_perror() and sf_error_str() are deprecated and will be dropped 391b815c7f3Sopenharmony_cifrom the library at some later date. 392b815c7f3Sopenharmony_ci 393b815c7f3Sopenharmony_ci## File Close Function {#close} 394b815c7f3Sopenharmony_ci 395b815c7f3Sopenharmony_ci```c 396b815c7f3Sopenharmony_ciint sf_close (SNDFILE *sndfile) ; 397b815c7f3Sopenharmony_ci``` 398b815c7f3Sopenharmony_ci 399b815c7f3Sopenharmony_ciThe close function closes the file, deallocates its internal buffers and returns 400b815c7f3Sopenharmony_ci0 on success or an error value otherwise. 401b815c7f3Sopenharmony_ci 402b815c7f3Sopenharmony_ci## Write Sync Function {#write_sync} 403b815c7f3Sopenharmony_ci 404b815c7f3Sopenharmony_ci```c 405b815c7f3Sopenharmony_civoid sf_write_sync (SNDFILE *sndfile) ; 406b815c7f3Sopenharmony_ci``` 407b815c7f3Sopenharmony_ci 408b815c7f3Sopenharmony_ciIf the file is opened SFM_WRITE or SFM_RDWR, call the operating system's 409b815c7f3Sopenharmony_cifunction to force the writing of all file cache buffers to disk. If the file is 410b815c7f3Sopenharmony_ciopened SFM_READ no action is taken. 411b815c7f3Sopenharmony_ci 412b815c7f3Sopenharmony_ci## File Read Functions {#read} 413b815c7f3Sopenharmony_ci 414b815c7f3Sopenharmony_ci```c 415b815c7f3Sopenharmony_cisf_count_t sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ; 416b815c7f3Sopenharmony_cisf_count_t sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ; 417b815c7f3Sopenharmony_cisf_count_t sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ; 418b815c7f3Sopenharmony_cisf_count_t sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ; 419b815c7f3Sopenharmony_ci``` 420b815c7f3Sopenharmony_ci 421b815c7f3Sopenharmony_ci{: #readf} 422b815c7f3Sopenharmony_ci```c 423b815c7f3Sopenharmony_cisf_count_t sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ; 424b815c7f3Sopenharmony_cisf_count_t sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ; 425b815c7f3Sopenharmony_cisf_count_t sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ; 426b815c7f3Sopenharmony_cisf_count_t sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ; 427b815c7f3Sopenharmony_ci``` 428b815c7f3Sopenharmony_ci 429b815c7f3Sopenharmony_ciThe file read functions fill the array pointed to by ptr with the requested 430b815c7f3Sopenharmony_cinumber of items or frames. 431b815c7f3Sopenharmony_ci 432b815c7f3Sopenharmony_ciFor the frames-count functions, the frames parameter specifies the number of 433b815c7f3Sopenharmony_ciframes. A frame is just a block of samples, one for each channel. 434b815c7f3Sopenharmony_ci 435b815c7f3Sopenharmony_ci**Care must be taken to ensure that there is enough space in the array pointed 436b815c7f3Sopenharmony_cito by ptr, to take (frames \* channels) number of items (shorts, ints, floats or 437b815c7f3Sopenharmony_cidoubles).** 438b815c7f3Sopenharmony_ci 439b815c7f3Sopenharmony_ciFor the items-count functions, the items parameter must be an integer product 440b815c7f3Sopenharmony_ciof the number of channels or an error will occur. Here, an item is just a 441b815c7f3Sopenharmony_cisample. 442b815c7f3Sopenharmony_ci 443b815c7f3Sopenharmony_ciNote: The only difference between the "items" and "frames" versions of each read 444b815c7f3Sopenharmony_cifunction is the units in which the object count is specified - calling 445b815c7f3Sopenharmony_cisf_readf_short() with a count argument of N, on a SNDFILE with C channels, is 446b815c7f3Sopenharmony_cithe same as calling sf_read_short with a count argument of N\*C. The buffer 447b815c7f3Sopenharmony_cipointed to by "ptr" should be the same number of bytes in each case. 448b815c7f3Sopenharmony_ci 449b815c7f3Sopenharmony_ciNote: The data type used by the calling program and the data format of the file 450b815c7f3Sopenharmony_cido not need to be the same. For instance, it is possible to open a 16 bit PCM 451b815c7f3Sopenharmony_ciencoded WAV file and read the data using sf_read_float(). The library seamlessly 452b815c7f3Sopenharmony_ciconverts between the two formats on-the-fly. See [Note 1](#note-1). 453b815c7f3Sopenharmony_ci 454b815c7f3Sopenharmony_ciThe sf_read_XXXX and sf_readf_XXXX functions return the number of items or 455b815c7f3Sopenharmony_ciframes read, respectively. Unless the end of the file was reached during the 456b815c7f3Sopenharmony_ciread, the return value should equal the number of objects requested. Attempts to 457b815c7f3Sopenharmony_ciread beyond the end of the file will not result in an error but will cause the 458b815c7f3Sopenharmony_ciread functions to return less than the number of objects requested or 0 if 459b815c7f3Sopenharmony_cialready at the end of the file. When the buffer is not is not completely filled, 460b815c7f3Sopenharmony_ciunused buffer space is filled by zeroes. 461b815c7f3Sopenharmony_ci 462b815c7f3Sopenharmony_ci## File Write Functions {#write} 463b815c7f3Sopenharmony_ci 464b815c7f3Sopenharmony_ci```c 465b815c7f3Sopenharmony_cisf_count_t sf_write_short (SNDFILE *sndfile, short *ptr, sf_count_t items) ; 466b815c7f3Sopenharmony_cisf_count_t sf_write_int (SNDFILE *sndfile, int *ptr, sf_count_t items) ; 467b815c7f3Sopenharmony_cisf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ; 468b815c7f3Sopenharmony_cisf_count_t sf_write_double (SNDFILE *sndfile, double *ptr, sf_count_t items) ; 469b815c7f3Sopenharmony_ci``` 470b815c7f3Sopenharmony_ci 471b815c7f3Sopenharmony_ci{: #writef} 472b815c7f3Sopenharmony_ci```c 473b815c7f3Sopenharmony_cisf_count_t sf_writef_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ; 474b815c7f3Sopenharmony_cisf_count_t sf_writef_int (SNDFILE *sndfile, int *ptr, sf_count_t frames) ; 475b815c7f3Sopenharmony_cisf_count_t sf_writef_float (SNDFILE *sndfile, float *ptr, sf_count_t frames) ; 476b815c7f3Sopenharmony_cisf_count_t sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames) ; 477b815c7f3Sopenharmony_ci``` 478b815c7f3Sopenharmony_ci 479b815c7f3Sopenharmony_ciThe file write functions write the data in the array pointed to by ptr to the 480b815c7f3Sopenharmony_cifile. 481b815c7f3Sopenharmony_ci 482b815c7f3Sopenharmony_ciFor items-count functions, the items parameter specifies the size of the array 483b815c7f3Sopenharmony_ciand must be an integer product of the number of channels or an error will occur. 484b815c7f3Sopenharmony_ci 485b815c7f3Sopenharmony_ciFor the frames-count functions, the array is expected to be large enough to hold 486b815c7f3Sopenharmony_cia number of items equal to the product of frames and the number of channels. 487b815c7f3Sopenharmony_ci 488b815c7f3Sopenharmony_ciAs with the read functions [above](#read), the only difference in the items and 489b815c7f3Sopenharmony_ciframes version of each write function is the units in which the buffer size is 490b815c7f3Sopenharmony_cispecified. Again, the data type used by the calling program and the data format 491b815c7f3Sopenharmony_ciof the file do not need to be the same ([Note 1](#note-1)). 492b815c7f3Sopenharmony_ci 493b815c7f3Sopenharmony_ciThe sf_write_XXXX and sf_writef_XXXX functions respectively return the number of 494b815c7f3Sopenharmony_ciitems or frames written (which should be the same as the items or frames 495b815c7f3Sopenharmony_ciparameter). 496b815c7f3Sopenharmony_ci 497b815c7f3Sopenharmony_ci## Raw File Read and Write Functions {#raw} 498b815c7f3Sopenharmony_ci 499b815c7f3Sopenharmony_ci```c 500b815c7f3Sopenharmony_cisf_count_t sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ; 501b815c7f3Sopenharmony_cisf_count_t sf_write_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes) ; 502b815c7f3Sopenharmony_ci``` 503b815c7f3Sopenharmony_ci 504b815c7f3Sopenharmony_ci**Note:** Unless you are writing an external decoder/encode that uses libsndfile 505b815c7f3Sopenharmony_cito handle the file headers, you should not be using these functions. 506b815c7f3Sopenharmony_ci 507b815c7f3Sopenharmony_ciThe raw read and write functions read raw audio data from the audio file (not to 508b815c7f3Sopenharmony_cibe confused with reading RAW header-less PCM files). The number of bytes read or 509b815c7f3Sopenharmony_ciwritten must always be an integer multiple of the number of channels multiplied 510b815c7f3Sopenharmony_ciby the number of bytes required to represent one sample from one channel. 511b815c7f3Sopenharmony_ci 512b815c7f3Sopenharmony_ciThe raw read and write functions return the number of bytes read or written 513b815c7f3Sopenharmony_ci(which should be the same as the bytes parameter). 514b815c7f3Sopenharmony_ci 515b815c7f3Sopenharmony_ci**Note : The result of using of both regular reads/writes and raw reads/writes 516b815c7f3Sopenharmony_cion compressed file formats other than SF_FORMAT_ALAW and SF_FORMAT_ULAW is 517b815c7f3Sopenharmony_ciundefined.** 518b815c7f3Sopenharmony_ci 519b815c7f3Sopenharmony_ciSee also : [SFC_RAW_NEEDS_ENDSWAP](command.md#sfc_raw_needs_endswap). 520b815c7f3Sopenharmony_ci 521b815c7f3Sopenharmony_ci## Functions for Reading and Writing String Data {#string} 522b815c7f3Sopenharmony_ci 523b815c7f3Sopenharmony_ci```c 524b815c7f3Sopenharmony_ciconst char* sf_get_string (SNDFILE *sndfile, int str_type) ; 525b815c7f3Sopenharmony_ciint sf_set_string (SNDFILE *sndfile, int str_type, const char* str) ; 526b815c7f3Sopenharmony_ci``` 527b815c7f3Sopenharmony_ci 528b815c7f3Sopenharmony_ciThese functions allow strings to be set on files opened for write and to be 529b815c7f3Sopenharmony_ciretrieved from files opened for read where supported by the given file type. The 530b815c7f3Sopenharmony_ci**str_type** parameter can be any one of the following string types: 531b815c7f3Sopenharmony_ci 532b815c7f3Sopenharmony_ci| Name | Value | Description | 533b815c7f3Sopenharmony_ci|:-------------------|:------|:--------------| 534b815c7f3Sopenharmony_ci| SF_STR_TITLE | 0x01 | Title. | 535b815c7f3Sopenharmony_ci| SF_STR_COPYRIGHT | 0x02 | Copyright. | 536b815c7f3Sopenharmony_ci| SF_STR_SOFTWARE | 0x03 | Software. | 537b815c7f3Sopenharmony_ci| SF_STR_ARTIST | 0x04 | Artist. | 538b815c7f3Sopenharmony_ci| SF_STR_COMMENT | 0x05 | Comment. | 539b815c7f3Sopenharmony_ci| SF_STR_DATE | 0x06 | Date. | 540b815c7f3Sopenharmony_ci| SF_STR_ALBUM | 0x07 | Album. | 541b815c7f3Sopenharmony_ci| SF_STR_LICENSE | 0x08 | License. | 542b815c7f3Sopenharmony_ci| SF_STR_TRACKNUMBER | 0x09 | Track number. | 543b815c7f3Sopenharmony_ci| SF_STR_GENRE | 0x10 | Genre. | 544b815c7f3Sopenharmony_ci 545b815c7f3Sopenharmony_ciThe sf_get_string() function returns the specified string if it exists and a 546b815c7f3Sopenharmony_ciNULL pointer otherwise. In addition to the string ids above, SF_STR_FIRST (== 547b815c7f3Sopenharmony_ciSF_STR_TITLE) and SF_STR_LAST (always the same as the highest numbers string id) 548b815c7f3Sopenharmony_ciare also available to allow iteration over all the available string ids. 549b815c7f3Sopenharmony_ci 550b815c7f3Sopenharmony_ciThe sf_set_string() function sets the string data. It returns zero on success 551b815c7f3Sopenharmony_ciand non-zero on error.The error code can be converted to a string using 552b815c7f3Sopenharmony_cisf_error_number(). 553b815c7f3Sopenharmony_ci 554b815c7f3Sopenharmony_ciStrings passed to and retrieved from these two functions are assumed to be 555b815c7f3Sopenharmony_ciutf-8. However, while formats like Ogg/Vorbis and FLAC fully support utf-8, 556b815c7f3Sopenharmony_ciothers like WAV and AIFF officially only support ASCII. Writing utf-8 strings to 557b815c7f3Sopenharmony_ciWAV and AIF files with libsndfile will work when read back with libsndfile, but 558b815c7f3Sopenharmony_cimay not work with other programs. 559b815c7f3Sopenharmony_ci 560b815c7f3Sopenharmony_ciThe suggested method of dealing with tags retrived using sf_get_string() is to 561b815c7f3Sopenharmony_ciassume they are utf-8. Similarly if you have a string in some exotic format like 562b815c7f3Sopenharmony_ciutf-16, it should be encoded to utf-8 before being written using libsndfile. 563b815c7f3Sopenharmony_ci 564b815c7f3Sopenharmony_ci## Function for retrieving library version {#version_string} 565b815c7f3Sopenharmony_ci 566b815c7f3Sopenharmony_ci```c 567b815c7f3Sopenharmony_ciconst char *sf_version_string (void) ; 568b815c7f3Sopenharmony_ci``` 569b815c7f3Sopenharmony_ci 570b815c7f3Sopenharmony_ciReturn the library version string. 571b815c7f3Sopenharmony_ci 572b815c7f3Sopenharmony_ci## Function for retrieving current byterate {#current_byterate} 573b815c7f3Sopenharmony_ci 574b815c7f3Sopenharmony_ci```c 575b815c7f3Sopenharmony_ciint sf_current_byterate (SNDFILE *sndfile) ; 576b815c7f3Sopenharmony_ci``` 577b815c7f3Sopenharmony_ci 578b815c7f3Sopenharmony_ciReturn the current byterate at this point in the file. The byte rate in this 579b815c7f3Sopenharmony_cicase is the number of bytes per second of audio data. For instance, for a 580b815c7f3Sopenharmony_cistereo, 18 bit PCM encoded file with an 16kHz sample rate, the byte rate 581b815c7f3Sopenharmony_ciwould be 2 (stereo) \* 2 (two bytes per sample) * 16000 => 64000 bytes/sec. 582b815c7f3Sopenharmony_ci 583b815c7f3Sopenharmony_ciFor some file formats the returned value will be accurate and exact, for some 584b815c7f3Sopenharmony_ciit will be a close approximation, for some it will be the average bitrate for 585b815c7f3Sopenharmony_cithe whole file and for some it will be a time varying value that was accurate 586b815c7f3Sopenharmony_ciwhen the file was most recently read or written. 587b815c7f3Sopenharmony_ci 588b815c7f3Sopenharmony_ciTo get the bitrate, multiple this value by 8. 589b815c7f3Sopenharmony_ci 590b815c7f3Sopenharmony_ci`sf_current_byterate` returns byte per second or -1 if byterate is 591b815c7f3Sopenharmony_ciunknown. 592b815c7f3Sopenharmony_ci 593b815c7f3Sopenharmony_ci## Functions to get and set chunks from within a sound file 594b815c7f3Sopenharmony_ci 595b815c7f3Sopenharmony_ciThese functions allow the getting and setting of chunks within a sound file (for 596b815c7f3Sopenharmony_cithose formats which allow it). 597b815c7f3Sopenharmony_ci 598b815c7f3Sopenharmony_ciThese functions fail safely. Specifically, they will not allow you to overwrite 599b815c7f3Sopenharmony_ciexisting chunks or add extra versions of format specific reserved chunks but 600b815c7f3Sopenharmony_cishould allow you to retrieve any and all chunks (may not be implemented for all 601b815c7f3Sopenharmony_cichunks or all file formats). 602b815c7f3Sopenharmony_ci 603b815c7f3Sopenharmony_ci### sf_set_chunk 604b815c7f3Sopenharmony_ci 605b815c7f3Sopenharmony_ci```c 606b815c7f3Sopenharmony_ciint sf_set_chunk (SNDFILE *sndfile, const SF_CHUNK_INFO *chunk_info) ; 607b815c7f3Sopenharmony_ci``` 608b815c7f3Sopenharmony_ci 609b815c7f3Sopenharmony_ciSet the specified chunk info (must be done before any audio data is written to 610b815c7f3Sopenharmony_cithe file). This will fail for format specific reserved chunks. The 611b815c7f3Sopenharmony_ci`chunk_info->data` pointer must be valid until the file is closed. 612b815c7f3Sopenharmony_ci 613b815c7f3Sopenharmony_ciThe `SF_CHUNK_INFO` struct is documented as follows: 614b815c7f3Sopenharmony_ci 615b815c7f3Sopenharmony_ci```c 616b815c7f3Sopenharmony_cistruct SF_CHUNK_INFO 617b815c7f3Sopenharmony_ci{ char id [64] ; /* The chunk identifier. */ 618b815c7f3Sopenharmony_ci unsigned id_size ; /* The size of the chunk identifier. */ 619b815c7f3Sopenharmony_ci unsigned datalen ; /* The size of that data. */ 620b815c7f3Sopenharmony_ci void *data ; /* Pointer to the data. */ 621b815c7f3Sopenharmony_ci} ; 622b815c7f3Sopenharmony_ci typedef struct SF_CHUNK_INFO SF_CHUNK_INFO ; 623b815c7f3Sopenharmony_ci``` 624b815c7f3Sopenharmony_ci 625b815c7f3Sopenharmony_ci`sf_set_chunk` returns `SF_ERR_NO_ERROR` on success or non-zero on failure. 626b815c7f3Sopenharmony_ci 627b815c7f3Sopenharmony_ci### sf_get_chunk_iterator 628b815c7f3Sopenharmony_ci 629b815c7f3Sopenharmony_ci```c 630b815c7f3Sopenharmony_ciSF_CHUNK_ITERATOR * 631b815c7f3Sopenharmony_cisf_get_chunk_iterator (SNDFILE *sndfile, const SF_CHUNK_INFO *chunk_info) ; 632b815c7f3Sopenharmony_ci``` 633b815c7f3Sopenharmony_ci 634b815c7f3Sopenharmony_ciGet an iterator for all chunks matching `chunk_info`. 635b815c7f3Sopenharmony_ci 636b815c7f3Sopenharmony_ci`SF_CHUNK_ITERATOR` is an opaque structure to an iterator over the all chunks of 637b815c7f3Sopenharmony_cia given id and defined as follows: 638b815c7f3Sopenharmony_ci 639b815c7f3Sopenharmony_ci```c 640b815c7f3Sopenharmony_citypedef struct SF_CHUNK_ITERATOR SF_CHUNK_ITERATOR ; 641b815c7f3Sopenharmony_ci``` 642b815c7f3Sopenharmony_ci 643b815c7f3Sopenharmony_ciThe iterator will point to the first chunk matching `chunk_info`. Chunks are 644b815c7f3Sopenharmony_cimatching, if (`chunk_info->id`) matches the first (`chunk_info->id_size`) bytes 645b815c7f3Sopenharmony_ciof a chunk found in the `SNDFILE*` handle. If `chunk_info` is `NULL`, an 646b815c7f3Sopenharmony_ciiterator to all chunks in the `SNDFILE*` handle is returned. The values of 647b815c7f3Sopenharmony_ci`chunk_info->datalen` and `chunk_info->data` are ignored. If no matching chunks 648b815c7f3Sopenharmony_ciare found in the sndfile, `NULL` is returned. 649b815c7f3Sopenharmony_ci 650b815c7f3Sopenharmony_ciThe returned iterator will stay valid until one of the following occurs: 651b815c7f3Sopenharmony_ci 652b815c7f3Sopenharmony_ci* The sndfile is closed. 653b815c7f3Sopenharmony_ci* A new chunk is added using [`sf_set_chunk()`](#sf_set_chunk). 654b815c7f3Sopenharmony_ci* Another chunk iterator function is called on the same `SNDFILE*` 655b815c7f3Sopenharmony_ci handle that causes the iterator to be modified. 656b815c7f3Sopenharmony_ci 657b815c7f3Sopenharmony_ciThe memory for the iterator belongs to the SNDFILE* handle and is freed when 658b815c7f3Sopenharmony_ci[sf_close](#close) is called. 659b815c7f3Sopenharmony_ci 660b815c7f3Sopenharmony_ci### sf_next_chunk_iterator 661b815c7f3Sopenharmony_ci 662b815c7f3Sopenharmony_ci```c 663b815c7f3Sopenharmony_cisf_next_chunk_iterator (SF_CHUNK_ITERATOR * iterator) ; 664b815c7f3Sopenharmony_ci``` 665b815c7f3Sopenharmony_ci 666b815c7f3Sopenharmony_ciIterate through chunks by incrementing the iterator. 667b815c7f3Sopenharmony_ci 668b815c7f3Sopenharmony_ciIncrements the iterator and returns a handle to the new one. After this call, 669b815c7f3Sopenharmony_ciiterator will no longer be valid, and you must use the newly returned handle 670b815c7f3Sopenharmony_cifrom now on. The returned handle can be used to access the next chunk matching 671b815c7f3Sopenharmony_cithe criteria as defined in [sf_get_chunk_iterator](#sf_get_chunk_iterator). 672b815c7f3Sopenharmony_ciIf iterator points to the last chunk, this will free all resources associated 673b815c7f3Sopenharmony_ciwith iterator and return `NULL`. The returned iterator will stay valid until 674b815c7f3Sopenharmony_ci`sf_get_next_chunk_iterator` is called again, the sndfile is closed or a new 675b815c7f3Sopenharmony_cichunk us added. 676b815c7f3Sopenharmony_ci 677b815c7f3Sopenharmony_ci### sf_get_chunk_size 678b815c7f3Sopenharmony_ci 679b815c7f3Sopenharmony_ci```c 680b815c7f3Sopenharmony_ciint 681b815c7f3Sopenharmony_cisf_get_chunk_size (const SF_CHUNK_ITERATOR * it, SF_CHUNK_INFO * chunk_info) ; 682b815c7f3Sopenharmony_ci``` 683b815c7f3Sopenharmony_ci 684b815c7f3Sopenharmony_ciGet the size of the specified chunk. 685b815c7f3Sopenharmony_ci 686b815c7f3Sopenharmony_ciIf the specified chunk exists, the size will be returned in the `datalen` field 687b815c7f3Sopenharmony_ciof the `SF_CHUNK_INFO` struct. Additionally, the id of the chunk will be copied 688b815c7f3Sopenharmony_cito the `id` field of the `SF_CHUNK_INFO` struct and it's `id_size` field will be 689b815c7f3Sopenharmony_ciupdated accordingly. 690b815c7f3Sopenharmony_ci 691b815c7f3Sopenharmony_ciIf the chunk doesn't exist `chunk_info->datalen` will be zero, and the `id` and 692b815c7f3Sopenharmony_ci`id_size` fields will be undefined. 693b815c7f3Sopenharmony_ci 694b815c7f3Sopenharmony_ciThe function will return `SF_ERR_NO_ERROR` on success or non-zero on failure. 695b815c7f3Sopenharmony_ci 696b815c7f3Sopenharmony_ci### sf_get_chunk_data 697b815c7f3Sopenharmony_ci 698b815c7f3Sopenharmony_ci```c 699b815c7f3Sopenharmony_ciint 700b815c7f3Sopenharmony_cisf_get_chunk_data (const SF_CHUNK_ITERATOR *it, SF_CHUNK_INFO *chunk_info) ; 701b815c7f3Sopenharmony_ci``` 702b815c7f3Sopenharmony_ci 703b815c7f3Sopenharmony_ciGet the specified chunk data. 704b815c7f3Sopenharmony_ci 705b815c7f3Sopenharmony_ciIf the specified chunk exists, up to `chunk_info->datalen` bytes of the chunk 706b815c7f3Sopenharmony_cidata will be copied into the `chunk_info->data` buffer (allocated by the caller) 707b815c7f3Sopenharmony_ciand the `chunk_info->datalen` field updated to reflect the size of the data. The 708b815c7f3Sopenharmony_ci`id` and `id_size` field will be updated according to the retrieved chunk. If 709b815c7f3Sopenharmony_cithe chunk doesn't exist `chunk_info->datalen` will be zero, and the `id` and 710b815c7f3Sopenharmony_ci`id_size` fields will be undefined. 711b815c7f3Sopenharmony_ci 712b815c7f3Sopenharmony_ciThe function will return `SF_ERR_NO_ERROR` on success or non-zero on failure. 713b815c7f3Sopenharmony_ci 714b815c7f3Sopenharmony_ci## Note 1 715b815c7f3Sopenharmony_ci 716b815c7f3Sopenharmony_ciWhen converting between integer PCM formats of differing size (e.g. using 717b815c7f3Sopenharmony_cisf_read_int() to read a 16 bit PCM encoded WAV file) libsndfile obeys one simple 718b815c7f3Sopenharmony_cirule: 719b815c7f3Sopenharmony_ci 720b815c7f3Sopenharmony_ciWhenever integer data is moved from one sized container to another sized 721b815c7f3Sopenharmony_cicontainer, the most significant bit in the source container will become the most 722b815c7f3Sopenharmony_cisignificant bit in the destination container. 723b815c7f3Sopenharmony_ci 724b815c7f3Sopenharmony_ciWhen converting between integer data and floating point data, different rules 725b815c7f3Sopenharmony_ciapply. The default behaviour when reading floating point data (sf_read_float() 726b815c7f3Sopenharmony_cior sf_read_double ()) from a file with integer data is normalisation. Regardless 727b815c7f3Sopenharmony_ciof whether data in the file is 8, 16, 24 or 32 bit wide, the data will be read 728b815c7f3Sopenharmony_cias floating point data in the range [-1.0, 1.0]. Similarly, data in the range 729b815c7f3Sopenharmony_ci[-1.0, 1.0] will be written to an integer PCM file so that a data value of 1.0 730b815c7f3Sopenharmony_ciwill be the largest allowable integer for the given bit width. This 731b815c7f3Sopenharmony_cinormalisation can be turned on or off using the [sf_command](command.md) 732b815c7f3Sopenharmony_ciinterface. 733b815c7f3Sopenharmony_ci 734b815c7f3Sopenharmony_ci## Note 2 735b815c7f3Sopenharmony_ci 736b815c7f3Sopenharmony_ciReading a file containg floating point data (allowable with WAV, AIFF, AU and 737b815c7f3Sopenharmony_ciother file formats) using integer read methods (sf_read_short() or 738b815c7f3Sopenharmony_cisf_read_int()) can produce unexpected results. For instance the data in the file 739b815c7f3Sopenharmony_cimay have a maximum absolute value < 1.0 which would mean that all sample 740b815c7f3Sopenharmony_civalues read from the file will be zero. In order to read these files correctly 741b815c7f3Sopenharmony_ciusing integer read methods, it is recommended that you use the 742b815c7f3Sopenharmony_ci[sf_command](command.md) interface, a command of 743b815c7f3Sopenharmony_ci[SFC_SET_SCALE_FLOAT_INT_READ](command.md#sfc_set_scale_float_int_read) and a 744b815c7f3Sopenharmony_ciparameter of SF_TRUE to force correct scaling. 745