Skip to content

Commit

Permalink
Merge pull request #26470 from miiizen/xml-perc-import
Browse files Browse the repository at this point in the history
Create parent percussion/drumkit instrument on xml import
  • Loading branch information
mike-spa authored Feb 18, 2025
2 parents ce70d0b + c43d2c7 commit ed33c7f
Show file tree
Hide file tree
Showing 5 changed files with 6,445 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,20 @@ static void updatePartWithInstrumentChange(Part* const part, const MusicXmlInstr
}
}

static void setPercussionInstrument(MusicXmlInstrument& mxmlInstr, const String& partName)
{
// If there are multiple unpitched instruments we should set the part's instrument to drumkit or percussion
if (partName.contains(u"drumset",
CaseSensitivity::CaseInsensitive) || partName.contains(u"drumkit", CaseSensitivity::CaseInsensitive)) {
mxmlInstr = MusicXmlInstrument();
mxmlInstr.name = u"Drum Kit";
} else if (partName.contains(u"percussion", CaseSensitivity::CaseInsensitive)) {
mxmlInstr = MusicXmlInstrument();
mxmlInstr.name = u"Percussion";
mxmlInstr.sound = u"drum.group.set";
}
}

//---------------------------------------------------------
// setPartInstruments
//---------------------------------------------------------
Expand All @@ -638,7 +652,8 @@ static void setPartInstruments(MusicXmlLogger* logger, const XmlStreamReader* xm
const Score* score,
const MusicXmlInstrList& instrList,
const MusicXmlIntervalList& intervList,
const MusicXmlInstruments& instruments)
const MusicXmlInstruments& instruments,
const String& partName)
{
if (instruments.empty()) {
// no instrument details found, create a default instrument
Expand All @@ -651,6 +666,9 @@ static void setPartInstruments(MusicXmlLogger* logger, const XmlStreamReader* xm
// do not create multiple instruments for a drum part
//LOGD("hasDrumset");
MusicXmlInstrument mxmlInstr = instruments.begin()->second;
if (instruments.size() > 1) {
setPercussionInstrument(mxmlInstr, partName);
}
updatePartWithInstrument(part, mxmlInstr, {}, true);
return;
}
Expand Down Expand Up @@ -2149,13 +2167,11 @@ void MusicXmlParserPass2::part()
const MusicXmlInstruments& instruments = m_pass1.getInstruments(id);
m_hasDrumset = hasDrumset(instruments);

// set the parts first instrument
Part* part = m_pass1.getPart(id);
setPartInstruments(m_logger, &m_e, part, id, m_score, m_pass1.getInstrList(id), m_pass1.getIntervals(id), instruments);

// set the part name
Part* part = m_pass1.getPart(id);
MusicXmlPart mxmlPart = m_pass1.getMusicXmlPart(id);
String partName = mxmlPart.getName();
setPartInstruments(m_logger, &m_e, part, id, m_score, m_pass1.getInstrList(id), m_pass1.getIntervals(id), instruments, partName);
partName = replacePartNameAccidentals(partName);
part->setPartName(partName);
if (mxmlPart.getPrintName() && !isLikelyIncorrectPartName(partName)) {
Expand All @@ -2168,6 +2184,7 @@ void MusicXmlParserPass2::part()
} else {
m_pass1.getPart(id)->setPlainShortNameAll(u"");
}
// set the parts first instrument
// try to prevent an empty track name
if (part->partName() == "") {
String instrId = m_pass1.getInstrList(id).instrument(Fraction(0, 1));
Expand Down Expand Up @@ -6500,8 +6517,7 @@ static void setPitch(Note* note, const MusicXmlInstruments& instruments, const S
// get pitch from instrument definition in drumset instead
int unpitched = instruments.at(instrumentId).unpitched;
note->setPitch(std::clamp(unpitched, 0, 127));
// TODO - does this need to be key-aware?
note->setTpc(pitch2tpc(unpitched, Key::C, Prefer::NEAREST)); // TODO: necessary ?
note->setTpcFromPitch();
} else {
//LOGD("disp step %d oct %d", displayStep, displayOctave);
xmlSetPitch(note, mnp.displayStep(), 0, 0.0, mnp.displayOctave(), 0, instrument);
Expand Down
Loading

0 comments on commit ed33c7f

Please sign in to comment.