The latest Sparrow version finally brought support for audio playback. We designed the classes in a way that you don’t have to care about the file format; no matter what kind of sound you play, you use the same code.

However, because of the hardware limitations of the iPhone family, there are some guidelines you should follow for best performance. This post summarizes the most important points and shows you how to apply them.

Compressed vs. uncompressed Audio

Behind the scenes, Sparrow uses two libraries to play sound: OpenAL and AVAudioPlayer.

OpenAL is a very fast audio library that is available on a huge number of platforms and is primarily used for games. Its downside is that it does not support any compressed file formats.

AVAudioPlayer is part of the iPhone SDK. While not blazingly fast, it supports a wide range of file formats, including compressed formats like aifc or mp3.

When Sparrow loads an audio file, it automatically chooses the optimal backend for playback. In either case, you receive SPSound and SPSoundChannel objects. Thus, forget about these internals right away — all you have to know is when to use which format.

SPSound *sound;

// uncompressed => OpenAL
sound = [SPSound soundWithContentsOfFile:@"sound.caf"];
// compressed => AVAudioPlayer
sound = [SPSound soundWithContentsOfFile:@"sound.aifc"];

Which format should I use?

The short answer: Use CAFF (uncompressed) for short sound effects and AIFF IMA4 (compressed) for music. This is also the recommendation from Apple.

  • CAFF (Core Audio File Format) is, in a nutshell, the iPhone’s native audio file format for uncompressed sounds.
  • AIFF (Audio Interchange File Format) with IMA4 gets you about 4:1 compression on audio files. It is supported natively by the iPhone. One other advantage of this format is that it loops seamlessly — most other compressed audio files are problematic in this regard.

Compared to others, those formats have the lowest impact on the CPU. If you use them as described above, you get the best of both worlds: sound effects will play fast and without delay. Background music, being compressed, keeps the application size small; it will be streamed into memory, saving precious RAM.

Another tip in relation to the file format: most players use the internal speakers of the iPhone (iPod/iPad). You will agree that they do not provide quite the best acoustic quality. Thus, you can safely use low bitrates and mono sounds. This will reduce the CPU impact even more, reserving more resources for smooth rendering of the visuals.

How do I convert my files into the correct formats?

Thankfully, Apple provides a very useful little command line tool with the iPhone SDK: “afconvert”. If you have installed the SDK, it will already be available to you. Here are examples of the most useful calls:

# creates sound.caf (little endian, 16 bit)
afconvert -f caff -d LEI16 sound.wav

# creates sound.caf (little endian, 16 bit, 22kHz)
afconvert -f caff -d LEI16@22050 sound.wav

# creates sound.caf (little endian, 16 bit, MONO)
afconvert -f caff -d LEI16 -c 1 sound.wav

# creates sound.aifc (IMA4 compression)
afconvert -f AIFC -d ima4 sound.wav

# creates sound.aifc (IMA4 compression, MONO)
afconvert -f AIFC -d ima4 -c 1 sound.wav

If you want to convert all audio files in a directory at once, you could create a shell script like this:

#!/bin/bash

for i in *.wav; do
  afconvert -f caff -d LEI16 $i
done

# move new files to project directory
mv *.caf ~/my_project_path/sounds/

Save this script into the file “convert_sounds.sh”, copy it into your sound directory, and make it executable:

# allow script to be executed
chmod u+x convert_sounds.sh  

# Later, to execute it, just call
./convert_sounds.sh

Conclusion

If you keep these best practices in mind, you will be able to add sound to your game in no time. If you have any questions, don’t hesitate to post them below or in the forum. Good luck!

12 Responses to “Sound on iOS: Best Practices”

  1. alex_h June 22, 2010 at 10:18 #

    thanks Daniel, that is extremely useful info!

  2. Daniel June 22, 2010 at 11:24 #

    You’re welcome, Alex!

  3. Alin August 9, 2010 at 13:15 #

    Thanks for the tutorials.. keep up the great job.

  4. Ilya Kamens June 28, 2011 at 04:39 #

    Hi Daniel,

    Thanks for the information. I have a quick question about what “short” and “long” mean. I imagine that a 2 second sound file means short, but what about a 20-30 second sound file? Would I want to use .caf or .aifc? I’m guessing .aifc, but I’d just like to be sure.

    Thank you,
    Ilya

  5. Daniel June 28, 2011 at 08:22 #

    Hi Ilya,
    you’re right, for a sound with that length, aifc would be appropriate! In the end, I guess it’s more about file size than sound length (though this correlates, of course). A caf-file will be moved completely into memory – a huge file will thus use a lot of RAM. Compressed audio files, on the other hand, are streamed, taking up only a small amount of memory – but more CPU time.

  6. eli July 14, 2011 at 08:19 #

    I am having a hard time getting the shell script to work. Can anyone help me?

  7. Daniel July 14, 2011 at 09:29 #

    Hi Eli,
    I recommend you post your question in the forum, with a detailed description of your problem – I’m sure we’ll be able to help you.

  8. Vinay August 31, 2011 at 23:42 #

    Hi Daniel,

    What will be the size of AIFF format of 2-3 minutes clip?

    Thanks,
    Vinay

  9. Daniel September 1, 2011 at 08:06 #

    Hi Vinay, it depends on the exact settings, but expect roughly 1 MB per minute for a mono clip. I recommend you just try it out, though! :-)

Trackbacks/Pingbacks

  1. PhoneGap: Lessons learned (Guest post) | Billy Cravens - January 23, 2011

    [...] For audio, avoid MP3 and WAV and use native formats as CAFF and AIFC (see: http://www.sparrow-framework.org/2010/06/sound-on-ios-best-practices/) [...]

  2. PhoneGap: Lessons learned (Guest post) - Billy Cravens - September 15, 2011

    [...] For audio, avoid MP3 and WAV and use native formats as CAFF and AIFC (see: http://www.sparrow-framework.org/2010/06/sound-on-ios-best-practices/) [...]

  3. Gebruik audio bestanden xcode - iCulture forum | iPhone, iPad, iPod touch, Apple TV en iOS - January 17, 2012

    [...] gegooglet: Sound on iOS: Best Practices | Sparrow Framework – The Open Source Game Engine for iOS Compressen als IMA4 geeft je 1/4de van de filesize. Ik weet niet of dat veel uitmaakt ten opzichte [...]