Debian Codegraph @

sam.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # sam.sh, v. .01a
3 # Still Another Morse (code training script)
4 # With profuse apologies to Sam (F.B.) Morse.
5 # Author: Mendel Cooper
6 # License: GPL3
7 # Reldate: 05/25/11
8
9 # Morse code training script.
10 # Converts arguments to audible dots and dashes.
11 # Note: lowercase input only at this time.
12
13
14
15 # Get the wav files from the source tarball:
16 # http://bash.deta.in/abs-guide-latest.tar.bz2
17 DOT='soundfiles/dot.wav'
18 DASH='soundfiles/dash.wav'
19 # Maybe move soundfiles to /usr/local/sounds?
20
21 LETTERSPACE=300000 # Microseconds.
22 WORDSPACE=980000
23 # Nice and slow, for beginners. Maybe 5 wpm?
24
25 EXIT_MSG="May the Morse be with you!"
26 E_NOARGS=75 # No command-line args?
27
28
29
30 declare -A morse # Associative array!
31 # ======================================= #
32 morse[a]="dot; dash"
33 morse[b]="dash; dot; dot; dot"
34 morse[c]="dash; dot; dash; dot"
35 morse[d]="dash; dot; dot"
36 morse[e]="dot"
37 morse[f]="dot; dot; dash; dot"
38 morse[g]="dash; dash; dot"
39 morse[h]="dot; dot; dot; dot"
40 morse[i]="dot; dot;"
41 morse[j]="dot; dash; dash; dash"
42 morse[k]="dash; dot; dash"
43 morse[l]="dot; dash; dot; dot"
44 morse[m]="dash; dash"
45 morse[n]="dash; dot"
46 morse[o]="dash; dash; dash"
47 morse[p]="dot; dash; dash; dot"
48 morse[q]="dash; dash; dot; dash"
49 morse[r]="dot; dash; dot"
50 morse[s]="dot; dot; dot"
51 morse[t]="dash"
52 morse[u]="dot; dot; dash"
53 morse[v]="dot; dot; dot; dash"
54 morse[w]="dot; dash; dash"
55 morse[x]="dash; dot; dot; dash"
56 morse[y]="dash; dot; dash; dash"
57 morse[z]="dash; dash; dot; dot"
58 morse[0]="dash; dash; dash; dash; dash"
59 morse[1]="dot; dash; dash; dash; dash"
60 morse[2]="dot; dot; dash; dash; dash"
61 morse[3]="dot; dot; dot; dash; dash"
62 morse[4]="dot; dot; dot; dot; dash"
63 morse[5]="dot; dot; dot; dot; dot"
64 morse[6]="dash; dot; dot; dot; dot"
65 morse[7]="dash; dash; dot; dot; dot"
66 morse[8]="dash; dash; dash; dot; dot"
67 morse[9]="dash; dash; dash; dash; dot"
68 # The following must be escaped or quoted.
69 morse[?]="dot; dot; dash; dash; dot; dot"
70 morse[.]="dot; dash; dot; dash; dot; dash"
71 morse[,]="dash; dash; dot; dot; dash; dash"
72 morse[/]="dash; dot; dot; dash; dot"
73 morse[\@]="dot; dash; dash; dot; dash; dot"
74 # ======================================= #
75
76 play_letterscip-shell shell abs-guide 10-4 play_letter(). ()
77 {
78 eval ${morse[$1]} # Play dots, dashes from appropriate sound files.
79 # Why is 'eval' necessary here?
80 usleep $LETTERSPACE # Pause in between letters.
81 }
82
83 extract_lettersscip-shell shell abs-guide 10-4 extract_letters(). ()
84 { # Slice string apart, letter by letter.
85 local pos=0 # Starting at left end of string.
86 local len=1 # One letter at a time.
87 strlen=${#1}
88
89 while [scip-shell system . . `[`. $pos -lt $strlen ]
90 do
91 letter=${1:pos:len}
92 # ^^^^^^^^^^^^ See Chapter 10.1.
93 play_letterscip-shell shell abs-guide 10-4 play_letter(). $letter
94 echoscip-shell system . . echo. -n "*" # Mark letter just played.
95 ((pos++))
96 done
97 }
98
99 ######### Play the sounds ############
100 dotscip-shell shell abs-guide 10-4 dot().() { aplay "$DOT" 2&>/dev/nullscip-shell filesystem . . `/dev/null`.; }
101 dashscip-shell shell abs-guide 10-4 dash().() { aplay "$DASH" 2&>/dev/nullscip-shell filesystem . . `/dev/null`.; }
102 ######################################
103
104 no_argsscip-shell shell abs-guide 10-4 no_args(). ()
105 {
106 declare -a usage
107 usage=( $0 word1 word2 ... )
108
109 echoscip-shell system . . echo. "Usage:"; echoscip-shell system . . echo.
110 echoscip-shell system . . echo. ${usage[*]}
111 for index in 0 1 2 3
112 do
113 extract_lettersscip-shell shell abs-guide 10-4 extract_letters(). ${usage[index]}
114 usleep $WORDSPACE
115 echoscip-shell system . . echo. -n " " # Print space between words.
116 done
117 # echo "Usage: $0 word1 word2 ... "
118 echoscip-shell system . . echo.; echoscip-shell system . . echo.
119 }
120
121
122 # int main()
123 # {
124
125 clearscip-shell system . . clear. # Clear the terminal screen.
126 echoscip-shell system . . echo. " SAM"
127 echoscip-shell system . . echo. "Still Another Morse code trainer"
128 echoscip-shell system . . echo. " Author: Mendel Cooper"
129 echoscip-shell system . . echo.; echoscip-shell system . . echo.;
130
131 if [scip-shell system . . `[`. -z "$1" ]
132 then
133 no_argsscip-shell shell abs-guide 10-4 no_args().
134 echoscip-shell system . . echo.; echoscip-shell system . . echo.; echoscip-shell system . . echo. "$EXIT_MSG"; echoscip-shell system . . echo.
135 exit $E_NOARGS
136 fi
137
138 echoscip-shell system . . echo.; echoscip-shell system . . echo. "$*" # Print text that will be played.
139
140 until [scip-shell system . . `[`. -z "$1" ]
141 do
142 extract_lettersscip-shell shell abs-guide 10-4 extract_letters(). $1
143 shift # On to next word.
144 usleep $WORDSPACE
145 echoscip-shell system . . echo. -n " " # Print space between words.
146 done
147
148 echoscip-shell system . . echo.; echoscip-shell system . . echo.; echoscip-shell system . . echo. "$EXIT_MSG"; echoscip-shell system . . echo.
149
150 exit 0
151 # }
152
153 # Exercises:
154 # ---------
155 # 1) Have the script accept either lowercase or uppercase words
156 #+ as arguments. Hint: Use 'tr' . . .
157 # 2) Have the script optionally accept input from a text file.