referenceChord function

HarmonyChord? referenceChord(
  1. HarmonySegment segment,
  2. List<HarmonyChord> fitting, {
  3. required int tonicPc,
  4. required int segmentCount,
})

A referência entre os que cabem (fitting): o acorde com mais notas do trecho (uma forte vale 2, as outras 1). No empate, a tônica no primeiro e no último trecho, a dominante no penúltimo; senão, a ordem da lista. null se nenhum cabe.

Implementation

HarmonyChord? referenceChord(
  HarmonySegment segment,
  List<HarmonyChord> fitting, {
  required int tonicPc,
  required int segmentCount,
}) {
  HarmonyChord? best;
  var bestScore = -1;
  var bestRank = -1;
  for (final chord in fitting) {
    final pcs = chord.symbol.pitchClasses;
    var score = 0;
    for (final note in segment.notes) {
      if (!pcs.contains(note.pitch % 12)) continue;
      score += segment.strong.contains(note) ? 2 : 1;
    }
    final rank = _cadenceRank(
      segment,
      chord.symbol,
      tonicPc: tonicPc,
      segmentCount: segmentCount,
    );
    if (score > bestScore || (score == bestScore && rank > bestRank)) {
      best = chord;
      bestScore = score;
      bestRank = rank;
    }
  }
  return best;
}