OpenAL Tutorial 1
Posted by Mike | Filed under Programming
This is a tutorial on OpenAL and how to use it. This will guide you through creating a basic audio engine with OpenAL for audio playback in games.
Introduction
OpenAL is an audio API for use with games (among other things). It is crossplatform and useful for positioning audio sources within a 3d world. OpenAL has a utility toolkit, similar to glut for OpenGL, called alut. This library is use. This tutorial will be broken up into several pieces to make it easier to digest and understand.
Lesson 1: Initalization and Basic Error Handling
The first thing that you need to do, when dealing with almost any API, is to initialize it for use. Because I am using the alut library in addition, we have two sets of APIs that we need to setup and initalize. The first thing to know is that there are two initalization options for alut, both which are documented below
ALboolean alutInit (int *argcp, char **argv);
Initializes the ALUT internals and creates an OpenAL context on the default device and makes it the current OpenAL context. If you want something more complex than that (e.g. running on a non-default device or opening multiple contexts on multiple devices), you can use alutInitWithoutContext instead. alutInit examines the commandline arguments passed to it and remove those it recognizes. It is acceptable to pass two NULL pointers in settings where no useful information can be obtained from argc and argv.
ALboolean alutInitWithoutContext (int *argcp, char **argv);
Initializes the ALUT internals. It does not create any OpenAL context or device, so this has to be done via the usual ALC calls. alutInitWithoutContext examines the commandline arguments passed to it and remove those it recognizes. It is acceptable to pass two NULL pointers in settings where no useful information can be obtained from argc and argv.
Basically one function (alutInit) will initalize both alut and OpenAL, and another will just initalize alut. Which function you use is your preference, but I prefer to initalize only alut with alut initalization routine, you could do the other. Please see the example below for initalizing alut.
Example 1-1
if (!alutInitWithoutContext(NULL, NULL))
{
ALenum error = alutGetError();
printf("AudioEngine: ERROR: There is a problem with alut. Audio not avaiable. ALUT Error: %s\n", alutGetErrorString(error));
}
All this code does it initalize alut only, without passing anything. If the alut init routine fails, it returns an ALboolean as false. Then check for the error and you can get an error string to help in your debugging.
Alright, now that alut is init’d, you can now move on to the actual initalization routine of OpenAL. There are a two basic steps you need to do to setup everything: First you need to open the device, and then create the context. First, lets begin with openning the device. You use the following function call
ALCdevice *alcOpenDevice(const ALCchar *devicename);
Which opens a device on the current system. devicename can be a string used to describe the current device, or NULL to obtain the system default. This can be used to specify a specific device, which you should rarely need to do. After opening the device, ensure that you check the current error state using:
ALCenum alcGetError(ALCdevice *device);
ALenum alGetError();
The device parameter is the return value from the alcOpenDevice function. After checking to ensure that we are still sane in our current state, we move on to the creation of our context. Again, this is pretty simple to do, and we need only two function:
ALCcontext * alcCreateContext(ALCdevice *device, ALCint* attrlist);
Which creates a new OpenAL context with the device you just accessed. The attrlist can be NULL (see the documentation for more information regarding the paramets). Then using this call:
ALCboolean alcMakeContextCurrent(ALCcontext *context);
we set the newly created context to be the one we want to use. And that is it! See Example 1-2 for the OpenAL init code
ALenum error;
ALCdevice* audioDevice = alcOpenDevice(NULL);
if((error = alcGetError(audioDevice)) != ALC_NO_ERROR)
{
printf("ERROR: There is a problem with the audio device, no audio will be available as a result: %s\n", alutGetErrorString(error));
}
if (audioDevice)
{
ALCcontext* curContext = alcCreateContext(audioDevice, NULL);
alcMakeContextCurrent(curContext);
if((error = alcGetError(audioDevice)) != ALC_NO_ERROR)
{
printf("ERROR: There is a problem with the audio context, no audio will be available as a result: %s\n", alutGetErrorString(error));
}
}
Excercise 1-1
The above code is not complete with all the necessary error handling code. Find and add in the appropriate error handling code
External Sources
Please note that alut API documentation came from the following website.
All documentation regarding OpenAL’s API came from the OpenAL Programmer Guide.
Tags: alut, alutinitwithoutcontext, audio, Games, OpenAL, OpenAL API, Programming, tutorial
One Response to “OpenAL Tutorial 1”
-
Marian Says:
March 13th, 2010 at 5:43 pmThis is why I love http://www.mikefilion.com. Fascinating post.
RSS Feed