forked from kamushadenes/elevenlabs-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
178 lines (149 loc) · 5.14 KB
/
examples_test.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package elevenlabs_test
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
"github.com/haguro/elevenlabs-go"
)
func ExampleClient_TextToSpeech() {
// Create a new client
client := elevenlabs.NewClient(context.Background(), "your-api-key", 30*time.Second)
// Create a TextToSpeechRequest
ttsReq := elevenlabs.TextToSpeechRequest{
Text: "Hello, world! My name is Adam, nice to meet you!",
ModelID: "eleven_monolingual_v1",
}
// Call the TextToSpeech method on the client, using the "Adam"'s voice ID.
audio, err := client.TextToSpeech("pNInz6obpgDQGcFmaJgB", ttsReq)
if err != nil {
log.Fatal(err)
}
// Write the audio file bytes to disk
if err := os.WriteFile("adam.mp3", audio, 0644); err != nil {
log.Fatal(err)
}
log.Println("Successfully generated audio file")
}
func ExampleClient_TextToSpeechStream() {
message := `The concept of "flushing" typically applies to I/O buffers in many programming
languages, which store data temporarily in memory before writing it to a more permanent location
like a file or a network connection. Flushing the buffer means writing all the buffered data
immediately, even if the buffer isn't full.`
// Set your API key
elevenlabs.SetAPIKey("your-api-key")
// Set a large enough timeout to ensure the stream is not interrupted.
elevenlabs.SetTimeout(1 * time.Minute)
// We'll use mpv to play the audio from the stream piped to standard input
cmd := exec.CommandContext(context.Background(), "mpv", "--no-cache", "--no-terminal", "--", "fd://0")
// Get a pipe connected to the mpv's standard input
pipe, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
// Attempt to run the command in a separate process
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
// Stream the audio to the pipe connected to mpv's standard input
if err := elevenlabs.TextToSpeechStream(
pipe,
"pNInz6obpgDQGcFmaJgB",
elevenlabs.TextToSpeechRequest{
Text: message,
ModelID: "eleven_multilingual_v1",
}); err != nil {
log.Fatalf("Got %T error: %q\n", err, err)
}
// Close the pipe when all stream has been copied to the pipe
if err := pipe.Close(); err != nil {
log.Fatalf("Could not close pipe: %s", err)
}
log.Print("Streaming finished.")
// Wait for mpv to exit. With the pipe closed, it will do that as
// soon as it finishes playing
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
log.Print("All done.")
}
func ExampleClient_TextToSpeechInputStream() {
message := `The concept of "flushing" typically applies to I/O buffers in many programming
languages, which store data temporarily in memory before writing it to a more permanent location
like a file or a network connection. Flushing the buffer means writing all the buffered data
immediately, even if the buffer isn't full.`
// Set your API key
elevenlabs.SetAPIKey("your-api-key")
// Set a large enough timeout to ensure the stream is not interrupted.
elevenlabs.SetTimeout(1 * time.Minute)
// We'll use mpv to play the audio from the stream piped to standard input
cmd := exec.CommandContext(context.Background(), "mpv", "--no-cache", "--no-terminal", "--", "fd://0")
// Get a pipe connected to the mpv's standard input
pipe, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
// Attempt to run the command in a separate process
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
// Stream the audio to the pipe connected to mpv's standard input
if err := elevenlabs.TextToSpeechInputStream(
strings.NewReader(message),
pipe,
"pNInz6obpgDQGcFmaJgB",
"eleven_multilingual_v1",
elevenlabs.TextToSpeechInputStreamingRequest{
Text: " ",
TryTriggerGeneration: true,
GenerationConfig: &elevenlabs.GenerationConfig{
ChunkLengthSchedule: []int{50},
},
}); err != nil {
log.Fatalf("Got %T error: %q\n", err, err)
}
// Close the pipe when all stream has been copied to the pipe
if err := pipe.Close(); err != nil {
log.Fatalf("Could not close pipe: %s", err)
}
log.Print("Streaming finished.")
// Wait for mpv to exit. With the pipe closed, it will do that as
// soon as it finishes playing
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
log.Print("All done.")
}
func ExampleClient_GetHistory() {
// Define a helper function to print history items
printHistory := func(r elevenlabs.GetHistoryResponse, p int) {
fmt.Printf("--Page %d--\n", p)
for i, h := range r.History {
t := time.Unix(int64(h.DateUnix), 0)
fmt.Printf("%d. %s - %s: %d bytes\n", p+i, t.Format("2006-01-02 15:04:05"), h.HistoryItemId, len(h.Text))
}
}
// Create a new client
client := elevenlabs.NewClient(context.Background(), "your-api-key", 30*time.Second)
// Get and print the first page (5 items).
page := 1
historyResp, nextPage, err := client.GetHistory(elevenlabs.PageSize(5))
if err != nil {
log.Fatal(err)
}
printHistory(historyResp, page)
// Get all other pages
for nextPage != nil {
page++
// Retrieve the next page. The page size from the original call is retained but
// can be overwritten by passing a call to PageSize with the new size.
historyResp, nextPage, err = nextPage()
if err != nil {
log.Fatal(err)
}
printHistory(historyResp, page)
}
}