topical media & game development

talk show tell print

mashup-rmx-20-Visualizer.ax

mashup-rmx-20-Visualizer.ax (swf ) [ flash ] flex


  package com.almerblank.media{
          import mx.core.Application;
          import flash.media.Sound;
          import flash.media.SoundChannel;
          import flash.media.SoundMixer;
          import flash.utils.ByteArray;
          import flash.net.URLRequest;
          import flash.events.*;
          import flash.filters.*;
          import mx.events.*;
          import components.Equalizer;
  
          public class Visualizer extends Application{
                  [Inspectable]
                  private var _mp3:String;
                  
                  public function get file():String{
                          return _mp3;
                  }
  
                  public function set file(str:String):void{
                          _mp3 = str;
                  }
                  
                  private var _sound:Sound;
                  private var _channel:SoundChannel;
                  
                  private var _leftStereoBarX:int = 131;
                  private var _rightStereoBarX:int = 140;
                  
                  public var eq:Equalizer;
                  
                  public function Visualizer(){
                          super();
                          this.setStyle("backgroundGradientColors", [0x0033cc, 0x191970]);
                          addEventListener(FlexEvent.CREATION_COMPLETE, _creationCompleteHandler);
                  }
                  
                  private function _creationCompleteHandler(event:FlexEvent):void{
                          addEventListener(Event.ENTER_FRAME, _onEnterFrame, false, 0, true);
                          _initEQ();
                          _sound = new Sound(new URLRequest(_mp3));
                          _channel = _sound.play();
                  }
                  
                  private function _initEQ():void{
                          var eqX:uint;
                          var a:uint;
                          
                          //draw background lines for the eq
                          eq.bands.graphics.lineStyle(.1, 0xcccccc, .1);
                          
                          for(var i:uint = 2; i < 26; i++){
                                  eqX = i*5;
                                  for(a = 0; a < 40; a+=2){
                                          eq.bands.graphics.moveTo(eqX, -a);
                                          eq.bands.graphics.lineTo(eqX+4, -a);
                                  }
                          }
                          
                          //draw the background lines for the left/right peaks
                          for(a = 0; a < 60; a+=2){
                                  eq.bands.graphics.moveTo(_leftStereoBarX, -a);
                                  eq.bands.graphics.lineTo(_leftStereoBarX+8, -a);
                          }
                          
                          for(a = 0; a < 60; a+=2){
                                  eq.bands.graphics.moveTo(_rightStereoBarX, -a);
                                  eq.bands.graphics.lineTo(_rightStereoBarX+8, -a);
                          }
                  }
                  
                  private function _onEnterFrame(event:Event):void{
                          _drawEQ();
                  }
                   
                  private function _drawEQ():void{
                           var singleBand:Number = 0;
                          
                          //create the byte array
                          var eqBytes:ByteArray = new ByteArray();
                          
                          //fill it with data
                          SoundMixer.computeSpectrum(eqBytes);
                          
                          //clear the graphics object every trip
                          eq.bands.graphics.clear();
                          
                          eq.bands.graphics.beginFill(0xff6600, .1);
                          
                          //create the left channel visualization
                          for(var i:uint = 0; i < 256; i++){
                                  singleBand += eqBytes.readFloat()*2;
                                  
                                  if(i%10==0 && i > 0 && i != 10){
                                          for(var a:uint = 0; a < singleBand; a++){
                                                  eq.bands.graphics.beginFill(0xff6600, a/4);
                                                  eq.bands.graphics.drawRect(i/2, -(a*2), 4, .2);
                                          }
                                          singleBand = 0;
                                  }
                                  
                                  if(i == 10){
                                          singleBand = 0;
                                  }
                          }
                          
                          //draw the stereo bars
                          for(a = 0; a < _channel.leftPeak*20; a++){
                                  eq.bands.graphics.beginFill(0x66CC00, a/10);
                                  eq.bands.graphics.drawRect(_leftStereoBarX, -(a*2), 8, .5);
                          }
                          
                          for(a = 0; a < _channel.rightPeak*20; a++){
                                  eq.bands.graphics.beginFill(0x66CC00, a/10);
                                  eq.bands.graphics.drawRect(_rightStereoBarX, -(a*2), 8, .5);
                          }                                
                          eq.bands.graphics.endFill();
                   }
                  
          }
  }


(C) Æliens 18/6/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.