Add logical time to transportprocessor

This commit is contained in:
2023-08-24 23:15:36 +03:00
parent dd48d40745
commit 4a03a72645
3 changed files with 37 additions and 18 deletions

View File

@ -21,20 +21,14 @@ export class TransportNode extends AudioWorkletNode {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
this.indexOfLastLatencies = 0;
this.logicalTime = 0;
// setInterval(() => this.ping(), 1000);
this.startTime = null;
this.elapsedTime = 0;
}
/** @type {(this: MessagePort, ev: MessageEvent<any>) => any} */
handleMessage = (message) => {
if (message.data && message.data.type === "bang") {
if (this.startTime === null) {
this.startTime = message.data.currentTime;
}
this.elapsedTime = message.data.currentTime - this.startTime;
this.prevCurrentTime = message.data.currentTime;
let { futureTimeStamp, timeToNextPulse, nextPulsePosition } = this.convertTimeToNextBarsBeats(this.elapsedTime);
let { futureTimeStamp, timeToNextPulse, nextPulsePosition } = this.convertTimeToNextBarsBeats(message.data.logicalTime);
// Evaluate the global buffer only once per ppqn value
if (this.nextPulsePosition !== nextPulsePosition) {
@ -65,8 +59,6 @@ export class TransportNode extends AudioWorkletNode {
}
stop() {
this.startTime = null;
this.elapsedTime = null;
this.app.clock.tick = 0;
// this.$clock.innerHTML = `[${1} | ${1} | ${zeroPad(1, '2')}]`;
this.port.postMessage("stop");

View File

@ -4,7 +4,11 @@ class TransportProcessor extends AudioWorkletProcessor {
super(options);
this.port.addEventListener("message", this.handleMessage);
this.port.start();
this.stated = false;
this.started = false;
this.totalPausedTime = 0;
this.lastPausedTime = 0;
this.startedAgainTime = 0;
this.wasStopped = false;
}
handleMessage = (message) => {
@ -14,14 +18,33 @@ class TransportProcessor extends AudioWorkletProcessor {
this.started = true;
} else if (message.data === "pause") {
this.started = false;
if(this.lastPausedTime === 0) {
this.lastPausedTime = currentTime;
}
} else if (message.data === "stop") {
this.started = false;
this.currentTime = 0;
this.totalPausedTime = 0;
this.lastPausedTime = 0;
this.startedAgainTime = 0;
this.wasStopped = true;
}
};
process(inputs, outputs, parameters) {
if (this.started) this.port.postMessage({ type: "bang", currentTime });
if (this.started) {
if(this.lastPausedTime>0) {
const pausedTime = currentTime-this.lastPausedTime;
this.totalPausedTime += pausedTime;
this.lastPausedTime = 0;
}
if(this.wasStopped) {
this.startedAgainTime = currentTime;
this.wasStopped = false;
}
const logicalTime = currentTime-this.totalPausedTime-this.startedAgainTime;
//console.log("Logical/Current:", logicalTime, currentTime);
this.port.postMessage({ type: "bang", logicalTime });
}
return true;
}
}

View File

@ -37,6 +37,12 @@ export class Player extends Event {
}
areWeThereYet = (): boolean => {
// If clock has stopped
if(this.app.clock.pulses_since_origin<this.callTime) {
this.callTime = 0;
this.tick = 0;
}
const howAboutNow = (
(this.notStarted() && this.app.clock.time_position.pulse === 1) ||
(
@ -46,11 +52,9 @@ export class Player extends Event {
(this.current.duration*4) * this.pulseToSecond(this.app.api.ppqn())
)
);
if(howAboutNow) {
this.tick = 0;
} else {
this.tick++;
}
this.tick = howAboutNow ? 0 : this.tick+1;
return howAboutNow;
}