topical media & game development

talk show tell print

student-mma-16-FrequencySpectrum.mx

student-mma-16-FrequencySpectrum.mx [swf] flex


  <?xml version="1.0" encoding="utf-8"?>
  <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" creationComplete="init()">
          <mx:Script>
                  <![CDATA[
                          import mx.controls.Alert;
                          import flash.media.SoundMixer;
                          
                          private var barWidth:int;
                          private var ba:ByteArray;
                          private var gr:Shape;
                          
                          private function init():void {
                                  //16 bars
                                  barWidth = this.width / 16;
                                  ba = new ByteArray();
                                  gr = new Shape();
                                  gr.x = 0;
                                  gr.y = 0;
                                  this.rawChildren.addChild(gr);
                                  
                                  var timer:Timer = new Timer(50, 0);
                                  timer.addEventListener(TimerEvent.TIMER, update);
                                  timer.start();
                          }
                          
                          private function update(e:TimerEvent):void {
                                  SoundMixer.computeSpectrum(ba, true);
                              var i:int;
                              gr.graphics.clear();
                              gr.graphics.beginFill(0x0000FF);
                              var w:uint = 32;
                              var c:uint = 0;
                              
                              for (i=0; i<512; i+=w) {
                                  var t:Number = ba.readFloat();
                                  var n:Number = Math.floor(t * this.height * 0.9);
                                  gr.graphics.drawRect(c * barWidth, this.height, barWidth - 2, -n);
                                  c++;
                              }
                                  
                          }
                          
                  ]]>
          </mx:Script>
  </mx:Canvas>
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.