-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDolet8.plg
executable file
·26519 lines (25105 loc) · 1.44 MB
/
Dolet8.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 "() {
// Dolet 8 for Sibelius
// Copyright © 2003-2023 MakeMusic, Inc.
// If you see are seeing this, you are probably
// trying to run the Dolet 8 for Sibelius plug-in
// on an older version of Sibelius. This plug-in
// requires Sibelius 5.1 or later. You may use the
// Dolet 1 plug-in with Sibelius 2.1, Sibelius 3.1,
// and Sibelius 4 to export MusicXML 1.0 files.
// Check for Sibelius 5 because LocalizeData depends
// on Sibelius 5 functions. During Run, we will check
// for Sibelius 2019.5 so we can display a message box
// instead of just disappearing from the menu.
if (Sibelius.ProgramVersion >= 5000) {
// Localize first to get correct plug-in menu name
LocalizeData();
AddToPluginsMenu(_PluginMenuName, 'Run');
}
}"
Run "() {
// Dolet 8 for Sibelius
// Copyright © 2003-2023 MakeMusic, Inc.
// The top level method for the MusicXML exporter.
// Sibelius 2019.5 is the minimum version for the
// Dolet 8 for Sibelius plug-in.
if (Sibelius.ProgramVersion < 19500) {
Sibelius.MessageBox(_SibVersionTooEarly);
return False;
}
if (Sibelius.ScoreCount = 0)
{
Sibelius.MessageBox(_NoScore);
return False;
}
oMyScore = Sibelius.ActiveScore;
// Use Sibelius standard features to select the file
// name, versus the custom stuff we had in earlier versions.
oTempFile = Sibelius.GetFile(oMyScore.FileName);
sPathName = oTempFile.Path;
sFileName = oTempFile.NameNoPath;
oFile = Sibelius.SelectFileToSave(_PluginMenuName, sFileName, sPathName,
'musicxml', 'MUSICXML', 'MusicXML File');
fOK = False;
if (oFile != null) {
// @todo On Windows (at least), the file data returned
// will be corrupted if you try to save the XML in a parent
// directory, and the file name is shorter than the parent
// directory name. Other scenarios may also cause this
// same problem. For now we are waiting for a Sibelius fix.
FileName = '' & oFile.NameWithExt;
if (Length(FileName) > 0) {
fOK = True;
}
}
if (fOK) {
fOK = ExportXML();
return fOK;
} else {
return False;
}
}"
FileName
InitializeXML "(ThisScore) {
// Dolet 8 for Sibelius
// Copyright © 2003-2023 MakeMusic, Inc.
sData = '<?xml version=\'1.0\' encoding=\'UTF-16\' standalone=\'no\'?>';
WriteXML(sData, 0);
sData = '<!DOCTYPE score-partwise PUBLIC';
// We are using MusicXML 4.0 features now.
sData = sData & ' \'-//Recordare//DTD MusicXML 4.0 Partwise//EN\'';
sData = sData & ' \'http://www.musicxml.org/dtds/partwise.dtd\'>';
WriteXML(sData, 0);
WriteXML('<score-partwise version=\'4.0\'>', 0);
// Global fields have already done the ToXmlText conversion,
// so do not do it again here.
// Put Sibelius title in movement-title. Take the printed text
// over the score info for metadata; it is more likely to be accurate.
if (Length(Title) = 0) {
Title = ToXmlText(ConvertFormattedText(ThisScore.Title));
}
if (Length(Title) > 0) {
WriteXML('<movement-title>' & Title & '</movement-title>', 1);
}
WriteXML('<identification>', 1);
if (Length(Composer) = 0) {
Composer = ToXmlText(ConvertFormattedText(ThisScore.Composer));
}
if (Length(Composer) > 0){
sData = '<creator type=\'composer\'>' & Composer & '</creator>';
WriteXML(sData, 2);
}
if (Length(Lyricist) = 0) {
Lyricist = ToXmlText(ConvertFormattedText(ThisScore.Lyricist));
}
if (Length(Lyricist) > 0){
sData = '<creator type=\'lyricist\'>' & Lyricist & '</creator>';
WriteXML(sData, 2);
}
sArranger = ToXmlText(ConvertFormattedText(ThisScore.Arranger));
if (Length(sArranger) > 0){
sData = '<creator type=\'arranger\'>' & sArranger & '</creator>';
WriteXML(sData, 2);
}
sArtist = ToXmlText(ConvertFormattedText(ThisScore.Artist));
if (Length(sArtist) > 0){
sData = '<creator type=\'artist\'>' & sArtist & '</creator>';
WriteXML(sData, 2);
}
if (Length(Copyright) = 0) {
Copyright = ToXmlText(ConvertFormattedText(ThisScore.Copyright));
}
if (Length(Copyright) > 0){
sData = '<rights>' & Copyright & '</rights>';
WriteXML(sData, 2);
}
// We always have an encoding to write out.
WriteXML('<encoding>', 2);
sData = '<software>' & VersionString() & '</software>';
WriteXML(sData, 3);
WriteXML('<software>Dolet 8.3 for Sibelius</software>', 3);
oDate = Sibelius.CurrentDate;
sIsoDate = IsoDate(oDate);
WriteXML('<encoding-date>' & sIsoDate & '</encoding-date>', 3);
// Indicate that some features are or are not supported.
// For accidentals, support was added in Sibelius 7.1.3.
WriteXML('<supports element=\'accidental\' type=\'yes\'/>', 3);
WriteXML('<supports element=\'transpose\' type=\'yes\'/>', 3);
// Starting in Sibelius 6, we can export all the system breaks
// and page breaks whether or not the layout if locked.
WriteXML('<supports attribute=\'new-page\' element=\'print\'' &
' type=\'yes\' value=\'yes\'/>', 3);
WriteXML('<supports attribute=\'new-system\' element=\'print\'' &
' type=\'yes\' value=\'yes\'/>', 3);
WriteXML('</encoding>', 2);
// Finish the identification
WriteXML('</identification>', 1);
// Now write out the defaults for scaling and page layout.
InitializeDefaults(ThisScore);
// Now handle the credits from page-aligned system text items.
InitializeCredits();
// And we are done here. The part-list is next.
}"
InitializeParts "(ThisScore) {
// Dolet 8 for Sibelius
// Copyright © 2003-2023 MakeMusic, Inc.
sLastInstrument = '';
nInstruments = 0;
nStaves = 0; // Staves left in current instrument
// Sibelius 6.1 lets us know if full, short, or no names are printed
// on first system, subsequent systems, and starts of sections. For
// now we just check for no names and ignore starts of sections.
sPrintObjectName = '';
sPrintObjectAbbr = '';
// Sibelius 6.1 also lets us access brackets and barlines, used to
// create MusicXML part-group elements. InitializeGroups returns
// a Sibelius 6 sparse array of dictionary objects, each dictionary
// corresponding to a part-group.
oGroups = null;
oHasPartGroupStart = null;
oHasPartGroupStop = null;
oGroupNumber = null;
oRules = ThisScore.EngravingRules;
if (oRules.InstrumentNamesFirstSystem = InstrumentNamesNone) {
sPrintObjectName = ' print-object=\'no\'';
}
if (oRules.InstrumentNamesSubsequentSystems = InstrumentNamesNone) {
sPrintObjectAbbr = ' print-object=\'no\'';
}
oGroups = InitializeGroups(ThisScore);
oHasPartGroupStart = CreateSparseArray();
oHasPartGroupStop = CreateSparseArray();
oGroupNumber = CreateSparseArray();
for i = 0 to ThisScore.StaffCount {
oHasPartGroupStart[i] = False;
oHasPartGroupStop[i] = False;
oGroupNumber[i] = -1;
}
for each oGroup in oGroups {
oHasPartGroupStart[oGroup.TopStaff] = True;
oHasPartGroupStop[oGroup.BottomStaff] = True;
}
WriteXML('<part-list>', 1);
for each oStaff in ThisScore {
if (oStaff.Type = 'Stave') {
if (nStaves = 0) {
nInstruments = nInstruments + 1;
// Check for part-group starts
for i = 0 to oStaff.NumStavesInSameInstrument {
if (oHasPartGroupStart[oStaff.StaffNum + i]) {
nGroupIndex = 0;
for each oGroup in oGroups {
if ((oStaff.StaffNum + i) = oGroup.TopStaff) {
fNoGroupNum = True;
nGroup = 1;
while (fNoGroupNum and (nGroup <= ThisScore.StaffCount)) {
if (oGroupNumber[nGroup] < 0) {
oGroupNumber[nGroup] = nGroupIndex;
// We now have everything we need to write this group.
// There can be multiple groups per part.
WritePartGroup(oGroup, nGroup, 'start');
fNoGroupNum = False;
}
nGroup = nGroup + 1;
}
}
nGroupIndex = nGroupIndex + 1;
}
}
}
WriteXML('<score-part id=\'P' & nInstruments & '\'>', 2);
WritePartNameAndAbbr(oStaff, sPrintObjectName, sPrintObjectAbbr);
// By default, this part does not end a group.
PartEndGroup[nInstruments] = 0;
// Is this a part or a score?
sGroup = 'score';
if (ThisScore.IsDynamicPart) {
sGroup = 'parts';
}
WriteXML('<group>' & sGroup & '</group>', 3);
// Now set up the score instrument with as much information as we can.
InitializeScoreInstruments(ThisScore, oStaff, nInstruments);
WriteXML('</score-part>', 2);
// Check for part-group stops
for i = 0 to oStaff.NumStavesInSameInstrument {
if (oHasPartGroupStop[oStaff.StaffNum + i]) {
nGroupIndex = 0;
for each oGroup in oGroups {
if ((oStaff.StaffNum + i) = oGroup.BottomStaff) {
fNoGroupNum = True;
nGroup = 1;
while (fNoGroupNum and (nGroup <= ThisScore.StaffCount)) {
if (oGroupNumber[nGroup] = nGroupIndex) {
// We now have everything we need to write this group.
// There can be multiple groups per part.
WritePartGroup(oGroup, nGroup, 'stop');
fNoGroupNum = False;
oGroupNumber[nGroup] = -1;
// Save away that this part ends a group, as that
// affects additional spacing below the part.
// Cannot use booleans in array, so use integers.
PartEndGroup[nInstruments] = 1;
}
nGroup = nGroup + 1;
}
}
nGroupIndex = nGroupIndex + 1;
}
}
}
nStaves = oStaff.NumStavesInSameInstrument - 1;
} else {
nStaves = nStaves - 1;
}
}
}
WriteXML('</part-list>', 1);
}"
FinalizeXML "() {
// Dolet 8 for Sibelius
// Copyright © 2003-2023 MakeMusic, Inc.
WriteXML('</score-partwise>', 0);
fOK = Sibelius.AppendTextFile(FileName, XmlText, True);
if (not fOK) {
trace('Error writing \'</score-partwise>\' to line ' & XmlLineNumber &
'. Your MusicXML file may be invalid.');
}
}"
WriteXML "(XMLString, Tabs) {
// Dolet 8 for Sibelius
// Copyright © 2003-2023 MakeMusic, Inc.
sTabs = '';
// Watch out for idiosyncratic for syntax (stops before last value)
if (Tabs > 0) {
for i = 0 to Tabs {
sTabs = sTabs & ' ';
}
}
// Buffer our writes to make things faster.
XmlText = XmlText & sTabs & XMLString & XmlCrLf;
XmlLineNumber = XmlLineNumber + 1;
// It is possible for the append to fail. This seems to
// happen when there is a lot of memory use or in unusual
// disk conditions. We used to retry up to 10 times to
// write the line, but now the next line will do the retry
// since we do not reset the buffer if we fail.
if (Length(XmlText) > XmlTextBufferSize) {
fOK = Sibelius.AppendTextFile(FileName, XmlText, True);
if (fOK) {
XmlText = '';
return True;
} else {
// If the append failed, send a message to the trace window
// and return False. We want the intended line number in the
// error message, which is why we increment it before appending.
// Note that Sibelius has confirmed that there is a bug in
// AppendLineToFile causing these errors, and that there is
// no available workaround for our plug-in. Retrying and
// reporting is the best we can do. We do not know if the
// bug is also present in AppendTextFile or if it may have
// been fixed in later Sibelius releases.
// @todo Internationalize this message
trace('Error writing \'' & XMLString & '\' to line ' & XmlLineNumber &
'. Your MusicXML file may be invalid.');
return False;
}
}
return True;
}"
WriteStaff "(ThisScore, MyStaff, StaffNumber, PartNumber) {
// Dolet 8 for Sibelius
// Copyright © 2003-2023 MakeMusic, Inc.
if (ShowTrace) {
trace('Writing part P' & PartNumber);
}
// Write a comment separator before the first part
if (PartNumber = 1) {
WriteXML('<!--=========================================================-->', 0);
}
Staves = 0 + MyStaff.NumStavesInSameInstrument;
nBars = MyStaff.BarCount;
WriteXML('<part id=\'P' & PartNumber & '\'>', 1);
nHighStaff = StaffNumber + Staves;
// Reset clefs
TabClef = -1;
PercussionClef = -1;
PercStaffLines = 5;
// Reset transpose data
LastTransposeChromatic = 0;
LastTransposeDiatonic = 0;
LastTransposeOctaveChange = 0;
TransposeChromatic = 0;
TransposeDiatonic = 0;
TransposeOctaveChange = 0;
TransposePartClefStyleId = '';
WrittenDiatonicPitchOK = True;
// Reset key data
LastKeyFifths = 0;
LastKeyMode = '';
KeyFifths = 0;
KeyMode = '';
// Reset per-staff arrays
CurrentPartInstrument[0] = '';
for i = 1 to Staves + 1 {
HiddenStaff[i] = False;
StaffDetailsPrintObject[i] = '';
StaffLines[i] = 5;
LastMeasureRepeat[i] = 0;
MeasureRepeatStopBars[i] = 0;
LastClefSign[i] = '';
LastClefLine[i] = 0;
LastClefOctaveChange[i] = 0;
LastClefBar[i] = 0;
LastClefPosition[i] = 0;
CurrentPartInstrument[i] = '';
}
// Reset per-voice arrays
PreviousEntryDuration[0] = 0;
PreviousEntryBeam[0] = NoBeam;
PreviousPrimaryBeamOnly[0] = False;
PreviousGraceEntryDuration[0] = 0;
PreviousGraceEntryBeam[0] = NoBeam;
PreviousGracePrimaryBeamOnly[0] = False;
nMaxVoice = (Staves * 4) + 1;
for i = 1 to nMaxVoice {
ArpeggioBars[i] = 0;
ArpeggioPositions[i] = 0;
DoubleTremolos[i] = 0;
TremoloType[i] = '';
NonArpeggiateBars[i] = 0;
NonArpeggiatePositions[i] = 0;
PreviousEntryDuration[i] = 0;
PreviousEntryBeam[i] = NoBeam;
PreviousPrimaryBeamOnly[i] = False;
PreviousGraceEntryDuration[i] = 0;
PreviousGraceEntryBeam[i] = NoBeam;
PreviousGracePrimaryBeamOnly[i] = False;
SlideStop[i] = 0;
VerseMax[i] = 0;
AnyVisibleLyric[i] = False;
Verse1Text[i] = '';
Verse2Text[i] = '';
Verse3Text[i] = '';
Verse4Text[i] = '';
Verse5Text[i] = '';
ChorusText[i] = '';
AboveText[i] = '';
Verse1PrevType[i] = EndOfWord;
Verse2PrevType[i] = EndOfWord;
Verse3PrevType[i] = EndOfWord;
Verse4PrevType[i] = EndOfWord;
Verse5PrevType[i] = EndOfWord;
ChorusPrevType[i] = EndOfWord;
AbovePrevType[i] = EndOfWord;
}
// Reset 16-element slur and direction arrays.
for i = 1 to 17 {
SlurStopBars[i] = 0;
BracketStopBars[i] = 0;
DashesStopBars[i] = 0;
OctaveStopBars[i] = 0;
WedgeStopBars[i] = 0;
GlissandoStopBars[i] = 0;
TrillStopBars[i] = 0;
TrillStopDx[i] = 0;
SlideNumbers[i] = 0;
}
PedalStopBar = 0;
BracketStops = 0;
DashesStops = 0;
OctaveStops = 0;
WedgeStops = 0;
PedalStops = 0;
DirectionStops = 0;
SlurCounts = 0;
GlissandoCounts = 0;
TrillCounts = 0;
// Rest tie pitch arrays
for i = 0 to 128 {
TieStopNextBar[i] = 0;
TieStopThisBar[i] = 0;
TieStartVoice[i] = 0;
}
// Loop through each bar. Watch out for
// idiosyncratic for syntax.
for i = 1 to nBars + 1 {
BarCounter = i;
nOldBreakType = MiddleOfPage;
oBarRef = ThisScore.NthStaff(StaffNumber).NthBar(i);
if (i > 1) {
oBarRef = oStaff.NthBar(i - 1);
}
InitializeMeasure(ThisScore, i, StaffNumber, oBarRef);
fCouldBeFirstSystemStaff = False;
if (not SystemStaffDone[i]) {
fCouldBeFirstSystemStaff = True;
}
for j = StaffNumber to nHighStaff {
oStaff = ThisScore.NthStaff(j);
oBar = oStaff.NthBar(i);
nStaff = j - StaffNumber + 1;
if (Staves = 1) {
WriteMeasure(ThisScore, oBar, i, 0, PartNumber);
} else {
WriteMeasure(ThisScore, oBar, i, nStaff, PartNumber);
}
}
// In order to get endings to appear on the first system
// staff, temporarily tweak the global so it looks like
// we are not done yet.
if (fCouldBeFirstSystemStaff and SystemStaffDone[i]) {
SystemStaffDone[i] = False;
FinalizeMeasure(ThisScore, i, StaffNumber);
SystemStaffDone[i] = True;
} else {
FinalizeMeasure(ThisScore, i, StaffNumber);
}
ProgressCurrent = ProgressCurrent + Staves;
// Localized progress dialog. Tradeoff between export speed and feedback.
if ((i % 20) = 1) {
sProgressPart = '' & PartNumber;
sProgressBar = '' & i;
sProgress = FormatMessage2(_Progress, '&', sProgressPart, sProgressBar);
UserContinue =
Sibelius.UpdateProgressDialog(ProgressCurrent, sProgress);
// If the user cancelled, close off this part and stop.
if (UserContinue = False) {
WriteXML('</part>', 1);
return Staves;
}
}
}
WriteXML('</part>', 1);
// Write a comment separator after each part
WriteXML('<!--=========================================================-->', 0);
return Staves;
}"
InitializeMeasure "(ThisScore, MeasureNumber, StaffNumber, BarRef) {
// Dolet 8 for Sibelius
// Copyright © 2003-2023 MakeMusic, Inc.
oStaff = ThisScore.NthStaff(StaffNumber);
oSystemStaff = ThisScore.SystemStaff;
nMeasureNumber = 0 + MeasureNumber;
oBar = oStaff.NthBar(nMeasureNumber);
nMaxVoice = (Staves * 4) + 1;
for i = 0 to nMaxVoice {
PreviousEntryDuration[i] = 0;
PreviousEntryBeam[i] = NoBeam;
PreviousPrimaryBeamOnly[i] = False;
PreviousGraceEntryDuration[i] = 0;
PreviousGraceEntryBeam[i] = NoBeam;
PreviousGracePrimaryBeamOnly[i] = False;
LastTupletPosition[i] = -1;
}
// Check the hidden staff settings now for when we
// write staff-details elements later.
SetStaffDetailsPrintObject(ThisScore, MeasureNumber, StaffNumber);
// If this is not the first measure, put a comment
// separator before it for easier reading of the
// resulting MusicXML file.
if (nMeasureNumber > 1) {
WriteXML('<!--=========================================================-->', 0);
}
// Use measure numbers and implicit values that were
// initialized earlier.
sMeasureNumber = '' & MeasureNumbers[MeasureNumber];
sImplicit = '' & MeasureImplicit[MeasureNumber];
sMeasureText = '' & MeasureTexts[MeasureNumber];
nMultiRest = MultiRest[MeasureNumber];
if (Length(sImplicit) > 0) {
sData = '<measure number=\'' & sMeasureNumber & '\' implicit=\'' & sImplicit & '\'>';
} else {
if (sMeasureNumber = sMeasureText) {
sData = '<measure number=\'' & sMeasureNumber & '\'>';
} else {
sData = '<measure number=\'' & sMeasureNumber & '\' text=\'' & sMeasureText & '\'>';
}
}
WriteXML(sData, 2);
// Handle break from previous meaasure.
nBreakType = BarRef.BreakType;
nBlankPages = BarRef.NumBlankPages;
if (MeasureNumber = 1) {
nBlankPages = BarRef.NumBlankPagesBefore;
if (nBlankPages > 0) {
nBreakType = SpecialPageBreak;
} else {
nBreakType = MiddleOfPage;
}
}
// Get instrument type to check for tab staff etc.
// Look for the first visible, non-0 line staff, rather
// than simply the first staff. We used to do this
// before writing attributes, but now we use the
// instrument type to write processing instructions
// about vertical justification in the first measure.
oInstrumentType = null;
nInstrumentTypeCategory = 0;
oTypeList = ThisScore.InstrumentTypes;
nSingleStaff = -1;
for i = 0 to Staves {
oStaff = ThisScore.NthStaff(StaffNumber + i);
// Loop through the list.
oStaffInstrumentType = oStaff.InitialInstrumentType;
// See if this staff is visible. Do this here to make
// it easier to limit this check to Sibelius 6 only.
fVisible = True;
if (HiddenStaff[i + 1]) {
fVisible = False;
}
if (i = 0) {
oInstrumentType = oStaffInstrumentType;
if ((oInstrumentType.NumStaveLines > 0) and fVisible) {
nSingleStaff = 0;
}
} else {
if (nSingleStaff < 0) {
if ((oStaffInstrumentType.NumStaveLines > 0) and fVisible) {
nSingleStaff = i;
oInstrumentType = oStaffInstrumentType;
}
}
}
}
if (nSingleStaff < 0) {
nSingleStaff = 0;
}
oStaff = ThisScore.NthStaff(StaffNumber + nSingleStaff);
nInstrumentTypeCategory = oInstrumentType.Category;
// Because we need to handle endings are barlines together,
// we need to do this outside of the regular system item handling.
// Do not print endings if we are not on a system staff.
// Need to do up here because we are also checking for extra
// spacing on system object staves.
fSystemStaff = CheckSystemStaff(ThisScore, MeasureNumber, StaffNumber, 1);
// If we are exporting system layout, we will change
// this to a non-empty print element.
sEnd = '/>';
fPrintSystemLayout = False;
fPrintStaffLayout = False;
fPrintMeasureLayout = False;
oSetup = null;
oRules = null;
if (nMeasureNumber = 1) {
oSetup = ThisScore.DocumentSetup;
oRules = ThisScore.EngravingRules;
fSomeStaffVisible = False;
for i = 0 to Staves {
if (not HiddenStaff[i + 1]) {
fSomeStaffVisible = True;
}
}
if (fSomeStaffVisible) {
if ((oSetup.FirstPageHasUniqueVerticalStaveMargins) or
(oRules.InstrumentNamesFirstSystem !=
oRules.InstrumentNamesSubsequentSystems)) {
// As stated in MusicXML 4.0 spec, only write system-layout
// in the part containing the first visible staff.
if ((FirstVisibleStaffForFirstMeasure >= StaffNumber) and
(FirstVisibleStaffForFirstMeasure < (StaffNumber + Staves))) {
fPrintSystemLayout = True;
sEnd = '' & '>';
}
}
}
if (oInstrumentType != null) {
if (Staves > 1) {
fPrintStaffLayout = True;
sEnd = '' & '>';
}
if (oInstrumentType.IsVocal and
(oRules.ExtraSpacesBelowVocalStaves != 0)) {
fPrintStaffLayout = True;
sEnd = '' & '>';
}
}
if (fSystemStaff and (oRules.ExtraSpacesAboveForSystemObjectPositions != 0)) {
fPrintStaffLayout = True;
sEnd = '' & '>';
}
if ((oRules.ExtraSpaceBetweenGroupsOfStaves != 0) and
(PartEndGroup[PartCounter] = 1)) {
fPrintStaffLayout = True;
sEnd = '' & '>';
}
}
// Check for Bar.GapBefore to see if we can write a
// measure-layout element. There is also a Bar.GapAfter
// but do not see how that can be set in the Sibelius
// GUI (last checked in 7.5). Could access it here via
// BarRef.GapAfter if needed, making sure not to double
// count on the first measure or other bars that begin a
// system.
if (oBar.GapBefore > 0) {
fPrintMeasureLayout = True;
sEnd = '' & '>';
}
fPrintStarted = False;
switch (nBreakType) {
case (EndOfPage) {
WriteXML('<print new-page=\'yes\'' & sEnd, 3);
fPrintStarted = True;
}
case (EndOfSystem) {
WriteXML('<print new-system=\'yes\'' & sEnd, 3);
fPrintStarted = True;
}
case (EndOfSystemOrPage) {
if (MeasureNewPage[MeasureNumber]) {
WriteXML('<print new-page=\'yes\'' & sEnd, 3);
fPrintStarted = True;
} else {
WriteXML('<print new-system=\'yes\'' & sEnd, 3);
fPrintStarted = True;
}
}
case (MiddleOfSystem) {
// This lets applications import systems better with locked format.
WriteXML('<print new-system=\'no\'' & sEnd, 3);
fPrintStarted = True;
}
case (SpecialPageBreak) {
// We can handle special page breaks that specify the
// number of pages, but since we do not know which
// page number we are on, we cannot handle the options
// for music restarting on left or right pages, except
// to create the page break.
if (nBlankPages > 0) {
WriteXML('<print new-page=\'yes\' blank-page=\'' & nBlankPages & '\'' & sEnd, 3);
fPrintStarted = True;
} else {
// We should not have a special page break in bar 1
// without blank pages first, but check just in case
// that changes in future versions of Sibelius.
if (MeasureNumber > 1) {
WriteXML('<print new-page=\'yes\'' & sEnd, 3);
fPrintStarted = True;
}
}
}
default {
// We can tell the starts of new systems and new pages
// without formatting being locked.
if (MeasureNewPage[MeasureNumber]) {
WriteXML('<print new-page=\'yes\'' & sEnd, 3);
fPrintStarted = True;
} else {
if (MeasureNewSystem[MeasureNumber]) {
WriteXML('<print new-system=\'yes\'' & sEnd, 3);
fPrintStarted = True;
}
}
}
}
// Now print the system layout if needed.
if (fPrintSystemLayout) {
if (not fPrintStarted) {
WriteXML('<print>', 3);
fPrintStarted = True;
}
WriteXML('<system-layout>', 4);
// If the system margins ar different, export them.
if (oRules.InstrumentNamesFirstSystem !=
oRules.InstrumentNamesSubsequentSystems) {
fExportSystemMargins = True;
nWhichLeft = 0;
switch (oRules.InstrumentNamesFirstSystem) {
case (InstrumentNamesFull) {
nWhichLeft = oSetup.StaffLeftMarginFullNames;
}
case (InstrumentNamesShort) {
nWhichLeft = oSetup.StaffLeftMarginShortNames;
}
case (InstrumentNamesNone) {
nWhichLeft = oSetup.StaffLeftMarginNoNames;
}
default {
fExportSystemMargins = False;
}
}
nLeftMargin = 40.0 * nWhichLeft / ThisScore.StaffHeight;
// Export the system layout for the first system.
if (fExportSystemMargins) {
WriteXML('<system-margins>', 5);
WriteXML('<left-margin>' & nLeftMargin & '</left-margin>', 6);
WriteXML('<right-margin>0</right-margin>', 6);
WriteXML('</system-margins>', 5);
}
}
// If the top system distance is different, export it.
if (oSetup.FirstPageHasUniqueVerticalStaveMargins) {
nTopSysDist = 40.0 * oSetup.AboveTopStaveGap / ThisScore.StaffHeight;
WriteXML('<top-system-distance>' & nTopSysDist & '</top-system-distance>', 5);
} else {
if (InitialTopSystemDistanceAddition > 0) {
nTopSysDist = 40.0 * oSetup.AboveTopStaveGap / ThisScore.StaffHeight;
nTopSysDist = nTopSysDist + InitialTopSystemDistanceAddition;
WriteXML('<top-system-distance>' & nTopSysDist & '</top-system-distance>', 5);
}
}
WriteXML('</system-layout>', 4);
}
if (fPrintStaffLayout) {
// @todo Export additional gaps for system objects, vocal staff,
// and groups if feasible. System objects may be the one that works
// since it is related to the current staff, not to the previous staff
// which may or may not be visible.
// Currently we are not exporting system and staff spacing default
// due to problems with using them without vertical justification
// values. Instead, we export processing instructions within an empty
// staff-layout element as convenient. If we do add them to the
// MusicXML export using elements and attributes, then the overrides
// would go here, though the logic would get more complex.
if (not fPrintStarted) {
WriteXML('<print>', 3);
fPrintStarted = True;
}
WriteXML('<staff-layout>', 4);
if (Staves > 1) {
if (oInstrumentType.HasBracket) {
LogError('JustifyAllStaves=' & oRules.JustifyGrandStaveInstruments, 5);
} else {
LogError('JustifyAllStaves=' & oRules.JustifyMultiStaveInstruments, 5);
}
}
if (fSystemStaff and (oRules.ExtraSpacesAboveForSystemObjectPositions != 0)) {
LogError('ExtraSpacesAbove=' & oRules.ExtraSpacesAboveForSystemObjectPositions, 5);
}
// There are two different settings for extra space below. Combine them
// here, but always print out if one of the settings is non-zero, even
// if the two cancel out. Otherwise we could have a strange-looking
// empty staff-layout element.
nExtraBelow = 0;
fPrintExtraBelow = False;
if (oInstrumentType.IsVocal and (oRules.ExtraSpacesBelowVocalStaves != 0)) {
fPrintExtraBelow = True;
nExtraBelow = nExtraBelow + oRules.ExtraSpacesBelowVocalStaves;
}
if ((oRules.ExtraSpaceBetweenGroupsOfStaves != 0) and
(PartEndGroup[PartCounter] = 1)) {
fPrintExtraBelow = True;
nExtraBelow = nExtraBelow + oRules.ExtraSpaceBetweenGroupsOfStaves;
}
if (fPrintExtraBelow) {
LogError('ExtraSpacesBelow=' & nExtraBelow, 5);
}
WriteXML('</staff-layout>', 4);
}
// Write measure-layout for gap before bar.
if (fPrintMeasureLayout) {
if (not fPrintStarted) {
WriteXML('<print>', 3);
fPrintStarted = True;
}
WriteXML('<measure-layout>', 4);
nMeasureDistance = 10.0 * oBar.GapBefore;
WriteXML('<measure-distance>' & nMeasureDistance & '</measure-distance>', 5);
WriteXML('</measure-layout>', 4);
}
if (fPrintSystemLayout or fPrintStaffLayout or fPrintMeasureLayout) {
WriteXML('</print>', 3);
}
// Ending lines may be reported one measure off, so be
// sure not to overwrite something due to that.
fInitializeEndings = True;
if (RightEndingBar = MeasureNumber) {
fInitializeEndings = False;
}
// When we check at the start of a measure, the last parameter
// indicates we want the first ending to win. This will change
// when we call InitializeBarlines from FinalizeMeasure.
InitializeBarlines(ThisScore, MeasureNumber, fInitializeEndings, True);
if (LeftEndingBar = MeasureNumber) {
WriteSystemBarline(ThisScore, LeftBarline, LeftEnding, 'start', 'left',
LeftEndingColor, fSystemStaff);
LeftEndingBumped = 0;
} else {
if ((Length(LeftBarline) > 0) or (Length(LeftBarlineColor) > 0)) {
WriteSystemBarline(ThisScore, LeftBarline, '', '', 'left',
LeftBarlineColor, fSystemStaff);
}
}
if (MeasureNumber = 1) {
WriteXML('<attributes>', 3);
// Sibelius duration units are 256ths of a quarter.
// Multiply by 3 so triplets work better.
WriteXML('<divisions>768</divisions>', 4);
// For now, assume key signature is same in all
// staves of instrument. Do not write a key signature
// on a percussion staff. Do an extra check now because
// we want the category test to override the clef test,
// not vice versa. Starting with 6.1, we also do not
// write a key on a normal notation staff that disables
// key signatures.
oKey = oStaff.CurrentKeySignature(1);
fNoKey = False;
if (nInstrumentTypeCategory = 1) {
fNoKey = True;
} else {
if ((nInstrumentTypeCategory = 0) and
(not oInstrumentType.HasKeySignatureOrTuning)) {
fNoKey = True;
}
}
if (not fNoKey) {
CheckKey(oKey, oInstrumentType);
WriteKey(0);
}
// Time signature is on system staff
oTime = oSystemStaff.CurrentTimeSignature(1);
WriteTime(oTime, 0);
// Instrument type of tab staff for Sibelius 5.
nTabClef = 0 + TabClef;
fUseStringArray = False;
// Check for multiple instruments
nInstruments = PartInstrumentsCount[PartCounter];
// Sibelius 6.1 lets us check for small staves, which
// can be represented as a cue or ossia staff-type in MusicXML.
fSmall = CreateSparseArray();
if (Staves = 1) {
if (nInstruments > 1) {
WriteXML('<instruments>' & nInstruments & '</instruments>', 4);
}
WriteXML('<clef>', 4);
sClef = oStaff.InitialClefStyleId;
WriteClefStyle(sClef, 0, oInstrumentType, 1, 0);
WriteXML('</clef>', 4);
// Sibelius 5.1 has a category variable that is
// more accurate for tab than the clef method.
if (oInstrumentType.Category = 2) {
TabClef = 0; // For StaffNumber match
TabStrings = 0 + oInstrumentType.NumStrings;
for nThisString = 0 to TabStrings {
StringPitch[nThisString] = oInstrumentType.PitchOfNthString(nThisString);
}
fUseStringArray = True;
StaffLines[1] = 0 + TabStrings;
} else {
if (oInstrumentType.Category = 1) {
PercussionClef = 0; // For StaffNumber match
PercStaffLines = 0 + oInstrumentType.NumStaveLines;
}
StaffLines[1] = 0 + oInstrumentType.NumStaveLines;
}
fSmall[1] = oStaff.Small;
AssignCurrentPartInstrument(oInstrumentType.StyleId, 0);
} else {
// Staves attribute first.
WriteXML('<staves>' & Staves & '</staves>', 4);
if (nInstruments > 1) {
WriteXML('<instruments>' & nInstruments & '</instruments>', 4);
}
// Because octave-transposing clefs are related to transpositions