Fix for bpm change issue

This commit is contained in:
2023-10-28 21:25:24 +03:00
parent 6beb5f2c84
commit 2a44840ee7
3 changed files with 17 additions and 9 deletions

View File

@ -131,6 +131,13 @@ export class Clock {
return 60 / this.bpm / this.ppqn;
}
public pulse_duration_at_bpm(bpm: number = this.bpm): number {
/**
* Returns the duration of a pulse in seconds at a specific bpm.
*/
return 60 / bpm / this.ppqn;
}
get bpm(): number {
return this._bpm;
}
@ -167,9 +174,9 @@ export class Clock {
}
}
public incrementTick() {
public incrementTick(bpm: number) {
this.tick++;
this.logicalTime += this.pulse_duration;
this.logicalTime += this.pulse_duration_at_bpm(bpm);
}
public nextTickFrom(time: number, nudge: number): number {

View File

@ -31,7 +31,7 @@ export class TransportNode extends AudioWorkletNode {
} else {
tryEvaluate(this.app, this.app.global_buffer);
}
this.app.clock.incrementTick();
this.app.clock.incrementTick(message.data.bpm);
} else {
console.log("STILLLLLLLLLLLLLLLL BANGING!");
}

View File

@ -11,7 +11,6 @@ class TransportProcessor extends AudioWorkletProcessor {
this.currentPulsePosition = 0;
this.totalPausedTime = 0;
this.lastPauseTime = null;
}
handleMessage = (message) => {
@ -35,13 +34,13 @@ class TransportProcessor extends AudioWorkletProcessor {
this.started = false;
} else if (message.data.type === 'bpm') {
this.bpm = message.data.value;
this.currentPulsePosition = currentTime;
} else if (message.data.type === 'ppqn') {
this.ppqn = message.data.value;
} 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);
console.log("Message delay: ", currentTime - message.data.sentAt);
}
process(inputs, outputs, parameters) {
@ -50,10 +49,12 @@ class TransportProcessor extends AudioWorkletProcessor {
const beatNumber = adjustedCurrentTime / (60 / this.bpm);
const currentPulsePosition = Math.ceil(beatNumber * this.ppqn);
const elapsedTime = (currentTime - this.lastPlayPressTime) - this.totalPausedTime;
this.port.postMessage({ type: "elapsed", value: elapsedTime })
this.port.postMessage({ type: "elapsed", value: elapsedTime });
if (currentPulsePosition > this.currentPulsePosition) {
this.currentPulsePosition = currentPulsePosition;
this.port.postMessage({ type: "bang" });
this.port.postMessage({ type: "bang", bpm: this.bpm });
} else {
console.log("No bang", currentPulsePosition, this.currentPulsePosition);
}
}
return true;