![](/media/cache/profiles/90d89eccfe88456d58a2872a9350f6a9.50x50_q85.jpg)
KAMUI USB MIDI-CV Example
midi_parser.c@0:9ddbc6e0cd90, 2012-05-05 (annotated)
- Committer:
- radiojunkbox
- Date:
- Sat May 05 11:36:56 2012 +0000
- Revision:
- 0:9ddbc6e0cd90
Rev. 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
radiojunkbox | 0:9ddbc6e0cd90 | 1 | //------------------------------------------------------------- |
radiojunkbox | 0:9ddbc6e0cd90 | 2 | // KAMUI MIDI-CV Exapmle |
radiojunkbox | 0:9ddbc6e0cd90 | 3 | // file : midi_parser.c |
radiojunkbox | 0:9ddbc6e0cd90 | 4 | // Copyright (C) 2012 RJB RadioJunkBox |
radiojunkbox | 0:9ddbc6e0cd90 | 5 | // Released under the MIT License: http://mbed.org/license/mit |
radiojunkbox | 0:9ddbc6e0cd90 | 6 | //------------------------------------------------------------- |
radiojunkbox | 0:9ddbc6e0cd90 | 7 | |
radiojunkbox | 0:9ddbc6e0cd90 | 8 | #include "mbed.h" |
radiojunkbox | 0:9ddbc6e0cd90 | 9 | #include "midi_parser.h" |
radiojunkbox | 0:9ddbc6e0cd90 | 10 | |
radiojunkbox | 0:9ddbc6e0cd90 | 11 | //------------------------------------------------------------- |
radiojunkbox | 0:9ddbc6e0cd90 | 12 | // MIDI Parser |
radiojunkbox | 0:9ddbc6e0cd90 | 13 | |
radiojunkbox | 0:9ddbc6e0cd90 | 14 | void MIDI_Parser(unsigned char mididata) |
radiojunkbox | 0:9ddbc6e0cd90 | 15 | { |
radiojunkbox | 0:9ddbc6e0cd90 | 16 | RxByte = mididata; |
radiojunkbox | 0:9ddbc6e0cd90 | 17 | |
radiojunkbox | 0:9ddbc6e0cd90 | 18 | if(MIDI_SystemMessage()) { |
radiojunkbox | 0:9ddbc6e0cd90 | 19 | MIDI_ChannelMessage(); |
radiojunkbox | 0:9ddbc6e0cd90 | 20 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 21 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 22 | |
radiojunkbox | 0:9ddbc6e0cd90 | 23 | //------------------------------------------------------------- |
radiojunkbox | 0:9ddbc6e0cd90 | 24 | // MIDI System Meassage |
radiojunkbox | 0:9ddbc6e0cd90 | 25 | |
radiojunkbox | 0:9ddbc6e0cd90 | 26 | int MIDI_SystemMessage(void) |
radiojunkbox | 0:9ddbc6e0cd90 | 27 | { |
radiojunkbox | 0:9ddbc6e0cd90 | 28 | if(SysEx){ |
radiojunkbox | 0:9ddbc6e0cd90 | 29 | if(RxByte == MIDI_EndSysEx){ |
radiojunkbox | 0:9ddbc6e0cd90 | 30 | SysEx = FALSE; |
radiojunkbox | 0:9ddbc6e0cd90 | 31 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 32 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 33 | else{ |
radiojunkbox | 0:9ddbc6e0cd90 | 34 | if(RxByte < 0xF8){ |
radiojunkbox | 0:9ddbc6e0cd90 | 35 | if(RxByte > 0x7F){ |
radiojunkbox | 0:9ddbc6e0cd90 | 36 | if(RxByte == MIDI_StartSysEx){ |
radiojunkbox | 0:9ddbc6e0cd90 | 37 | SysEx = TRUE; |
radiojunkbox | 0:9ddbc6e0cd90 | 38 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 39 | else{ |
radiojunkbox | 0:9ddbc6e0cd90 | 40 | MidiCh = RxByte & 0x0F; |
radiojunkbox | 0:9ddbc6e0cd90 | 41 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 42 | PC = 0; |
radiojunkbox | 0:9ddbc6e0cd90 | 43 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 44 | else{ |
radiojunkbox | 0:9ddbc6e0cd90 | 45 | MByte[PC & 0x01] = RxByte; |
radiojunkbox | 0:9ddbc6e0cd90 | 46 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 47 | return TRUE; |
radiojunkbox | 0:9ddbc6e0cd90 | 48 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 49 | else { |
radiojunkbox | 0:9ddbc6e0cd90 | 50 | MIDI_SystemRealtimeMessage(); |
radiojunkbox | 0:9ddbc6e0cd90 | 51 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 52 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 53 | return FALSE; |
radiojunkbox | 0:9ddbc6e0cd90 | 54 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 55 | |
radiojunkbox | 0:9ddbc6e0cd90 | 56 | //------------------------------------------------------------- |
radiojunkbox | 0:9ddbc6e0cd90 | 57 | // MIDI System Realtime Message |
radiojunkbox | 0:9ddbc6e0cd90 | 58 | |
radiojunkbox | 0:9ddbc6e0cd90 | 59 | void MIDI_SystemRealtimeMessage(void) |
radiojunkbox | 0:9ddbc6e0cd90 | 60 | { |
radiojunkbox | 0:9ddbc6e0cd90 | 61 | switch(RxByte) { |
radiojunkbox | 0:9ddbc6e0cd90 | 62 | case MIDI_TimingClock: |
radiojunkbox | 0:9ddbc6e0cd90 | 63 | gMIDISYNC_CLK |= 0x01; |
radiojunkbox | 0:9ddbc6e0cd90 | 64 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 65 | case MIDI_Start: |
radiojunkbox | 0:9ddbc6e0cd90 | 66 | gMIDISYNC_RUN = 0x01; |
radiojunkbox | 0:9ddbc6e0cd90 | 67 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 68 | case MIDI_Continue: |
radiojunkbox | 0:9ddbc6e0cd90 | 69 | gMIDISYNC_RUN = 0x01; |
radiojunkbox | 0:9ddbc6e0cd90 | 70 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 71 | case MIDI_Stop: |
radiojunkbox | 0:9ddbc6e0cd90 | 72 | gMIDISYNC_RUN = 0x00; |
radiojunkbox | 0:9ddbc6e0cd90 | 73 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 74 | case MIDI_ActiveSensing: |
radiojunkbox | 0:9ddbc6e0cd90 | 75 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 76 | case MIDI_SystemReset: |
radiojunkbox | 0:9ddbc6e0cd90 | 77 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 78 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 79 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 80 | |
radiojunkbox | 0:9ddbc6e0cd90 | 81 | //------------------------------------------------------------- |
radiojunkbox | 0:9ddbc6e0cd90 | 82 | // MIDI Channel Message |
radiojunkbox | 0:9ddbc6e0cd90 | 83 | |
radiojunkbox | 0:9ddbc6e0cd90 | 84 | void MIDI_ChannelMessage(void) |
radiojunkbox | 0:9ddbc6e0cd90 | 85 | { |
radiojunkbox | 0:9ddbc6e0cd90 | 86 | switch(PC){ |
radiojunkbox | 0:9ddbc6e0cd90 | 87 | case 0: |
radiojunkbox | 0:9ddbc6e0cd90 | 88 | switch(RxByte & 0xF0){ |
radiojunkbox | 0:9ddbc6e0cd90 | 89 | case MIDI_NoteOff: |
radiojunkbox | 0:9ddbc6e0cd90 | 90 | PC = 2; |
radiojunkbox | 0:9ddbc6e0cd90 | 91 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 92 | case MIDI_NoteOn: |
radiojunkbox | 0:9ddbc6e0cd90 | 93 | PC = 4; |
radiojunkbox | 0:9ddbc6e0cd90 | 94 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 95 | case MIDI_PolykeyPressure: |
radiojunkbox | 0:9ddbc6e0cd90 | 96 | PC = 6; |
radiojunkbox | 0:9ddbc6e0cd90 | 97 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 98 | case MIDI_ProgramChange: |
radiojunkbox | 0:9ddbc6e0cd90 | 99 | PC = 8; |
radiojunkbox | 0:9ddbc6e0cd90 | 100 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 101 | case MIDI_ControlChange: |
radiojunkbox | 0:9ddbc6e0cd90 | 102 | PC = 10; |
radiojunkbox | 0:9ddbc6e0cd90 | 103 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 104 | case MIDI_ChannelPressure: |
radiojunkbox | 0:9ddbc6e0cd90 | 105 | PC = 12; |
radiojunkbox | 0:9ddbc6e0cd90 | 106 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 107 | case MIDI_PitchBend: |
radiojunkbox | 0:9ddbc6e0cd90 | 108 | PC = 14; |
radiojunkbox | 0:9ddbc6e0cd90 | 109 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 110 | } break; |
radiojunkbox | 0:9ddbc6e0cd90 | 111 | |
radiojunkbox | 0:9ddbc6e0cd90 | 112 | // Note OFF |
radiojunkbox | 0:9ddbc6e0cd90 | 113 | case 2: |
radiojunkbox | 0:9ddbc6e0cd90 | 114 | PC = 3; |
radiojunkbox | 0:9ddbc6e0cd90 | 115 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 116 | case 3: |
radiojunkbox | 0:9ddbc6e0cd90 | 117 | PC = 2; |
radiojunkbox | 0:9ddbc6e0cd90 | 118 | NoteOFF(); |
radiojunkbox | 0:9ddbc6e0cd90 | 119 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 120 | |
radiojunkbox | 0:9ddbc6e0cd90 | 121 | // Note ON |
radiojunkbox | 0:9ddbc6e0cd90 | 122 | case 4: |
radiojunkbox | 0:9ddbc6e0cd90 | 123 | PC = 5; |
radiojunkbox | 0:9ddbc6e0cd90 | 124 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 125 | case 5: |
radiojunkbox | 0:9ddbc6e0cd90 | 126 | PC = 4; |
radiojunkbox | 0:9ddbc6e0cd90 | 127 | if( MByte[1] == 0){ |
radiojunkbox | 0:9ddbc6e0cd90 | 128 | NoteOFF(); |
radiojunkbox | 0:9ddbc6e0cd90 | 129 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 130 | else{ |
radiojunkbox | 0:9ddbc6e0cd90 | 131 | NoteON(); |
radiojunkbox | 0:9ddbc6e0cd90 | 132 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 133 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 134 | |
radiojunkbox | 0:9ddbc6e0cd90 | 135 | // Polyphonic Key Pressure |
radiojunkbox | 0:9ddbc6e0cd90 | 136 | case 6: |
radiojunkbox | 0:9ddbc6e0cd90 | 137 | PC = 7; |
radiojunkbox | 0:9ddbc6e0cd90 | 138 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 139 | case 7: |
radiojunkbox | 0:9ddbc6e0cd90 | 140 | PC = 6; |
radiojunkbox | 0:9ddbc6e0cd90 | 141 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 142 | |
radiojunkbox | 0:9ddbc6e0cd90 | 143 | // Program Change |
radiojunkbox | 0:9ddbc6e0cd90 | 144 | case 8: |
radiojunkbox | 0:9ddbc6e0cd90 | 145 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 146 | |
radiojunkbox | 0:9ddbc6e0cd90 | 147 | // Control Change |
radiojunkbox | 0:9ddbc6e0cd90 | 148 | case 10: PC = 11; break; |
radiojunkbox | 0:9ddbc6e0cd90 | 149 | case 11: |
radiojunkbox | 0:9ddbc6e0cd90 | 150 | switch(MByte[0]) { |
radiojunkbox | 0:9ddbc6e0cd90 | 151 | case MIDI_CC_Moduration: |
radiojunkbox | 0:9ddbc6e0cd90 | 152 | gModWheelBuf[MidiCh] = MByte[1] >> 2; |
radiojunkbox | 0:9ddbc6e0cd90 | 153 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 154 | case MIDI_CC_DataEntry: |
radiojunkbox | 0:9ddbc6e0cd90 | 155 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 156 | case MIDI_CC_RPN_LSB: |
radiojunkbox | 0:9ddbc6e0cd90 | 157 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 158 | case MIDI_CC_RPN_MSB: |
radiojunkbox | 0:9ddbc6e0cd90 | 159 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 160 | case MIDI_MM_AllSoundOff: |
radiojunkbox | 0:9ddbc6e0cd90 | 161 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 162 | case MIDI_MM_ResetAllControl: |
radiojunkbox | 0:9ddbc6e0cd90 | 163 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 164 | case MIDI_MM_AllNoteOff: |
radiojunkbox | 0:9ddbc6e0cd90 | 165 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 166 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 167 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 168 | |
radiojunkbox | 0:9ddbc6e0cd90 | 169 | // Channel Pressure |
radiojunkbox | 0:9ddbc6e0cd90 | 170 | case 12: |
radiojunkbox | 0:9ddbc6e0cd90 | 171 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 172 | |
radiojunkbox | 0:9ddbc6e0cd90 | 173 | // Pitch Bend |
radiojunkbox | 0:9ddbc6e0cd90 | 174 | case 14: |
radiojunkbox | 0:9ddbc6e0cd90 | 175 | PC = 15; |
radiojunkbox | 0:9ddbc6e0cd90 | 176 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 177 | |
radiojunkbox | 0:9ddbc6e0cd90 | 178 | case 15: |
radiojunkbox | 0:9ddbc6e0cd90 | 179 | PC = 14; |
radiojunkbox | 0:9ddbc6e0cd90 | 180 | gPitchBendBuf[MidiCh] = (MByte[1] << 1) | (MByte[0] >> 6); |
radiojunkbox | 0:9ddbc6e0cd90 | 181 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 182 | |
radiojunkbox | 0:9ddbc6e0cd90 | 183 | default: |
radiojunkbox | 0:9ddbc6e0cd90 | 184 | break; |
radiojunkbox | 0:9ddbc6e0cd90 | 185 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 186 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 187 | |
radiojunkbox | 0:9ddbc6e0cd90 | 188 | //------------------------------------------------------------- |
radiojunkbox | 0:9ddbc6e0cd90 | 189 | // Note ON Message Processing |
radiojunkbox | 0:9ddbc6e0cd90 | 190 | |
radiojunkbox | 0:9ddbc6e0cd90 | 191 | void NoteON(void) |
radiojunkbox | 0:9ddbc6e0cd90 | 192 | { |
radiojunkbox | 0:9ddbc6e0cd90 | 193 | unsigned char i; |
radiojunkbox | 0:9ddbc6e0cd90 | 194 | unsigned char h = 0; // higheest note |
radiojunkbox | 0:9ddbc6e0cd90 | 195 | |
radiojunkbox | 0:9ddbc6e0cd90 | 196 | // ignore note if registed buffer |
radiojunkbox | 0:9ddbc6e0cd90 | 197 | for(i = 0; i < NoteCnt[MidiCh]; i++) { |
radiojunkbox | 0:9ddbc6e0cd90 | 198 | if(NoteBuf[MidiCh][i] == MByte[0]) { |
radiojunkbox | 0:9ddbc6e0cd90 | 199 | return; |
radiojunkbox | 0:9ddbc6e0cd90 | 200 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 201 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 202 | |
radiojunkbox | 0:9ddbc6e0cd90 | 203 | // full note buffer? |
radiojunkbox | 0:9ddbc6e0cd90 | 204 | if(NoteCnt[MidiCh] == MAX_NOTE_CNT) { |
radiojunkbox | 0:9ddbc6e0cd90 | 205 | for(i = 0; i < (MAX_NOTE_CNT - 1); i++) { |
radiojunkbox | 0:9ddbc6e0cd90 | 206 | NoteBuf[MidiCh][i] = NoteBuf[MidiCh][i+1]; |
radiojunkbox | 0:9ddbc6e0cd90 | 207 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 208 | NoteBuf[MidiCh][MAX_NOTE_CNT - 1] = MByte[0]; |
radiojunkbox | 0:9ddbc6e0cd90 | 209 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 210 | else { |
radiojunkbox | 0:9ddbc6e0cd90 | 211 | NoteBuf[MidiCh][NoteCnt[MidiCh]] = MByte[0]; |
radiojunkbox | 0:9ddbc6e0cd90 | 212 | NoteCnt[MidiCh]++; |
radiojunkbox | 0:9ddbc6e0cd90 | 213 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 214 | |
radiojunkbox | 0:9ddbc6e0cd90 | 215 | // set highest note |
radiojunkbox | 0:9ddbc6e0cd90 | 216 | for(i = 0; i < NoteCnt[MidiCh]; i++) { |
radiojunkbox | 0:9ddbc6e0cd90 | 217 | if(h < NoteBuf[MidiCh][i]) { |
radiojunkbox | 0:9ddbc6e0cd90 | 218 | h = NoteBuf[MidiCh][i]; |
radiojunkbox | 0:9ddbc6e0cd90 | 219 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 220 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 221 | gPlayNoteBuf[MidiCh] = h; |
radiojunkbox | 0:9ddbc6e0cd90 | 222 | gGateBuf[MidiCh] = ON; |
radiojunkbox | 0:9ddbc6e0cd90 | 223 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 224 | |
radiojunkbox | 0:9ddbc6e0cd90 | 225 | //------------------------------------------------------------- |
radiojunkbox | 0:9ddbc6e0cd90 | 226 | // Note OFF Message Processing |
radiojunkbox | 0:9ddbc6e0cd90 | 227 | |
radiojunkbox | 0:9ddbc6e0cd90 | 228 | void NoteOFF(void) |
radiojunkbox | 0:9ddbc6e0cd90 | 229 | { |
radiojunkbox | 0:9ddbc6e0cd90 | 230 | unsigned char i; |
radiojunkbox | 0:9ddbc6e0cd90 | 231 | unsigned char h = 0; // highest note |
radiojunkbox | 0:9ddbc6e0cd90 | 232 | int flg = FALSE; |
radiojunkbox | 0:9ddbc6e0cd90 | 233 | |
radiojunkbox | 0:9ddbc6e0cd90 | 234 | // Delete Note If Registed Buffer |
radiojunkbox | 0:9ddbc6e0cd90 | 235 | for(i = 0; i < NoteCnt[MidiCh]; i++) { |
radiojunkbox | 0:9ddbc6e0cd90 | 236 | if(flg) { |
radiojunkbox | 0:9ddbc6e0cd90 | 237 | NoteBuf[MidiCh][i-1] = NoteBuf[MidiCh][i]; |
radiojunkbox | 0:9ddbc6e0cd90 | 238 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 239 | if(NoteBuf[MidiCh][i] == MByte[0]) { |
radiojunkbox | 0:9ddbc6e0cd90 | 240 | flg = TRUE; |
radiojunkbox | 0:9ddbc6e0cd90 | 241 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 242 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 243 | if(flg) NoteCnt[MidiCh]--; |
radiojunkbox | 0:9ddbc6e0cd90 | 244 | |
radiojunkbox | 0:9ddbc6e0cd90 | 245 | if(NoteCnt[MidiCh] == 0) { |
radiojunkbox | 0:9ddbc6e0cd90 | 246 | // Empty Buffer then Gate OFF |
radiojunkbox | 0:9ddbc6e0cd90 | 247 | gGateBuf[MidiCh] = OFF; |
radiojunkbox | 0:9ddbc6e0cd90 | 248 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 249 | else { |
radiojunkbox | 0:9ddbc6e0cd90 | 250 | // Highest Note |
radiojunkbox | 0:9ddbc6e0cd90 | 251 | for(i = 0; i < NoteCnt[MidiCh]; i++) { |
radiojunkbox | 0:9ddbc6e0cd90 | 252 | if( h < NoteBuf[MidiCh][i]) { |
radiojunkbox | 0:9ddbc6e0cd90 | 253 | h = NoteBuf[MidiCh][i]; |
radiojunkbox | 0:9ddbc6e0cd90 | 254 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 255 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 256 | gPlayNoteBuf[MidiCh] = h; |
radiojunkbox | 0:9ddbc6e0cd90 | 257 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 258 | } |
radiojunkbox | 0:9ddbc6e0cd90 | 259 |