28
Apr

Another variation of Sound Spectrum.

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=400,height=400, backgroundColor="0x0", frameRate="32")]
  24.     public class SpectrumBar4 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 = 64;
  32.        
  33.         public function SpectrumBar4()
  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.             var sndCh:SoundChannel = new SoundChannel();
  80.             sndCh = snd.play();
  81.            
  82.             addEventListener(Event.ENTER_FRAME, onEnter);
  83.            
  84.         }
  85.         private function errorHandler(errorEvent:IOErrorEvent):void {
  86.             txt.text = "The sound could not be loaded: " + errorEvent.text;
  87.         }
  88.         private function completeHandler(event:Event):void {               
  89.             txt.text = "";
  90.            
  91.         }
  92.         private function onEnter(event:Event):void
  93.         {
  94.             SoundMixer.computeSpectrum(bytes, false, 3);
  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 = stage.stageWidth/2;
  110.             var startY:int = stage.stageHeight/2;
  111.             var w:int = 2;
  112.             var rot:Number = 360 / bars;
  113.             for (var i:int = 0; i <bars; i++) {
  114.                 nx = i * (w+2);
  115.                 var b:Bar = new Bar();
  116.                 b.drawBar();
  117.                 b.x = startX;
  118.                 b.y = startY;
  119.                 b.rotation = rot*i;
  120.                 addChild(b);
  121.                 containerArray.push(b);
  122.             }
  123.         }
  124.         public function rgbToHex(r:int,g:int,b:int):uint
  125.         {
  126.             var hex:uint = (r <<16 | g <<8 | b);
  127.             return hex;
  128.         }
  129.        
  130.         private function onPlaybackComplete(event:Event):void
  131.         {
  132.             removeEventListener(Event.ENTER_FRAME, onEnter);
  133.         }
  134.        
  135.     }
  136.  
  137. }
  138.  
  139. import flash.display.Bitmap;
  140. import flash.display.BitmapData;
  141. import flash.display.Sprite;
  142. import flash.display.Shape;
  143. import com.ajaychhaya.utils.Util;
  144.  
  145. class Bar extends Sprite {
  146.     public static const NUM_RECT:uint = 20;
  147.     private var boxW:int = 2;
  148.     private var boxH:int = 6;
  149.     private var gap:int = 2;
  150.     private var _rects:Vector.<Box> = new Vector.<Box>;
  151.     private var currentLevel:int = NUM_RECT;
  152.    
  153.     public function drawBar():void
  154.     {
  155.         var b:Box;
  156.         var nx:int, ny:int;
  157.         var startY:int = 0;
  158.        
  159.         var r:Number = Math.random() * 255;
  160.         var g:Number = Math.random() * 255;
  161.         var bb:Number = Math.random() * 255;
  162.        
  163.         var clr:uint = Util.rgbToHex(r, g, bb);
  164.  
  165.         for (var j:int = 0; j <NUM_RECT; j++) {
  166.             ny = j * (boxH + 4);
  167.             b = new Box(clr);
  168.             b.x = nx;
  169.             b.y = startY + ny;
  170.             b.cacheAsBitmap = true;
  171.             //b.visible = false;
  172.             addChild(b);
  173.             _rects.push(b);
  174.         }
  175.  
  176.     }
  177.     public function setLevel(level:Number):void {
  178.         //level = Math.abs(level * NUM_RECT);
  179.         level = level * NUM_RECT;
  180.         currentLevel = (currentLevel <level) ? level : currentLevel - 1;
  181.         var i:int;
  182.         for (i = 0; i<NUM_RECT; i++) {
  183.             _rects[i].visible = i <currentLevel;
  184.  
  185.         }
  186.        
  187.     }
  188. }
  189.  
  190.  
  191. class Box extends Shape {
  192.     private var boxW:int = 2;
  193.     private var boxH:int = 6;
  194.     public function Box(clr:uint)
  195.     {
  196.         graphics.lineStyle(0, 0, 0);
  197.         graphics.beginFill(clr);
  198.         graphics.drawRect(0, 0, boxW, boxH);
  199.         //graphics.drawCircle(0, 0, boxW);
  200.         graphics.endFill();
  201.     }
  202. }

Leave a Reply