An example: coding an object for movement and sound, part 2
The xldemo program illustrates the basic use of OpenAL. A scene is created that includes two stationary objects (houses), two moving objects with fixed paths (a car and a van), and one freely moving object (the listener, i.e., you). The Linux version of the xldemo program is built from the Linux-specific sources in
openal/demos/XLDEMO/linux and the shared sources in the
openal/demos/XLDEMO/COMMON directory. Let's take a closer look into those sources to watch OpenAL at work.
Note: Code comments preceded by "tsao" are by Joseph I. Valenzuela. The author is especially indebted to Mr. Valenzuela for his assistance with this section.
In the Linux directory,
xlmain.cpp sets up OpenGL/OpenAL and starts the main program loop. Looking into the code, we first see some standard includes (such as
sys/time.h) declared along with these files:
#include "GL/glut.h" // sets up OpenGL #include "../COMMON/common.h" // defines the AVEnvironment class #include "../COMMON/audioenv.h" // defines the audio environment class
The AVEnvironment class includes items such as window height and width, camera/listener angles and positions, and the named objects in the virtual environment (
CarList, etc). It also references functions such as
ChangeView (both found in
xlmain.cpp). The audio environment class contains functions for loading and playing sound files, starting and stopping playback, and managing the positions of Sources and Listener.
xlheader.h, which in turn calls the OpenAL headers -- view this section of code here.
Now let's look at
audioenv.cpp in the
openal/demos/XLDEMO/COMMON directory. View that code here.
These code fragments include most of the OpenAL calls used in the xldemo program. As you can see, quite a lot gets done by not a lot of code. The OpenAL API functions take care of the gritty details, freeing the programmer from considerable tedious calculations. Furthermore, both OpenGL and OpenAL share the same mechanisms for determining the camera/listener position, velocity, angle, etc., all of which get updated simultaneously for visuals and audio, but I leave that investigation to the motivated reader. More examples of interesting OpenAL code can be found in the sources to the Chromium BSU game and the excellent tutorial by Fotis Hadginikos.
Discuss this article in the O'Reilly Network Linux Forum.
Return to the Linux DevCenter.