audio

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package audio defines bounded, transport-free audio device observation and caller-selected capture or playback. It opens no device, process, or connection itself: a backend such as the PulseAudio or ALSA adapter implements the interfaces, and the consumer selects the device and grants access.

Index

Constants

View Source
const (
	// DefaultMaxDevices bounds one observation.
	DefaultMaxDevices = 64
	// MaxDevices is the library ceiling for MaxDevices.
	MaxDevices = 1024
	// DefaultMaxPacketBytes bounds one read or written PCM packet.
	DefaultMaxPacketBytes = 192_000
	// MaxPacketBytes is the library ceiling for MaxPacketBytes.
	MaxPacketBytes = 1 << 20
)

Variables

View Source
var (
	// ErrInvalid reports a malformed device, snapshot, or packet.
	ErrInvalid = errors.New("invalid audio value")
	// ErrLimit reports a value beyond a caller-selected bound.
	ErrLimit = errors.New("audio limit exceeded")
	// ErrUnavailable reports a backend that cannot observe or open the device.
	ErrUnavailable = errors.New("audio backend is unavailable")
)

Functions

func Code added in v0.4.0

func Code(err error) string

Code is the stable error code of err for another language to match: invalid, too_large or unavailable. It is empty for an error this package does not define.

func ValidatePacket

func ValidatePacket(packet media.PCM16, limits Limits) error

ValidatePacket checks PCM format, alignment, and the caller's packet ceiling: a packet with no data, a rate outside 8 to 192 kHz, 1 to 8 channels or a size that is not whole frames is invalid, and one over the ceiling is too large. The format is judged first.

func ValidateSnapshot

func ValidateSnapshot(snapshot Snapshot, limits Limits) error

ValidateSnapshot checks identity, kind, format, and device bounds.

Types

type CaptureBackend

type CaptureBackend interface {
	OpenCapture(context.Context, string, Limits) (Source, error)
}

CaptureBackend opens a bounded source for one caller-selected device.

type Device

type Device struct {
	ID           string `json:"id"`
	Name         string `json:"name"`
	Kind         Kind   `json:"kind"`
	State        string `json:"state,omitempty"`
	SampleFormat string `json:"sample_format,omitempty"`
	SampleRate   int    `json:"sample_rate,omitempty"`
	Channels     int    `json:"channels,omitempty"`
}

Device is one observed endpoint. ID is backend-local and opaque to the library; Name is display metadata. Sample values are zero when the backend does not know them.

func Select

func Select(snapshot Snapshot, kind Kind, id string) (Device, error)

Select returns the first device of a kind, or the explicitly named one. It never falls back to a different kind or to a default device.

type Kind

type Kind string

Kind classifies a device without exposing a native endpoint or routing rule.

const (
	KindOutput  Kind = "output"
	KindInput   Kind = "input"
	KindMonitor Kind = "monitor"
)

type Limits

type Limits struct {
	MaxDevices     int `json:"max_devices"`
	MaxPacketBytes int `json:"max_packet_bytes"`
}

Limits bound an observation and one PCM packet. Zero values select defaults.

func (Limits) Normalize

func (l Limits) Normalize() (Limits, error)

type Observer

type Observer interface {
	Observe(context.Context, Limits) (Snapshot, error)
}

Observer reports caller-visible local audio devices. It sends no audio and starts no stream.

type ObserverFunc

type ObserverFunc func(context.Context, Limits) (Snapshot, error)

func (ObserverFunc) Observe

func (f ObserverFunc) Observe(ctx context.Context, limits Limits) (Snapshot, error)

type PlaybackBackend

type PlaybackBackend interface {
	OpenPlayback(context.Context, string, Limits) (Sink, error)
}

PlaybackBackend opens a bounded sink for one caller-selected device.

type Sink

type Sink interface {
	Write(context.Context, media.PCM16) error
	Close() error
}

Sink consumes bounded PCM16 packets and reports backpressure through Write.

type Snapshot

type Snapshot struct {
	Backend    string    `json:"backend"`
	ObservedAt time.Time `json:"observed_at"`
	Devices    []Device  `json:"devices"`
	Status     Status    `json:"status"`
}

Snapshot is one bounded observation. Backend names the selected native mechanism for diagnostics; the library never chooses it.

type Source

type Source interface {
	Read(context.Context) (media.PCM16, error)
	Close() error
}

Source produces bounded PCM16 packets. Read must respect the limits supplied when the source was opened and must not block forever after context cancellation.

type Status

type Status struct {
	State  string `json:"state"`
	Reason string `json:"reason,omitempty"`
	Detail string `json:"detail,omitempty"`
}

Status distinguishes an empty device list from an unavailable backend. State is complete, partial or unsupported; an unsupported status says why in Reason, from the set every package shares (testdata/capability/v1): unsupported on this platform, or tool_missing when the native tool is not installed (the adapter that owns the tool reports that).

Jump to

Keyboard shortcuts

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