Thursday, June 10, 2010

Code to convert .mp3 or .wav audio file to iPhone .caf format

iPhone likes to work with audio files compressed with ima4 and wrapped in the .caf format. On OS X the afconvert command will do this, but I needed something for linux. So I wrote some C code to do the conversion I needed: wav2caf.c

wav2caf takes a wav file as input, and outputs an ima4-compressed .caf file suitable for playing on the iphone. For source code, build, and install instructions, download here.

Caveats: This source code expect the wav file to be 22050 Hz and single channel. SOX will convert just about any audio file (e.g. mp3, m4a) to this format with a command such as "sox -r 22050 -c 1 output.wav"

There's not much to the source file. wav2caf.c looks a bit like aifc2wav, because that's what I started with, then reversed it, sort of. Good luck. If you make improvements let me know.

Thursday, May 6, 2010

Help us move from Flash to HTML5 (part 1).

We found out that about 1 million internet users (those using an iPad) are unable to use our social/streaming radio site because it doesn't support Flash.

So we've tried to convert everything to HTML5, to get the same or good-enough functionality out of HTML5. There's no problem with the graphic stuff (HTML and Javascript can move images around just fine, thank you very much), but now we're down to replacing Flash audio with HTML5 audio, and it's giving us trouble.

To make the very best audio experience (under even bad wifi connections), this is what we've been doing with the Flash Sound and SoundChannel classes:
  • buffer the downloading of MP3 files, a few ahead of time
  • measure download rates (so we can download hifi or lofi files accordingly)
  • play during download
  • measure how much has been downloaded to display that information to the user
  • allow user to pause/play/pause
  • allow the user to reposition where in the track to play
With HTML5's <audio> tag and "HTMLAudioElement" we can download an MP3 file and make it play (although even this tends to crash Safari on OSX about as much as our Flash code would if pushed). What we haven't figured out is how to know exactly when buffering has made progress, how much is buffered, how fast it is buffering, when playing starts, and how to restart play from any given position.

We keep hearing that Flash is dead and no longer needed because HTML5 is ready to replace Flash.

So, come on you Flash haters. A little help, please? Let us know (in the comments, so everyone can benefit) how to replace our Flash sound capabilities with HTML5.