-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
206 lines (191 loc) · 4.29 KB
/
main.go
File metadata and controls
206 lines (191 loc) · 4.29 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"
"unicode"
"github.com/jlondonobo/morse/internal/sound"
"sync"
"github.com/urfave/cli/v3"
)
const (
DefaultPitch = 700
DefaultWordsPerMinute = 20
DefaultTone = "sine"
)
var morseDictionary = map[string]string{
"a": ".-",
"b": "-...",
"c": "-.-.",
"d": "-..",
"e": ".",
"f": "..-.",
"g": "--.",
"h": "....",
"i": "..",
"j": ".---",
"k": "-.-",
"l": ".-..",
"m": "--",
"n": "-.",
"o": "---",
"p": ".--.",
"q": "--.-",
"r": ".-.",
"s": "...",
"t": "-",
"u": "..-",
"v": "...-",
"w": ".--",
"x": "-..-",
"y": "-.--",
"z": "--..",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
"0": "-----",
" ": "/",
".": ".-.-.-",
",": "--..--",
"?": "..--..",
"'": ".----.",
"/": "-..-.",
"(": "-.--.",
")": "-.--.-",
":": "---...",
"=": "-...-",
"+": ".-.-.",
"-": "-....-",
"\"": ".-..-.",
"@": ".--.-.",
}
func toMorse(s string) string {
var sb strings.Builder
for _, v := range s {
chr := string(unicode.ToLower(v))
repl, ok := morseDictionary[chr]
if !ok {
log.Fatalf(
"Character `%s` not defined in the International Morse Code Recommendation.",
string(v),
)
}
sb.WriteString(repl)
sb.WriteString(" ")
}
return sb.String()
}
var ErrInvalidParameter = errors.New("invalid parameter")
func main() {
var wg sync.WaitGroup
var translateInput string
var play bool
var output bool
var file string
var pitch uint16
var wpm uint8
var waveType string
cmd := &cli.Command{
UseShortOptionHandling: true,
Name: "morse",
Usage: "beep beep beeep",
Arguments: []cli.Argument{
&cli.StringArg{
Name: "input",
Destination: &translateInput,
},
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "sound",
Aliases: []string{"s"},
Value: false,
Usage: "plays on speakers",
Destination: &play,
},
&cli.BoolFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "writes sound to sound.wav file",
Destination: &output,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"f"},
Usage: "writes sound to wav file",
Destination: &file,
},
&cli.Uint16Flag{
Name: "pitch",
Aliases: []string{"p"},
Usage: "sets the ouput pitch",
Destination: &pitch,
Value: DefaultPitch,
Action: func(ctxt context.Context, cmd *cli.Command, v uint16) error {
if v < 300 || v > 1000 {
return fmt.Errorf("%w: pitch value '%d' out of range [300-1000]", ErrInvalidParameter, pitch)
}
return nil
},
},
&cli.Uint8Flag{
Name: "speed",
Usage: "sets the ouput speed in words per minute, higher means faster",
Destination: &wpm,
Value: DefaultWordsPerMinute,
Action: func(ctxt context.Context, cmd *cli.Command, v uint8) error {
if v < 5 || v > 40 {
return fmt.Errorf("%w: speed value '%d' out of range [5-40]", ErrInvalidParameter, wpm)
}
return nil
},
},
&cli.StringFlag{
Name: "tone",
Usage: "sets the ouput tone, possible values are 'sine', 'triangle', 'sawtooth', 'square'",
Destination: &waveType,
Value: DefaultTone,
Action: func(ctxt context.Context, cmd *cli.Command, v string) error {
_, ok := sound.ToneGenerator[v]
if !ok {
return fmt.Errorf("invalid tone '%v' must be one of ['sine' 'triangle' 'sawtooth' 'square']", v)
}
return nil
},
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
seq := toMorse(translateInput)
conf := &sound.Config{Pitch: pitch, Wpm: wpm, WaveType: waveType}
fmt.Println(seq)
if play {
wg.Go(func() { sound.Play(seq, conf) })
}
if output && (len(file) > 0) {
log.Fatal("Cannot use --output and --output-file at the same time.")
return nil
}
if output {
wg.Go(func() { sound.Write(seq, "sound.wav", conf) })
} else if len(file) > 0 {
wg.Go(func() {
sound.Write(seq, file, conf)
})
}
wg.Wait()
return nil
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}