sound.mixer

index · sound

Overview

In-process software mixer.

A static-musl Lilush cannot use ALSA's dmix, so the PCM device is exclusive and every simultaneous sound has to be summed in-process. The mixer keeps a set of active voices (each a sample buffer + playhead + gain/pan) and folds the next N frames of each into one output buffer via the C buffer:mix hot path. Finished non-looping voices are reaped automatically.

Mixing is in float32; clipping (clamp to [-1,1]) happens once, in the device on write, not here — so layering many voices never clips intermediate sums.

Functions

NameSignature
voice:stopvoice:stop()
voice:set_gainvoice:set_gain(gain)
voice:set_panvoice:set_pan(pan)
voice:is_activevoice:is_active() -> active
mixer:playmixer:play(buf, opts) -> voice
mixer:stopmixer:stop(id)
mixer:stop_allmixer:stop_all()
mixer:set_gainmixer:set_gain(id, gain)
mixer:set_panmixer:set_pan(id, pan)
mixer:is_activemixer:is_active(id) -> active
mixer:activemixer:active() -> count
mixer:fillmixer:fill(out, n) -> out
newnew(opts) -> mixer

voice:stop()

Stop this voice

voice:set_gain(gain)

Set this voice's gain

voice:set_pan(pan)

Set this voice's stereo pan

pan is -1 (left) .. 1 (right), equal-power.

voice:is_active() -> active

Check if this voice is still playing

mixer:play(buf, opts) -> voice

Start playing a sample buffer

opts fielddefaultdescription
gain1volume, 0..1+
pan0-1 (left) .. 1 (right), equal-power
loopfalserestart from the top when the buffer ends

Returns a voice handle with :stop(), :set_gain(g), :set_pan(p), :is_active().

mixer:stop(id)

Stop a voice by id

mixer:stop_all()

Stop all voices

mixer:set_gain(id, gain)

Set a voice's gain by id

mixer:set_pan(id, pan)

Set a voice's stereo pan by id

pan is -1 (left) .. 1 (right), equal-power.

mixer:is_active(id) -> active

Check if a voice is still playing

mixer:active() -> count

Count the currently playing voices

mixer:fill(out, n) -> out

Fill an output buffer with the next N frames of all voices

Clears out then mixes each active voice into it, advancing playheads, looping or reaping voices as needed. n defaults to the output buffer's full length. out must have the mixer's channel count. Returns out.

new(opts) -> mixer

Create a mixer

opts.channels (default 2) must match the output buffer / device channel count. Voices added with mono buffers are upmixed to stereo by the C mixer.