Sound equalizer in Flash Player 10

over 3 years 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.

 1 function drawSpec(et:TimerEvent):void
 2 {   
 3     //mp3sc is SoundSpectrum object returned by mp3.play() method
 4  if ( mp3sc.position )
 5  {
 6      //mp3ba is ByteArray object used to store samples
 7     mp3ba.length = mp3ba.position = 0;
 8     mp3.extract(mp3ba, 1764, mp3sc.position * 44.1 );
 9     mp3ba.position = 0;
10     var r:Number;
11     var l:Number;
12     var xx:Number = 0;
13 
14     //equalize is Shape object
15     equalizer.graphics.clear();
16     graphics.clear();
17         
18         graphics.lineStyle();
19     graphics.beginFill( 0xff9966, 0.3 );
20     l = -80 * mp3sc.leftPeak;
21     r = 80 * mp3sc.rightPeak;
22      graphics.drawRect( 0, 99, 588, l );
23      graphics.drawRect( 0, 101, 588, r );
24     graphics.endFill();
25 
26         graphics.lineStyle();
27     graphics.beginFill( 0xff9966, 0.5 );
28      graphics.drawRect( 0, 99+l, 
29                 Math.floor( mp3sc.position/mp3.length*588), (r-l)+2 );
30     graphics.endFill();
31         
32      while ( mp3ba.bytesAvailable )
33      {
34          //right channel
35          r = Math.abs(mp3ba.readFloat()*80);         
36          equalizer.graphics.lineStyle(3,0x9966ff,0.7,false,
37                            LineScaleMode.NORMAL,CapsStyle.SQUARE);
38          equalizer.graphics.moveTo( xx, 101 );  
39          equalizer.graphics.lineTo( xx, 101+r+1 );
40          equalizer.graphics.lineTo( xx, 101+r+1 );       
41          graphics.lineStyle(1,0x0000ff,0.9);
42          graphics.moveTo( xx, 101 );    
43          graphics.lineTo( xx, 101+r+1 );         
44          
45          //left channel
46          l = Math.abs(mp3ba.readFloat()*80);
47          equalizer.graphics.lineStyle(3,0x66ff99,0.7,false,
48                             LineScaleMode.NORMAL,CapsStyle.SQUARE);
49          equalizer.graphics.moveTo( xx, 99 );   
50          equalizer.graphics.lineTo( xx, 99-l-1 );        
51          graphics.lineStyle(1,0x00ff00,0.9);
52          graphics.moveTo( xx, 99 );     
53          graphics.lineTo( xx, 99-l-1 );      
54          xx+=1;
55          
56          //skip some samples
57          mp3ba.position += 2*8;
58      }; 
59     et.updateAfterEvent();
60  };
61 
62 };

Comments

No comments as yet. Be first to comment.

New comment

  1. (with http or https prefix)

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

  3. (javascript is required)

Website content premeditately commited by malczak & sobstel.
Layout by mlando. Icons by dryicons.com. All rights reserved.