SiON Test 2

by shohei909 forked from SiON Test (diff: 255)
※イヤホン推奨
くるくる回ります。目をつむるとわかりやすいかもしれません。
♥0 | Line 116 | Modified 2010-10-09 22:23:01 | MIT License
play

ActionScript3 source code

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

// forked from shohei909's SiON Test

package {
    import flash.events.Event;
    import flash.display.Sprite;
    import org.si.sion.events.*;
    import org.si.sound.events.*;
import gs.*;
import gs.easing.*;
    [SWF(height="465",width="465",backgroundColor="0x080808")]
    public class FlashTest extends Sprite {
        static public const WIDTH:int = 465, HEIGHT:int = 465;
        private var time:int = 15;
        function FlashTest (){
            //
            addChild(new BackMap(WIDTH,HEIGHT));
            
            //
            var v:SoundVisualiser = new SoundVisualiser();
            v.x = 10; v.y = 355; 
            addChild(v);
            SiON.driver.pan = -1.0;
            toRight()
        }
        
        private function toRight():void{
           SiON.driver.bpm = 1200/time;
           time--;
           if(time<3){time = 3}
           TweenLite.to(SiON.driver, time, { pan:1.0, ease:Sine.easeInOut, onComplete:toLeft } );
        } 
        private function toLeft():void{
           SiON.driver.bpm = 1200/time;
           time--;
           if(time<3){time = 3}
           TweenLite.to(SiON.driver, time, { pan:-1.0, ease:Sine.easeInOut, onComplete:toRight  } );
        } 
    }
}
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.display.BitmapData;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.display.Bitmap;
import flash.events.Event;

//現在のサウンドの波形を表示
class SoundVisualiser extends Bitmap{
    private var _byte:ByteArray = new ByteArray();
    private var black:BitmapData = new BitmapData(450,100,false,0);
    private var filter:BlurFilter = new BlurFilter(2,2);
    private const CHANNEL_LENGTH:int = 256;
    private const SPAN:int = 4;
    function SoundVisualiser(){
        this.addEventListener( "exitFrame", onFrame );
        super( new BitmapData(445, 100, false, 0 ) ) 
    }
    
    private function onFrame(e:Event):void{
        SoundMixer.computeSpectrum(_byte,true,2);
        var b:BitmapData = bitmapData;
        b.lock()
        b.merge( black, b.rect, new Point, 100,100,100,10 );
        b.applyFilter( b, b.rect, new Point, filter );
        
        var sx:int = 0;
        var len:int = (CHANNEL_LENGTH / SPAN); 
        var sp:int = ( 256 / len ) >> 1;
        var rRect:Rectangle = new Rectangle(445 - sp , 100 - sx , sp - 1,10);  
        var lRect:Rectangle = new Rectangle(sp, 100 - sp ,sp-1,10);  
        rRect.bottom = (465 - sx);
        
        for (var i:int = 0; i < len; i++) 
        {
            var l:Number = 0;
            var r:Number = 0;
            
            for(var j:int = 0; j < SPAN; j++ ){
                _byte.position = (i * SPAN + j) * 4;
                l +=  _byte.readFloat();
                _byte.position = (i * SPAN + j) * 4  + 1024;
                r +=  _byte.readFloat();
            }
            l /= SPAN; r /= SPAN;
            
            rRect.height = r * 100;
            rRect.x -= sp;    
            rRect.y = (100 - sx) - rRect.height;
            
            lRect.height = l * 100;
            lRect.y = (100 - sx) - lRect.height;
            
            b.fillRect( rRect, 0x006666 );
            b.fillRect( lRect, 0x006666 );
            lRect.x += sp;    
        }
        
        b.unlock();
    }
}
class BackMap extends Bitmap {
    function BackMap(w:int,h:int){
        super( new BitmapData(w,h,false,0) );
        for(var i:int = 0; i<w; i++ ){ 
            for(var j:int = 0; j<h; j++ ){ 
                bitmapData.setPixel(i,j,(i%2==0&&j%2==0)?0x151515:0x080808)
            }
        }
    }
}


//ライブラリ
//SiON-音声全般 http://www.libspark.org/wiki/keim/SiON
//サンプルコード:http://wonderfl.net/search?q=org.si.sion
import org.si.sion.*;
import org.si.sound.*;
class MyDriver extends SiONDriver {
    public var dm:DrumMachine = new DrumMachine(2, 2, 2, 3, 3, 3);
    public var fill:SiONData;
    function MyDriver():void{
        super();
        volume = 1;

        dm.volume = 1;
        pan = -1;
        setVoice(0, new SiONVoice(5,2,63,63,-10,0,2,20));
        play(); 
        dm.play();
        dm.fadeIn(4);
    }
}
class SiON{
    //SiONDriverは一つしか作れないので、staticで作る。
    static public var driver:MyDriver = new MyDriver();
    static public function se(i:int,delay:int=0):void{ }
}