-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyatts.go
112 lines (99 loc) · 3.33 KB
/
yatts.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
// The MIT License (MIT)
//
// Copyright (c) 2021 Amangeldy Kadyl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package yatts
import (
"context"
"fmt"
"github.com/lEx0/yatts/auth"
"github.com/lEx0/yatts/request"
"io"
"net/http"
)
type (
// TTS is the interface for text to speech
TTS interface {
Speak(ctx context.Context, entity request.TextEntity, options ...request.Option) (io.ReadCloser, error)
}
// YaTTS is implementation of TTS based on Yandex TTS
YaTTS struct {
auth auth.Authable
client *http.Client
url string
options []request.Option
}
)
// DefaultYandexTTSEndpointURL is the default endpoint for the TTS service
const DefaultYandexTTSEndpointURL = "https://tts.api.cloud.yandex.net/speech/v1/tts:synthesize"
// NewYaTTS creates a new YaTTS instance
func NewYaTTS(
authenticator auth.Authable,
client *http.Client,
options ...request.Option,
) *YaTTS {
if client == nil {
client = http.DefaultClient
}
return &YaTTS{
auth: authenticator,
client: client,
url: DefaultYandexTTSEndpointURL,
options: options,
}
}
// SetTTSEndpointURL sets the endpoint url for the TTS service.
func (y *YaTTS) SetTTSEndpointURL(url string) {
y.url = url
}
// Speak sends a request to the TTS endpoint and receives an audio stream.
func (y *YaTTS) Speak(ctx context.Context, entity request.TextEntity, options ...request.Option) (io.ReadCloser, error) {
if req, err := y.buildRequest(ctx, entity, options...); err != nil {
return nil, err
} else if resp, err := y.client.Do(req); err != nil {
return nil, err
} else if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
} else {
return resp.Body, nil
}
}
// build http.Request to Yandex TTS API
func (y *YaTTS) buildRequest(ctx context.Context, entity request.TextEntity, options ...request.Option) (*http.Request, error) {
r := request.NewRequest()
for _, option := range append(y.options, options...) {
if err := option(r); err != nil {
return nil, err
}
}
if err := entity.Process(r); err != nil {
return nil, err
}
if body, err := r.Body(); err != nil {
return nil, err
} else if req, err := http.NewRequestWithContext(
ctx, http.MethodPost, y.url, body,
); err != nil {
return nil, err
} else if err := y.auth.Do(req); err != nil {
return nil, err
} else {
return req, nil
}
}