-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeatBranch.cpp
369 lines (295 loc) · 14.1 KB
/
BeatBranch.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
#include "BeatBranch.h"
#include <math.h>
namespace RhythmTranscriber
{
unsigned int divisionDepth = 14;
unsigned int beatDivisions[] = {1, 2, 4, 3, 6, 12, 8, 5, 10, 9, 18, 16,
20, 7, 14, 15, 21, 11, 22, 13, 17, 19, 23};
/* float beatDivisionScores[] = {
1, 1, 0.75, 2.0 / 3.0, 2.0 / 3.0, 0.5f, 0.5f,
0.4f, 0.4f, 1.0 / 3.0, 5.0 / 16.0, 0.3, 2.0 / 7.0, 4.0 / 15.0,
4.0 / 21.0, 2.0 / 11.0, 2.0 / 11.0, 2.0 / 13.0, 2.0 / 17.0, 2.0 / 19.0, 2.0 / 23.0};
unsigned int beatDivisionsSize = 21; */
bool BeatBranch::create_beats()
{
unsigned int divisionBeatLen = 0;
unsigned int divisionNoteLen = 0;
unsigned int beatIndex = 0;
float recentDownbeatTime = dataBuffer[0].notes[0].timestamp;
unsigned int recentDownbeatIndex = 0;
bool shouldUpdate = false;
for (unsigned int i = 0; i < length; i++)
{
/// @todo See if it's faster to avoid copying to local variable.
auto data = dataBuffer[i];
if (data.needsUpdate)
{
shouldUpdate = true;
}
if (data.length > 0 && data.notes[data.length - 1].duration > expectedBeatDuration * 2)
{
/// We may need to add more beats depending on where the long note falls into the
/// beat.
/// In testing, if the combined note duration is even slightly less than
/// `expectedBeatDuration`, it lowers the beat count by 1, which will give incorrect
/// results. Adding 0.25 shifts this threshold over to give a bit more room for
/// error.
divisionBeatLen +=
(((data.notes[data.length - 1].timestamp - data.notes[0].timestamp) +
data.notes[data.length - 1].duration) /
expectedBeatDuration) +
0.25f;
}
else
{
divisionBeatLen++;
}
divisionNoteLen += data.length;
if (!data.endsOffbeat)
{
if (shouldUpdate)
{
if (!create_beats_at(beatIndex, divisionBeatLen, divisionNoteLen))
{
return false;
}
set_updated(beatIndex, i - recentDownbeatIndex + 1);
/* set_updated(beatIndex, divisionBeatLen); */
shouldUpdate = false;
}
divisionBeatLen = 0;
divisionNoteLen = 0;
beatIndex = i + 1;
/// Avoid going out of bounds on the last index while also avoiding branching. It's
/// gooberish, I know. Maybe there's a better way to do it.
recentDownbeatTime = dataBuffer[i + 1 - (i == length - 1)].notes[0].timestamp;
recentDownbeatIndex = i + 1;
}
}
if (divisionBeatLen != 0)
{
/// Branch ends on `divisionBeatLen` number of offbeats. Here, we try to figure out what
/// they are without using a downbeat note.
if (divisionBeatLen >= length)
{
/// No downbeat in this branch.
return false;
}
/// Local duration must be long enough to include all notes in `dataBuffer` at the
/// index. If it's too short, it will essentially round to the downbeat of the last
/// note.
float localDuration =
std::max(dataBuffer[length - divisionBeatLen].notes[divisionNoteLen - 1].timestamp -
dataBuffer[length - 1].notes->timestamp,
beatBuffer[length - divisionBeatLen - 1].get_duration());
return create_beats_at(length - divisionBeatLen, divisionBeatLen, divisionNoteLen,
dataBuffer[length - 1].notes->timestamp + localDuration -
dataBuffer[length - divisionBeatLen].notes->timestamp);
}
return true;
}
bool BeatBranch::create_beats_at(unsigned int beatIndex, unsigned int beatLength,
unsigned int noteLength)
{
Beat beat = Beat(dataBuffer[beatIndex].notes, noteLength);
float bestScore = 0.f;
float beatScore;
for (unsigned int i = 0; i <= divisionDepth; i++)
{
/// Multiplying the division by `beatLength` will allow us to use a single beat instance
/// to score multiple beats (that don't start/end on beat) as one. This has it's
/// issues, but for right now it seems to work alright doing it this way instead of
/// trying to separate them and deal with offbeat beats individually.
/// Any division that would normally be possible for a single beat will be 1:1 to
/// multiple beats, just as multiples. For example, if 9 is a possible division
/// normally and `beatLength` is 2, 9 will become 18, and 9 will not be tested.
auto division = beatDivisions[i] * beatLength;
if (division < noteLength)
{
/// The beat will not be able to represent a division lower than `noteLength`.
/// Example: representing 5 notes in a beat as sixteenth notes isn't possible.
continue;
}
if (!beat.set_note_ratios(division))
{
/// Weren't able to correctly set note ratios based off of `division`.
continue;
}
/// Since we may be using the beat as a combination of multiple beats, to get accurate
/// scores we need to modify the division to be what it *would* be when the beats are
/// split out, which will cause note ratios to add up to be `beatLength` beats. This is
/// essentially reversing the multiplication of the division done earlier.
beat.division.consequent /= beatLength;
/// We should really be scoring the expanded beat instead, but this seems to work for
/// the most part.
beatScore = beat.calc_score();
if (beatScore > bestScore)
{
/// Add tiny amount to prevent a beat with an equal score overriding this one due to
/// floating point comparison issues.
bestScore = beatScore + 0.000001f;
/// Copy to buffer at startIndex. If our beat is actually a "multi-beat", we will
/// expand it to fill the buffer accordingly a little later.
beatBuffer[beatIndex] = beat;
}
}
if (bestScore == 0.f)
{
/// After trying every division, we still couldn't create the beat.
return false;
}
if (beatLength > 1)
{
expand_beat(beatIndex, beatLength);
}
return true;
}
bool BeatBranch::create_beats_at(unsigned int beatIndex, unsigned int beatLength,
unsigned int noteLength, float effectiveBeatDuration)
{
/// @todo Combine into a single `create_beats_at` method.
Beat beat = Beat(dataBuffer[beatIndex].notes, noteLength, effectiveBeatDuration);
float bestScore = 0.f;
float beatScore;
for (unsigned int i = 0; i <= divisionDepth; i++)
{
/// Multiplying the division by `beatLength` will allow us to use a single beat instance
/// to score multiple beats (that don't start/end on beat) as one. This has it's
/// issues, but for right now it seems to work alright doing it this way instead of
/// trying to separate them and deal with offbeat beats individually.
/// Any division that would normally be possible for a single beat will be 1:1 to
/// multiple beats, just as multiples. For example, if 9 is a possible division
/// normally and `beatLength` is 2, 9 will become 18, and 9 will not be tested.
auto division = beatDivisions[i] * beatLength;
if (division < noteLength)
{
/// The beat will not be able to represent a division lower than `noteLength`.
/// Example: representing 5 notes in a beat as sixteenth notes isn't possible.
continue;
}
if (!beat.set_offbeat_note_ratios(division))
{
/// Weren't able to correctly set note ratios based off of `division`.
continue;
}
/// Since we may be using the beat as a combination of multiple beats, to get accurate
/// scores we need to modify the division to be what it *would* be when the beats are
/// split out, which will cause note ratios to add up to be `beatLength` beats. This is
/// essentially reversing the multiplication of the division done earlier.
beat.division.consequent /= beatLength;
beatScore = beat.calc_score();
if (beatScore > bestScore)
{
/// Add tiny amount to prevent a beat with an equal score overriding this one
/// due to floating point comparison issues.
bestScore = beatScore + 0.000001f;
/// Copy to buffer at startIndex. If our beat is actually a "multi-beat", we
/// will expand it to fill the buffer accordingly a little later.
beatBuffer[beatIndex] = beat;
}
}
if (bestScore == 0.f)
{
/// After trying every division, we still couldn't create the beat.
return false;
}
if (beatLength > 1)
{
expand_beat(beatIndex, beatLength);
}
else
{
/// It's possible to have beatLength of 1 with an offbeat if the beat is the last one of
/// the branch. We need to clean up the beat a little bit (which would normally be done
/// by `expand_beat`) before returning.
/// Fix beat end time.
beatBuffer[beatIndex].calc_time();
}
return true;
}
void BeatBranch::expand_beat(unsigned int beatIndex, unsigned int beatLength)
{
/// @todo Optimize by having local variables keep track of division, notes pointer, notes
/// length, etc. instead of keeping track of it on beatBuffer, or a beat instance at all.
/// Copy multi-beat.
auto multiBeat = beatBuffer[beatIndex];
unsigned int consequent = multiBeat.division.consequent;
beatBuffer[beatIndex] = Beat(multiBeat.notes);
/// Set this now to avoid having to simplify later.
beatBuffer[beatIndex].division.consequent = consequent;
for (unsigned int i = 0; i < multiBeat.notesLen; i++)
{
/// The note ratios still have the consequent as beatLength * division, so fix it here
/// before adding.
beatBuffer[beatIndex].add_note(
NoteRatio{multiBeat.noteRatios[i].antecedent, multiBeat.division.consequent});
if (beatBuffer[beatIndex].division.antecedent >= consequent)
{
beatBuffer[beatIndex].calc_note_partials();
beatBuffer[beatIndex].calc_score();
beatIndex++;
/// Create (initialize) next beat
beatBuffer[beatIndex] = beatBuffer[beatIndex - 1].create_next();
}
}
}
std::string BeatData::str()
{
std::string str = "";
str += '[';
for (unsigned int i = 0; i < length; i++)
{
str += std::to_string(notes[i].timestamp);
if (i < length - 1)
{
str += ", ";
}
}
str += "] (" + std::to_string(length) + ") ";
str += "startsOffbeat: " + std::to_string(startsOffbeat) +
", endsOffbeat: " + std::to_string(endsOffbeat);
return str;
/* str += (notes == nullptr
? "nullptr"
: (std::to_string(notes->timestamp) + (startsOffbeat ? "(?)" : "") + "->" +
(length == 0 ? "nullptr"
: std::to_string(notes[length - 1].timestamp) +
(endsOffbeat ? "(?)" : "")))) +
" (" + std::to_string(length) +
"), startsOffbeat: " + std::to_string(startsOffbeat) +
", endsOffbeat: " + std::to_string(endsOffbeat);
return str; */
float startTime =
startsOffbeat ? (((notes - 1)->timestamp + notes->timestamp) / 2) : notes->timestamp;
float endTime = endsOffbeat
? (((notes + length - 1)->timestamp + (notes + length)->timestamp) / 2)
: (notes + length)->timestamp;
str += std::to_string(startTime) + "->" + std::to_string(endTime) + " (" +
std::to_string(length) + "), startsOffbeat: " + std::to_string(startsOffbeat) +
", endsOffbeat: " + std::to_string(endsOffbeat);
/* str += "notes: " + (notes == nullptr ? "nullptr" : std::to_string(startTime)) + "->" +
(length == 0 ? "null" : std::to_string(endTime)) + " (" + std::to_string(length) +
"), startsOffbeat: " + std::to_string(startsOffbeat) +
", endsOffbeat: " + std::to_string(endsOffbeat); */
return str;
}
std::string BeatBranch::data_buffer_str()
{
std::string str = "";
for (unsigned int i = 0; i < length; i++)
{
str += dataBuffer[i].str() + '\n';
}
return str;
}
std::string BeatBranch::beat_buffer_str()
{
std::string str = "";
for (unsigned int i = 0; i < length; i++)
{
str += beatBuffer[i].str() + '\n';
}
return str;
}
std::string BeatBranch::str() { return data_buffer_str() + beat_buffer_str(); }
}