Home / Tutorials / ActionScript / Psychedelic Sound Spectrum Analyzer in Actionscript 3.0 /

Flash Tutorials

Psychedelic Sound Spectrum Analyzer in Actionscript 3.0 - Tutorial

Posted by : FireCode on Nov 15, 2010

 

5.0/5

This tutorial will use the SoundMixer class. This class contains static properties and methods for global sound control in a Flash Player.

Using the computeSpectrum method of this class we can do some nice visual fx tricks.

We begin by opening a new document in Flash ( making sure it's a Actionscript 3.0 document ). Save the project as tutorial.fla.

Press the F9 key to bring up the ActionScript editor.

The first thing we are going to do is to create the variables that we will need during this tutorial.

var _sound:Sound  = null;
var _ba:ByteArray   =  null;
var _gfx:Sprite        =  null;

Now we are going to initialize our variables inside a function that we will call it Init()

function Init()
{
    _sound = new Sound(new URLRequest("tutorial.mp3"));

    _ba    = new ByteArray();

    _gfx   = new Sprite();
    addChild(_gfx);

    addEventListener(Event.ENTER_FRAME, Update, false, 0, true);

    _sound.play();
}

So, basically we've created a new sound instance that is requesting the path to our sound resource.

The ByteArray instance is going to work with the binary data sent by the sound we've loaded.

The graphical instance will help us to display the results of this tutorial.

We've added an event listener to the stage so we can update the changes in the graphical representation of the sound.

Finally we play the sound we've loaded.

Now let's create the updating function

function Update($e:Event):void
{
    SoundMixer.computeSpectrum(_ba,true);

    _gfx.graphics.clear();
    _gfx.graphics.beginFill(Math.random()*0xFFFFFF);

    for(var i:uint=0; i<100;i++)
    {
        var num:Number=-_ba.readFloat()*100-1;
        _gfx.graphics.drawRect(i*10,200,10,num);
    }
}

We are using the computeSpectrum method of the SoundMixer class to retrieve information about the sound is playing. Based on the information we are going to draw our graphics.

Now we simply call our initialization function.

Init();

Compile the project and LET IT ROCK on Motorhead - Ace of Spades !


FireCode is a promising project created by a couple of passionate Flash developers. Our purpose is to help you understand how Flash works and show you how to develop or customize your components.

We hope you enjoyed this tutorial. Don't forget to visit our portfolio to see our awesome Flash components.

no comment

Add comment

Please login to post comments.