這是 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 許可證的許可。
該程式碼庫「按原樣」提供,不提供任何保證。使用風險自負。許可證文件中有更多詳細資訊。