From 97b3f89a20afbc25379c43b0cfbb016bbc84be7d Mon Sep 17 00:00:00 2001 From: Miika Alonen Date: Sat, 26 Aug 2023 18:43:39 +0300 Subject: [PATCH] Add wait method to ziffers --- package.json | 2 +- src/API.ts | 5 +--- src/Clock.ts | 1 - src/classes/ZPlayer.ts | 65 +++++++++++++++++++++++++++++++++++------- yarn.lock | 8 +++--- 5 files changed, 61 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 49f9613..f0e1838 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "tone": "^14.8.49", "unique-names-generator": "^4.7.1", "vite-plugin-markdown": "^2.1.0", - "zifferjs": "^0.0.13", + "zifferjs": "^0.0.14", "zzfx": "^1.2.0" } } diff --git a/src/API.ts b/src/API.ts index fb1fde3..4407bc8 100644 --- a/src/API.ts +++ b/src/API.ts @@ -554,10 +554,7 @@ export class UserAPI { player = new Player(input, options, this.app); this.app.api.patternCache.set(key, player); } - if ((player && player.notStarted()) || player.played) { - player.callTime = this.epulse(); - player.played = false; - } + if(player) player.updateLastCallTime(); return player; }; diff --git a/src/Clock.ts b/src/Clock.ts index a1d5ea7..ed6c987 100644 --- a/src/Clock.ts +++ b/src/Clock.ts @@ -121,7 +121,6 @@ export class Clock { return n * this.pulse_duration } - public start(): void { /** * Starts the TransportNode (starts the clock). diff --git a/src/classes/ZPlayer.ts b/src/classes/ZPlayer.ts index f5a0294..ee4b9f9 100644 --- a/src/classes/ZPlayer.ts +++ b/src/classes/ZPlayer.ts @@ -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(); } @@ -39,28 +57,34 @@ export class Player extends Event { // Check if it's time to play the event areWeThereYet = (): boolean => { // If clock has stopped - if(this.app.clock.pulses_since_origin= this.app.clock.next_beat_in_ticks) + 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.clock.pulses_since_origin+1) >= - this.pulseToSecond(this.callTime) + + this.pulseToSecond(this.lastCallTime) + (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; + // 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; } @@ -110,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? } diff --git a/yarn.lock b/yarn.lock index 799b986..347cbc0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1446,10 +1446,10 @@ yaml@^2.1.1: resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.3.1.tgz#02fe0975d23cd441242aa7204e09fc28ac2ac33b" integrity sha512-2eHWfjaoXgTBC2jNM1LRef62VQa0umtvRiDSk6HSzW7RvS5YtkabJrwYLLEKWBc8a5U2PTSCs+dJjUTJdlHsWQ== -zifferjs@^0.0.13: - version "0.0.13" - resolved "https://registry.yarnpkg.com/zifferjs/-/zifferjs-0.0.13.tgz#af155633357c95da6a4e5aaa84f23013b5574236" - integrity sha512-eNOQOn+NM4L3v2FqQEf0RSiJOKiZMaotGLGj1VBCPHi5WhHp3N61R7k9ZrnQKhPnfSI80NBoplhQ1Q1sdEjFlQ== +zifferjs@^0.0.14: + version "0.0.14" + resolved "https://registry.yarnpkg.com/zifferjs/-/zifferjs-0.0.14.tgz#7876c799a08e799be7af22b65f4cb6f0b44f79ca" + integrity sha512-CpS3zTm8Btm8aTxd7sSUgVCF/S/jJ3hqwgp7uRzbZI8k6yJWhzo/rjMlEZoOmeBhs7Qy4XsVk7pfrLdS8AAIVA== zzfx@^1.2.0: version "1.2.0"