-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtts.go
182 lines (157 loc) · 3.38 KB
/
tts.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
package golang_tts
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/bmizerany/aws4"
)
const api = "https://polly.us-west-2.amazonaws.com"
const (
MP3 format = iota
OGG
PCM
)
const (
NEURAL engine = "neural"
STANDARD engine = "standard"
)
const (
RATE_8000 rate = 8000
RATE_16000 rate = 16000
RATE_22050 rate = 22050
)
const (
Geraint = "Geraint"
Gwyneth = "Gwyneth"
Mads = "Mads"
Naja = "Naja"
Hans = "Hans"
Marlene = "Marlene"
Nicole = "Nicole"
Russell = "Russell"
Amy = "Amy"
Brian = "Brian"
Emma = "Emma"
Raveena = "Raveena"
Ivy = "Ivy"
Joanna = "Joanna"
Joey = "Joey"
Justin = "Justin"
Kendra = "Kendra"
Kimberly = "Kimberly"
Salli = "Salli"
Conchita = "Conchita"
Enrique = "Enrique"
Miguel = "Miguel"
Penelope = "Penelope"
Chantal = "Chantal"
Celine = "Celine"
Mathieu = "Mathieu"
Dora = "Dora"
Karl = "Karl"
Carla = "Carla"
Giorgio = "Giorgio"
Mizuki = "Mizuki"
Liv = "Liv"
Lotte = "Lotte"
Ruben = "Ruben"
Ewa = "Ewa"
Jacek = "Jacek"
Jan = "Jan"
Maja = "Maja"
Ricardo = "Ricardo"
Vitoria = "Vitoria"
Cristiano = "Cristiano"
Ines = "Ines"
Carmen = "Carmen"
Maxim = "Maxim"
Tatyana = "Tatyana"
Astrid = "Astrid"
Filiz = "Filiz"
Aditi = "Aditi"
Matthew = "Matthew"
)
type format int
type rate int
type voice int
type engine string
type TTS struct {
accessKey string
secretKey string
request request
}
type request struct {
Engine string
LanguageCode string
OutputFormat string
SampleRate string
Text string
VoiceId string
TextType string
}
func New(accessKey string, secretKey string) *TTS {
return &TTS{
accessKey: accessKey,
secretKey: secretKey,
request: request{
Engine: "standard",
LanguageCode: "en-US",
OutputFormat: "mp3",
SampleRate: "22050",
Text: "",
TextType: "text",
VoiceId: "Brian"}}
}
func (tts *TTS) Format(format format) {
switch format {
case MP3:
tts.request.OutputFormat = "mp3"
case OGG:
tts.request.OutputFormat = "ogg_vorbis"
case PCM:
tts.request.OutputFormat = "pcm"
}
}
func (tts *TTS) Engine(engine engine) {
tts.request.Engine = fmt.Sprintf("%s", engine)
}
func (tts *TTS) SampleRate(rate rate) {
tts.request.SampleRate = fmt.Sprintf("%d", rate)
}
func (tts *TTS) Voice(voice string) {
tts.request.VoiceId = fmt.Sprintf("%s", voice)
}
func (tts *TTS) TextType(textType string) {
tts.request.TextType = fmt.Sprintf("%s", textType)
}
func (tts *TTS) Language(lang string) {
tts.request.LanguageCode = fmt.Sprintf("%s", lang)
}
func (tts *TTS) Speech(text string) (data []byte, err error) {
tts.request.Text = text
b, err := json.Marshal(tts.request)
if err != nil {
return []byte{}, err
}
r, _ := http.NewRequest("POST", api+"/v1/speech", bytes.NewReader(b))
r.Header.Set("Content-Type", "application/json")
client := aws4.Client{Keys: &aws4.Keys{
AccessKey: tts.accessKey,
SecretKey: tts.secretKey}}
res, err := client.Do(r)
if err != nil {
return []byte{}, err
}
defer func() {
err = res.Body.Close()
}()
data, err = ioutil.ReadAll(res.Body)
if err != nil {
return []byte{}, err
} else if res.StatusCode != 200 {
return []byte{}, fmt.Errorf("returned status code: %s %q", res.Status, data)
}
return data, nil
}