Skip to content

Commit

Permalink
guitar bend import: choosing notes by index in chord, instead of pitch
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpavlov96 committed Jan 8, 2025
1 parent 8aa7c53 commit a92c81c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ namespace mu::iex::guitarpro {
constexpr int BEND_DIVISIONS = 60;

static void fillChordDurationsFromBendDiagram(BendDataContext& bendDataCtx, const BendDataCollector::ImportedBendInfo& importedInfo);
static void fillBendDataForNote(BendDataContext& bendDataCtx, const BendDataCollector::ImportedBendInfo& importedInfo);
static void fillBendDataForNote(BendDataContext& bendDataCtx, const BendDataCollector::ImportedBendInfo& importedInfo,
int noteIndexInChord);
static std::vector<BendDataCollector::BendSegment> bendSegmentsFromPitchValues(const PitchValues& pitchValues, bool noteTiedBack);
static bool isSlightBend(const BendDataCollector::ImportedBendInfo& importedInfo);
static BendDataCollector::ImportedBendInfo fillBendInfo(const Note* note, const PitchValues& pitchValues);

void BendDataCollector::storeBendData(mu::engraving::Note* note, const mu::engraving::PitchValues& pitchValues)
void BendDataCollector::storeBendData(const mu::engraving::Note* note, const mu::engraving::PitchValues& pitchValues)
{
if (!pitchValues.empty()) {
m_bendInfoForNote[note->track()][note->tick().ticks()][note->pitch()] = fillBendInfo(note, pitchValues);
m_bendInfoForNote[note->track()][note->tick().ticks()][note] = fillBendInfo(note, pitchValues);
}
}

Expand All @@ -61,8 +62,9 @@ BendDataContext BendDataCollector::collectBendDataContext()

for (const auto& [track, trackInfo] : m_bendInfoForNote) {
for (const auto& [tick, tickInfo] : trackInfo) {
for (const auto& [pitch, importedBendInfo] : tickInfo) {
fillBendDataForNote(bendDataCtx, importedBendInfo);
for (const auto& [note, importedBendInfo] : tickInfo) {
int idx = muse::indexOf(note->chord()->notes(), note);
fillBendDataForNote(bendDataCtx, importedBendInfo, idx);
}
}
}
Expand Down Expand Up @@ -173,7 +175,7 @@ static bool isSlightBend(const BendDataCollector::ImportedBendInfo& importedInfo
return false;
}

void fillSlightBendData(BendDataContext& bendDataCtx, const BendDataCollector::ImportedBendInfo& importedInfo)
void fillSlightBendData(BendDataContext& bendDataCtx, const BendDataCollector::ImportedBendInfo& importedInfo, int noteIndexInChord)
{
const Note* note = importedInfo.note;
const Chord* chord = note->chord();
Expand All @@ -199,7 +201,7 @@ void fillSlightBendData(BendDataContext& bendDataCtx, const BendDataCollector::I
slightBendNoteData.endFactor = (double)(firstSeg.endTime + 1) / BEND_DIVISIONS;
slightBendNoteData.type = GuitarBendType::SLIGHT_BEND;

slightBendChordData.noteDataByPitch[note->pitch()] = std::move(slightBendNoteData);
slightBendChordData.noteDataByIdx[noteIndexInChord] = std::move(slightBendNoteData);
}

static bool isFirstPrebend(const BendDataCollector::ImportedBendInfo& importedInfo)
Expand All @@ -208,7 +210,7 @@ static bool isFirstPrebend(const BendDataCollector::ImportedBendInfo& importedIn
return firstSeg.startTime == firstSeg.endTime;
}

static void fillPrebendData(BendDataContext& bendDataCtx, const BendDataCollector::ImportedBendInfo& importedInfo)
static void fillPrebendData(BendDataContext& bendDataCtx, const BendDataCollector::ImportedBendInfo& importedInfo, int noteIndexInChord)
{
const Note* note = importedInfo.note;
Fraction tick = note->tick();
Expand All @@ -221,11 +223,11 @@ static void fillPrebendData(BendDataContext& bendDataCtx, const BendDataCollecto
prebendNoteData.type = GuitarBendType::PRE_BEND;
prebendNoteData.quarterTones = firstSeg.endPitch / 25;

prebendChordData.noteDataByPitch[note->pitch()] = std::move(prebendNoteData);
prebendChordData.noteDataByIdx[noteIndexInChord] = std::move(prebendNoteData);
}

static void fillNormalBendData(BendDataContext& bendDataCtx, const BendDataCollector::ImportedBendInfo& importedInfo,
size_t startIndex)
size_t startIndex, int noteIndexInChord)
{
if (startIndex >= importedInfo.segments.size()) {
return;
Expand Down Expand Up @@ -265,7 +267,7 @@ static void fillNormalBendData(BendDataContext& bendDataCtx, const BendDataColle
BendDataContext::BendNoteData bendNoteData;
bendNoteData.type = GuitarBendType::BEND;
bendNoteData.quarterTones = seg.endPitch / 25;
bendChordData.noteDataByPitch[note->pitch()] = std::move(bendNoteData);
bendChordData.noteDataByIdx[noteIndexInChord] = std::move(bendNoteData);

currentTick += tickDuration;
currentIndex++;
Expand Down Expand Up @@ -368,7 +370,7 @@ static void fillChordDurationsFromBendDiagram(BendDataContext& bendDataCtx, cons
splitBendChordDurations(bendDataCtx, importedInfo, startIndex);
}

static void fillBendDataForNote(BendDataContext& bendDataCtx, const BendDataCollector::ImportedBendInfo& importedInfo)
static void fillBendDataForNote(BendDataContext& bendDataCtx, const BendDataCollector::ImportedBendInfo& importedInfo, int noteIndexInChord)
{
const Note* note = importedInfo.note;
IF_ASSERT_FAILED(note) {
Expand All @@ -383,20 +385,20 @@ static void fillBendDataForNote(BendDataContext& bendDataCtx, const BendDataColl
}

if (isSlightBend(importedInfo)) {
fillSlightBendData(bendDataCtx, importedInfo);
fillSlightBendData(bendDataCtx, importedInfo, noteIndexInChord);
return;
}

size_t startIndex = 0;

if (isFirstPrebend(importedInfo)) {
fillPrebendData(bendDataCtx, importedInfo);
fillPrebendData(bendDataCtx, importedInfo, noteIndexInChord);
startIndex = 1;
if (importedInfo.segments.size() > 1 && importedInfo.segments[1].pitchDiff() == 0) {
startIndex = 2;
}
}

fillNormalBendData(bendDataCtx, importedInfo, startIndex);
fillNormalBendData(bendDataCtx, importedInfo, startIndex, noteIndexInChord);
}
} // namespace mu::iex::guitarpro
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BendDataCollector
{
public:

void storeBendData(mu::engraving::Note* note, const mu::engraving::PitchValues& pitchValues);
void storeBendData(const mu::engraving::Note* note, const mu::engraving::PitchValues& pitchValues);
BendDataContext collectBendDataContext();

struct BendSegment {
Expand All @@ -58,6 +58,7 @@ class BendDataCollector
};

private:
std::unordered_map<mu::engraving::track_idx_t, std::map<int, std::map<int /*pitch*/, ImportedBendInfo> > > m_bendInfoForNote;
std::unordered_map<mu::engraving::track_idx_t,
std::map<int, std::unordered_map<const mu::engraving::Note*, ImportedBendInfo> > > m_bendInfoForNote;
};
} // mu::iex::guitarpro
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct BendDataContext {

struct BendChordData {
mu::engraving::Fraction startTick;
std::map<int /* pitch */, BendNoteData> noteDataByPitch;
std::map<int /* idx in chord */, BendNoteData> noteDataByIdx;
};

std::unordered_map<mu::engraving::track_idx_t, std::map<int, std::vector<mu::engraving::Fraction> > > bendChordDurations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,19 @@ static void createGuitarBends(const BendDataContext& bendDataCtx, mu::engraving:
std::sort(endChordNotes.begin(), endChordNotes.end(), [](Note* l, Note* r) {
return l->pitch() < r->pitch();
});

auto bendInfoIt = bendChordData.noteDataByPitch.begin();
for (size_t noteIndex = 0; noteIndex < endChordNotes.size(); noteIndex++) {
Note* startNote = startChordNotes[noteIndex];
Note* note = endChordNotes[noteIndex];

if (bendChordData.noteDataByPitch.size() <= noteIndex) {
LOGE() << "bend import error : bend data filled incorrectly, not all bends will be created";
return;
if (bendChordData.noteDataByIdx.find(noteIndex) == bendChordData.noteDataByIdx.end()) {
Tie* tie = Factory::createTie(score->dummy());
startNote->add(tie);
tie->setEndNote(note);
note->setTieBack(tie);
continue;
}

const BendDataContext::BendNoteData& bendNoteData = bendInfoIt->second;
Note* startNote = startChordNotes[noteIndex];

const auto& bendNoteData = bendChordData.noteDataByIdx.at(noteIndex);
int pitch = bendNoteData.quarterTones / 2;

if (bendNoteData.type == GuitarBendType::PRE_BEND) {
Expand Down Expand Up @@ -225,8 +225,6 @@ static void createGuitarBends(const BendDataContext& bendDataCtx, mu::engraving:

tiedNote = tie->endNote();
}

bendInfoIt = std::next(bendInfoIt);
}
}
} // namespace mu::iex::guitarpro

0 comments on commit a92c81c

Please sign in to comment.