From 0dcf7237369fe7c406e24fa0bd072e43bf62a8ec Mon Sep 17 00:00:00 2001 From: Miika Alonen Date: Sat, 26 Aug 2023 15:00:52 +0300 Subject: [PATCH] Added syncing to next beat in ZPlayer --- src/Clock.ts | 17 ++++++++++++++--- src/classes/ZPlayer.ts | 17 ++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Clock.ts b/src/Clock.ts index bbd23c7..a1d5ea7 100644 --- a/src/Clock.ts +++ b/src/Clock.ts @@ -65,12 +65,23 @@ export class Clock { * * @returns number of ticks until next bar */ - const currentBeatInTicks = ((this.app.clock.beats_since_origin - 1) * 48) + this.time_position.pulse + 1 + const currentBeatInTicks = ((this.app.clock.beats_since_origin - 1) * this.ppqn) + this.time_position.pulse + 1 const nextBarinTicks = (this.beats_per_bar * this.ppqn) * this.time_position.bar + 1 - return nextBarinTicks - currentBeatInTicks + return nextBarinTicks - currentBeatInTicks; } - get beats_per_bar(): number { + get next_beat_in_ticks(): number { + /** + * This function returns the number of ticks separating the current moment + * from the beginning of the next beat. + * + * @returns number of ticks until next beat + */ + const ticksMissingToNextBeat = (this.time_position.pulse + 1) % this.ppqn; + return this.app.clock.pulses_since_origin + ticksMissingToNextBeat; + } + + get beats_per_bar(): number { /** * Returns the number of beats per bar. */ diff --git a/src/classes/ZPlayer.ts b/src/classes/ZPlayer.ts index 4526bc1..f5a0294 100644 --- a/src/classes/ZPlayer.ts +++ b/src/classes/ZPlayer.ts @@ -36,23 +36,30 @@ export class Player extends Event { return this.app.clock.convertPulseToSecond(pulse); } + // Check if it's time to play the event areWeThereYet = (): boolean => { // If clock has stopped if(this.app.clock.pulses_since_origin= this.app.clock.next_beat_in_ticks) + ) + || + ( // If pattern is already playing this.current && - this.pulseToSecond(this.app.api.epulse()+1) >= + this.pulseToSecond(this.app.clock.pulses_since_origin+1) >= this.pulseToSecond(this.callTime) + (this.current.duration*4) * this.pulseToSecond(this.app.api.ppqn()) ) ); - + + // Increment local tick (how many times sound/midi has been called) this.tick = howAboutNow ? 0 : this.tick+1; return howAboutNow;