mirror of
https://github.com/taigrr/elevenlabs.git
synced 2026-04-17 10:45:09 -07:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b925ef1471 | ||
|
e095a7ec13
|
|||
| 32972d4ff2 | |||
| 84e59417d6 | |||
|
3e3b7004b8
|
@@ -1,7 +1,6 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -13,11 +12,12 @@ import (
|
|||||||
"github.com/taigrr/elevenlabs/client/types"
|
"github.com/taigrr/elevenlabs/client/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c Client) TTSWriter(ctx context.Context, w io.Writer, text, voiceID string, options types.SynthesisOptions) error {
|
func (c Client) TTSWriter(ctx context.Context, w io.Writer, text, modelID, voiceID string, options types.SynthesisOptions) error {
|
||||||
options.Clamp()
|
options.Clamp()
|
||||||
url := fmt.Sprintf(c.endpoint+"/v1/text-to-speech/%s", voiceID)
|
url := fmt.Sprintf(c.endpoint+"/v1/text-to-speech/%s", voiceID)
|
||||||
opts := types.TTS{
|
opts := types.TTS{
|
||||||
Text: text,
|
Text: text,
|
||||||
|
ModelID: modelID,
|
||||||
VoiceSettings: options,
|
VoiceSettings: options,
|
||||||
}
|
}
|
||||||
b, _ := json.Marshal(opts)
|
b, _ := json.Marshal(opts)
|
||||||
@@ -30,14 +30,13 @@ func (c Client) TTSWriter(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
|
||||||
@@ -56,12 +55,13 @@ func (c Client) TTSWriter(ctx context.Context, w io.Writer, text, voiceID string
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) TTS(ctx context.Context, text, voiceID string, options types.SynthesisOptions) ([]byte, error) {
|
func (c Client) TTS(ctx context.Context, text, voiceID, modelID string, options types.SynthesisOptions) ([]byte, error) {
|
||||||
options.Clamp()
|
options.Clamp()
|
||||||
url := fmt.Sprintf(c.endpoint+"/v1/text-to-speech/%s", voiceID)
|
url := fmt.Sprintf(c.endpoint+"/v1/text-to-speech/%s", voiceID)
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
opts := types.TTS{
|
opts := types.TTS{
|
||||||
Text: text,
|
Text: text,
|
||||||
|
ModelID: modelID,
|
||||||
VoiceSettings: options,
|
VoiceSettings: options,
|
||||||
}
|
}
|
||||||
b, _ := json.Marshal(opts)
|
b, _ := json.Marshal(opts)
|
||||||
@@ -73,19 +73,17 @@ func (c Client) TTS(ctx context.Context, text, voiceID string, options types.Syn
|
|||||||
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
|
||||||
@@ -119,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
|
||||||
|
|||||||
@@ -19,6 +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"`
|
||||||
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.
|
||||||
}
|
}
|
||||||
@@ -103,6 +104,22 @@ type LanguageResponseModel struct {
|
|||||||
IsoCode string `json:"iso_code"`
|
IsoCode string `json:"iso_code"`
|
||||||
DisplayName string `json:"display_name"`
|
DisplayName string `json:"display_name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Language struct {
|
||||||
|
LanguageID string `json:"language_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ModelResponseModel struct {
|
||||||
|
ModelID string `json:"model_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
CanBeFinetuned bool `json:"can_be_finetuned"`
|
||||||
|
CanDoTextToSpeech bool `json:"can_do_text_to_speech"`
|
||||||
|
CanDoVoiceConversion bool `json:"can_do_voice_conversion"`
|
||||||
|
TokenCostFactor float64 `json:"token_cost_factor"`
|
||||||
|
Languages []Language `json:"languages"`
|
||||||
|
}
|
||||||
type RecordingResponseModel struct {
|
type RecordingResponseModel struct {
|
||||||
RecordingID string `json:"recording_id"`
|
RecordingID string `json:"recording_id"`
|
||||||
MimeType string `json:"mime_type"`
|
MimeType string `json:"mime_type"`
|
||||||
|
|||||||
Reference in New Issue
Block a user