forked from: forked from: -- --- ・-・ ・・・ ・

by WeiChih_Lin forked from forked from: -- --- ・-・ ・・・ ・ (diff: 73)
♥0 | Line 291 | Modified 2013-06-18 01:16:33 | MIT License
play

ActionScript3 source code

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

// forked from WeiChih_Lin's forked from: -- --- ・-・ ・・・ ・
// forked from yasnis's -- --- ・-・ ・・・ ・
package {
[SWF(backgroundColor=0x000000, frameRate=60)]
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.display.Bitmap;
import flash.display.BitmapData;
import com.flashdynamix.utils.SWFProfiler;
public class FlashTest extends Sprite {
private var background:String = "-- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  -- --- ・-・ ・・・ ・  -・-・ --- -・・ ・  ・--・ ・-・ ・ ・・・ ・・・  ・ -・ -・--  -・- ・ -・--  ";

private var count:int = 0;
        private var morse:Morse = new Morse();
        private var description_txt:TextField = new TextField();
        public function FlashTest() {
            addEventListener(Event.ADDED_TO_STAGE, initialize);
        }
        
        private function initialize(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, initialize);
            SWFProfiler.init(this);
            initializeText();
            addChild(morse);
            addChild(description_txt);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownHandler);
        }
        private function KeyDownHandler(e:KeyboardEvent):void {
            morse.send(e.keyCode, String.fromCharCode(e.charCode));
        }
        private function initializeText():void {
            var tf:TextFormat = new TextFormat();
            tf.color = 0xffffff;
            tf.align = TextFormatAlign.JUSTIFY;
            description_txt.defaultTextFormat = tf;
            description_txt.width = stage.stageWidth;
            description_txt.autoSize = TextFieldAutoSize.LEFT;
            description_txt.wordWrap = true;
            description_txt.selectable = false;
       
            description_txt.text = background;
            var bmpd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xff000000);
            var bmp:Bitmap = new Bitmap(bmpd);
            bmpd.draw(description_txt);
            addChild(bmp);
        }
    }
    
}


import flash.events.Event;
import flash.events.SampleDataEvent;
import flash.events.TimerEvent;
import flash.display.Sprite;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.utils.Timer;
class Morse extends Sprite {
    static public const CODE_SHORT:uint = 1;
    static public const CODE_LONG:uint = 3;
    static public const CODE_BREAK:uint = 7;
    static private const TIME_UNIT:uint = 80;
    private var code_gap_timer:Timer = new Timer(TIME_UNIT * 1, 1);
    private var char_gap_timer:Timer = new Timer(TIME_UNIT * 3, 1);
    private var word_gap_timer:Timer = new Timer(TIME_UNIT * 7, 1);
    private var short_timer:Timer = new Timer(TIME_UNIT * 1, 1);
    private var long_timer:Timer = new Timer(TIME_UNIT * 3, 1);
    private var sound:Sound = new Sound();
    private var channel:SoundChannel;
    private var trans:SoundTransform;
    private var playlist:Vector.<Array> = new Vector.<Array>();
    private var charlist:Vector.<String> = new Vector.<String>();
    private var char_txt:TextField = new TextField();
    function Morse () {
        addEventListener(Event.ADDED_TO_STAGE, initializeHandler);
    }
    private function initializeHandler(e:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, initializeHandler);
        //
        sound.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
        channel = sound.play();
        trans = channel.soundTransform;
        trans.volume = 0;
        channel.soundTransform = trans;
        addChild(char_txt);
        initializeText();
        initializeTimer();
        x = stage.stageWidth / 2;
        y = stage.stageHeight / 2;
    }
    private function initializeText():void {
        var tf:TextFormat = new TextFormat();
        tf.color = 0xffffff;
        tf.align = TextFormatAlign.CENTER;
        tf.size = 100;
        char_txt.defaultTextFormat = tf;
        char_txt.selectable = false;
        //char_txt.visible = false;
        char_txt.scaleX = char_txt.scaleY = 2;
        char_txt.x = -char_txt.width / 2;
        char_txt.y = -char_txt.height / 2;
    }
    private function initializeTimer():void{
        code_gap_timer.addEventListener(TimerEvent.TIMER, timerHandler);
        char_gap_timer.addEventListener(TimerEvent.TIMER, timerHandler);
        word_gap_timer.addEventListener(TimerEvent.TIMER, timerHandler);
        short_timer.addEventListener(TimerEvent.TIMER, timerHandler);
        long_timer.addEventListener(TimerEvent.TIMER, timerHandler);
    }

    private function timerHandler(e:TimerEvent):void {
        trans.volume = 0;
        channel.soundTransform = trans;
        switch (e.target) {
            case code_gap_timer:
            case char_gap_timer:
            case word_gap_timer:
                send();
                break;
            default:
                if (currentCode && currentCode.length < 1) {
                    currentCode = null;
                    char_gap_timer.start();
                }else {
                    code_gap_timer.start();
                }
                break;
        }
    }
    private var currentCode:Array;
    public function send(keycode:uint = 0, char:String = null):void {
        if (keycode) {
            var a:Array = MorseCode.getCode(keycode);
            if (a) {
            playlist.push(a);
            if (char == "") char = (keycode-96) + "";
                charlist.push(char);
                if (currentCode) return;
            }else {
                return;
            }
        }
        if (currentCode && currentCode.length == 0) currentCode = null;
        if (!currentCode) {
            if (playlist.length < 1) return;
            currentCode = playlist.shift();
            char_txt.text = charlist.shift().toUpperCase();
        }
        var code:uint = currentCode.shift();
        var t:Timer;
        switch (code) {
            case CODE_SHORT:
                trans.volume = 1;
                t = short_timer;
                break;
            case CODE_LONG:
                trans.volume = 1;
                t = long_timer;
                break;
            case CODE_BREAK:
                trans.volume = 0;
                t = word_gap_timer;
                break;
        }
        channel.soundTransform = trans;
        t.start();
    }
    private function onSampleData(e:SampleDataEvent):void{
        for ( var c:int=0; c<8192; c++ ) {
            e.data.writeFloat(Math.sin((Number(c+e.position)/Math.PI/2))*0.25);
            e.data.writeFloat(Math.sin((Number(c+e.position)/Math.PI/2))*0.25);
        }
    }
}
class MorseCode {
    static public function getCode(keycode:uint):Array {
        var code:Array;
        if (keycode >= codelist.length) return code;
        if (codelist[keycode].length > 0) code = codelist[keycode].concat();
        return code;
    }
    static private const codelist:/*Array*/Array = [
        [],        //0:
        [],        //1:
        [],        //2:
        [],        //3:
        [],        //4:
        [],        //5:
        [],        //6:
        [],        //7:
        [],        //8:
        [],        //9:
        [],        //10:
        [],        //11:
        [],        //12:
        [],        //13:
        [],        //14:
        [],        //15:
        [],        //16:
        [],        //17:
        [],        //18:
        [],        //19:
        [],        //20:
        [],        //21:
        [],        //22:
        [],        //23:
        [],        //24:
        [],        //25:
        [],        //26:
        [],        //27:
        [],        //28:
        [],        //29:
        [],        //30:
        [],        //31:
        [7],    //32:
        [],        //33:
        [],        //34:
        [],        //35:
        [],        //36:
        [],        //37:
        [],        //38:
        [],        //39:
        [],        //40:
        [],        //41:
        [],        //42:
        [],        //43:
        [],        //44:
        [],        //45:
        [],        //46:
        [],        //47:
        [3,3,3,3,3],        //48:0
        [1,3,3,3,3],        //49:1
        [1,1,3,3,3],        //50:2
        [1,1,1,3,3],        //51:3
        [1,1,1,1,3],        //52:4
        [1,1,1,1,1],        //53:5
        [3,1,1,1,1],        //54:6
        [3,3,1,1,1],        //55:7
        [3,3,3,1,1],        //56:8
        [3,3,3,3,1],        //57:9
        [],        //58:
        [],        //59:
        [],        //60:
        [],        //61:
        [],        //62:
        [],        //63:
        [],        //64:
        [1,3],            //65:a
        [3,1,1,1],        //66:b
        [3,1,3,1],        //67:c
        [3,1,1],            //68:d
        [1],                //69:e
        [1,1,3,1],        //70:f
        [3,3,1],            //71:g
        [1,1,1,1],        //72:h
        [1,1],            //73:i
        [1,3,3,3],        //74:j
        [3,1,3],            //75:k
        [1,3,1,1],        //76:l
        [3,3],            //77:m
        [3,1],            //78:n
        [3,3,3],            //79:o
        [1,3,3,1],        //80:p
        [3,3,1,3],        //81:q
        [1,3,1],            //82:r
        [1,1,1],            //83:s
        [3],                //84:t
        [1,1,3],            //85:u
        [1,1,1,3],        //86:v
        [1,3,3],            //87:w
        [3,1,1,3],        //88:x
        [3,1,3,3],        //89:y
        [3,3,1,1],        //90:z
        [],        //91:
        [],        //92:
        [],        //93:
        [],        //94:
        [],        //95:
        [3,3,3,3,3],        //96:0
        [1,3,3,3,3],        //97:1
        [1,1,3,3,3],        //98:2
        [1,1,1,3,3],        //99:3
        [1,1,1,1,3],        //100:4
        [1,1,1,1,1],        //101:5
        [3,1,1,1,1],        //102:6
        [3,3,1,1,1],        //103:7
        [3,3,3,1,1],        //104:8
        [3,3,3,3,1],        //105:9
    ];
}

Forked