From 4a03a726452c442f26a224f1621a6a2b81de3991 Mon Sep 17 00:00:00 2001 From: Miika Alonen Date: Thu, 24 Aug 2023 23:15:36 +0300 Subject: [PATCH] Add logical time to transportprocessor --- src/TransportNode.js | 12 ++---------- src/TransportProcessor.js | 29 ++++++++++++++++++++++++++--- src/classes/ZPlayer.ts | 14 +++++++++----- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/TransportNode.js b/src/TransportNode.js index 3ffd396..b9df84b 100644 --- a/src/TransportNode.js +++ b/src/TransportNode.js @@ -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} */ 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"); diff --git a/src/TransportProcessor.js b/src/TransportProcessor.js index da91ba9..aab0fdf 100644 --- a/src/TransportProcessor.js +++ b/src/TransportProcessor.js @@ -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; } } diff --git a/src/classes/ZPlayer.ts b/src/classes/ZPlayer.ts index 2486e08..4526bc1 100644 --- a/src/classes/ZPlayer.ts +++ b/src/classes/ZPlayer.ts @@ -37,6 +37,12 @@ export class Player extends Event { } areWeThereYet = (): boolean => { + // If clock has stopped + if(this.app.clock.pulses_since_origin