Archive

Archive for the ‘Flash’ Category

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

MP3 to SWF converter in Actionscript 3

February 13th, 2009

A while ago I was playing with SWF generation within Flash itself.
I read the Adobe SWF specs and now I'm able to generate SWF's from FLV's and MP3's or both.

A simple Flash 10 example, haven't tested all possible MP3 rates so if someone encounters a bug, please let me know.
(Just found out that some MP3's with embedded art-work won't work... added to my TODO's).

Code snippet which writes the MP3 data to a SWF bytearray:

while(mp3.audioFrames.length > currentAudioFrame)
{
	var needSamples:int = 0.001 * ((1000/(__frameRate))*i) * audioRate;
	var startSamples:int = currentSamples;
	var audioData:ByteArray = new ByteArray();
 
	while(mp3.audioFrames.length > currentAudioFrame && currentSamples< needSamples)
	{
		var aframe:AudioFrame = AudioFrame(mp3.audioFrames[currentAudioFrame]);
		audioData.writeBytes(aframe.frameData);
		currentSamples = (currentAudioFrame+1) * (576*2);
		currentAudioFrame++;
	}
 
	var samples:int = currentSamples - startSamples;
	var seek:int = startSamples ? needSamples - startSamples : 0;
 
	var af:AudioFrame = mp3.audioFrames[i] as AudioFrame;
	_swfBytes.writeShort(createHeader(SWFTags.SOUNDSTREAMBLOCK,63));
	_swfBytes.writeInt(audioData.length+4);
	_swfBytes.writeShort(samples);
	_swfBytes.writeShort(seek);
	_swfBytes.writeBytes(audioData);
	_swfBytes.writeShort(createHeader(SWFTags.PLACE_OBJECT2,3)); //Place frame data
	_swfBytes.writeByte(1);
	_swfBytes.writeShort(1); //depth
	_swfBytes.writeShort(createHeader(SWFTags.SHOWFRAME,0));
 
	i++;
}

This way I write all SWF headers the correct way and not like most other code out there which uses a precompiled SWF and replace the sound object.

Bernard Actionscript, Flash

Doc? Air Local LiveDocs

February 12th, 2009

Doc? is an Air application that allows you to view your documentation outside of your IDE.
It supports Flash CS3 documentation, Flash CS4, Flex and ASDoc.

Searching can be done by keyword or by regular expression!
Doc? provides two plugins, one for Flash and one for Eclipse (Flex, FDT etc..). These plugins send the selected text to Doc? and Doc? will search it's database for matching items.

I love it :D

http://www.airdoc.be

Screenshot with the PV3D ASdocs imported:

Papervion API in Doc?

Papervion API in Doc?

Bernard Actionscript, Flash, Flex