效果:
/** * * *--------------------------------* * | *** as3.0声音波形频谱系列 *** | * *--------------------------------* * * 编辑修改收录:fengzi(疯子、wu341、wgq341) * * 不会写代码,我是代码搬运工。 * * 联系方式:QQ(493712833)。 * * 随 笔: https://www.cnblogs.com/fengziwu/ * * 版权协议:请自觉遵守LGPL协议,欢迎修改、复制、转载、传播给更多需要的人。 * 免责声明:任何因使用此软件导致的纠纷与软件/程序开发者无关。 * 日 期: 2014.02.23 * * * ------------------ Example -------------------- * * var spectrum:Spectrum=new Spectrum(550,400,true); addChild(spectrum); * @***欢迎添加修改*** **/ package { import flash.display.Sprite; import flash.geom.Matrix; import flash.utils.ByteArray; import flash.media.SoundMixer; import flash.events.Event; import flash.utils.setInterval; import flash.display.BitmapData; import flash.display.Bitmap; import flash.filters.BlurFilter; import flash.filters.ColorMatrixFilter; import flash.geom.Point; import flash.geom.ColorTransform; import flash.geom.Rectangle; import flash.display.GradientType; import flash.display.PixelSnapping; public class Spectrum extends Sprite { private var bitmapData:BitmapData; private var bitmap:Bitmap; private var fadeFill:BitmapData; private var gradient:Array; private var displace:Matrix; private var bytes:ByteArray; private var peaks:BitmapData; private var rect:Rectangle; private var blur:BlurFilter; private var colorMatrixFilter:ColorMatrixFilter; private var thisW:Number; private var thisH:Number; private var _sprite:Sprite; private var matrix:Matrix; private var transformMatrix:Matrix; private var _bitmapdata:BitmapData; private var effect:Boolean; private var wx:Number; /*** * Spectrum * @param w 显示的宽度 * @param h 显示的高度 * @param effect 效果选择 ***/ public function Spectrum(w:Number=512,h:Number=300,effect:Boolean=true):void { thisW = w; wx=w/512; thisH = h; this.effect = effect; _sprite = new Sprite(); bytes = new ByteArray(); colorMatrixFilter = new ColorMatrixFilter([1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0]); bitmapData = new BitmapData(thisW,thisH,false,0); peaks = new BitmapData(thisW,thisH,true,0); gradient = createRainbowGradientArray(); rect = new Rectangle(0,0,1,0); bitmap = addChild(new Bitmap(bitmapData,PixelSnapping.AUTO,false)) as Bitmap; _bitmapdata = new BitmapData(thisW,thisH,true,0); transformMatrix = new Matrix((thisW + thisW / 40) / thisW,0,0,(thisH + thisH / 80) / thisH,(-thisW / 40) /2,(-thisH /80) /8); addChild(new Bitmap(peaks)); addEventListener(Event.ENTER_FRAME, enterFrameHandler); } private function enterFrameHandler(_arg_1:Event ):void { var float:Number; var num:Number; bitmapData.lock(); peaks.fillRect(peaks.rect, 0); var colors:Array = new Array(0x00ff00,0xffff00,0xff0000,0xff0000,0xff00ff,0xff0000,0xff0000,0xffff00,0x00ff00);//颜色 var ratios:Array=new Array(20,45,80,110,128,146,185,210,235)//渐变位置 if (effect) { var alphas:Array= new Array(0.8,0.5,1,0.8,1,0.8,1,0.5,0.8);//透明度 }else{ alphas=[1,1,1,1,1,1,1,1,1];//透明度 ratios=[0,45,80,100,128,156,185,210,255]//渐变位置 } var matrixs:Matrix= new Matrix(); matrixs.createGradientBox(thisW, 1, 0, 0, 0); SoundMixer.computeSpectrum(bytes, true, 0); _sprite.graphics.clear(); _sprite.graphics.lineStyle(0,0,0); _sprite.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrixs); /*left*/ _sprite.graphics.moveTo(thisW/2,thisH); for (var i:int=0; i<256; i++) { float = bytes.readFloat() / 1.4; if (i == 0) { num = float; } else { num += (float - num) / 20; } rect.x = i * wx + thisW / 2; rect.y = thisH + (i >> 20) - num * thisH; peaks.setPixel32(rect.x, rect.y, 0xFFFFFFFF); peaks.setPixel32(i+thisW/2+i*wx, rect.y, 0xFFFFFFFF); _sprite.graphics.lineTo(rect.x, rect.y); } _sprite.graphics.lineTo(thisW,thisH); _sprite.graphics.moveTo(thisW/2, thisH); for (i=0; i<256; i++) { float = bytes.readFloat() / 1.4; if (i == 0) { num = float; } else { num += (float - num) / 20; } rect.x = thisW / 2 - i * wx; rect.y = thisH + (i >> 20) - num * thisH; peaks.setPixel32(rect.x, rect.y, 0xFFFFFFFF); peaks.setPixel32(rect.x-i*wx, rect.y, 0xFFFFFFFF); _sprite.graphics.lineTo(rect.x,rect.y); } _sprite.graphics.lineTo(0,thisH); _sprite.graphics.endFill(); /**** 选择效果 ****/ if (effect) { _bitmapdata = bitmapData.clone(); bitmapData.draw(_bitmapdata, transformMatrix, new ColorTransform(0.90, 0.92, 0.95), null, null, true); } else { matrix=new Matrix(); matrix.translate( - thisW/2, - thisH/2); matrix.scale(1.14,1.01); matrix.translate(thisW/2,thisH/2); bitmapData.draw(bitmapData,matrix,new ColorTransform(0.92,0.92,0.92)); } /*****************************/ bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point(),colorMatrixFilter); bitmapData.applyFilter(bitmapData, bitmapData.rect, new Point(), new BlurFilter(8, 8,2)); bitmapData.draw(_sprite, null, null, "add"); bitmapData.unlock(); } private function createRainbowGradientArray():Array { var clorArray:Array= new Array(); var bitmapdata:BitmapData; var sprite:Sprite= new Sprite(); //渐变填充sprite//渐变填充sprite var colors:Array = new Array(0xff00ff,0xff0000,0xffff00,0x00ff00);//颜色 var alphas:Array = new Array(1,1,1,1);//透明度 var ratios:Array = new Array(10,72,155,255);//渐变位置 var matrix:Matrix= new Matrix(); bitmapdata = new BitmapData(256,1,false,0); matrix.createGradientBox(256, 1, 0, 0, 0); //GradientType.LINEAR线性渐变 sprite.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix); sprite.graphics.drawRect(0, 0, 256, 1); sprite.graphics.endFill(); bitmapdata.draw(sprite); //bitmapdata的getPixel方法(获取位图sprite的颜色值); for (var i:int=0; i< 256; i++) { clorArray[i] = bitmapdata.getPixel(i,0); } //返回一长度为256的数组(里面装载了256个32位的颜色值) return clorArray; } } }