Improved filter FX

This commit is contained in:
patben8 committed 2026-11-03 10:37:53 +01:00
1 parent 832dbed341
commit 717685f621
10 files changed
+426 -215

No files matched your search

+133 -97
View File
@@ -1,11 +1,11 @@
// The mixer's audio graph (performance mode, decided 2026-11-02). Both decks
// play into it; it follows the controls in `mixerState.ts`.
//
// deck → trim → 3-band isolator → colour FX → fader ─┬→ crossfader → bus
// deck → gain → 3-band isolator → filter FX → fader ─┬→ crossfader → bus
// └→ meter
// bus → limiter → master meter → speakers
//
// TRIM starts at the track's own auto gain (the DJ app's, `Track.auto_gain_db`;
// GAIN (`trim` in the state) starts at the track's own auto gain (the DJ app's, `Track.auto_gain_db`;
// none = 0 dB) plus the knob. The ISOLATOR splits at 250 Hz / 2.5 kHz with
// Linkwitz-Riley (LR4) crossovers, the low band through an all-pass matching
// the upper crossover, so the three bands sum back to flat — measured to 0.0001
@@ -23,18 +23,21 @@
import { audioContext } from './context'
import {
beatSeconds,
dbToGain,
echoSeconds,
eqGain,
EQ_HIGH_HZ,
EQ_LOW_HZ,
delayBeatsAt,
faderGain,
filterSweep,
phaserBeatsAt,
REVERB_GROWTH,
FX_CENTRE,
TRIM_RANGE_DB,
xfadeGains,
} from './mixerMath'
import { getMixerState, subscribeMixer, type Channel, type ChannelState, type FxSettings, type FxType } from './mixerState'
import { getMixerState, rangeFor, subscribeMixer, type Channel, type ChannelState, type FxSettings, type FxType } from './mixerState'
/** Butterworth Q in Web Audio's lowpass/highpass units (dB). */
const BUTTERWORTH_DB = 20 * Math.log10(Math.SQRT1_2)
@@ -44,6 +47,9 @@ const LIMIT_RATIO = 20
* gain at 0 dBFS, to the power 0.6), in dB. */
const MAKEUP_DB = -0.6 * (LIMIT_DB + (0 - LIMIT_DB) / LIMIT_RATIO)
/** The longest delay (s): 8 × a 1-beat setting at 120 BPM. */
const MAX_DELAY_S = 4
/** Parameter smoothing (s): fast enough to feel direct, slow enough not to click. */
const GLIDE = 0.012
@@ -100,38 +106,52 @@ function crushCurve(bits: number): Float32Array<ArrayBuffer> {
}
/**
* One channel's colour effect: ONE knob, -1..1, out of the path at the centre;
* which effect it drives is the channel's own choice (decided 2026-11-02), its
* one parameter an app setting (`FxSettings`, decided 2026-11-03).
* Every effect is built once and switched by gains, so a change never clicks;
* echo and reverb tails ring out after the knob returns.
* One channel's FILTER FX (decided 2026-11-03, after Traktor's mixer): ONE
* knob, -1..1, out of the path at the centre. It always FILTERS — low-pass
* left, high-pass right; over the `filterRange` setting for the Filter alone,
* the musical range under an effect (`rangeFor`) — and the channel's
* chosen effect rides with it, fed the FILTERED signal, its amount growing
* with the knob — and the more filter, the longer the reverb and delay and
* the faster the phaser: Reverb and Delay are sends whose tails ring out
* after the knob returns (echo- and wash-outs); Bit crusher replaces the
* filtered signal with a crushed copy; Phaser mixes it with a beat-synced
* sweeping all-pass chain, faster as the knob opens (`phaserBeatsAt`).
* `filter` is the filter alone. Everything is built
* once and switched by gains, so a change never clicks. Each effect's one
* parameter is an app setting (`FxSettings`).
*/
class ColorFx {
class FilterFx {
readonly input: GainNode
readonly output: GainNode
private readonly ctx: AudioContext
private readonly dry: GainNode
// Filter (also the music's path for Crush's tone).
/** Two stages, 24 dB/octave: the first Butterworth, the second carrying the
* resonance. One 12 dB stage left a 100 Hz bass only 28 dB down at the end
* of a full-range low-pass (measured) — not the near-silence asked for. */
private readonly filterIn: BiquadFilterNode
private readonly filter: BiquadFilterNode
private readonly filterWet: GainNode
// Echo: a send into a delay with filtered feedback.
private readonly echoSend: GainNode
/** The filtered music itself (every effect but Bit crusher keeps it). */
private readonly filtered: GainNode
// Reverb: sends into a SHORT and a LONG convolver; the knob blends from one
// into the other (re-generating one impulse instead would cut its tail).
private readonly reverbShort: GainNode
private readonly reverbLong: GainNode
private readonly convShort: ConvolverNode
private readonly convLong: ConvolverNode
private reverbSize = -1
// Delay: a send into a delay with filtered feedback.
private readonly delaySend: GainNode
private readonly delay: DelayNode
private readonly echoTone: BiquadFilterNode
private readonly delayTone: BiquadFilterNode
private readonly feedback: GainNode
// Space: a send into a convolver.
private readonly spaceSend: GainNode
private readonly spaceTone: BiquadFilterNode
private readonly convolver: ConvolverNode
private spaceSize = -1
// Noise: filtered white noise mixed in.
private readonly noiseFilter: BiquadFilterNode
private readonly noiseGain: GainNode
// Crush.
// Bit crusher.
private readonly crushDrive: GainNode
private readonly shaper: WaveShaperNode
private readonly crushWet: GainNode
private crushBits = -1
// Phaser: all-pass stages swept by one LFO.
private readonly lfo: OscillatorNode
private readonly phaserWet: GainNode
constructor(ctx: AudioContext) {
this.ctx = ctx
@@ -140,108 +160,124 @@ class ColorFx {
this.dry = ctx.createGain()
this.input.connect(this.dry).connect(this.output)
this.filterIn = biquad(ctx, 'lowpass', 20000, BUTTERWORTH_DB)
this.filter = biquad(ctx, 'lowpass', 20000, 0)
this.filterWet = ctx.createGain()
this.filterWet.gain.value = 0
this.input.connect(this.filter).connect(this.filterWet).connect(this.output)
this.input.connect(this.filterIn).connect(this.filter)
this.filtered = ctx.createGain()
this.filtered.gain.value = 0
this.filter.connect(this.filtered).connect(this.output)
this.echoSend = ctx.createGain()
this.echoSend.gain.value = 0
this.delay = ctx.createDelay(4)
this.echoTone = biquad(ctx, 'lowpass', 2500, 0)
this.reverbShort = ctx.createGain()
this.reverbShort.gain.value = 0
this.reverbLong = ctx.createGain()
this.reverbLong.gain.value = 0
this.convShort = ctx.createConvolver()
this.convLong = ctx.createConvolver()
this.filter.connect(this.reverbShort).connect(this.convShort).connect(this.output)
this.filter.connect(this.reverbLong).connect(this.convLong).connect(this.output)
this.delaySend = ctx.createGain()
this.delaySend.gain.value = 0
this.delay = ctx.createDelay(MAX_DELAY_S + 0.1)
this.delayTone = biquad(ctx, 'lowpass', 2500, 0)
this.feedback = ctx.createGain()
this.feedback.gain.value = 0.45
this.input.connect(this.echoSend).connect(this.delay)
this.delay.connect(this.echoTone).connect(this.feedback).connect(this.delay)
this.filter.connect(this.delaySend).connect(this.delay)
this.delay.connect(this.delayTone).connect(this.feedback).connect(this.delay)
this.delay.connect(this.output)
this.spaceSend = ctx.createGain()
this.spaceSend.gain.value = 0
this.spaceTone = biquad(ctx, 'lowpass', 20000, 0)
this.convolver = ctx.createConvolver()
this.input.connect(this.spaceSend).connect(this.spaceTone).connect(this.convolver).connect(this.output)
const noise = ctx.createBufferSource()
noise.buffer = noiseBuffer(ctx, 2)
noise.loop = true
this.noiseFilter = biquad(ctx, 'lowpass', 20000, 0)
this.noiseGain = ctx.createGain()
this.noiseGain.gain.value = 0
noise.connect(this.noiseFilter).connect(this.noiseGain).connect(this.output)
noise.start()
this.crushDrive = ctx.createGain()
this.shaper = ctx.createWaveShaper()
this.crushWet = ctx.createGain()
this.crushWet.gain.value = 0
this.input.connect(this.crushDrive).connect(this.shaper).connect(this.crushWet).connect(this.output)
this.filter.connect(this.crushDrive).connect(this.shaper).connect(this.crushWet).connect(this.output)
// Four all-pass stages an octave apart, each swept ±60% around its centre.
this.lfo = ctx.createOscillator()
this.lfo.frequency.value = 0.5
this.phaserWet = ctx.createGain()
this.phaserWet.gain.value = 0
let stage: AudioNode = this.filter
for (const centre of [350, 700, 1400, 2800]) {
const ap = biquad(ctx, 'allpass', centre, 0.6)
const depth = ctx.createGain()
depth.gain.value = centre * 0.6
this.lfo.connect(depth).connect(ap.frequency)
stage.connect(ap)
stage = ap
}
stage.connect(this.phaserWet).connect(this.output)
this.lfo.start()
}
apply(type: FxType, color: number, fx: FxSettings, bpm: number): void {
apply(type: FxType, knob: number, fx: FxSettings, bpm: number): void {
const ctx = this.ctx
const a = Math.min(1, Math.abs(color))
const a = Math.min(1, Math.abs(knob))
const on = a >= FX_CENTRE
// Whatever replaces the dry signal (filter, crush) fades in over the first
// few percent of the knob, so leaving the centre never jumps.
// The filtered path replaces the dry one over the first few percent of
// the knob, so leaving the centre never jumps.
const take = on ? Math.min(1, a / 0.06) : 0
const sweep = filterSweep(color)
// Filter: the music itself, swept.
const filtering = type === 'filter' && sweep != null
if (filtering) {
if (this.filter.type !== sweep.type) this.filter.type = sweep.type
const sweep = filterSweep(knob, rangeFor(type, fx))
if (sweep) {
if (this.filter.type !== sweep.type) {
this.filterIn.type = sweep.type
this.filter.type = sweep.type
}
ramp(this.filterIn.frequency, sweep.freq, ctx)
ramp(this.filter.frequency, sweep.freq, ctx)
ramp(this.filter.Q, fx.filterResonance * 15, ctx)
// Resonance fades over the last stretch: a resonant peak at 20 Hz (or
// 20 kHz) at the end of a full sweep would be a boom (or a whistle).
ramp(this.filter.Q, fx.filterResonance * 15 * (1 - a ** 4), ctx)
}
ramp(this.filterWet.gain, filtering ? take : 0, ctx)
ramp(this.dry.gain, 1 - take, ctx)
// Echo: send + feedback grow with the knob; left darker, right thinner.
const echoing = type === 'echo' && on
ramp(this.echoSend.gain, echoing ? a * 0.8 : 0, ctx)
ramp(this.feedback.gain, echoing ? 0.35 + 0.5 * a : 0.45, ctx, 0.05)
if (type === 'echo') {
ramp(this.delay.delayTime, echoSeconds(fx.echoBeats, bpm), ctx, 0.05)
const tone = color < 0 ? 'lowpass' : 'highpass'
if (this.echoTone.type !== tone) this.echoTone.type = tone
ramp(this.echoTone.frequency, color < 0 ? 2500 : 600, ctx)
}
// How much of the filtered music stays: all of it, but half under the
// phaser (whose notches come from mixing the two) and none under the
// crusher (which replaces it).
const phasing = type === 'phaser' && on
const mix = phasing ? Math.min(1, a / 0.25) : 0
const crushing = type === 'crusher' && on
ramp(this.filtered.gain, crushing ? 0 : take * (1 - 0.5 * mix), ctx)
ramp(this.phaserWet.gain, take * 0.5 * mix, ctx)
if (type === 'phaser') ramp(this.lfo.frequency, 1 / beatSeconds(phaserBeatsAt(fx.phaserBeats, a), bpm), ctx, 0.05)
// Space: a reverb send; left darker, right thinner.
const spacing = type === 'space' && on
ramp(this.spaceSend.gain, spacing ? a : 0, ctx)
if (type === 'space') {
const size = Math.round(Math.max(0.3, Math.min(8, fx.spaceSize)) * 10) / 10
if (size !== this.spaceSize) {
this.spaceSize = size
this.convolver.buffer = impulse(ctx, size)
// Reverb: the send grows with the knob, and moves from the short reverb
// into the long one — the more filter, the longer the tail.
const reverbing = type === 'reverb' && on
ramp(this.reverbShort.gain, reverbing ? a * (1 - a) : 0, ctx)
ramp(this.reverbLong.gain, reverbing ? a * a : 0, ctx)
if (type === 'reverb') {
const size = Math.round(Math.max(0.3, Math.min(6, fx.reverbSize)) * 10) / 10
if (size !== this.reverbSize) {
this.reverbSize = size
this.convShort.buffer = impulse(ctx, size)
this.convLong.buffer = impulse(ctx, Math.min(12, size * REVERB_GROWTH))
}
const tone = color < 0 ? 'lowpass' : 'highpass'
if (this.spaceTone.type !== tone) this.spaceTone.type = tone
ramp(this.spaceTone.frequency, color < 0 ? 1500 : 700, ctx)
}
// Noise: white noise swept the way the filter is, the music untouched.
const noising = type === 'noise' && sweep != null
if (noising) {
if (this.noiseFilter.type !== sweep.type) this.noiseFilter.type = sweep.type
ramp(this.noiseFilter.frequency, sweep.freq, ctx)
ramp(this.noiseFilter.Q, fx.noiseResonance * 15, ctx)
// Delay: send, feedback AND length grow with the knob (the length in
// doubling steps, `delayBeatsAt`); the repeats darken (left) or thin
// (right) as they go round.
const delaying = type === 'delay' && on
ramp(this.delaySend.gain, delaying ? a * 0.8 : 0, ctx)
ramp(this.feedback.gain, delaying ? 0.35 + 0.5 * a : 0.45, ctx, 0.05)
if (type === 'delay') {
ramp(this.delay.delayTime, Math.min(MAX_DELAY_S, beatSeconds(delayBeatsAt(fx.delayBeats, a), bpm)), ctx, 0.05)
const tone = knob < 0 ? 'lowpass' : 'highpass'
if (this.delayTone.type !== tone) this.delayTone.type = tone
ramp(this.delayTone.frequency, knob < 0 ? 2500 : 600, ctx)
}
ramp(this.noiseGain.gain, noising ? a * 0.25 : 0, ctx)
// Crush: fewer bits as the knob opens (16 → 3), driven by the setting.
const crushing = type === 'crush' && on
// Bit crusher: fewer bits as the knob opens (16 → 3), driven by the setting.
if (crushing) {
const bits = Math.round(16 - a * 13)
if (bits !== this.crushBits) {
this.crushBits = bits
this.shaper.curve = crushCurve(bits)
}
ramp(this.crushDrive.gain, fx.crushDrive, ctx)
ramp(this.crushDrive.gain, fx.crusherDrive, ctx)
}
ramp(this.crushWet.gain, crushing ? take / Math.max(1, fx.crushDrive) ** 0.5 : 0, ctx)
ramp(this.dry.gain, filtering || crushing ? 1 - take : 1, ctx)
ramp(this.crushWet.gain, crushing ? take / Math.max(1, fx.crusherDrive) ** 0.5 : 0, ctx)
}
}
@@ -251,7 +287,7 @@ class Strip {
private readonly ctx: AudioContext
private readonly trim: GainNode
private readonly bands: [GainNode, GainNode, GainNode]
private readonly fx: ColorFx
private readonly fx: FilterFx
private readonly fader: GainNode
private readonly xfade: GainNode
/** In circuit (performance mode) vs straight through (prep). */
@@ -284,7 +320,7 @@ class Strip {
mlB.connect(this.bands[1]).connect(eqOut)
mhB.connect(this.bands[2]).connect(eqOut)
this.fx = new ColorFx(ctx)
this.fx = new FilterFx(ctx)
eqOut.connect(this.fx.input)
this.fader = ctx.createGain()
this.fx.output.connect(this.fader)