What is a lightweight cross platform WAV playing library? What is a lightweight cross platform WAV playing library? linux linux

What is a lightweight cross platform WAV playing library?


Since I'm also looking for an answer for question I did a bit of research, and I haven't find any simple (simple like calling one function) way to play an audio file. But with some lines of code, it is possible even in a portable way using the already mentioned portaudio and libsndfile (LGPL).

Here is a small test case I've written to test both libs:

#include <portaudio.h>#include <sndfile.h>static intoutput_cb(const void * input, void * output, unsigned long frames_per_buffer,        const PaStreamCallbackTimeInfo *time_info,        PaStreamCallbackFlags flags, void * data){    SNDFILE * file = data;    /* this should not actually be done inside of the stream callback     * but in an own working thread      *     * Note although I haven't tested it for stereo I think you have     * to multiply frames_per_buffer with the channel count i.e. 2 for     * stereo */    sf_read_short(file, output, frames_per_buffer);    return paContinue;}static voidend_cb(void * data){    printf("end!\n");}#define error_check(err) \    do {\        if (err) { \            fprintf(stderr, "line %d ", __LINE__); \            fprintf(stderr, "error number: %d\n", err); \            fprintf(stderr, "\n\t%s\n\n", Pa_GetErrorText(err)); \            return err; \        } \    } while (0)intmain(int argc, char ** argv){    PaStreamParameters out_param;    PaStream * stream;    PaError err;    SNDFILE * file;    SF_INFO sfinfo;    if (argc < 2)    {        fprintf(stderr, "Usage %s \n", argv[0]);        return 1;    }    file = sf_open(argv[1], SFM_READ, &sfinfo);    printf("%d frames %d samplerate %d channels\n", (int)sfinfo.frames,            sfinfo.samplerate, sfinfo.channels);    /* init portaudio */    err = Pa_Initialize();    error_check(err);    /* we are using the default device */    out_param.device = Pa_GetDefaultOutputDevice();    if (out_param.device == paNoDevice)    {        fprintf(stderr, "Haven't found an audio device!\n");        return -1;    }    /* stero or mono */    out_param.channelCount = sfinfo.channels;    out_param.sampleFormat = paInt16;    out_param.suggestedLatency = Pa_GetDeviceInfo(out_param.device)->defaultLowOutputLatency;    out_param.hostApiSpecificStreamInfo = NULL;    err = Pa_OpenStream(&stream, NULL, &out_param, sfinfo.samplerate,            paFramesPerBufferUnspecified, paClipOff,            output_cb, file);    error_check(err);    err = Pa_SetStreamFinishedCallback(stream, &end_cb);    error_check(err);    err = Pa_StartStream(stream);    error_check(err);    printf("Play for 5 seconds.\n");    Pa_Sleep(5000);    err = Pa_StopStream(stream);    error_check(err);    err = Pa_CloseStream(stream);    error_check(err);    sf_close(file);    Pa_Terminate();    return 0;}

Some notes to the example. It is not good practice to do the data loading inside of the stream callback, but inside an own loading thread. If you need to play several audio files it becomes even more difficult, because not all portaudio backends support multiple streams for one device, for example the OSS backend doesn't, but the ALSA backend does. I don't know how the situation is on windows. Since all your input files are of the same type you could mix them on you own, which complicates the code a bit more, but then you'd have also support for OSS. If you would have also different sample rates or number of channels, it'd become very difficult.

So If you don't want to play multiple files at the same time, this could be a solution or at least a start for you.


SDL_Mixer, although not very lightweight, does have a simple interface to play WAV files. I believe, like SDL, SDL_Mixer is also LGPL.

OpenAL is another cross platform audio library that is more geared towards 3D audio.

Yet another open source audio library that you might want to check it out is PortAudio


I've used OpenAL to play wav files as alerts/warnings in an Air Traffic Control system

The advantages I've found are

  1. it is cross platform
  2. works with C (and others but your question is about C)
  3. light weight
  4. good documentation available on the web
  5. the license is LGPL so you call the API with no license problems