audio

package
v0.68.2 Latest Latest
Warning

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

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

Documentation

Overview

Package audio holds the reference app's private audio playback layer (oto/malgo players for TTS and voice-agent output) plus compatibility re-exports: PCM helpers forward to pkg/speechkit/audio (pcm_compat.go) and the capture layer forwards to pkg/speechkit/audio/capture (shim.go), so existing call sites keep using the audio.* names unchanged. New capture code goes in the public capture package.

Audio playback via ebitengine/oto only requires cgo on Linux (ALSA/PulseAudio); the Windows and Darwin backends are pure-Go via purego. The build tag lets plain-Go cross-compiles for Linux skip this file, which is relevant when developer machines don't have a Linux C toolchain. Production server builds (Dockerfile.server) enable cgo, so this file compiles in — even though the Server-Target never plays audio locally, transitive imports from internal/stt must stay safe.

The capture layer (backend registry, capture Session, device enumeration, frame pool) moved to the public pkg/speechkit/audio/capture package. This shim re-exports that surface so the existing device-app call sites (cmd/speechkit, internal/meeting, internal/wakeword, cmd/sk-*-smoke) keep compiling unchanged. New capture code goes in pkg/speechkit/audio/capture; playback (player.go, stream_player.go) stays private in this package.

Streaming audio player — see player.go for the rationale behind the build tag.

Index

Constants

View Source
const (
	SampleRate     = audiopkg.SampleRate
	Channels       = audiopkg.Channels
	BitsPerSample  = audiopkg.BitsPerSample
	BytesPerSample = audiopkg.BytesPerSample
)

Canonical capture PCM format: 16 kHz, 16-bit signed, mono.

View Source
const (
	BackendAuto                = capture.BackendAuto
	BackendWindowsWASAPIMalgo  = capture.BackendWindowsWASAPIMalgo
	BackendWindowsWASAPINative = capture.BackendWindowsWASAPINative

	InputSourceMicrophone     = capture.InputSourceMicrophone
	InputSourceSystemLoopback = capture.InputSourceSystemLoopback
	InputSourceMicAndSystem   = capture.InputSourceMicAndSystem

	EventStarted = capture.EventStarted
	EventStopped = capture.EventStopped
	EventWarning = capture.EventWarning
	EventError   = capture.EventError
	EventOverrun = capture.EventOverrun
	EventStalled = capture.EventStalled

	DefaultFrameCapacity = capture.DefaultFrameCapacity
)

Variables

View Source
var (
	PCMToWAV        = audiopkg.PCMToWAV
	PCMDurationSecs = audiopkg.PCMDurationSecs
	PCMLevel        = audiopkg.PCMLevel
)

PCM helpers (function values so const/var callers and the cgo capture code keep resolving audio.PCMToWAV etc. without edits).

View Source
var (
	ErrUnsupportedBackend      = capture.ErrUnsupportedBackend
	ErrBackendUnavailable      = capture.ErrBackendUnavailable
	ErrUnsupportedSource       = capture.ErrUnsupportedSource
	ErrOutputDeviceUnavailable = capture.ErrOutputDeviceUnavailable
)

Sentinel errors re-exported for callers that branch on them.

View Source
var (
	RegisterBackend       = capture.RegisterBackend
	Open                  = capture.Open
	NewCapturer           = capture.NewCapturer
	NewCapturerWithConfig = capture.NewCapturerWithConfig
	ListCaptureDevices    = capture.ListCaptureDevices
	ListOutputDevices     = capture.ListOutputDevices
	Get                   = capture.Get
	Put                   = capture.Put
)

Functions and constructors (function-value aliases keep signatures in sync).

View Source
var DefaultFramePool = &capture.DefaultFramePool

DefaultFramePool forwards to the capture package's pool so counters and recycled buffers stay shared with the capture backends. Declared as a pointer (Go cannot alias vars) — method calls read identically at the call sites.

View Source
var OnCaptureDeviceRebound func(oldID, newID, name string)

OnCaptureDeviceRebound mirrors capture.OnCaptureDeviceRebound for existing assignment sites (cmd/speechkit assigns audio.OnCaptureDeviceRebound). Go cannot alias vars, so the capture package's hook is wired once at init to forward to whatever this var currently holds.

Functions

This section is empty.

Types

type Backend

type Backend = capture.Backend

Core capture contracts.

type Capturer

type Capturer = capture.Capturer

Core capture contracts.

type Config

type Config = capture.Config

Core capture contracts.

type DeviceInfo

type DeviceInfo = capture.DeviceInfo

Core capture contracts.

type Event

type Event = capture.Event

Core capture contracts.

type EventType

type EventType = capture.EventType

Core capture contracts.

type Factory

type Factory = capture.Factory

Core capture contracts.

type FramePool added in v0.40.1

type FramePool = capture.FramePool

Core capture contracts.

type InputSource added in v0.48.0

type InputSource = capture.InputSource

Core capture contracts.

type Player

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

Player plays audio through the system's default output device.

func NewPlayer

func NewPlayer() (*Player, error)

NewPlayer creates an audio player for TTS output. Call once at app startup; reuse for all playback.

func (*Player) Close

func (p *Player) Close()

Close releases audio resources. Call on app shutdown.

func (*Player) IsPlaying

func (p *Player) IsPlaying() bool

IsPlaying returns true if audio is currently being played.

func (*Player) OnFinished

func (p *Player) OnFinished(fn func())

OnFinished sets a callback that fires when playback completes naturally (not when stopped via Stop()).

func (*Player) PlayMP3

func (p *Player) PlayMP3(ctx context.Context, data []byte) error

PlayMP3 decodes and plays MP3 audio data. Blocks until playback completes or Stop() is called.

func (*Player) PlayPCM

func (p *Player) PlayPCM(ctx context.Context, data []byte, sampleRate int) error

PlayPCM plays raw PCM audio (16-bit signed int, little-endian, mono). IMPORTANT: The oto context is initialized at 24kHz. Audio with a different sample rate will play at the wrong pitch/speed. Callers must resample to 24kHz before calling this method, or use PlayMP3 which handles decoding.

func (*Player) Stop

func (p *Player) Stop()

Stop immediately stops current playback (for barge-in support).

type PooledPCMHandler added in v0.40.1

type PooledPCMHandler = capture.PooledPCMHandler

Core capture contracts.

type Session

type Session = capture.Session

Core capture contracts.

type Stats added in v0.40.1

type Stats = capture.Stats

Core capture contracts.

type StreamPlayer added in v0.18.0

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

StreamPlayer plays a continuous stream of PCM audio chunks through a single playback backend. Unlike Player.PlayPCM which stops previous playback on each call, StreamPlayer buffers chunks and plays them sequentially. Designed for real-time voice agent audio output (Gemini Live, OpenAI Realtime).

func NewStreamPlayer added in v0.18.0

func NewStreamPlayer() (*StreamPlayer, error)

NewStreamPlayer creates a StreamPlayer using the system default output device.

func NewStreamPlayerWithOutputDevice added in v0.22.1

func NewStreamPlayerWithOutputDevice(outputDeviceID string) (*StreamPlayer, error)

NewStreamPlayerWithOutputDevice creates a StreamPlayer for the selected output device. An empty device ID uses the system default output device.

func (*StreamPlayer) Close added in v0.18.0

func (sp *StreamPlayer) Close()

func (*StreamPlayer) IsActive added in v0.18.0

func (sp *StreamPlayer) IsActive() bool

func (*StreamPlayer) Start added in v0.18.0

func (sp *StreamPlayer) Start(ctx context.Context)

func (*StreamPlayer) StopAndDrain added in v0.18.0

func (sp *StreamPlayer) StopAndDrain()

func (*StreamPlayer) WriteChunk added in v0.18.0

func (sp *StreamPlayer) WriteChunk(chunk []byte)

Jump to

Keyboard shortcuts

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