forked from jpadilla/ivona-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivona.go
113 lines (87 loc) · 2.56 KB
/
ivona.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
// Package ivona provides the binding for IVONA Speech Cloud API
package ivona
import (
"bytes"
"encoding/json"
"fmt"
"github.com/bmizerany/aws4"
"io/ioutil"
"net/http"
)
// ivonaAPI is the public API IVONA Speech Cloud URL.
const ivonaAPI = "https://tts.eu-west-1.ivonacloud.com"
// createSpeechAPI is the public API IVONA Speech Cloud URL for the CreateSpeech action.
const createSpeechAPI = ivonaAPI + "/CreateSpeech"
const listVoicesAPI = ivonaAPI + "/ListVoices"
// Ivona is used to invoke API calls
type Ivona struct {
AccessKey string
SecretKey string
}
// New returns a new Ivona client.
func New(accessKey string, secretKey string) *Ivona {
return &Ivona{AccessKey: accessKey, SecretKey: secretKey}
}
// CreateSpeech performs a synthesis of the requested text and returns the audio stream containing the speech.
func (client *Ivona) CreateSpeech(options SpeechOptions) (*SpeechResponse, error) {
b, err := json.Marshal(options)
if err != nil {
return nil, err
}
r, _ := http.NewRequest("POST", createSpeechAPI, bytes.NewReader(b))
r.Header.Set("Content-Type", "application/json")
awsClient := aws4.Client{Keys: &aws4.Keys{
AccessKey: client.AccessKey,
SecretKey: client.SecretKey,
}}
resp, err := awsClient.Do(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Got non 200 status code: %s %q", resp.Status, data)
}
return &SpeechResponse{
Audio: data,
RequestID: resp.Header["X-Amzn-Ivonattsrequestid"][0],
ContentType: resp.Header["Content-Type"][0],
}, nil
}
// ListVoices retrieves list of voices from the api
func (client *Ivona) ListVoices(options Voice) (*ListResponse, error) {
b, err := json.Marshal(options)
if err != nil {
return nil, err
}
r, _ := http.NewRequest("POST", listVoicesAPI, bytes.NewReader(b))
r.Header.Set("Content-Type", "application/json")
awsClient := aws4.Client{Keys: &aws4.Keys{
AccessKey: client.AccessKey,
SecretKey: client.SecretKey,
}}
resp, err := awsClient.Do(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Got non 200 status code: %s %q", resp.Status, data)
}
list := new(ListResponse)
err = json.Unmarshal(data, list)
if err != nil {
return nil, err
}
list.RequestID = resp.Header["X-Amzn-Ivonattsrequestid"][0]
list.ContentType = resp.Header["Content-Type"][0]
return list, nil
}