File size: 5,859 Bytes
b0bfea8 |
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
import { IndexedByteArray } from "../utils/indexed_array.js";
/**
* midi_message.js
* purpose: contains enums for midi events and controllers and functions to parse them
*/
export class MIDIMessage
{
/**
* Absolute number of MIDI ticks from the start of the track.
* @type {number}
*/
ticks;
/**
* The MIDI message status byte. Note that for meta events, it is the second byte. (not 0xFF)
* @type {number}
*/
messageStatusByte;
/**
* Message's binary data
* @type {IndexedByteArray}
*/
messageData;
/**
* @param ticks {number}
* @param byte {number} the message status byte
* @param data {IndexedByteArray}
*/
constructor(ticks, byte, data)
{
this.ticks = ticks;
this.messageStatusByte = byte;
this.messageData = data;
}
}
/**
* Gets the status byte's channel
* @param statusByte
* @returns {number} channel is -1 for system messages -2 for meta and -3 for sysex
*/
export function getChannel(statusByte)
{
const eventType = statusByte & 0xF0;
const channel = statusByte & 0x0F;
let resultChannel = channel;
switch (eventType)
{
// midi (and meta and sysex headers)
case 0x80:
case 0x90:
case 0xA0:
case 0xB0:
case 0xC0:
case 0xD0:
case 0xE0:
break;
case 0xF0:
switch (channel)
{
case 0x0:
resultChannel = -3;
break;
case 0x1:
case 0x2:
case 0x3:
case 0x4:
case 0x5:
case 0x6:
case 0x7:
case 0x8:
case 0x9:
case 0xA:
case 0xB:
case 0xC:
case 0xD:
case 0xE:
resultChannel = -1;
break;
case 0xF:
resultChannel = -2;
break;
}
break;
default:
resultChannel = -1;
}
return resultChannel;
}
// all the midi statuses dictionary
export const messageTypes = {
noteOff: 0x80,
noteOn: 0x90,
polyPressure: 0xA0,
controllerChange: 0xB0,
programChange: 0xC0,
channelPressure: 0xD0,
pitchBend: 0xE0,
systemExclusive: 0xF0,
timecode: 0xF1,
songPosition: 0xF2,
songSelect: 0xF3,
tuneRequest: 0xF6,
clock: 0xF8,
start: 0xFA,
continue: 0xFB,
stop: 0xFC,
activeSensing: 0xFE,
reset: 0xFF,
sequenceNumber: 0x00,
text: 0x01,
copyright: 0x02,
trackName: 0x03,
instrumentName: 0x04,
lyric: 0x05,
marker: 0x06,
cuePoint: 0x07,
programName: 0x08,
midiChannelPrefix: 0x20,
midiPort: 0x21,
endOfTrack: 0x2F,
setTempo: 0x51,
smpteOffset: 0x54,
timeSignature: 0x58,
keySignature: 0x59,
sequenceSpecific: 0x7F
};
/**
* Gets the event's status and channel from the status byte
* @param statusByte {number} the status byte
* @returns {{channel: number, status: number}} channel will be -1 for sysex and meta
*/
export function getEvent(statusByte)
{
const status = statusByte & 0xF0;
const channel = statusByte & 0x0F;
let eventChannel = -1;
let eventStatus = statusByte;
if (status >= 0x80 && status <= 0xE0)
{
eventChannel = channel;
eventStatus = status;
}
return {
status: eventStatus,
channel: eventChannel
};
}
/**
* @enum {number}
*/
export const midiControllers = {
bankSelect: 0,
modulationWheel: 1,
breathController: 2,
footController: 4,
portamentoTime: 5,
dataEntryMsb: 6,
mainVolume: 7,
balance: 8,
pan: 10,
expressionController: 11,
effectControl1: 12,
effectControl2: 13,
generalPurposeController1: 16,
generalPurposeController2: 17,
generalPurposeController3: 18,
generalPurposeController4: 19,
lsbForControl0BankSelect: 32,
lsbForControl1ModulationWheel: 33,
lsbForControl2BreathController: 34,
lsbForControl4FootController: 36,
lsbForControl5PortamentoTime: 37,
lsbForControl6DataEntry: 38,
lsbForControl7MainVolume: 39,
lsbForControl8Balance: 40,
lsbForControl10Pan: 42,
lsbForControl11ExpressionController: 43,
lsbForControl12EffectControl1: 44,
lsbForControl13EffectControl2: 45,
sustainPedal: 64,
portamentoOnOff: 65,
sostenutoPedal: 66,
softPedal: 67,
legatoFootswitch: 68,
hold2Pedal: 69,
soundVariation: 70,
filterResonance: 71,
releaseTime: 72,
attackTime: 73,
brightness: 74,
decayTime: 75,
vibratoRate: 76,
vibratoDepth: 77,
vibratoDelay: 78,
soundController10: 79,
generalPurposeController5: 80,
generalPurposeController6: 81,
generalPurposeController7: 82,
generalPurposeController8: 83,
portamentoControl: 84,
reverbDepth: 91,
tremoloDepth: 92,
chorusDepth: 93,
detuneDepth: 94,
phaserDepth: 95,
dataIncrement: 96,
dataDecrement: 97,
NRPNLsb: 98,
NRPNMsb: 99,
RPNLsb: 100,
RPNMsb: 101,
allSoundOff: 120,
resetAllControllers: 121,
localControlOnOff: 122,
allNotesOff: 123,
omniModeOff: 124,
omniModeOn: 125,
monoModeOn: 126,
polyModeOn: 127
};
/**
* @type {{"11": number, "12": number, "13": number, "14": number, "8": number, "9": number, "10": number}}
*/
export const dataBytesAmount = {
0x8: 2, // note off
0x9: 2, // note on
0xA: 2, // note at
0xB: 2, // cc change
0xC: 1, // pg change
0xD: 1, // channel after touch
0xE: 2 // pitch wheel
}; |