[转] 一个使用SoundMixer的炫酷频分析

http://bbs.9ria.com/viewthread.php?tid=77229&extra=page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter%3D2592000


[转] 一个使用SoundMixer的炫酷频分析_第1张图片


package {
	/*
	*引入绘图类,用于绘制圆圈
	*引入基本事件类
	*引入鼠标事件类,用于监听鼠标的点击
	*引入声音类,用于播放音乐
	*引入声音混音器类,用于获取flash当前播放声音的各个频段的分量
	*引入图形类
	*引入URL请求类,用于请求MP3文件
	*引入图像滤镜质量类,用于设置各个滤镜的渲染质量
	*引入模糊滤镜类
	*引入发光滤镜类
	*引入字节数组类
	*/
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundMixer;
	import flash.net.URLRequest;
	import flash.display.Sprite;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.utils.ByteArray;

	public class SoundMixerExample extends Sprite {
		private var req:URLRequest;
		private var soundFactory:Sound;
		private var method:uint;
		private var mainSprite:Sprite;
		private var getParme:Function;

		public function SoundMixerExample() {
			play_btn.addEventListener(MouseEvent.CLICK, playClickHandler);
			change_btn.addEventListener(MouseEvent.CLICK, changeClickHandler);
		}


		private function playClickHandler(event:MouseEvent):void {
			method=0;
			getParme=getParmeMethod0;
			mainSprite=new Sprite();
			this.addChild(mainSprite);
			change_sprite.addEventListener(MouseEvent.CLICK, changeClickHandler);
			soundFactory=new Sound();
			req=new URLRequest(path.text.replace("\r","").replace("\n",""));
			soundFactory.load(req);
			soundFactory.play();
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function changeClickHandler(event:MouseEvent):void {
			method=++method%3;
			switch (method) {
				case 0 :
					getParme=getParmeMethod0;
					mainSprite.mask=null;
					break;
				case 1 :
					getParme=getParmeMethod1;
					mainSprite.mask=null;
					break;
				case 2 :
					getParme=getParmeMethod2;
					mainSprite.mask=mainMask;
					break;
			}
		}

		private function getGlowFilter(color) {
			var alpha:Number=1;
			var blurX:Number=30;
			var blurY:Number=30;
			var strength:Number=15;
			var inner:Boolean=false;
			var knockout:Boolean=false;
			var quality:Number=3;

			return new GlowFilter(color,
			    alpha,
			    blurX,
			    blurY,
			    strength,
			    quality,
			    inner,
			    knockout);

		}
		private function getBlurFilter() {
			var blurX:Number=0;
			var blurY:Number=3;
			return new BlurFilter(blurX, blurY, BitmapFilterQuality.HIGH);
		}
		private function onEnterFrame(event:Event):void {
			const PLOT_HEIGHT:int=50;
			const CHANNEL_LENGTH:int=64;
			const STEP=128/CHANNEL_LENGTH;
			var bytes:ByteArray = new ByteArray();
			SoundMixer.computeSpectrum(bytes, true, 0);
			var g:Graphics=mainSprite.graphics;
			g.clear();
			var n:Number=0;


			// left channel
			for (var i:int = 0; i < CHANNEL_LENGTH; i+=STEP) {
				g.lineStyle(0,Math.floor(0x111111*bytes.readFloat()*16));
				var parme=getParme(i);
				g.drawCircle(parme[0],parme[1],Math.pow(bytes.readFloat()+0,2)*PLOT_HEIGHT);
			}
			// right channel
			g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);

			for (i = CHANNEL_LENGTH; i > 0; i-=STEP) {
				g.lineStyle(0,Math.floor(0x11111111*bytes.readFloat()*16));
				parme=getParme(i);
				g.drawCircle(parme[0],parme[1]-100,Math.pow(bytes.readFloat()+0,2)*PLOT_HEIGHT);
			}
			g.endFill();
			mainSprite.filters=new Array(getBlurFilter(),getGlowFilter(Math.floor(0x11111111*bytes.readFloat()*16)));
		}
		private function getParmeMethod0(num:Number) {
			return new Array(10*num+100,200);
		}
		private function getParmeMethod1(num:Number) {
			return new Array(10*Math.random()*45+175,220);
		}
		private function getParmeMethod2(num:Number) {
			return new Array(Math.random()*550+125,400*Math.random());
		}

	}
}

你可能感兴趣的:(.net,PHP,Flash,音乐,bbs)