summaryrefslogtreecommitdiff
path: root/doc/FAQ.html
blob: 029fdc6c9903caaee22db598dd70808bc034e8d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>

<HEAD>
	<TITLE>
	libsndfile : Frequently Asked Questions.
	</TITLE>
	<META NAME="Author"      CONTENT="Erik de Castro Lopo (erikd AT mega-nerd DOT com)">
	<META NAME="Description" CONTENT="The libsndfile FAQ.">
	<META NAME="Keywords"    CONTENT="WAV AIFF AU libsndfile sound audio dsp Linux">
	<LINK REL=StyleSheet HREF="libsndfile.css" TYPE="text/css" MEDIA="all">
</HEAD>

<BODY>

<H1><B>libsndfile : Frequently Asked Questions.</B></H1>
<P>
<A HREF="#Q001">Q1 : On Linux, sf_open returns weird info about files. Why?</A><BR>
<A HREF="#Q002">Q2 : In version 0 the SF_INFO struct had a pcmbitwidth field
		but version 1 does not. Why?</A><BR>
<A HREF="#Q003">Q3 : Compiling is really slow on MacOSX. Why?</A><BR>
<A HREF="#Q004">Q4 : When trying to compile libsndfile on Solaris I get a "bad
		substitution" error during linking. What can I do to fix this?</A><BR>
<A HREF="#Q005">Q5 : Why doesn't libsndfile do interleaving/de-interleaving?</A><BR>
<A HREF="#Q006">Q6 : What's the best format for storing temporary files?</A><BR>
<A HREF="#Q007">Q7 : On Linux/Unix/MaxOSX, what's the best way of detecting the
	presence of libsndfile?</A><BR>
<A HREF="#Q008">Q8 : But I just want a simple Makefile! What do I do?</A><BR>
<A HREF="#Q009">Q9 : How about adding the ability to write/read sound files to/from
	memory buffers?</A><BR>
<A HREF="#Q010">Q10 : Reading a 16 bit PCM file as normalised floats and then
	writing them back changes some sample values. Why?</A><BR>
<A HREF="#Q011">Q11 : I'm having problems with u-law encoded WAV files generated by
	libsndfile in Winamp. Why?</A><BR>
<A HREF="#Q012">Q12 : I'm loking at sf_read*. What are items? What are frames?</A><BR>
<A HREF="#Q013">Q13 : Why can't libsndfile open this Sound Designer II (SD2)
	file?</A><BR>
<A HREF="#Q014">Q14 : I'd like to statically link libsndfile to my closed source
	application. Can I buy a license so that this is possible?</A><BR>
<HR>

<!-- ========================================================================= -->
<A NAME="Q001"></A>
<H2><BR><B>Q1 : On Linux, sf_open returns weird info about files. Why?</B></H2>
<P>
	This simple call to sf_open :
</P>
<PRE>
        file = sf_open (filename, SFM_READ, &amp;sfinfo) ;

        printf ("srate    : %d\n", sfinfo.samplerate) ;
        printf ("frames   : %d\n", sfinfo.frames) ;
        printf ("channels : %d\n", sfinfo.channels) ;
        printf ("format   : %d\n", sfinfo.format) ;
        printf ("sections : %d\n", sfinfo.sections);
        printf ("seekable : %d\n", sfinfo.seekable) ;
</PRE>
<P>
returns this weird info:
</P>
<PRE>
        srate     : 0
        frames    : 1445760
        channels  : 44100
        format    : 2
        sections  : 65538
        seekable  : 1

</PRE>
<H2><B>Answer</B></H2>
<P>
This is only a problem on Linux and other 32 bit OSes (possibly 32 bit Solaris)
which require special compiler command line options to allow access to files
greater than 2 Gig in size.
<A HREF="pkgconfig.html">These instructions</A> should show you a way around this
problem.
</P>

<!-- ========================================================================= -->
<A NAME="Q002"></A>
<H2><BR><B>Q2 : In version 0 the SF_INFO struct had a pcmbitwidth field
		but version 1 does not. Why?</B></H2>
<P>
	This was dropped for a number of reasons:
</P>
<UL>
	<LI> pcmbitwidth makes little sense on compressed or floating point formats
	<LI> with the new API you really don't need to know it
</UL>
<P>
As documented
    <A HREF="http://www.mega-nerd.com/libsndfile/api.html#note1">here</A>
there is now a well defined behavior which ensures that no matter what the
bit width of the source file, the scaling always does something sensible.
This makes it safe to read 8, 16, 24 and 32 bit PCM files using sf_read_short()
and always have the optimal behavior.
</P>

<!-- ========================================================================= -->
<A NAME="Q003"></A>
<H2><BR><B>Q3 : Compiling is really slow on MacOSX. Why?</B></H2>
<P>
When you configure and compile libsndfile, it uses the Bourne shell for a number
of tasks (ie configure script and libtool).
However, Apple has decided to ship their system with a really crappy Bourne shell.
</P>
<P>
To fix this I suggest that you install the GNU Bash shell, rename /bin/sh to
/bin/sh.old and make a softlink from /bin/sh to the bash shell.
Bash is designed to behave as a Bourne shell when is is called as /bin/sh.
</P>
<P>
When I did this on my iBook running MacOSX, compile times dropped from 13 minutes
to 3 minutes.
</P>

<!-- ========================================================================= -->
<A NAME="Q004"></A>
<H2><BR><B>Q4 : When trying to compile libsndfile on Solaris I get a "bad
		substitution" error on linking. Why?</B></H2>
<P>
It seems that the Solaris Bourne shell disagrees with GNU libtool.
</P>
<P>
To fix this I suggest that you install the GNU Bash shell, rename /bin/sh to
/bin/sh.old and make a softlink from /bin/sh to the bash shell.
Bash is designed to behave as a Bourne shell when is is called as /bin/sh.
</P>

<!-- ========================================================================= -->
<A NAME="Q005"></A>
<H2><BR><B>Q5 : Why doesn't libsndfile do interleaving/de-interleaving?</B></H2>
<P>
This problem is bigger than it may seem at first.
</P>
<P>
For a stereo file, it is a pretty safe bet that a simple interleaving/de-interleaving
could satisfy most users.
However, for files with more than 2 channels this is unlikely to be the case.
If the user has a 4 channel file and want to play that file on a stereo output
sound card they either want the first two channels or they want some mixed combination
of the 4 channels.
</P>
<P>
When you add more channels, the combinations grow exponentially and it becomes
increasingly difficult to cover even a sensible subset of the possible combinations.
On top of that, coding any one style of interleaver/de-interleaver is trivial, while
coding one that can cover all combinations is far from trivial.
This means that this feature will not be added any time soon.
</P>

<!-- ========================================================================= -->
<A NAME="Q006"></A>
<H2><BR><B>Q6 : What's the best format for storing temporary files?</B></H2>

<P>
When you want to store temporary data there are a number of requirements;
</P>
<UL>
	<LI> A simple, easy to parse header.
	<LI> The format must provide the fastest possible read and write rates (ie
		avoid conversions and encoding/decoding).
	<LI> The file format must be reasonably common and playable by most players.
	<LI> Able to store data in either endian-ness.
</UL>
<P>
The format which best meets these requirements is AU, which allows data to be
stored in any one of short, int, float and double (among others) formats.
</P>
<P>
For instance, if an application uses float data internally, its temporary files
should use a format of (SF_ENDIAN_CPU | SF_FORMAT_AU | SF_FORMAT_FLOAT) which
will store big endian float data in big endian CPUs and little endian float data
on little endian CPUs.
Reading and writing this format will not require any conversions or byte swapping
regardless of the host CPU.
</P>

<!-- ========================================================================= -->

<A NAME="Q007"></A>
<H2><BR><B>Q7 : On Linux/Unix/MaxOSX, what's the best way of detecting the presence
	of libsndfile using autoconf?</B></H2>

<P>
libsndfile uses the pkg-config (man pkg-config) method of registering itself with the
host system.
The best way of detecting its presence is using something like this in configure.ac
(or configure.in):
</P>
<PRE>
        PKG_CHECK_MODULES(SNDFILE, sndfile >= 1.0.2, ac_cv_sndfile=1, ac_cv_sndfile=0)

        AC_DEFINE_UNQUOTED([HAVE_SNDFILE],${ac_cv_sndfile},
			[Set to 1 if you have libsndfile.])

        AC_SUBST(SNDFILE_CFLAGS)
        AC_SUBST(SNDFILE_LIBS)
</PRE>
<P>
This will automatically set the <B>SNDFILE_CFLAGS</B> and <B>SNDFILE_LIBS</B>
variables which can be used in Makefile.am like this:
</P>
<PRE>
        SNDFILE_CFLAGS = @SNDFILE_CFLAGS@
        SNDFILE_LIBS = @SNDFILE_LIBS@
</PRE>
<P>
If you install libsndfile from source, you will probably need to set the
<B>PKG_CONFIG_PATH</B> environment variable as suggested at the end of the
libsndfile configure process. For instance on my system I get this:
</P>
<PRE>
        -=-=-=-=-=-=-=-=-=-= Configuration Complete =-=-=-=-=-=-=-=-=-=-

          Configuration summary :

            Version : ..................... 1.0.5
            Experimental code : ........... no

          Tools :

            Compiler is GCC : ............. yes
            GCC major version : ........... 3

          Installation directories :

            Library directory : ........... /usr/local/lib
            Program directory : ........... /usr/local/bin
            Pkgconfig directory : ......... /usr/local/lib/pkgconfig

        Compiling some other packages against libsndfile may require
        the addition of "/usr/local/lib/pkgconfig" to the
        PKG_CONFIG_PATH environment variable.
</PRE>

<!-- ========================================================================= -->

<A NAME="Q008"></A>
<H2><BR><B>Q8 : But I just want a simple Makefile! What do I do?</B></H2>

<P>
The <B>pkg-config</B> program makes finding the correct compiler flag values and
library location far easier.
During the installation of libsndfile, a file named <B>sndfile.pc</B> is installed
in the directory <B>${libdir}/pkgconfig</B> (ie if libsndfile is installed in
<B>/usr/local/lib</B>, <B>sndfile.pc</B> will be installed in
<B>/usr/local/lib/pkgconfig/</B>).
</P>
<P>
In order for pkg-config to find sndfile.pc it may be necessary to point the
environment variable <B>PKG_CONFIG_PATH</B> in the right direction.
</P>
<PRE>
        export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
</PRE>

<P>
Then, to compile a C file into an object file, the command would be:
</P>
<PRE>
        gcc `pkg-config --cflags sndfile` -c somefile.c
</PRE>
<P>
and to link a number of objects into an executable that links against libsndfile,
the command would be:
</P>
<PRE>
        gcc `pkg-config --libs sndfile` obj1.o obj2.o -o program
</PRE>

<!-- ========================================================================= -->

<A NAME="Q009"></A>
<H2><BR><B>Q9 : How about adding the ability to write/read sound files to/from
	memory buffers?</B></H2>

<P>
I have a had a number of requests like this over the last 18 months.
As yet, no-one has been able to justify why this is a good idea and why this
cannot be done in a different way.
</P>
<P>
One example of a different way is temporary files stored on a temporary file
system (ie tmpfs on both Linux and Solaris).
These require no changes to libsndfile, no extra code in libsndfile, achieve
exactly what is required and work now.
</P>

<!-- ========================================================================= -->

<A NAME="Q010"></A>
<H2><BR><B>Q10 : Reading a 16 bit PCM file as normalised floats and then
	writing them back changes some sample values. Why?</B></H2>

<P>
This is caused by the fact that the conversion from 16 bit short to float is
done by dividing by 32768 (0x8000 in hexadecimal) while the conversion from
float to 16 bit short is done by multiplying by 32767 (0x7FFF in hex).
So for instance, a value in a 16 bit PCM file of 20000 gets read as a floating
point number of 0.6103515625 (20000.0 / 0x8000).
Converting that back to a 16 bit short results in a value of 19999.3896484375
(0.6103515625 * 0x7FFF) which then gets rounded down to 19999.
</P>
<P>
You will notice that for this particular case, the error is 1 in 20000 or
0.005%.
Interestingly, for values of less than 16369, dividing by 0x8000 followed
by multiplying by 0x7FFF and then rounding the result, gives back the
original value.
It turns out that as long as the host operating system supplies the 1999 ISO
C Standard functions <B>lrintf</B> and <B>lrint</B> (or a replacement has
been supplied) then the maximum possible error is 1 in 16369 or about 0.006%.
</P>
<P>
Regardless of the size of the error, the reason why this is done is rather
subtle.
</P>
<P>
In a file containing 16 bit PCM samples, the values are restricted to the range
[-32768, 32767] while we want floating point values in the range [-1.0, 1.0].
The only way to do this conversion is to do a floating point division by a value
of 0x8000.
Converting the other way, the only way to ensure that floating point values in
the range [-1.0, 1.0] are within the valid range allowed by a 16 bit short is
to multiply by 0x7FFF.
</P>
<P>
Some people would say that this is a severe short-coming of libsndfile.
I would counter that anybody who is constantly converting back and forth
between 16 bit shorts and normalised floats is going to suffer other losses
in audio quality that they should also be concerned about.
</P>
<P>
The correct way to deal with this problem is to consider 16 bit short data as
a final destination format only, not as an intermediate storage format.
All intermediate data (ie which is going to be processed further) should be
stored in floating point format which is supported by all of the most common
file formats.
If floating point files are considered too large (2 times the size of a 16 bit
PCM file), it would also be possible to use 24 bit PCM as an intermediate
storage format (and which is also supported by most common file types).
</P>

<!-- ========================================================================= -->

<A NAME="Q011"></A>
<H2><BR><B>Q11 : I'm having problems with u-law encoded WAV files generated by
	libsndfile in Winamp. Why?
</B></H2>

<P>
This is actually a Winamp problem.
The official Microsoft spec suggests that the 'fmt ' chunk should be 18 bytes.
Unfortunately at least one of Microsoft's own applications (Sound Recorder on
Win98 I believe) did not accept 18 bytes 'fmt ' chunks.
</P>
<P>
Michael Lee did some experimenting and found that:
</P>
<PRE>
    I have checked that Windows Media Player 9, QuickTime Player 6.4,
    RealOne Player 2.0 and GoldWave 5.06 can all play u-law files with
    16-byte or 18-byte 'fmt ' chunk. Only Winamp (2.91) and foobar2000
    are unable to play u-law files with 16-byte 'fmt ' chunk.
</PRE>

<P>
Even this is a very small sampling of all the players out there.
For that reason it is probably not a good idea to change this now because there
is the risk of breaking something that currently works.
</P>

<!-- ========================================================================= -->

<A NAME="Q012"></A>
<H2><BR><B>Q12 : I'm loking at sf_read*. What are items? What are frames?
</B></H2>

<P>
For a sound file with only one channel, a frame is the same as a item.
</P>
<P>
For multi channel sound files, a single frame contains a single item for
each channel.
</P>

<!-- ========================================================================= -->

<A NAME="Q013"></A>
<H2><BR><B>Q13 : Why can't libsndfile open this Sound Designer II (SD2) file?
</B></H2>

<P>
This is somewhat complicated.
First some background.
</P>

<P>
SD2 files are native to the Apple Macintosh platform and use features of
the Mac filesystem (file resource forks) to store the file's sample rate,
number of channels, sample width and more.
When you look at a file and its resource fork on Mac OSX it looks like
this:
</P>

<PRE>
        -rw-r--r--  1 erikd erikd   46512 Oct 18 22:57 file.sd2
        -rw-r--r--  1 erikd erikd     538 Oct 18 22:57 file.sd2/rsrc
</PRE>

<P>
Notice how the file itself looks like a directory containing a single file
named <B>rsrc</B>.
When libsndfile is compiled for MacOSX, it should open SD2 file with
resource forks like this without any problems.
</P>

<P>
When SD2 files are moved to other platforms, the resource fork of the file
can sometimes be dropped altogether.
All that remains is the raw audio data and no information about the number
of channels, sample rate or bit width which makes it a little difficult for
libsndfile to open the file.
</P>

<P>
However, it is possible to safely move an SD2 file to a Linux or Windows
machine.
For instance, when an SD2 file is copied from inside MacOSX to a windows
shared directory or a Samba share (ie Linux), MacOSX is clever enough to
store the resource fork of the file in a separate hidden file in the
same directory like this:
</P>
<PRE>
        -rw-r--r--  1 erikd erikd     538 Oct 18 22:57 ._file.sd2
        -rw-r--r--  1 erikd erikd   46512 Oct 18 22:57 file.sd2
</PRE>

<P>
Regardless of what platform it is running on, when libsndfile is asked to
open a file named <B>"foo"</B> and it can't recognize the file type from
the data in the file, it then tries to open a file named <B>"._foo"</B>
to see if the file has a valid resource fork.
</P>

<P>
In short, libsndfile should open SD2 files with a valid resource fork on
all of the platforms that libsndfile supports.
If a file has lost its resource fork, the only option is the open the file
using the SF_FORMAT_RAW option and guessing its sample rate, channel count
and bit width.
</P>

<P>
Occasionally, when SD2 files are moved to other systems, the file is
	<A HREF="http://www.macdisk.com/binhexen.php3">BinHexed</A>
which wraps the resource fork and the data fork together.
For these files, it would be possible to write a BinHex parser but
there is not a lot to gain considering how rare these BinHexed SD2
files are.
</P>

<!-- ========================================================================= -->
<A NAME="Q014"></A>
<H2><BR><B>Q14 : I'd like to statically link libsndfile to my closed source
	application. Can I buy a license so that this is possible?
</B></H2>

<P>
Unfortunately no.
libsndfile contains code written by other people who have agreed that their
code be used under the GNU LGPL but no more.
Even if they were to agree, there would be significant difficulties in
dividing up the payments fairly.
</P>

<P>
The <B>only</B> way you can legally use libsndfile as a statically linked
library is if your application is released under the GNU GPL or LGPL.
</P>

<!-- ========================================================================= -->
<HR>
<P>
	The libsndfile home page is here :
		<A HREF="http://www.mega-nerd.com/libsndfile/">
			http://www.mega-nerd.com/libsndfile/</A>.
<BR>
Version : 1.0.11
</P>

</BODY>
</HTML>

<!--
  Do not edit or modify anything in this comment block.
  The arch-tag line is a file identity tag for the GNU Arch
  revision control system.

  arch-tag: 508f827d-b9a7-4a86-8c2f-d82321df91f7
-->