Delphi

OpenAL

Tutorials

Lesson #2 (moving sound)

You have to use the result of Lesson #1 as the base for this lesson. Some changes have to be made in order to have sound to move in 3d space.

As the sound is now moving we have to change the sourcevel value into:
sourcevel: array [0..2] of TALfloat= ( 0.5, 0.0, 0.5);
It moves from left to right and front to back.

The sourcepos has to be changed to:
sourcepos: array [0..2] of TALfloat= ( -5.0, 0.0, -5.0 );
It is placed left and front of you.

Also we want some more interesting sound as ding so we use:
AlutLoadWavFile('footsteps.wav', format, data, size, freq, loop);

If you haven't done already make the sound loopable:
AlSourcei ( source, AL_LOOPING, AL_TRUE);

Now we need something to change the source position over time. The easiest is to use a timer for that, so place one on the form. Set it to disabled. In the ontimer event you place the following code:
SourcePos[0] := SourcePos[0] + SourceVel[0];
SourcePos[1] := SourcePos[1] + SourceVel[1];
SourcePos[2] := SourcePos[2] + SourceVel[2];
If SourcePos[0] >= 5 then SourcePos[0]:=-5;
If SourcePos[2] >= 5 then SourcePos[2]:=-5;
alSourcefv(source, AL_POSITION, @SourcePos);

Every second the sourcevel is added to the soundpos and the new soundpos is passed on to OpenAL. If the source is at right back of you, the sourcepos is reset to its initial position.

At the play button code you need to add the following code at the end:
Timer1.Enabled:=True;

At the stop button code you need to add the following code at the beginning:
Timer1.Enabled:=False;