forked from: soundtest16

by aobyrne forked from soundtest16 (diff: 267)
...
@author gaina
♥0 | Line 385 | Modified 2012-05-19 03:48:47 | MIT License
play

ActionScript3 source code

/**
 * Copyright aobyrne ( http://wonderfl.net/user/aobyrne )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/AP31
 */

package 
{
    import caurina.transitions.properties.ColorShortcuts;
    import caurina.transitions.Tweener;
    import com.bit101.components.CheckBox;
    import com.bit101.components.Label;
    import com.bit101.components.Panel;
    import com.bit101.components.Style;
    import com.bit101.components.VBox;
    import flash.display.BlendMode;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.media.SoundMixer;
    import flash.utils.ByteArray;
    
    
    /**
     * ...
     * @author gaina
     */
    [SWF(width="465",height="465",frameRate="30")]
    public class Soundtest16 extends Sprite 
    {
        private var QTY:int = 8;
        
        private var _array:Array;
        private var _sp:Sprite;
        private var _sp_switch:Boolean = true;
        private var _line:Array;
        private var _line_switch:Boolean = true;
        private var _blend_swicth:Boolean = true;
        private var qFollower:Vector.<SCLineChart>;
        private var pFollower:Vector.<SCLineChart>;
        private var pTweenerThreshold:Number;
        private var pVisibleThreshold:Number;
        private var panel:Panel;
        private var pSwitchBlendThreshold:Number;
        private var hasSwitchedBlend:Boolean;
        
        public function Soundtest16():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            ColorShortcuts.init();
            //Style.setStyle(Style.DARK);
            _line = LineCircles();
            _array = Circles();
            
            var _sound:PlaySound = new PlaySound("http://www.takasumi-nagai.com/soundfiles/sound007.mp3");
            //var _sound:PlaySound = new PlaySound("assets/realentada.mp3");
            
            _sp = new Sprite();
            _sp.graphics.beginFill(0xFF0000);
            _sp.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
            _sp.graphics.endFill();
            addChild(_sp);
            _sp.blendMode = "invert";
            _sp.addEventListener(MouseEvent.CLICK, BlendSwitch);
            panel = new Panel(this, 11, 1);
            panel.setSize(430, 460);
            panel.alpha = 0.86;
            qFollower = follower(10, 0, 2, 'scale, q', [0,0.5]);
            pVisibleThreshold = 3;
            pTweenerThreshold = 3.5;
            pSwitchBlendThreshold = 5;
            pFollower = follower(215,0,8, 'switch (visible/tween), p',[pVisibleThreshold,pTweenerThreshold,pSwitchBlendThreshold]);
            addEventListener(Event.ENTER_FRAME, loop);
            new CheckBox(this, 0, 0, '', doPanelVisibility);
            new CheckBox(this, 0, 12, '', doTrace);
            doPanelVisibility(null);
        }
        
        private function doTrace(e:Event):void 
        {
            trace( "pFollower[0].data : " + pFollower[0].data );
        }
        
        private function doPanelVisibility(e:Event):void 
        {
            panel.visible = !panel.visible;
        }
        
        private function loop(e:Event):void 
        {
            var _byte:ByteArray = new ByteArray();
            SoundMixer.computeSpectrum(_byte, true, 0);
            var qf:Vector.<SCLineChart> =          qFollower;
            var pf:Vector.<SCLineChart> =          pFollower;
            for (var i:int = 0; i < QTY; i++) {
                _byte.position = i * 64;
                var p:Number = 0;
                var q:Number = 0;
                for (var j:int = 0; j < 64; j++) {
                    p += _byte.readFloat();
                    if (i == 0) {
                        q = Math.max(0.3, p / 14);
                    }else {
                        q = Math.max(0.3, p / 4);
                    }
                    if (i == 6 && p > pTweenerThreshold) {
                        Switch();
                    }
                    if (i == 7 && p > pVisibleThreshold) {
                        VisibleSwicth();
                    }
                    if (i == 1 && p > pSwitchBlendThreshold) {
                        BlendSwitch(null);
                    }
                }
                qFollower[i].addToArray(q)
                pFollower[i].addToArray(i==1||i==6||i==7?p:0);
                //pFollower[i].addToArray(p);
                Tweener.addTween(_array[i] as Sprite, { scaleX:q, scaleY:q, time:0.5 } );
                Tweener.addTween(_line[i] as Sprite, { scaleX:q, scaleY:q, time:0.5 } );
            }
            //trace( "_sp.blendMode : " + _sp.blendMode );
            //trace( "_sp_switch : " + _sp_switch );
            //trace( "_line_switch : " + _line_switch );
            pFollower[0].addToArray(_sp_switch?5:0);
            pFollower[2].addToArray(_sp.blendMode==BlendMode.INVERT?5:0);
            pFollower[3].addToArray(_line_switch?5:0);
        }
        
        private function Switch():void
        {
            if (_sp_switch) {
                Tweener.addTween(_sp, {_color:0x000000 } );
                _sp_switch = false;
            } else {
                Tweener.addTween(_sp, {_color:Math.random()*0xFFFFFF } );
                _sp_switch = true;
            }
        }
        
        private function VisibleSwicth():void
        {
            if (_line_switch) {
                for (var i:int = 0; i < QTY; i++) {
                    _array[i].visible = false;
                    _line[i].visible = true;
                }
                _line_switch = false;
            } else {
                for (var j:int = 0; j < QTY; j++) {
                    _array[j].visible = true;
                    _line[j].visible = false;
                }
                _line_switch = true;
            }
        }
        
        private function BlendSwitch(e:MouseEvent):void 
        {
            if (_blend_swicth) {
                _sp.blendMode = "screen";
                _blend_swicth = false;
            } else {
                _sp.blendMode = "invert";
                _blend_swicth = true;
            }
        }
        
        
        private function Circles():Array
        {
            var _arr:Array = [];
            for (var i:int = 0; i < QTY; i++)
            {
                var _radius:Number = 465 - 465 / QTY * i;
                var _circle:Sprite = new Sprite();
                _circle.graphics.beginFill(0);
                _circle.graphics.lineStyle(2, 0xFFFFFF);
                _circle.graphics.drawCircle(0, 0, _radius);
                _circle.graphics.endFill();
                _circle.x = stage.stageWidth / 2;
                _circle.y = stage.stageHeight / 2;
                addChild(_circle);
                _arr.push(_circle);
            }
            return _arr;
        }
        
        private function LineCircles():Array
        {
            var _arr:Array = [];
            for (var i:int = 0; i < QTY; i++)
            {
                var _radius:Number = 465 - 465 / QTY * i;
                var _circle:Sprite = new Sprite();
                _circle.graphics.lineStyle(2, 0);
                _circle.graphics.drawCircle(0, 0, _radius);
                _circle.graphics.endFill();
                _circle.x = stage.stageWidth / 2;
                _circle.y = stage.stageHeight / 2;
                addChild(_circle);
                _arr.push(_circle);
            }
            return _arr;
        }
        
        private function follower(xx:Number, yy:Number, maximum:Number, namef:String, references:Array=null):Vector.<SCLineChart>
        {
            
            var sCLineChart:SCLineChart;
            var vBox:VBox = new VBox(panel, xx, yy);
            vBox.alpha = 0.87;
            new Label(vBox, 0, 0, namef);
            var charts:Vector.<SCLineChart> = Vector.<SCLineChart>([]);
            for (var i:int = 0; i < QTY; i++) 
            {
                sCLineChart = new SCLineChart(vBox, 0, 0, [],references);
                charts[i] = sCLineChart ;
                sCLineChart.setSize(sCLineChart.width, 50);
                sCLineChart.autoScale = false;
                sCLineChart.maximum = maximum;
                sCLineChart.minimum = -1;
                
            }
            return charts;
        }
        
    }
    
}
import com.bit101.components.Label;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.media.SoundTransform;
import flash.net.URLRequest;

class PlaySound
{
    private var sound:Sound;

        public function PlaySound(url:String)
        {
            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 
        {
            sound.play(0, 10, new SoundTransform(0.3, 0));
        }
}
import flash.display.Sprite;
class FollowerModel
{
    private var _xx:Number;
    private var _yy:Number;
    private var _name:String;
    private var _referencesx:Array;
    private var _referencesy:Array;
    private var _maximum:Number;
    private var _minimum:Number;
    private var _sprite:Sprite;
    private var _amount:int;
    
    public function FollowerModel() 
    {
        super();
        xx = 0;
        yy = 0;
        name = '';
        referencesx = [];
        referencesy = [];
        maximum = 100;
        minimum = 0;
        sprite = new Sprite
        amount = 0;
    }
    
    public function get xx():Number 
    {
        return _xx;
    }
    
    public function set xx(value:Number):void 
    {
        _xx = value;
    }
    
    public function get yy():Number 
    {
        return _yy;
    }
    
    public function set yy(value:Number):void 
    {
        _yy = value;
    }
    
    public function get name():String 
    {
        return _name;
    }
    
    public function set name(value:String):void 
    {
        _name = value;
    }
    
    public function get referencesx():Array 
    {
        return _referencesx;
    }
    
    public function set referencesx(value:Array):void 
    {
        _referencesx = value;
    }
    
    public function get referencesy():Array 
    {
        return _referencesy;
    }
    
    public function set referencesy(value:Array):void 
    {
        _referencesy = value;
    }
    
    public function get maximum():Number 
    {
        return _maximum;
    }
    
    public function set maximum(value:Number):void 
    {
        _maximum = value;
    }
    
    public function get minimum():Number 
    {
        return _minimum;
    }
    
    public function set minimum(value:Number):void 
    {
        _minimum = value;
    }
    
    public function get sprite():Sprite 
    {
        return _sprite;
    }
    
    public function set sprite(value:Sprite):void 
    {
        _sprite = value;
    }
    
    public function get amount():int 
    {
        return _amount;
    }
    
    public function set amount(value:int):void 
    {
        _amount = value;
    }
}

import com.bit101.charts.LineChart;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
class SCLineChart extends LineChart
{
    private var array1:Array;
    private var referenceYs:Array;
        public function SCLineChart(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, data:Array=null,referenceYs:Array=null)
        {
            super(parent, xpos, ypos, data);
            this.referenceYs = referenceYs;
            array1 = [];
        }
        public function addToArray(value:Number,index:int=-1):void 
        {
            if (index==-1) 
            {
                array1[array1.length]=value
            }
            else
            {
                array1[index] = value;
                
            }
            if (array1.length>100) 
            {
                array1.shift();
            }
            data = array1;
            
        }
        override protected function drawChart():void 
        {
            super.drawChart();
            if (referenceYs) 
            {
                var rr:Array = referenceYs;    
                var rl:uint = rr.length;
                for (var i:int = 0; i < rl; i++) 
                {
                var referenceY:Number = referenceYs[i];    
                var border:Number = 2;
                var chartHeight:Number = _height - border;
                var max:Number = getMaxValue();
                var min:Number = getMinValue();
                var scale:Number = chartHeight / (max - min);
                _chartHolder.graphics.moveTo(border, -scale * referenceY);
                _chartHolder.graphics.lineTo(_width, -scale * referenceY);
                }
            }
            
        }
        //private function follower(vBox:Sprite, QTY:int, xx:Number, yy:Number, maximum:Number, namef:String, references:Array = null):Vector.<SCLineChart>
        private function follower(followerModel:FollowerModel):Vector.<SCLineChart>
        {
            followerModel = new FollowerModel;
            var sCLineChart:SCLineChart;
            new Label(followerModel.sprite, 0, 0, followerModel.name);
            var charts:Vector.<SCLineChart> = Vector.<SCLineChart>([]);
            for (var i:int = 0; i < followerModel.amount; i++) 
            {
                sCLineChart = new SCLineChart(followerModel.sprite, 0, 0, [],followerModel.referencesx);
                charts[i] = sCLineChart ;
                sCLineChart.setSize(sCLineChart.width, 50);
                sCLineChart.autoScale = false;
                sCLineChart.maximum = followerModel.maximum;
                sCLineChart.minimum = followerModel.minimum;
                
            }
            return charts;
        }
}