/*

import flash.display.BlendMode;
blendMode=BlendMode.SCREEN  关闭黑色背景

public function onStageReSize():void 自适应缩放 

*/


package 
{
	import color.ColorChange
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.utils.ByteArray;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.media.SoundMixer;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Matrix;
	import flash.display.BlendMode;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.events.MouseEvent;
	import flash.display.BlendMode;

	public class LineToGroups extends Sprite
	{
		private var sprite:Sprite;
		private var bitmap:Bitmap;
		private var bitmapData:BitmapData;
		private var byteArray:ByteArray;
		private var thisW:Number;
		private var thisH:Number;
		private var singleW:Number;
		private var pos:Number = 0;
		private var matrix:Matrix;
		private var mode:int =1;
		private var count:int = 0;
		private var numArr:Array = [];
		private var fftMode:Boolean = true;
		private var colorChange:ColorChange=new ColorChange();

		public function LineToGroups(w:Number=720,h:Number=480)
		{
			thisW = w;
			thisH = h;
			sprite=new Sprite();
			byteArray = new ByteArray();

			for (var i:int= 0; i < 256; ++ i)
			{
				numArr[i] = 0;
			}


			bitmapData = new BitmapData(thisW,thisH,false,0);
			bitmap = addChild(new Bitmap(bitmapData)) as Bitmap;

			//blendMode=BlendMode.SCREEN
			
			this.addEventListener(MouseEvent.CLICK,onClickHandler);
			addEventListener(Event.ENTER_FRAME,enterframeHandler);
		}

		//ReSize
		public function onStageReSize():void
		{
			thisW = stage.stageWidth;
			thisH = stage.stageHeight;
			if (bitmapData)
			{
				bitmapData.dispose();
			}
			bitmapData = new BitmapData(thisW,thisH,false,0);

			bitmap = addChild(new Bitmap(bitmapData)) as Bitmap;
			
			
			//blendMode=BlendMode.SCREEN
		}

		private function onClickHandler(event:MouseEvent):void
		{
			count = 0;
			mode = Math.floor(Math.random() * 5) + 1;
			
		}

		private function enterframeHandler(event:Event):void
		{
			var num:Number;
			var i:uint;
			var xPos:Number;
			var yPos:Number;

			if (count %500 == 0)
			{
				if (++mode > 6)
				{
					mode = 1;
				}
			}
			SoundMixer.computeSpectrum(byteArray,fftMode,1);

			sprite.graphics.clear();
			sprite.graphics.lineStyle(1,colorChange.color);
			if (mode==1)
			{
				fftMode = false;
				for (i= 0; i <256; i+=4)
				{
					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 / 256 * Math.PI / 180) * (30+num*thisH/4),thisH/2 + Math.cos(i * 360 / 256 * Math.PI / 180+pos) * (30+num*thisH/4), num * 20 );
				}
				sprite.graphics.beginFill(colorChange.color, 1 );
				for (i = 0; i <256; i+=4)
				{
					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 /256 * Math.PI / 180) * (40+num*thisH/3),thisH/2 + Math.cos(i * 360 /256 * Math.PI / 180+pos) * (30+num*thisH/3)+thisH/8,num *5 );
				}
				pos +=  0.04;

			}



			if (mode==2)
			{
				pos = 0;
				fftMode = true;
				sprite.graphics.moveTo(-Math.sin(0)*2 + thisW/2 + thisW/8*Math.sin(pos), Math.cos(0) + thisH*0.6 + thisH/8*Math.cos(pos/2));
				for (i= 0; i <512; i++)
				{
					num = byteArray.readFloat() + 0.1;
					var a:uint = i;
					if (i <256)
					{
						a +=  256;
					}
					var _size:Number = Math.ceil(thisH* 0.75*0.8*(num+0.5));
					xPos= -Math.sin(i/256*Math.PI)*_size*num*2 + thisW/2 + thisW/8*Math.sin(pos);
					yPos= Math.cos(a/256*Math.PI)*_size*num/2 + thisH*0.6 + thisH/8*Math.cos(pos/2);
					sprite.graphics.lineTo(xPos, yPos);
				}
				//pos +=  0.02;

			}



			if (mode==3)
			{
				fftMode = false;
				for (i= 0; i <512; i+=2)
				{
					num = byteArray.readFloat();
					var b:uint = i;
					if (i >= 256)
					{
						b -=  256;
					}
					if (i ==256)
					{
						sprite.graphics.moveTo(0, thisH/2);
					}
					xPos= b/512*(thisW*4+2);
					yPos = thisH / 4 *  -  num;
					xPos -=  2;
					if (i >= 256)
					{
						yPos *=  -1;
					}
					sprite.graphics.lineTo(xPos, yPos + thisH/2);
				}

			}

			if (mode==4)
			{
				fftMode = false;
				sprite.graphics.moveTo(thisW/2,thisH/2-thisH/2*0.1);
				for (i= 0; i <512; i++)
				{
					num = byteArray.readFloat();
					var angleInRadians:Number=i*(360/512)*(Math.PI/180);
					xPos = thisW / 2 + thisW / 2 * Math.sin(angleInRadians) * num;
					yPos = thisH / 2 - thisH / 2 * Math.cos(angleInRadians) * num;
					sprite.graphics.lineTo(xPos, yPos);
				}

			}

			if (mode==5)
			{
				fftMode = false;
				for (i= 0; i <512; i+=4)
				{
					num = byteArray.readFloat();
					var c:uint = i;
					if (i >=512)
					{
						c -=  256;

					}
					xPos = c / 256 * (thisW + 10);
					yPos =  -  num * thisH / 4 + Math.cos(pos) * (thisH / 8);
					if (i >=256)
					{
						yPos *=  -1;
					}

					if (i ==256)
					{
						sprite.graphics.moveTo(0, yPos + thisH/2);
					}
					sprite.graphics.lineTo(xPos, yPos + thisH/2);
				}
				pos +=  0.01;

			}

			if (mode==6)
			{
				fftMode = true;
				sprite.graphics.moveTo(thisW/4,thisH/2);
				singleW = (thisW - thisW / 2) / 256;
				for (i= 0; i < 256; i++)
				{
					num = byteArray.readFloat();
					sprite.graphics.lineTo(thisW/4+ i*singleW,thisH/2 + num *thisH/3);
				}

				sprite.graphics.moveTo(thisW/4,thisH/2);
				for (i = 0; i < 256; i++)
				{
					num = byteArray.readFloat();
					sprite.graphics.lineTo(thisW/4  + i*singleW,thisH/2 - num *thisH/3);
				}
			}
			sprite.graphics.endFill();






			matrix=new Matrix();
			matrix.translate( -  thisW/2, -  thisH/2);
			matrix.scale(1.1,1.1);
			matrix.translate(thisW/2,thisH/2);

			bitmapData.lock();

			bitmapData.draw(bitmapData,matrix,new ColorTransform(0.96,0.96,0.96));
			//bitmapData.draw(bitmapData,matrix,new ColorTransform(0.96,0.96,0.96),BlendMode.NORMAL);


			//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,.998,0]));

			bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point(0,0),new BlurFilter(4,4,1));

			bitmapData.draw(sprite);

			bitmapData.unlock();
			count++;
		}

	}

}

  

posted on 2021-10-10 20:28  疯子_wu  阅读(87)  评论(0编辑  收藏  举报