File: importmidi_beat.cpp

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 218,192 kB
  • sloc: cpp: 291,369; xml: 200,226; sh: 3,779; ansic: 1,447; python: 393; makefile: 249; perl: 82; pascal: 79
file content (478 lines) | stat: -rw-r--r-- 19,199 bytes parent folder | download | duplicates (4)
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
#include "importmidi_beat.h"

#include "importmidi_chord.h"
#include "importmidi_fraction.h"
#include "importmidi_inner.h"
#include "importmidi_quant.h"
#include "importmidi_meter.h"
#include "importmidi_tempo.h"
#include "importmidi_operations.h"
#include "thirdparty/beatroot/BeatTracker.h"
#include "mscore/preferences.h"
#include "libmscore/mscore.h"

#include <functional>


namespace Ms {
namespace MidiBeat {

int beatsInBar(const ReducedFraction &barFraction)
      {
      const auto beatLen = Meter::beatLength(barFraction);
      const auto div = barFraction / beatLen;
      return div.numerator() / div.denominator();
      }

double findChordSalience1(
            const std::pair<const ReducedFraction, MidiChord> &chord,
            double ticksPerSec)
      {
      ReducedFraction duration(0, 1);
      int pitch = std::numeric_limits<int>::max();
      int velocity = 0;

      for (const MidiNote &note: chord.second.notes) {
            if (note.offTime - chord.first > duration)
                  duration = note.offTime - chord.first;
            if (note.pitch < pitch)
                  pitch = note.pitch;
            velocity += note.velo;
            }
      const double durationInSeconds = duration.ticks() / ticksPerSec;

      const double c4 = 84;
      const int pmin = 48;
      const int pmax = 72;

      if (pitch < pmin)
            pitch = pmin;
      else if (pitch > pmax)
            pitch = pmax;

      if (velocity <= 0)
            velocity = 1;

      return durationInSeconds * (c4 - pitch) * log(velocity);
      }

double findChordSalience2(
            const std::pair<const ReducedFraction, MidiChord> &chord, double)
      {
      int velocity = 0;
      for (const MidiNote &note: chord.second.notes) {
            velocity += note.velo;
            }
      if (velocity <= 0)
            velocity = 1;

      return velocity;
      }

::EventList prepareChordEvents(
            const std::multimap<ReducedFraction, MidiChord> &chords,
            const std::function<double(const std::pair<const ReducedFraction, MidiChord> &,
                                       double)> &findChordSalience,
            double ticksPerSec)
      {
      ::EventList events;
      double minSalience = std::numeric_limits<double>::max();
      for (const auto &chord: chords) {
            ::Event e;
            e.time = chord.first.ticks() / ticksPerSec;
            e.salience = findChordSalience(chord, ticksPerSec);
            if (e.salience < minSalience)
                  minSalience = e.salience;
            events.push_back(e);
            }
                  // all saliences should be non-negative
      if (minSalience < 0) {
            for (auto &e: events) {
                  e.salience -= minSalience;
                  }
            }

      return events;
      }

ReducedFraction findLastChordTick(const std::multimap<ReducedFraction, MidiChord> &chords)
      {
      ReducedFraction lastOffTime(0, 1);
      for (const auto &chord: chords) {
            for (const auto &note: chord.second.notes) {
                  if (note.offTime > lastOffTime)
                        lastOffTime = note.offTime;
                  }
            }
      return lastOffTime;
      }

// first beat time can be larger than first chord onTime
// so insert additional beats at the beginning to cover all chords

void addFirstBeats(
            std::set<ReducedFraction> &beatSet,
            const ReducedFraction &firstTick,
            int beatsInBar,
            int &addedBeatCount)
      {
      if (beatSet.empty())
            return;

      addedBeatCount = 0;
      auto firstBeat = *beatSet.begin();
      if (firstTick < firstBeat) {
            if (beatSet.size() > 1) {
                  const auto beatLen = *std::next(beatSet.begin()) - firstBeat;
                  do {
                        firstBeat -= beatLen;
                        beatSet.insert(firstBeat);
                        ++addedBeatCount;
                        } while (firstBeat > firstTick || addedBeatCount % beatsInBar);
                  }
            }
      }

// last beat time can be smaller than the last chord onTime
// so insert additional beats at the end to cover all chords

void addLastBeats(
            std::set<ReducedFraction> &beatSet,
            const ReducedFraction &lastTick,
            int beatsInBar,
            int &addedBeatCount)
      {
      if (beatSet.empty())
            return;

      addedBeatCount = 0;
                  // theoretically it's possible that every chord have off time
                  // at the end of the piece - so check all chords for max off time
      auto lastBeat = *(std::prev(beatSet.end()));
      if (lastTick > lastBeat) {
            if (beatSet.size() > 1) {
                  const auto beatLen = lastBeat - *std::prev(beatSet.end(), 2);
                  do {
                        lastBeat += beatLen;
                        beatSet.insert(lastBeat);
                        ++addedBeatCount;
                        } while (lastBeat < lastTick || addedBeatCount % beatsInBar);
                  }
            }
      }

MidiOperations::HumanBeatData prepareHumanBeatData(
            const std::vector<double> &beatTimes,
            const std::multimap<ReducedFraction, MidiChord> &chords,
            double ticksPerSec,
            int beatsInBar)
      {
      MidiOperations::HumanBeatData beatData;
      if (chords.empty())
            return beatData;

      for (const auto &beatTime: beatTimes)
            beatData.beatSet.insert(MidiTempo::time2Tick(beatTime, ticksPerSec));

      beatData.firstChordTick = chords.begin()->first;
      beatData.lastChordTick = findLastChordTick(chords);

      addFirstBeats(beatData.beatSet, beatData.firstChordTick,
                    beatsInBar, beatData.addedFirstBeats);
      addLastBeats(beatData.beatSet, beatData.lastChordTick,
                   beatsInBar, beatData.addedLastBeats);

      return beatData;
      }

double findMatchRank(const std::set<ReducedFraction> &beatSet,
                     const ::EventList &events,
                     const std::vector<int> &levels,
                     int beatsInBar,
                     double ticksPerSec)
      {
      std::map<ReducedFraction, double> saliences;
      for (const auto &e: events) {
            saliences.insert({MidiTempo::time2Tick(e.time, ticksPerSec), e.salience});
            }
      std::vector<ReducedFraction> beatsOfBar;
      double matchFrac = 0;
      int matchCount = 0;
      int beatCount = 0;

      for (const auto &beat: beatSet) {
            beatsOfBar.push_back(beat);
            ++beatCount;
            if (beatCount == beatsInBar) {
                  beatCount = 0;
                  int relationCount = 0;
                  int relationMatches = 0;
                  for (int i = 0; i != (int)beatsOfBar.size() - 1; ++i) {
                        const auto s1 = saliences.find(beatsOfBar[i]);
                        for (int j = i + 1; j != (int)beatsOfBar.size(); ++j) {
                              ++relationCount;    // before s1 search check
                              if (s1 == saliences.end())
                                    continue;
                              const auto s2 = saliences.find(beatsOfBar[j]);
                              if (s2 == saliences.end())
                                    continue;
                              if ((s1->second < s2->second) == (levels[i] < levels[j]))
                                    ++relationMatches;
                              }
                        }
                  if (relationCount) {
                        matchFrac += relationMatches * 1.0 / relationCount;
                        ++matchCount;
                        }
                  beatsOfBar.clear();
                  }
            }
      if (matchCount)
            matchFrac /= matchCount;

      return matchFrac;
      }

void removeEvery2ndBeat(std::set<ReducedFraction> &beatSet)
      {
      auto it = beatSet.begin();
      while (it != beatSet.end() && std::next(it) != beatSet.end())
            it = beatSet.erase(std::next(it));
      if (it == beatSet.end()) {
                        // insert additional beat at the end to cover all chords
            const auto beatLen = *std::prev(it) - *std::prev(it, 2);
            beatSet.insert(*std::prev(it) + beatLen);
            }
      }

// we can use ReducedFraction for time signature (bar fraction)
// because it reduces itself only if numerator or denominator
// is greater than some big number (see ReducedFraction class definition)

std::vector<ReducedFraction> findTimeSignatures(const ReducedFraction &timeSigFromMidiFile)
      {
      std::vector<ReducedFraction> fractions{ReducedFraction(4, 4), ReducedFraction(3, 4)};
      bool match = false;
      for (const ReducedFraction &f: fractions) {
            if (f.isIdenticalTo(timeSigFromMidiFile)) {
                  match = true;
                  break;
                  }
            }
      if (!match) {     // some special time sig in MIDI file - use only it
            fractions.clear();
            fractions.push_back(timeSigFromMidiFile);
            }
      return fractions;
      }

void setTimeSig(TimeSigMap *sigmap, const ReducedFraction &timeSig)
      {
      sigmap->clear();
      sigmap->add(0, timeSig.fraction());
      }

void findBeatLocations(
            const std::multimap<ReducedFraction, MidiChord> &allChords,
            TimeSigMap *sigmap,
            double ticksPerSec)
      {
      const size_t MIN_BEAT_COUNT = 8;
      const auto barFractions = findTimeSignatures(ReducedFraction(sigmap->timesig(0).timesig()));
      const std::vector<
                 std::function<double(const std::pair<const ReducedFraction, MidiChord> &, double)>
                       >
                 salienceFuncs = {findChordSalience1, findChordSalience2};

            // <match rank, beat data, comparator>
      std::map<double, MidiOperations::HumanBeatData, std::greater<double>> beatResults;

      for (const auto &func: salienceFuncs) {
            const auto events = prepareChordEvents(allChords, func, ticksPerSec);
            const auto beatTimes = BeatTracker::beatTrack(events);
            if (beatTimes.size() <= MIN_BEAT_COUNT)
                  continue;

            for (const ReducedFraction &barFraction: barFractions) {
                  const auto beatLen = Meter::beatLength(barFraction);
                  const auto div = barFraction / beatLen;
                  const int beatsInBar = div.numerator() / div.denominator();

                  const std::vector<Meter::DivisionInfo> divsInfo
                                    = { Meter::metricDivisionsOfBar(barFraction) };
                  const auto levels = Meter::metricLevelsOfBar(barFraction, divsInfo, beatLen);

                  Q_ASSERT_X((int)levels.size() == beatsInBar,
                             "MidiBeat::findBeatLocations", "Wrong count of bar levels");

                              // beat set - first case
                  MidiOperations::HumanBeatData beatData = prepareHumanBeatData(
                                                beatTimes, allChords, ticksPerSec, beatsInBar);
                  beatData.timeSig = barFraction;
                  const double matchRank = findMatchRank(beatData.beatSet, events,
                                                         levels, beatsInBar, ticksPerSec);
                  beatResults.insert({matchRank, beatData});
                  }
            }

      auto *data = midiImportOperations.data();
      if (!beatResults.empty()) {
            const MidiOperations::HumanBeatData &beatData = beatResults.begin()->second;
            setTimeSig(sigmap, beatData.timeSig);
            data->humanBeatData = beatData;
            data->trackOpers.measureCount2xLess.setDefaultValue(beatData.measureCount2xLess);
            data->trackOpers.timeSigNumerator.setDefaultValue(
                              Meter::fractionNumeratorToUserValue(beatData.timeSig.numerator()));
            data->trackOpers.timeSigDenominator.setDefaultValue(
                              Meter::fractionDenominatorToUserValue(beatData.timeSig.denominator()));
            }
      else {
            const auto currentTimeSig = ReducedFraction(sigmap->timesig(0).timesig());
            data->trackOpers.timeSigNumerator.setDefaultValue(
                              Meter::fractionNumeratorToUserValue(currentTimeSig.numerator()));
            data->trackOpers.timeSigDenominator.setDefaultValue(
                              Meter::fractionDenominatorToUserValue(currentTimeSig.denominator()));
            }
      }

void scaleOffTimes(
            QList<MidiNote> &notes,
            const std::set<ReducedFraction> &beats,
            const std::set<ReducedFraction>::const_iterator &onTimeBeatEndIt,
            const ReducedFraction &newOnTimeBeatStart,
            const ReducedFraction &newBeatLen)
      {
      for (auto &note: notes) {
            int beatCount = 0;      // beat count between note on time and off time

            Q_ASSERT_X(onTimeBeatEndIt != beats.begin(),
                       "MidiBeat::scaleOffTimes",
                       "End beat iterator cannot be the first beat iterator");

            auto bStart = *std::prev(onTimeBeatEndIt);
            for (auto bit = onTimeBeatEndIt; bit != beats.end(); ++bit) {
                  const auto &bEnd = *bit;

                  Q_ASSERT_X(bEnd > bStart,
                             "MidiBeat::scaleOffTimes",
                             "Beat end <= beat start for note off time that is incorrect");

                  if (note.offTime >= bStart && note.offTime < bEnd) {
                        const auto scale = newBeatLen / (bEnd - bStart);
                        auto newOffTimeInBeat = (note.offTime - bStart) * scale;
                        newOffTimeInBeat = Quantize::quantizeValue(
                                                newOffTimeInBeat, MChord::minAllowedDuration());
                        const auto desiredBeatStart = newOnTimeBeatStart + newBeatLen * beatCount;
                        note.offTime = desiredBeatStart + newOffTimeInBeat;
                        break;
                        }

                  bStart = bEnd;
                  ++beatCount;
                  }
            }
      }

void adjustChordsToBeats(std::multimap<int, MTrack> &tracks)
      {
      const auto &opers = midiImportOperations;
      std::set<ReducedFraction> beats = opers.data()->humanBeatData.beatSet;  // copy
      if (beats.empty())
            return;

      if (opers.data()->trackOpers.isHumanPerformance.value()) {
            if (opers.data()->trackOpers.measureCount2xLess.value())
                  removeEvery2ndBeat(beats);

            Q_ASSERT_X(beats.size() > 1, "MidiBeat::adjustChordsToBeats", "Human beat count < 2");

            const auto newBeatLen = ReducedFraction::fromTicks(MScore::division);

            for (auto trackIt = tracks.begin(); trackIt != tracks.end(); ++trackIt) {
                  auto &chords = trackIt->second.chords;
                  if (chords.empty())
                        continue;
                              // do chord alignment according to recognized beats
                  std::multimap<ReducedFraction, MidiChord> newChords;
                  auto chordIt = chords.begin();
                  auto it = beats.begin();
                  auto beatStart = *it;
                  auto newBeatStart = ReducedFraction(0, 1);

                  for (++it; it != beats.end(); ++it) {
                        const auto &beatEnd = *it;

                        Q_ASSERT_X(beatEnd > beatStart, "MidiBeat::adjustChordsToBeats",
                                   "Beat end <= beat start that is incorrect");

                        const auto scale = newBeatLen / (beatEnd - beatStart);

                        for (; chordIt != chords.end() && chordIt->first < beatEnd; ++chordIt) {
                              auto newOnTimeInBeat = (chordIt->first - beatStart) * scale;
                                          // quantize to prevent ReducedFraction overflow
                              newOnTimeInBeat = Quantize::quantizeValue(
                                                 newOnTimeInBeat, MChord::minAllowedDuration());
                              scaleOffTimes(chordIt->second.notes, beats, it,
                                            newBeatStart, newBeatLen);
                              const auto newOnTime = newBeatStart + newOnTimeInBeat;
                              for (auto &note: chordIt->second.notes) {
                                    if (note.offTime - newOnTime < MChord::minAllowedDuration())
                                          note.offTime = newOnTime + MChord::minAllowedDuration();
                                    }
                              newChords.insert({newOnTime, chordIt->second});
                              }

                        if (chordIt == chords.end())
                              break;

                        beatStart = beatEnd;
                        newBeatStart += newBeatLen;
                        }

                  std::swap(chords, newChords);

                  Q_ASSERT_X(MChord::areNotesLongEnough(chords),
                             "MidiBeat::adjustChordsToBeats", "There are too short notes");
                  }
            }
      }

void updateFirstLastBeats(MidiOperations::HumanBeatData &beatData, const ReducedFraction &timeSig)
      {
      for (int i = 0; i != beatData.addedFirstBeats; ++i) {

            Q_ASSERT_X(!beatData.beatSet.empty(), "MidiBeat::updateFirstLastBeats",
                       "Empty beat set after first beats deletion");

            beatData.beatSet.erase(beatData.beatSet.begin());
            }
      for (int i = 0; i != beatData.addedLastBeats; ++i) {

            Q_ASSERT_X(!beatData.beatSet.empty(), "MidiBeat::updateFirstLastBeats",
                       "Empty beat set after last beats deletion");

            beatData.beatSet.erase(std::prev(beatData.beatSet.end()));
            }

      const int beatsInBar = MidiBeat::beatsInBar(timeSig);

      MidiBeat::addFirstBeats(beatData.beatSet, beatData.firstChordTick,
                              beatsInBar, beatData.addedFirstBeats);
      MidiBeat::addLastBeats(beatData.beatSet, beatData.lastChordTick,
                             beatsInBar, beatData.addedLastBeats);
      }

void setTimeSignature(TimeSigMap *sigmap)
      {
      auto *data = midiImportOperations.data();
      const std::set<ReducedFraction> &beats = data->humanBeatData.beatSet;
      if (beats.empty())
            return;           // don't set time sig for non-human performed MIDI files
      const auto timeSig = Meter::userTimeSigToFraction(data->trackOpers.timeSigNumerator.value(),
                                                        data->trackOpers.timeSigDenominator.value());
      setTimeSig(sigmap, timeSig);
      updateFirstLastBeats(data->humanBeatData, timeSig);
      }

} // namespace MidiBeat
} // namespace Ms