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

Tuplet sanitize #2920

Merged
merged 4 commits into from
Dec 30, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions libmscore/read114.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,14 @@ static void readTuplet(Tuplet* tuplet, XmlReader& e)
}
else if (tag == "baseLen") // obsolete even in 1.3
bl = e.readInt();
else if (tag == "tick")
tuplet->setTick(e.readInt());
else if (!tuplet->readProperties(e))
e.unknown();
}
Fraction r = (tuplet->ratio() == 1) ? tuplet->ratio() : tuplet->ratio().reduced();
// this may be wrong, but at this stage it is kept for compatibility. It will be corrected afterwards
// during "sanitize" step
Fraction f(r.denominator(), tuplet->baseLen().fraction().denominator());
tuplet->setDuration(f.reduced());
if (bl != -1) { // obsolete, even in 1.3
Expand Down Expand Up @@ -1345,6 +1349,30 @@ static void readMeasure(Measure* m, int staffIdx, XmlReader& e)
else
e.unknown();
}
// For nested tuplets created with MuseScore 1.3 tuplet dialog (i.e. "Other..." dialog),
// the parent tuplet was not set. Try to infere if the tuplet was actually a nested tuplet
for (Tuplet* tuplet : e.tuplets()) {
int tupletTick = tuplet->tick();
int tupletDuration = tuplet->actualTicks() - 1;
std::vector<DurationElement*> tElements = tuplet->elements();
for (Tuplet* tuplet2 : e.tuplets()) {
if ((tuplet2->tuplet()) || (tuplet2->voice() != tuplet->voice())) // already a nested tuplet or in a different voice
continue;
int possibleDuration = tuplet2->duration().ticks() * tuplet->ratio().denominator() / tuplet->ratio().numerator() - 1;
if ((tuplet2 != tuplet) && (tuplet2->tick() >= tupletTick) && (tuplet2->tick() < tupletTick + tupletDuration) && (tuplet2->tick() + possibleDuration < tupletTick + tupletDuration)) {
bool found = false;
for (DurationElement* de : tElements) {
if (de == tuplet2)
found = true;
}
if (!found) {
qDebug("Adding tuplet %p as nested tuplet to tuplet %p",tuplet2,tuplet);
tuplet2->setTuplet(tuplet);
tuplet->add(tuplet2);
}
}
}
}
e.checkTuplets();
}

Expand Down
2 changes: 1 addition & 1 deletion libmscore/segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ void Segment::add(Element* el)
// the tick position of a tuplet is the tick position of its
// first element:
ChordRest* cr = toChordRest(el);
if (cr->tuplet() && !cr->tuplet()->elements().empty() && cr->tuplet()->elements().front() == cr)
if (cr->tuplet() && !cr->tuplet()->elements().empty() && cr->tuplet()->elements().front() == cr && cr->tuplet()->tick() < 0)
cr->tuplet()->setTick(cr->tick());
}
// fall through
Expand Down
50 changes: 50 additions & 0 deletions libmscore/tuplet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1069,5 +1069,55 @@ void Tuplet::styleChanged()
}
}

//---------------------------------------------------------
// sanitizeTuplet
/// Check validity of tuplets and coherence between duration
/// and baselength. Needed for importing old files due to a bug
/// in the released version for corner-case tuplets.
/// See issue #136406 and Pull request #2881
//---------------------------------------------------------

void Tuplet::sanitizeTuplet()
{
Fraction tupletDuration = duration().reduced();
Fraction baseLenDuration = (Fraction(ratio().denominator(),1) * baseLen().fraction()).reduced();
if ((tupletDuration - baseLenDuration).reduced().numerator() == 0)
return;
// Mismatch between the duration and the duration computed from the base length.
// A tentative will now be made to retrieve the correct duration by summing up all the
// durations of the elements constituting the tuplet. This does not work for
// not-completely filled tuplets, such as tuplets in voices > 0 with
// gaps (for example, a tuplet in second voice with a deleted chordrest element)
Fraction testDuration(0,1);
for (DurationElement* de : elements()) {
if (de == 0)
continue;
Fraction elementDuration(0,1);
if (de->isTuplet()){
Tuplet* t = toTuplet(de);
t->sanitizeTuplet();
elementDuration = t->duration();
}
else {
elementDuration = de->duration();
}
testDuration += elementDuration;
}
testDuration = testDuration / ratio();
testDuration.reduce();
if (((testDuration - tupletDuration).reduced().numerator() != 0) || ((testDuration - baseLenDuration).reduced().numerator() != 0)) {
Fraction f = testDuration * Fraction(1, ratio().denominator());
f.reduce();
Fraction fbl(1, f.denominator());
if (TDuration::isValid(fbl)) {
setDuration(testDuration);
setBaseLen(fbl);
qDebug("Tuplet %p sanitized",this);
}
else {
qDebug("Impossible to sanitize the tuplet");
}
}
}
} // namespace Ms

1 change: 1 addition & 0 deletions libmscore/tuplet.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class Tuplet : public DurationElement {
virtual void resetProperty(P_ID id) override;
virtual void styleChanged() override;
virtual StyleIdx getPropertyStyle(P_ID) const override;
void sanitizeTuplet();
};


Expand Down
5 changes: 5 additions & 0 deletions libmscore/xmlreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ void XmlReader::checkTuplets()
tuplet->id(), tuplet);
delete tuplet;
}
else {
//sort tuplet elements. Needed for nested tuplets #22537
tuplet->sortElements();
tuplet->sanitizeTuplet();
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions mtest/libmscore/compat114/tst_compat114.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ void TestCompat114::compat_data()
QTest::newRow("markers") << "markers";
QTest::newRow("drumset") << "drumset";
QTest::newRow("tuplets") << "tuplets";
QTest::newRow("tuplets_1") << "tuplets_1";
QTest::newRow("tuplets_2") << "tuplets_2";
}

//---------------------------------------------------------
Expand Down
30 changes: 15 additions & 15 deletions mtest/libmscore/compat114/tuplets-ref.mscx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
<Tuplet id="1">
<normalNotes>4</normalNotes>
<actualNotes>6</actualNotes>
<baseNote>eighth</baseNote>
<baseNote>16th</baseNote>
<Number>
<style>Tuplet</style>
<text>6</text>
Expand Down Expand Up @@ -239,7 +239,7 @@
<Tuplet id="2">
<normalNotes>8</normalNotes>
<actualNotes>10</actualNotes>
<baseNote>quarter</baseNote>
<baseNote>eighth</baseNote>
<Number>
<style>Tuplet</style>
<text>10</text>
Expand Down Expand Up @@ -418,8 +418,18 @@
<l1>31</l1>
<l2>27</l2>
</Beam>
<Tuplet id="5">
<direction>up</direction>
<normalNotes>8</normalNotes>
<actualNotes>9</actualNotes>
<baseNote>16th</baseNote>
<Number>
<style>Tuplet</style>
<text>9</text>
</Number>
</Tuplet>
<Chord>
<Tuplet>4</Tuplet>
<Tuplet>5</Tuplet>
<durationType>16th</durationType>
<Beam>6</Beam>
<Note>
Expand All @@ -428,7 +438,7 @@
</Note>
</Chord>
<Chord>
<Tuplet>4</Tuplet>
<Tuplet>5</Tuplet>
<durationType>16th</durationType>
<Beam>6</Beam>
<Note>
Expand All @@ -437,24 +447,14 @@
</Note>
</Chord>
<Chord>
<Tuplet>4</Tuplet>
<Tuplet>5</Tuplet>
<durationType>16th</durationType>
<Beam>6</Beam>
<Note>
<pitch>72</pitch>
<tpc>14</tpc>
</Note>
</Chord>
<Tuplet id="5">
<direction>up</direction>
<normalNotes>8</normalNotes>
<actualNotes>9</actualNotes>
<baseNote>16th</baseNote>
<Number>
<style>Tuplet</style>
<text>9</text>
</Number>
</Tuplet>
<Tuplet id="6">
<Tuplet>5</Tuplet>
<normalNotes>4</normalNotes>
Expand Down