Main.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:comp="com.borealkiss.controls.*"
    creationComplete="init(event)"
    backgroundColor="#101010" viewSourceURL="srcview/index.html">
    
    <mx:HBox 
        width="100%" 
        horizontalAlign="left">
        <mx:Button label="Load MP3" click="onClick(event)"/>
        <comp:AmountSlider target="{_filter}"/>
        <comp:RotationSlider target="{_filter}"/>
    </mx:HBox>
    
    <mx:VBox 
        width="100%" 
        height="100%" 
        horizontalAlign="center" 
        verticalAlign="middle">
        <mx:Canvas width="700" height="500" id="_canvas"/>
    </mx:VBox>
    
    <!--
    <mx:Style source="PlainSkin/css/plain.css"/>
    -->

    <mx:Script>
        <![CDATA[
            /**
             * Requires the following libraries, MP3FileReferenceLoader and RadialBlurFilter
             * @see http://www.flexiblefactory.co.uk/flexible/?p=46
             * @see http://www.libspark.org/browser/as3/Astro/RadialBlur
             */             
            import org.audiofx.mp3.MP3FileReferenceLoader;
            import org.audiofx.mp3.MP3SoundEvent;
            import RadialBlurFilter;
        
            private const REF_RADIUS:Number = 150;
            private var _channel:SoundChannel;
            private var _mp3loader:MP3FileReferenceLoader;
            private var _fr:FileReference;
            
            //The BitmapData will be drawn on the canvas rather than Shape object itself.
            private var _cache:Shape;
            private var _bmpd:BitmapData;
            
            []
            private var _filter:RadialBlurFilter;
            
            private function init(e:Event):void{                
                _mp3loader = new MP3FileReferenceLoader();
                _mp3loader.addEventListener(MP3SoundEvent.COMPLETE,onComplete);
                _fr = new FileReference();
                _fr.addEventListener(Event.SELECT,onSelect);
                
                _cache = new Shape();
                _bmpd = new BitmapData(_canvas.width,_canvas.height,true,0x00000000);
                _canvas.rawChildren.addChild(new Bitmap(_bmpd));
                
                _filter = new RadialBlurFilter(_canvas.width/2,_canvas.height/2,60);
                _canvas.filters = [_filter];
            }
            
            private function onClick(e:MouseEvent):void{
                _fr.browse([new FileFilter("mp3 files","*.mp3")]);
            }
        
            private function onSelect(e:Event):void{
                if (_channel != null){
                    _channel.stop();
                }
                _mp3loader.getSound(_fr);
            }
            
            private function onComplete(e:MP3SoundEvent):void{
                _channel = e.sound.play(0,999);
                this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
            }
            
            private function onEnterFrame(e:Event):void{
                var g:Graphics = _cache.graphics;
                g.clear();
                _bmpd.fillRect(_bmpd.rect,0x00000000);
                
                var ba:ByteArray = new ByteArray();
                SoundMixer.computeSpectrum(ba);
                
                var hemisphere:Array = new Array();
                var otherHemisphere:Array = new Array();
                
                for (var i:int=0; i<=0xFF; i++){
                    var theta:Number = Math.PI*i/0xFF;
                    var rad:Number = ba.readFloat()*REF_RADIUS;
                    
                    //0<theta<PI
                    var x1:Number = _canvas.width/2 + (REF_RADIUS + rad)*Math.cos(theta);
                    var y1:Number = _canvas.height/2 + (REF_RADIUS + rad)*Math.sin(theta);
                    hemisphere.push(new Point(x1,y1));
                    
                    //-PI<theta<0
                    var x2:Number = _canvas.width/2 + (REF_RADIUS + rad)*Math.cos(-theta);
                    var y2:Number = _canvas.height/2 + (REF_RADIUS + rad)*Math.sin(-theta);
                    otherHemisphere.push(new Point(x2,y2));                    
                }
                
                draw(g,hemisphere,0x005885);
                draw(g,otherHemisphere,0x005885);
                _bmpd.draw(_cache);
                
                chaseFilterCenter();
            }
            
            private function draw(target:Graphics,points:Array,color:uint=0xFFFFFF):void{
                target.beginFill(color);
                for (var i:int=0; i<points.length; i++){
                    var p:Point = points[i];
                    if (i==0){
                        target.moveTo(p.x,p.y);
                    }
                    else{
                        target.lineTo(p.x,p.y);
                    }
                }
                target.endFill();
            }
            
            private function chaseFilterCenter():void{
                _filter.centerX = _canvas.mouseX;
                _filter.centerY = _canvas.mouseY;
                _canvas.filters = [_filter];
            }
        ]]>
    </mx:Script>
</mx:Application>

RotationSlider.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:HSlider xmlns:mx="http://www.adobe.com/2006/mxml"
    width="200"
    buttonMode="true"
    labels="Rotation" 
    minimum="0" 
    maximum="90"
    tickInterval="10"
    value="0"
    change="onChange(event)">
    
    <mx:Script>
        <![CDATA[
            import RadialBlurFilter;
            private var _target:RadialBlurFilter;
            
            public function set target(target:RadialBlurFilter):void{
                _target = target;
            }
        
            private function onChange(e:Event):void{
                _target.rotation = e.target.value;
            }
        ]]>
    </mx:Script>
</mx:HSlider>

AmountSlider.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:HSlider xmlns:mx="http://www.adobe.com/2006/mxml"
    width="200"
    buttonMode="true"
    labels="Amount" 
    minimum="1" 
    maximum="100"
    tickInterval="10"
    value="60"
    change="onChange(event)">
    
    <mx:Script>
        <![CDATA[
            import RadialBlurFilter;
            private var _target:RadialBlurFilter;
            
            public function set target(target:RadialBlurFilter):void{
                _target = target;
            }
        
            private function onChange(e:Event):void{
                _target.amount = e.target.value;
            }
        ]]>
    </mx:Script>
</mx:HSlider>
Powered by blog Boreal Kiss 2008.