Just realised I never published my OpenAL library

It is for simple sounds - there is no fading, moving, stereo etc. This is designed for straight forward puzzle games. This library was created after reading through the OpanAL site.

The Library contains two objects. The main Object is the SoundEngine that controls and manages the various sound items.

Make sure that the file OpenAL.dll is avaialble on the destination PC (I include it in my install program).

[pascal]unit ALSound;

interface

// ------------------------------------------
//
// ALSound
//
// (C) CairnsGames S.A. 2004
//
// Version 1.0 2004/07/12
//
//-------------------------------------------

uses
Classes, al, altypes, alut;

Type
TOpenALSoundItem = Class
Private
buffer : TALuint;
source : TALuint;
Volume : Integer;
FName: String;
procedure SetName(const Value: String);
Public
Property Name : String read FName write SetName;
Constructor Create; Overload;
Constructor Create(FileName : String); Overload;
Destructor Destroy; Override;
Procedure Play(NewVolume : Integer); Overload;
Procedure Play; Overload;
Procedure Stop;
Procedure Pause;
End;
TOpenALSoundEngine = Class(TComponent)
Private
FItems: TList;
procedure SetItems(const Value: TList);
Public
Property Items : TList read FItems write SetItems;
Constructor Create(AnOwner : TComponent); Override;
Destructor Destroy; Override;
Procedure Load(Name, FileName : String);
Procedure Play(Index : Integer; Volume : Integer = 100);
Procedure Stop(Index : Integer);
Procedure Pause(Index : Integer);
Function Find(SoundName : String) : TOpenALSoundItem;
End;

implementation

{ TOpenALSoundEngine }

const
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);


constructor TOpenALSoundEngine.Create(AnOwner: TComponent);
var
argv: array of PChar;
Item : TOpenALSoundItem;
begin
Items := TList.Create;
AlutInit(nil,argv);

Item := TOpenALSoundItem.Create;
Items.Add(Item);
end;

destructor TOpenALSoundEngine.Destroy;
begin
AlutExit();

end;

function TOpenALSoundEngine.Find(SoundName: String): TOpenALSoundItem;
Var
I : Integer;
SoundItem : TOpenALSoundItem;
begin
For I := 0 to Items.Count - 1 do
Begin
SoundItem := TOpenALSoundItem(Items[I]);
If SoundItem.Name = SoundName then
Begin
Result := SoundItem;
Exit;
End;
End;
Result := Nil;
end;

procedure TOpenALSoundEngine.Load(Name, FileName: String);
var
Item : TOpenALSoundItem;
begin
Item := TOpenALSoundItem.Create(FileName);
Item.Name := Name;
Items.Add(Item);
end;

procedure TOpenALSoundEngine.Pause(Index: Integer);
begin
TOpenALSoundItem(Items[Index]).Pause;


end;

procedure TOpenALSoundEngine.Play(Index: Integer; Volume : Integer);
begin
TOpenALSoundItem(Items[Index]).Play(Volume);
end;

procedure TOpenALSoundEngine.SetItems(const Value: TList);
begin
FItems := Value;
end;

procedure TOpenALSoundEngine.Stop(Index: Integer);
begin
TOpenALSoundItem(Items[Index]).Stop;

end;

{ TOpenALSoundItem }

constructor TOpenALSoundItem.Create;
var
format: TALEnum;
size: TALSizei;
freq: TALSizei;
loop: TALInt;
data: TALVoid;
begin
AlGenBuffers(1, @buffer);
// AlutLoadWavFile('..\sound\dog1.wav', format, data, size, freq, loop);
AlBufferData(buffer, format, data, size, freq);
AlutUnloadWav(format, data, size, freq);

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

AlListenerfv ( AL_POSITION, @listenerpos);
AlListenerfv ( AL_VELOCITY, @listenervel);
AlListenerfv ( AL_ORIENTATION, @listenerori);

Volume := 100;
end;

constructor TOpenALSoundItem.Create(FileName: String);
var
format: TALEnum;
size: TALSizei;
freq: TALSizei;
loop: TALInt;
data: TALVoid;
begin
AlGenBuffers(1, @buffer);
AlutLoadWavFile(FileName, format, data, size, freq, loop);
AlBufferData(buffer, format, data, size, freq);
AlutUnloadWav(format, data, size, freq);

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

AlListenerfv ( AL_POSITION, @listenerpos);
AlListenerfv ( AL_VELOCITY, @listenervel);
AlListenerfv ( AL_ORIENTATION, @listenerori);

Volume := 100;
end;

destructor TOpenALSoundItem.Destroy;
begin
AlDeleteBuffers(1, @buffer);
AlDeleteSources(1, @source);
end;

procedure TOpenALSoundItem.Pause;
begin
AlSourcePause(source);
end;

procedure TOpenALSoundItem.Play(NewVolume : Integer);
begin
Volume := NewVolume;
AlSourcef (source, AL_GAIN, Volume / 100);
AlSourcePlay(source);
end;

procedure TOpenALSoundItem.Play;
begin
AlSourcef ( source, AL_GAIN, Volume / 100);
AlSourcePlay(source);
end;

procedure TOpenALSoundItem.SetName(const Value: String);
begin
FName := Value;
end;

procedure TOpenALSoundItem.Stop;
begin
AlSourceStop(source);
end;

end.[/pascal]

To Use the Library you first need to create the engine, load a sound and play it.

To create the engine and load some sounds:
[pascal]procedure TMainForm.LoadSounds;
begin
SoundEngine := TOpenALSoundEngine.Create(Self);
SoundEngine.Load('Click1','Sound\Click1.wav');
SoundEngine.Load('Click2','Sound\Click2.wav');
end;
[/pascal]

To Play a sound you just find the sound and call play:
[pascal] SoundEngine.Find('Click1').Play;
[/pascal]


Actually quite simple.