This commit is contained in:
2023-07-28 16:18:59 +02:00
parent c99962b7ef
commit 3732adb6f0

View File

@ -10,7 +10,6 @@ export class TransportNode extends AudioWorkletNode {
this.port.start(); this.port.start();
/** @type {HTMLSpanElement} */ /** @type {HTMLSpanElement} */
this.$clock = document.getElementById("clockviewer"); this.$clock = document.getElementById("clockviewer");
this.offset_time = 0;
} }
/** @type {(this: MessagePort, ev: MessageEvent<any>) => any} */ /** @type {(this: MessagePort, ev: MessageEvent<any>) => any} */
handleMessage = (message) => { handleMessage = (message) => {
@ -31,20 +30,14 @@ export class TransportNode extends AudioWorkletNode {
} }
convertTimeToBarsBeats(currentTime) { convertTimeToBarsBeats(currentTime) {
// Calculate the duration of one beat in seconds
const beatDuration = 60 / this.app.clock.bpm; const beatDuration = 60 / this.app.clock.bpm;
const beatNumber = (currentTime) / beatDuration;
// Calculate the beat number
const beatNumber = (currentTime - this.offset_time) / beatDuration;
// Calculate the bar and beat numbers
const beatsPerBar = this.app.clock.time_signature[0]; const beatsPerBar = this.app.clock.time_signature[0];
const barNumber = Math.floor(beatNumber / beatsPerBar) + 1; // Adding 1 to make it 1-indexed const barNumber = Math.floor(beatNumber / beatsPerBar) + 1; // Adding 1 to make it 1-indexed
const beatWithinBar = Math.floor(beatNumber % beatsPerBar) + 1; // Adding 1 to make it 1-indexed const beatWithinBar = Math.floor(beatNumber % beatsPerBar) + 1; // Adding 1 to make it 1-indexed
// Calculate the PPQN position
const ppqnPosition = Math.floor((beatNumber % 1) * this.app.clock.ppqn); const ppqnPosition = Math.floor((beatNumber % 1) * this.app.clock.ppqn);
return { bar: barNumber, beat: beatWithinBar, ppqn: ppqnPosition }; return { bar: barNumber, beat: beatWithinBar, ppqn: ppqnPosition };
} }
} }