Skip to content

Commit

Permalink
basic typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed Nov 6, 2024
1 parent fc169a4 commit ea541c4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion example/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ await configure({ addDevices: true });

// mount framebuffer & dsp
fs.mounts.get('/dev').createDevice('/fb0', framebuffer({ canvas }));
fs.mounts.get('/dev').createDevice('/dsp', dsp({audioContext}));
fs.mounts.get('/dev').createDevice('/dsp', dsp({ audioContext }));

// example: write static to framebuffer
const screen = new Uint8Array(canvas.width * canvas.height * 4);
Expand Down
7 changes: 4 additions & 3 deletions src/dsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ registerProcessor('zenfs-dsp', ZenFSDsp)
`], { type: 'application/javascript' }))

export const dsp = (options = {}) => {
export const dsp = (options:any = {}) => {
const audioCtx = options.audioContext || new AudioContext()
const audioBuffer = new ArrayBuffer(audioCtx.sampleRate * 4)

Expand All @@ -60,8 +60,9 @@ export const dsp = (options = {}) => {
name: 'dsp',
isBuffered: false,
read () {},
write({ device: { driver: { name }, ino }, fs, path, position }, data) {
if (data?.byteLength){
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))
}
Expand Down
15 changes: 10 additions & 5 deletions src/framebuffer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
export function framebuffer({ canvas }) {
const ctx = canvas.getContext('2d')
export function framebuffer(options:any = {}) {
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 ({ device: { driver: { name }, ino }, fs, path, position }, data) {
if (data?.byteLength){
const imageData = new ImageData(new Uint8ClampedArray(data), canvas.width, canvas.height)
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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/input.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const input = ({ canvas }) => {};
export const input = (options:any = {}) => {};
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"module": "NodeNext",
"target": "ES2020",
"outDir": "dist",
"lib": ["ESNext", "ESNext.Disposable"],
"lib": ["ESNext", "ESNext.Disposable", "dom"],
"moduleResolution": "NodeNext",
"declaration": true,
"strict": true
"strict": true,
"allowImportingTsExtensions": true,
"noEmit": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
Expand Down

0 comments on commit ea541c4

Please sign in to comment.