Home

Awesome

SoundTouchJS Audio Worklet

The SoundTouchNode is an extension of the Web Audio API AudioWorkletNode. It works in conjunction with the SoundTouchWorklet, the AudioWorkletProcessor that processes the audio from the buffer. You must have the worklet registered in your code prior to utilizing the node. The node is represented as a true AudioNode.

Browsers and Usage Considerations

AudioWorklets are currently only available in Chrome, MS Edge, and Electron (with hacks). We use RollupJS and Babel to compile all of our dependencies.

The SoundTouchWorklet runs in it's own context, off the main thread, when using the native WebAudio API. As such, the Babel runtime is bundled with the other worklet dependencies. All of the necessary SoundTouch bits are precompiled into the worklet, and the SoundTouchNode controls their properties. Adding worklets/webworkers to your application can be tricky. Read the documentation below for more information.

Setting Up the AudioWorkletProcessor (SoundTouchWorklet)

The worklet must first be registered with the audio context, prior to creating an instance of your SoundTouchNode, and is compiled and included in this package (/dist/soundtouch-worklet.js). According to new browser security bits, you must have some form of user interaction prior to creating your AudioContext. The following is a setupContext() method called from the 'loadSource()' method

const setupContext = function () {
  audioCtx = new AudioContext();
  return audioCtx.audioWorklet
    .addModule('./js/soundtouch-worklet.js')
    .catch((err) => console.log(err));
};

The AudioContext audioWorklet.addModule() method returns a Promise. The path passed to this method is the path to the SoundTouchWorklet, available from the build @soundtouchjs/audio-worklet/dist/soundtouch-worklet.js. Pathing to this file is important, and your server must include a 'Content-Type' header of text/javascript or application/javascript for the file to run properly.

[NOTE]: If you are using a bundler (webpack, rollup, etc) to bundle your app, you may require a special 'loader' for worklets/ web workers.

Setting Up the AudioWorkletNode (SoundTouchNode)

Once you have setup your worklet, and retrieved your raw (undecoded) file for processing, you now need to create an instance of the SoundTouchNode. We provide a factory method for this, so that you can use polyfilled/ponyfilled WebAudio API classes if necessary.

//top of the file
import createSoundTouchNode from '@soundtouchjs/audio-worklet';
//... and later

// called from our `loadSource()` method, after we've retrieved the
// raw audio data
const setupSoundtouch = function () {
  if (soundtouch) {
    soundtouch.off();
  }
  soundtouch = createSoundTouchNode(audioCtx, AudioWorkletNode, buffer);
  soundtouch.on('initialized', onInitialized);
};

Chaining AudioNodes

You must connect the SoundTouchNode with the AudioBufferSourceNode, so that audio will move from the AudioBuffer through the AudioWorkletNode and, by extension, through the AudioWorkletProcessor. You do this by calling the connectToBuffer() method of the SoundTouchNode, which returns the AudioBufferSourceNode object. This means that additional audio processing happens by chaining additional AudioNodes, passing audio from the SoundTouchNode on to the next node in the chain, until you finally connect the AudioContext AudioDestintionNode. You can see this in our example by looking at it's play() method.

const play = function () {
  if (is_ready) {
    bufferNode = soundtouch.connectToBuffer();
    gainNode = audioCtx.createGain();
    soundtouch.connect(gainNode);
    gainNode.connect(audioCtx.destination);
    // ...
  }
};

This also means that, when pausing or stopping audio, you must disconnect all AudioNodes as well. You can see this in our example by looking at it's pause() method.

const pause = function (stop = false, override = false) {
  if (bufferNode) {
    gainNode.disconnect();
    soundtouch.disconnect();
    soundtouch.disconnectFromBuffer();
    // ...
  }
};

Events

The SoundTouchNode provides several 'events' that your interface may require:

Player controls

Three methods are provided for controlling that 'playHead' of your audio

[Note]: 'stop()' does not fire the 'end' event, and will require you to setup new AudioContext, worklet, and SoundTouchNode instances to start audio again.

Controllable Properties

There are several properties you can set to control the SoundTouchWorklet, effecting the playback.

Read Only Properties

You also have several other read-only properties available

(There are other read-only properties, but they are derived values, and shouldn't be accessed externally when the Node is 'playing' as latency may effect the values given)

Memory Considerations

Due to the Stretch and Rate Transposition features of SoundTouch, as well as the nature of AudioWorklets, it is currently necessary to maintain copies of the AudioBuffer in both the browser's main process thread as well as in the AudioWorkletGlobalScope (where the processor processes). This may change in the future, once the SharedArrayBuffer gains complete adoption in other browsers, but for now it's necessary, and you should be aware of the overhead it creates.

Credits

Thank You to Janick Delot for sponsoring this feature for SoundTouchJS

Thanks to Christoph Guttandin, of standardized-audio-context fame, for the idea of providing the factory method to enable polyfill/ponyfill usage.

1