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

Dynamic Soundでキーボード

// forked from 024t910's Dynamic Sound Generationで蛙の歌
// write as3 code here..
package
{
    import flash.events.EventDispatcher;
    import flash.events.Event;
    import flash.media.Sound;
    import flash.events.SampleDataEvent;
    import flash.media.*;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.display.SimpleButton;

    public class KeyBordSimulator extends Sprite
    {
        //初期パラメータを設定
        private var freq:Number = 261;
        private var rate:Number = 44100;
        private var baseRate:Number = 44100;
        private var noise:Number = 0;

        // 各コードの比率を設定
        private static const C:Number = 1;
        private static const D:Number = 9 / 8;
        private static const E:Number = 5 / 4;
        private static const F:Number = 4 / 3;
        private static const G:Number = 3 / 2;
        private static const A:Number = 5 / 3;
        private static const B:Number = 15 / 8;

        private var _toneRange:Array = [ C, D, E, F, G, A, B ];

        //音声の再生を700ms秒に設定
        private var timer:Timer = new Timer(500,0);

        //簡易楽譜
        private var rateArray:Array = new Array();

        //再生用Sound系クラス
        private var sound:Sound = new Sound();
        private var soundChannelObject:SoundChannel = new SoundChannel();

        //Key Bord
        private var _keyBord:KeyBord;

        public function KeyBordSimulator()
        {
            _keyBord = new KeyBord(_toneRange);
            _keyBord.x = 50;
            _keyBord.y = 50;
            addChild(_keyBord);
            rate = baseRate/_keyBord.rate.shift();
            timer.addEventListener (TimerEvent.TIMER, onTimer);
            timer.start ();
            sound.addEventListener (SampleDataEvent.SAMPLE_DATA, onSamplesCallback);
            soundChannelObject = sound.play();
        }

        private function onTimer (e:TimerEvent):void
        {                
            if (!_keyBord.rate.length)
            {
                rate = baseRate/0;
            }
            else
            {
                rate = baseRate/_keyBord.rate.shift();
            }
        }
	
        private function onSamplesCallback (e:SampleDataEvent) :void
        {
            for(var i:int=0; i<2048; i++)
            {
                noise += (freq) / rate;
                //左音声の波形データの生成
                var samples:Number = noise * Math.PI*2;
                e.data.writeFloat(Math.sin(samples));
                //右音声の波形データの生成
                e.data.writeFloat(Math.sin(samples));
            }
        }
		
    }

}

import flash.display.Sprite;
import flash.display.Stage;
import flash.utils.describeType;

class KeyBord extends Sprite
{
    private static const KEYBLACKWIDTH:Number = 30;
    private static const KEYBLACKHEIGHT:Number = 100;
    private static const KEYWHITEWIDTH:Number = 50;
    private static const KEYWHITEHEIGHT:Number = 150;
    private var _rateArray:Array = [0];

    public function KeyBord(range:Array):void
    {
        super();
        var bordX:int;
        var bordY:int = 0;
        for(var i:int=0; i < range.length; i++)
        {
            var key:Key = new Key(KEYWHITEWIDTH, KEYWHITEHEIGHT, range[i], 0xFFFFFF);
            key.x = bordX;
            key.y = bordY;
            key.addEventListener(KeyEvent.BEAT, onClick);
            addChild(key);
            //これは駄目だろ。
            switch(i)
            {
                case 1:
                case 2:
                case 4:
                case 5:
                case 6:
                var blackKey:Key = new Key(KEYBLACKWIDTH, KEYBLACKHEIGHT, range[i], 0x000000);
                blackKey.x = bordX - KEYBLACKWIDTH/2;
                blackKey.y = bordY;
                addChild(blackKey);
                break;
            }
            bordX += KEYWHITEWIDTH;
        }
    }

    private function onClick (e:KeyEvent):void
    {
        _rateArray = [e.tone];
    }

    public function get rate():Array
    {
        return _rateArray;
    }
}

import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.EventDispatcher;

class Key extends SimpleButton
{
    private var _tone:Number;
    private var _width:int;
    private var _height:int;

    public function Key(width:int, height:int, tone:Number, color:Number):void
    {
        super();
        _width = width;
        _height = height;
        this.overState = drawBox(color);
        this.upState = drawBox(color);
        this.downState = drawBox(color);
        this.hitTestState = this.upState;

        _tone = tone;
 
        this.addEventListener(MouseEvent.CLICK, beatKey);
    }

    public function beatKey(e:MouseEvent):void
    {
        var event:KeyEvent = new KeyEvent(KeyEvent.BEAT);
        event.tone = _tone;
        dispatchEvent(event);
    }

    public function drawBox(color:uint):Sprite
    {
        var sprite:Sprite = new Sprite();
        sprite.graphics.lineStyle(4, 0x000000);
        sprite.graphics.beginFill(color);
        sprite.graphics.drawRect(0,0,_width,_height);
        return sprite;
    }
}

import flash.events.Event;

class KeyEvent extends Event
{
    private var _tone:Number;
    public static const BEAT:String = "key_beat_event";

    public function KeyEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false):void
    {
        super(type, bubbles, cancelable);
    }
    
    public function get tone():Number
    {
        return this._tone;
    }
    
    public function set tone(tone:Number):void
    {
        this._tone = tone;
    }
}