This article is the second in a three-part Audio Tutorial series covering audio topics of interest to the iPhone developer.
In the first article in the Audio Tutorial series, I covered the difference between file formats and data formats, and the various formats that are supported on the iPhone. Now let’s talk about how you can convert between different formats!
(If you’re in a hurry to learn how to actually play audio on the iPhone, jump to the third article in the Audio Tutorial series.)
afplay, afconvert, and afinfo
Converting audio files on the Mac is extremely easy due to three built in command-line utilities on the Mac:
The easiest to use is
afplay – just give it the name of your audio file from a Terminal and it will play away. This is quite convenient when compressing files to various bit rates to hear how they sound.
The next one is
afinfo – just give it the name of your audio file, and it will display the file format, data format, bit rate, and other useful info like so:
File type ID: caff
Data format: 1 ch, 44100 Hz, 'lpcm' (0x0000000C)
16-bit little-endian signed integer no channel layout.
estimated duration: 0.560181 sec
audio bytes: 49408
audio packets: 24704
bit rate: 705600 bits per second
packet size upper bound: 2
maximum packet size: 2
audio data file offset: 4096
audio 24704 valid frames + 0 priming + 0 remainder = 24704
source bit depth: I16
The above shows you that this file has a file type of CAF, a data format of 16-bit little-endian signed integer (LEI16), a sample rate of 44,100 Hz, and a bit rate of 705,600 bits per second.
Finally, let’s discuss the best utility of all:
afconvert. This is extremely easy to use – just issue a command line like the following:
afconvert -d [out data format] -f [out file format] [in file] [out file]
So to convert a file to the preferred uncompressed audio encoding for the iPhone (reminder: the little-endian integer 16-bit variant of linear PCM, a.k.a. LEI16) and the preferred file format for the iPhone (reminder: Core Audio File Format a.k.a. CAFF), you would issue a command like the following:
afconvert -d LEI16 -f 'caff' input_file.xxx output_file.caf
Note I didn’t specify the extension for the input file, because
afconvert is smart enough to detect the type of audio file and convert appropriately, so it can be any audio data format with any audio file format.
One other note: You can add the
-b option right before the input/output files to set the bit rate. So for example, here I save the file at 128kbit/s, then 32kbit/s:
afconvert -d aac -f 'caff' -b 128000 background-music-lei.caf test_128.caf
afconvert -d aac -f 'caff' -b 32000 background-music-lei.caf test_32.caf
Recording Audio on the Mac
I wanted to jot down a couple of notes about good ways to make music and sounds for your apps on the Mac.
First, there is GarageBand. GarageBand makes it really easy to put together some premade loops of drums, guitars, and other sound instruments and make a little song out of it. And if you’re musically inclined, you can record yourself playing along and make some much cooler stuff.
So if you haven’t already, take a couple minutes to go through the GarageBand Help from Apple. Specifically, “Use Apple Loops in your projects” is the one I found the most useful.
Note that after you are happy with your song, you’ll have to share it to iTunes or Media Browser, and then “Reveal in Finder” to grab your file for future use.
I found that GarageBand wasn’t the greatest for recording simple sound effects. For that, I turned to a great free audio program called Audacity. You can plug in your mike (I used my Rock Band mike and it worked just fine!) and record your effect, and save it out easily.
Don’t forget that when you make your own sounds like this, they will be most likely be saved as 16-bit big-endian signed integer, or BEI16. So don’t forget to convert to LEI16 before you include them in your app.
If you aren’t musically inclined, there are some sounds licensed under the Creative Commons license at The Freesound Project. Or you can always hire a professional!
In the next and final article in the Audio Tutorial series I show how to play audio programmatically on the iPhone.