Creating an Equalizer that responds to the SoundMixer


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  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

You need to login to post a comment.