-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
371 lines (342 loc) · 15.7 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import { importedMessage } from './src/ts/imported';
import { Clicks, ClicksBase, ClickPayload, MetronomeClicks } from './clicks'
import { ClicksBuilder } from './clicksBuilder';
import { SynctrackTempo, defaultTempo } from "./src/ts/schemas/music/tempo";
import { tempoToHeartbeat, heartbeatToTempo } from "./src/ts/utils/tempo-conversion";
importedMessage();
interface DebugClickRange {
id: string;
start: number;
end: number;
}
let debugClickRanges: Array<DebugClickRange> = [{
id: "",
start: 0,
end: 0,
}];
let debugClickIndex = 0;
export function setDebugClickbaseRanges(debugClickRangesParm: Array<DebugClickRange>): void {
debugClickRanges = debugClickRangesParm;
}
export const quarternotes44Label = "4/4 quarter notes";
export const eighthnotes44Label = "eighth notes";
export const rondoMelodyLabel = "Rondo Melody First Measure";
export const rondoLeftHandLabel = "Rondo Left Hand";
export const hemiolaTripletsLabel = "3 against 2 triplets";
export const hemiolaDupletsLabel = "3 against 2 duplets";
export function getQuarterNotesBuilder_4_4(): Array<ClicksBuilder> {
const builder4_4QuarterNotes = new ClicksBuilder()
.withId(quarternotes44Label)
.withTempos(60);
return [
builder4_4QuarterNotes
];
}
export function getEighthNotesEmphasisBuilder_4_4(): Array<ClicksBuilder> {
const builder4_4QuarterNotes = new ClicksBuilder()
.withId(quarternotes44Label)
.withTempos(60);
const builderEighthNotes = new ClicksBuilder()
.withId(eighthnotes44Label)
.withTempos(120)
.withNumberOfClicksPerClickGroup(2);
return [
builder4_4QuarterNotes,
builderEighthNotes
]
}
export function getBlueRondoALaTurkFirstMeasureBuilder(): Array<ClicksBuilder> {
const rondoMelody = new ClicksBuilder()
.withId(rondoMelodyLabel)
.withTempos(260)
.withClickRepeats([3, 1])
.withNumberOfClicksPerClickGroup([2, 3]);
const rondoLeftHand = new ClicksBuilder()
.withId(rondoLeftHandLabel)
.withTempos([ 130, 65 ])
.withClickRepeats([ 3, 1 ]);
return [
rondoMelody,
rondoLeftHand
]
}
export function getHemiolaBuilder(): Array<ClicksBuilder> {
const triplets = new ClicksBuilder()
.withId(hemiolaTripletsLabel)
.withTempos(180)
.withNumberOfClicksPerClickGroup(3)
const duplets = new ClicksBuilder()
.withId(hemiolaDupletsLabel)
.withTempos(120)
.withNumberOfClicksPerClickGroup(2)
return [
triplets,
duplets
]
}
export type ClickPayloadCallback = (clicksPayload: ClickPayload) => void;
export const dummyCallback: ClickPayloadCallback = ((callback: ClickPayload) => {});
function completeStartParameter(callbacks: Array<ClickPayloadCallback>, partialBuilder: () => Array<ClicksBuilder>): MetronomeClicks {
const returnPartialBuilder = partialBuilder();
const returnObjects: Array<Clicks> = [];
for (let callbackIndex = 0; callbackIndex < callbacks.length; ++callbackIndex) {
returnObjects.push(returnPartialBuilder[callbackIndex]
.withCallback(callbacks[callbackIndex])
.build());
}
return {
clicks: returnObjects
} as MetronomeClicks;
}
export function buildDummyCallbacks(numberOfMetronomes: number): Array<ClickPayloadCallback> {
const callbacks: Array<ClickPayloadCallback> = [];
for (let index = 0; index < numberOfMetronomes; ++index) {
callbacks.push(dummyCallback);
}
return callbacks;
}
export function getQuarternotesStartParameter(callbacks = buildDummyCallbacks(1)): MetronomeClicks {
return completeStartParameter(callbacks, getQuarterNotesBuilder_4_4);
}
export function getEighthNotesEmphasis_4_4StartParameter(callbacks = buildDummyCallbacks(2)): MetronomeClicks {
return completeStartParameter(callbacks, getEighthNotesEmphasisBuilder_4_4);
}
export function getBlueRondoALaTurkSFirstMeasureStartParameter(callbacks = buildDummyCallbacks(2)): MetronomeClicks {
return completeStartParameter(callbacks, getBlueRondoALaTurkFirstMeasureBuilder);
}
export function getHemiolaStartParameter(callbacks = buildDummyCallbacks(2)): MetronomeClicks {
return completeStartParameter(callbacks, getHemiolaBuilder);
}
const quarterNotes_4_4: MetronomeClicks = {
clicks: [
getQuarterNotesBuilder_4_4()[0]
.build()
]
};
export function start(clicks = quarterNotes_4_4): void {
debugClickIndex = 0;
if (Array.isArray(clicks.clicks)) {
clicks.clicks.forEach((click) => {
if (click.click) {
fullResetBookmarkAttributes(click);
flashTrigger(click);
}
});
} else {
fullResetBookmarkAttributes(clicks.clicks);
flashTrigger(clicks.clicks);
}
}
export async function stop(clicks = quarterNotes_4_4): Promise<void> {
return new Promise<void>((resolve) => {
if (setTimeoutHandle) {
try {
clearTimeout(setTimeoutHandle);
} catch(e) {
try {
clearTimeout(setTimeoutHandle);
} catch(ee) {
console.error(`Did not clear timeout: ${JSON.stringify(ee)}`, ee);
}
}
setTimeoutHandle = undefined;
}
resetClick(clicks);
resolve();
});
}
export function getClickBuilder(): ClicksBuilder {
return new ClicksBuilder();
}
export function getSynctrackTempo(tempo = defaultTempo): SynctrackTempo {
return {
tempo: tempo,
heartbeat: tempoToHeartbeat(tempo)
};
}
export function getSynctrackHeartbeat(heartbeat = tempoToHeartbeat(defaultTempo)): SynctrackTempo {
return {
tempo: heartbeatToTempo(heartbeat),
heartbeat: heartbeat
};
}
function resetBookmarkAttributes(clickbase: ClicksBase): ClicksBase {
clickbase.tempoHeartbeatIndex = 0;
clickbase.clickRepeatIndex = 0;
clickbase.clickGroupIndex = 0;
clickbase.clickPerClickGroupIndex = 0;
clickbase.delayIndex = 0;
return clickbase;
}
function fullResetBookmarkAttributes(clickbase: ClicksBase): ClicksBase {
resetBookmarkAttributes(clickbase);
clickbase.clickIndex = 0;
clickbase.groupRepeatIndex = 0;
return clickbase;
}
let setTimeoutHandle: NodeJS.Timeout | undefined = undefined;
function flashTrigger(clicks: Clicks, firsttime = true): void { // NOSONAR
if (clicks.click) {
const clickPayload = getClickPayload(clicks);
const delayElapsed = getDelay(clicks);
if (delayElapsed.delay === 0) {
clicks.click(clickPayload);
}
delayAndUpdateClicks(clickPayload);
}
function getClickPayload(clickbase: ClicksBase): ClickPayload {
const clickPayload: ClickPayload = {
id: clickbase.id,
clickIndex: clickbase.clickIndex,
duration: Array.isArray(clickbase.heartbeats) ? clickbase.heartbeats[clickbase.tempoHeartbeatIndex] : clickbase.heartbeats
}
return clickPayload;
}
interface DelayElapsed {
delay: number;
elapsed: number;
}
function getDelay(clicksParm: Clicks): DelayElapsed {
let delayElapsedTally = { delay: Array.isArray(clicksParm.delays) ? clicksParm.delays[clicksParm.delayIndex] : clicksParm.delays, elapsed: 0 };
let delayElapsed = getCurrentDelay(delayElapsedTally, clicksParm);
if (clicksParm.subclicks) {
clicksParm.subclicks.forEach((click) => {
delayElapsed = getCurrentDelay(delayElapsed, click);
});
}
return delayElapsed;
function getCurrentDelay(delayElapsedTallyParm: DelayElapsed, subclicksParm: ClicksBase): DelayElapsed {
let delay = Number.MAX_VALUE;
let currentDelay = Array.isArray(subclicksParm.delays) ? subclicksParm.delays[subclicksParm.delayIndex] : subclicksParm.delays;
let elapsedDuration = delayElapsedTallyParm.elapsed;
if (currentDelay < delay) {
delay = currentDelay;
if (currentDelay > elapsedDuration) {
elapsedDuration = currentDelay;
}
}
return { delay: delay, elapsed: elapsedDuration };
}
}
function delayAndUpdateClicks(clickPayloadParm: ClickPayload): void {
if (isDebugClickInRange(clickPayloadParm.id)) {
console.log(`delayAndUpdateClicks - clickPayloadParm: ${JSON.stringify(clickPayloadParm, null, 4)}`);
console.log();
}
setTimeoutHandle = setTimeout(() => {
updateIndices(clicks, firsttime);
if (clicks.subclicks) {
clicks.subclicks.forEach((subclick) => {
updateIndices(subclick, firsttime);
});
}
flashTrigger(clicks, false);
}, clickPayloadParm.duration);
function updateIndices(clickbase: ClicksBase, firsttime: boolean): void {
/*
* Blue Rondo A La Turk
*
* A F A F A F E F G - melody
* 0 1 2 3 4 5 6 7 8 - click index
* 0 1 0 1 0 1 0 1 2 - clicks per click group index
* 0 0 0 0 - clicks per group index
* 0 1 2 0 - clicks repeat
* 260 260 260 260 - tempos
*
* quarter quarter quarter dotted-quarter - left hand
* 0 1 2 3 - click index
* 0 0 0 0 - clicks per click group index
* 0 0 0 1 - clicks per group index
* 0 1 2 0 - clicks repeat
* 130 130 130 65 - tempos
*/
const temposHeartbeatsArray = Array.isArray(clickbase.temposHeartbeats) ? clickbase.temposHeartbeats : [ clickbase.temposHeartbeats ];
const temposArray = Array.isArray(clickbase.tempos) ? clickbase.tempos : [ clickbase.tempos ];
const heartbeatsArray = Array.isArray(clickbase.heartbeats) ? clickbase.heartbeats : [ clickbase.heartbeats ];
const groupsRepeatArray = Array.isArray(clickbase.groupRepeats) ? clickbase.groupRepeats : [ clickbase.groupRepeats ];
const clickRepeatsArray = Array.isArray(clickbase.clickRepeats) ? clickbase.clickRepeats : [ clickbase.clickRepeats ];
const numberOfClicksPerGroupArray = Array.isArray(clickbase.numberOfClicksPerGroup) ? clickbase.numberOfClicksPerGroup : [ clickbase.numberOfClicksPerGroup ];
const numberOfClicksPerClickGroupArray = Array.isArray(clickbase.numberOfClicksPerClickGroup) ? clickbase.numberOfClicksPerClickGroup
: [ clickbase.numberOfClicksPerClickGroup ];
clickbase.tempoHeartbeatValue = temposHeartbeatsArray[clickbase.tempoHeartbeatIndex];
clickbase.tempoValue = temposArray[clickbase.tempoHeartbeatIndex];
clickbase.heartbeatValue = heartbeatsArray[clickbase.tempoHeartbeatIndex];
if (isDebugClickInRange(clickbase.id)) {
console.log(`${debugClickIndex}) start updateIndices - clickbase: ${JSON.stringify(clickbase, null, 4)}`);
}
let numberOfGroupRepeats = groupsRepeatArray[clickbase.groupRepeatIndex];
let numberOfClickRepeats = clickRepeatsArray[clickbase.clickRepeatIndex];
let numberOfClicksPerGroup = numberOfClicksPerGroupArray[clickbase.clickGroupIndex];
let numberOfClicksPerClickGroup = numberOfClicksPerClickGroupArray[clickbase.clickPerClickGroupIndex];
let numberOfClicks = numberOfClicksPerGroup * numberOfClicksPerClickGroup;
if (isDebugClickInRange(clickbase.id)) {
console.log();
console.log(`updateIndices -`);
console.log(` numberOfGroupRepeats: ${numberOfGroupRepeats}`);
console.log(` numberOfClickRepeats: ${numberOfClickRepeats}`);
console.log(` numberOfClicksPerGroup: ${numberOfClicksPerGroup}`);
console.log(` numberOfClicksPerClickGroup: ${numberOfClicksPerClickGroup}`);
console.log(` numberOfClicks: ${numberOfClicks}`);
console.log();
}
let clickIndex = clickbase.clickIndex;
clickPayloadParm.clickIndex = clickIndex;
clickbase.clickIndex = (clickbase.clickIndex + 1) % numberOfClicks;
if (clickIndex === 0 && !firsttime) {
resetBookmarkAttributes(clickbase);
clickbase.groupRepeatValue = (clickbase.groupRepeatValue + 1) % numberOfGroupRepeats;
}
let clickPerClickGroupValue = clickbase.clickPerClickGroupValue;
clickbase.clickPerClickGroupValue = (clickbase.clickPerClickGroupValue + 1) % numberOfClicksPerClickGroup;
if (clickPerClickGroupValue === 0) {
clickbase.clickPerClickGroupIndex = (clickbase.clickPerClickGroupIndex + 1) % numberOfClicksPerClickGroupArray.length;
let clickRepeatValue = clickbase.clickRepeatValue;
clickbase.clickRepeatValue = (clickbase.clickRepeatValue + 1) % numberOfClickRepeats;
if (clickRepeatValue === 0 && !firsttime) {
clickbase.clickRepeatIndex = (clickbase.clickRepeatIndex + 1) % clickRepeatsArray.length;
clickbase.tempoHeartbeatIndex = (clickbase.tempoHeartbeatIndex + 1) % temposHeartbeatsArray.length;
clickbase.tempoHeartbeatValue = temposHeartbeatsArray[clickbase.tempoHeartbeatIndex];
clickbase.tempoValue = temposArray[clickbase.tempoHeartbeatIndex];
clickbase.heartbeatValue = heartbeatsArray[clickbase.tempoHeartbeatIndex];
clickPayloadParm.duration = clickbase.heartbeatValue;
}
let clickPerGroupValue = clickbase.clickPerGroupValue;
clickbase.clickPerGroupValue = (clickbase.clickPerGroupValue + 1) % numberOfClicksPerGroup;
if (clickPerGroupValue === 0 && !firsttime) {
clickbase.clickGroupIndex = (clickbase.clickGroupIndex + 1) % numberOfClicksPerGroupArray.length;
clickbase.clickIndex = 0;
}
}
if (isDebugClickInRange(clickbase.id)) {
console.log(`end updateIndices - clickbase: ${JSON.stringify(clickbase, null, 4)}`);
console.log();
}
++debugClickIndex;
}
function isDebugClickInRange(id: string): boolean {
for (let range of debugClickRanges) {
const idAllowed = (range.id !== undefined && (range.id === id || range.id.length < 1)) || range.id === undefined;
const rangestartAllowed = (range.start !== undefined && (debugClickIndex >= range.start || range.start < 0)) || range.start === undefined;
const rangeendAllowed = (range.end !== undefined && (debugClickIndex < range.end || range.end < 0)) || range.end === undefined;
if (idAllowed && rangestartAllowed && rangeendAllowed) {
return true;
}
}
return false;
}
}
}
export function resetClick(payload: MetronomeClicks): void {
if (Array.isArray(payload.clicks)) {
payload.clicks.forEach((click, index) => {
(payload.clicks as Array<Clicks>)[index] = resetIndividualClick(click);
})
}
function resetIndividualClick(click: Clicks): Clicks {
click = new ClicksBuilder()
.withInputClicks(click)
.resetIndices()
.build();
return click;
}
}