The WaveAnalyzer class
The core of the movie is the WaveAnalyzer class, which has the methods to create an analyzer. We call this class from the movie. It is here that an instance of the sound object is created.
We import flash classes.
package biz.Flashscript
{
import flash.display.Sprite;
import flash.media.Sound;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.events.Event;
Then we import our custom classes including the class for the Sound.
import biz.Flashscript.Beat;
import biz.Flashscript.RectBar;
public class WaveAnalyzer extends Sprite
{
We declare static variables, scaleBar, for scaling the columns and, soundSenser, for setting a limit to the sound intensity, soundIntense.
private static var scaleBar:Number = 0.5; private static var soundSenser:Number = 10; private static var soundIntense:Number;
We create a new Sound object, which is the wave sound in the movie library and a new ByteArray object.
private var mySound:Sound = new Beat();
private var ba:ByteArray = new ByteArray();
public function WaveAnalyzer()
{
}
There is a public function, which is called from the movie.
public function arrangeStage():void
{
We create several bars by using a for loop and position them. We create random colors.
for (var a = 0; a<=9; a++)
{
var myBar:Sprite = new RectBar(0xFFFFFFFF*Math.random(), 0, 0x000000, 10, 2);
myBar.name = "myBar"+a;
myBar.x = 45+a*11;
myBar.y = 150;
this.addChild (myBar);
}
Then we play the sound repeatedly.
mySound.play(0, 999);
We add an event listener with an "ENTER_FRAME" event, since we want the analyzer to be continously active.
addEventListener(Event.ENTER_FRAME, processSound);
}
private function processSound(ev:Event):void
{
We use the computeSpectrum method to place sound waves into the ByteArray object.
SoundMixer.computeSpectrum(ba, true, 0);
We create a new variable, which will accumulate the current sound intensity. Since this a local variable it is constantly renewed. If we made this a class or static variable, the value would become bigger and bigger, which might be useful for creating certain effects in a movie.
var volSound:Number = 0;
We now loop through the ByteArray 256 times, since 1 byte is 11111111 and corresponds to a value of 256. The intensity of the sound wave is read from the byte stream as a floating number.
for (var i:uint = 0; i < 256; i++)
{
var soundIntense:Number = ba.readFloat();
volSound += soundIntense;
}
We loop through the number of columns.
for (var a = 0; a<=9; a++)
{
We create random numbers.
var b:Number = 1*Math.random(); var c:int = 5*Math.random();
We scale the bars according to the sound waves and multiply with a scale factor (scaleBar), but divide with a random number.
if (volSound > b*soundSenser && b != 0 && c != 0)
{
this.getChildByName("myBar"+a).scaleY = -volSound*scaleBar/c;
}
else
{
this.getChildByName("myBar"+a).scaleY = 1;
}
}
}
}
}
This brings us to the end of this tutorial. You can use any sound object, which you can import into Flash for this purpose. However, only mp3 files can be loaded externally.