Return to Snippet

Revision: 15770
at July 16, 2009 10:32 by sidneydekoning


Updated Code
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;
			}
		}

Revision: 15769
at July 16, 2009 10:22 by sidneydekoning


Updated Code
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
	 * 
	 * Make class more generic, pass through width height and amount of kHz
	 * Also make it able to have different views (bars, circles etc)
	 */
	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
			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;
			}
		}

Revision: 15768
at July 16, 2009 10:12 by sidneydekoning


Initial Code
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
	 * 
	 * Make class more generic, pass through width height and amount of kHz
	 * Also make it able to have different views (bars, circles etc)
	 */
	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
			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(Math.random() * 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 );
			}
		}
	}
}

Initial URL

                                

Initial Description

                                

Initial Title
Creating an Equalizer that responds to the SoundMixer

Initial Tags

                                

Initial Language
ActionScript 3