-
Notifications
You must be signed in to change notification settings - Fork 0
/
Beat.cpp
666 lines (544 loc) · 23.6 KB
/
Beat.cpp
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#include "Beat.h"
#include <iostream>
#include <math.h>
#include <numeric>
namespace RhythmTranscriber
{
extern unsigned int iters;
/// Weight definition
float divisionWeight = 0.2f;
float noteWeight = 0.3f;
float distWeight = 0.7f;
/// BaseRatio
void BaseRatio::simplify()
{
float gcd = std::gcd(antecedent, consequent);
antecedent /= gcd;
consequent /= gcd;
}
void BaseRatio::add(const BaseRatio ratio)
{
if (consequent == 0)
{
this->antecedent = ratio.antecedent;
this->consequent = ratio.consequent;
}
else if (ratio.consequent == consequent)
{
antecedent += ratio.antecedent;
}
else
{
/// I think `ratio` needs to be in simplest form for the below to work properly. Haven't
/// had to deal with it yet, but if it becomes an issue `simplify` will have to be used.
/* ratio.simplify(); */
auto consequentLCM = std::lcm(consequent, ratio.consequent);
antecedent = antecedent * (consequentLCM / consequent) +
ratio.antecedent * (consequentLCM / ratio.consequent);
consequent = consequentLCM;
}
}
BaseRatio &BaseRatio::operator+=(const BaseRatio ratio)
{
add(ratio);
return *this;
}
/// Beat
Beat::Beat()
{ /* noteRatios = std::vector<NoteRatio>(32, NoteRatio{}); */
}
Beat::Beat(BaseNote *notes)
{
this->notes = notes;
notesLen = 0;
/// Set start/end time, assuming all notes fit perfectly within the beat.
startTime = notes->timestamp;
/// Hopefully this doesn't cause any errors.
/* endTime = (notes + notesLen)->timestamp; */ /// It caused errors.
endTime = notes[0].timestamp + notes[0].duration;
duration = endTime - startTime;
noteRatios = std::vector<NoteRatio>(16, NoteRatio{});
}
Beat::Beat(BaseNote *notes, unsigned int notesLen)
{
this->notes = notes;
this->notesLen = notesLen;
/// Set start/end time, assuming all notes fit perfectly within the beat.
startTime = notes->timestamp;
/// Hopefully this doesn't cause any errors.
/* endTime = (notes + notesLen)->timestamp; */ /// It caused errors.
endTime = notes[notesLen - 1].timestamp + notes[notesLen - 1].duration;
/* endTime = (notes + notesLen - 1)->timestamp + (notes + notesLen - 1)->duration; */
duration = endTime - startTime;
/* noteRatios = std::vector<NoteRatio>(notesLen == 0 ? 16 : notesLen, NoteRatio{}); */
noteRatios = std::vector<NoteRatio>(notesLen, NoteRatio{});
/* if (noteRatios.size() < notesLen)
{
noteRatios.resize(notesLen, NoteRatio{});
} */
}
Beat::Beat(BaseNote *notes, unsigned int notesLen, float beatDuration)
{
this->notes = notes;
this->notesLen = notesLen;
/// Set start/end time
startTime = notes->timestamp;
endTime = startTime + beatDuration;
duration = beatDuration;
noteRatios = std::vector<NoteRatio>(notesLen == 0 ? 16 : notesLen, NoteRatio{});
/* if (noteRatios.size() < notesLen)
{
noteRatios.resize(notesLen, NoteRatio{});
} */
}
Beat Beat::create_next()
{
/* auto newBeat = Beat(notes + notesLen, 0); */
auto newBeat = Beat(notes + notesLen);
/// A beat with antecedent < consequent should not be trying to create the next beat, as it
/// itself is not complete yet. This is why modulus should catch all *correct* cases.
if (division.antecedent >= division.consequent * 2)
{
newBeat.set_offset(division.antecedent % division.consequent, division.consequent);
/// This essentially just adds the time of the empty trailing beats.
newBeat.startTime =
endTime + duration * (division.antecedent / division.consequent - 1);
}
else
{
if (division.antecedent > division.consequent)
{
newBeat.set_offset(division.antecedent - division.consequent, division.consequent);
}
newBeat.startTime = endTime;
}
return newBeat;
}
void Beat::add_note(NoteRatio noteRatio)
{
/// Add to division.
if (division.consequent == 0)
{
division.antecedent = noteRatio.antecedent;
division.consequent = noteRatio.consequent;
}
else if (noteRatio.consequent == division.consequent)
{
division.antecedent += noteRatio.antecedent;
}
else
{
auto consequentLCM = std::lcm(division.consequent, noteRatio.consequent);
if (consequentLCM != division.consequent)
{
/// Note ratio antecedents must be adjusted. This should be fairly rare when
/// transcribing, but adding notes from fully-simplified ratios could cause this.
transform_note_ratios(consequentLCM / division.consequent);
}
noteRatio.antecedent = noteRatio.antecedent * (consequentLCM / noteRatio.consequent);
noteRatio.consequent = consequentLCM;
division.antecedent =
division.antecedent * (consequentLCM / division.consequent) + noteRatio.antecedent;
division.consequent = consequentLCM;
}
/// Add note to noteRatios.
if (noteRatios.size() == notesLen)
{
noteRatios.resize(notesLen + 8, NoteRatio{});
}
noteRatios[notesLen] = noteRatio;
notesLen++;
/// If beat is complete, set `endTime`.
if (division.antecedent == division.consequent)
{
endTime = notes[notesLen - 1].timestamp + notes[notesLen - 1].duration;
duration = endTime - startTime;
}
else if (division.antecedent > division.consequent)
{
endTime =
(notes + notesLen - 1)->timestamp +
(notes + notesLen - 1)->duration *
(float)(division.consequent - (division.antecedent - noteRatio.antecedent)) /
noteRatio.antecedent;
duration = endTime - startTime;
}
}
void Beat::transform_note_ratios(float ratio)
{
for (unsigned int i = 0; i < notesLen; i++)
{
noteRatios[i].antecedent *= ratio;
}
}
bool Beat::set_note_ratios(unsigned int division)
{
if (!offset.antecedent)
{
this->division = BaseRatio{0, division};
}
float baseDuration = duration / division;
set_note_ratios(division, baseDuration);
unsigned int doubleDivision = division * 2;
/// We could have an option to immediately eliminate any division possibilities if the
/// initial note divisions don't fit into the beat perfectly. This would be advised against,
/// as it would really only work for simpler rhythms.
unsigned int iters = 0;
while (this->division.antecedent != division)
{
if (iters > 4)
{
/// It is possible that there is no beat duration that can represent the specific
/// division while fitting all notes perfectly in the beat. This can happen when two
/// notes have the exact same duration.
/// It is also rarely possible that this loop can get stuck infinitely ping-ponging
/// around the desired antecedent.
return false;
}
/// Estimates the beat duration to set to get the desired antecedent.
baseDuration *= (partialSum + this->division.antecedent) / doubleDivision;
set_note_ratios(division, baseDuration);
iters++;
}
return true;
}
bool Beat::set_offbeat_note_ratios(unsigned int division)
{
if (!offset.antecedent)
{
this->division = BaseRatio{0, division};
}
float baseDuration = duration / division;
set_note_ratios(division, baseDuration);
/// Factor to multiply/divide `baseDuration` by. Since this is static, it could lead to
/// ping-ponging, but due to the low chance of an actual offbeat requiring multiple
/// adjustments to beat duration it doesn't matter too much at the moment.
float divisionFactor = 1 + 1.f / division;
bool endIsValid = end_is_valid();
bool hasTail = has_tail();
unsigned int iters = 0;
while (!endIsValid || !hasTail)
{
/// Invalid end => increase base duration (divisions have become too big)
/// No tail => decrease base duration (divisions have become too small)
/// End validity and tail presence are exclusive, they cannot both be false (invalid end
/// means tail is added to already complete beat).
if (iters > 4)
{
/// It is possible that there is no beat duration that can represent the specific
/// division while fitting all notes perfectly in the beat. This can happen when two
/// notes have the exact same duration.
/// It is also rarely possible that this loop can get stuck infinitely ping-ponging
/// around the desired antecedent.
return false;
}
if (!hasTail)
{
/// Decrease `baseDuration`.
baseDuration /= divisionFactor;
}
else
{
/// End must be invalid due to the condition for the while loop + conditions cannot
/// both be false.
/// Increase `baseDuration`.
baseDuration *= divisionFactor;
}
set_note_ratios(division, baseDuration);
endIsValid = end_is_valid();
hasTail = has_tail();
iters++;
}
return true;
}
void Beat::calc_note_partials()
{
float baseDuration = duration / division.consequent;
for (unsigned int i = 0; i < notesLen; i++)
{
noteRatios[i].partial = notes[i].duration / baseDuration;
}
}
/// @brief
/// @note This assumes all `noteRatio`s have `partial` set correctly.
/// @return
float Beat::calc_score()
{
distScore = 0.f;
divisionScore = 1.f;
noteScore = 0.f;
unsigned int noteChangeCount = 0;
unsigned int prevAntecedent = noteRatios[0].antecedent;
unsigned int beatAntecedent = 0;
float divScore = 0.f;
float distScoreSum = 0.f;
float distWeightSum = 0.f;
float beatScoreSum = 0.f;
float beatWeightSum = 0.f;
for (unsigned int i = 0; i < notesLen; i++)
{
float beatScore = 0.f;
float beatWeight = 0.f;
auto antecedent = noteRatios[i].antecedent;
/// Dist score
float distScoreBase = noteRatios[i].partial / antecedent;
/// Humans aren't as good at timing longer notes than they are shorter notes (I think),
/// unless the notes are repeated consecutively with a metronome.
/// Basically, longer notes leave more room for error, so make longer notes have
/// slightly less impact on overall dist score.
float noteDistWeight =
(0.05f / ((notes + i)->duration * (notes + i)->duration + 0.05f));
distScore += noteDistWeight * (distScoreBase > 1 ? 1.f / distScoreBase : distScoreBase);
distWeightSum += noteDistWeight;
/// In the case we're scoring a multi-beat, going over the consequent should reset the
/// running antecedent.
/// `prevAntecedent` should also be reset since we're essentially in a new beat and
/// don't want the actual previous antecedent to affect anything.
if (beatAntecedent >= this->division.consequent)
{
beatAntecedent = beatAntecedent - this->division.consequent;
prevAntecedent = beatAntecedent == 0 ? antecedent : beatAntecedent;
}
/// Division score
/// Omit when division changes on eighth note (at least for now) to avoid always buffing
/// (or hurting) with ~0.97 division score.
if (antecedent != prevAntecedent /* &&
beatAntecedent != (float)this->division.consequent / 2 */
&& beatAntecedent < this->division.consequent)
{
if (antecedent > prevAntecedent)
{
if (antecedent % prevAntecedent != 0)
{
divisionScore *=
beatDivisionScoreTable[this->division.consequent][beatAntecedent];
noteChangeCount++;
}
}
else if (prevAntecedent % antecedent != 0)
{
divisionScore *=
beatDivisionScoreTable[this->division.consequent][beatAntecedent];
noteChangeCount++;
}
}
/// Note score
noteScore += noteDivisionScoreTable[this->division.consequent][antecedent];
prevAntecedent = antecedent;
beatAntecedent += antecedent;
}
distScore /= distWeightSum;
noteScore /= notesLen;
return score = ((distWeight * distScore * divisionScore) + noteWeight * noteScore) /
(distWeight + noteWeight);
}
void Beat::calc_time()
{
/// I'm not sure which method for calculating time is the correct one, but I think it's
/// this one.
/* endTime = (notes + notesLen - 1)->timestamp +
(notes + notesLen - 1)->duration *
((float)(division.consequent -
(division.antecedent - noteRatios[notesLen - 1].antecedent)) /
division.consequent); */
endTime = (notes + notesLen - 1)->timestamp +
(notes + notesLen - 1)->duration *
((float)(division.consequent -
(division.antecedent - noteRatios[notesLen - 1].antecedent)) /
noteRatios[notesLen - 1].antecedent);
duration = endTime - startTime;
}
std::vector<Beat> Beat::get_trailing_beats()
{
if (division.antecedent <= division.consequent)
{
return std::vector<Beat>();
}
auto tailAntecedent = division.antecedent - division.consequent;
auto tailConsequent = division.consequent;
unsigned int trailingBeatLen = std::ceil(tailAntecedent / tailConsequent);
std::vector<Beat> trailingBeats = std::vector<Beat>(trailingBeatLen, Beat());
trailingBeats.at(trailingBeatLen - 1).division.antecedent =
tailConsequent - (trailingBeatLen * tailConsequent - tailAntecedent);
return trailingBeats;
}
std::string Beat::str()
{
std::string str =
"bpm: " + std::to_string(60.f / duration) + " " + std::to_string(startTime) + "->" +
std::to_string(endTime) + /* ", notesLen: " + std::to_string(notesLen) + */
(offset.antecedent == 0 ? ""
: (", offset: " + std::to_string(offset.antecedent) + '/' +
std::to_string(offset.consequent))) +
", division: " + std::to_string(division.antecedent) + "/" +
std::to_string(division.consequent) + ", score: " + std::to_string(score) +
(score == 0.f ? ""
: " (distScore: " + std::to_string(distScore) +
", divScore: " + std::to_string(divisionScore) +
", noteScore: " + std::to_string(noteScore)) +
", notes: ";
for (unsigned int i = 0; i < notesLen; i++)
{
str += std::to_string(noteRatios[i].antecedent) + '/' +
std::to_string(/* noteRatios[i].consequent */ division.consequent);
if (i < notesLen - 1)
{
str += ", ";
}
}
return str;
}
/* Beat::Beat(float timestamp) { this->timestamp = timestamp; }
Beat::Beat(float timestamp, BeatDivision offset) : Beat::Beat(timestamp)
{
/// Since forceDivision wasn't passed, we don't have to worry about it existing already
this->offset = offset;
this->division = offset;
this->duration = offset.duration;
}
Beat::Beat(float timestamp, BeatDivisionValue forceDivision) : Beat::Beat(timestamp)
{
/// Since offset wasn't passed, we don't have to worry about it existing already
this->forceDivision = forceDivision;
}
Beat::Beat(float timestamp, BeatDivision offset, BeatDivisionValue forceDivision)
: Beat::Beat(timestamp, offset)
{
/// Offset has priority over forceDivision, which is why the above constructor is delegated.
this->forceDivision = forceDivision;
/// Check if attempting to create a beat with difference forced division and offset division
if (forceDivision != offset.denominator)
{
auto denomLCM = std::lcm(offset.denominator, forceDivision);
this->division.denominator = denomLCM;
this->division.numerator = offset.numerator * (denomLCM / offset.denominator);
}
}
void Beat::addNoteInterpretation(NoteInterpretation ¬e)
{
auto noteDuration = note.rhythm.get_duration();
/// Division of the note itself (as a BeatDivision), beats becomes numerator and notes
/// becomes denominator, since notes/rhythms are represented as notes/beats and beat
/// divisions are represented as fractions of space in the beat.
/// Ex: 4/1 (rhythm) => 4 notes / 1 beat => sixteenth note, which takes up 1/4th of a beat.
BeatDivision noteDivision =
BeatDivision{note.rhythm.beats, note.rhythm.notes, noteDuration};
if (division.denominator == 0)
{
division = noteDivision;
}
else
{
auto denomLCM = std::lcm(division.denominator, noteDivision.denominator);
auto numerator1 = division.numerator * (denomLCM / division.denominator);
auto numerator2 = noteDivision.numerator * (denomLCM / noteDivision.denominator);
division.numerator = numerator1 + numerator2;
division.denominator = denomLCM;
division.duration += noteDivision.duration;
}
notes.push_back(note);
/// Check if we're adding a note that extends past the note's duration
if (division.numerator > division.denominator)
{
/// unsigned char - unsigned char results in an int output, and since numerator >
/// denominator we will guaranteed have enough space anyways.
tail = BeatDivision{(BeatDivisionValue)(division.numerator - division.denominator),
division.denominator, note.rhythm.bpm};
}
/// Only add to duration the part of the note that would be inside this beat
duration += noteDuration - (tail.denominator == 0 ? 0 : tail.duration);
/// Stats
/// TODO
}
/// @brief TESTING PURPOSES
/// @param timestamp
/// @param duration
/// @param rhythm
void Beat::addNote(float timestamp, float duration, NoteRhythm rhythm)
{
/// Fix rhythm BPM
rhythm.set_duration(duration);
/// Division of the note itself (as a BeatDivision), beats becomes numerator and notes
/// becomes denominator, since notes/rhythms are represented as notes/beats and beat
/// divisions are represented as fractions of space in the beat.
/// Ex: 4/1 (rhythm) => 4 notes / 1 beat => sixteenth note, which takes up 1/4th of a beat.
BeatDivision noteDivision = BeatDivision{rhythm.beats, rhythm.notes, duration};
if (division.denominator == 0)
{
division = noteDivision;
}
else
{
auto denomLCM = std::lcm(division.denominator, noteDivision.denominator);
auto numerator1 = division.numerator * (denomLCM / division.denominator);
auto numerator2 = noteDivision.numerator * (denomLCM / noteDivision.denominator);
division.numerator = numerator1 + numerator2;
division.denominator = denomLCM;
division.duration += noteDivision.duration;
}
/// Check if we're adding a note that extends past the note's duration
if (division.numerator > division.denominator)
{
/// unsigned char - unsigned char results in an int output, and since numerator >
/// denominator we will guaranteed have enough space anyways.
tail = BeatDivision{(BeatDivisionValue)(division.numerator - division.denominator),
division.denominator, rhythm.bpm};
}
/// Only add to duration the part of the note that would be inside this beat
this->duration += duration - (tail.denominator == 0 ? 0 : tail.duration);
/// Stats
/// TODO
}
/// BeatDivision
BeatDivision::operator bool() const
{
return (bool)numerator;
/// A better check would go as follows:
/// return (bool)numerator || (bool)denominator || (bool)duration
/// However, this would probably slow down every time the division exists it has to check
/// all three instead of just one
}
std::string Beat::str(bool compact)
{
std::string str = "\n";
if (compact)
{
str += "timestamp: " + std::to_string(timestamp) +
", duration: " + std::to_string(duration) +
", division: " + std::to_string(division.numerator) + "/" +
std::to_string(division.denominator) +
", offset: " + std::to_string(offset.numerator) + "/" +
std::to_string(offset.denominator) +
" duration: " + std::to_string(offset.duration) +
", forceDivision: " + std::to_string(forceDivision) + '\n';
}
else
{
str += "- Timestamp: " + std::to_string(timestamp) +
"\n- Duration: " + std::to_string(duration) +
"\n- Division: " + std::to_string(division.numerator) + "/" +
std::to_string(division.denominator) + ", with a duration of " +
std::to_string(division.duration) + '\n';
if (offset.denominator != 0)
{
str += "- Offset: " + std::to_string(offset.numerator) + "/" +
std::to_string(offset.denominator) + ", with a duration of " +
std::to_string(offset.duration) + '\n';
}
if (forceDivision)
{
str += "- forceDivision: " + std::to_string(forceDivision) + '\n';
}
auto notesSize = notes.size();
for (unsigned int i = 0; i < notesSize; i++)
{
auto note = notes[i];
str += "\t- Note at " + std::to_string(note.note->timestamp) +
" rhythm: " + std::to_string(note.rhythm.notes) + "/" +
std::to_string(note.rhythm.beats) + " at " +
std::to_string(note.rhythm.bpm) + " BPM\n";
}
}
return str;
} */
}