process_midi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "process_midi.h"
  2. #if 0
  3. bool midi_activated = false;
  4. uint8_t midi_starting_note = 0x0C;
  5. int midi_offset = 7;
  6. #endif
  7. typedef union {
  8. uint16_t raw;
  9. struct {
  10. uint8_t octave :4;
  11. uint8_t velocity :4;
  12. uint8_t channel :4;
  13. };
  14. } midi_config_t;
  15. midi_config_t midi_config;
  16. #define MIDI_INVALID_NOTE 0xFF
  17. #define MIDI_MAX_NOTES_ON 10
  18. typedef struct {
  19. uint8_t note;
  20. uint8_t tone;
  21. } midi_notes_on_array_entry_t;
  22. typedef struct {
  23. uint8_t length;
  24. midi_notes_on_array_entry_t values[MIDI_MAX_NOTES_ON];
  25. } midi_notes_on_array_t;
  26. static midi_notes_on_array_t notes_on;
  27. inline uint8_t compute_velocity(uint8_t setting)
  28. {
  29. return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
  30. }
  31. void midi_init(void)
  32. {
  33. midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN;
  34. midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
  35. midi_config.channel = 0;
  36. notes_on.length = 0;
  37. }
  38. bool process_midi(uint16_t keycode, keyrecord_t *record)
  39. {
  40. switch (keycode) {
  41. case MIDI_TONE_MIN ... MIDI_TONE_MAX:
  42. {
  43. uint8_t channel = midi_config.channel;
  44. uint8_t tone = keycode - MIDI_TONE_MIN;
  45. uint8_t velocity = compute_velocity(midi_config.velocity);
  46. if (record->event.pressed && notes_on.length < MIDI_MAX_NOTES_ON) {
  47. uint8_t note = 12 * midi_config.octave + tone;
  48. midi_send_noteon(&midi_device, channel, note, velocity);
  49. dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
  50. notes_on.values[notes_on.length].note = note;
  51. notes_on.values[notes_on.length].tone = tone;
  52. notes_on.length++;
  53. }
  54. else {
  55. for (uint8_t i = 0; i < notes_on.length; i++) {
  56. uint8_t note = notes_on.values[i].note;
  57. if (tone == notes_on.values[i].tone) {
  58. midi_send_noteoff(&midi_device, channel, note, velocity);
  59. dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
  60. for (uint8_t j=i; j < notes_on.length - 1; j++)
  61. {
  62. notes_on.values[j] = notes_on.values[j + 1];
  63. }
  64. notes_on.length--;
  65. break;
  66. }
  67. }
  68. }
  69. return false;
  70. }
  71. case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
  72. if (record->event.pressed)
  73. midi_config.octave = keycode - MIDI_OCTAVE_MIN;
  74. return false;
  75. case MI_OCTD:
  76. if (record->event.pressed && midi_config.octave > 0)
  77. midi_config.octave--;
  78. return false;
  79. case MI_OCTU:
  80. if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN))
  81. midi_config.octave++;
  82. return false;
  83. case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
  84. if (record->event.pressed)
  85. midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
  86. return false;
  87. case MI_VELD:
  88. if (record->event.pressed && midi_config.velocity > 0)
  89. midi_config.velocity--;
  90. return false;
  91. case MI_VELU:
  92. if (record->event.pressed)
  93. midi_config.velocity++;
  94. return false;
  95. case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
  96. if (record->event.pressed)
  97. midi_config.channel = keycode - MIDI_CHANNEL_MIN;
  98. return false;
  99. case MI_CHD:
  100. if (record->event.pressed)
  101. midi_config.channel--;
  102. return false;
  103. case MI_CHU:
  104. if (record->event.pressed)
  105. midi_config.channel++;
  106. return false;
  107. case MI_SUS:
  108. //TODO
  109. return false;
  110. };
  111. return true;
  112. }