Thursday, July 24, 2008

Action Script 3 Sound Spectrum

AS3 came up with many changes. Its complete new architecture with many new classes.
Sound Spectrum is one of that. Now using AS3 you can read the sound bytes using Sound Spectrum class. I had tried one simple example to learn about this new class.
It is very simple and want to share with you guys.

Hope you all will enjoy this.

ScrinShot



// 
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.media.SoundMixer;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.utils.ByteArray;
import flash.net.URLRequest;

public class Main extends Sprite
{

private var s:Sound = new Sound();
private var sc:SoundChannel;
private var ba:ByteArray = new ByteArray();
private var array:Array;
private var a:Number = 0;
public function Main():void
{
s.load(new URLRequest("Jaane Ja Dhoondta Raha.mp3"));
sc = s.play(0, 1000);
this.addEventListener(Event.ENTER_FRAME, spectrum);
trace("Main Construction called");
}

private function spectrum(event:Event):void
{
a = 0;
graphics.clear();
SoundMixer.computeSpectrum(ba,true,0);
for(var i:Number=0; i <256; i=i+8)
{
a = ba.readFloat();
var num:Number = a*360;
graphics.lineStyle(num / 15, 0x006600,0.5);
graphics.beginFill(0x006600,.05)
graphics.drawRect(stage.stageWidth/2,stage.stageHeight/2, i, i );
graphics.drawRect(stage.stageWidth/2,stage.stageHeight/2, -i, -i );
graphics.endFill();
graphics.lineStyle(num / 15, 0x000066,0.5);
graphics.beginFill(0x000066,.05)
graphics.drawRect(stage.stageWidth/2,stage.stageHeight/2, -i, i );
graphics.drawRect(stage.stageWidth/2,stage.stageHeight/2, i, -i );
graphics.endFill();
}

}
}


}

0 comments:

Post a Comment