Delphi

OpenAL

Tutorials

Lesson #3 (multiple sources)

One moving sound is not that special, what about multiple sounds playing at once? You have to use the code made in lesson #2 as the base for this lesson.

Multiple sound sources is not that difficult. In your unit add a const section containing:
numbuffers = 3;
numsources = 3;
walk = 0;
ding = 1;
zap = 2;

This means we will now use 3 buffers and 3 sources. Also we give some easy names to the sources.

The buffer and source var need to be replaced with:
buffer : array [0..numbuffers] of TALuint;
source : array [0..numsources] of TALuint;

That gives you an array of buffers and an array of sources. Now every buffer has to be initialised in the oncreate event:
alGenBuffers(numbuffers, buffer);
alGenSources(numsources, source)

and each sound has to be loaded:
AlutLoadWavFile(’footsteps.wav’, format, data, size, freq, loop);
AlBufferData(buffer[walk], format, data, size, freq);
AlutUnloadWav(format, data, size, freq);

AlutLoadWavFile(’ding.wav’, format, data, size, freq, loop);
AlBufferData(buffer[ding], format, data, size, freq);
AlutUnloadWav(format, data, size, freq);

AlutLoadWavFile(’phaser.wav’, format, data, size, freq, loop);
AlBufferData(buffer[zap], format, data, size, freq);
AlutUnloadWav(format, data, size, freq);

Also we have to set each source:
AlSourcei ( source[walk], AL_BUFFER, buffer[walk]);
AlSourcef ( source[walk], AL_PITCH, 1.0 );
AlSourcef ( source[walk], AL_GAIN, 1.0 );
AlSourcefv ( source[walk], AL_POSITION, @sourcepos);
AlSourcefv ( source[walk], AL_VELOCITY, @sourcevel);
AlSourcei ( source[walk], AL_LOOPING, AL_TRUE);

You need to add the same for ding and zap.

Also for play stop and pause the single source has to be replaced with multiple sources for play this looks like:
AlSourcePlay(source[walk]);
AlSourcePlay(source[ding]);
AlSourcePlay(source[zap]);

The same needs to be done for pause and stop. You might want to make buttons for play, stop and pause for each source instead of playing al sounds at once with the same button.

The ontimer event also needs calls to multiple sources instead of one. You should have an idea on thow to change this.

Now all the sources move in the same way. That is not so interesting. Change the ontimer event so that it only updates the walk source. In front of setting the source for ding and zap place something like this:
SourcePos[0] := 8.0;
SourcePos[1] := 1.0;
SourcePos[2] := -8.0;

You may also want to lower the gain (loudnes) of ding and zap to 0.2 The walk could use a boost to 1.5 . But you could place ding and zap further away to make them sound softer.