OpenAL
From GPWikiThis page is far from complete, go ahead and write some more!
You can extend this page by editing it. OpenAL is a cross platform audio API. It is used in many games and audio applications, including the Unreal Tournament games and the Torque Engine. It is licensed under the GNU LGPL.
[edit] General Sound Theory[edit] The ListenerSound takes form in waves through a medium, usually the air. General wave theory applies to sound waves although an in depth discussion of wave theory is beyond the scope of this article. However a few key concepts will help guide you in adding high quality sound components to your games. First, our primary concern is the listener. It sounds obvious, but it can cause a lot of headaches when thinking about multiple sources and the effects like the doppler effect. In general, we concern all calculations and orientation with regard to the listener. [edit] Wave SpeedAnother key feature of sound is that it has velocity through it's medium. This is important because we hardly ever mention units in game programming. The reason is that, in general, units don't matter...as long as you are consistent. That is to say if you use yards for your units in graphics, then your sound will be moving in yards per second. Although you must pay attention if you change units to inches or miles which is one reason why metric units are preferred in science and engineering; no nasty conversions needed. [edit] Sound MediumOf course, the wave travels through a medium. This is usually air, but not always. For example, if you want realistic sound under water, you will need to take into account the density of the medium. Again, the density of the medium should be coordinated with your project to use the same units. To do otherwise will produce unexpected results. Here's the part where we break from reality a little. In space there is no sound because there is no medium in a vacuum. However, space games with no sound aren't much fun. So, it's a bit of knowing what levels of detail are and are not necessary. TipUse a single data structure to hold an object's position, velocity, and orientation for use in your physics engine, graphics engine, and sound engine. [edit] Loading a Sound SampleLoading a sound source in OpenAL is extremely similar to loading textures in OpenGL. First a sound is loaded into memory as a sound resource called a buffer and is recalled by an identification number similar to the texture id found in OpenGL. The sound can then be applied to sources or points in space from which the sound will emanate. This method is simple and eliminates the need to load an audio sample more than once. ALuint buffer; ALuint source; ALenum format; ALsizei size; ALvoid* data; ALsizei freq; ALboolean loop; alGenBuffers(1, &buffer); if (alGetError() != AL_NO_ERROR) return AL_FALSE; alutLoadWAVFile("sound.wav", &format, &data, &size, &freq, &loop); alBufferData(buffer, format, data, size, freq); alutUnloadWAV(format, data, size, freq); [edit] Applying a Sound SourceApplying the sound source is very similar to applying a texture in OpenGL which uses a paint by numbers method of applying graphical resources. The same sound buffer can be applied to many sound sources. First, we need to set-up the source with a few properties. The sound needs a position and a velocity. Be careful to be consistent with your units. The units that you used to position objects graphically should also be used for position and velocity. ALfloat sposition[] = { 0.f, 0.f, 0.f }; ALfloat svelocity[] = { 0.f, 0.f, 0.f }; alSourcei (source, AL_BUFFER, buffer); alSourcefv (source, AL_POSITION, sposition); alSourcefv (source, AL_VELOCITY, svelocity); [edit] Positioning the ListenerFinally you will need to position the listener. The listener is very similar to setting up a camera and viewing volume in OpenGL. First, the listener also has a position and a velocity. The listener has another set of important properties; orientation. The sound sources are assumed to emanate in all directions. The listener however does not hear sounds equally from all directions because of binaural hearing; ears. So, a forward direction and an up direction are needed to define an orientation. ALfloat lposition[] = { 0.f, 0.f, 0.f }; ALfloat lvelocity[] = { 0.f, 0.f, 0.f }; ALfloat lorientation[] = { 0.f, 0.f, -1.f, 0.f, 1.f, 0.f }; alListenerfv(AL_POSITION, lposition); alListenerfv(AL_VELOCITY, lvelocity); alListenerfv(AL_ORIENTATION, lorientation); [edit] Playing the SoundOnce the sound sources and listener are set-up, the rest is easy. There are three main commands to playing a sound: alSourcePlay, alSourcePause, and alSourceStop. The difference between alSourceStop and alSourcePause is that stop will reset the sound to the beginning and pause does not. The alSourcePlay command is pretty self-explanatory and plays a sound from either the beginning or from it's paused location. // plays a sound from it's current listening position alSourcePlay(source); // stops the sound and moves listening position to the beginning alSourceStop(source); // pauses the sound alSourcePause(source);
[edit] Tutorials and Source[edit] External LinksCategories: Stub | Library | Audio |
Tip

