Merge branch 'logicaltime' of https://github.com/Bubobubobubobubo/Topos into dev
This commit is contained in:
@ -38,7 +38,7 @@
|
|||||||
"tone": "^14.8.49",
|
"tone": "^14.8.49",
|
||||||
"unique-names-generator": "^4.7.1",
|
"unique-names-generator": "^4.7.1",
|
||||||
"vite-plugin-markdown": "^2.1.0",
|
"vite-plugin-markdown": "^2.1.0",
|
||||||
"zifferjs": "^0.0.13",
|
"zifferjs": "^0.0.14",
|
||||||
"zzfx": "^1.2.0"
|
"zzfx": "^1.2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -316,10 +316,7 @@ export class UserAPI {
|
|||||||
player = new Player(input, options, this.app);
|
player = new Player(input, options, this.app);
|
||||||
this.app.api.patternCache.set(key, player);
|
this.app.api.patternCache.set(key, player);
|
||||||
}
|
}
|
||||||
if ((player && player.notStarted()) || player.played) {
|
if(player) player.updateLastCallTime();
|
||||||
player.callTime = this.epulse();
|
|
||||||
player.played = false;
|
|
||||||
}
|
|
||||||
return player;
|
return player;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
18
src/Clock.ts
18
src/Clock.ts
@ -65,12 +65,23 @@ export class Clock {
|
|||||||
*
|
*
|
||||||
* @returns number of ticks until next bar
|
* @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
|
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.
|
* Returns the number of beats per bar.
|
||||||
*/
|
*/
|
||||||
@ -110,7 +121,6 @@ export class Clock {
|
|||||||
return n * this.pulse_duration
|
return n * this.pulse_duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public start(): void {
|
public start(): void {
|
||||||
/**
|
/**
|
||||||
* Starts the TransportNode (starts the clock).
|
* Starts the TransportNode (starts the clock).
|
||||||
|
|||||||
@ -18,7 +18,9 @@ const tryCatchWrapper = (
|
|||||||
return new Promise((resolve, _) => {
|
return new Promise((resolve, _) => {
|
||||||
try {
|
try {
|
||||||
Function(
|
Function(
|
||||||
`"use strict";try{${codeReplace(code)}} catch (e) {console.log(e); _reportError(e);};`
|
`"use strict";try{${codeReplace(
|
||||||
|
code
|
||||||
|
)}} catch (e) {console.log(e); _reportError(e);};`
|
||||||
).call(application.api);
|
).call(application.api);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -64,7 +66,7 @@ export const tryEvaluate = async (
|
|||||||
const newFunction = new Function(
|
const newFunction = new Function(
|
||||||
`"use strict";try{${codeReplace(
|
`"use strict";try{${codeReplace(
|
||||||
wrappedCode
|
wrappedCode
|
||||||
)}} catch (e) {console.log(e)};`
|
)}} catch (e) {console.log(e); _reportError(e);};`
|
||||||
);
|
);
|
||||||
addFunctionToCache(candidateCode, newFunction);
|
addFunctionToCache(candidateCode, newFunction);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -21,20 +21,12 @@ export class TransportNode extends AudioWorkletNode {
|
|||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
];
|
];
|
||||||
this.indexOfLastLatencies = 0;
|
this.indexOfLastLatencies = 0;
|
||||||
// setInterval(() => this.ping(), 1000);
|
|
||||||
this.startTime = null;
|
|
||||||
this.elapsedTime = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {(this: MessagePort, ev: MessageEvent<any>) => any} */
|
/** @type {(this: MessagePort, ev: MessageEvent<any>) => any} */
|
||||||
handleMessage = (message) => {
|
handleMessage = (message) => {
|
||||||
if (message.data && message.data.type === "bang") {
|
if (message.data && message.data.type === "bang") {
|
||||||
if (this.startTime === null) {
|
let { futureTimeStamp, timeToNextPulse, nextPulsePosition } = this.convertTimeToNextBarsBeats(message.data.logicalTime);
|
||||||
this.startTime = message.data.currentTime;
|
|
||||||
}
|
|
||||||
this.elapsedTime = message.data.currentTime - this.startTime;
|
|
||||||
this.prevCurrentTime = message.data.currentTime;
|
|
||||||
let { futureTimeStamp, timeToNextPulse, nextPulsePosition } = this.convertTimeToNextBarsBeats(this.elapsedTime);
|
|
||||||
|
|
||||||
// Evaluate the global buffer only once per ppqn value
|
// Evaluate the global buffer only once per ppqn value
|
||||||
if (this.nextPulsePosition !== nextPulsePosition) {
|
if (this.nextPulsePosition !== nextPulsePosition) {
|
||||||
@ -42,7 +34,6 @@ export class TransportNode extends AudioWorkletNode {
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const now = this.app.audioContext.currentTime;
|
const now = this.app.audioContext.currentTime;
|
||||||
this.app.clock.time_position = futureTimeStamp;
|
this.app.clock.time_position = futureTimeStamp;
|
||||||
// this.$clock.innerHTML = `[${futureTimeStamp.bar}:${futureTimeStamp.beat}:${zeroPad(futureTimeStamp.pulse, '2')}]`;
|
|
||||||
tryEvaluate(this.app, this.app.global_buffer);
|
tryEvaluate(this.app, this.app.global_buffer);
|
||||||
this.hasBeenEvaluated = true;
|
this.hasBeenEvaluated = true;
|
||||||
this.currentPulsePosition = nextPulsePosition;
|
this.currentPulsePosition = nextPulsePosition;
|
||||||
@ -65,8 +56,6 @@ export class TransportNode extends AudioWorkletNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
this.startTime = null;
|
|
||||||
this.elapsedTime = null;
|
|
||||||
this.app.clock.tick = 0;
|
this.app.clock.tick = 0;
|
||||||
// this.$clock.innerHTML = `[${1} | ${1} | ${zeroPad(1, '2')}]`;
|
// this.$clock.innerHTML = `[${1} | ${1} | ${zeroPad(1, '2')}]`;
|
||||||
this.port.postMessage("stop");
|
this.port.postMessage("stop");
|
||||||
|
|||||||
@ -4,7 +4,11 @@ class TransportProcessor extends AudioWorkletProcessor {
|
|||||||
super(options);
|
super(options);
|
||||||
this.port.addEventListener("message", this.handleMessage);
|
this.port.addEventListener("message", this.handleMessage);
|
||||||
this.port.start();
|
this.port.start();
|
||||||
this.stated = false;
|
this.started = false;
|
||||||
|
this.totalPausedTime = 0;
|
||||||
|
this.lastPausedTime = 0;
|
||||||
|
this.startedAgainTime = 0;
|
||||||
|
this.wasStopped = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
handleMessage = (message) => {
|
handleMessage = (message) => {
|
||||||
@ -14,14 +18,33 @@ class TransportProcessor extends AudioWorkletProcessor {
|
|||||||
this.started = true;
|
this.started = true;
|
||||||
} else if (message.data === "pause") {
|
} else if (message.data === "pause") {
|
||||||
this.started = false;
|
this.started = false;
|
||||||
|
if(this.lastPausedTime === 0) {
|
||||||
|
this.lastPausedTime = currentTime;
|
||||||
|
}
|
||||||
} else if (message.data === "stop") {
|
} else if (message.data === "stop") {
|
||||||
this.started = false;
|
this.started = false;
|
||||||
this.currentTime = 0;
|
this.totalPausedTime = 0;
|
||||||
|
this.lastPausedTime = 0;
|
||||||
|
this.startedAgainTime = 0;
|
||||||
|
this.wasStopped = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
process(inputs, outputs, parameters) {
|
process(inputs, outputs, parameters) {
|
||||||
if (this.started) this.port.postMessage({ type: "bang", currentTime });
|
if (this.started) {
|
||||||
|
if(this.lastPausedTime>0) {
|
||||||
|
const pausedTime = currentTime-this.lastPausedTime;
|
||||||
|
this.totalPausedTime += pausedTime;
|
||||||
|
this.lastPausedTime = 0;
|
||||||
|
}
|
||||||
|
if(this.wasStopped) {
|
||||||
|
this.startedAgainTime = currentTime;
|
||||||
|
this.wasStopped = false;
|
||||||
|
}
|
||||||
|
const logicalTime = currentTime-this.totalPausedTime-this.startedAgainTime;
|
||||||
|
//console.log("Logical/Current:", logicalTime, currentTime);
|
||||||
|
this.port.postMessage({ type: "bang", logicalTime });
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,12 +9,14 @@ import { RestEvent } from "./RestEvent";
|
|||||||
export class Player extends Event {
|
export class Player extends Event {
|
||||||
input: string;
|
input: string;
|
||||||
ziffers: Ziffers;
|
ziffers: Ziffers;
|
||||||
callTime: number = 0;
|
firstCallTime: number = 0;
|
||||||
|
lastCallTime: number = 0;
|
||||||
|
waitTime = 0;
|
||||||
startBeat: number = 0;
|
startBeat: number = 0;
|
||||||
played: boolean = false;
|
played: boolean = false;
|
||||||
current!: Pitch|Chord|ZRest;
|
current!: Pitch|Chord|ZRest;
|
||||||
retro: boolean = false;
|
retro: boolean = false;
|
||||||
tick: number = 0;
|
index: number = -1;
|
||||||
|
|
||||||
constructor(input: string, options: object, public app: Editor) {
|
constructor(input: string, options: object, public app: Editor) {
|
||||||
super(app);
|
super(app);
|
||||||
@ -22,6 +24,22 @@ export class Player extends Event {
|
|||||||
this.ziffers = new Ziffers(input, options);
|
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 {
|
notStarted(): boolean {
|
||||||
return this.ziffers.notStarted();
|
return this.ziffers.notStarted();
|
||||||
}
|
}
|
||||||
@ -36,21 +54,38 @@ export class Player extends Event {
|
|||||||
return this.app.clock.convertPulseToSecond(pulse);
|
return this.app.clock.convertPulseToSecond(pulse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if it's time to play the event
|
||||||
areWeThereYet = (): boolean => {
|
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 = (
|
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.current &&
|
||||||
this.pulseToSecond(this.app.api.epulse()+1) >=
|
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())
|
(this.current.duration*4) * this.pulseToSecond(this.app.api.ppqn())
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
if(howAboutNow) {
|
|
||||||
this.tick = 0;
|
// Increment index of how many times sound/midi have been called
|
||||||
} else {
|
this.index = howAboutNow ? this.index+1 : this.index;
|
||||||
this.tick++;
|
|
||||||
|
if(howAboutNow && this.notStarted()) {
|
||||||
|
this.firstCallTime = this.app.clock.pulses_since_origin+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return howAboutNow;
|
return howAboutNow;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,12 +134,33 @@ export class Player extends Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
retrograde() {
|
retrograde() {
|
||||||
if(this.tick === 0 && this.ziffers.index === 0) {
|
if(this.index === -1 && this.ziffers.index === -1) {
|
||||||
this.ziffers.retrograde();
|
this.ziffers.retrograde();
|
||||||
}
|
}
|
||||||
return this;
|
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 => {
|
out = (): void => {
|
||||||
// TODO?
|
// TODO?
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1446,10 +1446,10 @@ yaml@^2.1.1:
|
|||||||
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.3.1.tgz#02fe0975d23cd441242aa7204e09fc28ac2ac33b"
|
resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.3.1.tgz#02fe0975d23cd441242aa7204e09fc28ac2ac33b"
|
||||||
integrity sha512-2eHWfjaoXgTBC2jNM1LRef62VQa0umtvRiDSk6HSzW7RvS5YtkabJrwYLLEKWBc8a5U2PTSCs+dJjUTJdlHsWQ==
|
integrity sha512-2eHWfjaoXgTBC2jNM1LRef62VQa0umtvRiDSk6HSzW7RvS5YtkabJrwYLLEKWBc8a5U2PTSCs+dJjUTJdlHsWQ==
|
||||||
|
|
||||||
zifferjs@^0.0.13:
|
zifferjs@^0.0.14:
|
||||||
version "0.0.13"
|
version "0.0.14"
|
||||||
resolved "https://registry.yarnpkg.com/zifferjs/-/zifferjs-0.0.13.tgz#af155633357c95da6a4e5aaa84f23013b5574236"
|
resolved "https://registry.yarnpkg.com/zifferjs/-/zifferjs-0.0.14.tgz#7876c799a08e799be7af22b65f4cb6f0b44f79ca"
|
||||||
integrity sha512-eNOQOn+NM4L3v2FqQEf0RSiJOKiZMaotGLGj1VBCPHi5WhHp3N61R7k9ZrnQKhPnfSI80NBoplhQ1Q1sdEjFlQ==
|
integrity sha512-CpS3zTm8Btm8aTxd7sSUgVCF/S/jJ3hqwgp7uRzbZI8k6yJWhzo/rjMlEZoOmeBhs7Qy4XsVk7pfrLdS8AAIVA==
|
||||||
|
|
||||||
zzfx@^1.2.0:
|
zzfx@^1.2.0:
|
||||||
version "1.2.0"
|
version "1.2.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user