USBDevice for STM support
Dependents: DISCO-F429ZI_LCDTS_demo_richard
Fork of USBDEVICE by
USBMIDI/MIDIMessage.h@6:5a6efd08d588, 2017-06-27 (annotated)
- Committer:
- pierrebizouard
- Date:
- Tue Jun 27 13:11:46 2017 +0000
- Revision:
- 6:5a6efd08d588
- Parent:
- 1:2a3ae13b45ef
fixup in definition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 2 | * |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 7 | * Software is furnished to do so, subject to the following conditions: |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 8 | * |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 10 | * substantial portions of the Software. |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 11 | * |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 17 | */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 18 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 19 | #ifndef MIDIMESSAGE_H |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 20 | #define MIDIMESSAGE_H |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 21 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 22 | #include "mbed.h" |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 23 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 24 | #define MAX_MIDI_MESSAGE_SIZE 256 // Max message size. SysEx can be up to 65536 but 256 should be fine for most usage |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 25 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 26 | // MIDI Message Format |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 27 | // |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 28 | // [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ] |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 29 | // |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 30 | // MIDI Data Messages (Channel Specific) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 31 | // |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 32 | // Message msg n m |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 33 | // --------------------------------------------- |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 34 | // Note Off 0x8 Key Velocity |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 35 | // Note On 0x9 Key Velocity |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 36 | // Polyphonic Aftertouch 0xA Key Pressure |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 37 | // Control Change 0xB Controller Value |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 38 | // Program Change 0xC Program - |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 39 | // Channel Aftertouch 0xD Pressure - |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 40 | // Pitch Wheel 0xE LSB MSB |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 41 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 42 | #define CABLE_NUM (0<<4) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 43 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 44 | /** A MIDI message container */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 45 | class MIDIMessage { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 46 | public: |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 47 | MIDIMessage() : length(4) {} |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 48 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 49 | MIDIMessage(uint8_t *buf) : length(4) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 50 | for (int i = 0; i < 4; i++) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 51 | data[i] = buf[i]; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 52 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 53 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 54 | // New constructor, buf is a true MIDI message (not USBMidi message) and buf_len true message length. |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 55 | MIDIMessage(uint8_t *buf, int buf_len) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 56 | length=buf_len+1; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 57 | // first byte keeped for retro-compatibility |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 58 | data[0]=0; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 59 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 60 | for (int i = 0; i < buf_len; i++) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 61 | data[i+1] = buf[i]; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 62 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 63 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 64 | // create messages |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 65 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 66 | /** Create a NoteOff message |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 67 | * @param key Key ID |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 68 | * @param velocity Key velocity (0-127, default = 127) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 69 | * @param channel Key channel (0-15, default 0) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 70 | * @returns A MIDIMessage |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 71 | */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 72 | static MIDIMessage NoteOff(int key, int velocity = 127, int channel = 0) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 73 | MIDIMessage msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 74 | msg.data[0] = CABLE_NUM | 0x08; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 75 | msg.data[1] = 0x80 | (channel & 0x0F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 76 | msg.data[2] = key & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 77 | msg.data[3] = velocity & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 78 | return msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 79 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 80 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 81 | /** Create a NoteOn message |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 82 | * @param key Key ID |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 83 | * @param velocity Key velocity (0-127, default = 127) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 84 | * @param channel Key channel (0-15, default 0) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 85 | * @returns A MIDIMessage |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 86 | */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 87 | static MIDIMessage NoteOn(int key, int velocity = 127, int channel = 0) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 88 | MIDIMessage msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 89 | msg.data[0] = CABLE_NUM | 0x09; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 90 | msg.data[1] = 0x90 | (channel & 0x0F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 91 | msg.data[2] = key & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 92 | msg.data[3] = velocity & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 93 | return msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 94 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 95 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 96 | /** Create a PolyPhonic Aftertouch message |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 97 | * @param key Key ID |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 98 | * @param pressure Aftertouch pressure (0-127) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 99 | * @param channel Key channel (0-15, default 0) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 100 | * @returns A MIDIMessage |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 101 | */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 102 | static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel = 0) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 103 | MIDIMessage msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 104 | msg.data[0] = CABLE_NUM | 0x0A; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 105 | msg.data[1] = 0xA0 | (channel & 0x0F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 106 | msg.data[2] = key & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 107 | msg.data[3] = pressure & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 108 | return msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 109 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 110 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 111 | /** Create a Control Change message |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 112 | * @param control Controller ID |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 113 | * @param value Controller value (0-127) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 114 | * @param channel Controller channel (0-15, default 0) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 115 | * @returns A MIDIMessage |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 116 | */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 117 | static MIDIMessage ControlChange(int control, int value, int channel = 0) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 118 | MIDIMessage msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 119 | msg.data[0] = CABLE_NUM | 0x0B; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 120 | msg.data[1] = 0xB0 | (channel & 0x0F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 121 | msg.data[2] = control & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 122 | msg.data[3] = value & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 123 | return msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 124 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 125 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 126 | /** Create a Program Change message |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 127 | * @param program Program ID |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 128 | * @param channel Channel (0-15, default 0) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 129 | * @returns A MIDIMessage |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 130 | */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 131 | static MIDIMessage ProgramChange(int program, int channel = 0) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 132 | MIDIMessage msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 133 | msg.data[0] = CABLE_NUM | 0x0C; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 134 | msg.data[1] = 0xC0 | (channel & 0x0F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 135 | msg.data[2] = program & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 136 | msg.data[3] = 0x00; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 137 | return msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 138 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 139 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 140 | /** Create a Channel Aftertouch message |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 141 | * @param pressure Pressure |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 142 | * @param channel Key channel (0-15, default 0) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 143 | * @returns A MIDIMessage |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 144 | */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 145 | static MIDIMessage ChannelAftertouch(int pressure, int channel = 0) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 146 | MIDIMessage msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 147 | msg.data[0] = CABLE_NUM | 0x0D; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 148 | msg.data[1] = 0xD0 | (channel & 0x0F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 149 | msg.data[2] = pressure & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 150 | msg.data[3] = 0x00; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 151 | return msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 152 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 153 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 154 | /** Create a Pitch Wheel message |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 155 | * @param pitch Pitch (-8192 - 8191, default = 0) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 156 | * @param channel Channel (0-15, default 0) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 157 | * @returns A MIDIMessage |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 158 | */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 159 | static MIDIMessage PitchWheel(int pitch = 0, int channel = 0) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 160 | MIDIMessage msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 161 | int p = pitch + 8192; // 0 - 16383, 8192 is center |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 162 | msg.data[0] = CABLE_NUM | 0x0E; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 163 | msg.data[1] = 0xE0 | (channel & 0x0F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 164 | msg.data[2] = p & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 165 | msg.data[3] = (p >> 7) & 0x7F; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 166 | return msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 167 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 168 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 169 | /** Create an All Notes Off message |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 170 | * @param channel Channel (0-15, default 0) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 171 | * @returns A MIDIMessage |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 172 | */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 173 | static MIDIMessage AllNotesOff(int channel = 0) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 174 | return ControlChange(123, 0, channel); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 175 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 176 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 177 | /** Create a SysEx message |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 178 | * @param data SysEx data (including 0xF0 .. 0xF7) |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 179 | * @param len SysEx data length |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 180 | * @returns A MIDIMessage |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 181 | */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 182 | static MIDIMessage SysEx(uint8_t *data, int len) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 183 | MIDIMessage msg=MIDIMessage(data,len); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 184 | return msg; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 185 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 186 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 187 | // decode messages |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 188 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 189 | /** MIDI Message Types */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 190 | enum MIDIMessageType { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 191 | ErrorType, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 192 | NoteOffType, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 193 | NoteOnType, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 194 | PolyphonicAftertouchType, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 195 | ControlChangeType, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 196 | ProgramChangeType, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 197 | ChannelAftertouchType, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 198 | PitchWheelType, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 199 | AllNotesOffType, |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 200 | SysExType |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 201 | }; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 202 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 203 | /** Read the message type |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 204 | * @returns MIDIMessageType |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 205 | */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 206 | MIDIMessageType type() { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 207 | switch((data[1] >> 4) & 0xF) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 208 | case 0x8: return NoteOffType; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 209 | case 0x9: return NoteOnType; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 210 | case 0xA: return PolyphonicAftertouchType; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 211 | case 0xB: |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 212 | if(controller() < 120) { // standard controllers |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 213 | return ControlChangeType; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 214 | } else if(controller() == 123) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 215 | return AllNotesOffType; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 216 | } else { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 217 | return ErrorType; // unsupported atm |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 218 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 219 | case 0xC: return ProgramChangeType; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 220 | case 0xD: return ChannelAftertouchType; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 221 | case 0xE: return PitchWheelType; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 222 | case 0xF: return SysExType; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 223 | default: return ErrorType; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 224 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 225 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 226 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 227 | /** Read the channel number */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 228 | int channel() { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 229 | return (data[1] & 0x0F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 230 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 231 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 232 | /** Read the key ID */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 233 | int key() { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 234 | return (data[2] & 0x7F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 235 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 236 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 237 | /** Read the velocity */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 238 | int velocity() { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 239 | return (data[3] & 0x7F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 240 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 241 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 242 | /** Read the controller value */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 243 | int value() { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 244 | return (data[3] & 0x7F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 245 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 246 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 247 | /** Read the aftertouch pressure */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 248 | int pressure() { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 249 | if(type() == PolyphonicAftertouchType) { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 250 | return (data[3] & 0x7F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 251 | } else { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 252 | return (data[2] & 0x7F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 253 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 254 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 255 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 256 | /** Read the controller number */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 257 | int controller() { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 258 | return (data[2] & 0x7F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 259 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 260 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 261 | /** Read the program number */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 262 | int program() { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 263 | return (data[2] & 0x7F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 264 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 265 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 266 | /** Read the pitch value */ |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 267 | int pitch() { |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 268 | int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F); |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 269 | return p - 8192; // 0 - 16383, 8192 is center |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 270 | } |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 271 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 272 | uint8_t data[MAX_MIDI_MESSAGE_SIZE+1]; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 273 | uint8_t length; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 274 | }; |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 275 | |
frq08711@LMECWL0871.LME.ST.COM | 1:2a3ae13b45ef | 276 | #endif |