Sound in QML for BlackBerry 10

I have been working on a side project of mine called GiraffPanic – a logic game for mobile phones. It was originally developed with QML and C++ for Meego platform (N9) and then, quickly ported over to Symbian with Anna or above. If you got one of those devices and want to try the game you can get it here.
A friend pointed out that the new BlackBerry 10 device was coming out soon and the game could be easily ported over as well as BB10 supports Qt 4.8. Almost everything worked out of the box when I tried to run the game in the BB10 emulator for the first time.
The only thing not working was sound. I looked around and found out that sound was a in fact only available starting from Qt 5.0 :-(. So I set off to implement a sound support library for use in QML. At first I looked at the different sound systems BlackBerry 10 offers natively (it supports quite a lot as can be seen here: http://developer.blackberry.com/native/documentation/bb10/multimedia.html). First I tried to use the Multimedia renderer library, however after trying some examples I found out that the simulator did not support this library, so testing was not possible. I then looked at libasound and SDL, but neither of those has a nice API in my opinion.
I eventually ended up settling for OpenAL library which is a 3-D space audio library. The interface seemed sensible and as far as I understood it is also used in Qt 5.0 on BlackBerry. Getting it to work was quite simple. Unfortunately OpenAL does not provide any callbacks to check when the playback of a sample is finished. As GiraffPanic uses signals to trigger different sounds from QML interface, a different solution was needed.
To work around this issue I used a timer to check if the playback is finished so I can send a notification back to QML. To reduce CPU usage the timer is only active when needed – that means only if a sound is played then the timer is active. One other thing not supported out of the box is looping of a sound sample for a set number of times. This I solved by setting up a buffer queue and adding the same sound into the queue multiple times.

The library interface is the same as the one from qtgameenabler which we use on Symbian and Meego, which was done for backwards compatibility reasons.

To use the library add the source to your project and put the following lines into your project file:

qnx {
  include(qtblackberryaudio/qtblackberryaudio.pri)
}

To make the library known to qml put the following lines into main.cpp (or any other sensible place):

qmlRegisterType<DeclarativeBBAudioMixer>("Audio", 1, 0, "AudioMixer");
qmlRegisterType<DeclarativeBBAudioBuffer>("Audio", 1, 0, "AudioBuffer");

in QML you can call the library as follows:
(this will init the audio mixer and load up the sample sounds)

import Audio 1.0

AudioMixer {
  id: mixer
  enabled: true
  AudioBuffer {
    id: sound1
    volume: 0.4
    source: basePath + "/audio/sound1.wav"
    loopCount: -1 // infinite looping
  }

  AudioBuffer {
    id: sound2
    volume: 0.8
    source: basePath + "/audio/sound2.wav"
    loopCount: 2
    onPlaybackStopped: {
      console.log("sound2 playback stopped");
    }
  }
}

And to play a sound use:

function soundHandling() {
  sound1.play();
  sound2.play();
  if (sound2.playing()) {
    sound2.pause();         // pause sound2
  }
  sound1.stop();            // stop sound1
  sound2.pause();           // unpause sound2
}

In the example above you can see me basePath variable. This variable is set in:

context->setContextProperty("basePath", "file:/" + 
                            QCoreApplication::applicationDirPath().remove("/bin"));

This allows to handle different platforms and set the path as it should be on each platform.

In case you find this helpful you can find the library git repository at: https://www.gitorious.org/qtaudio/qtblackberryaudio and clone the git repository.

If you have comments or feedback send a mail to t (dot) zachmann (at) zagge (dot) de

And finally, in case you already have a BB10 you can get the game from here:
http://appworld.blackberry.com/webstore/search/giraffpanic/

This entry was posted in Thorsten Zachmann and tagged , , , , . Bookmark the permalink.

3 Responses to Sound in QML for BlackBerry 10

  1. pascal says:

    No Offense but why code for these irrelevant platforms? Do this for android or iOS.

    Ovi store seriously???

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>