max4146x_comp

Dependencies:   MAX14690

Committer:
sdivarci
Date:
Sun Oct 25 20:10:02 2020 +0000
Revision:
0:0061165683ee
sdivarci

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sdivarci 0:0061165683ee 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
sdivarci 0:0061165683ee 2 *
sdivarci 0:0061165683ee 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
sdivarci 0:0061165683ee 4 * and associated documentation files (the "Software"), to deal in the Software without
sdivarci 0:0061165683ee 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
sdivarci 0:0061165683ee 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
sdivarci 0:0061165683ee 7 * Software is furnished to do so, subject to the following conditions:
sdivarci 0:0061165683ee 8 *
sdivarci 0:0061165683ee 9 * The above copyright notice and this permission notice shall be included in all copies or
sdivarci 0:0061165683ee 10 * substantial portions of the Software.
sdivarci 0:0061165683ee 11 *
sdivarci 0:0061165683ee 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
sdivarci 0:0061165683ee 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
sdivarci 0:0061165683ee 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
sdivarci 0:0061165683ee 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sdivarci 0:0061165683ee 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
sdivarci 0:0061165683ee 17 */
sdivarci 0:0061165683ee 18
sdivarci 0:0061165683ee 19 #ifndef MIDIMESSAGE_H
sdivarci 0:0061165683ee 20 #define MIDIMESSAGE_H
sdivarci 0:0061165683ee 21
sdivarci 0:0061165683ee 22 #include "mbed.h"
sdivarci 0:0061165683ee 23
sdivarci 0:0061165683ee 24 #define MAX_MIDI_MESSAGE_SIZE 256 // Max message size. SysEx can be up to 65536 but 256 should be fine for most usage
sdivarci 0:0061165683ee 25
sdivarci 0:0061165683ee 26 // MIDI Message Format
sdivarci 0:0061165683ee 27 //
sdivarci 0:0061165683ee 28 // [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ]
sdivarci 0:0061165683ee 29 //
sdivarci 0:0061165683ee 30 // MIDI Data Messages (Channel Specific)
sdivarci 0:0061165683ee 31 //
sdivarci 0:0061165683ee 32 // Message msg n m
sdivarci 0:0061165683ee 33 // ---------------------------------------------
sdivarci 0:0061165683ee 34 // Note Off 0x8 Key Velocity
sdivarci 0:0061165683ee 35 // Note On 0x9 Key Velocity
sdivarci 0:0061165683ee 36 // Polyphonic Aftertouch 0xA Key Pressure
sdivarci 0:0061165683ee 37 // Control Change 0xB Controller Value
sdivarci 0:0061165683ee 38 // Program Change 0xC Program -
sdivarci 0:0061165683ee 39 // Channel Aftertouch 0xD Pressure -
sdivarci 0:0061165683ee 40 // Pitch Wheel 0xE LSB MSB
sdivarci 0:0061165683ee 41
sdivarci 0:0061165683ee 42 #define CABLE_NUM (0<<4)
sdivarci 0:0061165683ee 43
sdivarci 0:0061165683ee 44 /** A MIDI message container */
sdivarci 0:0061165683ee 45 class MIDIMessage {
sdivarci 0:0061165683ee 46 public:
sdivarci 0:0061165683ee 47 MIDIMessage() : length(4) {}
sdivarci 0:0061165683ee 48
sdivarci 0:0061165683ee 49 MIDIMessage(uint8_t *buf) : length(4) {
sdivarci 0:0061165683ee 50 for (int i = 0; i < 4; i++)
sdivarci 0:0061165683ee 51 data[i] = buf[i];
sdivarci 0:0061165683ee 52 }
sdivarci 0:0061165683ee 53
sdivarci 0:0061165683ee 54 // New constructor, buf is a true MIDI message (not USBMidi message) and buf_len true message length.
sdivarci 0:0061165683ee 55 MIDIMessage(uint8_t *buf, int buf_len) {
sdivarci 0:0061165683ee 56 length=buf_len+1;
sdivarci 0:0061165683ee 57 // first byte keeped for retro-compatibility
sdivarci 0:0061165683ee 58 data[0]=0;
sdivarci 0:0061165683ee 59
sdivarci 0:0061165683ee 60 for (int i = 0; i < buf_len; i++)
sdivarci 0:0061165683ee 61 data[i+1] = buf[i];
sdivarci 0:0061165683ee 62 }
sdivarci 0:0061165683ee 63
sdivarci 0:0061165683ee 64 // create messages
sdivarci 0:0061165683ee 65
sdivarci 0:0061165683ee 66 /** Create a NoteOff message
sdivarci 0:0061165683ee 67 * @param key Key ID
sdivarci 0:0061165683ee 68 * @param velocity Key velocity (0-127, default = 127)
sdivarci 0:0061165683ee 69 * @param channel Key channel (0-15, default 0)
sdivarci 0:0061165683ee 70 * @returns A MIDIMessage
sdivarci 0:0061165683ee 71 */
sdivarci 0:0061165683ee 72 static MIDIMessage NoteOff(int key, int velocity = 127, int channel = 0) {
sdivarci 0:0061165683ee 73 MIDIMessage msg;
sdivarci 0:0061165683ee 74 msg.data[0] = CABLE_NUM | 0x08;
sdivarci 0:0061165683ee 75 msg.data[1] = 0x80 | (channel & 0x0F);
sdivarci 0:0061165683ee 76 msg.data[2] = key & 0x7F;
sdivarci 0:0061165683ee 77 msg.data[3] = velocity & 0x7F;
sdivarci 0:0061165683ee 78 return msg;
sdivarci 0:0061165683ee 79 }
sdivarci 0:0061165683ee 80
sdivarci 0:0061165683ee 81 /** Create a NoteOn message
sdivarci 0:0061165683ee 82 * @param key Key ID
sdivarci 0:0061165683ee 83 * @param velocity Key velocity (0-127, default = 127)
sdivarci 0:0061165683ee 84 * @param channel Key channel (0-15, default 0)
sdivarci 0:0061165683ee 85 * @returns A MIDIMessage
sdivarci 0:0061165683ee 86 */
sdivarci 0:0061165683ee 87 static MIDIMessage NoteOn(int key, int velocity = 127, int channel = 0) {
sdivarci 0:0061165683ee 88 MIDIMessage msg;
sdivarci 0:0061165683ee 89 msg.data[0] = CABLE_NUM | 0x09;
sdivarci 0:0061165683ee 90 msg.data[1] = 0x90 | (channel & 0x0F);
sdivarci 0:0061165683ee 91 msg.data[2] = key & 0x7F;
sdivarci 0:0061165683ee 92 msg.data[3] = velocity & 0x7F;
sdivarci 0:0061165683ee 93 return msg;
sdivarci 0:0061165683ee 94 }
sdivarci 0:0061165683ee 95
sdivarci 0:0061165683ee 96 /** Create a PolyPhonic Aftertouch message
sdivarci 0:0061165683ee 97 * @param key Key ID
sdivarci 0:0061165683ee 98 * @param pressure Aftertouch pressure (0-127)
sdivarci 0:0061165683ee 99 * @param channel Key channel (0-15, default 0)
sdivarci 0:0061165683ee 100 * @returns A MIDIMessage
sdivarci 0:0061165683ee 101 */
sdivarci 0:0061165683ee 102 static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel = 0) {
sdivarci 0:0061165683ee 103 MIDIMessage msg;
sdivarci 0:0061165683ee 104 msg.data[0] = CABLE_NUM | 0x0A;
sdivarci 0:0061165683ee 105 msg.data[1] = 0xA0 | (channel & 0x0F);
sdivarci 0:0061165683ee 106 msg.data[2] = key & 0x7F;
sdivarci 0:0061165683ee 107 msg.data[3] = pressure & 0x7F;
sdivarci 0:0061165683ee 108 return msg;
sdivarci 0:0061165683ee 109 }
sdivarci 0:0061165683ee 110
sdivarci 0:0061165683ee 111 /** Create a Control Change message
sdivarci 0:0061165683ee 112 * @param control Controller ID
sdivarci 0:0061165683ee 113 * @param value Controller value (0-127)
sdivarci 0:0061165683ee 114 * @param channel Controller channel (0-15, default 0)
sdivarci 0:0061165683ee 115 * @returns A MIDIMessage
sdivarci 0:0061165683ee 116 */
sdivarci 0:0061165683ee 117 static MIDIMessage ControlChange(int control, int value, int channel = 0) {
sdivarci 0:0061165683ee 118 MIDIMessage msg;
sdivarci 0:0061165683ee 119 msg.data[0] = CABLE_NUM | 0x0B;
sdivarci 0:0061165683ee 120 msg.data[1] = 0xB0 | (channel & 0x0F);
sdivarci 0:0061165683ee 121 msg.data[2] = control & 0x7F;
sdivarci 0:0061165683ee 122 msg.data[3] = value & 0x7F;
sdivarci 0:0061165683ee 123 return msg;
sdivarci 0:0061165683ee 124 }
sdivarci 0:0061165683ee 125
sdivarci 0:0061165683ee 126 /** Create a Program Change message
sdivarci 0:0061165683ee 127 * @param program Program ID
sdivarci 0:0061165683ee 128 * @param channel Channel (0-15, default 0)
sdivarci 0:0061165683ee 129 * @returns A MIDIMessage
sdivarci 0:0061165683ee 130 */
sdivarci 0:0061165683ee 131 static MIDIMessage ProgramChange(int program, int channel = 0) {
sdivarci 0:0061165683ee 132 MIDIMessage msg;
sdivarci 0:0061165683ee 133 msg.data[0] = CABLE_NUM | 0x0C;
sdivarci 0:0061165683ee 134 msg.data[1] = 0xC0 | (channel & 0x0F);
sdivarci 0:0061165683ee 135 msg.data[2] = program & 0x7F;
sdivarci 0:0061165683ee 136 msg.data[3] = 0x00;
sdivarci 0:0061165683ee 137 return msg;
sdivarci 0:0061165683ee 138 }
sdivarci 0:0061165683ee 139
sdivarci 0:0061165683ee 140 /** Create a Channel Aftertouch message
sdivarci 0:0061165683ee 141 * @param pressure Pressure
sdivarci 0:0061165683ee 142 * @param channel Key channel (0-15, default 0)
sdivarci 0:0061165683ee 143 * @returns A MIDIMessage
sdivarci 0:0061165683ee 144 */
sdivarci 0:0061165683ee 145 static MIDIMessage ChannelAftertouch(int pressure, int channel = 0) {
sdivarci 0:0061165683ee 146 MIDIMessage msg;
sdivarci 0:0061165683ee 147 msg.data[0] = CABLE_NUM | 0x0D;
sdivarci 0:0061165683ee 148 msg.data[1] = 0xD0 | (channel & 0x0F);
sdivarci 0:0061165683ee 149 msg.data[2] = pressure & 0x7F;
sdivarci 0:0061165683ee 150 msg.data[3] = 0x00;
sdivarci 0:0061165683ee 151 return msg;
sdivarci 0:0061165683ee 152 }
sdivarci 0:0061165683ee 153
sdivarci 0:0061165683ee 154 /** Create a Pitch Wheel message
sdivarci 0:0061165683ee 155 * @param pitch Pitch (-8192 - 8191, default = 0)
sdivarci 0:0061165683ee 156 * @param channel Channel (0-15, default 0)
sdivarci 0:0061165683ee 157 * @returns A MIDIMessage
sdivarci 0:0061165683ee 158 */
sdivarci 0:0061165683ee 159 static MIDIMessage PitchWheel(int pitch = 0, int channel = 0) {
sdivarci 0:0061165683ee 160 MIDIMessage msg;
sdivarci 0:0061165683ee 161 int p = pitch + 8192; // 0 - 16383, 8192 is center
sdivarci 0:0061165683ee 162 msg.data[0] = CABLE_NUM | 0x0E;
sdivarci 0:0061165683ee 163 msg.data[1] = 0xE0 | (channel & 0x0F);
sdivarci 0:0061165683ee 164 msg.data[2] = p & 0x7F;
sdivarci 0:0061165683ee 165 msg.data[3] = (p >> 7) & 0x7F;
sdivarci 0:0061165683ee 166 return msg;
sdivarci 0:0061165683ee 167 }
sdivarci 0:0061165683ee 168
sdivarci 0:0061165683ee 169 /** Create an All Notes Off message
sdivarci 0:0061165683ee 170 * @param channel Channel (0-15, default 0)
sdivarci 0:0061165683ee 171 * @returns A MIDIMessage
sdivarci 0:0061165683ee 172 */
sdivarci 0:0061165683ee 173 static MIDIMessage AllNotesOff(int channel = 0) {
sdivarci 0:0061165683ee 174 return ControlChange(123, 0, channel);
sdivarci 0:0061165683ee 175 }
sdivarci 0:0061165683ee 176
sdivarci 0:0061165683ee 177 /** Create a SysEx message
sdivarci 0:0061165683ee 178 * @param data SysEx data (including 0xF0 .. 0xF7)
sdivarci 0:0061165683ee 179 * @param len SysEx data length
sdivarci 0:0061165683ee 180 * @returns A MIDIMessage
sdivarci 0:0061165683ee 181 */
sdivarci 0:0061165683ee 182 static MIDIMessage SysEx(uint8_t *data, int len) {
sdivarci 0:0061165683ee 183 MIDIMessage msg=MIDIMessage(data,len);
sdivarci 0:0061165683ee 184 return msg;
sdivarci 0:0061165683ee 185 }
sdivarci 0:0061165683ee 186
sdivarci 0:0061165683ee 187 // decode messages
sdivarci 0:0061165683ee 188
sdivarci 0:0061165683ee 189 /** MIDI Message Types */
sdivarci 0:0061165683ee 190 enum MIDIMessageType {
sdivarci 0:0061165683ee 191 ErrorType,
sdivarci 0:0061165683ee 192 NoteOffType,
sdivarci 0:0061165683ee 193 NoteOnType,
sdivarci 0:0061165683ee 194 PolyphonicAftertouchType,
sdivarci 0:0061165683ee 195 ControlChangeType,
sdivarci 0:0061165683ee 196 ProgramChangeType,
sdivarci 0:0061165683ee 197 ChannelAftertouchType,
sdivarci 0:0061165683ee 198 PitchWheelType,
sdivarci 0:0061165683ee 199 AllNotesOffType,
sdivarci 0:0061165683ee 200 SysExType
sdivarci 0:0061165683ee 201 };
sdivarci 0:0061165683ee 202
sdivarci 0:0061165683ee 203 /** Read the message type
sdivarci 0:0061165683ee 204 * @returns MIDIMessageType
sdivarci 0:0061165683ee 205 */
sdivarci 0:0061165683ee 206 MIDIMessageType type() {
sdivarci 0:0061165683ee 207 switch((data[1] >> 4) & 0xF) {
sdivarci 0:0061165683ee 208 case 0x8: return NoteOffType;
sdivarci 0:0061165683ee 209 case 0x9: return NoteOnType;
sdivarci 0:0061165683ee 210 case 0xA: return PolyphonicAftertouchType;
sdivarci 0:0061165683ee 211 case 0xB:
sdivarci 0:0061165683ee 212 if(controller() < 120) { // standard controllers
sdivarci 0:0061165683ee 213 return ControlChangeType;
sdivarci 0:0061165683ee 214 } else if(controller() == 123) {
sdivarci 0:0061165683ee 215 return AllNotesOffType;
sdivarci 0:0061165683ee 216 } else {
sdivarci 0:0061165683ee 217 return ErrorType; // unsupported atm
sdivarci 0:0061165683ee 218 }
sdivarci 0:0061165683ee 219 case 0xC: return ProgramChangeType;
sdivarci 0:0061165683ee 220 case 0xD: return ChannelAftertouchType;
sdivarci 0:0061165683ee 221 case 0xE: return PitchWheelType;
sdivarci 0:0061165683ee 222 case 0xF: return SysExType;
sdivarci 0:0061165683ee 223 default: return ErrorType;
sdivarci 0:0061165683ee 224 }
sdivarci 0:0061165683ee 225 }
sdivarci 0:0061165683ee 226
sdivarci 0:0061165683ee 227 /** Read the channel number */
sdivarci 0:0061165683ee 228 int channel() {
sdivarci 0:0061165683ee 229 return (data[1] & 0x0F);
sdivarci 0:0061165683ee 230 }
sdivarci 0:0061165683ee 231
sdivarci 0:0061165683ee 232 /** Read the key ID */
sdivarci 0:0061165683ee 233 int key() {
sdivarci 0:0061165683ee 234 return (data[2] & 0x7F);
sdivarci 0:0061165683ee 235 }
sdivarci 0:0061165683ee 236
sdivarci 0:0061165683ee 237 /** Read the velocity */
sdivarci 0:0061165683ee 238 int velocity() {
sdivarci 0:0061165683ee 239 return (data[3] & 0x7F);
sdivarci 0:0061165683ee 240 }
sdivarci 0:0061165683ee 241
sdivarci 0:0061165683ee 242 /** Read the controller value */
sdivarci 0:0061165683ee 243 int value() {
sdivarci 0:0061165683ee 244 return (data[3] & 0x7F);
sdivarci 0:0061165683ee 245 }
sdivarci 0:0061165683ee 246
sdivarci 0:0061165683ee 247 /** Read the aftertouch pressure */
sdivarci 0:0061165683ee 248 int pressure() {
sdivarci 0:0061165683ee 249 if(type() == PolyphonicAftertouchType) {
sdivarci 0:0061165683ee 250 return (data[3] & 0x7F);
sdivarci 0:0061165683ee 251 } else {
sdivarci 0:0061165683ee 252 return (data[2] & 0x7F);
sdivarci 0:0061165683ee 253 }
sdivarci 0:0061165683ee 254 }
sdivarci 0:0061165683ee 255
sdivarci 0:0061165683ee 256 /** Read the controller number */
sdivarci 0:0061165683ee 257 int controller() {
sdivarci 0:0061165683ee 258 return (data[2] & 0x7F);
sdivarci 0:0061165683ee 259 }
sdivarci 0:0061165683ee 260
sdivarci 0:0061165683ee 261 /** Read the program number */
sdivarci 0:0061165683ee 262 int program() {
sdivarci 0:0061165683ee 263 return (data[2] & 0x7F);
sdivarci 0:0061165683ee 264 }
sdivarci 0:0061165683ee 265
sdivarci 0:0061165683ee 266 /** Read the pitch value */
sdivarci 0:0061165683ee 267 int pitch() {
sdivarci 0:0061165683ee 268 int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F);
sdivarci 0:0061165683ee 269 return p - 8192; // 0 - 16383, 8192 is center
sdivarci 0:0061165683ee 270 }
sdivarci 0:0061165683ee 271
sdivarci 0:0061165683ee 272 uint8_t data[MAX_MIDI_MESSAGE_SIZE+1];
sdivarci 0:0061165683ee 273 uint8_t length;
sdivarci 0:0061165683ee 274 };
sdivarci 0:0061165683ee 275
sdivarci 0:0061165683ee 276 #endif