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.
| Name | Signature |
|---|---|
| voice:stop | voice:stop() |
| voice:set_gain | voice:set_gain(gain) |
| voice:set_pan | voice:set_pan(pan) |
| voice:is_active | voice:is_active() -> active |
| mixer:play | mixer:play(buf, opts) -> voice |
| mixer:stop | mixer:stop(id) |
| mixer:stop_all | mixer:stop_all() |
| mixer:set_gain | mixer:set_gain(id, gain) |
| mixer:set_pan | mixer:set_pan(id, pan) |
| mixer:is_active | mixer:is_active(id) -> active |
| mixer:active | mixer:active() -> count |
| mixer:fill | mixer:fill(out, n) -> out |
| new | new(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 field | default | description |
|---|---|---|
gain | 1 | volume, 0..1+ |
pan | 0 | -1 (left) .. 1 (right), equal-power |
loop | false | restart 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.