@nodetool-ai/audio-nodes 0.8.1

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 (59) hide show
  1. package/README.md +62 -0
  2. package/dist/.nodetool-build-stamp +1 -0
  3. package/dist/index.d.ts +12 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +14 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/lib/audio-context.d.ts +7 -0
  8. package/dist/lib/audio-context.d.ts.map +1 -0
  9. package/dist/lib/audio-context.js +42 -0
  10. package/dist/lib/audio-context.js.map +1 -0
  11. package/dist/lib/audio-wav.d.ts +133 -0
  12. package/dist/lib/audio-wav.d.ts.map +1 -0
  13. package/dist/lib/audio-wav.js +504 -0
  14. package/dist/lib/audio-wav.js.map +1 -0
  15. package/dist/lib/biquad.d.ts +48 -0
  16. package/dist/lib/biquad.d.ts.map +1 -0
  17. package/dist/lib/biquad.js +127 -0
  18. package/dist/lib/biquad.js.map +1 -0
  19. package/dist/lib/cv.d.ts +141 -0
  20. package/dist/lib/cv.d.ts.map +1 -0
  21. package/dist/lib/cv.js +261 -0
  22. package/dist/lib/cv.js.map +1 -0
  23. package/dist/lib/synth-dsp.d.ts +43 -0
  24. package/dist/lib/synth-dsp.d.ts.map +1 -0
  25. package/dist/lib/synth-dsp.js +88 -0
  26. package/dist/lib/synth-dsp.js.map +1 -0
  27. package/dist/nodes/audio.d.ts +527 -0
  28. package/dist/nodes/audio.d.ts.map +1 -0
  29. package/dist/nodes/audio.js +1816 -0
  30. package/dist/nodes/audio.js.map +1 -0
  31. package/dist/nodes/game.d.ts +68 -0
  32. package/dist/nodes/game.d.ts.map +1 -0
  33. package/dist/nodes/game.js +304 -0
  34. package/dist/nodes/game.js.map +1 -0
  35. package/dist/nodes/lib-audio-dsp.d.ts +146 -0
  36. package/dist/nodes/lib-audio-dsp.d.ts.map +1 -0
  37. package/dist/nodes/lib-audio-dsp.js +477 -0
  38. package/dist/nodes/lib-audio-dsp.js.map +1 -0
  39. package/dist/nodes/lib-audio-effects.d.ts +174 -0
  40. package/dist/nodes/lib-audio-effects.d.ts.map +1 -0
  41. package/dist/nodes/lib-audio-effects.js +962 -0
  42. package/dist/nodes/lib-audio-effects.js.map +1 -0
  43. package/dist/nodes/output.d.ts +29 -0
  44. package/dist/nodes/output.d.ts.map +1 -0
  45. package/dist/nodes/output.js +179 -0
  46. package/dist/nodes/output.js.map +1 -0
  47. package/dist/nodes/preview.d.ts +26 -0
  48. package/dist/nodes/preview.d.ts.map +1 -0
  49. package/dist/nodes/preview.js +44 -0
  50. package/dist/nodes/preview.js.map +1 -0
  51. package/dist/nodes/realtime-audio.d.ts +118 -0
  52. package/dist/nodes/realtime-audio.d.ts.map +1 -0
  53. package/dist/nodes/realtime-audio.js +391 -0
  54. package/dist/nodes/realtime-audio.js.map +1 -0
  55. package/dist/nodes/synthesis.d.ts +194 -0
  56. package/dist/nodes/synthesis.d.ts.map +1 -0
  57. package/dist/nodes/synthesis.js +955 -0
  58. package/dist/nodes/synthesis.js.map +1 -0
  59. package/package.json +80 -0
@@ -0,0 +1,955 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ /**
8
+ * Modular synthesis nodes (`nodetool.audio.synth.*`) — a subtractive synth
9
+ * voice operating on streamed signal chunks: oscillator, LFO, ADSR, gate
10
+ * source, VCA, VCF, CV utilities, and a mixer.
11
+ *
12
+ * Timing is sample-domain: time = sample count; the wall clock only paces
13
+ * emission. Free-running generators (Oscillator, LFO, Gate) are realtime
14
+ * modules: they emit forever, paced by `RealtimePacer`, until the run is
15
+ * cancelled (pacing delays emission but never changes signal content).
16
+ * Driven nodes derive all timing from their input chunks (one output chunk
17
+ * per input chunk, same frame count), so a patch driven by one Gate source
18
+ * is sample-aligned by construction.
19
+ *
20
+ * Control voltage (CV) is a first-class socket type riding the chunk wire
21
+ * shape with `encoding: "f32le"` (see ../lib/cv.ts) — interchangeable with
22
+ * audio chunk streams, Eurorack-style. Pitch CV is volts/octave equivalent:
23
+ * `freq = base_frequency * 2^cv`, cv in octaves.
24
+ *
25
+ * Limitation: audio-rate feedback patching (e.g. an envelope modulating its
26
+ * own gate) is not supported. The only cycles a workflow graph may contain
27
+ * are Loop node iterations (docs/workflow-loops.md), one value per iteration.
28
+ */
29
+ import { BaseNode, prop } from "@nodetool-ai/node-sdk";
30
+ import { tagAsHybrid } from "@nodetool-ai/nodes-utils";
31
+ import { deinterleave, interleave } from "../lib/audio-wav.js";
32
+ import { alignedSignalStreams, CHUNK_PROP_DEFAULT, makeDoneSignalChunk, makeSignalChunk, readSignalChunk, RealtimePacer, SampleFifo, SYNTH_CHUNK_FRAMES, SYNTH_SAMPLE_RATE } from "../lib/cv.js";
33
+ import { biquadCoeffs, createBiquadState, processBiquad, DEFAULT_Q } from "../lib/biquad.js";
34
+ import { adsrCoeff, adsrStep, createAdsrState, GATE_THRESHOLD, oscSample } from "../lib/synth-dsp.js";
35
+ // ── Shared generator scaffolding ───────────────────────────────────
36
+ /**
37
+ * Run a free-running generator loop: calls `fill` to produce each chunk's
38
+ * samples, paces emission with the wall clock, and runs until cancellation
39
+ * (`signal`) — free-running synth sources have no other end; they play
40
+ * until the patch is stopped. Pacing is what keeps the infinite stream
41
+ * legal: inbox buffering is unbounded by default, and an unpaced infinite
42
+ * producer would exhaust memory.
43
+ */
44
+ async function runGenerator(outputs, opts) {
45
+ const { slot, sampleRate, encoding, signal } = opts;
46
+ const pacer = new RealtimePacer(signal);
47
+ const chunkMs = (SYNTH_CHUNK_FRAMES / sampleRate) * 1000;
48
+ let emitted = 0;
49
+ while (await pacer.waitNext(chunkMs)) {
50
+ const buf = new Float32Array(SYNTH_CHUNK_FRAMES);
51
+ opts.fill(buf, emitted);
52
+ await outputs.emit(slot, makeSignalChunk(buf, sampleRate, 1, false, encoding));
53
+ emitted += SYNTH_CHUNK_FRAMES;
54
+ }
55
+ await outputs.emit(slot, makeDoneSignalChunk(sampleRate, 1, encoding));
56
+ }
57
+ /** Channel 0 of an interleaved chunk — CV inputs are conceptually mono. */
58
+ function chunkMono(samples, channels) {
59
+ const ch = Math.max(1, channels);
60
+ if (ch === 1)
61
+ return samples;
62
+ const frames = Math.floor(samples.length / ch);
63
+ const out = new Float32Array(frames);
64
+ for (let i = 0; i < frames; i++)
65
+ out[i] = samples[i * ch];
66
+ return out;
67
+ }
68
+ // ── Oscillator (VCO) ───────────────────────────────────────────────
69
+ export class OscillatorNode extends BaseNode {
70
+ static nodeType = "nodetool.audio.synth.Oscillator";
71
+ static title = "Oscillator";
72
+ static description = "Voltage-controlled oscillator generating sine, saw, square, triangle or noise audio.\n audio, synthesis, oscillator, vco, modular\n\n Pitch CV is volts/octave: freq = frequency * 2^cv (cv in octaves). With pitch_cv or fm patched, output follows that stream's chunk cadence (sample-aligned); otherwise it free-runs wall-clock paced until the run is stopped. Naive waveforms — saw/square alias at high frequencies.\n\n Use cases:\n - Sound source for a modular synth voice\n - FM/vibrato via the fm input driven by an LFO\n - Melodic sequences via a pitch CV stream";
73
+ static metadataOutputTypes = { chunk: "chunk" };
74
+ static inlineFields = [];
75
+ static inputFields = ["pitch_cv", "fm"];
76
+ static isStreamingInput = true;
77
+ // Required by BaseNode but unused for streaming
78
+ async process() {
79
+ return {};
80
+ }
81
+ /** Current property values — read per chunk so live updates apply. */
82
+ params() {
83
+ return {
84
+ waveform: this.waveform,
85
+ baseFreq: this.frequency,
86
+ amplitude: this.amplitude,
87
+ pulseWidth: this.pulse_width,
88
+ fmAmount: this.fm_amount
89
+ };
90
+ }
91
+ async run(inputs, outputs) {
92
+ // Double-precision phase accumulator carried across chunks — no drift.
93
+ // Properties are re-read at every chunk boundary (params()), so live
94
+ // updates pushed via applyProperties land on the next chunk.
95
+ let phase = 0;
96
+ const sampleAt = (p, freq, sampleRate) => {
97
+ const f = Math.min(Math.max(freq, 0), sampleRate / 2 - 1e-6);
98
+ const out = p.amplitude * oscSample(p.waveform, phase, p.pulseWidth);
99
+ phase = (phase + f / sampleRate) % 1;
100
+ return out;
101
+ };
102
+ const hasPitch = inputs.hasStream("pitch_cv");
103
+ const hasFm = inputs.hasStream("fm");
104
+ if (hasPitch || hasFm) {
105
+ // Clocked: the patched CV stream drives chunk cadence; the other
106
+ // handle (if any) is frame-aligned via zip-with-hold.
107
+ const primary = hasPitch ? "pitch_cv" : "fm";
108
+ const secondary = hasPitch ? "fm" : "pitch_cv";
109
+ let sampleRate = SYNTH_SAMPLE_RATE;
110
+ for await (const frame of alignedSignalStreams(inputs, {
111
+ primary,
112
+ cvHandles: [secondary]
113
+ })) {
114
+ const p = this.params();
115
+ sampleRate = frame.primary.sampleRate;
116
+ const drive = chunkMono(frame.primary.samples, frame.primary.channels);
117
+ const other = frame.cv[secondary];
118
+ const buf = new Float32Array(drive.length);
119
+ for (let i = 0; i < drive.length; i++) {
120
+ const pitch = hasPitch ? drive[i] : (other?.[i] ?? 0);
121
+ const fm = hasPitch ? (other?.[i] ?? 0) : drive[i];
122
+ const freq = p.baseFreq * Math.pow(2, pitch) * (1 + p.fmAmount * fm);
123
+ buf[i] = sampleAt(p, freq, sampleRate);
124
+ }
125
+ await outputs.emit("chunk", makeSignalChunk(buf, sampleRate, 1, false, "pcm16le"));
126
+ }
127
+ await outputs.emit("chunk", makeDoneSignalChunk(sampleRate, 1, "pcm16le"));
128
+ return;
129
+ }
130
+ const sampleRate = this.sample_rate;
131
+ await runGenerator(outputs, {
132
+ slot: "chunk",
133
+ sampleRate,
134
+ encoding: "pcm16le",
135
+ signal: inputs.signal,
136
+ fill: (buf) => {
137
+ const p = this.params();
138
+ for (let i = 0; i < buf.length; i++) {
139
+ buf[i] = sampleAt(p, p.baseFreq, sampleRate);
140
+ }
141
+ }
142
+ });
143
+ }
144
+ }
145
+ __decorate([
146
+ prop({
147
+ type: "cv",
148
+ default: CHUNK_PROP_DEFAULT,
149
+ title: "Pitch CV",
150
+ description: "Pitch control stream in octaves (volts/octave): freq = frequency * 2^cv. When patched, drives the output chunk cadence."
151
+ })
152
+ ], OscillatorNode.prototype, "pitch_cv", void 0);
153
+ __decorate([
154
+ prop({
155
+ type: "cv",
156
+ default: CHUNK_PROP_DEFAULT,
157
+ title: "FM",
158
+ description: "Linear frequency modulation stream: freq is multiplied by (1 + fm_amount * fm)."
159
+ })
160
+ ], OscillatorNode.prototype, "fm", void 0);
161
+ __decorate([
162
+ prop({
163
+ type: "enum",
164
+ default: "sine",
165
+ title: "Waveform",
166
+ description: "Oscillator waveform.",
167
+ values: ["sine", "saw", "square", "triangle", "noise"]
168
+ })
169
+ ], OscillatorNode.prototype, "waveform", void 0);
170
+ __decorate([
171
+ prop({
172
+ type: "float",
173
+ default: 220,
174
+ title: "Frequency",
175
+ description: "Base frequency in Hz.",
176
+ min: 0.1,
177
+ max: 20000
178
+ })
179
+ ], OscillatorNode.prototype, "frequency", void 0);
180
+ __decorate([
181
+ prop({
182
+ type: "float",
183
+ default: 0.8,
184
+ title: "Amplitude",
185
+ description: "Output amplitude (linear).",
186
+ min: 0,
187
+ max: 1
188
+ })
189
+ ], OscillatorNode.prototype, "amplitude", void 0);
190
+ __decorate([
191
+ prop({
192
+ type: "float",
193
+ default: 0.5,
194
+ title: "Pulse Width",
195
+ description: "Duty cycle for the square waveform.",
196
+ min: 0.05,
197
+ max: 0.95
198
+ })
199
+ ], OscillatorNode.prototype, "pulse_width", void 0);
200
+ __decorate([
201
+ prop({
202
+ type: "float",
203
+ default: 0,
204
+ title: "FM Amount",
205
+ description: "Depth of linear frequency modulation from the fm input.",
206
+ min: 0,
207
+ max: 10
208
+ })
209
+ ], OscillatorNode.prototype, "fm_amount", void 0);
210
+ __decorate([
211
+ prop({
212
+ type: "int",
213
+ default: SYNTH_SAMPLE_RATE,
214
+ title: "Sample Rate",
215
+ description: "Free-run sample rate in Hz.",
216
+ min: 8000,
217
+ max: 96000
218
+ })
219
+ ], OscillatorNode.prototype, "sample_rate", void 0);
220
+ // ── LFO ────────────────────────────────────────────────────────────
221
+ export class LfoNode extends BaseNode {
222
+ static nodeType = "nodetool.audio.synth.LFO";
223
+ static title = "LFO";
224
+ static description = "Low-frequency oscillator emitting a control-voltage stream.\n cv, synthesis, lfo, modulation, modular\n\n Output is offset + depth * wave(phase). With a clock patched, emits one chunk per clock chunk (sample-aligned) and resets phase on each clock rising edge; otherwise free-runs wall-clock paced until the run is stopped.\n\n Use cases:\n - Vibrato/tremolo via Oscillator fm or VCA cv\n - Filter sweeps via VCF cutoff_cv\n - Slow parameter drift in generative patches";
225
+ static metadataOutputTypes = { cv: "cv" };
226
+ static inlineFields = [];
227
+ static inputFields = ["clock"];
228
+ static isStreamingInput = true;
229
+ // Required by BaseNode but unused for streaming
230
+ async process() {
231
+ return {};
232
+ }
233
+ /** Current property values — read per chunk so live updates apply. */
234
+ params() {
235
+ return {
236
+ waveform: this.waveform,
237
+ rateHz: this.rate_hz,
238
+ depth: this.depth,
239
+ offset: this.offset
240
+ };
241
+ }
242
+ async run(inputs, outputs) {
243
+ let phase = 0;
244
+ if (inputs.hasStream("clock")) {
245
+ let sampleRate = SYNTH_SAMPLE_RATE;
246
+ let clockWasHigh = false;
247
+ for await (const item of inputs.stream("clock")) {
248
+ const chunk = readSignalChunk(item);
249
+ if (!chunk)
250
+ continue;
251
+ if (chunk.done)
252
+ break;
253
+ if (chunk.samples.length === 0)
254
+ continue;
255
+ const p = this.params();
256
+ sampleRate = chunk.sampleRate;
257
+ const clock = chunkMono(chunk.samples, chunk.channels);
258
+ const buf = new Float32Array(clock.length);
259
+ for (let i = 0; i < clock.length; i++) {
260
+ const high = clock[i] >= GATE_THRESHOLD;
261
+ if (high && !clockWasHigh)
262
+ phase = 0; // sample-accurate reset
263
+ clockWasHigh = high;
264
+ buf[i] = p.offset + p.depth * oscSample(p.waveform, phase);
265
+ phase = (phase + p.rateHz / sampleRate) % 1;
266
+ }
267
+ await outputs.emit("cv", makeSignalChunk(buf, sampleRate, 1, false, "f32le"));
268
+ }
269
+ await outputs.emit("cv", makeDoneSignalChunk(sampleRate, 1, "f32le"));
270
+ return;
271
+ }
272
+ const sampleRate = this.sample_rate;
273
+ await runGenerator(outputs, {
274
+ slot: "cv",
275
+ sampleRate,
276
+ encoding: "f32le",
277
+ signal: inputs.signal,
278
+ fill: (buf) => {
279
+ const p = this.params();
280
+ for (let i = 0; i < buf.length; i++) {
281
+ buf[i] = p.offset + p.depth * oscSample(p.waveform, phase);
282
+ phase = (phase + p.rateHz / sampleRate) % 1;
283
+ }
284
+ }
285
+ });
286
+ }
287
+ }
288
+ __decorate([
289
+ prop({
290
+ type: "cv",
291
+ default: CHUNK_PROP_DEFAULT,
292
+ title: "Clock",
293
+ description: "Optional clock stream: drives chunk cadence; phase resets on each rising edge."
294
+ })
295
+ ], LfoNode.prototype, "clock", void 0);
296
+ __decorate([
297
+ prop({
298
+ type: "enum",
299
+ default: "sine",
300
+ title: "Waveform",
301
+ description: "LFO waveform.",
302
+ values: ["sine", "triangle", "saw", "square"]
303
+ })
304
+ ], LfoNode.prototype, "waveform", void 0);
305
+ __decorate([
306
+ prop({
307
+ type: "float",
308
+ default: 2,
309
+ title: "Rate Hz",
310
+ description: "LFO frequency in Hz.",
311
+ min: 0.01,
312
+ max: 100
313
+ })
314
+ ], LfoNode.prototype, "rate_hz", void 0);
315
+ __decorate([
316
+ prop({
317
+ type: "float",
318
+ default: 1,
319
+ title: "Depth",
320
+ description: "Output amplitude scale.",
321
+ min: 0,
322
+ max: 10
323
+ })
324
+ ], LfoNode.prototype, "depth", void 0);
325
+ __decorate([
326
+ prop({
327
+ type: "float",
328
+ default: 0,
329
+ title: "Offset",
330
+ description: "Constant added to the output.",
331
+ min: -10,
332
+ max: 10
333
+ })
334
+ ], LfoNode.prototype, "offset", void 0);
335
+ __decorate([
336
+ prop({
337
+ type: "int",
338
+ default: SYNTH_SAMPLE_RATE,
339
+ title: "Sample Rate",
340
+ description: "Free-run sample rate in Hz.",
341
+ min: 8000,
342
+ max: 96000
343
+ })
344
+ ], LfoNode.prototype, "sample_rate", void 0);
345
+ // ── ADSR ───────────────────────────────────────────────────────────
346
+ export class AdsrNode extends BaseNode {
347
+ static nodeType = "nodetool.audio.synth.ADSR";
348
+ static title = "ADSR";
349
+ static description = "ADSR envelope generator driven by a gate CV stream.\n cv, synthesis, envelope, adsr, modular\n\n Gate edges (threshold 0.5) are detected at exact sample offsets inside chunks; attack/decay/release run as exponential one-pole segments counted in samples, so timing is sample-accurate without any clock. Emits one envelope chunk per gate chunk (same frame count). Retriggering mid-release continues from the current level.\n\n Use cases:\n - Amplitude envelope via VCA cv\n - Filter envelope via VCF cutoff_cv\n - Click-free gating of any signal";
350
+ static metadataOutputTypes = { cv: "cv" };
351
+ static inlineFields = [];
352
+ static inputFields = ["gate"];
353
+ static isStreamingInput = true;
354
+ // Required by BaseNode but unused for streaming
355
+ async process() {
356
+ return {};
357
+ }
358
+ async run(inputs, outputs) {
359
+ const state = createAdsrState();
360
+ let sampleRate = SYNTH_SAMPLE_RATE;
361
+ for await (const item of inputs.stream("gate")) {
362
+ const chunk = readSignalChunk(item);
363
+ if (!chunk)
364
+ continue;
365
+ if (chunk.done)
366
+ break;
367
+ if (chunk.samples.length === 0)
368
+ continue;
369
+ sampleRate = chunk.sampleRate;
370
+ // Coefficients recomputed per chunk from the current property values
371
+ // (three exp() calls per ~21 ms chunk) so live updates apply.
372
+ const params = {
373
+ attackCoeff: adsrCoeff(this.attack, sampleRate),
374
+ decayCoeff: adsrCoeff(this.decay, sampleRate),
375
+ releaseCoeff: adsrCoeff(this.release, sampleRate),
376
+ sustain: this.sustain
377
+ };
378
+ const gate = chunkMono(chunk.samples, chunk.channels);
379
+ const buf = new Float32Array(gate.length);
380
+ for (let i = 0; i < gate.length; i++) {
381
+ buf[i] = adsrStep(state, gate[i] >= GATE_THRESHOLD, params);
382
+ }
383
+ await outputs.emit("cv", makeSignalChunk(buf, sampleRate, 1, false, "f32le"));
384
+ }
385
+ await outputs.emit("cv", makeDoneSignalChunk(sampleRate, 1, "f32le"));
386
+ }
387
+ }
388
+ __decorate([
389
+ prop({
390
+ type: "cv",
391
+ default: CHUNK_PROP_DEFAULT,
392
+ title: "Gate",
393
+ description: "Gate CV stream — values ≥ 0.5 count as gate-high."
394
+ })
395
+ ], AdsrNode.prototype, "gate", void 0);
396
+ __decorate([
397
+ prop({
398
+ type: "float",
399
+ default: 0.01,
400
+ title: "Attack",
401
+ description: "Attack time in seconds.",
402
+ min: 0.0005,
403
+ max: 10
404
+ })
405
+ ], AdsrNode.prototype, "attack", void 0);
406
+ __decorate([
407
+ prop({
408
+ type: "float",
409
+ default: 0.1,
410
+ title: "Decay",
411
+ description: "Decay time in seconds.",
412
+ min: 0.0005,
413
+ max: 10
414
+ })
415
+ ], AdsrNode.prototype, "decay", void 0);
416
+ __decorate([
417
+ prop({
418
+ type: "float",
419
+ default: 0.7,
420
+ title: "Sustain",
421
+ description: "Sustain level (0–1).",
422
+ min: 0,
423
+ max: 1
424
+ })
425
+ ], AdsrNode.prototype, "sustain", void 0);
426
+ __decorate([
427
+ prop({
428
+ type: "float",
429
+ default: 0.3,
430
+ title: "Release",
431
+ description: "Release time in seconds.",
432
+ min: 0.0005,
433
+ max: 10
434
+ })
435
+ ], AdsrNode.prototype, "release", void 0);
436
+ // ── Gate ───────────────────────────────────────────────────────────
437
+ export class GateNode extends BaseNode {
438
+ static nodeType = "nodetool.audio.synth.Gate";
439
+ static title = "Gate";
440
+ static description = "Generates a repeating on/off gate CV pattern — the patch's master clock.\n cv, synthesis, gate, trigger, clock, modular\n\n Emits `amplitude` for on_duration seconds, 0 for off_duration, cycling wall-clock paced until the run is stopped. Drive an ADSR with it; every node downstream is sample-aligned to it by construction and paced with it.\n\n Use cases:\n - Trigger envelopes rhythmically\n - Master clock for clocked LFOs and sample & hold\n - Heartbeat of a live patch";
441
+ static metadataOutputTypes = { cv: "cv" };
442
+ static inlineFields = [];
443
+ static inputFields = [];
444
+ static isStreamingInput = true;
445
+ // Required by BaseNode but unused for streaming
446
+ async process() {
447
+ return {};
448
+ }
449
+ async run(inputs, outputs) {
450
+ const sampleRate = this.sample_rate;
451
+ await runGenerator(outputs, {
452
+ slot: "cv",
453
+ sampleRate,
454
+ encoding: "f32le",
455
+ signal: inputs.signal,
456
+ fill: (buf, startFrame) => {
457
+ // Timing re-read per chunk so live updates apply; the phase derives
458
+ // from the absolute frame counter, so a period change just re-grids
459
+ // the pattern instead of glitching it.
460
+ const onFrames = Math.max(1, Math.round(this.on_duration * sampleRate));
461
+ const periodFrames = onFrames +
462
+ Math.max(1, Math.round(this.off_duration * sampleRate));
463
+ const amplitude = this.amplitude;
464
+ for (let i = 0; i < buf.length; i++) {
465
+ buf[i] =
466
+ (startFrame + i) % periodFrames < onFrames ? amplitude : 0;
467
+ }
468
+ }
469
+ });
470
+ }
471
+ }
472
+ __decorate([
473
+ prop({
474
+ type: "float",
475
+ default: 0.25,
476
+ title: "On Duration",
477
+ description: "Gate-high time per cycle in seconds.",
478
+ min: 0.001,
479
+ max: 60
480
+ })
481
+ ], GateNode.prototype, "on_duration", void 0);
482
+ __decorate([
483
+ prop({
484
+ type: "float",
485
+ default: 0.25,
486
+ title: "Off Duration",
487
+ description: "Gate-low time per cycle in seconds.",
488
+ min: 0.001,
489
+ max: 60
490
+ })
491
+ ], GateNode.prototype, "off_duration", void 0);
492
+ __decorate([
493
+ prop({
494
+ type: "float",
495
+ default: 1,
496
+ title: "Amplitude",
497
+ description: "Gate-high output level.",
498
+ min: 0,
499
+ max: 10
500
+ })
501
+ ], GateNode.prototype, "amplitude", void 0);
502
+ __decorate([
503
+ prop({
504
+ type: "int",
505
+ default: SYNTH_SAMPLE_RATE,
506
+ title: "Sample Rate",
507
+ description: "Output sample rate in Hz.",
508
+ min: 8000,
509
+ max: 96000
510
+ })
511
+ ], GateNode.prototype, "sample_rate", void 0);
512
+ // ── VCA ────────────────────────────────────────────────────────────
513
+ export class VcaNode extends BaseNode {
514
+ static nodeType = "nodetool.audio.synth.VCA";
515
+ static title = "VCA";
516
+ static description = "Voltage-controlled amplifier: multiplies an audio stream by a CV stream.\n audio, cv, synthesis, vca, amplifier, modular\n\n Output = audio * gain * max(0, cv) per sample (negative CV is clamped — a VCA doesn't invert). The audio input drives chunk cadence; the CV is frame-aligned with hold-last when it lags or ends. With nothing patched into cv, acts as a plain gain.\n\n Use cases:\n - Shape a note with an ADSR envelope\n - Tremolo via an LFO\n - Sidechain-style ducking from any CV source";
517
+ static metadataOutputTypes = { chunk: "chunk" };
518
+ static inlineFields = [];
519
+ static inputFields = ["audio", "cv"];
520
+ static isStreamingInput = true;
521
+ // Required by BaseNode but unused for streaming
522
+ async process() {
523
+ return {};
524
+ }
525
+ async run(inputs, outputs) {
526
+ let sampleRate = SYNTH_SAMPLE_RATE;
527
+ let channels = 1;
528
+ for await (const frame of alignedSignalStreams(inputs, {
529
+ primary: "audio",
530
+ cvHandles: ["cv"]
531
+ })) {
532
+ const gain = this.gain; // per chunk — live updates apply
533
+ sampleRate = frame.primary.sampleRate;
534
+ channels = frame.primary.channels;
535
+ const input = frame.primary.samples;
536
+ const cv = frame.cv["cv"];
537
+ const buf = new Float32Array(input.length);
538
+ for (let i = 0; i < input.length; i++) {
539
+ const level = cv !== undefined ? Math.max(0, cv[Math.floor(i / channels)]) : 1;
540
+ buf[i] = input[i] * gain * level;
541
+ }
542
+ await outputs.emit("chunk", makeSignalChunk(buf, sampleRate, channels, false, "pcm16le"));
543
+ }
544
+ await outputs.emit("chunk", makeDoneSignalChunk(sampleRate, channels, "pcm16le"));
545
+ }
546
+ }
547
+ __decorate([
548
+ prop({
549
+ type: "chunk",
550
+ default: CHUNK_PROP_DEFAULT,
551
+ title: "Audio",
552
+ description: "Audio chunk stream to amplify (drives chunk cadence)."
553
+ })
554
+ ], VcaNode.prototype, "audio", void 0);
555
+ __decorate([
556
+ prop({
557
+ type: "cv",
558
+ default: CHUNK_PROP_DEFAULT,
559
+ title: "CV",
560
+ description: "Amplitude control stream; negative values clamp to 0."
561
+ })
562
+ ], VcaNode.prototype, "cv", void 0);
563
+ __decorate([
564
+ prop({
565
+ type: "float",
566
+ default: 1,
567
+ title: "Gain",
568
+ description: "Constant gain multiplier.",
569
+ min: 0,
570
+ max: 10
571
+ })
572
+ ], VcaNode.prototype, "gain", void 0);
573
+ // ── VCF ────────────────────────────────────────────────────────────
574
+ /** Control-rate block size for cutoff recomputation (frames). */
575
+ const VCF_CONTROL_BLOCK = 64;
576
+ export class VcfNode extends BaseNode {
577
+ static nodeType = "nodetool.audio.synth.VCF";
578
+ static title = "VCF";
579
+ static description = "Voltage-controlled filter: a biquad whose cutoff is modulated by a CV stream.\n audio, cv, synthesis, vcf, filter, modular\n\n Cutoff = cutoff_hz * 2^(cv * cv_amount) (volts/octave), recomputed every 64 samples (control rate); filter state persists across chunks so chunked output equals a single pass. The audio input drives chunk cadence; CV is frame-aligned with hold-last.\n\n Use cases:\n - Classic envelope filter sweeps (ADSR → cutoff_cv)\n - Wah/auto-filter via an LFO\n - Static tone shaping with nothing patched into cutoff_cv";
580
+ static metadataOutputTypes = { chunk: "chunk" };
581
+ static inlineFields = [];
582
+ static inputFields = ["audio", "cutoff_cv"];
583
+ static isStreamingInput = true;
584
+ // Required by BaseNode but unused for streaming
585
+ async process() {
586
+ return {};
587
+ }
588
+ async run(inputs, outputs) {
589
+ let sampleRate = SYNTH_SAMPLE_RATE;
590
+ let channels = 1;
591
+ let states = [];
592
+ for await (const frame of alignedSignalStreams(inputs, {
593
+ primary: "audio",
594
+ cvHandles: ["cutoff_cv"]
595
+ })) {
596
+ // Per chunk — live updates apply (coefficients are recomputed per
597
+ // 64-frame control block below anyway).
598
+ const mode = this.mode;
599
+ const cutoffHz = this.cutoff_hz;
600
+ const q = this.q;
601
+ const cvAmount = this.cv_amount;
602
+ sampleRate = frame.primary.sampleRate;
603
+ if (states.length !== frame.primary.channels) {
604
+ channels = frame.primary.channels;
605
+ states = Array.from({ length: channels }, createBiquadState);
606
+ }
607
+ const cv = frame.cv["cutoff_cv"];
608
+ const planes = deinterleave(frame.primary.samples, channels);
609
+ const outPlanes = planes.map(() => new Float32Array(planes[0].length));
610
+ // Control-rate cutoff: recompute coefficients per 64-frame block from
611
+ // the CV value at block start (biquadCoeffs clamps to Nyquist).
612
+ const frames = planes[0].length;
613
+ for (let block = 0; block < frames; block += VCF_CONTROL_BLOCK) {
614
+ const end = Math.min(block + VCF_CONTROL_BLOCK, frames);
615
+ const fc = cv !== undefined
616
+ ? cutoffHz * Math.pow(2, cv[block] * cvAmount)
617
+ : cutoffHz;
618
+ const coeffs = biquadCoeffs(mode, sampleRate, fc, q, 0);
619
+ for (let ch = 0; ch < channels; ch++) {
620
+ const filtered = processBiquad(coeffs, states[ch], planes[ch].subarray(block, end));
621
+ outPlanes[ch].set(filtered, block);
622
+ }
623
+ }
624
+ await outputs.emit("chunk", makeSignalChunk(interleave(outPlanes), sampleRate, channels, false, "pcm16le"));
625
+ }
626
+ await outputs.emit("chunk", makeDoneSignalChunk(sampleRate, channels, "pcm16le"));
627
+ }
628
+ }
629
+ __decorate([
630
+ prop({
631
+ type: "chunk",
632
+ default: CHUNK_PROP_DEFAULT,
633
+ title: "Audio",
634
+ description: "Audio chunk stream to filter (drives chunk cadence)."
635
+ })
636
+ ], VcfNode.prototype, "audio", void 0);
637
+ __decorate([
638
+ prop({
639
+ type: "cv",
640
+ default: CHUNK_PROP_DEFAULT,
641
+ title: "Cutoff CV",
642
+ description: "Cutoff modulation in octaves: cutoff = cutoff_hz * 2^(cv * cv_amount)."
643
+ })
644
+ ], VcfNode.prototype, "cutoff_cv", void 0);
645
+ __decorate([
646
+ prop({
647
+ type: "enum",
648
+ default: "lowpass",
649
+ title: "Mode",
650
+ description: "Filter mode.",
651
+ values: ["lowpass", "highpass"]
652
+ })
653
+ ], VcfNode.prototype, "mode", void 0);
654
+ __decorate([
655
+ prop({
656
+ type: "float",
657
+ default: 1000,
658
+ title: "Cutoff Hz",
659
+ description: "Base cutoff frequency in Hz.",
660
+ min: 20,
661
+ max: 20000
662
+ })
663
+ ], VcfNode.prototype, "cutoff_hz", void 0);
664
+ __decorate([
665
+ prop({
666
+ type: "float",
667
+ default: DEFAULT_Q,
668
+ title: "Q",
669
+ description: "Filter resonance (quality factor).",
670
+ min: 0.1,
671
+ max: 10
672
+ })
673
+ ], VcfNode.prototype, "q", void 0);
674
+ __decorate([
675
+ prop({
676
+ type: "float",
677
+ default: 1,
678
+ title: "CV Amount",
679
+ description: "Octaves of cutoff shift per unit of CV.",
680
+ min: 0,
681
+ max: 5
682
+ })
683
+ ], VcfNode.prototype, "cv_amount", void 0);
684
+ // ── Attenuverter ───────────────────────────────────────────────────
685
+ export class AttenuverterNode extends BaseNode {
686
+ static nodeType = "nodetool.audio.synth.Attenuverter";
687
+ static title = "Attenuverter";
688
+ static description = "Scales and offsets a CV (or audio) stream: out = in * scale + offset.\n cv, synthesis, utility, attenuverter, modular\n\n Negative scale inverts the signal. The Swiss-army CV utility — adapt an LFO's range to a pitch input, invert an envelope, add a constant bias.\n\n Use cases:\n - Reduce LFO depth before a pitch input\n - Invert an envelope for ducking\n - Add a DC offset to centre a modulation";
689
+ static metadataOutputTypes = { cv: "cv" };
690
+ static inlineFields = [];
691
+ static inputFields = ["signal"];
692
+ static isStreamingInput = true;
693
+ // Required by BaseNode but unused for streaming
694
+ async process() {
695
+ return {};
696
+ }
697
+ async run(inputs, outputs) {
698
+ let sampleRate = SYNTH_SAMPLE_RATE;
699
+ let channels = 1;
700
+ for await (const item of inputs.stream("signal")) {
701
+ const chunk = readSignalChunk(item);
702
+ if (!chunk)
703
+ continue;
704
+ if (chunk.done)
705
+ break;
706
+ if (chunk.samples.length === 0)
707
+ continue;
708
+ const scale = this.scale; // per chunk — live updates apply
709
+ const offset = this.offset;
710
+ sampleRate = chunk.sampleRate;
711
+ channels = chunk.channels;
712
+ const buf = chunk.samples;
713
+ for (let i = 0; i < buf.length; i++)
714
+ buf[i] = buf[i] * scale + offset;
715
+ await outputs.emit("cv", makeSignalChunk(buf, sampleRate, channels, false, "f32le"));
716
+ }
717
+ await outputs.emit("cv", makeDoneSignalChunk(sampleRate, channels, "f32le"));
718
+ }
719
+ }
720
+ __decorate([
721
+ prop({
722
+ type: "cv",
723
+ default: CHUNK_PROP_DEFAULT,
724
+ title: "Signal",
725
+ description: "Input CV or audio stream."
726
+ })
727
+ ], AttenuverterNode.prototype, "signal", void 0);
728
+ __decorate([
729
+ prop({
730
+ type: "float",
731
+ default: 1,
732
+ title: "Scale",
733
+ description: "Multiplier; negative values invert.",
734
+ min: -10,
735
+ max: 10
736
+ })
737
+ ], AttenuverterNode.prototype, "scale", void 0);
738
+ __decorate([
739
+ prop({
740
+ type: "float",
741
+ default: 0,
742
+ title: "Offset",
743
+ description: "Constant added after scaling.",
744
+ min: -10,
745
+ max: 10
746
+ })
747
+ ], AttenuverterNode.prototype, "offset", void 0);
748
+ // ── Sample & Hold ──────────────────────────────────────────────────
749
+ export class SampleHoldNode extends BaseNode {
750
+ static nodeType = "nodetool.audio.synth.SampleHold";
751
+ static title = "Sample & Hold";
752
+ static description = "Samples the signal input on each trigger rising edge and holds it.\n cv, synthesis, sample-hold, modular\n\n On a trigger rising edge (≥ 0.5) at sample k, the held value becomes signal[k]; the output is the held value every sample. The signal input drives chunk cadence; the trigger is frame-aligned with hold-last.\n\n Use cases:\n - Classic random-stepped CV (noise → signal, clock → trigger)\n - Quantize an LFO into steps\n - Freeze a modulation value on demand";
753
+ static metadataOutputTypes = { cv: "cv" };
754
+ static inlineFields = [];
755
+ static inputFields = ["signal", "trigger"];
756
+ static isStreamingInput = true;
757
+ // Required by BaseNode but unused for streaming
758
+ async process() {
759
+ return {};
760
+ }
761
+ async run(inputs, outputs) {
762
+ let sampleRate = SYNTH_SAMPLE_RATE;
763
+ let held = 0;
764
+ let triggerWasHigh = false;
765
+ for await (const frame of alignedSignalStreams(inputs, {
766
+ primary: "signal",
767
+ cvHandles: ["trigger"]
768
+ })) {
769
+ sampleRate = frame.primary.sampleRate;
770
+ const signal = chunkMono(frame.primary.samples, frame.primary.channels);
771
+ const trigger = frame.cv["trigger"];
772
+ const buf = new Float32Array(signal.length);
773
+ for (let i = 0; i < signal.length; i++) {
774
+ const high = trigger !== undefined && trigger[i] >= GATE_THRESHOLD;
775
+ if (high && !triggerWasHigh)
776
+ held = signal[i];
777
+ triggerWasHigh = high;
778
+ buf[i] = held;
779
+ }
780
+ await outputs.emit("cv", makeSignalChunk(buf, sampleRate, 1, false, "f32le"));
781
+ }
782
+ await outputs.emit("cv", makeDoneSignalChunk(sampleRate, 1, "f32le"));
783
+ }
784
+ }
785
+ __decorate([
786
+ prop({
787
+ type: "cv",
788
+ default: CHUNK_PROP_DEFAULT,
789
+ title: "Signal",
790
+ description: "Stream to sample (drives chunk cadence)."
791
+ })
792
+ ], SampleHoldNode.prototype, "signal", void 0);
793
+ __decorate([
794
+ prop({
795
+ type: "cv",
796
+ default: CHUNK_PROP_DEFAULT,
797
+ title: "Trigger",
798
+ description: "Sampling clock — samples on each rising edge (≥ 0.5)."
799
+ })
800
+ ], SampleHoldNode.prototype, "trigger", void 0);
801
+ // ── Mixer ──────────────────────────────────────────────────────────
802
+ const MIXER_HANDLES = ["in1", "in2", "in3", "in4"];
803
+ export class MixerNode extends BaseNode {
804
+ static nodeType = "nodetool.audio.synth.Mixer";
805
+ static title = "Mixer";
806
+ static description = "Sums up to four audio streams with per-input levels.\n audio, synthesis, mixer, modular\n\n Output = Σ level_i * in_i, emitted as soon as every still-open input can cover the frames; inputs that have ended contribute silence. The stream ends when all connected inputs end. Assumes matching sample rates and channel counts (no resampling).\n\n Use cases:\n - Combine oscillators into a richer voice\n - Mix a dry signal with a filtered copy\n - Sub-mix several synth voices";
807
+ static metadataOutputTypes = { chunk: "chunk" };
808
+ static inlineFields = [];
809
+ static inputFields = [...MIXER_HANDLES];
810
+ static isStreamingInput = true;
811
+ // Required by BaseNode but unused for streaming
812
+ async process() {
813
+ return {};
814
+ }
815
+ async run(inputs, outputs) {
816
+ // Read per emitted block — live updates apply.
817
+ const levels = () => ({
818
+ in1: this.level1,
819
+ in2: this.level2,
820
+ in3: this.level3,
821
+ in4: this.level4
822
+ });
823
+ const connected = MIXER_HANDLES.filter((h) => inputs.hasStream(h));
824
+ const fifos = new Map(connected.map((h) => [h, new SampleFifo()]));
825
+ const done = new Map(connected.map((h) => [h, false]));
826
+ let sampleRate = SYNTH_SAMPLE_RATE;
827
+ let channels = 1;
828
+ let formatSeen = false;
829
+ const emitMixed = async (n) => {
830
+ if (n <= 0)
831
+ return;
832
+ const lv = levels();
833
+ const buf = new Float32Array(n);
834
+ for (const h of connected) {
835
+ // Ended/short inputs contribute zeros — silence is correct for
836
+ // finished audio, unlike CV's hold-last.
837
+ const part = fifos.get(h).pull(n, "zero");
838
+ const level = lv[h];
839
+ for (let i = 0; i < n; i++)
840
+ buf[i] += level * part[i];
841
+ }
842
+ await outputs.emit("chunk", makeSignalChunk(buf, sampleRate, channels, false, "pcm16le"));
843
+ };
844
+ for await (const [handle, item] of inputs.any()) {
845
+ if (!fifos.has(handle))
846
+ continue;
847
+ const chunk = readSignalChunk(item);
848
+ if (!chunk)
849
+ continue;
850
+ if (!formatSeen && chunk.samples.length > 0) {
851
+ sampleRate = chunk.sampleRate;
852
+ channels = chunk.channels;
853
+ formatSeen = true;
854
+ }
855
+ if (chunk.done)
856
+ done.set(handle, true);
857
+ else
858
+ fifos.get(handle).push(chunk.samples);
859
+ const open = connected.filter((h) => !done.get(h));
860
+ if (open.length === 0)
861
+ break;
862
+ const n = Math.min(...open.map((h) => fifos.get(h).available));
863
+ if (n > 0 && Number.isFinite(n))
864
+ await emitMixed(n);
865
+ }
866
+ // All inputs ended — flush the remainder zero-padded to the longest.
867
+ const remaining = Math.max(0, ...connected.map((h) => fifos.get(h).available));
868
+ await emitMixed(remaining);
869
+ await outputs.emit("chunk", makeDoneSignalChunk(sampleRate, channels, "pcm16le"));
870
+ }
871
+ }
872
+ __decorate([
873
+ prop({
874
+ type: "chunk",
875
+ default: CHUNK_PROP_DEFAULT,
876
+ title: "In 1",
877
+ description: "Audio input 1."
878
+ })
879
+ ], MixerNode.prototype, "in1", void 0);
880
+ __decorate([
881
+ prop({
882
+ type: "chunk",
883
+ default: CHUNK_PROP_DEFAULT,
884
+ title: "In 2",
885
+ description: "Audio input 2."
886
+ })
887
+ ], MixerNode.prototype, "in2", void 0);
888
+ __decorate([
889
+ prop({
890
+ type: "chunk",
891
+ default: CHUNK_PROP_DEFAULT,
892
+ title: "In 3",
893
+ description: "Audio input 3."
894
+ })
895
+ ], MixerNode.prototype, "in3", void 0);
896
+ __decorate([
897
+ prop({
898
+ type: "chunk",
899
+ default: CHUNK_PROP_DEFAULT,
900
+ title: "In 4",
901
+ description: "Audio input 4."
902
+ })
903
+ ], MixerNode.prototype, "in4", void 0);
904
+ __decorate([
905
+ prop({
906
+ type: "float",
907
+ default: 1,
908
+ title: "Level 1",
909
+ description: "Gain for input 1.",
910
+ min: 0,
911
+ max: 2
912
+ })
913
+ ], MixerNode.prototype, "level1", void 0);
914
+ __decorate([
915
+ prop({
916
+ type: "float",
917
+ default: 1,
918
+ title: "Level 2",
919
+ description: "Gain for input 2.",
920
+ min: 0,
921
+ max: 2
922
+ })
923
+ ], MixerNode.prototype, "level2", void 0);
924
+ __decorate([
925
+ prop({
926
+ type: "float",
927
+ default: 1,
928
+ title: "Level 3",
929
+ description: "Gain for input 3.",
930
+ min: 0,
931
+ max: 2
932
+ })
933
+ ], MixerNode.prototype, "level3", void 0);
934
+ __decorate([
935
+ prop({
936
+ type: "float",
937
+ default: 1,
938
+ title: "Level 4",
939
+ description: "Gain for input 4.",
940
+ min: 0,
941
+ max: 2
942
+ })
943
+ ], MixerNode.prototype, "level4", void 0);
944
+ export const SYNTHESIS_NODES = tagAsHybrid([
945
+ OscillatorNode,
946
+ LfoNode,
947
+ AdsrNode,
948
+ GateNode,
949
+ VcaNode,
950
+ VcfNode,
951
+ AttenuverterNode,
952
+ SampleHoldNode,
953
+ MixerNode
954
+ ]);
955
+ //# sourceMappingURL=synthesis.js.map