Documentation
¶
Index ¶
- Constants
- func AdjustClockSkew(serverDate string) error
- func GenerateSecMsGec(clientToken string) string
- func ParseOutputFormat(format string) (tts.VoiceAudioProfile, bool)
- type Provider
- func (p *Provider) Close()
- func (p *Provider) FormatRegistry() *tts.FormatRegistry
- func (p *Provider) IsAvailable(ctx context.Context) bool
- func (p *Provider) ListVoices(ctx context.Context, locale string) ([]tts.Voice, error)
- func (p *Provider) Name() string
- func (p *Provider) OutputOptions() []tts.OutputOption
- func (p *Provider) SupportedFormats() []tts.OutputFormat
- func (p *Provider) Synthesize(ctx context.Context, opts *SynthesizeOptions) ([]byte, error)
- func (p *Provider) SynthesizeStream(ctx context.Context, opts *SynthesizeOptions) (tts.AudioStream, error)
- func (p *Provider) WithClientToken(token string) *Provider
- func (p *Provider) WithConnectTimeout(timeout time.Duration) *Provider
- func (p *Provider) WithFormatRegistry(r *tts.FormatRegistry) *Provider
- func (p *Provider) WithHTTPTimeout(timeout time.Duration) *Provider
- func (p *Provider) WithMaxAttempts(attempts int) *Provider
- func (p *Provider) WithProxy(proxyURL string) *Provider
- func (p *Provider) WithReceiveTimeout(timeout time.Duration) *Provider
- func (p *Provider) WithVoiceCache(opts ...tts.VoiceCacheOption) *Provider
- type SynthesizeOptions
- type VoiceExtra
Constants ¶
const ( OutputFormatMP3_24khz_48k = "audio-24khz-48kbitrate-mono-mp3" // Default, 48kbps OutputFormatMP3_24khz_96k = "audio-24khz-96kbitrate-mono-mp3" // 96kbps OutputFormatMP3_24khz_160k = "audio-24khz-160kbitrate-mono-mp3" // 160kbps OutputFormatMP3_48khz_192k = "audio-48khz-192kbitrate-mono-mp3" // 192kbps OutputFormatMP3_48khz_320k = "audio-48khz-320kbitrate-mono-mp3" // 320kbps (highest quality MP3) OutputFormatOpus_24khz = "audio-24khz-16bit-mono-opus" // Opus OutputFormatPCM_24khz = "raw-24khz-16bit-mono-pcm" // Raw PCM OutputFormatWebM_24khz = "webm-24khz-16bit-mono-opus" // WebM container OutputFormatOgg_24khz = "ogg-24khz-16bit-mono-opus" // Ogg container )
Common Edge TTS output formats
Variables ¶
This section is empty.
Functions ¶
func AdjustClockSkew ¶
AdjustClockSkew adjusts the clock skew based on server time. Called when a 403 error is received. Note: This uses additive adjustment like the Python reference implementation.
func GenerateSecMsGec ¶
GenerateSecMsGec generates the Sec-MS-GEC token value. Reference: https://github.com/rany2/edge-tts/blob/master/src/edge_tts/drm.py
func ParseOutputFormat ¶
func ParseOutputFormat(format string) (tts.VoiceAudioProfile, bool)
ParseOutputFormat parses an Edge TTS output format string into a VoiceAudioProfile. Format examples:
"audio-24khz-48kbitrate-mono-mp3" "audio-48khz-192kbitrate-mono-mp3" "audio-24khz-16bit-mono-opus" "audio-48khz-96kbitrate-mono-mp3" "webm-24khz-16bit-mono-opus" "ogg-24khz-16bit-mono-opus" "raw-24khz-16bit-mono-pcm" "riff-24khz-16bit-mono-pcm" "raw-16khz-16bit-mono-truesilk" "amr-wb-16000hz" "g722-16khz-64kbps"
Types ¶
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider Edge TTS provider
func New ¶
func New() *Provider
New creates Edge TTS provider with default settings Use With* methods to customize configuration:
provider := edgetts.New().
WithClientToken("your-token").
WithTimeout(30*time.Second).
WithMaxAttempts(5)
func (*Provider) Close ¶
func (p *Provider) Close()
Close releases resources held by the provider. If voice caching is enabled, its background goroutine (if any) is stopped. Close is safe to call multiple times.
func (*Provider) FormatRegistry ¶
func (p *Provider) FormatRegistry() *tts.FormatRegistry
FormatRegistry returns the provider's format registry.
func (*Provider) IsAvailable ¶
IsAvailable checks if the provider is available by attempting a WebSocket connection to the Edge TTS service.
To monitor availability continuously, wrap IsAvailable with tts.ProviderHealth:
p := edgetts.New()
health := tts.NewProviderHealth(func(ctx context.Context) bool {
return p.IsAvailable(ctx)
}, tts.WithCheckInterval(5*time.Minute))
health.Start(context.Background())
defer health.Stop()
if health.IsHealthy() { /* provider is reachable */ }
func (*Provider) ListVoices ¶
ListVoices lists available voices for the specified locale. When voice caching is enabled (via WithVoiceCache), the cached list is used and filtered by locale. Otherwise the list is fetched from the remote API.
func (*Provider) OutputOptions ¶
func (p *Provider) OutputOptions() []tts.OutputOption
OutputOptions 返回 EdgeTTS 支持的输出格式选项列表(按比特率从低到高排列)。
格式来源: 通过实际调用 Edge TTS WebSocket API 探测验证(2026-02-21)。 Edge TTS 注册表中共 37 个格式,其中 9 个验证为可用(FormatAvailable), 28 个验证为不可用(FormatUnavailable)。本方法仅返回推荐的 5 种常用格式。 完整的 9 个可用格式可通过 provider.FormatRegistry().Available() 获取。
如需重新验证:
go test -v -tags probe -run TestEdgeTTSFormatProbe ./providers/edgetts/
使用示例:
provider := edgetts.New()
for _, opt := range provider.OutputOptions() {
fmt.Printf("%-45s %s\n", opt.FormatID, opt.Label)
}
func (*Provider) SupportedFormats ¶
func (p *Provider) SupportedFormats() []tts.OutputFormat
SupportedFormats returns all formats verified as available in the registry.
func (*Provider) Synthesize ¶
Synthesize synthesizes text to speech
func (*Provider) SynthesizeStream ¶
func (p *Provider) SynthesizeStream(ctx context.Context, opts *SynthesizeOptions) (tts.AudioStream, error)
SynthesizeStream returns a streaming audio reader
func (*Provider) WithClientToken ¶
WithClientToken sets custom client token
func (*Provider) WithConnectTimeout ¶
WithConnectTimeout sets WebSocket connection timeout
func (*Provider) WithFormatRegistry ¶
func (p *Provider) WithFormatRegistry(r *tts.FormatRegistry) *Provider
WithFormatRegistry replaces the default format registry with a custom one. Useful for testing or providers that need isolated format state.
func (*Provider) WithHTTPTimeout ¶
WithHTTPTimeout sets HTTP client timeout
func (*Provider) WithMaxAttempts ¶
WithMaxAttempts sets maximum retry attempts (including first try)
func (*Provider) WithReceiveTimeout ¶
WithReceiveTimeout sets WebSocket receive timeout
func (*Provider) WithVoiceCache ¶
func (p *Provider) WithVoiceCache(opts ...tts.VoiceCacheOption) *Provider
WithVoiceCache enables voice list caching. The cache fetches the full voice list once and filters by locale on each ListVoices call. Pass VoiceCacheOption values (e.g. tts.WithTTL, tts.WithBackgroundRefresh) to customise cache behaviour.
type SynthesizeOptions ¶
type SynthesizeOptions struct {
Text string
Voice string
Rate float64 // Speech rate, 0.5-2.0, default 1.0
Volume float64 // Volume, 0.0-1.0, default 1.0
Pitch float64 // Pitch, 0.5-2.0, default 1.0
WordBoundaryEnabled bool
SentenceBoundaryEnabled bool
// OutputFormat Edge TTS 输出格式字符串,如 "audio-48khz-192kbitrate-mono-mp3"。
// 不设置时使用 Provider 默认格式(MP3 24kHz 48kbps)。
// 可通过 provider.OutputOptions() 查看所有可用的输出格式。
OutputFormat string
BoundaryCallback func(event tts.BoundaryEvent)
ProgressCallback func(completed, total int) // 合成进度回调:completed=已完成块数, total=总块数
BackgroundMusic *tts.BackgroundMusicOptions
}
SynthesizeOptions EdgeTTS synthesis options
type VoiceExtra ¶
type VoiceExtra struct {
ShortName string `json:"short_name"`
FriendlyName string `json:"friendly_name,omitempty"`
Locale string `json:"locale"`
SecondaryLocales []string `json:"secondary_locales,omitempty"`
Status string `json:"status,omitempty"` // "GA", "Preview", "Deprecated"
Styles []string `json:"styles,omitempty"`
Roles []string `json:"roles,omitempty"`
Categories []string `json:"categories,omitempty"`
Personalities []string `json:"personalities,omitempty"`
SuggestedCodec string `json:"suggested_codec,omitempty"`
}
VoiceExtra EdgeTTS voice extended information (reference edge-tts Voice structure)