/ Published in: ActionScript 3
Expand |
Embed | Plain Text
package nl.funkymonkey.drawing { import nl.funkymonkey.ui.Colors; import nl.funkymonkey.utils.MathUtils; import flash.display.Shape; import flash.display.Sprite; import flash.media.SoundMixer; import flash.utils.ByteArray; /** * @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( ); } 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():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, 0.5)); } spectrumData.position = 0; } _bars.graphics.clear( ); _bars.graphics.beginFill( Colors.GREY ); for (var i:Number = 0; i < 32 ; i += 2) { var n:Number = spectrumData.readFloat( ) * 20; _bars.graphics.drawRect( i, 20, 2, -n ); } } } } // // Create a Class called MathUtils and place this function in there: 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; } }
Comments
Subscribe to comments
You need to login to post a comment.

Hey this is great...i searched for this snippet one day :-) thank you.
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?
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! :-)
//Here is my script with the 512 bars
package { import MathUtils;
}
//and the class MathUtils
package com.flashdo.flashblue {
}
Hi Mikolo,
Sorry about not commenting, i only saw your post now. But yeah, that is the correct MathUtils :)
Happy coding :)
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.