Archive for March, 2009

Using ByteArrays in Actionscript and Alchemy

March 31st, 2009

A few days ago I got the question how I did the transfer of data between Actionscript and C (Alchemy).
There are a few ways to push ByteArrays back and forth, I will give some examples of the methods I used.

Bernard Actionscript, Alchemy, Source

MP3 encoder in Flash with Alchemy

March 24th, 2009

A week ago I started playing with Adobe Alchemy. First just a few return values or simple calculations.
After a few days of playing and testing I thought it would be nice to have a MP3 Encoder in flash.
With the new Sound.extract method in Actionscript you can get raw data like a "WAV" but there is no easy way to compress this data.

I started searching for C code which implements a MP3 Encoder and found the 8Hz MP3 implementation.
I could have used LAME, but hey.. have you looked at the file count!
The 8Hz implementation uses about 10 files, so it's readable. It's not the best encoder, but good enough to test the possibilities.

I was very surprised that the 8Hz code would compile with llvm-gcc (Alchemy)! That is a good starting point, now I only have to rewrite some code to get input from and output to Actionscript.

The original source used fopen to read and write files. This isn't possible with Alchemy so I replaced fopen with funopen and gave a pointer to Actionscript bytearrays.

The next problem I encountered was the execution timeout in Flash. When encoding large WAV files (more then 30 seconds of sound on my MacBook Pro) the flashplayer will trigger an error about exceeding the maximum execution time. I tried to alter it with the compiler option "-default-script-limits" but no luck (maybe it has something to do with Alchemy).
But it forced me to take a deeper look in the encoding source. I've rewritten the layer3.c file to process a few frames and then give feedback to Actionscript. This way no timeout occurs.

I'm still working on speed optimization, because encoding is slow at the moment.
I will keep you updated!

Here is the first version:

Bernard Actionscript, Alchemy, Flash