Sound formats for iPhone development

Ahh, back to work today. It’s pretty tough getting back into the swing of things after what turned out to be a long break this year. While I was off I finally got to spend some time working on an iPhone game. After getting hold of the SDK a while back, it’s only now that I’ve gotten around to doing something with it.

One of the things that seemed a little odd about the SDK is it’s use of CAF-format audio files, detailed here. I got hold of a few very nice audio samples from the freesound site, but needed to convert them from WAVs to CAFs.

I thought ffmpeg might be up to the job, but the version I had didn’t list it as an avaliable output format using ffmpeg -formats. However after a bit of digging I discovered that it is supported by libsndfile, so set about installing it using MacPorts:

sudo port install libsndfile

Then I used the included libsndfile-convert app to convert my file:

libsndfile-convert file.wav file.caf

The output format is inferred from the file extension, so you don’t have to specify it. However, when I rebuilt and ran my iPhone app using the new file, it didn’t play back. I suspected there may be something wrong with the format of the file, so I took a look to see what file reported. For the original WAV file I got

file.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 44100 Hz

Unfortunately file doesn’t work on .CAF files, but you can open them using QuickTime Player, and using the Movie Inspector window you can see that the file has the following format:

16-bit Integer (Big Endian), Mono, 44.100 kHz

So it looks like the problem may be libsndfile-convert changing the endian-ness of the file contents, from the x86-style little-endian to Motorola-ish (i.e. pre-x86 Mac) big-endian, which is a bit of a pain. According to the docs, the libsndfile API supports endian-ness manipulation, so it’s probably just the case that the helper app is doing the wrong thing automatically. I’ll look at putting together a small command line app to use the API directly and enable me to batch process .WAV files correctly.

This entry was posted in Mac, Software Development, iPhone and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Erik

    Hi,

    I’m the main author of libsndfile.

    Rather that writing your own converter, why not help me update the sndfile-convert program so it can (optionally) write little endian CAF files :-) .

    Cheers,
    Erik

  • http://www.voyce.com voyce

    Sounds like a good idea!

    You could just add little-endian CAF as an explicit encoding option, I guess. A nicer option might be to “pass-through” the endian-ness from the source file. The problem is that some encoding formats don’t support it. Would sf_format_check barf in that case?

    Ian

  • rosegrif

    Hi guys, I know a number of people would be very interested in using this with ffmpeg. Especially me!

    Did you guys get anywhere on making the changes to libsndfile?

    great work btw – thanks for this one.

  • J

    If you’re doing this on your computer to convert wav (or something else) to caf, you can use the following:

    afconvert -f caff -d ima4 input_file [output_file]

  • George Bakiris

    Hi, we have a radio application in App Store streaming only mp3 format.
    Some stations that we need to add in our app. are using W.M.A. format.
    We need encode the w.m.a. to mp3 but we don’t know the way!
    May you do this project?
    Please answer to us and tell us the coast of this feature in our application!

    Thanks
    George

blog comments powered by Disqus
  • Follow me on Twitter Follow me on Twitter @voyce

  • Categories

  • Archives