From 46105f8dfaa09540c74b373ff22f8192fc464a38 Mon Sep 17 00:00:00 2001 From: James Prevett Date: Tue, 5 Nov 2024 21:10:47 -0600 Subject: [PATCH] Return values are now `DeviceDriver` compatible --- src/dsp.ts | 41 ++++++++++++++++++----------------------- src/framebuffer.ts | 18 +++++++++--------- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/dsp.ts b/src/dsp.ts index 152cf23..dd8a5fe 100644 --- a/src/dsp.ts +++ b/src/dsp.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ -import type { DeviceFile } from '@zenfs/core'; +import type { DeviceDriver, DeviceFile } from '@zenfs/core'; interface DspOptions { audioContext?: AudioContext; @@ -49,37 +49,32 @@ registerProcessor('zenfs-dsp', ZenFSDsp) ) ); -export const dsp = (options: DspOptions = {}) => { - const audioCtx = options.audioContext || new AudioContext(); - const audioBuffer = new ArrayBuffer(audioCtx.sampleRate * 4); +export async function dsp(options: DspOptions = {}): Promise { + const context = options.audioContext || new AudioContext(); + const audioBuffer = new ArrayBuffer(context.sampleRate * 4); - let dsp: AudioWorkletNode; + await context.audioWorklet.addModule(workletUrl); - audioCtx.audioWorklet - .addModule(workletUrl) - .then(() => { - dsp = new AudioWorkletNode(audioCtx, 'zenfs-dsp'); - dsp.connect(audioCtx.destination); - dsp.port?.postMessage(audioBuffer); - }) - .catch(e => {}); + const dsp = new AudioWorkletNode(context, 'zenfs-dsp'); + dsp.connect(context.destination); + dsp.port?.postMessage(audioBuffer); // add a click-handler to resume (due to web security) https://goo.gl/7K7WLu document.addEventListener('click', () => { - if (audioCtx.state !== 'running') { - audioCtx.resume().catch(e => {}); + if (context.state !== 'running') { + context.resume().catch(e => {}); } }); return { name: 'dsp', - isBuffered: false, - read() {}, - write(file: DeviceFile, data: ArrayLike) { - if (data?.length) { - new Uint8Array(audioBuffer).set(data); - dsp.port?.postMessage(new Float32Array(audioBuffer)); - } + read() { + return 0; + }, + write(file: DeviceFile, data: Uint8Array): number { + new Uint8Array(audioBuffer).set(data); + dsp.port?.postMessage(new Float32Array(audioBuffer)); + return data.byteLength; }, }; -}; +} diff --git a/src/framebuffer.ts b/src/framebuffer.ts index bf22fd6..c69397b 100644 --- a/src/framebuffer.ts +++ b/src/framebuffer.ts @@ -1,10 +1,10 @@ -import { Errno, ErrnoError, type DeviceFile } from '@zenfs/core'; +import { Errno, ErrnoError, type DeviceDriver, type DeviceFile } from '@zenfs/core'; interface FramebufferOptions { canvas?: HTMLCanvasElement; } -export function framebuffer({ canvas }: FramebufferOptions = {}) { +export function framebuffer({ canvas }: FramebufferOptions = {}): DeviceDriver { if (!canvas) { canvas = document.createElement('canvas'); document.body.appendChild(canvas); @@ -17,13 +17,13 @@ export function framebuffer({ canvas }: FramebufferOptions = {}) { return { name: 'framebuffer', - isBuffered: false, - read() {}, - write(file: DeviceFile, data: ArrayLike) { - if (data?.length) { - const imageData = new ImageData(new Uint8ClampedArray(data), canvas.width, canvas.height); - ctx.putImageData(imageData, 0, 0); - } + read() { + return 0; + }, + write(file: DeviceFile, data: Uint8Array) { + const imageData = new ImageData(new Uint8ClampedArray(data), canvas.width, canvas.height); + ctx.putImageData(imageData, 0, 0); + return data.byteLength; }, }; }