Documentation
¶
Overview ¶
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.
Streaming audio player — see player.go for the rationale behind the build tag.
Index ¶
- Constants
- Variables
- func PCMDurationSecs(pcm []byte) float64
- func PCMLevel(pcm []byte) float64
- func PCMToWAV(pcm []byte) []byte
- func RegisterBackend(name Backend, factory Factory) error
- type Backend
- type Capturer
- type Config
- type DeviceInfo
- type Event
- type EventType
- type Factory
- type Player
- type Session
- type StreamPlayer
Constants ¶
const ( SampleRate = 16000 Channels = 1 BitsPerSample = 16 BytesPerSample = BitsPerSample / 8 )
Variables ¶
var ( ErrUnsupportedBackend = errors.New("unsupported audio backend") )
Functions ¶
func PCMDurationSecs ¶
PCMDurationSecs returns the duration of PCM audio in seconds.
func RegisterBackend ¶
Types ¶
type Capturer ¶
type Capturer = Session
Capturer is kept as an alias while the app migrates to the session terminology.
func NewCapturer ¶
func NewCapturerWithConfig ¶
type DeviceInfo ¶
type DeviceInfo struct {
ID string `json:"deviceId"`
Name string `json:"label"`
IsDefault bool `json:"isDefault"`
}
DeviceInfo describes a capture device that can be presented to the user.
func ListCaptureDevices ¶
func ListCaptureDevices(cfg Config) ([]DeviceInfo, error)
ListCaptureDevices returns the available microphone devices for the selected backend.
func ListOutputDevices ¶ added in v0.22.1
func ListOutputDevices(cfg Config) ([]DeviceInfo, error)
ListOutputDevices returns the available speaker devices for the selected backend.
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 Session ¶
type Session interface {
Start() error
Stop() ([]byte, error)
IsRunning() bool
Events() <-chan Event
SetLevelHandler(func(float64))
SetPCMHandler(func([]byte))
Close() error
}
Session records microphone PCM and exposes both level and live-audio callbacks.
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)