cleaning clock file a bit
This commit is contained in:
97
src/Clock.ts
97
src/Clock.ts
@ -33,9 +33,6 @@ export class Clock {
|
|||||||
* @param ppqn - The pulses per quarter note
|
* @param ppqn - The pulses per quarter note
|
||||||
* @param tick - The current tick since origin
|
* @param tick - The current tick since origin
|
||||||
* @param running - Is the clock running?
|
* @param running - Is the clock running?
|
||||||
* @param lastPauseTime - The last time the clock was paused
|
|
||||||
* @param lastPlayPressTime - The last time the clock was started
|
|
||||||
* @param totalPauseTime - The total time the clock has been paused / stopped
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
clock: any;
|
clock: any;
|
||||||
@ -47,9 +44,6 @@ export class Clock {
|
|||||||
private _ppqn: number;
|
private _ppqn: number;
|
||||||
tick: number;
|
tick: number;
|
||||||
running: boolean;
|
running: boolean;
|
||||||
lastPauseTime: number;
|
|
||||||
lastPlayPressTime: number;
|
|
||||||
totalPauseTime: number;
|
|
||||||
timeviewer: HTMLElement;
|
timeviewer: HTMLElement;
|
||||||
deadline: number;
|
deadline: number;
|
||||||
|
|
||||||
@ -65,10 +59,7 @@ export class Clock {
|
|||||||
this._ppqn = 48;
|
this._ppqn = 48;
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.running = true;
|
this.running = true;
|
||||||
this.lastPauseTime = 0;
|
|
||||||
this.deadline = 0;
|
this.deadline = 0;
|
||||||
this.lastPlayPressTime = 0;
|
|
||||||
this.totalPauseTime = 0;
|
|
||||||
this.timeviewer = document.getElementById("timeviewer")!;
|
this.timeviewer = document.getElementById("timeviewer")!;
|
||||||
this.clock = getAudioContext().createClock(this.clockCallback, this.pulse_duration)
|
this.clock = getAudioContext().createClock(this.clockCallback, this.pulse_duration)
|
||||||
}
|
}
|
||||||
@ -102,12 +93,6 @@ export class Clock {
|
|||||||
};
|
};
|
||||||
|
|
||||||
convertTicksToTimeposition(ticks: number): TimePosition {
|
convertTicksToTimeposition(ticks: number): TimePosition {
|
||||||
/**
|
|
||||||
* Converts ticks to a TimePosition object.
|
|
||||||
* @param ticks The number of ticks to convert.
|
|
||||||
* @returns The TimePosition object representing the converted ticks.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const beatsPerBar = this.app.clock.time_signature[0];
|
const beatsPerBar = this.app.clock.time_signature[0];
|
||||||
const ppqnPosition = ticks % this.app.clock.ppqn;
|
const ppqnPosition = ticks % this.app.clock.ppqn;
|
||||||
const beatNumber = Math.floor(ticks / this.app.clock.ppqn);
|
const beatNumber = Math.floor(ticks / this.app.clock.ppqn);
|
||||||
@ -117,73 +102,39 @@ export class Clock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get ticks_before_new_bar(): number {
|
get ticks_before_new_bar(): number {
|
||||||
/**
|
|
||||||
* This function returns the number of ticks separating the current moment
|
|
||||||
* from the beginning of the next bar.
|
|
||||||
*
|
|
||||||
* @returns number of ticks until next bar
|
|
||||||
*/
|
|
||||||
const ticskMissingFromBeat = this.ppqn - this.time_position.pulse;
|
const ticskMissingFromBeat = this.ppqn - this.time_position.pulse;
|
||||||
const beatsMissingFromBar = this.beats_per_bar - this.time_position.beat;
|
const beatsMissingFromBar = this.beats_per_bar - this.time_position.beat;
|
||||||
return beatsMissingFromBar * this.ppqn + ticskMissingFromBeat;
|
return beatsMissingFromBar * this.ppqn + ticskMissingFromBeat;
|
||||||
}
|
}
|
||||||
|
|
||||||
get next_beat_in_ticks(): number {
|
get next_beat_in_ticks(): number {
|
||||||
/**
|
return this.app.clock.pulses_since_origin + this.time_position.pulse;
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
return this.app.clock.pulses_since_origin + this.time_position.pulse;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get beats_per_bar(): number {
|
get beats_per_bar(): number {
|
||||||
/**
|
return this.time_signature[0];
|
||||||
* Returns the number of beats per bar.
|
|
||||||
*/
|
|
||||||
return this.time_signature[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get beats_since_origin(): number {
|
get beats_since_origin(): number {
|
||||||
/**
|
return Math.floor(this.tick / this.ppqn);
|
||||||
* Returns the number of beats since the origin.
|
|
||||||
*
|
|
||||||
* @returns number of beats since origin
|
|
||||||
*/
|
|
||||||
return Math.floor(this.tick / this.ppqn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get pulses_since_origin(): number {
|
get pulses_since_origin(): number {
|
||||||
/**
|
return this.tick;
|
||||||
* Returns the number of pulses since the origin.
|
|
||||||
*
|
|
||||||
* @returns number of pulses since origin
|
|
||||||
*/
|
|
||||||
return this.tick;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get pulse_duration(): number {
|
get pulse_duration(): number {
|
||||||
/**
|
return 60 / this.bpm / this.ppqn;
|
||||||
* Returns the duration of a pulse in seconds.
|
|
||||||
*/
|
|
||||||
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 {
|
||||||
/**
|
return 60 / bpm / this.ppqn;
|
||||||
* Returns the duration of a pulse in seconds at a specific bpm.
|
|
||||||
*/
|
|
||||||
return 60 / bpm / this.ppqn;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get bpm(): number {
|
get bpm(): number {
|
||||||
return this._bpm;
|
return this._bpm;
|
||||||
}
|
}
|
||||||
|
|
||||||
set nudge(nudge: number) {
|
|
||||||
}
|
|
||||||
|
|
||||||
get tickDuration() {
|
get tickDuration() {
|
||||||
return 1 / this.ppqn;
|
return 1 / this.ppqn;
|
||||||
}
|
}
|
||||||
@ -200,34 +151,18 @@ export class Clock {
|
|||||||
return this._ppqn;
|
return this._ppqn;
|
||||||
}
|
}
|
||||||
|
|
||||||
get realTime(): number {
|
|
||||||
return this.app.audioContext.currentTime - this.totalPauseTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
get deviation(): number {
|
|
||||||
return Math.abs(this.logicalTime - this.realTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public incrementTick(bpm: number) {
|
public incrementTick() {
|
||||||
this.tick++;
|
this.tick++;
|
||||||
this.logicalTime += this.pulse_duration_at_bpm(bpm);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public nextTickFrom(time: number, nudge: number): number {
|
public nextTickFrom(time: number, nudge: number): number {
|
||||||
/**
|
const pulseDuration = this.pulse_duration;
|
||||||
* Compute the time remaining before the next clock tick.
|
|
||||||
* @param time - audio context currentTime
|
|
||||||
* @param nudge - nudge in the future (in seconds)
|
|
||||||
* @returns remainingTime
|
|
||||||
*/
|
|
||||||
const pulseDuration = this.pulse_duration;
|
|
||||||
const nudgedTime = time + nudge;
|
const nudgedTime = time + nudge;
|
||||||
const nextTickTime = Math.ceil(nudgedTime / pulseDuration) * pulseDuration;
|
const nextTickTime = Math.ceil(nudgedTime / pulseDuration) * pulseDuration;
|
||||||
const remainingTime = nextTickTime - nudgedTime;
|
const remainingTime = nextTickTime - nudgedTime;
|
||||||
@ -236,9 +171,6 @@ export class Clock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public convertPulseToSecond(n: number): number {
|
public convertPulseToSecond(n: number): number {
|
||||||
/**
|
|
||||||
* Converts a pulse to a second.
|
|
||||||
*/
|
|
||||||
return n * this.pulse_duration;
|
return n * this.pulse_duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -251,21 +183,12 @@ export class Clock {
|
|||||||
this.app.audioContext.resume();
|
this.app.audioContext.resume();
|
||||||
this.running = true;
|
this.running = true;
|
||||||
this.app.api.MidiConnection.sendStartMessage();
|
this.app.api.MidiConnection.sendStartMessage();
|
||||||
this.lastPlayPressTime = this.app.audioContext.currentTime;
|
|
||||||
this.totalPauseTime += this.lastPlayPressTime - this.lastPauseTime;
|
|
||||||
this.clock.start()
|
this.clock.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
public pause(): void {
|
public pause(): void {
|
||||||
/**
|
|
||||||
* Pauses the TransportNode (pauses the clock).
|
|
||||||
*
|
|
||||||
* @remark also sends a MIDI message if a port is declared
|
|
||||||
*/
|
|
||||||
this.running = false;
|
this.running = false;
|
||||||
this.app.api.MidiConnection.sendStopMessage();
|
this.app.api.MidiConnection.sendStopMessage();
|
||||||
this.lastPauseTime = this.app.audioContext.currentTime;
|
|
||||||
this.logicalTime = this.realTime;
|
|
||||||
this.clock.pause()
|
this.clock.pause()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,10 +200,8 @@ export class Clock {
|
|||||||
*/
|
*/
|
||||||
this.running = false;
|
this.running = false;
|
||||||
this.tick = 0;
|
this.tick = 0;
|
||||||
this.lastPauseTime = this.app.audioContext.currentTime;
|
|
||||||
this.logicalTime = this.realTime;
|
|
||||||
this.time_position = { bar: 0, beat: 0, pulse: 0 };
|
this.time_position = { bar: 0, beat: 0, pulse: 0 };
|
||||||
this.app.api.MidiConnection.sendStopMessage();
|
this.app.api.MidiConnection.sendStopMessage();
|
||||||
this.clock.stop();
|
this.clock.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user