Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/speak.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ func inferFormatFromExt(path string) string {
return "mp3_44100_128"
case ".wav", ".wave":
return "pcm_44100"
case ".ogg", ".opus":
return "opus_48000_64"
default:
return ""
}
Expand Down
18 changes: 14 additions & 4 deletions internal/elevenlabs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,15 @@ func (c *Client) StreamTTS(ctx context.Context, voiceID string, payload TTSReque
return nil, err
}
u.Path = path.Join(u.Path, "/v1/text-to-speech", voiceID, "stream")
q := u.Query()
if latency > 0 {
q := u.Query()
q.Set("optimize_streaming_latency", fmt.Sprint(latency))
u.RawQuery = q.Encode()
}
if payload.OutputFormat != "" {
q.Set("output_format", payload.OutputFormat)
payload.OutputFormat = "" // don't send in body
}
u.RawQuery = q.Encode()

bodyBytes, err := json.Marshal(payload)
if err != nil {
Expand All @@ -229,7 +233,7 @@ func (c *Client) StreamTTS(ctx context.Context, voiceID string, payload TTSReque
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "audio/mpeg")
req.Header.Set("Accept", "*/*")
req.Header.Set("xi-api-key", c.apiKey)

resp, err := c.httpClient.Do(req)
Expand All @@ -253,6 +257,12 @@ func (c *Client) ConvertTTS(ctx context.Context, voiceID string, payload TTSRequ
return nil, err
}
u.Path = path.Join(u.Path, "/v1/text-to-speech", voiceID)
q := u.Query()
if payload.OutputFormat != "" {
q.Set("output_format", payload.OutputFormat)
payload.OutputFormat = ""
}
u.RawQuery = q.Encode()

bodyBytes, err := json.Marshal(payload)
if err != nil {
Expand All @@ -264,7 +274,7 @@ func (c *Client) ConvertTTS(ctx context.Context, voiceID string, payload TTSRequ
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "audio/mpeg")
req.Header.Set("Accept", "*/*")
req.Header.Set("xi-api-key", c.apiKey)

resp, err := c.httpClient.Do(req)
Expand Down