-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtts.go
66 lines (59 loc) · 1.42 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
package openai
import (
"encoding/json"
"io"
"net/http"
"github.com/Simplou/goxios"
)
const (
alloy = "alloy"
echo = "echo"
fable = "fable"
onyx = "onyx"
nova = "nova"
shimmer = "shimmer"
)
var SpeechVoices = &openaiSpeechVoices{
Alloy: alloy,
Echo: echo,
Fable: fable,
Onyx: onyx,
Nova: nova,
Shimmer: shimmer,
}
type (
// openaiSpeechVoices holds the available voices for OpenAI speech synthesis.
openaiSpeechVoices struct {
Alloy string
Echo string
Fable string
Onyx string
Nova string
Shimmer string
}
// SpeechRequestBody represents the request body for the speech API.
SpeechRequestBody struct {
Model string `json:"model"` // The model for speech synthesis.
Input string `json:"input"` // The input text for synthesis.
Voice string `json:"voice"` // The voice to be used for synthesis.
}
)
func TextToSpeech(api OpenAIClient, httpClient HTTPClient, body *SpeechRequestBody) (io.ReadCloser, *OpenAIErr) {
api.AddHeader(contentTypeJSON)
b, err := json.Marshal(body)
if err != nil {
return nil, NewOpenAIErr(err, 500, "marshal_json_error")
}
options := goxios.RequestOpts{
Headers: api.Headers(),
Body: ioReader(b),
}
res, err := httpClient.Post(api.BaseURL()+"/audio/speech", &options)
if res.StatusCode != http.StatusOK {
return nil, openaiHttpError(res)
}
if err != nil {
return nil, closeBody(res.Body, err)
}
return res.Body, nil
}