-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (97 loc) · 3.37 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"math"
"os"
"strings"
"github.com/go-audio/audio"
"github.com/go-audio/wav"
)
var morseCodeDict = map[rune]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 textToMorse(text string) string {
var morse []string
for _, char := range strings.ToUpper(text) {
if code, exists := morseCodeDict[char]; exists {
morse = append(morse, code)
}
}
return strings.Join(morse, " ")
}
func generateTone(frequency, duration, sampleRate float64, amplitude float64) []float64 {
var tone []float64
sampleCount := int(sampleRate * duration)
for i := 0; i < sampleCount; i++ {
t := float64(i) / sampleRate
sample := amplitude * math.Sin(2*math.Pi*frequency*t)
tone = append(tone, sample)
}
return tone
}
func appendSilence(duration, sampleRate float64) []float64 {
return make([]float64, int(sampleRate*duration))
}
func textToMorseAudio(text, filename string) {
morseCode := textToMorse(text)
dotDuration := 0.1 // Duration of a dot in seconds
dashDuration := 3 * dotDuration // Duration of a dash
frequency := 1000.0 // Frequency of the tone
sampleRate := 44100.0 // Samples per second
amplitude := 0.5 // Amplitude of the tone
var audioSequence []float64
for _, char := range morseCode {
switch char {
case '.':
audioSequence = append(audioSequence, generateTone(frequency, dotDuration, sampleRate, amplitude)...)
case '-':
audioSequence = append(audioSequence, generateTone(frequency, dashDuration, sampleRate, amplitude)...)
case ' ':
audioSequence = append(audioSequence, appendSilence(dotDuration, sampleRate)...)
}
audioSequence = append(audioSequence, appendSilence(dotDuration, sampleRate)...)
}
f, err := os.Create(filename)
if err != nil {
panic(err)
}
defer f.Close()
enc := wav.NewEncoder(f, int(sampleRate), 16, 1, 1)
intBuf := &audio.IntBuffer{Data: make([]int, len(audioSequence)), Format: &audio.Format{SampleRate: int(sampleRate), NumChannels: 1}}
for i, v := range audioSequence {
intBuf.Data[i] = int(v * (1 << 15-1))
}
if err := enc.Write(intBuf); err != nil {
panic(err)
}
if err := enc.Close(); err != nil {
panic(err)
}
fmt.Printf("Message to encode: %s\n", text)
fmt.Printf("Message in Morse code encode: %s\n", morseCode)
fmt.Printf("Generated audio morse code to %s\n", filename)
}
func main() {
text := flag.String("t", "", "Input text to convert to Morse code")
output := flag.String("o", "morse_output.wav", "Output filename for the Morse code audio")
help := flag.Bool("h", false, "Show usage information")
flag.Parse()
if *help || *text == "" {
fmt.Println("Usage: morsecode -t \"TEXT\" [-o output.wav]")
fmt.Println(" -t Input text to convert to Morse code")
fmt.Println(" -o Output filename for the Morse code audio (default: morse_output.wav)")
fmt.Println(" -h Show usage information")
return
}
filename := *output + ".wav"
if filename == "" {
filename = "morse_output.wav"
}
textToMorseAudio(*text, filename)
}