Posted By

sidneydekoning on 07/16/09


Tagged

mp3 sound Drawing as3 equalizer soundmizer


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

macKaminski
Pingpongbal
TrentSkunk


Creating an Equalizer that responds to the SoundMixer


 / Published in: ActionScript 3
 

  1. package nl.funkymonkey.drawing {
  2. import nl.funkymonkey.ui.Colors;
  3. import nl.funkymonkey.utils.MathUtils;
  4.  
  5. import flash.display.Shape;
  6. import flash.display.Sprite;
  7. import flash.media.SoundMixer;
  8. import flash.utils.ByteArray;
  9.  
  10. /**
  11. * @author sidneydekoning
  12. *
  13. * This class is instanciated like so:
  14. *
  15. * var _eq:Equalizer = new Equalizer( );
  16. * _eq.x = 200;
  17. * _eq.y = 200;
  18. * addChild( _eq );
  19. *
  20. * Any sound that you load in and play, the SoundMixer Class will respond to.
  21. * Dont forget to call _eq.update( ) every time you want the graphics to be updated
  22. * For instance on a timer or ENTER_FRAME.
  23. */
  24. public class Equalizer extends Sprite {
  25.  
  26. private var _bars:Sprite;
  27.  
  28.  
  29. public function Equalizer() {
  30.  
  31. _bars = new Sprite( );
  32. _bars.x = 0;
  33. _bars.y = 0;
  34. addChild( _bars );
  35.  
  36. drawScanLines( );
  37. }
  38.  
  39. private function drawScanLines():Shape {
  40.  
  41. var noOfLines:int = 12;
  42.  
  43. var line:Shape = new Shape( );
  44. var runningY:int = 0;
  45. for(var i:int = 0; i < noOfLines ; ++i) {
  46. line.graphics.lineStyle( 1, 0x000000, 1 );
  47. line.graphics.moveTo( 0, runningY );
  48. line.graphics.lineTo( 32, runningY );
  49. addChild( line );
  50. runningY += 2;
  51. }
  52. return line;
  53. }
  54.  
  55. public function updates():void {
  56. var spectrumData:ByteArray = new ByteArray( );
  57.  
  58. // FIXME: -- Workaround for Adobe Bug: computeSpectrum doen not only compute this instance of the flashplayer, but all instances. So if it fails, generate a random byteArray.
  59. try {
  60. SoundMixer.computeSpectrum(spectrumData, true, 2);
  61. }
  62. catch(e:Error) {
  63. spectrumData = new ByteArray();
  64. for(var j : uint = 0;j < 2048; j++) {
  65. spectrumData.position = j;
  66. spectrumData.writeFloat(MathUtils.random(-0.5, 0.5));
  67. }
  68. spectrumData.position = 0;
  69. }
  70.  
  71. _bars.graphics.clear( );
  72. _bars.graphics.beginFill( Colors.GREY );
  73.  
  74. for (var i:Number = 0; i < 32 ; i += 2) {
  75. var n:Number = spectrumData.readFloat( ) * 20;
  76. _bars.graphics.drawRect( i, 20, 2, -n );
  77. }
  78. }
  79. }
  80. }
  81.  
  82. //
  83. // Create a Class called MathUtils and place this function in there:
  84. static public function random(...args) : Number {
  85. var argLength : uint = args.length;
  86. switch(argLength) {
  87. case 0:
  88. // perform the standard Math.random
  89. return Math.random();
  90. break;
  91. case 2: // returns a random float between args[0] and args[1];
  92. case 3:
  93. // returns a random int between args[0] and args[1] if args[2] is set
  94. var returnValue : Number = (Math.random() * (Number(args[1]) - Number(args[0]))) + Number(args[0]);
  95. return (args[2] != undefined && Boolean(args[2])) ? int(returnValue) : returnValue;
  96. break;
  97. default:
  98. throw new IllegalOperationError("MathUtils.random takes either 0, 2 or 3 arguments");
  99. return undefined;
  100. break;
  101. }
  102. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: mikolo on August 3, 2009

Hey this is great...i searched for this snippet one day :-) thank you.

Posted By: mikolo on August 3, 2009

but the line in the MathUtils Class - throw new IllegalOperationError("MathUtils.random takes either 0, 2 or 3 arguments"); We need this class i think?

Posted By: mikolo on August 4, 2009

So i tested once more with 512 equalizer bars...if the random equalizer runs.... the result is not very nice.. it looks to simialary .. the bar heights are all evan long? Do you know a solution for this problem? I want a realy good fake equalizer...so nobody can see the difference! :-)

Posted By: mikolo on August 4, 2009

//Here is my script with the 512 bars

package { import MathUtils;

import flash.display.Shape;
import flash.display.Sprite;
import flash.media.SoundMixer;
import flash.utils.ByteArray;

import flash.events.*;

/**
 * @author sidneydekoning
 * 
 * This class is instanciated like so:
 * 
 * var _eq:Equalizer = new Equalizer( );
 * _eq.x = 200;
 * _eq.y = 200;
 * addChild( _eq );
 * 
 * Any sound that you load in and play, the SoundMixer Class will respond to.
 * Dont forget to call _eq.update( ) every time you want the graphics to be updated
 * For instance on a timer or ENTER_FRAME.
 */

public class Equalizer extends Sprite {

    private var _bars:Sprite;


    public function Equalizer() {

        _bars = new Sprite( );
        _bars.x = 0;
        _bars.y = 0;
        addChild( _bars );

        //drawScanLines( );

        this.addEventListener(Event.ENTER_FRAME, updates);
    }

    private function drawScanLines():Shape {

        var noOfLines:int = 12;

        var line:Shape = new Shape( );
        var runningY:int = 0;
        for(var i:int = 0; i < noOfLines ; ++i) {
            line.graphics.lineStyle( 1, 0x000000, 1 );
            line.graphics.moveTo( 0, runningY );
            line.graphics.lineTo( 32, runningY );
            addChild( line );
            runningY += 2;
        }
        return line;
    }

    public function updates(event:Event):void {
        var spectrumData:ByteArray = new ByteArray( );

        // FIXME: -- Workaround for Adobe Bug: computeSpectrum doen not only compute this instance of the flashplayer, but all instances. So if it fails, generate a random byteArray.
        try {
            SoundMixer.computeSpectrum(spectrumData, true, 2);
        }
        catch(e:Error) {
            spectrumData = new ByteArray();
            for(var j : uint = 0;j < 2048; j++) {
                spectrumData.position = j;
                spectrumData.writeFloat(MathUtils.random(-0.5, 1.5));
            }
            spectrumData.position = 0;  
        }

        _bars.graphics.clear( );
        _bars.graphics.beginFill(0x000000);

        for (var i:Number = 0; i < 512 ; i += 3) {
            var n:Number = spectrumData.readFloat( ) * 100;
            _bars.graphics.drawRect( i, 20, 2, -n );
            _bars.graphics.drawRect( i, 20, 2, +n );
        }
    }
}

}

//and the class MathUtils

package com.flashdo.flashblue {

public class MathUtils
{

    static public function random(...args) : Number {
        var argLength : uint = args.length;
        switch(argLength) {
            case 0:  
                // perform the standard Math.random
                return Math.random();
                break;
            case 2:  // returns a random float between args[0] and args[1];
            case 3:  
                // returns a random int between args[0] and args[1] if args[2] is set
                var returnValue : Number = (Math.random() * (Number(args[1]) - Number(args[0]))) + Number(args[0]);
                return (args[2] != undefined && Boolean(args[2])) ? int(returnValue) : returnValue;
                break;
            default:  
                //throw new IllegalOperationError("MathUtils.random takes either 0, 2 or 3 arguments");
                return undefined;
                break;
        }
    }

}

}

Posted By: sidneydekoning on July 14, 2010

Hi Mikolo,

Sorry about not commenting, i only saw your post now. But yeah, that is the correct MathUtils :)

Happy coding :)

Posted By: flashblue on March 9, 2011

Hi Mikolo,

It's not your code. It's my commercial item's code which is sold here:

http://www.flashcomponents.net/component/singlemp3player.html

Please, write your own code when you reply something.

You need to login to post a comment.