-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
390 lines (368 loc) · 12.1 KB
/
index.ts
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import { BufferReader } from "./BufferReader.js"
export type MidiEvent =
| { type: "meta"; subType: "sequenceNumber"; typeByte: number; subTypeByte: number; deltaTime: number; number: number }
| { type: "meta"; subType: "text"; typeByte: number; subTypeByte: number; deltaTime: number; text: string }
| { type: "meta"; subType: "copyrightNotice"; typeByte: number; subTypeByte: number; deltaTime: number; text: string }
| { type: "meta"; subType: "trackName"; typeByte: number; subTypeByte: number; deltaTime: number; text: string }
| { type: "meta"; subType: "instrumentName"; typeByte: number; subTypeByte: number; deltaTime: number; text: string }
| { type: "meta"; subType: "lyrics"; typeByte: number; subTypeByte: number; deltaTime: number; text: string }
| { type: "meta"; subType: "marker"; typeByte: number; subTypeByte: number; deltaTime: number; text: string }
| { type: "meta"; subType: "cuePoint"; typeByte: number; subTypeByte: number; deltaTime: number; text: string }
| { type: "meta"; subType: "midiChannelPrefix"; typeByte: number; subTypeByte: number; deltaTime: number; channel: number }
| { type: "meta"; subType: "endOfTrack"; typeByte: number; subTypeByte: number; deltaTime: number }
| { type: "meta"; subType: "setTempo"; typeByte: number; subTypeByte: number; deltaTime: number; microsecondsPerBeat: number }
| { type: "meta"; subType: "smpteOffset"; typeByte: number; subTypeByte: number; deltaTime: number; frameRate: number; hour: number; min: number; sec: number; frame: number; subFrame: number }
| { type: "meta"; subType: "timeSignature"; typeByte: number; subTypeByte: number; deltaTime: number; numerator: number; denominator: number; metronome: number; thirtySeconds: number }
| { type: "meta"; subType: "keySignature"; typeByte: number; subTypeByte: number; deltaTime: number; key: number; scale: number }
| { type: "meta"; subType: "sequencerSpecific"; typeByte: number; subTypeByte: number; deltaTime: number; data: ArrayBuffer }
| { type: "meta"; subType: undefined; typeByte: number; subTypeByte: number; deltaTime: number; data: ArrayBuffer }
| { type: "sysEx"; subType: undefined; typeByte: number; deltaTime: number; data: ArrayBuffer }
| { type: "dividedSysEx"; subType: undefined; typeByte: number; deltaTime: number; data: ArrayBuffer }
| { type: "midi"; subType: "noteOff"; typeByte: number; deltaTime: number; channel: number; note: number; velocity: number }
| { type: "midi"; subType: "noteOff" | "noteOn"; typeByte: number; deltaTime: number; channel: number; note: number; velocity: number }
| { type: "midi"; subType: "noteAftertouch"; typeByte: number; deltaTime: number; channel: number; note: number; amount: number }
| { type: "midi"; subType: "controller"; typeByte: number; deltaTime: number; channel: number; controllerType: number; value: number }
| { type: "midi"; subType: "programChange"; typeByte: number; deltaTime: number; channel: number; program: number }
| { type: "midi"; subType: "channelAftertouch"; typeByte: number; deltaTime: number; channel: number; amount: number }
| { type: "midi"; subType: "pitchBend"; typeByte: number; deltaTime: number; channel: number; value: number }
export function parseMidiFile(buffer: ArrayBufferLike) {
const reader = new BufferReader(buffer)
const header = parseHeader(reader)
const tracks = parseTracks(reader)
return { header, tracks }
}
function parseHeader(reader: BufferReader) {
const headerChunk = reader.midiChunk()
if (headerChunk.id !== "MThd" || headerChunk.length !== 6) {
throw "Bad .mid file, header not found"
}
const headerReader = new BufferReader(headerChunk.data)
const formatType = headerReader.uint16()
const trackCount = headerReader.uint16()
const timeDivision = headerReader.uint16()
if (timeDivision & 0x8000) {
throw "Expressing time division in SMTPE frames is not supported yet"
}
const ticksPerBeat = timeDivision
return { formatType, trackCount, ticksPerBeat }
}
function parseTracks(reader: BufferReader) {
let tracks: MidiEvent[][] = []
while (!reader.eof()) {
const trackChunk = reader.midiChunk()
if (trackChunk.id !== "MTrk") {
throw "Unexpected chunk, expected MTrk, got " + trackChunk.id
}
const trackTrack = new BufferReader(trackChunk.data)
let track: MidiEvent[] = []
while (!trackTrack.eof()) {
const lastEvent = track[track.length - 1]
const event = parseEvent(trackTrack, lastEvent ? lastEvent.typeByte : undefined)
track = [...track, event]
}
tracks = [...tracks, track]
}
return tracks
}
function parseEvent(reader: BufferReader, lastTypeByte: number | undefined): MidiEvent {
const deltaTime = reader.midiInt()
let typeByte = reader.uint8()
if (typeByte === 0xff) {
/** meta event */
const type = "meta" as "meta"
const subTypeByte = reader.uint8()
const length = reader.midiInt()
switch (subTypeByte) {
case 0x00:
if (length !== 2) {
throw "Expected length for sequenceNumber event is 2, got " + length
}
return {
type,
subType: "sequenceNumber" as "sequenceNumber",
typeByte,
subTypeByte,
deltaTime,
number: reader.uint16(),
}
case 0x01:
return {
type,
subType: "text" as "text",
typeByte,
subTypeByte,
deltaTime,
text: reader.string(length),
}
case 0x02:
return {
type,
subType: "copyrightNotice" as "copyrightNotice",
typeByte,
subTypeByte,
deltaTime,
text: reader.string(length),
}
case 0x03:
return {
type,
subType: "trackName" as "trackName",
typeByte,
subTypeByte,
deltaTime,
text: reader.string(length),
}
case 0x04:
return {
type,
subType: "instrumentName" as "instrumentName",
typeByte,
subTypeByte,
deltaTime,
text: reader.string(length),
}
case 0x05:
return {
type,
subType: "lyrics" as "lyrics",
typeByte,
subTypeByte,
deltaTime,
text: reader.string(length),
}
case 0x06:
return {
type,
subType: "marker" as "marker",
typeByte,
subTypeByte,
deltaTime,
text: reader.string(length),
}
case 0x07:
return {
type,
subType: "cuePoint" as "cuePoint",
typeByte,
subTypeByte,
deltaTime,
text: reader.string(length),
}
case 0x20:
if (length !== 1) {
throw "Expected length for midiChannelPrefix event is 1, got " + length
}
return {
type,
subType: "midiChannelPrefix" as "midiChannelPrefix",
typeByte,
subTypeByte,
deltaTime,
channel: reader.uint8(),
}
case 0x2f:
if (length !== 0) {
throw "Expected length for endOfTrack event is 0, got " + length
}
return {
type,
subType: "endOfTrack" as "endOfTrack",
typeByte,
subTypeByte,
deltaTime,
}
case 0x51:
if (length !== 3) {
throw "Expected length for setTempo event is 3, got " + length
}
return {
type,
subType: "setTempo" as "setTempo",
typeByte,
subTypeByte,
deltaTime,
microsecondsPerBeat: (reader.uint8() << 16) + (reader.uint8() << 8) + reader.uint8(),
}
case 0x54:
if (length != 5) {
throw "Expected length for smpteOffset event is 5, got " + length
}
const hourByte = reader.uint8()
return {
type,
subType: "smpteOffset" as "smpteOffset",
typeByte,
subTypeByte,
deltaTime,
frameRate: getFrameRate(hourByte),
hour: hourByte & 0x1f,
min: reader.uint8(),
sec: reader.uint8(),
frame: reader.uint8(),
subFrame: reader.uint8(),
}
case 0x58:
if (length != 4) {
throw "Expected length for timeSignature event is 4, got " + length
}
return {
type,
subType: "timeSignature" as "timeSignature",
typeByte,
subTypeByte,
deltaTime,
numerator: reader.uint8(),
denominator: Math.pow(2, reader.uint8()),
metronome: reader.uint8(),
thirtySeconds: reader.uint8(),
}
case 0x59:
if (length != 2) {
throw "Expected length for keySignature event is 2, got " + length
}
return {
type,
subType: "keySignature" as "keySignature",
typeByte,
subTypeByte,
deltaTime,
key: reader.int8(),
scale: reader.uint8(),
}
case 0x7f:
return {
type,
subType: "sequencerSpecific" as "sequencerSpecific",
typeByte,
subTypeByte,
deltaTime,
data: reader.read(length),
}
default:
return {
type,
subType: undefined,
typeByte,
subTypeByte,
deltaTime,
data: reader.read(length),
}
}
} else if (typeByte === 0xf0) {
/** system event */
const length = reader.midiInt()
return {
type: "sysEx" as "sysEx",
subType: undefined,
typeByte,
deltaTime,
data: reader.read(length),
}
} else if (typeByte === 0xf7) {
/** divided system event */
const length = reader.midiInt()
return {
type: "dividedSysEx" as "dividedSysEx",
subType: undefined,
typeByte,
deltaTime,
data: reader.read(length),
}
} else {
/** midi event */
const type = "midi" as "midi"
/**
* running status - reuse lastEventTypeByte as the event type
* typeByte is actually the first parameter
*/
const isRunningStatus = (typeByte & 0b10000000) === 0
const value = isRunningStatus ? typeByte : reader.uint8()
typeByte = isRunningStatus ? (lastTypeByte === undefined ? 0 : lastTypeByte) : typeByte
const channel = typeByte & 0x0f
switch (typeByte >> 4) {
case 0x08:
return {
type,
subType: "noteOff" as "noteOff",
typeByte,
deltaTime,
channel,
note: value,
velocity: reader.uint8(),
}
case 0x09:
const velocity = reader.uint8()
return {
type,
subType: velocity === 0 ? ("noteOff" as "noteOff") : ("noteOn" as "noteOn"),
typeByte,
deltaTime,
channel,
note: value,
velocity,
}
case 0x0a:
return {
type,
subType: "noteAftertouch" as "noteAftertouch",
typeByte,
deltaTime,
channel,
note: value,
amount: reader.uint8(),
}
case 0x0b:
return {
type,
subType: "controller" as "controller",
typeByte,
deltaTime,
channel,
controllerType: value,
value: reader.uint8(),
}
case 0x0c:
return {
type,
subType: "programChange" as "programChange",
typeByte,
deltaTime,
channel,
program: value,
}
case 0x0d:
return {
type,
subType: "channelAftertouch" as "channelAftertouch",
typeByte,
deltaTime,
channel,
amount: value,
}
case 0x0e:
return {
type,
subType: "pitchBend" as "pitchBend",
typeByte,
deltaTime,
channel,
value: value + (reader.uint8() << 7),
}
}
}
throw "Unrecognised MIDI event type byte: " + typeByte
}
function getFrameRate(hourByte: number) {
switch (hourByte & 0b1100000) {
case 0x00:
return 24
case 0x20:
return 25
case 0x40:
return 29
case 0x60:
return 30
default:
return 0
}
}