generated from napi-rs/package-template
-
Notifications
You must be signed in to change notification settings - Fork 24
Add Faust examples #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Faust examples #162
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // Example: French church bell physical model with parameter tweaks. | ||
| // Source DSP: https://faustdoc.grame.fr/examples/physicalModeling/#frenchbell | ||
| // Requires: npm install @grame/faustwasm | ||
| // Usage: node faust-bell.js | ||
|
|
||
| import { fileURLToPath } from 'node:url'; | ||
| import { AudioContext, AudioWorkletNode } from '../index.mjs'; | ||
|
|
||
| // Expose AudioWorkletNode globally so faustwasm can patch it into worklet factories. | ||
| if (typeof globalThis.AudioWorkletNode === 'undefined') { | ||
| globalThis.AudioWorkletNode = AudioWorkletNode; | ||
| } | ||
|
|
||
| const { | ||
| instantiateFaustModuleFromFile, | ||
| LibFaust, | ||
| FaustCompiler, | ||
| FaustMonoDspGenerator, | ||
| } = await import('@grame/faustwasm/dist/esm/index.js'); | ||
|
|
||
| const audioContext = new AudioContext({ latencyHint: 'interactive' }); | ||
|
|
||
| // French bell physical model with the stock UI (strike position, cutoff, sharpness, gain, gate) | ||
| // plus a stereo freeverb tail with mix/feedback/damping controls. | ||
| const dspCode = ` | ||
| declare name "FrenchChurchBell"; | ||
| declare description "French church bell physical model."; | ||
| declare license "MIT"; | ||
| declare copyright "(c)Romain Michon, CCRMA (Stanford University), GRAME"; | ||
|
|
||
| import("stdfaust.lib"); | ||
|
|
||
| mix = hslider("reverb/[0]mix", 0.3, 0, 1, 0.01); | ||
| fb1 = hslider("reverb/[1]feedback1", 0.7, 0, 0.95, 0.01); | ||
| fb2 = hslider("reverb/[2]feedback2", 0.5, 0, 0.95, 0.01); | ||
| damp = hslider("reverb/[3]damp", 0.3, 0, 1, 0.01); | ||
| spread = hslider("reverb/[4]spread[samp]", 30, 0, 80, 1); | ||
|
|
||
| dryWet(mix, fx) = (_,_) <: (_,_),fx : *(1-mix), *(1-mix), *(mix), *(mix) : +, +; | ||
|
|
||
| // The bell UI is mono; duplicate to stereo before the wet/dry split. | ||
| process = pm.frenchBell_ui <: dryWet(mix, re.stereo_freeverb(fb1, fb2, damp, spread)); | ||
| `; | ||
|
|
||
| // Load the Faust toolchain from the wasm distribution. | ||
| const faustModule = await instantiateFaustModuleFromFile( | ||
| fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.js')), | ||
| fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.data')), | ||
| fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.wasm')), | ||
| ); | ||
|
|
||
| const libFaust = new LibFaust(faustModule); | ||
| const compiler = new FaustCompiler(libFaust); | ||
| const generator = new FaustMonoDspGenerator(); | ||
|
|
||
| const compiled = await generator.compile(compiler, 'faust-bell', dspCode, '-I libraries/'); | ||
| if (!compiled) { | ||
| throw new Error('Faust compilation failed'); | ||
| } | ||
|
|
||
| const faustNode = await generator.createNode(audioContext, 'faust-bell', generator.factory, false); | ||
| if (!faustNode) { | ||
| throw new Error('Failed to create Faust bell node'); | ||
| } | ||
|
|
||
| console.log('Faust bell node created.', faustNode); | ||
|
|
||
| faustNode.connect(audioContext.destination); | ||
| faustNode.start(); | ||
|
|
||
| // Gather parameters from the compiled UI and prepare helpers. | ||
| const params = faustNode.getParams?.() ?? []; | ||
| console.log('Available parameters:', params); | ||
|
|
||
| // Find the first parameter path that contains all substrings (case-insensitive). | ||
| const findParam = substrings => { | ||
| const match = params.find(p => { | ||
| const low = p.toLowerCase(); | ||
| return substrings.every(s => low.includes(s)); | ||
| }); | ||
| if (!match) throw new Error(`Parameter containing "${substrings.join(' & ')}" not found`); | ||
| return match; | ||
| }; | ||
|
|
||
| const set = (name, value) => { | ||
| faustNode.setParamValue(name, value); | ||
| console.log(`Set ${name} -> ${value}`); | ||
| }; | ||
|
|
||
| const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); | ||
|
|
||
| // Map important controls. | ||
| const strikePosition = findParam(['strikeposition']); // 0..6 (higher = nearer to rim) | ||
| const strikeCutoff = findParam(['strikecutoff']); // Hz | ||
| const strikeSharpness = findParam(['strikesharpness']); // 0.01..5 | ||
| const gain = findParam(['gain']); // 0..1 | ||
| const gate = findParam(['gate']); // trigger (button) | ||
|
|
||
| const rvMix = findParam(['reverb', 'mix']); | ||
| const rvFb1 = findParam(['reverb', 'feedback1']); | ||
| const rvFb2 = findParam(['reverb', 'feedback2']); | ||
| const rvDamp = findParam(['reverb', 'damp']); | ||
| const rvSpread = findParam(['reverb', 'spread']); | ||
|
|
||
| // Strike helper: short pulse on the gate param. | ||
| const strike = async () => { | ||
| set(gate, 1); | ||
| await sleep(40); | ||
| set(gate, 0); | ||
| }; | ||
|
|
||
| // Initialize to a mellow strike. | ||
| set(strikePosition, 2); // toward the waist | ||
| set(strikeCutoff, 6500); | ||
| set(strikeSharpness, 0.6); | ||
| set(gain, 0.7); | ||
| set(rvMix, 0.35); | ||
| set(rvFb1, 0.7); | ||
| set(rvFb2, 0.5); | ||
| set(rvDamp, 0.35); | ||
| set(rvSpread, 35); | ||
|
|
||
| console.log('Playing bell variations...'); | ||
| await strike(); | ||
|
|
||
| await sleep(3500); | ||
| set(strikePosition, 4); // closer to the rim, brighter partials | ||
| set(strikeCutoff, 9000); | ||
| set(strikeSharpness, 1.2); | ||
| set(gain, 0.9); | ||
| set(rvMix, 0.5); | ||
| set(rvDamp, 0.25); | ||
| set(rvSpread, 45); | ||
| await strike(); | ||
|
|
||
| await sleep(3500); | ||
| set(strikePosition, 1); // nearer the crown, darker tone | ||
| set(strikeCutoff, 4500); | ||
| set(strikeSharpness, 0.4); | ||
| set(gain, 0.6); | ||
| set(rvMix, 0.42); | ||
| set(rvFb1, 0.78); | ||
| set(rvFb2, 0.55); | ||
| set(rvDamp, 0.55); | ||
| set(rvSpread, 55); | ||
| await strike(); | ||
|
|
||
| await sleep(2500); | ||
| console.log('Stopping...'); | ||
|
|
||
| faustNode.stop(); | ||
| await audioContext.close(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Example: Faust DSP with parameters and runtime tweaks using node-web-audio-api. | ||
| // Requires: npm install @grame/faustwasm | ||
| // Usage: node faust-params.js | ||
|
|
||
| import { fileURLToPath } from 'node:url'; | ||
| import { AudioContext, AudioWorkletNode } from '../index.mjs'; | ||
|
|
||
| // Expose AudioWorkletNode globally so faustwasm can patch it into worklet factories. | ||
| if (typeof globalThis.AudioWorkletNode === 'undefined') { | ||
| globalThis.AudioWorkletNode = AudioWorkletNode; | ||
| } | ||
|
|
||
| const { | ||
| instantiateFaustModuleFromFile, | ||
| LibFaust, | ||
| FaustCompiler, | ||
| FaustMonoDspGenerator, | ||
| } = await import('@grame/faustwasm/dist/esm/index.js'); | ||
|
|
||
| const audioContext = new AudioContext({ latencyHint: 'interactive' }); | ||
|
|
||
| // Stereo tone with controllable gain, pitch and filter cutoff. | ||
| const dspCode = ` | ||
| import("stdfaust.lib"); | ||
|
|
||
| gain = hslider("gain[dB]", -6, -24, 6, 0.1) : ba.db2linear; | ||
| freq = hslider("freq[Hz]", 220, 50, 2000, 1); | ||
| cutoff = hslider("cutoff[Hz]", 800, 200, 4000, 1); | ||
|
|
||
| process = vgroup("faust-params", | ||
| os.osc(freq) * gain : fi.lowpass(2, cutoff) | ||
| ) <: _,_; | ||
| `; | ||
|
|
||
| // Load the Faust toolchain from the wasm distribution. | ||
| const faustModule = await instantiateFaustModuleFromFile( | ||
| fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.js')), | ||
| fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.data')), | ||
| fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.wasm')), | ||
| ); | ||
|
|
||
| const libFaust = new LibFaust(faustModule); | ||
| const compiler = new FaustCompiler(libFaust); | ||
| const generator = new FaustMonoDspGenerator(); | ||
|
|
||
| const compiled = await generator.compile(compiler, 'faust-params', dspCode, '-ftz 2'); | ||
| if (!compiled) { | ||
| throw new Error('Faust compilation failed'); | ||
| } | ||
|
|
||
| const faustNode = await generator.createNode(audioContext); | ||
| if (!faustNode) { | ||
| throw new Error('Failed to create Faust node'); | ||
| } | ||
|
|
||
| console.log('Faust parameterized node created.', faustNode); | ||
|
|
||
| faustNode.connect(audioContext.destination); | ||
| faustNode.start(); | ||
|
|
||
| // Helpers to inspect and tweak parameters. The parameter names come from the Faust UI paths. | ||
| const params = faustNode.getParams?.() ?? []; | ||
| console.log('Available parameters:', params); | ||
|
|
||
| const set = (name, value) => { | ||
| faustNode.setParamValue(name, value); | ||
| console.log(`Set ${name} -> ${value}`); | ||
| }; | ||
|
|
||
| const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); | ||
|
|
||
| // Parameter automation demo. | ||
| set('/faust-params/gain', -12); | ||
| set('/faust-params/freq', 220); | ||
| set('/faust-params/cutoff', 800); | ||
| set('/faust-params/resonance', 0.9); | ||
|
|
||
| console.log('Sweeping parameters for 6 seconds...'); | ||
| await sleep(1500); | ||
| set('/faust-params/freq', 440); | ||
| set('/faust-params/cutoff', 1200); | ||
|
|
||
| await sleep(1500); | ||
| set('/faust-params/gain', -3); | ||
| set('/faust-params/resonance', 1.8); | ||
|
|
||
| await sleep(1500); | ||
| set('/faust-params/freq', 660); | ||
| set('/faust-params/cutoff', 1800); | ||
|
|
||
| await sleep(1500); | ||
| console.log('Stopping...'); | ||
|
|
||
| faustNode.stop(); | ||
| await audioContext.close(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
|
|
||
|
|
||
| // Example: run a simple Faust oscillator graph using node-web-audio-api. | ||
| // Requires: npm install @grame/faustwasm | ||
| // Usage: node faust.js | ||
|
|
||
| import { fileURLToPath } from 'node:url'; | ||
| import { AudioContext, AudioWorkletNode } from '../index.mjs'; | ||
|
|
||
| // Expose AudioWorkletNode globally so faustwasm can patch it into worklet factories. | ||
| if (typeof globalThis.AudioWorkletNode === 'undefined') { | ||
| globalThis.AudioWorkletNode = AudioWorkletNode; | ||
| } | ||
|
|
||
| const { | ||
| instantiateFaustModuleFromFile, | ||
| LibFaust, | ||
| FaustCompiler, | ||
| FaustMonoDspGenerator, | ||
| } = await import('@grame/faustwasm/dist/esm/index.js'); | ||
|
|
||
| const latencyHint = process.env.WEB_AUDIO_LATENCY === 'playback' ? 'playback' : 'interactive'; | ||
| const audioContext = new AudioContext({ latencyHint }); | ||
|
|
||
| // Minimal Faust DSP: two sine oscillators mixed to stereo. | ||
| const dspCode = ` | ||
| import("stdfaust.lib"); | ||
| process = os.osc(440)*0.3, os.osc(800)*0.3; | ||
| `; | ||
|
|
||
| // Load the Faust toolchain from the wasm distribution. | ||
| const faustModule = await instantiateFaustModuleFromFile( | ||
| fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.js')), | ||
| fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.data')), | ||
| fileURLToPath(import.meta.resolve('@grame/faustwasm/libfaust-wasm/libfaust-wasm.wasm')), | ||
| ); | ||
|
|
||
| const libFaust = new LibFaust(faustModule); | ||
| const compiler = new FaustCompiler(libFaust); | ||
| const generator = new FaustMonoDspGenerator(); | ||
|
|
||
| const compiled = await generator.compile(compiler, 'faust-osc', dspCode, '-ftz 2'); | ||
| if (!compiled) { | ||
| throw new Error('Faust compilation failed'); | ||
| } | ||
|
|
||
| const faustNode = await generator.createNode(audioContext); | ||
| if (!faustNode) { | ||
| throw new Error('Failed to create Faust node'); | ||
| } | ||
|
|
||
| console.log('Faust node created successfully.', faustNode); | ||
|
|
||
| faustNode.connect(audioContext.destination); | ||
| faustNode.start(); | ||
|
|
||
| console.log('Faust oscillator playing for 4 seconds...'); | ||
| await new Promise(resolve => setTimeout(resolve, 4000)); | ||
|
|
||
| faustNode.stop(); | ||
| await audioContext.close(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great to see this is working, but we should probably not add Faust to the dependencies. Maybe have it in the devDependencies?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, nice catch. Thanks! I will fix this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done is this new PR: #163