其他分享
首页 > 其他分享> > as3.0声音波形频谱系列_01

as3.0声音波形频谱系列_01

作者:互联网

效果:

 

/**
*
* *--------------------------------*
* | *** as3.0声音波形频谱系列 *** |
* *--------------------------------*
*
* 编辑修改收录:fengzi(疯子、wu341、wgq341)
*
* 不会写代码,我是代码搬运工。
*
* 联系方式:QQ(493712833)。
*
* 随 笔: https://www.cnblogs.com/fengziwu/
*
* 版权协议:请自觉遵守LGPL协议,欢迎修改、复制、转载、传播给更多需要的人。
* 免责声明:任何因使用此软件导致的纠纷与软件/程序开发者无关。
* 日 期: 2014.02.23
*
* * ------------------ Example -------------------- *
*
var spectrum:Spectrum=new Spectrum(550,400);
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.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.display.GradientType;


public class Spectrum extends Sprite
{
private var bitmapData:BitmapData;
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 thisW:Number;
private var thisH:Number;



/***
* Spectrum
* @param w 显示的宽度
* @param h 显示的高度
***/
public function Spectrum(w:Number=550,h:Number=400):void
{
thisW=w;
thisH=h;
bytes = new ByteArray();
bitmapData = new BitmapData(thisW,thisH,true,0);
peaks = new BitmapData(thisW,thisH,true,0);
fadeFill = new BitmapData(thisW,thisH,true,0x5000000);
displace = new Matrix();
displace.tx = 5;
displace.ty = 0;

gradient = createRainbowGradientArray();
rect = new Rectangle(0,0,1,0);
addChild(new Bitmap(bitmapData));
addChild(new Bitmap(peaks));
addEventListener(Event.ENTER_FRAME, enterFrameHandler);

//画一个黑色背景
graphics.beginFill(0);
graphics.drawRect(0, 0, thisW, thisH);
graphics.endFill();

}

private function enterFrameHandler(_arg_1:Event ):void
{
var float:Number;
var num:Number;
bitmapData.lock();

peaks.fillRect(peaks.rect, thisH);
SoundMixer.computeSpectrum(bytes, true, 0);

for (var i:int=0; i<256; i++)
{
float = bytes.readFloat();
if (i == 0)
{
num = float;
}
else
{
num += (float - num) /8;
}

rect.x = i +8;
rect.y = thisH + (i >> 20) - num * thisH + 2;
rect.height = num * thisH + 2;
peaks.setPixel32(rect.x, rect.y, 0xFFFFFFFF);
bitmapData.fillRect(rect, (0xFF000000 | gradient[i]));

}
//把图形贴到位图上显示出来
bitmapData.draw(fadeFill, null, null, null, null, true);
bitmapData.draw(bitmapData, displace, null, null, null, true);
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(0x6633cc,0xff0000,0xffff00,0x00ff00,0x00ffff);//颜色
var alphas:Array= new Array(0.3,0.5,0.8,1,1);//透明度
var ratios:Array=new Array(1,32,128,192,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;
}




}
}

  

标签:as3.0,频谱,01,flash,private,import,var,new,Array
来源: https://www.cnblogs.com/fengziwu/p/14418050.html