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
|
#include "midi_event.h"
const char *MIDIEvent::cc_names[CC_MAX]{
"BankSelectMSB",
"Modulation",
"Breath",
"Foot",
"PortamentoTime",
"DataEntryMSB",
"MainVolume",
"Pan",
"Expression",
"BankSelectLSB",
"DataEntryLSB",
"DamperPedalToggle",
"PortamentoToggle",
"SostenutoToggle",
"SoftPedalToggle",
"FilterCutoff",
"ReleaseTime",
"AttackTime",
"FilterResonance",
"DecayTime",
"VibratoDepth",
"VibratoRate",
"VibratoDelay",
"PortamentoControl",
"ReverbSend",
"Fx2Send",
"ChorusSend",
"Fx4Send",
"DataIncrement",
"DataDecrement",
"NRPN_LSB",
"NRPN_MSB",
"RPN_LSB",
"RPN_MSB",
"AllSoundsOffCmd",
"ResetAllCmd",
"LocalCtrlToggle",
"AllNotesOff"
};
const unsigned char MIDIEvent::cc_indices[CC_MAX]{
0,
1,
2,
4,
5,
6,
7,
10,
11,
32,
38,
64,
65,
66,
67,
71,
72,
73,
74,
75,
76,
77,
78,
84,
91,
92,
93,
94,
96,
97,
98,
99,
120,
121,
122,
123,
128
};
Error MIDIEvent::parse(unsigned char *p_raw) {
switch (p_raw[0] >> 4) {
case 0x8: {
type = MIDI_NOTE_OFF;
note.note = p_raw[1];
note.velocity = p_raw[2];
} break;
case 0x9: {
type = MIDI_NOTE_ON;
note.note = p_raw[1];
note.velocity = p_raw[2];
} break;
case 0xA: {
type = MIDI_NOTE_PRESSURE;
note.note = p_raw[1];
note.velocity = p_raw[2];
} break;
case 0xB: {
type = MIDI_CONTROLLER;
control.index = p_raw[1];
control.parameter = p_raw[2];
} break;
case 0xC: {
type = MIDI_PATCH_SELECT;
patch.index = p_raw[1];
} break;
case 0xD: {
type = MIDI_AFTERTOUCH;
aftertouch.pressure = p_raw[1];
} break;
case 0xE: {
type = MIDI_PITCH_BEND;
pitch_bend.bend = (short(p_raw[1]) << 7) | short(p_raw[2]);
} break;
default: {
return ERR_INVALID_PARAMETER;
}
}
channel = p_raw[0] & 0xF;
return OK;
}
bool MIDIEvent::write(unsigned char *p_to) const {
if (type == MIDI_NOTE_OFF) {
p_to[0] = 0x8;
p_to[0] <<= 4;
p_to[0] |= channel;
p_to[1] = note.note;
p_to[2] = note.velocity;
} else if (type == MIDI_NOTE_ON) {
p_to[0] = 0x9;
p_to[0] <<= 4;
p_to[0] |= channel;
p_to[1] = note.note;
p_to[2] = note.velocity;
} else if (type == MIDI_NOTE_PRESSURE) {
p_to[0] = 0xA;
p_to[0] <<= 4;
p_to[0] |= channel;
p_to[1] = note.note;
p_to[2] = note.velocity;
} else if (type == MIDI_CONTROLLER) {
p_to[0] = 0xB;
p_to[0] <<= 4;
p_to[0] |= channel;
p_to[1] = control.index;
p_to[2] = control.parameter;
} else if (type == MIDI_PATCH_SELECT) {
p_to[0] = 0xC;
p_to[0] <<= 4;
p_to[0] |= channel;
p_to[1] = patch.index;
p_to[2] = 0;
} else if (type == MIDI_AFTERTOUCH) {
p_to[0] = 0xD;
p_to[0] <<= 4;
p_to[0] |= channel;
p_to[1] = aftertouch.pressure;
p_to[2] = 0;
} else if (type == MIDI_PITCH_BEND) {
p_to[0] = 0xD;
p_to[0] <<= 4;
p_to[0] |= channel;
p_to[1] = pitch_bend.bend >> 7;
p_to[2] = pitch_bend.bend & 0x7F;
} else {
return false;
}
return true;
}
MIDIEvent::MIDIEvent() {
type = NONE;
raw.param1 = 0;
raw.param2 = 0;
}
MIDIEvent::MIDIEvent(Type p_type, unsigned char p_chan, unsigned char data1, unsigned char data2) {
type = p_type;
raw.param1 = data1;
raw.param2 = data2;
}
MIDIEvent::MIDIEvent(Type p_type, unsigned char p_chan, unsigned short data) {
type = p_type;
raw2.param = data;
}
|