1 /* Copyright (c) 2010-2011 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #define MAX_MIDI_MESSAGE_SIZE 256 // Max message size. SysEx can be up to 65536 but 256 should be fine for most usage
26 // MIDI Message Format
28 // [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ]
30 // MIDI Data Messages (Channel Specific)
33 // ---------------------------------------------
34 // Note Off 0x8 Key Velocity
35 // Note On 0x9 Key Velocity
36 // Polyphonic Aftertouch 0xA Key Pressure
37 // Control Change 0xB Controller Value
38 // Program Change 0xC Program -
39 // Channel Aftertouch 0xD Pressure -
40 // Pitch Wheel 0xE LSB MSB
42 #define CABLE_NUM (0<<4)
44 /** A MIDI message container */
47 MIDIMessage() : length(4) {}
49 MIDIMessage(uint8_t *buf) : length(4) {
50 for (int i = 0; i < 4; i++)
54 // New constructor, buf is a true MIDI message (not USBMidi message) and buf_len true message length.
55 MIDIMessage(uint8_t *buf, int buf_len) {
57 // first byte keeped for retro-compatibility
60 for (int i = 0; i < buf_len; i++)
66 /** Create a NoteOff message
68 * @param velocity Key velocity (0-127, default = 127)
69 * @param channel Key channel (0-15, default 0)
70 * @returns A MIDIMessage
72 static MIDIMessage NoteOff(int key, int velocity = 127, int channel = 0) {
74 msg.data[0] = CABLE_NUM | 0x08;
75 msg.data[1] = 0x80 | (channel & 0x0F);
76 msg.data[2] = key & 0x7F;
77 msg.data[3] = velocity & 0x7F;
81 /** Create a NoteOn message
83 * @param velocity Key velocity (0-127, default = 127)
84 * @param channel Key channel (0-15, default 0)
85 * @returns A MIDIMessage
87 static MIDIMessage NoteOn(int key, int velocity = 127, int channel = 0) {
89 msg.data[0] = CABLE_NUM | 0x09;
90 msg.data[1] = 0x90 | (channel & 0x0F);
91 msg.data[2] = key & 0x7F;
92 msg.data[3] = velocity & 0x7F;
96 /** Create a PolyPhonic Aftertouch message
98 * @param pressure Aftertouch pressure (0-127)
99 * @param channel Key channel (0-15, default 0)
100 * @returns A MIDIMessage
102 static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel = 0) {
104 msg.data[0] = CABLE_NUM | 0x0A;
105 msg.data[1] = 0xA0 | (channel & 0x0F);
106 msg.data[2] = key & 0x7F;
107 msg.data[3] = pressure & 0x7F;
111 /** Create a Control Change message
112 * @param control Controller ID
113 * @param value Controller value (0-127)
114 * @param channel Controller channel (0-15, default 0)
115 * @returns A MIDIMessage
117 static MIDIMessage ControlChange(int control, int value, int channel = 0) {
119 msg.data[0] = CABLE_NUM | 0x0B;
120 msg.data[1] = 0xB0 | (channel & 0x0F);
121 msg.data[2] = control & 0x7F;
122 msg.data[3] = value & 0x7F;
126 /** Create a Program Change message
127 * @param program Program ID
128 * @param channel Channel (0-15, default 0)
129 * @returns A MIDIMessage
131 static MIDIMessage ProgramChange(int program, int channel = 0) {
133 msg.data[0] = CABLE_NUM | 0x0C;
134 msg.data[1] = 0xC0 | (channel & 0x0F);
135 msg.data[2] = program & 0x7F;
140 /** Create a Channel Aftertouch message
141 * @param pressure Pressure
142 * @param channel Key channel (0-15, default 0)
143 * @returns A MIDIMessage
145 static MIDIMessage ChannelAftertouch(int pressure, int channel = 0) {
147 msg.data[0] = CABLE_NUM | 0x0D;
148 msg.data[1] = 0xD0 | (channel & 0x0F);
149 msg.data[2] = pressure & 0x7F;
154 /** Create a Pitch Wheel message
155 * @param pitch Pitch (-8192 - 8191, default = 0)
156 * @param channel Channel (0-15, default 0)
157 * @returns A MIDIMessage
159 static MIDIMessage PitchWheel(int pitch = 0, int channel = 0) {
161 int p = pitch + 8192; // 0 - 16383, 8192 is center
162 msg.data[0] = CABLE_NUM | 0x0E;
163 msg.data[1] = 0xE0 | (channel & 0x0F);
164 msg.data[2] = p & 0x7F;
165 msg.data[3] = (p >> 7) & 0x7F;
169 /** Create an All Notes Off message
170 * @param channel Channel (0-15, default 0)
171 * @returns A MIDIMessage
173 static MIDIMessage AllNotesOff(int channel = 0) {
174 return ControlChange(123, 0, channel);
177 /** Create a SysEx message
178 * @param data SysEx data (including 0xF0 .. 0xF7)
179 * @param len SysEx data length
180 * @returns A MIDIMessage
182 static MIDIMessage SysEx(uint8_t *data, int len) {
183 MIDIMessage msg=MIDIMessage(data,len);
189 /** MIDI Message Types */
190 enum MIDIMessageType {
194 PolyphonicAftertouchType,
197 ChannelAftertouchType,
203 /** Read the message type
204 * @returns MIDIMessageType
206 MIDIMessageType type() {
207 switch((data[1] >> 4) & 0xF) {
208 case 0x8: return NoteOffType;
209 case 0x9: return NoteOnType;
210 case 0xA: return PolyphonicAftertouchType;
212 if(controller() < 120) { // standard controllers
213 return ControlChangeType;
214 } else if(controller() == 123) {
215 return AllNotesOffType;
217 return ErrorType; // unsupported atm
219 case 0xC: return ProgramChangeType;
220 case 0xD: return ChannelAftertouchType;
221 case 0xE: return PitchWheelType;
222 case 0xF: return SysExType;
223 default: return ErrorType;
227 /** Read the channel number */
229 return (data[1] & 0x0F);
232 /** Read the key ID */
234 return (data[2] & 0x7F);
237 /** Read the velocity */
239 return (data[3] & 0x7F);
242 /** Read the controller value */
244 return (data[3] & 0x7F);
247 /** Read the aftertouch pressure */
249 if(type() == PolyphonicAftertouchType) {
250 return (data[3] & 0x7F);
252 return (data[2] & 0x7F);
256 /** Read the controller number */
258 return (data[2] & 0x7F);
261 /** Read the program number */
263 return (data[2] & 0x7F);
266 /** Read the pitch value */
268 int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F);
269 return p - 8192; // 0 - 16383, 8192 is center
272 uint8_t data[MAX_MIDI_MESSAGE_SIZE+1];