Development/Tutorials/Games/KALEngine: Difference between revisions
m (→Abstract: KALEngine -> Gluon's audio module) |
m (→The Code: try to update explanations, need more work) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 25: | Line 25: | ||
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. | 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". | |||
<syntaxhighlight lang="cpp-qt"> | |||
#include "audio/sound.h" | |||
#include <QtCore/QDebug> | |||
int main( int argc, char* argv[] ) | |||
{ | |||
// create a sound object | |||
GluonAudio::Sound* sound = new GluonAudio::Sound; | |||
// load KDE's log in sound to the object | |||
sound->load( "/usr/share/sounds/KDE-Sys-Log-In-Long.ogg" ); | |||
// set sound volume, it can be between 0 and 1 | |||
sound->setVolume( 0.9 ); | |||
// print the elapsed time until the sound has finished playing | |||
qDebug() << "Playing sound with duration" << sound->duration() << "seconds."; | |||
< | |||
// play the sound | // play the sound | ||
sound->play(); | sound->play(); | ||
// | // wait until playing is finished | ||
while (sound-> | while( sound->isPlaying() ); | ||
// clear the buffer | // clear the buffer | ||
delete sound; | delete sound; | ||
return 0; | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
- First, we | - First, we create a GluonAudio::Sound. GluonAudio::Sound 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 KDE's log in sound, you can download a sample [http://ktank.free.fr/kalengine/ocean.wav ocean sound] and replace the sound load definition by: | |||
If you want to try playing something else than | |||
<syntaxhighlight lang="cpp-qt"> | <syntaxhighlight lang="cpp-qt"> | ||
sound->load( "ocean.wav" ); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
You can only use it to play wav file at the moment, but we hope to bring | You can only use it to play ogg or wav file at the moment, but we hope to bring more audio support soon. | ||
- The sound can now be played by simply calling <b>play()</b>. This function is asynchronous, meaning your program doesn't have to wait until the sound has finished playing to continue. | - The sound can now be played by simply calling <b>play()</b>. 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 | - Then, to know when the sound has ended, we use <b>sound->isPlaying()</b>. | ||
- | |||
- Finally, we have to clear the | - Finally, we have to clear the GluonAudio::Sound buffer by deleting the GluonAudio::Sound pointer. | ||
== Multi-channel == | == Multi-channel == |
Latest revision as of 19:00, 13 May 2013
Tutorial Series | KALEngine developement |
Prerequisites | None |
What's Next | Nothing at the moment |
Further Reading | KGLEngine2d's code |
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>
int main( int argc, char* argv[] )
{
// create a sound object
GluonAudio::Sound* sound = new GluonAudio::Sound;
// load KDE's log in sound to the object
sound->load( "/usr/share/sounds/KDE-Sys-Log-In-Long.ogg" );
// set sound volume, it can be between 0 and 1
sound->setVolume( 0.9 );
// print the elapsed time until the sound has finished playing
qDebug() << "Playing sound with duration" << sound->duration() << "seconds.";
// play the sound
sound->play();
// wait until playing is finished
while( sound->isPlaying() );
// clear the buffer
delete sound;
return 0;
}
- First, we create a GluonAudio::Sound. GluonAudio::Sound 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 KDE's log in sound, you can download a sample ocean sound and replace the sound load definition by:
sound->load( "ocean.wav" );
You can only use it to play ogg or wav file at the moment, but we hope to bring more audio support soon.
- 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 use sound->isPlaying().
- Finally, we have to clear the GluonAudio::Sound buffer by deleting the GluonAudio::Sound pointer.
Multi-channel
sound1->play(); sound2->play();
...to develop
3d sound
KALSource->setPosition(x,y,z) ...to develop
TODO |
---|
Create KALStream |