|
| 1 | +// Example: Faust DSP with parameters and runtime tweaks using node-web-audio-api. |
| 2 | +// Requires: npm install @grame/faustwasm |
| 3 | +// Usage: node faust-params.js |
| 4 | + |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | +import { AudioContext, AudioWorkletNode } from '../index.mjs'; |
| 7 | + |
| 8 | +// Expose AudioWorkletNode globally so faustwasm can patch it into worklet factories. |
| 9 | +if (typeof globalThis.AudioWorkletNode === 'undefined') { |
| 10 | + globalThis.AudioWorkletNode = AudioWorkletNode; |
| 11 | +} |
| 12 | + |
| 13 | +const { |
| 14 | + instantiateFaustModuleFromFile, |
| 15 | + LibFaust, |
| 16 | + FaustCompiler, |
| 17 | + FaustMonoDspGenerator, |
| 18 | +} = await import('@grame/faustwasm/dist/esm/index.js'); |
| 19 | + |
| 20 | +const audioContext = new AudioContext({ latencyHint: 'interactive' }); |
| 21 | + |
| 22 | +// Stereo tone with controllable gain, pitch and filter cutoff. |
| 23 | +const dspCode = ` |
| 24 | +import("stdfaust.lib"); |
| 25 | +
|
| 26 | +gain = hslider("gain[dB]", -6, -24, 6, 0.1) : ba.db2linear; |
| 27 | +freq = hslider("freq[Hz]", 220, 50, 2000, 1); |
| 28 | +cutoff = hslider("cutoff[Hz]", 800, 200, 4000, 1); |
| 29 | +
|
| 30 | +process = vgroup("faust-params", |
| 31 | + os.osc(freq) * gain : fi.lowpass(2, cutoff) |
| 32 | +) <: _,_; |
| 33 | +`; |
| 34 | + |
| 35 | +// Load the Faust toolchain from the wasm distribution. |
| 36 | +const faustModule = await instantiateFaustModuleFromFile( |
| 37 | + fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.js')), |
| 38 | + fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.data')), |
| 39 | + fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.wasm')), |
| 40 | +); |
| 41 | + |
| 42 | +const libFaust = new LibFaust(faustModule); |
| 43 | +const compiler = new FaustCompiler(libFaust); |
| 44 | +const generator = new FaustMonoDspGenerator(); |
| 45 | + |
| 46 | +const compiled = await generator.compile(compiler, 'faust-params', dspCode, '-ftz 2'); |
| 47 | +if (!compiled) { |
| 48 | + throw new Error('Faust compilation failed'); |
| 49 | +} |
| 50 | + |
| 51 | +const faustNode = await generator.createNode(audioContext); |
| 52 | +if (!faustNode) { |
| 53 | + throw new Error('Failed to create Faust node'); |
| 54 | +} |
| 55 | + |
| 56 | +console.log('Faust parameterized node created.', faustNode); |
| 57 | + |
| 58 | +faustNode.connect(audioContext.destination); |
| 59 | +faustNode.start(); |
| 60 | + |
| 61 | + |
| 62 | +// Helpers to inspect and tweak parameters. The parameter names come from the Faust UI paths. |
| 63 | +const params = faustNode.getParams?.() ?? []; |
| 64 | +console.log('Available parameters:', params); |
| 65 | + |
| 66 | +const set = (name, value) => { |
| 67 | + faustNode.setParamValue(name, value); |
| 68 | + console.log(`Set ${name} -> ${value}`); |
| 69 | +}; |
| 70 | + |
| 71 | +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); |
| 72 | + |
| 73 | +// Parameter automation demo. |
| 74 | +set('/faust-params/gain', -12); |
| 75 | +set('/faust-params/freq', 220); |
| 76 | +set('/faust-params/cutoff', 800); |
| 77 | +set('/faust-params/resonance', 0.9); |
| 78 | + |
| 79 | +console.log('Sweeping parameters for 6 seconds...'); |
| 80 | +await sleep(1500); |
| 81 | +set('/faust-params/freq', 440); |
| 82 | +set('/faust-params/cutoff', 1200); |
| 83 | + |
| 84 | +await sleep(1500); |
| 85 | +set('/faust-params/gain', -3); |
| 86 | +set('/faust-params/resonance', 1.8); |
| 87 | + |
| 88 | +await sleep(1500); |
| 89 | +set('/faust-params/freq', 660); |
| 90 | +set('/faust-params/cutoff', 1800); |
| 91 | + |
| 92 | +await sleep(1500); |
| 93 | +console.log('Stopping...'); |
| 94 | + |
| 95 | +faustNode.stop(); |
| 96 | +await audioContext.close(); |
0 commit comments