-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathMidiTickLookup.ts
441 lines (379 loc) · 16.9 KB
/
MidiTickLookup.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import { BeatTickLookup } from '@src/midi/BeatTickLookup';
import { MasterBarTickLookup } from '@src/midi/MasterBarTickLookup';
import { MidiUtils } from '@src/midi/MidiUtils';
import { Beat } from '@src/model/Beat';
import { MasterBar } from '@src/model/MasterBar';
/**
* Represents the results of searching the currently played beat.
* @see MidiTickLookup.FindBeat
*/
export class MidiTickLookupFindBeatResult {
/**
* Gets or sets the beat that is currently played and used for the start
* position of the cursor animation.
*/
public beat!: Beat;
/**
* Gets or sets the parent MasterBarTickLookup to which this beat lookup belongs to.
*/
public masterBar: MasterBarTickLookup;
/**
* Gets or sets the related beat tick lookup.
*/
public beatLookup!: BeatTickLookup;
/**
* Gets or sets the beat that will be played next.
*/
public nextBeat: MidiTickLookupFindBeatResult | null = null;
/**
* Gets or sets the duration in midi ticks how long this lookup is valid.
*/
public tickDuration: number = 0;
/**
* Gets or sets the duration in milliseconds how long this lookup is valid.
*/
public duration: number = 0;
public get start(): number {
return this.masterBar.start + this.beatLookup.start;
}
public get end(): number {
return this.start + this.tickDuration;
}
public constructor(masterBar: MasterBarTickLookup) {
this.masterBar = masterBar;
}
}
/**
* This class holds all information about when {@link MasterBar}s and {@link Beat}s are played.
*
* On top level it is organized into {@link MasterBarTickLookup} objects indicating the
* master bar start and end times. This information is used to highlight the currently played bars
* and it gives access to the played beats in this masterbar and their times.
*
* The {@link BeatTickLookup} are then the slices into which the masterbar is separated by the voices and beats
* of all tracks. An example how things are organized:
*
* Time (eighths): | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
*
* Track 1: | B1 | B2 | B3 | B4 | B5 | B6 |
* Track 2: | B7 | B7 | B9 | B10| B11| B12|
* Track 3: | B13 |
*
* Lookup: | L1 | L2 | L3 | L4 | L5 | L6 | L7 | L8 |
* Active Beats:
* - L1 B1,B7,B13
* - L2 B2,B7,B13
* - L3 B3,B7,B13
* - L4 B4,B7,B13
* - L5 B5,B9,B13
* - L6 B5,B10,B13
* - L7 B6,B11,B13
* - L8 B6,B12,B13
*
* Then during playback we build out of this list {@link MidiTickLookupFindBeatResult} objects which are sepcific
* to the visible tracks displayed. This is required because if only Track 2 is displayed we cannot use the the
* Lookup L1 alone to determine the start and end of the beat cursor. In this case we will derive a
* MidiTickLookupFindBeatResult which holds for Time 01 the lookup L1 as start and L3 as end. This will be used
* both for the cursor and beat highlighting.
*/
export class MidiTickLookup {
private _currentMasterBar: MasterBarTickLookup | null = null;
/**
* Gets a dictionary of all master bars played. The index is the index equals to {@link MasterBar.index}.
* This lookup only contains the first time a MasterBar is played. For a whole sequence of the song refer to {@link MasterBars}.
* @internal
*/
public readonly masterBarLookup: Map<number, MasterBarTickLookup> = new Map();
/**
* Gets a list of all {@link MasterBarTickLookup} sorted by time.
* @internal
*/
public readonly masterBars: MasterBarTickLookup[] = [];
/**
* Finds the currently played beat given a list of tracks and the current time.
* @param trackLookup The tracks indices in which to search the played beat for.
* @param tick The current time in midi ticks.
* @returns The information about the current beat or null if no beat could be found.
*/
public findBeat(
trackLookup: Set<number>,
tick: number,
currentBeatHint: MidiTickLookupFindBeatResult | null = null
): MidiTickLookupFindBeatResult | null {
let result: MidiTickLookupFindBeatResult | null = null;
if (currentBeatHint) {
result = this.findBeatFast(trackLookup, currentBeatHint, tick);
}
if (!result) {
result = this.findBeatSlow(trackLookup, currentBeatHint, tick, false);
}
return result;
}
private findBeatFast(
trackLookup: Set<number>,
currentBeatHint: MidiTickLookupFindBeatResult,
tick: number
): MidiTickLookupFindBeatResult | null {
// still within current lookup.
if (tick >= currentBeatHint.start && tick < currentBeatHint.end) {
return currentBeatHint;
}
// already on the next beat?
else if (
currentBeatHint.nextBeat &&
tick >= currentBeatHint.nextBeat.start &&
tick < currentBeatHint.nextBeat.end
) {
const next = currentBeatHint.nextBeat!;
// fill next in chain
this.fillNextBeat(next, trackLookup);
return next;
}
// likely a loop or manual seek, need to fallback to slow path
return null;
}
private fillNextBeat(current: MidiTickLookupFindBeatResult, trackLookup: Set<number>) {
current.nextBeat = this.findBeatInMasterBar(
current.masterBar,
current.beatLookup.nextBeat,
current.end, trackLookup, false, true);
if (current.nextBeat == null) {
current.nextBeat = this.findBeatSlow(trackLookup, current, current.end, true);
}
// if we have the next beat take the difference between the times as duration
if (current.nextBeat) {
current.tickDuration = current.nextBeat.start - current.start;
current.duration = MidiUtils.ticksToMillis(current.tickDuration, current.masterBar.tempo);
}
// no next beat, animate to the end of the bar (could be an incomplete bar)
if (!current.nextBeat) {
current.tickDuration = current.masterBar.end - current.start;
current.duration = MidiUtils.ticksToMillis(current.tickDuration, current.masterBar.tempo);
}
}
private findBeatSlow(trackLookup: Set<number>, currentBeatHint: MidiTickLookupFindBeatResult | null, tick: number, isNextSearch: boolean): MidiTickLookupFindBeatResult | null {
// get all beats within the masterbar
let masterBar: MasterBarTickLookup | null = null;
if (currentBeatHint != null) {
// same masterbar?
if (currentBeatHint.masterBar.start <= tick &&
currentBeatHint.masterBar.end > tick) {
masterBar = currentBeatHint.masterBar;
}
// next masterbar
else if (currentBeatHint.masterBar.nextMasterBar &&
currentBeatHint.masterBar.nextMasterBar.start <= tick &&
currentBeatHint.masterBar.nextMasterBar.end > tick
) {
masterBar = currentBeatHint.masterBar.nextMasterBar;
}
}
// slowest lookup
if (!masterBar) {
masterBar = this.findMasterBar(tick);
}
// no match
if (!masterBar) {
return null;
}
// scan through beats and find first one which has a beat visible
while (masterBar) {
if (masterBar.firstBeat) {
let beat = this.findBeatInMasterBar(
masterBar,
masterBar.firstBeat,
tick,
trackLookup,
true,
isNextSearch);
if (beat) {
return beat;
}
}
masterBar = masterBar.nextMasterBar;
}
return null;
}
/**
* Finds the beat at a given tick position within the known master bar.
* @param masterBar
* @param currentStartLookup
* @param tick
* @param visibleTracks
* @param fillNext
* @returns
*/
private findBeatInMasterBar(
masterBar: MasterBarTickLookup,
currentStartLookup: BeatTickLookup | null,
tick: number,
visibleTracks: Set<number>,
fillNext: boolean,
isNextSeach: boolean): MidiTickLookupFindBeatResult | null {
if (!currentStartLookup) {
return null;
}
let startBeatLookup: BeatTickLookup | null = null;
let startBeat: Beat | null = null;
const relativeTick = tick - masterBar.start;
while (currentStartLookup != null && startBeat == null) {
if (currentStartLookup.start <= relativeTick && relativeTick < currentStartLookup.end) {
startBeatLookup = currentStartLookup;
startBeat = currentStartLookup.getVisibleBeatAtStart(visibleTracks);
// found the matching beat lookup but none of the beats are visible
// in this case scan further to the next lookup which has any visible beat
if (!startBeat) {
if (isNextSeach) {
let currentMasterBar: MasterBarTickLookup | null = masterBar;
while (currentMasterBar != null && startBeat == null) {
while (currentStartLookup != null) {
startBeat = currentStartLookup.getVisibleBeatAtStart(visibleTracks);
if (startBeat) {
startBeatLookup = currentStartLookup;
masterBar = currentMasterBar;
break;
}
currentStartLookup = currentStartLookup.nextBeat;
}
if (!startBeat || !startBeatLookup) {
currentMasterBar = currentMasterBar.nextMasterBar;
currentStartLookup = currentMasterBar?.firstBeat ?? null;
}
}
} else {
let currentMasterBar: MasterBarTickLookup | null = masterBar;
while (currentMasterBar != null && startBeat == null) {
while (currentStartLookup != null) {
startBeat = currentStartLookup.getVisibleBeatAtStart(visibleTracks);
if (startBeat) {
startBeatLookup = currentStartLookup;
masterBar = currentMasterBar;
break;
}
currentStartLookup = currentStartLookup.previousBeat;
}
if (!startBeat || !startBeatLookup) {
currentMasterBar = currentMasterBar.previousMasterBar;
currentStartLookup = currentMasterBar?.firstBeat ?? null;
}
}
}
}
} else if (currentStartLookup.end > relativeTick) {
break;
}
currentStartLookup = currentStartLookup?.nextBeat ?? null;
}
if (startBeat == null) {
return null;
}
const result = this.createResult(masterBar, startBeatLookup!, startBeat, fillNext, visibleTracks);
return result;
}
private createResult(
masterBar: MasterBarTickLookup,
beatLookup: BeatTickLookup,
beat: Beat,
fillNext: boolean,
visibleTracks: Set<number>) {
const result = new MidiTickLookupFindBeatResult(masterBar);
result.beat = beat;
result.beatLookup = beatLookup;
result.tickDuration = beatLookup!.end - beatLookup!.start;
if (fillNext) {
this.fillNextBeat(result, visibleTracks);
}
result.duration = MidiUtils.ticksToMillis(result.tickDuration, masterBar.tempo);
return result;
}
private findMasterBar(tick: number): MasterBarTickLookup | null {
const bars: MasterBarTickLookup[] = this.masterBars;
let bottom: number = 0;
let top: number = bars.length - 1;
while (bottom <= top) {
const middle: number = ((top + bottom) / 2) | 0;
const bar: MasterBarTickLookup = bars[middle];
// found?
if (tick >= bar.start && tick < bar.end) {
return bar;
}
// search in lower half
if (tick < bar.start) {
top = middle - 1;
} else {
bottom = middle + 1;
}
}
return null;
}
/**
* Gets the {@link MasterBarTickLookup} for a given masterbar at which the masterbar is played the first time.
* @param bar The masterbar to find the time period for.
* @returns A {@link MasterBarTickLookup} containing the details about the first time the {@link MasterBar} is played.
*/
public getMasterBar(bar: MasterBar): MasterBarTickLookup {
if (!this.masterBarLookup.has(bar.index)) {
const fallback = new MasterBarTickLookup();
fallback.masterBar = bar;
return fallback;
}
return this.masterBarLookup.get(bar.index)!;
}
/**
* Gets the start time in midi ticks for a given masterbar at which the masterbar is played the first time.
* @param bar The masterbar to find the time period for.
* @returns The time in midi ticks at which the masterbar is played the first time or 0 if the masterbar is not contained
*/
public getMasterBarStart(bar: MasterBar): number {
if (!this.masterBarLookup.has(bar.index)) {
return 0;
}
return this.masterBarLookup.get(bar.index)!.start;
}
/**
* Gets the start time in midi ticks for a given beat at which the masterbar is played the first time.
* @param beat The beat to find the time period for.
* @returns The time in midi ticks at which the beat is played the first time or 0 if the beat is not contained
*/
public getBeatStart(beat: Beat): number {
if (!this.masterBarLookup.has(beat.voice.bar.index)) {
return 0;
}
return this.masterBarLookup.get(beat.voice.bar.index)!.start + beat.playbackStart;
}
/**
* Adds a new {@link MasterBarTickLookup} to the lookup table.
* @param masterBar The item to add.
*/
public addMasterBar(masterBar: MasterBarTickLookup): void {
this.masterBars.push(masterBar);
if (this._currentMasterBar) {
masterBar.previousMasterBar = this._currentMasterBar;
this._currentMasterBar.nextMasterBar = masterBar;
}
this._currentMasterBar = masterBar;
if (!this.masterBarLookup.has(masterBar.masterBar.index)) {
this.masterBarLookup.set(masterBar.masterBar.index, masterBar);
}
}
public addBeat(beat: Beat, start: number, duration: number): void {
const currentMasterBar = this._currentMasterBar;
if (currentMasterBar) {
// pre-beat grace notes at the start of the bar we also add the beat to the previous bar
if (start < 0 && currentMasterBar.previousMasterBar) {
const previousStart = currentMasterBar.previousMasterBar!.end + start;
const previousEnd = previousStart + duration;
// add to previous bar
currentMasterBar.previousMasterBar!.addBeat(beat, previousStart, previousStart, currentMasterBar.previousMasterBar!.end - previousStart);
// overlap to current bar?
if(previousEnd > currentMasterBar.previousMasterBar!.end) {
// the start is negative and representing the overlap to the previous bar.
const overlapDuration = duration + start;
currentMasterBar.addBeat(beat, start, 0, overlapDuration);
}
} else {
currentMasterBar.addBeat(beat, start, start, duration);
}
}
}
}