-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaudio_speech.go
56 lines (48 loc) · 1.19 KB
/
audio_speech.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
package coze
import (
"context"
"io"
"net/http"
"os"
)
func (r *audioSpeech) Create(ctx context.Context, req *CreateAudioSpeechReq) (*CreateAudioSpeechResp, error) {
uri := "/v1/audio/speech"
resp, err := r.core.RawRequest(ctx, http.MethodPost, uri, req)
if err != nil {
return nil, err
}
res := &CreateAudioSpeechResp{
Data: resp.Body,
}
res.SetHTTPResponse(newHTTPResponse(resp))
return res, nil
}
type audioSpeech struct {
core *core
}
func newSpeech(core *core) *audioSpeech {
return &audioSpeech{core: core}
}
// CreateAudioSpeechReq represents the request for creating speech
type CreateAudioSpeechReq struct {
Input string `json:"input"`
VoiceID string `json:"voice_id"`
ResponseFormat *AudioFormat `json:"response_format"`
Speed *float32 `json:"speed"`
}
// CreateAudioSpeechResp represents the response for creating speech
type CreateAudioSpeechResp struct {
baseResponse
// TODO 没有 json tag?
Data io.ReadCloser
}
func (c *CreateAudioSpeechResp) WriteToFile(path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
defer c.Data.Close()
_, err = io.Copy(file, c.Data)
return err
}