Changed clock to use performance.now()
This commit is contained in:
@ -213,7 +213,7 @@ export class UserAPI {
|
|||||||
/**
|
/**
|
||||||
* @returns the current AudioContext time (wall clock)
|
* @returns the current AudioContext time (wall clock)
|
||||||
*/
|
*/
|
||||||
return this.app.audioContext.currentTime;
|
return this.app.clock.lastPulseAt;
|
||||||
};
|
};
|
||||||
|
|
||||||
public play = (): void => {
|
public play = (): void => {
|
||||||
@ -1595,7 +1595,7 @@ export class UserAPI {
|
|||||||
* @returns A sine wave between -1 and 1
|
* @returns A sine wave between -1 and 1
|
||||||
*/
|
*/
|
||||||
return (
|
return (
|
||||||
(Math.sin(this.app.clock.ctx.currentTime * Math.PI * 2 * freq) + offset) *
|
(Math.sin(this.app.clock.lastPulseAt * Math.PI * 2 * freq) + offset) *
|
||||||
times
|
times
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@ -1625,7 +1625,7 @@ export class UserAPI {
|
|||||||
* @see noise
|
* @see noise
|
||||||
*/
|
*/
|
||||||
return (
|
return (
|
||||||
(((this.app.clock.ctx.currentTime * freq) % 1) * 2 - 1 + offset) * times
|
(((this.app.clock.lastPulseAt * freq) % 1) * 2 - 1 + offset) * times
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
55
src/Clock.ts
55
src/Clock.ts
@ -35,9 +35,8 @@ export class Clock {
|
|||||||
* @param totalPauseTime - The total time the clock has been paused / stopped
|
* @param totalPauseTime - The total time the clock has been paused / stopped
|
||||||
* @param _nudge - The current nudge value
|
* @param _nudge - The current nudge value
|
||||||
*/
|
*/
|
||||||
|
lastPulseAt: number;
|
||||||
ctx: AudioContext;
|
afterEvaluation: number;
|
||||||
logicalTime: number;
|
|
||||||
private _bpm: number;
|
private _bpm: number;
|
||||||
time_signature: number[];
|
time_signature: number[];
|
||||||
time_position: TimePosition;
|
time_position: TimePosition;
|
||||||
@ -45,24 +44,21 @@ export class Clock {
|
|||||||
tick: number;
|
tick: number;
|
||||||
running: boolean;
|
running: boolean;
|
||||||
private timerWorker: Worker | null = null;
|
private timerWorker: Worker | null = null;
|
||||||
private timeAtStart: number;
|
|
||||||
_nudge: number;
|
_nudge: number;
|
||||||
|
|
||||||
timeviewer: HTMLElement;
|
timeviewer: HTMLElement;
|
||||||
|
|
||||||
constructor(public app: Editor, ctx: AudioContext) {
|
constructor(public app: Editor) {
|
||||||
this.timeviewer = document.getElementById("timeviewer")!;
|
this.timeviewer = document.getElementById("timeviewer")!;
|
||||||
this.time_position = { bar: 0, beat: 0, pulse: 0 };
|
this.time_position = { bar: 0, beat: 0, pulse: 0 };
|
||||||
this.time_signature = [4, 4];
|
this.time_signature = [4, 4];
|
||||||
this.logicalTime = 0;
|
this.lastPulseAt = 0;
|
||||||
|
this.afterEvaluation = 0;
|
||||||
this.tick = 0;
|
this.tick = 0;
|
||||||
this._bpm = 120;
|
this._bpm = 120;
|
||||||
this._ppqn = 48;
|
this._ppqn = 48;
|
||||||
this._nudge = 0;
|
this._nudge = 0;
|
||||||
this.ctx = ctx;
|
this.running = false;
|
||||||
this.running = true;
|
|
||||||
this.timeAtStart = ctx.currentTime;
|
|
||||||
this.initializeWorker();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private initializeWorker(): void {
|
private initializeWorker(): void {
|
||||||
@ -111,22 +107,12 @@ export class Clock {
|
|||||||
* @returns void
|
* @returns void
|
||||||
*/
|
*/
|
||||||
if (this.running) {
|
if (this.running) {
|
||||||
const adjustedCurrentTime = this.ctx.currentTime + this._nudge / 1000;
|
this.lastPulseAt = performance.now();
|
||||||
const beatNumber = adjustedCurrentTime / (60 / this._bpm);
|
|
||||||
const currentPulsePosition = Math.ceil(beatNumber * this._ppqn);
|
|
||||||
|
|
||||||
if (currentPulsePosition > this.time_position.pulse) {
|
|
||||||
const futureTimeStamp = this.convertTicksToTimeposition(this.tick);
|
const futureTimeStamp = this.convertTicksToTimeposition(this.tick);
|
||||||
this.app.clock.incrementTick(this.bpm);
|
this.time_position = futureTimeStamp;
|
||||||
|
|
||||||
this.time_position.pulse = currentPulsePosition;
|
|
||||||
|
|
||||||
if (this.app.settings.send_clock) {
|
if (this.app.settings.send_clock) {
|
||||||
if (futureTimeStamp.pulse % 2 == 0)
|
|
||||||
// TODO: Why?
|
|
||||||
this.app.api.MidiConnection.sendMidiClock();
|
this.app.api.MidiConnection.sendMidiClock();
|
||||||
}
|
}
|
||||||
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)}:${
|
this.timeviewer.innerHTML = `${zeroPad(futureTimeStamp.bar, 2)}:${
|
||||||
futureTimeStamp.beat + 1
|
futureTimeStamp.beat + 1
|
||||||
@ -137,7 +123,9 @@ export class Clock {
|
|||||||
} else {
|
} else {
|
||||||
tryEvaluate(this.app, this.app.global_buffer);
|
tryEvaluate(this.app, this.app.global_buffer);
|
||||||
}
|
}
|
||||||
}
|
this.afterEvaluation = performance.now();
|
||||||
|
console.log("DEVIATION", this.deviation);
|
||||||
|
this.tick++;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -297,7 +285,7 @@ export class Clock {
|
|||||||
* @returns current time of the audio context
|
* @returns current time of the audio context
|
||||||
* @remark This is the time of the audio context, not the time of the clock.
|
* @remark This is the time of the audio context, not the time of the clock.
|
||||||
*/
|
*/
|
||||||
return this.app.audioContext.currentTime;
|
return this.lastPulseAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
get deviation(): number {
|
get deviation(): number {
|
||||||
@ -306,7 +294,7 @@ export class Clock {
|
|||||||
*
|
*
|
||||||
* @returns deviation between the logical time and the real time
|
* @returns deviation between the logical time and the real time
|
||||||
*/
|
*/
|
||||||
return this.logicalTime - this.realTime;
|
return (this.afterEvaluation - this.lastPulseAt) / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
set ppqn(ppqn: number) {
|
set ppqn(ppqn: number) {
|
||||||
@ -321,17 +309,6 @@ export class Clock {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public incrementTick(bpm: number) {
|
|
||||||
/**
|
|
||||||
* Increments the tick by one.
|
|
||||||
*
|
|
||||||
* @param bpm - beats per minute
|
|
||||||
* @returns void
|
|
||||||
*/
|
|
||||||
this.tick++;
|
|
||||||
this.logicalTime += this.pulse_duration_at_bpm(bpm);
|
|
||||||
}
|
|
||||||
|
|
||||||
public nextTickFrom(time: number, nudge: number): number {
|
public nextTickFrom(time: number, nudge: number): number {
|
||||||
/**
|
/**
|
||||||
* Compute the time remaining before the next clock tick.
|
* Compute the time remaining before the next clock tick.
|
||||||
@ -369,15 +346,15 @@ export class Clock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.running = true;
|
this.running = true;
|
||||||
this.app.audioContext.resume();
|
|
||||||
this.app.api.MidiConnection.sendStartMessage();
|
this.app.api.MidiConnection.sendStartMessage();
|
||||||
|
this.lastPulseAt = 0;
|
||||||
|
this.afterEvaluation = 0;
|
||||||
|
|
||||||
if (!this.timerWorker) {
|
if (!this.timerWorker) {
|
||||||
this.initializeWorker();
|
this.initializeWorker();
|
||||||
}
|
}
|
||||||
this.setWorkerInterval();
|
this.setWorkerInterval();
|
||||||
this.timeAtStart = this.ctx.currentTime;
|
|
||||||
this.logicalTime = this.timeAtStart;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public pause(): void {
|
public pause(): void {
|
||||||
|
|||||||
@ -449,8 +449,7 @@ export class SoundEvent extends AudibleEvent {
|
|||||||
const filteredEvent = event;
|
const filteredEvent = event;
|
||||||
// No need for note if there is freq
|
// No need for note if there is freq
|
||||||
if (filteredEvent.freq) { delete filteredEvent.note; }
|
if (filteredEvent.freq) { delete filteredEvent.note; }
|
||||||
// const correction = Math.max(this.nudge - this.app.clock.deviation, 0);
|
superdough(filteredEvent, this.nudge - this.app.clock.deviation, filteredEvent.dur);
|
||||||
superdough(filteredEvent, this.nudge, filteredEvent.dur);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -125,7 +125,7 @@ export class Editor {
|
|||||||
// ================================================================================
|
// ================================================================================
|
||||||
|
|
||||||
this.audioContext = new AudioContext({ latencyHint: "playback" });
|
this.audioContext = new AudioContext({ latencyHint: "playback" });
|
||||||
this.clock = new Clock(this, this.audioContext);
|
this.clock = new Clock(this);
|
||||||
|
|
||||||
// ================================================================================
|
// ================================================================================
|
||||||
// User API
|
// User API
|
||||||
|
|||||||
Reference in New Issue
Block a user