audioout

package
v0.1.0-alpha.6 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 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 audioout is the audio output layer of the browser: a mixer with a clocked output device behind it.

Everything that makes sound (HTMLMediaElement, Web Audio, WebRTC remote audio) opens a Stream, writes interleaved float32 PCM into it and reads the stream's Position as the master clock. The mixer converts every stream to the device format (48 kHz, stereo), applies per-stream and per-group volume and mute, and hands the sum to a Backend.

Backends: the real device (github.com/ebitengine/oto, pure Go on Linux, macOS and Windows; PulseAudio native protocol first, ALSA through purego as the fallback), a null sink that consumes audio in real time and discards it (used when no device exists: CI, containers, headless), and a test sink that records what was mixed. The null sink keeps the audio clock running at the right speed, so playback timing and A/V sync behave the same without a sound card.

See docs/audio-output.md for the contract.

Index

Constants

View Source
const (
	DeviceRate     = 48000
	DeviceChannels = 2
)

Device format. Every backend receives interleaved float32 frames in this format, so the mixer converts exactly once.

Variables

View Source
var (
	ErrClosed   = errors.New("audioout: stream closed")
	ErrFlushed  = errors.New("audioout: stream flushed while writing")
	ErrChannels = errors.New("audioout: channel count does not match the stream")
)

Errors returned by Stream.Write.

Functions

func NewRealtimeTestOutput

func NewRealtimeTestOutput() (*Output, *TestSink)

NewRealtimeTestOutput returns an Output over a recording sink that runs on the wall clock (for tests that exercise real timing, such as A/V sync).

func NewTestOutput

func NewTestOutput() (*Output, *TestSink)

NewTestOutput returns an Output over a manual TestSink.

func SetDefault

func SetDefault(o *Output)

SetDefault replaces the process-wide output (tests install a TestSink). The previous default is closed.

Types

type Backend

type Backend interface {
	// Name is a short label for logs and diagnostics ("oto", "null", "test").
	Name() string
	// Start begins pulling audio. pull fills dst (interleaved stereo float32
	// at DeviceRate) with exactly len(dst) samples and is called from one
	// goroutine at a time. It does not block for long.
	Start(pull func(dst []float32)) error
	// Latency is the estimated time between pull returning a frame and the
	// listener hearing it.
	Latency() time.Duration
	// Now is the backend's clock. Real backends return time.Now(); the test
	// sink returns its virtual time so tests are deterministic.
	Now() time.Time
	// Close stops the device. The mixer calls it at most once.
	Close() error
	// Real reports whether sound reaches a device (false for null and test).
	Real() bool
}

Backend is an audio device (or a stand-in) driven by the mixer.

func NewNullBackend

func NewNullBackend() Backend

NewNullBackend returns the real-time null sink.

type Group

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

Group is a set of streams that share a volume, a mute and an audible indicator: the browser keeps one per tab. Muting a group silences its streams but not their clocks, so a muted video keeps playing in sync.

func (*Group) Audible

func (g *Group) Audible() bool

Audible reports the current indicator.

func (*Group) Close

func (g *Group) Close()

Close detaches the group from the output. Its streams keep playing.

func (*Group) Muted

func (g *Group) Muted() bool

Muted reports the mute state.

func (*Group) Name

func (g *Group) Name() string

Name is the label given at creation.

func (*Group) OnAudible

func (g *Group) OnAudible(f func(audible bool))

OnAudible registers the function called (from the output's poll goroutine) when the group starts or stops making sound. A group is audible while one of its streams played a non-silent chunk within the last 1.5 s; muting does not change that (like a browser tab, it stays marked while muted).

func (*Group) SetMuted

func (g *Group) SetMuted(m bool)

SetMuted mutes or unmutes every stream of the group.

func (*Group) SetVolume

func (g *Group) SetVolume(v float64)

SetVolume sets the group gain (0..1).

type Output

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

Output is a mixer in front of one Backend. The zero value is not usable; use NewOutput or Default.

func Default

func Default() *Output

Default returns the process-wide output, creating it on first use. It uses the real device when one can be opened and the null sink otherwise; SWEEF_AUDIO=null forces the null sink. The result never is nil.

func NewOutput

func NewOutput(b Backend) (*Output, error)

NewOutput starts backend and returns the mixer driving it.

func (*Output) Backend

func (o *Output) Backend() Backend

Backend returns the device behind the mixer.

func (*Output) Close

func (o *Output) Close() error

Close stops the backend and closes every stream.

func (*Output) Latency

func (o *Output) Latency() time.Duration

Latency is the backend's output latency estimate.

func (*Output) NewGroup

func (o *Output) NewGroup(name string) *Group

NewGroup creates a mix group (one per tab): a volume, a mute and an "audible" indicator over all its streams.

func (*Output) Open

func (o *Output) Open(rate, channels int, group *Group) *Stream

Open creates a stream that accepts interleaved float32 PCM with the given sample rate and channel count. The stream starts paused: write a little audio, then call Play. group may be nil.

func (*Output) Poll

func (o *Output) Poll()

Poll updates every group's audible state and fires its callbacks. It runs from a timer; tests with a virtual clock call it directly.

func (*Output) Real

func (o *Output) Real() bool

Real reports whether sound reaches an audio device.

func (*Output) SetMasterMuted

func (o *Output) SetMasterMuted(m bool)

SetMasterMuted mutes the whole output.

func (*Output) SetMasterVolume

func (o *Output) SetMasterVolume(v float64)

SetMasterVolume sets the volume applied to the whole mix (0..1).

type Stream

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

Stream is one audio source in the mixer. Write feeds it; the mixer drains it at the device rate. All methods are safe for concurrent use.

The stream keeps a timeline measured in device frames. Every frame ever accepted by Write, minus the frames discarded by Flush, has an index on that timeline; Written is the index the next frame will get and Position is the index of the frame the listener hears right now. A frame with index i is audible once Position reaches i. A player that restarts at media time T after a Flush therefore anchors (T, Written) and reads T + (Position - anchor) as its clock.

func (*Stream) Buffered

func (s *Stream) Buffered() time.Duration

Buffered is the duration of audio queued but not yet consumed by the device.

func (*Stream) Close

func (s *Stream) Close()

Close removes the stream from the mixer and releases blocked writers.

func (*Stream) Done

func (s *Stream) Done() <-chan struct{}

Done is closed after End once every buffered frame has been consumed.

func (*Stream) End

func (s *Stream) End()

End marks the end of the input: once the buffer drains the stream stops consuming and Done is closed.

func (*Stream) Flush

func (s *Stream) Flush()

Flush discards buffered audio. The discarded frames leave the timeline, so Written afterwards equals the frames consumed so far and the next frame written is the next one the listener hears.

func (*Stream) Group

func (s *Stream) Group() *Group

Group returns the group the stream was opened in (may be nil).

func (*Stream) Latency

func (s *Stream) Latency() time.Duration

Latency is the delay between a frame leaving the mixer and being heard.

func (*Stream) Pause

func (s *Stream) Pause()

Pause stops consumption; buffered audio is kept.

func (*Stream) Play

func (s *Stream) Play()

Play starts (or resumes) consumption by the mixer.

func (*Stream) Playing

func (s *Stream) Playing() bool

Playing reports whether the stream is being consumed.

func (*Stream) Position

func (s *Stream) Position() time.Duration

Position is the timeline index of the frame the listener hears now: frames consumed by the device, interpolated between device callbacks and reduced by the output latency. It stands still while the stream is paused or starved and can be negative shortly after start (the first frame is not yet audible).

func (*Stream) SetMuted

func (s *Stream) SetMuted(m bool)

SetMuted mutes or unmutes the stream. A muted stream keeps its clock running.

func (*Stream) SetSpeed

func (s *Stream) SetSpeed(v float64)

SetSpeed sets the playback speed: input is consumed speed times faster than real time with the pitch changing accordingly (no time stretching). It applies to audio written after the call; callers that need it to take effect immediately Flush and rewrite from the current position.

func (*Stream) SetVolume

func (s *Stream) SetVolume(v float64)

SetVolume sets the stream gain (0..1).

func (*Stream) Underruns

func (s *Stream) Underruns() int64

Underruns counts mixer pulls that found the stream empty (or short) while it was playing and not ended.

func (*Stream) Write

func (s *Stream) Write(pcm []float32) (int, error)

Write converts interleaved PCM (the stream's channel count and rate) to the device format and queues it, blocking while the stream's buffer (two seconds) is full. It returns the number of input frames accepted. A Flush that happens while Write is blocked abandons the rest and returns ErrFlushed.

func (*Stream) WritePlanar

func (s *Stream) WritePlanar(ch [][]float32) (int, error)

WritePlanar is Write for one slice per channel (the layout decoders produce).

func (*Stream) Written

func (s *Stream) Written() time.Duration

Written is the timeline index the next written frame will get, as a duration since the stream opened.

type Suspender

type Suspender interface {
	Suspend()
	Resume()
}

Suspender is implemented by backends that can stop their device while nothing plays (so the desktop does not show the browser as an audio client forever and the device is not polled for silence).

type TestSink

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

TestSink is a backend that records everything the mixer produces. In manual mode (the default) time is virtual and advances only through Advance, so tests are deterministic and as fast as the CPU allows. With Realtime it paces itself on the wall clock like the null sink.

func NewTestSink

func NewTestSink() *TestSink

NewTestSink returns a manual-mode sink with a virtual clock and no latency. Use NewTestOutput for the common case.

func (*TestSink) Advance

func (t *TestSink) Advance(d time.Duration)

Advance runs the device for d of virtual time (manual mode): the mixer is pulled in 10 ms chunks and the virtual clock advances with each. Streams that are playing consume audio as they would on a device.

func (*TestSink) Close

func (t *TestSink) Close() error

func (*TestSink) FirstSound

func (t *TestSink) FirstSound(threshold float32) int

FirstSound returns the first recorded frame whose absolute value exceeds threshold, or -1.

func (*TestSink) FrameTime

func (t *TestSink) FrameTime(i int) time.Time

FrameTime is the wall time at which recorded device frame i was handed to the (virtual) speaker, in real-time mode: the sink's start plus i/DeviceRate.

func (*TestSink) Frames

func (t *TestSink) Frames() int

Frames is the number of device frames recorded so far.

func (*TestSink) Latency

func (t *TestSink) Latency() time.Duration

func (*TestSink) Name

func (t *TestSink) Name() string

func (*TestSink) Now

func (t *TestSink) Now() time.Time

func (*TestSink) Peak

func (t *TestSink) Peak(from, to int) float32

Peak returns the largest absolute sample in frames [from, to) of the recording (device frames).

func (*TestSink) Real

func (t *TestSink) Real() bool

func (*TestSink) Recorded

func (t *TestSink) Recorded() []float32

Recorded returns a copy of the recorded interleaved stereo PCM.

func (*TestSink) Reset

func (t *TestSink) Reset()

Reset forgets the recording.

func (*TestSink) SetLatency

func (t *TestSink) SetLatency(d time.Duration)

SetLatency sets the latency the sink reports (call before NewOutput's first stream; it models a device buffer).

func (*TestSink) Start

func (t *TestSink) Start(pull func([]float32)) error

Jump to

Keyboard shortcuts

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