-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSequenceFile.cs
More file actions
578 lines (486 loc) · 22.6 KB
/
SequenceFile.cs
File metadata and controls
578 lines (486 loc) · 22.6 KB
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
using GotaSoundIO;
using GotaSoundIO.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GotaSequenceLib {
/// <summary>
/// Sequence file.
/// </summary>
public abstract class SequenceFile : IOFile {
/// <summary>
/// Commands.
/// </summary>
public List<SequenceCommand> Commands = new List<SequenceCommand>();
/// <summary>
/// Labels.
/// </summary>
public Dictionary<string, uint> Labels = new Dictionary<string, uint>();
/// <summary>
/// Command indices.
/// </summary>
private Dictionary<uint, int> CommandIndices;
/// <summary>
/// Song name.
/// </summary>
public string Name;
/// <summary>
/// Raw data.
/// </summary>
public byte[] RawData = new byte[0];
/// <summary>
/// If success to writing commands.
/// </summary>
public bool WritingCommandSuccess { get; protected set; } = true;
/// <summary>
/// The sequence platform.
/// </summary>
/// <returns>The platform.</returns>
public abstract SequencePlatform Platform();
/// <summary>
/// Public labels.
/// </summary>
public Dictionary<string, int> PublicLabels { get; protected set; } = new Dictionary<string, int>();
/// <summary>
/// Other labels.
/// </summary>
public List<int> OtherLabels { get; protected set; } = new List<int>();
/// <summary>
/// Blank constructor.
/// </summary>
public SequenceFile() {}
/// <summary>
/// Reads the sequence file from a file path.
/// </summary>
/// <param name="filePath">The file path.</param>
public SequenceFile(string filePath) : base(filePath) {}
/// <summary>
/// Read command data into the commands list.
/// </summary>
/// <param name="globalMode">If to make private labels global.</param>
public void ReadCommandData(bool globalMode = false) {
//New reader.
using (MemoryStream src = new MemoryStream(RawData)) {
using (FileReader r = new FileReader(src)) {
//Command index.
int commandInd = 0;
//Platform.
var p = Platform();
//Offsets to command index.
Dictionary<uint, int> offsetMap = new Dictionary<uint, int>();
//Commands.
Commands = new List<SequenceCommand>();
//Labels.
PublicLabels = new Dictionary<string, int>();
OtherLabels = new List<int>();
//Read until end.
while (r.Position < RawData.Length) {
//Add command index.
offsetMap.Add((uint)r.Position, commandInd);
//MIDI2SSEQ fix because it doesn't understand that jumps use UInt24, not UInt32.
if (r.Position < RawData.Length - 1 && Commands.Count > 0 && Commands.Last().CommandType == SequenceCommands.Jump) {
long bak = r.Position;
if (r.ReadByte() == 0) {
continue;
} else {
r.Position = bak;
}
}
//Read the command.
SequenceCommand c = new SequenceCommand();
c.Read(r, p);
Commands.Add(c);
commandInd++;
}
//Public labels.
for (int i = 0; i < Labels.Count; i++) {
PublicLabels.Add(Labels.Keys.ElementAt(i), offsetMap[Labels.Values.ElementAt(i)]);
}
//Get labels.
for (int i = 0; i < Commands.Count; i++) {
//Command index.
int commandIndex = 0;
//Switch the type.
SequenceCommands trueType = Playback.Player.GetTrueCommandType(Commands[i]);
switch (trueType) {
//It has an offset.
case SequenceCommands.Call:
case SequenceCommands.Jump:
case SequenceCommands.OpenTrack:
commandIndex = SetOffsetIndex(Commands[i], offsetMap);
break;
}
//Label.
string label = "";
//Possible type.
if (trueType == SequenceCommands.Call || trueType == SequenceCommands.Jump || trueType == SequenceCommands.OpenTrack) {
//Get label.
uint offset = offsetMap.FirstOrDefault(x => x.Value == commandIndex).Key;
if (Labels.ContainsValue(offset)) {
label = Labels.FirstOrDefault(x => x.Value == offset).Key;
} else {
label = (globalMode ? "C" : "_c") + "ommand_" + commandIndex;
OtherLabels.Add(commandIndex);
}
}
//Set label.
switch (trueType) {
//Set the label.
case SequenceCommands.Call:
case SequenceCommands.Jump:
case SequenceCommands.OpenTrack:
SetCommandLabel(Commands[i], label);
break;
}
}
//Set reference commands.
for (int i = 0; i < Commands.Count; i++) {
SequenceCommands trueType = Playback.Player.GetTrueCommandType(Commands[i]);
switch (trueType) {
case SequenceCommands.Call:
case SequenceCommands.Jump:
case SequenceCommands.OpenTrack:
SetReferenceCommand(Commands[i]);
break;
}
}
//Set offset.
CommandIndices = offsetMap;
}
}
}
/// <summary>
/// Write command data.
/// </summary>
public void WriteCommandData() {
//Output stream.
using (MemoryStream o = new MemoryStream()) {
using (FileWriter w = new FileWriter(o)) {
//Platform.
var p = Platform();
//Convert indices to offsets.
Dictionary<int, uint> indexMap = new Dictionary<int, uint>();
int commandInd = 0;
foreach (var c in Commands) {
indexMap.Add(commandInd, (uint)w.Position);
if (c.CommandType == SequenceCommands.Note || p.CommandMap().ContainsKey(c.CommandType) || p.ExtendedCommands().ContainsKey(c.CommandType)) {
c.Write(w, p);
}
commandInd++;
}
//Set position back.
w.Position = 0;
//Fix labels.
Labels = new Dictionary<string, uint>();
for (int i = 0; i < PublicLabels.Count; i++) {
Labels.Add(PublicLabels.Keys.ElementAt(i), indexMap[PublicLabels.Values.ElementAt(i)]);
}
//Fix commands.
for (int i = 0; i < Commands.Count; i++) {
SequenceCommands trueCommandType = Playback.Player.GetTrueCommandType(Commands[i]);
switch (trueCommandType) {
case SequenceCommands.Call:
case SequenceCommands.Jump:
case SequenceCommands.OpenTrack:
SetIndexOffset(Commands[i], indexMap);
break;
}
}
//Write every command for real now.
foreach (var c in Commands) {
//Only if supported command.
if (c.CommandType == SequenceCommands.Note || p.CommandMap().ContainsKey(c.CommandType) || p.ExtendedCommands().ContainsKey(c.CommandType)) {
c.Write(w, p);
}
}
//Set data.
RawData = o.ToArray();
//Set map.
CommandIndices = indexMap.ToDictionary(x => x.Value, x => x.Key);
}
}
}
/// <summary>
/// Set the index for a command from an offset.
/// </summary>
/// <param name="c">The command.</param>
/// <param name="offsetMap">The offset map.</param>
public int SetOffsetIndex(SequenceCommand c, Dictionary<uint, int> offsetMap) {
switch (c.CommandType) {
case SequenceCommands.Random:
case SequenceCommands.TimeRandom:
return SetOffsetIndex((c.Parameter as RandomParameter).Command, offsetMap);
case SequenceCommands.If:
return SetOffsetIndex(c.Parameter as SequenceCommand, offsetMap);
case SequenceCommands.Variable:
case SequenceCommands.TimeVariable:
return SetOffsetIndex((c.Parameter as VariableParameter).Command, offsetMap);
case SequenceCommands.Time:
return SetOffsetIndex((c.Parameter as TimeParameter).Command, offsetMap);
case SequenceCommands.Jump:
case SequenceCommands.Call:
(c.Parameter as UInt24Parameter).m_Index = offsetMap[(c.Parameter as UInt24Parameter).Offset];
return (c.Parameter as UInt24Parameter).m_Index;
case SequenceCommands.OpenTrack:
(c.Parameter as OpenTrackParameter).m_Index = offsetMap[(c.Parameter as OpenTrackParameter).Offset];
return (c.Parameter as OpenTrackParameter).m_Index;
}
return -1;
}
/// <summary>
/// Set the index for a command from an offset.
/// </summary>
/// <param name="c">The command.</param>
/// <param name="label">The label.</param>
public int SetCommandLabel(SequenceCommand c, string label) {
switch (c.CommandType) {
case SequenceCommands.Random:
case SequenceCommands.TimeRandom:
SetCommandLabel((c.Parameter as RandomParameter).Command, label);
break;
case SequenceCommands.If:
SetCommandLabel(c.Parameter as SequenceCommand, label);
break;
case SequenceCommands.Variable:
case SequenceCommands.TimeVariable:
SetCommandLabel((c.Parameter as VariableParameter).Command, label);
break;
case SequenceCommands.Time:
SetCommandLabel((c.Parameter as TimeParameter).Command, label);
break;
case SequenceCommands.Jump:
case SequenceCommands.Call:
(c.Parameter as UInt24Parameter).Label = label;
break;
case SequenceCommands.OpenTrack:
(c.Parameter as OpenTrackParameter).Label = label;
break;
}
return -1;
}
/// <summary>
/// Set the offset for a command from an index.
/// </summary>
/// <param name="c">The command.</param>
/// <param name="offsetMap">The offset map.</param>
public uint SetIndexOffset(SequenceCommand c, Dictionary<int, uint> offsetMap) {
switch (c.CommandType) {
case SequenceCommands.Random:
case SequenceCommands.TimeRandom:
return SetIndexOffset((c.Parameter as RandomParameter).Command, offsetMap);
case SequenceCommands.If:
return SetIndexOffset(c.Parameter as SequenceCommand, offsetMap);
case SequenceCommands.Variable:
case SequenceCommands.TimeVariable:
return SetIndexOffset((c.Parameter as VariableParameter).Command, offsetMap);
case SequenceCommands.Time:
return SetIndexOffset((c.Parameter as TimeParameter).Command, offsetMap);
case SequenceCommands.Jump:
case SequenceCommands.Call:
(c.Parameter as UInt24Parameter).Offset = offsetMap[(c.Parameter as UInt24Parameter).m_Index];
return (c.Parameter as UInt24Parameter).Offset;
case SequenceCommands.OpenTrack:
(c.Parameter as OpenTrackParameter).Offset = offsetMap[(c.Parameter as OpenTrackParameter).m_Index];
return (c.Parameter as OpenTrackParameter).Offset;
}
return 0xFFFFFFFF;
}
/// <summary>
/// Set the reference command.
/// </summary>
/// <param name="c">The command to have its reference set.</param>
public void SetReferenceCommand(SequenceCommand c) {
switch (c.CommandType) {
case SequenceCommands.Random:
case SequenceCommands.TimeRandom:
SetReferenceCommand((c.Parameter as RandomParameter).Command);
break;
case SequenceCommands.If:
SetReferenceCommand(c.Parameter as SequenceCommand);
break;
case SequenceCommands.Variable:
case SequenceCommands.TimeVariable:
SetReferenceCommand((c.Parameter as VariableParameter).Command);
break;
case SequenceCommands.Time:
SetReferenceCommand((c.Parameter as TimeParameter).Command);
break;
case SequenceCommands.Jump:
case SequenceCommands.Call:
(c.Parameter as UInt24Parameter).ReferenceCommand = Commands[(c.Parameter as UInt24Parameter).Index(Commands)];
break;
case SequenceCommands.OpenTrack:
(c.Parameter as OpenTrackParameter).ReferenceCommand = Commands[(c.Parameter as OpenTrackParameter).Index(Commands)];
break;
}
}
/// <summary>
/// Convert the file to text.
/// </summary>
/// <returns>The file as text.</returns>
public string[] ToText() {
//Command list.
ReadCommandData();
List<string> l = new List<string>();
//Add header.
l.Add(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;");
l.Add(";");
l.Add("; " + Name);
l.Add("; Generated By Gota's Sound Tools");
l.Add(";");
l.Add(";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;");
l.Add("");
//For each command. Last one isn't counted.
for (int i = 0; i < Commands.Count; i++) {
//Add labels.
bool labelAdded = false;
var labels = PublicLabels.Where(x => x.Value == i).Select(x => x.Key);
foreach (var label in labels) {
if (i != 0 && !labelAdded && Commands[i - 1].CommandType == SequenceCommands.Fin) {
l.Add(" ");
}
l.Add(label + ":");
labelAdded = true;
}
if (OtherLabels.Contains(i)) {
if (i != 0 && !labelAdded && Commands[i - 1].CommandType == SequenceCommands.Fin) {
l.Add(" ");
}
l.Add("_command_" + i + ":");
labelAdded = true;
}
//Add command.
if (i < Commands.Count - 1) {
l.Add("\t" + Commands[i].ToString());
}
}
//Return the list.
return l.ToArray();
}
/// <summary>
/// Create a sequence from text.
/// </summary>
/// <param name="text">The text.</param>
public void FromText(List<string> text) {
//Success by default.
WritingCommandSuccess = true;
//Reset labels.
PublicLabels = new Dictionary<string, int>();
OtherLabels = new List<int>();
Dictionary<string, int> privateLabels = new Dictionary<string, int>();
List<int> labelLines = new List<int>();
//Format text.
List<string> t = text.ToList();
int comNum = 0;
for (int i = t.Count - 1; i >= 0; i--) {
t[i] = t[i].Replace("\t", " ").Replace("\r", "").Replace(" ", " ").Replace(" ", " ").Replace(" ", " ").Replace(" ", " ").Replace(" ", " ");
try { t[i] = t[i].Split(';')[0]; } catch { }
if (t[i].Replace(" ", "").Length == 0) { t.RemoveAt(i); continue; }
for (int j = 0; j < t[i].Length; j++) {
if (t[i][j].Equals(' ')) {
t[i] = t[i].Substring(j + 1);
j--;
} else {
break;
}
}
}
//Fetch labels.
for (int i = 0; i < t.Count; i++) {
//If it's a label.
if (t[i].EndsWith(":")) {
labelLines.Add(i);
if (t[i].StartsWith("_")) {
privateLabels.Add(t[i].Replace(":", ""), comNum);
OtherLabels.Add(comNum);
} else {
PublicLabels.Add(t[i].Replace(":", ""), comNum);
}
} else {
comNum++;
}
}
//Sort labels.
PublicLabels = PublicLabels.OrderBy(obj => new NullTerminatedString(obj.Key)).ToDictionary(obj => obj.Key, obj => obj.Value);
//Get commands.
Commands = new List<SequenceCommand>();
for (int i = 0; i < t.Count; i++) {
if (labelLines.Contains(i)) {
continue;
}
SequenceCommand seq = new SequenceCommand();
try { seq.FromString(t[i], PublicLabels, privateLabels); } catch (Exception e) { WritingCommandSuccess = false; throw new Exception("Command " + i + ": \"" + t[i] + "\" is invalid.", e); }
Commands.Add(seq);
}
//Set reference commands.
for (int i = 0; i < Commands.Count; i++) {
SequenceCommands trueType = Playback.Player.GetTrueCommandType(Commands[i]);
switch (trueType) {
case SequenceCommands.Call:
case SequenceCommands.Jump:
case SequenceCommands.OpenTrack:
SetReferenceCommand(Commands[i]);
break;
}
}
//Fin.
Commands.Add(new SequenceCommand() { CommandType = SequenceCommands.Fin });
WriteCommandData();
}
/// <summary>
/// Convert a data offset to an index.
/// </summary>
/// <param name="offset">Data offset.</param>
/// <returns>Index.</returns>
public int ConvertOffset(uint offset) {
//Get lowest distance.
int lowest = -1;
long minDist = long.MaxValue;
for (int i = 0; i < CommandIndices.Count; i++) {
long dist = Math.Abs(offset - CommandIndices.Keys.ElementAt(i));
if (dist < minDist) {
minDist = dist;
lowest = i;
}
}
return CommandIndices.Values.ElementAt(lowest);
}
/// <summary>
/// Create a sequence from an MIDI.
/// </summary>
/// <param name="filePath">The MIDI path.</param>
/// <param name="timeBase">Time base.</param>
public void FromMIDI(string filePath, int timeBase = 48, bool privateLabelsForCalls = false) {
Sanford.Multimedia.Midi.Sequence s = new Sanford.Multimedia.Midi.Sequence(filePath);
Dictionary<string, int> pub;
List<int> priv;
Commands = SMF.ToSequenceCommands(s, out pub, out priv, Path.GetFileNameWithoutExtension(filePath), timeBase);
PublicLabels = pub;
OtherLabels = priv;
WriteCommandData();
}
/// <summary>
/// Convert the file to an MIDI.
/// </summary>
/// <param name="filePath">Path to save the MIDI.</param>
/// <param name="trackMask">Track mask.</param>
public void SaveMIDI(string filePath, ushort trackMask = 0xFFFF) {
ReadCommandData();
Sanford.Multimedia.Midi.Sequence s = SMF.FromSequenceCommands(Commands, 0, trackMask);
s.Save(filePath);
}
/// <summary>
/// Copy from another sequence.
/// </summary>
/// <param name="other">Other sequence.</param>
public void CopyFromOther(SequenceFile other) {
other.ReadCommandData();
Commands = other.Commands;
PublicLabels = other.PublicLabels;
OtherLabels = other.OtherLabels;
}
}
}