tts

package
v0.35.13 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package tts implements the SpeechKit text-to-speech surface: a small provider interface plus concrete adapters for OpenAI, Google, and Hugging Face. The router in this file picks a provider per request based on Strategy (CloudFirst, LocalFirst) and degrades gracefully when a provider is unavailable.

All providers must go through github.com/kombifyio/SpeechKit/internal/netsec for outbound HTTP. STT has a parallel routing layer in package router; this package is TTS-only.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Google

type Google struct {
	BaseURL    string
	Validation netsec.ValidationOptions
	// contains filtered or unexported fields
}

Google implements Provider using the Google Cloud Text-to-Speech API.

BaseURL is configurable for testing. It is validated against Validation on every request. Default Validation is strict (public https only).

func NewGoogle

func NewGoogle(opts GoogleOpts) *Google

NewGoogle creates a Google Cloud TTS provider.

func (*Google) Health

func (g *Google) Health(ctx context.Context) error

func (*Google) Name

func (g *Google) Name() string

func (*Google) Synthesize

func (g *Google) Synthesize(ctx context.Context, text string, opts SynthesizeOpts) (*Result, error)

type GoogleOpts

type GoogleOpts struct {
	APIKey string
	Voice  string // e.g. "de-DE-Neural2-B", "en-US-Neural2-J"
}

GoogleOpts configures the Google TTS provider.

type HuggingFace

type HuggingFace struct {
	BaseURL    string
	Validation netsec.ValidationOptions
	// contains filtered or unexported fields
}

HuggingFace implements Provider using the HuggingFace Inference API with text-to-speech models (e.g. parler-tts).

BaseURL is configurable for testing. It is validated against Validation on every request. Default Validation is strict (public https only).

func NewHuggingFace

func NewHuggingFace(opts HuggingFaceOpts) *HuggingFace

NewHuggingFace creates a HuggingFace TTS provider.

func (*HuggingFace) Health

func (h *HuggingFace) Health(ctx context.Context) error

func (*HuggingFace) Name

func (h *HuggingFace) Name() string

func (*HuggingFace) Synthesize

func (h *HuggingFace) Synthesize(ctx context.Context, text string, opts SynthesizeOpts) (*Result, error)

type HuggingFaceOpts

type HuggingFaceOpts struct {
	Token string // HF API token
	Model string // Model ID, e.g. "parler-tts/parler-tts-mini-multilingual-v1.1"
}

HuggingFaceOpts configures the HuggingFace TTS provider.

type OpenAI

type OpenAI struct {
	BaseURL    string
	Validation netsec.ValidationOptions
	// contains filtered or unexported fields
}

OpenAI implements Provider using the OpenAI TTS API.

BaseURL is configurable for testing. It is validated against Validation on every request. Default Validation is strict (public https only).

func NewOpenAI

func NewOpenAI(opts OpenAIOpts) *OpenAI

NewOpenAI creates an OpenAI TTS provider.

func (*OpenAI) Health

func (o *OpenAI) Health(ctx context.Context) error

func (*OpenAI) Name

func (o *OpenAI) Name() string

func (*OpenAI) Synthesize

func (o *OpenAI) Synthesize(ctx context.Context, text string, opts SynthesizeOpts) (*Result, error)

type OpenAIOpts

type OpenAIOpts struct {
	APIKey string
	Model  string // "tts-1" or "tts-1-hd"
	Voice  string // alloy, echo, fable, onyx, nova, shimmer
}

OpenAIOpts configures the OpenAI TTS provider.

type Provider

type Provider interface {
	// Synthesize converts text to audio bytes.
	Synthesize(ctx context.Context, text string, opts SynthesizeOpts) (*Result, error)

	// Name returns the provider identifier (e.g. "openai", "google", "kokoro").
	Name() string

	// Health checks if the provider is reachable and ready.
	Health(ctx context.Context) error
}

Provider defines the interface for all text-to-speech backends.

type Result

type Result struct {
	Audio      []byte
	Format     string        // Actual format of the audio data
	SampleRate int           // Sample rate in Hz (e.g. 24000)
	Duration   time.Duration // Estimated duration of the audio
	Provider   string
	Voice      string
}

Result holds the output of a TTS synthesis.

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router selects and falls back between TTS providers.

func NewRouter

func NewRouter(strategy Strategy, providers ...Provider) *Router

NewRouter creates a TTS router with the given strategy and providers. Providers are tried in order according to the strategy.

func (*Router) HealthCheck

func (r *Router) HealthCheck(ctx context.Context) map[string]error

HealthCheck returns health status for all providers.

func (*Router) SetProviders

func (r *Router) SetProviders(providers ...Provider)

SetProviders replaces the provider list (thread-safe for runtime reconfiguration).

func (*Router) Synthesize

func (r *Router) Synthesize(ctx context.Context, text string, opts SynthesizeOpts) (*Result, error)

Synthesize tries each provider in order until one succeeds.

type Strategy

type Strategy string

Strategy determines how the TTS router selects a provider.

const (
	StrategyCloudFirst Strategy = "cloud-first" // Default: try cloud providers first
	StrategyLocalFirst Strategy = "local-first" // Try local (Kokoro) first, cloud fallback
	StrategyCloudOnly  Strategy = "cloud-only"
	StrategyLocalOnly  Strategy = "local-only"
)

type SynthesizeOpts

type SynthesizeOpts struct {
	Locale string  // "de-DE", "en-US", "auto"
	Voice  string  // Provider-specific voice ID; empty = default
	Speed  float64 // 0.25 - 4.0, default 1.0
	Format string  // "wav", "mp3", "opus", "pcm"; default "mp3"
}

SynthesizeOpts configures a single TTS request.

Jump to

Keyboard shortcuts

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