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/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 ¶
const ( SampleRate = audiopkg.SampleRate Channels = audiopkg.Channels BitsPerSample = audiopkg.BitsPerSample BytesPerSample = audiopkg.BytesPerSample )
Canonical capture PCM format: 16 kHz, 16-bit signed, mono.
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 ¶
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).
var ( ErrUnsupportedBackend = capture.ErrUnsupportedBackend ErrUnsupportedSource = capture.ErrUnsupportedSource )
Sentinel errors re-exported for callers that branch on them.
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).
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.
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 Player ¶
type Player struct {
// contains filtered or unexported fields
}
Player plays audio through the system's default output device.
func NewPlayer ¶
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) 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 ¶
PlayMP3 decodes and plays MP3 audio data. Blocks until playback completes or Stop() is called.
func (*Player) PlayPCM ¶
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.
type PooledPCMHandler ¶ added in v0.40.1
type PooledPCMHandler = capture.PooledPCMHandler
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)