diff --git a/src/Clock.ts b/src/Clock.ts index aca16bc..9ef51c9 100644 --- a/src/Clock.ts +++ b/src/Clock.ts @@ -127,8 +127,10 @@ export class Clock { } set bpm(bpm: number) { - this._bpm = bpm; - this.transportNode?.setBPM(bpm); + if(bpm>0 && this._bpm !== bpm) { + this._bpm = bpm; + this.transportNode?.setBPM(bpm); + } } get ppqn(): number { @@ -136,8 +138,10 @@ export class Clock { } set ppqn(ppqn: number) { - this._ppqn = ppqn; - this.transportNode?.setPPQN(ppqn); + if(ppqn>0 && this._ppqn !== ppqn) { + this._ppqn = ppqn; + this.transportNode?.setPPQN(ppqn); + } } public convertPulseToSecond(n: number): number { diff --git a/src/TransportNode.js b/src/TransportNode.js index 589fe29..456cefd 100644 --- a/src/TransportNode.js +++ b/src/TransportNode.js @@ -8,19 +8,16 @@ export class TransportNode extends AudioWorkletNode { this.app = application this.port.addEventListener("message", this.handleMessage); this.port.start(); - this.logicalTime = 0; } /** @type {(this: MessagePort, ev: MessageEvent) => any} */ handleMessage = (message) => { if (message.data && message.data.type === "bang") { - this.logicalTime = message.data.logicalTime; this.app.clock.tick++ - const futureTimeStamp = this.app.clock.convertTicksToTimeposition(this.app.clock.tick); - this.app.clock.time_position = futureTimeStamp; + if (this.app.exampleIsPlaying) { tryEvaluate(this.app, this.app.example_buffer); } else { diff --git a/src/TransportProcessor.js b/src/TransportProcessor.js index 19dc8b3..0542ca7 100644 --- a/src/TransportProcessor.js +++ b/src/TransportProcessor.js @@ -5,10 +5,6 @@ class TransportProcessor extends AudioWorkletProcessor { this.port.addEventListener("message", this.handleMessage); this.port.start(); this.started = false; - this.totalPausedTime = 0; - this.lastPausedTime = 0; - this.startedAgainTime = 0; - this.wasStopped = false; this.bpm = 120; this.ppqn = 48; this.currentPulsePosition = 0; @@ -21,41 +17,24 @@ class TransportProcessor extends AudioWorkletProcessor { this.started = true; } else if(message.data === "pause") { this.started = false; - if(this.lastPausedTime === 0) { - this.lastPausedTime = currentTime; - } } else if(message.data === "stop") { this.started = false; - this.totalPausedTime = 0; - this.lastPausedTime = 0; - this.wasStopped = true; - this.currentPulsePosition = 0; } else if(message.data.type === 'bpm') { this.bpm = message.data.value; + this.currentPulsePosition = 0; } else if(message.data.type === 'ppqn') { this.ppqn = message.data.value; + this.currentPulsePosition = 0; } }; process(inputs, outputs, parameters) { if (this.started) { - if(this.lastPausedTime>0) { - const pausedTime = currentTime-this.lastPausedTime; - this.totalPausedTime += pausedTime; - this.lastPausedTime = 0; - } - if(this.wasStopped) { - this.startedAgainTime = currentTime; - this.wasStopped = false; - } - - const logicalTime = currentTime-this.totalPausedTime-this.startedAgainTime; - const beatNumber = logicalTime / (60 / this.bpm); + const beatNumber = currentTime / (60 / this.bpm); const currentPulsePosition = Math.ceil(beatNumber * this.ppqn); - if(currentPulsePosition > this.currentPulsePosition) { this.currentPulsePosition = currentPulsePosition; - this.port.postMessage({ type: "bang", logicalTime }); + this.port.postMessage({ type: "bang" }); } } return true;