AS3.0频谱系列-01:

package fengzi.spectrum
{
//import fengzi.colors.GetColor;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.utils.Timer;
import flash.geom.Matrix;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.ColorTransform;
import flash.filters.BlurFilter;
import flash.filters.DisplacementMapFilter; public class Spectrum_01 extends Sprite
{
private var _sprite:Sprite;
private var timer:Timer;
private var thisW:Number;
private var thisH:Number; private var byteArray:ByteArray; var sDis:Number = 30;
var lDis:Number = 100;
var maxDis:Number;
var bitmapData:BitmapData;
private var bitmap:Bitmap;
var colorTF:ColorTransform = new ColorTransform(0.90,1,1,0.90);
var numArr:Array = [];
private var onBg:Boolean;
private var color:uint; /**
* Spectrum_01 构造函数(实例)
* @param width 宽
* @param height 高
* @param _color 颜色
* @param onBg 黑色背景的布尔值(默认false黑色背景,true透明)
*/
public function Spectrum_01(width:Number=550,height:Number=400,_color:uint=0xFF00FF,_onBg:Boolean=false)
{
thisW = width;
thisH = height;
color = _color;
onBg = _onBg;
maxDis = height / 4;
byteArray = new ByteArray ;
_sprite = new Sprite ;
for (var i:int = 0; i < 256; ++i)
{
numArr[i] = 0;
} bitmapData = new BitmapData(thisW,thisH,onBg,0);
bitmap = addChild(new Bitmap(bitmapData)) as Bitmap;
addChild(_sprite); timer = new Timer(30);
timer.addEventListener(TimerEvent.TIMER,timerHandler);
timer.start(); } /**
* onStageReSize()
* Event.RESIZE时调用此方法,频谱随舞台缩放
*/
public function onStageReSize():void
{
thisW = stage.stageWidth;
thisH = stage.stageHeight;
maxDis = height / 4;
if (bitmapData)
{
bitmapData.dispose();
}
bitmapData = new BitmapData(thisW,thisH,onBg,0); bitmap = addChild(new Bitmap(bitmapData)) as Bitmap;
} private function timerHandler(event:TimerEvent):void
{
var aPower:Number = 3;
var num:Number; SoundMixer.computeSpectrum(byteArray,false); aPower /= 2;
_sprite.graphics.clear();
_sprite.graphics.lineStyle(1,color);
for (var i:int = 0; i < 128; i++)
{
num = Math.abs(byteArray.readFloat() + 0.1);
numArr[i] += ((num - numArr[i]) * 0.25);
num = numArr[i];
_sprite.graphics.drawCircle(((thisW / 2) + Math.sin(((((i * 360) / 128) * Math.PI) / 180)) * (sDis + num * maxDis)),thisH / 2 + Math.cos(((((i * 360) / 128) * Math.PI) / 180)) * (sDis + num * maxDis),num * 20); } _sprite.graphics.beginFill(0xFF33FF,1);
for (i = 0; i < 128; ++i)
{
num = Math.abs(byteArray.readFloat() + 0.1);
numArr[i] += ((num - numArr[i]) * 0.25);
_sprite.graphics.drawCircle(((thisW / 2) + Math.sin(((((i * 360) / 128) * Math.PI) / 180)) * (lDis + num * maxDis)),thisH / 2 + Math.cos(((((i * 360) / 128) * Math.PI) / 180)) * (lDis + num * maxDis),num * num * 10);
}
bitmapData.draw(_sprite);
//bitmapData.fillRect( new Rectangle( 0,0, bitmapData.width, 1 ), 0 );
//bitmapData.fillRect( new Rectangle( 0,0, 1, bitmapData.height ), 0 );
//bitmapData.scroll( aPower *0, aPower *0);
bitmapData.colorTransform(bitmapData.rect,colorTF);
bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point ,new BlurFilter(2,2));
bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point ,new DisplacementMapFilter(bitmapData,new Point ,1,2,5,5,"clamp"));
} }
}

  

05-13 05:13