Files
konduktor/frontend/src/lib/audio/mixerMath.test.ts
T
2026-11-03 10:43:44 +01:00

117 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest'
import { beatSeconds, delayBeatsAt, eqGain, sweepBeatsAt, faderGain, filterSweep, gainToDb, xfadeGains } from './mixerMath'
describe('eqGain', () => {
it('is flat at the centre, +6 dB at full right', () => {
expect(eqGain(0, false)).toBe(1)
expect(gainToDb(eqGain(1, false))).toBeCloseTo(6, 6)
expect(gainToDb(eqGain(0.5, true))).toBeCloseTo(3, 6)
})
it('cuts to -26 dB, DJM-style', () => {
expect(gainToDb(eqGain(-1, false))).toBeCloseTo(-26, 6)
expect(gainToDb(eqGain(-0.5, false))).toBeCloseTo(-13, 6)
})
it('kills to silence over the last fifth, continuously', () => {
expect(eqGain(-1, true)).toBe(0)
expect(eqGain(-0.8, true)).toBeCloseTo(eqGain(-0.8, false), 9)
expect(eqGain(-0.5, true)).toBe(eqGain(-0.5, false))
let prev = Infinity
for (let v = -0.7; v >= -1; v -= 0.01) {
const g = eqGain(v, true)
expect(g).toBeLessThanOrEqual(prev)
prev = g
}
})
})
describe('xfadeGains', () => {
it('sharpness 0: constant power, -3 dB each at the centre', () => {
for (const x of [-1, -0.4, 0, 0.3, 1]) {
const [a, b] = xfadeGains(x, 0)
expect(a * a + b * b).toBeCloseTo(1, 9)
}
expect(gainToDb(xfadeGains(0, 0)[0])).toBeCloseTo(-3.01, 2)
expect(xfadeGains(-1, 0)).toEqual([1, expect.closeTo(0, 9)])
})
it('sharpness 1: both full until the last 4%, then a cut', () => {
expect(xfadeGains(0, 1)).toEqual([1, 1])
expect(xfadeGains(0.9, 1)).toEqual([1, 1])
expect(xfadeGains(1, 1)).toEqual([expect.closeTo(0, 9), 1])
expect(xfadeGains(-0.96, 1)[1]).toBeCloseTo(Math.SQRT1_2, 9) // halfway through its fade
})
it('in between: the centre rises from -3 dB to full, and every curve falls monotonically', () => {
let centre = 0
for (let s = 0; s <= 1; s += 0.1) {
const c = xfadeGains(0, s)[0]
expect(c).toBeGreaterThanOrEqual(centre - 1e-12)
centre = c
let prev = Infinity
for (let x = -1; x <= 1; x += 0.05) {
const [a, b] = xfadeGains(x, s)
expect(a).toBeLessThanOrEqual(prev + 1e-12)
expect(b).toBeCloseTo(xfadeGains(-x, s)[0], 9) // mirror images
prev = a
}
}
expect(xfadeGains(0, 0.6)[0]).toBe(1) // past ~0.52 the fades no longer reach the middle: no dip
})
})
describe('faderGain', () => {
it('runs 0 → 1 on an audio taper', () => {
expect(faderGain(0)).toBe(0)
expect(faderGain(1)).toBe(1)
expect(gainToDb(faderGain(0.7071))).toBeCloseTo(-6.02, 1)
})
})
describe('filterSweep', () => {
it('is bypassed at the centre', () => {
expect(filterSweep(0)).toBeNull()
expect(filterSweep(0.01)).toBeNull()
})
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('sweepBeatsAt', () => {
it('starts at the slowest sweep and halves at each quarter of the knob', () => {
expect(sweepBeatsAt(4, 0.05)).toBe(4)
expect(sweepBeatsAt(4, 0.3)).toBe(2)
expect(sweepBeatsAt(4, 0.6)).toBe(1)
expect(sweepBeatsAt(4, 0.8)).toBe(0.5)
expect(sweepBeatsAt(4, 1)).toBe(0.5)
expect(sweepBeatsAt(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
})
})