28
Apr

After a long time I had some time to experiment. I played with SoundMixer computeSpectrum. following is a very basic sound spectrum. click to play. it may will take some time to load the sound. enjoy!

Sound is downloaded from http://incompetech.com

here is the code.

Actionscript:
  1. package 
  2. {
  3.     import flash.display.Shape;
  4.     import flash.display.Sprite;
  5.     import flash.display.StageAlign;
  6.     import flash.display.StageScaleMode;
  7.     import flash.events.Event;
  8.     import flash.events.IOErrorEvent;
  9.     import flash.events.MouseEvent;
  10.     import flash.geom.ColorTransform;
  11.     import flash.media.Sound;
  12.     import flash.media.SoundChannel;
  13.     import flash.media.SoundLoaderContext;
  14.     import flash.media.SoundMixer;
  15.     import flash.net.URLRequest;
  16.     import flash.text.TextField;
  17.     import flash.text.TextFormat;
  18.     import flash.utils.ByteArray;
  19.     /**
  20.      * ...
  21.      * @author Ajay Chhaya
  22.      */
  23.     [SWF(width=512,height=200, backgroundColor="0x0", frameRate="32")]
  24.     public class SpectrumBar3 extends Sprite
  25.     {
  26.         private var txt:TextField = new TextField();
  27.         private var containerArray:Vector.<Bar> = new Vector.<Bar>;
  28.         private var snd:Sound;
  29.         private var bytes:ByteArray = new ByteArray();
  30.        
  31.         private var bars:int = 128;
  32.        
  33.         public function SpectrumBar3()
  34.         {
  35.             if (stage) init();
  36.             else addEventListener(Event.ADDED_TO_STAGE, init);
  37.            
  38.         }
  39.         private function init():void {
  40.             stage.scaleMode = StageScaleMode.NO_SCALE;
  41.             stage.align = "LT";
  42.             removeEventListener(Event.ADDED_TO_STAGE, init);
  43.             stage.addEventListener(MouseEvent.MOUSE_DOWN, prepareSound);
  44.             var tf:TextFormat = new TextFormat();
  45.             tf.font = "Arial";
  46.             txt.textColor = 0x888888;
  47.             tf.color = 0x888888;
  48.             txt.text = "Click to Start";
  49.             txt.setTextFormat(tf);
  50.             txt.x = txt.y = 10;
  51.             txt.selectable = false;
  52.             txt.width = stage.stageWidth;
  53.             addChild(txt);
  54.             rectDraw();
  55.         }
  56.         private function prepareSound(e:Event = null):void {
  57.            
  58.            
  59.             stage.removeEventListener(MouseEvent.MOUSE_DOWN, prepareSound);
  60.             //mp3 downloaded from incompetech.com
  61.             playSound('Pure Attitude.mp3');
  62.            
  63.            
  64.         }
  65.        
  66.         private function playSound(url:String):void
  67.         {
  68.             snd = new Sound();
  69.             var context:SoundLoaderContext = new SoundLoaderContext(0, true);
  70.             var sndReq:URLRequest = new URLRequest(url);
  71.            
  72.             snd.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
  73.             snd.addEventListener(Event.COMPLETE, completeHandler);
  74.            
  75.             txt.text = "Please wait for the file to be loaded.";
  76.            
  77.             snd.load(sndReq,context);
  78.            
  79.            
  80.         }
  81.         private function errorHandler(errorEvent:IOErrorEvent):void {
  82.             txt.text = "The sound could not be loaded: " + errorEvent.text;
  83.         }
  84.         private function completeHandler(event:Event):void {               
  85.             txt.text = "File is ready.";
  86.             addEventListener(Event.ENTER_FRAME, onEnter);
  87.             var sndCh:SoundChannel = new SoundChannel();
  88.             sndCh = snd.play();
  89.         }
  90.  
  91.        
  92.         private function onEnter(event:Event):void
  93.         {
  94.             SoundMixer.computeSpectrum(bytes, false, 1);
  95.             var sp:Bar;
  96.             var i:int;
  97.             for (i = 0; i <bars; i++) {
  98.                 sp = containerArray[i];
  99.                 var rl:Number = bytes.readFloat();
  100.                 sp.setLevel(rl);
  101.             }
  102.            
  103.         }
  104.  
  105.         private function rectDraw():void
  106.         {
  107.             var shape:Shape;
  108.             var nx:int, ny:int;
  109.             var startX:int = 0;
  110.             var startY:int = stage.stageHeight-50;
  111.             var w:int = 2;
  112.             for (var i:int = 0; i <bars; i++) {
  113.                 nx = i * (w+2);
  114.                 var b:Bar = new Bar();
  115.                 b.drawBar();
  116.                 b.x = nx + startX;
  117.                 b.y = startY;
  118.                 addChild(b);
  119.                 containerArray.push(b);
  120.             }
  121.             graphics.lineStyle(1, 0x333333, 1);
  122.             graphics.moveTo(startX, startY + w);
  123.             graphics.lineTo(nx + startX, startY + w);
  124.         }
  125.         public function rgbToHex(r:int,g:int,b:int):uint
  126.         {
  127.             //trace(r, g, b);
  128.             var hex:uint = (r <<16 | g <<8 | b);
  129.             return hex;
  130.         }
  131.        
  132.     }
  133.  
  134. }
  135.  
  136. import flash.display.Bitmap;
  137. import flash.display.BitmapData;
  138. import flash.display.Sprite;
  139. import flash.display.Shape;
  140. import com.ajaychhaya.utils.Util;
  141.  
  142. class Bar extends Sprite {
  143.     public static const NUM_RECT:uint = 20;
  144.     private var boxW:int = 2;
  145.     private var gap:int = 2;
  146.     private var _rects:Vector.<Box> = new Vector.<Box>;
  147.     private var currentLevel:int = 20;
  148.    
  149.     public function drawBar():void
  150.     {
  151.         var b:Box;
  152.         var nx:int, ny:int;
  153.         var startY:int = 0;
  154.        
  155.         var r:Number = Math.random() * 255;
  156.         var g:Number = Math.random() * 255;
  157.         var bb:Number = Math.random() * 255;
  158.        
  159.         var clr:uint = Util.rgbToHex(r, g, bb);
  160.  
  161.         for (var j:int = 0; j <NUM_RECT; j++) {
  162.             ny = j * (boxW + 2);
  163.             b = new Box(clr);
  164.             b.x = nx;
  165.             b.y = startY - ny;
  166.             b.cacheAsBitmap = true;
  167.             //b.visible = false;
  168.             addChild(b);
  169.             _rects.push(b);
  170.         }
  171.  
  172.     }
  173.     public function setLevel(level:Number):void {
  174.         level = level * NUM_RECT;
  175.         currentLevel = (currentLevel <level) ? level : currentLevel - 1;
  176.         var i:int;
  177.         for (i = 0; i<NUM_RECT; i++) {
  178.             _rects[i].visible = i <currentLevel;
  179.  
  180.         }
  181.        
  182.     }
  183. }
  184.  
  185.  
  186. class Box extends Shape {
  187.     private var boxW:int = 2;
  188.     public function Box(clr:uint)
  189.     {
  190.         graphics.lineStyle(0, 0, 0);
  191.         graphics.beginFill(clr);
  192.         graphics.drawRect(0, 0, boxW, boxW);
  193.         graphics.endFill();
  194.     }
  195. }

One Response to “Sound Spectrum”

  1. Sound Spectrum 3 Says:

    [...] variation of Sound Spectrum 1. Used bitmap data and applied some blur. [...]

Leave a Reply