Reverting to JS version of Transport
This commit is contained in:
@ -1,22 +1,9 @@
|
|||||||
import { tryEvaluate } from "./Evaluator";
|
import { tryEvaluate } from "./Evaluator";
|
||||||
import { Editor } from './main';
|
const zeroPad = (num, places) => String(num).padStart(places, '0')
|
||||||
const zeroPad = (num: number, places: any) => String(num).padStart(places, '0')
|
|
||||||
|
|
||||||
export class TransportNode extends AudioWorkletNode {
|
export class TransportNode extends AudioWorkletNode {
|
||||||
|
|
||||||
app: Editor
|
constructor(context, options, application) {
|
||||||
$clock: HTMLSpanElement|null
|
|
||||||
hasBeenEvaluated: boolean
|
|
||||||
currentPulsePosition: number
|
|
||||||
nextPulsePosition: number
|
|
||||||
executionLatency: number
|
|
||||||
lastLatencies: number[]
|
|
||||||
indexOfLastLatencies: number
|
|
||||||
startTime: number|undefined
|
|
||||||
elapsedTime: number|undefined
|
|
||||||
prevCurrentTime: number
|
|
||||||
|
|
||||||
constructor(context: BaseAudioContext, options: AudioWorkletNodeOptions, application: Editor) {
|
|
||||||
super(context, "transport", options);
|
super(context, "transport", options);
|
||||||
this.app = application
|
this.app = application
|
||||||
this.port.addEventListener("message", this.handleMessage);
|
this.port.addEventListener("message", this.handleMessage);
|
||||||
@ -30,22 +17,17 @@ export class TransportNode extends AudioWorkletNode {
|
|||||||
this.lastLatencies = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
this.lastLatencies = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
||||||
this.indexOfLastLatencies = 0;
|
this.indexOfLastLatencies = 0;
|
||||||
// setInterval(() => this.ping(), 1000);
|
// setInterval(() => this.ping(), 1000);
|
||||||
this.startTime = undefined;
|
this.startTime = null;
|
||||||
this.elapsedTime = 0;
|
this.elapsedTime = 0;
|
||||||
this.prevCurrentTime = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ping() {
|
|
||||||
// this.port.postMessage({ type: "ping", t: performance.now() })
|
|
||||||
// }
|
|
||||||
|
|
||||||
/** @type {(this: MessagePort, ev: MessageEvent<any>) => any} */
|
/** @type {(this: MessagePort, ev: MessageEvent<any>) => any} */
|
||||||
handleMessage = (message: MessageEvent) => {
|
handleMessage = (message) => {
|
||||||
if (message.data && message.data.type === "bang") {
|
if (message.data && message.data.type === "bang") {
|
||||||
if (this.startTime === undefined) {
|
if (this.startTime === null) {
|
||||||
this.startTime = message.data.currentTime;
|
this.startTime = message.data.currentTime;
|
||||||
}
|
}
|
||||||
this.elapsedTime = message.data.currentTime - this.startTime!;
|
this.elapsedTime = message.data.currentTime - this.startTime;
|
||||||
this.prevCurrentTime = message.data.currentTime;
|
this.prevCurrentTime = message.data.currentTime;
|
||||||
let { futureTimeStamp, timeToNextPulse, nextPulsePosition } = this.convertTimeToNextBarsBeats(this.elapsedTime);
|
let { futureTimeStamp, timeToNextPulse, nextPulsePosition } = this.convertTimeToNextBarsBeats(this.elapsedTime);
|
||||||
|
|
||||||
@ -55,7 +37,7 @@ export class TransportNode extends AudioWorkletNode {
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const now = performance.now();
|
const now = performance.now();
|
||||||
this.app.clock.time_position = futureTimeStamp;
|
this.app.clock.time_position = futureTimeStamp;
|
||||||
this.$clock!.innerHTML = `[${futureTimeStamp.bar}:${futureTimeStamp.beat}:${zeroPad(futureTimeStamp.pulse, '2')}]`;
|
this.$clock.innerHTML = `[${futureTimeStamp.bar}:${futureTimeStamp.beat}:${zeroPad(futureTimeStamp.pulse, '2')}]`;
|
||||||
tryEvaluate(
|
tryEvaluate(
|
||||||
this.app,
|
this.app,
|
||||||
this.app.global_buffer,
|
this.app.global_buffer,
|
||||||
@ -81,14 +63,14 @@ export class TransportNode extends AudioWorkletNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
this.startTime = undefined;
|
this.startTime = null;
|
||||||
this.elapsedTime = undefined;
|
this.elapsedTime = null;
|
||||||
this.app.clock.tick = 0;
|
this.app.clock.tick = 0;
|
||||||
this.$clock!.innerHTML = `[${1} | ${1} | ${zeroPad(1, '2')}]`;
|
this.$clock.innerHTML = `[${1} | ${1} | ${zeroPad(1, '2')}]`;
|
||||||
this.port.postMessage("stop");
|
this.port.postMessage("stop");
|
||||||
}
|
}
|
||||||
|
|
||||||
convertTimeToBarsBeats(currentTime: number) {
|
convertTimeToBarsBeats(currentTime) {
|
||||||
const beatDuration = 60 / this.app.clock.bpm;
|
const beatDuration = 60 / this.app.clock.bpm;
|
||||||
const beatNumber = (currentTime) / beatDuration;
|
const beatNumber = (currentTime) / beatDuration;
|
||||||
|
|
||||||
@ -101,7 +83,7 @@ export class TransportNode extends AudioWorkletNode {
|
|||||||
return { bar: barNumber, beat: beatWithinBar, ppqn: ppqnPosition };
|
return { bar: barNumber, beat: beatWithinBar, ppqn: ppqnPosition };
|
||||||
}
|
}
|
||||||
|
|
||||||
convertTimeToNextBarsBeats(currentTime: number) {
|
convertTimeToNextBarsBeats(currentTime) {
|
||||||
|
|
||||||
const beatDuration = 60 / this.app.clock.bpm;
|
const beatDuration = 60 / this.app.clock.bpm;
|
||||||
const beatNumber = (currentTime) / beatDuration;
|
const beatNumber = (currentTime) / beatDuration;
|
||||||
@ -1,17 +1,13 @@
|
|||||||
class TransportProcessor extends AudioWorkletProcessor {
|
class TransportProcessor extends AudioWorkletProcessor {
|
||||||
|
|
||||||
started: boolean;
|
constructor(options) {
|
||||||
options: object;
|
super(options);
|
||||||
|
|
||||||
constructor(options: AudioWorkletNodeOptions) {
|
|
||||||
super();
|
|
||||||
this.port.addEventListener("message", this.handleMessage);
|
this.port.addEventListener("message", this.handleMessage);
|
||||||
this.port.start();
|
this.port.start();
|
||||||
this.started = false;
|
this.stated = false;
|
||||||
this.options = options;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handleMessage = (message: MessageEvent) => {
|
handleMessage = (message) => {
|
||||||
if (message.data && message.data.type === "ping") {
|
if (message.data && message.data.type === "ping") {
|
||||||
this.port.postMessage(message.data);
|
this.port.postMessage(message.data);
|
||||||
} else if (message.data === "start") {
|
} else if (message.data === "start") {
|
||||||
@ -20,11 +16,11 @@ class TransportProcessor extends AudioWorkletProcessor {
|
|||||||
this.started = false;
|
this.started = false;
|
||||||
} else if (message.data === "stop") {
|
} else if (message.data === "stop") {
|
||||||
this.started = false;
|
this.started = false;
|
||||||
|
this.currentTime = 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// @ts-expect-error
|
process(inputs, outputs, parameters) {
|
||||||
process(inputs: Float32Array[][], outputs: Float32Array[][], parameters: Record<string, Float32Array>) {
|
|
||||||
if (this.started) this.port.postMessage({ type: "bang", currentTime });
|
if (this.started) this.port.postMessage({ type: "bang", currentTime });
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user