-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorsebeep
executable file
·146 lines (123 loc) · 3.89 KB
/
morsebeep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
# Default parameters
F=880 # Frequency
D1=0.05 # Length of dot is one unit
S=false # Show dots and dash in screen
INPUT=""
if [ -p /dev/stdin ]; then
INPUT=$(cat /dev/stdin)
fi
# FUNCTIONS -----------------------------------------------------------------
# Placement of "-t alsa" is critical. So do not change the order of parameters
function dot()
{
if [ "$S" = "true" ]; then printf "."; fi
play -nq -t alsa synth $D1 square $F
}
function dash()
# Placement of "-t alsa" is critical. So do not change the order of parameters
{
if [ "$S" = "true" ]; then printf "-"; fi
play -nq -t alsa synth $D3 square $F
}
function w1() # Wait 1 unit
{
if [ "$S" = "true" ]; then printf ""; fi
sleep $D1
}
function w3() # Wait 3 units
{
if [ "$S" = "true" ]; then printf " "; fi
sleep $D3
}
function w7() # Wait 7 units
{
if [ "$S" = "true" ]; then printf "\n"; fi
sleep $D7
}
function usage() # Help usage --help
{
echo "Usage: $ $(basename $0) <parameters>"
echo
echo " PARAMETERS"
echo " -f <frequency> tone of sound (default 880)"
echo " -m <message> message to transform into morse. It can be read from stdin"
echo " -t <time> length of dot (default 0.05)"
echo " -s show dashes and dots (default false)"
echo " --help (this help)"
echo
echo " EXAMPLES"
echo " $ $(basename $0) -m sos -f 900"
echo " $ $(basename $0) -m \"Hello world\""
echo " $ $(basename $0) -m \"Hello world\" -t 0.1 -f 1000"
echo " $ cat news1.txt | $(basename $0) "
}
# Parameters -----------------------------------------------------
while [ ! -z $1 ]; do
case $1 in
# Frequency
"-f") shift; F=$1;;
# Message
"-m") shift; INPUT=$1;;
# Help
"-h" | "--help") usage; exit 0;;
# Time
"-t") shift; D1=$1;;
# Show dashes and dots
"-s") S=true;;
*) usage; exit 1;;
esac
shift
done
# Main program ----------------------------------------------
# Length of 3 and 7 units
D3=`echo $D1 \* 3 | bc` # A dash is three units
D7=`echo $D1 \* 7 | bc` # Space between words is seven units
LENGTH=`echo -n $INPUT | wc -m` # Length of input string
for ((i=0; i<$LENGTH; i++)); do
c="${INPUT:$i:1}" # Just one character each time
c=${c,} # To lower
case $c in
" ") w7; continue;; # End of word. Wait 7 units
"a" | "á") dot; w1; dash;;
"b") dash; w1; dot; w1; dot; w1; dot;;
"c") dash; w1; dot; w1; dash; w1; dot;;
"d") dash; w1; dot; w1; dot;;
"e" | "é") dot;;
"f") dot; w1; dot; w1; dash; w1; dot;;
"g") dash; w1; dash; w1; dot;;
"h") dot; w1; dot; w1; dot; w1; dot;;
"i" | "í") dot; w1; dot;;
"j") dot; w1; dash; w1; dash; w1; dash;;
"k") dash; w1; dot; w1; dash;;
"l") dot; w1; dash; w1; dot; w1; dot;;
"m") dash; w1; dash;;
"n") dash; w1; dot;;
"ñ") dash; w1; dash; w1; dot; w1; dash; w1; dash;;
"o" | "ó") dash; w1; dash; w1; dash;;
"p") dot; w1; dash; w1; dash; w1; dot;;
"q") dash; w1; dash; w1; dot; w1; dash;;
"r") dot; w1; dash; w1; dot;;
"s") dot; w1; dot; w1; dot;;
"t") dash;;
"u" | "ú") dot; w1; dot; w1; dash;;
"v") dot; w1; dot; w1; dot; w1; dash;;
"w") dot; w1; dash; w1; dash;;
"x") dash; w1; dot; w1; dot; w1; dash;;
"y") dash; w1; dot; w1; dash; w1; dash;;
"z") dash; w1; dash; w1; dot; w1; dot;;
"1") dot; w1; dash; w1; dash; w1; dash; w1; dash;;
"2") dot; w1; dot; w1; dash; w1; dash; w1; dash;;
"3") dot; w1; dot; w1; dot; w1; dash; w1; dash;;
"4") dot; w1; dot; w1; dot; w1; dot; w1; dash;;
"5") dot; w1; dot; w1; dot; w1; dot; w1; dot;;
"6") dash; w1; dot; w1; dot; w1; dot; w1; dot;;
"7") dash; w1; dash; w1; dot; w1; dot; w1; dot;;
"8") dash; w1; dash; w1; dash; w1; dot; w1; dot;;
"9") dash; w1; dash; w1; dash; w1; dash; w1; dot;;
"0") dash; w1; dash; w1; dash; w1; dash; w1; dash;;
esac
w3 # End of letter. Wait 3 units. Gap between dots and dashes
done
echo
exit 0