process_midi.c 3.3 KB

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