sound.mixer

index ยท sound

Overview

A software mixer. It holds the playing voices, each a sample buffer with a playhead, a gain and a pan. fill sums the next frames of every voice into one buffer through the C buffer:mix. A voice that ends without looping is dropped.

The mixer sums in float32 and never clamps. A card that takes only s16 clamps on write; a float card and the sound server take the samples as they stand.

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:stop_allmixer:stop_all()
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

A gain equal to the current one returns at once, so a caller can set the gain of every looped voice on every tick. The test is exact equality. An exponential slew lands exactly on its target, or snaps to exact zero, so a settled voice compares equal and a moving one does not.

voice:set_pan(pan)

Set this voice's stereo pan

pan runs from -1 (left) to 1 (right), equal-power. A pan equal to the current one returns at once, as in set_gain.

voice:is_active() -> active

Check if this voice is still playing

mixer:play(buf, opts) -> voice

Start playing a sample buffer

opts fielddefaultdescription
gain1volume; 1 is unity, above 1 amplifies
pan0-1 left to 1 right, equal-power
loopfalserestart from the top when the buffer ends

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

mixer:stop_all()

Stop all voices

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 every voice into it and advances the playheads. n defaults to the length of out, which must have the mixer's channel count. Returns out.

A voice whose gains are all exactly zero advances without mixing. Its playhead and loop position match a mixed voice's.

new(opts) -> mixer

Create a mixer

opts.channels, default 2, must match the device and the output buffer. A mono buffer plays on both channels of a stereo mixer.