Skip to content

Commit

Permalink
subverting pedantic tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed Nov 6, 2024
1 parent 66b394b commit 9f39dda
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 70 deletions.
18 changes: 9 additions & 9 deletions example/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
81 changes: 45 additions & 36 deletions src/dsp.ts
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -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(() => {

Check failure on line 54 in src/dsp.ts

View workflow job for this annotation

GitHub Actions / CI

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
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();

Check failure on line 63 in src/dsp.ts

View workflow job for this annotation

GitHub Actions / CI

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}
});

return {
name: 'dsp',
isBuffered: false,
read () {},
write (writeOptions:any = {}, data:ArrayLike<number>) {
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<number>) {

Check warning on line 71 in src/dsp.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected any. Specify a different type
const {

Check warning on line 72 in src/dsp.ts

View workflow job for this annotation

GitHub Actions / CI

Unsafe assignment of an `any` value
device: {
driver: { name },

Check warning on line 74 in src/dsp.ts

View workflow job for this annotation

GitHub Actions / CI

'name' is assigned a value but never used
ino,

Check warning on line 75 in src/dsp.ts

View workflow job for this annotation

GitHub Actions / CI

'ino' is assigned a value but never used
},
fs,

Check warning on line 77 in src/dsp.ts

View workflow job for this annotation

GitHub Actions / CI

'fs' is assigned a value but never used
path,

Check warning on line 78 in src/dsp.ts

View workflow job for this annotation

GitHub Actions / CI

'path' is assigned a value but never used
position,

Check warning on line 79 in src/dsp.ts

View workflow job for this annotation

GitHub Actions / CI

'position' is assigned a value but never used
} = writeOptions;
if (data?.length) {
new Uint8Array(audioBuffer).set(data);
dsp.port?.postMessage(new Float32Array(audioBuffer));
}
},
};
};



48 changes: 28 additions & 20 deletions src/framebuffer.ts
Original file line number Diff line number Diff line change
@@ -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<number>) {
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<number>) {

Check warning on line 15 in src/framebuffer.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected any. Specify a different type
const {

Check warning on line 16 in src/framebuffer.ts

View workflow job for this annotation

GitHub Actions / CI

Unsafe assignment of an `any` value
device: {
driver: { name },

Check warning on line 18 in src/framebuffer.ts

View workflow job for this annotation

GitHub Actions / CI

'name' is assigned a value but never used
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);
}
},
};
}
4 changes: 2 additions & 2 deletions src/input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface InputOptions {
canvas?: HTMLElement
canvas?: HTMLElement;
}

export function input(options:InputOptions = {}){};
export function input(options: InputOptions = {}) {}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"declaration": true,
"strict": false,
"allowImportingTsExtensions": true,
"noEmit": true
"noEmit": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
Expand Down
4 changes: 2 additions & 2 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down

0 comments on commit 9f39dda

Please sign in to comment.