-
Notifications
You must be signed in to change notification settings - Fork 1
/
gofliteweb.go
141 lines (122 loc) · 3.88 KB
/
gofliteweb.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
// Copyright 2013, Carnegie Mellon University. All Rights Reserved.
// Use of this code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Author: Alok Parlikar <[email protected]>
// GoFliteWeb: Flite Speech Synthesis Demo on the Web
package main
import (
"flag"
"github.com/happyalu/goflite"
"html/template"
"io/ioutil"
"log"
"net/http"
"path"
"strings"
)
var (
serverAddr = flag.String("addr", "localhost:8080", "Server Address")
voxpath = flag.String("voxpath", "", "Absolute path to *.flitevox files")
voicelist []string
)
func main() {
flag.Parse()
// Look for *.flitevox files in given voxpath and add voices to goflite
if *voxpath != "" {
voxdirFiles, _ := ioutil.ReadDir(*voxpath)
for _, v := range voxdirFiles {
name := v.Name()
if strings.HasSuffix(name, ".flitevox") {
err := goflite.AddVoice(name, path.Join(*voxpath, name))
if err != nil {
log.Printf("FAILED to add voice %s", name)
} else {
voicelist = append(voicelist, name)
log.Printf("ADDED voice %s", name)
}
}
}
}
http.HandleFunc("/", IndexHandler)
http.HandleFunc("/wav", WaveHandler)
http.ListenAndServe(*serverAddr, nil)
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
const indexpage = `
<!DOCTYPE html>
<html>
<head>
<title>Flite Synthesis Demo</title>
<script type="text/javascript">
function play_tts() {
var text = document.getElementById('textarea').value;
var voice = document.getElementById('voice').options[document.getElementById('voice').selectedIndex].text;
var audio = document.getElementById('player');
audio.setAttribute('src', '/wav?text=' + encodeURIComponent(text) + '&voice=' + encodeURIComponent(voice));
audio.play();
}
function download_tts() {
var text = document.getElementById('textarea').value;
var voice = document.getElementById('voice').options[document.getElementById('voice').selectedIndex].text;
var downloader = document.getElementById('downloader');
downloader.setAttribute('href', '/wav?text=' + encodeURIComponent(text) + '&voice=' + encodeURIComponent(voice));
downloader.click();
}
</script>
</head>
<body>
<h1>Flite Synthesis Demo</h1>
Choose Voice:
<select id="voice">
{{range .Voices}}
<option value="{{.}}">{{.}}</option>
{{end}}
<option value="Default">Default</option>
</select> <br /> <br />
<textarea rows=3 cols=80 id="textarea" name="text">A whole joy was reaping, but they've gone south. Go fetch azure mike!</textarea>
<br /> <br />
<input type="submit" value="Speak!" onclick="play_tts();">
<input type="submit" value="Download!" onclick="download_tts();"> <br />
<audio id="player"></audio>
<a id="downloader" href="" download="flite.wav" target="_blank" style="display:none;"></a>
<br />
<br />
<br />
<small>Synthesis Powered by <a href="http://www.cmuflite.org">Flite</a>.</small> <br />
<small>Demo Powered by <a href="http://www.github.com/happyalu/goflite">GoFlite</a>.</small>
</body>
</html>`
type Data struct {
Voices []string
}
data := Data{voicelist}
t := template.Must(template.New("indexpage").Parse(indexpage))
err := t.Execute(w, data)
if err != nil {
log.Println("INDEX: Fail", err)
}
}
func WaveHandler(w http.ResponseWriter, r *http.Request) {
text := r.FormValue("text")
voice := r.FormValue("voice")
w.Header().Set("Content-Type", "audio/x-wav")
clientIP := r.RemoteAddr
if colon := strings.LastIndex(clientIP, ":"); colon != -1 {
clientIP = clientIP[:colon]
}
log.Printf("wavegen: %s\t%s\t%s", clientIP, voice, text)
if voice == "Default" {
// Empty voice makes goflite choose the default voice
voice = ""
}
wav, err := goflite.TextToWave(text, voice)
if err != nil {
log.Printf("WAVE: Could not synthesize %s: %s", voice, text)
http.Error(w, "Could not Synthesize Speech", 500)
return
}
err = wav.EncodeRIFF(w)
if err != nil {
log.Println("Could not write waveform")
}
}