work in progress
This commit is contained in:
28
src/Clock.ts
28
src/Clock.ts
@ -44,6 +44,7 @@ export class Clock {
|
|||||||
running: boolean;
|
running: boolean;
|
||||||
private timerWorker: Worker | null = null;
|
private timerWorker: Worker | null = null;
|
||||||
private timeAtStart: number;
|
private timeAtStart: number;
|
||||||
|
nudge: number
|
||||||
|
|
||||||
timeviewer: HTMLElement
|
timeviewer: HTMLElement
|
||||||
|
|
||||||
@ -56,6 +57,7 @@ export class Clock {
|
|||||||
this.tick = 0;
|
this.tick = 0;
|
||||||
this._bpm = 120;
|
this._bpm = 120;
|
||||||
this._ppqn = 48;
|
this._ppqn = 48;
|
||||||
|
this.nudge = 0;
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.running = true;
|
this.running = true;
|
||||||
this.initializeWorker();
|
this.initializeWorker();
|
||||||
@ -66,7 +68,6 @@ export class Clock {
|
|||||||
const blob = new Blob([workerScript], { type: 'text/javascript' });
|
const blob = new Blob([workerScript], { type: 'text/javascript' });
|
||||||
this.timerWorker = new Worker(URL.createObjectURL(blob));
|
this.timerWorker = new Worker(URL.createObjectURL(blob));
|
||||||
this.timerWorker.onmessage = () => {
|
this.timerWorker.onmessage = () => {
|
||||||
// Handle tick update
|
|
||||||
this.run();
|
this.run();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -84,12 +85,22 @@ export class Clock {
|
|||||||
|
|
||||||
private run = () => {
|
private run = () => {
|
||||||
if (this.running) {
|
if (this.running) {
|
||||||
if (this.app.settings.send_clock) {
|
const adjustedCurrentTime = this.ctx.currentTime + (this.nudge / 1000);
|
||||||
this.app.api.MidiConnection.sendMidiClock();
|
const beatNumber = adjustedCurrentTime / (60 / this._bpm);
|
||||||
}
|
const currentPulsePosition = Math.ceil(beatNumber * this._ppqn);
|
||||||
|
|
||||||
|
if (currentPulsePosition > this.time_position.pulse) {
|
||||||
const futureTimeStamp = this.convertTicksToTimeposition(
|
const futureTimeStamp = this.convertTicksToTimeposition(
|
||||||
this.tick
|
this.tick
|
||||||
);
|
);
|
||||||
|
this.app.clock.incrementTick(this.bpm);
|
||||||
|
|
||||||
|
this.time_position.pulse = currentPulsePosition;
|
||||||
|
|
||||||
|
if (this.app.settings.send_clock) {
|
||||||
|
if (futureTimeStamp.pulse % 2 == 0) // TODO: Why?
|
||||||
|
this.app.api.MidiConnection.sendMidiClock();
|
||||||
|
}
|
||||||
this.time_position = futureTimeStamp;
|
this.time_position = futureTimeStamp;
|
||||||
if (futureTimeStamp.pulse % this.app.clock.ppqn == 0) {
|
if (futureTimeStamp.pulse % this.app.clock.ppqn == 0) {
|
||||||
this.timeviewer.innerHTML = `${zeroPad(futureTimeStamp.bar, 2)}:${futureTimeStamp.beat + 1
|
this.timeviewer.innerHTML = `${zeroPad(futureTimeStamp.bar, 2)}:${futureTimeStamp.beat + 1
|
||||||
@ -100,10 +111,9 @@ export class Clock {
|
|||||||
} else {
|
} else {
|
||||||
tryEvaluate(this.app, this.app.global_buffer);
|
tryEvaluate(this.app, this.app.global_buffer);
|
||||||
}
|
}
|
||||||
this.app.clock.incrementTick(this.bpm);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
convertTicksToTimeposition(ticks: number): TimePosition {
|
convertTicksToTimeposition(ticks: number): TimePosition {
|
||||||
const beatsPerBar = this.app.clock.time_signature[0];
|
const beatsPerBar = this.app.clock.time_signature[0];
|
||||||
@ -165,7 +175,7 @@ export class Clock {
|
|||||||
/**
|
/**
|
||||||
* Returns the duration of a pulse in seconds.
|
* Returns the duration of a pulse in seconds.
|
||||||
*/
|
*/
|
||||||
return 60 / this.bpm / this.ppqn;
|
return 60 / this._bpm / this.ppqn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public pulse_duration_at_bpm(bpm: number = this.bpm): number {
|
public pulse_duration_at_bpm(bpm: number = this.bpm): number {
|
||||||
@ -191,7 +201,6 @@ export class Clock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private restartWorker(): void {
|
private restartWorker(): void {
|
||||||
// Terminate the existing worker and start a new one with updated interval
|
|
||||||
if (this.timerWorker) {
|
if (this.timerWorker) {
|
||||||
this.timerWorker.terminate();
|
this.timerWorker.terminate();
|
||||||
}
|
}
|
||||||
@ -207,7 +216,6 @@ export class Clock {
|
|||||||
return this.app.audioContext.currentTime;
|
return this.app.audioContext.currentTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
get deviation(): number {
|
get deviation(): number {
|
||||||
return this.logicalTime - this.realTime;
|
return this.logicalTime - this.realTime;
|
||||||
}
|
}
|
||||||
@ -215,7 +223,6 @@ export class Clock {
|
|||||||
set ppqn(ppqn: number) {
|
set ppqn(ppqn: number) {
|
||||||
if (ppqn > 0 && this._ppqn !== ppqn) {
|
if (ppqn > 0 && this._ppqn !== ppqn) {
|
||||||
this._ppqn = ppqn;
|
this._ppqn = ppqn;
|
||||||
this.logicalTime = this.realTime;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,7 +279,6 @@ export class Clock {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public stop(): void {
|
public stop(): void {
|
||||||
/**
|
/**
|
||||||
* Stops the TransportNode (stops the clock).
|
* Stops the TransportNode (stops the clock).
|
||||||
|
|||||||
Reference in New Issue
Block a user