Skip to content

Commit

Permalink
Merge pull request musescore#18508 from alexpavlov96/gp_hide_rests_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpavlov96 committed Jul 11, 2023
2 parents fccb957 + 9ede16f commit a59b372
Show file tree
Hide file tree
Showing 162 changed files with 1,202 additions and 576 deletions.
90 changes: 40 additions & 50 deletions src/importexport/guitarpro/internal/gtp/gpconverter.cpp
Expand Up @@ -344,13 +344,6 @@ void GPConverter::convert(const std::vector<std::unique_ptr<GPMasterBar> >& mast
// glueing line segment elements separated with rests
m_continiousElementsBuilder->addElementsToScore();

// fixing last measure barline
if (_lastMeasure) {
for (size_t staffIdx = 0; staffIdx < _score->staves().size(); staffIdx++) {
_lastMeasure->setEndBarLineType(mu::engraving::BarLineType::FINAL, staffIdx * VOICES);
}
}

addTempoMap();
addInstrumentChanges();
StretchedBend::prepareBends(m_stretchedBends);
Expand All @@ -363,25 +356,15 @@ void GPConverter::convertMasterBar(const GPMasterBar* mB, Context ctx)
Measure* measure = addMeasure(mB);

addTimeSig(mB, measure);

addKeySig(mB, measure);

addBarline(mB, measure);

addBarline(mB, measure, ctx.masterBarIndex);
addRepeat(mB, measure);

collectFermatas(mB, measure);

convertBars(mB->bars(), ctx);

addTripletFeel(mB, measure);

addSection(mB, measure);

addDirection(mB, measure);

_lastMeasure = measure;

fixEmptyMeasures();
}

Expand All @@ -391,10 +374,11 @@ void GPConverter::fixEmptyMeasures()
// Also store root Segment ptr will need it later to delete some rest elems
std::map<track_idx_t, std::vector<std::pair<Segment*, EngravingItem*> > > elems;

auto ntracks = _score->ntracks();
auto type = SegmentType::ChordRest;
size_t ntracks = _score->ntracks();
SegmentType type = SegmentType::ChordRest;
Measure* lastMeasure = _score->lastMeasure();

for (Segment* s = _lastMeasure->segments().first(type); s; s = s->next(type)) {
for (Segment* s = lastMeasure->segments().first(type); s; s = s->next(type)) {
for (track_idx_t i = 0; i < ntracks; ++i) {
auto e = s->element(i);
if (!e) {
Expand All @@ -406,7 +390,7 @@ void GPConverter::fixEmptyMeasures()

for (const auto& [staffIdx, segItemPairs] : elems) {
bool shouldClear = true;
for (auto p : segItemPairs) {
for (const auto& p : segItemPairs) {
// Don't need to do anything if there is not "rest" elem on a staff
if (!p.second->isRest()) {
shouldClear = false;
Expand All @@ -425,7 +409,7 @@ void GPConverter::fixEmptyMeasures()
}

Rest* rest = toRest(segItemPairs.at(0).second);
rest->setTicks(_lastMeasure->ticks());
rest->setTicks(lastMeasure->ticks());
rest->setDurationType(DurationType::V_MEASURE);
}
}
Expand All @@ -435,6 +419,8 @@ void GPConverter::convertBars(const std::vector<std::unique_ptr<GPBar> >& bars,
{
ctx.curTrack = 0;
for (const auto& bar : bars) {
m_chordExistsInBar = false;
m_chordExistsForVoice.fill(false);
convertBar(bar.get(), ctx);
ctx.curTrack += VOICES;
}
Expand All @@ -449,18 +435,16 @@ void GPConverter::convertBar(const GPBar* bar, Context ctx)
}

convertVoices(bar->voices(), ctx);

for (track_idx_t i = ctx.curTrack; i < ctx.curTrack + VOICES; i++) {
hideRestsInEmptyMeasures(i);
}
hideRestsInEmptyMeasures(ctx.curTrack, ctx.curTrack + VOICES);
}

void GPConverter::addBarline(const GPMasterBar* mB, Measure* measure)
void GPConverter::addBarline(const GPMasterBar* mB, Measure* measure, int32_t masterBarIndex)
{
static bool insideFreeTime = false;
size_t staves = _score->staves().size();
bool lastMeasure = (masterBarIndex == _gpDom->masterBars().size() - 1);

if (mB->barlineType() == GPMasterBar::BarlineType::DOUBLE) {
if (!lastMeasure && mB->barlineType() == GPMasterBar::BarlineType::DOUBLE) {
for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
measure->setEndBarLineType(mu::engraving::BarLineType::DOUBLE, staffIdx * VOICES);
}
Expand All @@ -470,7 +454,7 @@ void GPConverter::addBarline(const GPMasterBar* mB, Measure* measure)
auto scoreTimeSig = Fraction(sig.numerator, sig.denominator);

if (mB->freeTime()) {
if (mB->barlineType() != GPMasterBar::BarlineType::DOUBLE) {
if (!lastMeasure && mB->barlineType() != GPMasterBar::BarlineType::DOUBLE) {
for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
measure->setEndBarLineType(mu::engraving::BarLineType::BROKEN, staffIdx * VOICES);
}
Expand Down Expand Up @@ -570,11 +554,11 @@ Fraction GPConverter::convertBeat(const GPBeat* beat, ChordRestContainer& graceC
curSegment->add(cr);

if (cr->isChord()) {
m_chordsInMeasureByVoice[lastMeasure][cr->voice()]++;
m_chordsInMeasure[lastMeasure]++;
m_chordExistsForVoice[ctx.curTrack % VOICES] = true;
m_chordExistsInBar = true;

if (beat->stemOrientationUserDefined()) {
static_cast<Chord*>(cr)->setStemDirection(beat->stemOrientationUp() ? DirectionV::UP : DirectionV::DOWN);
toChord(cr)->setStemDirection(beat->stemOrientationUp() ? DirectionV::UP : DirectionV::DOWN);
}

setBeamMode(beat, cr, lastMeasure, ctx.curTick);
Expand Down Expand Up @@ -1188,17 +1172,29 @@ void GPConverter::fillUncompletedMeasure(const Context& ctx)
}
}

void GPConverter::hideRestsInEmptyMeasures(track_idx_t track)
void GPConverter::hideRestsInEmptyMeasures(track_idx_t startTrack, track_idx_t endTrack)
{
Measure* lastMeasure = _score->lastMeasure();
for (Segment* segment = lastMeasure->first(SegmentType::ChordRest); segment; segment = segment->next(SegmentType::ChordRest)) {
EngravingItem* element = segment->element(track);
if (element && element->isRest()) {
if (m_chordsInMeasureByVoice[lastMeasure][element->voice()] == 0) {
bool measureHasChords = m_chordsInMeasure[lastMeasure] != 0;
if (measureHasChords || (!measureHasChords && element->voice() != 0)) {
toRest(element)->setGap(true);
}
for (Segment* segment = _score->lastMeasure()->first(SegmentType::ChordRest); segment;
segment = segment->next(SegmentType::ChordRest)) {
for (track_idx_t trackIdx = startTrack; trackIdx < endTrack; trackIdx++) {
EngravingItem* element = segment->element(trackIdx);
if (!element || !element->isRest()) {
continue;
}

Rest* rest = toRest(element);
size_t voice = trackIdx % VOICES;
bool mainVoice = (voice == 0);

// hiding rests in secondary voices for measures without any chords
if (!m_chordExistsInBar) {
rest->setGap(!mainVoice);
continue;
}

// hiding rests in voices without chords
if (!m_chordExistsForVoice[voice]) {
rest->setGap(true);
}
}
}
Expand Down Expand Up @@ -2873,13 +2869,7 @@ void GPConverter::setBeamMode(const GPBeat* beat, ChordRest* cr, Measure* measur
}
}

cr->setBeamMode(beamMode);

/// last chord of the measure has always type BeamMode::AUTO, which makes layout incorrect
if (measure != _lastMeasure) {
cr->setBeamMode(m_previousBeamMode);
}

cr->setBeamMode(m_previousBeamMode);
m_previousBeamMode = beamMode;
}
} // namespace mu::iex::guitarpro
15 changes: 8 additions & 7 deletions src/importexport/guitarpro/internal/gtp/gpconverter.h
Expand Up @@ -45,8 +45,8 @@ class GPConverter
using TieMap = std::unordered_map<track_idx_t, std::vector<mu::engraving::Tie*> >;

struct Context {
int32_t masterBarIndex{ 0 };
track_idx_t curTrack{ 0 };
int32_t masterBarIndex = 0;
track_idx_t curTrack = 0;
Fraction curTick;
};

Expand Down Expand Up @@ -83,7 +83,7 @@ class GPConverter
void doAddVolta(const GPMasterBar* mB, Measure* measure);
void addClef(const GPBar* bar, int curTrack);
bool addSimileMark(const GPBar* bar, int curTrack);
void addBarline(const GPMasterBar* mB, Measure* measure);
void addBarline(const GPMasterBar* mB, Measure* measure, int32_t masterBarIndex);

void addTie(const GPNote* gpnote, Note* note, TieMap& ties);
void addFretDiagram(const GPBeat* gpnote, ChordRest* note, const Context& ctx, bool asHarmony = true);
Expand Down Expand Up @@ -145,7 +145,7 @@ class GPConverter
void addTempoMap();
void addInstrumentChanges();
void fillUncompletedMeasure(const Context& ctx);
void hideRestsInEmptyMeasures(track_idx_t track);
void hideRestsInEmptyMeasures(track_idx_t startTrack, track_idx_t endTrack);
int getStringNumberFor(Note* pNote, int pitch) const;
void fillTuplet();
bool tupletParamsChanged(const GPBeat* beat, const ChordRest* cr);
Expand Down Expand Up @@ -201,10 +201,11 @@ class GPConverter

static constexpr mu::engraving::voice_idx_t VOICES = 4;

Measure* _lastMeasure = nullptr;
bool m_showCapo = true; // TODO-gp : settings
std::unordered_map<Measure*, std::array<int, VOICES> > m_chordsInMeasureByVoice; /// if measure has any chord for specific voice, rests are hidden
std::unordered_map<Measure*, size_t> m_chordsInMeasure;

bool m_chordExistsInBar = false;
std::array<bool, VOICES> m_chordExistsForVoice;

mu::engraving::BeamMode m_previousBeamMode = mu::engraving::BeamMode::AUTO;

std::unique_ptr<GPDrumSetResolver> _drumResolver;
Expand Down
Expand Up @@ -108,9 +108,6 @@
<Rest>
<durationType>half</durationType>
</Rest>
<BarLine>
<subtype>end</subtype>
</BarLine>
</voice>
</Measure>
</Staff>
Expand Down
3 changes: 0 additions & 3 deletions src/importexport/guitarpro/tests/data/accent.gp-ref.mscx
Expand Up @@ -132,9 +132,6 @@
<string>3</string>
</Note>
</Chord>
<BarLine>
<subtype>end</subtype>
</BarLine>
</voice>
</Measure>
</Staff>
Expand Down
3 changes: 0 additions & 3 deletions src/importexport/guitarpro/tests/data/accent.gpx-ref.mscx
Expand Up @@ -132,9 +132,6 @@
<string>3</string>
</Note>
</Chord>
<BarLine>
<subtype>end</subtype>
</BarLine>
</voice>
</Measure>
</Staff>
Expand Down
Expand Up @@ -1243,9 +1243,6 @@
<Rest>
<durationType>half</durationType>
</Rest>
<BarLine>
<subtype>end</subtype>
</BarLine>
</voice>
</Measure>
</Staff>
Expand Down
3 changes: 0 additions & 3 deletions src/importexport/guitarpro/tests/data/arpeggio.gp-ref.mscx
Expand Up @@ -147,9 +147,6 @@
<timeStretch>0.966667</timeStretch>
</Arpeggio>
</Chord>
<BarLine>
<subtype>end</subtype>
</BarLine>
</voice>
</Measure>
</Staff>
Expand Down
3 changes: 0 additions & 3 deletions src/importexport/guitarpro/tests/data/arpeggio.gpx-ref.mscx
Expand Up @@ -147,9 +147,6 @@
<timeStretch>0.966667</timeStretch>
</Arpeggio>
</Chord>
<BarLine>
<subtype>end</subtype>
</BarLine>
</voice>
</Measure>
</Staff>
Expand Down
Expand Up @@ -345,9 +345,6 @@
<string>4</string>
</Note>
</Chord>
<BarLine>
<subtype>end</subtype>
</BarLine>
<Spanner type="HarmonicMark">
<prev>
<location>
Expand Down
Expand Up @@ -345,9 +345,6 @@
<string>4</string>
</Note>
</Chord>
<BarLine>
<subtype>end</subtype>
</BarLine>
<Spanner type="HarmonicMark">
<prev>
<location>
Expand Down
Binary file not shown.

0 comments on commit a59b372

Please sign in to comment.