OpenAL: replacing alutLoadWAVFile

Yesterday I finally got around to replacing some deprecated code in a library using  OpenAL.

For those of you who don’t know what I’m talking about: OpenAL is a cross-platform audio API that is specifically designed to blend in with OpenGL based programs. Also, you might not be interested in the rest of this blog post, since it is mostly interesting to people already using OpenAL…

Someone still reading? Great 😉

If you’ve been working with legacy OpenAL + ALUT application, you might be familiar with deprecation warnings like these:

warning: ‘void alutLoadWAVFile(ALbyte*, ALenum*, void**, ALsizei*, ALsizei*, ALboolean*)’ is deprecated
warning: ‘void alutUnloadWAV(ALenum, ALvoid*, ALsizei, ALsizei)’ is deprecated

These two functions are unfortunately still in wide use. Worse than that, there are still tutorials that use them.

Replacing the alutLoadWAVFile and alutUnloadWAV function calls is actually rather easy,
but finding this out took me way too much research, especially given the simplicity of it. That’s why I post this here as google-fodder.

So you probably have something like the following in your code right now:
[cpp]ALenum format;
ALvoid* data;
ALsizei size;
ALsizei freq;
ALboolean loop;
ALuint sndBuffer;

// allocate data, read file
alutLoadWAVFile(filename, &format, &data, &size, &freq, &loop);
if(alGetError() != AL_NO_ERROR) {
// handle the error

// create a buffer
alGenBuffers(1, &sndBuffer);
if(alGetError() != AL_NO_ERROR) {
// handle the error

// move the data into the buffer
alBufferData(sndBuffer, format, data, size, freq);

// free memory used by data
alutUnloadWAV(format, data, size, freq);[/cpp]
With ALUT 1.1, you can replace this with a simple call to alutCreateBufferFromFile:
[cpp]ALuint sndBuffer;
// make sure to call alutInitWithoutContext first
sndBuffer = alutCreateBufferFromFile(filename);
if ( alutGetError() != ALUT_ERROR_NO_ERROR )
// handle the error
Nota bene: With alutLoadWAVFile, you didn’t need to call alutInit first. When using alutCreateBufferFromFile, you need to do this, even if you initialise the OpenAL context yourself! In this case (managing the context yourself), you should call alutInitWithoutContext(argc,argv) instead of alutInit (you can use NULL for both argc and argv). Finish it up with a call to alutExit() and you are done…