@toneflix/studio-wams 0.0.0-stage → 0.6.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.
@@ -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
@@ -0,0 +1,47 @@
1
+ import { makePlugin } from './base.js';
2
+
3
+ const STAGES = 6;
4
+
5
+ /** A six-stage phaser with an LFO-swept centre and feedback. */
6
+ export default makePlugin({
7
+ identifier: 'net.toneflix.studio.phaser',
8
+ name: 'Phaser',
9
+ params: {
10
+ rate: { label: 'Rate', defaultValue: 0.4, minValue: 0.05, maxValue: 5, step: 0.05, units: 'Hz' },
11
+ centre: { label: 'Centre', defaultValue: 900, minValue: 200, maxValue: 4000, step: 10, units: 'Hz' },
12
+ depth: { label: 'Depth', defaultValue: 0.7, minValue: 0, maxValue: 1 },
13
+ feedback: { label: 'Feedback', defaultValue: 0.4, minValue: 0, maxValue: 0.9 },
14
+ mix: { label: 'Mix', defaultValue: 0.5, minValue: 0, maxValue: 1 },
15
+ },
16
+ build(context, node) {
17
+ const lfo = new OscillatorNode(context, { type: 'sine' });
18
+ const sweep = new GainNode(context);
19
+ const stages = Array.from({ length: STAGES }, () => new BiquadFilterNode(context, { type: 'allpass', Q: 0.5 }));
20
+ const feedback = new GainNode(context);
21
+ // A loop needs a delay; this one is a single render block
22
+ const loop = new DelayNode(context, { delayTime: 128 / context.sampleRate, maxDelayTime: 0.01 });
23
+ const wet = new GainNode(context);
24
+ const dry = new GainNode(context);
25
+
26
+ lfo.connect(sweep);
27
+ node.connectInput(stages[0]);
28
+ for (let i = 1; i < STAGES; i++) stages[i - 1].connect(stages[i]);
29
+ for (const stage of stages) sweep.connect(stage.frequency);
30
+ stages[STAGES - 1].connect(feedback).connect(loop).connect(stages[0]);
31
+ stages[STAGES - 1].connect(wet).connect(node.output);
32
+ node.connectInput(dry).connect(node.output);
33
+ lfo.start();
34
+
35
+ return { lfo, sweep, stages, feedback, wet, dry, stop: () => lfo.stop() };
36
+ },
37
+ apply({ lfo, sweep, stages, feedback, wet, dry }, v, _transport, context) {
38
+ const now = context.currentTime;
39
+
40
+ lfo.frequency.setTargetAtTime(v.rate, now, 0.02);
41
+ sweep.gain.setTargetAtTime(v.centre * v.depth * 0.9, now, 0.02);
42
+ for (const stage of stages) stage.frequency.setTargetAtTime(v.centre, now, 0.02);
43
+ feedback.gain.setTargetAtTime(v.feedback, now, 0.02);
44
+ wet.gain.setTargetAtTime(v.mix, now, 0.02);
45
+ dry.gain.setTargetAtTime(1 - v.mix * 0.5, now, 0.02);
46
+ },
47
+ });
Binary file
@@ -0,0 +1,58 @@
1
+ import { makePlugin } from './base.js';
2
+
3
+ /** A stereo impulse response of decaying noise, the same every time for a given length. */
4
+ const impulse = (context, seconds) => {
5
+ const length = Math.max(1, Math.floor(context.sampleRate * seconds));
6
+ const buffer = context.createBuffer(2, length, context.sampleRate);
7
+ let seed = 1;
8
+ const random = () => {
9
+ seed = (seed * 1664525 + 1013904223) >>> 0;
10
+ return seed / 2 ** 31 - 1;
11
+ };
12
+
13
+ for (let channel = 0; channel < 2; channel++) {
14
+ const data = buffer.getChannelData(channel);
15
+
16
+ // Falls 60 dB over the length
17
+ for (let i = 0; i < length; i++) data[i] = random() * Math.exp((-6.9 * i) / length);
18
+ }
19
+
20
+ return buffer;
21
+ };
22
+
23
+ /** A room-to-hall reverb with decay, pre-delay and tone. */
24
+ export default makePlugin({
25
+ identifier: 'net.toneflix.studio.reverb',
26
+ name: 'Reverb',
27
+ params: {
28
+ decay: { label: 'Decay', defaultValue: 2, minValue: 0.2, maxValue: 8, step: 0.1, units: 's' },
29
+ predelay: { label: 'Pre-delay', defaultValue: 20, minValue: 0, maxValue: 200, step: 1, units: 'ms' },
30
+ tone: { label: 'Tone', defaultValue: 7000, minValue: 1000, maxValue: 16000, step: 50, units: 'Hz' },
31
+ mix: { label: 'Mix', defaultValue: 0.25, minValue: 0, maxValue: 1 },
32
+ },
33
+ build(context, node) {
34
+ const predelay = new DelayNode(context, { maxDelayTime: 0.2 });
35
+ const convolver = new ConvolverNode(context);
36
+ const tone = new BiquadFilterNode(context, { type: 'lowpass' });
37
+ const wet = new GainNode(context);
38
+ const dry = new GainNode(context);
39
+
40
+ node.connectInput(dry).connect(node.output);
41
+ node.connectInput(predelay).connect(convolver).connect(tone).connect(wet).connect(node.output);
42
+
43
+ return { predelay, convolver, tone, wet, dry, decay: -1 };
44
+ },
45
+ apply(parts, v, _transport, context) {
46
+ const now = context.currentTime;
47
+
48
+ if (parts.decay !== v.decay) {
49
+ parts.convolver.buffer = impulse(context, v.decay);
50
+ parts.decay = v.decay;
51
+ }
52
+
53
+ parts.predelay.delayTime.setTargetAtTime(v.predelay / 1000, now, 0.02);
54
+ parts.tone.frequency.setTargetAtTime(v.tone, now, 0.02);
55
+ parts.wet.gain.setTargetAtTime(v.mix, now, 0.02);
56
+ parts.dry.gain.setTargetAtTime(1 - v.mix * 0.5, now, 0.02);
57
+ },
58
+ });
Binary file
@@ -0,0 +1,142 @@
1
+ import { makePlugin } from './base.js';
2
+
3
+ /**
4
+ * A port of Stone Phaser by Jean Pierre Cimalando (CC0-1.0), a model of a four-stage
5
+ * analog phaser pedal after Kiiski, Esqueda and Välimäki, "Time-variant gray-box
6
+ * modeling of a phaser pedal" (DAFx-16).
7
+ */
8
+ const processor = `
9
+ const smoothing = Math.exp(-1 / (0.1 * sampleRate));
10
+ const key = (hz) => 69 + 12 * Math.log2(hz / 440);
11
+ const hz = (k) => 440 * 2 ** ((k - 69) / 12);
12
+
13
+ /** The rounded triangle the pedal's LFO makes, from 0 to 1 over a cycle. */
14
+ const roundness = 0.975;
15
+ const shape = (pos) => 1 - Math.sin(2 * (pos < 0.5 ? pos : 1 - pos) * Math.asin(roundness)) / roundness;
16
+
17
+ class Smooth {
18
+ constructor(value) {
19
+ this.value = value;
20
+ }
21
+
22
+ step(target) {
23
+ return (this.value = (1 - smoothing) * target + smoothing * this.value);
24
+ }
25
+ }
26
+
27
+ /** One-pole highpass. */
28
+ class Highpass {
29
+ x1 = 0;
30
+ y1 = 0;
31
+
32
+ step(x, f) {
33
+ const p = Math.exp((-2 * Math.PI * f) / sampleRate);
34
+ const y = 0.5 * (1 + p) * (x - this.x1) + p * this.y1;
35
+
36
+ this.x1 = x;
37
+ this.y1 = y;
38
+
39
+ return y;
40
+ }
41
+ }
42
+
43
+ /** One-pole allpass. */
44
+ class Allpass {
45
+ x1 = 0;
46
+ y1 = 0;
47
+
48
+ step(x, a) {
49
+ const y = a * x + this.x1 - a * this.y1;
50
+
51
+ this.x1 = x;
52
+ this.y1 = y;
53
+
54
+ return y;
55
+ }
56
+ }
57
+
58
+ class Channel {
59
+ input = new Highpass();
60
+ loop = new Highpass();
61
+ stages = [new Allpass(), new Allpass(), new Allpass(), new Allpass()];
62
+ last = 0;
63
+
64
+ step(x, a, feedback, cut) {
65
+ let y = this.input.step(x, 33) + this.loop.step(this.last, cut) * feedback;
66
+
67
+ for (const stage of this.stages) y = stage.step(y, a);
68
+
69
+ return (this.last = y);
70
+ }
71
+ }
72
+
73
+ class ToneflixStonePhaser extends AudioWorkletProcessor {
74
+ static get parameterDescriptors() {
75
+ return ['color', 'rate', 'feedback', 'cut', 'mix', 'phase'].map((name) => ({ name, defaultValue: 0, automationRate: 'k-rate' }));
76
+ }
77
+
78
+ constructor() {
79
+ super();
80
+ this.channels = [new Channel(), new Channel()];
81
+ this.pos = 0;
82
+ this.s = { rate: new Smooth(0.2), feedback: new Smooth(0.75), cut: new Smooth(500), wet: new Smooth(0.7), dry: new Smooth(0.7), phase: new Smooth(1), low: new Smooth(key(80)), high: new Smooth(key(2200)), colorFeedback: new Smooth(0.75) };
83
+ }
84
+
85
+ process([input], [output], p) {
86
+ const color = p.color[0] >= 0.5;
87
+ const mix = p.mix[0] / 100;
88
+ const s = this.s;
89
+
90
+ for (let i = 0; i < output[0].length; i++) {
91
+ const rate = s.rate.step(p.rate[0]);
92
+ const feedback = s.feedback.step(p.feedback[0] / 100);
93
+ const cut = s.cut.step(p.cut[0]);
94
+ const wet = s.wet.step(Math.sin((mix * Math.PI) / 2));
95
+ const dry = s.dry.step(Math.cos((mix * Math.PI) / 2));
96
+ const phase = s.phase.step(p.phase[0] / 360 + 1);
97
+ const low = s.low.step(key(color ? 80 : 300));
98
+ const high = s.high.step(key(color ? 2200 : 6000));
99
+ const loop = s.colorFeedback.step(color ? feedback : 0.1 * feedback);
100
+
101
+ this.pos = (this.pos + rate / sampleRate) % 1;
102
+
103
+ for (let c = 0; c < output.length; c++) {
104
+ const pos = c ? (this.pos + phase) % 1 : this.pos;
105
+ const f = hz(low + shape(pos) * (high - low));
106
+ const a = -1 + (2 * Math.PI * f) / sampleRate;
107
+ const x = (input[c] ?? input[0])?.[i] ?? 0;
108
+
109
+ output[c][i] = x * dry + this.channels[c].step(x, a, loop, cut) * wet;
110
+ }
111
+ }
112
+
113
+ return true;
114
+ }
115
+ }
116
+
117
+ registerProcessor('toneflix-stone-phaser', ToneflixStonePhaser);
118
+ `;
119
+
120
+ export default makePlugin({
121
+ identifier: 'net.toneflix.studio.stone-phaser',
122
+ name: 'Stone Phaser',
123
+ worklet: processor,
124
+ params: {
125
+ rate: { label: 'Rate', defaultValue: 0.2, minValue: 0.01, maxValue: 5, step: 0.01, units: 'Hz' },
126
+ feedback: { label: 'Feedback', defaultValue: 75, minValue: 0, maxValue: 99, step: 1, units: '%' },
127
+ cut: { label: 'Feedback bass cut', defaultValue: 500, minValue: 10, maxValue: 5000, step: 10, units: 'Hz' },
128
+ color: { label: 'Color', defaultValue: 1, minValue: 0, maxValue: 1, step: 1 },
129
+ phase: { label: 'Stereo phase', defaultValue: 0, minValue: -180, maxValue: 180, step: 1, units: '°' },
130
+ mix: { label: 'Mix', defaultValue: 50, minValue: 0, maxValue: 100, step: 1, units: '%' },
131
+ },
132
+ build(context, node) {
133
+ const phaser = new AudioWorkletNode(context, 'toneflix-stone-phaser', { outputChannelCount: [2] });
134
+
135
+ node.connectInput(phaser).connect(node.output);
136
+
137
+ return { phaser };
138
+ },
139
+ apply({ phaser }, v, _transport, context) {
140
+ for (const id of ['color', 'rate', 'feedback', 'cut', 'mix', 'phase']) phaser.parameters.get(id).setValueAtTime(v[id], context.currentTime);
141
+ },
142
+ });