-
Notifications
You must be signed in to change notification settings - Fork 0
/
sib2hum_0.11.plg
2330 lines (1835 loc) · 129 KB
/
sib2hum_0.11.plg
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
{
Initialize "(){ AddToPluginsMenu('Convert Sibelius Score to Humdrum','Run');
// The following enables the latest behavior for the ManuScript interpreter.
// If you intend your plugin to run on previous versions of Sibelius where that functionality
// didn't exist, you will likely have to revisit the following:
if (Sibelius.ProgramVersion > 20200600) {
SetInterpreterOption(TreatSingleCharacterAsString);
SetInterpreterOption(SupportHalfSemitonePitchValues);
} }"
Run "() {
// Convert Sibelius Score to Humdrum Kern Format
// Version 0.11
// Written by: Hubert Léveillé Gauvin
// Date: January - February 2022
// NOTE: GRACE NOTES ARE NOT SUPPORTED AT THE MOMENT (they are ignored by NoteRestAtBarPosVoice)
clefDictionary = CreateClefDictionary();
instrumentDictionary = CreateInstrumentDictionary();
keySignatureDictionary = CreateKeySignatureDictionary();
specialBarlineDictionary = CreateSpecialBarlineDictionary();
durationDictionary = CreateDurationDictionary();
pitchDictionary = CreatePitchDictionary();
articulationDictionary = CreateArticulationDictionary();
openArticulationReferenceNeeded = false; // keep track whether needed to add !!!RDF**kern: o = harmonic/open to output. See CreateArticulationDictionary and ConvertArticulation for details.
closedArticulationReferenceNeeded = false; // keep track whether needed to add !!!RDF**kern: O = plus/closed to output. See CreateArticulationDictionary and ConvertArticulation for details.
// create array to track ongoing ties
/*
In humdrum, ties have two or three elements: Beginning and End, with maybe Middle. Thus, when note.IsTied = true, we need to track where this tie ends.
In this array, we will store ongoing ties, i.e. ties that have been started but not yet terminated.
On going ties are identified using a unique code, following this syntax: StaffNumber_VoiceNumber_humPitchToken. Thus middle c in staff 2 voice 3 will be '2_3_c'.
*/
ongoingTiesArray = CreateSparseArray();
thisScore = Sibelius.ActiveScore;
sysStaff = thisScore.SystemStaff;
barlines = thisScore.Barlines;
// Create blank text file
fname = '';
fname = Sibelius.GetDocumentsFolder();
fname = fname & 'sib2humtest.krn';
Sibelius.CreateTextFile(fname);
// Get info from File > Score Info, reformat for Humdrum and append to file
AppendScoreInfoToFile(fname);
// Add Exclusive interpretations
/* Assume for the moment that everything **kern */
humExclusiveInterpretationsLine = '';
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
humExclusiveInterpretationsLine = humExclusiveInterpretationsLine & '\*\*kern' & '\t';
currentStaff = currentStaff-1;
}
humExclusiveInterpretationsLine = TrimTabsLeading(humExclusiveInterpretationsLine,false);
// Add Exclusive interpretations to krn file
Sibelius.AppendLineToFile(fname,humExclusiveInterpretationsLine);
// Get name of instruments
humInstrumentLine = '';
humInstrumentNameFirstSystemLine = '';
humInstrumentNameSubSytemLine = '';
currentStaff = thisScore.StaffCount;
currentNumberOfVoices = CreateSparseArray();
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
currentNumberOfVoices[currentStaff] = 1; // when we start, all number of voices need to be set 1 per staff. This is before measure 1 in Humdrum
humInstrumentLine = humInstrumentLine & ConvertInstrumentCode(s.InitialStyleId,instrumentDictionary) & '\t';
humInstrumentNameFirstSystemLine = humInstrumentNameFirstSystemLine & '*I' & Chr(34) & s.FullInstrumentName & '\t';
humInstrumentNameSubSytemLine = humInstrumentNameSubSytemLine & '*I\'' & s.ShortInstrumentName & '\t';
currentStaff = currentStaff-1;
}
humInstrumentLine = TrimTabsLeading(humInstrumentLine,false);
humInstrumentNameFirstSystemLine = TrimTabsLeading(humInstrumentNameFirstSystemLine,false);
humInstrumentNameSubSytemLine = TrimTabsLeading(humInstrumentNameSubSytemLine,false);
//Add Humdrum Instrument Line to krn file
Sibelius.AppendLineToFile(fname,humInstrumentLine);
Sibelius.AppendLineToFile(fname,humInstrumentNameFirstSystemLine);
Sibelius.AppendLineToFile(fname,humInstrumentNameSubSytemLine);
// Convert Inital clef name
humInitialClefLine = '';
currentClef = CreateSparseArray();
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
currentClef[currentStaff] = ConvertClef(s.InitialClefStyleId,clefDictionary);
humInitialClefLine = humInitialClefLine & ConvertClef(s.InitialClefStyleId,clefDictionary) & '\t';
currentStaff = currentStaff-1;
}
humInitialClefLine = TrimTabsLeading(humInitialClefLine,false);
//Add Humdrum Clef Line to krn file
Sibelius.AppendLineToFile(fname,humInitialClefLine);
// Convert Initial Key Signature
initialKeySignature = thisScore.SystemStaff.InitialKeySignature.Sharps;
currentKeySignature = initialKeySignature;
humInitialKeySignatureLine = '';
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
if (ConvertClef(s.InitialClefStyleId,clefDictionary) = '*clefX' ) // if clef is percussion, no key sig
{
humInitialKeySignatureLine = humInitialKeySignatureLine & '*' & '\t';
} else {
humInitialKeySignatureLine = humInitialKeySignatureLine & ConvertKeySignature(initialKeySignature,keySignatureDictionary) & '\t';
}
currentStaff = currentStaff-1;
}
humInitialKeySignatureLine = TrimTabsLeading(humInitialKeySignatureLine,false);
//Add Humdrum Initial Key Signature Line to krn file
Sibelius.AppendLineToFile(fname,humInitialKeySignatureLine);
// Convert Initial Time Signature
initialTimeSignature = thisScore.SystemStaff.CurrentTimeSignature(1);
currentTimeSignature = initialTimeSignature;
humInitialTimeSignatureLine = '';
initialTimeSignatureArray = ConvertTimeSignature(initialTimeSignature.Numerator,initialTimeSignature.Denominator,initialTimeSignature.Text);
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
humInitialTimeSignatureLine = humInitialTimeSignatureLine & initialTimeSignatureArray[0] & '\t';
currentStaff = currentStaff-1;
}
humInitialTimeSignatureLine = TrimTabsLeading(humInitialTimeSignatureLine,false);
//Add Humdrum Initial Time Signature Line to krn file
Sibelius.AppendLineToFile(fname,humInitialTimeSignatureLine);
//Check if meter info exists (e.g. common-time), and if so add to krn file
if (initialTimeSignatureArray[1] != '')
{
humInitialMeterLine = '';
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
humInitialMeterLine = humInitialMeterLine & initialTimeSignatureArray[1] & '\t';
currentStaff = currentStaff-1;
}
humInitialMeterLine = TrimTabsLeading(humInitialMeterLine,false);
//Add Humdrum Initial Meter Line to krn file
Sibelius.AppendLineToFile(fname,humInitialMeterLine);
}
// Create a humline with the barline before First Measure
// According to C.Sapp, 'Labeling the first bar is necessary if you need to extract it by measure number with a tool such as myank.'
nextExternalBarNumber = sysStaff.NthBar(1).ExternalBarNumberString;
x = 1;
sblCount = 1;
startsWithRepeat = false;
// this is not elegant, but basically see if there's a start repeat in bar 1
for each SpecialBarline sbl in sysStaff.NthBar(1)
{
if (sblCount = 1)
{
if (sbl.BarlineInternalType = SpecialBarlineStartRepeat)
{
startsWithRepeat = true;
startRepeat = sbl.BarlineInternalType;
}
}
}
if (nextExternalBarNumber = '')
{
if (startsWithRepeat = false)
{
thisMeasureBarline = '=-';
} else {
thisMeasureBarline = '=' & '1' & ConvertSpecialBarlineCode(startRepeat,specialBarlineDictionary);
}
} else {
if (startsWithRepeat = false)
{
thisMeasureBarline = '=' & nextExternalBarNumber;
} else {
thisMeasureBarline = '=' & nextExternalBarNumber & ConvertSpecialBarlineCode(startRepeat,specialBarlineDictionary);
}
}
humBarlineLine = '';
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
humBarlineLine = humBarlineLine & thisMeasureBarline & '\t';
currentStaff = currentStaff-1;
}
humBarlineLine = TrimTabsLeading(humBarlineLine,false);
//Add Humdrum Barline Line to krn file
Sibelius.AppendLineToFile(fname,humBarlineLine);
// prepare progress bar
firstbarnum = 1;
lastbarnum = thisScore.SystemStaff.BarCount;
thisScore.Redraw = False;
progress = 0;
totalBars = lastbarnum;
Sibelius.CreateProgressDialog('Converting to Humdrum format', firstbarnum, totalBars);
addNewKeySignature = false;
addNewTimeSignature = false;
wholeBarRestTrackerArray = ResetWholeBarRestTracker();
thisBarNumberOfVoices = CreateSparseArray();
voiceNumberArray = CreateSparseArray();
for each Bar b in sysStaff
{
//for progress bar
continue = Sibelius.UpdateProgressDialog(progress,'');
if (continue = False)
{
Sibelius.DestroyProgressDialog();
thisScore.Redraw = True;
return False;
}
progress = progress + 1;
currentInternalBarNumber = b.BarNumber;
if (b.BarNumber < sysStaff.BarCount) // As long as not on the last bar
{
nextInternalBarNumber = currentInternalBarNumber+1;
nextExternalBarNumber = sysStaff.NthBar(nextInternalBarNumber).ExternalBarNumberString;
}
noteRestPositionArray = CreateSparseArray();
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
voiceNumberArray = CreateSparseArray();
//get a list of all the unique NoteRest position for this bar, regardless of the staff
for each BarObject in s.NthBar(currentInternalBarNumber)
{
voiceNumberArray[voiceNumberArray.Length] = BarObject.VoiceNumber ; // keep track of all voice number to later identify the largest one
if (BarObject.Type = 'BarRest')
{
if (BarObject.RestType = '0') // if RestType = WholeBarRest
{
wholeBarRestTrackerArray[currentStaff] = true ; // set the tracker for this staff to true
if (utils.IsInArray(noteRestPositionArray,0) = false) //add if 0 not already in array
{
noteRestPositionArray[noteRestPositionArray.Length] = 0; // Create an 'event' at position 0, and add it at the end of the array. Because array iteration starts at 0, Length is the equivalent of one after last item
}
}
} else {
if (BarObject.Type = 'NoteRest')
{
if (utils.IsInArray(noteRestPositionArray,BarObject.Position) = false) //add if not already in array
{
noteRestPositionArray[noteRestPositionArray.Length] = BarObject.Position;
}
}
}
}
//sort the array, reverse it and get the first item, i.e. the largest number of voices.
voiceNumberArray = utils.SortArrayNumeric(voiceNumberArray,false);
numberOfVoices = voiceNumberArray[voiceNumberArray.Length-1] ;
thisBarNumberOfVoices[currentStaff] = numberOfVoices;
currentStaff=currentStaff-1;
}
// test if the time sig for this element is different than current.
if( currentTimeSignature.Text != sysStaff.CurrentTimeSignature(currentInternalBarNumber).Text)
{ currentTimeSignature = thisScore.SystemStaff.CurrentTimeSignature(currentInternalBarNumber);
addNewTimeSignature = true;
humNewTimeSignatureLine = '';
currentTimeSignatureArray = ConvertTimeSignature(currentTimeSignature.Numerator,currentTimeSignature.Denominator,currentTimeSignature.Text);
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
v = currentNumberOfVoices[currentStaff] ;
while (v>0)
{
humNewTimeSignatureLine = humNewTimeSignatureLine & currentTimeSignatureArray[0] & '\t';
v = v-1;
}
currentStaff = currentStaff-1;
}
humNewTimeSignatureLine = TrimTabsLeading(humNewTimeSignatureLine,false);
//Add Humdrum New Time Signature Line to krn file
Sibelius.AppendLineToFile(fname,humNewTimeSignatureLine);
//Check if meter info exists (e.g. common-time), and if so add to krn file
humNewMeterLine = '';
if (currentTimeSignatureArray[1] != '')
{
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
v = currentNumberOfVoices[currentStaff] ;
while (v>0)
{
humNewMeterLine = humNewMeterLine & currentTimeSignatureArray[1] & '\t';
v = v-1;
}
currentStaff = currentStaff-1;
}
humNewMeterLine = TrimTabsLeading(humNewMeterLine,false);
//Add Humdrum Initial Meter Line to krn file
Sibelius.AppendLineToFile(fname,humNewMeterLine);
}
}
// check to see if the number of voices has changed between this bar and last bar
keepLooping = true;
writeHumSpinePathLineToFile = false;
while (keepLooping=true)
{
keepLoopingArray = CreateSparseArray();
keepLooping = false;
currentStaff = thisScore.StaffCount;
writeHumSpinePathLineToFile = false;
humSpinePathLine = '';
alreadyRemovedASpine = false;
while (currentStaff>0) //for each staff
{
if (thisBarNumberOfVoices[currentStaff] = currentNumberOfVoices[currentStaff])
{
for x = 0 to thisBarNumberOfVoices[currentStaff]
{
humSpinePathLine = humSpinePathLine & '*' & '\t' ;
}
} else {
writeHumSpinePathLineToFile = true;
if (thisBarNumberOfVoices[currentStaff] > currentNumberOfVoices[currentStaff]) //if we need to add a spine
{
humSpinePathLine = humSpinePathLine & CreateSpinePathIndicators(currentNumberOfVoices[currentStaff],currentNumberOfVoices[currentStaff]+1) & '\t' ;
currentNumberOfVoices[currentStaff] = currentNumberOfVoices[currentStaff]+1 ;
} else { //if we need to remove a spine
if (alreadyRemovedASpine = true)
{
for x = 0 to currentNumberOfVoices[currentStaff]
{
humSpinePathLine = humSpinePathLine & '*' & '\t' ;
}
} else {
humSpinePathLine = humSpinePathLine & CreateSpinePathIndicators(currentNumberOfVoices[currentStaff],currentNumberOfVoices[currentStaff]-1) & '\t' ;
currentNumberOfVoices[currentStaff] = currentNumberOfVoices[currentStaff]-1 ;
alreadyRemovedASpine = true;
}
}
}
if (thisBarNumberOfVoices[currentStaff] != currentNumberOfVoices[currentStaff])
{
keepLoopingArray[keepLoopingArray.Length] = 'true';
} else {
keepLoopingArray[keepLoopingArray.Length] = 'false';
}
currentStaff=currentStaff-1;
}
for each i in keepLoopingArray
{
if (i='true')
{
keepLooping = true;
}
}
if (writeHumSpinePathLineToFile = true)
{
humSpinePathLine = TrimTabsLeading(humSpinePathLine,false);
Sibelius.AppendLineToFile(fname,humSpinePathLine);
}
}
// sort the array
noteRestPositionArray = utils.SortArrayNumeric(noteRestPositionArray,false);
// create one humLine per Rhythmic Position in noteRestPositionArray
line = '1';
for each e in noteRestPositionArray
{
/* I think the loop for grace note should go here
but I'm not sure how to go about it.
*/
writeNewClefArray = CreateSparseArray();
newClefNeeded = false;
humLine = '';
// test if the key sig for this element is different than current.
// NOTE: This method assumes that all staves have same key signature, which might not always be true...
if( currentKeySignature != b.GetKeySignatureAt(e).Sharps)
{ currentKeySignature = b.GetKeySignatureAt(e).Sharps;
addNewKeySignature = true;
}
currentStaff = thisScore.StaffCount;
while (currentStaff>0) // for each staff, but reverse order...
{
s = thisScore.NthStaff(currentStaff);
v = currentNumberOfVoices[currentStaff] ;
// Store the clef at this staff/pos to compare later with currentClef
clefObject = s.NthBar(currentInternalBarNumber).GetClefAt(e);
if (ConvertClef(clefObject.ConcertClefStyleId,clefDictionary) != currentClef[currentStaff]) // if the clef for this staff has changed
{
writeNewClefArray[currentStaff] = ConvertClef(clefObject.ConcertClefStyleId,clefDictionary);
currentClef[currentStaff] = ConvertClef(clefObject.ConcertClefStyleId,clefDictionary);
newClefNeeded = true;
} else {
writeNewClefArray[currentStaff] = 'false';
}
while (v>0)
{
if (wholeBarRestTrackerArray[currentStaff] = true) // if this measure is wholebarrest
{
kernToken = 'rr' & '\t';
wholeBarRestTrackerArray[currentStaff] = false ;
} else {
result = NoteRestAtBarPosVoice(s.NthBar(currentInternalBarNumber),e,v);
kernToken = '' ;
if (result != null)
{
humDuration = ConvertDuration(result,durationDictionary);
if (result.NoteCount = 0) // is NoteRest is rest
{
if (result.Hidden = true)
{
kernToken = humDuration & 'ryy' & '\t'; //yy means invisible
} else {
kernToken = humDuration & 'r' & '\t';
}
} else {// if NoteRest is note or chord
for each n in result // for each note of chord
{
continueOrEndTieOutputArray = ContinueOrEndTie(n,ongoingTiesArray,currentStaff,v,pitchDictionary);
humEndToken = continueOrEndTieOutputArray[0];
articulationArray = ConvertArticulation(n,articulationDictionary);
humArticulation = articulationArray[0];
kernToken = kernToken & BeginTie(n,ongoingTiesArray,currentStaff,v,pitchDictionary) & humDuration & ConvertPitch(n,pitchDictionary) & humArticulation & humEndToken & ' ';
ongoingTiesArray = AddToOngoiontTiesArray(n,ongoingTiesArray,currentStaff,v,pitchDictionary);
tieUniqueCode = continueOrEndTieOutputArray[1] ;
removeTieUniqueCode = continueOrEndTieOutputArray[2];
if (removeTieUniqueCode = true)
{
ongoingTiesArray = RemoveFromSparseArray(tieUniqueCode,ongoingTiesArray);
}
}
kernToken = utils.TrimBlanks(kernToken) & '\t'; // Remove that last space that was added in the loop above AND ADD tab
if (articulationArray[1] = true) // if openArticualationReferenceNeeded
{
openArticulationReferenceNeeded = true;
}
if (articulationArray[2] = true) // if closedArticualationReferenceNeeded
{
closedArticulationReferenceNeeded = true;
}
}
} else { // if there is no event at that position in that bar, null token is needed
kernToken = '.' & '\t';
}
}
humLine = humLine & kernToken;
v = v-1;
}
currentStaff = currentStaff-1;
}
// if addNewKeySignature is true, add a new key signature before adding Humdrum Line
if (addNewKeySignature = true)
{
humNewKeySignatureLine = '';
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
v = currentNumberOfVoices[currentStaff] ;
while (v>0)
{
if (currentClef[currentStaff] = '*clefX' ) // if clef is percussion, no key sig
{
humNewKeySignatureLine = humNewKeySignatureLine & '*' & '\t';
} else {
humNewKeySignatureLine = humNewKeySignatureLine & ConvertKeySignature(currentKeySignature,keySignatureDictionary) & '\t';
}
v = v-1;
}
currentStaff = currentStaff-1;
}
humNewKeySignatureLine = TrimTabsLeading(humNewKeySignatureLine,false);
//Add New Key Signature Line to krn file
Sibelius.AppendLineToFile(fname,humNewKeySignatureLine);
addNewKeySignature = false ; // set back flag to false
}
// check if a staff has a new clef, and if so, add new Clef Line befor main humLine
if (newClefNeeded = true) // if something was added to the array, this means we need a new clef somewhere
{
currentStaff = thisScore.StaffCount;
humNewClefLine = '';
while (currentStaff>0) // for each staff, but reverse order...
{
s = thisScore.NthStaff(currentStaff);
v = currentNumberOfVoices[currentStaff];
while (v>0)
{
if (writeNewClefArray[currentStaff] != 'false')
{
humNewClefLine = humNewClefLine & writeNewClefArray[currentStaff] & '\t';
} else {
humNewClefLine = humNewClefLine & '\*' & '\t';
}
v = v-1;
}
currentStaff = currentStaff-1;
}
humNewClefLine = TrimTabsLeading(humNewClefLine,false);
//Add Humdrum Line to krn file
Sibelius.AppendLineToFile(fname,humNewClefLine);
}
humLine = TrimTabsLeading(humLine,false);
//Add Humdrum Line to krn file
Sibelius.AppendLineToFile(fname,humLine);
line = line+1;
}
/* This bit is a train wreck, because of the different way in which Sib and Humdrum handle barlines.
The complicated bit is the end-repeat-start-repeat combinaision.
I need to look at special barlines always for the current and next barline
*/
specialBarline = false;
thisMeasureBarline = '';
x=1;
for each SpecialBarline sbl in b
{
specialBarline = true;
if (b.BarNumber = sysStaff.BarCount)
{
thisMeasureBarline = '=' & ConvertSpecialBarlineCode(sbl.BarlineInternalType,specialBarlineDictionary); // if final barline, no bar number
} else {
if (startsWithRepeat = true)
{
startsWithRepeat = false;
specialBarline = false;
} else {
thisMeasureBarline = '=' & nextExternalBarNumber & ConvertSpecialBarlineCode(sbl.BarlineInternalType,specialBarlineDictionary) & CreateTokenIfBarStartsWithStartRepeat(nextInternalBarNumber);
thisMeasureBarline = utils.Replace(thisMeasureBarline,'!!','!',false); // when there is end repeat followed by start repeat, this removes the superfluous !
if (CreateTokenIfBarStartsWithStartRepeat(nextInternalBarNumber) != '')
{
startsWithRepeat = true;
} else {
startsWithRepeat = false;
}
}
}
x=x+1;
}
if (specialBarline = false)
{
if (b.BarNumber = sysStaff.BarCount)
{
thisMeasureBarline = '=' ; //in the rare case where last measure does not have a special barline, make sure to not add a bar number
} else {
thisMeasureBarline = '=' & nextExternalBarNumber & CreateTokenIfBarStartsWithStartRepeat(nextInternalBarNumber) ;
thisMeasureBarline = utils.Replace(thisMeasureBarline,'!!','!',false);
if (CreateTokenIfBarStartsWithStartRepeat(nextInternalBarNumber) != '')
{
startsWithRepeat = true;
} else {
startsWithRepeat = false;
}
}
}
// Create a humline with the barline at the end of this measure
humBarlineLine = '';
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
v = currentNumberOfVoices[currentStaff] ;
while (v>0)
{
humBarlineLine = humBarlineLine & thisMeasureBarline & '\t';
v = v-1;
}
currentStaff = currentStaff-1;
}
humBarlineLine = TrimTabsLeading(humBarlineLine,false);
//Add Humdrum Barline Line to krn file
Sibelius.AppendLineToFile(fname,humBarlineLine);
}
// bring the number of spines to 1 per staff before spine-terminator
keepLooping = true;
writeHumSpinePathLineToFile = false;
while (keepLooping=true)
{
keepLoopingArray = CreateSparseArray();
keepLooping = false;
humSpinePathLine = '';
currentStaff = thisScore.StaffCount;
while (currentStaff>0) //for each staff
{
if (currentNumberOfVoices[currentStaff] = 1)
{
for x = 0 to currentNumberOfVoices[currentStaff]
{
humSpinePathLine = humSpinePathLine & '*' & '\t' ;
}
} else { //if we need to remove a spine
writeHumSpinePathLineToFile = true;
humSpinePathLine = humSpinePathLine & CreateSpinePathIndicators(currentNumberOfVoices[currentStaff],currentNumberOfVoices[currentStaff]-1) & '\t' ;
currentNumberOfVoices[currentStaff] = currentNumberOfVoices[currentStaff]-1 ;
}
if (currentNumberOfVoices[currentStaff] != 1)
{
keepLoopingArray[keepLoopingArray.Length] = 'true';
} else {
keepLoopingArray[keepLoopingArray.Length] = 'false';
}
currentStaff=currentStaff-1;
}
for each i in keepLoopingArray
{
if (i='true')
{
keepLooping = true;
}
}
if (writeHumSpinePathLineToFile = true)
{
humSpinePathLine = TrimTabsLeading(humSpinePathLine,false);
Sibelius.AppendLineToFile(fname,humSpinePathLine);
}
}
// create one Humdrum spine-path terminator Line
humSpinePathTerminatorLine = '';
currentStaff = thisScore.StaffCount;
while (currentStaff>0)
{
s = thisScore.NthStaff(currentStaff);
humSpinePathTerminatorLine = humSpinePathTerminatorLine & '*-' & '\t';
currentStaff = currentStaff-1;
}
humSpinePathTerminatorLine = TrimTabsLeading(humSpinePathTerminatorLine,false);
//Add Humdrum spine-path terminator Line to krn file
Sibelius.AppendLineToFile(fname,humSpinePathTerminatorLine);
// for progress bar
Sibelius.DestroyProgressDialog();
thisScore.Redraw = True;
// Add !!!RDF**kern: o = harmonic/open if needed
if (openArticulationReferenceNeeded = true)
{
Sibelius.AppendLineToFile(fname,'!!!RDF**kern: o = harmonic/open');
}
// Add !!!RDF**kern: O = plus/closed if needed
if (closedArticulationReferenceNeeded = true)
{
Sibelius.AppendLineToFile(fname,'!!!RDF**kern: O = plus/closed');
}
//Add final line that doesn't end with new line car, hence AppendTextFile and not AppendLineToFile
Sibelius.AppendTextFile(fname,'!! Converted from Sibelius with sib2hum plugin');
//Message Box at the end to know it's done is nice
Sibelius.MessageBox('Done! The file is available here; ' & fname);
}"
ConvertInstrumentCode "(InitialStyleId,instrumentDictionary) {
/* Instruments that are not currently mapped to a Humdrum equivalent will become **I?? for the moment */
if (instrumentDictionary[InitialStyleId] = '')
{
return '*I??';
} else {
return instrumentDictionary[InitialStyleId];
}}"
ConvertClef "(clefStyle,clefDictionary) {
/* Convert Sib clefs to Humdrum format using the dictionary createde with CreateClefDictionary() ;
I'm ignoring all TAB clefs for the moment; they will become *clef?? */
if (clefDictionary[clefStyle] = '')
{
return '*clef??';
} else {
return clefDictionary[clefStyle];
}
}"
CreateInstrumentDictionary "() {
instrumentDictionary = CreateDictionary(
'instrument.brass.cornet.a','*Icornt',
'instrument.brass.cornet.bflat','*Icornt',
'instrument.brass.cornet.soprano.eflat','*Icornt',
'instrument.brass.flugelhorn','*Iflugh',
'instrument.brass.horn.a.nokeysig','*Icor',
'instrument.brass.horn.alto.aflat.nokeysig','*Icor',
'instrument.brass.horn.alto.eflat','*Icor',
'instrument.brass.horn.alto.f','*Icor',