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.

A voice can join the mixer's one low-pass bus when it opens. While the bus has a cutoff, fill sums its voices apart, filters the sum through the C buffer:lowpass and adds it in. A one-pole is linear, so this is what a filter on each voice would give at one cutoff, and a voice opened before the cutoff moved follows it. With no cutoff the bus's voices mix straight into the output, as every other voice does.

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:set_lowpassmixer:set_lowpass(hz)
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
lowpassfalsejoin the low-pass bus; fixed for the voice's life

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

mixer:set_lowpass(hz)

Set the low-pass bus's cutoff

hz is the bus's -3 dB point; nil bypasses the bus, and its voices mix as any other. A cutoff equal to the current one returns at once, as in voice:set_gain. Needs the mixer opened with rate, or raises.

The filter's state starts from the bus's first sample after a bypass, so engaging never steps. Bypassing drops the state, and the step it leaves is the filter's lag at the cutoff it left: small near the top of the range and large near the bottom. A caller glides the cutoff up before it lets go.

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.

While the low-pass bus has a cutoff, its voices sum into the mixer's own scratch, which is filtered and then added to out.

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. opts.rate, the device's frames a second, is needed only by set_lowpass: a guessed rate would put the cutoff in the wrong place without a word.