From 4fd9f01a28b5dcc9ae6d002a3a4fdfda83d3935a Mon Sep 17 00:00:00 2001 From: Miika Alonen Date: Thu, 26 Oct 2023 02:09:33 +0300 Subject: [PATCH] Fix sync problems --- package.json | 4 ++-- src/API.ts | 8 ++++++-- src/Clock.ts | 1 + src/classes/ZPlayer.ts | 26 ++++++++++++++++---------- yarn.lock | 8 ++++---- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 91d20dc..abd6c4f 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "tone": "^14.8.49", "unique-names-generator": "^4.7.1", "vite-plugin-markdown": "^2.1.0", - "zifferjs": "^0.0.33", + "zifferjs": "^0.0.34", "zzfx": "^1.2.0" } -} \ No newline at end of file +} diff --git a/src/API.ts b/src/API.ts index 09cca89..6436e69 100644 --- a/src/API.ts +++ b/src/API.ts @@ -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"); } diff --git a/src/Clock.ts b/src/Clock.ts index c94ff96..a2ed80b 100644 --- a/src/Clock.ts +++ b/src/Clock.ts @@ -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(); } diff --git a/src/classes/ZPlayer.ts b/src/classes/ZPlayer.ts index dce79a8..85fb3ba 100644 --- a/src/classes/ZPlayer.ts +++ b/src/classes/ZPlayer.ts @@ -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) + diff --git a/yarn.lock b/yarn.lock index 96ee3d0..46f10ce 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1451,10 +1451,10 @@ yaml@^2.1.1: resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.3.1.tgz#02fe0975d23cd441242aa7204e09fc28ac2ac33b" integrity sha512-2eHWfjaoXgTBC2jNM1LRef62VQa0umtvRiDSk6HSzW7RvS5YtkabJrwYLLEKWBc8a5U2PTSCs+dJjUTJdlHsWQ== -zifferjs@^0.0.33: - version "0.0.33" - resolved "https://registry.yarnpkg.com/zifferjs/-/zifferjs-0.0.33.tgz#286e3c9ccaebe88d4acaad59f99db0cff5cc1884" - integrity sha512-sYD4wOSKTip4GTkK3epMNAzlRWN3YpQlgR9sCGvyj6xdHzaaEHdU8F/PvkI092UbfYi4NNM/pVZKF1RLUlJPAw== +zifferjs@^0.0.34: + version "0.0.34" + resolved "https://registry.yarnpkg.com/zifferjs/-/zifferjs-0.0.34.tgz#887fb2db1ec2aff21ff1742cbbbbc4621838588f" + integrity sha512-q2eFi+j+yXkPTXU53at3Agrh67JmBJ5zloZ13kc5ObT9R8R9L/if21mbnFxpDJC6Sjugql40aM0Ko4p/zBTs4w== zzfx@^1.2.0: version "1.2.0"