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
- Variables
- func NewRealtimeTestOutput() (*Output, *TestSink)
- func NewTestOutput() (*Output, *TestSink)
- func SetDefault(o *Output)
- type Backend
- type Group
- type Output
- func (o *Output) Backend() Backend
- func (o *Output) Close() error
- func (o *Output) Latency() time.Duration
- func (o *Output) NewGroup(name string) *Group
- func (o *Output) Open(rate, channels int, group *Group) *Stream
- func (o *Output) Poll()
- func (o *Output) Real() bool
- func (o *Output) SetMasterMuted(m bool)
- func (o *Output) SetMasterVolume(v float64)
- type Stream
- func (s *Stream) Buffered() time.Duration
- func (s *Stream) Close()
- func (s *Stream) Done() <-chan struct{}
- func (s *Stream) End()
- func (s *Stream) Flush()
- func (s *Stream) Group() *Group
- func (s *Stream) Latency() time.Duration
- func (s *Stream) Pause()
- func (s *Stream) Play()
- func (s *Stream) Playing() bool
- func (s *Stream) Position() time.Duration
- func (s *Stream) SetMuted(m bool)
- func (s *Stream) SetSpeed(v float64)
- func (s *Stream) SetVolume(v float64)
- func (s *Stream) Underruns() int64
- func (s *Stream) Write(pcm []float32) (int, error)
- func (s *Stream) WritePlanar(ch [][]float32) (int, error)
- func (s *Stream) Written() time.Duration
- type Suspender
- type TestSink
- func (t *TestSink) Advance(d time.Duration)
- func (t *TestSink) Close() error
- func (t *TestSink) FirstSound(threshold float32) int
- func (t *TestSink) FrameTime(i int) time.Time
- func (t *TestSink) Frames() int
- func (t *TestSink) Latency() time.Duration
- func (t *TestSink) Name() string
- func (t *TestSink) Now() time.Time
- func (t *TestSink) Peak(from, to int) float32
- func (t *TestSink) Real() bool
- func (t *TestSink) Recorded() []float32
- func (t *TestSink) Reset()
- func (t *TestSink) SetLatency(d time.Duration)
- func (t *TestSink) Start(pull func([]float32)) error
Constants ¶
const ( DeviceRate = 48000 DeviceChannels = 2 )
Device format. Every backend receives interleaved float32 frames in this format, so the mixer converts exactly once.
Variables ¶
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 ¶
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 ¶
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.
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) Close ¶
func (g *Group) Close()
Close detaches the group from the output. Its streams keep playing.
func (*Group) OnAudible ¶
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).
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 (*Output) NewGroup ¶
NewGroup creates a mix group (one per tab): a volume, a mute and an "audible" indicator over all its streams.
func (*Output) Open ¶
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) SetMasterMuted ¶
SetMasterMuted mutes the whole output.
func (*Output) SetMasterVolume ¶
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 ¶
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) Position ¶
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 ¶
SetMuted mutes or unmutes the stream. A muted stream keeps its clock running.
func (*Stream) SetSpeed ¶
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) Underruns ¶
Underruns counts mixer pulls that found the stream empty (or short) while it was playing and not ended.
func (*Stream) Write ¶
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 ¶
WritePlanar is Write for one slice per channel (the layout decoders produce).
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 ¶
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) FirstSound ¶
FirstSound returns the first recorded frame whose absolute value exceeds threshold, or -1.
func (*TestSink) FrameTime ¶
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) Peak ¶
Peak returns the largest absolute sample in frames [from, to) of the recording (device frames).
func (*TestSink) SetLatency ¶
SetLatency sets the latency the sink reports (call before NewOutput's first stream; it models a device buffer).