OpenAL Lesson 3: Multiple Sources(转载)

转载 2007年10月14日 19:53:00


Hello. It's been a while since my last tutorial. But better late than never I guess. Since I'm sure your all impatient to read the latest tutorial, I'll just jump right into it. What we hope to accomplish with this one is to be able to play more that one audio sample at a time. Very intense games have all kinds of stuff going on usually involving different sound clips. It won't be hard to implement any of this though. Doing multiple sounds is similar to doing just one.

#include <conio.h>

// Maximum data buffers we will need.
#define NUM_BUFFERS 3

// Maximum emissions we will need.
#define NUM_SOURCES 3

// These index the buffers and sources.
#define BATTLE      0
#define GUN1        1
#define GUN2        2

// Buffers hold sound data.
ALuint Buffers[NUM_BUFFERS];

// Sources are points of emitting sound.
ALuint Sources[NUM_SOURCES];

// Position of the source sounds.
ALfloat SourcesPos[NUM_SOURCES][3];

// Velocity of the source sounds.
ALfloat SourcesVel[NUM_SOURCES][3];

// Position of the listener.
ALfloat ListenerPos[] = ... };

// Velocity of the listener.
ALfloat ListenerVel[] = ... };

// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
ALfloat ListenerOri[] = ...0.00.0- };

I guess this little piece of source code will be familiar to a lot of you who've read the first two tutorials. The only difference is that we now have 3 different sound effects that we are going to load into the OpenAL sound system.

ALboolean LoadALData()
// Variables to load into.

    ALenum format;
    ALsizei size;
* data;
    ALsizei freq;
    ALboolean loop;

// Load wav data into buffers.

    alGenBuffers(NUM_BUFFERS, Buffers);

if (alGetError() != AL_NO_ERROR)
return AL_FALSE;

"wavdata/Battle.wav"&format, &data, &size, &freq, &loop);
    alBufferData(Buffers[BATTLE], format, data, size, freq);
    alutUnloadWAV(format, data, size, freq);

"wavdata/Gun1.wav"&format, &data, &size, &freq, &loop);
    alBufferData(Buffers[GUN1], format, data, size, freq);
    alutUnloadWAV(format, data, size, freq);

"wavdata/Gun2.wav"&format, &data, &size, &freq, &loop);
    alBufferData(Buffers[GUN2], format, data, size, freq);
    alutUnloadWAV(format, data, size, freq);

// Bind buffers into audio sources.

    alGenSources(NUM_SOURCES, Sources);

if (alGetError() != AL_NO_ERROR)
return AL_FALSE;

    alSourcei (Sources[BATTLE], AL_BUFFER,   Buffers[BATTLE]  );
    alSourcef (Sources[BATTLE], AL_PITCH,    
1.0              );
    alSourcef (Sources[BATTLE], AL_GAIN,     
1.0              );
    alSourcefv(Sources[BATTLE], AL_POSITION, SourcePos[BATTLE]);
    alSourcefv(Sources[BATTLE], AL_VELOCITY, SourceVel[BATTLE]);
    alSourcei (Sources[BATTLE], AL_LOOPING,  AL_TRUE          );

    alSourcei (Sources[GUN1], AL_BUFFER,   Buffers[GUN1]  );
    alSourcef (Sources[GUN1], AL_PITCH,    
1.0            );
    alSourcef (Sources[GUN1], AL_GAIN,     
1.0            );
    alSourcefv(Sources[GUN1], AL_POSITION, SourcePos[GUN1]);
    alSourcefv(Sources[GUN1], AL_VELOCITY, SourceVel[GUN1]);
    alSourcei (Sources[GUN1], AL_LOOPING,  AL_FALSE       );

    alSourcei (Sources[GUN2], AL_BUFFER,   Buffers[GUN2]  );
    alSourcef (Sources[GUN2], AL_PITCH,    
1.0            );
    alSourcef (Sources[GUN2], AL_GAIN,     
1.0            );
    alSourcefv(Sources[GUN2], AL_POSITION, SourcePos[GUN2]);
    alSourcefv(Sources[GUN2], AL_VELOCITY, SourceVel[GUN2]);
    alSourcei (Sources[GUN2], AL_LOOPING,  AL_FALSE       );

// Do another error check and return.

if( alGetError() != AL_NO_ERROR)
return AL_FALSE;

return AL_TRUE;

This code looks quite a bit different at first, but it isn't really. Basically we load the file data into our 3 buffers, then lock the 3 buffers to our 3 sources relatively. The only other difference is that the "Battle.wav" (Source index 0) is looping while the rest are not.

void SetListenerValues()
    alListenerfv(AL_POSITION,    ListenerPos);
    alListenerfv(AL_VELOCITY,    ListenerVel);
    alListenerfv(AL_ORIENTATION, ListenerOri);

void KillALData()

I don't think we changed anything in this code.

int main(int argc, char *argv[])
// Initialize OpenAL and clear the error bit.
    alutInit(NULL, 0);

// Load the wav data.
    if (LoadALData() == AL_FALSE)
return 0;


// Setup an exit procedure.

// Begin the battle sample to play.

// Go through all the sources and check that they are playing.
// Skip the first source because it is looping anyway (will always be playing).
    ALint play;

while (!kbhit())
for (int i = 1; i < NUM_SOURCES; i++)
                alGetSourcei(Sources[i], AL_SOURCE_STATE, 

if (play != AL_PLAYING)
// Pick a random position around the listener to play the source.

double theta = (double) (rand() % 360* 3.14 / 180.0;

0= -float(cos(theta));
1= -float(rand()%2);
2= -float(sin(theta));

                alSourcefv(Sources[i], AL_POSITION, SourcePos[i] );




return 0;

Here is the interesting part of this tutorial. We go through each of the sources to make sure it's playing. If it is not then we set it to play but we select a new point in 3D space for it to play (just for kicks).

And bang! We are done. As most of you have probably seen, you don't have to do anything special to play more than one source at a time. OpenAL will handle all the mixing features to get the sounds right for their respective distances and velocities. And when it comes right down to it, isn't that the beauty of OpenAL?

You know that was a lot easier than I thought. I don't know why I waited so long to write it. Anyway, if anyone reading wants to see something specific in future tutorials (not necessarily pertaining to OpenAL, I have quite an extensive knowledge base) drop me a line at I plan to do tutorials on sharing buffers and the Doppler effect in some later tutorial unless there is request for something else. Have fun with the code!


Ubuntu 16.04 LTS安装搜狗输入法解析

通过搜狗官方网站(如下): 下载的deb包属于dpkg包,Ubuntu软件中心照理说是应该能够安装的,但由于是第三方程序...
  • u010447234
  • u010447234
  • 2016年12月10日 14:12
  • 874


因为之前在原公司上班的时候,用过openAL来播放PCM实时数据流.现在换了新公司又要求我用自己的方法写个音频播放器.我就选用这openAL H-,RzL/   但是这次使用openAL的时候,就...
  • xiaojun111111
  • xiaojun111111
  • 2015年03月05日 16:47
  • 608


不使用alut,只使用openal播放WAV文件: #include #include struct WAVE_Data { char subChunkID[4]; //should con...
  • u011417605
  • u011417605
  • 2015年11月05日 18:48
  • 1207


OpenAL(Open Audio Library)是自由软件界的跨平台音效API。它设计给多通道三维位置音效的特效表现。其 API 风格模仿自 OpenGL。刚开始接触的时候可以下载工具包Alut,...
  • T20091
  • T20091
  • 2013年11月29日 19:04
  • 1839


使用OpenAL和FFMPEG解码并播放音乐OpenAL是一个开源的音效库,然而这里只用它来播放音乐。 FFMPEG负责把音乐解码并转换成指定的编码和采样率,然后送到OpenAL中播放。 (已在w...
  • beichen2012
  • beichen2012
  • 2017年03月10日 11:18
  • 760


1. 前言前面的文章讲了如何使用OpenAL对音频添加音效,并播放,参见: OpenAL 使用基本流程 使用OpenAL混音,添加音频特效本文谈谈如何对音频进行渲染,然后存储下来。2. 初始化初始...
  • xiyanlgu
  • xiyanlgu
  • 2016年12月17日 11:24
  • 879


  • stonesing
  • stonesing
  • 2016年08月25日 17:07
  • 2092

Lesson 3: 创建窗口

  • coderNim
  • coderNim
  • 2016年12月12日 19:02
  • 106

Lesson 3:复合类型

  • ko_tin
  • ko_tin
  • 2016年07月04日 11:12
  • 325


转载地址: 1、找到要转载的文章,用chrome浏览器打开,右键选择审查元...
  • dlhlSC
  • dlhlSC
  • 2016年08月19日 16:51
  • 5068
您举报文章:OpenAL Lesson 3: Multiple Sources(转载)