Lesson #1 (one sound at one position)

In this first tutorial you will learn how to play a single sound using OpenAL.

First start Delphi and start a new new (vcl) program. Then extend the uses line with:

Add the folowing lines to the var section:
buffer : TALuint;
source : TALuint;
sourcepos: array [0..2] of TALfloat= ( 0.0, 0.0, 0.0 );
sourcevel: array [0..2] of TALfloat= ( 0.0, 0.0, 0.0 );
listenerpos: array [0..2] of TALfloat= ( 0.0, 0.0, 0.0);
listenervel: array [0..2] of TALfloat= ( 0.0, 0.0, 0.0);
listenerori: array [0..5] of TALfloat= ( 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);

The buffer var will be used to store the actual sound that will be played. The source(...) vars will be used to describe where and how the sound will be played. The listener(...) vars are used to describe where the sound is heard in 3d space. For now we leave them at the center of 3D space.

Before OpenAl is able to play any sound it needs to be initialized. Therefore the following has to be added to your forms oncreate event:
argv: array of PalByte;
format: TALEnum;
size: TALSizei;
freq: TALSizei;
loop: TALInt;
data: TALVoid;

First you need to init the openal library in the forms oncreate event:

Next in the forms oncreate event we have init opeal like this. It will automaticaly choose the best sound hardware on your pc.

Next you have to create a buffer to hold the sound data loaded from a .wav file. Once the .wav file is loaded into the buffer you can unload it again to save some memory.
AlGenBuffers(1, @buffer);
AlutLoadWavFile(’ding.wav’, format, data, size, freq, loop);
AlBufferData(buffer, format, data, size, freq);
AlutUnloadWav(format, data, size, freq);

If you want to hear the sound you have to say where it is in 3d space, how loud it is, if it is looped, etc...
AlGenSources(1, @source);
AlSourcei ( source, AL_BUFFER, buffer);
AlSourcef ( source, AL_PITCH, 1.0 );
AlSourcef ( source, AL_GAIN, 1.0 );
AlSourcefv ( source, AL_POSITION, @sourcepos);
AlSourcefv ( source, AL_VELOCITY, @sourcevel);
AlSourcei ( source, AL_LOOPING, loop);
With AL_PITCH you can make the source sound higher.
With AL_GAIN you specify how loud the sound is played.

Next you have to specify where the sound is heard. This can be where the camera is located or where the object is that represents the player.
AlListenerfv ( AL_POSITION, @listenerpos);
AlListenerfv ( AL_VELOCITY, @listenervel);
AlListenerfv ( AL_ORIENTATION, @listenerori);

Now your application is almost ready to play some sounds.

Add a button to the form, name it play and add the following code to it:

Add a button to the form, name it stop and add the following code to it:

Add a button to the form, name it pause and add the following code to it:

In order to close the application with no memory leaks your forms ondestroy event needs to have the following code:
AlDeleteBuffers(1, @buffer);
AlDeleteSources(1, @source);

Now you are ready to execute your first OpenAL program. Click on Play to start playing your sound once.

If you want to loop your sound you have to change the line:
AlSourcei ( source, AL_LOOPING, loop );
AlSourcei ( source, AL_LOOPING, AL_TRUE );

Now you can use the stop and pause buttons to stop or pause the sound. Try to change the pitch and gain values to hear what they do. Also try to change the sourcepos value to some other positions.