In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: SoundVisualizer

// forked from hiro_rec's SoundVisualizer
package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.filters.DisplacementMapFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.net.URLRequest;
	import flash.system.Security;
	import flash.utils.ByteArray;
	
	[SWF( backgroundColor='#000000')]
	public class Main extends Sprite
	{
		private var container:Sprite;

		private var s:Sound;
		private var sc:SoundChannel;
		private var ba:ByteArray;
		private var mode:Boolean;
 		
 		private var max:int;
		private var offset:Number;
		private var bar:Sprite;
		private var centerY:int;
		
		private var grad:Shape;
		private var gradBmd:BitmapData;
		
		private var rect:Rectangle;
		private var bmd:BitmapData;
		private var bmp:Bitmap;
		private var blur:BlurFilter;
		private var target:Sprite;
		
		private var radius:Number;
		private var rot:Number;
		private var vs:Number;
		
		private var dm:DisplacementMapFilter;;


		public function Main()
		{
			buttonMode = true;
			
			Security.loadPolicyFile("http://start-rec.net/crossdomain.xml"); 
			
			container = new Sprite();

			s = new Sound();
			mode = false;
	 		
	 		max = 256;
			offset = stage.stageWidth/max;
			bar = new Sprite();
			centerY = stage.stageHeight/2;
			
			grad = new Shape();
			gradBmd = new BitmapData(stage.stageWidth, stage.stageHeight);
			
			rect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
			bmd = new BitmapData(rect.width, rect.height, true, 0x00000000);
			bmp = new Bitmap(bmd);
			blur = new BlurFilter(3, 3, 1);
			target = bar;
			
			radius = 200;
			rot = 0;
			vs = 1.03 * -1;
			
			dm = new DisplacementMapFilter(gradBmd, null, 0, 0, 0, 0, "wrap", 0xFF0000, 1);
			
			
			s.load(new URLRequest("http://start-rec.net/data/wonderfl/sounds/bgm01.mp3"));
			sc = s.play();

			addChild(container);
			addChild(bar);
		
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			
			grad.graphics.beginGradientFill(GradientType.LINEAR, [0xFF0000, 0x0000FF], [100, 100], [0, 255]);
			grad.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			grad.graphics.endFill();
			gradBmd.draw(grad);
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		}
 
		private function enterFrameHandler(event:Event):void
		{
			spectrum();
			render()
		} 

		private function spectrum():void
		{
			var n:Number = 0;
			
			ba = new ByteArray();
			SoundMixer.computeSpectrum(ba, mode, 0);
			
			bar.graphics.clear();
			bar.graphics.lineStyle(1, Math.random() > 0.5 ? 0xFFFFFF : 0 );
			bar.graphics.moveTo(-1, centerY);
			
                        var s:uint = Math.ceil(Math.random()*3);
			for(var i:int=0; i < max; i++)
			{
				if (i % 2 == 0)
				{
					n = ba.readFloat()*300*s;
					var px:int = offset * i;
					bar.graphics.lineTo(px, centerY-n );
				}
			}
			
			bar.graphics.moveTo(stage.stageWidth, centerY);

		}

		private function render():void
		{
			var radian:Number = Math.PI/180 * rot;
			var val:Number = Math.sin(radian) * radius;
			rot += 1
			
			var r:Number = -0;
			var g:Number = Math.abs(val / 15) * -1;
			var b:Number = val * 1.5;
			
			container.transform.colorTransform = new ColorTransform(1, 1, 1, 1, r, g, b, 100)
		
			var matrix:Matrix = new Matrix();
			matrix.scale(vs, vs);
			matrix.tx = (rect.width * vs - rect.width) / 2 * -1;
			matrix.ty = (rect.height * vs - rect.height) / 2 * -1;
			
			xRotateAt(matrix, rect.width/2, rect.height/2, 8 );
			
			bmd.draw(stage, matrix);
			//bmd.applyFilter(bmd, rect, new Point(), blur);
			
			dm.componentX = rot;
			container.filters = [dm];
			
			container.addChild(bmp);
		}

		private function xRotateAt(m:Matrix, nx:Number, ny:Number, degree:Number):Matrix
		{
 			var point:Point = new Point(nx, ny);
			point = m.transformPoint(point);
			m.tx -= point.x;
			m.ty -= point.y;
			m.rotate(degree * (Math.PI/180));
			m.tx += point.x;
			m.ty += point.y;
		  
			return m;
		} 
		
			private function mouseDownHandler(event:MouseEvent):void
		{
			mode = !mode;
		}
	}
}