fork
Fork of USBDevice_STM32F103 by
Diff: USBMIDI/MIDIMessage.h
- Revision:
- 25:7c72828865f3
- Parent:
- 11:eeb3cbbaa996
- Child:
- 48:03f8e580579a
--- a/USBMIDI/MIDIMessage.h Fri May 16 09:00:39 2014 +0100 +++ b/USBMIDI/MIDIMessage.h Tue Jun 03 11:30:32 2014 +0100 @@ -22,7 +22,7 @@ #include "mbed.h" // MIDI Message Format -// +// // [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ] // // MIDI Data Messages (Channel Specific) @@ -43,15 +43,15 @@ class MIDIMessage { public: MIDIMessage() {} - + MIDIMessage(uint8_t *buf) { for (int i = 0; i < 4; i++) data[i] = buf[i]; } - + // create messages - - /** Create a NoteOff message + + /** Create a NoteOff message * @param key Key ID * @param velocity Key velocity (0-127, default = 127) * @param channel Key channel (0-15, default 0) @@ -62,11 +62,11 @@ msg.data[0] = CABLE_NUM | 0x08; msg.data[1] = 0x80 | (channel & 0x0F); msg.data[2] = key & 0x7F; - msg.data[3] = velocity & 0x7F; + msg.data[3] = velocity & 0x7F; return msg; } - - /** Create a NoteOn message + + /** Create a NoteOn message * @param key Key ID * @param velocity Key velocity (0-127, default = 127) * @param channel Key channel (0-15, default 0) @@ -77,26 +77,26 @@ msg.data[0] = CABLE_NUM | 0x09; msg.data[1] = 0x90 | (channel & 0x0F); msg.data[2] = key & 0x7F; - msg.data[3] = velocity & 0x7F; + msg.data[3] = velocity & 0x7F; return msg; } - - /** Create a PolyPhonic Aftertouch message + + /** 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; + msg.data[3] = pressure & 0x7F; return msg; } - - /** Create a Control Change message + + /** Create a Control Change message * @param control Controller ID * @param value Controller value (0-127) * @param channel Controller channel (0-15, default 0) @@ -107,63 +107,63 @@ msg.data[0] = CABLE_NUM | 0x0B; msg.data[1] = 0xB0 | (channel & 0x0F); msg.data[2] = control & 0x7F; - msg.data[3] = value & 0x7F; + msg.data[3] = value & 0x7F; return msg; } - - /** Create a Program Change message + + /** 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; + msg.data[3] = 0x00; return msg; } - - /** Create a Channel Aftertouch message - * @param pressure Pressure + + /** 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; + msg.data[3] = 0x00; return msg; } - - /** Create a Pitch Wheel message + + /** 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; + msg.data[3] = (p >> 7) & 0x7F; return msg; } - - /** Create an All Notes Off message + + /** 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); } - + // decode messages - + /** MIDI Message Types */ enum MIDIMessageType { ErrorType, @@ -176,16 +176,16 @@ PitchWheelType, AllNotesOffType }; - + /** 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: + case 0xB: if(controller() < 120) { // standard controllers return ControlChangeType; } else if(controller() == 123) { @@ -200,51 +200,51 @@ } } - /** Read the channel number */ + /** Read the channel number */ int channel() { return (data[1] & 0x0F); } - - /** Read the key ID */ + + /** Read the key ID */ int key() { - return (data[2] & 0x7F); + return (data[2] & 0x7F); } - - /** Read the velocity */ + + /** Read the velocity */ int velocity() { - return (data[3] & 0x7F); + return (data[3] & 0x7F); } - /** Read the controller value */ + /** Read the controller value */ int value() { - return (data[3] & 0x7F); + return (data[3] & 0x7F); } - - /** Read the aftertouch pressure */ + + /** Read the aftertouch pressure */ int pressure() { if(type() == PolyphonicAftertouchType) { - return (data[3] & 0x7F); + return (data[3] & 0x7F); } else { - return (data[2] & 0x7F); + return (data[2] & 0x7F); } } - /** Read the controller number */ + /** Read the controller number */ int controller() { - return (data[2] & 0x7F); + return (data[2] & 0x7F); } - /** Read the program number */ + /** Read the program number */ int program() { - return (data[2] & 0x7F); + return (data[2] & 0x7F); } - - /** Read the pitch value */ + + /** 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[4]; };