mirror of
https://github.com/taigrr/elevenlabs.git
synced 2026-04-02 03:08:57 -07:00
update to add test code
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
const apiEndpoint = "https://api.elevenlabs.io"
|
const apiEndpoint = "https://api.elevenlabs.io"
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
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, voiceID string, options types.SynthesisOptions) error {
|
||||||
|
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,
|
||||||
@@ -55,7 +56,8 @@ func (c Client) TTSWriter(ctx context.Context, w io.Writer, text, voiceID string
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) TTS(ctx context.Context, w io.Writer, text, voiceID string, options types.SynthesisOptions) ([]byte, error) {
|
func (c Client) TTS(ctx context.Context, text, voiceID string, options types.SynthesisOptions) ([]byte, error) {
|
||||||
|
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{
|
||||||
@@ -101,6 +103,7 @@ func (c Client) TTS(ctx context.Context, w io.Writer, text, voiceID string, opti
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) TTSStream(ctx context.Context, w io.Writer, text, voiceID string, options types.SynthesisOptions) error {
|
func (c Client) TTSStream(ctx context.Context, w io.Writer, text, voiceID string, options types.SynthesisOptions) error {
|
||||||
|
options.Clamp()
|
||||||
url := fmt.Sprintf(c.endpoint+"/v1/text-to-speech/%s/stream", voiceID)
|
url := fmt.Sprintf(c.endpoint+"/v1/text-to-speech/%s/stream", voiceID)
|
||||||
opts := types.TTS{
|
opts := types.TTS{
|
||||||
Text: text,
|
Text: text,
|
||||||
|
|||||||
@@ -23,6 +23,15 @@ type TTS struct {
|
|||||||
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.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (so *SynthesisOptions) Clamp() {
|
||||||
|
if so.Stability > 1 || so.Stability < 0 {
|
||||||
|
so.Stability = 0.75
|
||||||
|
}
|
||||||
|
if so.SimilarityBoost > 1 || so.SimilarityBoost < 0 {
|
||||||
|
so.SimilarityBoost = 0.75
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type SynthesisOptions struct {
|
type SynthesisOptions struct {
|
||||||
Stability float64 `json:"stability"`
|
Stability float64 `json:"stability"`
|
||||||
SimilarityBoost float64 `json:"similarity_boost"`
|
SimilarityBoost float64 `json:"similarity_boost"`
|
||||||
|
|||||||
2
cmd/say/.gitignore
vendored
Normal file
2
cmd/say/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
*.mp3
|
||||||
|
main
|
||||||
@@ -1,4 +1,27 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/taigrr/elevenlabs/client"
|
||||||
|
"github.com/taigrr/elevenlabs/client/types"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
ctx := context.Background()
|
||||||
|
client := client.New(os.Getenv("XI_API_KEY"))
|
||||||
|
ids, err := client.GetVoiceIDs(ctx)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
saveFile, err := os.Create("sample.mp3")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer saveFile.Close()
|
||||||
|
err = client.TTSWriter(ctx, saveFile, "hello, golang", ids[0], types.SynthesisOptions{Stability: 0.75, SimilarityBoost: 0.75})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user