UNPKG

tone

Version:

A Web Audio framework for making interactive music in the browser.

74 lines (67 loc) 2.1 kB
import "./ToneAudioWorkletProcessor.worklet.js"; import { addToWorklet } from "./WorkletGlobalScope.js"; export const singleIOProcess = /* javascript */ ` /** * Abstract class for a single input/output processor. * has a 'generate' function which processes one sample at a time */ class SingleIOProcessor extends ToneAudioWorkletProcessor { constructor(options) { super(Object.assign(options, { numberOfInputs: 1, numberOfOutputs: 1 })); /** * Holds the name of the parameter and a single value of that * parameter at the current sample * @type { [name: string]: number } */ this.params = {} } /** * Generate an output sample from the input sample and parameters * @abstract * @param input number * @param channel number * @param parameters { [name: string]: number } * @returns number */ generate(){} /** * Update the private params object with the * values of the parameters at the given index * @param parameters { [name: string]: Float32Array }, * @param index number */ updateParams(parameters, index) { for (const paramName in parameters) { const param = parameters[paramName]; if (param.length > 1) { this.params[paramName] = parameters[paramName][index]; } else { this.params[paramName] = parameters[paramName][0]; } } } /** * Process a single frame of the audio * @param inputs Float32Array[][] * @param outputs Float32Array[][] */ process(inputs, outputs, parameters) { const input = inputs[0]; const output = outputs[0]; // get the parameter values const channelCount = Math.max(input && input.length || 0, output.length); for (let sample = 0; sample < this.blockSize; sample++) { this.updateParams(parameters, sample); for (let channel = 0; channel < channelCount; channel++) { const inputSample = input && input.length ? input[channel][sample] : 0; output[channel][sample] = this.generate(inputSample, channel, this.params); } } return !this.disposed; } }; `; addToWorklet(singleIOProcess);