Sound equalizer in Flash Player 10

over 1 year ago malczak actionscript comments

This is just a quick note, or i should rather say sound experiment with new flash player 10. This was inspired by a topic on the flashzone.pl forum.

This example shows how to use
Sound.extract(target:ByteArray, length:Number, startPosition:Number = -1):Number
method to draw sound spectrum.
Method fills target buffer with 44100Hz sampled sound data. This data can be then used to draw sound specturm. Parameter startPosition,sample at which extraction begins, is easily determined from current sound stream position.

In working example screen is updated every 40ms. You need Flash Player 10 to run this example !

Flash player (ver. 10.0.0) is required to see this content. Please update to newest possible version.

Get newest Adobe Flash player

Full source code can be downloaded here, screen redraw function is also shown below.

function drawSpec(et:TimerEvent):void
{   
    //mp3sc is SoundSpectrum object returned by mp3.play() method
 if ( mp3sc.position )
 {
     //mp3ba is ByteArray object used to store samples
    mp3ba.length = mp3ba.position = 0;
    mp3.extract(mp3ba, 1764, mp3sc.position * 44.1 );
    mp3ba.position = 0;
    var r:Number;
    var l:Number;
    var xx:Number = 0;

    //equalize is Shape object
    equalizer.graphics.clear();
    graphics.clear();
        
        graphics.lineStyle();
    graphics.beginFill( 0xff9966, 0.3 );
    l = -80 * mp3sc.leftPeak;
    r = 80 * mp3sc.rightPeak;
     graphics.drawRect( 0, 99, 588, l );
     graphics.drawRect( 0, 101, 588, r );
    graphics.endFill();

        graphics.lineStyle();
    graphics.beginFill( 0xff9966, 0.5 );
     graphics.drawRect( 0, 99+l, 
                Math.floor( mp3sc.position/mp3.length*588), (r-l)+2 );
    graphics.endFill();
        
     while ( mp3ba.bytesAvailable )
     {
         //right channel
         r = Math.abs(mp3ba.readFloat()*80);         
         equalizer.graphics.lineStyle(3,0x9966ff,0.7,false,
                           LineScaleMode.NORMAL,CapsStyle.SQUARE);
         equalizer.graphics.moveTo( xx, 101 );  
         equalizer.graphics.lineTo( xx, 101+r+1 );
         equalizer.graphics.lineTo( xx, 101+r+1 );       
         graphics.lineStyle(1,0x0000ff,0.9);
         graphics.moveTo( xx, 101 );    
         graphics.lineTo( xx, 101+r+1 );         
         
         //left channel
         l = Math.abs(mp3ba.readFloat()*80);
         equalizer.graphics.lineStyle(3,0x66ff99,0.7,false,
                            LineScaleMode.NORMAL,CapsStyle.SQUARE);
         equalizer.graphics.moveTo( xx, 99 );   
         equalizer.graphics.lineTo( xx, 99-l-1 );        
         graphics.lineStyle(1,0x00ff00,0.9);
         graphics.moveTo( xx, 99 );     
         graphics.lineTo( xx, 99-l-1 );      
         xx+=1;
         
         //skip some samples
         mp3ba.position += 2*8;
     }; 
    et.updateAfterEvent();
 };

};
Share |

Comments

No comments as yet. Be first to comment.

New comment

  1. (never shown, just for us for possible contact)

  2. (with http or https prefix)

  3. (use [code][/code] tags to post a code snippet)

  4. (javascript is required)

Website content premeditately commited by segfaultlabs (malczak & sobstel).
Layout by mlando. Icons by DryIcons.com. All rights reserved, of course.