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