mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Improved filter FX
This commit is contained in:
1 parent
832dbed341
commit
717685f621
10 files changed
+426
-215
No files matched your search
+133
-97
@@ -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)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { echoSeconds, eqGain, faderGain, filterSweep, gainToDb, xfadeGains } from './mixerMath'
|
||||
import { beatSeconds, delayBeatsAt, eqGain, phaserBeatsAt, faderGain, filterSweep, gainToDb, xfadeGains } from './mixerMath'
|
||||
|
||||
describe('eqGain', () => {
|
||||
it('is flat at the centre, +6 dB at full right', () => {
|
||||
@@ -70,18 +70,47 @@ describe('filterSweep', () => {
|
||||
expect(filterSweep(0)).toBeNull()
|
||||
expect(filterSweep(0.01)).toBeNull()
|
||||
})
|
||||
it('low-passes left and high-passes right, on a log scale', () => {
|
||||
expect(filterSweep(-1)).toEqual({ type: 'lowpass', freq: expect.closeTo(150, 6) })
|
||||
expect(filterSweep(1)).toEqual({ type: 'highpass', freq: expect.closeTo(8000, 6) })
|
||||
const half = filterSweep(-0.5)!
|
||||
expect(half.freq).toBeCloseTo(Math.sqrt(20000 * 150), 3)
|
||||
it('full range (the default): low-pass to 20 Hz, high-pass to 20 kHz, on a log scale', () => {
|
||||
expect(filterSweep(-1)).toEqual({ type: 'lowpass', freq: expect.closeTo(20, 6) })
|
||||
expect(filterSweep(1)).toEqual({ type: 'highpass', freq: expect.closeTo(20000, 6) })
|
||||
expect(filterSweep(-0.5)!.freq).toBeCloseTo(Math.sqrt(20000 * 20), 3)
|
||||
expect(filterSweep(0.03)!.freq).toBeCloseTo(20 * 1000 ** 0.03, 6) // starts at the bottom
|
||||
})
|
||||
it('gentle range: stops at 500 Hz / 2 kHz', () => {
|
||||
expect(filterSweep(-1, 'gentle')).toEqual({ type: 'lowpass', freq: expect.closeTo(500, 6) })
|
||||
expect(filterSweep(1, 'gentle')).toEqual({ type: 'highpass', freq: expect.closeTo(2000, 6) })
|
||||
})
|
||||
it('musical range: stops at 150 Hz / 8 kHz', () => {
|
||||
expect(filterSweep(-1, 'musical')).toEqual({ type: 'lowpass', freq: expect.closeTo(150, 6) })
|
||||
expect(filterSweep(1, 'musical')).toEqual({ type: 'highpass', freq: expect.closeTo(8000, 6) })
|
||||
expect(filterSweep(-0.5, 'musical')!.freq).toBeCloseTo(Math.sqrt(20000 * 150), 3)
|
||||
})
|
||||
})
|
||||
|
||||
describe('echo', () => {
|
||||
it('is its length in beats at the deck’s tempo', () => {
|
||||
expect(echoSeconds(1, 120)).toBeCloseTo(0.5, 9)
|
||||
expect(echoSeconds(0.5, 128)).toBeCloseTo(60 / 128 / 2, 9)
|
||||
expect(echoSeconds(1, 0)).toBeCloseTo(0.5, 9) // unknown tempo: 120
|
||||
describe('phaserBeatsAt', () => {
|
||||
it('starts at the slowest sweep and halves at each quarter of the knob', () => {
|
||||
expect(phaserBeatsAt(4, 0.05)).toBe(4)
|
||||
expect(phaserBeatsAt(4, 0.3)).toBe(2)
|
||||
expect(phaserBeatsAt(4, 0.6)).toBe(1)
|
||||
expect(phaserBeatsAt(4, 0.8)).toBe(0.5)
|
||||
expect(phaserBeatsAt(4, 1)).toBe(0.5)
|
||||
expect(phaserBeatsAt(8, 1)).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('delayBeatsAt', () => {
|
||||
it('starts at the shortest echo and doubles at each quarter of the knob', () => {
|
||||
expect(delayBeatsAt(0.25, 0.1)).toBe(0.25)
|
||||
expect(delayBeatsAt(0.25, 0.4)).toBe(0.5)
|
||||
expect(delayBeatsAt(0.25, 0.7)).toBe(1)
|
||||
expect(delayBeatsAt(0.25, 1)).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('beatSeconds', () => {
|
||||
it('is a length in beats at the deck’s tempo', () => {
|
||||
expect(beatSeconds(1, 120)).toBeCloseTo(0.5, 9)
|
||||
expect(beatSeconds(0.5, 128)).toBeCloseTo(60 / 128 / 2, 9)
|
||||
expect(beatSeconds(4, 0)).toBeCloseTo(2, 9) // unknown tempo: 120
|
||||
})
|
||||
})
|
||||
@@ -59,23 +59,57 @@ export function xfadeGains(x: number, sharpness: number): [number, number] {
|
||||
export const FX_CENTRE = 0.02
|
||||
|
||||
/**
|
||||
* The filter at colour `c` (-1..1): left a low-pass falling from 20 kHz to
|
||||
* 150 Hz, right a high-pass rising from 20 Hz to 8 kHz, both on a log scale so
|
||||
* the sweep sounds even. Null at the centre (bypassed).
|
||||
* How far the FILTER knob sweeps (settings, decided 2026-11-03): `full` runs
|
||||
* the low-pass down to 20 Hz and the high-pass up to 20 kHz, so either end is
|
||||
* near silence; `musical` stops at 150 Hz / 8 kHz, where the track is thinned
|
||||
* but still there; `gentle` at 500 Hz / 2 kHz, where it stays clearly heard.
|
||||
*/
|
||||
export function filterSweep(c: number): { type: 'lowpass' | 'highpass'; freq: number } | null {
|
||||
export type FilterRange = 'full' | 'musical' | 'gentle'
|
||||
export const FILTER_RANGES: Record<FilterRange, { low: number; high: number }> = {
|
||||
full: { low: 20, high: 20000 },
|
||||
musical: { low: 150, high: 8000 },
|
||||
gentle: { low: 500, high: 2000 },
|
||||
}
|
||||
|
||||
/**
|
||||
* The filter at knob `c` (-1..1): left a low-pass falling from 20 kHz, right a
|
||||
* high-pass rising from 20 Hz, to the range's ends, on a log scale so the
|
||||
* sweep sounds even. Null at the centre (bypassed).
|
||||
*/
|
||||
export function filterSweep(
|
||||
c: number,
|
||||
range: FilterRange = 'full',
|
||||
): { type: 'lowpass' | 'highpass'; freq: number } | null {
|
||||
c = clamp(c, -1, 1)
|
||||
if (Math.abs(c) < FX_CENTRE) return null
|
||||
const a = Math.abs(c)
|
||||
const end = FILTER_RANGES[range]
|
||||
return c < 0
|
||||
? { type: 'lowpass', freq: 20000 * (150 / 20000) ** a }
|
||||
: { type: 'highpass', freq: 20 * (8000 / 20) ** a }
|
||||
? { type: 'lowpass', freq: 20000 * (end.low / 20000) ** a }
|
||||
: { type: 'highpass', freq: 20 * (end.high / 20) ** a }
|
||||
}
|
||||
|
||||
/** The echo lengths offered (beats). */
|
||||
export const ECHO_BEATS = [1 / 8, 1 / 4, 1 / 2, 3 / 4, 1]
|
||||
/**
|
||||
* The Filter FX grow as the knob opens (decided 2026-11-03), in STEPS of two
|
||||
* at each further quarter of its travel, so every length stays a musical one
|
||||
* of the tempo: 0 near the centre, 3 in the last quarter.
|
||||
*/
|
||||
export const knobStep = (a: number): number => Math.min(3, Math.floor(clamp(a, 0, 1) * 4))
|
||||
|
||||
/** Seconds of echo for `beats` at `bpm` (120 when the tempo is unknown). */
|
||||
export function echoSeconds(beats: number, bpm: number): number {
|
||||
/** The phaser's sweep (beats): `slowest` (the setting), then ½, ¼, ⅛ of it —
|
||||
* faster as the filter opens. */
|
||||
export const phaserBeatsAt = (slowest: number, a: number): number => slowest / 2 ** knobStep(a)
|
||||
|
||||
/** The delay's length (beats): `shortest` (the setting), then ×2, ×4, ×8 —
|
||||
* longer as the filter opens. */
|
||||
export const delayBeatsAt = (shortest: number, a: number): number => shortest * 2 ** knobStep(a)
|
||||
|
||||
/** The long reverb is this many times the set (short) one; the knob blends
|
||||
* from the short into the long as it opens. */
|
||||
export const REVERB_GROWTH = 3
|
||||
|
||||
/** Seconds for `beats` at `bpm` (120 when the tempo is unknown) — the delay's
|
||||
* length, the phaser's sweep. */
|
||||
export function beatSeconds(beats: number, bpm: number): number {
|
||||
return (beats * 60) / (bpm > 0 ? bpm : 120)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { DEFAULT_FX, FX_TYPES, rangeFor } from './mixerState'
|
||||
|
||||
describe('rangeFor', () => {
|
||||
it('the Filter alone follows its setting, an effect the other', () => {
|
||||
const fx = { ...DEFAULT_FX, filterRange: 'full' as const, effectRange: 'gentle' as const }
|
||||
expect(rangeFor('filter', fx)).toBe('full')
|
||||
for (const t of FX_TYPES.filter((x) => x !== 'filter')) expect(rangeFor(t, fx)).toBe('gentle')
|
||||
})
|
||||
it('defaults: Full alone, Musical with an effect', () => {
|
||||
expect(rangeFor('filter', DEFAULT_FX)).toBe('full')
|
||||
expect(rangeFor('reverb', DEFAULT_FX)).toBe('musical')
|
||||
})
|
||||
})
|
||||
@@ -5,42 +5,75 @@
|
||||
// mixer starts flat.
|
||||
|
||||
import { useSyncExternalStore } from 'react'
|
||||
import type { FilterRange } from './mixerMath'
|
||||
|
||||
export type Channel = 'A' | 'B'
|
||||
export const CHANNELS: Channel[] = ['A', 'B']
|
||||
|
||||
export const FX_TYPES = ['filter', 'echo', 'space', 'noise', 'crush'] as const
|
||||
/**
|
||||
* FILTER FX (decided 2026-11-03, after Traktor's mixer): each channel's FILTER
|
||||
* knob always filters — low-pass left, high-pass right — and the effect chosen
|
||||
* here rides with it, its amount growing with the knob. `filter` is the filter
|
||||
* alone.
|
||||
*/
|
||||
export const FX_TYPES = ['filter', 'reverb', 'delay', 'crusher', 'phaser'] as const
|
||||
export type FxType = (typeof FX_TYPES)[number]
|
||||
export const FX_LABELS: Record<FxType, string> = {
|
||||
filter: 'Filter',
|
||||
echo: 'Echo',
|
||||
space: 'Space',
|
||||
noise: 'Noise',
|
||||
crush: 'Crush',
|
||||
reverb: 'Reverb',
|
||||
delay: 'Delay',
|
||||
crusher: 'Bit crusher',
|
||||
phaser: 'Phaser',
|
||||
}
|
||||
/** For the channel's narrow picker. */
|
||||
export const FX_SHORT: Record<FxType, string> = {
|
||||
filter: 'Filter',
|
||||
reverb: 'Reverb',
|
||||
delay: 'Delay',
|
||||
crusher: 'Crush',
|
||||
phaser: 'Phaser',
|
||||
}
|
||||
|
||||
/**
|
||||
* Each effect's one setting — app settings, not knobs (decided 2026-11-03: the
|
||||
* per-channel parameter knob was removed), the same for both channels.
|
||||
* The filter and each effect's one setting — app settings, not knobs (the
|
||||
* per-channel parameter knob was removed, decided 2026-11-03), the same for
|
||||
* both channels.
|
||||
*/
|
||||
export interface FxSettings {
|
||||
/** The filter's range for the Filter alone … */
|
||||
filterRange: FilterRange
|
||||
/** … and with an effect riding along. */
|
||||
effectRange: FilterRange
|
||||
/** Filter resonance, 0..1. */
|
||||
filterResonance: number
|
||||
/** Echo length in beats of the deck's tempo. */
|
||||
echoBeats: number
|
||||
/** Space: the reverb's decay (s). */
|
||||
spaceSize: number
|
||||
/** Noise sweep resonance, 0..1. */
|
||||
noiseResonance: number
|
||||
/** Crush drive, 1..4. */
|
||||
crushDrive: number
|
||||
/** The SHORT reverb's decay (s) — near the centre; the knob blends into
|
||||
* one REVERB_GROWTH times as long as it opens. */
|
||||
reverbSize: number
|
||||
/** The delay's SHORTEST length, in beats of the deck's tempo — near the
|
||||
* centre; it doubles as the knob opens (`delayBeatsAt`). */
|
||||
delayBeats: number
|
||||
/** Bit crusher drive, 1..4. */
|
||||
crusherDrive: number
|
||||
/** The phaser's SLOWEST sweep, in beats of the deck's tempo — near the
|
||||
* centre; it halves as the knob opens (`phaserBeatsAt`). */
|
||||
phaserBeats: number
|
||||
}
|
||||
|
||||
export const DEFAULT_FX: FxSettings = {
|
||||
filterRange: 'full',
|
||||
effectRange: 'musical',
|
||||
filterResonance: 0.3,
|
||||
echoBeats: 0.5,
|
||||
spaceSize: 2.5,
|
||||
noiseResonance: 0.3,
|
||||
crushDrive: 2,
|
||||
reverbSize: 2,
|
||||
delayBeats: 0.25,
|
||||
crusherDrive: 2,
|
||||
phaserBeats: 4,
|
||||
}
|
||||
|
||||
/** The filter's range under `type`: one setting for the Filter alone (Full by
|
||||
* default), another with an effect riding along (Musical by default — swept
|
||||
* to near silence, the filter would starve the effect it feeds). */
|
||||
export function rangeFor(type: FxType, fx: FxSettings): FilterRange {
|
||||
return type === 'filter' ? fx.filterRange : fx.effectRange
|
||||
}
|
||||
|
||||
export interface ChannelState {
|
||||
@@ -51,7 +84,8 @@ export interface ChannelState {
|
||||
mid: number
|
||||
high: number
|
||||
fx: FxType
|
||||
/** The colour knob, -1..1 (centre = effect off). */
|
||||
/** The FILTER knob, -1..1 (centre = filter and effect off). Named `color`
|
||||
* from before the rename to Filter FX. */
|
||||
color: number
|
||||
/** The channel fader, 0..1. */
|
||||
fader: number
|
||||
|
||||
Reference in new issue
Block a user