-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParsedMidi.cs
382 lines (357 loc) · 14.7 KB
/
ParsedMidi.cs
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
using Godot;
using Godot.Collections;
using System.Collections.Generic;
using System;
using System.Text;
using HellSyncer.Midi;
namespace HellSyncer.Midi
{
/// <summary>
/// Resource which contains Godot-friendly MIDI data.
/// </summary>
public partial class ParsedMidi : Resource
{
[Export()]
public Track[] tracks;
[Export()]
public ushort ticksPerQN;
private byte[] raw;
private ushort trackCount;
private byte holdoverEventID;
private ulong trackTickPos = 0;
private uint readHead = 0;
#region Basic Readers
public byte ReadByte()
{
byte ret = raw[readHead];
readHead += 1;
return ret;
}
public (byte, byte) ReadTwoBytes()
{
byte retA = raw[readHead];
byte retB = raw[readHead + 1];
readHead += 2;
return (retA, retB);
}
public byte[] ReadNBytes(uint n)
{
byte[] ret = new byte[n];
for (int i = 0; i < n; ++i) { ret[i] = raw[readHead + i]; }
readHead += n;
return ret;
}
public ushort ReadShort()
{
ushort ret = (ushort)(
(raw[readHead] * 0x100)
| (raw[readHead + 1])
);
readHead += 2;
return ret;
}
public uint ReadInt()
{
uint ret = (uint)(
(raw[readHead] * 0x1000000)
| (raw[readHead + 1] * 0x10000)
| (raw[readHead + 2] * 0x100)
| (raw[readHead + 3])
);
readHead += 4;
return ret;
}
public uint ReadIntVarLen()
{
byte curr = 0;
uint ret = 0;
do
{
curr = raw[readHead];
ret <<= 7;
ret |= (uint)(curr & 0x7F);
readHead++;
}
while ((curr & 0x80) != 0);
return ret;
}
public string ReadString()
{
byte len = ReadByte();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; ++i) { sb.Append((char)ReadByte()); }
return sb.ToString();
}
#endregion
public void ReadHeader()
{
ushort format = ReadShort();
if (format != 1) { throw new FormatException("Sorry, I only understand MIDI format 1."); }
trackCount = ReadShort();
ticksPerQN = ReadShort();
if (ticksPerQN >= 0x8000) { throw new FormatException("Sorry, I don't understand SMPTE timing."); }
}
public TrackEvent ReadTrackEvent()
{
TrackEvent ret = null;
uint tickDelta = ReadIntVarLen();
trackTickPos += tickDelta;
byte ID = ReadByte();
Identify:
byte channel = (byte)(ID & 0xF);
int highID = ID >> 4;
switch (highID)
{
case 0x8:
{
(byte n, byte v) = ReadTwoBytes();
ret = new NoteOffEvent(trackTickPos, channel, n, v);
}
break;
case 0x9:
{
(byte n, byte v) = ReadTwoBytes();
ret = new NoteOnEvent(trackTickPos, channel, n, v);
}
break;
case 0xA:
{
(byte n, byte v) = ReadTwoBytes();
ret = new KeyPressureEvent(trackTickPos, channel, n, v);
}
break;
case 0xB:
{
(byte n, byte v) = ReadTwoBytes();
ret = new ControlChangeEvent(trackTickPos, channel, n, v);
}
break;
case 0xC:
ret = new ProgramChangeEvent(trackTickPos, channel, ReadByte());
break;
case 0xD:
ret = new ChannelPressureEvent(trackTickPos, channel, ReadByte());
break;
case 0xE:
{
(byte low7, byte high7) = ReadTwoBytes();
short bend = (short)((high7 << 7) + low7 - 0x2000);
ret = new PitchBendEvent(trackTickPos, channel, bend);
}
break;
case 0xF:
switch (channel) // not actually channel. abuse of notation
{
case 0x0:
{
SysExF0Event f0 = new SysExF0Event(trackTickPos);
byte dataLength = ReadByte();
f0.data = new byte[dataLength];
for (int i = 0; i < dataLength; ++i) { f0.data[i] = ReadByte(); }
ret = f0;
}
break;
case 0x7:
{
SysExF0Event f7 = new SysExF0Event(trackTickPos);
byte dataLength = ReadByte();
f7.data = new byte[dataLength];
for (int i = 0; i < dataLength; ++i) { f7.data[i] = ReadByte(); }
ret = f7;
}
break;
case 0xF:
// meta-event
byte metaID = ReadByte();
switch (metaID)
{
case 0x01:
ret = new TextEvent(trackTickPos, ReadString());
break;
case 0x02:
ret = new CopyrightEvent(trackTickPos, ReadString());
break;
case 0x03:
ret = new TrackNameEvent(trackTickPos, ReadString());
break;
case 0x04:
ret = new InstrumentNameEvent(trackTickPos, ReadString());
break;
case 0x05:
ret = new LyricEvent(trackTickPos, ReadString());
break;
case 0x06:
ret = new MarkerEvent(trackTickPos, ReadString());
break;
case 0x07:
ret = new CueEvent(trackTickPos, ReadString());
break;
case 0x2F:
ret = new EndOfTrackEvent(trackTickPos);
ReadByte(); // length of this event is always 0
break;
case 0x51:
ret = new TempoEvent(trackTickPos, ReadInt() & 0x00FFFFFF);
break;
case 0x58:
{
byte[] info = ReadNBytes(5);
// info[0] is the length of the following data; always 4
byte num = info[1];
byte denom = (byte)(1 << info[2]);
// we aren't using info[3] nor info[4]. they are metronome info
ret = new TimeSignatureEvent(trackTickPos, num, denom);
}
break;
case 0x59:
{
byte[] info = ReadNBytes(3);
// info[0] is the length of the following data; always 2
sbyte sf = (sbyte)(info[1] >= 0x80 ? (info[1] - 0x100) : info[1]);
bool minor = info[2] == 0x1;
ret = new KeySignatureEvent(trackTickPos, sf, minor);
}
break;
default:
ret = new UnsupportedEvent(trackTickPos);
ReadString(); // events that get here contain a string; throw it away
break;
}
break;
default:
// events that get here contain no data.
ret = new UnsupportedEvent(trackTickPos);
break;
}
break;
default:
int highHoldoverID = holdoverEventID >> 4;
if (highHoldoverID >= 0x8 && highHoldoverID <= 0xE)
{
ID = holdoverEventID;
readHead -= 1;
goto Identify;
}
throw new FormatException($"Hey! That's not a MIDI event! (byte {readHead - 1} = {ID})");
}
holdoverEventID = ID;
return ret;
}
public class SortFullNotesByStart : IComparer<FullNoteInfo>
{
public int Compare(FullNoteInfo x, FullNoteInfo y)
{
return x.tick.CompareTo(y.tick);
}
}
public Track ReadTrack(bool skip)
{
ReadInt(); // MTrk
uint trackLenBytes = ReadInt();
uint endOfTrackByte = readHead + trackLenBytes;
if (skip)
{
readHead = endOfTrackByte;
return new Track(
new TrackEvent[1] { new EndOfTrackEvent(0) },
new FullNoteInfo[0]
);
}
List<TrackEvent> eventsTemp = new List<TrackEvent>();
trackTickPos = 0;
do { eventsTemp.Add(ReadTrackEvent()); }
while (readHead < endOfTrackByte);
// correspond NoteOn with NoteOff to create full note info
List<FullNoteInfo> fullNotesTemp = new List<FullNoteInfo>();
List<NoteOnEvent> notesPlaying = new List<NoteOnEvent>();
for (int i = 0; i < eventsTemp.Count; ++i)
{
if (eventsTemp[i] is NoteOnEvent && ((NoteOnEvent)eventsTemp[i]).velocity > 0)
{
notesPlaying.Add((NoteOnEvent)eventsTemp[i]);
}
else if (eventsTemp[i] is NoteOffEvent
|| (eventsTemp[i] is NoteOnEvent && ((NoteOnEvent)eventsTemp[i]).velocity == 0))
{
PVEvent pv = (PVEvent)eventsTemp[i];
bool closed = false;
for (int j = 0; j < notesPlaying.Count; ++j)
{
if (notesPlaying[j].channel == pv.channel && notesPlaying[j].note == pv.note)
{
fullNotesTemp.Add(
new FullNoteInfo(notesPlaying[j], pv.tick - notesPlaying[j].tick)
);
notesPlaying.RemoveAt(j);
closed = true;
break;
}
}
if (!closed)
{
//GD.PushWarning("Couldn't find a NoteOn to suit a NoteOff. The parser may be erroneous, or the MIDI is malformed.");
}
}
}
for (int j = 0; j < notesPlaying.Count; ++j)
{
fullNotesTemp.Add(
new FullNoteInfo(notesPlaying[j], ticksPerQN)
);
//GD.PushWarning("Couldn't find a NoteOff to suit a NoteOn. The parser may be erroneous, or the MIDI is malformed.");
}
fullNotesTemp.Sort(new SortFullNotesByStart());
//GD.Print(fullNotesTemp.Count);
return new Track(eventsTemp.ToArray(), fullNotesTemp.ToArray());
}
private bool TrackIsInRange(int trackId, ref Vector2[] trackRanges)
{
if (trackId == 0) { return true; }
foreach (Vector2 trackRange in trackRanges)
{
if (trackRange.X <= trackId && trackId <= trackRange.Y)
{
return true;
}
}
return false;
}
/// <summary>
/// Creates a ParsedMidi from a .mid/.midi file.
/// </summary>
/// <param name="raw">The file as a byte array.</param>
/// <param name="trackRanges">
/// Whitelist of track numbers to import. Each vector (X, Y) is a range of track numbers X to Y, including X and Y.
/// Track 0 is always imported regardless of this list.
/// </param>
public void ParseRaw(byte[] raw, Vector2[] trackRanges)
{
this.raw = raw;
readHead = 0;
ReadInt(); // MThd
ReadInt(); // header length, but it's always 6
ReadHeader();
tracks = new Track[trackCount];
for (int i = 0; i < trackCount; ++i)
{
bool skip = !TrackIsInRange(i, ref trackRanges);
tracks[i] = ReadTrack(skip);
}
}
/// <summary>
/// Creates a simple ParsedMidi using a tempo and time signature.
/// </summary>
/// <param name="bpm">Quarter notes per minute.</param>
/// <param name="timeSignature">(X, Y) is the (numerator, denominator).</param>
public void GenerateForBeat(float bpm, Vector2I timeSignature)
{
// only a control track.
ticksPerQN = 360;
TrackEvent[] events = new TrackEvent[3];
events[0] = new TempoEvent(0, (ulong)Math.Round(1e6f * 60f / bpm));
events[1] = new TimeSignatureEvent(0, (byte)timeSignature.X, (byte)timeSignature.Y);
events[2] = new EndOfTrackEvent(0);
tracks = new Track[1] { new Track(events, new FullNoteInfo[0]) };
}
}
}