summaryrefslogtreecommitdiff
path: root/tool/mbed/mbed-sdk/libraries/USBDevice/USBMIDI/MIDIMessage.h
blob: 9cfcf13635d11ae519d5f36801d4af88000c7a33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* Copyright (c) 2010-2011 mbed.org, MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef MIDIMESSAGE_H
#define MIDIMESSAGE_H

#include "mbed.h"

#define MAX_MIDI_MESSAGE_SIZE 256 // Max message size. SysEx can be up to 65536 but 256 should be fine for most usage

// MIDI Message Format
//
// [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ]
//
// MIDI Data Messages (Channel Specific)
//
// Message               msg n          m
// ---------------------------------------------
// Note Off              0x8 Key        Velocity
// Note On               0x9 Key        Velocity
// Polyphonic Aftertouch 0xA Key        Pressure
// Control Change        0xB Controller Value
// Program Change        0xC Program    -
// Channel Aftertouch    0xD Pressure   -
// Pitch Wheel           0xE LSB        MSB

#define CABLE_NUM (0<<4)

/** A MIDI message container */
class MIDIMessage {
public:
    MIDIMessage() : length(4) {}

    MIDIMessage(uint8_t *buf) : length(4) {
        for (int i = 0; i < 4; i++)
            data[i] = buf[i];
    }

    // New constructor, buf is a true MIDI message (not USBMidi message) and buf_len true message length.
    MIDIMessage(uint8_t *buf, int buf_len) {
        length=buf_len+1;
        // first byte keeped for retro-compatibility
        data[0]=0;

        for (int i = 0; i < buf_len; i++)
            data[i+1] = buf[i];
    }

    // create messages

    /** Create a NoteOff message
     * @param key Key ID
     * @param velocity Key velocity (0-127, default = 127)
     * @param channel Key channel (0-15, default 0)
     * @returns A MIDIMessage
     */
    static MIDIMessage NoteOff(int key, int velocity = 127, int channel = 0) {
        MIDIMessage msg;
        msg.data[0] = CABLE_NUM | 0x08;
        msg.data[1] = 0x80 | (channel & 0x0F);
        msg.data[2] = key & 0x7F;
        msg.data[3] = velocity & 0x7F;
        return msg;
    }

    /** Create a NoteOn message
     * @param key Key ID
     * @param velocity Key velocity (0-127, default = 127)
     * @param channel Key channel (0-15, default 0)
     * @returns A MIDIMessage
     */
    static MIDIMessage NoteOn(int key, int velocity = 127, int channel = 0) {
        MIDIMessage msg;
        msg.data[0] = CABLE_NUM | 0x09;
        msg.data[1] = 0x90 | (channel & 0x0F);
        msg.data[2] = key & 0x7F;
        msg.data[3] = velocity & 0x7F;
        return msg;
    }

    /** Create a PolyPhonic Aftertouch message
     * @param key Key ID
     * @param pressure Aftertouch pressure (0-127)
     * @param channel Key channel (0-15, default 0)
     * @returns A MIDIMessage
     */
    static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel = 0) {
        MIDIMessage msg;
        msg.data[0] = CABLE_NUM | 0x0A;
        msg.data[1] = 0xA0 | (channel & 0x0F);
        msg.data[2] = key & 0x7F;
        msg.data[3] = pressure & 0x7F;
        return msg;
    }

    /** Create a Control Change message
     * @param control Controller ID
     * @param value Controller value (0-127)
     * @param channel Controller channel (0-15, default 0)
     * @returns A MIDIMessage
     */
    static MIDIMessage ControlChange(int control, int value, int channel = 0) {
        MIDIMessage msg;
        msg.data[0] = CABLE_NUM | 0x0B;
        msg.data[1] = 0xB0 | (channel & 0x0F);
        msg.data[2] = control & 0x7F;
        msg.data[3] = value & 0x7F;
        return msg;
    }

    /** Create a Program Change message
     * @param program Program ID
     * @param channel Channel (0-15, default 0)
     * @returns A MIDIMessage
     */
    static MIDIMessage ProgramChange(int program, int channel = 0) {
        MIDIMessage msg;
        msg.data[0] = CABLE_NUM | 0x0C;
        msg.data[1] = 0xC0 | (channel & 0x0F);
        msg.data[2] = program & 0x7F;
        msg.data[3] = 0x00;
        return msg;
    }

    /** Create a Channel Aftertouch message
     * @param pressure Pressure
     * @param channel Key channel (0-15, default 0)
     * @returns A MIDIMessage
     */
    static MIDIMessage ChannelAftertouch(int pressure, int channel = 0) {
        MIDIMessage msg;
        msg.data[0] = CABLE_NUM | 0x0D;
        msg.data[1] = 0xD0 | (channel & 0x0F);
        msg.data[2] = pressure & 0x7F;
        msg.data[3] = 0x00;
        return msg;
    }

    /** Create a Pitch Wheel message
     * @param pitch Pitch (-8192 - 8191, default = 0)
     * @param channel Channel (0-15, default 0)
     * @returns A MIDIMessage
     */
    static MIDIMessage PitchWheel(int pitch = 0, int channel = 0) {
        MIDIMessage msg;
        int p = pitch + 8192;    // 0 - 16383, 8192 is center
        msg.data[0] = CABLE_NUM | 0x0E;
        msg.data[1] = 0xE0 | (channel & 0x0F);
        msg.data[2] = p & 0x7F;
        msg.data[3] = (p >> 7) & 0x7F;
        return msg;
    }

    /** Create an All Notes Off message
     * @param channel Channel (0-15, default 0)
     * @returns A MIDIMessage
     */
    static MIDIMessage AllNotesOff(int channel = 0) {
        return ControlChange(123, 0, channel);
    }

     /** Create a SysEx message
     * @param data SysEx data (including 0xF0 .. 0xF7)
     * @param len SysEx data length
     * @returns A MIDIMessage
     */
    static MIDIMessage SysEx(uint8_t *data, int len) {
        MIDIMessage msg=MIDIMessage(data,len);
        return msg;
    }

    // decode messages

    /** MIDI Message Types */
    enum MIDIMessageType {
        ErrorType,
        NoteOffType,
        NoteOnType,
        PolyphonicAftertouchType,
        ControlChangeType,
        ProgramChangeType,
        ChannelAftertouchType,
        PitchWheelType,
        AllNotesOffType,
        SysExType
    };

    /** Read the message type
     * @returns MIDIMessageType
     */
    MIDIMessageType type() {
        switch((data[1] >> 4) & 0xF) {
            case 0x8: return NoteOffType;
            case 0x9: return NoteOnType;
            case 0xA: return PolyphonicAftertouchType;
            case 0xB:
                if(controller() < 120) { // standard controllers
                    return ControlChangeType;
                } else if(controller() == 123) {
                    return AllNotesOffType;
                } else {
                    return ErrorType; // unsupported atm
                }
            case 0xC: return ProgramChangeType;
            case 0xD: return ChannelAftertouchType;
            case 0xE: return PitchWheelType;
            case 0xF: return SysExType;
            default: return ErrorType;
        }
    }

    /** Read the channel number */
    int channel() {
        return (data[1] & 0x0F);
    }

    /** Read the key ID */
    int key() {
        return (data[2] & 0x7F);
    }

    /** Read the velocity */
    int velocity() {
        return (data[3] & 0x7F);
    }

    /** Read the controller value */
    int value() {
        return (data[3] & 0x7F);
    }

    /** Read the aftertouch pressure */
    int pressure() {
        if(type() == PolyphonicAftertouchType) {
            return (data[3] & 0x7F);
        } else {
            return (data[2] & 0x7F);
        }
    }

    /** Read the controller number */
    int controller() {
        return (data[2] & 0x7F);
    }

    /** Read the program number */
    int program() {
        return (data[2] & 0x7F);
    }

    /** Read the pitch value */
    int pitch() {
        int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F);
        return p - 8192; // 0 - 16383, 8192 is center
    }

    uint8_t data[MAX_MIDI_MESSAGE_SIZE+1];
    uint8_t length;
};

#endif