【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 2342|回复: 0

Programming 3D Sound With OpenAL

[复制链接]
发表于 2006-10-24 14:18:00 | 显示全部楼层 |阅读模式

Introduction

As the capabilities of sound cards and sound APIs have increased over the years, 3D sound has played an increasingly important role in games. Creating an immersive experience for the gamer goes beyond nice looking graphics. Done correctly, ambient sound and music can take games to a whole new level. To achieve this effect, different sound programming APIs have been developed such as Microsoft's DirectSound and DirectSound3D, Aureal's A3D and Creative Labs EAX. More recently, Loki Entertainment and Creative Labs have developed OpenAL. They have done this in an effort to create a more standard, cross-platform API to allow developers to add sound to any title. This article explains the basics of OpenAL and how to get started adding 3D sound to your project.

OpenAL is basically an audio library that contains functions for playing back sounds and music in a game environment. It allows a programmer to load whatever sounds they like and control certain characteristics such as position, velocity, direction and angles for cones that determine how the sound is traveling. All sounds are positioned relative to the listener which represents the current place in the game universe where the user is. The closer the listener gets to a sound, the louder it gets. OpenAL can also be used for the playback of music. OpenAL can make use of streaming to continuously play music back in the background. OpenAL supports the use of DirectSound3D and EAX. There are functions contained in the library that allow the use of these APIs without any extra programming.

The main advantage of OpenAL over DirectSound and EAX is that OpenAL is designed to be a cross platform API. It is possible to download Windows, Mac and
Linux binaries of it so it can be used on many Operating Systems. DirectSound and DirectSound3D were designed only for Windows and EAX is basically an extension of DirectSound3D. For those wishing to create an application for multiple Operating Systems, OpenAL might be the way to go. This tutorial is designed to show you how to get basic functionality out of OpenAL in Windows. The code shown here is from the demo application that you can download which contains the complete example. OpenGL is used for the rendering so the demo
shows some similarities between OpenGL and OpenAL.

Getting Started

The first thing to do is to obtain the OpenAL SDK. This can be obtained from Creative Labs developer site at [URL=http://developer.creative.com]. Once the SDK is set up you can begin using its functions. You will first need to initialize OpenAL at the start of your game or application. For my application I have chosen to use DirectSound3D but it is also possible to make use of EAX. The initialization code looks like this:

ALCcontext *Context;
ALCdevice *Device;
Device = alcOpenDevice((ALubyte*)"DirectSound3D");

if (Device == NULL)
exit(-1);

//Create context(s)
Context=alcCreateContext(Device, NULL);
//Set active context
alcMakeContextCurrent(Context);
// Clear Error Code
alGetError();

Loading Sounds

Once OpenAL has been initialized you can start filling buffers with sounds. The first step is to fill a buffer with sound using the alutLoadWAVFile function. Once you have filled the buffer you need to attach it to a source. This source can then be used to play back the sound. The code for loading a sound looks like this:

char* alBuffer;            //data for the buffer
ALenum alFormatBuffer; //for the buffer format
ALsizei alFreqBuffer; //for the frequency of the buffer
long alBufferLen; //the bit depth
ALboolean alLoop; //looped
unsigned int alSource; //buffer source
unsigned int alSampleSet;
//load the wave file
alutLoadWAVFile("test.wav",&alFormatBuffer, (void**) &alBuffer, (unsigned int *)&alBufferLen, &alFreqBuffer, &loop);

//create 1 source
alGenSources(1, &alSource);

//create 1 buffer
alGenBuffers(1, &alSampleSet);

//fills sample set with buffer data from alBuffer
alBufferData(alSampleSet, alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);

//assign the buffer to this source
alSourcei(alSource, AL_BUFFER, alSampleSet);

//release the data
alutUnloadWAV(alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);

The first step here was to load the file "test.wav". The data is read in and stored in alBuffer. Next a source and a buffer are created. Then the buffer is filled with data fromalBuffer. Next the buffer is assigned to the source and the wave data is released.

Setting Source Properties

Once you have a sound source set up you will need to set some of its properties. To do this you use the alSource command. This function works similar to functions in OpenGL where different versions of the function exist such as alSourcei,alSourcef, alSource3f, and so on. The properties you are setting determine which function you use. Setting something like pitch for example just needs one floating point parameter so alSourcef is used. The first parameter passed to the function is that of the source you are modifying. The second parameter corresponds to what property you are changing. The rest of the parameters are the actual settings for this property. Here is an example of setting position and velocity for a sound source:

//set source position
alSource3f(alSource,AL_POSITION, xposition, yposition, zposition);

//set source velocity
alSource3f(alSource,AL_VELOCITY, xvelocity, yvelocity, zvelocity);

Here alSource is our sound source andAL_POSITION and AL_VELOCITY signal that we want to change the position and velocity parameters. The rest correspond to floating point numbers with preset values.

Using a Listener

The use of a listener in 3D sound is important so that you have a reference point from which to hear sounds. The closer a listener is to a sound source's position, the louder the sound will get provided there are no obstacles. OpenAL already has a listener object set up so all you need to do is modify its properties as needed. The most important properties of the listener are the position and orientation of the listener. These properties are generally updated once for every loop in a game. The position indicates where the listener currently is while the orientation is where the listener is facing. For example, if a listener is facing west and a sound is coming from the north, the sound will appear to come from the right. However if the user orientation changes and turns to face the north, the sound will now appear to come from straight ahead.

Modifying a listener's properties works similarly to the way you change a sound source's properties. The function alListener is used and has variations depending on the property you are modifying. For example, to set the listener position you can call alListener3f and specify 3 separate floating point numbers or you could call alListenerfv and pass an array of floating point numbers. The parameters for the position are simply the x, y and z values for the listener in the game world. The orientation property however takes 6 parameters. The first 3 are for the vector that corresponds to what the user of listener is currently looking at. The next 3 are for the vector leading straight up from where the listener is facing. Here is an example of setting the listener position and orientation:

float listenerx, listenery, listenerz;
float vec[6];

listenerx=10.0f;
listenery=0.0f;
listenerz=5.0f;

vec[0] = fvecx; //forward vector x value
vec[1] = fvecy; //forward vector y value
vec[2] = fvecz; //forward vector z value
vec[3] = uvecx; //up vector x value
vec[4] = uvecy; //up vector y value
vec[5] = uvecz; //up vector z value//set current listener position
alListener3f(AL_POSITION, listenerx, listenery, listenerz);

//set current listener orientation
alListenerfv(AL_ORIENTATION, vec);

In the first function call, 3 separate floating point numbers were passed for the x, y and z of the listener's position. For the second call, an array called vec is passed. This array is comprised of the six values for the two vectors dealing with the listener's orientation.

Playing Sounds

Once your sounds are loaded and you have set your listener properties you can then play your sounds. To do this you call the function alSourcePlay. This function takes a single parameter which is the source you want to play. To stop a sound you call the function alSourceStop which also takes the name of the source you want to stop. If you want your sound effect to loop such as if you have a sound of running water, you can set the looping property of the sound to true before playing the sound. Here is some sample code:

//tell the sound to loop continuously
alSourcei(alSource,AL_LOOPING,AL_TRUE);

//play the sound
alSourcePlay(alSource);

// To stop the sound
alSourceStop(alSource);

Cleaning Up

Once you are finished with your program you need to free up your sources and buffers and shut down OpenAL. The commands are relatively simple. Here is how to delete your sources and buffers:

//delete our source
alDeleteSources(1,&alSource);
//delete our buffer
alDeleteBuffers(1,&alSampleSet);

Here the first parameter for each is the number of buffers or sources to be deleted and the second is the buffer or source to be deleted. The last step is to close out OpenAL itself. This is done as follows:

//Get active context
Context=alcGetCurrentContext();
//Get device for active context
Device=alcGetContextsDevice(Context);
//Disable context
alcMakeContextCurrent(NULL);
//Release context(s)
alcDestroyContext(Context);
//Close device
alcCloseDevice(Device);

That's about it for playing basic sounds in OpenAL. The basic steps are to first initialize OpenAL, then load in whatever sounds you need, set the properties of the sounds and listener and then play the sounds. There is a lot more you can do with OpenAL but this was just written to describe some of the basics. The source code that comes along with this demonstrates the use of all of this in an OpenGL demo. In the demo, you control the listener and its position is set to your current position every loop. If you have any questions or comments, e-mail me at ricart3@tcnj.edu

Download source code for this article
Discuss this article in the forums
Print article

[此贴子已经被作者于2006-11-9 16:06:24编辑过]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-2-6 03:56

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表