Lessons learned doing game sound in iOS

[Note: there are some problem with the conclusions drawn in this post. See this follow up post for corrections]

For my current mobile game project, I have a need to do both background music, and sound effects.

In a previous project, I used AVAudioPlayer to loop a MP3 file continuously in the background, and you can see an example of code to do that here (just make sure you call setNumberOfLoops to “-1” if you want infinite looping). For the sound effects, originally I planned to use the same API, however there are some problems with this. The biggest of these is there can be a large delay (a second or more) between when you kick off playback and the sound actually plays. This is fine for background music, but for a series of quick sound effects (imagine the sound of bricks falling, like in Tetris) it just doesn’t fly.

One of the other ways to play sound files with a bit more control is OpenAL. The documentation by apple (here) is pretty good, but it has a pretty steep learning curve, especially if you are like me and didn’t want to spend hours on experimenting. One of the reasons for this is if you want to do multiple sounds you have to do extra work to manage the simultaneous sounds, since unfortunately OpenAL doesn’t do this out of the box.

Fortunately, someone put together a class called AudioSamplePlayer (get it here) which covers much of the basics needed for playing simultaneous sounds, plus the added bonus of easily modifying the pitch and volume of them. The class is pretty easy to use, with the only minor annoyance that you need to pre-load sounds. However, this is understandable since it is designed to improve performance by avoiding loading each sample every time it is played. The licensing of the code is also such that you can use it pretty freely (but be sure to read it in full before using it yourself).

With AudioSamplePlayer you can save some development time, but you may need to do extra work depending on your requirements. One minor tweak I made is to allow .mp3 files (instead of just .caf files) which was a simple one line change.

A bigger problem is that if you game has the possibility to play the same sound quickly in repetition, the sounds can overlap and the resultant sound can sound strange (tinny or mechanical). For example, if you were playing the sound of each rocks hitting the ground in a simulation with 1000 rocks, some would likely occur near the same time.

In this case, one option is to limit the number of simultaneous sounds (and the above class allows this with a simple constant), but that can lead to unexpected behavior as well, especially if your sound file is relatively long (this includes and echo or space at the end of the file). You can also write logic to use a few different sounds to avoid exact-file collisions, or modulate the sounds in some way. You could even delay sounds a bit to reduce collisions, but again that would often be undesirable since the visuals would be out of sync with the sound.

In my case, I am considering just allowing the sound collisions since the resultant effect sounds sort of cool and seems to fit the atmosphere of the game.

Your next question is probably where to get the sound effects, since you may not want to record them yourself. There is a bunch of free sound effects sites on there, but I’ve found this site to have many interesting effects which can be used given the condition that you attribute the site.