Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InstrumentOrdering updates #6880

Merged
merged 2 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions libmscore/instrtemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,25 @@ InstrumentIndex searchTemplateIndexForTrackName(const QString& trackName)
return InstrumentIndex(-1, -1, nullptr);
}

//---------------------------------------------------------
// searchTemplateIndexForId
//---------------------------------------------------------

InstrumentIndex searchTemplateIndexForId(const QString& id)
{
int instIndex = 0;
int grpIndex = 0;
for (InstrumentGroup* g : instrumentGroups) {
for (InstrumentTemplate* it : g->instrumentTemplates) {
if (it->id == id)
return InstrumentIndex(grpIndex, instIndex, it);
++instIndex;
}
++grpIndex;
}
return InstrumentIndex(-1, -1, nullptr);
}

//---------------------------------------------------------
// linkGenre
// link the current instrument template to the genre list specified by "genre"
Expand Down
1 change: 1 addition & 0 deletions libmscore/instrtemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ extern bool saveInstrumentTemplates(const QString& instrTemplates);
extern InstrumentTemplate* searchTemplate(const QString& name);
extern InstrumentTemplate* searchTemplateForMusicXmlId(const QString& mxmlId);
extern InstrumentIndex searchTemplateIndexForTrackName(const QString& trackName);
extern InstrumentIndex searchTemplateIndexForId(const QString& id);
extern InstrumentGroup* searchInstrumentGroup(const QString& name);
extern ClefType defaultClef(int patch);

Expand Down
32 changes: 29 additions & 3 deletions libmscore/instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ bool MidiArticulation::operator==(const MidiArticulation& i) const
// Instrument
//---------------------------------------------------------

Instrument::Instrument()
Instrument::Instrument(QString id)
{
_id = id;
Channel* a = new Channel;
a->setName(Channel::DEFAULT_NAME);
_channel.append(a);
Expand All @@ -111,6 +112,7 @@ Instrument::Instrument()

Instrument::Instrument(const Instrument& i)
{
_id = i._id;
_longNames = i._longNames;
_shortNames = i._shortNames;
_trackName = i._trackName;
Expand Down Expand Up @@ -139,6 +141,7 @@ void Instrument::operator=(const Instrument& i)
_channel.clear();
delete _drumset;

_id = i._id;
_longNames = i._longNames;
_shortNames = i._shortNames;
_trackName = i._trackName;
Expand Down Expand Up @@ -214,7 +217,10 @@ void StaffName::read(XmlReader& e)

void Instrument::write(XmlWriter& xml, const Part* part) const
{
xml.stag("Instrument");
if (_id.isEmpty())
xml.stag("Instrument");
else
xml.stag(QString("Instrument id=\"%1\"").arg(_id));
_longNames.write(xml, "longName");
_shortNames.write(xml, "shortName");
// if (!_trackName.empty())
Expand Down Expand Up @@ -286,6 +292,7 @@ void Instrument::read(XmlReader& e, Part* part)
bool readSingleNoteDynamics = false;

_channel.clear(); // remove default channel
_id = e.attribute("id");
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "singleNoteDynamics") {
Expand Down Expand Up @@ -1480,7 +1487,7 @@ void Instrument::setTrackName(const QString& s)

Instrument Instrument::fromTemplate(const InstrumentTemplate* t)
{
Instrument instr;
Instrument instr(t->id);
instr.setAmateurPitchRange(t->minPitchA, t->maxPitchA);
instr.setProfessionalPitchRange(t->minPitchP, t->maxPitchP);
for (StaffName sn : t->longNames)
Expand All @@ -1504,6 +1511,25 @@ Instrument Instrument::fromTemplate(const InstrumentTemplate* t)
return instr;
}

//---------------------------------------------------------
// updateInstrumentId
//---------------------------------------------------------

void Instrument::updateInstrumentId()
{
if (!_id.isEmpty() || _instrumentId.isEmpty())
return;

for (InstrumentGroup* g : instrumentGroups) {
for (InstrumentTemplate* it : g->instrumentTemplates) {
if (it->musicXMLid == instrumentId()) {
_id = it->id;
return;
}
}
}
}

//---------------------------------------------------------
// Instrument::playbackChannel
//---------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion libmscore/instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class Instrument {
StaffNameList _longNames;
StaffNameList _shortNames;
QString _trackName;
QString _id;

char _minPitchA, _maxPitchA, _minPitchP, _maxPitchP;
Interval _transpose;
Expand All @@ -271,7 +272,7 @@ class Instrument {
bool _singleNoteDynamics;

public:
Instrument();
Instrument(QString id="");
Instrument(const Instrument&);
void operator=(const Instrument&);
~Instrument();
Expand All @@ -288,6 +289,7 @@ class Instrument {
bool operator==(const Instrument&) const;
bool isDifferentInstrument(const Instrument& i) const;

QString getId() const { return _id; }
void setMinPitchP(int v) { _minPitchP = v; }
void setMaxPitchP(int v) { _maxPitchP = v; }
void setMinPitchA(int v) { _minPitchA = v; }
Expand Down Expand Up @@ -345,6 +347,8 @@ class Instrument {
void setTrackName(const QString& s);
static Instrument fromTemplate(const InstrumentTemplate* t);

void updateInstrumentId();

bool singleNoteDynamics() const { return _singleNoteDynamics; }
void setSingleNoteDynamics(bool val) { _singleNoteDynamics = val; }
void setSingleNoteDynamicsFromTemplate();
Expand Down
6 changes: 6 additions & 0 deletions libmscore/read302.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ bool Score::read(XmlReader& e)
}
}
#endif
// Make sure every instrument has an instrumentId set.
for (Part* part : parts()) {
const InstrumentList* il = part->instruments();
for (auto it = il->begin(); it != il->end(); it++)
static_cast<Instrument*>(it->second)->updateInstrumentId();
}
if (order) {
ScoreOrder* defined = scoreOrders.findByName(order->getName());
if (defined)
Expand Down
51 changes: 14 additions & 37 deletions libmscore/scoreOrder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,16 @@ void ScoreOrder::readSection(XmlReader& e)

QString ScoreOrder::getFamilyName(const InstrumentTemplate* instrTemplate, bool soloist) const
{
if (!instrTemplate)
return QString("<unsorted>");
if (soloist)
return QString("<soloists>");
else if (instrumentMap.contains(instrTemplate->trackName.toLower()))
return instrumentMap[instrTemplate->trackName.toLower()].id;
else if (instrTemplate->family)
return instrTemplate->family->id;
else
return QString("<orphan>");
return QString("<unsorted>");
}

//---------------------------------------------------------
Expand Down Expand Up @@ -441,13 +443,10 @@ ScoreGroup* ScoreOrder::getGroup(const QString family, const QString instrumentG
return unsorted;
}

ScoreGroup* ScoreOrder::getGroup(const QString instrumentName, bool soloist) const
ScoreGroup* ScoreOrder::getGroup(const QString id, const bool soloist) const
{
InstrumentIndex ii = searchTemplateIndexForTrackName(instrumentName);
Q_ASSERT(ii.instrTemplate);

InstrumentIndex ii = searchTemplateIndexForId(id);
QString family { getFamilyName(ii.instrTemplate, soloist) };

return getGroup(family, instrumentGroups[ii.groupIndex]->id);
}

Expand Down Expand Up @@ -534,22 +533,22 @@ void ScoreOrder::write(XmlWriter& xml) const
// instrumentIndex
//---------------------------------------------------------

int ScoreOrder::instrumentIndex(const QString name, bool soloist) const
int ScoreOrder::instrumentIndex(const QString id, bool soloist) const
{
ScoreGroup* sg = getGroup(name, soloist);
ScoreGroup* sg = getGroup(id, soloist);
int groupIndex = sg ? sg->index() : _groupMultiplier;
bool unsorted = sg && sg->isUnsorted();

return _groupMultiplier * groupIndex + (unsorted ? 0 : searchTemplateIndexForTrackName(name).instrIndex);
return _groupMultiplier * groupIndex + (unsorted ? 0 : searchTemplateIndexForId(id).instrIndex);
}

//---------------------------------------------------------
// instrumentInUnsortedSection
//---------------------------------------------------------

bool ScoreOrder::instrumentInUnsortedSection(const QString name, bool soloist) const
bool ScoreOrder::instrumentInUnsortedSection(const QString id, bool soloist) const
{
return soloist || getGroup(name, soloist)->isUnsorted();
return soloist || getGroup(id, soloist)->isUnsorted();
}

//---------------------------------------------------------
Expand All @@ -560,7 +559,7 @@ void ScoreOrder::updateInstruments(const Score* score)
{
for (Part* part : score->parts())
{
InstrumentIndex ii = searchTemplateIndexForTrackName(part->instrument()->trackName());
InstrumentIndex ii = searchTemplateIndexForId(part->instrument()->getId());
if (!ii.instrTemplate || !ii.instrTemplate->family)
continue;

Expand Down Expand Up @@ -589,9 +588,7 @@ void ScoreOrder::setBracketsAndBarlines(Score* score)

for (Part* part : score->parts())
{
InstrumentIndex ii = searchTemplateIndexForTrackName(part->instrument()->trackName());
Q_ASSERT(ii.instrTemplate);

InstrumentIndex ii = searchTemplateIndexForId(part->instrument()->getId());
QString family { getFamilyName(ii.instrTemplate, part->soloist()) };
ScoreGroup* sg = getGroup(family, instrumentGroups[ii.groupIndex]->id);

Expand Down Expand Up @@ -680,31 +677,11 @@ bool ScoreOrder::isScoreOrder(const Score* score) const
{
QList<int> indices;
for (Part* part : score->parts())
indices << instrumentIndex(part->instrument()->trackName(), part->soloist());
indices << instrumentIndex(part->instrument()->getId(), part->soloist());

return isScoreOrder(indices);
}

//---------------------------------------------------------
// debug
//---------------------------------------------------------

void ScoreOrder::debug(const QString& name, bool soloist) const
{
InstrumentIndex ii = searchTemplateIndexForTrackName(name);
Q_ASSERT(ii.instrTemplate);

QString family { getFamilyName(ii.instrTemplate, soloist) };

ScoreGroup* sg = getGroup(family, instrumentGroups[ii.groupIndex]->id);
dump();
std::cout << ">> " << name.toStdString() << ": family = " << family.toStdString()
<< ", group = " << (sg ? sg->index() : -1) << std::endl;

saveScoreOrders(QString("/scratch/MuseScoreDevelopment/InstrumentOrdering/tmp_order.xml"));

}

//---------------------------------------------------------
// dump
//---------------------------------------------------------
Expand Down Expand Up @@ -859,7 +836,7 @@ QList<ScoreOrder*> ScoreOrderList::searchScoreOrders(const Score* score) const
{
if (order->isCustom())
continue;
indices << order->instrumentIndex(part->instrument()->trackName(), part->soloist());
indices << order->instrumentIndex(part->instrument()->getId(), part->soloist());
if (order->isScoreOrder(indices))
orders << order;
}
Expand Down
7 changes: 3 additions & 4 deletions libmscore/scoreOrder.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,19 @@ class ScoreOrder {
void setCustomised();

ScoreGroup* getGroup(const QString family, const QString instrumentGroup) const;
ScoreGroup* getGroup(const QString instrumentName, bool soloist) const;
ScoreGroup* getGroup(const QString instrumentId, bool soloist) const;

void read(XmlReader& e);
void write(XmlWriter& xml) const;

int instrumentIndex(const QString name, bool soloist) const;
bool instrumentInUnsortedSection(const QString name, bool soloist) const;
int instrumentIndex(const QString id, bool soloist) const;
bool instrumentInUnsortedSection(const QString id, bool soloist) const;

void updateInstruments(const Score* score);
void setBracketsAndBarlines(Score* score);
bool isScoreOrder(const QList<int>& indices) const;
bool isScoreOrder(const Score* score) const;

void debug(const QString& name, bool soloist) const;
void dump() const;
};

Expand Down
Loading