-
Notifications
You must be signed in to change notification settings - Fork 3
/
midiBLEdecoder.js
180 lines (148 loc) · 4.7 KB
/
midiBLEdecoder.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
169
170
171
172
173
174
175
176
177
178
179
180
parserMessage = [];
let runningStatus;
let thirdByteFlag;
let prevReturnedTimestamp;
let prevReceivedTimestamp;
let connTimePrevious;
let firstMessageReceived = true;
function parseMidiByte(currentByte) {
if ((currentByte >> 7) === 1) {
/** Current byte is statusbyte */
runningStatus = currentByte;
thirdByteFlag = false;
if (((currentByte >> 7) === 1) && ((currentByte >> 3) === 0b11111)) {
/** System Real-Time Messages */
parserMessage[0] = currentByte;
return parserMessage;
}
/** Message with only one byte */
if ((currentByte >> 2) === 0b111101) {
if(currentByte === 0xF7) {
/** End of exclusive, not supported. Discarded for now. */
return;
}
parserMessage[0] = currentByte;
return parserMessage;
}
return false;
}
if (thirdByteFlag === true) {
/** Expected third, and last, byte of message */
thirdByteFlag = false;
parserMessage[2] = currentByte;
return parserMessage;
}
if (runningStatus === 0) {
/** System Exclusive (SysEx) databytes, from 3rd byte until EoX, or
* orphaned databytes. */
return;
}
/** Channel Voice Messages */
switch (runningStatus >> 4) {
case 0x8:
case 0x9:
case 0xA:
case 0xB:
case 0xE:
thirdByteFlag = true;
parserMessage[0] = runningStatus;
parserMessage[1] = currentByte;
return;
case 0xC:
case 0xD:
parserMessage[0] = runningStatus;
parserMessage[1] = currentByte;
return parserMessage;
}
/** System Common Message */
switch (runningStatus) {
case 0xF2:
thirdByteFlag = true;
parserMessage[0] = runningStatus;
parserMessage[1] = currentByte;
runningStatus = 0;
return parserMessage;
case 0xF1:
case 0xF3:
thirdByteFlag = false;
parserMessage[0] = runningStatus;
parserMessage[1] = currentByte;
runningStatus = 0;
return parserMessage;
case 0xF0:
break;
}
runningStatus = 0;
return;
}
function convertTimestamp(timestampBLE, connTime) {
if (firstMessageReceived) {
firstMessageReceived = false;
connTimePrevious = connTime;
prevReceivedTimestamp = timestampBLE;
prevReturnedTimestamp = connTime;
return 0;
}
let trueTSInterval = (((timestampBLE - prevReceivedTimestamp) & 8191)
+ Math.round((((connTime - connTimePrevious)
- ((timestampBLE - prevReceivedTimestamp) & 8191)))
/ 8192)
* 8192);
let addedDelay = (trueTSInterval - (connTime - prevReturnedTimestamp));
if (addedDelay < 0) {
addedDelay = 0;
}
connTimePrevious = connTime;
prevReceivedTimestamp = timestampBLE;
prevReturnedTimestamp = performance.now() + addedDelay;
return addedDelay;
}
function bleMIDIrx(blepacket) {
console.log('BLE-in: ' + blepacket);
midiMessage = [];
let connTime = performance.now();
let currentByte;
let nextIsNewTimestamp = false;
let timestampBLE = (blepacket[1] & 0x7F) + (((blepacket[0] & 0x3F) << 7));
let delay = convertTimestamp(timestampBLE, connTime);
/** Check messages in package. */
for (pos = 2; pos < blepacket.length; pos++) {
currentByte = blepacket[pos];
/** Check MSB and expectations to ID current byte as timestamp,
* statusbyte or databyte */
if ((currentByte >> 7) && (nextIsNewTimestamp)) {
/** New Timestamp means last message is complete */
nextIsNewTimestamp = false;
if ((currentByte & 0x7F) < (timestampBLE & 0x7F)) {
/** Timestamp overflow, increment Timestamp High */
timestampBLE += 1 << 7;
}
/** Storing newest timestamp for later reference, and translating
* timestamp to local time */
timestampBLE = ((currentByte & 0x7F) + (timestampBLE & 0x1F80));
delay = convertTimestamp(timestampBLE, connTime);
if(midiMessage === undefined) {
/** Previous message was not complete */
console.log('Incomplete message: pos ' + pos);
}
} else {
/** Statusbytes and databytes */
nextIsNewTimestamp = true;
midiMessage = parseMidiByte(currentByte)
if (midiMessage) {
/** Message completed */
parserMessage = [];
playMIDImessage(midiMessage, delay);
}
}
}
}
/** Sends MIDI-messages
* There are some issues with an Uncaught RangeError,
* someone who actually knows what they're doing should look into this. */
function playMIDImessage(midiMessage, delay) {
let timestamp = (performance.now()+delay);
console.log('MIDI Message @' + (timestamp&8191) + ': ' + midiMessage);
synth.send(midiMessage, timestamp);
mididecoder(midiMessage, (timestamp&8191));
}