Skip to content

Commit

Permalink
Fix GH#17397: Fully export instrument changes to MusicXML
Browse files Browse the repository at this point in the history
Backport of musescore#24932 and enable and fix testMultiInstrumentPart3.
Also fixing some clazy warnings.
  • Loading branch information
rettinghaus authored and Jojo-Schmitz committed Nov 8, 2024
1 parent f391d7b commit 057904b
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 10 deletions.
50 changes: 45 additions & 5 deletions importexport/musicxml/exportxml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ class ExportMusicXml {
double getTenthsFromInches(double) const;
double getTenthsFromDots(double) const;
Fraction tick() const { return _tick; }
void writeInstrumentChange(const InstrumentChange* instrChange);
void writeInstrumentDetails(const Instrument* instrument, const bool concertPitch);
static bool canWrite(const Element* e);
};
Expand Down Expand Up @@ -1482,9 +1483,9 @@ static void textAsCreditWords(const ExportMusicXml* const expMxml, XmlWriter& xm
void ExportMusicXml::credits(XmlWriter& xml)
{
// find the vboxes in every page and write their elements as credit-words
for (Page* const page : _score->pages()) {
for (Page* const page : qAsConst(_score->pages())) {
const int pageIdx = _score->pageIdx(page);
for (const System* system : page->systems()) {
for (const System* system : qAsConst(page->systems())) {
for (const MeasureBase* mb : system->measures()) {
if (mb->isVBox()) {
for (const Element* element : mb->el()) {
Expand Down Expand Up @@ -1690,7 +1691,7 @@ static void ending(XmlWriter& xml, Volta* v, bool left)
{
QString number;
QString type;
for (int i : v->endings()) {
for (int& i : v->endings()) {
if (!number.isEmpty())
number += ", ";
number += QString("%1").arg(i);
Expand Down Expand Up @@ -3971,7 +3972,7 @@ void ExportMusicXml::chord(Chord* chord, int staff, const std::vector<Lyrics*>*
_xml.tag("voice", voice);

writeType(_xml, note);
for (NoteDot* dot : note->dots()) {
for (const NoteDot* dot : qAsConst(note->dots())) {
QString dotTag = "dot";
if (note->userDotPosition() != Direction::AUTO) {
if (note->dotIsUp())
Expand Down Expand Up @@ -5844,7 +5845,7 @@ static bool commonAnnotations(ExportMusicXml* exp, const Element* e, int sstaff)
// optionally writing the associated staff text is done below
if (e->isInstrumentChange()) {
const InstrumentChange* instrChange = toInstrumentChange(e);
exp->writeInstrumentDetails(instrChange->instrument(), false);
exp->writeInstrumentChange(instrChange);
instrChangeHandled = true;
}

Expand Down Expand Up @@ -7041,6 +7042,45 @@ static void writeStaffDetails(XmlWriter& xml, const Part* part)
}
}

//---------------------------------------------------------
// writeInstrumentChange
//---------------------------------------------------------

/**
Write the instrument change.
*/

void ExportMusicXml::writeInstrumentChange(const InstrumentChange* instrChange)
{
const Instrument* instr = instrChange->instrument();
Part* const part = instrChange->part();
const int partNr = _score->parts().indexOf(part);
const int instNr = instrMap.value(instr, -1);
const QString longName = instr->longNames()[0].name();
const QString shortName = instr->shortNames()[0].name();

_xml.stag("print");
if (!longName.isEmpty()) {
_xml.stag("part-name-display");
writeDisplayName(_xml, longName);
_xml.etag();
}
if (!shortName.isEmpty()) {
_xml.stag("part-abbreviation-display");
writeDisplayName(_xml, shortName);
_xml.etag();
}
_xml.etag();

writeInstrumentDetails(instr, _score->styleB(Sid::concertPitch));

_xml.stag("sound");
_xml.stag("instrument-change");
scoreInstrument(_xml, partNr + 1, instNr + 1, instr->trackName(), instr);
_xml.etag();
_xml.etag();
}

//---------------------------------------------------------
// writeInstrumentDetails
//---------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions mtest/musicxml/io/testChangeTranspose-no-diatonic_ref.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,27 @@
<fifths>2</fifths>
</key>
</attributes>
<print>
<part-name-display>
<display-text>A Clarinet</display-text>
</part-name-display>
<part-abbreviation-display>
<display-text>A Cl.</display-text>
</part-abbreviation-display>
</print>
<attributes>
<transpose>
<diatonic>-1</diatonic>
<chromatic>-2</chromatic>
</transpose>
</attributes>
<sound>
<instrument-change>
<score-instrument id="P1-I2">
<instrument-name>B♭ Clarinet</instrument-name>
</score-instrument>
</instrument-change>
</sound>
<direction placement="above">
<direction-type>
<words font-weight="bold" font-size="12">To B♭ Clarinet</words>
Expand Down
15 changes: 15 additions & 0 deletions mtest/musicxml/io/testChangeTranspose.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,27 @@
<fifths>2</fifths>
</key>
</attributes>
<print>
<part-name-display>
<display-text>A Clarinet</display-text>
</part-name-display>
<part-abbreviation-display>
<display-text>A Cl.</display-text>
</part-abbreviation-display>
</print>
<attributes>
<transpose>
<diatonic>-1</diatonic>
<chromatic>-2</chromatic>
</transpose>
</attributes>
<sound>
<instrument-change>
<score-instrument id="P1-I2">
<instrument-name>B♭ Clarinet</instrument-name>
</score-instrument>
</instrument-change>
</sound>
<direction placement="above">
<direction-type>
<words font-weight="bold" font-size="12">To B♭ Clarinet</words>
Expand Down
15 changes: 15 additions & 0 deletions mtest/musicxml/io/testInstrumentChangeMIDIportExport_ref.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,21 @@
</note>
</measure>
<measure number="2">
<print>
<part-name-display>
<display-text>Voice</display-text>
</part-name-display>
<part-abbreviation-display>
<display-text>Vo.</display-text>
</part-abbreviation-display>
</print>
<sound>
<instrument-change>
<score-instrument id="P15-I2">
<instrument-name>Voice</instrument-name>
</score-instrument>
</instrument-change>
</sound>
<direction placement="above">
<direction-type>
<words font-weight="bold">Voice</words>
Expand Down
15 changes: 15 additions & 0 deletions mtest/musicxml/io/testMultiInstrumentPart1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@
</note>
</measure>
<measure number="3">
<print>
<part-name-display>
<display-text>Piano</display-text>
</part-name-display>
<part-abbreviation-display>
<display-text>Pno.</display-text>
</part-abbreviation-display>
</print>
<sound>
<instrument-change>
<score-instrument id="P1-I2">
<instrument-name>Flute</instrument-name>
</score-instrument>
</instrument-change>
</sound>
<direction placement="above">
<direction-type>
<words font-weight="bold" font-size="12">Flute</words>
Expand Down
30 changes: 30 additions & 0 deletions mtest/musicxml/io/testMultiInstrumentPart2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@
</note>
</measure>
<measure number="3">
<print>
<part-name-display>
<display-text>Piano</display-text>
</part-name-display>
<part-abbreviation-display>
<display-text>Pno.</display-text>
</part-abbreviation-display>
</print>
<sound>
<instrument-change>
<score-instrument id="P1-I2">
<instrument-name>Flute</instrument-name>
</score-instrument>
</instrument-change>
</sound>
<direction placement="above">
<direction-type>
<words font-weight="bold" font-size="12">Flute</words>
Expand Down Expand Up @@ -246,6 +261,21 @@
</note>
</measure>
<measure number="7">
<print>
<part-name-display>
<display-text>Voice</display-text>
</part-name-display>
<part-abbreviation-display>
<display-text>Vo.</display-text>
</part-abbreviation-display>
</print>
<sound>
<instrument-change>
<score-instrument id="P2-I2">
<instrument-name>C Trumpet</instrument-name>
</score-instrument>
</instrument-change>
</sound>
<direction placement="above">
<direction-type>
<words font-weight="bold" font-size="12">Trumpet</words>
Expand Down
29 changes: 24 additions & 5 deletions mtest/musicxml/io/testMultiInstrumentPart3_ref.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,34 @@
<fifths>-3</fifths>
</key>
</attributes>
<print>
<part-name-display>
<display-text>B</display-text>
<accidental-text>flat</accidental-text>
<display-text> Clarinet</display-text>
</part-name-display>
<part-abbreviation-display>
<display-text>B</display-text>
<accidental-text>flat</accidental-text>
<display-text> Cl.</display-text>
</part-abbreviation-display>
</print>
<attributes>
<transpose>
<diatonic>-1</diatonic>
<chromatic>-2</chromatic>
</transpose>
</attributes>
<sound>
<instrument-change>
<score-instrument id="P1-I2">
<instrument-name>B♭ Clarinet</instrument-name>
</score-instrument>
</instrument-change>
</sound>
<direction placement="above">
<direction-type>
<words font-weight="bold" font-size="12">B♭ Clarinet</words>
<words font-weight="bold">B♭ Clarinet</words>
</direction-type>
</direction>
<note>
Expand Down Expand Up @@ -276,28 +295,28 @@
</measure>
<measure number="5">
<note>
<rest/>
<rest measure="yes"/>
<duration>4</duration>
<voice>1</voice>
</note>
</measure>
<measure number="6">
<note>
<rest/>
<rest measure="yes"/>
<duration>4</duration>
<voice>1</voice>
</note>
</measure>
<measure number="7">
<note>
<rest/>
<rest measure="yes"/>
<duration>4</duration>
<voice>1</voice>
</note>
</measure>
<measure number="8">
<note>
<rest/>
<rest measure="yes"/>
<duration>4</duration>
<voice>1</voice>
</note>
Expand Down
1 change: 1 addition & 0 deletions mtest/musicxml/io/tst_mxml_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ private slots:
void midiPortExport() { mxmlMscxExportTestRef("testMidiPortExport"); }
void multiInstrumentPart1() { mxmlIoTest("testMultiInstrumentPart1"); }
void multiInstrumentPart2() { mxmlIoTest("testMultiInstrumentPart2"); }
void multiInstrumentPart3() { mxmlMscxExportTestRef("testMultiInstrumentPart3"); }
void multiMeasureRest1() { mxmlIoTestRef("testMultiMeasureRest1"); }
void multiMeasureRest2() { mxmlIoTestRef("testMultiMeasureRest2"); }
void multiMeasureRest3() { mxmlIoTestRef("testMultiMeasureRest3"); }
Expand Down

0 comments on commit 057904b

Please sign in to comment.