say

package
v1.223.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package say provides a cross-platform text-to-speech abstraction.

It mirrors the pkg/browser package: a small interface (Speaker) backed by per-OS detection of a TTS engine (macOS `say`, Linux `spd-say`/`espeak`, Windows PowerShell System.Speech), with a CommandRunner seam for testing.

When no engine is available — or the process is running in CI or a test — New returns a Speaker that invokes a caller-supplied fallback instead of emitting audio, so callers degrade gracefully on headless machines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListVoices

func ListVoices(info *SayInfo, runner CommandRunner) ([]string, error)

ListVoices enumerates the voices installed for the detected backend. Returns ErrVoiceListUnsupported for backends that cannot enumerate voices (e.g. spd-say, whose voice model is module-based rather than named).

Types

type Backend

type Backend int

Backend identifies a platform text-to-speech engine.

const (
	// BackendMacSay is the macOS `say` command.
	BackendMacSay Backend = iota
	// BackendSpdSay is the Linux Speech Dispatcher `spd-say` command.
	BackendSpdSay
	// BackendEspeak is the Linux `espeak`/`espeak-ng` command.
	BackendEspeak
	// BackendPowerShell is the Windows PowerShell System.Speech backend.
	BackendPowerShell
)

type CommandRunner

type CommandRunner interface {
	// Run executes the command and waits for it to complete.
	Run(name string, args ...string) error
	// Output executes the command, waits, and returns its standard output.
	Output(name string, args ...string) ([]byte, error)
}

CommandRunner executes external commands. Abstracted for testing.

type FallbackFunc

type FallbackFunc func(text string) error

FallbackFunc renders text when speech is unavailable.

type Option

type Option func(*config)

Option configures a Speaker.

func WithCommandRunner

func WithCommandRunner(runner CommandRunner) Option

WithCommandRunner sets a custom command runner (for testing).

func WithFallback

func WithFallback(fn FallbackFunc) Option

WithFallback sets the function used to render text when speech is unavailable.

func WithRate

func WithRate(rate string) Option

WithRate sets the speech rate: slow, normal, or fast.

func WithVoices

func WithVoices(voices []string) Option

WithVoices sets the ordered list of candidate voices. The first voice actually installed on the host is used (CSS font-family style); if none match, the backend's default voice is used.

type SayInfo

type SayInfo struct {
	// Path is the resolved path to the TTS executable.
	Path string
	// Backend identifies which TTS engine Path refers to.
	Backend Backend
}

SayInfo holds information about a detected text-to-speech backend.

func DetectSay

func DetectSay() (*SayInfo, error)

DetectSay finds a text-to-speech backend on the system. Returns ErrSayNotFound if no backend is available.

type Speaker

type Speaker interface {
	// Speak speaks the given text, or invokes the configured fallback when
	// speech is unavailable.
	Speak(text string) error
}

Speaker speaks text aloud (text-to-speech).

func New

func New(opts ...Option) Speaker

New returns a Speaker configured with the given options. When a TTS backend is available and the environment supports audio, it returns a speaker that emits speech; otherwise it returns a speaker that invokes the fallback.

Jump to

Keyboard shortcuts

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