forked from openai/openai-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
47 lines (39 loc) · 1.03 KB
/
main.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
package main
import (
"context"
"time"
"github.com/ebitengine/oto/v3"
"github.com/openai/openai-go"
)
func main() {
client := openai.NewClient()
ctx := context.Background()
res, err := client.Audio.Speech.New(ctx, openai.AudioSpeechNewParams{
Model: openai.F(openai.SpeechModelTTS1),
Input: openai.String(`Why did the chicken cross the road? To get to the other side.`),
ResponseFormat: openai.F(openai.AudioSpeechNewParamsResponseFormatPCM),
Voice: openai.F(openai.AudioSpeechNewParamsVoiceAlloy),
})
defer res.Body.Close()
if err != nil {
panic(err)
}
op := &oto.NewContextOptions{}
op.SampleRate = 24000
op.ChannelCount = 1
op.Format = oto.FormatSignedInt16LE
otoCtx, readyChan, err := oto.NewContext(op)
if err != nil {
panic("oto.NewContext failed: " + err.Error())
}
<-readyChan
player := otoCtx.NewPlayer(res.Body)
player.Play()
for player.IsPlaying() {
time.Sleep(time.Millisecond)
}
err = player.Close()
if err != nil {
panic("player.Close failed: " + err.Error())
}
}