Getting the sound output ‘right’

There’s at least three ways to do sound for the iPhone. The simplest way however – using System Sound Services – is not recommended for games, where you want to be playing multiple, and potentially long, sounds.

So for our needs, we were basically left with a choice between Audio Queue services, which seems to be the generally recommended way to do sound in games, and with using the SoundEngine class from Apple’s CrashLanding example, which uses the Open Audio Library, which also has the potential advantage of supposedly being cross-platform.

I tried both using Audio Queue services and the SoundEngine. It turns out that the Audio Queue services have the advantage that you can at least hear something in the simulator when using. The disadvantage that I found however was that it was tricky to get working nicely, and had some issues I was never able to solve, for example if I played stereo sounds then there were audible audio artifacts whenever I stopped/started them, even though doing everything the same with mono sounds there were no problems, which seemed to indicate the problem was internal.

So I went for using this SoundEngine class. It turns out this is very simple to use and basically just works. In our first release of Fuzzle, we released with this, however we then discovered one problem – by default, it fades out, and stops any existing music playback, when the application starts. Turns out a number of games have this same problem …. the user can always, after application start, double-tap the home key and restart the music, however then the game sounds stop working, and most importantly, some users don’t realize this and complain in reviews!

The fix for this, which is in our next update already, turns out to be the following three lines of code, which should come immediate before the call to SoundEngine_Initialize(…):

    AudioSessionInitialize (NULL,NULL,NULL,NULL);
    UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
        sizeof( sessionCategory ),
        &sessionCategory );

And with this code added, the game seems to co-exist perfectly happily with background music. Of course, if your application/game has its own music etc, then it may make sense to leave the default method of stopping any existing music playback. And once this update has been out, we’ll see if any other problems surface, but at the moment this seems to be the ‘right way’ to handle this 🙂


3 Responses to “Getting the sound output ‘right’”

  1. Steph Thirion Says:

    Great tip! thanks

  2. Theresa Says:

    Well i am going to use this code . Let you know what ‘ll happen. 🙂

  3. Justin Brown Says:

    Hi there.
    My name is Justin and I’ve been producing music independently for about 5 years.
    I love Melodica and I use it as a musical notepad to jot down melodies to make into full songs later.
    I know that this is not the correct forum, but a search of “melodica” on this site yielded no results. So I am posting here in the hopes that the dev for this program will read it.
    I have a suggestion which I believe will make Melodica easily twice as good, and generate many more sales by word of mouth – effectively this one change could allow melodica to be used as more of a performance piece (for example, to set a rhythm down for a jam session, etc.)
    This is my suggestion:

    The iphone has a tilt sensor. Use this tilt sensor as an X-Y control.
    The X axis should control a significant EQ-based effect, for example, the frequency band of a gentle parametric EQ boost, so that rolling the iphone side-to-side will produce a (wahh-yeoww) sound. Obviously, it would be up to the user to determine the speed and rhythm of the tilt, but it would make any melody infinitely more interesting and less repetitive.
    The Y axis could be used either for a generic stereo effect such as distortion, chorus/flanger, or delay, but the best option would be to allow a choice. Alternately, it could be used to control the volume of a preset drum loop (though i don’t recomment this, and many melodies do not sync with the typical 1-3 pattern. Alternately, it would be great to toggle some notes as dynamic (for example, double-click a note to turn it red. Red notes will change by +/- 7 semitones whien tilted >45 degrees.)

    Anyway, I love the product, but it would be many times better if there were more interaction with the unit, e.g. a way to PLAY it as an instrument.

    I hope you take this into consideration, I would love to see this in a future update.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: