GithubHelp home page GithubHelp logo

garlicgarrison / elevenlabs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from taigrr/elevenlabs

0.0 0.0 0.0 113 KB

ElevenLabs Artificial Voice Synthesis Client

License: BSD Zero Clause License

Go 100.00%

elevenlabs's Introduction

elevenlabs

License 0BSD GoDoc Go Mod Go Report Card

Unofficial elevenlabs.io (11.ai) voice synthesis client

This library is not affiliated with, nor associated with ElevenLabs in any way.

ElevenLabs' official api documentation, upon which this client has been derived, can be found here.

Purpose

This go client provides an easy interface to create synthesized voices and 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 your API key from here.

Test Program

To test out an example say program, run:

go install github.com/garlicgarrison/elevenlabs/cmd/say@latest

Set the XI_API_KEY environment variable, and pipe it some text to give it a whirl!

Example Code

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. I've opted to go with faiface's beep package, but you can also save the file to an 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/garlicgarrison/elevenlabs/client"
        "github.com/garlicgarrison/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})
                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
}

elevenlabs's People

Contributors

taigrr avatar garlicgarrison avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.