]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/USBDevice/USBMIDI/MIDIMessage.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / USBDevice / USBMIDI / MIDIMessage.h
1 /* Copyright (c) 2010-2011 mbed.org, MIT License
2 *
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:
8 *
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
11 *
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.
17 */
18
19 #ifndef MIDIMESSAGE_H
20 #define MIDIMESSAGE_H
21
22 #include "mbed.h"
23
24 #define MAX_MIDI_MESSAGE_SIZE 256 // Max message size. SysEx can be up to 65536 but 256 should be fine for most usage
25
26 // MIDI Message Format
27 //
28 // [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ]
29 //
30 // MIDI Data Messages (Channel Specific)
31 //
32 // Message               msg n          m
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
41
42 #define CABLE_NUM (0<<4)
43
44 /** A MIDI message container */
45 class MIDIMessage {
46 public:
47     MIDIMessage() : length(4) {}
48
49     MIDIMessage(uint8_t *buf) : length(4) {
50         for (int i = 0; i < 4; i++)
51             data[i] = buf[i];
52     }
53
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) {
56         length=buf_len+1;
57         // first byte keeped for retro-compatibility
58         data[0]=0;
59
60         for (int i = 0; i < buf_len; i++)
61             data[i+1] = buf[i];
62     }
63
64     // create messages
65
66     /** Create a NoteOff message
67      * @param key Key ID
68      * @param velocity Key velocity (0-127, default = 127)
69      * @param channel Key channel (0-15, default 0)
70      * @returns A MIDIMessage
71      */
72     static MIDIMessage NoteOff(int key, int velocity = 127, int channel = 0) {
73         MIDIMessage msg;
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;
78         return msg;
79     }
80
81     /** Create a NoteOn message
82      * @param key Key ID
83      * @param velocity Key velocity (0-127, default = 127)
84      * @param channel Key channel (0-15, default 0)
85      * @returns A MIDIMessage
86      */
87     static MIDIMessage NoteOn(int key, int velocity = 127, int channel = 0) {
88         MIDIMessage msg;
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;
93         return msg;
94     }
95
96     /** Create a PolyPhonic Aftertouch message
97      * @param key Key ID
98      * @param pressure Aftertouch pressure (0-127)
99      * @param channel Key channel (0-15, default 0)
100      * @returns A MIDIMessage
101      */
102     static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel = 0) {
103         MIDIMessage msg;
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;
108         return msg;
109     }
110
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
116      */
117     static MIDIMessage ControlChange(int control, int value, int channel = 0) {
118         MIDIMessage msg;
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;
123         return msg;
124     }
125
126     /** Create a Program Change message
127      * @param program Program ID
128      * @param channel Channel (0-15, default 0)
129      * @returns A MIDIMessage
130      */
131     static MIDIMessage ProgramChange(int program, int channel = 0) {
132         MIDIMessage msg;
133         msg.data[0] = CABLE_NUM | 0x0C;
134         msg.data[1] = 0xC0 | (channel & 0x0F);
135         msg.data[2] = program & 0x7F;
136         msg.data[3] = 0x00;
137         return msg;
138     }
139
140     /** Create a Channel Aftertouch message
141      * @param pressure Pressure
142      * @param channel Key channel (0-15, default 0)
143      * @returns A MIDIMessage
144      */
145     static MIDIMessage ChannelAftertouch(int pressure, int channel = 0) {
146         MIDIMessage msg;
147         msg.data[0] = CABLE_NUM | 0x0D;
148         msg.data[1] = 0xD0 | (channel & 0x0F);
149         msg.data[2] = pressure & 0x7F;
150         msg.data[3] = 0x00;
151         return msg;
152     }
153
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
158      */
159     static MIDIMessage PitchWheel(int pitch = 0, int channel = 0) {
160         MIDIMessage msg;
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;
166         return msg;
167     }
168
169     /** Create an All Notes Off message
170      * @param channel Channel (0-15, default 0)
171      * @returns A MIDIMessage
172      */
173     static MIDIMessage AllNotesOff(int channel = 0) {
174         return ControlChange(123, 0, channel);
175     }
176
177      /** Create a SysEx message
178      * @param data SysEx data (including 0xF0 .. 0xF7)
179      * @param len SysEx data length
180      * @returns A MIDIMessage
181      */
182     static MIDIMessage SysEx(uint8_t *data, int len) {
183         MIDIMessage msg=MIDIMessage(data,len);
184         return msg;
185     }
186
187     // decode messages
188
189     /** MIDI Message Types */
190     enum MIDIMessageType {
191         ErrorType,
192         NoteOffType,
193         NoteOnType,
194         PolyphonicAftertouchType,
195         ControlChangeType,
196         ProgramChangeType,
197         ChannelAftertouchType,
198         PitchWheelType,
199         AllNotesOffType,
200         SysExType
201     };
202
203     /** Read the message type
204      * @returns MIDIMessageType
205      */
206     MIDIMessageType type() {
207         switch((data[1] >> 4) & 0xF) {
208             case 0x8: return NoteOffType;
209             case 0x9: return NoteOnType;
210             case 0xA: return PolyphonicAftertouchType;
211             case 0xB:
212                 if(controller() < 120) { // standard controllers
213                     return ControlChangeType;
214                 } else if(controller() == 123) {
215                     return AllNotesOffType;
216                 } else {
217                     return ErrorType; // unsupported atm
218                 }
219             case 0xC: return ProgramChangeType;
220             case 0xD: return ChannelAftertouchType;
221             case 0xE: return PitchWheelType;
222             case 0xF: return SysExType;
223             default: return ErrorType;
224         }
225     }
226
227     /** Read the channel number */
228     int channel() {
229         return (data[1] & 0x0F);
230     }
231
232     /** Read the key ID */
233     int key() {
234         return (data[2] & 0x7F);
235     }
236
237     /** Read the velocity */
238     int velocity() {
239         return (data[3] & 0x7F);
240     }
241
242     /** Read the controller value */
243     int value() {
244         return (data[3] & 0x7F);
245     }
246
247     /** Read the aftertouch pressure */
248     int pressure() {
249         if(type() == PolyphonicAftertouchType) {
250             return (data[3] & 0x7F);
251         } else {
252             return (data[2] & 0x7F);
253         }
254     }
255
256     /** Read the controller number */
257     int controller() {
258         return (data[2] & 0x7F);
259     }
260
261     /** Read the program number */
262     int program() {
263         return (data[2] & 0x7F);
264     }
265
266     /** Read the pitch value */
267     int pitch() {
268         int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F);
269         return p - 8192; // 0 - 16383, 8192 is center
270     }
271
272     uint8_t data[MAX_MIDI_MESSAGE_SIZE+1];
273     uint8_t length;
274 };
275
276 #endif