morse

var cnt, code, chk, tmrID;
var tempo = new Array(100, 70, 50);
var sign = {
"A" : ".-", "B" : "-...", "C" : "-.-.", "D" : "-..", "E" : ".",
"F" : "..-.", "G" : "--.", "H" : "....", "I" : "..", "J" : ".---",
"K" : "-.-", "L" : ".-..", "M" : "--.", "N" : "-.", "O" : "---",
"P" : ".--.", "Q" : "--.-", "R" : ".-.", "S" : "...", "T" : "-",
"U" : "..-", "V" : "...-", "W" : ".--", "X" : "-..-", "Y" : "-.--",
"Z" : "--..",
"1" : ".----", "2" : "..---", "3" : "...--", "4" : "....-", "5" : ".....",
"6" : "----.", "7" : "---..", "8" : "--...", "9" : "-....", "0" : "-----",
"," : "--..--", "." : ".-.-.-", "-" : "-....-", "?" : "..--..", "_" : "..--.-",
"\"" : ".-..-.", "'" : ".----.", ";" : "-.-.-.", "/" : "-..-.", "+" : ".-.-.",
"=" : "-...-", "(" : "-.--.", ")" : "-.--.-", ":" : "---...", "$" : "...-..-",
"!" : "-.-.--", " " : "/"
}

function translate(){
	var str = document.fm.input.value;
	var tmp = document.fm.tempo;
	chk = tmp[0].checked ? 0 : tmp[1].checked ? 1 : tmp[2].checked ? 2 : 0;
	if(!str.match(/^[a-z\d\,\.\-\?\_\"\'\;\/\+\=\(\)\:\$\!\r\n ]*$/i)){
		document.fm.msg.value = "不正な文字が含まれています";
		return false;
	}
	str = str.toUpperCase().replace(/^( |\r\n|\r|\n)+|( |\r\n|\r|\n)+$/g, "").replace(/( |\r\n|\r|\n)( |\r\n|\r|\n)+/g, " ");
	code = str.replace(/./g, function (chr){return " " + sign[chr]}).replace(/^ /, "");
	cnt = 0;
	clearTimeout(tmrID);
	document.fm.msg.value = "再生中";
	document.fm.output.value = code;
	toSound(code);
}

function toSound(s){
	if(cnt == s.length){
		document.fm.msg.value = "終了";
		return false;
	}
	var chr = s.charAt(cnt++);
	if(chr == "-"){
		signal(3 * tempo[chk]);
		tmrID = setTimeout("toSound(code)", 3.8 * tempo[chk]);
	}
	else if(chr == "."){
		signal(1 * tempo[chk]);
		tmrID = setTimeout("toSound(code)", 1.8 * tempo[chk]);
	}
	else if(chr == " "){
		tmrID = setTimeout("toSound(code)", 3 * tempo[chk]);
	}
	else if(chr == "/"){
		tmrID = setTimeout("toSound(code)", 7 * tempo[chk]);
	}
}

function signal(t){
	document.sound.play();
	setTimeout("document.sound.stop()", t);
}

function stopSound(){
	clearTimeout(tmrID);
	document.sound.stop();
	document.fm.msg.value = "終了";
}
http://www.exfiction.net/~nojimake/jswh/jswh18.html