-
Notifications
You must be signed in to change notification settings - Fork 21
/
GP4File.cs
executable file
·1302 lines (1105 loc) · 41.3 KB
/
GP4File.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;
public class GP4File : GPFile
{
//Members of GPFile
/*
public string version;
public int[] versionTuple;
public string title = "";
public string subtitle = "";
public string interpret = "";
public string album = "";
public string author = "";
public string copyright = "";
public string tab_author = "";
public string instructional = "";
public Lyrics lyrics;
public int tempo;
public List<Track> tracks = new List<Track>();
public List<MeasureHeader> measureHeaders = new List<MeasureHeader>();
public TripletFeel _tripletFeel;
*/
public string[] notice;
public KeySignature key;
public MidiChannel[] channels;
public int measureCount;
public int trackCount;
public RepeatGroup _currentRepeatGroup = new RepeatGroup();
public GP4File(byte[] _data)
{
GPBase.pointer = 0;
GPBase.data = _data;
}
public void addMeasureHeader(MeasureHeader header)
{
header.song = this;
measureHeaders.Add(header);
if (header.isRepeatOpen || (header.repeatAlternatives.Count > 0 && _currentRepeatGroup.isClosed && header.repeatAlternatives[0] <= 0))
{
_currentRepeatGroup = new RepeatGroup();
}
_currentRepeatGroup.addMeasureHeader(header);
}
public void addTrack(Track track)
{
track.song = this;
tracks.Add(track);
}
public override void readSong()
{
//HEADERS
//VERSION
version = readVersion();
versionTuple = readVersionTuple();
clipboard = readClipboard();
//INFORMATION ABOUT THE PIECE
readInfo();
_tripletFeel = GPBase.readBool()[0] ? TripletFeel.eigth : TripletFeel.none;
readLyrics();
tempo = GPBase.readInt()[0];
key = (KeySignature)(GPBase.readInt()[0] * 10); //key + 0
GPBase.readSignedByte(); //octave
readMidiChannels();
measureCount = GPBase.readInt()[0];
trackCount = GPBase.readInt()[0];
readMeasureHeaders(measureCount);
readTracks(trackCount, channels);
readMeasures();
}
public Clipboard readClipboard()
{
if (!isClipboard()) return null;
var clipboard = new Clipboard();
clipboard.startMeasure = GPBase.readInt()[0];
clipboard.stopMeasure = GPBase.readInt()[0];
clipboard.startTrack = GPBase.readInt()[0];
clipboard.stopTrack = GPBase.readInt()[0];
return clipboard;
}
private bool isClipboard()
{
return version.StartsWith("CLIPBOARD");
}
private string readVersion()
{
var version = GPBase.readByteSizeString(30);
return version;
}
private int[] readVersionTuple() //bl0.12
{
if (version.Equals("")) return new int[] { 4, 0 };
var tuple = version.Substring(version.Length - 4).Split('.');
return new int[] { Convert.ToInt32(tuple[0]), Convert.ToInt32(tuple[1]) };
}
private void readMeasures()
{
/*Read measures.
Measures are written in the following order:
- measure 1/track 1
- measure 1/track 2
- ...
- measure 1/track m
- measure 2/track 1
- measure 2/track 2
- ...
- measure 2/track m
- ...
- measure n/track 1
- measure n/track 2
- ...
- measure n/track m
*/
var tempo = new Tempo(this.tempo);
var start = Duration.quarterTime;
foreach (MeasureHeader header in measureHeaders)
{
header.start = start;
foreach (Track track in tracks)
{
var measure = new Measure(track, header);
tempo = header.tempo;
track.measures.Add(measure);
readMeasure(measure);
}
header.tempo = tempo;
start += header.length();
}
}
private void readMeasure(Measure measure)
{
/*The measure is written as number of beats followed by sequence
of beats.*/
var start = measure.start();
var voice = measure.voices[0];
readVoice(start, voice);
}
private void readVoice(int start, Voice voice)
{
//TODO: The pointer is 13 bytes too early here (when reading for measure 0xa of track 0x2, beats should return 1, not 898989)
var beats = GPBase.readInt()[0];
for (int beat = 0; beat < beats; beat++)
{
start += readBeat(start, voice);
}
}
private int readBeat(int start, Voice voice)
{
/* The first byte is the beat flags. It lists the data present in
the current beat:
- *0x01*: dotted notes
- *0x02*: presence of a chord diagram
- *0x04*: presence of a text
- *0x08*: presence of effects
- *0x10*: presence of a mix table change event
- *0x20*: the beat is a n-tuplet
- *0x40*: status: True if the beat is empty of if it is a rest
- *0x80*: *blank*
Flags are followed by:
- Status: :ref:`byte`. If flag at *0x40* is true, read one byte.
If value of the byte is *0x00* then beat is empty, if value is
*0x02* then the beat is rest.
- Beat duration: :ref:`byte`. See :meth:`readDuration`.
- Chord diagram. See :meth:`readChord`.
- Text. See :meth:`readText`.
- Beat effects. See :meth:`readBeatEffects`.
- Mix table change effect. See :meth:`readMixTableChange`.*/
var flags = GPBase.readByte()[0];
var beat = getBeat(voice, start);
if ((flags & 0x40) != 0)
{
beat.status = (BeatStatus)((int)GPBase.readByte()[0]);
}
else
{
beat.status = BeatStatus.normal;
}
var duration = readDuration(flags);
var effect = new NoteEffect();
if ((flags & 0x02) != 0) beat.effect.chord = readChord(voice.measure.track.strings.Count);
if ((flags & 0x04) != 0) beat.text = readText();
if ((flags & 0x08) != 0) beat.effect = readBeatEffects(effect);
if ((flags & 0x10) != 0)
{
var mixTableChange = readMixTableChange(voice.measure);
beat.effect.mixTableChange = mixTableChange;
}
readNotes(voice.measure.track, beat, duration, effect);
return (!(beat.status == BeatStatus.empty)) ? duration.time() : 0;
}
private void readNotes(Track track, Beat beat, Duration duration, NoteEffect effect)
{
/* First byte lists played strings:
- *0x01*: 7th string
- *0x02*: 6th string
- *0x04*: 5th string
- *0x08*: 4th string
- *0x10*: 3th string
- *0x20*: 2th string
- *0x40*: 1th string
- *0x80*: *blank**/
var stringFlags = GPBase.readByte()[0];
foreach (var str in track.strings)
{
if ((stringFlags & 1 << (7 - str.number)) != 0)
{
var note = new Note(beat);
beat.notes.Add(note);
readNote(note, str, track);
}
beat.duration = duration;
}
}
private void readNote(Note note, GuitarString guitarString, Track track)
{
/*The first byte is note flags:
- *0x01*: time-independent duration
- *0x02*: heavy accentuated note
- *0x04*: ghost note
- *0x08*: presence of note effects
- *0x10*: dynamics
- *0x20*: fret
- *0x40*: accentuated note
- *0x80*: right hand or left hand fingering
Flags are followed by:
- Note type: :ref:`byte`. Note is normal if values is 1, tied if
value is 2, dead if value is 3.
- Time-independent duration: 2 :ref:`SignedBytes <signed-byte>`.
Correspond to duration and tuplet. See :meth:`readDuration`
for reference.
- Note dynamics: :ref:`signed-byte`. See :meth:`unpackVelocity`.
- Fret number: :ref:`signed-byte`. If flag at *0x20* is set then
read fret number.
- Fingering: 2 :ref:`SignedBytes <signed-byte>`. See
:class:`guitarpro.models.Fingering`.
- Note effects. See :meth:`readNoteEffects`.*/
var flags = GPBase.readByte()[0];
note.str = guitarString.number;
note.effect.ghostNote = ((flags & 0x04) != 0);
if ((flags & 0x20) != 0) note.type = (NoteType)(GPBase.readByte()[0]);
if ((flags & 0x01) != 0)
{
note.duration = GPBase.readSignedByte()[0];
note.tuplet = GPBase.readSignedByte()[0];
}
if ((flags & 0x10) != 0)
{
var dyn = GPBase.readSignedByte()[0];
note.velocity = unpackVelocity(dyn);
}
if ((flags & 0x20) != 0)
{
int value;
var fret = GPBase.readSignedByte()[0];
if (note.type == NoteType.tie) { value = getTiedNoteValue(guitarString.number, track); }
else { value = fret; }
note.value = Math.Max(0, Math.Min(99, value));
}
if ((flags & 0x80) != 0)
{
note.effect.leftHandFinger = (Fingering)GPBase.readSignedByte()[0];
note.effect.rightHandFinger = (Fingering)GPBase.readSignedByte()[0];
}
if ((flags & 0x08) != 0)
{
note.effect = readNoteEffects(note);
if (note.effect.isHarmonic() && note.effect.harmonic is TappedHarmonic)
{
note.effect.harmonic.fret = note.value + 12;
}
}
}
private NoteEffect readNoteEffects(Note note)
{
/*First byte is note effects flags:
- *0x01*: bend presence
- *0x02*: hammer-on/pull-off
- *0x04*: slide
- *0x08*: let-ring
- *0x10*: grace note presence
Flags are followed by:
- Bend. See :meth:`readBend`.
- Grace note. See :meth:`readGrace`.*/
var noteEffect = note.effect;
if (noteEffect == null) noteEffect = new NoteEffect();
var flags1 = GPBase.readSignedByte()[0];
var flags2 = GPBase.readSignedByte()[0];
noteEffect.hammer = ((flags1 & 0x02) != 0);
noteEffect.letRing = ((flags1 & 0x08) != 0);
noteEffect.staccato = ((flags2 & 0x01) != 0);
noteEffect.palmMute = ((flags2 & 0x02) != 0);
noteEffect.vibrato = ((flags2 & 0x40) != 0) || noteEffect.vibrato;
if ((flags1 & 0x01) != 0) noteEffect.bend = readBend();
if ((flags1 & 0x10) != 0) noteEffect.grace = readGrace();
if ((flags2 & 0x04) != 0) noteEffect.tremoloPicking = readTremoloPicking();
if ((flags2 & 0x08) != 0) noteEffect.slides = readSlides();
if ((flags2 & 0x10) != 0) noteEffect.harmonic = readHarmonic(note);
if ((flags2 & 0x20) != 0) noteEffect.trill = readTrill();
return noteEffect;
}
private TremoloPickingEffect readTremoloPicking()
{
var value = GPBase.readSignedByte()[0];
var tp = new TremoloPickingEffect();
tp.duration.value = fromTremoloValue(value);
return tp;
}
private int fromTremoloValue(sbyte value)
{
switch (value)
{
case 1:
return Duration.eigth;
case 2:
return Duration.sixteenth;
case 3:
return Duration.thirtySecond;
}
return 8;
}
private List<SlideType> readSlides()
{
var ret_val = new List<SlideType>();
ret_val.Add((SlideType)GPBase.readSignedByte()[0]);
return ret_val;
}
private HarmonicEffect readHarmonic(Note note)
{
/*Harmonic is encoded in :ref:`signed-byte`. Values correspond to:
- *1*: natural harmonic
- *3*: tapped harmonic
- *4*: pinch harmonic
- *5*: semi-harmonic
- *15*: artificial harmonic on (*n + 5*)th fret
- *17*: artificial harmonic on (*n + 7*)th fret
- *22*: artificial harmonic on (*n + 12*)th fret
*/
var harmonicType = GPBase.readSignedByte()[0];
HarmonicEffect harmonic = null;
switch (harmonicType)
{
case 1:
harmonic = new NaturalHarmonic(); break;
case 3:
harmonic = new TappedHarmonic(); break;
case 4:
harmonic = new PinchHarmonic(); break;
case 5:
harmonic = new SemiHarmonic(); break;
case 15:
var pitch = new PitchClass((note.realValue() + 7) % 12, -1, "", "", 7.0f);
var octave = Octave.ottava;
harmonic = new ArtificialHarmonic(pitch, octave);
break;
case 17:
pitch = new PitchClass(note.realValue(), -1, "", "",12.0f);
octave = Octave.quindicesima;
harmonic = new ArtificialHarmonic(pitch, octave);
break;
case 22:
pitch = new PitchClass(note.realValue(), -1, "", "",5.0f);
octave = Octave.ottava;
harmonic = new ArtificialHarmonic(pitch, octave);
break;
}
return harmonic;
}
private TrillEffect readTrill()
{
var trill = new TrillEffect();
trill.fret = GPBase.readSignedByte()[0];
trill.duration.value = fromTrillPeriod(GPBase.readSignedByte()[0]);
return trill;
}
private int fromTrillPeriod(sbyte period)
{
switch (period)
{
case 1:
return Duration.sixteenth;
case 2:
return Duration.thirtySecond;
case 3:
return Duration.sixtyFourth;
}
return Duration.sixteenth;
}
private GraceEffect readGrace()
{
/*- Fret: :ref:`signed-byte`. Number of fret.
- Dynamic: :ref:`byte`. Dynamic of a grace note, as in
:attr:`guitarpro.models.Note.velocity`.
- Transition: :ref:`byte`. See
:class:`guitarpro.models.GraceEffectTransition`.
- Duration: :ref:`byte`. Values are:
- *1*: Thirty-second note.
- *2*: Twenty-fourth note.
- *3*: Sixteenth note.*/
var grace = new GraceEffect();
grace.fret = GPBase.readSignedByte()[0];
grace.velocity = unpackVelocity(GPBase.readByte()[0]);
grace.duration = 1 << (7 - GPBase.readByte()[0]);
grace.isDead = (grace.fret == -1);
grace.isOnBeat = false;
grace.transition = (GraceEffectTransition)GPBase.readSignedByte()[0];
return grace;
}
private BendEffect readBend()
{
/*Encoded as:
-Bend type: :ref:`signed - byte`. See
:class:`guitarpro.models.BendType`.
- Bend value: :ref:`int`.
- Number of bend points: :ref:`int`.
- List of points.Each point consists of:
* Position: :ref:`int`. Shows where point is set along
*x*-axis.
* Value: :ref:`int`. Shows where point is set along *y*-axis.
* Vibrato: :ref:`bool`. */
var bendEffect = new BendEffect();
bendEffect.type = (BendType)GPBase.readSignedByte()[0];
bendEffect.value = GPBase.readInt()[0];
var pointCount = GPBase.readInt()[0];
for (int x = 0; x < pointCount; x++)
{
var position = (int)Math.Round(GPBase.readInt()[0] * BendEffect.maxPosition / (float)GPBase.bendPosition);
var value = (int)Math.Round(GPBase.readInt()[0] * BendEffect.semitoneLength / (float)GPBase.bendSemitone);
var vibrato = GPBase.readBool()[0];
bendEffect.points.Add(new BendPoint(position, value, vibrato));
}
return bendEffect;
}
private int getTiedNoteValue(int stringIndex, Track track)
{
for (int measure = track.measures.Count - 1; measure >= 0; measure--)
{
for (int voice = track.measures[measure].voices.Count - 1; voice >= 0; voice--)
{
foreach (var beat in track.measures[measure].voices[voice].beats)
{
if (beat.status != BeatStatus.empty)
{
foreach (var note in beat.notes)
{
if (note.str == stringIndex) return note.value;
}
}
}
}
}
return -1;
}
private int unpackVelocity(sbyte dyn)
{
return (Velocities.minVelocity +
Velocities.velocityIncrement * dyn -
Velocities.velocityIncrement);
}
private int unpackVelocity(byte dyn)
{
return (Velocities.minVelocity +
Velocities.velocityIncrement * dyn -
Velocities.velocityIncrement);
}
private MixTableChange readMixTableChange(Measure measure)
{
var tableChange = new MixTableChange();
readMixTableChangeValues(tableChange, measure);
readMixTableChangeDurations(tableChange);
readMixTableChangeFlags(tableChange);
return tableChange;
}
private void readMixTableChangeFlags(MixTableChange tableChange)
{
/* The meaning of flags:
- *0x01*: change volume for all tracks
- *0x02*: change balance for all tracks
- *0x04*: change chorus for all tracks
- *0x08*: change reverb for all tracks
- *0x10*: change phaser for all tracks
- *0x20*: change tremolo for all tracks*/
var flags = GPBase.readSignedByte()[0];
if (tableChange.volume != null) tableChange.volume.allTracks = ((flags & 0x01) != 0);
if (tableChange.balance != null) tableChange.balance.allTracks = ((flags & 0x02) != 0);
if (tableChange.chorus != null) tableChange.chorus.allTracks = ((flags & 0x04) != 0);
if (tableChange.reverb != null) tableChange.reverb.allTracks = ((flags & 0x08) != 0);
if (tableChange.phaser != null) tableChange.phaser.allTracks = ((flags & 0x10) != 0);
if (tableChange.tremolo != null) tableChange.tremolo.allTracks = ((flags & 0x20) != 0);
}
private void readMixTableChangeValues(MixTableChange tableChange, Measure measure)
{
var instrument = GPBase.readSignedByte()[0];
var volume = GPBase.readSignedByte()[0];
var balance = GPBase.readSignedByte()[0];
var chorus = GPBase.readSignedByte()[0];
var reverb = GPBase.readSignedByte()[0];
var phaser = GPBase.readSignedByte()[0];
var tremolo = GPBase.readSignedByte()[0];
var tempo = GPBase.readInt()[0];
if (instrument >= 0)
{
tableChange.instrument = new MixTableItem(instrument);
}
if (volume >= 0)
{
tableChange.volume = new MixTableItem(volume);
}
if (balance >= 0)
{
tableChange.balance = new MixTableItem(balance);
}
if (chorus >= 0)
{
tableChange.chorus = new MixTableItem(chorus);
}
if (reverb >= 0)
{
tableChange.reverb = new MixTableItem(reverb);
}
if (phaser >= 0)
{
tableChange.phaser = new MixTableItem(phaser);
}
if (tremolo >= 0)
{
tableChange.tremolo = new MixTableItem(tremolo);
}
if (tempo >= 0)
{
tableChange.tempo = new MixTableItem(tempo);
measure.tempo().value = tempo;
}
}
private void readMixTableChangeDurations(MixTableChange tableChange)
{
if (tableChange.volume != null) tableChange.volume.duration = GPBase.readSignedByte()[0];
if (tableChange.balance != null) tableChange.balance.duration = GPBase.readSignedByte()[0];
if (tableChange.chorus != null) tableChange.chorus.duration = GPBase.readSignedByte()[0];
if (tableChange.reverb != null) tableChange.reverb.duration = GPBase.readSignedByte()[0];
if (tableChange.phaser != null) tableChange.phaser.duration = GPBase.readSignedByte()[0];
if (tableChange.tremolo != null) tableChange.tremolo.duration = GPBase.readSignedByte()[0];
if (tableChange.tempo != null)
{
tableChange.tempo.duration = GPBase.readSignedByte()[0];
tableChange.hideTempo = false;
}
}
private BeatEffect readBeatEffects(NoteEffect effect)
{
/*
* The first byte is effects flags:
- *0x01*: vibrato
- *0x02*: wide vibrato
- *0x04*: natural harmonic
- *0x08*: artificial harmonic
- *0x10*: fade in
- *0x20*: tremolo bar or slap effect
- *0x40*: beat stroke direction
- *0x80*: *blank*
- Tremolo bar or slap effect: :ref:`byte`. If it's 0 then
tremolo bar should be read (see :meth:`readTremoloBar`). Else
it's tapping and values of the byte map to:
- *1*: tap
- *2*: slap
- *3*: pop
- Beat stroke direction. See :meth:`readBeatStroke`.*/
var beatEffect = new BeatEffect();
var flags1 = GPBase.readSignedByte()[0];
var flags2 = GPBase.readSignedByte()[0];
//effect.vibrato = ((flags1 & 0x01) != 0) || effect.vibrato;
beatEffect.vibrato = ((flags1 & 0x02) != 0) || beatEffect.vibrato;
beatEffect.fadeIn = ((flags1 & 0x10) != 0);
if ((flags1 & 0x20) != 0)
{
var value = GPBase.readSignedByte()[0];
beatEffect.slapEffect = (SlapEffect)value;
}
if ((flags2 & 0x04) != 0) beatEffect.tremoloBar = readTremoloBar();
if ((flags1 & 0x40) != 0) beatEffect.stroke = readBeatStroke();
if ((flags2 & 0x02) != 0)
{
var direction = GPBase.readSignedByte()[0];
beatEffect.pickStroke = (BeatStrokeDirection)direction;
}
return beatEffect;
}
private BeatStroke readBeatStroke()
{
var strokeDown = GPBase.readSignedByte()[0];
var strokeUp = GPBase.readSignedByte()[0];
if (strokeUp > 0)
{
return new BeatStroke(BeatStrokeDirection.up, toStrokeValue(strokeUp),0.0f);
}
else
{
return new BeatStroke(BeatStrokeDirection.down, toStrokeValue(strokeDown),0.0f);
}
}
private int toStrokeValue(sbyte value)
{
/*Unpack stroke value.
Stroke value maps to:
- *1*: hundred twenty-eighth
- *2*: sixty-fourth
- *3*: thirty-second
- *4*: sixteenth
- *5*: eighth
- *6*: quarter*/
switch (value)
{
case 1: return Duration.hundredTwentyEigth; break;
case 2: return Duration.sixtyFourth; break;
case 3: return Duration.thirtySecond; break;
case 4: return Duration.sixteenth; break;
case 5: return Duration.eigth; break;
case 6: return Duration.quarter; break;
default: return Duration.sixtyFourth; break;
}
}
private BendEffect readTremoloBar()
{
return readBend();
}
private BeatText readText()
{
var text = new BeatText();
text.value = GPBase.readIntByteSizeString();
return text;
}
private Chord readChord(int stringCount)
{
var chord = new Chord(stringCount);
chord.newFormat = GPBase.readBool()[0];
if (!chord.newFormat)
{
readOldChord(chord);
}
else
{
readNewChord(chord);
}
if ((chord.notes().Length) > 0) return chord;
return null;
}
private void readOldChord(Chord chord)
{
/*Read chord diagram encoded in GP3 format.
Chord diagram is read as follows:
- Name: :ref:`int-byte-size-string`. Name of the chord, e.g.
*Em*.
- First fret: :ref:`int`. The fret from which the chord is
displayed in chord editor.
- List of frets: 6 :ref:`Ints <int>`. Frets are listed in order:
fret on the string 1, fret on the string 2, ..., fret on the
string 6. If string is untouched then the values of fret is
*-1*.*/
chord.name = GPBase.readIntByteSizeString();
chord.firstFret = GPBase.readInt()[0];
if (chord.firstFret > 0)
{
for (int i = 0; i < 6; i++)
{
var fret = GPBase.readInt()[0];
if (i < chord.strings.Length) chord.strings[i] = fret;
}
}
}
private void readNewChord(Chord chord)
{
/*Read new-style (GP4) chord diagram.
New-style chord diagram is read as follows:
- Sharp: :ref:`bool`. If true, display all semitones as sharps,
otherwise display as flats.
- Blank space, 3 :ref:`Bytes <byte>`.
- Root: :ref:`int`. Values are:
* -1 for customized chords
* 0: C
* 1: C#
* ...
- Type: :ref:`int`. Determines the chord type as followed. See
:class:`guitarpro.models.ChordType` for mapping.
- Chord extension: :ref:`int`. See
:class:`guitarpro.models.ChordExtension` for mapping.
- Bass note: :ref:`int`. Lowest note of chord as in *C/Am*.
- Tonality: :ref:`int`. See
:class:`guitarpro.models.ChordAlteration` for mapping.
- Add: :ref:`bool`. Determines if an "add" (added note) is
present in the chord.
- Name: :ref:`byte-size-string`. Max length is 22.
- Fifth alteration: :ref:`int`. Maps to
:class:`guitarpro.models.ChordAlteration`.
- Ninth alteration: :ref:`int`. Maps to
:class:`guitarpro.models.ChordAlteration`.
- Eleventh alteration: :ref:`int`. Maps to
:class:`guitarpro.models.ChordAlteration`.
- List of frets: 6 :ref:`Ints <int>`. Fret values are saved as
in default format.
- Count of barres: :ref:`int`. Maximum count is 2.
- Barre frets: 2 :ref:`Ints <int>`.
- Barre start strings: 2 :ref:`Ints <int>`.
- Barre end string: 2 :ref:`Ints <int>`.
- Omissions: 7 :ref:`Bools <bool>`. If the value is true then
note is played in chord.
- Blank space, 1 :ref:`byte`.*/
chord.sharp = GPBase.readBool()[0];
var intonation = chord.sharp ? "sharp" : "flat";
GPBase.skip(3);
chord.root = new PitchClass(GPBase.readByte()[0], -1, "", intonation);
chord.type = (ChordType)GPBase.readByte()[0];
chord.extension = (ChordExtension)GPBase.readByte()[0];
chord.bass = new PitchClass(GPBase.readInt()[0], -1, "", intonation);
chord.tonality = (ChordAlteration)GPBase.readInt()[0];
chord.add = GPBase.readBool()[0];
chord.name = GPBase.readByteSizeString(22);
chord.fifth = (ChordAlteration)GPBase.readByte()[0];
chord.ninth = (ChordAlteration)GPBase.readByte()[0];
chord.eleventh = (ChordAlteration)GPBase.readByte()[0];
chord.firstFret = GPBase.readInt()[0];
for (int i = 0; i < 7; i++)
{
var fret = GPBase.readInt()[0];
if (i < chord.strings.Length) chord.strings[i] = fret;
}
chord.barres.Clear();
var barresCount = GPBase.readByte()[0];
var barreFrets = GPBase.readByte(5);
var barreStarts = GPBase.readByte(5);
var barreEnds = GPBase.readByte(5);
for (int x = 0; x < Math.Min(5, (int)barresCount); x++)
{
var barre = new Barre(barreFrets[x], barreStarts[x], barreEnds[x]);
chord.barres.Add(barre);
}
chord.omissions = GPBase.readBool(7);
GPBase.skip(1);
List <Fingering> f = new List<Fingering>();
for (int x=0; x < 7; x++)
{
f.Add((Fingering)GPBase.readSignedByte()[0]);
}
chord.fingerings = f;
chord.show = GPBase.readBool()[0];
}
private Duration readDuration(byte flags)
{
/*Duration is composed of byte signifying duration and an integer
that maps to :class:`guitarpro.models.Tuplet`.
The byte maps to following values:
- *-2*: whole note
- *-1*: half note
- *0*: quarter note
- *1*: eighth note
- *2*: sixteenth note
- *3*: thirty-second note
If flag at *0x20* is true, the tuplet is read.*/
var duration = new Duration();
duration.value = 1 << (GPBase.readSignedByte()[0] + 2);
duration.isDotted = ((flags & 0x01) != 0);
if ((flags & 0x20) != 0)
{
var iTuplet = GPBase.readInt()[0];
switch (iTuplet)
{
case 3: duration.tuplet.enters = 3; duration.tuplet.times = 2; break;
case 5: duration.tuplet.enters = 5; duration.tuplet.times = 4; break;
case 6: duration.tuplet.enters = 6; duration.tuplet.times = 4; break;
case 7: duration.tuplet.enters = 7; duration.tuplet.times = 4; break;
case 9: duration.tuplet.enters = 9; duration.tuplet.times = 8; break;
case 10: duration.tuplet.enters = 10; duration.tuplet.times = 8; break;
case 11: duration.tuplet.enters = 11; duration.tuplet.times = 8; break;
case 12: duration.tuplet.enters = 12; duration.tuplet.times = 8; break;
}
}
return duration;
}
private Beat getBeat(Voice voice, int start)
{
for (int x = voice.beats.Count - 1; x >= 0; x--)
{
if (voice.beats[x].start == start) return voice.beats[x];
}
var newBeat = new Beat(voice);
newBeat.start = start;
voice.beats.Add(newBeat);
return newBeat;
}
private void readTracks(int trackCount, MidiChannel[] channels)
{
for (int i = 0; i < trackCount; i++)
{
Track track = new Track(this, i + 1, new List<GuitarString>(), new List<Measure>());
readTrack(track, channels);
this.tracks.Add(track);
}
}
private void readTrack(Track track, MidiChannel[] channels)
{
/*
* Read track.
The first byte is the track's flags. It presides the track's
attributes:
- *0x01*: drums track
- *0x02*: 12 stringed guitar track
- *0x04*: banjo track
- *0x08*: *blank*
- *0x10*: *blank*
- *0x20*: *blank*
- *0x40*: *blank*
- *0x80*: *blank*
Flags are followed by:
- Name: :ref:`byte-size-string`. A 40 characters long string
containing the track's name.
- Number of strings: :ref:`int`. An integer equal to the number
of strings of the track.
- Tuning of the strings: List of 7 :ref:`Ints <int>`. The tuning
of the strings is stored as a 7-integers table, the "Number of
strings" first integers being really used. The strings are
stored from the highest to the lowest.
- Port: :ref:`int`. The number of the MIDI port used.
- Channel. See :meth:`GP3File.readChannel`.
- Number of frets: :ref:`int`. The number of frets of the
instrument.
- Height of the capo: :ref:`int`. The number of the fret on
which a capo is set. If no capo is used, the value is 0.
- Track's color. The track's displayed color in Guitar Pro.*/
byte flags = GPBase.readByte()[0];
track.isPercussionTrack = ((flags & 0x01) != 0);
track.is12StringedGuitarTrack = ((flags & 0x02) != 0);
track.isBanjoTrack = ((flags & 0x04) != 0);
track.name = GPBase.readByteSizeString(40);
var stringCount = GPBase.readInt()[0];
for (int i = 0; i < 7; i++)
{
int iTuning = GPBase.readInt()[0];
if (stringCount > i)
{
var oString = new GuitarString(i + 1, iTuning);
track.strings.Add(oString);
}
}
track.port = GPBase.readInt()[0];
track.channel = readChannel(channels);