computeSpectrum
Real-Time Flash Audio Visualizer
When Flash 9 and ActionScript3 came out, I was scrambling to learn as much as possible. I took part in (and won) an online competition on Adobe Flash Evangelist Lee Brimelow's site The Flash Blog, to see who could make the coolest example. It was humbling, and a great learning experience.
Here is the source for the winning entry
class Simple {
private var s:Sound = new Sound();
private var sc:SoundChannel = new SoundChannel();
private var ba:ByteArray = new ByteArray();
private var gfx:BitmapData;
private var pic:Bitmap;
private var ___width:uint;
private var ___height:uint;
private var res:Number = 7;
private var z:Number = 0;
function Simple(hostMC:MovieClip, mp3:String, __width:Number, __height:Number) {
___width = __width;
___height = __height;
gfx = new BitmapData(__width, __height, false, 0x000000);
pic = new Bitmap(gfx)
hostMC.addChild(pic);
s.load(new URLRequest(mp3));
sc = s.play();
hostMC.addEventListener(Event.ENTER_FRAME, processSpectrum);
}
private function processSpectrum(ev:Event) {
SoundMixer.computeSpectrum(ba,true,0);
var num:Number = ba.readFloat();
l = Math.pow(num,3)*___width/3;
for (var i:int = 0; i < 1440; i++) {
gfx.setPixel(l,i,0xffffff*Math.pow(i,2));
for (var j:int=0;j < res; j++) {
gfx.setPixel(Math.sin(i/360*Math.PI)*l+j*(___width/res)-50,i/j,0xffffff*Math.pow(i,2));
}
}
var blur:BlurFilter = new BlurFilter(2,2);
var displace = new DisplacementMapFilter(gfx, new Point(0, 0), 2, 1, z*l/6, z+3);
gfx.applyFilter(gfx, new Rectangle(0, 0, ___width, ___height), new Point(0, 0), displace);
gfx.applyFilter(gfx, new Rectangle(0, 0, ___width, ___height), new Point(0, 0), blur);
z += 0.02;
}
}
