forked from: soundtest18

by aobyrne forked from soundtest18 (diff: 238)
♥0 | Line 302 | Modified 2012-05-11 23:37:13 | 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/e4lk
 */

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    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 gaina
     */
    
    [SWF(width=465,height=465,backgroundColor=0)]
    public class Soundtest18 extends Sprite 
    {
        private var _sound:Sound;
        
        public function Soundtest18():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
            
            graphics.beginFill(0);
            graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
            graphics.endFill();
            
            var _context:SoundLoaderContext  = new SoundLoaderContext(1000, true);
            _sound = new Sound();
            _sound.load(new URLRequest("http://www.takasumi-nagai.com/soundfiles/sound006.mp3"), _context);
            _sound.addEventListener(Event.COMPLETE, SoundComplete);
            setCharts();
        }
        
        private function SoundComplete(e:Event):void 
        {
            _sound.play(0, 100, new SoundTransform(0.8));
            addEventListener(Event.ENTER_FRAME, loop);
        }
        
        private var _array:Array = [];
        private var charts:Vector.<SCLineChart>;
        private var charts2:Vector.<SCLineChart>;
        
        private function loop(e:Event):void
        {
            var _bytes:ByteArray = new ByteArray();
            SoundMixer.computeSpectrum(_bytes, false, 1);
            var _scale:Number = 0;
            var pcount:int = 0;
            for (var i:int = 0; i < 512; i++) {
                var _x:Number = stage.stageWidth / 512 * i;
                var _read:Number = _bytes.readFloat();
                if (_read > 0.5) {
                    pcount++;
                    var p:Particles = new Particles();
                    p.x = _x;
                    stage.addChild(p);
                    _array.push(p);
                    _scale += _read
                }
            }
            
            _scale = Math.max(1, _scale / 3);
            charts[0].addToArray(_scale);
            charts2[0].addToArray(pcount);
            if (_array.length > 0) {
                for (var j:int = 0; j < _array.length; j++) {
                    var _p:Particles = _array[j] as Particles;
                    _p.step();
                    
                    //_p.scaleY = _p.scaleX = _scale;
                    _p.scaleY = _scale;
                    
                    if (_p.y > stage.stageHeight - 10) {
                        _p.eraze();
                        _p = null;
                        stage.removeChild(_array[j] as Particles);
                        _array.splice(j, 1);
                        j--;
                    }
                }
            }
        }
        
        private function setCharts():void 
        {
            var yyy:Number = 0;
            var followerModel:FollowerModel = new FollowerModel;
            followerModel.sprite = this;
            followerModel.name = 'scale';
            followerModel.yy = yyy;
            followerModel.referencesy = [0];
            followerModel.amount  1;
            followerModel.autoScale = false;
            followerModel.minimum = -1;
            followerModel.maximum = 10;
            charts = SCLineChart.follower(followerModel);
            var followerModel2:FollowerModel = new FollowerModel;
            followerModel2.sprite = this;
            followerModel2.name = 'p count';
            followerModel2.amount  1;
            followerModel2.autoScale = false;
            followerModel2.maximum = 100;
            followerModel2.xx = 200;
            followerModel2.yy = yyy;
            charts2 = SCLineChart.follower(followerModel2);
        }
    }   
}

import com.bit101.components.Label;
import com.bit101.components.VBox;
import flash.display.Sprite;
class Particles extends Sprite
{
    public var vx:Number = 1;
    public var vy:Number = 1;
    public function Particles(color:uint=0xFFFFFF)
    {
        //graphics.lineStyle(1, 0, 1);
        graphics.beginFill(color, 1);
        //graphics.drawCircle(0, 0, 2);
        graphics.drawRect( -1, -1, 2, 2);
        graphics.endFill();
    }
    
    public function step():void 
    {
        this.y +=  vy;
        vy += 0.5;
        if (vy % 10 ==0) {
            vy = 1;
        }
    }
    
    public function eraze():void
    {
        graphics.clear();
    }
}
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;
    private var _autoScale:Boolean;
    
    public function FollowerModel() 
    {
        super();
        xx = 0;
        yy = 0;
        name = 'defalut name';
        referencesx = [];
        referencesy = [];
        maximum = 1;
        minimum = 0;
        amount = 1;
        autoScale = true;
    }
    
    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;
    }
    
    public function get autoScale():Boolean 
    {
        return _autoScale;
    }
    
    public function set autoScale(value:Boolean):void 
    {
        _autoScale = 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;
    private var model:FollowerModel;
        public function SCLineChart(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, data:Array=null,model:FollowerModel=null)
        {
            super(parent, xpos, ypos, data);
            this.model = model;
            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 (model) 
            {
                var rr:Array = model.referencesy;    
                var rl:uint = rr.length;
                for (var i:int = 0; i < rl; i++) 
                {
                    var referenceY:Number = model.referencesy[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>
        public static function follower(modelm:FollowerModel):Vector.<SCLineChart>
        {
            var sCLineChart:SCLineChart;
            var charts:Vector.<SCLineChart> = Vector.<SCLineChart>([]);
            var vBox:VBox = new VBox(modelm.sprite,modelm.xx,modelm.yy);
            new Label(vBox, 0, 0, modelm.name);
            for (var i:int = 0; i < modelm.amount; i++) 
            {
                sCLineChart = new SCLineChart(vBox, 0, 0, [],modelm);
                charts[i] = sCLineChart ;
                sCLineChart.setSize(sCLineChart.width, 50);
                sCLineChart.autoScale = modelm.autoScale;
                sCLineChart.maximum = modelm.maximum;
                sCLineChart.minimum = modelm.minimum;
                
            }
            return charts;
        }
}