Contents

Abstract

Phonon is not really designed to handle multiple sounds at the same time. Gluon's audio module is made for games and create one thread per sound, using the openAL library. You also get all the other openAL goodness like 3D sounds, effects and multichannel.

The Code

In this example, we will play a simple sound file as "Hello Word".

#include "audio/sound.h"#include <QtCore/QDebug>intmain(intargc,char*argv[]){// create a sound objectGluonAudio::Sound*sound=newGluonAudio::Sound;// load KDE's log in sound to the objectsound->load("/usr/share/sounds/KDE-Sys-Log-In-Long.ogg");// set sound volume, it can be between 0 and 1sound->setVolume(0.9);// print the elapsed time until the sound has finished playingqDebug()<<"Playing sound with duration"<<sound->duration()<<"seconds.";// play the soundsound->play();// wait until playing is finishedwhile(sound->isPlaying());// clear the bufferdeletesound;return0;}

- First, we initialize the sound engine. The init() function can optionally takes one argument, the device name. You can retrieve the list of devices, using this static function :

QStringListdevicesList().

- Then, we create a KALsource. KALSource can be used for playing small sounds(shoot, scream, tic, walk, etc). It will load the entire file inside a buffer, so be careful if you try to play a big file using it.

If you want to try playing something else than the default "Hello World", you can download this short simple : [ocean.wav] and replace the sound object definition by:

KALSource*sound=newKALSource("ocean.wav");

You can only use it to play wav file at the moment, but we hope to bring ogg support soon.
For playing music, please wait until KALStream tutorial is up.

- The sound can now be played by simply calling play(). This function is asynchronous, meaning your program doesn't have to wait until the sound has finished playing to continue.

- Then, to know when the sound has ended, we create a loop using status() which returns ever AL_PLAYING or AL_STOPED.

- Inside the loop, we print the time since the sound started playing using elapsedTime(). It returns an ALfloat with the elapsed time expressed in seconds.

- Finally, we have to clear the KALSource buffer by deleting the KALSource pointer and close the device by calling shutdown().