3 Commits

Author SHA1 Message Date
fzqxzhang
261398509a feat: model_id tag add omitempty (#3) 2023-07-25 15:43:48 +00:00
Marcel Molina
b925ef1471 Fix compile error from variable typo (#2)
* Fix compile error from variable typo

* bytes.Buffer pointer
2023-07-09 16:00:45 +00:00
e095a7ec13 don't check resposne code before checcking err !=nil 2023-06-27 12:31:56 -07:00
2 changed files with 8 additions and 12 deletions

View File

@@ -1,7 +1,6 @@
package client package client
import ( import (
"bufio"
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
@@ -74,19 +73,17 @@ func (c Client) TTS(ctx context.Context, text, voiceID, modelID string, options
req.Header.Set("User-Agent", "github.com/taigrr/elevenlabs") req.Header.Set("User-Agent", "github.com/taigrr/elevenlabs")
req.Header.Set("accept", "audio/mpeg") req.Header.Set("accept", "audio/mpeg")
res, err := client.Do(req) res, err := client.Do(req)
if err != nil {
return []byte{}, err
}
switch res.StatusCode { switch res.StatusCode {
case 401: case 401:
return []byte{}, ErrUnauthorized return []byte{}, ErrUnauthorized
case 200: case 200:
if err != nil {
return []byte{}, err
}
b := bytes.Buffer{} b := bytes.Buffer{}
w := bufio.NewWriter(&b)
defer res.Body.Close() defer res.Body.Close()
io.Copy(w, res.Body) io.Copy(&b, res.Body)
return b.Bytes(), nil return b.Bytes(), nil
case 422: case 422:
fallthrough fallthrough
@@ -120,14 +117,13 @@ func (c Client) TTSStream(ctx context.Context, w io.Writer, text, voiceID string
req.Header.Set("User-Agent", "github.com/taigrr/elevenlabs") req.Header.Set("User-Agent", "github.com/taigrr/elevenlabs")
req.Header.Set("accept", "audio/mpeg") req.Header.Set("accept", "audio/mpeg")
res, err := client.Do(req) res, err := client.Do(req)
if err != nil {
return err
}
switch res.StatusCode { switch res.StatusCode {
case 401: case 401:
return ErrUnauthorized return ErrUnauthorized
case 200: case 200:
if err != nil {
return err
}
defer res.Body.Close() defer res.Body.Close()
io.Copy(w, res.Body) io.Copy(w, res.Body)
return nil return nil

View File

@@ -19,7 +19,7 @@ type Voice struct {
Labels string `json:"labels,omitempty"` // Serialized labels dictionary for the voice. Labels string `json:"labels,omitempty"` // Serialized labels dictionary for the voice.
} }
type TTS struct { type TTS struct {
ModelID string `json:"model_id"` ModelID string `json:"model_id,omitempty"`
Text string `json:"text"` // The text that will get converted into speech. Currently only English text is supported. Text string `json:"text"` // The text that will get converted into speech. Currently only English text is supported.
VoiceSettings SynthesisOptions `json:"voice_settings,omitempty"` // Voice settings are applied only on the given TTS request. VoiceSettings SynthesisOptions `json:"voice_settings,omitempty"` // Voice settings are applied only on the given TTS request.
} }