Debian Codegraph @

docs/FAQ.md

raw
1 ---
2 layout: default
3 title: libsndfile : Frequently Asked Questions.
4 ---
5
6 # libsndfile : Frequently Asked Questions
7
8 1. [Do you plan to support XYZ codec in libsndfile?](#Q001)
9 2. [In version 0 the SF\_INFO struct had a pcmbitwidth field but version 1 does not. Why?](#Q002)
10 3. [Compiling is really slow on MacOS X. Why?](#Q003)
11 4. [When trying to compile libsndfile on Solaris I get a "bad substitution" error during linking. What can I do to fix this?](#Q004)
12 5. [Why doesn't libsndfile do interleaving/de-interleaving?](#Q005)
13 6. [What's the best format for storing temporary files?](#Q006)
14 7. [On Linux/Unix/MacOS X, what's the best way of detecting the presence of libsndfile?](#Q007)
15 8. [I have libsndfile installed and now I want to use it. I just want a simple Makefile\! What do I do?](#Q008)
16 9. [How about adding the ability to write/read sound files to/from memory buffers?](#Q009)
17 10. [Reading a 16 bit PCM file as normalised floats and then writing them back changes some sample values. Why?](#Q010)
18 11. [I'm having problems with u-law encoded WAV files generated by libsndfile in Winamp. Why?](#Q011)
19 12. [I'm looking at sf\_read\*. What are items? What are frames?](#Q012)
20 13. [Why can't libsndfile open this Sound Designer II (SD2) file?](#Q013)
21 14. [I'd like to statically link libsndfile to my closed source application. Can I buy a license so that this is possible?](#Q014)
22 15. [My program is crashing during a call to a function in libsndfile. Is this a bug in libsndfile?](#Q015)
23 16. [Will you accept a fix for compiling libsndfile with compiler X?](#Q016)
24 17. [Can libsndfile read/write files from/to UNIX pipes?](#Q017)
25 18. [Is it possible to build a Universal Binary on Mac OS X?](#Q018)
26 19. [I have project files for Visual Studio / XCode / Whatever. Why don't you distribute them with libsndfile?](#Q019)
27 20. [Why doesn't libsndfile support MP3?](#Q020)
28 21. [How do I use libsndfile in a closed source or commercial program and comply with the license?](#Q021)
29 22. [What versions of windows does libsndfile work on?](#Q022)
30 23. [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
36 If source code for XYZ codec is available under a suitable license (LGPL, BSD,
37 MIT etc) then yes, I'd like to add it.
38
39 If suitable documentation is available on how to decode and encode the format
40 then maybe, depending on how much work is involved.
41
42 If XYZ is some proprietary codec where no source code or documentation is
43 available then no.
44
45 So if you want support for XYZ codec, first find existing source code or
46 documentation. 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
50 This 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
55 As documented [here](api.md#note-1) there is now a well defined behaviour which
56 ensures that no matter what the bit width of the source file, the scaling always
57 does something sensible. This makes it safe to read 8, 16, 24 and 32 bit PCM
58 files using `sf_read_short()` and always have the optimal behaviour.
59
60 ## Q3 : Compiling is really slow on MacOS X. Why? {#Q003}
61
62 When you configure and compile libsndfile, it uses the /bin/sh shell for a
63 number 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
66 this answer doesn't apply in that case.
67
68 To 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
70 to behave as a Bourne shell when it is called as /bin/sh.
71
72 When I did this on my iBook running MacOS X, compile times dropped from 13
73 minutes to 3 minutes.
74
75 ## Q4 : When trying to compile libsndfile on Solaris I get a "bad substitution" error on linking. Why? {#Q004}
76
77 It seems that the Solaris Bourne shell disagrees with GNU libtool.
78
79 To 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
81 to 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
85 This problem is bigger than it may seem at first.
86
87 For a stereo file, it is a pretty safe bet that a simple interleaving/
88 de-interleaving could satisfy most users. However, for files with more than 2
89 channels this is unlikely to be the case. If the user has a 4 channel file and
90 want to play that file on a stereo output sound card they either want the first
91 2 channels or they want some mixed combination of the 4 channels.
92
93 When you add more channels, the combinations grow exponentially and it becomes
94 increasingly difficult to cover even a sensible subset of the possible
95 combinations. On top of that, coding any one style of interleaver/de-interleaver
96 is trivial, while coding one that can cover all combinations is far from
97 trivial. 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
101 When 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
109 The format which best meets these requirements is AU, which allows data to be
110 stored in any one of short, int, float and double (among others) formats.
111
112 For instance, if an application uses float data internally, its temporary files
113 should use a format of (SF_ENDIAN_CPU | SF_FORMAT_AU | SF_FORMAT_FLOAT) which
114 will store big endian float data in big endian CPUs and little endian float data
115 on little endian CPUs. Reading and writing this format will not require any
116 conversions 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
120 libsndfile uses the pkg-config (man pkg-config) method of registering itself
121 with the host system. The best way of detecting its presence is using something
122 like 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
132 This will automatically set the **SNDFILE_CFLAGS** and **SNDFILE_LIBS**
133 variables which can be used in Makefile.am like this:
134
135 SNDFILE_CFLAGS = @SNDFILE_CFLAGS@
136 SNDFILE_LIBS = @SNDFILE_LIBS@
137
138 If 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
140 libsndfile 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
166 The **pkg-config** program makes finding the correct compiler flag values and
167 library location far easier. During the installation of libsndfile, a file named
168 **sndfile.pc** is installed in the directory **${libdir}/pkgconfig** (ie if
169 libsndfile is installed in **/usr/local/lib**, **sndfile.pc** will be installed
170 in **/usr/local/lib/pkgconfig/**).
171
172 In order for pkg-config to find sndfile.pc it may be necessary to point the
173 environment variable **PKG_CONFIG_PATH** in the right direction.
174
175 export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
176
177 Then, to compile a C file into an object file, the command would be:
178
179 gcc `pkg-config --cflags sndfile` -c somefile.c
180
181 and to link a number of objects into an executable that links against
182 libsndfile, 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
188 This 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
192 This is caused by the fact that the conversion from 16 bit short to float is
193 done by dividing by 32768 (0x8000 in hexadecimal) while the conversion from
194 float to 16 bit short is done by multiplying by 32767 (0x7FFF in hex). So for
195 instance, a value in a 16 bit PCM file of 20000 gets read as a floating point
196 number of 0.6103515625 (20000.0 / 0x8000). Converting that back to a 16 bit
197 short results in a value of 19999.3896484375 (0.6103515625 \* 0x7FFF) which then
198 gets rounded down to 19999.
199
200 You will notice that for this particular case, the error is 1 in 20000 or
201 0.005%. Interestingly, for values of less than 16369, dividing by 0x8000
202 followed by multiplying by 0x7FFF and then rounding the result, gives back the
203 original value. It turns out that as long as the host operating system supplies
204 the 1999 ISO C Standard functions **lrintf** and **lrint** (or a replacement has
205 been supplied) then the maximum possible error is 1 in 16369 or about 0.006%.
206
207 Regardless of the size of the error, the reason why this is done is rather
208 subtle.
209
210 In 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].
212 The only way to do this conversion is to do a floating point division by a value
213 of 0x8000. Converting the other way, the only way to ensure that floating point
214 values in the range [-1.0, 1.0] are within the valid range allowed by a 16 bit
215 short is to multiply by 0x7FFF.
216
217 Some people would say that this is a severe short-coming of libsndfile. I would
218 counter that anybody who is constantly converting back and forth between 16 bit
219 shorts and normalised floats is going to suffer other losses in audio quality
220 that they should also be concerned about.
221
222 Since this problem only occurs when converting between integer data on disk and
223 normalized floats in the application, it can be avoided by using something other
224 than normalized floats in the application. Alternatives to normalized floats are
225 the **short** and **int** data types (ie using sf_read_short or sf_read_int) or
226 using un-normalized floats (see
227 [SFC_SET_NORM_FLOAT](command.html#sfc_set_norm_float)).
228
229 Another way to deal with this problem is to consider 16 bit short data as a
230 final destination format only, not as an intermediate storage format. All
231 intermediate data (ie which is going to be processed further) should be stored
232 in floating point format which is supported by all of the most common file
233 formats. If floating point files are considered too large (2 times the size of a
234 16 bit PCM file), it would also be possible to use 24 bit PCM as an intermediate
235 storage 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
239 This 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
241 applications (Sound Recorder on Win98 I believe) did not accept 18 bytes 'fmt '
242 chunks.
243
244 Michael 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
251 Even this is a very small sampling of all the players out there. For that reason
252 it is probably not a good idea to change this now because there is the risk of
253 breaking something that currently works.
254
255 ## Q12 : I'm looking at sf_read*. What are items? What are frames? {#Q012}
256
257 An `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
260 For a sound file with only one channel, a frame is the same as a item (ie a
261 single sample) while for multi channel sound files, a single frame contains a
262 single item for each channel.
263
264 Here are two simple, correct examples, both of which are assumed to be working
265 on a stereo file, first using items:
266
267 ```c
268 #define CHANNELS 2
269 short data [CHANNELS * 100] ;
270 sf_count items_read = sf_read_short (file, data, 200) ;
271 assert (items_read == 200) ;
272 ```
273
274 and now reading the exact same amount of data using frames:
275
276 ```c
277 #define CHANNELS 2
278 short data [CHANNELS * 100] ;
279 sf_count frames_read = sf_readf_short (file, data, 100) ;
280 assert (frames_read == 100) ;
281 ```
282
283 ## Q13 : Why can't libsndfile open this Sound Designer II (SD2) file? {#Q013}
284
285 This is somewhat complicated. First some background.
286
287 SD2 files are native to the Apple Macintosh platform and use features of the Mac
288 filesystem (file resource forks) to store the file's sample rate, number of
289 channels, sample width and more. When you look at a file and its resource fork
290 on 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
295 Notice 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
297 read) SD2 file with resource forks like this without any problems. It will also
298 handle files with the resource fork in a separate file as described below.
299
300 When SD2 files are moved to other platforms, the resource fork of the file can
301 sometimes be dropped altogether. All that remains is the raw audio data and no
302 information about the number of channels, sample rate or bit width which makes
303 it a little difficult for libsndfile to open the file.
304
305 However, it is possible to safely move an SD2 file to a Linux or Windows
306 machine. For instance, when an SD2 file is copied from inside MacOS X to a
307 windows shared directory or a Samba share (ie Linux), MacOS X is clever enough
308 to store the resource fork of the file in a separate hidden file in the same
309 directory 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
314 Regardless of what platform it is running on, when libsndfile is asked to open a
315 file named **"foo"** and it can't recognize the file type from the data in the
316 file, it will attempt to open the resource fork and if that fails, it then tries
317 to open a file named **"._foo"** to see if the file has a valid resource fork.
318 This is the same regardless of whether the file is being opened for read or
319 write.
320
321 In short, libsndfile should open SD2 files with a valid resource fork on all of
322 the platforms that libsndfile supports. If a file has lost its resource fork,
323 the only option is the open the file using the SF_FORMAT_RAW option and guessing
324 its sample rate, channel count and bit width.
325
326 Occasionally, when SD2 files are moved to other systems, the file is
327 [BinHexed](http://www.macdisk.com/binhexen.php3) which wraps the resource fork
328 and the data fork together. For these files, it would be possible to write a
329 BinHex parser but there is not a lot to gain considering how rare these BinHexed
330 SD2 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
334 Unfortunately no. libsndfile contains code written by other people who have
335 agreed that their code be used under the GNU LGPL but no more. Even if they were
336 to agree, there would be significant difficulties in dividing up the payments
337 fairly.
338
339 The **only** way you can legally use libsndfile as a statically linked library
340 is 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
344 libsndfile is being used by large numbers of people all over the world without
345 any problems like this. That means that it is much more likely that your code
346 has a bug than libsndfile. However, it is still possible that there is a bug in
347 libsndfile.
348
349 To 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
365 If compiler X is a C++ compiler then no. C and C++ are different enough to make
366 writing code that compiles as valid C and valid C++ too difficult. I would
367 rather spend my time fixing bugs and adding features.
368
369 If compiler X is a C compiler then I will do what I can as long as that does not
370 hamper the correctness, portability and maintainability of the existing code. It
371 should be noted however that libsndfile uses features specified by the 1999 ISO
372 C Standard. This can make compiling libsndfile with some older compilers
373 difficult.
374
375 ## Q17 : Can libsndfile read/write files from/to UNIX pipes? {#Q017}
376
377 Yes, libsndfile can read files from pipes. Unfortunately, the write case is much
378 more complicated.
379
380 File formats like AIFF and WAV have information at the start of the file (the
381 file header) which states the length of the file, the number of sample frames
382 etc. This information must be filled in correctly when the file header is
383 written, but this information is not reliably known until the file is closed.
384 This means that libsndfile cannot write AIFF, WAV and many other file types to a
385 pipe.
386
387 However, there is at least one file format (AU) which is specifically designed
388 to be written to a pipe. Like AIFF and WAV, AU has a header with a sample frames
389 field, but it is specifically allowable to set that frames field to 0x7FFFFFFF
390 if the file length is not known when the header is written. The AU file format
391 can also hold data in many of the standard formats (ie SF_FORMAT_PCM_16,
392 SF_FORMAT_PCM_24, SF_FORMAT_FLOAT etc) as well as allowing data in both big and
393 little endian format.
394
395 See also [FAQ Q6](#Q006).
396
397 ## Q18 : Is it possible to build a Universal Binary on Mac OS X? {#Q018}
398
399 Yes, but you must do two separate configure/build/test runs; one on PowerPC and
400 one on Intel. It is then possible to merge the binaries into a single universal
401 binary using one of the programs in the Apple tool chain.
402
403 It is **not** possible to build a working universal binary via a single
404 compile/build run on a single CPU.
405
406 The problem is that the libsndfile build process detects features of the CPU its
407 being built for during the configure process and when building a universal
408 binary, configure is only run once and that data is then used for both CPUs.
409 That configure data will be wrong for one of those CPUs. You will still be able
410 to compile libsndfile, and the test suite will pass on the machine you compiled
411 it on. However, if you take the universal binary test suite programs compiled on
412 one CPU and run them on the other, the test suite will fail.
413
414 Part of the problem is that the CPU endian-ness is detected at configure time.
415 Yes, I know the Apple compiler defines one of the macros \_\_LITTLE\_ENDIAN\_\_
416 and \_\_BIG\_ENDIAN\_\_, but those macros are not part of the 1999 ISO C
417 Standard and they are not portable.
418
419 Endian issues are not the only reason why the cross compiled binary will fail.
420 The configure script also detects other CPU specific idiosyncrasies to provide
421 more optimized code.
422
423 Finally, the real show stopper problem with universal binaries is the problem
424 with the test suite. libsndfile contains a huge, comprehensive test suite. When
425 you compile a universal binary and run the test suite, you only test the native
426 compile. The cross compiled binary (the one with the much higher chance of
427 having problems) cannot be tested.
428
429 Now, if you have read this far you're probably thinking there must be a way to
430 fix this and there probably is. The problem is that its a hell of a lot of work
431 and would require significant changes to the configure process, the internal
432 code and the test suite. In addition, these changes must not break compilation
433 on 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
437 Use CMake project.
438
439 ## Q20 : Why doesn't libsndfile support MP3? {#Q020}
440
441 ~~In the past, MP3 was not supported because the technology behind MP3 was
442 patented. Those patents have now expired and there is an
443 [open ticket](https://github.com/libsndfile/libsndfile/issues/258) to implement
444 MP3 support.~~
445
446 **Update :** Starting from version 1.1.0 libsndfile supports MP3 format.
447
448 ## Q21 : How do I use libsndfile in a closed source or commercial program and comply with the license? {#Q021}
449
450 Here is a checklist of things you need to do to make sure your use of libsndfile
451 in a closed source or commercial project complies with the license libsndfile is
452 released under, the GNU Lesser General Public License (LGPL):
453
454 - Make sure you are linking to libsndfile as a shared library (Linux and Unix
455 systems), Dynamic Link Library (Microsoft Windows) or dynlib (Mac OS X). If
456 you are using some other operating system that doesn't allow dynamically
457 linked libraries, you will not be able to use libsndfile unless you release
458 the source code to your program.
459 - In the licensing documentation for your program, add a statement that your
460 software depends on libsndfile and that libsndfile is released under the GNU
461 Lesser General Public License, either
462 [version 2.1](http://www.gnu.org/licenses/lgpl-2.1.txt) or optionally
463 [version 3](http://www.gnu.org/licenses/lgpl.txt).
464 - Include the text for both versions of the license, possibly as separate files
465 named libsndfile_lgpl_v2_1.txt and libsndfile_lgpl_v3.txt.
466
467 ## Q22 : What versions of Windows does libsndfile work on? {#Q022}
468
469 New versions of libsndfile binary releases require Wiindows Vista. If you need
470 Windows XP support, you can build DLL from sources, we don't use specific WinXP
471 features.
472
473 ## Q23 : I'm cross compiling libsndfile for another platform. How can I run the test suite? {#Q023}
474
475 Since version 1.0.21 the top level Makefile has an extra make target,
476 'test-tarball'. Building this target creates a tarball called called:
477
478 ` libsndfile-testsuite-${host_triplet}-${version}.tar.gz`
479
480 in the top level directory. This tarball can then be copied to the target
481 platform. Once untarred and test script `test_wrapper.sh` can be run from the
482 top level of the extracted tarball.