...Wasn't sure if anyone would be interested.
It's not pretty and not optimized (haven't had any more time to hack on it since early this AM when I got it to work), but it works. :)
Note: you should create a directory 'temp' in the same directory you run it from that has full rw access.
~Scott
Code: Select all
<?
// morse.php
// skehoe - 1.11.03
$morse = array(
'0' => '-----',
'1' => '.----',
'2' => '..---',
'3' => '...--',
'4' => '....-',
'5' => '.....',
'6' => '-....',
'7' => '--...',
'8' => '---..',
'9' => '----.',
'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' => '--..',
'PERIOD' => '.-.-.-',
'COMMA' => '--..--',
'COLON' => '---...',
'QUESTION' => '..--..',
'APOS' => '.----.',
'HYPHEN' => '-....-',
'PAREN' => '-.--.-',
'QUOTE' => '.-..-.',
'SLASH' => '-..-.',
'SEMI' => '-.-.-',
'UNDER' => '..--.-',
'SPACE' => '/'
);
function create_sound($in) {
$length = array(
'dash' => 3,
'pause1' => 1,
'pause2' => 3,
'pause3' => 7,
'word' => 50,
'dot' => 1000 * 60 / ($in[wpm] * 50)
);
$pieces = preg_split('//', $in
Code: Select all
);
$count[dot] = 0;
$count[dash] = 0;
$count[space] = 0;
$count[slash] = 0;
foreach ($pieces as $key => $val) {
if ($val == '.') {
$count[dot]++;
} else if ($val == '-') {
$count[dash]++;
} else if ($val == ' ') {
$count[space]++;
} else if ($val == '/') {
$count[slash]++;
}
}
// Sound Header
$bytes = $length[dot] * 8 * ($count[dot] * (1 + $length[pause1])
+ $count[dash] * ($length[dash] + $length[pause1])
+ $count[space] * ($length[pause2] - $length[pause1])
+ $count[slash] * ($length[pause3] - $length[pause1]));
$sound .= pack(H8,"2e736e64"); // magic number
$sound .= pack(H8,"0000001c"); // data location (=28)
$b1 = ($bytes & (255 << 24)) >> 24;
$b2 = ($bytes & (255 << 16)) >> 16;
$b3 = ($bytes & (255 << 8)) >> 8;
$b4 = $bytes & (255);
$sound .= pack(C4,$b1,$b2,$b3,$b4);
$sound .= pack(H8,"00000002"); // sound type - 8bit linear
$sound .= pack(H8,"00001f40"); // sampling rate
$sound .= pack(H8,"00000001"); // channel count
$sound .= pack(H8,"00000000"); // optional info (minimum 4 bytes)
foreach ($pieces as $key => $val) {
if ($val == '.') {
$sound .= sound_data(1,1,$length);
$sound .= sound_data($length[pause1],0,$length);
} else if ($val == '-') {
$sound .= sound_data($length[dash],1,$length);
$sound .= sound_data($length[pause1],0,$length);
} else if ($val == ' ') {
$sound .= sound_data($length[pause2] - $length[pause1],0,$length);
} else if ($val == '/') {
$sound .= sound_data($length[pause3] - $length[pause1],0,$length);
}
}
return $sound;
}
function sound_data($units,$onoff,$length) {
$sound = pack(H16,"a781a700597f5900");
$no_sound = pack(H16,"0000000000000000");
if ($onoff == '1') {
$out = $sound;
} else {
$out = $no_sound;
}
for ($count = $length[dot] * $units; $count > 0; $count--) {
$return .= $out;
}
return $return;
}
function check_punct($val) {
// Replace with case
if ($val == '.') { $val = 'PERIOD'; }
else if ($val == ',') { $val = 'COMMA'; }
else if ($val == ':') { $val = 'COLON'; }
else if ($val == '?') { $val = 'QUESTION'; }
else if ($val == "'") { $val = 'APOS'; }
else if ($val == '-') { $val = 'HYPHEN'; }
else if ($val == '(') { $val = 'PAREN'; }
else if ($val == ')') { $val = 'PAREN'; }
else if ($val == '"') { $val = 'QUOTE'; }
else if ($val == '/') { $val = 'SLASH'; }
else if ($val == ';') { $val = 'SEMI'; }
else if ($val == '_') { $val = 'UNDER'; }
else if ($val == ' ') { $val = 'SPACE'; }
else if ($val == 'COMMA') { $val = ','; }
else if ($val == 'PERIOD') { $val = '.'; }
else if ($val == 'COLON') { $val = ':'; }
else if ($val == 'QUESTION') { $val = '?'; }
else if ($val == 'APOS') { $val = "'"; }
else if ($val == 'HYPHEN') { $val = '-'; }
else if ($val == 'PAREN') { $val = '('; }
else if ($val == 'QUOTE') { $val = '"'; }
else if ($val == 'SLASH') { $val = '/'; }
else if ($val == 'SEMI') { $val = ';'; }
else if ($val == 'UNDER') { $val = '_'; }
else if ($val == 'SPACE') { $val = ' '; }
return $val;
}
function ascii_to_morse($in, $morse) {
$clean = str_replace(' ', ' ', $in);
$in_array = preg_split('//', strtoupper($clean), -1, PREG_SPLIT_NO_EMPTY);
foreach ($in_array as $key => $val) {
$out[input] .= $val;
$val = check_punct($val);
$out[output] .= $morse[$val]." ";
}
return $out;
}
function morse_to_ascii($in, $morse) {
$in_array = split(' ', $in);
foreach ($in_array as $key => $val) {
$out[input] .= $val." ";
foreach ($morse as $foo => $bar) {
if ($bar == $val) {
$foo = check_punct($foo);
$out[output] .= $foo;
break;
}
}
}
// Check for open and close parens...
return $out;
}
function display_main($in, $listen, $wpm) {
?>
<center>
<h2>Morse Code Translator</h2>
<form method="post" action="index.php">
<b>Enter Text or Morse Code Here:</b><br>
<font color="blue">Acceptable Entries: A-Z 0-9 . , : ; ? ' " - _ / ( )</font><br>
<br><textarea name="user_in" rows="10" cols="80"></textarea><br>
<br><b>Text to Morse </b><input type="radio" name="type" value="t_to_m" checked> <b>Morse to Text <input type="radio" name="type" value="m_to_t">
<b>WPM: </b><input type="text" size="5" name="wpm" value="15"><br>
<br><input type="submit" value="Translate Now">
</form>
<?
if (is_array($in)) {
echo "<p><hr><b><u>Your Text/Code</u></b><br><br>".stripslashes($in[input]);
echo "<p><hr><b><u>Translation</u></b><br><br>".stripslashes($in[output])."<br><br>";
if ($listen == 1) {
$out
Code: Select all
= $in[output];
$out[wpm] = $wpm;
$sound = create_sound($out);
create_file($sound);
echo '<embed src="temp/morse.au" volume="50" autostart="false" height="15" width="300"><br>';
}
echo "<br><hr>";
}
}
function create_file($sound) {
$filename = 'temp/morse.au';
$fp = fopen($filename, 'w+');
fwrite($fp, $sound);
fclose($fp);
}
// MAIN
$out = '';
$listen = 0;
if (isset($_POST[user_in])) {
if ($_POST[type] == 'm_to_t') {
$out = morse_to_ascii($_POST[user_in], $morse);
} else if ($_POST[type] == 't_to_m') {
$out = ascii_to_morse($_POST[user_in], $morse);
$listen = 1;
}
}
display_main($out, $listen, $_POST[wpm]);
?>