3 Commits

Author SHA1 Message Date
6fcc65115d add comments to the readme code 2023-04-19 14:02:09 -07:00
5451bcd0b1 fix readme and say 2023-04-19 14:00:03 -07:00
58581c3c46 add example binary to readme 2023-04-18 22:35:17 -07:00
2 changed files with 17 additions and 2 deletions

View File

@@ -18,7 +18,15 @@ make TTS (text-to-speech) requests to elevenlabs.io
As a prerequisite, you must already have an account with elevenlabs.io.
After creating your account, you can get you API key [from here](https://help.elevenlabs.io/hc/en-us/articles/14599447207697-How-to-authorize-yourself-using-your-xi-api-key-).
After creating your account, you can get your API key [from here](https://help.elevenlabs.io/hc/en-us/articles/14599447207697-How-to-authorize-yourself-using-your-xi-api-key-).
## Test Program
To test out an example `say` program, run:
`go install github.com/taigrr/elevenlabs/cmd/say@latest`
Set the `XI_API_KEY` environment variable, and pipe it some text to give it a whirl!
## Example Code
@@ -48,21 +56,26 @@ import (
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})
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)
@@ -70,6 +83,7 @@ func main() {
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
})))

View File

@@ -26,7 +26,8 @@ func main() {
pipeReader, pipeWriter := io.Pipe()
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
b, _ := io.ReadAll(reader)
text := string(b)
go func() {
err = client.TTSStream(ctx, pipeWriter, text, ids[0], types.SynthesisOptions{Stability: 0.75, SimilarityBoost: 0.75})