Added syncing to next beat in ZPlayer
This commit is contained in:
17
src/Clock.ts
17
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.
|
||||
*/
|
||||
|
||||
@ -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.callTime) {
|
||||
this.callTime = 0;
|
||||
this.tick = 0;
|
||||
}
|
||||
|
||||
// Main logic
|
||||
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.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;
|
||||
|
||||
Reference in New Issue
Block a user