Merge branch 'logicaltime' of https://github.com/Bubobubobubobubo/Topos into dev

This commit is contained in:
2023-08-26 18:48:05 +03:00
8 changed files with 118 additions and 41 deletions

View File

@ -9,12 +9,14 @@ import { RestEvent } from "./RestEvent";
export class Player extends Event {
input: string;
ziffers: Ziffers;
callTime: number = 0;
firstCallTime: number = 0;
lastCallTime: number = 0;
waitTime = 0;
startBeat: number = 0;
played: boolean = false;
current!: Pitch|Chord|ZRest;
retro: boolean = false;
tick: number = 0;
index: number = -1;
constructor(input: string, options: object, public app: Editor) {
super(app);
@ -22,6 +24,22 @@ export class Player extends Event {
this.ziffers = new Ziffers(input, options);
}
get ticks(): number {
const dur = this.ziffers.duration;
return dur * 4 * this.app.clock.ppqn;
}
nextEndTime(): number {
return this.firstCallTime + this.ticks;
}
updateLastCallTime(): void {
if (this.notStarted() || this.played) {
this.lastCallTime = this.app.clock.pulses_since_origin;
this.played = false;
}
}
notStarted(): boolean {
return this.ziffers.notStarted();
}
@ -36,21 +54,38 @@ 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.lastCallTime) {
this.lastCallTime = 0;
this.index = 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) &&
(this.app.clock.pulses_since_origin+1 >= this.firstCallTime+this.waitTime)
)
||
( // If pattern is already playing
this.current &&
this.pulseToSecond(this.app.api.epulse()+1) >=
this.pulseToSecond(this.callTime) +
this.pulseToSecond(this.app.clock.pulses_since_origin+1) >=
this.pulseToSecond(this.lastCallTime) +
(this.current.duration*4) * this.pulseToSecond(this.app.api.ppqn())
)
);
if(howAboutNow) {
this.tick = 0;
} else {
this.tick++;
// Increment index of how many times sound/midi have been called
this.index = howAboutNow ? this.index+1 : this.index;
if(howAboutNow && this.notStarted()) {
this.firstCallTime = this.app.clock.pulses_since_origin+1;
}
return howAboutNow;
}
@ -99,12 +134,33 @@ export class Player extends Event {
}
retrograde() {
if(this.tick === 0 && this.ziffers.index === 0) {
if(this.index === -1 && this.ziffers.index === -1) {
this.ziffers.retrograde();
}
return this;
}
wait(value: number) {
if(this.index === -1 && this.ziffers.index === -1) {
// TODO: THIS LATER!
/* if(typeof value === "string") {
const cueKey = this.app.api.patternCues.get(value);
if(cueKey) {
const waitedPatter = this.app.api.patternCache.get(cueKey) as Player;
if(waitedPatter) {
this.waitTime = waitedPatter.nextEndTime();
}
}
} */
this.waitTime = Math.ceil(value*4*this.app.clock.ppqn);
}
return this;
}
out = (): void => {
// TODO?
}