-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtts.go
69 lines (59 loc) · 1.26 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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"os"
)
type GMina struct {
APIKey string
CacheDir string
}
func New(apiKey string, cacheDir string) (*GMina, error) {
if !isFileExist(cacheDir) {
err := os.MkdirAll(cacheDir, 0755)
if err != nil {
return nil, err
}
}
return &GMina{
APIKey: apiKey,
CacheDir: cacheDir,
}, nil
}
func (g *GMina) TTS(text string) (string, error) {
input := map[string]interface{}{
"input": map[string]interface{}{
"text": text,
},
"voice": map[string]interface{}{
"languageCode": "en-US",
"name": "en-US-Wavenet-D",
},
"audioConfig": map[string]interface{}{
"audioEncoding": "MP3",
"pitch": 0.00,
"speakingRate": 1.00,
},
}
jsonBytes, _ := json.Marshal(input)
req, err := http.NewRequest("POST", "https://texttospeech.googleapis.com/v1beta1/text:synthesize?key="+g.APIKey, bytes.NewBuffer(jsonBytes))
if err != nil {
return "", err
}
resp, err := g.request(req)
if err != nil {
return "", err
}
jsonBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
defer resp.Body.Close()
var output struct {
AudioContent string `json:"audioContent"`
}
err = json.Unmarshal(jsonBytes, &output)
return output.AudioContent, err
}