libsndfile : Frequently Asked Questions.

Q1 : On Linux, sf_open returns weird info about files. Why?
Q2 : In version 0 the SF_INFO struct had a pcmbitwidth field but version 1 does not. Why?
Q3 : Compiling is really slow on MacOSX. Why?
Q4 : When trying to compile libsndfile on Solaris I get a "bad substitution" error during linking. What can I do to fix this?
Q5 : Why doesn't libsndfile do interleaving/de-interleaving?
Q6 : What's the best format for storing temporary files?
Q7 : On Linux/Unix/MaxOSX, what's the best way of detecting the presence of libsndfile?
Q8 : How about adding the ability to write/read sound files to/from memory buffers?
Q9 : Reading a 16 bit PCM file as normalised floats and then writing them back changes some sample values. Why?
Q10 : I'm having problems with u-law encoded WAV files generated by libsndfile in Winamp. Why?
Q11 : What are samples? What are frames?



Q1 : On Linux, sf_open returns weird info about files. Why?

This simple call to sf_open :

        file = sf_open (filename, SFM_READ, &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) ;

returns this weird info:

        srate     : 0
        frames    : 1445760
        channels  : 44100
        format    : 2
        sections  : 65538
        seekable  : 1

Answer

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. These instructions should show you a way around this problem.


Q2 : In version 0 the SF_INFO struct had a pcmbitwidth field but version 1 does not. Why?

This was dropped for a number of reasons:

As documented here 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.


Q3 : Compiling is really slow on MacOSX. Why?

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.

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.

When I did this on my iBook running MacOSX, compile times dropped from 13 minutes to 3 minutes.


Q4 : When trying to compile libsndfile on Solaris I get a "bad substitution" error on linking. Why?

It seems that the Solaris Bourne shell disagrees with GNU libtool.

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.


Q5 : Why doesn't libsndfile do interleaving/de-interleaving?

This problem is bigger than it may seem at first.

For a stereo file, it a pretty safe bet that a simple interleaving/de-interleaving could satisfy most users. However, for file 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.

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.


Q6 : What's the best format for storing temporary files?

When you want to store temporary data there are a number of requirements;

The format which best means these requirements is AU, which allows data to be stored in any one of short, int, float and double (among others).

For instance, if an application uses float data internally, it 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 float endian data on little endian CPUs. Reading and writing this format will not require any conversions or byte swapping regardless of the host CPU.


Q7 : On Linux/Unix/MaxOSX, what's the best way of detecting the presence of libsndfile using autoconf?

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):

        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)

This will automatically set the SNDFILE_CFLAGS and SNDFILE_LIBS variables which can be used in Makefile.am like this:

        SNDFILE_CFLAGS = @SNDFILE_CFLAGS@
        SNDFILE_LIBS = @SNDFILE_LIBS@

If you install libsndfile from source, you will probably need to set the PKG_CONFIG_PATH environment variable's suggested at the end of the libsndfile configure process. For instance on my system I get this:

        -=-=-=-=-=-=-=-=-=-= 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.


Q8 : How about adding the ability to write/read sound files to/from memory buffers?

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 a different way.

One example of different way is temporary files stored on a temporary file systems (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.


Q9 : Reading a 16 bit PCM file as normalised floats and then writing them back changes some sample values. Why?

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.

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 lrintf and lrint (or a replacement has been supplied) then the maximum possible error is 1 in 16369 or about 0.006%.

Regardless of the size of the error, the reason why this is done is rather subtle.

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 divide by a value 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 0x7FFF.

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.

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).


Q10 : I'm having problems with u-law encoded WAV files generated by libsndfile in Winamp. Why?

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.

Michael Lee did some experimenting and found that:

    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.

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


Q11 : What are samples? What are frames?

For a sound file with only one channel, a frame is the same as a sample.

For multi channel sound files, a single frame contains a single sample for each channel.


The libsndfile home page is here : http://www.zip.com.au/~erikd/libsndfile/.
Version : 1.0.6