Skip to content

Commit

Permalink
Return values are now DeviceDriver compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Nov 6, 2024
1 parent b0b6ba9 commit 46105f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
41 changes: 18 additions & 23 deletions src/dsp.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<DeviceDriver> {
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<number>) {
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;
},
};
};
}
18 changes: 9 additions & 9 deletions src/framebuffer.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -17,13 +17,13 @@ export function framebuffer({ canvas }: FramebufferOptions = {}) {

return {
name: 'framebuffer',
isBuffered: false,
read() {},
write(file: DeviceFile, data: ArrayLike<number>) {
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;
},
};
}

0 comments on commit 46105f8

Please sign in to comment.