Browse Source

Add edge case for Cb0

Cb0 is an invalid MIDI note and should have a sentinel value of 255u.
master
TheoryOfNekomata 1 year ago
parent
commit
696da13fc3
3 changed files with 15 additions and 3 deletions
  1. +1
    -0
      cases.txt
  2. +4
    -0
      midi-utils.c
  3. +10
    -3
      test-note-names.c

+ 1
- 0
cases.txt View File

@@ -34,3 +34,4 @@ C19 228
C20 240
C21 252
D#21 255
Cb0 255

+ 4
- 0
midi-utils.c View File

@@ -87,5 +87,9 @@ unsigned char MIDI_GetNoteFromName(const char* name) {
return 255u;
}

if (octave == 0 && octave_offset < 0) {
return 255u;
}

return ((octave * 12) + octave_offset) + pitch_class;
}

+ 10
- 3
test-note-names.c View File

@@ -13,12 +13,19 @@ int main(void) {
fscanf(f, "%s %d\n", read_note_name, &read_note_value);
unsigned int actual_note_value = MIDI_GetNoteFromName(read_note_name);
printf("MIDI_GetNoteFromName(\"%s\")...", read_note_name);
printf("(%d == %d) ", read_note_value, actual_note_value);
assert(read_note_value == actual_note_value);
printf("OK!\n");

printf("MIDI_GetNoteName(%d)...", read_note_value);
assert(!strcmp(MIDI_GetNoteName(read_note_value), read_note_name));
printf("OK!\n");
if (read_note_value != 255u || !strcmp(read_note_name, "D#21")) {
printf("MIDI_GetNoteName(%d)...", read_note_value);
char* actual_note_name = MIDI_GetNoteName(read_note_value);

printf("(%s == %s) ", read_note_name, actual_note_name);
assert(!strcmp(read_note_name, actual_note_name));

printf("OK!\n");
}
}

fclose(f);


Loading…
Cancel
Save