Skip to content

Commit

Permalink
Combine worklet code into dsp.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Nov 6, 2024
1 parent efd0698 commit 15ca560
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
7 changes: 0 additions & 7 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
<body>
<p>This will demonstrate drawing to a framebuffer & creating audio:</p>
<canvas id="fb"></canvas>
<script type="importmap">
{
"imports": {
"@zenfs/": "https://esm.sh/@zenfs/"
}
}
</script>
<script type="module" src="./demo.js"></script>
</body>
</html>
39 changes: 38 additions & 1 deletion src/dsp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
import type { DeviceDriver, DeviceFile } from '@zenfs/core';

/// <reference types="./audioworklet.d.ts" />

if ('AudioWorkletProcessor' in globalThis) {
class Dsp extends AudioWorkletProcessor {
protected buffer?: Float32Array;

public constructor() {
super();
this.port.onmessage = ({ data }: MessageEvent<Float32Array>) => {
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;
}
Expand All @@ -8,7 +45,7 @@ export async function dsp(options: DspOptions = {}): Promise<DeviceDriver<AudioW
const context = options.audioContext || new AudioContext();
const audioBuffer = new ArrayBuffer(context.sampleRate * 4);

await context.audioWorklet.addModule(new URL('./dsp.worklet.js', import.meta.url).href);
await context.audioWorklet.addModule(import.meta.url);

const dsp = new AudioWorkletNode(context, 'zenfs:dsp');
dsp.connect(context.destination);
Expand Down
34 changes: 0 additions & 34 deletions src/dsp.worklet.ts

This file was deleted.

0 comments on commit 15ca560

Please sign in to comment.