views:

36

answers:

2

I'm interested in learning how to decode and playback audio in ActionScript 3. I understand how to write bytes to a Sound object using the SAMPLE_DATA event, so that's not really a problem. What I want to understand is how I could implement alternate audio formats for native playback inside of Flash Player.

I guess what I'm asking is: how do I take something in X format and "convert/decode" it to WAV format and write the bytes to a Sound object, playing back the audio? I'm interested in writing a decoder for FLAC audio and possibly OGG audio, as these seem to be some of the most widely used open source audio formats.

Can anyone give me any advice on this?

+1  A: 

You can try, but you're not going to have much grunt left to do other stuff. Prior to Flash 10, I wrote an article detailing a hack to feed PCM data into sound output in Flash. Someone got in touch because they had written an AS3 Ogg decoder, but... even after fully optimzing the code, it was found that AVM2 is really not that much up to the job. Basically, it's rather slow and decoding OGG is quite processor intensive. I can't see that things will have changed that much in the years since, because CPUs have become "wider" and not really that much faster. ActionScript is single threaded, so you can't offload to another core.

Probably worth checking out this... maybe performance has improved.

EDIT: Having said all that, as Juan has said, don't be discouraged by this answer. I suspect the computational demands of FLAC decoding are probably considerably less than OGG, and if DSP gets you excited, taking the time to figure all this stuff out is 100% worth it, even if the Flash route (possibly) leads to disappointment. Personally I think that the MediaStreamSource for Silverlight looks really promising,but haven't really dabbled that much.

spender
@spender. Let me tell you, that was an awesome hack. I remeber I toyed with it in an audio mixer project I was working on at the time (although the resulting user generated audio raw data was too big to be practictal to send it to the server and writting an MP3 encoder was way out of my league; I ended up sending the "mix data" in a midi like format and letting the server do the real audio mixing). But it was a lot of fun to work with it. +1 for that! (an also for a good answer).
Juan Pablo Califano
@Juan... Thanks. Discovering how to make Andre Michelle's secret recipe was definitely the most fun I ever had with Flash.
spender
+1  A: 

If you want to write a decoder, the first thing you should probably look at is the spec for the format you want to decode.

The ogg/vorbis spec can be found here: http://xiph.org/vorbis/doc/Vorbis_I_spec.html.

Also, it could be of help to take a look (or maybe port) some other open source library that already does this (I'm not aware of any written in Actionscript), such as this, in Java: http://www.jcraft.com/jorbis/ (I don't know this library, I've just found it googling "ogg vorbis open source".

At any rate, you'll have to put some work to get it working and I don't mean this to discourage you, but I'm not sure Actionscript is fast enough for real time audio decoding.

Juan Pablo Califano
"I'm not sure Actionscript is fast enough for real time audio decoding"... nor am I.
spender
Decode it first and then playback it, or maybe decode only a buffer and decode slowly during the playback.
M28
@M28. Yes, decoding while playing back was what I meant by "real time audio decoding". I doubt AS is fast enough, as I said, but I might be wrong. On the other hand, decoding the whole thing upfront is not always an option; and also, I've got a feeling it would take ages (and you'd have to use "green threads", which will make it slower).
Juan Pablo Califano