Wednesday, July 4, 2012

Tackling Core Audio

I've been working with Alex Wiltchko's Novocaine, which works nicely as a pain-reducing wrapper for lots of basic core audio stuff. Unfortunately, it's not idiot-proof, and I've made at least one idiot-level mistake with it.

Alex very nicely packaged up an AudioFileWriter class with the project which works great for me in the simulator, but fails unpredictably when I try to use it on a device. So for me, anyway, it seems that dream of avoiding touching core audio, and the dreaded AudioStreamBasicDescription has not arrived.

Today I'm playing around with ExtAudioFileWriteAsync, seeing which sorts of settings will work for me.

Writing to AIFF is working without too much trouble, but I'm having a heck of a time writing to m4a. It looks as if ExtAudioFileWriteAsync is sensitive about the number of frames passed when writing files of format kAudioFormatMPEG4AAC. When passing a buffer of 10,000 frames, I get a crash with this error code: -66567.

For anyone reading this who can't make sense of these error codes (like me, until yesterday), you can search the headers of whichever framework you think the error originated from for the code. Here's some explanation.

Anyway, -66567 resolves to kExtAudioFileError_MaxPacketSizeUnknown. I'm unable to find any documentation indicating just what this message means.

Now, when I reduce the number of frames I'm passing to ExtAudioFileWriteAsync to 512, the error message goes away, but the resulting file is only 28 bytes long and is unplayable. This is only in on my iPad. All works fine in the simulator.

Now I'm simplifying and trying to work with the synchronous version of ExtAudioFileWrite. This is failing immediately with kExtAudioFileError_MaxPacketSizeUnknown, even when I give it a very small number of frames (100).

This StackOverflow post indicates that there are some properties that are required to be set. I'm not sure that I read the documentation the same way this poster does, however it seems that in their case, setting kExtAudioFileProperty_CodecManufacturer solved a problem.

No comments: