1--- 2layout: default 3title: libsndfile : Frequently Asked Questions. 4--- 5 6# libsndfile : Frequently Asked Questions 7 81. [Do you plan to support XYZ codec in libsndfile?](#Q001) 92. [In version 0 the SF\_INFO struct had a pcmbitwidth field but version 1 does not. Why?](#Q002) 103. [Compiling is really slow on MacOS X. Why?](#Q003) 114. [When trying to compile libsndfile on Solaris I get a "bad substitution" error during linking. What can I do to fix this?](#Q004) 125. [Why doesn't libsndfile do interleaving/de-interleaving?](#Q005) 136. [What's the best format for storing temporary files?](#Q006) 147. [On Linux/Unix/MacOS X, what's the best way of detecting the presence of libsndfile?](#Q007) 158. [I have libsndfile installed and now I want to use it. I just want a simple Makefile\! What do I do?](#Q008) 169. [How about adding the ability to write/read sound files to/from memory buffers?](#Q009) 1710. [Reading a 16 bit PCM file as normalised floats and then writing them back changes some sample values. Why?](#Q010) 1811. [I'm having problems with u-law encoded WAV files generated by libsndfile in Winamp. Why?](#Q011) 1912. [I'm looking at sf\_read\*. What are items? What are frames?](#Q012) 2013. [Why can't libsndfile open this Sound Designer II (SD2) file?](#Q013) 2114. [I'd like to statically link libsndfile to my closed source application. Can I buy a license so that this is possible?](#Q014) 2215. [My program is crashing during a call to a function in libsndfile. Is this a bug in libsndfile?](#Q015) 2316. [Will you accept a fix for compiling libsndfile with compiler X?](#Q016) 2417. [Can libsndfile read/write files from/to UNIX pipes?](#Q017) 2518. [Is it possible to build a Universal Binary on Mac OS X?](#Q018) 2619. [I have project files for Visual Studio / XCode / Whatever. Why don't you distribute them with libsndfile?](#Q019) 2720. [Why doesn't libsndfile support MP3?](#Q020) 2821. [How do I use libsndfile in a closed source or commercial program and comply with the license?](#Q021) 2922. [What versions of windows does libsndfile work on?](#Q022) 3023. [I'm cross compiling libsndfile for another platform. How can I run the test suite?](#Q023) 31 32----- 33 34## Q1 : Do you plan to support XYZ codec in libsndfile? {#Q001} 35 36If source code for XYZ codec is available under a suitable license (LGPL, BSD, 37MIT etc) then yes, I'd like to add it. 38 39If suitable documentation is available on how to decode and encode the format 40then maybe, depending on how much work is involved. 41 42If XYZ is some proprietary codec where no source code or documentation is 43available then no. 44 45So if you want support for XYZ codec, first find existing source code or 46documentation. If you can't find either then the answer is no. 47 48## Q2 : In version 0 the SF\_INFO struct had a pcmbitwidth field but version 1 does not. Why? {#Q002} 49 50This was dropped for a number of reasons: 51 52- pcmbitwidth makes little sense on compressed or floating point formats 53- with the new API you really don't need to know it 54 55As documented [here](api.md#note-1) there is now a well defined behaviour which 56ensures that no matter what the bit width of the source file, the scaling always 57does something sensible. This makes it safe to read 8, 16, 24 and 32 bit PCM 58files using `sf_read_short()` and always have the optimal behaviour. 59 60## Q3 : Compiling is really slow on MacOS X. Why? {#Q003} 61 62When you configure and compile libsndfile, it uses the /bin/sh shell for a 63number of tasks (ie configure script and libtool). Older versions of OS X 64(10.2?) shipped a really crappy Bourne shell as /bin/sh which resulted in 65**really** slow compiles. Newer version of OS X ship GNU Bash as /bin/sh and 66this answer doesn't apply in that case. 67 68To fix this I suggest that you install the GNU Bash shell, rename /bin/sh to 69/bin/sh.old and make a symlink from /bin/sh to the bash shell. Bash is designed 70to behave as a Bourne shell when it is called as /bin/sh. 71 72When I did this on my iBook running MacOS X, compile times dropped from 13 73minutes to 3 minutes. 74 75## Q4 : When trying to compile libsndfile on Solaris I get a "bad substitution" error on linking. Why? {#Q004} 76 77It seems that the Solaris Bourne shell disagrees with GNU libtool. 78 79To fix this I suggest that you install the GNU Bash shell, rename /bin/sh to 80/bin/sh.old and make a symlink from /bin/sh to the bash shell. Bash is designed 81to behave as a Bourne shell when it is called as /bin/sh. 82 83## Q5 : Why doesn't libsndfile do interleaving/de-interleaving? {#Q005} 84 85This problem is bigger than it may seem at first. 86 87For a stereo file, it is a pretty safe bet that a simple interleaving/ 88de-interleaving could satisfy most users. However, for files with more than 2 89channels this is unlikely to be the case. If the user has a 4 channel file and 90want to play that file on a stereo output sound card they either want the first 912 channels or they want some mixed combination of the 4 channels. 92 93When you add more channels, the combinations grow exponentially and it becomes 94increasingly difficult to cover even a sensible subset of the possible 95combinations. On top of that, coding any one style of interleaver/de-interleaver 96is trivial, while coding one that can cover all combinations is far from 97trivial. This means that this feature will not be added any time soon. 98 99## Q6 : What's the best format for storing temporary files? {#Q006} 100 101When you want to store temporary data there are a number of requirements: 102 103- A simple, easy to parse header. 104- The format must provide the fastest possible read and write rates (ie avoid 105 conversions and encoding/decoding). 106- The file format must be reasonably common and playable by most players. 107- Able to store data in either endian-ness. 108 109The format which best meets these requirements is AU, which allows data to be 110stored in any one of short, int, float and double (among others) formats. 111 112For instance, if an application uses float data internally, its temporary files 113should use a format of (SF_ENDIAN_CPU | SF_FORMAT_AU | SF_FORMAT_FLOAT) which 114will store big endian float data in big endian CPUs and little endian float data 115on little endian CPUs. Reading and writing this format will not require any 116conversions or byte swapping regardless of the host CPU. 117 118## Q7 : On Linux/Unix/MaxOS X, what's the best way of detecting the presence of libsndfile using autoconf? {#Q007} 119 120libsndfile uses the pkg-config (man pkg-config) method of registering itself 121with the host system. The best way of detecting its presence is using something 122like this in configure.ac (or configure.in): 123 124 PKG_CHECK_MODULES(SNDFILE, sndfile >= 1.0.2, ac_cv_sndfile=1, ac_cv_sndfile=0) 125 126 AC_DEFINE_UNQUOTED([HAVE_SNDFILE],${ac_cv_sndfile}, 127 [Set to 1 if you have libsndfile.]) 128 129 AC_SUBST(SNDFILE_CFLAGS) 130 AC_SUBST(SNDFILE_LIBS) 131 132This will automatically set the **SNDFILE_CFLAGS** and **SNDFILE_LIBS** 133variables which can be used in Makefile.am like this: 134 135 SNDFILE_CFLAGS = @SNDFILE_CFLAGS@ 136 SNDFILE_LIBS = @SNDFILE_LIBS@ 137 138If you install libsndfile from source, you will probably need to set the 139**PKG_CONFIG_PATH** environment variable as suggested at the end of the 140libsndfile configure process. For instance on my system I get this: 141 142 -=-=-=-=-=-=-=-=-=-= Configuration Complete =-=-=-=-=-=-=-=-=-=- 143 144 Configuration summary : 145 146 Version : ..................... 1.0.5 147 Experimental code : ........... no 148 149 Tools : 150 151 Compiler is GCC : ............. yes 152 GCC major version : ........... 3 153 154 Installation directories : 155 156 Library directory : ........... /usr/local/lib 157 Program directory : ........... /usr/local/bin 158 Pkgconfig directory : ......... /usr/local/lib/pkgconfig 159 160 Compiling some other packages against libsndfile may require 161 the addition of "/usr/local/lib/pkgconfig" to the 162 PKG_CONFIG_PATH environment variable. 163 164## Q8 : I have libsndfile installed and now I want to use it. I just want a simple Makefile\! What do I do? {#Q008} 165 166The **pkg-config** program makes finding the correct compiler flag values and 167library location far easier. During the installation of libsndfile, a file named 168**sndfile.pc** is installed in the directory **${libdir}/pkgconfig** (ie if 169libsndfile is installed in **/usr/local/lib**, **sndfile.pc** will be installed 170in **/usr/local/lib/pkgconfig/**). 171 172In order for pkg-config to find sndfile.pc it may be necessary to point the 173environment variable **PKG_CONFIG_PATH** in the right direction. 174 175 export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig 176 177Then, to compile a C file into an object file, the command would be: 178 179 gcc `pkg-config --cflags sndfile` -c somefile.c 180 181and to link a number of objects into an executable that links against 182libsndfile, the command would be: 183 184 gcc `pkg-config --libs sndfile` obj1.o obj2.o -o program 185 186## Q9 : How about adding the ability to write/read sound files to/from memory buffers? {#Q009} 187 188This has been [added](api.md#open_virtual) for version 1.0.12. 189 190## Q10 : Reading a 16 bit PCM file as normalised floats and then writing them back changes some sample values. Why? {#Q010} 191 192This is caused by the fact that the conversion from 16 bit short to float is 193done by dividing by 32768 (0x8000 in hexadecimal) while the conversion from 194float to 16 bit short is done by multiplying by 32767 (0x7FFF in hex). So for 195instance, a value in a 16 bit PCM file of 20000 gets read as a floating point 196number of 0.6103515625 (20000.0 / 0x8000). Converting that back to a 16 bit 197short results in a value of 19999.3896484375 (0.6103515625 \* 0x7FFF) which then 198gets rounded down to 19999. 199 200You will notice that for this particular case, the error is 1 in 20000 or 2010.005%. Interestingly, for values of less than 16369, dividing by 0x8000 202followed by multiplying by 0x7FFF and then rounding the result, gives back the 203original value. It turns out that as long as the host operating system supplies 204the 1999 ISO C Standard functions **lrintf** and **lrint** (or a replacement has 205been supplied) then the maximum possible error is 1 in 16369 or about 0.006%. 206 207Regardless of the size of the error, the reason why this is done is rather 208subtle. 209 210In a file containing 16 bit PCM samples, the values are restricted to the range 211[-32768, 32767] while we want floating point values in the range [-1.0, 1.0]. 212The only way to do this conversion is to do a floating point division by a value 213of 0x8000. Converting the other way, the only way to ensure that floating point 214values in the range [-1.0, 1.0] are within the valid range allowed by a 16 bit 215short is to multiply by 0x7FFF. 216 217Some people would say that this is a severe short-coming of libsndfile. I would 218counter that anybody who is constantly converting back and forth between 16 bit 219shorts and normalised floats is going to suffer other losses in audio quality 220that they should also be concerned about. 221 222Since this problem only occurs when converting between integer data on disk and 223normalized floats in the application, it can be avoided by using something other 224than normalized floats in the application. Alternatives to normalized floats are 225the **short** and **int** data types (ie using sf_read_short or sf_read_int) or 226using un-normalized floats (see 227[SFC_SET_NORM_FLOAT](command.html#sfc_set_norm_float)). 228 229Another way to deal with this problem is to consider 16 bit short data as a 230final destination format only, not as an intermediate storage format. All 231intermediate data (ie which is going to be processed further) should be stored 232in floating point format which is supported by all of the most common file 233formats. If floating point files are considered too large (2 times the size of a 23416 bit PCM file), it would also be possible to use 24 bit PCM as an intermediate 235storage format (and which is also supported by most common file types). 236 237## Q11 : I'm having problems with u-law encoded WAV files generated by libsndfile in Winamp. Why? {#Q011} 238 239This is actually a Winamp problem. The official Microsoft spec suggests that the 240'fmt ' chunk should be 18 bytes. Unfortunately at least one of Microsoft's own 241applications (Sound Recorder on Win98 I believe) did not accept 18 bytes 'fmt ' 242chunks. 243 244Michael Lee did some experimenting and found that: 245 246> I have checked that Windows Media Player 9, QuickTime Player 6.4, RealOne 247> Player 2.0 and GoldWave 5.06 can all play u-law files with 16-byte or 18-byte 248> 'fmt ' chunk. Only Winamp (2.91) and foobar2000 are unable to play u-law files 249> with 16-byte 'fmt ' chunk. 250 251Even this is a very small sampling of all the players out there. For that reason 252it is probably not a good idea to change this now because there is the risk of 253breaking something that currently works. 254 255## Q12 : I'm looking at sf_read*. What are items? What are frames? {#Q012} 256 257An `item` is a single sample of the data type you are reading; ie a single 258`short` value for `sf_read_short` or a single `float` for `sf_read_float`. 259 260For a sound file with only one channel, a frame is the same as a item (ie a 261single sample) while for multi channel sound files, a single frame contains a 262single item for each channel. 263 264Here are two simple, correct examples, both of which are assumed to be working 265on a stereo file, first using items: 266 267```c 268#define CHANNELS 2 269short data [CHANNELS * 100] ; 270sf_count items_read = sf_read_short (file, data, 200) ; 271assert (items_read == 200) ; 272``` 273 274and now reading the exact same amount of data using frames: 275 276```c 277#define CHANNELS 2 278short data [CHANNELS * 100] ; 279sf_count frames_read = sf_readf_short (file, data, 100) ; 280assert (frames_read == 100) ; 281``` 282 283## Q13 : Why can't libsndfile open this Sound Designer II (SD2) file? {#Q013} 284 285This is somewhat complicated. First some background. 286 287SD2 files are native to the Apple Macintosh platform and use features of the Mac 288filesystem (file resource forks) to store the file's sample rate, number of 289channels, sample width and more. When you look at a file and its resource fork 290on Mac OS X it looks like this: 291 292 -rw-r--r-- 1 erikd erikd 46512 Oct 18 22:57 file.sd2 293 -rw-r--r-- 1 erikd erikd 538 Oct 18 22:57 file.sd2/rsrc 294 295Notice how the file itself looks like a directory containing a single file named 296**rsrc**. When libsndfile is compiled for MacOS X, it should open (for write and 297read) SD2 file with resource forks like this without any problems. It will also 298handle files with the resource fork in a separate file as described below. 299 300When SD2 files are moved to other platforms, the resource fork of the file can 301sometimes be dropped altogether. All that remains is the raw audio data and no 302information about the number of channels, sample rate or bit width which makes 303it a little difficult for libsndfile to open the file. 304 305However, it is possible to safely move an SD2 file to a Linux or Windows 306machine. For instance, when an SD2 file is copied from inside MacOS X to a 307windows shared directory or a Samba share (ie Linux), MacOS X is clever enough 308to store the resource fork of the file in a separate hidden file in the same 309directory like this: 310 311 -rw-r--r-- 1 erikd erikd 538 Oct 18 22:57 ._file.sd2 312 -rw-r--r-- 1 erikd erikd 46512 Oct 18 22:57 file.sd2 313 314Regardless of what platform it is running on, when libsndfile is asked to open a 315file named **"foo"** and it can't recognize the file type from the data in the 316file, it will attempt to open the resource fork and if that fails, it then tries 317to open a file named **"._foo"** to see if the file has a valid resource fork. 318This is the same regardless of whether the file is being opened for read or 319write. 320 321In short, libsndfile should open SD2 files with a valid resource fork on all of 322the platforms that libsndfile supports. If a file has lost its resource fork, 323the only option is the open the file using the SF_FORMAT_RAW option and guessing 324its sample rate, channel count and bit width. 325 326Occasionally, when SD2 files are moved to other systems, the file is 327[BinHexed](http://www.macdisk.com/binhexen.php3) which wraps the resource fork 328and the data fork together. For these files, it would be possible to write a 329BinHex parser but there is not a lot to gain considering how rare these BinHexed 330SD2 files are. 331 332## Q14 : I'd like to statically link libsndfile to my closed source application. Can I buy a license so that this is possible? {#Q014} 333 334Unfortunately no. libsndfile contains code written by other people who have 335agreed that their code be used under the GNU LGPL but no more. Even if they were 336to agree, there would be significant difficulties in dividing up the payments 337fairly. 338 339The **only** way you can legally use libsndfile as a statically linked library 340is if your application is released under the GNU GPL or LGPL. 341 342## Q15 : My program is crashing during a call to a function in libsndfile. Is this a bug in libsndfile? {#Q015} 343 344libsndfile is being used by large numbers of people all over the world without 345any problems like this. That means that it is much more likely that your code 346has a bug than libsndfile. However, it is still possible that there is a bug in 347libsndfile. 348 349To figure out whether it is your code or libsndfile you should do the following: 350 351- Make sure you are compiling your code with warnings switched on and that you 352 fix as many warnings as possible. With the GNU compiler (gcc) I would 353 recommend at least **-W -Wall -Werror** which will force you to fix all 354 warnings before you can run the code. 355- Try using a memory debugger. [Valgrind](http://valgrind.kde.org/) on x86 Linux 356 is excellent. [Purify](http://www.ibm.com/software/awdtools/purify/) also has 357 a good reputation. 358- If the code is clean after the above two steps and you still get a crash in 359 libsndfile, then send me a small snippet of code (no more than 30-40 lines) 360 which includes the call to sf_open() and also shows how all variables passed 361 to/returned from sf_open() are defined. 362 363## Q16 : Will you accept a fix for compiling libsndfile with compiler X? {#Q016} 364 365If compiler X is a C++ compiler then no. C and C++ are different enough to make 366writing code that compiles as valid C and valid C++ too difficult. I would 367rather spend my time fixing bugs and adding features. 368 369If compiler X is a C compiler then I will do what I can as long as that does not 370hamper the correctness, portability and maintainability of the existing code. It 371should be noted however that libsndfile uses features specified by the 1999 ISO 372C Standard. This can make compiling libsndfile with some older compilers 373difficult. 374 375## Q17 : Can libsndfile read/write files from/to UNIX pipes? {#Q017} 376 377Yes, libsndfile can read files from pipes. Unfortunately, the write case is much 378more complicated. 379 380File formats like AIFF and WAV have information at the start of the file (the 381file header) which states the length of the file, the number of sample frames 382etc. This information must be filled in correctly when the file header is 383written, but this information is not reliably known until the file is closed. 384This means that libsndfile cannot write AIFF, WAV and many other file types to a 385pipe. 386 387However, there is at least one file format (AU) which is specifically designed 388to be written to a pipe. Like AIFF and WAV, AU has a header with a sample frames 389field, but it is specifically allowable to set that frames field to 0x7FFFFFFF 390if the file length is not known when the header is written. The AU file format 391can also hold data in many of the standard formats (ie SF_FORMAT_PCM_16, 392SF_FORMAT_PCM_24, SF_FORMAT_FLOAT etc) as well as allowing data in both big and 393little endian format. 394 395See also [FAQ Q6](#Q006). 396 397## Q18 : Is it possible to build a Universal Binary on Mac OS X? {#Q018} 398 399Yes, but you must do two separate configure/build/test runs; one on PowerPC and 400one on Intel. It is then possible to merge the binaries into a single universal 401binary using one of the programs in the Apple tool chain. 402 403It is **not** possible to build a working universal binary via a single 404compile/build run on a single CPU. 405 406The problem is that the libsndfile build process detects features of the CPU its 407being built for during the configure process and when building a universal 408binary, configure is only run once and that data is then used for both CPUs. 409That configure data will be wrong for one of those CPUs. You will still be able 410to compile libsndfile, and the test suite will pass on the machine you compiled 411it on. However, if you take the universal binary test suite programs compiled on 412one CPU and run them on the other, the test suite will fail. 413 414Part of the problem is that the CPU endian-ness is detected at configure time. 415Yes, I know the Apple compiler defines one of the macros \_\_LITTLE\_ENDIAN\_\_ 416and \_\_BIG\_ENDIAN\_\_, but those macros are not part of the 1999 ISO C 417Standard and they are not portable. 418 419Endian issues are not the only reason why the cross compiled binary will fail. 420The configure script also detects other CPU specific idiosyncrasies to provide 421more optimized code. 422 423Finally, the real show stopper problem with universal binaries is the problem 424with the test suite. libsndfile contains a huge, comprehensive test suite. When 425you compile a universal binary and run the test suite, you only test the native 426compile. The cross compiled binary (the one with the much higher chance of 427having problems) cannot be tested. 428 429Now, if you have read this far you're probably thinking there must be a way to 430fix this and there probably is. The problem is that its a hell of a lot of work 431and would require significant changes to the configure process, the internal 432code and the test suite. In addition, these changes must not break compilation 433on any of the platforms libsndfile is currently working on. 434 435## Q19 : I have project files for Visual Studio / XCode / Whatever. Why don't you distribute them with libsndfile? {#Q019} 436 437Use CMake project. 438 439## Q20 : Why doesn't libsndfile support MP3? {#Q020} 440 441In the past, MP3 was not supported because the technology behind MP3 was 442patented. Those patents have now expired and there is an 443[open ticket](https://github.com/libsndfile/libsndfile/issues/258) to implement 444MP3 support. 445 446## Q21 : How do I use libsndfile in a closed source or commercial program and comply with the license? {#Q021} 447 448Here is a checklist of things you need to do to make sure your use of libsndfile 449in a closed source or commercial project complies with the license libsndfile is 450released under, the GNU Lesser General Public License (LGPL): 451 452- Make sure you are linking to libsndfile as a shared library (Linux and Unix 453 systems), Dynamic Link Library (Microsoft Windows) or dynlib (Mac OS X). If 454 you are using some other operating system that doesn't allow dynamically 455 linked libraries, you will not be able to use libsndfile unless you release 456 the source code to your program. 457- In the licensing documentation for your program, add a statement that your 458 software depends on libsndfile and that libsndfile is released under the GNU 459 Lesser General Public License, either 460 [version 2.1](http://www.gnu.org/licenses/lgpl-2.1.txt) or optionally 461 [version 3](http://www.gnu.org/licenses/lgpl.txt). 462- Include the text for both versions of the license, possibly as separate files 463 named libsndfile_lgpl_v2_1.txt and libsndfile_lgpl_v3.txt. 464 465## Q22 : What versions of Windows does libsndfile work on? {#Q022} 466 467New versions of libsndfile binary releases require Wiindows Vista. If you need 468Windows XP support, you can build DLL from sources, we don't use specific WinXP 469features. 470 471## Q23 : I'm cross compiling libsndfile for another platform. How can I run the test suite? {#Q023} 472 473Since version 1.0.21 the top level Makefile has an extra make target, 474'test-tarball'. Building this target creates a tarball called called: 475 476 ` libsndfile-testsuite-${host_triplet}-${version}.tar.gz` 477 478in the top level directory. This tarball can then be copied to the target 479platform. Once untarred and test script `test_wrapper.sh` can be run from the 480top level of the extracted tarball. 481