Skip to content

Commit

Permalink
Merge pull request rism-digital#3926 from brdvd/feat/generate-transpo…
Browse files Browse the repository at this point in the history
…sition-keysig

Generate auxiliary KeySig during transposition
  • Loading branch information
lpugin authored Jan 31, 2025
2 parents da06d58 + fcffeb3 commit 25a12df
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
10 changes: 9 additions & 1 deletion include/vrv/transposefunctor.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ class TransposeFunctor : public DocFunctor {
FunctorCode VisitNote(Note *note) override;
FunctorCode VisitRest(Rest *rest) override;
FunctorCode VisitScore(Score *score) override;
FunctorCode VisitStaffDef(StaffDef *staffDef) override;
///@}

protected:
//
/*
* Retrieve corresponding elements
*/
///@{
const KeySig *GetKeySigForStaffDef(const StaffDef *staffDef) const;
int GetStaffNForKeySig(const KeySig *keySig) const;
///@}

private:
//
public:
Expand Down
56 changes: 41 additions & 15 deletions src/transposefunctor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,7 @@ FunctorCode TransposeFunctor::VisitHarm(Harm *harm)
FunctorCode TransposeFunctor::VisitKeySig(KeySig *keySig)
{
// Store current KeySig
int staffN = -1;
const StaffDef *staffDef = vrv_cast<StaffDef *>(keySig->GetFirstAncestor(STAFFDEF));
if (staffDef) {
staffN = staffDef->GetN();
}
else {
const Staff *staff = keySig->GetAncestorStaff(ANCESTOR_ONLY, false);
if (staff) staffN = staff->GetN();
}
const int staffN = this->GetStaffNForKeySig(keySig);
m_keySigForStaffN[staffN] = keySig;

// Transpose
Expand Down Expand Up @@ -234,6 +226,41 @@ FunctorCode TransposeFunctor::VisitScore(Score *score)
return FUNCTOR_CONTINUE;
}

FunctorCode TransposeFunctor::VisitStaffDef(StaffDef *staffDef)
{
if (!this->GetKeySigForStaffDef(staffDef)) {
KeySig *keySig = new KeySig();
staffDef->AddChild(keySig);
LogWarning("Adding auxiliary KeySig for transposition");
}

return FUNCTOR_CONTINUE;
}

const KeySig *TransposeFunctor::GetKeySigForStaffDef(const StaffDef *staffDef) const
{
const KeySig *keySig = vrv_cast<const KeySig *>(staffDef->FindDescendantByType(KEYSIG));
if (!keySig) {
const ScoreDef *scoreDef = vrv_cast<const ScoreDef *>(staffDef->GetFirstAncestor(SCOREDEF));
keySig = vrv_cast<const KeySig *>(scoreDef->FindDescendantByType(KEYSIG, 1));
}
return keySig;
}

int TransposeFunctor::GetStaffNForKeySig(const KeySig *keySig) const
{
int staffN = -1;
const StaffDef *staffDef = vrv_cast<const StaffDef *>(keySig->GetFirstAncestor(STAFFDEF));
if (staffDef) {
staffN = staffDef->GetN();
}
else {
const Staff *staff = keySig->GetAncestorStaff(ANCESTOR_ONLY, false);
if (staff) staffN = staff->GetN();
}
return staffN;
}

//----------------------------------------------------------------------------
// TransposeSelectedMdivFunctor
//----------------------------------------------------------------------------
Expand Down Expand Up @@ -362,12 +389,11 @@ FunctorCode TransposeToSoundingPitchFunctor::VisitStaff(Staff *staff)

FunctorCode TransposeToSoundingPitchFunctor::VisitStaffDef(StaffDef *staffDef)
{
// Retrieve the key signature
const KeySig *keySig = vrv_cast<const KeySig *>(staffDef->FindDescendantByType(KEYSIG));
if (!keySig) {
const ScoreDef *scoreDef = vrv_cast<const ScoreDef *>(staffDef->GetFirstAncestor(SCOREDEF));
keySig = vrv_cast<const KeySig *>(scoreDef->FindDescendantByType(KEYSIG));
}
// Call base method (creates KeySig if missing)
TransposeFunctor::VisitStaffDef(staffDef);

const KeySig *keySig = this->GetKeySigForStaffDef(staffDef);

// Determine and store the transposition interval (based on keySig)
if (keySig && staffDef->HasTransSemi() && staffDef->HasN()) {
const int fifths = keySig->GetFifthsInt();
Expand Down

0 comments on commit 25a12df

Please sign in to comment.