The Audio Programming Book: Errata

In this post I’ll try to keep track of mistakes I found in “The Audio Programming Book”. It will get updated frequently.
If you have found some problems on your own, I’d appreciate you posting it to the comments. I would append them to this post and for sure accredit it to you.

P. 114, Listing 1.6.1, Line 5


double* ptr = buffer + 1024; /* point to last element */
double maxval = 0.0;
unsigned long pos = 0;

while(--ptr != buffer){
    if(*ptr >= maxval){
        maxval = *ptr;
        pos = ptr - buffer;
printf("the maximum sample is %f, at position %d\n", maxval, pos);


The first thing to notice here is, that ptr initially doesn’t point to the last element (as stated in the comment in line 1), but exactly behind the end of it. But it’s just a comment, so don’t mind.

The real problem is line 5, where we have a while loop which is decreasing as long as –ptr != buffer. But buffer points exactly to the first element of a buffer of float values, so in the original function definition, the first value will never be used. The solution is just to replace the comparison in line 5 with –ptr >= buffer, so that the loop stops, when all elements of the buffer have been checked.


double* ptr = buffer + 1024; /* point behind last element */
double maxval = 0.0;
unsigned long pos = 0;

while(--ptr >= buffer){
    if(*ptr >= maxval){
        maxval = *ptr;
        pos = ptr - buffer;
printf("the maximum sample is %f, at position %d\n", maxval, pos);

P.268ff., siggen.c


There’s a strange error, when you go step by step through the tutorial to create siggen.c, a plain test programm to test a small lib for creating oscillator-functions. You are told to use the portsf library, that comes with the book, to create an outfile in 16bit format. What you are not told is, that this library has an option to dither audio data before saving, which is not used by default. This results in digital clipping of the audio files you create, some amplitude values, rather then hitting 1.0 are reversed to -1.0.

On the picture, you can see, how the result looks, when you try to render a sine wave with an amplitude of 1.0. When you listen to such a file, you will recognize the clipping as a distorted, crackling noise. But as already pointed out, you can avoid the whole problem by utilizing the already included dithering option of portsf.


Use the following chunk of code just after creating the outfile with portsf. It will activate the dithering. If you want to have a look, have it is done in the library, look for the string “PSF_DITHER_TPDF” in the function “psf_sndWriteFloatFrames” in portsf.c.
It is assumed that ofd is the ID of the soundfile in portsf.

if(psf_sndSetDither(ofd, PSF_DITHER_TPDF) != 0){
        printf("Error: unable to activate dithering.\n");
        goto exit;

6 thoughts on “The Audio Programming Book: Errata

  1. Thanks a lot, your post really helped me!
    I was’nt aware about the dithering option. I also encountered the clipping issue when writing an audio file with a max_amplitude = +/-1 but couldn’t figure why a maximum value of +1 results in -1.
    Kind regards,

  2. I really was initially hunting for plans for my own blog site and stumbled
    upon your own posting, “The Audio Programming Book:
    Errata | boundary functions” Blinds Online , would you mind in the event I really start
    using a handful of of your own suggestions? Thank you -Alfred

    • Hi Alfred,

      my answer is probably about a year too late but any way: no problem, just use whatevery content you want from this site. It would be great if you link back to the source, though.

  3. Page 77. Exercise 1.2.5: The length of a string to produce a C4 is not 660cm but 660mm, or 66 cm! Otherwise a grand piano would need to be 13 meters long only to produce a C3 and 26 meters long to produce a C2. Can you back me up on this one? 🙂

    • I haven’t looked into this blog for a while but thanks a lot for your comment!

      You’re quoting from an exercise and I think the values given there are actually chosen for educational purposes and don’t reference anything in the real world.

      Have a look at the real world physics of the oscillation of a stretched string described by Mersenne’s laws:
      The frequency of an oscillating string is not dependent of the string’s length alone but also relative to it’s weight and the applied tension.

      You’re right, obviously, that the C4 string in a piano isn’t 660cm long, but this doesn’t mean that there’s no 660cm long string that could produce a C4. In comparison to the piano string it would only have to be a) very light and/or b) highly tensioned to oscillate at the same pitch – at least in theory, I have no idea whether this is actually physically possible to construct with common materials.

      I also have no idea how long the actual string for C4 is in a “normal” piano and whether this even exists. Inside pianos wires of different gauges are used to make the extreme range of different tones possible, so different measures might be used to accomplish the same tone. It would be great if someone else had more detailed information about this.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s