-
Notifications
You must be signed in to change notification settings - Fork 21
/
GP6File.cs
executable file
·1537 lines (1342 loc) · 58.9 KB
/
GP6File.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
public class GP6File : GPFile
{
private byte[] udata; //uncompressed data
private static List<GP6Tempo> tempos = new List<GP6Tempo>();
private static List<GP6Chord> chords = new List<GP6Chord>();
private static List<GP6Rhythm> rhythms = new List<GP6Rhythm>();
//public List<Track> tracks;
public GP6File(byte[] _data)
{
GPBase.pointer = 0;
this.udata = _data;
}
private void decompressFile()
{
List<byte> data = new List<byte>();
BitStream bs = new BitStream(udata);
int estFileSize = System.BitConverter.ToInt32(udata, 4);
bs.SkipBytes(8);
while (!bs.finished)
{
bool isCompressed = bs.GetBit();
if (isCompressed)
{
int wordSize = bs.GetBitsBE(4);
int offset = bs.GetBitsLE(wordSize);
int length = bs.GetBitsLE(wordSize);
int sourcePosition = data.Count - offset;
if (sourcePosition < 0) break;
int to_read = Math.Min(length, offset);
for (int r = sourcePosition; r < sourcePosition + to_read; r++)
{
data.Add(data[r]);
}
}
else
{
int byteLength = bs.GetBitsLE(2);
for (int x = 0; x < byteLength; x++) data.Add(bs.GetByte());
}
}
GPBase.data = data.ToArray();
}
public override void readSong()
{
decompressFile();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
int startOfXml = 0;
for (int x = 0; x < GPBase.data.Length; x++) //8150
{
//string hex = BitConverter.ToString(GPBase.data.ToList().GetRange(x,Math.Min(8150,GPBase.data.Length-x)).ToArray()).Replace("-", string.Empty);
sb.Append((char)(GPBase.data[x]));
if (startOfXml == 0 && (char)GPBase.data[x] == '<' && (char)GPBase.data[x + 1] == 'G') startOfXml = x;
}
string xml = sb.ToString();
for (int x = startOfXml; x<xml.Length; x+=8000)
{
//Debug.Log(xml.Substring(x, 8000));
}
Node parsedXml = ParseGP6(xml, startOfXml);
GP5File gp5File = GP6NodeToGP5File(parsedXml.subnodes[0]);
tracks = gp5File.tracks;
self = gp5File;
}
public static GP5File GP6NodeToGP5File(Node node) //node = GPIF tag
{
var file = new GP5File(new byte[] { });
file.version = "GUITAR PRO 6.0";
file.versionTuple = new int[] { 6, 0 };
//set direct members of song:
file.title = node.getSubnodeByName("Score",true).subnodes[0].content;
file.subtitle = node.getSubnodeByName("Score").subnodes[1].content;
file.interpret = node.getSubnodeByName("Score").subnodes[2].content;
file.album = node.getSubnodeByName("Score").subnodes[3].content;
file.words = node.getSubnodeByName("Score").subnodes[4].content;
file.music = node.getSubnodeByName("Score").subnodes[5].content;
file.copyright = node.getSubnodeByName("Score").subnodes[7].content;
file.tab_author = node.getSubnodeByName("Score").subnodes[8].content;
file.instructional= node.getSubnodeByName("Score").subnodes[9].content;
file.notice = node.getSubnodeByName("Score").subnodes[10].content.Split('\n'); //?
//Page Layout
Node nPageLayout = node.getSubnodeByName("Score", true).getSubnodeByName("PageSetup", true);
file.pageSetup = new global::PageSetup();
if (nPageLayout != null) {
file.pageSetup.pageSize = new Point(int.Parse(nPageLayout.subnodes[0].content), int.Parse(nPageLayout.subnodes[1].content));
file.pageSetup.pageMargin = new Padding(int.Parse(nPageLayout.subnodes[5].content),
int.Parse(nPageLayout.subnodes[3].content),
int.Parse(nPageLayout.subnodes[4].content),
int.Parse(nPageLayout.subnodes[6].content));
file.pageSetup.scoreSizeProportion = float.Parse(nPageLayout.subnodes[7].content);
}
file.lyrics = transferLyrics(node.getSubnodeByName("Tracks"));
//tempo, key, midiChannels, directions only on a per track / per measureHeader (MasterBar) basis
file.measureCount = node.getSubnodeByName("MasterBars",true).subnodes.Count;
file.trackCount = node.getSubnodeByName("Tracks",true).subnodes.Count;
Node nAutomations = node.getSubnodeByName("MasterTrack", true).getSubnodeByName("Automations",true);
foreach(Node nAutomation in nAutomations.subnodes)
{
tempos.Add(new GP6Tempo(nAutomation));
}
file.measureHeaders = transferMeasureHeaders(node.getSubnodeByName("MasterBars"), file);
file.tracks = transferTracks(node.getSubnodeByName("Tracks", true),file);
rhythms = readRhythms(node.getSubnodeByName("Rhythms", true));
chords = readChords(node.getSubnodeByName("Tracks", true));
transferBars( node, file); //Bars > Voices > Beats > Notes
//TODO update global vars tempo, key, midiChannels, directions based on first value?
return file;
}
public static int currentMeasure = 0;
public static int currentTrack = 0;
public static void transferBars(Node node, GP5File song)
{
Node nBars = node.getSubnodeByName("Bars", true);
int cnt = 0;
int barCnt = -1;
foreach (Node nBar in nBars.subnodes)
{
var _bar = new Measure();
string clef = nBar.getSubnodeByName("Clef").content;
if (clef.Equals("G2")) _bar.clef = MeasureClef.treble;
if (clef.Equals("F4")) _bar.clef = MeasureClef.bass;
if (clef.Equals("Neutral")) _bar.clef = MeasureClef.neutral;
//.. not important for this app.
string[] voices = nBar.getSubnodeByName("Voices").content.Split(' ');
_bar.track = song.tracks[cnt % song.trackCount];
if (cnt % song.trackCount == 0) barCnt++;
_bar.header = song.measureHeaders[barCnt];
currentMeasure = barCnt;
currentTrack = cnt % song.trackCount;
if (currentTrack == 9)
{
int a = 3;
}
cnt++;
Node nSimileMark = nBar.getSubnodeByName("SimileMark", true);
if (nSimileMark != null)
{
if (nSimileMark.content.Equals("Simple")) _bar.simileMark = SimileMark.simple;
if (nSimileMark.content.Equals("FirstOfDouble")) _bar.simileMark = SimileMark.firstOfDouble;
if (nSimileMark.content.Equals("SecondOfDouble")) _bar.simileMark = SimileMark.secondOfDouble;
}
_bar.voices = new List<Voice>();
foreach (string voice in voices)
{
if (int.Parse(voice) >= 0) _bar.voices.Add(transferVoice(node, int.Parse(voice), _bar));
}
song.tracks[(cnt - 1) % song.trackCount].addMeasure(_bar);
}
}
private static int flipDuration(Duration d)
{
int ticks_per_beat = 960;
int result = 0;
switch (d.value)
{
case 1: result += ticks_per_beat * 4; break;
case 2: result += ticks_per_beat * 2; break;
case 4: result += ticks_per_beat; break;
case 8: result += ticks_per_beat / 2; break;
case 16: result += ticks_per_beat / 4; break;
case 32: result += ticks_per_beat / 8; break;
case 64: result += ticks_per_beat / 16; break;
case 128: result += ticks_per_beat / 32; break;
}
if (d.isDotted) result = (int)(result * 1.5f);
if (d.isDoubleDotted) result = (int)(result * 1.75f);
return result;
}
public static int totalLength;
public static int lengthPassed = 0;
public static Voice transferVoice(Node node, int index, Measure bar)
{
totalLength = flipDuration(bar.header.timeSignature.denominator) * bar.header.timeSignature.numerator;
lengthPassed = 0;
Voice voice = new Voice();
string[] beats = node.getSubnodeByName("Voices",true).subnodes[index].getSubnodeByName("Beats",true).content.Split(' ');
voice.beats = new List<Beat>();
voice.measure = bar;
foreach (string beat in beats)
{
voice.beats.Add(transferBeat(node, int.Parse(beat),voice));
}
return voice;
}
public static Beat transferBeat(Node node, int index, Voice voice)
{
Beat beat = new Beat();
Node nBeat = node.getSubnodeByName("Beats", true).subnodes[index];
Node nNotes = nBeat.getSubnodeByName("Notes");
beat.notes = new List<Note>();
beat.voice = voice;
beat.effect = new BeatEffect();
beat.effect.mixTableChange = new MixTableChange();
//Beat Duration
beat.duration = new Duration();
var rhythmRef = int.Parse(nBeat.getSubnodeByName("Rhythm", true).propertyValues[0]);
beat.duration.value = rhythms[rhythmRef].noteValue;
beat.duration.isDotted = rhythms[rhythmRef].augmentationDots == 1;
beat.duration.isDoubleDotted = rhythms[rhythmRef].augmentationDots == 2;
beat.duration.tuplet = new Tuplet();
beat.duration.tuplet = rhythms[rhythmRef].primaryTuplet;
//Check if should add tempo mark
if (currentTrack == 0) {
lengthPassed += flipDuration(beat.duration);
foreach (GP6Tempo tempo in tempos)
{
if (tempo.bar == currentMeasure && tempo.transferred == false)
{
if ((float)lengthPassed / totalLength > tempo.position)
{
//Place tempo value
float myTempo = tempo.tempo;
if (tempo.tempoType == 1) myTempo /= 2.0f;
if (tempo.tempoType == 3) myTempo *= 1.5f;
if (tempo.tempoType == 4) myTempo *= 2.0f;
if (tempo.tempoType == 5) myTempo *= 3.0f;
beat.effect.mixTableChange.tempo = new MixTableItem((int)myTempo, 0, true);
tempo.transferred = true;
}
}
}
}
if (nNotes == null) //No notes
{
beat.status = BeatStatus.rest;
return beat;
}
string[] notes = nNotes.content.Split(' ');
Node nChord = nBeat.getSubnodeByName("Chord");
if (nChord != null)
{
beat.effect.chord = new Chord(0);
foreach (GP6Chord chord in chords)
{
if (chord.forTrack == beat.voice.measure.track.number && chord.id == int.Parse(nChord.content))
{
beat.effect.chord.name = chord.name;
}
}
//Here later can go further infos..
}
int velocity = Velocities.forte;
Node nDynamic = nBeat.getSubnodeByName("Dynamic");
if (nDynamic != null)
{
string dynamicSymbol = nDynamic.content;
string[] GP6symbols = {"PPP","PP","P", "MP", "MF","F","FF","FFF"};
int[] velocities = { Velocities.pianoPianissimo, Velocities.pianissimo, Velocities.piano,
Velocities.mezzoPiano,Velocities.mezzoForte, Velocities.forte, Velocities.fortissimo,
Velocities.forteFortissimo};
for (int x=0;x < GP6symbols.Length; x++)
{
if (GP6symbols[x].Equals(dynamicSymbol)) { velocity = velocities[x]; break; }
}
}
beat.effect.fadeIn = nBeat.getSubnodeByName("Fadding", true) != null && nBeat.getSubnodeByName("Fadding", true).content.Equals("FadeIn");
beat.effect.fadeOut = nBeat.getSubnodeByName("Fadding", true) != null && nBeat.getSubnodeByName("Fadding", true).content.Equals("FadeOut");
beat.effect.volumeSwell = nBeat.getSubnodeByName("Fadding", true) != null && nBeat.getSubnodeByName("Fadding", true).content.Equals("VolumeSwell");
if (nBeat.getSubnodeByName("FreeText",true) != null)
{
beat.text = new BeatText(nBeat.getSubnodeByName("FreeText", true).content);
}
bool searchArpeggioParams = false;
Node nArpeggio = nBeat.getSubnodeByName("Arpeggio");
if (nArpeggio != null)
{
string direction = nArpeggio.content;
BeatStrokeDirection bsd = (direction.Equals("Up")) ? BeatStrokeDirection.up : BeatStrokeDirection.down;
beat.effect.stroke = new BeatStroke();
beat.effect.stroke.direction = bsd;
searchArpeggioParams = true;
}
bool searchBrushParams = false;
Node nProperties = nBeat.getSubnodeByName("Properties");
if (nProperties != null)
{
//Whammy values in GP6 format: (GP7 below in subnode "Whammy")
float whammyBarOriginValue = 0.0f;
float whammyBarMiddleValue = 0.0f;
float whammyBarDestinationValue = 0.0f;
float whammyBarOriginOffset = 0.0f;
float whammyBarMiddleOffset1 = -1.0f;
float whammyBarMiddleOffset2 = -1.0f;
float whammyBarDestinationOffset = 100.0f;
bool hasWhammy = false;
foreach (Node nProperty in nProperties.subnodes)
{
if (nProperty.propertyValues[0].Equals("Slapped"))
{
beat.effect.slapEffect = SlapEffect.slapping;
}
else if (nProperty.propertyValues[0].Equals("Popped"))
{
beat.effect.slapEffect = SlapEffect.popping;
}
else if (nProperty.propertyValues[0].Equals("Brush"))
{
string direction = nProperty.subnodes[0].content;
BeatStrokeDirection bsd = (direction.Equals("Up")) ? BeatStrokeDirection.up : BeatStrokeDirection.down;
beat.effect.stroke = new BeatStroke();
beat.effect.stroke.direction = bsd;
searchBrushParams = true; //search in Xproperty
}
else if (nProperty.propertyValues[0].Equals("PickStroke"))
{
string direction = nProperty.subnodes[0].content;
BeatStrokeDirection bsd = (direction.Equals("Up")) ? BeatStrokeDirection.up : BeatStrokeDirection.down;
beat.effect.pickStroke = bsd;
}
else if (nProperty.propertyValues[0].Equals("VibratoWTremBar"))
beat.effect.vibrato = true;
else if (nProperty.propertyValues[0].Equals("WhammyBar")) hasWhammy = true;
else if (nProperty.propertyValues[0].Equals("WhammyBarOriginValue")) whammyBarOriginValue = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("WhammyBarMiddleValue")) whammyBarMiddleValue = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("WhammyBarDestinationValue")) whammyBarDestinationValue = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("WhammyBarMiddleOffset1")) whammyBarMiddleOffset1 = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("WhammyBarMiddleOffset2")) whammyBarMiddleOffset2 = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("WhammyBarOriginOffset")) whammyBarOriginOffset = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("WhammyBarDestinationOffset")) whammyBarDestinationOffset = float.Parse(nProperty.subnodes[0].content);
}
if (hasWhammy)
{
if (whammyBarMiddleOffset1 == -1.0f)
{
whammyBarMiddleOffset1 = whammyBarOriginOffset + (whammyBarDestinationOffset - whammyBarOriginOffset) / 2.0f;
whammyBarMiddleValue = whammyBarOriginValue + (whammyBarDestinationValue - whammyBarOriginValue) / 2.0f;
}
if (whammyBarMiddleOffset2 == -1.0f) whammyBarMiddleOffset2 = whammyBarMiddleOffset1;
beat.effect.tremoloBar = new BendEffect();
beat.effect.tremoloBar.type = BendType.none; //Not defined in GP6
beat.effect.tremoloBar.points = new List<BendPoint>();
beat.effect.tremoloBar.points.Add(new BendPoint(0.0f, whammyBarOriginValue));
beat.effect.tremoloBar.points.Add(new BendPoint(whammyBarOriginOffset, whammyBarOriginValue));
//Peak or Valley
if ((whammyBarMiddleValue - whammyBarOriginValue) * (whammyBarDestinationValue - whammyBarMiddleValue) < 0)
{
beat.effect.tremoloBar.points.Add(new BendPoint(whammyBarMiddleOffset1, whammyBarMiddleValue));
beat.effect.tremoloBar.points.Add(new BendPoint(whammyBarMiddleOffset2, whammyBarMiddleValue));
}
beat.effect.tremoloBar.points.Add(new BendPoint(whammyBarDestinationOffset, whammyBarDestinationValue));
beat.effect.tremoloBar.points.Add(new BendPoint(100.0f, whammyBarDestinationValue));
}
}
Node nWhammy = nBeat.getSubnodeByName("Whammy", true);
if (nWhammy != null)
{
beat.effect.tremoloBar = new BendEffect();
beat.effect.tremoloBar.type = BendType.none; //Not defined in GP6
beat.effect.tremoloBar.points = new List<BendPoint>();
float originValue = float.Parse(nWhammy.propertyValues[0]);
float middleValue = float.Parse(nWhammy.propertyValues[1]);
float destinationValue = float.Parse(nWhammy.propertyValues[2]);
float originOffset = float.Parse(nWhammy.propertyValues[3]);
float middleOffset1 = float.Parse(nWhammy.propertyValues[4]);
float middleOffset2 = float.Parse(nWhammy.propertyValues[5]);
float destinationOffset = float.Parse(nWhammy.propertyValues[6]);
beat.effect.tremoloBar.points.Add(new BendPoint(0.0f,originValue));
beat.effect.tremoloBar.points.Add(new BendPoint(originOffset, originValue));
//Peak or Valley
if ((middleValue - originValue) * (destinationValue - middleValue) < 0)
{
beat.effect.tremoloBar.points.Add(new BendPoint(middleOffset1, middleValue));
beat.effect.tremoloBar.points.Add(new BendPoint(middleOffset2, middleValue));
}
beat.effect.tremoloBar.points.Add(new BendPoint(destinationOffset, destinationValue));
beat.effect.tremoloBar.points.Add(new BendPoint(100.0f, destinationValue));
}
Node nXProperty = nBeat.getSubnodeByName("XProperties");
if (nXProperty != null)
{
if (searchBrushParams)
{
int duration = int.Parse(nXProperty.getSubnodeByProperty("id","687935489").subnodes[0].content);
float startsOnTime = float.Parse(nXProperty.getSubnodeByProperty("id", "687935490").subnodes[0].content);
beat.effect.stroke.setByGP6Standard(duration);
beat.effect.stroke.startTime = startsOnTime;
}
if (searchArpeggioParams)
{
int duration = int.Parse(nXProperty.getSubnodeByProperty("id", "687931393").subnodes[0].content);
float startsOnTime = float.Parse(nXProperty.getSubnodeByProperty("id", "687931394").subnodes[0].content);
beat.effect.stroke.setByGP6Standard(duration);
beat.effect.stroke.startTime = startsOnTime;
}
}
if (nBeat.getSubnodeByName("Wah") != null)
{
beat.effect.mixTableChange.wah = new WahEffect();
beat.effect.mixTableChange.wah.state = (nBeat.getSubnodeByName("Wah").content.Equals("Open")) ? WahState.opened : WahState.closed;
}
GraceEffect graceEffect = null; //Stay null if there is none
Node nGraceEffect = nBeat.getSubnodeByName("GraceNotes");
if (nGraceEffect != null)
{
graceEffect = new GraceEffect();
bool beforeBeat = nGraceEffect.content.Equals("BeforeBeat");
graceEffect.isOnBeat = !beforeBeat;
//All other infos will be filled in by the note
}
Node nTremolo = nBeat.getSubnodeByName("Tremolo");
string tremolo = "";
if (nTremolo != null)
{
tremolo = nTremolo.content;
}
beat.notes = new List<Note>();
foreach (string note in notes)
{
//Give each Note a GraceEffect obj & Velocities val
//velocity;
bool tapping;
beat.notes.Add(transferNote(node, int.Parse(note), beat,velocity, graceEffect, tremolo, out tapping));
if (tapping) beat.effect.slapEffect = SlapEffect.tapping;
}
return beat;
}
public static Note transferNote(Node node, int index, Beat beat, int velocity, GraceEffect graceEffect, string tremolo, out bool tapping)
{
tapping = false;
Note note = new Note();
Node nNote = node.getSubnodeByName("Notes", true).subnodes[index];
note.beat = beat;
note.effect = new NoteEffect();
note.type = NoteType.normal;
//Properties
Node nProperties = nNote.getSubnodeByName("Properties", true);
if (nProperties != null)
{
float harmonicFret = -1;
string harmonicType = "";
float bendDestOff = 100.0f;
float bendDestVal = 0.0f;
float bendMidOff1 = -1.0f;
float bendMidOff2 = -1.0f;
float bendMidVal = 0.0f;
float bendOrigVal = 0.0f;
float bendOrigOff = 0.0f;
int element = -1; //GP6-style drums
int variation = 0;
BendEffect bendEffect = new BendEffect();
bool hasBendEffect = false;
foreach (Node nProperty in nProperties.subnodes)
{
if (nProperty.propertyValues[0].Equals("Element"))
{
element = int.Parse(nProperty.subnodes[0].content);
}
if (nProperty.propertyValues[0].Equals("Variation"))
{
variation = int.Parse(nProperty.subnodes[0].content);
}
if (nProperty.propertyValues[0].Equals("Fret"))
{
note.value = int.Parse(nProperty.subnodes[0].content);
}
else if (nProperty.propertyValues[0].Equals("String"))
{
note.str = int.Parse(nProperty.subnodes[0].content)+1;
}
else if (nProperty.propertyValues[0].Equals("PalmMuted"))
{
note.effect.palmMute = true;
}
else if (nProperty.propertyValues[0].Equals("Muted"))
{
note.type = NoteType.dead;
}
else if (nProperty.propertyValues[0].Equals("HarmonicFret"))
{
harmonicFret = float.Parse(nProperty.subnodes[0].content);
}
else if (nProperty.propertyValues[0].Equals("HarmonicType"))
{
harmonicType = nProperty.subnodes[0].content;
}
else if (nProperty.propertyValues[0].Equals("Bended"))
{
hasBendEffect = true;
}
else if (nProperty.propertyValues[0].Equals("BendDestinationOffset")) bendDestOff = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("BendDestinationValue")) bendDestVal = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("BendMiddleOffset1")) bendMidOff1 = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("BendMiddleOffset2")) bendMidOff2 = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("BendMiddleValue")) bendMidVal = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("BendOriginValue")) bendOrigVal = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("BendOriginOffset")) bendOrigOff = float.Parse(nProperty.subnodes[0].content);
else if (nProperty.propertyValues[0].Equals("Slide"))
{
note.effect.slides = new List<SlideType>();
int flags = int.Parse(nProperty.subnodes[0].content);
if (flags % 2 == 1) note.effect.slides.Add(SlideType.shiftSlideTo);
if ((flags >> 1) % 2 == 1) note.effect.slides.Add(SlideType.legatoSlideTo);
if ((flags >> 2) % 2 == 1) note.effect.slides.Add(SlideType.outDownwards);
if ((flags >> 3) % 2 == 1) note.effect.slides.Add(SlideType.outUpwards);
if ((flags >> 4) % 2 == 1) note.effect.slides.Add(SlideType.intoFromBelow);
if ((flags >> 5) % 2 == 1) note.effect.slides.Add(SlideType.intoFromAbove);
if ((flags >> 6) % 2 == 1) note.effect.slides.Add(SlideType.pickScrapeOutDownwards);
if ((flags >> 7) % 2 == 1) note.effect.slides.Add(SlideType.pickScrapeOutUpwards);
}
else if (nProperty.propertyValues[0].Equals("LeftHandTapped"))
{
note.effect.hammer = true;
}
else if (nProperty.propertyValues[0].Equals("HopoDestination"))
{
note.effect.hammer = true;
}
else if (nProperty.propertyValues[0].Equals("Tapped"))
{
tapping = true;
}
}
if (hasBendEffect)
{
if (bendMidOff1 == -1.0f)
{
bendMidOff1 = bendOrigOff + (bendDestOff - bendOrigOff) / 2.0f;
bendMidVal = bendOrigVal + (bendDestVal - bendOrigVal) / 2.0f;
}
if (bendMidOff2 == -1.0f) bendMidOff2 = bendMidOff1;
bendEffect.points.Add(new BendPoint(0.0f, bendOrigVal));
bendEffect.points.Add(new BendPoint(bendOrigOff, bendOrigVal));
//Peak or Valley
if ((bendMidVal - bendOrigVal) * (bendDestVal - bendMidVal) < 0)
{
bendEffect.points.Add(new BendPoint(bendMidOff1, bendMidVal));
bendEffect.points.Add(new BendPoint(bendMidOff2, bendMidVal));
}
bendEffect.points.Add(new BendPoint(bendDestOff, bendDestVal));
bendEffect.points.Add(new BendPoint(100.0f, bendDestVal));
note.effect.bend = bendEffect;
}
if (harmonicFret != -1)
{
if (harmonicType.Equals("Natural") || harmonicType.Equals(""))
{
note.effect.harmonic = new NaturalHarmonic(); //Ignore the complicated GP3-5 settings
} else if (harmonicType.Equals("Artificial")) //There should be during playback a function that reads only fret and type and creates the harmonic + for GP3-5 files that transfers the old format
note.effect.harmonic = new ArtificialHarmonic();
else if (harmonicType.Equals("Pinch")) note.effect.harmonic = new PinchHarmonic();
else if (harmonicType.Equals("Tap")) note.effect.harmonic = new TappedHarmonic();
else if (harmonicType.Equals("Semi")) note.effect.harmonic = new SemiHarmonic();
else if (harmonicType.Equals("Feedback")) note.effect.harmonic = new FeedbackHarmonic();
note.effect.harmonic.fret = harmonicFret;
}
if (element != -1) //GP6-style Drumset
{
int midiValue = getGP6DrumValue(element, variation);
note.value = midiValue;
note.str = 1;
}
}
//XProperties (are there more?)
int trillLength = 0;
Node nXProperties = nNote.getSubnodeByName("XProperties", true);
if (nXProperties != null)
{
Node nTrillLength = nXProperties.getSubnodeByProperty("id", "688062467");
if (nTrillLength != null)
{
trillLength = int.Parse(nTrillLength.subnodes[0].content);
}
}
//Other Subnodes
Node nTrill = nNote.getSubnodeByName("Trill");
if (nTrill != null)
{
int secondNote = int.Parse(nTrill.content);
note.effect.trill = new TrillEffect();
note.effect.trill.fret = secondNote;
note.effect.trill.duration = new Duration(trillLength);
}
Node nVibrato = nNote.getSubnodeByName("Vibrato");
if (nVibrato != null)
{
note.effect.vibrato = true;
}
if (nNote.getSubnodeByName("LetRing") != null) note.effect.letRing = true;
Node nAntiAccent = nNote.getSubnodeByName("AntiAccent");
if (nAntiAccent != null)
{
note.effect.ghostNote = true;
}
Node nAccent = nNote.getSubnodeByName("Accent");
if (nAccent != null)
{
int val = int.Parse(nAccent.content);
note.effect.accentuatedNote = val == 4;
note.effect.heavyAccentuatedNote = val == 8;
note.effect.staccato = val == 1;
}
// Node nAccidental = nNote.getSubnodeByName("Accidental"); Doesn't matter for this app.
if (nNote.getSubnodeByName("Tie") != null && nNote.getSubnodeByName("Tie").propertyValues[1].Equals("true"))
note.type = NoteType.tie;
if (!tremolo.Equals("")) {
note.effect.tremoloPicking = new TremoloPickingEffect();
//1/2 = 8th, 1/4 = 16ths, 1/8 = 32nds
note.effect.tremoloPicking.duration = new Duration();
if (tremolo.Equals("1/2")) note.effect.tremoloPicking.duration.value = 8;
if (tremolo.Equals("1/4")) note.effect.tremoloPicking.duration.value = 16;
if (tremolo.Equals("1/8")) note.effect.tremoloPicking.duration.value = 32;
}
note.effect.grace = graceEffect;
note.velocity = velocity;
return note;
}
public static int getGP6DrumValue(int element, int variation)
{
int val = element * 10 + variation;
if (val == 0) return 35;
if (val == 10) return 38;
if (val == 11) return 91;
if (val == 12) return 37;
if (val == 20) return 99;
if (val == 30) return 56;
if (val == 40) return 102;
if (val == 50) return 43;
if (val == 60) return 45;
if (val == 70) return 47;
if (val == 80) return 48;
if (val == 90) return 50;
if (val == 100) return 42;
if (val == 101) return 92;
if (val == 102) return 46;
if (val == 110) return 44;
if (val == 120) return 57;
if (val == 130) return 49;
if (val == 140) return 55;
if (val == 150) return 51;
if (val == 151) return 93;
if (val == 152) return 53;
if (val == 160) return 52;
return 0;
}
public static List<GP6Chord> readChords(Node nTracks)
{
var ret_val = new List<GP6Chord>();
int tcnt = 0;
foreach (Node nTrack in nTracks.subnodes)
{
Node nProperties = nTrack.getSubnodeByName("Properties");
if (nProperties != null)
{
Node nDiagrams = nProperties.getSubnodeByProperty("name", "DiagramCollection");
if (nDiagrams != null)
{
Node nItems = nDiagrams.getSubnodeByName("Items");
int chordcnt = 0;
foreach (Node Item in nItems.subnodes)
{
var chord = new GP6Chord();
chord.id = chordcnt;
chord.forTrack = tcnt;
chord.name = Item.propertyValues[1];
//Here I can later parse the chord picture
ret_val.Add(chord);
chordcnt++;
}
}
}
tcnt++;
}
return ret_val;
}
public static List<GP6Rhythm> readRhythms(Node nRhythms)
{
var ret_val = new List<GP6Rhythm>();
int cnt = 0;
foreach (Node nRhythm in nRhythms.subnodes)
{
string[] durations = { "Whole", "Half", "Quarter", "Eighth", "16th", "32nd" };
string noteValue = nRhythm.getSubnodeByName("NoteValue", true).content;
int note = 4;
for (int x = 0; x < durations.Length; x++)
{
if (noteValue.Equals(durations[x])) { note = (int)Math.Pow(2, x); }
}
Node nAug = nRhythm.getSubnodeByName("AugmentationDot", true);
int augCnt = 0;
if (nAug != null)
{
augCnt = int.Parse(nAug.propertyValues[0]);
}
Node nTuplet = nRhythm.getSubnodeByName("PrimaryTuplet");
int n = 1, m = 1;
if (nTuplet != null)
{
n = int.Parse(nTuplet.propertyValues[0]);
m = int.Parse(nTuplet.propertyValues[1]);
}
ret_val.Add(new GP6Rhythm(cnt++,note, augCnt,n,m));
}
return ret_val;
}
public static List<Track> transferTracks(Node nTracks, GP5File song)
{
List<Track> ret_val = new List<Track>();
int cnt = 0;
foreach (Node nTrack in nTracks.subnodes)
{
Track _track = new Track(song, cnt++);
_track.name = nTrack.getSubnodeByName("Name").content;
string[] colors = nTrack.getSubnodeByName("Color").content.Split(' ');
_track.color = new myColor(int.Parse(colors[0]), int.Parse(colors[1]), int.Parse(colors[2]));
_track.channel = new MidiChannel();
string[] param = nTrack.getSubnodeByName("RSE").getSubnodeByName("ChannelStrip").getSubnodeByName("Parameters").content.Split(' ');
_track.channel.bank = 0;
_track.channel.balance = (int)(100 * float.Parse(param[11]));
_track.channel.volume = (int)(100 * float.Parse(param[12]));
Node nMidi = nTrack.getSubnodeByName("GeneralMidi", true);
if (nMidi != null) //GP6
{
_track.channel.instrument = int.Parse(nMidi.getSubnodeByName("Program").content);
_track.channel.channel = int.Parse(nMidi.getSubnodeByName("PrimaryChannel").content);
_track.channel.effectChannel = int.Parse(nMidi.getSubnodeByName("SecondaryChannel").content);
_track.port = int.Parse(nMidi.getSubnodeByName("Port").content);
} else
{ //GP7
_track.channel.instrument = int.Parse(nTrack.getSubnodeByName("Sounds").subnodes[0].getSubnodeByName("MIDI").getSubnodeByName("Program").content);
_track.channel.channel = int.Parse(nTrack.getSubnodeByName("MidiConnection").getSubnodeByName("PrimaryChannel").content);
_track.channel.effectChannel = int.Parse(nTrack.getSubnodeByName("MidiConnection").getSubnodeByName("SecondaryChannel").content);
_track.port = int.Parse(nTrack.getSubnodeByName("MidiConnection").getSubnodeByName("Port").content);
}
_track.strings = new List<GuitarString>();
Node nProperties = nTrack.getSubnodeByName("Properties");
if (nProperties != null)
{
Node nTuning = nProperties.getSubnodeByProperty("name", "Tuning");
if (nTuning != null)
{
string[] tuning = nTuning.subnodes[0].content.Split(' ');
int gcnt = 0;
foreach (string str in tuning)
{
_track.strings.Add(new GuitarString(gcnt++, int.Parse(str)));
}
}
}
if (nProperties != null) {
Node nCapoFret = nProperties.getSubnodeByProperty("name", "CapoFret");
Node nFretCount = nProperties.getSubnodeByProperty("name", "FretCount");
if (nCapoFret != null) _track.offset = int.Parse(nCapoFret.subnodes[0].content);
_track.fretCount = 24;
if (nFretCount != null) _track.fretCount = int.Parse(nFretCount.subnodes[0].content); //Not saved anymore
Node nPropertyName = nProperties.getSubnodeByName("Name", true);
if (nPropertyName != null)
{
if (nPropertyName.subnodes.Count > 0) { _track.tuningName = nPropertyName.subnodes[0].content; }
else { _track.tuningName = nPropertyName.content; }
}
}
_track.isPercussionTrack = _track.channel.channel == 9;
_track.settings = new TrackSettings();
Node nPlaybackState = nTrack.getSubnodeByName("PlaybackState");
if (nPlaybackState != null)
{
if (nPlaybackState.content.Equals("Solo")) _track.isSolo = true;
if (nPlaybackState.content.Equals("Mute")) _track.isMute = true;
}
//Do not matter for me:
//_track.indicateTuning, track.settings
ret_val.Add(_track);
}
return ret_val;
}
public static List<MeasureHeader> transferMeasureHeaders(Node nMasterBars, GP5File song)
{
var ret_val = new List<MeasureHeader>();
int cnt = 0;
foreach (Node nMasterBar in nMasterBars.subnodes)
{
var _measureHeader = new MeasureHeader();
int accidentals = int.Parse(nMasterBar.getSubnodeByName("Key", true).subnodes[0].content);
int mode = (nMasterBar.getSubnodeByName("Key", true).subnodes[1].content.Equals("Major")) ? 0: 1;
_measureHeader.keySignature = (KeySignature)(accidentals * 10 + ((accidentals < 0) ? -mode : mode));
_measureHeader.hasDoubleBar = nMasterBar.getSubnodeByName("DoubleBar", true) != null;
_measureHeader.direction = transferDirections(nMasterBar.getSubnodeByName("Directions",true));
_measureHeader.fromDirection = transferFromDirections(nMasterBar.getSubnodeByName("Directions", true));
_measureHeader.isRepeatOpen = nMasterBar.getSubnodeByName("Repeat", true) != null && nMasterBar.getSubnodeByName("Repeat", true).propertyValues[0].Equals("true");
_measureHeader.repeatClose = 0;
if (nMasterBar.getSubnodeByName("Repeat", true) != null && nMasterBar.getSubnodeByName("Repeat", true).propertyValues[1].Equals("true"))
_measureHeader.repeatClose = int.Parse(nMasterBar.getSubnodeByName("Repeat", true).propertyValues[2]);
if (nMasterBar.getSubnodeByName("AlternateEndings", true) != null) {
var _aes = nMasterBar.getSubnodeByName("AlternateEndings", true).content.Split(' ');
foreach (string _ in _aes)
{
_measureHeader.repeatAlternatives.Add(int.Parse(_));
}
}
_measureHeader.timeSignature = new TimeSignature(); //Time
string[] timeSig = nMasterBar.getSubnodeByName("Time", true).content.Split('/');
_measureHeader.timeSignature.numerator = int.Parse(timeSig[0]);
_measureHeader.timeSignature.denominator = new Duration();
_measureHeader.timeSignature.denominator.value = int.Parse(timeSig[1]);
_measureHeader.tripletFeel = TripletFeel.none;
if (nMasterBar.getSubnodeByName("TripletFeel",true) != null)
{
string feel = nMasterBar.getSubnodeByName("TripletFeel", true).content;
if (feel.Equals("Triplet8th")) _measureHeader.tripletFeel = TripletFeel.eigth;
if (feel.Equals("Triplet16th")) _measureHeader.tripletFeel = TripletFeel.sixteenth;
if (feel.Equals("Dotted8th")) _measureHeader.tripletFeel = TripletFeel.dotted8th;
if (feel.Equals("Dotted16th")) _measureHeader.tripletFeel = TripletFeel.dotted16th;
if (feel.Equals("Scottish8th")) _measureHeader.tripletFeel = TripletFeel.scottish8th;
if (feel.Equals("Scottish16th")) _measureHeader.tripletFeel = TripletFeel.scottish16th;
}
_measureHeader.song = song;
_measureHeader.number = cnt++;
//Do I really need these:
//_measureHeader.marker ? repeatGroup ?
//_measureHeader.start ? realStart ?
//_measureHeader.tempo - useless, as the real tempo is saved as MixTableChange on the BeatEffect
ret_val.Add(_measureHeader);
}
return ret_val;
}
public static List<string> transferDirections(Node nDirections)
{
List<string> ret_val = new List<string>();
if (nDirections == null) return ret_val;
foreach (Node nElement in nDirections.subnodes)
{
if (nElement.name.Equals("Target")) ret_val.Add(nElement.content);
}
return ret_val;
}
public static List<string> transferFromDirections(Node nDirections)
{
List<string> ret_val = new List<string>();
if (nDirections == null) return ret_val;
foreach (Node nElement in nDirections.subnodes)
{
if (nElement.name.Equals("Jump")) ret_val.Add(nElement.content);
}
return ret_val;
}
public static List<Lyrics> transferLyrics(Node nTracks)
{
List<Lyrics> ret_val = new List<Lyrics>();
if (nTracks == null) return ret_val;
foreach (Node nTrack in nTracks.subnodes)
{
Node nLyrics = nTrack.getSubnodeByName("Lyrics");
Lyrics lyrics = new Lyrics();
int cnt = 0;
foreach (Node nLine in nLyrics.subnodes)
{
var _line = new LyricLine();
_line.lyrics = nLine.subnodes[0].content;
_line.startingMeasure = int.Parse(nLine.subnodes[1].content);
lyrics.lines[cnt++] = _line;
}
ret_val.Add(lyrics);
}
return ret_val;
}
public static Node ParseGP6(string xml, int start)
{
//Remove '<' chars inside CDATA tags
bool skipMode = false;
for (int x = 0; x < xml.Length-3; x++)
{
string sub = xml.Substring(x, 3);
if (sub.Equals("<!-")) { xml = xml.Substring(0, x) + '{' + xml.Substring(x + 1); continue; }
if (sub.Equals("<![")) { skipMode = true; continue; }
if (sub.Equals("]]>")) skipMode = false;
if (skipMode && xml[x] == '<') xml = xml.Substring(0,x)+'{'+xml.Substring(x+1);