From 0b0d613b62a31ec934dc21120ac22a3642181686 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 18 Apr 2023 14:56:02 -0700 Subject: [PATCH] attempt tts stream, needs checking --- client/tts.go | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/client/tts.go b/client/tts.go index 2efd197..d822576 100644 --- a/client/tts.go +++ b/client/tts.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "net/http" - "strings" "github.com/taigrr/elevenlabs/client/types" ) @@ -101,8 +100,44 @@ func (c Client) TTS(ctx context.Context, w io.Writer, text, voiceID string, opti } } -func (c Client) TextToSpeechV1TextToSpeechVoiceIdStreamPost(ctx context.Context, w io.Writer, voiceId string, options types.SynthesisOptions) error { - localVarHttpMethod = strings.ToUpper("Post") +func (c Client) TextToSpeechV1TextToSpeechVoiceIdStreamPost(ctx context.Context, w io.Writer, text, voiceID string, options types.SynthesisOptions) error { + url := fmt.Sprintf(c.endpoint+"/v1/text-to-speech/%s/stream", voiceID) + opts := types.TTS{ + Text: text, + VoiceSettings: options, + } + b, _ := json.Marshal(opts) + client := &http.Client{} + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(b)) + if err != nil { + return err + } + req.Header.Set("xi-api-key", c.apiKey) + req.Header.Set("User-Agent", "github.com/taigrr/elevenlabs") + req.Header.Set("accept", "audio/mpeg") + res, err := client.Do(req) - localVarPath := a.client.cfg.BasePath + "/v1/text-to-speech/{voice_id}/stream" + switch res.StatusCode { + case 422: + ve := types.ValidationError{} + defer res.Body.Close() + jerr := json.NewDecoder(res.Body).Decode(&ve) + if jerr != nil { + err = errors.Join(err, jerr) + } else { + err = errors.Join(err, ve) + } + return err + case 401: + return ErrUnauthorized + case 200: + if err != nil { + return err + } + defer res.Body.Close() + io.Copy(w, res.Body) + return nil + default: + return errors.Join(err, ErrUnspecified) + } }