Loading...

Audio Visualizer

Actionscript Audio Player & Visualizer
Actionscript: Audio Visualizer & Player

Yeah, I know it's a dead language, but it's close enough to JavaScript and C# someone might get a use from it. That and it was a lot of fun to make, so whatever, it's my website, back off honkey.

Some code I wrote a while ago for the media player/visualizer for Stan Bush’s Facebook App. I grew another grey hair just from typing that.

stanwidget.as

Honestly, this is 8 year old code who knows what it does.


package
{
    //     -- IMPORT
    // my scripts
    import com.drpunchlogic.*;
    // tweening
    import com.greensock.*;
    import com.greensock.easing.*;
    // -- flash imoports
    // display
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.Stage;
    // events
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    // media
    import flash.media.*;
    // net
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    // system
    import flash.system.Security;
    
    
    public class stanwidget extends MovieClip 
    {

        //         -- VARS
        // xml
        public var X:easyxml;
        // wildfire
        public var _wf:wildfire;
        // stage dimentions
        public var _sw:Number;
        public var _sh:Number;
        public var _swc:Number;
        public var _shc:Number;
        // player info
        public var _count:uint; // total number of items
        public var _now:uint; // current media item
        public var _old:uint; // previous media item
        // media info
        public var     _sound:Sound; // the sound item
        private var _scn:SoundChannel; // the sound channel
        // controller
        public var    _ctrl:ctrl_mc; // player controlls
        // visualizer
        public var _vis:audiovis; // audio visualizer
        
        // link to site
        public var _http:String = "http://widget.stanbush.com/mp3player/";
        
        
/*
--------------------------------------------------------
    SET IT UP
--------------------------------------------------------
*/
        public function stanwidget( ) 
        {    
            
            //Set up security 
            Security.allowDomain("widget.stanbush.com");
            Security.allowInsecureDomain("widget.stanbush.com");
            
            _sw = stage.stageWidth;
            _sh = stage.stageHeight;
            _swc = _sw / 2;
            _shc = _sh / 2;
            
            //X = new easyxml( _http+'music.xml', this );        
            X = new easyxml( _http+'music.xml', this );        
            _wf = new  wildfire( _http, '1109321', 'StanBush', 2, 2, _sw - 4, _sh - 4 );
            
            
            this.loaderInfo.addEventListener (ProgressEvent.PROGRESS, preload);
            
            init();
        }
        
/*
--------------------------------------------------------
    PRELOADER
--------------------------------------------------------
*/
        private function preload( e:ProgressEvent) : void
        {
            
            var pcent:Number = e.bytesLoaded/e.bytesTotal*100;
            
            //pre_bar.gotondtop( Math.floor( pcent/100 ) );
            pre_txt.text = "LOADING " + int(pcent) + "%";
            
            if(pcent==100)
            {
                /*
                pre_bar.visible = false;
                pre_bar.y = _sh + 1000;
                
                init();
                */
                pre_txt.visible = false;
                pre_txt.y = _sh + 1000;
            }
        }

/*
--------------------------------------------------------
    START CLASS
--------------------------------------------------------
*/
        // INIT        
        public function init() : void
        {
            addChild( X );
            // after xml loads start effects
            this.addEventListener( 'XMLFIN', startup );
        }
        
        // START UP
        private function startup( e:Event ) : void
        {
            /*
            trace( '---------------');
            trace( X._arr[0]._title );
            trace( X._arr[0]._artist );
            trace( X._arr[0]._album );
            trace( X._arr[0]._shop );
            trace( X._arr[0]._img );
            trace( X._arr[0]._vid );
            trace( X._arr[0]._link );
            trace( '---------------');
            */
            
            // add visualizer 
            _vis = new audiovis();
            _vis.y = 0;
            _vis.x = 0;
            _vis.name = 'vis';
            addChild( _vis );
            _vis.init( this );
            
            // add controller
            _ctrl = new ctrl_mc();
            _ctrl.name = 'ctrl_mc';
            _ctrl.y = _sh;
            _ctrl.x = _swc;
            addChild( _ctrl );
            _ctrl.init( this );
            
            // put wildfire on top
            addChild( _wf );
            
            // detect mouse leave events
            stage.addEventListener(Event.MOUSE_LEAVE, mouseLeft);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseBack);
        }
        
/*
--------------------------------------------------------
    DETECT MOUSE
--------------------------------------------------------
*/
        private function mouseLeft( e:Event ) : void
        {
            _ctrl.aniOut();
        }
        
        private function mouseBack( e:Event ) : void
        {
            _ctrl.aniIn();
        }
        
/*
    PLAY MEDIA
*/

        
        
//     -- END CLASS
    } 
//     -- END PACKAGE
}
audiovis.as

Oh man, this just keeps going. Maybe I'll find the actual source .FLA sometime.

Acutally, this block of code is a group of diferent visual options (AAA-DDD). It makes use of Flash's filters and a bunch of fun stuff. I loved experimenting in Flash. Back then, the internet was the Wild West.


package com.drpunchlogic
{
    // -- IMPORT
    // events
    import flash.events.*;
    // graphics
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.display.Graphics;
    import flash.display.BlendMode;
    import flash.display.Shape;
    import flash.display.DisplayObject;
    // media
    import flash.media.Sound;
    import flash.media.SoundLoaderContext;
    import flash.media.SoundMixer;
    // utils
    import flash.utils.ByteArray;
    // filters
    import flash.filters.BlurFilter;
    import flash.filters.BitmapFilterQuality;
    // tween
    import com.greensock.*;
    import com.greensock.easing.*;
    
    // loader
    import flash.net.URLRequest;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.ProgressEvent;


    public class audiovis extends MovieClip
    {

        // -- VARS
        // parenting
        private var _parent:Object;
        // graphics
        private var _poleW:Number = Math.round( _sw / _num );
        private var _ba:ByteArray;
        // sprites
        private var _fg:Sprite = new Sprite();  
        private var _bg:Sprite = new Sprite();  
        // scene items
        private var _scene:Array
        // stage dimentions
        private var _sw:Number;
        private var _sh:Number;
        private var _swc:Number;
        private var _shc:Number;
        // throw aways
        private var _num:Number; // just a number holder
        // which vis to play
        public var _visNow:Number = 0;
        public var _visNum:Number = 5; // <-- NUMBER OF VISUAL EFFECTS
        // effectx
        public var blur:BlurFilter ;
        public var blurNO:BlurFilter ;
        
        
        // external vis
        public var _xVis:MovieClip;
        

/*
--------------------------------------------------------
SET IT UP
--------------------------------------------------------
*/
        public function audiovis ()
        {
            trace( '[ VISUALIZER ]' );
                                
        }
        
        // kill the items
        public function killvis () : void
        {
            for( var i:uint = 0; i < _scene.length; i++ )
            {
                ( _scene[ i ].parent as MovieClip ).removeChild( _scene[ i ] );
            }
        }

/*
--------------------------------------------------------
START CLASS
--------------------------------------------------------
*/
        // INIT
        public function init ( holder:Object ):void
        {
            _parent = holder;
            
            _sw = _parent._sw;
            _sh = _parent._sh;
            _swc = _parent._swc;
            _shc = _parent._shc;
            
            _bg = new Sprite(); 
            _fg = new Sprite();  
                         
        }
        
        // VIS ON
        public function visON ( e:Event ) : void
        {                        
            _ba = new ByteArray();
            
            addChild( _bg );
            addChild( _fg );
            
            _scene = new Array( _bg,_fg );
            
            // load outside movie
            startLoad( _visNow );
            
            //this.addEventListener(Event.ENTER_FRAME, updateSpectrum);
        }
        
        // VIS OFF
        public function visOFF ( e:Event ) : void
        {
            killvis();
            
            this.removeEventListener(Event.ENTER_FRAME, updateSpectrum);
        }

/*
--------------------------------------------------------
    VIS CONTENT
--------------------------------------------------------
*/
    private function updateSpectrum(e:Event) : void
    {
        _xVis.spectrum();
    }
    /*
        var graphics:Graphics = this.graphics;
        _bg.graphics.clear();
        _fg.graphics.clear();
                        
        // array of effects
        switch ( _visNow )
        {
            case 1:
                _bg.x = _swc;
                _bg.y = _sh+20;
                 
                _fg.x = _swc;
                _fg.y = _sh+20;
                                
                blur = new BlurFilter( 4, 4, BitmapFilterQuality.LOW);
                _fg.filters = [blur];
                _bg.filters = [blur];
                _fg.blendMode = "invert";
                _bg.blendMode = "normal";
                _fg.rotationY = _bg.rotationY = 0;
                _fg.rotationX = _bg.rotationX = 0;
                _fg.rotation = _bg.rotation = 0;
                
                visB();
                break;
            case 2:
                
                _bg.x = _swc;
                _bg.y = _shc;
                 
                _fg.x = _swc;
                _fg.y = _shc;
                
                blur = new BlurFilter( 16, 24, BitmapFilterQuality.LOW);
                _fg.filters = [blur];
                _bg.filters = [blur];
                _fg.blendMode = "darken";
                _bg.blendMode = "add";
                _fg.rotationY = _bg.rotationY = 0;
                _fg.rotationX = _bg.rotationX = 0;
                
                visC();
                break;
            case 3:
                
                _bg.x = -20;
                _bg.y = _shc + 30;
                 
                _fg.x = -20;
                _fg.y = _shc + 26;
            
                blur = new BlurFilter( 0, 2, BitmapFilterQuality.LOW);
                _fg.filters = [blur];
                blur = new BlurFilter( 2, 4, BitmapFilterQuality.LOW);
                _bg.filters = [blur];
                _fg.blendMode = "normal";
                _bg.blendMode = "normal";
                _fg.rotationY = _bg.rotationY = 0;
                _fg.rotation = _bg.rotation = 0;
                visD();
                break;
            case 4:
                _bg.x = _swc;
                _bg.y = -50;
                 
                _fg.x = _swc;
                _fg.y = _sh + 50;
                
                blur = new BlurFilter( 100, 20, BitmapFilterQuality.HIGH);
                _fg.filters = [blur];
                _bg.filters = [blur];
                _fg.blendMode = "hardlight";
                _bg.blendMode = "normal";
                _fg.rotationY = _bg.rotationY = 0;
                _fg.rotationX = _bg.rotationX = 0;
                _fg.rotation = 0;
                _bg.rotation = 180;
                visE();
                break;
            default:
                _bg.x = -20;
                _bg.y = _sh;
                 
                _fg.x = -20;
                _fg.y = _sh;
                
                blur = new BlurFilter( 0, 4, BitmapFilterQuality.LOW);
                _fg.filters = [blur];
                _bg.filters = [blur];
                _fg.blendMode = "normal";
                _bg.blendMode = "normal";
                _fg.rotationY = _bg.rotationY = 0;
                _fg.rotationX = _bg.rotationX = 0;
                _fg.rotation = _bg.rotation = 0;
                visA();
                break;
            
        }
    }
    */
    
    public function onNext() : void
    {
        _visNow++;
        if (_visNow >= _visNum) _visNow = 0;
        
        this.updateSpectrum( new Event("fakeEvent") );
        trace('[ VIS : ' + _visNow + ']');
    }
    
    public function onPrev() : void
    {    
        _visNow--;
        if (_visNow < 0) _visNow = _visNum-1;
        
        this.updateSpectrum( new Event("fakeEvent") );
        trace('[ VIS : ' + _visNow + ']');
    }
        
/*
--------------------------------------------------------
    VIS AAA 
--------------------------------------------------------
*/
        public function visA () : void
        {
            
            SoundMixer.computeSpectrum( _ba, true, 0 );
            
            var stepping:int = 3;
            var sample:Number;
            var bgADJ:Number;
            var fgADJ:Number;
            
            var j:int = 0;

            for( j; j < 8; j++ )
            {
                _bg.graphics.lineStyle(2, 0x5811FF );
                _fg.graphics.lineStyle(1, 0xCCCCCC);
                var l:Number = (j*8)+8;
                var i:int;    // Left = 0-255, Right = 256-511
                for (i = 0; i < l; i += stepping)
                {
                    sample = _ba.readFloat();
                    bgADJ = sample * 200;
                    fgADJ = sample * 100;
                    _fg.graphics.drawRect((((i/2)+j)*5)+18, 0, 3, -fgADJ);
                    _bg.graphics.drawRect((((i/2)+j)*5)+18, 0, 3, -bgADJ);                    
                }
            }
        }

/*
--------------------------------------------------------
    VIS BBB 
--------------------------------------------------------
*/
        public function visB() : void
        {
            SoundMixer.computeSpectrum( _ba, true, 0 );
            
            var stepping:int = 1;
            var sample:Number;
            var bgADJ:Number;
            var fgADJ:Number;
            
            var j:int = 0;
            
            for( j; j < 8; j++ )
            {
                _bg.graphics.lineStyle(0.5, 0xcccccc );
                _fg.graphics.lineStyle(0.5, 0x999999);
                var l:Number = (j*2)+8;
                var i:int;    // Left = 0-255, Right = 256-511
                for (i = 0; i < l; i += stepping)
                {
                    sample = _ba.readFloat();
                    bgADJ = sample * 200;
                    fgADJ = sample * 100;
                    _fg.graphics.drawCircle( (( Math.random()*fgADJ)/10 ) - 5, -( ((i+j)*5)+18 -bgADJ ), ((i+j)*5)+5 -fgADJ);
                    _bg.graphics.drawCircle( (( Math.random()*bgADJ)/20 ) - 10,-( ((i+j)*5)+18 -bgADJ ), ((i+j)*5)+5 -bgADJ);
                    
                }
            }
        }

/*
--------------------------------------------------------
    VIS CCC
--------------------------------------------------------
*/
        public function visC() : void
        {
            SoundMixer.computeSpectrum( _ba, false, 0 );
            
            var stepping:int = 1;
            var sample:Number;
            var bgADJ:Number;
            var fgADJ:Number;
            
            var j:int = 0;
            
            for( j; j < 8; j++ )
            {
                
                var l:Number = (j*8)+4;
                var i:int;    // Left = 0-255, Right = 256-511
                for (i = 0; i < l; i += stepping)
                {
                    sample = _ba.readFloat();
                    bgADJ = sample * 200;
                    fgADJ = sample * 100;
                    _bg.x += -(bgADJ)/30;
                    _fg.x -= (fgADJ)/20;
                    _bg.graphics.lineStyle(0.5, 0x001199 + ( sample * 10000 ) );
                    _fg.graphics.lineStyle(0.5, 0x009911 + ( sample * 10000 ) );
                    _fg.graphics.drawRect( -(((i+j)*5) + fgADJ)/2, -(((i+j)*5) + fgADJ)/2, ((i+j)*5)+10 + fgADJ, ((i+j)*5)+10 + fgADJ);
                    _bg.graphics.drawRect( -(((i+j)*5) + fgADJ)/2, -(((i+j)*5) + fgADJ)/2, ((i+j)*5)+20 + bgADJ, ((i+j)*5)+20 + bgADJ);
                    _fg.rotation += 0.000015 * ( ((i+j)*5));
                    _bg.rotation -= 0.00002 * (((i+j)*5) );
                }
            }
        }
        
/*
--------------------------------------------------------
    VIS DDD
--------------------------------------------------------
*/
        public function visD() : void
        {
            SoundMixer.computeSpectrum( _ba, true, 0 );
            
            var stepping:int = 3;
            var sample:Number;
            var bgADJ:Number;
            var fgADJ:Number;
            
            var j:int = 0;

            for( j; j < 8; j++ )
            {
                _bg.graphics.lineStyle(1, 0x113333 + ( sample * 100000 ) );
                _fg.graphics.lineStyle(1, 0x667799 + ( sample * 200000 ) );
                var l:Number = (j*4)+8;
                var i:int;    // Left = 0-255, Right = 256-511
                for (i = 0; i < l; i += stepping)
                {
                    sample = _ba.readFloat();
                    bgADJ = sample * 100;
                    fgADJ = sample * 200;
                    _fg.graphics.drawRect(((i+j)*5)+18, 0, 2, +fgADJ);
                    _bg.graphics.drawRect(((i+j)*5)+18, 0, 2, -bgADJ);        
                    _fg.rotationX = ((i+j)*5) + fgADJ;
                    _bg.rotationX = ((i+j)*5) - bgADJ;
                }
            }
        }
/*
--------------------------------------------------------
    VIS EEE
--------------------------------------------------------
*/
        public function visE() : void
        {
            SoundMixer.computeSpectrum( _ba, false, 0 );
            
            var stepping:int = 1;
            var sample:Number;
            var bgADJ:Number;
            var fgADJ:Number;
            
                        
            var j:int = 0;

            for( j; j < 8; j++ )
            {
                _bg.graphics.lineStyle(1, 0x000000 + ( sample * 200000 ) );
                _fg.graphics.lineStyle(1, 0x333333 + ( sample * 200000 ) );
                var l:Number = (j*2)+8;
                var i:int;    // Left = 0-255, Right = 256-511
                for (i = 0; i < l; i += stepping)
                {
                    sample = _ba.readFloat();
                    bgADJ = sample * 100;
                    fgADJ = sample * 200;
                        
                    _fg.graphics.drawCircle( (( Math.random()*fgADJ)/20 ) - 10,-( ((i+j)*5)+18 -fgADJ ), ((i+j)*5)+fgADJ);
                    _bg.graphics.drawCircle( (( Math.random()*bgADJ)/20 ) - 10,-( ((i+j)*5)+18 -bgADJ ), ((i+j)*5)+5 -bgADJ);    
                                    
                }
            }
        }
        
/*

    VIS LOADER

*/
        public function startLoad( i:uint )
        {
            trace( _parent._http + _parent.X._vrr[i]._vis );
            var mLoader:Loader = new Loader();
            //var mRequest:URLRequest = new URLRequest( _parent._http + _parent.X._vrr[i]._vis );
            var mRequest:URLRequest = new URLRequest( _parent.X._vrr[i]._vis );
            mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
            mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
            mLoader.load(mRequest);
        }
        
        private function onCompleteHandler(loadEvent:Event)
        {
            _xVis = ( loadEvent.currentTarget.content as MovieClip );
            addChild( _xVis );
            
            this.addEventListener(Event.ENTER_FRAME, updateSpectrum);
        }
        private function onProgressHandler(mProgress:ProgressEvent)
        {
            var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
            trace(percent);
        }
        

/*
--------------------------------------------------------
    END -------------
--------------------------------------------------------
*/
        // -- END CLASS
    }
    // -- END PACKAGE
}x