/
exportxml.cpp
4792 lines (4309 loc) · 192 KB
/
exportxml.cpp
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
//=============================================================================
// MusE Score
// Linux Music Score Editor
// $Id: exportxml.cpp 5598 2012-04-30 07:20:39Z lvinken $
//
// Copyright (C) 2002-2013 Werner Schweer and others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================
/**
MusicXML export.
*/
// TODO: trill lines need to be handled the same way as slurs
// in MuseScore they are measure level elements, while in MusicXML
// they are attached to notes (as ornaments)
//=========================================================
// LVI FIXME
//
// Evaluate paramenter handling between the various classes, could be simplified
//=========================================================
// TODO LVI 2011-10-30: determine how to report export errors.
// Currently all output (both debug and error reports) are done using qDebug.
#include <math.h>
#include "config.h"
//#include "musescore.h"
#include "file.h"
#include "libmscore/score.h"
#include "libmscore/rest.h"
#include "libmscore/chord.h"
#include "libmscore/sig.h"
#include "libmscore/key.h"
#include "libmscore/clef.h"
#include "libmscore/note.h"
#include "libmscore/segment.h"
#include "libmscore/xml.h"
#include "libmscore/beam.h"
#include "libmscore/staff.h"
#include "libmscore/part.h"
#include "libmscore/measure.h"
#include "libmscore/style.h"
#include "musicxml.h"
#include "libmscore/slur.h"
#include "libmscore/hairpin.h"
#include "libmscore/dynamic.h"
#include "libmscore/barline.h"
#include "libmscore/timesig.h"
#include "libmscore/ottava.h"
#include "libmscore/pedal.h"
#include "libmscore/text.h"
#include "libmscore/tuplet.h"
#include "libmscore/lyrics.h"
#include "libmscore/volta.h"
#include "libmscore/keysig.h"
#include "libmscore/bracket.h"
#include "libmscore/arpeggio.h"
#include "libmscore/jump.h"
#include "libmscore/marker.h"
#include "libmscore/tremolo.h"
#include "libmscore/trill.h"
#include "libmscore/harmony.h"
#include "libmscore/tempotext.h"
#include "libmscore/sym.h"
#include "libmscore/pitchspelling.h"
#include "libmscore/utils.h"
#include "libmscore/articulation.h"
#include "libmscore/page.h"
#include "libmscore/system.h"
#include "libmscore/element.h"
#include "libmscore/glissando.h"
#include "libmscore/navigate.h"
#include "libmscore/spanner.h"
#include "libmscore/drumset.h"
#include "preferences.h"
#include "libmscore/mscore.h"
#include "libmscore/accidental.h"
#include "libmscore/breath.h"
#include "libmscore/chordline.h"
#include "libmscore/figuredbass.h"
#include "libmscore/stringdata.h"
#include "libmscore/rehearsalmark.h"
#include "thirdparty/qzip/qzipwriter_p.h"
#include "libmscore/fret.h"
#include "libmscore/tie.h"
#include "musicxmlfonthandler.h"
namespace Ms {
//---------------------------------------------------------
// local defines for debug output
//---------------------------------------------------------
// #define DEBUG_CLEF true
// #define DEBUG_REPEATS true
// #define DEBUG_TICK true
#ifdef DEBUG_CLEF
#define clefDebug(...) qDebug(__VA_ARGS__)
#else
#define clefDebug(...) ;
#endif
//---------------------------------------------------------
// typedefs
//---------------------------------------------------------
typedef QMap<int, const FiguredBass*> FigBassMap;
//---------------------------------------------------------
// attributes -- prints <attributes> tag when necessary
//---------------------------------------------------------
class Attributes {
bool inAttributes;
public:
Attributes() { start(); }
void doAttr(Xml& xml, bool attr);
void start();
void stop(Xml& xml);
};
//---------------------------------------------------------
// doAttr - when necessary change state and print <attributes> tag
//---------------------------------------------------------
void Attributes::doAttr(Xml& xml, bool attr)
{
if (!inAttributes && attr) {
xml.stag("attributes");
inAttributes = true;
}
else if (inAttributes && !attr) {
xml.etag();
inAttributes = false;
}
}
//---------------------------------------------------------
// start -- initialize
//---------------------------------------------------------
void Attributes::start()
{
inAttributes = false;
}
//---------------------------------------------------------
// stop -- print </attributes> tag when necessary
//---------------------------------------------------------
void Attributes::stop(Xml& xml)
{
if (inAttributes) {
xml.etag();
inAttributes = false;
}
}
//---------------------------------------------------------
// notations -- prints <notations> tag when necessary
//---------------------------------------------------------
class Notations {
bool notationsPrinted;
public:
Notations() { notationsPrinted = false; }
void tag(Xml& xml);
void etag(Xml& xml);
};
//---------------------------------------------------------
// articulations -- prints <articulations> tag when necessary
//---------------------------------------------------------
class Articulations {
bool articulationsPrinted;
public:
Articulations() { articulationsPrinted = false; }
void tag(Xml& xml);
void etag(Xml& xml);
};
//---------------------------------------------------------
// ornaments -- prints <ornaments> tag when necessary
//---------------------------------------------------------
class Ornaments {
bool ornamentsPrinted;
public:
Ornaments() { ornamentsPrinted = false; }
void tag(Xml& xml);
void etag(Xml& xml);
};
//---------------------------------------------------------
// technical -- prints <technical> tag when necessary
//---------------------------------------------------------
class Technical {
bool technicalPrinted;
public:
Technical() { technicalPrinted = false; }
void tag(Xml& xml);
void etag(Xml& xml);
};
//---------------------------------------------------------
// slur handler -- prints <slur> tags
//---------------------------------------------------------
class SlurHandler {
const Slur* slur[MAX_NUMBER_LEVEL];
bool started[MAX_NUMBER_LEVEL];
int findSlur(const Slur* s) const;
public:
SlurHandler();
void doSlurStart(Chord* chord, Notations& notations, Xml& xml, bool grace = false);
void doSlurStop(Chord* chord, Notations& notations, Xml& xml);
};
//---------------------------------------------------------
// glissando handler -- prints <glissando> tags
//---------------------------------------------------------
class GlissandoHandler {
const Chord* glissChrd[MAX_NUMBER_LEVEL];
const Chord* slideChrd[MAX_NUMBER_LEVEL];
int findChord(const Chord* c, int st) const;
public:
GlissandoHandler();
void doGlissandoStart(Chord* chord, Notations& notations, Xml& xml);
void doGlissandoStop(Chord* chord, Notations& notations, Xml& xml);
};
//---------------------------------------------------------
// ExportMusicXml
//---------------------------------------------------------
typedef QHash<const Chord*, const Trill*> TrillHash;
class ExportMusicXml {
Score* _score;
Xml xml;
SlurHandler sh;
GlissandoHandler gh;
int tick;
Attributes attr;
TextLine const* bracket[MAX_BRACKETS];
int div;
double millimeters;
int tenths;
TrillHash trillStart;
TrillHash trillStop;
int findBracket(const TextLine* tl) const;
void chord(Chord* chord, int staff, const QList<Lyrics*>* ll, DrumsetKind useDrumset);
void rest(Rest* chord, int staff);
void clef(int staff, ClefType clef);
void timesig(TimeSig* tsig);
void keysig(Key, int staff = 0, bool visible = true);
void barlineLeft(Measure* m);
void barlineRight(Measure* m);
void lyrics(const QList<Lyrics*>* ll, const int trk);
void work(const MeasureBase* measure);
void calcDivMoveToTick(int t);
void calcDivisions();
double getTenthsFromInches(double);
double getTenthsFromDots(double);
void keysigTimesig(Measure* m, int strack, int etrack);
public:
ExportMusicXml(Score* s)
{
_score = s; tick = 0; div = 1; tenths = 40;
millimeters = _score->spatium() * tenths / (10 * MScore::DPMM);
}
void write(QIODevice* dev);
void credits(Xml& xml);
void moveToTick(int t);
void words(Text const* const text, int staff);
void rehearsal(RehearsalMark const* const rmk, int staff);
void hairpin(Hairpin const* const hp, int staff, int tick);
void ottava(Ottava const* const ot, int staff, int tick);
void pedal(Pedal const* const pd, int staff, int tick);
void textLine(TextLine const* const tl, int staff, int tick);
void dynamic(Dynamic const* const dyn, int staff);
void symbol(Symbol const* const sym, int staff);
void tempoText(TempoText const* const text, int staff);
void harmony(Harmony const* const, FretDiagram const* const fd, int offset = 0);
Score* score() { return _score; }
};
//---------------------------------------------------------
// tag
//---------------------------------------------------------
void Notations::tag(Xml& xml)
{
if (!notationsPrinted)
xml.stag("notations");
notationsPrinted = true;
}
//---------------------------------------------------------
// etag
//---------------------------------------------------------
void Notations::etag(Xml& xml)
{
if (notationsPrinted)
xml.etag();
notationsPrinted = false;
}
//---------------------------------------------------------
// tag
//---------------------------------------------------------
void Articulations::tag(Xml& xml)
{
if (!articulationsPrinted)
xml.stag("articulations");
articulationsPrinted = true;
}
//---------------------------------------------------------
// etag
//---------------------------------------------------------
void Articulations::etag(Xml& xml)
{
if (articulationsPrinted)
xml.etag();
articulationsPrinted = false;
}
//---------------------------------------------------------
// tag
//---------------------------------------------------------
void Ornaments::tag(Xml& xml)
{
if (!ornamentsPrinted)
xml.stag("ornaments");
ornamentsPrinted = true;
}
//---------------------------------------------------------
// etag
//---------------------------------------------------------
void Ornaments::etag(Xml& xml)
{
if (ornamentsPrinted)
xml.etag();
ornamentsPrinted = false;
}
//---------------------------------------------------------
// tag
//---------------------------------------------------------
void Technical::tag(Xml& xml)
{
if (!technicalPrinted)
xml.stag("technical");
technicalPrinted = true;
}
//---------------------------------------------------------
// etag
//---------------------------------------------------------
void Technical::etag(Xml& xml)
{
if (technicalPrinted)
xml.etag();
technicalPrinted = false;
}
//---------------------------------------------------------
// slurHandler
//---------------------------------------------------------
SlurHandler::SlurHandler()
{
for (int i = 0; i < MAX_NUMBER_LEVEL; ++i) {
slur[i] = 0;
started[i] = false;
}
}
static QString slurTieLineStyle(const SlurTie* s)
{
QString lineType;
QString rest;
switch (s->lineType()) {
case 1:
lineType = "dotted";
break;
case 2:
lineType = "dashed";
break;
default:
lineType = "";
}
if (!lineType.isEmpty())
rest = QString(" line-type=\"%1\"").arg(lineType);
return rest;
}
//---------------------------------------------------------
// findSlur -- get index of slur in slur table
// return -1 if not found
//---------------------------------------------------------
int SlurHandler::findSlur(const Slur* s) const
{
for (int i = 0; i < MAX_NUMBER_LEVEL; ++i)
if (slur[i] == s) return i;
return -1;
}
//---------------------------------------------------------
// doSlurStart
//---------------------------------------------------------
void SlurHandler::doSlurStart(Chord* chord, Notations& notations, Xml& xml, bool grace)
{
// slurs on grace notes are not in spanner list, therefore:
if (grace){
foreach(Element* el, chord->el()){
if (el->type() == Element::Type::SLUR){
const Slur* s = static_cast<const Slur*>(el);
//define line type
QString rest = slurTieLineStyle(s);
if (chord->isGraceBefore()){
int i = findSlur(0);
if (i >= 0) {
slur[i] = s;
started[i] = true;
notations.tag(xml);
xml.tagE(QString("slur%1 type=\"start\" number=\"%2\"").arg(rest).arg(i + 1));
}
else
qDebug("no free slur slot");
}
else if (chord->isGraceAfter()){
int i = findSlur(s);
if (i >= 0) {
// remove from list and print stop
slur[i] = 0;
started[i] = false;
notations.tag(xml);
xml.tagE(QString("slur%1 type=\"stop\"%2 number=\"%3\"").arg(rest).arg(s->slurDirection() == MScore::Direction::UP ? " placement=\"above\"" : "").arg(i + 1));
}
}
}
}
return;
}
// search for slur(s) starting at this chord
for (auto it : chord->score()->spanner()) {
Spanner* sp = it.second;
if (sp->type() != Element::Type::SLUR || sp->tick() != chord->tick() || sp->track() != chord->track())
continue;
const Slur* s = static_cast<const Slur*>(sp);
// check if on slur list (i.e. stop already seen)
int i = findSlur(s);
//define line type
QString rest = slurTieLineStyle(s);
if (i >= 0) {
// remove from list and print start
slur[i] = 0;
started[i] = false;
notations.tag(xml);
xml.tagE(QString("slur%1 type=\"start\"%2 number=\"%3\"").arg(rest).arg(s->slurDirection() == MScore::Direction::UP ? " placement=\"above\"" : "").arg(i + 1));
}
else {
// find free slot to store it
i = findSlur(0);
if (i >= 0) {
slur[i] = s;
started[i] = true;
notations.tag(xml);
xml.tagE(QString("slur%1 type=\"start\" number=\"%2\"").arg(rest).arg(i + 1));
}
else
qDebug("no free slur slot");
}
}
}
//---------------------------------------------------------
// doSlurStop
//---------------------------------------------------------
// Note: a slur may start in a higher voice in the same measure.
// In that case it is not yet started (i.e. on the active slur list)
// when doSlurStop() is executed. Handle this slur as follows:
// - generate stop anyway and put it on the slur list
// - doSlurStart() starts slur but doesn't store it
void SlurHandler::doSlurStop(Chord* chord, Notations& notations, Xml& xml)
{
// search for slur(s) stopping at this chord but not on slur list yet
for (auto it : chord->score()->spanner()) {
Spanner* sp = it.second;
if (sp->type() != Element::Type::SLUR || sp->tick2() != chord->tick() || sp->track() != chord->track())
continue;
const Slur* s = static_cast<const Slur*>(sp);
// check if on slur list
int i = findSlur(s);
if (i < 0) {
// if not, find free slot to store it
i = findSlur(0);
if (i >= 0) {
slur[i] = s;
started[i] = false;
notations.tag(xml);
xml.tagE("slur type=\"stop\" number=\"%d\"", i + 1);
}
else
qDebug("no free slur slot");
}
}
// search slur list for already started slur(s) stopping at this chord
for (int i = 0; i < MAX_NUMBER_LEVEL; ++i) {
if (slur[i] && slur[i]->tick2() == chord->tick() && slur[i]->track() == chord->track()) {
if (started[i]) {
slur[i] = 0;
started[i] = false;
notations.tag(xml);
xml.tagE("slur type=\"stop\" number=\"%d\"", i + 1);
}
}
}
// surch for slurs to grace notes after
QList<Chord*> graceNotesAfter;
chord->getGraceNotesAfter(&graceNotesAfter);
for (Chord* g : graceNotesAfter) {
foreach(Element* el, g->el()){
if (el->type() == Element::Type::SLUR){
const Slur* s = static_cast<const Slur*>(el);
//define line type
QString rest = slurTieLineStyle(s);
int i = findSlur(0);
if (i >= 0) {
slur[i] = s;
started[i] = true;
notations.tag(xml);
xml.tagE(QString("slur%1 type=\"start\" number=\"%2\"").arg(rest).arg(i + 1));
}
else
qDebug("no free slur slot"); break;
}
}
}
}
//---------------------------------------------------------
// glissando
//---------------------------------------------------------
// <notations>
// <slide line-type="solid" number="1" type="start"/>
// </notations>
// <notations>
// <glissando line-type="wavy" number="1" type="start"/>
// </notations>
static void glissando(const Glissando* gli, int number, bool start, Notations& notations, Xml& xml)
{
Glissando::Type st = gli->glissandoType();
QString tagName;
switch (st) {
case Glissando::Type::STRAIGHT:
tagName = "slide line-type=\"solid\"";
break;
case Glissando::Type::WAVY:
tagName = "glissando line-type=\"wavy\"";
break;
default:
qDebug("unknown glissando subtype %hhd", st);
return;
break;
}
tagName += QString(" number=\"%1\" type=\"%2\"").arg(number).arg(start ? "start" : "stop");
if (gli->color() != MScore::defaultColor)
tagName += " color=\"" + gli->color().name().toUpper() + "\"";
notations.tag(xml);
if (start && gli->showText() && gli->text() != "")
xml.tag(tagName, gli->text());
else
xml.tagE(tagName);
}
//---------------------------------------------------------
// GlissandoHandler
//---------------------------------------------------------
GlissandoHandler::GlissandoHandler()
{
for (int i = 0; i < MAX_NUMBER_LEVEL; ++i) {
glissChrd[i] = 0;
slideChrd[i] = 0;
}
}
//---------------------------------------------------------
// findChord -- get index of chord in chord table for subtype st
// return -1 if not found
//---------------------------------------------------------
int GlissandoHandler::findChord(const Chord* c, int st) const
{
if (st != 0 && st != 1) {
qDebug("GlissandoHandler::findChord: unknown glissando subtype %d", st);
return -1;
}
for (int i = 0; i < MAX_NUMBER_LEVEL; ++i) {
if (st == 0 && slideChrd[i] == c) return i;
if (st == 1 && glissChrd[i] == c) return i;
}
return -1;
}
//---------------------------------------------------------
// doGlissandoStart
//---------------------------------------------------------
void GlissandoHandler::doGlissandoStart(Chord* chord, Notations& notations, Xml& xml)
{
Glissando::Type st = chord->glissando()->glissandoType();
if (st != Glissando::Type::STRAIGHT && st != Glissando::Type::WAVY) {
qDebug("doGlissandoStart: unknown glissando subtype %hhd", st);
return;
}
// check if on chord list
int i = findChord(chord, int(st));
if (i >= 0) {
// print error and remove from list
qDebug("doGlissandoStart: chord %p already on list", chord);
if (st == Glissando::Type::STRAIGHT) slideChrd[i] = 0;
if (st == Glissando::Type::WAVY) glissChrd[i] = 0;
}
// find free slot to store it
i = findChord(0, int(st));
if (i >= 0) {
if (st == Glissando::Type::STRAIGHT) slideChrd[i] = chord;
if (st == Glissando::Type::WAVY) glissChrd[i] = chord;
glissando(chord->glissando(), i + 1, true, notations, xml);
}
else
qDebug("doGlissandoStart: no free slot");
}
//---------------------------------------------------------
// doGlissandoStop
//---------------------------------------------------------
void GlissandoHandler::doGlissandoStop(Chord* chord, Notations& notations, Xml& xml)
{
Glissando::Type st = chord->glissando()->glissandoType();
if (st != Glissando::Type::STRAIGHT && st != Glissando::Type::WAVY) {
qDebug("doGlissandoStart: unknown glissando subtype %hhd", st);
return;
}
for (int i = 0; i < MAX_NUMBER_LEVEL; ++i) {
if (st == Glissando::Type::STRAIGHT && slideChrd[i] == chord) {
slideChrd[i] = 0;
glissando(chord->glissando(), i + 1, false, notations, xml);
return;
}
if (st == Glissando::Type::WAVY && glissChrd[i] == chord) {
glissChrd[i] = 0;
glissando(chord->glissando(), i + 1, false, notations, xml);
return;
}
}
qDebug("doGlissandoStop: glissando chord %p not found", chord);
}
//---------------------------------------------------------
// directions anchor -- anchor directions at another element or a specific tick
//---------------------------------------------------------
class DirectionsAnchor {
Element* direct; // the element containing the direction
Element* anchor; // the element it is attached to
bool start; // whether it is attached to start or end
int tick; // the timestamp
public:
DirectionsAnchor(Element* a, bool s, int t) { direct = 0; anchor = a; start = s; tick = t; }
DirectionsAnchor(int t) { direct = 0; anchor = 0; start = true; tick = t; }
Element* getDirect() { return direct; }
Element* getAnchor() { return anchor; }
bool getStart() { return start; }
int getTick() { return tick; }
void setDirect(Element* d) { direct = d; }
};
//---------------------------------------------------------
// trill hadling
//---------------------------------------------------------
// Find chords to attach trills to. This is necessary because in MuseScore
// trills are spanners (thus attached to segments), while in MusicXML trills
// are attached to notes.
// TBD: must trill end be in the same staff as trill started ?
// if so, no need to pass in strack and etrack (trill has a track)
static void findTrillAnchors(const Trill* trill, Chord*& startChord, Chord*& stopChord)
{
const Segment* seg = trill->startSegment();
const int endTick = trill->tick2();
const int strack = trill->track();
// try to find chords in the same track:
// find a track with suitable chords both for start and stop
for (int i = 0; i < VOICES; ++i) {
Element* el = seg->element(strack + i);
if (!el)
continue;
if (el->type() != Element::Type::CHORD)
continue;
startChord = static_cast<Chord*>(el);
Segment* s = trill->score()->tick2segmentEnd(strack + i, endTick);
if (!s)
continue;
el = s->element(strack + i);
if (!el)
continue;
if (el->type() != Element::Type::CHORD)
continue;
stopChord = static_cast<Chord*>(el);
return;
}
// try to find start/stop chords in different tracks
for (int i = 0; i < VOICES; ++i) {
Element* el = seg->element(strack + i);
if (!el)
continue;
if (el->type() != Element::Type::CHORD)
continue;
startChord = static_cast<Chord*>(el);
break; // first chord found is OK
}
for (int i = 0; i < VOICES; ++i) {
Segment* s = trill->score()->tick2segmentEnd(strack + i, endTick);
if (!s)
continue;
Element* el = s->element(strack + i);
if (!el)
continue;
if (el->type() != Element::Type::CHORD)
continue;
stopChord = static_cast<Chord*>(el);
break; // first chord found is OK
}
}
// find all trills in this measure and this part
static void findTrills(Measure* measure, int strack, int etrack, TrillHash& trillStart, TrillHash& trillStop)
{
// loop over all spanners in this measure
int stick = measure->tick();
int etick = measure->tick() + measure->ticks();
for (auto it = measure->score()->spanner().lower_bound(stick); it != measure->score()->spanner().upper_bound(etick); ++it) {
Spanner* e = it->second;
//qDebug("findTrills 1 trill %p type %d track %d tick %d", e, e->type(), e->track(), e->tick());
if (e->type() == Element::Type::TRILL && strack <= e->track() && e->track() < etrack
&& e->tick() >= measure->tick() && e->tick() < (measure->tick() + measure->ticks()))
{
//qDebug("findTrills 2 trill %p", e);
// a trill is found starting in this segment, trill end time is known
// determine notes to write trill start and stop
const Trill* tr = static_cast<const Trill*>(e);
Chord* startChord = 0; // chord where trill starts
Chord* stopChord = 0; // chord where trill stops
findTrillAnchors(tr, startChord, stopChord);
//qDebug("findTrills 3 startChord %p stopChord %p", startChord, stopChord);
if (startChord && stopChord) {
trillStart.insert(startChord, tr);
trillStop.insert(stopChord, tr);
}
}
}
}
//---------------------------------------------------------
// helpers for ::calcDivisions
//---------------------------------------------------------
typedef QList<int> IntVector;
static IntVector integers;
static IntVector primes;
// check if all integers can be divided by d
static bool canDivideBy(int d)
{
bool res = true;
for (int i = 0; i < integers.count(); i++) {
if ((integers[i] <= 1) || ((integers[i] % d) != 0)) {
res = false;
}
}
return res;
}
// divide all integers by d
static void divideBy(int d)
{
for (int i = 0; i < integers.count(); i++) {
integers[i] /= d;
}
}
static void addInteger(int len)
{
if (!integers.contains(len)) {
integers.append(len);
}
}
//---------------------------------------------------------
// calcDivMoveToTick
//---------------------------------------------------------
void ExportMusicXml::calcDivMoveToTick(int t)
{
if (t < tick) {
#ifdef DEBUG_TICK
qDebug("backup %d", tick - t);
#endif
addInteger(tick - t);
}
else if (t > tick) {
#ifdef DEBUG_TICK
qDebug("forward %d", t - tick);
#endif
addInteger(t - tick);
}
tick = t;
}
//---------------------------------------------------------
// isTwoNoteTremolo - determine is chord is part of two note tremolo
//---------------------------------------------------------
static bool isTwoNoteTremolo(Chord* chord)
{
return (chord->tremolo() && chord->tremolo()->twoNotes());
}
//---------------------------------------------------------
// calcDivisions
//---------------------------------------------------------
// Loop over all voices in all staffs and determine a suitable value for divisions.
// Length of time in MusicXML is expressed in "units", which should allow expressing all time values
// as an integral number of units. Divisions contains the number of units in a quarter note.
// MuseScore uses division (480) midi ticks to represent a quarter note, which expresses all note values
// plus triplets and quintuplets as integer values. Solution is to collect all time values required,
// and divide them by the highest common denominator, which is implemented as a series of
// divisions by prime factors. Initialize the list with division to make sure a quarter note can always
// be written as an integral number of units.
/**
*/
void ExportMusicXml::calcDivisions()
{
// init
integers.clear();
primes.clear();
integers.append(MScore::division);
primes.append(2);
primes.append(3);
primes.append(5);
const QList<Part*>& il = _score->parts();
for (int idx = 0; idx < il.size(); ++idx) {
Part* part = il.at(idx);
tick = 0;
int staves = part->nstaves();
int strack = _score->staffIdx(part) * VOICES;
int etrack = strack + staves * VOICES;
for (MeasureBase* mb = _score->measures()->first(); mb; mb = mb->next()) {
if (mb->type() != Element::Type::MEASURE)
continue;
Measure* m = (Measure*)mb;
for (int st = strack; st < etrack; ++st) {
// sstaff - xml staff number, counting from 1 for this
// instrument
// special number 0 -> dont show staff number in
// xml output (because there is only one staff)
int sstaff = (staves > 1) ? st - strack + VOICES : 0;
sstaff /= VOICES;
for (Segment* seg = m->first(); seg; seg = seg->next()) {
Element* el = seg->element(st);
if (!el)
continue;
// must ignore start repeat to prevent spurious backup/forward
if (el->type() == Element::Type::BAR_LINE && static_cast<BarLine*>(el)->barLineType() == BarLineType::START_REPEAT)
continue;
if (tick != seg->tick())
calcDivMoveToTick(seg->tick());
if (el->isChordRest()) {
int l = static_cast<ChordRest*>(el)->actualTicks();
if (el->type() == Element::Type::CHORD) {
if (isTwoNoteTremolo(static_cast<Chord*>(el)))
l /= 2;
}
#ifdef DEBUG_TICK
qDebug("chordrest %d", l);
#endif
addInteger(l);
tick += l;
}
}
}
// move to end of measure (in case of incomplete last voice)
calcDivMoveToTick(m->tick() + m->ticks());
}
}
// do it: divide by all primes as often as possible
for (int u = 0; u < primes.count(); u++)
while (canDivideBy(primes[u]))
divideBy(primes[u]);
div = MScore::division / integers[0];
#ifdef DEBUG_TICK
qDebug("divisions=%d div=%d", integers[0], div);
#endif
}
//---------------------------------------------------------
// writePageFormat
//---------------------------------------------------------
static void writePageFormat(const PageFormat* pf, Xml& xml, double conversion)
{
xml.stag("page-layout");
xml.tag("page-height", pf->size().height() * conversion);
xml.tag("page-width", pf->size().width() * conversion);
QString type("both");
if (pf->twosided()) {
type = "even";
xml.stag(QString("page-margins type=\"%1\"").arg(type));
xml.tag("left-margin", pf->evenLeftMargin() * conversion);
xml.tag("right-margin", pf->evenRightMargin() * conversion);
xml.tag("top-margin", pf->evenTopMargin() * conversion);
xml.tag("bottom-margin", pf->evenBottomMargin() * conversion);
xml.etag();
type = "odd";
}