]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_midi.c
Alternative version with a tone array
[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_USE_NOTE_ON_ARRAY
23
24 #ifdef MIDI_USE_NOTE_ON_ARRAY
25
26 #define MIDI_MAX_NOTES_ON 10
27
28 typedef struct {
29     uint8_t note;
30     uint8_t tone;
31 } midi_notes_on_array_entry_t;
32
33 typedef struct {
34     uint8_t length;
35     midi_notes_on_array_entry_t values[MIDI_MAX_NOTES_ON];
36 } midi_notes_on_array_t;
37
38 static midi_notes_on_array_t notes_on;
39
40 #else
41
42 #define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1)
43 static uint8_t tone_status[MIDI_TONE_COUNT];
44
45 #endif
46
47
48
49 inline uint8_t compute_velocity(uint8_t setting)
50 {
51     return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
52 }
53
54 void midi_init(void)
55 {
56     midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN;
57     midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
58     midi_config.channel = 0;
59     #ifdef MIDI_USE_NOTE_ON_ARRAY
60     notes_on.length = 0;
61     #else
62     for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
63     {
64         tone_status[i] = MIDI_INVALID_NOTE;
65     }
66     #endif
67 }
68
69 bool process_midi(uint16_t keycode, keyrecord_t *record)
70 {
71     switch (keycode) {
72         case MIDI_TONE_MIN ... MIDI_TONE_MAX:
73         {
74             uint8_t channel = midi_config.channel;
75             uint8_t tone = keycode - MIDI_TONE_MIN;
76             uint8_t velocity = compute_velocity(midi_config.velocity);
77             #ifdef MIDI_USE_NOTE_ON_ARRAY
78             if (record->event.pressed && notes_on.length < MIDI_MAX_NOTES_ON) {
79             #else
80             if (record->event.pressed) {
81             #endif
82                 uint8_t note = 12 * midi_config.octave + tone;
83                 midi_send_noteon(&midi_device, channel, note, velocity);
84                 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
85
86                 #ifdef MIDI_USE_NOTE_ON_ARRAY
87                 
88                 notes_on.values[notes_on.length].note = note;
89                 notes_on.values[notes_on.length].tone = tone;
90                 notes_on.length++;
91                 
92                 #else
93
94                 tone_status[tone] = note;
95
96                 #endif
97             }
98             else {
99                 
100                 #ifdef MIDI_USE_NOTE_ON_ARRAY
101
102                 for (uint8_t i = 0; i < notes_on.length; i++) {
103                     uint8_t note = notes_on.values[i].note;
104                     if (tone == notes_on.values[i].tone) {
105                         midi_send_noteoff(&midi_device, channel, note, velocity);
106                         dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
107
108                         for (uint8_t j=i; j < notes_on.length - 1; j++)
109                         {
110                             notes_on.values[j] = notes_on.values[j + 1];
111                         }
112
113                         notes_on.length--;
114                         break;
115                     }
116                 }
117
118                 #else
119
120                 uint8_t note = tone_status[tone];
121                 if (note != MIDI_INVALID_NOTE)
122                 {
123                     midi_send_noteoff(&midi_device, channel, note, velocity);
124                     dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
125                 }
126                 tone_status[tone] = MIDI_INVALID_NOTE;
127
128                 #endif
129             }
130             return false;
131         }
132         case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
133             if (record->event.pressed)
134                 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
135             return false;
136         case MI_OCTD:
137             if (record->event.pressed && midi_config.octave > 0)
138                 midi_config.octave--;
139             return false;
140         case MI_OCTU:
141             if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN))
142                 midi_config.octave++;
143             return false;
144         case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
145             if (record->event.pressed)
146                 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
147             return false;
148         case MI_VELD:
149             if (record->event.pressed && midi_config.velocity > 0)
150                 midi_config.velocity--;
151             return false;
152         case MI_VELU:
153             if (record->event.pressed)
154                 midi_config.velocity++;
155             return false;
156         case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
157             if (record->event.pressed)
158                 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
159             return false;
160         case MI_CHD:
161             if (record->event.pressed)
162                 midi_config.channel--;
163             return false;
164         case MI_CHU:
165             if (record->event.pressed)
166                 midi_config.channel++;
167             return false;
168         case MI_SUS:
169             //TODO
170             return false;
171     };
172
173 #if 0
174     if (keycode == MI_ON && record->event.pressed) {
175       midi_activated = true;
176 #ifdef AUDIO_ENABLE
177       music_scale_user();
178 #endif
179       return false;
180     }
181
182     if (keycode == MI_OFF && record->event.pressed) {
183       midi_activated = false;
184       midi_send_cc(&midi_device, 0, 0x7B, 0);
185       return false;
186     }
187
188     if (midi_activated) {
189       if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
190           if (record->event.pressed) {
191               midi_starting_note++; // Change key
192               midi_send_cc(&midi_device, 0, 0x7B, 0);
193           }
194           return false;
195       }
196       if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
197           if (record->event.pressed) {
198               midi_starting_note--; // Change key
199               midi_send_cc(&midi_device, 0, 0x7B, 0);
200           }
201           return false;
202       }
203       if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
204           midi_offset++; // Change scale
205           midi_send_cc(&midi_device, 0, 0x7B, 0);
206           return false;
207       }
208       if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
209           midi_offset--; // Change scale
210           midi_send_cc(&midi_device, 0, 0x7B, 0);
211           return false;
212       }
213       // basic
214       // uint8_t note = (midi_starting_note + SCALE[record->event.key.col + midi_offset])+12*(MATRIX_ROWS - record->event.key.row);
215       // advanced
216       // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+12*(MATRIX_ROWS - record->event.key.row);
217       // guitar
218       uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+5*(MATRIX_ROWS - record->event.key.row);
219       // violin
220       // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+7*(MATRIX_ROWS - record->event.key.row);
221
222       if (record->event.pressed) {
223         // midi_send_noteon(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
224         midi_send_noteon(&midi_device, 0, note, 127);
225       } else {
226         // midi_send_noteoff(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
227         midi_send_noteoff(&midi_device, 0, note, 127);
228       }
229
230       if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
231         return false;
232     }
233 #endif
234     return true;
235 }