Fix sync problems

This commit is contained in:
2023-10-26 02:09:33 +03:00
parent c03315c1d0
commit 4fd9f01a28
5 changed files with 29 additions and 18 deletions

View File

@ -683,6 +683,10 @@ export class UserAPI {
return args.map((arg) => JSON.stringify(arg)).join(",");
};
public resetAllFromCache = (): void => {
this.patternCache.forEach((player) => (player as Player).reset());
}
public z = (
input: string,
options: InputOptions = {},
@ -701,7 +705,7 @@ export class UserAPI {
}
if (!player) {
player = new Player(input, options, this.app);
player = new Player(input, options, this.app, zid);
this.app.api.patternCache.set(key, player);
}
@ -709,7 +713,7 @@ export class UserAPI {
player.updateLastCallTime();
if (id !== "") {
if (id !== "" && zid !== "z0") {
// Sync named patterns to z0 by default
player.sync("z0");
}

View File

@ -201,6 +201,7 @@ export class Clock {
* @remark also sends a MIDI message if a port is declared
*/
this.app.clock.tick = -1;
this.time_position = { bar: -1, beat: -1, pulse: -1 };
this.app.api.MidiConnection.sendStopMessage();
this.transportNode?.stop();
}

View File

@ -15,7 +15,6 @@ export class Player extends Event {
startCallTime: number = 0;
lastCallTime: number = 0;
waitTime = 0;
startBeat: number = 0;
played: boolean = false;
current!: Pitch | Chord | ZRest;
retro: boolean = false;
@ -23,13 +22,25 @@ export class Player extends Event {
zid: string = "";
options: InputOptions = {};
skipIndex = 0;
endTime = 0;
constructor(input: string, options: InputOptions, public app: Editor) {
constructor(input: string, options: InputOptions, public app: Editor, zid: string = "") {
super(app);
this.input = input;
this.options = options;
this.ziffers = new Ziffers(input, options);
this.zid = zid;
}
reset() {
this.initCallTime = 0;
this.startCallTime = 0;
this.lastCallTime = 0;
this.waitTime = 0;
this.index = 0;
this.skipIndex = 0;
this.played = false;
this.skipIndex = 0;
this.ziffers.reset();
}
get ticks(): number {
@ -94,18 +105,13 @@ export class Player extends Event {
areWeThereYet = (): boolean => {
// If clock has stopped
if (this.app.clock.pulses_since_origin < this.lastCallTime) {
this.lastCallTime = 0;
this.startCallTime = 0;
this.index = 0;
this.waitTime = 0;
this.skipIndex = 0;
this.ziffers.index = 0;
this.app.api.resetAllFromCache();
}
const patternIsStarting = (this.notStarted() &&
(this.pulse() === 0 || this.origin() >= this.nextBeatInTicks()) &&
this.origin() >= this.waitTime);
const timeToPlayNext = (this.current &&
this.pulseToSecond(this.origin()) >=
this.pulseToSecond(this.lastCallTime) +