Some more logging on message delay

This commit is contained in:
2023-10-28 12:57:37 +03:00
parent 0dbb26feb8
commit 6beb5f2c84
3 changed files with 34 additions and 24 deletions

View File

@ -42,6 +42,7 @@ export class Clock {
private _ppqn: number; private _ppqn: number;
tick: number; tick: number;
running: boolean; running: boolean;
messageSent: number;
constructor(public app: Editor, ctx: AudioContext) { constructor(public app: Editor, ctx: AudioContext) {
this.time_position = { bar: 0, beat: 0, pulse: 0 }; this.time_position = { bar: 0, beat: 0, pulse: 0 };
@ -54,6 +55,7 @@ export class Clock {
this.transportNode = null; this.transportNode = null;
this.ctx = ctx; this.ctx = ctx;
this.running = true; this.running = true;
this.messageSent = 0;
ctx.audioWorklet ctx.audioWorklet
.addModule(TransportProcessor) .addModule(TransportProcessor)
.then((e) => { .then((e) => {
@ -139,7 +141,8 @@ export class Clock {
set bpm(bpm: number) { set bpm(bpm: number) {
if (bpm > 0 && this._bpm !== bpm) { if (bpm > 0 && this._bpm !== bpm) {
this.transportNode?.setBPM(bpm); this.messageSent = this.app.audioContext.currentTime;
this.transportNode?.setBPM(bpm, this.messageSent);
this._bpm = bpm; this._bpm = bpm;
} }
} }
@ -159,7 +162,8 @@ export class Clock {
set ppqn(ppqn: number) { set ppqn(ppqn: number) {
if (ppqn > 0 && this._ppqn !== ppqn) { if (ppqn > 0 && this._ppqn !== ppqn) {
this._ppqn = ppqn; this._ppqn = ppqn;
this.transportNode?.setPPQN(ppqn); this.messageSent = this.app.audioContext.currentTime;
this.transportNode?.setPPQN(ppqn, this.messageSent);
} }
} }
@ -201,12 +205,14 @@ export class Clock {
*/ */
this.app.audioContext.resume(); this.app.audioContext.resume();
this.running = true; this.running = true;
this.messageSent = this.app.audioContext.currentTime;
this.app.api.MidiConnection.sendStartMessage(); this.app.api.MidiConnection.sendStartMessage();
if (this.tick > 0) { if (this.tick > 0) {
this.transportNode?.resume(); this.transportNode?.resume(this.messageSent);
} else { } else {
this.transportNode?.start(); this.transportNode?.start(this.messageSent);
} }
} }
public pause(): void { public pause(): void {
@ -216,7 +222,8 @@ export class Clock {
* @remark also sends a MIDI message if a port is declared * @remark also sends a MIDI message if a port is declared
*/ */
this.running = false; this.running = false;
this.transportNode?.pause(); this.messageSent = this.app.audioContext.currentTime;
this.transportNode?.pause(this.messageSent);
this.app.api.MidiConnection.sendStopMessage(); this.app.api.MidiConnection.sendStopMessage();
} }
@ -232,6 +239,7 @@ export class Clock {
this.elapsed = 0; this.elapsed = 0;
this.time_position = { bar: 0, beat: 0, pulse: 0 }; this.time_position = { bar: 0, beat: 0, pulse: 0 };
this.app.api.MidiConnection.sendStopMessage(); this.app.api.MidiConnection.sendStopMessage();
this.transportNode?.stop(); this.messageSent = this.app.audioContext.currentTime;
this.transportNode?.stop(this.messageSent);
} }
} }

View File

@ -39,31 +39,31 @@ export class TransportNode extends AudioWorkletNode {
} }
}; };
start() { start(sentAt) {
this.port.postMessage("start"); this.port.postMessage({ type: "start", sentAt: sentAt});
} }
pause() { pause(sentAt) {
this.port.postMessage("pause"); this.port.postMessage({ type: "pause", sentAt: sentAt});
} }
resume() { resume(sentAt) {
this.port.postMessage("resume"); this.port.postMessage({ type: "resume", sentAt: sentAt });
} }
setBPM(bpm) { setBPM(bpm, sentAt) {
this.port.postMessage({ type: "bpm", value: bpm }); this.port.postMessage({ type: "bpm", value: bpm, sentAt: sentAt });
} }
setPPQN(ppqn) { setPPQN(ppqn, sentAt) {
this.port.postMessage({ type: "ppqn", value: ppqn }); this.port.postMessage({ type: "ppqn", value: ppqn, sentAt: sentAt });
} }
setNudge(nudge) { setNudge(nudge, sentAt) {
this.port.postMessage({ type: "nudge", value: nudge }); this.port.postMessage({ type: "nudge", value: nudge, sentAt: sentAt });
} }
stop() { stop(sentAt) {
this.port.postMessage("stop"); this.port.postMessage({type: "stop", sentAt: sentAt});
} }
} }

View File

@ -17,21 +17,21 @@ class TransportProcessor extends AudioWorkletProcessor {
handleMessage = (message) => { 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.type === "start") {
this.started = true; this.started = true;
this.lastPlayPressTime = currentTime; this.lastPlayPressTime = currentTime;
this.totalPausedTime = 0; this.totalPausedTime = 0;
} else if (message.data === "resume") { } else if (message.data.type === "resume") {
this.started = true; this.started = true;
if (this.lastPauseTime !== null) { if (this.lastPauseTime !== null) {
this.totalPausedTime += currentTime - this.lastPauseTime; this.totalPausedTime += currentTime - this.lastPauseTime;
this.lastPauseTime = null; this.lastPauseTime = null;
} }
} else if (message.data === "pause") { } else if (message.data.type === "pause") {
this.started = false; this.started = false;
this.lastPauseTime = currentTime; this.lastPauseTime = currentTime;
} else if (message.data === "stop") { } else if (message.data.type === "stop") {
this.started = false; this.started = false;
} else if (message.data.type === 'bpm') { } else if (message.data.type === 'bpm') {
this.bpm = message.data.value; this.bpm = message.data.value;
@ -40,6 +40,8 @@ class TransportProcessor extends AudioWorkletProcessor {
} else if (message.data.type === 'nudge') { } else if (message.data.type === 'nudge') {
this.nudge = message.data.value this.nudge = message.data.value
} }
// Log difference between currentTime and message.data.sentAt
console.log("Message delay: ", currentTime - message.data.sentAt);
} }
process(inputs, outputs, parameters) { process(inputs, outputs, parameters) {