playback

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToneNames

func ToneNames() []string

ToneNames returns a sorted list of all registered tone names.

Types

type CadenceSegment

type CadenceSegment struct {
	DurationMs int
	On         bool
}

CadenceSegment defines a segment of a tone cadence pattern. On=true means tone is playing, On=false means silence.

type Player

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

Player manages audio playback to an io.Writer (PCM stream).

func NewPlayer

func NewPlayer(log *slog.Logger) *Player

func (*Player) IsPlaying

func (p *Player) IsPlaying() bool

func (*Player) OnStart

func (p *Player) OnStart(fn func())

OnStart registers a callback that fires once when audio streaming begins, after a successful fetch and format detection. Not called on fetch errors.

func (*Player) Play

func (p *Player) Play(ctx context.Context, writer io.Writer, url string, mimeType string, repeat int) error

Play fetches audio from url and streams 16kHz mono 16-bit PCM to writer. WAV files are parsed and resampled/downmixed as needed. Supports PCM, mu-law, and A-law encoded WAV files. repeat controls looping: 0 or 1 = play once, -1 = infinite, N > 1 = N times. Blocks until playback completes or is stopped.

func (*Player) PlayAt8kHz

func (p *Player) PlayAt8kHz(ctx context.Context, writer io.Writer, url string, mimeType string, repeat int) error

PlayAt8kHz fetches audio and streams 8kHz mono 16-bit PCM to writer. Used for direct leg playback where the leg expects 8kHz PCM. repeat controls looping: 0 or 1 = play once, -1 = infinite, N > 1 = N times.

func (*Player) PlayAtRate

func (p *Player) PlayAtRate(ctx context.Context, writer io.Writer, url, mimeType string, targetRate uint32, repeat int) error

PlayAtRate fetches audio and streams mono 16-bit PCM at the given target rate. Used for direct leg playback at the leg's native sample rate.

func (*Player) PlayReader

func (p *Player) PlayReader(ctx context.Context, writer io.Writer, reader io.Reader, mimeType string) error

PlayReader streams audio from an io.Reader at 16kHz (default mixer rate). Unlike Play, it does not fetch from a URL and plays only once.

func (*Player) PlayReaderAt8kHz

func (p *Player) PlayReaderAt8kHz(ctx context.Context, writer io.Writer, reader io.Reader, mimeType string) error

PlayReaderAt8kHz streams audio from an io.Reader at 8kHz (leg native rate). Unlike PlayAt8kHz, it does not fetch from a URL and plays only once.

func (*Player) PlayReaderAtRate

func (p *Player) PlayReaderAtRate(ctx context.Context, writer io.Writer, reader io.Reader, mimeType string, targetRate uint32) error

PlayReaderAtRate streams audio from an io.Reader at the given target rate. Used for direct leg playback at the leg's native sample rate.

func (*Player) SetVolume

func (p *Player) SetVolume(v int)

SetVolume sets the playback volume level. Range: -8 (quietest) to 8 (loudest). 0 means no change. Each unit is ~3dB. Safe to call during active playback.

func (*Player) Stop

func (p *Player) Stop()

type ToneReader

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

ToneReader generates infinite cadenced PCM from a ToneSpec. It implements io.Reader producing 16-bit signed little-endian mono PCM.

func NewToneReader

func NewToneReader(spec ToneSpec, sampleRate int) *ToneReader

NewToneReader creates a ToneReader for the given spec at the given sample rate.

func (*ToneReader) Read

func (tr *ToneReader) Read(p []byte) (int, error)

Read fills p with 16-bit LE mono PCM. Never returns EOF.

type ToneSpec

type ToneSpec struct {
	Frequencies  []float64        // base frequencies to mix (e.g. 440, 480)
	ModulationHz float64          // amplitude modulation frequency (0 = none)
	Cadence      []CadenceSegment // on/off pattern; nil or empty = continuous
}

ToneSpec defines a telephone tone: frequencies, optional AM modulation, and cadence.

func LookupTone

func LookupTone(name string) (ToneSpec, bool)

LookupTone finds a tone by name. Accepts "country_type" (e.g. "us_ringback") or bare "type" (e.g. "ringback") which defaults to US. Also accepts common aliases (e.g. "uk_ringback" → "gb_ringback").

Jump to

Keyboard shortcuts

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