23#define MAX_MIDI_MESSAGE_SIZE 256
41#define CABLE_NUM (0<<4)
54 MIDIMessage() : data(
new uint8_t[MAX_MIDI_MESSAGE_SIZE + 1]), length(0) {}
56 MIDIMessage(uint8_t *buf) : data(
new uint8_t[MAX_MIDI_MESSAGE_SIZE + 1]), length(0)
58 for (
int i = 0; i < 4; i++) {
76 length = other.length;
77 for (
int i = 0; i < length; i++) {
78 data[i] = other.data[i];
98 if (length > MAX_MIDI_MESSAGE_SIZE) {
105 data[0] = CABLE_NUM | 0x08;
107 for (
int i = 0; i < buf_len; i++) {
108 data[i + 1] = buf[i];
123 msg.data[0] = CABLE_NUM | 0x08;
124 msg.data[1] = 0x80 | (
channel & 0x0F);
125 msg.data[2] =
key & 0x7F;
140 msg.data[0] = CABLE_NUM | 0x09;
141 msg.data[1] = 0x90 | (
channel & 0x0F);
142 msg.data[2] =
key & 0x7F;
157 msg.data[0] = CABLE_NUM | 0x0A;
158 msg.data[1] = 0xA0 | (
channel & 0x0F);
159 msg.data[2] =
key & 0x7F;
174 msg.data[0] = CABLE_NUM | 0x0B;
175 msg.data[1] = 0xB0 | (
channel & 0x0F);
176 msg.data[2] = control & 0x7F;
177 msg.data[3] =
value & 0x7F;
190 msg.data[0] = CABLE_NUM | 0x0C;
191 msg.data[1] = 0xC0 | (
channel & 0x0F);
206 msg.data[0] = CABLE_NUM | 0x0D;
207 msg.data[1] = 0xD0 | (
channel & 0x0F);
222 int p =
pitch + 8192;
223 msg.data[0] = CABLE_NUM | 0x0E;
224 msg.data[1] = 0xE0 | (
channel & 0x0F);
225 msg.data[2] = p & 0x7F;
226 msg.data[3] = (p >> 7) & 0x7F;
259 PolyphonicAftertouchType,
262 ChannelAftertouchType,
264 ResetAllControllersType,
277 switch ((data[1] >> 4) & 0xF) {
283 message_type = NoteOffType;
290 message_type = NoteOnType;
297 message_type = PolyphonicAftertouchType;
303 if ((data[2] & 0x7F) < 120) {
304 message_type = ControlChangeType;
305 }
else if ((data[2] & 0x7F) == 121) {
306 message_type = ResetAllControllersType;
307 }
else if ((data[2] & 0x7F) == 123) {
308 message_type = AllNotesOffType;
310 message_type = ErrorType;
317 message_type = ProgramChangeType;
323 message_type = ChannelAftertouchType;
330 message_type = PitchWheelType;
334 message_type = SysExType;
337 message_type = ErrorType;
342 if (length < min_size) {
344 message_type = ErrorType;
357 return (data[1] & 0x0F);
368 if ((msg_type != NoteOffType) &&
369 (msg_type != NoteOnType) &&
370 (msg_type != PolyphonicAftertouchType)) {
374 return data[2] & 0x7F;
385 if ((msg_type != NoteOffType) &&
386 (msg_type != NoteOnType)) {
390 return data[3] & 0x7F;
401 if ((msg_type != ControlChangeType) &&
402 (msg_type != ResetAllControllersType) &&
403 (msg_type != AllNotesOffType)) {
407 return data[3] & 0x7F;
418 if ((msg_type != PolyphonicAftertouchType) &&
419 (msg_type != ChannelAftertouchType)) {
423 if (
type() == PolyphonicAftertouchType) {
424 return data[3] & 0x7F;
426 return data[2] & 0x7F;
438 if ((msg_type != ControlChangeType) &&
439 (msg_type != ResetAllControllersType) &&
440 (msg_type != AllNotesOffType)) {
444 return data[2] & 0x7F;
455 if (msg_type != ProgramChangeType) {
459 return data[2] & 0x7F;
470 if (msg_type != PitchWheelType) {
474 int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F);
A MIDI message container.
static MIDIMessage ChannelAftertouch(int pressure, int channel=0)
Create a Channel Aftertouch message.
static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel=0)
Create a PolyPhonic Aftertouch message.
void from_raw(uint8_t *buf, int buf_len)
Set this MIDIMessage to a raw MIDI message.
MIDIMessage & operator=(const MIDIMessage &other)
Assignment operator.
int key()
Read the key ID.
int pressure()
Read the aftertouch pressure.
MIDIMessage(const MIDIMessage &other)
Copy constructor.
static MIDIMessage NoteOff(int key, int velocity=127, int channel=0)
Create a NoteOff message.
int channel()
Read the channel number.
int pitch()
Read the pitch value.
int controller()
Read the controller number.
static MIDIMessage AllNotesOff(int channel=0)
Create an All Notes Off message.
static MIDIMessage ControlChange(int control, int value, int channel=0)
Create a Control Change message.
static MIDIMessage ProgramChange(int program, int channel=0)
Create a Program Change message.
int program()
Read the program number.
static MIDIMessage NoteOn(int key, int velocity=127, int channel=0)
Create a NoteOn message.
int value()
Read the controller value.
MIDIMessageType
MIDI Message Types.
MIDIMessageType type()
Read the message type.
static MIDIMessage PitchWheel(int pitch=0, int channel=0)
Create a Pitch Wheel message.
int velocity()
Read the velocity.
static MIDIMessage SysEx(uint8_t *data, int len)
Create a SysEx message.