CocoaDev

Edit AllPages

In my app, I need to figure out some way to play an MP3 file that I decrypt into RAM. I have already looked into MPEG123 - I’m already using it to play normal MP3s in my app, but I don’t think it will decode from a buffer. I can’t just write the buffer out to a temp file, as there are legal complications with doing that. Any suggestions? I would REALLY like to refrain from using the QuickTime framework.

Thanks in advance.

-TylerStromberg


CoreAudio can do this.


Couldn’t you just make a file handle that points to the memory? I don’t kow the specifics of doing this, but I am sure that UNIX has some facilities for this. That way, you can avoid copying, and still use a familiar framework. Of course, if you WANT to learn CoreAudio, then go ahead, but its a little bit deep… –JeremyJurksztowicz


funopen(3), a BSD extension available in Mac OS X, might do what you need.


You can use MAD (http://www.underbit.com/products/mad/) to convert MP3 frames (in a buffer or wherever) to PCM. Then, you can use one of several approaches to play the audio using CoreAudio. The easiest is to grab an output AudioUnit, install a render callback, and in that callback pass the PCM generated by MAD.

-sbooth


If you’re going to use CoreAudio anyway, you might as well use its MP3 decoder too instead of some third-party one. Most of the pain in the CoreAudio approach comes from getting playback set up. Adding an audio converter to directly decompress MP3 data won’t add much to it, and it will probably be easier than a weird mixed approach involving third-party libraries.


I am thinking about using CoreAudio, even though it would make things substantially different from the PC version that I’m porting from. Here’s what I’m thinking about doing:

  1. The audio file comes in encrypted

  2. I write an AudioUnit to decrypt it

  3. The decrypted buffer gets passed to the MP3 decoder in CoreAudio

  4. Music!

Is it feasible? Does it sound like the correct work-flow?

Thanks for all the help!


I would personally say that #2 is overcomplicated, and you’d be better off just writing some normal code to decrypt your MP3s, then either using that as the callback for the MP3 decoder or using it to dump the entire MP3 into a buffer. However, creating an AU would probably work too, so it’s really a personal preference. Most of the code should transfer so if you change your mind in mid-stream you won’t lose much.


#2 is a big step. Remember that AU’s are not just dynamic libraries, they are bundles, with all sorts of fun stuff inside. While creating an AudioUnit would make a good component for applications, if you just need it to port one application, you are creating more headaches for yourself than you need. I would first focus on getting the code correct in the application, and then, if you need/want, move the tested code into it’s own AU. Good luck. -JeremyJurksztowicz


Yeah, for now I’ll try to just decode it in the app and pass the buffer to CoreAudio. Any ideas on where to start? I’ve been perusing the CoreAudio sample project “PlayFile” to get a better handle on how CoreAudio works and all - and I think I’ve got the basics down, but I’m having trouble figuring out how to use a buffer instead of an audio file.