Tinker Blog

Make and break: food, gadgets, and internet things.

Five iPhone development sound tips

April24

A few sound-related tips I’ve discovered along the way that I’d like to share:

1. You can’t play two compressed (aac, mp3, alac) files at the same time because there is only one hardware uncompressor available. Here’s a little more info from Apple’s documentation:

The following list summarizes how iPhone OS supports audio formats for single or multiple playback:

  • Linear PCM and IMA4 (IMA/ADPCM) You can play multiple linear PCM or IMA4 format sounds simultaneously in iPhone OS without incurring CPU resource problems. The same is true for the AMR and iLBC speech-quality formats, and for the µ-law and a-law compressed formats.
  • AAC, MP3, and ALAC (Apple Lossless) Playback for AAC, MP3, and ALAC sounds uses efficient hardware-based decoding on iPhone OS–based devices, but these codecs all share a single hardware path. The device can play only a single instance of one of these formats at a time.

The single hardware path for AAC, MP3, and ALAC audio entails implications for “play along” style applications, such as a virtual piano. If the user is playing a sound in one of these three formats in the iPod application, then your application—to play along over that audio—must use one of the software-decoded formats: linear PCM, IMA4, AMR, iLBC, µ-law, or a-law.

2. Use the compressed format for the largest file (for example, background music.) In my case, I’m using a very compressed mp3. (22050 sample rate, mono, low quality VBR).

3. Use Audacity (I’m using the beta with no problems) to create your compressed mp3s. It provides a nice GUI for you to trim your sound file.  The sampling rate is in the lower left corner of the window, and you can change the variable-bit-rate range by selecting File->Export…, selecting mp3 as your format, and clicking the ‘Options’ button.

4. For your other sounds, like sound effects, that you’d like to play concurrently with your background music, run the following command in Terminal to convert your sounds to a software-compressed format (I’m using IMA4, seems to be the best size/quality compromise). I first tried to use sox, but afconvert will more reliably give you a file that the apple development environment is happy with:

% afconvert -d 'ima4' -f 'caff' inputfile.mp3 outputfile.caf

You can also check to make sure they are readable by the iPhone SDK with afinfo and listen to them with afplay. IMA4 is a compressed format but the quality seems to be pretty decent.

5. AVAudioPlayer is a very easy way to get compatible sound files into your application. It only works on OS 2.2 or greater. To use it,


#import <AVFoundation/AVFoundation.h>

bgMusicPlayer = [AVAudioPlayer alloc ];
[bgMusicPlayer initWithContentsOfURL: [NSURL fileURLWithPath:
[ [ NSBundle mainBundle ] pathForResource: @”street_level-small”
ofType:@”mp3″
inDirectory:@”/” ] ]
error:nil
];

[bgMusicPlayer prepareToPlay];

// when you want to play the file

[bgMusicPlayer play];

You will also need to include the AVFoundation framework. Ctrl-click (or right click) on Frameworks in your Xcode window, then select Add->Existing Frameworks…  The AVFoundation framework is in /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.sdk/System/Library/Frameworks/

Good luck!

Hat tips to AkumaStreak on the #cocos2d-iphone irc channel and Sirr & Vadan Less from Integrated Alchemy for pointing me in the right directions.

posted under iPhone | 2 Comments »

Tool time

April20

I have recently started working on a new iPhone application for a client that looks to be very fun. It’s a very simple game concept, so I thought I’d check out some development options. I thought about using the existing user interface tools that come with the iPhone SDK. As I did a little more research, I found a nice little framework called cocos2d that specializes in helping to create two dimensional (2D) games. Like the old school Sega Genesis Sonic the Hedgehog rather than the newfangled Sonic Adventure.

Anyway, I have just started out, so not much to report yet other than encouraging anyone else interested in iPhone game development to download it. (link)

It has a nice object model around Scenes, Layers, and Sprites, and has a lot of fun game functionality built in. I’ll post more as my project continues.

posted under iPhone | No Comments »