Git Product home page Git Product logo

faust-loader's Introduction

faust-loader

Webpack loader for the Faust language. Import .dsp files, and get back an AudioWorklet or ScriptProcessor node.

This loader is confirmed working with Faust v2.30.5, but may break on lower versions. Help is wanted on getting the WASM version of libfaust working in NodeJS.

Installation

  1. Install the Faust compiler version >=2.30.5. You can download it or build it from source. It's also available from the AUR.

  2. Install faust-loader and standardized-audio-context:

npm install --save faust-loader standardized-audio-context
# OR
yarn add faust-loader standardized-audio-context
  1. Add faust-loader to your webpack config:
module: {
    rules: [
      // ...
      {
        test: /\.dsp$/,
        use: [
          {
            loader: "faust-loader",
            options: { inline: true },
          },
        ],
      },
    ],
  },

Usage

import { AudioContext } from "standardized-audio-context";
import createCompressor from "./Compressor.dsp";

const context = new AudioContext();
createCompressor(context).then((node) => {
  node.connect(context.destination);
  node.getParams();
  node.setParam("attack", 0.3);
});

faust-loader makes use of standardized-audio-context for instantiating AudioWorkletNodes. This allows it to automatically fallback to a ScriptProcessorNode on Safari and other browsers that don't support AudioWorklets, as well as interoperate seamlessly with Tone.js, a popular web audio framework.

Because of this, you have to use an AudioContext from standardized-audio-context when creating Faust nodes. If you want to use this loader with a vanilla AudioContext, please submit an issue or PR!

Loader Options

inline = false

Switch between inline and split file modes.

Inline mode embeds the AudioWorkletProcessor and WASM code in the Javascript bundle as base64 data URLs . This allows for single-file builds, and supports code splitting via dynamic imports (await import()).

Split file mode (the default) emits separate files for the AudioWorkletProcessor and WASM in a specified outputPath, fetching them over the network from publicPath.

Inline mode is recommended, since it's more flexible and easier to configure, and will likely become the default in a future release. However, emitting and fetching separate files may make sense if you are already serving webpack static assets and want to keep your main bundle size as small as possible.

outputPath = ""

Where the generated files should be placed relative to the output directory in split file mode. Ignored when inline: true.

publicPath = "/"

What base path the generated files will be served from in split file mode. Ignored when inline: true.

Examples

With Typescript

Typescript definitions are available for the imported modules. To automatically get the correct types when you import a .dsp file, add a file ending in .d.ts to your project containing the following:

// faust-modules.d.ts

declare module "*.dsp" {
  import { ProcessorLoader } from "faust-loader";
  const loader: ProcessorLoader;
  export = loader;
}

You may need to update your tsconfig.json to ensure declaration files in your project are included by the Typescript compiler.

With Tone.js

import { getContext, connect } from "tone";
import createSynth from "./Synthesizer.dsp";

async function connectSynth() {
  const context = getContext();

  // `context` is the Tone Context, we need to get the raw standardized-audio-context.
  const node = await createSynth(context.rawContext);

  // We also need to use the global `connect` function since the node isn't a Tone AudioNode.
  connect(node, context.destination);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.