C: Processing and playing PCMed audio

Playing compressed audio contains can be quiete complicated and needs some process power. So I’m going to start to play .WAV, .AIFF and raw PCM data. The first step will be to read a container format and decode the contained audio stream. There are multiple libraries which can do the job for us. For now we’ll use libsndfile because it’s simple and supports common FOS audio containers.

For cross-platform playback I’m using libao, which supports alsa and has direct access to the pulseaudio server on linux.

Start by reading a audio container. In this example a .WAV file.A

1    #include <sndfile.h>
2
3    SF_INFO sfinfo;
4
5    SNDFILE *file = sf_open("test.wav", SFM_READ, &sfinfo);

Now initialize our device driver.

1    #include <ao/ao.h>
2
3    int default_driver;
4    ao_device *device;
5
6    ao_initialize();
7
8    default_driver = ao_default_driver_id();

And setup our playback format. We’re setting the sample size, then our channels, the audio’s sample rate and the supplied byte format.

 1    ao_sample_format format;
 2    
 3    switch (sfinfo.format & SF_FORMAT_SUBMASK) {
 4        case SF_FORMAT_PCM_16:
 5            format.bits = 16;
 6            break;
 7        case SF_FORMAT_PCM_24:
 8            format.bits = 24;
 9            break;
10        case SF_FORMAT_PCM_32:
11            format.bits = 32;
12            break;
13        case SF_FORMAT_PCM_S8:
14            format.bits = 8;
15            break;
16        case SF_FORMAT_PCM_U8:
17            format.bits = 8;
18            break;
19        default:
20            format.bits = 16;
21            break;
22    }
23
24    format.channels = sfinfo.channels;
25    format.rate = sfinfo.samplerate;
26    format.byte_format = AO_FMT_NATIVE;
27    format.matrix = 0;

Now open our device.

1    device = ao_open_live(default_driver, &format, NULL);
2
3    if (device == NULL) {
4        fprintf(stderr, "Error opening device.\n");
5        return;
6    }

Then we can decode our complete audio container to a raw pcm format by allocating the needed memory and start the decoding. The size of the buffer has to be channels * samples * sizeof(short) as we’re reading shorts.

1    buf_size = (uint_32) (format.channels * sfinfo.frames * sizeof(short));
2    buffer = calloc(buf_size, sizeof(char));
3
4    sf_readf_short(file, buffer, buf_size);

The last step is the play the buffer and close everything.

1    ao_play(device, (char *) buffer, buf_size);
2
3    
4    ao_close(device);
5    ao_shutdown();
6    sf_close(file);

Here’s the code with some extras. It includes for example canceling.

Do you have questions? Send an email to max@maxammann.org