@toneflix/studio-wams 0.0.0-stage → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +39 -2
  3. package/THIRD_PARTY_NOTICES.md +43 -0
  4. package/dist/catalog.d.ts +22 -0
  5. package/dist/catalog.js +272 -0
  6. package/dist/index.d.ts +45 -0
  7. package/dist/index.js +38 -0
  8. package/dist/version.d.ts +2 -0
  9. package/dist/version.js +2 -0
  10. package/licenses/MPL-2.0.txt +373 -0
  11. package/package.json +50 -3
  12. package/plugins/plugins.json +357 -0
  13. package/plugins/sequencer-party/delay/descriptor.json +16 -0
  14. package/plugins/sequencer-party/delay/index.js +247 -0
  15. package/plugins/sequencer-party/delay/screenshot.png +0 -0
  16. package/plugins/sequencer-party/distortion/descriptor.json +16 -0
  17. package/plugins/sequencer-party/distortion/index.js +257 -0
  18. package/plugins/sequencer-party/distortion/screenshot.png +0 -0
  19. package/plugins/sequencer-party/simple-eq/descriptor.json +16 -0
  20. package/plugins/sequencer-party/simple-eq/index.js +237 -0
  21. package/plugins/sequencer-party/simple-eq/screenshot.png +0 -0
  22. package/plugins/toneflix/base.js +182 -0
  23. package/plugins/toneflix/chorus.js +48 -0
  24. package/plugins/toneflix/chorus.png +0 -0
  25. package/plugins/toneflix/compressor.js +35 -0
  26. package/plugins/toneflix/compressor.png +0 -0
  27. package/plugins/toneflix/drive.js +44 -0
  28. package/plugins/toneflix/drive.png +0 -0
  29. package/plugins/toneflix/echo.js +35 -0
  30. package/plugins/toneflix/echo.png +0 -0
  31. package/plugins/toneflix/eq.js +36 -0
  32. package/plugins/toneflix/eq.png +0 -0
  33. package/plugins/toneflix/gate.js +79 -0
  34. package/plugins/toneflix/gate.png +0 -0
  35. package/plugins/toneflix/kbverb.js +153 -0
  36. package/plugins/toneflix/kbverb.png +0 -0
  37. package/plugins/toneflix/limiter.js +47 -0
  38. package/plugins/toneflix/limiter.png +0 -0
  39. package/plugins/toneflix/phaser.js +47 -0
  40. package/plugins/toneflix/phaser.png +0 -0
  41. package/plugins/toneflix/reverb.js +58 -0
  42. package/plugins/toneflix/reverb.png +0 -0
  43. package/plugins/toneflix/stone-phaser.js +142 -0
  44. package/plugins/toneflix/stone-phaser.png +0 -0
@@ -0,0 +1,182 @@
1
+ /**
2
+ * A tiny base for the Toneflix plugins: a Web Audio Module (WAM 2) made of plain
3
+ * Web Audio nodes, with parameters, state, transport events and a slider editor.
4
+ */
5
+
6
+ /** One AudioNode outside, a graph inside: input is this node, output is `this.output`. */
7
+ class CompositeNode extends GainNode {
8
+ constructor(context) {
9
+ super(context);
10
+ this.output = new GainNode(context);
11
+ }
12
+
13
+ connect(...args) {
14
+ return this.output.connect(...args);
15
+ }
16
+
17
+ disconnect(...args) {
18
+ return this.output.disconnect(...args);
19
+ }
20
+
21
+ /** Connect the input itself, not the output. */
22
+ connectInput(target) {
23
+ return GainNode.prototype.connect.call(this, target);
24
+ }
25
+ }
26
+
27
+ /** AudioWorklet modules already added, per context. */
28
+ const worklets = new WeakMap();
29
+
30
+ /** Add a worklet's processor code to a context once. */
31
+ const loadWorklet = (context, code) => {
32
+ let loaded = worklets.get(context);
33
+
34
+ if (!loaded) {
35
+ loaded = new Map();
36
+ worklets.set(context, loaded);
37
+ }
38
+
39
+ if (!loaded.has(code)) {
40
+ const url = URL.createObjectURL(new Blob([code], { type: 'text/javascript' }));
41
+
42
+ loaded.set(
43
+ code,
44
+ context.audioWorklet.addModule(url).finally(() => URL.revokeObjectURL(url)),
45
+ );
46
+ }
47
+
48
+ return loaded.get(code);
49
+ };
50
+
51
+ /** Decimals to show for a parameter's step. */
52
+ const decimals = (step) => Math.min(2, String(step).split('.')[1]?.length ?? 0);
53
+
54
+ /**
55
+ * `params` are the sliders, `build` makes the graph, `apply` sets it from the values.
56
+ * A `stop` function in the parts `build` returns runs when the plugin is destroyed.
57
+ * `worklet` is AudioWorklet processor code the graph needs, added before `build` runs.
58
+ */
59
+ export const makePlugin = ({ identifier, name, params, build, apply, worklet }) => {
60
+ class Node extends CompositeNode {
61
+ constructor(context) {
62
+ super(context);
63
+ this.values = Object.fromEntries(Object.entries(params).map(([id, p]) => [id, p.defaultValue]));
64
+ this.transport = { tempo: 120, playing: false };
65
+ this.parts = build(context, this);
66
+ this.update();
67
+ }
68
+
69
+ update() {
70
+ apply(this.parts, this.values, this.transport, this.context);
71
+ }
72
+
73
+ async getState() {
74
+ return { ...this.values };
75
+ }
76
+
77
+ async setState(state) {
78
+ Object.assign(this.values, state);
79
+ this.update();
80
+ }
81
+
82
+ async getParameterInfo(...ids) {
83
+ return Object.fromEntries(Object.entries(params).filter(([id]) => !ids.length || ids.includes(id)).map(([id, p]) => [id, { id, type: 'float', ...p }]));
84
+ }
85
+
86
+ async getParameterValues(normalized, ...ids) {
87
+ return Object.fromEntries(Object.entries(this.values).filter(([id]) => !ids.length || ids.includes(id)).map(([id, value]) => [id, { id, value, normalized: false }]));
88
+ }
89
+
90
+ async setParameterValues(values) {
91
+ for (const { id, value } of Object.values(values)) this.values[id] = value;
92
+ this.update();
93
+ }
94
+
95
+ scheduleEvents(...events) {
96
+ for (const event of events) {
97
+ if (event.type === 'wam-transport') this.transport = event.data;
98
+ if (event.type === 'wam-automation') this.values[event.data.id] = event.data.value;
99
+ }
100
+ this.update();
101
+ }
102
+
103
+ clearEvents() {}
104
+
105
+ destroy() {
106
+ this.parts.stop?.();
107
+ this.disconnect();
108
+ }
109
+ }
110
+
111
+ return class Plugin {
112
+ static async createInstance(groupId, audioContext, initialState) {
113
+ if (worklet) await loadWorklet(audioContext, worklet);
114
+
115
+ const plugin = new Plugin(groupId, audioContext);
116
+ if (initialState) await plugin.audioNode.setState(initialState);
117
+ return plugin;
118
+ }
119
+
120
+ constructor(groupId, audioContext) {
121
+ this.groupId = groupId;
122
+ this.audioContext = audioContext;
123
+ this.audioNode = new Node(audioContext);
124
+ this.descriptor = { identifier, name, vendor: 'Toneflix', version: '1.0.0', apiVersion: '2.0.0', hasAudioInput: true, hasAudioOutput: true };
125
+ this.instanceId = `${identifier}-${Math.random().toString(36).slice(2)}`;
126
+ }
127
+
128
+ get isWebAudioModule() {
129
+ return true;
130
+ }
131
+
132
+ get moduleId() {
133
+ return identifier;
134
+ }
135
+
136
+ get name() {
137
+ return name;
138
+ }
139
+
140
+ get vendor() {
141
+ return this.descriptor.vendor;
142
+ }
143
+
144
+ async createGui() {
145
+ const node = this.audioNode;
146
+ const root = document.createElement('div');
147
+
148
+ root.style.cssText = 'display:grid;gap:10px;padding:14px 16px;min-width:220px;font:12px system-ui,sans-serif;color:#e8e8ea';
149
+
150
+ for (const [id, p] of Object.entries(params)) {
151
+ const row = document.createElement('label');
152
+ const top = document.createElement('div');
153
+ const value = document.createElement('span');
154
+ const input = document.createElement('input');
155
+ const show = () => (value.textContent = `${(+node.values[id]).toFixed(decimals(p.step ?? 0.01))}${p.units ? ` ${p.units}` : ''}`);
156
+
157
+ top.style.cssText = 'display:flex;justify-content:space-between;margin-bottom:4px';
158
+ top.append(p.label, value);
159
+ input.type = 'range';
160
+ input.min = p.minValue;
161
+ input.max = p.maxValue;
162
+ input.step = p.step ?? 0.01;
163
+ input.value = node.values[id];
164
+ input.style.cssText = 'width:100%;accent-color:#03bfff';
165
+ input.addEventListener('input', () => {
166
+ node.values[id] = +input.value;
167
+ node.update();
168
+ show();
169
+ });
170
+ show();
171
+ row.append(top, input);
172
+ root.append(row);
173
+ }
174
+
175
+ return root;
176
+ }
177
+
178
+ destroyGui(gui) {
179
+ gui.remove();
180
+ }
181
+ };
182
+ };
@@ -0,0 +1,48 @@
1
+ import { makePlugin } from './base.js';
2
+
3
+ /** A stereo chorus: two short delays swept in opposite directions by one LFO. */
4
+ export default makePlugin({
5
+ identifier: 'net.toneflix.studio.chorus',
6
+ name: 'Chorus',
7
+ params: {
8
+ rate: { label: 'Rate', defaultValue: 0.8, minValue: 0.05, maxValue: 5, step: 0.05, units: 'Hz' },
9
+ depth: { label: 'Depth', defaultValue: 3, minValue: 0, maxValue: 10, step: 0.1, units: 'ms' },
10
+ delay: { label: 'Delay', defaultValue: 15, minValue: 5, maxValue: 40, step: 1, units: 'ms' },
11
+ mix: { label: 'Mix', defaultValue: 0.5, minValue: 0, maxValue: 1 },
12
+ },
13
+ build(context, node) {
14
+ const lfo = new OscillatorNode(context, { type: 'sine' });
15
+ const dry = new GainNode(context);
16
+ const wet = new GainNode(context);
17
+ const voices = [-1, 1].map((side) => {
18
+ const delay = new DelayNode(context, { maxDelayTime: 0.06 });
19
+ const depth = new GainNode(context);
20
+ const pan = new StereoPannerNode(context, { pan: side * 0.8 });
21
+
22
+ lfo.connect(depth).connect(delay.delayTime);
23
+ node.connectInput(delay).connect(pan).connect(wet);
24
+
25
+ return { delay, depth, side };
26
+ });
27
+
28
+ node.connectInput(dry).connect(node.output);
29
+ wet.connect(node.output);
30
+ lfo.start();
31
+
32
+ return { lfo, dry, wet, voices, stop: () => lfo.stop() };
33
+ },
34
+ apply({ lfo, dry, wet, voices }, v, _transport, context) {
35
+ const now = context.currentTime;
36
+
37
+ lfo.frequency.setTargetAtTime(v.rate, now, 0.02);
38
+
39
+ for (const voice of voices) {
40
+ voice.delay.delayTime.setTargetAtTime(v.delay / 1000, now, 0.02);
41
+ voice.depth.gain.setTargetAtTime((voice.side * v.depth) / 1000 / 2, now, 0.02);
42
+ }
43
+
44
+ // Two voices: each at half level
45
+ wet.gain.setTargetAtTime(v.mix * 0.5, now, 0.02);
46
+ dry.gain.setTargetAtTime(1 - v.mix * 0.5, now, 0.02);
47
+ },
48
+ });
Binary file
@@ -0,0 +1,35 @@
1
+ import { makePlugin } from './base.js';
2
+
3
+ const gain = (db) => 10 ** (db / 20);
4
+
5
+ /** A compressor with threshold, ratio, timing and make-up gain. */
6
+ export default makePlugin({
7
+ identifier: 'net.toneflix.studio.compressor',
8
+ name: 'Compressor',
9
+ params: {
10
+ threshold: { label: 'Threshold', defaultValue: -18, minValue: -60, maxValue: 0, step: 0.5, units: 'dB' },
11
+ ratio: { label: 'Ratio', defaultValue: 4, minValue: 1, maxValue: 20, step: 0.5, units: ':1' },
12
+ attack: { label: 'Attack', defaultValue: 10, minValue: 0, maxValue: 200, step: 1, units: 'ms' },
13
+ release: { label: 'Release', defaultValue: 150, minValue: 10, maxValue: 1000, step: 5, units: 'ms' },
14
+ knee: { label: 'Knee', defaultValue: 6, minValue: 0, maxValue: 40, step: 1, units: 'dB' },
15
+ makeup: { label: 'Make-up', defaultValue: 0, minValue: -12, maxValue: 24, step: 0.5, units: 'dB' },
16
+ },
17
+ build(context, node) {
18
+ const compressor = new DynamicsCompressorNode(context);
19
+ const makeup = new GainNode(context);
20
+
21
+ node.connectInput(compressor).connect(makeup).connect(node.output);
22
+
23
+ return { compressor, makeup };
24
+ },
25
+ apply({ compressor, makeup }, v, _transport, context) {
26
+ const now = context.currentTime;
27
+
28
+ compressor.threshold.setValueAtTime(v.threshold, now);
29
+ compressor.ratio.setValueAtTime(v.ratio, now);
30
+ compressor.attack.setValueAtTime(v.attack / 1000, now);
31
+ compressor.release.setValueAtTime(v.release / 1000, now);
32
+ compressor.knee.setValueAtTime(v.knee, now);
33
+ makeup.gain.setTargetAtTime(gain(v.makeup), now, 0.02);
34
+ },
35
+ });
Binary file
@@ -0,0 +1,44 @@
1
+ import { makePlugin } from './base.js';
2
+
3
+ const curve = (amount) => {
4
+ const k = amount * 50;
5
+ const data = new Float32Array(1024);
6
+
7
+ for (let i = 0; i < data.length; i++) {
8
+ const x = (i / (data.length - 1)) * 2 - 1;
9
+ data[i] = ((1 + k) * x) / (1 + k * Math.abs(x));
10
+ }
11
+
12
+ return data;
13
+ };
14
+
15
+ /** Warm saturation with a tone control. */
16
+ export default makePlugin({
17
+ identifier: 'net.toneflix.studio.drive',
18
+ name: 'Drive',
19
+ params: {
20
+ drive: { label: 'Drive', defaultValue: 0.3, minValue: 0, maxValue: 1 },
21
+ tone: { label: 'Tone', defaultValue: 6000, minValue: 800, maxValue: 16000, step: 50, units: 'Hz' },
22
+ level: { label: 'Level', defaultValue: 0.8, minValue: 0, maxValue: 1.5 },
23
+ },
24
+ build(context, node) {
25
+ const shaper = new WaveShaperNode(context, { oversample: '4x' });
26
+ const tone = new BiquadFilterNode(context, { type: 'lowpass' });
27
+ const level = new GainNode(context);
28
+
29
+ node.connectInput(shaper).connect(tone).connect(level).connect(node.output);
30
+
31
+ return { shaper, tone, level, amount: -1 };
32
+ },
33
+ apply(parts, v, _transport, context) {
34
+ const now = context.currentTime;
35
+
36
+ if (parts.amount !== v.drive) {
37
+ parts.shaper.curve = curve(v.drive);
38
+ parts.amount = v.drive;
39
+ }
40
+
41
+ parts.tone.frequency.setTargetAtTime(v.tone, now, 0.02);
42
+ parts.level.gain.setTargetAtTime(v.level, now, 0.02);
43
+ },
44
+ });
Binary file
@@ -0,0 +1,35 @@
1
+ import { makePlugin } from './base.js';
2
+
3
+ /** A tempo-synced echo: repeats every `beats` beats of the host's tempo. */
4
+ export default makePlugin({
5
+ identifier: 'net.toneflix.studio.echo',
6
+ name: 'Echo',
7
+ params: {
8
+ beats: { label: 'Beats', defaultValue: 0.75, minValue: 0.25, maxValue: 2, step: 0.25 },
9
+ feedback: { label: 'Feedback', defaultValue: 0.35, minValue: 0, maxValue: 0.9 },
10
+ tone: { label: 'Tone', defaultValue: 3500, minValue: 500, maxValue: 12000, step: 50, units: 'Hz' },
11
+ mix: { label: 'Mix', defaultValue: 0.3, minValue: 0, maxValue: 1 },
12
+ },
13
+ build(context, node) {
14
+ const delay = new DelayNode(context, { maxDelayTime: 4 });
15
+ const feedback = new GainNode(context);
16
+ const tone = new BiquadFilterNode(context, { type: 'lowpass' });
17
+ const wet = new GainNode(context);
18
+ const dry = new GainNode(context);
19
+
20
+ node.connectInput(dry).connect(node.output);
21
+ node.connectInput(delay).connect(tone).connect(feedback).connect(delay);
22
+ tone.connect(wet).connect(node.output);
23
+
24
+ return { delay, feedback, tone, wet, dry };
25
+ },
26
+ apply({ delay, feedback, tone, wet, dry }, v, transport, context) {
27
+ const now = context.currentTime;
28
+
29
+ delay.delayTime.setTargetAtTime(Math.min(4, v.beats * (60 / (transport.tempo || 120))), now, 0.02);
30
+ feedback.gain.setTargetAtTime(v.feedback, now, 0.02);
31
+ tone.frequency.setTargetAtTime(v.tone, now, 0.02);
32
+ wet.gain.setTargetAtTime(v.mix, now, 0.02);
33
+ dry.gain.setTargetAtTime(1 - v.mix * 0.5, now, 0.02);
34
+ },
35
+ });
Binary file
@@ -0,0 +1,36 @@
1
+ import { makePlugin } from './base.js';
2
+
3
+ /** A three-band EQ: low shelf, a sweepable mid and a high shelf. */
4
+ export default makePlugin({
5
+ identifier: 'net.toneflix.studio.eq',
6
+ name: 'EQ',
7
+ params: {
8
+ low: { label: 'Low', defaultValue: 0, minValue: -18, maxValue: 18, step: 0.5, units: 'dB' },
9
+ lowFreq: { label: 'Low freq', defaultValue: 120, minValue: 40, maxValue: 500, step: 5, units: 'Hz' },
10
+ mid: { label: 'Mid', defaultValue: 0, minValue: -18, maxValue: 18, step: 0.5, units: 'dB' },
11
+ midFreq: { label: 'Mid freq', defaultValue: 1000, minValue: 200, maxValue: 8000, step: 10, units: 'Hz' },
12
+ midQ: { label: 'Mid width', defaultValue: 1, minValue: 0.3, maxValue: 8, step: 0.1, units: 'Q' },
13
+ high: { label: 'High', defaultValue: 0, minValue: -18, maxValue: 18, step: 0.5, units: 'dB' },
14
+ highFreq: { label: 'High freq', defaultValue: 8000, minValue: 2000, maxValue: 16000, step: 50, units: 'Hz' },
15
+ },
16
+ build(context, node) {
17
+ const low = new BiquadFilterNode(context, { type: 'lowshelf' });
18
+ const mid = new BiquadFilterNode(context, { type: 'peaking' });
19
+ const high = new BiquadFilterNode(context, { type: 'highshelf' });
20
+
21
+ node.connectInput(low).connect(mid).connect(high).connect(node.output);
22
+
23
+ return { low, mid, high };
24
+ },
25
+ apply({ low, mid, high }, v, _transport, context) {
26
+ const now = context.currentTime;
27
+
28
+ low.gain.setTargetAtTime(v.low, now, 0.02);
29
+ low.frequency.setTargetAtTime(v.lowFreq, now, 0.02);
30
+ mid.gain.setTargetAtTime(v.mid, now, 0.02);
31
+ mid.frequency.setTargetAtTime(v.midFreq, now, 0.02);
32
+ mid.Q.setTargetAtTime(v.midQ, now, 0.02);
33
+ high.gain.setTargetAtTime(v.high, now, 0.02);
34
+ high.frequency.setTargetAtTime(v.highFreq, now, 0.02);
35
+ },
36
+ });
Binary file
@@ -0,0 +1,79 @@
1
+ import { makePlugin } from './base.js';
2
+
3
+ /** The gate's processor: a peak detector opens it, it holds, then closes to `range`. */
4
+ const processor = `
5
+ class ToneflixGate extends AudioWorkletProcessor {
6
+ static get parameterDescriptors() {
7
+ return [
8
+ { name: 'threshold', defaultValue: -40, automationRate: 'k-rate' },
9
+ { name: 'attack', defaultValue: 1, automationRate: 'k-rate' },
10
+ { name: 'hold', defaultValue: 50, automationRate: 'k-rate' },
11
+ { name: 'release', defaultValue: 120, automationRate: 'k-rate' },
12
+ { name: 'range', defaultValue: -80, automationRate: 'k-rate' },
13
+ ];
14
+ }
15
+
16
+ constructor() {
17
+ super();
18
+ this.level = 0;
19
+ this.gain = 0;
20
+ this.held = 0;
21
+ }
22
+
23
+ process([input], [output], p) {
24
+ if (!input.length) return true;
25
+
26
+ const threshold = 10 ** (p.threshold[0] / 20);
27
+ const floor = 10 ** (p.range[0] / 20);
28
+ const attack = Math.exp(-1 / (sampleRate * Math.max(0.0001, p.attack[0] / 1000)));
29
+ const release = Math.exp(-1 / (sampleRate * Math.max(0.001, p.release[0] / 1000)));
30
+ const fall = Math.exp(-1 / (sampleRate * 0.01));
31
+ const hold = (p.hold[0] / 1000) * sampleRate;
32
+
33
+ for (let i = 0; i < input[0].length; i++) {
34
+ let peak = 0;
35
+
36
+ for (const channel of input) peak = Math.max(peak, Math.abs(channel[i]));
37
+
38
+ this.level = peak > this.level ? peak : this.level * fall;
39
+
40
+ if (this.level >= threshold) this.held = hold;
41
+ else if (this.held > 0) this.held--;
42
+
43
+ const target = this.held > 0 ? 1 : floor;
44
+
45
+ this.gain = target + (this.gain - target) * (target > this.gain ? attack : release);
46
+
47
+ for (let c = 0; c < output.length; c++) output[c][i] = (input[c] ?? input[0])[i] * this.gain;
48
+ }
49
+
50
+ return true;
51
+ }
52
+ }
53
+
54
+ registerProcessor('toneflix-gate', ToneflixGate);
55
+ `;
56
+
57
+ /** A noise gate with threshold, hold and range. */
58
+ export default makePlugin({
59
+ identifier: 'net.toneflix.studio.gate',
60
+ name: 'Gate',
61
+ worklet: processor,
62
+ params: {
63
+ threshold: { label: 'Threshold', defaultValue: -40, minValue: -80, maxValue: 0, step: 0.5, units: 'dB' },
64
+ attack: { label: 'Attack', defaultValue: 1, minValue: 0.1, maxValue: 50, step: 0.1, units: 'ms' },
65
+ hold: { label: 'Hold', defaultValue: 50, minValue: 0, maxValue: 500, step: 5, units: 'ms' },
66
+ release: { label: 'Release', defaultValue: 120, minValue: 5, maxValue: 1000, step: 5, units: 'ms' },
67
+ range: { label: 'Range', defaultValue: -80, minValue: -80, maxValue: 0, step: 1, units: 'dB' },
68
+ },
69
+ build(context, node) {
70
+ const gate = new AudioWorkletNode(context, 'toneflix-gate', { outputChannelCount: [2] });
71
+
72
+ node.connectInput(gate).connect(node.output);
73
+
74
+ return { gate };
75
+ },
76
+ apply({ gate }, v, _transport, context) {
77
+ for (const id of ['threshold', 'attack', 'hold', 'release', 'range']) gate.parameters.get(id).setValueAtTime(v[id], context.currentTime);
78
+ },
79
+ });
Binary file
@@ -0,0 +1,153 @@
1
+ import { makePlugin } from './base.js';
2
+
3
+ /**
4
+ * A port of KBVerb by Corey Kereliuk (MIT), a reverb in the style of Keith Barr's
5
+ * allpass loop: chains of allpass pairs with prime lengths, fed back on themselves.
6
+ */
7
+ const processor = `
8
+ const primes = [];
9
+
10
+ for (let n = 2; primes.length < 300; n++) if (primes.every((p) => p * p > n || n % p)) primes.push(n);
11
+
12
+ class Line {
13
+ constructor(size) {
14
+ this.data = new Float32Array(size);
15
+ this.at = 0;
16
+ }
17
+
18
+ /** The sample written \`delay\` steps ago, then write \`value\`. */
19
+ step(value, delay) {
20
+ const size = this.data.length;
21
+ const out = this.data[(this.at - delay + size) % size];
22
+
23
+ this.data[this.at] = value;
24
+ this.at = (this.at + 1) % size;
25
+
26
+ return out;
27
+ }
28
+ }
29
+
30
+ /** A Schroeder allpass of length n + 1 and gain g. */
31
+ class Allpass {
32
+ constructor(n, g) {
33
+ this.n = n + 1;
34
+ this.g = g;
35
+ this.line = new Line(this.n + 1);
36
+ this.last = 0;
37
+ }
38
+
39
+ step(x) {
40
+ const s = x - this.g * this.last;
41
+ const out = this.last + this.g * s;
42
+
43
+ this.last = this.line.step(s, this.n - 1);
44
+
45
+ return out;
46
+ }
47
+ }
48
+
49
+ /** Two allpasses, then a plain delay of three quarters of their lengths. */
50
+ class Section {
51
+ constructor([n1, n2]) {
52
+ this.a = new Allpass(n1, 0.7);
53
+ this.b = new Allpass(n2, 0.7);
54
+ this.length = Math.floor(0.75 * (n1 + n2));
55
+ this.line = new Line(this.length + 1);
56
+ }
57
+
58
+ step(x) {
59
+ return this.line.step(this.b.step(this.a.step(x)), this.length);
60
+ }
61
+ }
62
+
63
+ /** One channel: the input feeds every section; the last one feeds back through its own section. */
64
+ class Tank {
65
+ constructor(forward, back) {
66
+ this.sections = forward.map((pair) => new Section(pair));
67
+ this.back = new Section(back);
68
+ this.fed = 0;
69
+ }
70
+
71
+ step(x, feedback) {
72
+ let input = x + this.fed;
73
+ let sum = 0;
74
+
75
+ for (const section of this.sections) {
76
+ const out = section.step(input);
77
+
78
+ sum += out;
79
+ input = out + x;
80
+ }
81
+
82
+ this.fed = this.back.step(input) * feedback;
83
+
84
+ return sum;
85
+ }
86
+ }
87
+
88
+ const pairs = (indices) => indices.map((i) => [primes[i], primes[i + 1]]);
89
+
90
+ class ToneflixKBVerb extends AudioWorkletProcessor {
91
+ static get parameterDescriptors() {
92
+ return [{ name: 'feedback', defaultValue: 0.5, minValue: 0, maxValue: 1, automationRate: 'k-rate' }];
93
+ }
94
+
95
+ constructor() {
96
+ super();
97
+ this.left = new Tank(pairs([110, 120, 140, 180, 260]), [primes[100], primes[101]]);
98
+ this.right = new Tank(pairs([111, 122, 144, 188]), [primes[97], primes[99]]);
99
+ this.feedback = 0.5;
100
+ }
101
+
102
+ process([input], [output], parameters) {
103
+ const target = parameters.feedback[0];
104
+ const l = input[0];
105
+ const r = input[1] ?? input[0];
106
+
107
+ for (let i = 0; i < output[0].length; i++) {
108
+ const inL = l ? l[i] : 0;
109
+ const inR = r ? r[i] : 0;
110
+
111
+ this.feedback = target + (this.feedback - target) * 0.99;
112
+ // The original rotates the inputs a quarter turn before the tanks
113
+ output[0][i] = this.left.step(-0.5 * inR, this.feedback);
114
+ if (output[1]) output[1][i] = this.right.step(0.5 * inL, this.feedback);
115
+ }
116
+
117
+ return true;
118
+ }
119
+ }
120
+
121
+ registerProcessor('toneflix-kbverb', ToneflixKBVerb);
122
+ `;
123
+
124
+ export default makePlugin({
125
+ identifier: 'net.toneflix.studio.kbverb',
126
+ name: 'KB Verb',
127
+ worklet: processor,
128
+ params: {
129
+ feedback: { label: 'Feedback', defaultValue: 0.5, minValue: 0, maxValue: 0.98 },
130
+ tone: { label: 'Tone', defaultValue: 9000, minValue: 1000, maxValue: 16000, step: 50, units: 'Hz' },
131
+ mix: { label: 'Mix', defaultValue: 0.3, minValue: 0, maxValue: 1 },
132
+ },
133
+ build(context, node) {
134
+ const verb = new AudioWorkletNode(context, 'toneflix-kbverb', { outputChannelCount: [2] });
135
+ const tone = new BiquadFilterNode(context, { type: 'lowpass' });
136
+ const wet = new GainNode(context);
137
+ const dry = new GainNode(context);
138
+
139
+ node.connectInput(dry).connect(node.output);
140
+ node.connectInput(verb).connect(tone).connect(wet).connect(node.output);
141
+
142
+ return { verb, tone, wet, dry };
143
+ },
144
+ apply({ verb, tone, wet, dry }, v, _transport, context) {
145
+ const now = context.currentTime;
146
+
147
+ verb.parameters.get('feedback').setValueAtTime(v.feedback, now);
148
+ tone.frequency.setTargetAtTime(v.tone, now, 0.02);
149
+ // The tanks sum several sections, so the wet path is scaled down
150
+ wet.gain.setTargetAtTime(v.mix * 0.25, now, 0.02);
151
+ dry.gain.setTargetAtTime(1 - v.mix * 0.5, now, 0.02);
152
+ },
153
+ });
Binary file
@@ -0,0 +1,47 @@
1
+ import { makePlugin } from './base.js';
2
+
3
+ const gain = (db) => 10 ** (db / 20);
4
+
5
+ /** Clips anything past the ceiling, so peaks the compressor lets through never pass it. */
6
+ const clip = (ceiling) => {
7
+ const data = new Float32Array(2049);
8
+
9
+ for (let i = 0; i < data.length; i++) {
10
+ const x = (i / (data.length - 1)) * 2 - 1;
11
+ data[i] = Math.max(-ceiling, Math.min(ceiling, x));
12
+ }
13
+
14
+ return data;
15
+ };
16
+
17
+ /** A peak limiter: gain in, a fast compressor, then a hard ceiling. */
18
+ export default makePlugin({
19
+ identifier: 'net.toneflix.studio.limiter',
20
+ name: 'Limiter',
21
+ params: {
22
+ input: { label: 'Gain', defaultValue: 0, minValue: 0, maxValue: 24, step: 0.5, units: 'dB' },
23
+ ceiling: { label: 'Ceiling', defaultValue: -1, minValue: -12, maxValue: 0, step: 0.1, units: 'dB' },
24
+ release: { label: 'Release', defaultValue: 60, minValue: 5, maxValue: 500, step: 5, units: 'ms' },
25
+ },
26
+ build(context, node) {
27
+ const input = new GainNode(context);
28
+ const compressor = new DynamicsCompressorNode(context, { ratio: 20, knee: 0, attack: 0 });
29
+ const ceiling = new WaveShaperNode(context, { oversample: '4x' });
30
+
31
+ node.connectInput(input).connect(compressor).connect(ceiling).connect(node.output);
32
+
33
+ return { input, compressor, ceiling, level: NaN };
34
+ },
35
+ apply(parts, v, _transport, context) {
36
+ const now = context.currentTime;
37
+
38
+ if (parts.level !== v.ceiling) {
39
+ parts.ceiling.curve = clip(gain(v.ceiling));
40
+ parts.level = v.ceiling;
41
+ }
42
+
43
+ parts.input.gain.setTargetAtTime(gain(v.input), now, 0.02);
44
+ parts.compressor.threshold.setValueAtTime(v.ceiling - 2, now);
45
+ parts.compressor.release.setValueAtTime(v.release / 1000, now);
46
+ },
47
+ });
Binary file