generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
230 lines (192 loc) · 5.13 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"os/exec"
"sort"
"strings"
packr "github.com/gobuffalo/packr/v2"
"github.com/peterbourgon/ff/ffcli"
)
var (
sayBox = packr.New("say", "./say")
pronounceBox = packr.New("pronounce", "./pronounce")
)
func main() {
log.SetFlags(0)
var (
sayFlags = flag.NewFlagSet("say", flag.ExitOnError)
sayVoice = sayFlags.String("v", "RANDOM", "voice")
pronounceFlags = flag.NewFlagSet("pronounce", flag.ExitOnError)
pronounceVoice = pronounceFlags.String("v", "RANDOM", "voice")
serverFlags = flag.NewFlagSet("server", flag.ExitOnError)
serverBind = serverFlags.String("b", ":8000", "bind address")
)
pronounce := &ffcli.Command{
Name: "pronounce",
Usage: "pronounce [-v VOICE] PRONOUNCE",
FlagSet: pronounceFlags,
LongHelp: fmt.Sprintf("VOICES\n %s", strings.Join(append(voiceList(pronounceBox), "RANDOM"), "\n ")),
Exec: func(args []string) error {
if len(args) < 1 {
return flag.ErrHelp
}
tosay := strings.Join(args, " ")
err := pronounceToFile(*pronounceVoice, tosay, "out.mp3")
if err != nil {
return fmt.Errorf("pronounce to file: %w", err)
}
return playFile("out.mp3")
},
}
say := &ffcli.Command{
Name: "say",
Usage: "say [-v VOICE] SAY",
FlagSet: sayFlags,
LongHelp: fmt.Sprintf("VOICES\n %s", strings.Join(append(voiceList(sayBox), "RANDOM"), "\n ")),
Exec: func(args []string) error {
if len(args) < 1 {
return flag.ErrHelp
}
_ = sayVoice
return fmt.Errorf("not implemented")
},
}
server := &ffcli.Command{
Name: "server",
Usage: "server [OPTS]",
FlagSet: serverFlags,
Exec: func(args []string) error {
pronounceHandler := func(w http.ResponseWriter, req *http.Request) {
var (
text string
voice = "RANDOM"
filename = "out.mp3" // FIXME: caching with hash
)
if query := req.URL.Query()["text"]; len(query) > 0 {
text = query[0]
}
if query := req.URL.Query()["voice"]; len(query) > 0 {
voice = query[0]
}
if text == "" {
http.Error(w, "invalid input", 500)
return
}
if err := pronounceToFile(voice, text, filename); err != nil {
http.Error(w, fmt.Sprintf("failed to generate: %v", err), 500)
return
}
content, err := ioutil.ReadFile(filename)
if err != nil {
http.Error(w, fmt.Sprintf("failed to read file: %w", err), 500)
return
}
b := bytes.NewBuffer(content)
w.Header().Set("Content-Type", "audio/mpeg")
_, err = b.WriteTo(w)
if err != nil {
http.Error(w, fmt.Sprintf("failed to write to stream: %v", err), 500)
return
}
log.Printf("mp3 sent, voice=%q, text=%q", voice, text)
}
http.HandleFunc("/api/pronounce", pronounceHandler)
log.Printf("Starting server on %q", *serverBind)
return http.ListenAndServe(*serverBind, nil)
},
}
root := &ffcli.Command{
Usage: "speechotron <subcommand> [flags] [args...]",
Subcommands: []*ffcli.Command{pronounce, say, server},
Exec: func([]string) error { return flag.ErrHelp },
}
if err := root.Run(os.Args[1:]); err != nil {
if errors.Is(err, flag.ErrHelp) {
return
}
log.Fatalf("fatal: %+v", err)
}
}
func playFile(dest string) error {
log.Printf("+ afplay %s", dest)
cmd := exec.Command("afplay", dest)
return cmd.Run()
}
func pronounceToFile(voice string, text string, dest string) error {
tosay := []rune(text)
parts := voiceParts(pronounceBox, voice)
selectedParts := []string{}
for i := 0; i < len(tosay); {
maxLen := 0
selectedPart := ""
for partString := range parts {
part := []rune(partString)
if len(part) <= maxLen {
continue
}
if string(part) == string(tosay[i:i+len(part)]) {
maxLen = len(part)
selectedPart = string(part)
}
}
if selectedPart != "" {
i += maxLen
selectedParts = append(selectedParts, selectedPart)
} else {
i++ // skip unmatched parts
}
}
selectedFiles := []string{}
for _, part := range selectedParts {
randomFile := parts[part][rand.Intn(len(parts[part]))]
selectedFiles = append(selectedFiles, fmt.Sprintf("./pronounce/%s", randomFile))
}
cmdArgs := append(selectedFiles, dest)
log.Printf("+ sox %s", strings.Join(cmdArgs, " "))
cmd := exec.Command("sox", cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func voiceList(box *packr.Box) []string {
voices := map[string]bool{}
for _, voice := range box.List() {
if !strings.HasSuffix(voice, ".wav") {
continue
}
voices[strings.Split(voice, "/")[0]] = true
}
ret := []string{}
for voice := range voices {
ret = append(ret, voice)
}
sort.Strings(ret)
return ret
}
func voiceParts(box *packr.Box, voice string) map[string][]string {
ret := map[string][]string{}
for _, file := range box.List() {
if !strings.HasSuffix(file, ".wav") {
continue
}
spl := strings.Split(file, "/")
actualVoice := spl[0]
if voice != "RANDOM" && voice != actualVoice {
continue
}
part := strings.Replace(spl[1], ".wav", "", -1)
if _, ok := ret[part]; !ok {
ret[part] = []string{}
}
ret[part] = append(ret[part], file)
}
return ret
}