@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,962 @@
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
+ import { BaseNode, prop } from "@nodetool-ai/node-sdk";
8
+ import { tagAsServer } from "@nodetool-ai/nodes-utils";
9
+ import { importHidden } from "@nodetool-ai/config";
10
+ import { requireAudioBytes, audioRefFromWav, decodeAudioToWav, encodeWav } from "../lib/audio-wav.js";
11
+ // ── DSP helpers ────────────────────────────────────────────────────
12
+ function processPerChannel(wav, fn) {
13
+ const { samples, sampleRate, numChannels } = wav;
14
+ const frameSamples = Math.floor(samples.length / numChannels);
15
+ const channels = [];
16
+ for (let ch = 0; ch < numChannels; ch++) {
17
+ const channelData = new Float32Array(frameSamples);
18
+ for (let i = 0; i < frameSamples; i++) {
19
+ channelData[i] = samples[i * numChannels + ch];
20
+ }
21
+ channels.push(fn(channelData, sampleRate));
22
+ }
23
+ const outFrames = channels[0].length;
24
+ const outSamples = new Float32Array(outFrames * numChannels);
25
+ for (let ch = 0; ch < numChannels; ch++) {
26
+ for (let i = 0; i < outFrames; i++) {
27
+ outSamples[i * numChannels + ch] = channels[ch][i];
28
+ }
29
+ }
30
+ return { samples: outSamples, sampleRate, numChannels };
31
+ }
32
+ export class BitcrushNode extends BaseNode {
33
+ static nodeType = "lib.audio.Bitcrush";
34
+ static title = "Bitcrush";
35
+ static description = "Applies a bitcrushing effect to an audio file, reducing bit depth and/or sample rate.\n audio, effect, distortion\n\n Use cases:\n - Create lo-fi or retro-style audio effects\n - Simulate vintage digital audio equipment\n - Add digital distortion and artifacts to sounds";
36
+ static metadataOutputTypes = {
37
+ output: "audio"
38
+ };
39
+ static inlineFields = [];
40
+ static inputFields = ["audio"];
41
+ async process(context) {
42
+ const audio = this.audio;
43
+ const bitDepth = this.bit_depth;
44
+ const srrFactor = this.sample_rate_reduction;
45
+ const bytes = await requireAudioBytes(audio, context);
46
+ const wav = await decodeAudioToWav(bytes);
47
+ const result = processPerChannel(wav, (ch) => {
48
+ // Number of quantization steps for the target bit depth. Guard against
49
+ // bitDepth === 1 (the prop minimum), which would otherwise yield 0 and
50
+ // produce NaN samples via division by zero.
51
+ const levels = Math.max(1, Math.pow(2, bitDepth) - 1);
52
+ const out = new Float32Array(ch.length);
53
+ for (let i = 0; i < ch.length; i++) {
54
+ const idx = Math.floor(i / srrFactor) * srrFactor;
55
+ const srcIdx = Math.min(idx, ch.length - 1);
56
+ out[i] = Math.round(ch[srcIdx] * levels) / levels;
57
+ }
58
+ return out;
59
+ });
60
+ return {
61
+ output: audioRefFromWav(encodeWav(result.samples, result.sampleRate, result.numChannels))
62
+ };
63
+ }
64
+ }
65
+ __decorate([
66
+ prop({
67
+ type: "audio",
68
+ default: {
69
+ type: "audio",
70
+ uri: "",
71
+ asset_id: null,
72
+ data: null,
73
+ metadata: null
74
+ },
75
+ title: "Audio",
76
+ description: "The audio file to process."
77
+ })
78
+ ], BitcrushNode.prototype, "audio", void 0);
79
+ __decorate([
80
+ prop({
81
+ type: "int",
82
+ default: 8,
83
+ title: "Bit Depth",
84
+ description: "The bit depth to reduce the audio to. Lower values create more distortion.",
85
+ min: 1,
86
+ max: 16
87
+ })
88
+ ], BitcrushNode.prototype, "bit_depth", void 0);
89
+ __decorate([
90
+ prop({
91
+ type: "int",
92
+ default: 1,
93
+ title: "Sample Rate Reduction",
94
+ description: "Factor by which to reduce the sample rate. Higher values create more aliasing.",
95
+ min: 1,
96
+ max: 100
97
+ })
98
+ ], BitcrushNode.prototype, "sample_rate_reduction", void 0);
99
+ export class CompressNode extends BaseNode {
100
+ static nodeType = "lib.audio.Compress";
101
+ static title = "Compress";
102
+ static description = "Applies dynamic range compression to an audio file.\n audio, effect, dynamics\n\n Use cases:\n - Even out volume levels in a recording\n - Increase perceived loudness of audio\n - Control peaks in audio signals";
103
+ static metadataOutputTypes = {
104
+ output: "audio"
105
+ };
106
+ static inlineFields = [];
107
+ static inputFields = ["audio"];
108
+ async process(context) {
109
+ const audio = this.audio;
110
+ const thresholdDb = this.threshold;
111
+ const ratio = this.ratio;
112
+ const attackMs = this.attack;
113
+ const releaseMs = this.release;
114
+ const autoGain = this.auto_gain;
115
+ const bytes = await requireAudioBytes(audio, context);
116
+ // Makeup gain: half of the reduction a 0 dBFS peak would receive — the
117
+ // usual "auto" heuristic. It restores most of the loudness lost to
118
+ // compression while staying clear of clipping the way full compensation
119
+ // (referenced to 0 dBFS) would on under-compressed transients.
120
+ const makeupDb = autoGain ? (-thresholdDb * (1 - 1 / ratio)) / 2 : 0;
121
+ const makeup = Math.pow(10, makeupDb / 20);
122
+ const wav = await decodeAudioToWav(bytes);
123
+ const result = processPerChannel(wav, (ch, sr) => {
124
+ const out = new Float32Array(ch.length);
125
+ const attackCoeff = Math.exp(-1 / ((sr * attackMs) / 1000));
126
+ const releaseCoeff = Math.exp(-1 / ((sr * releaseMs) / 1000));
127
+ const thresholdLin = Math.pow(10, thresholdDb / 20);
128
+ let envelope = 0;
129
+ for (let i = 0; i < ch.length; i++) {
130
+ const absVal = Math.abs(ch[i]);
131
+ if (absVal > envelope) {
132
+ envelope = attackCoeff * envelope + (1 - attackCoeff) * absVal;
133
+ }
134
+ else {
135
+ envelope = releaseCoeff * envelope + (1 - releaseCoeff) * absVal;
136
+ }
137
+ if (envelope > thresholdLin) {
138
+ const dbOver = 20 * Math.log10(envelope / thresholdLin);
139
+ const dbReduction = dbOver * (1 - 1 / ratio);
140
+ const gainReduction = Math.pow(10, -dbReduction / 20);
141
+ out[i] = ch[i] * gainReduction * makeup;
142
+ }
143
+ else {
144
+ out[i] = ch[i] * makeup;
145
+ }
146
+ }
147
+ return out;
148
+ });
149
+ return {
150
+ output: audioRefFromWav(encodeWav(result.samples, result.sampleRate, result.numChannels))
151
+ };
152
+ }
153
+ }
154
+ __decorate([
155
+ prop({
156
+ type: "audio",
157
+ default: {
158
+ type: "audio",
159
+ uri: "",
160
+ asset_id: null,
161
+ data: null,
162
+ metadata: null
163
+ },
164
+ title: "Audio",
165
+ description: "The audio file to process."
166
+ })
167
+ ], CompressNode.prototype, "audio", void 0);
168
+ __decorate([
169
+ prop({
170
+ type: "float",
171
+ default: -20,
172
+ title: "Threshold",
173
+ description: "Threshold in dB above which compression is applied.",
174
+ min: -60,
175
+ max: 0
176
+ })
177
+ ], CompressNode.prototype, "threshold", void 0);
178
+ __decorate([
179
+ prop({
180
+ type: "float",
181
+ default: 4,
182
+ title: "Ratio",
183
+ description: "Compression ratio. Higher values result in more compression.",
184
+ min: 1,
185
+ max: 20
186
+ })
187
+ ], CompressNode.prototype, "ratio", void 0);
188
+ __decorate([
189
+ prop({
190
+ type: "float",
191
+ default: 5,
192
+ title: "Attack",
193
+ description: "Attack time in milliseconds.",
194
+ min: 0.1,
195
+ max: 100
196
+ })
197
+ ], CompressNode.prototype, "attack", void 0);
198
+ __decorate([
199
+ prop({
200
+ type: "float",
201
+ default: 50,
202
+ title: "Release",
203
+ description: "Release time in milliseconds.",
204
+ min: 5,
205
+ max: 1000
206
+ })
207
+ ], CompressNode.prototype, "release", void 0);
208
+ __decorate([
209
+ prop({
210
+ type: "bool",
211
+ default: true,
212
+ title: "Auto Gain",
213
+ description: "Apply automatic makeup gain to compensate for the level lost to compression, keeping the output roughly as loud as the input."
214
+ })
215
+ ], CompressNode.prototype, "auto_gain", void 0);
216
+ export class DistortionNode extends BaseNode {
217
+ static nodeType = "lib.audio.Distortion";
218
+ static title = "Distortion";
219
+ static description = "Applies a distortion effect to an audio file.\n audio, effect, distortion\n\n Use cases:\n - Add grit and character to instruments\n - Create aggressive sound effects\n - Simulate overdriven amplifiers";
220
+ static metadataOutputTypes = {
221
+ output: "audio"
222
+ };
223
+ static inlineFields = [];
224
+ static inputFields = ["audio"];
225
+ async process(context) {
226
+ const audio = this.audio;
227
+ const driveDb = this.drive_db;
228
+ const bytes = await requireAudioBytes(audio, context);
229
+ const wav = await decodeAudioToWav(bytes);
230
+ const drive = Math.pow(10, driveDb / 20);
231
+ const result = processPerChannel(wav, (ch) => {
232
+ const out = new Float32Array(ch.length);
233
+ for (let i = 0; i < ch.length; i++) {
234
+ const driven = ch[i] * drive;
235
+ out[i] = (2 / Math.PI) * Math.atan(driven);
236
+ }
237
+ return out;
238
+ });
239
+ return {
240
+ output: audioRefFromWav(encodeWav(result.samples, result.sampleRate, result.numChannels))
241
+ };
242
+ }
243
+ }
244
+ __decorate([
245
+ prop({
246
+ type: "audio",
247
+ default: {
248
+ type: "audio",
249
+ uri: "",
250
+ asset_id: null,
251
+ data: null,
252
+ metadata: null
253
+ },
254
+ title: "Audio",
255
+ description: "The audio file to process."
256
+ })
257
+ ], DistortionNode.prototype, "audio", void 0);
258
+ __decorate([
259
+ prop({
260
+ type: "float",
261
+ default: 25,
262
+ title: "Drive Db",
263
+ description: "Amount of distortion to apply in decibels.",
264
+ min: 0,
265
+ max: 100
266
+ })
267
+ ], DistortionNode.prototype, "drive_db", void 0);
268
+ export class LimiterNode extends BaseNode {
269
+ static nodeType = "lib.audio.Limiter";
270
+ static title = "Limiter";
271
+ static description = "Applies a limiter effect to an audio file.\n audio, effect, dynamics\n\n Use cases:\n - Prevent audio clipping\n - Increase perceived loudness without distortion\n - Control dynamic range of audio";
272
+ static metadataOutputTypes = {
273
+ output: "audio"
274
+ };
275
+ static inlineFields = [];
276
+ static inputFields = ["audio"];
277
+ async process(context) {
278
+ const audio = this.audio;
279
+ const thresholdDb = this.threshold_db;
280
+ const releaseMs = this.release_ms;
281
+ const autoGain = this.auto_gain;
282
+ const bytes = await requireAudioBytes(audio, context);
283
+ const wav = await decodeAudioToWav(bytes);
284
+ const threshold = Math.pow(10, thresholdDb / 20);
285
+ // Makeup gain brings the ceiling up to 0 dBFS. Limited peaks sit at
286
+ // `threshold`, so scaling by 1/threshold normalizes them to full scale
287
+ // without pushing past it.
288
+ const makeup = autoGain ? 1 / threshold : 1;
289
+ const result = processPerChannel(wav, (ch, sr) => {
290
+ const out = new Float32Array(ch.length);
291
+ const releaseCoeff = Math.exp(-1 / ((sr * releaseMs) / 1000));
292
+ let gainReduction = 1;
293
+ for (let i = 0; i < ch.length; i++) {
294
+ const absVal = Math.abs(ch[i]);
295
+ if (absVal > threshold) {
296
+ const targetGain = threshold / absVal;
297
+ if (targetGain < gainReduction) {
298
+ gainReduction = targetGain;
299
+ }
300
+ }
301
+ else {
302
+ gainReduction =
303
+ releaseCoeff * gainReduction + (1 - releaseCoeff) * 1.0;
304
+ }
305
+ out[i] = ch[i] * gainReduction * makeup;
306
+ }
307
+ return out;
308
+ });
309
+ return {
310
+ output: audioRefFromWav(encodeWav(result.samples, result.sampleRate, result.numChannels))
311
+ };
312
+ }
313
+ }
314
+ __decorate([
315
+ prop({
316
+ type: "audio",
317
+ default: {
318
+ type: "audio",
319
+ uri: "",
320
+ asset_id: null,
321
+ data: null,
322
+ metadata: null
323
+ },
324
+ title: "Audio",
325
+ description: "The audio file to process."
326
+ })
327
+ ], LimiterNode.prototype, "audio", void 0);
328
+ __decorate([
329
+ prop({
330
+ type: "float",
331
+ default: -2,
332
+ title: "Threshold Db",
333
+ description: "Threshold in dB above which the limiter is applied.",
334
+ min: -60,
335
+ max: 0
336
+ })
337
+ ], LimiterNode.prototype, "threshold_db", void 0);
338
+ __decorate([
339
+ prop({
340
+ type: "float",
341
+ default: 250,
342
+ title: "Release Ms",
343
+ description: "Release time in milliseconds.",
344
+ min: 1,
345
+ max: 1000
346
+ })
347
+ ], LimiterNode.prototype, "release_ms", void 0);
348
+ __decorate([
349
+ prop({
350
+ type: "bool",
351
+ default: true,
352
+ title: "Auto Gain",
353
+ description: "Apply automatic makeup gain so the limited peaks reach 0 dBFS, turning the headroom below the ceiling into added loudness (maximizer behaviour)."
354
+ })
355
+ ], LimiterNode.prototype, "auto_gain", void 0);
356
+ export class ReverbNode extends BaseNode {
357
+ static nodeType = "lib.audio.Reverb";
358
+ static title = "Reverb";
359
+ static description = "Applies a reverb effect to an audio file.\n audio, effect, reverb\n\n Use cases:\n - Add spatial depth to dry recordings\n - Simulate different room acoustics\n - Create atmospheric sound effects";
360
+ static metadataOutputTypes = {
361
+ output: "audio"
362
+ };
363
+ static inlineFields = [];
364
+ static inputFields = ["audio"];
365
+ async process(context) {
366
+ const audio = this.audio;
367
+ const roomScale = this.room_scale;
368
+ const damping = this.damping;
369
+ const wetLevel = this.wet_level;
370
+ const dryLevel = this.dry_level;
371
+ const bytes = await requireAudioBytes(audio, context);
372
+ const wav = await decodeAudioToWav(bytes);
373
+ const result = processPerChannel(wav, (ch, sr) => {
374
+ // Schroeder reverb: 4 parallel comb filters -> 2 series allpass filters
375
+ const baseCombDelays = [1557, 1617, 1491, 1422];
376
+ const baseAllpassDelays = [225, 556];
377
+ const scale = sr / 44100;
378
+ // Comb filter
379
+ function combFilter(input, delaySamples, feedback, damp) {
380
+ const out = new Float32Array(input.length);
381
+ const buf = new Float32Array(delaySamples);
382
+ let bufIdx = 0;
383
+ let filterStore = 0;
384
+ for (let i = 0; i < input.length; i++) {
385
+ const delayed = buf[bufIdx];
386
+ filterStore = delayed * (1 - damp) + filterStore * damp;
387
+ buf[bufIdx] = input[i] + filterStore * feedback;
388
+ out[i] = delayed;
389
+ bufIdx = (bufIdx + 1) % delaySamples;
390
+ }
391
+ return out;
392
+ }
393
+ // Allpass filter
394
+ function allpassFilter(input, delaySamples, feedback) {
395
+ const out = new Float32Array(input.length);
396
+ const buf = new Float32Array(delaySamples);
397
+ let bufIdx = 0;
398
+ for (let i = 0; i < input.length; i++) {
399
+ const delayed = buf[bufIdx];
400
+ buf[bufIdx] = input[i] + delayed * feedback;
401
+ out[i] = delayed - input[i] * feedback;
402
+ bufIdx = (bufIdx + 1) % delaySamples;
403
+ }
404
+ return out;
405
+ }
406
+ const feedback = roomScale * 0.28 + 0.7;
407
+ // Sum comb filters
408
+ const combOut = new Float32Array(ch.length);
409
+ for (const baseDelay of baseCombDelays) {
410
+ const delay = Math.round(baseDelay * scale);
411
+ const filtered = combFilter(ch, delay, feedback, damping);
412
+ for (let i = 0; i < ch.length; i++) {
413
+ combOut[i] += filtered[i];
414
+ }
415
+ }
416
+ // Series allpass filters
417
+ let apOut = combOut;
418
+ for (const baseDelay of baseAllpassDelays) {
419
+ const delay = Math.round(baseDelay * scale);
420
+ apOut = allpassFilter(apOut, delay, 0.5);
421
+ }
422
+ // Mix dry/wet
423
+ const out = new Float32Array(ch.length);
424
+ for (let i = 0; i < ch.length; i++) {
425
+ out[i] = ch[i] * dryLevel + apOut[i] * wetLevel;
426
+ }
427
+ return out;
428
+ });
429
+ return {
430
+ output: audioRefFromWav(encodeWav(result.samples, result.sampleRate, result.numChannels))
431
+ };
432
+ }
433
+ }
434
+ __decorate([
435
+ prop({
436
+ type: "audio",
437
+ default: {
438
+ type: "audio",
439
+ uri: "",
440
+ asset_id: null,
441
+ data: null,
442
+ metadata: null
443
+ },
444
+ title: "Audio",
445
+ description: "The audio file to process."
446
+ })
447
+ ], ReverbNode.prototype, "audio", void 0);
448
+ __decorate([
449
+ prop({
450
+ type: "float",
451
+ default: 0.5,
452
+ title: "Room Scale",
453
+ description: "Size of the simulated room. Higher values create larger spaces.",
454
+ min: 0,
455
+ max: 1
456
+ })
457
+ ], ReverbNode.prototype, "room_scale", void 0);
458
+ __decorate([
459
+ prop({
460
+ type: "float",
461
+ default: 0.5,
462
+ title: "Damping",
463
+ description: "Amount of high frequency absorption. Higher values create a duller sound.",
464
+ min: 0,
465
+ max: 1
466
+ })
467
+ ], ReverbNode.prototype, "damping", void 0);
468
+ __decorate([
469
+ prop({
470
+ type: "float",
471
+ default: 0.15,
472
+ title: "Wet Level",
473
+ description: "Level of the reverb effect in the output.",
474
+ min: 0,
475
+ max: 1
476
+ })
477
+ ], ReverbNode.prototype, "wet_level", void 0);
478
+ __decorate([
479
+ prop({
480
+ type: "float",
481
+ default: 0.5,
482
+ title: "Dry Level",
483
+ description: "Level of the original signal in the output.",
484
+ min: 0,
485
+ max: 1
486
+ })
487
+ ], ReverbNode.prototype, "dry_level", void 0);
488
+ /**
489
+ * Lazily load rubberband via a bundler-hidden import. The hidden import keeps
490
+ * the native/WASM package out of the web bundle (this file is loaded in the
491
+ * browser for its hybrid effects); the two nodes that need it stay
492
+ * server-only and throw when invoked off Node.
493
+ */
494
+ async function loadRubberband() {
495
+ const mod = await importHidden("@k13engineering/rubberband");
496
+ if (!mod) {
497
+ throw new Error("PitchShift/TimeStretch require Node (rubberband is not available in the browser)");
498
+ }
499
+ return mod;
500
+ }
501
+ export class PitchShiftNode extends BaseNode {
502
+ static nodeType = "lib.audio.PitchShift";
503
+ static title = "Pitch Shift";
504
+ static description = "Shifts the pitch of an audio file without changing its duration.\n audio, effect, pitch\n\n Use cases:\n - Transpose audio to a different key\n - Create harmonies or vocal effects\n - Adjust instrument tuning";
505
+ static metadataOutputTypes = {
506
+ output: "audio"
507
+ };
508
+ static inlineFields = [];
509
+ static inputFields = ["audio"];
510
+ async process(context) {
511
+ const audio = this.audio;
512
+ const semitones = this.semitones;
513
+ if (semitones === 0) {
514
+ return { output: audio };
515
+ }
516
+ const bytes = await requireAudioBytes(audio, context);
517
+ const { createRubberbandWrapper } = await loadRubberband();
518
+ const wav = await decodeAudioToWav(bytes);
519
+ const { samples, sampleRate, numChannels } = wav;
520
+ const frameSamples = Math.floor(samples.length / numChannels);
521
+ const CHUNK_SIZE = 8192;
522
+ const rb = createRubberbandWrapper({
523
+ sampleRate,
524
+ channelCount: numChannels,
525
+ maxBufferSizeInFrames: CHUNK_SIZE,
526
+ options: {
527
+ processMode: "realtime",
528
+ engineMode: "finer",
529
+ pitchMode: "highquality"
530
+ }
531
+ });
532
+ rb.requestPitchScale({ pitchScale: Math.pow(2, semitones / 12) });
533
+ // Deinterleave into per-channel planes
534
+ const planes = [];
535
+ for (let ch = 0; ch < numChannels; ch++) {
536
+ const plane = new Float32Array(frameSamples);
537
+ for (let i = 0; i < frameSamples; i++) {
538
+ plane[i] = samples[i * numChannels + ch];
539
+ }
540
+ planes.push(plane);
541
+ }
542
+ // Process in chunks, collecting output
543
+ const outputParts = [];
544
+ const collectAvailable = () => {
545
+ let avail = rb.available();
546
+ while (avail > 0) {
547
+ const count = Math.min(avail, CHUNK_SIZE);
548
+ outputParts.push(rb.retrieve({ sampleCount: count }).planes);
549
+ avail = rb.available();
550
+ }
551
+ };
552
+ for (let offset = 0; offset < frameSamples; offset += CHUNK_SIZE) {
553
+ const end = Math.min(offset + CHUNK_SIZE, frameSamples);
554
+ const chunkPlanes = planes.map((p) => p.slice(offset, end));
555
+ rb.process({ audioData: { planes: chunkPlanes } });
556
+ collectAvailable();
557
+ }
558
+ rb.end();
559
+ collectAvailable();
560
+ // Concatenate output per channel
561
+ const totalOutFrames = outputParts.reduce((s, p) => s + p[0].length, 0);
562
+ const outPlanes = [];
563
+ for (let ch = 0; ch < numChannels; ch++) {
564
+ const plane = new Float32Array(totalOutFrames);
565
+ let pos = 0;
566
+ for (const part of outputParts) {
567
+ plane.set(part[ch], pos);
568
+ pos += part[ch].length;
569
+ }
570
+ outPlanes.push(plane);
571
+ }
572
+ // Trim processing latency — pitch shift preserves duration
573
+ const latency = rb.latencyInSamples();
574
+ const trimStart = Math.min(latency, totalOutFrames);
575
+ const trimEnd = Math.min(trimStart + frameSamples, totalOutFrames);
576
+ const outFrames = trimEnd - trimStart;
577
+ // Re-interleave trimmed output
578
+ const outSamples = new Float32Array(outFrames * numChannels);
579
+ for (let i = 0; i < outFrames; i++) {
580
+ for (let ch = 0; ch < numChannels; ch++) {
581
+ outSamples[i * numChannels + ch] = outPlanes[ch][trimStart + i];
582
+ }
583
+ }
584
+ return {
585
+ output: audioRefFromWav(encodeWav(outSamples, sampleRate, numChannels))
586
+ };
587
+ }
588
+ }
589
+ __decorate([
590
+ prop({
591
+ type: "audio",
592
+ default: {
593
+ type: "audio",
594
+ uri: "",
595
+ asset_id: null,
596
+ data: null,
597
+ metadata: null
598
+ },
599
+ title: "Audio",
600
+ description: "The audio file to process."
601
+ })
602
+ ], PitchShiftNode.prototype, "audio", void 0);
603
+ __decorate([
604
+ prop({
605
+ type: "float",
606
+ default: 0,
607
+ title: "Semitones",
608
+ description: "Number of semitones to shift the pitch. Positive values shift up, negative values shift down.",
609
+ min: -12,
610
+ max: 12
611
+ })
612
+ ], PitchShiftNode.prototype, "semitones", void 0);
613
+ // ── TimeStretch (rubberband WASM) ────────────────────────────────
614
+ export class TimeStretchNode extends BaseNode {
615
+ static nodeType = "lib.audio.TimeStretch";
616
+ static title = "Time Stretch";
617
+ static description = "Changes the speed of an audio file without altering its pitch.\n audio, transform, time\n\n Use cases:\n - Adjust audio duration to fit video length\n - Create slow-motion or fast-motion audio effects\n - Synchronize audio tracks of different lengths";
618
+ static metadataOutputTypes = {
619
+ output: "audio"
620
+ };
621
+ static inlineFields = [];
622
+ static inputFields = ["audio"];
623
+ async process(context) {
624
+ const audio = this.audio;
625
+ const rate = this.rate;
626
+ if (rate === 1.0) {
627
+ return { output: audio };
628
+ }
629
+ const bytes = await requireAudioBytes(audio, context);
630
+ const { createRubberbandWrapper } = await loadRubberband();
631
+ const wav = await decodeAudioToWav(bytes);
632
+ const { samples, sampleRate, numChannels } = wav;
633
+ const frameSamples = Math.floor(samples.length / numChannels);
634
+ const CHUNK_SIZE = 8192;
635
+ const rb = createRubberbandWrapper({
636
+ sampleRate,
637
+ channelCount: numChannels,
638
+ maxBufferSizeInFrames: CHUNK_SIZE,
639
+ options: {
640
+ processMode: "realtime",
641
+ engineMode: "finer"
642
+ }
643
+ });
644
+ // Rubber Band timeRatio = output_duration / input_duration
645
+ // rate > 1 means faster (shorter output), so timeRatio = 1 / rate
646
+ rb.requestTimeRatio({ timeRatio: 1 / rate });
647
+ // Deinterleave into per-channel planes
648
+ const planes = [];
649
+ for (let ch = 0; ch < numChannels; ch++) {
650
+ const plane = new Float32Array(frameSamples);
651
+ for (let i = 0; i < frameSamples; i++) {
652
+ plane[i] = samples[i * numChannels + ch];
653
+ }
654
+ planes.push(plane);
655
+ }
656
+ // Process in chunks, collecting output
657
+ const outputParts = [];
658
+ const collectAvailable = () => {
659
+ let avail = rb.available();
660
+ while (avail > 0) {
661
+ const count = Math.min(avail, CHUNK_SIZE);
662
+ outputParts.push(rb.retrieve({ sampleCount: count }).planes);
663
+ avail = rb.available();
664
+ }
665
+ };
666
+ for (let offset = 0; offset < frameSamples; offset += CHUNK_SIZE) {
667
+ const end = Math.min(offset + CHUNK_SIZE, frameSamples);
668
+ const chunkPlanes = planes.map((p) => p.slice(offset, end));
669
+ rb.process({ audioData: { planes: chunkPlanes } });
670
+ collectAvailable();
671
+ }
672
+ rb.end();
673
+ collectAvailable();
674
+ // Concatenate output per channel
675
+ const totalOutFrames = outputParts.reduce((s, p) => s + p[0].length, 0);
676
+ const outPlanes = [];
677
+ for (let ch = 0; ch < numChannels; ch++) {
678
+ const plane = new Float32Array(totalOutFrames);
679
+ let pos = 0;
680
+ for (const part of outputParts) {
681
+ plane.set(part[ch], pos);
682
+ pos += part[ch].length;
683
+ }
684
+ outPlanes.push(plane);
685
+ }
686
+ // Trim processing latency — expected output = input / rate
687
+ const latency = rb.latencyInSamples();
688
+ const expectedFrames = Math.round(frameSamples / rate);
689
+ const trimStart = Math.min(latency, totalOutFrames);
690
+ const trimEnd = Math.min(trimStart + expectedFrames, totalOutFrames);
691
+ const outFrames = trimEnd - trimStart;
692
+ // Re-interleave trimmed output
693
+ const outSamples = new Float32Array(outFrames * numChannels);
694
+ for (let i = 0; i < outFrames; i++) {
695
+ for (let ch = 0; ch < numChannels; ch++) {
696
+ outSamples[i * numChannels + ch] = outPlanes[ch][trimStart + i];
697
+ }
698
+ }
699
+ return {
700
+ output: audioRefFromWav(encodeWav(outSamples, sampleRate, numChannels))
701
+ };
702
+ }
703
+ }
704
+ __decorate([
705
+ prop({
706
+ type: "audio",
707
+ default: {
708
+ type: "audio",
709
+ uri: "",
710
+ asset_id: null,
711
+ data: null,
712
+ metadata: null
713
+ },
714
+ title: "Audio",
715
+ description: "The audio file to process."
716
+ })
717
+ ], TimeStretchNode.prototype, "audio", void 0);
718
+ __decorate([
719
+ prop({
720
+ type: "float",
721
+ default: 1,
722
+ title: "Rate",
723
+ description: "Time stretch factor. Values > 1 speed up, < 1 slow down.",
724
+ min: 0.5,
725
+ max: 2
726
+ })
727
+ ], TimeStretchNode.prototype, "rate", void 0);
728
+ export class NoiseGateNode extends BaseNode {
729
+ static nodeType = "lib.audio.NoiseGate";
730
+ static title = "Noise Gate";
731
+ static description = "Applies a noise gate effect to an audio file.\n audio, effect, dynamics\n\n Use cases:\n - Reduce background noise in recordings\n - Clean up audio tracks with unwanted low-level sounds\n - Create rhythmic effects by gating sustained sounds";
732
+ static metadataOutputTypes = {
733
+ output: "audio"
734
+ };
735
+ static inlineFields = [];
736
+ static inputFields = ["audio"];
737
+ async process(context) {
738
+ const audio = this.audio;
739
+ const thresholdDb = this.threshold_db;
740
+ const attackMs = Math.max(0.1, this.attack_ms);
741
+ const releaseMs = Math.max(1, this.release_ms);
742
+ const bytes = await requireAudioBytes(audio, context);
743
+ const wav = await decodeAudioToWav(bytes);
744
+ const thresholdLin = Math.pow(10, thresholdDb / 20);
745
+ const result = processPerChannel(wav, (ch, sr) => {
746
+ const out = new Float32Array(ch.length);
747
+ const attackCoeff = Math.exp(-1 / ((sr * attackMs) / 1000));
748
+ const releaseCoeff = Math.exp(-1 / ((sr * releaseMs) / 1000));
749
+ let envelope = 0;
750
+ let gain = 0;
751
+ for (let i = 0; i < ch.length; i++) {
752
+ const absVal = Math.abs(ch[i]);
753
+ // Envelope follower: fast attack, slow release
754
+ if (absVal > envelope) {
755
+ envelope = attackCoeff * envelope + (1 - attackCoeff) * absVal;
756
+ }
757
+ else {
758
+ envelope = releaseCoeff * envelope + (1 - releaseCoeff) * absVal;
759
+ }
760
+ // Gate: open when above threshold, close when below. Smooth the gain
761
+ // toward the target using the attack coefficient while opening and the
762
+ // release coefficient while closing, so both controls take effect.
763
+ const targetGain = envelope >= thresholdLin ? 1 : 0;
764
+ const coeff = targetGain > gain ? attackCoeff : releaseCoeff;
765
+ gain = coeff * gain + (1 - coeff) * targetGain;
766
+ out[i] = ch[i] * gain;
767
+ }
768
+ return out;
769
+ });
770
+ return {
771
+ output: audioRefFromWav(encodeWav(result.samples, result.sampleRate, result.numChannels))
772
+ };
773
+ }
774
+ }
775
+ __decorate([
776
+ prop({
777
+ type: "audio",
778
+ default: {
779
+ type: "audio",
780
+ uri: "",
781
+ asset_id: null,
782
+ data: null,
783
+ metadata: null
784
+ },
785
+ title: "Audio",
786
+ description: "The audio file to process."
787
+ })
788
+ ], NoiseGateNode.prototype, "audio", void 0);
789
+ __decorate([
790
+ prop({
791
+ type: "float",
792
+ default: -50,
793
+ title: "Threshold Db",
794
+ description: "Threshold in dB below which the gate is active.",
795
+ min: -90,
796
+ max: 0
797
+ })
798
+ ], NoiseGateNode.prototype, "threshold_db", void 0);
799
+ __decorate([
800
+ prop({
801
+ type: "float",
802
+ default: 1,
803
+ title: "Attack Ms",
804
+ description: "Attack time in milliseconds.",
805
+ min: 0.1,
806
+ max: 100
807
+ })
808
+ ], NoiseGateNode.prototype, "attack_ms", void 0);
809
+ __decorate([
810
+ prop({
811
+ type: "float",
812
+ default: 100,
813
+ title: "Release Ms",
814
+ description: "Release time in milliseconds.",
815
+ min: 5,
816
+ max: 1000
817
+ })
818
+ ], NoiseGateNode.prototype, "release_ms", void 0);
819
+ export class PhaserNode extends BaseNode {
820
+ static nodeType = "lib.audio.Phaser";
821
+ static title = "Phaser";
822
+ static description = "Applies a phaser effect to an audio file.\n audio, effect, modulation\n\n Use cases:\n - Create sweeping, swooshing sounds\n - Add movement to static sounds\n - Produce psychedelic or space-like effects";
823
+ static metadataOutputTypes = {
824
+ output: "audio"
825
+ };
826
+ static inlineFields = [];
827
+ static inputFields = ["audio"];
828
+ async process(context) {
829
+ const audio = this.audio;
830
+ const rateHz = this.rate_hz;
831
+ const depth = this.depth;
832
+ const centreFreqHz = this.centre_frequency_hz;
833
+ const feedback = this.feedback;
834
+ const mix = this.mix;
835
+ const bytes = await requireAudioBytes(audio, context);
836
+ const wav = await decodeAudioToWav(bytes);
837
+ const result = processPerChannel(wav, (ch, sr) => {
838
+ const out = new Float32Array(ch.length);
839
+ // 4-stage first-order all-pass phaser
840
+ const numStages = 4;
841
+ // Delay buffers: x[n-1] and y[n-1] for each stage
842
+ const xPrev = new Float32Array(numStages);
843
+ const yPrev = new Float32Array(numStages);
844
+ // Feedback delay
845
+ let feedbackSample = 0;
846
+ let lfoPhase = 0;
847
+ const lfoIncrement = (2 * Math.PI * rateHz) / sr;
848
+ for (let i = 0; i < ch.length; i++) {
849
+ // LFO modulates centre frequency
850
+ const lfo = Math.sin(lfoPhase) * depth;
851
+ lfoPhase += lfoIncrement;
852
+ if (lfoPhase > 2 * Math.PI)
853
+ lfoPhase -= 2 * Math.PI;
854
+ // Modulated frequency (centre ± depth range)
855
+ const modFreq = Math.max(20, Math.min(sr / 2 - 1, centreFreqHz * (1 + lfo)));
856
+ const tanVal = Math.tan((Math.PI * modFreq) / sr);
857
+ const a = (tanVal - 1) / (tanVal + 1);
858
+ // Input with feedback
859
+ const signal = ch[i] + feedbackSample * feedback;
860
+ // Apply all-pass filter stages
861
+ let apOut = signal;
862
+ for (let s = 0; s < numStages; s++) {
863
+ const newOut = a * apOut + xPrev[s] - a * yPrev[s];
864
+ xPrev[s] = apOut;
865
+ yPrev[s] = newOut;
866
+ apOut = newOut;
867
+ }
868
+ feedbackSample = apOut;
869
+ // Mix dry and wet
870
+ out[i] = ch[i] * (1 - mix) + apOut * mix;
871
+ }
872
+ return out;
873
+ });
874
+ return {
875
+ output: audioRefFromWav(encodeWav(result.samples, result.sampleRate, result.numChannels))
876
+ };
877
+ }
878
+ }
879
+ __decorate([
880
+ prop({
881
+ type: "audio",
882
+ default: {
883
+ type: "audio",
884
+ uri: "",
885
+ asset_id: null,
886
+ data: null,
887
+ metadata: null
888
+ },
889
+ title: "Audio",
890
+ description: "The audio file to process."
891
+ })
892
+ ], PhaserNode.prototype, "audio", void 0);
893
+ __decorate([
894
+ prop({
895
+ type: "float",
896
+ default: 1,
897
+ title: "Rate Hz",
898
+ description: "Rate of the phaser effect in Hz.",
899
+ min: 0.1,
900
+ max: 10
901
+ })
902
+ ], PhaserNode.prototype, "rate_hz", void 0);
903
+ __decorate([
904
+ prop({
905
+ type: "float",
906
+ default: 0.5,
907
+ title: "Depth",
908
+ description: "Depth of the phaser effect.",
909
+ min: 0,
910
+ max: 1
911
+ })
912
+ ], PhaserNode.prototype, "depth", void 0);
913
+ __decorate([
914
+ prop({
915
+ type: "float",
916
+ default: 1300,
917
+ title: "Centre Frequency Hz",
918
+ description: "Centre frequency of the phaser in Hz.",
919
+ min: 100,
920
+ max: 5000
921
+ })
922
+ ], PhaserNode.prototype, "centre_frequency_hz", void 0);
923
+ __decorate([
924
+ prop({
925
+ type: "float",
926
+ default: 0,
927
+ title: "Feedback",
928
+ description: "Feedback of the phaser effect. Negative values invert the phase.",
929
+ min: -1,
930
+ max: 1
931
+ })
932
+ ], PhaserNode.prototype, "feedback", void 0);
933
+ __decorate([
934
+ prop({
935
+ type: "float",
936
+ default: 0.5,
937
+ title: "Mix",
938
+ description: "Mix between the dry (original) and wet (effected) signals.",
939
+ min: 0,
940
+ max: 1
941
+ })
942
+ ], PhaserNode.prototype, "mix", void 0);
943
+ // ── Export ────────────────────────────────────────────────────────
944
+ /**
945
+ * All pedalboard effects run on the server. They decode the input audio to PCM
946
+ * (WAV directly, or mp3/flac/… via WebAudio `decodeAudioData`), which needs
947
+ * Node's `node-web-audio-api`; the rubberband-backed nodes additionally use a
948
+ * native/WASM addon that only loads on Node.
949
+ */
950
+ export const LIB_AUDIO_EFFECTS_NODES = tagAsServer([
951
+ BitcrushNode,
952
+ CompressNode,
953
+ DistortionNode,
954
+ LimiterNode,
955
+ ReverbNode,
956
+ NoiseGateNode,
957
+ PhaserNode,
958
+ PitchShiftNode,
959
+ TimeStretchNode
960
+ ]);
961
+ export const LIB_PEDALBOARD_EXTRA_NODES = LIB_AUDIO_EFFECTS_NODES;
962
+ //# sourceMappingURL=lib-audio-effects.js.map