diff --git a/example/demo.ts b/example/demo.ts index 29e79b1..6112fcc 100644 --- a/example/demo.ts +++ b/example/demo.ts @@ -27,13 +27,13 @@ configure({ addDevices: true }).then(() => { makestaticFb(); // example: write static to audio - const audioBuffer = new ArrayBuffer(audioContext.sampleRate * 4) - const audioBytes = new Uint8Array(audioBuffer) - const audioFloats = new Float32Array(audioBuffer) + const audioBuffer = new ArrayBuffer(audioContext.sampleRate * 4); + const audioBytes = new Uint8Array(audioBuffer); + const audioFloats = new Float32Array(audioBuffer); setInterval(() => { - for (let i in audioFloats){ - audioFloats[i] = Math.random() * 2 - 1 - } - fs.promises.writeFile('/dev/dsp', audioBytes) - }, 1000) -}) + for (let i in audioFloats) { + audioFloats[i] = Math.random() * 2 - 1; + } + fs.promises.writeFile('/dev/dsp', audioBytes); + }, 1000); +}); diff --git a/src/dsp.ts b/src/dsp.ts index c8ac171..e0ba6bc 100644 --- a/src/dsp.ts +++ b/src/dsp.ts @@ -1,9 +1,12 @@ interface DspOptions { - audioContext?: AudioContext + audioContext?: AudioContext; } // I inline worker, so no seperate file is needed. -const workletUrl = URL.createObjectURL(new Blob([` +const workletUrl = URL.createObjectURL( + new Blob( + [ + ` /* global AudioWorkletProcessor, registerProcessor, currentFrame, currentTime, sampleRate */ @@ -36,43 +39,49 @@ class ZenFSDsp extends AudioWorkletProcessor { registerProcessor('zenfs-dsp', ZenFSDsp) -`], { type: 'application/javascript' })) +`, + ], + { type: 'application/javascript' } + ) +); -export const dsp = (options:DspOptions = {}) => { - const audioCtx = options.audioContext || new AudioContext() - const audioBuffer = new ArrayBuffer(audioCtx.sampleRate * 4) +export const dsp = (options: DspOptions = {}) => { + const audioCtx = options.audioContext || new AudioContext(); + const audioBuffer = new ArrayBuffer(audioCtx.sampleRate * 4); - let dsp:AudioWorkletNode + let dsp: AudioWorkletNode; - audioCtx.audioWorklet.addModule(workletUrl).then(() => { - dsp = new AudioWorkletNode( - audioCtx, - 'zenfs-dsp' - ) - dsp.connect(audioCtx.destination) - dsp.port?.postMessage(audioBuffer) - }) + audioCtx.audioWorklet.addModule(workletUrl).then(() => { + dsp = new AudioWorkletNode(audioCtx, 'zenfs-dsp'); + dsp.connect(audioCtx.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() - } - }) + // add a click-handler to resume (due to web security) https://goo.gl/7K7WLu + document.addEventListener('click', () => { + if (audioCtx.state !== 'running') { + audioCtx.resume(); + } + }); - return { - name: 'dsp', - isBuffered: false, - read () {}, - write (writeOptions:any = {}, data:ArrayLike) { - const { device: { driver: { name }, ino }, fs, path, position } = writeOptions - if (data?.length){ - new Uint8Array(audioBuffer).set(data) - dsp.port?.postMessage(new Float32Array(audioBuffer)) - } - } - } + return { + name: 'dsp', + isBuffered: false, + read() {}, + write(writeOptions: any = {}, data: ArrayLike) { + const { + device: { + driver: { name }, + ino, + }, + fs, + path, + position, + } = writeOptions; + if (data?.length) { + new Uint8Array(audioBuffer).set(data); + dsp.port?.postMessage(new Float32Array(audioBuffer)); + } + }, + }; }; - - - diff --git a/src/framebuffer.ts b/src/framebuffer.ts index 87112ec..5fd7b00 100644 --- a/src/framebuffer.ts +++ b/src/framebuffer.ts @@ -1,23 +1,31 @@ interface FramebufferOptions { - canvas?: HTMLCanvasElement + canvas?: HTMLCanvasElement; } -export function framebuffer(options:FramebufferOptions = {}) { - if (!options.canvas) { - options.canvas = document.createElement('canvas') - document.body.appendChild(options.canvas) - } - const ctx = options.canvas.getContext('2d') - return { - name: 'framebuffer', - isBuffered: false, - read () {}, - write (writeOptions:any = {}, data:ArrayLike) { - const { device: { driver: { name }, ino }, fs, path, position } = writeOptions - if (data?.length){ - const imageData = new ImageData(new Uint8ClampedArray(data), options.canvas.width, options.canvas.height) - ctx.putImageData(imageData, 0, 0) - } - } - } -}; +export function framebuffer(options: FramebufferOptions = {}) { + if (!options.canvas) { + options.canvas = document.createElement('canvas'); + document.body.appendChild(options.canvas); + } + const ctx = options.canvas.getContext('2d'); + return { + name: 'framebuffer', + isBuffered: false, + read() {}, + write(writeOptions: any = {}, data: ArrayLike) { + const { + device: { + driver: { name }, + ino, + }, + fs, + path, + position, + } = writeOptions; + if (data?.length) { + const imageData = new ImageData(new Uint8ClampedArray(data), options.canvas.width, options.canvas.height); + ctx.putImageData(imageData, 0, 0); + } + }, + }; +} diff --git a/src/input.ts b/src/input.ts index 137855b..de24483 100644 --- a/src/input.ts +++ b/src/input.ts @@ -1,5 +1,5 @@ interface InputOptions { - canvas?: HTMLElement + canvas?: HTMLElement; } -export function input(options:InputOptions = {}){}; +export function input(options: InputOptions = {}) {} diff --git a/tsconfig.json b/tsconfig.json index a741baf..005886c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,7 @@ "declaration": true, "strict": false, "allowImportingTsExtensions": true, - "noEmit": true + "noEmit": true }, "include": ["src/**/*.ts"], "exclude": ["node_modules"] diff --git a/vite.config.js b/vite.config.js index d0c1152..0c18ac1 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,11 +1,11 @@ -import { defineConfig } from 'vite' +import { defineConfig } from 'vite'; export default defineConfig({ // base: '.', root: './example', build: { outDir: '../docs', - emptyOutDir: true + emptyOutDir: true, }, resolve: { alias: {