Skip to content

Commit

Permalink
Merge pull request #1103 from daslyfe/ladder_filter
Browse files Browse the repository at this point in the history
Add analog-style ladder filter
  • Loading branch information
daslyfe authored May 23, 2024
2 parents 1666fc5 + e60e9b2 commit cad2730
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 7,121 deletions.
25 changes: 19 additions & 6 deletions packages/core/controls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,17 @@ export const { crush } = registerControl('crush');
*/
export const { coarse } = registerControl('coarse');

/**
* filter overdrive for supported filter types
*
* @name drive
* @param {number | Pattern} amount
* @example
* note("{f g g c d a a#}%16".sub(17)).s("supersaw").lpenv(8).lpf(150).lpq(.8).ftype('ladder').drive("<.5 4>")
*
*/
export const { drive } = registerControl('drive');

/**
* Allows you to set the output channels on the interface
*
Expand Down Expand Up @@ -742,15 +753,17 @@ export const { hprelease, hpr } = registerControl('hprelease', 'hpr');
*/
export const { bprelease, bpr } = registerControl('bprelease', 'bpr');
/**
* Sets the filter type. The 24db filter is more aggressive. More types might be added in the future.
* Sets the filter type. The ladder filter is more aggressive. More types might be added in the future.
* @name ftype
* @param {number | Pattern} type 12db (default) or 24db
* @param {number | Pattern} type 12db (0), ladder (1), or 24db (2)
* @example
* note("c2 e2 f2 g2")
* note("{f g g c d a a#}%8").s("sawtooth").lpenv(4).lpf(500).ftype("<0 1 2>").lpq(1)
* @example
* note("c f g g a c d4").fast(2)
* .sound('sawtooth')
* .lpf(500)
* .bpenv(4)
* .ftype("12db 24db")
* .lpf(200).fanchor(0)
* .lpenv(3).lpq(1)
* .ftype("<ladder 12db 24db>")
*/
export const { ftype } = registerControl('ftype');
export const { fanchor } = registerControl('fanchor');
Expand Down
29 changes: 23 additions & 6 deletions packages/superdough/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ const getSlope = (y1, y2, x1, x2) => {
}
return (y2 - y1) / (x2 - x1);
};

export function getWorklet(ac, processor, params, config) {
const node = new AudioWorkletNode(ac, processor, config);
Object.entries(params).forEach(([key, value]) => {
node.parameters.get(key).value = value;
});
return node;
}

export const getParamADSR = (
param,
attack,
Expand Down Expand Up @@ -103,14 +112,22 @@ export const getADSRValues = (params, curve = 'linear', defaultValues) => {
return [Math.max(a ?? 0, envmin), Math.max(d ?? 0, envmin), Math.min(sustain, envmax), Math.max(r ?? 0, releaseMin)];
};

export function createFilter(context, type, frequency, Q, att, dec, sus, rel, fenv, start, end, fanchor) {
export function createFilter(context, type, frequency, Q, att, dec, sus, rel, fenv, start, end, fanchor, model, drive) {
const curve = 'exponential';
const [attack, decay, sustain, release] = getADSRValues([att, dec, sus, rel], curve, [0.005, 0.14, 0, 0.1]);
const filter = context.createBiquadFilter();
let filter;
let frequencyParam;
if (model === 'ladder') {
filter = getWorklet(context, 'ladder-processor', { frequency, q: Q, drive });
frequencyParam = filter.parameters.get('frequency');
} else {
filter = context.createBiquadFilter();
filter.type = type;
filter.Q.value = Q;
filter.frequency.value = frequency;
frequencyParam = filter.frequency;
}

filter.type = type;
filter.Q.value = Q;
filter.frequency.value = frequency;
// envelope is active when any of these values is set
const hasEnvelope = att ?? dec ?? sus ?? rel ?? fenv;
// Apply ADSR to filter frequency
Expand All @@ -122,7 +139,7 @@ export function createFilter(context, type, frequency, Q, att, dec, sus, rel, fe
let min = clamp(2 ** -offset * frequency, 0, 20000);
let max = clamp(2 ** (fenvAbs - offset) * frequency, 0, 20000);
if (fenv < 0) [min, max] = [max, min];
getParamADSR(filter.frequency, attack, decay, sustain, release, min, max, start, end, curve);
getParamADSR(frequencyParam, attack, decay, sustain, release, min, max, start, end, curve);
return filter;
}
return filter;
Expand Down
27 changes: 14 additions & 13 deletions packages/superdough/superdough.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ This program is free software: you can redistribute it and/or modify it under th
import './feedbackdelay.mjs';
import './reverb.mjs';
import './vowel.mjs';
import { clamp, nanFallback } from './util.mjs';
import { clamp, nanFallback, _mod } from './util.mjs';
import workletsUrl from './worklets.mjs?url';
import { createFilter, gainNode, getCompressor } from './helpers.mjs';
import { createFilter, gainNode, getCompressor, getWorklet } from './helpers.mjs';
import { map } from 'nanostores';
import { logger } from './logger.mjs';
import { loadBuffer } from './sampler.mjs';
Expand Down Expand Up @@ -50,14 +50,6 @@ function loadWorklets() {
return workletsLoading;
}

export function getWorklet(ac, processor, params, config) {
const node = new AudioWorkletNode(ac, processor, config);
Object.entries(params).forEach(([key, value]) => {
node.parameters.get(key).value = value;
});
return node;
}

// this function should be called on first user interaction (to avoid console warning)
export async function initAudio(options = {}) {
const { disableWorklets = false } = options;
Expand Down Expand Up @@ -186,10 +178,14 @@ function getPhaser(orbit, t, speed = 1, depth = 0.5, centerFrequency = 1000, swe
return filterChain[filterChain.length - 1];
}

let reverbs = {};
function getFilterType(ftype) {
ftype = ftype ?? 0;
const filterTypes = ['12db', 'ladder', '24db'];
return typeof ftype === 'number' ? filterTypes[Math.floor(_mod(ftype, filterTypes.length))] : ftype;
}

let reverbs = {};
let hasChanged = (now, before) => now !== undefined && now !== before;

function getReverb(orbit, duration, fade, lp, dim, ir) {
// If no reverb has been created for a given orbit, create one
if (!reverbs[orbit]) {
Expand Down Expand Up @@ -288,8 +284,9 @@ export const superdough = async (value, t, hapDuration) => {
postgain = 1,
density = 0.03,
// filters
ftype = '12db',

fanchor = 0.5,
drive = 0.69,
// low pass
cutoff,
lpenv,
Expand Down Expand Up @@ -394,6 +391,8 @@ export const superdough = async (value, t, hapDuration) => {
// gain stage
chain.push(gainNode(gain));

//filter
const ftype = getFilterType(value.ftype);
if (cutoff !== undefined) {
let lp = () =>
createFilter(
Expand All @@ -409,6 +408,8 @@ export const superdough = async (value, t, hapDuration) => {
t,
t + hapDuration,
fanchor,
ftype,
drive,
);
chain.push(lp());
if (ftype === '24db') {
Expand Down
3 changes: 2 additions & 1 deletion packages/superdough/synth.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clamp, midiToFreq, noteToMidi } from './util.mjs';
import { registerSound, getAudioContext, getWorklet } from './superdough.mjs';
import { registerSound, getAudioContext } from './superdough.mjs';
import {
applyFM,
gainNode,
Expand All @@ -8,6 +8,7 @@ import {
getPitchEnvelope,
getVibratoOscillator,
webAudioTimeout,
getWorklet,
} from './helpers.mjs';
import { getNoiseMix, getNoiseOscillator } from './noise.mjs';

Expand Down
73 changes: 72 additions & 1 deletion packages/superdough/worklets.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// coarse, crush, and shape processors adapted from dktr0's webdirt: https://github.com/dktr0/WebDirt/blob/5ce3d698362c54d6e1b68acc47eb2955ac62c793/dist/AudioWorklets.js
// LICENSE GNU General Public License v3.0 see https://github.com/dktr0/WebDirt/blob/main/LICENSE

import { clamp } from './util.mjs';
const blockSize = 128;
class CoarseProcessor extends AudioWorkletProcessor {
static get parameterDescriptors() {
Expand Down Expand Up @@ -106,6 +106,77 @@ class ShapeProcessor extends AudioWorkletProcessor {
}
registerProcessor('shape-processor', ShapeProcessor);

function fast_tanh(x) {
const x2 = x * x;
return (x * (27.0 + x2)) / (27.0 + 9.0 * x2);
}
const _PI = 3.14159265359;
//adapted from https://github.com/TheBouteillacBear/webaudioworklet-wasm?tab=MIT-1-ov-file
class LadderProcessor extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
{ name: 'frequency', defaultValue: 500 },
{ name: 'q', defaultValue: 1 },
{ name: 'drive', defaultValue: 0.69 },
];
}

constructor() {
super();
this.started = false;
this.p0 = [0, 0];
this.p1 = [0, 0];
this.p2 = [0, 0];
this.p3 = [0, 0];
this.p32 = [0, 0];
this.p33 = [0, 0];
this.p34 = [0, 0];
}

process(inputs, outputs, parameters) {
const input = inputs[0];
const output = outputs[0];

const hasInput = !(input[0] === undefined);
if (this.started && !hasInput) {
return false;
}

this.started = hasInput;

const resonance = parameters.q[0];
const drive = clamp(Math.exp(parameters.drive[0]), 0.1, 2000);

let cutoff = parameters.frequency[0];
// eslint-disable-next-line no-undef
cutoff = (cutoff * 2 * _PI) / sampleRate;
cutoff = cutoff > 1 ? 1 : cutoff;

const k = Math.min(8, resonance * 0.4);
// drive makeup * resonance volume loss makeup
let makeupgain = (1 / drive) * Math.min(1.75, 1 + k);

for (let n = 0; n < blockSize; n++) {
for (let i = 0; i < input.length; i++) {
const out = this.p3[i] * 0.360891 + this.p32[i] * 0.41729 + this.p33[i] * 0.177896 + this.p34[i] * 0.0439725;

this.p34[i] = this.p33[i];
this.p33[i] = this.p32[i];
this.p32[i] = this.p3[i];

this.p0[i] += (fast_tanh(input[i][n] * drive - k * out) - fast_tanh(this.p0[i])) * cutoff;
this.p1[i] += (fast_tanh(this.p0[i]) - fast_tanh(this.p1[i])) * cutoff;
this.p2[i] += (fast_tanh(this.p1[i]) - fast_tanh(this.p2[i])) * cutoff;
this.p3[i] += (fast_tanh(this.p2[i]) - fast_tanh(this.p3[i])) * cutoff;

output[i][n] = out * makeupgain;
}
}
return true;
}
}
registerProcessor('ladder-processor', LadderProcessor);

class DistortProcessor extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
Expand Down
Loading

0 comments on commit cad2730

Please sign in to comment.