-
Notifications
You must be signed in to change notification settings - Fork 3
/
midiContentDecoder.js
168 lines (164 loc) · 7.78 KB
/
midiContentDecoder.js
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
/** This function is only used for printing messages out on the page- */
function mididecoder(midiMessage,timeStamp){
let noteString = "";
switch(midiMessage[0] >>> 4) {
case 0b1000: // Note off
noteString = noteNamer(midiMessage[1]);
printToConsole(noteString.trimRight(6) + ' Off. @ Velocity: ' + pad(midiMessage[2],3) + '..| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b1001: // Note on
noteString = noteNamer(midiMessage[1]);
printToConsole(noteString.trimRight(6) + ' On.. @ Velocity: ' + pad(midiMessage[2],3) + '..| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b1010: // Polyphonic Key Pressure (Aftertouch)
printToConsole(noteString.trimRight(6) + 'Polyphonic Key Pressure: ' + pad(midiMessage[2],3) + ' | Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b1011: // Control Change
switch(midiMessage[1]){
case 0b01111000: // All sound off
printToConsole('All Sound Off................| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b01111001: // Reset all Controllers
printToConsole('Reset All Controllers........| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b01111010: // Local Control off
printToConsole('Local Control Off............| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b01111011: // All Notes off
printToConsole('All Notes Off................| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b01111100: // Omni Mode off
printToConsole('Omni Mode Off................| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b01111101: // Omni Mode on
printToConsole('Omni Mode On.................| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b01111110: // Mono Mode
printToConsole('Mono Mode....................| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b01111111: // Poly Mode
printToConsole('Poly Mode....................| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
default:
printToConsole('Control ' + pad(midiMessage[1],3) + ' Changed to: ' + pad( midiMessage[2],3) +'..| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
} // End Control Change
break;
case 0b1100: // Program Change
printToConsole('Program Changed to: ' + pad(midiMessage[1],3) + '......| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b1101: // Channel Pressure (After-touch)
printToConsole('Channel Pressure: ' + pad(midiMessage[1],3) + '........| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b1110: // Pitch Bend Change
printToConsole('Bending pitch: ' + pad((((midiMessage[1] & 127) * 128) + (midiMessage[2] & 127)), 10) + '....| Channel: ' + pad((1 + midiMessage[0]&0b00001111),2) + ' | Time: ' + pad(timeStamp,4));
break;
case 0b1111: // System Messages
switch (midiMessage[0]) {
case 0b11110000: // SysEx
printToConsole('System Exclusive message.....|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11110001: // MIDI Time Code Quarter Frame
//printToConsole('Time: ' + timeStamp + ' | MIDI Time Code Quarter Frame | Message: ' + (midiMessage[1] >>> 4) + ' | Values: ' + (midiMessage[1]&0b00001111));
printToConsole('MIDI Time Code Quarter Frame |.............| Time: ' + pad(timeStamp,4));
break;
case 0b11110010: // Song Position Pointer
printToConsole('Song Position to ' + pad((midiMessage[1]*128 + midiMessage[2]),10) + '..|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11110011: // Song Select
printToConsole('Song ' + pad(midiMessage[1],3) + ' selected...............|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11110100: // Not defined
printToConsole('.............................|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11110101: // Not defined
printToConsole('.............................|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11110110: // Tune Request
printToConsole('Tune Request.................|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11110111: // End of Exclusive
printToConsole('End of System Exclusive......|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11111000: // Timing Clock
printToConsole('Timing Clock.................|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11111001: // Not defined
printToConsole('.............................|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11111010: // Start
printToConsole('Start........................|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11111011: // Continue
printToConsole('Continue.....................|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11111100: // Stop
printToConsole('Stop.........................|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11111101: // Not defined
printToConsole('.............................|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11111110: // Active Sensing
printToConsole('Active Sensing...............|.............| Time: ' + pad(timeStamp,4));
break;
case 0b11111111: // Reset
printToConsole('Reset........................|.............| Time: ' + pad(timeStamp,4));
break;
default:
printToConsole('Unknown Message..............|.............| Time: ' + pad(timeStamp,4));
console.log('Default error: ' + midiMessage);
break;
}; // End System Messages
}; // End switch(firstNibble)
}; // End midiMessageParser function
function noteNamer(midiValue) {
let noteNumber = midiValue % 12;
let octaveNumber = ((midiValue - noteNumber) / 12) - 1;
let noteString = "";
switch(noteNumber) {
case 0:
noteString = "C" + octaveNumber + "....";
break;
case 1:
noteString = "C#/Db" + octaveNumber;
break;
case 2:
noteString = "D" + octaveNumber + "....";
break;
case 3:
noteString = "D#/Eb" + octaveNumber;
break;
case 4:
noteString = "E" + octaveNumber + "....";
break;
case 5:
noteString = "F" + octaveNumber + "....";
break;
case 6:
noteString = "F#/Gb" + octaveNumber;
break;
case 7:
noteString = "G" + octaveNumber + "....";
break;
case 8:
noteString = "G#/Ab" + octaveNumber;
break;
case 9:
noteString = "A" + octaveNumber + "....";
break;
case 10:
noteString = "A#/Bb" + octaveNumber;
break;
case 11:
noteString = "B" + octaveNumber + "....";
break;
default:
break;
}
return noteString;
}
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}