Visualizing Sound example - Wednesday, April 23rd, 2008



var snd:Sound = new Sound();
snd.load(new URLRequest("song.mp3"));

var channel:SoundChannel = new SoundChannel();
channel = snd.play();

this.addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);

function onLoop(evt:Event):void {
	lPeak.barMask.scaleY = channel.leftPeak;
	rPeak.barMask.scaleY = channel.rightPeak;
}

/* You do NOT have the following code on your 04/23/2008 handout.
   As you can see, my code only traces the activity of the left 
      channel of the sound.   */

var canvas:Sprite = new Sprite();
addChild(canvas);
var g:Graphics = canvas.graphics;

initCanvas();

function initCanvas():void {
	g.clear();
	g.lineStyle(2, 0x06600CC);
	g.moveTo(0, 300);
}

var myTimer:Timer = new Timer(50);
myTimer.addEventListener("timer", timerHandler);
myTimer.start();

var xInc:int = 0;

function timerHandler(ev:TimerEvent):void {
	g.lineTo(xInc, 300 - channel.leftPeak * 250);
	// g.lineTo(xInc, 280 - channel.rightPeak * 100);
	if (xInc > 550) {
		xInc = 0;
		myTimer.stop();
		//initCanvas();
	} else {
		xInc += 2;
	}
}
See the Striker ball mouseX and mouseY program for drawing lines with the lineTo() method on a Shape or a Sprite object.