Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active April 24, 2022 07:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kou1okada/4d35dce83a1824b2a296 to your computer and use it in GitHub Desktop.
Save kou1okada/4d35dce83a1824b2a296 to your computer and use it in GitHub Desktop.
Echo by the Morse code of beeps.
#!/usr/bin/env bash
#
# morsh.sh - Echo by the Morse code of beeps.
# Copyright (c) 2015 Koichi OKADA. All rights reserved.
# This script is distributed under the MIT license.
# http://www.opensource.org/licenses/mit-license.php
#
: ${freq:=10000}
: ${delay:=100}
: ${short:=50}
: ${long:=200}
: ${OPT_visualize}
if (( $# <= 0 )); then
cat <<-EOD
Usage: ${0##*/} <strings>
Echo by the Morse code of beep.
Environment variables:
freq : frequency [Hz] (= $freq)
delay : delay between signal [ms] (= $delay)
short : short signal [ms] (= $short)
long : long signal [ms] (= $long)
OPT_visualize : Visualize sequence of the Morse code
(= "${OPT_visualise}")
EOD
exit
fi
function gen_morse () # [ <char> ... ]
# Generate sequence of the Morse code from characters.
# Return:
# ${args[@]} : sequence of the Morse code.
{
local i
local sep=""
args=()
for i; do
args+=( $sep )
case "$i" in
a) args+=( . _ ) ;;
b) args+=( _ . . . ) ;;
c) args+=( _ . _ . ) ;;
d) args+=( _ . . ) ;;
e) args+=( . ) ;;
f) args+=( . . _ ) ;;
g) args+=( _ _ . ) ;;
h) args+=( . . . . ) ;;
i) args+=( . . ) ;;
j) args+=( . _ _ _ ) ;;
k) args+=( _ . _ ) ;;
l) args+=( . _ . .) ;;
m) args+=( _ _ ) ;;
n) args+=( _ . ) ;;
o) args+=( _ _ _ ) ;;
p) args+=( . _ _ . ) ;;
q) args+=( _ _ . _ ) ;;
r) args+=( . _ . ) ;;
s) args+=( . . . ) ;;
t) args+=( _ ) ;;
u) args+=( . . _ ) ;;
v) args+=( . . . _ ) ;;
w) args+=( . _ _ ) ;;
x) args+=( _ . . _ ) ;;
y) args+=( _ . _ _ ) ;;
z) args+=( _ _ . . ) ;;
1) args+=( . _ _ _ _ ) ;;
2) args+=( . . _ _ _ ) ;;
3) args+=( . . . _ _ ) ;;
4) args+=( . . . . _ ) ;;
5) args+=( . . . . . ) ;;
6) args+=( _ . . . . ) ;;
7) args+=( _ _ . . . ) ;;
8) args+=( _ _ _ . . ) ;;
9) args+=( _ _ _ _ . ) ;;
0) args+=( _ _ _ _ _ ) ;;
.) args+=( . _ . _ . _ ) ;;
,) args+=( _ _ . . _ _ ) ;;
\?) args+=( . . _ _ . . ) ;;
!) args+=( _ . _ . _ _ ) ;;
-) args+=( _ . . . . _ ) ;;
/) args+=( _ _ . . _ . ) ;;
@) args+=( . _ _ . _ . ) ;;
\() args+=( _ . _ _ . ) ;;
\)) args+=( _ . _ _ . _ ) ;;
\ ) args+=( , , ) ;;
esac
sep=,
done
}
function gen_beep () # [ <Morse code> ... ]
# Generate arguments for beep command
# from sequence of the Morse code.
# Arguments:
# <mores code> : "," or "." or "_"
# Return:
# ${args[@]} : arguments for beep command.
{
local i
local sep
args=()
for i; do
args+=( $sep )
case "$i" in
,) args+=( -f 1 -D $delay -l 0 ) ;;
.) args+=( -f $freq -D $delay -l $short ) ;;
_) args+=( -f $freq -D $delay -l $long ) ;;
esac
sep=-n
done
}
function split () # <strings>
# Split strings to characters.
# Return:
# Output to STDOUT for one charactor per line.
{
awk '
BEGIN {
for(i = 1; i <= length(ARGV[1]); i++) {
print substr(ARGV[1],i,1);
}
}
' "$1"
}
function visualize_morse () # [ <char> ... ]
# Visualize sequence of the Morse code.
{
local i args slpw slps slpl
read slpw < <(awk "BEGIN{print ($delay*2 )/1000}")
read slps < <(awk "BEGIN{print ($delay+$short)/1000}")
read slpl < <(awk "BEGIN{print ($delay+$long )/1000}")
for i; do
echo -n "$i ("
gen_morse "$i"
for j in "${args[@]}"; do
case "$j" in
,) sleep $slpw ;;
.) echo -n " $j"; sleep $slps ;;
_) echo -n " $j"; sleep $slpl ;;
esac
done
echo -n " ) "
done
echo
}
readarray -t chars < <(split "$@")
gen_morse "${chars[@]}"
morse=( "${args[@]}" )
gen_beep "${args[@]}"
[ -n "$DEBUG" ] && echo "${morse[@]}"
[ -n "$DEBUG" ] && echo "${args[@]}"
beep "${args[@]}" &
[ "$OPT_visualize" ] && visualize_morse "${chars[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment