In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: soundtest11

リサイズ対応
stageクリックでFFTModeの変更
してみました
Get Adobe Flash player
by gaina 02 Sep 2010
/**
 * Copyright gaina ( http://wonderfl.net/user/gaina )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/pWJ5
 */

// forked from gaina's soundtest11
package 
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.media.Sound;
    import flash.media.SoundLoaderContext;
    import flash.media.SoundMixer;
    import flash.media.SoundTransform;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
    
    /**
     * ...
     * @author nagai
     * sound by AlainMikuni
     * 
     */
    
    [SWF(width="465",height="465",frameRate=30)]
    public class Main extends Sprite 
    {
        private const ROW:int = 16;
        private const COL:int = 16;
        private const RADIUS:int = 5;
        
        private var original_rasio:Number = 0;
        private var rasio:Number = 0;
        
        private var FFTMode:Boolean = false;
        
        private var w:Number;
        private var h:Number;
        
        private var sound:Sound;
        private var obj_array_l:Array = [];
        private var obj_array_r:Array = [];
        private var position_array_l:Array = [];
        
        public function Main():void 
        {
            Wonderfl.capture_delay(10);
            
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            w = stage.stageWidth;
            h = stage.stageHeight;
            
            original_rasio = w / 16;
            
            PlaySound("http://www.takasumi-nagai.com/soundfiles/sound004.mp3");
        }
        
        private function PlaySound(url:String):void
        {
            sound = new Sound();
            var _context:SoundLoaderContext = new SoundLoaderContext(1000, true);
            sound.addEventListener(Event.COMPLETE, SoundLoadeComplete);
            sound.load(new URLRequest(url), _context);
        }
        
        private function SoundLoadeComplete(e:Event):void 
        {
            obj_array_l = Draw();
            obj_array_r = Draw();
            position_array_l = PointPosition(obj_array_l);
            
            addEventListener(Event.ENTER_FRAME, loop);
            sound.play(0, 10, new SoundTransform(0.7, 0));
            
            stage.addEventListener(Event.RESIZE, ResizeEventHandler);
            stage.addEventListener(MouseEvent.MOUSE_UP, FFTModeChanger);
        }
        
        private function loop(e:Event):void 
        {
            var _byte:ByteArray = new ByteArray();
            SoundMixer.computeSpectrum(_byte, FFTMode);
            for (var n:int = 0; n < 2; n++)
            {
                for (var i:int = 0; i < 256; i++)
                {
                    var data:Number = _byte.readFloat();
                    var p:Number = data * 2;
                    var _point:Point = position_array_l[i] as Point;

                    if (n==0)
                    {
                        var _sp:Sprite = obj_array_l[i] as Sprite;
                        
                        if (FFTMode)
                        {
                            _sp.x += p;
                            _sp.y += p;
                            
                            if (data == 0)
                            {
                                _sp.x += (_point.x - _sp.x) / 10;
                                _sp.y += (_point.y - _sp.y) / 10;
                                
                                _sp.scaleX += (1 - _sp.scaleX) / 10;
                                _sp.scaleY += (1 - _sp.scaleY) / 10;
                            }
                        }
                        else
                        {
                            _sp.scaleX += data;
                            _sp.scaleY += data;
                            
                            _sp.x += (_point.x - _sp.x) / 10;
                            _sp.y += (_point.y - _sp.y) / 10;
                            
                            if (data < 0.2 && data > -0.2)
                            {
                                _sp.scaleX += (1 - _sp.scaleX) / 10;
                                _sp.scaleY += (1 - _sp.scaleY) / 10;
                            }
                        }
                    }
                    else if (n==1)
                    {
                        var _sp_r:Sprite = obj_array_r[i] as Sprite;
                       
                        if (FFTMode)
                        {
                            _sp_r.scaleX += p;
                            _sp_r.scaleY += p;
                            
                            _sp_r.x += (_point.x - _sp_r.x) / 15;
                            _sp_r.y += (_point.y - _sp_r.y) / 15;
                            
                            if (data <0.2)
                            {
                                _sp_r.scaleX += (1 - _sp_r.scaleX) / 10;
                                _sp_r.scaleY += (1 - _sp_r.scaleY) / 10;
                            }
                        }
                        else
                        {
                            _sp_r.scaleX += data * rasio;
                            _sp_r.scaleY += data * rasio;
                            
                            _sp_r.x += (_point.x - _sp_r.x) / 15;
                            _sp_r.y += (_point.y - _sp_r.y) / 15;
                            
                            if (data <0.2)
                            {
                                _sp_r.scaleX += (1 - _sp_r.scaleX) / 5;
                                _sp_r.scaleY += (1 - _sp_r.scaleY) / 5;
                            }
                        }
                    }
                }
            }
        }
        
        private function FFTModeChanger(e:MouseEvent):void 
        {
            if (FFTMode)
            {
                FFTMode = false;
            }
            else
            {
                FFTMode = true;
            }
        }
        
        private function ResizeEventHandler(e:Event):void 
        {
            w = stage.stageWidth;
            h = stage.stageHeight;
            
            rasio = (w / 16) / original_rasio;
            
            for (var i:int = 0; i < position_array_l.length; i++)
            {
                var _point:Point = position_array_l[i] as Point;
                _point.x = Math.floor((w / 16 / 2) + w / 16 * (i % COL));
                _point.y = Math.floor((h / 16 / 2) + h / 16 * (Math.floor(i / ROW)));
            }
        }
        
        private function PointPosition(array:Array):Array
        {
            var _array:Array = [];
            for (var i:int = 0; i < array.length; i++)
            {
                var _obj:Object = array[i] as Object;
                var _point:Point = new Point(_obj.x, _obj.y);
                _array.push(_point);
            }
            return _array;
        }
        
        private function Draw():Array
        {
            var _array:Array = [];
            
            for (var i:int = 0; i < ROW ; i++)
            {
                for(var j:int = 0; j < COL; j++)
                {
                    var _sp:Sprite = new Sprite();
                    _sp.graphics.beginFill(0);
                    _sp.graphics.drawCircle(0, 0, RADIUS);
                    _sp.graphics.endFill();
                    _sp.x = 15 + j * (w - 15) / COL;
                    _sp.y = 15 + i * (h - 15) / ROW;
                    _sp.blendMode = "invert";
                    addChild(_sp);
                    _array.push(_sp);
                }
            }
            return _array;
        }
        
    }
    
}