#include "midi-utils.h" #if !defined __WIN32 void _strlwr(char* dest) { for (unsigned int i = 0; i < strlen(dest); i += 1) { if ('A' <= dest[i] && dest[i] <= 'Z') { dest[i] += 0x20; } } } #endif char* MIDI_GetNoteName(unsigned char midi_note) { static const char* pitch_names[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" }; const unsigned char pitch_class = midi_note % 12; const unsigned char octave = midi_note / 12; static char note_name[8]; sprintf(note_name, "%s%u", pitch_names[pitch_class], octave); return note_name; } unsigned char MIDI_GetNoteFromName(const char* name) { char name_copy[8]; strcpy(name_copy, name); _strlwr(name_copy); unsigned char octave = 0; unsigned char has_accidental = name_copy[1] == '#' || name_copy[1] == 'b'; unsigned char octave_start = has_accidental ? 2 : 1; unsigned char pitch_class; char octave_offset = 0; for (unsigned char i = octave_start; '0' <= name_copy[i] && name_copy[i] <= '9'; i += 1) { octave *= 10; octave += name_copy[i] - '0'; } if (strstr(name_copy, "c#") || strstr(name_copy, "db")) { pitch_class = 1; } else if (strstr(name_copy, "d#") || strstr(name_copy, "eb")) { pitch_class = 3; } else if (strstr(name_copy, "fb")) { pitch_class = 4; } else if (strstr(name_copy, "e#")) { pitch_class = 5; } else if (strstr(name_copy, "f#") || strstr(name_copy, "gb")) { pitch_class = 6; } else if (strstr(name_copy, "g#") || strstr(name_copy, "ab")) { pitch_class = 8; } else if (strstr(name_copy, "a#") || strstr(name_copy, "bb")) { pitch_class = 10; } else if (strstr(name_copy, "cb")) { pitch_class = 11; octave_offset = -1; } else if (strstr(name_copy, "b#")) { pitch_class = 0; octave_offset = 1; } else if (strstr(name_copy, "c")) { pitch_class = 0; } else if (strstr(name_copy, "d")) { pitch_class = 2; } else if (strstr(name_copy, "e")) { pitch_class = 4; } else if (strstr(name_copy, "f")) { pitch_class = 5; } else if (strstr(name_copy, "g")) { pitch_class = 7; } else if (strstr(name_copy, "a")) { pitch_class = 9; } else if (strstr(name_copy, "b")) { pitch_class = 11; } else { return 255u; } return ((octave * 12) + octave_offset) + pitch_class; }