Documentation
¶
Overview ¶
Package webaudio implements the Web Audio rendering graph in pure Go: nodes, AudioParam automation, channel mixing and the DSP kernels. The JS-visible API (AudioContext, nodes, params) lives in the engine prelude and drives a Context through string commands.
Index ¶
- Constants
- type Buffer
- type Context
- func (c *Context) AddNode(typ string, o map[string]any) (int, error)
- func (c *Context) AnalyserByteFreq(id int) []uint8
- func (c *Context) AnalyserByteTime(id int) []uint8
- func (c *Context) AnalyserFloatFreq(id int) []float32
- func (c *Context) AnalyserFloatTime(id int) []float32
- func (c *Context) CompressorReduction(id int) float64
- func (c *Context) Connect(from, out, to, in int) error
- func (c *Context) ConnectParam(from, out, to int, param string) error
- func (c *Context) Disconnect(from, out, to, in int, param string)
- func (c *Context) Done() bool
- func (c *Context) FrequencyResponse(id int, freqs []float64) (mag, phase []float64)
- func (c *Context) HasExternal() bool
- func (c *Context) ListenerNode() int
- func (c *Context) Lock()
- func (c *Context) Node(id int) *Node
- func (c *Context) ParamBlock(p *Param) []float32
- func (c *Context) Provide(n *Node, out [][]float32)
- func (c *Context) RemoveNode(id int)
- func (c *Context) Render(frames int)
- func (c *Context) Set(id int, key string, v any) error
- func (c *Context) SetBuffer(id int, rate float64, data [][]float32)
- func (c *Context) SetStreamIn(id int, r *Ring) error
- func (c *Context) SetTap(id int, f func(ch [][]float32)) error
- func (c *Context) SetWave(id int, real, imag []float64, disableNorm bool)
- func (c *Context) Start(id int, when, offset, duration float64, hasDur bool) error
- func (c *Context) Step() (*Node, [][][]float32)
- func (c *Context) Stop(id int, when float64) error
- func (c *Context) TakeEnded() []int
- func (c *Context) Time() float64
- func (c *Context) Unlock()
- type Event
- type EventType
- type Listener
- type Node
- type Param
- type PeriodicWave
- type Ring
Constants ¶
const Quantum = quantum
Quantum is the render quantum size in frames.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct {
SampleRate float64
Offline bool
Frame int64
Dest *Node
Output [][]float32 // offline destination capture
// Sink, when set on a real-time context, receives the destination's output (one slice per channel,
// valid only during the call) after every render quantum.
Sink func(channels [][]float32)
Ended []int
Buffers map[int]*Buffer
Listener *Listener
// contains filtered or unexported fields
}
Context is a Web Audio graph plus its render clock.
func NewContext ¶
NewContext creates a context. For offline contexts frames is the length.
func (*Context) AddNode ¶
AddNode creates a node of the given type (lower-case names such as "gain", "oscillator", "biquad"). It returns the node id.
func (*Context) AnalyserByteFreq ¶
AnalyserByteFreq scales dB magnitudes into 0..255.
func (*Context) AnalyserByteTime ¶
AnalyserByteTime maps samples to 0..255 around 128.
func (*Context) AnalyserFloatFreq ¶
AnalyserFloatFreq returns dB magnitudes.
func (*Context) AnalyserFloatTime ¶
AnalyserFloatTime returns the latest fftSize samples.
func (*Context) CompressorReduction ¶
CompressorReduction returns the current gain reduction in dB.
func (*Context) ConnectParam ¶
ConnectParam wires an output to an AudioParam.
func (*Context) Disconnect ¶
Disconnect removes connections from `from`. to < 0 removes every outgoing connection; out/in < 0 are wildcards; a non-empty param targets only that AudioParam of node `to`.
func (*Context) FrequencyResponse ¶
FrequencyResponse evaluates a biquad at the given frequencies (Hz). Values outside [0, nyquist] give NaN, as in the spec.
func (*Context) HasExternal ¶
HasExternal reports whether host-driven nodes (worklets) exist.
func (*Context) ListenerNode ¶
ListenerNode returns the id of the node holding listener params.
func (*Context) Lock ¶
func (c *Context) Lock()
Lock and Unlock serialise access to the graph between the page's host calls and a render goroutine.
func (*Context) ParamBlock ¶
ParamBlock evaluates a parameter for the current quantum: 128 values for a-rate/automated params, otherwise a single value.
func (*Context) RemoveNode ¶
RemoveNode detaches every connection of a node (used on GC/close).
func (*Context) Render ¶
Render advances the graph by `frames` frames (rounded up to quanta); offline contexts accumulate destination samples in c.Output.
func (*Context) SetStreamIn ¶
SetStreamIn gives a MediaStreamAudioSourceNode its audio: every quantum takes what the ring holds.
func (*Context) SetTap ¶
SetTap installs a function called after every quantum with what a MediaStreamAudioDestinationNode received (the slices are only valid during the call).
func (*Context) Step ¶
Step advances externally-driven rendering. It returns the next external node needing input (with its mixed input) or nil when the quantum is complete (and the clock has advanced).
type Event ¶
type Event struct {
Type EventType
Time float64 // seconds
Value float64
TimeCst float64 // SetTarget
Duration float64 // Curve
Curve []float32 // Curve
}
Event is one automation event.
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Listener is the AudioListener state (params live on a hidden node).
type Node ¶
type Node struct {
ID int
Type string
Params map[string]*Param
ChCount int
ChMode string // "max", "clamped-max", "explicit"
ChInterp string // "speakers", "discrete"
Props map[string]any
// Tap, when set on a MediaStreamAudioDestinationNode, receives each quantum it passes on.
Tap func(ch [][]float32)
// contains filtered or unexported fields
}
Node is one AudioNode.
type Param ¶
type Param struct {
Name string
Default float64
Min, Max float64
Value float64 // intrinsic value (used before the first event)
ARate bool // a-rate (per frame) or k-rate
Events []Event
// contains filtered or unexported fields
}
Param is an AudioParam.
func (*Param) CancelAndHold ¶
CancelAndHold freezes the value the timeline has at t.
func (*Param) CancelScheduled ¶
CancelScheduled removes events at or after t.
type PeriodicWave ¶
type PeriodicWave struct {
// contains filtered or unexported fields
}
PeriodicWave mirrors Chromium's band-limited wavetable design: 36 ranges (three per octave) of 4096-sample tables, normalised on the first range.
func NewPeriodicWave ¶
func NewPeriodicWave(rate float64, real, imag []float64, disableNorm bool) *PeriodicWave
NewPeriodicWave builds the tables from Fourier coefficients (real = cos terms, imag = sin terms; index 0 ignored).
type Ring ¶
type Ring struct {
// contains filtered or unexported fields
}
Ring is a first-in first-out buffer of planar float32 audio between a producer that is not tied to the render clock (a microphone, a remote WebRTC track, an AudioData generator) and the render thread that consumes it a quantum at a time. When it is full the oldest audio is dropped (a live source never waits for its reader); when it runs dry the reader gets silence.