NoteModel.fromMidiNoteNumber constructor

NoteModel.fromMidiNoteNumber(
  1. int midiNoteNumber,
  2. NoteType noteType
)

Implementation

factory NoteModel.fromMidiNoteNumber(int midiNoteNumber, NoteType noteType) {
  final noteNames = noteType.notes;
  final octave = (midiNoteNumber ~/ 12) - 1;
  int noteIndexWithFlats = midiNoteNumber % 12;
  final flatIndexes = [1, 3, 6, 8, 10];
  final isFlat = flatIndexes.contains(noteIndexWithFlats);
  if (isFlat) ++noteIndexWithFlats;
  int noteIndex = 0;
  String name = '';
  switch (noteIndexWithFlats) {
    case 0:
      noteIndex = 0;
      break;
    case 2:
      noteIndex = 1;
      break;
    case 4:
      noteIndex = 2;
      break;
    case 5:
      noteIndex = 3;
      break;
    case 7:
      noteIndex = 4;
      break;
    case 9:
      noteIndex = 5;
      break;
    case 11:
      noteIndex = 6;
      break;
    default:
      noteIndex = 0;
      break;
  }
  if (isFlat) {
    name = "${noteNames[noteIndex - 1]}♯\n${noteNames[noteIndex]}♭";
  } else {
    name = noteNames[noteIndex];
  }
  return NoteModel(
    name: name,
    octave: octave,
    noteIndex: noteIndex,
    isFlat: isFlat,
  );
}