Fix some clock properties, start cleaning up

This commit is contained in:
2024-04-20 02:44:35 +02:00
parent 0b09f16624
commit 605db460e5
4 changed files with 21 additions and 27 deletions

View File

@ -53,15 +53,15 @@ export const beat = (app: Editor) => (n: number | number[] = 1, nudge: number =
const nArray = Array.isArray(n) ? n : [n];
const results: boolean[] = nArray.map(
(value) =>
(app.clock.pulses_since_origin - Math.round(nudge * app.clock.ppqn)) %
Math.round(value * app.clock.ppqn) === 0,
(app.clock.time_position.grain - Math.round(nudge * app.clock.time_position.ppqn)) %
Math.round(value * app.clock.time_position.ppqn) === 0,
);
return results.some((value) => value === true);
};
export const bar = (app: Editor) => (n: number | number[] = 1, nudge: number = 0): boolean => {
const nArray = Array.isArray(n) ? n : [n];
const barLength = (app.clock.time_signature?.[1] ?? 4) * app.clock.ppqn;
const barLength = app.clock.time_position.num * app.clock.ppqn;
const nudgeInPulses = Math.floor(nudge * barLength);
const results: boolean[] = nArray.map(
(value) =>
@ -82,7 +82,7 @@ export const pulse = (app: Editor) => (n: number | number[] = 1, nudge: number =
export const tick = (app: Editor) => (tick: number | number[], offset: number = 0): boolean => {
const nArray = Array.isArray(tick) ? tick : [tick];
const results: boolean[] = nArray.map(
(value) => app.clock.time_position.pulse === value + offset,
(value) => app.clock.time_position.tick === value + offset,
);
return results.some((value) => value === true);
};
@ -111,7 +111,7 @@ export const flipbar = (app: Editor) => (chunk: number = 1): boolean => {
export const onbar = (app: Editor) => (
bars: number[] | number,
n: number = app.clock.time_signature[0] || 4,
n: number = app.clock.time_position.num,
): boolean => {
let current_bar = (app.clock.time_position.bar % n) + 1;
return typeof bars === "number"

View File

@ -3,14 +3,9 @@ import TransportProcessor from "./ClockProcessor?worker&url";
import { Editor } from "../main";
export interface TimePosition {
bpm: number;
ppqn: number;
time: number;
tick: number;
beat: number;
bar: number;
num: number;
den: number;
bpm: number; ppqn: number; time: number;
tick: number; beat: number; bar: number;
num: number; den: number; grain: number;
}
export class Clock {
@ -31,6 +26,7 @@ export class Clock {
bar: 0,
num: 0,
den: 0,
grain: 0,
};
this.transportNode = null;
this.ctx = ctx;
@ -46,6 +42,10 @@ export class Clock {
});
}
get grain(): number {
return this.time_position.grain;
}
get ticks_before_new_bar(): number {
/**
* This function returns the number of ticks separating the current moment
@ -53,9 +53,9 @@ export class Clock {
*
* @returns number of ticks until next bar
*/
const ticksMissingFromBeat = this.ppqn - this.time_position.tick;
const ticksMissingFromBeat = this.time_position.ppqn - this.time_position.tick;
const beatsMissingFromBar = this.beats_per_bar - this.time_position.beat;
return beatsMissingFromBar * this.ppqn + ticksMissingFromBeat;
return beatsMissingFromBar * this.time_position.ppqn + ticksMissingFromBeat;
}
get next_beat_in_ticks(): number {
@ -65,7 +65,7 @@ export class Clock {
*
* @returns number of ticks until next beat
*/
return this.app.clock.pulses_since_origin + this.time_position.tick;
return this.time_position.grain + this.time_position.tick;
}
get beats_per_bar(): number {
@ -84,15 +84,6 @@ export class Clock {
return Math.floor(this.time_position.tick / this.ppqn)
}
get pulses_since_origin(): number {
/**
* Returns the number of pulses since the origin.
*
* @returns number of pulses since origin
*/
return this.time_position.tick;
}
get pulse_duration(): number {
/**
* Returns the duration of a pulse in seconds.
@ -169,6 +160,7 @@ export class Clock {
}
public stop(): void {
this.app.api.MidiConnection.sendStopMessage();
this.transportNode?.stop()
}

View File

@ -23,6 +23,7 @@ export class ClockNode extends AudioWorkletNode {
bar: message.data.bar,
num: message.data.num,
den: message.data.den,
grain: message.data.grain,
}
this.app.settings.send_clock ?? this.app.api.MidiConnection.sendMidiClock();
tryEvaluate(

View File

@ -1,5 +1,3 @@
import { blinkScript } from "../DOM/Visuals/Blinkers";
class TransportProcessor extends AudioWorkletProcessor {
constructor(options) {
super(options);
@ -13,6 +11,7 @@ class TransportProcessor extends AudioWorkletProcessor {
this.currentPulsePosition = 0;
this.startTime = 0;
this.pauseTime = 0;
this.grain = 0;
this.totalPauseTime = 0;
}
@ -59,6 +58,7 @@ class TransportProcessor extends AudioWorkletProcessor {
const currentPulsePosition = Math.round(beatNumber * this.ppqn);
if (currentPulsePosition > this.currentPulsePosition) {
this.grain += 1;
this.currentPulsePosition = currentPulsePosition;
// Calculate current tick, beat, and bar
@ -81,6 +81,7 @@ class TransportProcessor extends AudioWorkletProcessor {
bpm: this.bpm,
num: this.timeSignature[0],
den: this.timeSignature[1],
grain: this.grain,
});
}
}