From 696da13fc3cdf04fa4f2d01b94b7034a444a067e Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sun, 26 Feb 2023 09:09:33 +0800 Subject: [PATCH] Add edge case for Cb0 Cb0 is an invalid MIDI note and should have a sentinel value of 255u. --- cases.txt | 1 + midi-utils.c | 4 ++++ test-note-names.c | 13 ++++++++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cases.txt b/cases.txt index 1c57e15..6422479 100644 --- a/cases.txt +++ b/cases.txt @@ -34,3 +34,4 @@ C19 228 C20 240 C21 252 D#21 255 +Cb0 255 diff --git a/midi-utils.c b/midi-utils.c index b726e20..0ac1efd 100644 --- a/midi-utils.c +++ b/midi-utils.c @@ -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; } diff --git a/test-note-names.c b/test-note-names.c index aa3fdcc..fa9deb9 100644 --- a/test-note-names.c +++ b/test-note-names.c @@ -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);