deepgram-go-text-to-speech
Sử dụng khi viết hoặc xem xét mã Go trong repo này để tổng hợp âm thanh với Speak v1 REST hoặc Speak WebSockets. Chuyển hướng công việc phiên âm đến…
npx skills add https://github.com/deepgram/deepgram-go-sdk --skill deepgram-go-text-to-speechUsing Deepgram Text-to-Speech from the Go SDK
When to use this product
Use this skill for pkg/client/speak work:
- file or stream synthesis over REST
- low-latency synthesis over WebSockets
- callback-based or channel-based audio playback pipelines
Use a different skill when:
- you need STT (
deepgram-go-speech-to-text) - you need live voice-agent orchestration (
deepgram-go-voice-agent) - you need repo workflow guidance (
deepgram-go-maintaining-sdk)
Authentication
Set DEEPGRAM_API_KEY before creating Speak clients.
export DEEPGRAM_API_KEY="your_api_key"
Use the repo's env-backed client defaults instead of embedding secrets in code.
Quick start
REST synthesis to file:
package main
import (
"context"
"log"
api "github.com/deepgram/deepgram-go-sdk/v3/pkg/api/speak/v1/rest"
speak "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/speak"
interfaces "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/interfaces"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
ctx := context.Background()
client := speak.NewRESTWithDefaults()
dg := api.New(client)
if _, err := dg.ToSave(
ctx,
"hello.wav",
"Hello from the Deepgram Go SDK.",
&interfaces.SpeakOptions{Model: "aura-2-thalia-en"},
); err != nil {
return err
}
return nil
}
Streaming synthesis with callbacks or channels:
package main
import (
"context"
"fmt"
"log"
speakws "github.com/deepgram/deepgram-go-sdk/v3/pkg/api/speak/v1/websocket"
speak "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/speak"
interfaces "github.com/deepgram/deepgram-go-sdk/v3/pkg/client/interfaces"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
ctx := context.Background()
handler := speakws.NewDefaultChanHandler()
conn, err := speak.NewWSUsingChanWithDefaults(
ctx,
&interfaces.WSSpeakOptions{Model: "aura-2-thalia-en"},
handler,
)
if err != nil {
return err
}
defer conn.Stop()
if ok := conn.Connect(); !ok {
return fmt.Errorf("connect failed")
}
conn.Start()
if err := conn.SpeakWithText("Streaming TTS from Go."); err != nil {
return err
}
// The handler receives binary audio and flow-control events.
if err := conn.Flush(); err != nil {
return err
}
return nil
}
Key parameters
interfaces.SpeakOptions- typical fields:
Model,Encoding,Container,SampleRate
- typical fields:
interfaces.WSSpeakOptions- typical fields:
Model, streaming audio format settings
- typical fields:
- REST methods
- via
pkg/api/speak/v1/rest:api.New(client).ToStream,ToFile,ToSave
- via
- WS methods
SpeakWithText,Speak,Flush,Reset
- constructors
speak.NewRESTWithDefaults()/speak.NewREST(...)speak.NewWSUsingCallback...speak.NewWSUsingChan...
API reference (layered)
- In-repo reference
README.mddocs.gopkg/client/speak/client.gopkg/client/speak/v1/rest/client.gopkg/client/speak/v1/websocket/client_callback.gopkg/client/speak/v1/websocket/client_channel.gopkg/client/interfaces/v1/types-speak.go
- OpenAPI
https://developers.deepgram.com/openapi.yaml
- AsyncAPI
https://developers.deepgram.com/asyncapi.yaml
- Context7
/llmstxt/developers_deepgram_llms_txt
- Product docs
https://developers.deepgram.com/reference/text-to-speech/speak-requesthttps://developers.deepgram.com/reference/text-to-speech/speak-streaminghttps://developers.deepgram.com/docs/tts-models
Gotchas
- REST and WebSocket Speak clients use different option structs.
- REST methods live on
pkg/api/speak/v1/rest; build them withapi.New(client). - WebSocket flows use a handler passed at construction,
Connect()returnsbool, and shutdown isStop(). - Keep the playback or message-consumer goroutine running while audio frames arrive.
- Follow the examples for file handling and output format selection instead of guessing encodings.
Example files in this repo
examples/text-to-speech/rest/file/hello-world/main.goexamples/text-to-speech/websocket/simple_channel/main.goexamples/text-to-speech/websocket/simple_callback/main.go
Central product skills
For cross-language Deepgram product knowledge — the consolidated API reference, documentation finder, focused runnable recipes, third-party integration examples, and MCP setup — install the central skills:
npx skills add deepgram/skills
This SDK ships language-idiomatic code skills; deepgram/skills ships cross-language product knowledge (see api, docs, recipes, examples, starters, setup-mcp).