Elevenlabs.io (11.ai) ไคลเอนต์เสียงที่ไม่เป็นทางการ
ห้องสมุดนี้ไม่ได้มีส่วนเกี่ยวข้องและไม่เกี่ยวข้องกับสิบเอ็ดคนในทางใดทางหนึ่ง
เอกสาร API อย่างเป็นทางการของ Elevenlabs ซึ่งลูกค้ารายนี้ได้รับมาที่นี่
ไคลเอนต์ Go นี้ให้อินเทอร์เฟซง่าย ๆ ในการสร้างเสียงสังเคราะห์และส่งคำขอ TTS (ข้อความเป็นคำพูด) ถึง Elevenlabs.io
ตามข้อกำหนดเบื้องต้นคุณต้องมีบัญชีกับ Elevenlabs.io อยู่แล้ว หลังจากสร้างบัญชีของคุณคุณสามารถรับคีย์ API ของคุณได้จากที่นี่
หากต้องการทดสอบตัวอย่างโปรแกรม say Run:
go install github.com/taigrr/elevenlabs/cmd/say@latest
ตั้งค่าตัวแปรสภาพแวดล้อม XI_API_KEY และส่งข้อความบางส่วนเพื่อให้มันหมุนวน!
หากต้องการใช้ไลบรารีนี้ให้สร้างไคลเอนต์ใหม่และส่งคำขอ TTS ไปยังเสียง บล็อกโค้ดต่อไปนี้แสดงให้เห็นถึงวิธีการทำซ้ำคำสั่ง Say/Espeak โดยใช้จุดสิ้นสุดการสตรีม ฉันเลือกที่จะไปกับแพ็คเกจบี๊บของ Faiface แต่คุณยังสามารถบันทึกไฟล์ลงใน MP3 on-disk
package main
import (
"bufio"
"context"
"io"
"log"
"os"
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
"github.com/taigrr/elevenlabs/client"
"github.com/taigrr/elevenlabs/client/types"
)
func main () {
ctx := context . Background ()
// load in an API key to create a client
client := client . New ( os . Getenv ( "XI_API_KEY" ))
// fetch a list of voice IDs from elevenlabs
ids , err := client . GetVoiceIDs ( ctx )
if err != nil {
panic ( err )
}
// prepare a pipe for streaming audio directly to beep
pipeReader , pipeWriter := io . Pipe ()
reader := bufio . NewReader ( os . Stdin )
text , _ := reader . ReadString ( 'n' )
go func () {
// stream audio from elevenlabs using the first voice we found
err = client . TTSStream ( ctx , pipeWriter , text , ids [ 0 ], types. SynthesisOptions { Stability : 0.75 , SimilarityBoost : 0.75 , Style : 0.0 , UseSpeakerBoost : true })
if err != nil {
panic ( err )
}
pipeWriter . Close ()
}()
// decode and prepare the streaming mp3 as it comes through
streamer , format , err := mp3 . Decode ( pipeReader )
if err != nil {
log . Fatal ( err )
}
defer streamer . Close ()
speaker . Init ( format . SampleRate , format . SampleRate . N ( time . Second / 10 ))
done := make ( chan bool )
// play the audio
speaker . Play ( beep . Seq ( streamer , beep . Callback ( func () {
done <- true
})))
<- done
}ตัวอย่างต่อไปนี้แสดงให้เห็นถึงวิธีการสร้างเอฟเฟกต์เสียงโดยใช้ API การสร้างเสียง:
package main
import (
"context"
"os"
"github.com/taigrr/elevenlabs/client"
)
func main () {
ctx := context . Background ()
// Create a new client with your API key
client := client . New ( os . Getenv ( "XI_API_KEY" ))
// Generate a sound effect and save it to a file
f , err := os . Create ( "footsteps.mp3" )
if err != nil {
panic ( err )
}
defer f . Close ()
// Basic usage (using default duration and prompt influence)
err = client . SoundGenerationWriter ( ctx , f , "footsteps on wooden floor" , 0 , 0 )
if err != nil {
panic ( err )
}
// Advanced usage with custom duration and prompt influence
audio , err := client . SoundGeneration (
ctx ,
"heavy rain on a tin roof" ,
5.0 , // Set duration to 5 seconds
0.5 , // Set prompt influence to 0.5
)
if err != nil {
panic ( err )
}
os . WriteFile ( "rain.mp3" , audio , 0644 )
}