| 1 |
/* |
| 2 |
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2024 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 14 марта 2016 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_PROTOCOL_MIDI_H_ |
| 23 |
#define LSP_PLUG_IN_PROTOCOL_MIDI_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/common/types.h> |
| 27 |
|
| 28 |
namespace lsp |
| 29 |
{ |
| 30 |
// Generalized midi event structure |
| 31 |
namespace midi |
| 32 |
{ |
| 33 |
#pragma pack(push, 1) |
| 34 |
typedef struct event_t |
| 35 |
{ |
| 36 |
uint32_t timestamp; // Timestamp |
| 37 |
uint8_t type; // Type of event |
| 38 |
uint8_t channel; // ID of the MIDI channel |
| 39 |
union |
| 40 |
{ |
| 41 |
// For MIDI_MSG_NOTE_OFF and MIDI_MSG_NOTE_ON |
| 42 |
struct |
| 43 |
{ |
| 44 |
uint8_t pitch; // Note key |
| 45 |
uint8_t velocity; // Note velocity |
| 46 |
} note; |
| 47 |
|
| 48 |
// For MIDI_MSG_NOTE_CONTROLLER |
| 49 |
struct |
| 50 |
{ |
| 51 |
uint8_t control; // Control |
| 52 |
uint8_t value; // Value |
| 53 |
} ctl; |
| 54 |
|
| 55 |
// For MIDI_MSG_NOTE_PRESSURE |
| 56 |
struct |
| 57 |
{ |
| 58 |
uint8_t pitch; // Note key |
| 59 |
uint8_t pressure; // Note pressure |
| 60 |
} atouch; |
| 61 |
|
| 62 |
// For MIDI_MSG_CHANNEL_PRESSURE |
| 63 |
struct |
| 64 |
{ |
| 65 |
uint8_t pressure; // Channel pressure |
| 66 |
} chn; |
| 67 |
|
| 68 |
// For MIDI_MSG_PROGRAM_CHANGE |
| 69 |
uint8_t program; // program |
| 70 |
|
| 71 |
// For MIDI_MSG_PITCH_BEND |
| 72 |
uint16_t bend; |
| 73 |
|
| 74 |
// For MIDI_MSG_MTC_QUARTER |
| 75 |
struct |
| 76 |
{ |
| 77 |
uint8_t type; |
| 78 |
uint8_t value; |
| 79 |
} mtc; |
| 80 |
|
| 81 |
// For MIDI_MSG_SONG_POS |
| 82 |
uint16_t beats; |
| 83 |
|
| 84 |
// For MIDI_MSG_SONG_SELECT |
| 85 |
uint8_t song; |
| 86 |
|
| 87 |
// For other messages |
| 88 |
uint8_t bparams[2]; // Byte parameters |
| 89 |
}; |
| 90 |
} event_t; |
| 91 |
#pragma pack(pop) |
| 92 |
|
| 93 |
/** |
| 94 |
* Maximum number of channels |
| 95 |
*/ |
| 96 |
constexpr uint8_t MIDI_CHANNELS = 0x10; |
| 97 |
|
| 98 |
enum message_t |
| 99 |
{ |
| 100 |
MIDI_MSG_NOTE_OFF = 0x80, |
| 101 |
MIDI_MSG_NOTE_ON = 0x90, |
| 102 |
MIDI_MSG_NOTE_PRESSURE = 0xa0, |
| 103 |
MIDI_MSG_NOTE_CONTROLLER = 0xb0, |
| 104 |
MIDI_MSG_PROGRAM_CHANGE = 0xc0, |
| 105 |
MIDI_MSG_CHANNEL_PRESSURE = 0xd0, |
| 106 |
MIDI_MSG_PITCH_BEND = 0xe0, |
| 107 |
MIDI_MSG_SYSTEM_EXCLUSIVE = 0xf0, |
| 108 |
MIDI_MSG_MTC_QUARTER = 0xf1, |
| 109 |
MIDI_MSG_SONG_POS = 0xf2, |
| 110 |
MIDI_MSG_SONG_SELECT = 0xf3, |
| 111 |
MIDI_MSG_TUNE_REQUEST = 0xf6, |
| 112 |
MIDI_MSG_END_EXCLUSIVE = 0xf7, |
| 113 |
MIDI_MSG_CLOCK = 0xf8, |
| 114 |
MIDI_MSG_START = 0xfa, |
| 115 |
MIDI_MSG_CONTINUE = 0xfb, |
| 116 |
MIDI_MSG_STOP = 0xfc, |
| 117 |
MIDI_MSG_ACTIVE_SENSING = 0xfe, |
| 118 |
MIDI_MSG_RESET = 0xff |
| 119 |
}; |
| 120 |
|
| 121 |
enum controller_t |
| 122 |
{ |
| 123 |
MIDI_CTL_MSB_BANK = 0x00, |
| 124 |
MIDI_CTL_MSB_MODWHEEL = 0x01, |
| 125 |
MIDI_CTL_MSB_BREATH = 0x02, |
| 126 |
MIDI_CTL_MSB_FOOT = 0x04, |
| 127 |
MIDI_CTL_MSB_PORTAMENTO_TIME = 0x05, |
| 128 |
MIDI_CTL_MSB_DATA_ENTRY = 0x06, |
| 129 |
MIDI_CTL_MSB_MAIN_VOLUME = 0x07, |
| 130 |
MIDI_CTL_MSB_BALANCE = 0x08, |
| 131 |
MIDI_CTL_MSB_PAN = 0x0a, |
| 132 |
MIDI_CTL_MSB_EXPRESSION = 0x0b, |
| 133 |
MIDI_CTL_MSB_EFFECT1 = 0x0c, |
| 134 |
MIDI_CTL_MSB_EFFECT2 = 0x0d, |
| 135 |
MIDI_CTL_MSB_GENERAL_PURPOSE1 = 0x10, |
| 136 |
MIDI_CTL_MSB_GENERAL_PURPOSE2 = 0x11, |
| 137 |
MIDI_CTL_MSB_GENERAL_PURPOSE3 = 0x12, |
| 138 |
MIDI_CTL_MSB_GENERAL_PURPOSE4 = 0x13, |
| 139 |
MIDI_CTL_LSB_BANK = 0x20, |
| 140 |
MIDI_CTL_LSB_MODWHEEL = 0x21, |
| 141 |
MIDI_CTL_LSB_BREATH = 0x22, |
| 142 |
MIDI_CTL_LSB_FOOT = 0x24, |
| 143 |
MIDI_CTL_LSB_PORTAMENTO_TIME = 0x25, |
| 144 |
MIDI_CTL_LSB_DATA_ENTRY = 0x26, |
| 145 |
MIDI_CTL_LSB_MAIN_VOLUME = 0x27, |
| 146 |
MIDI_CTL_LSB_BALANCE = 0x28, |
| 147 |
MIDI_CTL_LSB_PAN = 0x2a, |
| 148 |
MIDI_CTL_LSB_EXPRESSION = 0x2b, |
| 149 |
MIDI_CTL_LSB_EFFECT1 = 0x2c, |
| 150 |
MIDI_CTL_LSB_EFFECT2 = 0x2d, |
| 151 |
MIDI_CTL_LSB_GENERAL_PURPOSE1 = 0x30, |
| 152 |
MIDI_CTL_LSB_GENERAL_PURPOSE2 = 0x31, |
| 153 |
MIDI_CTL_LSB_GENERAL_PURPOSE3 = 0x32, |
| 154 |
MIDI_CTL_LSB_GENERAL_PURPOSE4 = 0x33, |
| 155 |
MIDI_CTL_SUSTAIN = 0x40, |
| 156 |
MIDI_CTL_PORTAMENTO = 0x41, |
| 157 |
MIDI_CTL_SOSTENUTO = 0x42, |
| 158 |
MIDI_CTL_SOFT_PEDAL = 0x43, |
| 159 |
MIDI_CTL_LEGATO_FOOTSWITCH = 0x44, |
| 160 |
MIDI_CTL_HOLD2 = 0x45, |
| 161 |
MIDI_CTL_SC1_SOUND_VARIATION = 0x46, |
| 162 |
MIDI_CTL_SC2_TIMBRE = 0x47, |
| 163 |
MIDI_CTL_SC3_RELEASE_TIME = 0x48, |
| 164 |
MIDI_CTL_SC4_ATTACK_TIME = 0x49, |
| 165 |
MIDI_CTL_SC5_BRIGHTNESS = 0x4a, |
| 166 |
MIDI_CTL_SC6 = 0x4b, |
| 167 |
MIDI_CTL_SC7 = 0x4c, |
| 168 |
MIDI_CTL_SC8 = 0x4d, |
| 169 |
MIDI_CTL_SC9 = 0x4e, |
| 170 |
MIDI_CTL_SC10 = 0x4f, |
| 171 |
MIDI_CTL_GENERAL_PURPOSE5 = 0x50, |
| 172 |
MIDI_CTL_GENERAL_PURPOSE6 = 0x51, |
| 173 |
MIDI_CTL_GENERAL_PURPOSE7 = 0x52, |
| 174 |
MIDI_CTL_GENERAL_PURPOSE8 = 0x53, |
| 175 |
MIDI_CTL_PORTAMENTO_CONTROL = 0x54, |
| 176 |
MIDI_CTL_E1_REVERB_DEPTH = 0x5b, |
| 177 |
MIDI_CTL_E2_TREMOLO_DEPTH = 0x5c, |
| 178 |
MIDI_CTL_E3_CHORUS_DEPTH = 0x5d, |
| 179 |
MIDI_CTL_E4_DETUNE_DEPTH = 0x5e, |
| 180 |
MIDI_CTL_E5_PHASER_DEPTH = 0x5f, |
| 181 |
MIDI_CTL_DATA_INCREMENT = 0x60, |
| 182 |
MIDI_CTL_DATA_DECREMENT = 0x61, |
| 183 |
MIDI_CTL_NRPN_LSB = 0x62, |
| 184 |
MIDI_CTL_NRPN_MSB = 0x63, |
| 185 |
MIDI_CTL_RPN_LSB = 0x64, |
| 186 |
MIDI_CTL_RPN_MSB = 0x65, |
| 187 |
MIDI_CTL_ALL_SOUNDS_OFF = 0x78, |
| 188 |
MIDI_CTL_RESET_CONTROLLERS = 0x79, |
| 189 |
MIDI_CTL_LOCAL_CONTROL_SWITCH = 0x7a, |
| 190 |
MIDI_CTL_ALL_NOTES_OFF = 0x7b, |
| 191 |
MIDI_CTL_OMNI_OFF = 0x7c, |
| 192 |
MIDI_CTL_OMNI_ON = 0x7d, |
| 193 |
MIDI_CTL_MONO1 = 0x7e, |
| 194 |
MIDI_CTL_MONO2 = 0x7f |
| 195 |
}; |
| 196 |
|
| 197 |
/** |
| 198 |
* Decode MIDI message |
| 199 |
* @param ev MIDI event structure to decode |
| 200 |
* @param bytes buffer containing MIDI message (1-3 bytes) |
| 201 |
* @return number of bytes used for decodingm negative value on error, never zero |
| 202 |
*/ |
| 203 |
ssize_t decode(event_t *ev, const uint8_t *bytes); |
| 204 |
|
| 205 |
/** |
| 206 |
* Decode MIDI message, more safe algorithm that checks the input array bounds |
| 207 |
* @param ev MIDI event structure to decode |
| 208 |
* @param bytes buffer containing MIDI message |
| 209 |
* @param length length of the buffer |
| 210 |
* @return number of bytes used for decodingm negative value on error, never zero |
| 211 |
*/ |
| 212 |
ssize_t decode(event_t *ev, const uint8_t *bytes, size_t length); |
| 213 |
|
| 214 |
/** |
| 215 |
* Encode MIDI message |
| 216 |
* @param bytes buffer to store encoded MIDI message |
| 217 |
* @param ev MIDI event to encode |
| 218 |
* @return number of bytes used for encoding, negative value on error, never zero |
| 219 |
*/ |
| 220 |
ssize_t encode(uint8_t *bytes, const event_t *ev); |
| 221 |
|
| 222 |
/** |
| 223 |
* Return number of bytes required for encoding |
| 224 |
* @param ev MIDI event |
| 225 |
* @return number of bytes required to encode MIDI event, negative value on error, never zero |
| 226 |
*/ |
| 227 |
ssize_t size_of(const event_t *ev); |
| 228 |
|
| 229 |
} /* namespace midi */ |
| 230 |
} /* namespace lsp */ |
| 231 |
|
| 232 |
#endif /* LSP_PLUG_IN_PROTOCOL_MIDI_H_ */ |