musicHandOf function
A medida "Mão" sobre as notas que a mão tocaria: uma por ataque.
Implementation
MusicHand musicHandOf(List<MusicNote> attacks) {
final pitched = attacks.where((note) => note.diatonic != null).toList();
if (pitched.isEmpty) {
return const MusicHand(
MusicHandPosition(kMusicHandEmptyLow),
inside: 0,
outside: 0,
positions: 0,
);
}
final degrees = [for (final note in pitched) note.diatonic!]..sort();
final median = _median(degrees);
// Candidatas: de (menor grau − (janela − 1)) até o maior grau, em ordem.
final firstLow = degrees.first - (kMusicHandWindow - 1);
var best = MusicHandPosition(firstLow);
var bestInside = -1;
var bestDistance = double.infinity;
for (var low = firstLow; low <= degrees.last; low++) {
final position = MusicHandPosition(low);
final inside = pitched.where(position.covers).length;
final centre = low + kMusicHandWindow ~/ 2;
final distance = (centre - median).abs();
if (inside > bestInside ||
(inside == bestInside && distance < bestDistance)) {
best = position;
bestInside = inside;
bestDistance = distance;
}
}
return MusicHand(
best,
inside: bestInside,
outside: pitched.length - bestInside,
positions: _windowsNeeded(degrees),
);
}