cleanup audio-worklet a bit

This commit is contained in:
SArpnt 2024-03-14 16:46:13 -04:00
parent ad94dd9273
commit 2d9f2af651
Signed by: SArpnt
SSH key fingerprint: SHA256:iDMeic8KkqqEsN4wODlgsk1d/oW1ojZ/cu/MEWyfLBw

View file

@ -1,4 +1,3 @@
// note: imports don't work: https://bugzilla.mozilla.org/show_bug.cgi?id=1636121
import type { DrawSample } from "./oscillioscope.ts";
function jsOptimize(script: string, isExpression = true) {
@ -49,7 +48,7 @@ function safeStringify(value: any, quoteString?: boolean) {
}
}
function getErrorMessage(err, time) {
function getErrorMessage(err: any, time: number) {
if (
err instanceof Error &&
typeof err.lineNumber === "number" &&
@ -73,11 +72,13 @@ function getErrorMessage(err, time) {
}
}
// delete most enumerable variables, and all single letter variables (not foolproof but works well enough)
function deleteGlobals() {
// delete all single letter variables
// a bytebeat might make some variables not enumerable, so this catches the important ones
for (let i = 0; i < 26; i++) {
delete globalThis[String.fromCharCode(65 + i)], globalThis[String.fromCharCode(97 + i)];
}
// delete all enumerable variables
for (let v in globalThis) {
if (
![
@ -116,29 +117,28 @@ function freezeExistingGlobals() {
}
class BytebeatProcessor extends AudioWorkletProcessor {
audioSample = 0; // TODO: is this needed? might be better to use currentTime
lastFlooredTime = -1;
byteSample = 0;
sampleRatio = NaN;
lastByteValue = [null, null];
lastValue = [0, 0];
lastFuncValue = [null, null];
isPlaying = false;
func = null;
calcByteValue = null;
songData = { sampleRate: null, mode: null };
sampleRateDivisor = 1;
playSpeed = 1;
postedErrorPriority = null;
constructor() {
super({ numberOfInputs: 0 });
this.audioSample = 0; // TODO: is this needed? might be better to use currentTime
this.lastFlooredTime = -1;
this.byteSample = 0;
this.sampleRatio = NaN;
this.lastByteValue = [null, null];
this.lastValue = [0, 0];
this.lastFuncValue = [null, null];
this.isPlaying = false;
this.func = null;
this.calcByteValue = null;
this.songData = { sampleRate: null, mode: null };
this.sampleRateDivisor = 1;
this.playSpeed = 1;
this.postedErrorPriority = null;
Object.seal(this);
deleteGlobals();
@ -146,8 +146,8 @@ class BytebeatProcessor extends AudioWorkletProcessor {
this.updateSampleRatio();
this.port.addEventListener("message", e => this.handleMessage(e));
this.port.start();
super.port.addEventListener("message", e => this.handleMessage(e));
super.port.start();
}
handleMessage(e) {
@ -200,9 +200,9 @@ class BytebeatProcessor extends AudioWorkletProcessor {
this.lastByteValue[c] = NaN;
};
}
setByteSample(value, clear = false) {
setByteSample(value: number, clear = false) {
this.byteSample = value;
this.port.postMessage({ [clear ? "clearCanvas" : "clearDrawBuffer"]: true });
super.port.postMessage({ [clear ? "clearCanvas" : "clearDrawBuffer"]: true });
this.audioSample = 0;
this.lastFlooredTime = -1;
for (let c = 0; c < 2; c++) {
@ -211,11 +211,11 @@ class BytebeatProcessor extends AudioWorkletProcessor {
this.lastFuncValue[c] = null;
}
}
refreshCode(code) {
refreshCode(code: string) {
// code is already trimmed
// create shortened functions
const params = Object.getOwnPropertyNames(Math);
const values = params.map(k => Math[k]);
const params: string[] = Object.getOwnPropertyNames(Math);
const values: any[] = params.map(k => Math[k as keyof Math]);
params.push("int");
values.push(Math.floor);
params.push("window");
@ -240,7 +240,7 @@ class BytebeatProcessor extends AudioWorkletProcessor {
}
errType = "runtime";
if (this.songData.mode === "Funcbeat") {
this.func = this.func();
this.func = this.func(); // TODO: dangerous?
}
this.func(0); // TODO: samplerate is undefined, not good for funcbeat
// this also has side effects
@ -252,7 +252,7 @@ class BytebeatProcessor extends AudioWorkletProcessor {
} else {
this.postedErrorPriority = 1;
}
this.port.postMessage({
super.port.postMessage({
updateUrl: true,
errorMessage: {
type: errType,
@ -263,7 +263,7 @@ class BytebeatProcessor extends AudioWorkletProcessor {
return;
}
this.postedErrorPriority = null;
this.port.postMessage({ updateUrl: true, errorMessage: null });
super.port.postMessage({ updateUrl: true, errorMessage: null });
}
updateSampleRatio() {
let flooredTimeOffset = isNaN(this.sampleRatio)
@ -304,7 +304,7 @@ class BytebeatProcessor extends AudioWorkletProcessor {
} catch (err) {
if (this.postedErrorPriority === null) {
this.postedErrorPriority = 0;
this.port.postMessage({
super.port.postMessage({
errorMessage: { type: "runtime", err: getErrorMessage(err, roundSample) },
});
}
@ -361,7 +361,7 @@ class BytebeatProcessor extends AudioWorkletProcessor {
if (drawBuffer.length) {
message.drawBuffer = drawBuffer;
}
this.port.postMessage(message);
super.port.postMessage(message);
this.byteSample = byteSample;
return true;