topical media & game development

talk show tell print

#graphic-flex-image-effects-11-Flex-SoundSpectrum.ax

#graphic-flex-image-effects-11-Flex-SoundSpectrum.ax [swf] [flash] flex


  package {
  
          import flash.display.Sprite;
          import flash.events.Event;
          import flash.utils.ByteArray;
  
          [SWF(width=400, height=300, backgroundColor=0x000000)]
  
          
Demonstrates how to visualize a playing sound file's data transformed to a frequency spectrum as a vertical column chart of 512 values.

  
          public class @ax-graphic-flex-image-effects-11-Flex-SoundSpectrum extends Sprite {
  
                  // the dimensions of the visualization
                  private const WAVE_WIDTH:uint = 300;
                  private const WAVE_HEIGHT:uint = 200;
                  // the color of the columns
                  private const WAVE_COLOR:uint = 0xFFFFFF;
  
                  private var _soundController:graphic_flex_image_effects_11_Flex_SoundController;
  
                  
Constructor. Loads the sound file using SoundController.

  
                  public function @ax-graphic-flex-image-effects-11-Flex-SoundSpectrum() {
                          _soundController = new graphic_flex_image_effects_11_Flex_SoundController("graphic-flex-image-effects-11-assets-IrishRock.mp3");
                          _soundController.addEventListener(Event.CHANGE, onSoundChange);
                  }
  
                  
Updates the column chart sound visualization with the current sound data.

  
                  private function updateGraph():void {
                          // returns the sound data of the sound playing, transformed to a frequency spectrum
                          var spectrumData:ByteArray = _soundController.getSoundSpectrum();
                          graphics.clear();
                          graphics.beginFill(WAVE_COLOR);
                          var ratio:Number = WAVE_WIDTH/512;
                          // how columns will be scaled horizontally to fit within the desired dimensions
                          var x:Number = (stage.stageWidth-WAVE_WIDTH)/2;
                          var y:Number = stage.stageHeight*4/5;
                          var value:Number;
                          var position:uint;
                          var i:int = -1;
                          while (++i < 512) {
                                  // the value is scaled vertically to fit within the desired dimensions
                                  value = Math.ceil(WAVE_HEIGHT*spectrumData.readFloat());
                                  // this results in columns being interspersed, with left and right channel data
                                  // at each frequency being next to each other, as opposed to having all left channel
                                  // data on one side and all right channel data on the other
                                  if (i < 256) {
                                          position = i*2;
                                  } else {
                                          position = (i%256)*2+1;
                                  }
                                  // draw the column in a negative direction so that is drawn "up" on the y axis
                                  graphics.drawRect(x+position*ratio, y, 1, -value);
                          }
                  }
  
                  
Handler for when the sound changes (basically, an ENTER_FRAME while the sound is playing). This calls updateGraph() to redraw column chart.
parameter: event Event dispatched by SoundController.

  
                  private function onSoundChange(event:Event):void {
                          updateGraph();
                  }
  
          }
  
  }
  


(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.