diff --git a/src/API.ts b/src/API.ts index c3835fe..8e447b6 100644 --- a/src/API.ts +++ b/src/API.ts @@ -213,7 +213,7 @@ export class UserAPI { /** * @returns the current AudioContext time (wall clock) */ - return this.app.audioContext.currentTime; + return this.app.clock.lastPulseAt; }; public play = (): void => { @@ -1595,7 +1595,7 @@ export class UserAPI { * @returns A sine wave between -1 and 1 */ 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 ); }; @@ -1625,7 +1625,7 @@ export class UserAPI { * @see noise */ return ( - (((this.app.clock.ctx.currentTime * freq) % 1) * 2 - 1 + offset) * times + (((this.app.clock.lastPulseAt * freq) % 1) * 2 - 1 + offset) * times ); }; diff --git a/src/Clock.ts b/src/Clock.ts index ded882a..b9f5fbc 100644 --- a/src/Clock.ts +++ b/src/Clock.ts @@ -35,9 +35,8 @@ export class Clock { * @param totalPauseTime - The total time the clock has been paused / stopped * @param _nudge - The current nudge value */ - - ctx: AudioContext; - logicalTime: number; + lastPulseAt: number; + afterEvaluation: number; private _bpm: number; time_signature: number[]; time_position: TimePosition; @@ -45,24 +44,21 @@ export class Clock { tick: number; running: boolean; private timerWorker: Worker | null = null; - private timeAtStart: number; _nudge: number; timeviewer: HTMLElement; - constructor(public app: Editor, ctx: AudioContext) { + constructor(public app: Editor) { this.timeviewer = document.getElementById("timeviewer")!; this.time_position = { bar: 0, beat: 0, pulse: 0 }; this.time_signature = [4, 4]; - this.logicalTime = 0; + this.lastPulseAt = 0; + this.afterEvaluation = 0; this.tick = 0; this._bpm = 120; this._ppqn = 48; this._nudge = 0; - this.ctx = ctx; - this.running = true; - this.timeAtStart = ctx.currentTime; - this.initializeWorker(); + this.running = false; } private initializeWorker(): void { @@ -111,22 +107,12 @@ export class Clock { * @returns void */ if (this.running) { - const adjustedCurrentTime = this.ctx.currentTime + this._nudge / 1000; - const beatNumber = adjustedCurrentTime / (60 / this._bpm); - const currentPulsePosition = Math.ceil(beatNumber * this._ppqn); - - if (currentPulsePosition > this.time_position.pulse) { + this.lastPulseAt = performance.now(); const futureTimeStamp = this.convertTicksToTimeposition(this.tick); - this.app.clock.incrementTick(this.bpm); - - this.time_position.pulse = currentPulsePosition; - + this.time_position = futureTimeStamp; if (this.app.settings.send_clock) { - if (futureTimeStamp.pulse % 2 == 0) - // TODO: Why? this.app.api.MidiConnection.sendMidiClock(); } - this.time_position = futureTimeStamp; if (futureTimeStamp.pulse % this.app.clock.ppqn == 0) { this.timeviewer.innerHTML = `${zeroPad(futureTimeStamp.bar, 2)}:${ futureTimeStamp.beat + 1 @@ -137,7 +123,9 @@ export class Clock { } else { 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 * @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 { @@ -306,7 +294,7 @@ export class Clock { * * @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) { @@ -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 { /** * Compute the time remaining before the next clock tick. @@ -369,15 +346,15 @@ export class Clock { } this.running = true; - this.app.audioContext.resume(); this.app.api.MidiConnection.sendStartMessage(); + this.lastPulseAt = 0; + this.afterEvaluation = 0; if (!this.timerWorker) { this.initializeWorker(); } this.setWorkerInterval(); - this.timeAtStart = this.ctx.currentTime; - this.logicalTime = this.timeAtStart; + } public pause(): void { diff --git a/src/classes/SoundEvent.ts b/src/classes/SoundEvent.ts index abe94e5..e7b0974 100644 --- a/src/classes/SoundEvent.ts +++ b/src/classes/SoundEvent.ts @@ -449,8 +449,7 @@ export class SoundEvent extends AudibleEvent { const filteredEvent = event; // No need for note if there is freq if (filteredEvent.freq) { delete filteredEvent.note; } - // const correction = Math.max(this.nudge - this.app.clock.deviation, 0); - superdough(filteredEvent, this.nudge, filteredEvent.dur); + superdough(filteredEvent, this.nudge - this.app.clock.deviation, filteredEvent.dur); } }; } diff --git a/src/main.ts b/src/main.ts index 7c0e824..618808c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -125,7 +125,7 @@ export class Editor { // ================================================================================ this.audioContext = new AudioContext({ latencyHint: "playback" }); - this.clock = new Clock(this, this.audioContext); + this.clock = new Clock(this); // ================================================================================ // User API