-
Notifications
You must be signed in to change notification settings - Fork 21
/
NativeFormat.cs
executable file
·1467 lines (1234 loc) · 63.2 KB
/
NativeFormat.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 UnityEngine;
namespace Native {
public class NativeFormat {
public string title = "";
public string subtitle = "";
public string artist = "";
public string album = "";
public string words = "";
public string music = "";
public List<DirectionSign> directions = new List<DirectionSign>();
public List<Annotation> annotations = new List<Annotation>();
public List<Tempo> tempos = new List<Tempo>();
public List<MasterBar> barMaster = new List<MasterBar>();
public List<Track> tracks = new List<Track>();
public List<Lyrics> lyrics = new List<Lyrics>();
private List<int> notesInMeasures = new List<int>();
public static bool[] availableChannels = new bool[16];
public MidiExport.MidiExport toMidi()
{
MidiExport.MidiExport mid = new MidiExport.MidiExport();
mid.midiTracks.Add(getMidiHeader()); //First, untitled track
foreach (Track track in tracks)
{
mid.midiTracks.Add(track.getMidi());
}
return mid;
}
private MidiExport.MidiTrack getMidiHeader()
{
var midiHeader = new MidiExport.MidiTrack();
//text(s) - name of song, artist etc., created by Gitaro
//copyright - by Gitaro
//midi port 0
//time signature
//key signature
//set tempo
///////marker text (will be seen in file) - also Gitaro copyright blabla
//end_of_track
midiHeader.messages.Add(new MidiExport.MidiMessage("track_name", new string[] { "untitled" }, 0));
midiHeader.messages.Add(new MidiExport.MidiMessage("text", new string[] { title }, 0));
midiHeader.messages.Add(new MidiExport.MidiMessage("text", new string[] { subtitle }, 0));
midiHeader.messages.Add(new MidiExport.MidiMessage("text", new string[] { artist }, 0));
midiHeader.messages.Add(new MidiExport.MidiMessage("text", new string[] { album }, 0));
midiHeader.messages.Add(new MidiExport.MidiMessage("text", new string[] { words }, 0));
midiHeader.messages.Add(new MidiExport.MidiMessage("text", new string[] { music }, 0));
midiHeader.messages.Add(new MidiExport.MidiMessage("copyright", new string[] { "Copyright 2017 by Gitaro" }, 0));
midiHeader.messages.Add(new MidiExport.MidiMessage("marker", new string[] { title+" / "+artist+" - Copyright 2017 by Gitaro" }, 0));
midiHeader.messages.Add(new MidiExport.MidiMessage("midi_port", new string[] { "0" }, 0));
//Get tempos from List tempos, get key_signature and time_signature from barMaster
var tempoIndex = 0;
var masterBarIndex = 0;
var currentIndex = 0;
var oldTimeSignature = "";
var oldKeySignature = "";
if (tempos.Count == 0) tempos.Add(new Tempo());
while (tempoIndex < tempos.Count || masterBarIndex < barMaster.Count)
{
//Compare next entry of both possible sources
if (tempoIndex == tempos.Count || tempos[tempoIndex].position >= barMaster[masterBarIndex].index) //next measure comes first
{
if (!barMaster[masterBarIndex].keyBoth.Equals(oldKeySignature))
{
//Add Key-Sig to midiHeader
midiHeader.messages.Add(new MidiExport.MidiMessage("key_signature", new string[] { ""+ barMaster[masterBarIndex].key, ""+ barMaster[masterBarIndex].keyType }, barMaster[masterBarIndex].index - currentIndex));
currentIndex = barMaster[masterBarIndex].index;
oldKeySignature = barMaster[masterBarIndex].keyBoth;
}
if (!barMaster[masterBarIndex].time.Equals(oldTimeSignature))
{
//Add Time-Sig to midiHeader
midiHeader.messages.Add(new MidiExport.MidiMessage("time_signature", new string[] { "" + barMaster[masterBarIndex].num, "" + barMaster[masterBarIndex].den, "24", "8" }, barMaster[masterBarIndex].index - currentIndex));
currentIndex = barMaster[masterBarIndex].index;
oldTimeSignature = barMaster[masterBarIndex].time;
}
masterBarIndex++;
}
else //next tempo signature comes first
{
//Add Tempo-Sig to midiHeader
int _tempo = (int)(Math.Round((60 * 1000000) / tempos[tempoIndex].value));
midiHeader.messages.Add(new MidiExport.MidiMessage("set_tempo", new string[] { "" + _tempo }, tempos[tempoIndex].position - currentIndex));
currentIndex = tempos[tempoIndex].position;
tempoIndex++;
}
}
midiHeader.messages.Add(new MidiExport.MidiMessage("end_of_track", new string[] { }, 0));
return midiHeader;
}
public NativeFormat(GPFile fromFile)
{
title = fromFile.title;
subtitle = fromFile.subtitle;
artist = fromFile.interpret;
album = fromFile.album;
words = fromFile.words;
music = fromFile.music;
tempos = retrieveTempos(fromFile);
directions = fromFile.directions;
barMaster = retrieveMasterBars(fromFile);
tracks = retrieveTracks(fromFile);
lyrics = fromFile.lyrics;
updateAvailableChannels();
}
private void updateAvailableChannels()
{
for (int x = 0; x < 16; x++) { if (x != 9) { availableChannels[x] = true; } else { availableChannels[x] = false; } }
foreach (Track track in tracks)
{
availableChannels[track.channel] = false;
}
}
public List<Track> retrieveTracks(GPFile file)
{
List<Track> tracks = new List<Track>();
foreach (global::Track tr in file.tracks)
{
Track track = new Track();
track.name = tr.name;
track.patch = tr.channel.instrument;
track.port = tr.port;
track.channel = tr.channel.channel;
track.playbackState = PlaybackState.def;
track.capo = tr.offset;
if (tr.isMute) track.playbackState = PlaybackState.mute;
if (tr.isSolo) track.playbackState = PlaybackState.solo;
track.tuning = getTuning(tr.strings);
track.notes = retrieveNotes(tr, track.tuning, track);
tracks.Add(track);
}
return tracks;
}
public void addToTremoloBarList(int index, int duration, BendEffect bend, Track myTrack)
{
int at;
myTrack.tremoloPoints.Add(new TremoloPoint(0.0f,index)); //So that it can later be recognized as the beginning
foreach (global::BendPoint bp in bend.points)
{
at = index + (int)(bp.GP6position * duration / 100.0f);
var point = new TremoloPoint();
point.index = at;
point.value = bp.GP6value;
myTrack.tremoloPoints.Add(point);
}
var tp = new TremoloPoint();
tp.index = index + duration;
tp.value = 0;
myTrack.tremoloPoints.Add(tp); //Back to 0 -> Worst case there will be on the same index the final of tone 1, 0, and the beginning of tone 2.
}
public List<BendPoint> getBendPoints(int index, int duration, BendEffect bend)
{
List<BendPoint> ret = new List<BendPoint>();
int at;
foreach (global::BendPoint bp in bend.points)
{
at = index + (int)(bp.GP6position * duration / 100.0f);
var point = new BendPoint();
point.index = at;
point.value = bp.GP6value;
ret.Add(point);
}
return ret;
}
public List<Note> retrieveNotes(global::Track track, int[] tuning, Track myTrack)
{
List<Note> notes = new List<Note>();
int index = 0;
Note[] last_notes = new Note[10];
bool[] last_was_tie = new bool[10];
for(int x = 0; x < 10; x++) { last_was_tie[x] = false; }
//GraceNotes if on beat - reducing the next note's length
bool rememberGrace = false;
bool rememberedGrace = false;
int graceLength = 0;
int subtractSubindex = 0;
for (int x = 0; x < 10; x++) last_notes[x] = null;
int measureIndex = -1;
int notesInMeasure = 0;
foreach (Measure m in track.measures)
{
notesInMeasure = 0;
measureIndex++;
bool skipVoice = false;
if (m.simileMark == SimileMark.simple) //Repeat last measure
{
int amountNotes = notesInMeasures[notesInMeasures.Count-1]; //misuse prohibited by guitarpro
int endPoint = notes.Count;
for (int x = endPoint - amountNotes; x < endPoint; x++)
{
Note newNote = new Note(notes[x]);
Measure oldM = track.measures[measureIndex - 1];
newNote.index += flipDuration(oldM.header.timeSignature.denominator) * oldM.header.timeSignature.numerator;
notes.Add(newNote);
notesInMeasure++;
}
skipVoice = true;
}
if (m.simileMark == SimileMark.firstOfDouble || m.simileMark == SimileMark.secondOfDouble) //Repeat first or second of last two measures
{
int secondAmount = notesInMeasures[notesInMeasures.Count - 1]; //misuse prohibited by guitarpro
int firstAmount = notesInMeasures[notesInMeasures.Count - 2];
int endPoint = notes.Count-secondAmount;
for (int x = endPoint - firstAmount; x < endPoint; x++)
{
Note newNote = new Note(notes[x]);
Measure oldM1 = track.measures[measureIndex - 2];
Measure oldM2 = track.measures[measureIndex - 1];
newNote.index += flipDuration(oldM1.header.timeSignature.denominator) * oldM1.header.timeSignature.numerator;
newNote.index += flipDuration(oldM2.header.timeSignature.denominator) * oldM2.header.timeSignature.numerator;
notes.Add(newNote);
notesInMeasure++;
}
skipVoice = true;
}
foreach (Voice v in m.voices)
{
if (skipVoice) break;
int subIndex = 0;
foreach (Beat b in v.beats)
{
if (b.text != null && !b.text.value.Equals("")) annotations.Add(new Annotation(b.text.value,index+subIndex));
if (b.effect.tremoloBar != null) addToTremoloBarList(index + subIndex, flipDuration(b.duration), b.effect.tremoloBar, myTrack);
//Prepare Brush or Arpeggio
bool hasBrush = false;
int brushInit = 0;
int brushIncrease = 0;
BeatStrokeDirection brushDirection = BeatStrokeDirection.none;
if (b.effect.stroke != null)
{
int notesCnt = b.notes.Count;
brushDirection = b.effect.stroke.direction;
if (brushDirection != BeatStrokeDirection.none && notesCnt > 1) {
hasBrush = true;
Duration temp = new Duration();
temp.value = b.effect.stroke.value;
int brushTotalDuration = flipDuration(temp);
int beatTotalDuration = flipDuration(b.duration);
brushIncrease = brushTotalDuration / (notesCnt);
int startPos = index + subIndex + (int)((brushTotalDuration-brushIncrease) * (b.effect.stroke.startTime - 1));
int endPos = startPos + brushTotalDuration - brushIncrease;
if (brushDirection == BeatStrokeDirection.down)
{
brushInit = startPos;
} else
{
brushInit = endPos;
brushIncrease = -brushIncrease;
}
}
}
foreach (global::Note n in b.notes)
{
Note note = new Note();
//Beat values
note.isTremBarVibrato = b.effect.vibrato;
note.fading = Fading.none;
if (b.effect.fadeIn) note.fading = Fading.fadeIn;
if (b.effect.fadeOut) note.fading = Fading.fadeOut;
if (b.effect.volumeSwell) note.fading = Fading.volumeSwell;
note.isSlapped = b.effect.slapEffect == SlapEffect.slapping;
note.isPopped = b.effect.slapEffect == SlapEffect.popping;
note.isHammer = n.effect.hammer;
note.isRHTapped = b.effect.slapEffect == SlapEffect.tapping;
note.index = index + subIndex;
note.duration = flipDuration(b.duration);
//Note values
note.fret = n.value;
note.str = n.str;
note.velocity = n.velocity;
note.isVibrato = n.effect.vibrato;
note.isPalmMuted = n.effect.palmMute;
note.isMuted = n.type == NoteType.dead;
if (n.effect.harmonic != null) {
note.harmonicFret = n.effect.harmonic.fret;
if (n.effect.harmonic.fret == 0) //older format..
{
if (n.effect.harmonic.type == 2) note.harmonicFret = ((ArtificialHarmonic)n.effect.harmonic).pitch.actualOvertone;
}
switch (n.effect.harmonic.type)
{
case 1: note.harmonic = HarmonicType.natural; break;
case 2: note.harmonic = HarmonicType.artificial; break;
case 3: note.harmonic = HarmonicType.pinch; break;
case 4: note.harmonic = HarmonicType.tapped; break;
case 5: note.harmonic = HarmonicType.semi; break;
default:
note.harmonic = HarmonicType.natural;
break;
}
}
if (n.effect.slides != null) {
foreach (SlideType sl in n.effect.slides)
{
note.slidesToNext = note.slidesToNext || sl == SlideType.shiftSlideTo || sl == SlideType.legatoSlideTo;
note.slideInFromAbove = note.slideInFromAbove || sl == SlideType.intoFromAbove;
note.slideInFromBelow = note.slideInFromBelow || sl == SlideType.intoFromBelow;
note.slideOutDownwards = note.slideOutDownwards || sl == SlideType.outDownwards;
note.slideOutUpwards = note.slideOutUpwards || sl == SlideType.outUpwards;
}
}
if (n.effect.bend != null) note.bendPoints = getBendPoints(index + subIndex, flipDuration(b.duration), n.effect.bend);
//Ties
bool dontAddNote = false;
if (n.type == NoteType.tie)
{
dontAddNote = true;
//Find if note can simply be added to previous note
var last = last_notes[Math.Max(0,note.str-1)];
if (last != null) {
note.fret = last.fret; //For GP3 & GP4
if (last.harmonic != note.harmonic || last.harmonicFret != note.harmonicFret
) dontAddNote = false;
if (dontAddNote)
{
note.connect = true;
last.duration += note.duration;
last.addBendPoints(note.bendPoints);
}
}
} else // not a tie
{
last_was_tie[Math.Max(0, note.str - 1)] = false;
}
//Extra notes to replicate certain effects
//Triplet Feel
if (!barMaster[measureIndex].tripletFeel.Equals("none"))
{
TripletFeel trip = barMaster[measureIndex].tripletFeel;
//Check if at regular 8th or 16th beat position
bool is_8th_pos = subIndex % 480 == 0;
bool is_16th_pos = subIndex % 240 == 0;
bool is_first = true; //first of note pair
if (is_8th_pos) is_first = subIndex % 960 == 0;
if (is_16th_pos) is_first = is_8th_pos;
bool is_8th = b.duration.value == 8 && !b.duration.isDotted && !b.duration.isDoubleDotted && b.duration.tuplet.enters == 1 && b.duration.tuplet.times == 1;
bool is_16th = b.duration.value == 16 && !b.duration.isDotted && !b.duration.isDoubleDotted && b.duration.tuplet.enters == 1 && b.duration.tuplet.times == 1;
if ((trip == TripletFeel.eigth && is_8th_pos && is_8th) || (trip == TripletFeel.sixteenth && is_16th_pos && is_16th))
{
if (is_first) note.duration = (int)(note.duration * (4.0f / 3.0f));
if (!is_first)
{
note.duration = (int)(note.duration * (2.0f / 3.0f));
note.resizeValue *= (2.0f / 3.0f);
note.index += (int)(note.duration * (1.0f / 3.0f));
}
}
if ((trip == TripletFeel.dotted8th && is_8th_pos && is_8th) || (trip == TripletFeel.dotted16th && is_16th_pos && is_16th))
{
if (is_first) note.duration = (int)(note.duration * 1.5f);
if (!is_first)
{
note.duration = (int)(note.duration * 0.5f);
note.resizeValue *= (0.5f);
note.index += (int)(note.duration * 0.5f);
}
}
if ((trip == TripletFeel.scottish8th && is_8th_pos && is_8th) || (trip == TripletFeel.scottish16th && is_16th_pos && is_16th))
{
if (is_first) note.duration = (int)(note.duration * 0.5f);
if (!is_first)
{
note.duration = (int)(note.duration * 1.5f);
note.resizeValue *= (1.5f);
note.index -= (int)(note.duration * 0.5f);
}
}
}
//Tremolo Picking & Trill
if (n.effect.tremoloPicking != null || n.effect.trill != null)
{
int len = note.duration;
if (n.effect.tremoloPicking != null) len = flipDuration(n.effect.tremoloPicking.duration);
if (n.effect.trill != null) len = flipDuration(n.effect.trill.duration);
int origDuration = note.duration;
note.duration = len;
note.resizeValue *= ((float)len / origDuration);
int currentIndex = note.index + len;
last_notes[Math.Max(0, note.str - 1)] = note;
notes.Add(note);
notesInMeasure++;
dontAddNote = true; //Because we're doing it here already
bool originalFret = false;
int secondFret = note.fret;
if (n.effect.trill != null) { secondFret = n.effect.trill.fret - tuning[note.str - 1]; }
while (currentIndex+len <= note.index + origDuration)
{
Note newOne = new Note(note);
newOne.index = currentIndex;
if (!originalFret) newOne.fret = secondFret; //For trills
last_notes[Math.Max(0, note.str - 1)] = newOne;
if (n.effect.trill != null) newOne.isHammer = true;
notes.Add(newOne);
notesInMeasure++;
currentIndex += len;
originalFret = !originalFret;
}
}
//Grace Note
if (rememberGrace && note.duration > graceLength)
{
int orig = note.duration;
note.duration -= graceLength;
note.resizeValue *= ((float)note.duration / orig);
//subIndex -= graceLength;
rememberedGrace = true;
}
if (n.effect.grace != null)
{
bool isOnBeat = n.effect.grace.isOnBeat;
if (n.effect.grace.duration != -1)
{ //GP3,4,5 format
Note graceNote = new Note();
graceNote.index = note.index;
graceNote.fret = n.effect.grace.fret;
graceNote.str = note.str;
Duration dur = new Duration();
dur.value = n.effect.grace.duration;
graceNote.duration = flipDuration(dur); //works at least for GP5
if (isOnBeat)
{
int orig = note.duration;
note.duration -= graceNote.duration;
note.index += graceNote.duration;
note.resizeValue *= ((float)note.duration / orig);
} else
{
graceNote.index -= graceNote.duration;
}
notes.Add(graceNote); //TODO: insert at correct position!
notesInMeasure++;
} else {
if (isOnBeat) // shorten next note
{
rememberGrace = true;
graceLength = note.duration;
} else //Change previous note
{
if ( notes.Count > 0)
{
note.index -= note.duration; //Can lead to negative indices. Midi should handle that
subtractSubindex = note.duration;
}
}
}
}
//Dead Notes
if (n.type == NoteType.dead) {
int orig = note.duration;
note.velocity = (int)(note.velocity * 0.9f); note.duration /= 6;
note.resizeValue *= ((float)note.duration / orig);
}
//Ghost Notes
if (n.effect.palmMute) {
int orig = note.duration;
note.velocity = (int)(note.velocity * 0.7f); note.duration /= 2;
note.resizeValue *= ((float)note.duration / orig);
}
if (n.effect.ghostNote) { note.velocity = (int)(note.velocity * 0.8f);}
//Staccato, Accented, Heavy Accented
if (n.effect.staccato) {
int orig = note.duration;
note.duration /= 2;
note.resizeValue *= ((float)note.duration / orig);
}
if (n.effect.accentuatedNote) note.velocity = (int)(note.velocity * 1.2f);
if (n.effect.heavyAccentuatedNote) note.velocity = (int)(note.velocity * 1.4f);
//Arpeggio / Brush
if (hasBrush)
{
note.index = brushInit;
brushInit += brushIncrease;
}
if (!dontAddNote)
{
last_notes[Math.Max(0, note.str - 1)] = note;
notes.Add(note);
notesInMeasure++;
}
}
if (rememberedGrace) { subIndex -= graceLength; rememberGrace = false; rememberedGrace = false; } //After the change in duration for the second beat has been done
subIndex -= subtractSubindex;
subtractSubindex = 0;
subIndex += flipDuration(b.duration);
//Sort brushed tones
if (hasBrush && brushDirection == BeatStrokeDirection.up)
{
//Have to reorder them xxx123 -> xxx321
int notesCnt = b.notes.Count;
Note[] temp = new Note[notesCnt];
for (int x = notes.Count - notesCnt; x < notes.Count; x++)
{
temp[x - (notes.Count - notesCnt)] = new Note(notes[x]);
}
for (int x = notes.Count - notesCnt; x < notes.Count; x++)
{
notes[x] = temp[temp.Length - (x - (notes.Count - notesCnt))-1];
}
}
hasBrush = false;
}
break; //Consider only the first voice
}
int measureDuration = flipDuration(m.header.timeSignature.denominator) * m.header.timeSignature.numerator;
barMaster[measureIndex].duration = measureDuration;
barMaster[measureIndex].index = index;
index += measureDuration;
notesInMeasures.Add(notesInMeasure);
}
return notes;
}
public int[] getTuning(List<GuitarString> strings)
{
int[] tuning = new int[strings.Count];
for (int x = 0; x < tuning.Length; x++)
{
tuning[x] = strings[x].value;
}
return tuning;
}
public List<MasterBar> retrieveMasterBars(GPFile file)
{
List<MasterBar> masterBars = new List<MasterBar>();
foreach (MeasureHeader mh in file.measureHeaders)
{
//(mh.timeSignature.denominator) * mh.timeSignature.numerator;
MasterBar mb = new MasterBar();
mb.time = mh.timeSignature.numerator + "/" + mh.timeSignature.denominator.value;
mb.num = mh.timeSignature.numerator;
mb.den = mh.timeSignature.denominator.value;
string keyFull = ""+(int)mh.keySignature;
if (!(keyFull.Length == 1)) {
mb.keyType = int.Parse(keyFull.Substring(keyFull.Length - 1));
mb.key = int.Parse(keyFull.Substring(0,keyFull.Length-1));
}
else
{
mb.key = 0;
mb.keyType = int.Parse(keyFull);
}
mb.keyBoth = keyFull; //Useful for midiExport later
mb.tripletFeel = mh.tripletFeel;
masterBars.Add(mb);
}
return masterBars;
}
public List<Tempo> retrieveTempos(GPFile file)
{
List<Tempo> tempos = new List<Tempo>();
//Version < 4 -> look at Measure Headers, >= 4 look at mixtablechanges
int version = file.versionTuple[0];
if (version < 4) //Look at MeasureHeaders
{
//Get inital tempo from file header
Tempo init = new Tempo();
init.position = 0;
init.value = file.tempo;
if (init.value != 0) tempos.Add(init);
int pos = 0;
float oldTempo = file.tempo;
foreach (MeasureHeader mh in file.measureHeaders)
{
Tempo t = new Tempo();
t.value = mh.tempo.value;
t.position = pos;
pos += flipDuration(mh.timeSignature.denominator) * mh.timeSignature.numerator;
if (oldTempo != t.value) tempos.Add(t);
oldTempo = t.value;
}
} else //Look at MixtableChanges - only on track 1, voice 1
{
int pos = 0;
//Get inital tempo from file header
Tempo init = new Tempo();
init.position = 0;
init.value = file.tempo;
if (init.value != 0) tempos.Add(init);
foreach (Measure m in file.tracks[0].measures)
{
int smallPos = 0; //inner measure position
if (m.voices.Count == 0) continue;
foreach (Beat b in m.voices[0].beats){
if (b.effect != null)
{
if (b.effect.mixTableChange != null)
{
MixTableItem tempo = b.effect.mixTableChange.tempo;
if (tempo != null)
{
Tempo t = new Tempo();
t.value = tempo.value;
t.position = pos + smallPos;
tempos.Add(t);
}
}
}
smallPos += flipDuration(b.duration);
}
pos += flipDuration(m.header.timeSignature.denominator) * m.header.timeSignature.numerator;
}
}
return tempos;
}
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);
int enters = d.tuplet.enters;
int times = d.tuplet.times;
//3:2 = standard triplet, 3 notes in the time of 2
result = (int)((result*times)/(float)enters);
return result;
}
}
public class Note
{
public float resizeValue = 1.0f; //Should reflect any later changes made to the note duration, so that bendPoints can be adjusted
//Values from Note
public int str = 0;
public int fret = 0;
public int velocity = 100;
public bool isVibrato = false;
public bool isHammer = false;
public bool isPalmMuted = false;
public bool isMuted = false;
public HarmonicType harmonic = HarmonicType.none;
public float harmonicFret = 0.0f;
public bool slidesToNext = false;
public bool slideInFromBelow = false;
public bool slideInFromAbove = false;
public bool slideOutUpwards = false;
public bool slideOutDownwards = false;
public List<BendPoint> bendPoints = new List<BendPoint>();
public bool connect = false; //= tie
//Values from Beat
public List<BendPoint> tremBarPoints = new List<BendPoint>();
public bool isTremBarVibrato = false;
public bool isSlapped = false;
public bool isPopped = false;
public int index = 0;
public int duration = 0;
public Fading fading = Fading.none;
public bool isRHTapped = false;
public Note(Note old)
{
str = old.str; fret = old.fret; velocity = old.velocity; isVibrato = old.isVibrato;
isHammer = old.isHammer; isPalmMuted = old.isPalmMuted; isMuted = old.isMuted;
harmonic = old.harmonic; harmonicFret = old.harmonicFret; slidesToNext = old.slidesToNext;
slideInFromAbove = old.slideInFromAbove; slideInFromBelow = old.slideInFromBelow;
slideOutDownwards = old.slideOutDownwards; slideOutUpwards = old.slideOutUpwards;
bendPoints.AddRange(old.bendPoints);
tremBarPoints.AddRange(old.tremBarPoints);
isTremBarVibrato = old.isTremBarVibrato; isSlapped = old.isSlapped; isPopped = old.isPopped;
index = old.index; duration = old.duration; fading = old.fading; isRHTapped = old.isRHTapped;
resizeValue = old.resizeValue;
}
public Note() { }
public void addBendPoints(List<BendPoint> bendPoints)
{
//Hopefully no calculation involved
this.bendPoints.AddRange(bendPoints);
}
}
public enum Fading
{
none = 0, fadeIn = 1, fadeOut = 2, volumeSwell = 3
}
public class Annotation
{
public string value = "";
public int position = 0;
public Annotation(string v = "", int pos = 0)
{
value = v; position = pos;
}
}
public class TremoloPoint
{
public float value = 0; //0 nothing, 100 one whole tone up
public int index = 0;
public TremoloPoint()
{
}
public TremoloPoint(float value, int index)
{
this.value = value;
this.index = index;
}
}
public class BendPoint
{
public float value = 0;
public int index = 0; //also global index of midi
public int usedChannel = 0; //After being part of BendingPlan
public BendPoint(float value, int index)
{
this.value = value;
this.index = index;
}
public BendPoint() { }
}
public enum HarmonicType
{
none = 0, natural = 1, artificial = 2, pinch = 3, semi = 4, tapped = 5
}
public class Track
{
public string name = "";
public int patch = 0;
public int port = 0;
public int channel = 0;
public int capo = 0;
public PlaybackState playbackState = PlaybackState.def;
public int[] tuning = new int[] { 40, 45, 50, 55, 59, 64 };
public List<Note> notes = new List<Note>();
public List<TremoloPoint> tremoloPoints = new List<TremoloPoint>();
private List<int[]> volumeChanges = new List<int[]>();
public MidiExport.MidiTrack getMidi()
{
var midiTrack = new MidiExport.MidiTrack();
midiTrack.messages.Add(new MidiExport.MidiMessage("midi_port", new string[] { ""+port }, 0));
midiTrack.messages.Add(new MidiExport.MidiMessage("track_name", new string[] { name }, 0));
midiTrack.messages.Add(new MidiExport.MidiMessage("program_change", new string[] { ""+channel,""+patch }, 0));
List<int[]> noteOffs = new List<int[]>();
List<int[]> channelConnections = new List<int[]>(); //For bending and trembar: [original Channel, artificial Channel, index at when to delete artificial]
List<BendingPlan> activeBendingPlans = new List<BendingPlan>();
int currentIndex = 0;
Note _temp = new Note();
_temp.index = notes[notes.Count - 1].index + notes[notes.Count - 1].duration;
_temp.str = -2;
notes.Add(_temp);
tremoloPoints = addDetailsToTremoloPoints(tremoloPoints, 60);
//var _notes = addSlidesToNotes(notes); //Adding slide notes here, as they should not appear as extra notes during playback
foreach (Note n in notes)
{
noteOffs.Sort((x, y) => x[0].CompareTo(y[0]));
//Check for active bendings in progress
List<BendPoint> currentBPs = findAndSortCurrentBendPoints(activeBendingPlans, n.index);
float _tremBarChange = 0.0f;
foreach (BendPoint bp in currentBPs)
{
//Check first if there is a note_off event happening in the meantime..
List<int[]> newNoteOffs = new List<int[]>();
foreach (int[] noteOff in noteOffs)
{
if (noteOff[0] <= bp.index) //between last and this note, a note off event should occur
{
midiTrack.messages.Add(
new MidiExport.MidiMessage("note_off",
new string[] { "" + noteOff[2], "" + noteOff[1], "0" }, noteOff[0] - currentIndex));
currentIndex = noteOff[0];
}
else
{
newNoteOffs.Add(noteOff);
}
}
noteOffs = newNoteOffs;
//Check if there are active tremPoints to be adjusted for
List<TremoloPoint> _newTremPoints = new List<TremoloPoint>();
foreach (TremoloPoint tp in tremoloPoints)
{
if (tp.index <= bp.index) //between last and this note, a note off event should occur
{
_tremBarChange = tp.value;
}
else
{
_newTremPoints.Add(tp);
}
}
tremoloPoints = _newTremPoints;
//Check if there are active volume changes
List<int[]> _newVolumeChanges = new List<int[]>();
foreach (int[] vc in volumeChanges)
{
if (vc[0] <= bp.index) //between last and this note, a volume change event should occur
{ //channel control value
midiTrack.messages.Add(
new MidiExport.MidiMessage("control_change",
new string[] { "" + bp.usedChannel, "7",""+vc[1] }, vc[0] - currentIndex));
currentIndex = vc[0];
}
else
{
_newVolumeChanges.Add(vc);
}
}
volumeChanges = _newVolumeChanges;
midiTrack.messages.Add(
new MidiExport.MidiMessage("pitchwheel",
new string[] { "" + bp.usedChannel, "" + (int)((bp.value+_tremBarChange) * 25.6f) }, bp.index - currentIndex));
currentIndex = bp.index;
}
//Delete no longer active Bending Plans
List<BendingPlan> final = new List<BendingPlan>();
foreach (BendingPlan bpl in activeBendingPlans)
{
BendingPlan newBPL = new BendingPlan(bpl.originalChannel, bpl.usedChannel, new List<BendPoint>());
foreach (BendPoint bp in bpl.bendingPoints)
{
if (bp.index > n.index)
{
newBPL.bendingPoints.Add(bp);
}
}
if (newBPL.bendingPoints.Count > 0)
{
final.Add(newBPL);
}
else //That bending plan has finished
{
midiTrack.messages.Add(new MidiExport.MidiMessage("pitchwheel", new string[] { "" + bpl.usedChannel, "-128" }, 0));
midiTrack.messages.Add(new MidiExport.MidiMessage("control_change", new string[] { "" + bpl.usedChannel, "101", "127" }, 0));
midiTrack.messages.Add(new MidiExport.MidiMessage("control_change", new string[] { "" + bpl.usedChannel, "10", "127" }, 0));
//Remove the channel from channelConnections
List<int[]> newChannelConnections = new List<int[]>();
foreach (int[] cc in channelConnections)
{
if (cc[1] != bpl.usedChannel) newChannelConnections.Add(cc);
}
channelConnections = newChannelConnections;
NativeFormat.availableChannels[bpl.usedChannel] = true;
}
}