spellings method

Set<Note> spellings({
  1. int distance = 0,
})

The different spellings at distance sharing the same number of semitones.

Example:

PitchClass.g.spellings() == {Note.g}
PitchClass.dSharp.spellings() == {Note.d.sharp, Note.e.flat}
PitchClass.b.spellings(distance: 1)
  == {Note.a.sharp.sharp, Note.b, Note.c.flat}

Implementation

Set<Note> spellings({int distance = 0}) {
  assert(distance >= 0, 'Distance must be greater or equal than zero.');
  final baseNote = BaseNote.fromSemitones(semitones);

  if (baseNote != null) {
    final note = Note(baseNote);

    return SplayTreeSet<Note>.of(
      {
        note,
        for (var i = 1; i <= distance; i++) ...[
          note.respellByBaseNoteDistance(distance),
          note.respellByBaseNoteDistance(-distance),
        ],
      },
      Note.compareByClosestDistance,
    );
  }

  final aboveNote =
      Note(BaseNote.fromSemitones(semitones - 1)!, Accidental.sharp);
  final belowNote =
      Note(BaseNote.fromSemitones(semitones + 1)!, Accidental.flat);

  return SplayTreeSet<Note>.of(
    {
      aboveNote,
      belowNote,
      for (var i = 1; i <= distance; i++) ...[
        belowNote.respellByBaseNoteDistance(distance),
        aboveNote.respellByBaseNoteDistance(-distance),
      ],
    },
    Note.compareByClosestDistance,
  );
}