OpenAL Lesson 3: Multiple Sources(转载)

转载http://www.devmaster.net/articles/openal-tutorials/lesson3.php

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 >
#include 
< time.h >
#include 
< stdlib.h >
#include 
< al / al.h >
#include 
< al / alc.h >
#include 
< al / alu.h >
#include 
< al / alut.c >

//  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[]  =   0.00.00.0 } ;

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

//  Orientation of the listener. (first 3 elements are "at", second 3 are "up")
ALfloat ListenerOri[]  =   0.00.0-1.00.01.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;
    ALvoid
* data;
    ALsizei freq;
    ALboolean loop;

    
// Load wav data into buffers.

    alGenBuffers(NUM_BUFFERS, Buffers);

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

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

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

    alutLoadWAVFile(
"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()
{
    alDeleteBuffers(NUM_BUFFERS, 
&Buffers[0]);
    alDeleteSources(NUM_SOURCES, 
&Sources[0]);
    alutExit();
}

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);
    alGetError();

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

    SetListenerValues();

    
// Setup an exit procedure.
    atexit(KillALData);

    
// Begin the battle sample to play.
    alSourcePlay(Sources[BATTLE]);

    
// 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, 
&play);

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

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

                SourcePos[i][
0= -float(cos(theta));
                SourcePos[i][
1= -float(rand()%2);
                SourcePos[i][
2= -float(sin(theta));

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

                alSourcePlay(Sourcev[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 lightonthewater@hotmail.com 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!

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值