mirror of
https://github.com/taigrr/elevenlabs.git
synced 2026-04-02 03:08:57 -07:00
add example usage to readme
This commit is contained in:
51
README.md
51
README.md
@@ -16,3 +16,54 @@ After creating your account, you can get you API key [from here](https://help.el
|
||||
|
||||
|
||||
To use this library, create a new client and send a TTS request to a voice.
|
||||
The following code block illustrates how one might replicate the say/espeak
|
||||
command, using the streaming endpoint.
|
||||
```go
|
||||
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()
|
||||
client := client.New(os.Getenv("XI_API_KEY"))
|
||||
ids, err := client.GetVoiceIDs(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
pipeReader, pipeWriter := io.Pipe()
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
text, _ := reader.ReadString('\n')
|
||||
go func() {
|
||||
err = client.TTSStream(ctx, pipeWriter, text, ids[0], types.SynthesisOptions{Stability: 0.75, SimilarityBoost: 0.75})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
pipeWriter.Close()
|
||||
}()
|
||||
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)
|
||||
speaker.Play(beep.Seq(streamer, beep.Callback(func() {
|
||||
done <- true
|
||||
})))
|
||||
<-done
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user