http://www.raywenderlich.com/233/audio-101-for-iphone-developers-converting-and-recording
This article is the second in a three-part series covering audio topics of interest to the iPhone developer.
In the first article in the series, I covered the difference between file formats and data formats, and the various formats that were supported on the iPhone. Now let’s talk about how you can convert between different formats!
(Jump to the third article in the series, where I cover how to actually play audio on your phone.)
Afplay, AFConvert, and AFInfo
Converting audio files on the Mac is extremely easy due to three built in command-line utilities on the Mac: afplay, afconvert, and afinfo.
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:
|
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. Afconvert is extremely easy to use, just issue a command line like the following:
|
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:
|
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 32kbit/sec, then 128kbit/sec:
|
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 tutorials from Apple. Specifically, “Using Built-In Software Instruments” is the one I found the most useful.
Note that after you are happy with your song, you’ll have to export it to iTunes 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!
What’s Next?
In the next and final article in the series I show how to play audio programmatically on the iPhone.