Small utility library for MIDI functions.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

96 lignes
2.3 KiB

  1. #include "midi-utils.h"
  2. #if !defined _WIN32
  3. void _strlwr(char* dest) {
  4. for (unsigned int i = 0; i < strlen(dest); i += 1) {
  5. if ('A' <= dest[i] && dest[i] <= 'Z') {
  6. dest[i] += 0x20;
  7. }
  8. }
  9. }
  10. #endif
  11. char* MIDI_GetNoteName(unsigned char midi_note) {
  12. static const char* pitch_names[] = {
  13. "C",
  14. "C#",
  15. "D",
  16. "D#",
  17. "E",
  18. "F",
  19. "F#",
  20. "G",
  21. "G#",
  22. "A",
  23. "A#",
  24. "B"
  25. };
  26. const unsigned char pitch_class = midi_note % 12;
  27. const unsigned char octave = midi_note / 12;
  28. static char note_name[8];
  29. sprintf(note_name, "%s%u", pitch_names[pitch_class], octave);
  30. return note_name;
  31. }
  32. unsigned char MIDI_GetNoteFromName(const char* name) {
  33. char name_copy[8];
  34. strcpy(name_copy, name);
  35. _strlwr(name_copy);
  36. unsigned char octave = 0;
  37. unsigned char has_accidental = name_copy[1] == '#' || name_copy[1] == 'b';
  38. unsigned char octave_start = has_accidental ? 2 : 1;
  39. unsigned char pitch_class;
  40. char octave_offset = 0;
  41. for (unsigned char i = octave_start; '0' <= name_copy[i] && name_copy[i] <= '9'; i += 1) {
  42. octave *= 10;
  43. octave += name_copy[i] - '0';
  44. }
  45. if (strstr(name_copy, "c#") || strstr(name_copy, "db")) {
  46. pitch_class = 1;
  47. } else if (strstr(name_copy, "d#") || strstr(name_copy, "eb")) {
  48. pitch_class = 3;
  49. } else if (strstr(name_copy, "fb")) {
  50. pitch_class = 4;
  51. } else if (strstr(name_copy, "e#")) {
  52. pitch_class = 5;
  53. } else if (strstr(name_copy, "f#") || strstr(name_copy, "gb")) {
  54. pitch_class = 6;
  55. } else if (strstr(name_copy, "g#") || strstr(name_copy, "ab")) {
  56. pitch_class = 8;
  57. } else if (strstr(name_copy, "a#") || strstr(name_copy, "bb")) {
  58. pitch_class = 10;
  59. } else if (strstr(name_copy, "cb")) {
  60. pitch_class = 11;
  61. octave_offset = -1;
  62. } else if (strstr(name_copy, "b#")) {
  63. pitch_class = 0;
  64. octave_offset = 1;
  65. } else if (strstr(name_copy, "c")) {
  66. pitch_class = 0;
  67. } else if (strstr(name_copy, "d")) {
  68. pitch_class = 2;
  69. } else if (strstr(name_copy, "e")) {
  70. pitch_class = 4;
  71. } else if (strstr(name_copy, "f")) {
  72. pitch_class = 5;
  73. } else if (strstr(name_copy, "g")) {
  74. pitch_class = 7;
  75. } else if (strstr(name_copy, "a")) {
  76. pitch_class = 9;
  77. } else if (strstr(name_copy, "b")) {
  78. pitch_class = 11;
  79. } else {
  80. return 255u;
  81. }
  82. if (octave == 0 && octave_offset < 0) {
  83. return 255u;
  84. }
  85. return ((octave * 12) + octave_offset) + pitch_class;
  86. }