Commits

Anonymous committed 4fa128e

add sound inspector draft

Comments (0)

Files changed (2)

 		{
 			var info : LoaderInfo = LoaderInfo(event.target);
 
+			if(info == null) {
+				throw new Error("info is null");
+			}
+
+			if(info.content == null) {
+				throw new Error("info.content is null");
+			}
+
+			if(info.content.root == null) {
+				throw new Error("info.content.root is null");
+			}
+
 			if(info.content.root.stage == null) {
 				return;
 			}
 			mainStage.addChild(this);
 
 			__mouseInspector = new UnderMouseInspector(mainStage);
+			var soundInspector : ISwitchable = new SoundInspector(mainStage);
 			__onOffControl = new OnOffControl(mainStage);
 			__onOffControl.addSwitchable(__mouseInspector);
+			__onOffControl.addSwitchable(soundInspector);
 		}
 	}
 }

src/SoundInspector.as

+package
+{
+	import flash.display.Graphics;
+	import flash.display.Sprite;
+	import flash.display.Stage;
+	import flash.events.Event;
+	import flash.events.MouseEvent;
+	import flash.media.SoundMixer;
+	import flash.utils.ByteArray;
+
+	public class SoundInspector implements ISwitchable
+	{
+		private static const PLOT_HEIGHT : int = 100;
+		private static const CHANNEL_LENGTH : int = 100;
+
+		private var __stage : Stage;
+		private var __bytes : ByteArray;
+		private var __holder : Sprite;
+		private var __enabled : Boolean;
+
+		public function SoundInspector(stage : Stage)
+		{
+			__stage = stage;
+			__bytes = new ByteArray();
+			__holder = new Sprite();
+			__holder.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
+		}
+
+		private function onMouseDown(event : MouseEvent) : void
+		{
+			if(__enabled) {
+				__holder.startDrag();
+				__stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
+			}
+		}
+
+		private function onMouseUp(event : MouseEvent) : void
+		{
+			__stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
+			__holder.stopDrag();
+		}
+
+		public function switchOnOff(value : Boolean) : void
+		{
+			__enabled = value;
+
+			if(__enabled) {
+				__stage.addChild(__holder);
+				__stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
+			} else {
+				__stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
+				__stage.removeChild(__holder);
+			}
+		}
+
+		private function onEnterFrame(event : Event) : void
+		{
+			SoundMixer.computeSpectrum(__bytes, false, 0);
+
+			var g : Graphics = __holder.graphics;
+			g.beginFill(0xffffff, 0.1);
+			g.drawRect(0, 0, CHANNEL_LENGTH, PLOT_HEIGHT);
+			g.endFill();
+
+			g.clear();
+			g.lineStyle(0, 0x6600CC);
+			g.beginFill(0x6600CC);
+			g.moveTo(0, PLOT_HEIGHT);
+
+			var n : Number = 0;
+
+			// left channel
+			for(var i : int = 0; i < CHANNEL_LENGTH; i++) {
+				n = (__bytes.readFloat() * PLOT_HEIGHT);
+				g.lineTo(i * 2, PLOT_HEIGHT - n);
+			}
+
+			g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
+			g.endFill();
+
+			// right channel
+			g.lineStyle(0, 0xCC0066);
+			g.beginFill(0xCC0066, 0.5);
+			g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);
+
+			for(i = CHANNEL_LENGTH; i > 0; i--) {
+				n = (__bytes.readFloat() * PLOT_HEIGHT);
+				g.lineTo(i * 2, PLOT_HEIGHT - n);
+			}
+
+			g.lineTo(0, PLOT_HEIGHT);
+			g.endFill();
+		}
+	}
+}