attempt tts stream, needs checking

This commit is contained in:
2023-04-18 14:56:02 -07:00
parent 281aa1275b
commit 0b0d613b62

View File

@@ -9,7 +9,6 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"strings"
"github.com/taigrr/elevenlabs/client/types" "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 { func (c Client) TextToSpeechV1TextToSpeechVoiceIdStreamPost(ctx context.Context, w io.Writer, text, voiceID string, options types.SynthesisOptions) error {
localVarHttpMethod = strings.ToUpper("Post") url := fmt.Sprintf(c.endpoint+"/v1/text-to-speech/%s/stream", voiceID)
opts := types.TTS{
localVarPath := a.client.cfg.BasePath + "/v1/text-to-speech/{voice_id}/stream" 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)
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)
}
} }