This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
ProjectParser.cs
610 lines (549 loc) · 26.3 KB
/
ProjectParser.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Monad.FLParser
{
internal class ProjectParser
{
private readonly Project _project = new Project();
private Pattern _curPattern;
private Channel _curChannel;
private Insert _curInsert;
private InsertSlot _curSlot;
private readonly bool _verbose;
private int _versionMajor;
public ProjectParser(bool verbose)
{
_verbose = verbose;
}
public Project Parse(BinaryReader reader)
{
_curInsert = _project.Inserts[0];
ParseHeader(reader);
ParseFldt(reader);
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
ParseEvent(reader);
}
return _project;
}
private void ParseHeader(BinaryReader reader)
{
if (Encoding.ASCII.GetString(reader.ReadBytes(4)) != "FLhd")
throw new FlParseException("Invalid magic number", reader.BaseStream.Position);
// header + type
var headerLength = reader.ReadInt32();
if (headerLength != 6)
throw new FlParseException($"Expected header length 6, not {headerLength}", reader.BaseStream.Position);
var type = reader.ReadInt16();
if (type != 0) throw new FlParseException($"Type {type} is not supported", reader.BaseStream.Position);
// channels
var channelCount = reader.ReadInt16();
if (channelCount < 1 || channelCount > 1000)
throw new FlParseException($"Invalid number of channels: {channelCount}", reader.BaseStream.Position);
for (var i = 0; i < channelCount; i++)
{
_project.Channels.Add(new Channel { Id = i, Data = new GeneratorData() });
}
// ppq
_project.Ppq = reader.ReadInt16();
if (_project.Ppq < 0) throw new Exception($"Invalid PPQ: {_project.Ppq}");
}
private void ParseFldt(BinaryReader reader)
{
string id;
var len = 0;
do
{
reader.ReadBytes(len);
id = Encoding.ASCII.GetString(reader.ReadBytes(4));
len = reader.ReadInt32();
// sanity check
if (len < 0 || len > 0x10000000)
throw new FlParseException($"Invalid chunk length: {len}", reader.BaseStream.Position);
} while (id != "FLdt");
}
private void ParseEvent(BinaryReader reader)
{
var startPos = reader.BaseStream.Position;
var eventId = (Enums.Event)reader.ReadByte();
Output($"{eventId} ({(int)eventId:X2}) at {startPos:X} ");
if (eventId < Enums.Event.Word) ParseByteEvent(eventId, reader);
else if (eventId < Enums.Event.Int) ParseWordEvent(eventId, reader);
else if (eventId < Enums.Event.Text) ParseDwordEvent(eventId, reader);
else if (eventId < Enums.Event.Data) ParseTextEvent(eventId, reader);
else ParseDataEvent(eventId, reader);
}
private void ParseByteEvent(Enums.Event eventId, BinaryReader reader)
{
var data = reader.ReadByte();
if (_verbose) OutputLine($"byte: {data:X2}");
var genData = _curChannel?.Data as GeneratorData;
switch (eventId)
{
case Enums.Event.ByteMainVol:
_project.MainVolume = data;
break;
case Enums.Event.ByteUseLoopPoints:
if (genData != null) genData.SampleUseLoopPoints = true;
break;
case Enums.Event.ByteMixSliceNum:
if (genData != null) genData.Insert = data;
break;
case Enums.Event.BytePlayTruncatedNotes:
_project.PlayTruncatedNotes = Convert.ToBoolean(data);
break;
}
}
private void ParseWordEvent(Enums.Event eventId, BinaryReader reader)
{
var data = reader.ReadUInt16();
OutputLine($"word: {data:X4}");
var genData = _curChannel?.Data as GeneratorData;
switch (eventId)
{
case Enums.Event.WordNewChan:
_curChannel = _project.Channels[data];
break;
case Enums.Event.WordNewPat:
while (_project.Patterns.Count < data) _project.Patterns.Add(new Pattern { Id = _project.Patterns.Count });
_curPattern = _project.Patterns[data - 1];
break;
case Enums.Event.WordTempo:
_project.Tempo = data;
break;
case Enums.Event.WordFadeStereo:
if (genData == null) break;
if ((data & 0x02) != 0) genData.SampleReversed = true;
else if ((data & 0x100) != 0) genData.SampleReverseStereo = true;
break;
case Enums.Event.WordPreAmp:
if (genData == null) break;
genData.SampleAmp = data;
break;
case Enums.Event.WordMainPitch:
_project.MainPitch = data;
break;
case Enums.Event.WordInsertIcon:
_curInsert.Icon = data;
break;
case Enums.Event.WordCurrentSlotNum:
if (_curSlot != null) // Current slot after plugin event, now re-arranged.
{
_curInsert.Slots[data] = _curSlot;
_curSlot = new InsertSlot();
}
_curChannel = null;
break;
}
}
private void ParseDwordEvent(Enums.Event eventId, BinaryReader reader)
{
var data = reader.ReadUInt32();
OutputLine($"int: {data:X8}");
switch (eventId)
{
case Enums.Event.DWordColor:
if (_curChannel != null) _curChannel.Color = data;
break;
case Enums.Event.DWordMiddleNote:
if (_curChannel != null && _curChannel.Data is GeneratorData genData) genData.BaseNote = data + 9;
break;
case Enums.Event.DWordInsertColor:
_curInsert.Color = data;
break;
case Enums.Event.DWordFineTempo:
_project.Tempo = data / 1000.0;
break;
}
}
private static int GetBufferLen(BinaryReader reader)
{
var data = reader.ReadByte();
var dataLen = data & 0x7F;
var shift = 0;
while ((data & 0x80) != 0)
{
data = reader.ReadByte();
dataLen = dataLen | ((data & 0x7F) << (shift += 7));
}
return dataLen;
}
private void ParseTextEvent(Enums.Event eventId, BinaryReader reader)
{
var dataLen = GetBufferLen(reader);
var dataBytes = reader.ReadBytes(dataLen);
var unicodeString = Encoding.Unicode.GetString(dataBytes);
if (unicodeString.EndsWith("\0")) unicodeString = unicodeString.Substring(0, unicodeString.Length - 1);
OutputLine($"text: {unicodeString}");
var genData = _curChannel?.Data as GeneratorData;
switch (eventId)
{
case Enums.Event.TextChanName:
if (_curChannel != null) _curChannel.Name = unicodeString;
break;
case Enums.Event.TextPatName:
if (_curPattern != null) _curPattern.Name = unicodeString;
break;
case Enums.Event.TextTitle:
_project.ProjectTitle = unicodeString;
break;
case Enums.Event.TextAuthor:
_project.Author = unicodeString;
break;
case Enums.Event.TextComment:
_project.Comment = unicodeString;
break;
case Enums.Event.TextGenre:
_project.Genre = unicodeString;
break;
case Enums.Event.TextSampleFileName:
if (genData == null) break;
genData.SampleFileName = unicodeString;
genData.GeneratorName = "Sampler";
break;
case Enums.Event.TextVersion:
_project.VersionString = Encoding.UTF8.GetString(dataBytes);
if (_project.VersionString.EndsWith("\0")) _project.VersionString = _project.VersionString.Substring(0, _project.VersionString.Length - 1);
var numbers = _project.VersionString.Split('.');
_versionMajor = int.Parse(numbers[0]);
_project.Version = (int.Parse(numbers[0]) << 8) +
(int.Parse(numbers[1]) << 4) +
(int.Parse(numbers[2]) << 0);
break;
case Enums.Event.GeneratorName:
if (genData != null) genData.GeneratorName = unicodeString;
break;
case Enums.Event.TextInsertName:
_curInsert.Name = unicodeString;
break;
}
}
private void ParseDataEvent(Enums.Event eventId, BinaryReader reader)
{
var dataLen = GetBufferLen(reader);
var dataStart = reader.BaseStream.Position;
var dataEnd = dataStart + dataLen;
var genData = _curChannel?.Data as GeneratorData;
var autData = _curChannel?.Data as AutomationData;
var slotData = _curSlot;
switch (eventId)
{
case Enums.Event.DataPluginParams:
if (slotData != null)
{
OutputLine($"Found plugin settings for insert (id {_curInsert.Id})");
_curSlot.PluginSettings = reader.ReadBytes(dataLen);
_curSlot.Plugin = ParsePluginChunk(slotData.PluginSettings);
}
else
{
if (genData == null) break;
if (genData.PluginSettings != null)
throw new Exception("Attempted to overwrite plugin");
OutputLine($"Found plugin settings for {genData.GeneratorName} (id {_curChannel.Id})");
genData.PluginSettings = reader.ReadBytes(dataLen);
genData.Plugin = ParsePluginChunk(genData.PluginSettings);
}
break;
case Enums.Event.DataChanParams:
{
if (genData == null) break;
var unknown1 = reader.ReadBytes(40);
genData.ArpDir = (Enums.ArpDirection)reader.ReadInt32();
genData.ArpRange = reader.ReadInt32();
genData.ArpChord = reader.ReadInt32();
genData.ArpTime = reader.ReadInt32() + 1;
genData.ArpGate = reader.ReadInt32();
genData.ArpSlide = reader.ReadBoolean();
var unknown2 = reader.ReadBytes(31);
genData.ArpRepeat = reader.ReadInt32();
var unknown3 = reader.ReadBytes(29);
}
break;
case Enums.Event.DataBasicChanParams:
if (genData == null) break;
genData.Panning = reader.ReadInt32();
genData.Volume = reader.ReadInt32();
break;
case Enums.Event.DataPatternNotes:
while (reader.BaseStream.Position < dataEnd)
{
var pos = reader.ReadInt32();
var unknown1 = reader.ReadInt16();
var ch = reader.ReadByte();
var unknown2 = reader.ReadByte();
var length = reader.ReadInt32();
var key = reader.ReadByte();
var unknown3 = reader.ReadInt16();
var unknown4 = reader.ReadByte();
var finePitch = reader.ReadUInt16();
var release = reader.ReadUInt16();
var pan = reader.ReadByte();
var velocity = reader.ReadByte();
var x1 = reader.ReadByte();
var x2 = reader.ReadByte();
var channel = _project.Channels[ch];
if (!_curPattern.Notes.ContainsKey(channel)) _curPattern.Notes.Add(channel, new List<Note>());
_curPattern.Notes[channel].Add(new Note
{
Position = pos,
Length = length,
Key = key,
FinePitch = finePitch,
Release = release,
Pan = pan,
Velocity = velocity
});
}
break;
case Enums.Event.DataInsertParams:
while (reader.BaseStream.Position < dataEnd)
{
var startPos = reader.BaseStream.Position;
var unknown1 = reader.ReadInt32();
var messageId = (Enums.InsertParam)reader.ReadByte();
var unknown2 = reader.ReadByte();
var channelData = reader.ReadUInt16();
var messageData = reader.ReadInt32();
var slotId = channelData & 0x3F;
var insertId = (channelData >> 6) & 0x7F;
var insertType = channelData >> 13;
var insert = _project.Inserts[insertId];
switch (messageId)
{
case Enums.InsertParam.SlotState:
insert.Slots[slotId].State = messageData;
break;
case Enums.InsertParam.SlotVolume:
insert.Slots[slotId].Volume = messageData;
break;
case Enums.InsertParam.Volume:
insert.Volume = messageData;
break;
case Enums.InsertParam.Pan:
insert.Pan = messageData;
break;
case Enums.InsertParam.StereoSep:
insert.StereoSep = messageData;
break;
case Enums.InsertParam.LowLevel:
insert.LowLevel = messageData;
break;
case Enums.InsertParam.BandLevel:
insert.BandLevel = messageData;
break;
case Enums.InsertParam.HighLevel:
insert.HighLevel = messageData;
break;
case Enums.InsertParam.LowFreq:
insert.LowFreq = messageData;
break;
case Enums.InsertParam.BandFreq:
insert.BandFreq = messageData;
break;
case Enums.InsertParam.HighFreq:
insert.HighFreq = messageData;
break;
case Enums.InsertParam.LowWidth:
insert.LowWidth = messageData;
break;
case Enums.InsertParam.BandWidth:
insert.BandWidth = messageData;
break;
case Enums.InsertParam.HighWidth:
insert.HighWidth = messageData;
break;
default:
if ((int)messageId >= 64 && (int)messageId <= 64 + 104) // any value 64 or above appears to be the desination insert
{
var insertDest = (int)messageId - 64;
insert.RouteVolumes[insertDest] = messageData;
OutputLine($"{startPos:X4} insert send from {insertId} to {insertDest} volume: {messageData:X8}");
}
else
{
OutputLine($"{startPos:X4} insert param: {messageId} {insertId}-{slotId}, data: {messageData:X8}");
}
break;
}
}
break;
case Enums.Event.DataAutomationChannels:
while (reader.BaseStream.Position < dataEnd)
{
var unknown1 = reader.ReadUInt16();
var automationChannel = reader.ReadByte();
var unknown2 = reader.ReadUInt32();
var unknown3 = reader.ReadByte();
var param = reader.ReadUInt16();
var paramDestination = reader.ReadInt16();
var unknown4 = reader.ReadUInt64();
var channel = _project.Channels[automationChannel];
if ((paramDestination & 0x2000) == 0) // Automation on channel
{
channel.Data = new AutomationData
{
Channel = _project.Channels[paramDestination],
Parameter = param & 0x7fff,
VstParameter = (param & 0x8000) > 0 ? true : false // switch determines if automation is on channel or vst
};
}
else
{
channel.Data = new AutomationData // automation on insert slot
{
Parameter = param & 0x7fff,
InsertId = (paramDestination & 0x0FF0) >> 6, // seems to be out by one
SlotId = paramDestination & 0x003F
};
}
}
break;
case Enums.Event.DataPlayListItems:
while (reader.BaseStream.Position < dataEnd)
{
var startTime = reader.ReadInt32();
var patternBase = reader.ReadUInt16();
var patternId = reader.ReadUInt16();
var length = reader.ReadInt32();
var track = reader.ReadInt32();
if (_versionMajor == 20)
track = 501 - track;
else
track = 198 - track;
var unknown1 = reader.ReadUInt16();
var itemFlags = reader.ReadUInt16();
var unknown3 = reader.ReadUInt32();
bool muted = (itemFlags & 0x2000) > 0 ? true : false; // flag determines if item is muted
// id of 0-patternBase is samples or automation, after is pattern
if (patternId <= patternBase)
{
var startOffset = (int)(reader.ReadSingle() * (float)_project.Ppq);
var endOffset = (int)(reader.ReadSingle() * (float)_project.Ppq);
_project.Tracks[track].Items.Add(new ChannelPlaylistItem
{
Position = startTime,
Length = length,
StartOffset = startOffset,
EndOffset = endOffset,
Channel = _project.Channels[patternId],
Muted = muted
});
}
else
{
var startOffset = reader.ReadInt32();
var endOffset = reader.ReadInt32();
_project.Tracks[track].Items.Add(new PatternPlaylistItem
{
Position = startTime,
Length = length,
StartOffset = startOffset,
EndOffset = endOffset,
Pattern = _project.Patterns[patternId - patternBase - 1],
Muted = muted
});
}
}
break;
case Enums.Event.DataAutomationData:
{
var unknown1 = reader.ReadUInt32(); // always 1?
var unknown2 = reader.ReadUInt32(); // always 64?
var unknown3 = reader.ReadByte();
var unknown4 = reader.ReadUInt16();
var unknown5 = reader.ReadUInt16(); // always 0?
var unknown6 = reader.ReadUInt32();
var keyCount = reader.ReadUInt32();
if (autData == null) break;
autData.Keyframes = new AutomationKeyframe[keyCount];
for (var i = 0; i < keyCount; i++)
{
var startPos = reader.BaseStream.Position;
var keyPos = reader.ReadDouble();
var keyVal = reader.ReadDouble();
var keyTension = reader.ReadSingle();
var unknown7 = reader.ReadUInt32(); // seems linked to tension?
var endPos = reader.BaseStream.Position;
reader.BaseStream.Position = startPos;
var byteData = reader.ReadBytes((int)(endPos - startPos));
OutputLine($"Key {i} data: {string.Join(" ", byteData.Select(x => x.ToString("X2")))}");
autData.Keyframes[i] = new AutomationKeyframe
{
Position = (int)(keyPos * _project.Ppq),
Tension = keyTension,
Value = keyVal
};
}
// remaining data is unknown
}
break;
case Enums.Event.DataInsertRoutes:
for (var i = 0; i < Project.MaxInsertCount; i++)
{
_curInsert.Routes[i] = reader.ReadBoolean();
}
var newIndex = _curInsert.Id + 1;
if (newIndex < _project.Inserts.Length) _curInsert = _project.Inserts[newIndex];
break;
case Enums.Event.DataInsertFlags:
reader.ReadUInt32();
var flags = (Enums.InsertFlags)reader.ReadUInt32();
_curInsert.Flags = flags;
_curSlot = new InsertSlot(); // New insert route, create new slot
break;
}
// make sure cursor is at end of data
reader.BaseStream.Position = dataEnd;
}
private Plugin ParsePluginChunk(byte[] chunk)
{
var plugin = new Plugin();
using (var reader = new BinaryReader(new MemoryStream(chunk)))
{
var pluginType = (Enums.PluginType)reader.ReadInt32();
if (pluginType != Enums.PluginType.Vst)
{
return null;
}
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
var eventId = (Enums.PluginChunkId)reader.ReadInt32();
var length = (int)reader.ReadInt64();
switch (eventId)
{
case Enums.PluginChunkId.VendorName:
plugin.VendorName = Encoding.ASCII.GetString(reader.ReadBytes(length));
break;
case Enums.PluginChunkId.Filename:
plugin.FileName = Encoding.ASCII.GetString(reader.ReadBytes(length));
break;
case Enums.PluginChunkId.Name:
plugin.Name = Encoding.ASCII.GetString(reader.ReadBytes(length));
break;
case Enums.PluginChunkId.State:
plugin.State = reader.ReadBytes(length);
break;
default:
OutputLine($"Event {eventId}, data: {string.Join(" ", reader.ReadBytes(length).Select(x => x.ToString("X2")))}");
break;
}
}
return plugin;
}
}
private void Output(string value)
{
if (_verbose)
Console.Write(value);
}
private void OutputLine(string value)
{
if (_verbose)
Console.WriteLine(value);
}
}
}