edgetts

package
v0.0.0-...-d6da613 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
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

func AdjustClockSkew(serverDate string) error

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

func GenerateSecMsGec(clientToken string) string

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

func (p *Provider) IsAvailable(ctx context.Context) bool

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

func (p *Provider) ListVoices(ctx context.Context, locale string) ([]tts.Voice, error)

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) Name

func (p *Provider) Name() string

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

func (p *Provider) Synthesize(ctx context.Context, opts *SynthesizeOptions) ([]byte, error)

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

func (p *Provider) WithClientToken(token string) *Provider

WithClientToken sets custom client token

func (*Provider) WithConnectTimeout

func (p *Provider) WithConnectTimeout(timeout time.Duration) *Provider

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

func (p *Provider) WithHTTPTimeout(timeout time.Duration) *Provider

WithHTTPTimeout sets HTTP client timeout

func (*Provider) WithMaxAttempts

func (p *Provider) WithMaxAttempts(attempts int) *Provider

WithMaxAttempts sets maximum retry attempts (including first try)

func (*Provider) WithProxy

func (p *Provider) WithProxy(proxyURL string) *Provider

WithProxy sets proxy URL

func (*Provider) WithReceiveTimeout

func (p *Provider) WithReceiveTimeout(timeout time.Duration) *Provider

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)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL