Create the audio engine

Start by adding the phxAudio unit to the uses list. We also need to include a provider, in this case we use the Bass provider. There is a OpenAL provider included aswell but it is not completed as of 2013-08-08

Now we have to create a instance of the engine as well as initialize it. Calling the Initialize function of the audio engine initializes the audio using the default sound device that is selected in the os.

procedure TGame.Init;begin...// Create the audio engine using the included BASS provider
Audio:= TPHXAudioEngine.Create;// Initialize the audio engine using the default audio device
Audio.Initialize;end;

Selecting the audio device

To select the audio output device we first have to determine the name or index of the device to use. The Devices property of the audio engine contains all supported devices and are initialized when the audio engine is creates.

Playing audio samples

To play a loaded audio sample we can use the Play method of the audio engine.

procedure TGame.Update;begin...// Update the audio engine
Audio.Update;// Play the bomb sample with the left mouse buttonif isButton1 in Input.Statesthenbegin
Audio.Play('Bomb');
Input.States:= Input.States-[isButton1];end;// Play the missile sample the right mouse buttonif isButton2 in Input.Statesthenbegin
Audio.Play('Missile');
Input.States:= Input.States-[isButton2];end;end;

It is also possible to play the sample with a specific volume

// Play the missile sample at 50% volume with the right mouse buttonif isButton3 in Input.Statesthenbegin
Audio.Play('Missile',0.5);// Remove the state
Input.States:= Input.States-[isButton3];end;

Loading audio streams

Audio streams are used to play music that are to large to upload into memory before playing, there might be some delay when starting playing them due to the disk access.

To load a music stream we call the LoadSample function of the audio engine with the filename and optional name of the audio file.

procedure TGame.Init;begin...// Streams are streamed from disk and are used for music
Music:= Audio.LoadStream('string-1-loop.wav','Music');// We want to loop the music
Music.Looped:=True;// Set the volume of the music from 0.0 (silent) to 1.0 (full)
Music.Volume:=0.5;// Play the music
Music.Play;end;

This function returns a TPHXAudioStream instance where it is possible to set if the stream should be looped and read the duration and position of the sample.