Skip to content

A Go API client library for the ElevenLabs speech synthesis platform (fork)

License

Notifications You must be signed in to change notification settings

clearlyip/elevenlabs-go

 
 

Repository files navigation

elevenlabs-go

Go version License Tests codecov Go Report Card Go Reference

This is a Go client library for the ElevenLabs voice cloning and speech synthesis platform. It provides a basic interface for Go programs to interact with the ElevenLabs API.

Forked from: github.com/haguro/elevenlabs-go

Installation

go get github.com/clearlyip/elevenlabs-go

Websocket doInputStreamingRequest (mod Jan 2025)

Using (Consumer side)

client := elevenlabs.NewClient(d.localContext, d.config.ApiKey, 1*time.Minute)
		err := client.TextToSpeechInputStream(
			d.InputChan,  // chan string
			d.ResponseChan, // chan StreamingOutputResponse
			d.config.Voice, // Voice ID
			d.config.Model, // TTS model
			elevenlabs.TextToSpeechInputStreamingRequest{
				Text:                 " ",
				TryTriggerGeneration: true,
				VoiceSettings: &elevenlabs.VoiceSettings{
					Stability:       d.config.ElevenlabsStability,
					SimilarityBoost: d.config.ElevenlabsSimilarityBoost,
					Style:           d.config.ElevenlabsStyle,
				},
			},
			elevenlabs.OutputFormat(d.codec))

		if err != nil {
			d.logger.Errorw("🧨 ElevenLabs: Got error", err)
			continue
		}

Responses

These are declared within the elevenlabs driver, for example: elevenlabs.StreamingOutputResponse

type StreamingOutputResponse struct {
	Audio               []byte                    `json:"audio"`
	IsFinal             bool                      `json:"isFinal"`
	NormalizedAlignment StreamingAlignmentSegment `json:"normalizedAlignment"`
	Alignment           StreamingAlignmentSegment `json:"alignment"`
}

type StreamingAlignmentSegment struct {
	CharStartTimesMs []int    `json:"charStartTimesMs"`
	CharDurationsMs  []int    `json:"charDurationsMs"`
	Chars            []string `json:"chars"`
}

Define a channel to handle these responses:

var FromElevenLabsChannel  chan elevenlabs.StreamingOutputResponse

Create the channel and assign it:

FromElevenLabsChannel = make(chan elevenlabs.StreamingOutputResponse)

Watch for responses:

for {
	select {
	case <-d.localContext.Done():
		d.log.Debug("ElevenLabs: Exiting output buffer handling loop via localContext.Done()")
		return
	case ttsMsg := <-FromElevenLabsChannel:
		// handle the response
}

About

A Go API client library for the ElevenLabs speech synthesis platform (fork)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%