diff --git a/src/Clock.ts b/src/Clock.ts index 23d26fd..343360f 100644 --- a/src/Clock.ts +++ b/src/Clock.ts @@ -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 { diff --git a/src/TransportNode.js b/src/TransportNode.js index f4d679f..ceb51b3 100644 --- a/src/TransportNode.js +++ b/src/TransportNode.js @@ -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!"); } diff --git a/src/TransportProcessor.js b/src/TransportProcessor.js index 7cc698a..7c8abff 100644 --- a/src/TransportProcessor.js +++ b/src/TransportProcessor.js @@ -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;