Blackberry – Mixing 2 or More Sounds Together (Concept Video)

One thing that I (and many others judging by forum posts) have run into is the fact that the Blackberry isn’t very good at mixing more than one sound together. You’ll be listening to music, or playing a game with music, and suddenly you’ll get a text message, and it will simply terminate the current sound and play the new one. If you’ve got a friendly application, it will restart/resume the original audio, but it’s a jarring audio experience.

What I’m about to post won’t fix that. However – developers also run into the issue when they want to mix sounds together in their applications and games. Play 2 or more sound effects simultaneously and/or while music is playing. I’ve seen official RIM developers comment that certain devices can achieve two simultaneous sounds by instantiating the Player class twice – but from what I gather, this is on GSM only phones – and still limited to two. On CDMA devices, you’re completely out of luck.

The Cheap and Quick Workaround

In my 6 part tutorial on writing a Blackberry game, in the audio article, I mentioned how the Alert.startAudio method can be used for simple sound effects (tone/duration pairs) that will play simultaneously while your midi or mp3 music plays in the background. For many applications, this is enough if you don’t need sophisticated sound effects.

The Start of a Mixer

I’m guessing that the limitation is imposed by the audio chipset/DAC inside of CDMA devices, and perhaps RIM realizes the CPU time involved in a software based audio mixer would slow the phone down too much. However, I think the software option should be there, and developers can use it if they’d like – they might not need a large amount of CPU, or might be dealing with low quality sound files – there are a few scenarios where a software mixer would just be a nice options.

Tonight I sat down and wrote a quick and dirty proof of concept application showing a PCM audio mixer in action. I loaded in 3 audio 44.1khz 8bit mono files and mixed them together in real time. I didn’t normalize the audio at all, so its a little soft, but the code works, and could be expanded on quite a bit to make a full featured mixer. Maybe I’ll run into problems down the road that RIM already has, but it’ll be interesting nonetheless.

5 Responses to Blackberry – Mixing 2 or More Sounds Together (Concept Video)

Hi, I’m seriously interested in how you’ve implemented this mixer. I’m looking into mixing audio for a blackberry application and will have to mix audio streams in realtime (i.e. I have a stream running, and an event will require that a new effect be layered into the stream).

Is the source code available for this yet? Is it dependent on audio streams being opened at the inception of the mixer, or is it capable of handling additional streams that are introduced after the mixer has been instantiated?

I’ve been googling furiously for information on this issue and your blog has been the most informative source of information thus far.

Yeah, there definitely doesn’t seem to be much on the web about manually creating a mixer on the Blackberry, unfortunately. The techniques can be used to do a few more things with audio on the Blackberry as well, especially with generated sounds/effects.

I can’t answer your question about streams being introduced after the mixer has been instantiated, because I’m honestly not sure yet. I have some idea on how to go about doing it, but I haven’t tested them yet. They involve mainly tricking the player into thinking its playing a very long/infinite length file, so that it will always be running, and new streams can be mixed in. I think I had actually read about another person doing this somewhere on the web at one point, but I may be imagining that.

I’ve been sucked into trying to get a Blackberry game done for Christmas, but I just added a calendar entry to remember to post more about this on Sunday – I’ll try to get the code finished up a little more and posted, just so you get the general idea. It is a little limited though – it probably wouldn’t be able to mix mp3s, just PCM wav files. I think it could probably handle mixing different bitrates/frequencies together though, since that’s not too difficult. More soon, thanks for the interest!

Andy – if you’re still following along, I have posted all my code involved in this. Unfortunately, I was not able to achieve a truly real-time mixer – there will always be second delays involved in mixing sounds, as I document in this article.

Sorry I don’t have better news. Hopefully in the QNX OS we’ll have more hardware access or a built in mixer.