First try
This commit is contained in:
@ -10,9 +10,7 @@ export class TransportNode extends AudioWorkletNode {
|
||||
this.port.start();
|
||||
/** @type {HTMLSpanElement} */
|
||||
this.$clock = document.getElementById("clockviewer");
|
||||
this.hasBeenEvaluated = false;
|
||||
this.currentPulsePosition = 0;
|
||||
this.nextPulsePosition = -1;
|
||||
this.executionLatency = 0;
|
||||
this.lastLatencies = [
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
@ -21,29 +19,29 @@ 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;
|
||||
}
|
||||
|
||||
/** @type {(this: MessagePort, ev: MessageEvent<any>) => any} */
|
||||
handleMessage = (message) => {
|
||||
if (message.data && message.data.type === "bang") {
|
||||
let { futureTimeStamp, timeToNextPulse, nextPulsePosition } = this.convertTimeToNextBarsBeats(message.data.logicalTime);
|
||||
|
||||
// Evaluate the global buffer only once per ppqn value
|
||||
if (this.nextPulsePosition !== nextPulsePosition) {
|
||||
this.nextPulsePosition = nextPulsePosition;
|
||||
setTimeout(() => {
|
||||
const now = this.app.audioContext.currentTime;
|
||||
this.app.clock.time_position = futureTimeStamp;
|
||||
tryEvaluate(this.app, this.app.global_buffer);
|
||||
this.hasBeenEvaluated = true;
|
||||
this.currentPulsePosition = nextPulsePosition;
|
||||
const then = this.app.audioContext.currentTime;
|
||||
this.lastLatencies[this.indexOfLastLatencies] = then - now;
|
||||
this.indexOfLastLatencies = (this.indexOfLastLatencies + 1) % this.lastLatencies.length;
|
||||
const averageLatency = this.lastLatencies.reduce((a, b) => a + b) / this.lastLatencies.length;
|
||||
this.executionLatency = averageLatency / 1000;
|
||||
}, (timeToNextPulse + this.executionLatency) * 1000);
|
||||
}
|
||||
this.logicalTime = message.data.logicalTime;
|
||||
this.app.clock.tick++
|
||||
let futureTimeStamp = this.convertTicksToTimeposition(this.app.clock.tick);
|
||||
console.log("BANG", this.logicalTime, futureTimeStamp);
|
||||
|
||||
setTimeout(() => {
|
||||
console.log("EVALUATING");
|
||||
const now = this.app.audioContext.currentTime;
|
||||
this.app.clock.time_position = futureTimeStamp;
|
||||
tryEvaluate(this.app, this.app.global_buffer);
|
||||
const then = this.app.audioContext.currentTime;
|
||||
this.lastLatencies[this.indexOfLastLatencies] = then - now;
|
||||
this.indexOfLastLatencies = (this.indexOfLastLatencies + 1) % this.lastLatencies.length;
|
||||
const averageLatency = this.lastLatencies.reduce((a, b) => a + b) / this.lastLatencies.length;
|
||||
this.executionLatency = averageLatency / 1000;
|
||||
}, (this.app.clock.pulse_duration + this.executionLatency) * 1000);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
@ -55,6 +53,14 @@ export class TransportNode extends AudioWorkletNode {
|
||||
this.port.postMessage("pause");
|
||||
}
|
||||
|
||||
setBPM(bpm) {
|
||||
this.port.postMessage({ type: "bpm", value: bpm });
|
||||
}
|
||||
|
||||
setPPQN(ppqn) {
|
||||
this.port.postMessage({ type: "ppqn", value: ppqn });
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.app.clock.tick = 0;
|
||||
// this.$clock.innerHTML = `[${1} | ${1} | ${zeroPad(1, '2')}]`;
|
||||
@ -86,17 +92,16 @@ export class TransportNode extends AudioWorkletNode {
|
||||
|
||||
const futureBeatNumber = this.nextPulsePosition / this.app.clock.ppqn;
|
||||
const futureBarNumber = futureBeatNumber / beatsPerBar;
|
||||
const futureTimeStamp = {
|
||||
bar: Math.floor(futureBarNumber) + 1,
|
||||
beat: Math.floor(futureBeatNumber) % beatsPerBar + 1,
|
||||
pulse: Math.floor(this.nextPulsePosition) % this.app.clock.ppqn
|
||||
};
|
||||
|
||||
this.app.clock.tick++
|
||||
return {
|
||||
futureTimeStamp,
|
||||
timeToNextPulse,
|
||||
nextPulsePosition
|
||||
};
|
||||
}
|
||||
|
||||
convertTicksToTimeposition(ticks) {
|
||||
const beatsPerBar = this.app.clock.time_signature[0];
|
||||
const ppqnPosition = (ticks % this.app.clock.ppqn)+1;
|
||||
const beatNumber = Math.floor(ticks / this.app.clock.ppqn);
|
||||
const barNumber = Math.floor(beatNumber / beatsPerBar)+1;
|
||||
const beatWithinBar = Math.floor(beatNumber % beatsPerBar)+1;
|
||||
return {bar: barNumber, beat: beatWithinBar, ppqn: ppqnPosition};
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user