audio

package
v0.14.9 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SampleRate     = 16000
	Channels       = 1
	BitsPerSample  = 16
	BytesPerSample = BitsPerSample / 8
)

Variables

View Source
var (
	ErrUnsupportedBackend = errors.New("unsupported audio backend")
	ErrBackendUnavailable = errors.New("audio backend unavailable in this build")
)

Functions

func PCMDurationSecs

func PCMDurationSecs(pcm []byte) float64

PCMDurationSecs returns the duration of PCM audio in seconds.

func PCMLevel

func PCMLevel(pcm []byte) float64

PCMLevel estimates a normalized RMS level from 16-bit PCM samples.

func PCMToWAV

func PCMToWAV(pcm []byte) []byte

PCMToWAV wraps raw 16kHz S16 Mono PCM data in a WAV header.

func RegisterBackend

func RegisterBackend(name Backend, factory Factory) error

Types

type Backend

type Backend string
const (
	BackendAuto                Backend = "auto"
	BackendWindowsWASAPIMalgo  Backend = "windows-wasapi-malgo"
	BackendWindowsWASAPINative Backend = "windows-wasapi-native"
)

type Capturer

type Capturer = Session

Capturer is kept as an alias while the app migrates to the session terminology.

func NewCapturer

func NewCapturer() (Capturer, error)

func NewCapturerWithConfig

func NewCapturerWithConfig(cfg Config) (Capturer, error)

type Config

type Config struct {
	Backend     Backend
	DeviceID    string
	SampleRate  int
	Channels    int
	FrameSizeMs int
	LatencyHint string
}

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.

type Event

type Event struct {
	Type    EventType
	Backend Backend
	Message string
	Err     error
}

type EventType

type EventType string
const (
	EventStarted EventType = "started"
	EventStopped EventType = "stopped"
	EventWarning EventType = "warning"
	EventError   EventType = "error"
)

type Factory

type Factory func(Config) (Session, error)

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 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.

func Open

func Open(cfg Config) (Session, error)

Jump to

Keyboard shortcuts

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