@nodetool-ai/audio-nodes 0.8.10 → 0.8.16

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 (67) hide show
  1. package/README.md +2 -0
  2. package/dist/.nodetool-build-stamp +1 -1
  3. package/dist/index.d.ts +2 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +2 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/lib/audio-context.js +1 -1
  8. package/dist/lib/audio-wav.d.ts.map +1 -1
  9. package/dist/lib/audio-wav.js +62 -37
  10. package/dist/lib/audio-wav.js.map +1 -1
  11. package/dist/lib/biquad.d.ts +3 -44
  12. package/dist/lib/biquad.d.ts.map +1 -1
  13. package/dist/lib/biquad.js +3 -123
  14. package/dist/lib/biquad.js.map +1 -1
  15. package/dist/lib/cv.d.ts +7 -25
  16. package/dist/lib/cv.d.ts.map +1 -1
  17. package/dist/lib/cv.js +18 -34
  18. package/dist/lib/cv.js.map +1 -1
  19. package/dist/lib/effect-descriptors.d.ts +17 -0
  20. package/dist/lib/effect-descriptors.d.ts.map +1 -0
  21. package/dist/lib/effect-descriptors.js +107 -0
  22. package/dist/lib/effect-descriptors.js.map +1 -0
  23. package/dist/lib/effect-properties.d.ts +66 -0
  24. package/dist/lib/effect-properties.d.ts.map +1 -0
  25. package/dist/lib/effect-properties.js +392 -0
  26. package/dist/lib/effect-properties.js.map +1 -0
  27. package/dist/lib/signal-effect.d.ts +14 -0
  28. package/dist/lib/signal-effect.d.ts.map +1 -0
  29. package/dist/lib/signal-effect.js +63 -0
  30. package/dist/lib/signal-effect.js.map +1 -0
  31. package/dist/lib/transport.d.ts +15 -0
  32. package/dist/lib/transport.d.ts.map +1 -0
  33. package/dist/lib/transport.js +53 -0
  34. package/dist/lib/transport.js.map +1 -0
  35. package/dist/nodes/audio.d.ts +9 -24
  36. package/dist/nodes/audio.d.ts.map +1 -1
  37. package/dist/nodes/audio.js +23 -69
  38. package/dist/nodes/audio.js.map +1 -1
  39. package/dist/nodes/lib-audio-dsp.d.ts +35 -62
  40. package/dist/nodes/lib-audio-dsp.d.ts.map +1 -1
  41. package/dist/nodes/lib-audio-dsp.js +31 -279
  42. package/dist/nodes/lib-audio-dsp.js.map +1 -1
  43. package/dist/nodes/lib-audio-effects.d.ts +29 -66
  44. package/dist/nodes/lib-audio-effects.d.ts.map +1 -1
  45. package/dist/nodes/lib-audio-effects.js +24 -517
  46. package/dist/nodes/lib-audio-effects.js.map +1 -1
  47. package/dist/nodes/realtime-audio.d.ts +14 -12
  48. package/dist/nodes/realtime-audio.d.ts.map +1 -1
  49. package/dist/nodes/realtime-audio.js +117 -116
  50. package/dist/nodes/realtime-audio.js.map +1 -1
  51. package/dist/nodes/sampler.d.ts +44 -0
  52. package/dist/nodes/sampler.d.ts.map +1 -0
  53. package/dist/nodes/sampler.js +243 -0
  54. package/dist/nodes/sampler.js.map +1 -0
  55. package/dist/nodes/signal-effects.d.ts +170 -0
  56. package/dist/nodes/signal-effects.d.ts.map +1 -0
  57. package/dist/nodes/signal-effects.js +309 -0
  58. package/dist/nodes/signal-effects.js.map +1 -0
  59. package/dist/nodes/synthesis.d.ts +34 -18
  60. package/dist/nodes/synthesis.d.ts.map +1 -1
  61. package/dist/nodes/synthesis.js +183 -218
  62. package/dist/nodes/synthesis.js.map +1 -1
  63. package/package.json +7 -6
  64. package/dist/lib/synth-dsp.d.ts +0 -43
  65. package/dist/lib/synth-dsp.d.ts.map +0 -1
  66. package/dist/lib/synth-dsp.js +0 -88
  67. package/dist/lib/synth-dsp.js.map +0 -1
@@ -4,14 +4,17 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
4
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
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
+ import { AUDIO_SAMPLE_RATE } from "@nodetool-ai/protocol";
8
+ import { getTransport } from "../lib/transport.js";
7
9
  /**
8
10
  * Modular synthesis nodes (`nodetool.audio.synth.*`) — a subtractive synth
9
11
  * voice operating on streamed signal chunks: oscillator, LFO, ADSR, gate
10
- * source, VCA, VCF, CV utilities, and a mixer.
12
+ * source, manual trigger, VCA, VCF, CV utilities, and a mixer. The Sampler,
13
+ * a polyphonic sample player on the same streams, is in ./sampler.ts.
11
14
  *
12
15
  * Timing is sample-domain: time = sample count; the wall clock only paces
13
16
  * emission. Free-running generators (Oscillator, LFO, Gate) are realtime
14
- * modules: they emit forever, paced by `RealtimePacer`, until the run is
17
+ * modules: they follow the run transport and finish at the render duration or when
15
18
  * cancelled (pacing delays emission but never changes signal content).
16
19
  * Driven nodes derive all timing from their input chunks (one output chunk
17
20
  * per input chunk, same frame count), so a patch driven by one Gate source
@@ -28,48 +31,31 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
28
31
  */
29
32
  import { BaseNode, prop } from "@nodetool-ai/node-sdk";
30
33
  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";
34
+ import { loadKernels, STATE_LEN } from "@nodetool-ai/kernels";
35
+ import { alignedSignalStreams, CHUNK_PROP_DEFAULT, makeDoneSignalChunk, makeSignalChunk, readSignalChunk, SampleFifo } from "../lib/cv.js";
36
+ import { DEFAULT_Q } from "../lib/biquad.js";
37
+ import { SamplerNode } from "./sampler.js";
35
38
  // ── 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
- */
39
+ /** Emit transport-sized blocks, ending at the render frame or cancellation. */
44
40
  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;
41
+ const { slot, transport, encoding, signal } = opts;
42
+ const sampleRate = transport.sampleRate;
48
43
  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;
44
+ while (!signal.aborted && (transport.endFrame === null || emitted < transport.endFrame)) {
45
+ await transport.until(emitted, signal);
46
+ if (signal.aborted)
47
+ break;
48
+ const count = Math.min(transport.blockFrames, transport.endFrame === null ? transport.blockFrames : transport.endFrame - emitted);
49
+ await outputs.emit(slot, makeSignalChunk(opts.fill(count, emitted), sampleRate, 1, false, encoding));
50
+ emitted += count;
54
51
  }
55
52
  await outputs.emit(slot, makeDoneSignalChunk(sampleRate, 1, encoding));
56
53
  }
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
54
  // ── Oscillator (VCO) ───────────────────────────────────────────────
69
55
  export class OscillatorNode extends BaseNode {
70
56
  static nodeType = "nodetool.audio.synth.Oscillator";
71
57
  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";
58
+ 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 follows the run transport until the render ends or 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
59
  static metadataOutputTypes = { chunk: "chunk" };
74
60
  static inlineFields = [];
75
61
  static inputFields = ["pitch_cv", "fm"];
@@ -82,23 +68,19 @@ export class OscillatorNode extends BaseNode {
82
68
  params() {
83
69
  return {
84
70
  waveform: this.waveform,
85
- baseFreq: this.frequency,
71
+ frequency: this.frequency,
86
72
  amplitude: this.amplitude,
87
73
  pulseWidth: this.pulse_width,
88
74
  fmAmount: this.fm_amount
89
75
  };
90
76
  }
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
- };
77
+ async run(inputs, outputs, context) {
78
+ // The kernel carries the double-precision phase (no drift) and the noise
79
+ // seed across chunks in `state`. Properties are re-read at every chunk
80
+ // boundary (params()), so live updates pushed via applyProperties land
81
+ // on the next chunk.
82
+ const kernels = await loadKernels();
83
+ const state = kernels.createOscillatorState(Math.floor(Math.random() * 2 ** 32));
102
84
  const hasPitch = inputs.hasStream("pitch_cv");
103
85
  const hasFm = inputs.hasStream("fm");
104
86
  if (hasPitch || hasFm) {
@@ -106,39 +88,28 @@ export class OscillatorNode extends BaseNode {
106
88
  // handle (if any) is frame-aligned via zip-with-hold.
107
89
  const primary = hasPitch ? "pitch_cv" : "fm";
108
90
  const secondary = hasPitch ? "fm" : "pitch_cv";
109
- let sampleRate = SYNTH_SAMPLE_RATE;
91
+ let sampleRate = AUDIO_SAMPLE_RATE;
110
92
  for await (const frame of alignedSignalStreams(inputs, {
111
93
  primary,
112
94
  cvHandles: [secondary]
113
95
  })) {
114
- const p = this.params();
115
96
  sampleRate = frame.primary.sampleRate;
116
- const drive = chunkMono(frame.primary.samples, frame.primary.channels);
97
+ const drive = kernels.extractChannel(frame.primary.samples, frame.primary.channels);
117
98
  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
- }
99
+ const buf = kernels.oscillator(state, this.params(), sampleRate, drive.length, hasPitch ? drive : other, hasPitch ? other : drive);
125
100
  await outputs.emit("chunk", makeSignalChunk(buf, sampleRate, 1, false, "pcm16le"));
126
101
  }
127
102
  await outputs.emit("chunk", makeDoneSignalChunk(sampleRate, 1, "pcm16le"));
128
103
  return;
129
104
  }
130
- const sampleRate = this.sample_rate;
105
+ const transport = getTransport(context);
106
+ const sampleRate = transport.sampleRate;
131
107
  await runGenerator(outputs, {
132
108
  slot: "chunk",
133
- sampleRate,
109
+ transport,
134
110
  encoding: "pcm16le",
135
111
  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
- }
112
+ fill: (count) => kernels.oscillator(state, this.params(), sampleRate, count)
142
113
  });
143
114
  }
144
115
  }
@@ -207,21 +178,11 @@ __decorate([
207
178
  max: 10
208
179
  })
209
180
  ], 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
181
  // ── LFO ────────────────────────────────────────────────────────────
221
182
  export class LfoNode extends BaseNode {
222
183
  static nodeType = "nodetool.audio.synth.LFO";
223
184
  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";
185
+ 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 paced by the run transport, or ending at the render duration.\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
186
  static metadataOutputTypes = { cv: "cv" };
226
187
  static inlineFields = [];
227
188
  static inputFields = ["clock"];
@@ -239,11 +200,12 @@ export class LfoNode extends BaseNode {
239
200
  offset: this.offset
240
201
  };
241
202
  }
242
- async run(inputs, outputs) {
243
- let phase = 0;
203
+ async run(inputs, outputs, context) {
204
+ const kernels = await loadKernels();
205
+ // {phase, clock was high}, carried across chunks by the kernel.
206
+ const state = new Float64Array(STATE_LEN.lfo);
244
207
  if (inputs.hasStream("clock")) {
245
- let sampleRate = SYNTH_SAMPLE_RATE;
246
- let clockWasHigh = false;
208
+ let sampleRate = AUDIO_SAMPLE_RATE;
247
209
  for await (const item of inputs.stream("clock")) {
248
210
  const chunk = readSignalChunk(item);
249
211
  if (!chunk)
@@ -252,36 +214,23 @@ export class LfoNode extends BaseNode {
252
214
  break;
253
215
  if (chunk.samples.length === 0)
254
216
  continue;
255
- const p = this.params();
256
217
  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
- }
218
+ // A clock rising edge resets the phase at that sample.
219
+ const clock = kernels.extractChannel(chunk.samples, chunk.channels);
220
+ const buf = kernels.lfo(state, this.params(), sampleRate, clock.length, clock);
267
221
  await outputs.emit("cv", makeSignalChunk(buf, sampleRate, 1, false, "f32le"));
268
222
  }
269
223
  await outputs.emit("cv", makeDoneSignalChunk(sampleRate, 1, "f32le"));
270
224
  return;
271
225
  }
272
- const sampleRate = this.sample_rate;
226
+ const transport = getTransport(context);
227
+ const sampleRate = transport.sampleRate;
273
228
  await runGenerator(outputs, {
274
229
  slot: "cv",
275
- sampleRate,
230
+ transport,
276
231
  encoding: "f32le",
277
232
  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
- }
233
+ fill: (count) => kernels.lfo(state, this.params(), sampleRate, count)
285
234
  });
286
235
  }
287
236
  }
@@ -332,16 +281,6 @@ __decorate([
332
281
  max: 10
333
282
  })
334
283
  ], 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
284
  // ── ADSR ───────────────────────────────────────────────────────────
346
285
  export class AdsrNode extends BaseNode {
347
286
  static nodeType = "nodetool.audio.synth.ADSR";
@@ -355,9 +294,11 @@ export class AdsrNode extends BaseNode {
355
294
  async process() {
356
295
  return {};
357
296
  }
358
- async run(inputs, outputs) {
359
- const state = createAdsrState();
360
- let sampleRate = SYNTH_SAMPLE_RATE;
297
+ async run(inputs, outputs, _context) {
298
+ const kernels = await loadKernels();
299
+ // {stage, envelope, gate was high}, carried across chunks by the kernel.
300
+ const state = new Float64Array(STATE_LEN.adsr);
301
+ let sampleRate = AUDIO_SAMPLE_RATE;
361
302
  for await (const item of inputs.stream("gate")) {
362
303
  const chunk = readSignalChunk(item);
363
304
  if (!chunk)
@@ -367,19 +308,15 @@ export class AdsrNode extends BaseNode {
367
308
  if (chunk.samples.length === 0)
368
309
  continue;
369
310
  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
- }
311
+ // Times are read per chunk from the current property values, so live
312
+ // updates apply.
313
+ const gate = kernels.extractChannel(chunk.samples, chunk.channels);
314
+ const buf = kernels.adsr(state, {
315
+ attack: this.attack,
316
+ decay: this.decay,
317
+ sustain: this.sustain,
318
+ release: this.release
319
+ }, sampleRate, gate);
383
320
  await outputs.emit("cv", makeSignalChunk(buf, sampleRate, 1, false, "f32le"));
384
321
  }
385
322
  await outputs.emit("cv", makeDoneSignalChunk(sampleRate, 1, "f32le"));
@@ -437,7 +374,7 @@ __decorate([
437
374
  export class GateNode extends BaseNode {
438
375
  static nodeType = "nodetool.audio.synth.Gate";
439
376
  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";
377
+ 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 until the render ends or 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
378
  static metadataOutputTypes = { cv: "cv" };
442
379
  static inlineFields = [];
443
380
  static inputFields = [];
@@ -446,26 +383,19 @@ export class GateNode extends BaseNode {
446
383
  async process() {
447
384
  return {};
448
385
  }
449
- async run(inputs, outputs) {
450
- const sampleRate = this.sample_rate;
386
+ async run(inputs, outputs, context) {
387
+ const kernels = await loadKernels();
388
+ const transport = getTransport(context);
389
+ const sampleRate = transport.sampleRate;
451
390
  await runGenerator(outputs, {
452
391
  slot: "cv",
453
- sampleRate,
392
+ transport,
454
393
  encoding: "f32le",
455
394
  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
- }
395
+ // Timing re-read per chunk so live updates apply; the phase derives
396
+ // from the absolute frame counter, so a period change just re-grids
397
+ // the pattern instead of glitching it.
398
+ fill: (count, startFrame) => kernels.gate(startFrame, this.on_duration, this.off_duration, this.amplitude, sampleRate, count)
469
399
  });
470
400
  }
471
401
  }
@@ -499,16 +429,86 @@ __decorate([
499
429
  max: 10
500
430
  })
501
431
  ], GateNode.prototype, "amplitude", void 0);
432
+ // ── Trigger ────────────────────────────────────────────────────────
433
+ export class TriggerNode extends BaseNode {
434
+ static nodeType = "nodetool.audio.synth.Trigger";
435
+ static title = "Trigger";
436
+ static description = "Fires one gate pulse for each change of its switch, with a pitch value: plays notes by hand while the patch runs.\n cv, synthesis, trigger, gate, note, manual, keyboard, modular\n\n Each change of trigger starts a gate pulse of length seconds at the next block. A change during a pulse starts the pulse again after one low sample, so the gate rises for every change. The pitch output carries note in octaves (note / 12, volts/octave). The gate stays low until the first change, so a render without a live change is silent.\n\n Use cases:\n - Play a Sampler voice with one click\n - Start an ADSR envelope by hand\n - Try a note at a chosen pitch";
437
+ static metadataOutputTypes = { gate: "cv", pitch: "cv" };
438
+ static inlineFields = ["trigger", "note"];
439
+ static inputFields = [];
440
+ static isStreamingInput = true;
441
+ // Required by BaseNode but unused for streaming
442
+ async process() {
443
+ return {};
444
+ }
445
+ async run(inputs, outputs, context) {
446
+ const transport = getTransport(context);
447
+ const sampleRate = transport.sampleRate;
448
+ const signal = inputs.signal;
449
+ let emitted = 0;
450
+ // The switch position of the last block, the frames of the pulse still
451
+ // to play, and whether the last frame of the last block was high.
452
+ let seen = this.trigger;
453
+ let remaining = 0;
454
+ let high = false;
455
+ while (!signal.aborted && (transport.endFrame === null || emitted < transport.endFrame)) {
456
+ await transport.until(emitted, signal);
457
+ if (signal.aborted)
458
+ break;
459
+ const count = Math.min(transport.blockFrames, transport.endFrame === null ? transport.blockFrames : transport.endFrame - emitted);
460
+ // The properties are read here, so a live change applies at this block.
461
+ let from = 0;
462
+ if (this.trigger !== seen) {
463
+ seen = this.trigger;
464
+ remaining = Math.max(1, Math.round(this.length * sampleRate));
465
+ // One low frame, so a pulse that still plays rises again.
466
+ if (high)
467
+ from = 1;
468
+ }
469
+ const to = Math.min(count, from + remaining);
470
+ const gate = new Float32Array(count);
471
+ if (to > from)
472
+ gate.fill(1, from, to);
473
+ remaining -= Math.max(0, to - from);
474
+ high = to > from && to === count;
475
+ const pitch = new Float32Array(count).fill(this.note / 12);
476
+ await outputs.emit("pitch", makeSignalChunk(pitch, sampleRate, 1, false, "f32le"));
477
+ await outputs.emit("gate", makeSignalChunk(gate, sampleRate, 1, false, "f32le"));
478
+ emitted += count;
479
+ }
480
+ await outputs.emit("pitch", makeDoneSignalChunk(sampleRate, 1, "f32le"));
481
+ await outputs.emit("gate", makeDoneSignalChunk(sampleRate, 1, "f32le"));
482
+ }
483
+ }
502
484
  __decorate([
503
485
  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
486
+ type: "bool",
487
+ default: false,
488
+ title: "Trigger",
489
+ description: "Each change fires one gate pulse while the patch runs."
510
490
  })
511
- ], GateNode.prototype, "sample_rate", void 0);
491
+ ], TriggerNode.prototype, "trigger", void 0);
492
+ __decorate([
493
+ prop({
494
+ type: "float",
495
+ default: 0,
496
+ title: "Note",
497
+ description: "Pitch of the note in semitones. The pitch output carries note / 12.",
498
+ min: -48,
499
+ max: 48
500
+ })
501
+ ], TriggerNode.prototype, "note", void 0);
502
+ __decorate([
503
+ prop({
504
+ type: "float",
505
+ default: 0.1,
506
+ title: "Length",
507
+ description: "Gate-high time of one pulse in seconds.",
508
+ min: 0.001,
509
+ max: 60
510
+ })
511
+ ], TriggerNode.prototype, "length", void 0);
512
512
  // ── VCA ────────────────────────────────────────────────────────────
513
513
  export class VcaNode extends BaseNode {
514
514
  static nodeType = "nodetool.audio.synth.VCA";
@@ -522,23 +522,18 @@ export class VcaNode extends BaseNode {
522
522
  async process() {
523
523
  return {};
524
524
  }
525
- async run(inputs, outputs) {
526
- let sampleRate = SYNTH_SAMPLE_RATE;
525
+ async run(inputs, outputs, _context) {
526
+ const kernels = await loadKernels();
527
+ let sampleRate = AUDIO_SAMPLE_RATE;
527
528
  let channels = 1;
528
529
  for await (const frame of alignedSignalStreams(inputs, {
529
530
  primary: "audio",
530
531
  cvHandles: ["cv"]
531
532
  })) {
532
- const gain = this.gain; // per chunk — live updates apply
533
533
  sampleRate = frame.primary.sampleRate;
534
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
- }
535
+ // Gain per chunk — live updates apply. Negative CV is clamped to 0.
536
+ const buf = kernels.vca(frame.primary.samples, frame.cv["cv"], channels, this.gain);
542
537
  await outputs.emit("chunk", makeSignalChunk(buf, sampleRate, channels, false, "pcm16le"));
543
538
  }
544
539
  await outputs.emit("chunk", makeDoneSignalChunk(sampleRate, channels, "pcm16le"));
@@ -571,8 +566,6 @@ __decorate([
571
566
  })
572
567
  ], VcaNode.prototype, "gain", void 0);
573
568
  // ── VCF ────────────────────────────────────────────────────────────
574
- /** Control-rate block size for cutoff recomputation (frames). */
575
- const VCF_CONTROL_BLOCK = 64;
576
569
  export class VcfNode extends BaseNode {
577
570
  static nodeType = "nodetool.audio.synth.VCF";
578
571
  static title = "VCF";
@@ -585,43 +578,24 @@ export class VcfNode extends BaseNode {
585
578
  async process() {
586
579
  return {};
587
580
  }
588
- async run(inputs, outputs) {
589
- let sampleRate = SYNTH_SAMPLE_RATE;
581
+ async run(inputs, outputs, _context) {
582
+ const kernels = await loadKernels();
583
+ let sampleRate = AUDIO_SAMPLE_RATE;
590
584
  let channels = 1;
591
- let states = [];
585
+ let state;
592
586
  for await (const frame of alignedSignalStreams(inputs, {
593
587
  primary: "audio",
594
588
  cvHandles: ["cutoff_cv"]
595
589
  })) {
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
590
  sampleRate = frame.primary.sampleRate;
603
- if (states.length !== frame.primary.channels) {
591
+ if (!state || channels !== frame.primary.channels) {
604
592
  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
- }
593
+ state = kernels.createBiquadState(channels);
623
594
  }
624
- await outputs.emit("chunk", makeSignalChunk(interleave(outPlanes), sampleRate, channels, false, "pcm16le"));
595
+ // Properties per chunk — live updates apply. The kernel recomputes the
596
+ // cutoff every 64 frames from the CV value at the block start.
597
+ const buf = kernels.vcf(state, this.mode, sampleRate, this.cutoff_hz, this.q, this.cv_amount, frame.cv["cutoff_cv"], frame.primary.samples, channels);
598
+ await outputs.emit("chunk", makeSignalChunk(buf, sampleRate, channels, false, "pcm16le"));
625
599
  }
626
600
  await outputs.emit("chunk", makeDoneSignalChunk(sampleRate, channels, "pcm16le"));
627
601
  }
@@ -694,8 +668,9 @@ export class AttenuverterNode extends BaseNode {
694
668
  async process() {
695
669
  return {};
696
670
  }
697
- async run(inputs, outputs) {
698
- let sampleRate = SYNTH_SAMPLE_RATE;
671
+ async run(inputs, outputs, _context) {
672
+ const kernels = await loadKernels();
673
+ let sampleRate = AUDIO_SAMPLE_RATE;
699
674
  let channels = 1;
700
675
  for await (const item of inputs.stream("signal")) {
701
676
  const chunk = readSignalChunk(item);
@@ -705,13 +680,10 @@ export class AttenuverterNode extends BaseNode {
705
680
  break;
706
681
  if (chunk.samples.length === 0)
707
682
  continue;
708
- const scale = this.scale; // per chunk — live updates apply
709
- const offset = this.offset;
710
683
  sampleRate = chunk.sampleRate;
711
684
  channels = chunk.channels;
712
- const buf = chunk.samples;
713
- for (let i = 0; i < buf.length; i++)
714
- buf[i] = buf[i] * scale + offset;
685
+ // Scale and offset per chunk — live updates apply.
686
+ const buf = kernels.scaleOffset(chunk.samples, this.scale, this.offset);
715
687
  await outputs.emit("cv", makeSignalChunk(buf, sampleRate, channels, false, "f32le"));
716
688
  }
717
689
  await outputs.emit("cv", makeDoneSignalChunk(sampleRate, channels, "f32le"));
@@ -758,25 +730,18 @@ export class SampleHoldNode extends BaseNode {
758
730
  async process() {
759
731
  return {};
760
732
  }
761
- async run(inputs, outputs) {
762
- let sampleRate = SYNTH_SAMPLE_RATE;
763
- let held = 0;
764
- let triggerWasHigh = false;
733
+ async run(inputs, outputs, _context) {
734
+ const kernels = await loadKernels();
735
+ // {held value, trigger was high}, carried across chunks by the kernel.
736
+ const state = new Float64Array(STATE_LEN.sampleHold);
737
+ let sampleRate = AUDIO_SAMPLE_RATE;
765
738
  for await (const frame of alignedSignalStreams(inputs, {
766
739
  primary: "signal",
767
740
  cvHandles: ["trigger"]
768
741
  })) {
769
742
  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
- }
743
+ const signal = kernels.extractChannel(frame.primary.samples, frame.primary.channels);
744
+ const buf = kernels.sampleHold(state, signal, frame.cv["trigger"]);
780
745
  await outputs.emit("cv", makeSignalChunk(buf, sampleRate, 1, false, "f32le"));
781
746
  }
782
747
  await outputs.emit("cv", makeDoneSignalChunk(sampleRate, 1, "f32le"));
@@ -812,7 +777,8 @@ export class MixerNode extends BaseNode {
812
777
  async process() {
813
778
  return {};
814
779
  }
815
- async run(inputs, outputs) {
780
+ async run(inputs, outputs, _context) {
781
+ const kernels = await loadKernels();
816
782
  // Read per emitted block — live updates apply.
817
783
  const levels = () => ({
818
784
  in1: this.level1,
@@ -823,7 +789,7 @@ export class MixerNode extends BaseNode {
823
789
  const connected = MIXER_HANDLES.filter((h) => inputs.hasStream(h));
824
790
  const fifos = new Map(connected.map((h) => [h, new SampleFifo()]));
825
791
  const done = new Map(connected.map((h) => [h, false]));
826
- let sampleRate = SYNTH_SAMPLE_RATE;
792
+ let sampleRate = AUDIO_SAMPLE_RATE;
827
793
  let channels = 1;
828
794
  let formatSeen = false;
829
795
  const emitMixed = async (n) => {
@@ -834,10 +800,7 @@ export class MixerNode extends BaseNode {
834
800
  for (const h of connected) {
835
801
  // Ended/short inputs contribute zeros — silence is correct for
836
802
  // 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];
803
+ kernels.mixAdd(buf, fifos.get(h).pull(n, "zero"), lv[h]);
841
804
  }
842
805
  await outputs.emit("chunk", makeSignalChunk(buf, sampleRate, channels, false, "pcm16le"));
843
806
  };
@@ -946,10 +909,12 @@ export const SYNTHESIS_NODES = tagAsHybrid([
946
909
  LfoNode,
947
910
  AdsrNode,
948
911
  GateNode,
912
+ TriggerNode,
949
913
  VcaNode,
950
914
  VcfNode,
951
915
  AttenuverterNode,
952
916
  SampleHoldNode,
953
- MixerNode
917
+ MixerNode,
918
+ SamplerNode
954
919
  ]);
955
920
  //# sourceMappingURL=synthesis.js.map