playback

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 12 Imported by: 1 module, 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 playback turns a Provider of interleaved float32 frames into sound through a named backend.

A backend is added by writing one file that implements Device plus an init that calls Register; the registry and this file never change. The first backend is oto; a second one (malgo) is expected to arrive the same way.

The package models a push audio API as a pull: the backend owns the thread that asks for PCM, and the adapter in this package hides that inversion so every Device sees the same Provider contract as the rest of the engine.

Index

Constants

This section is empty.

Variables

View Source
var Default = NewRegistry()

Default is the registry populated by the init functions in this package.

View Source
var ErrAudioInit = errors.New("playback/oto: audio init failed")

ErrAudioInit means the audio driver could not be brought up. It wraps the driver's own error so a caller can log the real cause (no server, no permission) instead of a generic failure.

View Source
var ErrClosed = errors.New("playback: device is closed")

ErrClosed is returned by every device operation after Close. Close itself stays callable and returns nil, so shutdown is idempotent.

View Source
var ErrNotOpen = errors.New("playback: device is not open")

ErrNotOpen means a device was started or reopened before Open succeeded.

View Source
var ErrOpen = errors.New("playback: device is already open")

ErrOpen means Open was called on a device that is already open. A Device is a single-output resource; opening it twice would silently drop the first Provider.

View Source
var ErrUnknownBackend = errors.New("playback: unknown backend")

ErrUnknownBackend means no backend was registered under the requested name.

View Source
var ErrUnsupportedFormat = errors.New("playback: device format must be 48000 Hz stereo float32")

ErrUnsupportedFormat means the caller asked for a layout the backend cannot produce. The engine's device format is fixed at 48 kHz stereo float32, so a mismatch is a bug above this layer, not something to resample here.

Functions

func Names

func Names() []string

Names lets a caller enumerate backends without constructing or opening any of them, which matters once a backend like malgo can be compiled in but refuse at runtime. It includes dev-only backends, so it is the list a CLI flag or a validation check should use.

func Register

func Register(name string, factory func() Device)

Register adds a backend to the process-wide Default registry. It panics on an empty or duplicate name: a shadowed backend is a bug that would otherwise only surface as the wrong sound card in production.

func RegisterDev

func RegisterDev(name string, factory func() Device)

RegisterDev adds a development-only backend to the process-wide registry. See Registry.RegisterDev for what that hides.

func UserNames

func UserNames() []string

UserNames lists the backends a UI should offer. It is Names without the dev-only entries, so a fake or null sink cannot appear as a real output.

Types

type Device

type Device interface {
	// Open binds the device to a format and a Provider. It must be called
	// before Start.
	Open(f core.FrameFormat, p Provider) error
	// Start begins consuming the Provider. It is safe on a device that is
	// already started.
	Start() error
	// Pause stops playback and input consumption. It may be called from a
	// goroutine other than the one that called Start.
	Pause() error
	// Resume continues after Pause. It is a no-op on a device that is not
	// started.
	Resume() error
	// Flush discards audio the backend has queued but not yet played, without
	// changing whether it is playing. It exists because Pause alone keeps that
	// queued audio: resuming would replay up to a buffer of the old position,
	// which is exactly what a seek or a track change must not do. A device that
	// is not open, or has nothing queued, treats it as a no-op.
	Flush() error
	// Close releases the sink. It is idempotent and safe to call from any
	// goroutine, including while audio is playing.
	Close() error
	// Latency reports how much audio the backend has accepted but not yet
	// played. It is a real queue measurement, not a fixed device figure.
	Latency() time.Duration
	// Err reports a failure that happened on the backend's own thread since
	// the last call. A nil error means the device is still healthy; reaching
	// the end of the Provider is not an error.
	Err() error
}

Device is one open output. It lives across tracks because the format is fixed, which is why Open is separate from Start: the sink is prepared once and fed many streams.

Err is the one method a pull-only Device would not need. A push backend reads the Provider on its own thread, so a decode failure or a dead audio server has no call site to be returned from; without Err the engine can only go silent. It must stay cheap and safe to poll.

func Open

func Open(name string) (Device, error)

Open resolves a backend by name. The returned Device is constructed but not opened, so the caller still chooses the format and Provider.

type FakeDevice

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

FakeDevice is the test implementation of Device. The exported type is deliberate: tests outside this package need to reach SetPace and the counters without a type assertion on an unexported name.

func NewFakeDevice

func NewFakeDevice() *FakeDevice

NewFakeDevice returns a quiet fake. The backend factory uses it, so playback.Open("fake") hands out a device that needs no writer.

func NewFakeDeviceWithWriter

func NewFakeDeviceWithWriter(w io.Writer) *FakeDevice

NewFakeDeviceWithWriter returns a fake that prints one line per consumed block: index, frames, and that block's peak and RMS in dBFS. It is the "print the changes instead of hearing them" mode for manually inspecting a pipeline without a card.

func (*FakeDevice) Blocks

func (d *FakeDevice) Blocks() int64

Blocks reports how many Provider reads have completed.

func (*FakeDevice) Close

func (d *FakeDevice) Close() error

Close releases the device. It is idempotent and safe from any goroutine, including while audio is playing. It signals the read loop to stop and waits for it to actually exit before returning, so the Provider and the writer are not touched again afterwards. A Provider that never returns would make the wait unbounded, matching oto, whose Close also waits out the in-flight read.

func (*FakeDevice) Err

func (d *FakeDevice) Err() error

Err surfaces a failure from the read loop. The Provider's error has no synchronous call site, so this poll is the only way it reaches a caller. End of stream is not an error.

func (*FakeDevice) Flush

func (d *FakeDevice) Flush() error

Flush is a no-op. Flush exists to drop audio a backend has queued but not yet played; the fake queues nothing, because each Provider pull is counted and discarded immediately, so there is never anything left to discard.

func (*FakeDevice) Frames

func (d *FakeDevice) Frames() int64

Frames reports how many frames the device has consumed. It is monotonic across Pause and Resume and is used by tests to observe progress.

func (*FakeDevice) Latency

func (d *FakeDevice) Latency() time.Duration

Latency is zero: the fake counts and drops every frame in the same step, so there is never audio accepted but not yet played.

func (*FakeDevice) Open

func (d *FakeDevice) Open(f core.FrameFormat, p Provider) error

Open binds the device to a format and a Provider. The format check mirrors oto: the engine only ever opens the canonical layout, so anything else is a bug above this layer and is rejected rather than resampled.

func (*FakeDevice) Pause

func (d *FakeDevice) Pause() error

Pause stops playback and stops consuming the Provider. The read loop is parked rather than cancelled, so the one read already in flight finishes and is accounted for before consumption halts. A device that was never started stays open and is left alone.

func (*FakeDevice) Resume

func (d *FakeDevice) Resume() error

Resume continues after Pause. On an open-but-never-started device it is a no-op rather than an implicit Start, matching oto.

func (*FakeDevice) SetPace

func (d *FakeDevice) SetPace(p time.Duration)

SetPace sets the wall-clock duration of one block. Zero disables the wait so tests run at full speed; the default paces reads at real time so a long stream does not finish instantly.

func (*FakeDevice) Start

func (d *FakeDevice) Start() error

Start begins consuming the Provider. It is a no-op on a device that is already playing, and it never restarts one that has reached end of stream: Start's contract is to not skip audio, not to replay from the beginning.

type Provider

type Provider interface {
	// ReadFrames fills dst with whole interleaved frames. It blocks while the
	// stream is live but has nothing buffered, and returns io.EOF once the
	// last frame has been delivered.
	ReadFrames(dst []float32) (frames int, err error)
}

Provider is the pull side of the engine: it hands out interleaved float32 frames at the device format. stream.Streamer.ReadFrames matches this exactly, which is why this package never imports stream and a *stream.Streamer is accepted structurally.

type Registry

type Registry interface {
	Register(name string, factory func() Device)

	// RegisterDev adds a backend that exists for development and testing only.
	// It is openable by name and accepted by validation, so a test can select
	// it, but it is left out of UserNames so a UI never offers it as a real
	// output device.
	RegisterDev(name string, factory func() Device)

	// Open resolves a name and constructs a fresh, unopened Device.
	Open(name string) (Device, error)

	// Names lists every registered backend, sorted. It is the engine's view: a
	// name here can be opened and passes validation.
	Names() []string

	// UserNames lists the backends a UI should offer, sorted. It is Names
	// minus the dev-only entries, so a null or fake sink never reaches a
	// dropdown a listener sees.
	UserNames() []string
}

Registry maps backend names to factories. It mirrors decode.Registry so a second backend is one file plus one init, with no edit to this one.

func NewRegistry

func NewRegistry() Registry

NewRegistry returns an empty registry. The package-level Default is what backends register into; this exists for tests and isolated embedders.

Jump to

Keyboard shortcuts

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