这是 ElevenLabs 语音克隆和语音合成平台的 Go 客户端库。它为 Go 程序提供了与 ElevenLabs API 交互的基本接口。
go get github.com/haguro/elevenlabs-go
确保将所有示例中的"your-api-key"
替换为您的实际 API 密钥。有关更多详细信息,请参阅官方 Elevenlabs API 文档。
该库的完整文档可在此处找到。
使用NewClient
函数返回一个新的Client
实例将允许传递父上下文、您的 API 密钥和超时持续时间。
package main
import (
"context"
"log"
"os"
"time"
"github.com/haguro/elevenlabs-go"
)
func main () {
// 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" )
}
该库有一个默认客户端,您可以配置并与代理函数一起使用,这些代理函数将方法调用包装到默认客户端。默认客户端的默认超时设置为 30 秒,并使用context.Background()
配置为父上下文。在利用默认客户端时,您只需要至少设置 API 密钥。这是上面示例的仅使用简写函数的版本。
package main
import (
"log"
"os"
"time"
el "github.com/haguro/elevenlabs-go"
)
func main () {
// Set your API key
el . SetAPIKey ( "your-api-key" )
// Set a different timeout (optional)
el . SetTimeout ( 15 * time . Second )
// Call the TextToSpeech method on the client, using the "Adam"'s voice ID.
audio , err := el . TextToSpeech ( "pNInz6obpgDQGcFmaJgB" , el. TextToSpeechRequest {
Text : "Hello, world! My name is Adam, nice to meet you!" ,
ModelID : "eleven_monolingual_v1" ,
})
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" )
}
Elevenlabs API 允许“在生成音频时”传输音频。在elevenlabs-go中,您需要将io.Writer
传递给TextToSpeechStream
方法,流将被连续复制到该方法。请注意,您需要将客户端超时设置为足够高的值,以确保请求不会在中途超时。
package main
import (
"context"
"log"
"os/exec"
"time"
"github.com/haguro/elevenlabs-go"
)
func main () {
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." )
}
截至撰写本文时(2023 年 6 月 24 日),该库为 Elevenlabs 100% 的 API 方法提供 Go 绑定。如果有一些需要或足够的要求,我确实计划添加更多实用程序类型的功能。
据 Elevenlabs 称,该 API 仍被认为是实验性的,可能会发生变化。
欢迎贡献!如果您有任何想法、改进或错误修复,请提出问题或提交拉取请求。
Elevenlabs 的官方 Python 库非常出色,鼓励 Python 爱好者使用它(也可以尝试一下 Go?)!
这是一个独立项目,不隶属于 Elevenlabs,也不受其认可。 Elevenlabs 及其商标是其各自所有者的财产。该项目的目的是提供一个客户端库,以便于访问 Go 程序中 Elevenlabs 提供的公共 API。本项目中对 Elevenlabs 商标的任何使用仅用于识别目的,并不意味着认可、赞助或从属关系。
该项目已获得 MIT 许可证的许可。
该代码库“按原样”提供,不提供任何保证。使用风险自负。许可证文件中有更多详细信息。