RosettaCodeData/Task/Morse-code/Ursa/morse-code.ursa

98 lines
2.3 KiB
Plaintext

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