views:

673

answers:

2

Hi

Is there a way I can convert an mp3 file into a wav/aiff in my iPhone app? I have an mp3 but I want to combine it with other files and the only way I know this can be done is when using PCM formats.

Can anyone help me out here?

Thanks.

Some things I tried

I tried using the AudioConverterFillComplexBuffer() method and its callback to convert the mp3 to caf but the bytes per packet and per frame are read as 0. Any ideas how I can or whether I can use this function?

Thanks again.

A: 

You can probably do this with the right set of Core Audio units, but it'll probably use a lot of CPU. You ought to convert the data ahead of time (maybe at build time?), if you can.

Mark Bessey
+4  A: 

WAV and AIFF often (usually?) contain PCM, so what's the objection there? Or are you saying that you have to get your MP3 data to PCM in order to mix it (perhaps in a multichannel mixer audio unit). If you seriously want to do this, look at Core Audio, specifically:

  • Audio File Services (to read the MP3 format and write AIFF or WAV)
  • Audio File Conversion Services (to convert the MP3 data to PCM, and/or to encode from PCM to some other codec if you were to write a file). Note that a given converter cannot convert between two encoded formats; you can do MP3-to-PCM or PCM-to-AAC, but to do MP3-to-AAC, you'd need two converters.
  • Extended Audio File Services, which combine both of the above.

Check out the ConvertFile example in the Core Audio SDK. This opens a source file for reading, describes the desired format of the target file, reads buffers from one and writes them to the other. The code exhibits both the separate use of file services + conversion services, and the convenient use of ExtAudioFiles.

Also, be sure to understand the difference between codecs and file formats, and what codec/format combinations are legal. I was surprised the first time I found out that PCM must be little-endian in a WAV, big-endian in an AIFF.

invalidname
Looks like some files are missing from the code. The one I downloaded from the link above is missing the files inside the "PublicUtility" group.
lostInTransit
On my system, the PublicUtility files are in /Developer/Extras/CoreAudio, and I believe they're installed as part of the Core Audio SDK when you install the Xcode tools.
invalidname