First try

This commit is contained in:
2023-08-29 16:08:11 +03:00
parent 9e993e8579
commit 6b7517ea4d
7 changed files with 105 additions and 64 deletions

View File

@ -9,24 +9,31 @@ class TransportProcessor extends AudioWorkletProcessor {
this.lastPausedTime = 0;
this.startedAgainTime = 0;
this.wasStopped = false;
this.bpm = 120;
this.ppqn = 48;
this.currentPulsePosition = 0;
}
handleMessage = (message) => {
if (message.data && message.data.type === "ping") {
if(message.data && message.data.type === "ping") {
this.port.postMessage(message.data);
} else if (message.data === "start") {
this.started = true;
} else if (message.data === "pause") {
} else if(message.data === "pause") {
this.started = false;
if(this.lastPausedTime === 0) {
this.lastPausedTime = currentTime;
}
} else if (message.data === "stop") {
} else if(message.data === "stop") {
this.started = false;
this.totalPausedTime = 0;
this.lastPausedTime = 0;
this.startedAgainTime = 0;
this.wasStopped = true;
this.currentPulsePosition = 0;
} else if(message.data === 'bpm') {
this.bpm = message.data.value;
} else if(message.data === 'ppqn') {
this.ppqn = message.data.value;
}
};
@ -42,8 +49,16 @@ class TransportProcessor extends AudioWorkletProcessor {
this.wasStopped = false;
}
const logicalTime = currentTime-this.totalPausedTime-this.startedAgainTime;
//console.log(currentTime, this.totalPausedTime, this.startedAgainTime);
//console.log("Logical/Current:", logicalTime, currentTime);
this.port.postMessage({ type: "bang", logicalTime });
const beatNumber = logicalTime / (60 / this.bpm);
const nextPulsePosition = Math.ceil(beatNumber * this.ppqn);
if(nextPulsePosition > this.currentPulsePosition) {
this.currentPulsePosition = nextPulsePosition;
this.port.postMessage({ type: "bang", logicalTime });
}
}
return true;
}