Added syncing to next beat in ZPlayer

This commit is contained in:
2023-08-26 15:00:52 +03:00
parent 75191fa6c1
commit 0dcf723736
2 changed files with 26 additions and 8 deletions

View File

@ -65,12 +65,23 @@ export class Clock {
* *
* @returns number of ticks until next bar * @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 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. * Returns the number of beats per bar.
*/ */

View File

@ -36,23 +36,30 @@ export class Player extends Event {
return this.app.clock.convertPulseToSecond(pulse); return this.app.clock.convertPulseToSecond(pulse);
} }
// Check if it's time to play the event
areWeThereYet = (): boolean => { areWeThereYet = (): boolean => {
// If clock has stopped // If clock has stopped
if(this.app.clock.pulses_since_origin<this.callTime) { if(this.app.clock.pulses_since_origin<this.callTime) {
this.callTime = 0; this.callTime = 0;
this.tick = 0; this.tick = 0;
} }
// Main logic
const howAboutNow = ( const howAboutNow = (
(this.notStarted() && this.app.clock.time_position.pulse === 1) || ( // If pattern is just starting
( this.notStarted() &&
(this.app.clock.time_position.pulse === 1 ||
this.app.clock.pulses_since_origin+1 >= this.app.clock.next_beat_in_ticks)
)
||
( // If pattern is already playing
this.current && this.current &&
this.pulseToSecond(this.app.api.epulse()+1) >= this.pulseToSecond(this.app.clock.pulses_since_origin+1) >=
this.pulseToSecond(this.callTime) + this.pulseToSecond(this.callTime) +
(this.current.duration*4) * this.pulseToSecond(this.app.api.ppqn()) (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; this.tick = howAboutNow ? 0 : this.tick+1;
return howAboutNow; return howAboutNow;