From 15ca5601575cca8c53a404d2e89a86c0323ffa94 Mon Sep 17 00:00:00 2001 From: James Prevett Date: Wed, 6 Nov 2024 15:44:51 -0600 Subject: [PATCH] Combine worklet code into dsp.ts --- demo/index.html | 7 ------- src/dsp.ts | 39 ++++++++++++++++++++++++++++++++++++++- src/dsp.worklet.ts | 34 ---------------------------------- 3 files changed, 38 insertions(+), 42 deletions(-) delete mode 100644 src/dsp.worklet.ts diff --git a/demo/index.html b/demo/index.html index de3215f..d99a685 100644 --- a/demo/index.html +++ b/demo/index.html @@ -6,13 +6,6 @@

This will demonstrate drawing to a framebuffer & creating audio:

- diff --git a/src/dsp.ts b/src/dsp.ts index 3953c4e..5b85c34 100644 --- a/src/dsp.ts +++ b/src/dsp.ts @@ -1,5 +1,42 @@ import type { DeviceDriver, DeviceFile } from '@zenfs/core'; +/// + +if ('AudioWorkletProcessor' in globalThis) { + class Dsp extends AudioWorkletProcessor { + protected buffer?: Float32Array; + + public constructor() { + super(); + this.port.onmessage = ({ data }: MessageEvent) => { + this.buffer = data; + }; + } + + public process(inputs: Float32Array[][], outputs: Float32Array[][]): boolean { + if (this.buffer) { + const c = currentFrame % sampleRate; + outputs[0][0].set(this.buffer.slice(c, c + 128)); + } + return true; + } + + public static get parameterDescriptors() { + return [ + { + name: 'gain', + defaultValue: 1, + minValue: 0, + maxValue: 1, + automationRate: 'a-rate', + }, + ]; + } + } + + registerProcessor('zenfs:dsp', Dsp); +} + interface DspOptions { audioContext?: AudioContext; } @@ -8,7 +45,7 @@ export async function dsp(options: DspOptions = {}): Promise - -class Dsp extends AudioWorkletProcessor { - protected buffer?: Float32Array; - - public constructor() { - super(); - this.port.onmessage = ({ data }: MessageEvent) => { - this.buffer = data; - }; - } - - public process(inputs: Float32Array[][], outputs: Float32Array[][]): boolean { - if (this.buffer) { - const c = currentFrame % sampleRate; - outputs[0][0].set(this.buffer.slice(c, c + 128)); - } - return true; - } - - public static get parameterDescriptors() { - return [ - { - name: 'gain', - defaultValue: 1, - minValue: 0, - maxValue: 1, - automationRate: 'a-rate', - }, - ]; - } -} - -registerProcessor('zenfs:dsp', Dsp);