Audio Tutorial for iOS: Converting and Recording [2014 Edition]

Audrey Tam

GarageBand Tracks

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: 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:

afinfo pew-pew-lei.caf 
File:           pew-pew-lei.caf
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
optimized
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.

Garage Band Screenshot

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.

Audacity Screenshot

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 Audio Tutorial series I show how to play audio programmatically on the iPhone.

Audrey Tam

Audrey Tam retired at the end of 2012 from a 25-year career as a computer science academic. Her teaching included Pascal, C/C++, Java, Java web services, web app development in php and mysql, user interface design and evaluation, and iOS programming. Before moving to Australia, she worked on Fortran and PL/1 simulation software at IBM's development lab in Silicon Valley. Audrey now teaches short courses in iOS app development to non-programmers, and organizes venues for Melbourne Cocoaheads monthly meetings.

User Comments

9 Comments

  • Hey Ray, just starting out here and wanted to say that I really appreciate your tutorials. Will try to convert my enthusiasm into discipline and hopefully some cash hehe. Once I got some, I'll try to pass some your way. Cheers. You're the best.
    fresidue
  • I have a problem with the .caf file i create using audio player, in my app it works fine but when i send it by mail to someone it cannot be played by any other audio player and i don't know how to converted to another format . How can i converted to another format programatically without using terminal to convert I need the client to recive a playable audio file. Sorry i thing i posted this twice but i just can't find an answer
    Radu
  • Can you please explain how to record sound in wav format in iphone
    Aiswarya
  • Here's a nice audio converter for core audio formats. http://gngrwzrd.com/microac/
    gngrwzrd
  • This post is absolutely epic. Thanks for the great tutorial! ;)
    Mazyod
  • The link to the Apple GarageBand Tutorials has changed since Apple rearranged their web site. The new link to the Using Built-In Software Instruments tutorial video is http://www.apple.com/findouthow/music/#softwareinstruments
    codelikethis
  • Does afconvert safe to convert .mp3 file directly to .caf?
    As my understand is that .caf is container format in which it may put .mp3 there and during the actual play time, it will play it in the way of .mp3 efficiency.
    Do I need to convert .mp3 to .wav or uncompressed format first before using afconvert? Or it already does the job for me without any worry.
    haxpor
  • Hi i want to record audio file in MP3 or Acc format. but i didn't find any source to record sound file to acc and mp3 format if you have any idea for this please share with me



    thanks in advance :( :(
    davinder527
  • I have a client with a McIntosh preamp. Using the optical line out from a Mac mini gives no audio. This is because the McIntosh only accepts PCM audio. Can you change the format of Afp's stream, on the fly?
    kht

Other Items of Interest

Ray's Monthly Newsletter

Sign up to receive a monthly newsletter with my favorite dev links, and receive a free epic-length tutorial as a bonus!

Advertise with Us!

Hang Out With Us!

Every month, we have a free live Tech Talk - come hang out with us!


Coming up in September: iOS 8 App Extensions!

Sign Up - September

RWDevCon Conference?

We are considering having an official raywenderlich.com conference called RWDevCon in DC in early 2015.

The conference would be focused on high quality Swift/iOS 8 technical content, and connecting as a community.

Would this be something you'd be interested in?

    Loading ... Loading ...

Our Books

Our Team

Tutorial Team

  • Matt Luedke

... 49 total!

Update Team

  • Zouhair Mahieddine

Editorial Team

... 23 total!

Code Team

  • Orta Therox

... 3 total!

Translation Team

  • Jiyeon Seo
  • Przemysław Rembelski

... 33 total!

Subject Matter Experts

  • Richard Casey

... 4 total!