matchKeys function

KeyMatch matchKeys(
  1. List<WrittenNote> written,
  2. List<PlayedKey> keys, {
  3. double windowMs = kMatchWindowMs,
})

Casa as notas escritas com as teclas, de forma gulosa e na ordem do ataque. Uma tecla casa uma vez só, e só com a mesma altura dentro de windowMs.

Implementation

KeyMatch matchKeys(
  List<WrittenNote> written,
  List<PlayedKey> keys, {
  double windowMs = kMatchWindowMs,
}) {
  final keyOf = List<int?>.filled(written.length, null);
  final used = List<bool>.filled(keys.length, false);
  for (final w in _onsetOrder(written.map((n) => n.onMs).toList())) {
    final note = written[w];
    int? best;
    var bestGap = double.infinity;
    for (var k = 0; k < keys.length; k++) {
      if (used[k] || keys[k].pitch != note.pitch) continue;
      final gap = (keys[k].onMs - note.onMs).abs();
      if (gap <= windowMs && gap < bestGap) {
        best = k;
        bestGap = gap;
      }
    }
    if (best != null) {
      used[best] = true;
      keyOf[w] = best;
    }
  }
  return KeyMatch(
    keyOf: keyOf,
    extra: [
      for (var k = 0; k < keys.length; k++)
        if (!used[k]) k,
    ],
  );
}