grame-cncm / faust

Functional programming language for signal processing and sound synthesis
http://faust.grame.fr
Other
2.58k stars 322 forks source link

Request: Provide ability to run faust compiler WASM version in Nodejs #598

Open Smona opened 3 years ago

Smona commented 3 years ago

Hello all! I'm very excited about the possibilities Faust creates when it comes to running custom DSP on the web for non-c++ programmers.

I'm trying to build a webpack loader for Faust DSP files. I've gotten it most of the way there, but unfortunately the version of libfaust provided by many package managers has some bugs in the AudioWorklet processor template.

I've been trying to figure out how to use libfaust-wasm in Node.js so I can package the compiler with the loader and make sure the version is correct. Unfortunately, when I try to run libfaust-wasm.js in a node environment, I get an error about preloaded memory only being supported in the browser and web workers. I got a bit stuck at that point because libfaust-wasm.js is quite opaque, and admittedly c++ and its compilation aren't my strong suit.

I was hoping that someone involved with the project would be interested in helping to create a version of libfaust-wasm that can be run in node. Ideally, it would support something like the following:

import { FaustCompiler } from "@grame/libfaust";
import { promises as fs } from "fs";

async function compileDSP(dspCode: string) {
  await FaustCompiler.initialize();
  const result = await FaustCompiler.compileAudioWorklet(dspCode, options);
  await fs.writeFile("MyDSP.wasm", result.wasm);
  await fs.writeFile("MyDSP-processor.js", result.processsor);
}

I really think supporting compilation from Nodejs would be a boon to Faust adoption, as it would enable integrating Faust compilation into all the various build tools used by application developers. I'd also be happy to provide help with any necessary planning or implementation! The part I need help with is just compiling and instantiating a version of libfaust that doesn't use preloaded memory.

sletz commented 3 years ago

Which libfaust-wasm.js version are you using? Note that we have completely rewritten the Faust/WeAudio layer in the wasm2 branch, see https://github.com/grame-cncm/faust/tree/wasm2/javascript. This new code (still in test) is already packaged as an npm package. Thus it will be better to start from there.