Morse code
It has been in use for more than 175 years — longer than any other electronic encoding system.



Task

Send a string as audible Morse code to an audio device   (e.g., the PC speaker).



As the standard Morse code does not contain all possible characters,
you may either ignore unknown characters in the file,
or indicate them somehow   (e.g. with a different pitch).




decl ursa.util.sound snd
decl string<> chars
decl string<> morse
 
append "!"    "\""     "$"       "'"      "("     ")"      "+"     chars
append "---." ".-..-." "...-..-" ".----." "-.--." "-.--.-" ".-.-." morse
append ","      "-"      "."      "/"     "0"     "1"     "2"      chars
append "--..--" "-....-" ".-.-.-" "-..-." "-----" ".----" "..---"  morse
append "3"     "4"     "5"     "6"     "7"     "8"     "9"         chars
append "...--" "....-" "....." "-...." "--..." "---.." "----."     morse
append ":"      ";"      "="     "?"      "@"      "A"  "B"        chars
append "---..." "-.-.-." "-...-" "..--.." ".--.-." ".-" "-..."     morse
append "C"    "D"  "E" "F"    "G"   "H"    "I"  "J"    "K"         chars
append "-.-." "-." "." "..-." "--." "...." ".." ".---" "-.-"       morse
append "L"    "M"  "N"  "O"   "P"    "Q"    "R"   "S"   "T" "U"    chars
append ".-.." "--" "-." "---" ".--." "--.-" ".-." "..." "-" "..-"  morse
append "V"    "W"   "X"    "Y"    "Z"    "["     "]"      "_"      chars
append "...-" ".--" "-..-" "-.--" "--.." "-.--." "-.--.-" "..--.-" morse
 
decl int e f chargap wordgap
# element time in ms. one dot is on for e then off for e
set e 50
# tone frequency in hertz
set f 1280
# time between characters of a word (in units of e)
set chargap 1
# time between words (in units of e)
set wordgap 7
 
 
def gap (int n)
	sleep (* n e)
end gap
decl function off
set off gap
 
 
def on (int n)
	 snd.beep f (/ (* n e) 1000)
end on
 
 
def dot ()
	on 1
	off 1
end dot
 
 
def dash ()
	on 3
	off 1
end dash
 
 
def bloop (int n)
	snd.beep (/ f 2) (/ (* n e) 1000)
end bloop
 
 
def encode_morse (string text)
	decl string<> words
	set words (split (upper (trim text)) " ")
	decl int i j k
	for () (< i (size words)) (inc i)
		for (set j 0) (< j (size words<i>)) (inc j)
			decl int loc
			set loc (locate words<i><j> chars)
			if (= loc -1)
				bloop 3
			else
				for (set k 0) (< k (size morse<loc>)) (inc k)
					if (= morse<loc><k> "-")
						dash
					elif (= morse<loc><k> ".")
						dot
					else
						bloop 3
					end if
				end for
			end if
			gap chargap
		end for
		gap wordgap
	end for
end encode_morse
 
 
# --- uncomment this block to output the source of this file as morse
# decl file src
# src.open args<0>
# encode_morse (src.readall)
 
 
while true
	out "A string to change into morse: " console
	encode_morse (in string console)
end while