// public onStageReSize():void package { import colors.ColorChange; import flash.events.EventDispatcher; import flash.media.SoundMixer; import flash.events.TimerEvent; import flash.utils.Timer; import flash.events.MouseEvent; import flash.display.BitmapData; import flash.display.Bitmap; import flash.geom.Point; import flash.display.Sprite; import flash.filters.BlurFilter; import flash.filters.ColorMatrixFilter; import flash.events.Event; import flash.filters.DisplacementMapFilter; import flash.utils.ByteArray; import flash.display.Shape; public class RonSoundFX extends Sprite { private var fxShape:Sprite; private var th:uint; private var tw:uint; private var _fxID:int; private var bn:int; private var dy:Number; private var bitmapdata:BitmapData; private var _bitmapdata:BitmapData; private var dz:Number = 0; private var fn:Function; private var listBar:Array; private var mlArray:Array; private var listVal:Array; private var list256:Array; private var listDot:Array; private var list512:Array; private var _isFilter:Boolean; private var myTimer:Timer; private var _color:ColorChange=new ColorChange(); private var bitmap:Bitmap; var float:Number = 0; var _byteArray:ByteArray; var alphaNumber:Number=0.66 /*宽度,高度,默认频谱ID,是否对频谱使用滤镜,是否自动切换频谱*/ public function RonSoundFX(w:int=400, h:int=300, n:int=0,_filter:Boolean=true,_start:Boolean=true,timer:uint=15,click:Boolean=true) { tw = w; th = h; _fxID = n; if (_filter) { _isFilter = true; } else { _isFilter = false; } init(); myTimer = new Timer(timer*1000);//频谱顺序切换时间 myTimer.addEventListener("timer", timerHandler); if (_start) { myTimer.start(); } else { myTimer.stop(); } if(click){ this.addEventListener(MouseEvent.CLICK,thisClickHandler); } else { this.removeEventListener(MouseEvent.CLICK,thisClickHandler); } } private function init():void { fxShape=new Sprite(); _bitmapdata = new BitmapData(tw,th,true,0); mlArray = gML(); start() } public function onStageReSize():void { tw =stage.stageWidth th =stage.stageHeight if(tw>800||th>800){ _isFilter = false }else{ _isFilter =true } start() } private function gML():Array { return ([waveForm01, waveForm02, waveForm03, waveForm04, waveForm05, waveForm06, waveForm07, waveForm08, waveForm09, waveForm10]); } private function waveForm01():void { alphaNumber=0.66 _byteArray = bytePosition(byteArray(true),true); var num:Number = tw / bn; fxShape.graphics.clear(); fxShape.graphics.beginFill(_color.color24); for (var i:int=0; i < bn; i++) { float = th * 0.666 * _byteArray.readFloat(); _byteArray.position = _byteArray.position + Math.floor(252 / bn - 1) * 4; listVal[i] = listVal[i] * 1.1; listDot[i] = listDot[i] - listVal[i]; if (listDot[i] < float) { listVal[i] = dy * 0.5; listDot[i] = float; } listBar[i] = listBar[i] - (listVal[i] + 2); if (listBar[i] < float) { listBar[i] = float; } fxShape.graphics.drawRect(num * i, th, num - 1, -listBar[i]); fxShape.graphics.drawRect(num * i, th - listDot[i] - 1, num - 1, 1); } fxShape.graphics.endFill(); waveFilter(false); } private function waveForm02():void { alphaNumber=0.998 _byteArray = bytePosition(byteArray()); fxShape.graphics.clear(); fxShape.graphics.lineStyle(1, _color.color24); if (_byteArray.bytesAvailable) { float = th * 0.4 * _byteArray.readFloat(); fxShape.graphics.moveTo(0, (th * 0.5 + float)); for (var i:int=1; i < 256; i++) { float = th * 0.4 * _byteArray.readFloat(); fxShape.graphics.lineTo(i * (tw * 0.00390625), th * 0.5 + float); } } waveFilter(false); } private function waveForm03():void { alphaNumber=0.92 _byteArray = byteArray(true); var num:Number = tw * 0.00195313; fxShape.graphics.clear(); fxShape.graphics.beginFill(_color.color24); fxShape.graphics.moveTo((tw * 0.5), th); if (_byteArray.bytesAvailable) { i = 255; for (var i:int=255; i > 0; i--) { float = th * 0.666 * _byteArray.readFloat(); fxShape.graphics.lineTo(i * num, th - float); } fxShape.graphics.lineTo(0, th); fxShape.graphics.moveTo(tw * 0.5, th); for (i=256; i < 512; i++) { float = th * 0.666 * _byteArray.readFloat(); fxShape.graphics.lineTo(i * num, th - float); } } fxShape.graphics.lineTo(tw, th); fxShape.graphics.endFill(); waveFilter(false); } private function waveForm04():void { alphaNumber=0.76 _byteArray = bytePosition(byteArray(true),true); fxShape.graphics.clear(); fxShape.graphics.lineStyle(1, _color.color24); fxShape.graphics.beginFill(_color.color24); fxShape.graphics.moveTo(0, th); for (var i:int=1; i < 256; i++) { float = th * 0.666 * _byteArray.readFloat(); list256[i] = list256[i] - (dy * 2); if (list256[i] < float) { list256[i] = float; } fxShape.graphics.lineTo(i * (tw * 0.00390625), th - list256[i]); } fxShape.graphics.lineTo(tw, th); fxShape.graphics.endFill(); waveFilter(false); } private function waveForm05():void { alphaNumber=0.82 _byteArray = byteArray(true); fxShape.graphics.clear(); fxShape.graphics.beginFill(_color.color24); fxShape.graphics.moveTo(0, th * 0.5); for (var i:int=0; i<512; i++) { float = th * 0.4 * _byteArray.readFloat(); list512[i] = (list512[i] - (dy * 2)); if (list512[i] < float) { list512[i] = float; } if (i >= 256) { if (i == 256) { fxShape.graphics.lineTo(tw, th * 0.5); fxShape.graphics.lineTo(0, th * 0.5); } fxShape.graphics.lineTo((i - 256) * tw * 0.00390625, th * 0.5 - list512[i]); } else { fxShape.graphics.lineTo((i * tw * 0.00390625), (th * 0.5 + list512[i])); } } fxShape.graphics.lineTo(tw, th * 0.5); fxShape.graphics.lineTo(0, th * 0.5); waveFilter(false); } private function waveForm06():void { alphaNumber=0.94 var cos:Number; var sin:Number; _byteArray = bytePosition(byteArray(true),true); fxShape.graphics.clear(); fxShape.graphics.lineStyle(5, _color.color24); fxShape.graphics.moveTo(tw * 0.5, th * 0.5); for (var i:int=0; i<256; i++) { float = th * 0.3 * _byteArray.readFloat() + 5; list256[i] = (list256[i] - dy); if (list256[i] < float) { list256[i] = float; list512[i] = (Math.random() * 360); } cos = float* Math.cos(list512[i] * (Math.PI / 180)) + tw * 0.5; sin = float* Math.sin(list512[i] * (Math.PI / 180)) + th * 0.5; fxShape.graphics.moveTo(tw * 0.5, th * 0.5); fxShape.graphics.lineTo(cos, sin); } waveFilter(false); } private function waveForm07():void { alphaNumber=0.40 var num:*; var list:*; _byteArray = bytePosition(byteArray(true),true); fxShape.graphics.clear(); for (var i:int=0; i < 256; i+=8) { if (list256[i] < 1) { list256[i] = 1; num = _byteArray.readFloat() * 20; float = _byteArray.readFloat() * 20; list256[i + 1] = num; list256[i + 2] = (tw - 40) * Math.random() + 20; float *= 0.5; if (float< 3) { float = 3; } list256[(i + 3)] = float; } else { num = list256; list = list256[(i + 1)] - 1; num[i + 1] = list; list256[i] = list256[i] + list256[i + 1]; /*if(list256[i + 3] > 3) { list256[i + 3] -=0.5; }*/ } fxShape.graphics.beginFill(_color.color24); fxShape.graphics.drawCircle(list256[i + 2], th - list256[i], list256[i + 3]); fxShape.graphics.endFill(); } waveFilter(); } private function waveForm08():void { alphaNumber=0.96 _byteArray = bytePosition(byteArray(true),true); fxShape.graphics.clear(); for (var i:int=0; i < 256; i+=8) { float = 10 * _byteArray.readFloat(); if (list256[i] < 1) { list256[i + 1] = (tw - (2 * 10)) * Math.random() + 10; list256[i + 2] = (th - (2 * 10)) * Math.random() + 10; list256[i + 3] = false; } if (list256[i] < float) { list256[i] = float; } list256[i] -= 0.5; if ((float > 8 && i) || list256[i + 3]) { if (list256[i + 3]) { if (list256[i + 4] > 0) { list256[i + 4]-= 0.05; } else { list256[i + 3] = false; } } else { list256[i + 4] = 1; list256[i + 3] = true; } fxShape.graphics.lineStyle(1, _color.color24, list256[i + 4]); fxShape.graphics.lineTo(list256[i + 1], list256[i + 2]); } fxShape.graphics.beginFill(_color.color24); fxShape.graphics.drawCircle(list256[i + 1], list256[i + 2], list256[i]); fxShape.graphics.moveTo(list256[i + 1], list256[i + 2]); fxShape.graphics.endFill(); } waveFilter(false); } private function waveForm09():void { alphaNumber=0.90 var abs:Number; var point:Point; var _round:int = 0; var cos:Number; var sin:Number; var _array:Array = []; var num:Number = 0; var def:Number = 0; var j:int = 1; var _int:int = 0; _byteArray = byteArray(true); fxShape.graphics.clear(); fxShape.graphics.lineStyle(1, _color.color24); while (_byteArray.bytesAvailable) { float = _byteArray.readFloat(); if (float> 0) { abs = float * th * 0.3 + 5; num += abs; def = num / j; _array.push(abs - def); j++; } } for (var i:int=0; i < _array.length; i++) { abs = Math.abs(((_array[i] - def) * 0.5)) + def; list512[i] -= dy; if (list512[i] < abs) { list512[i] = abs; } abs = list512[i]; _round =Math.round(i * 360 / _array.length) + dz; cos = Math.round(abs * Math.cos(_round * (Math.PI / 180))) + tw * 0.5; sin =Math.round(abs * Math.sin(_round * (Math.PI / 180))) + th * 0.5; if (i == 0) { fxShape.graphics.moveTo(tw * 0.5, th * 0.5); point = new Point(cos,sin); } fxShape.graphics.lineTo(cos, sin); if ((_round - _int) > 8) { fxShape.graphics.lineTo(tw * 0.5, th * 0.5); fxShape.graphics.moveTo(cos, sin); _int = _round; } } if (point) { fxShape.graphics.lineTo(point.x, point.y); } dz++; waveFilter(false); } private function waveForm10():void { alphaNumber=0.92 _byteArray = bytePosition(byteArray(true),true); fxShape.graphics.clear(); fxShape.graphics.lineStyle(1, _color.color24); for (var i:int = 0; i < 256; i+=8) { float = _byteArray.readFloat() * 20; if (list256[i] < 1) { list256[i + 1] = (tw - (2 * 20)) * Math.random() + 20; list256[i + 2] = (th - (2 * 20)) * Math.random() + 20; } if (list256[i] < float) { list256[i] = float; } var list:Array = list256; var j:int = i; var _list = list256[i] - 1; list[j] = _list; fxShape.graphics.drawCircle(list256[i + 1], list256[i + 2], list256[i]); } waveFilter(); } private function waveFilter(value:Boolean=true):void { _bitmapdata.lock(); if (_isFilter) { if (value) { applyDisplace(_bitmapdata); } else { _bitmapdata.applyFilter(_bitmapdata,_bitmapdata.rect,new Point(),new ColorMatrixFilter([1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,alphaNumber,0])); } } else { _bitmapdata.applyFilter(_bitmapdata,_bitmapdata.rect,new Point(),new ColorMatrixFilter([1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0.0,0])); } _bitmapdata.applyFilter(_bitmapdata, _bitmapdata.rect, new Point(), new BlurFilter(8,8,2)); _bitmapdata.draw(fxShape); _bitmapdata.unlock(); } private function zero(value:int):Array { var array:Array = []; for (var i:int = 0; i < value; i++) { array.push(0); } return array; } private function clearF():void { if (_bitmapdata) { _bitmapdata.fillRect(_bitmapdata.rect, 0); } } private function bytePosition(_byteArray:ByteArray, value:Boolean=false):ByteArray { var number:Number; var _byte:ByteArray=new ByteArray(); _byteArray.position = 0; if (_byteArray.bytesAvailable) { var byte:ByteArray = new ByteArray(); _byteArray.position = 1024; _byteArray.readBytes(byte); _byteArray.position = 0; while (byte.bytesAvailable) { var n:Number = _byteArray.readFloat(); var num:Number = byte.readFloat(); if (value ||n > 0 && num > 0) { number = n > num ? n:num; } else { if (n < 0&& num < 0) { number = n < num ? n:num; } else { number = (n + num) * 0.5; } } _byte.writeFloat(number); } _byte.position = 0; } return _byte; } private function applyDisplace(_bitmapdata:BitmapData):void { bitmapdata = _bitmapdata.clone(); var _bd:DisplacementMapFilter = new DisplacementMapFilter(bitmapdata,new Point(0,0),1,2,10,10,"clamp"); _bitmapdata.applyFilter(bitmapdata, _bitmapdata.rect, new Point(0,0), _bd); } private function byteArray(value:Boolean=false):ByteArray { var byte:ByteArray= new ByteArray(); if (SoundMixer.areSoundsInaccessible()) { stop(); } else { try { SoundMixer.computeSpectrum(byte,value); } catch (e:Error) { } } return byte; } public function start():void { stop(); dz = 0; bn = (tw * 0.125); if (bn >512) { bn = 512; } dy = th * 0.008; listVal = zero(bn); listDot = zero(bn); listBar = zero(bn); list256 = zero(256); list512 = zero(512); if (_bitmapdata) { _bitmapdata.clone(); } _bitmapdata = new BitmapData(tw,th,true,0); bitmap = addChild(new Bitmap(_bitmapdata)) as Bitmap; fn = mlArray[_fxID]; this.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler); } public function stop():void { if (! this.hasEventListener(Event.ENTER_FRAME)) { return; } this.removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler); fxShape.graphics.clear(); clearF(); } private function onEnterFrameHandler(event:Event):void { if (fn != null) { fn(); } } function timerHandler(event:TimerEvent):void { if (_fxID< 9) { _fxID++; } else { _fxID = 0; } start(); } function thisClickHandler(event:MouseEvent):void { var index:int = Math.floor(Math.random() * 10);//频谱随机ID trace(index); //先停止频谱 myTimer.stop(); stop(); _fxID = index; //从新运行频谱 myTimer.start(); start(); } } }