Skip to content

Commit

Permalink
adding GuidoGetPitchPos interface (in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfober committed Dec 5, 2023
1 parent 21f229b commit 161cac9
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 9 deletions.
24 changes: 23 additions & 1 deletion src/engine/graphic/GRMusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ void GRMusic::pagetrace(VGDevice & hdc)
const GRSystem* sys = (*sysl)[j];
const StaffVector * staves = sys->getStaves();
if (staves) {
for (int i = staves->GetMinimum(); i <= staves->GetMaximum(); i++) {
for (int i = staves->GetMinimum(); i < staves->GetMaximum(); i++) {
const GRStaff * staff = staves->Get(i);
if (staff) {
cerr << "staff --- " << i << endl;
Expand Down Expand Up @@ -684,6 +684,28 @@ float GRMusic::getStaffSize(int staffNum) {
return fStaffSizes[staffNum];
}

//-------------------------------------------------------------------------------
GRStaff* GRMusic::getStaff(int staffNum, TYPE_TIMEPOSITION at)
{
for (auto p: mPages) {
for (auto s: *(p->getSystems())) {
const StaffVector * staves = s->getStaves();
if (staves) {
int n = 1;
for (int i = staves->GetMinimum(); i <= staves->GetMaximum(); i++) {
const GRStaff * staff = staves->Get(i);
TYPE_TIMEPOSITION start = staff->getRelativeTimePosition();
TYPE_TIMEPOSITION end = staff->getRelativeEndTimePosition();
if ((n == staffNum) && (start <= at) && (end >= at) && (start != end))
return staves->Get(i);
n++;
}
}
}
}
return nullptr;
}

//-------------------------------------------------------------------------------
ARMusicalVoice* GRMusic::getARVoice (int n)
{
Expand Down
2 changes: 2 additions & 0 deletions src/engine/graphic/GRMusic.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class GRMusic : public GREvent

void setStaffSize(int staffNum, float size);
float getStaffSize(int staffNum);

GRStaff* getStaff(int staffNum, TYPE_TIMEPOSITION date);

std::vector<TCollisionInfo> getCollisions() const { return fCollisions.list(); }

Expand Down
25 changes: 17 additions & 8 deletions src/engine/graphic/GRStaff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ void GRStaff::accept (GRVisitor& visitor)
GuidoPos pos = elts->GetHeadPosition();
while (pos) {
GRNotationElement * e = elts->GetNext(pos);
cerr << "GRStaff::accept " << e << endl;
e->accept (visitor);
}
visitor.visitEnd (this);
Expand Down Expand Up @@ -902,31 +903,39 @@ float GRStaff::getKeyPosition(TYPE_PITCH pit, int numkeys) const
/** \brief Returns the graphical y-position of a note.
Idea: Each clef has a root-note (violin-
clef has G, base-clef has F) and a
clef has G, base-clef has F) and a
corresponding staff line
*/
float GRStaff::getNotePosition(TYPE_PITCH pit, TYPE_REGISTER oct) const
{
//cerr << "GRStaff::getNotePosition " << pit << " " << oct << " state: " << mStaffState.basepit << " " << mStaffState.baseline << " " << mStaffState.baseoct << endl;
return getNotePosition(pit, oct, mStaffState.basepit, mStaffState.baseline, mStaffState.baseoct);
}

// ----------------------------------------------------------------------------
/** \brief Returns the graphical y-position of a note.
*/
float GRStaff::getNotePosition(TYPE_PITCH pit, TYPE_REGISTER oct, int basePitch, int baseLine, int baseOct) const
{
// redundant correction of octave: already shifted when the GRNote is created
// oct -= mStaffState.octava; // depends on current clef.

const float myHalfSpace = getStaffLSPACE() * 0.5f;
float calc = 0;
if (pit >= NOTE_C && pit <= NOTE_H)
{
calc = (float)((mStaffState.basepit - pit ) * myHalfSpace + mStaffState.baseline * getStaffLSPACE() -
((int)oct - mStaffState.baseoct) * (7 * myHalfSpace));
calc = (float)((basePitch - pit ) * myHalfSpace + baseLine * getStaffLSPACE() -
((int)oct - baseOct) * (7 * myHalfSpace));
}
else if (pit>= NOTE_CIS && pit <= NOTE_DIS)
{
calc = (float)((mStaffState.basepit - (pit - 7) )* myHalfSpace + mStaffState.baseline * getStaffLSPACE()-
((int)oct - mStaffState.baseoct) * (7 * myHalfSpace));
calc = (float)((basePitch - (pit - 7) )* myHalfSpace + baseLine * getStaffLSPACE()-
((int)oct - baseOct) * (7 * myHalfSpace));
}
else if (pit>= NOTE_FIS && pit <= NOTE_AIS)
{
calc = (float)((mStaffState.basepit - (pit - 6) )* myHalfSpace + mStaffState.baseline * getStaffLSPACE() -
((int)oct - mStaffState.baseoct) * (7 * myHalfSpace));
calc = (float)((basePitch - (pit - 6) )* myHalfSpace + baseLine * getStaffLSPACE() -
((int)oct - baseOct) * (7 * myHalfSpace));
}
return calc;
}
Expand Down
1 change: 1 addition & 0 deletions src/engine/graphic/GRStaff.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class GRStaff : public GRCompositeNotationElement
const ARMeter * getCurMeter() const { return mStaffState.curmeter; }

virtual float getNotePosition(TYPE_PITCH pit, TYPE_REGISTER oct) const;
virtual float getNotePosition(TYPE_PITCH pit, TYPE_REGISTER oct, int basePitch, int baseLine, int baseOct) const;
virtual GDirection getDefaultThroatDirection(TYPE_PITCH pit, TYPE_REGISTER oct) const;
virtual int getNumHelplines(TYPE_PITCH pit, TYPE_REGISTER oct) const;
virtual VGColor getNoteColor(TYPE_PITCH pit) const;
Expand Down
12 changes: 12 additions & 0 deletions src/engine/include/GUIDOEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,18 @@ as by date. Page numbers start at 1.
*/
GUIDOAPI GuidoErrCode GuidoGetPageDate( CGRHandler inHandleGR, int pageNum, GuidoDate* date);

/** \brief Gives a pitch position on a staff.
\param inHandleGR a Guido opaque handle to a GR structure.
\param staff a staff number (starts at 1).
\param pitch a midi pitch
\param date the target date
\param x on output: the x position
\param y on output: the y position
\return a Guido error code.
*/
GUIDOAPI GuidoErrCode GuidoGetPitchPos( CGRHandler inHandleGR, int staff, int pitch, GuidoDate date, float& x, float& y);


/** \brief Gives the current meter on a given date and voice.
Expand Down
33 changes: 33 additions & 0 deletions src/engine/lib/GUIDOEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ using namespace std;
#include "GRShowVisitor.h"
#include "TagParametersMaps.h"

#include "GRPitchYVisitor.h"

//#include "GMNCodePrintVisitor.h"


Expand Down Expand Up @@ -336,6 +338,16 @@ GUIDOAPI GuidoErrCode GuidoAR2GR( ARHandler ar, const GuidoLayoutSettings * sett
GRHandler grh = CreateGr (ar, gARPageFormat, settings);
if (!gr) return guidoErrMemory;
*gr = grh;

float x, y;
GuidoDate date;
date.num = 3;
date.denom = 1;
GuidoErrCode err = GuidoGetPitchPos (grh, 1, 60, date, x, y);
if (err != guidoNoErr)
cerr << "GuidoAR2GR GuidoGetPitchPos err " << err << endl;
else
cerr << "GuidoAR2GR GuidoGetPitchPos at " << date << " : " << x << " " << y << endl;
return guidoNoErr;
}

Expand Down Expand Up @@ -617,6 +629,27 @@ GUIDOAPI GuidoErrCode GuidoGetPageDate( CGRHandler inHandleGR, int pageNum, Guid
return result ? guidoNoErr : guidoErrBadParameter;
}

// --------------------------------------------------------------------------
// introduced in guido 1.71 [DF Nov 30 2023]
GUIDOAPI GuidoErrCode GuidoGetPitchPos( CGRHandler inHandleGR, int staffnum, int pitch, GuidoDate date, float& x, float& y)
{
if ( ( !inHandleGR ) || ( !inHandleGR->grmusic ) )
return guidoErrInvalidHandle;

TYPE_TIMEPOSITION tp (date.num, date.denom);
GRStaff* staff = inHandleGR->grmusic->getStaff (staffnum, tp);
if (!staff)
return guidoErrBadParameter;

GRPitchYVisitor v;
NVPoint p = v.getPitchPos (staff, pitch, tp);
if (!p.x)
return guidoErrBadParameter;
x = p.x;
y = p.y;
return guidoNoErr;
}

// --------------------------------------------------------------------------
// introduced in guido 1.63 [DF July 1 2016]
GUIDOAPI GuidoErrCode GuidoGetMeterAt (CARHandler inHandleAR, int voicenum, const GuidoDate &date, GuidoMeter& meter)
Expand Down
93 changes: 93 additions & 0 deletions src/engine/operations/GRPitchYVisitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
GUIDO Library
Copyright (C) 2023 D. Fober
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <iostream>

#include "GRPitchYVisitor.h"

#include "GuidoDefs.h"
#include "GRClef.h"
#include "GREmpty.h"
#include "GROctava.h"
#include "GREvent.h"
#include "GRSingleNote.h"
#include "GRSingleRest.h"
#include "GRStaff.h"

using namespace std;

//-------------------------------------------------------------------------------
NVPoint GRPitchYVisitor::getPitchPos (GRStaff* staff, int midipitch, TYPE_TIMEPOSITION date)
{
fBasePitch = NOTE_G;
fBaseLine = 3;
fBaseOct = 1;
fOctava = 0;
fTargetDate = date;
fLastDate = -1;
fLastX = -1;
fDone = false;
staff->accept (*this);
NVPoint p;
if (fDone) {
// convert midi pitch in pitch class and octava
int oct = (midipitch / 12) - 4;
int pitch = midipitch % 12;
pitch = ((pitch < 5) ? (pitch / 2) : (pitch+1) / 2) + 2;
cerr << "GRPitchYVisitor::getPitchPos " << pitch << " " << oct << " date: " << date << endl;
float y = staff->getNotePosition ( pitch, oct, fBasePitch, fBaseLine, fBaseOct);
p.x = fLastX;
p.y = y;
}
cerr << " resultat : " << p << endl;
return p;
}

//-------------------------------------------------------------------------------
void GRPitchYVisitor::visitStart (GRSingleNote* o)
{
if (fDone) return;
cerr << "GRPitchYVisitor::visitStart GRSingleNote " << o << endl;
TYPE_TIMEPOSITION date = o->getRelativeTimePosition();
if (date > fTargetDate) fDone = true;
else fLastX = o->getPosition().x;
}

//-------------------------------------------------------------------------------
void GRPitchYVisitor::visitStart (GREmpty* o)
{
if (fDone) return;
cerr << "GRPitchYVisitor::visitStart GREmpty " << o << endl;
TYPE_TIMEPOSITION date = o->getRelativeTimePosition();
if (date > fTargetDate) fDone = true;
else fLastX = o->getPosition().x;
}

//-------------------------------------------------------------------------------
void GRPitchYVisitor::visitStart (GRSingleRest* o)
{
if (fDone) return;
cerr << "GRPitchYVisitor::visitStart GRSingleRest " << o << endl;
TYPE_TIMEPOSITION date = o->getRelativeTimePosition();
if (date > fTargetDate) fDone = true;
else fLastX = o->getPosition().x;
}

void GRPitchYVisitor::visitStart (GRClef* o)
{
if (fDone) return;
fBasePitch = o->getBasePitch();
fBaseOct = o->getBaseOct();
}

void GRPitchYVisitor::visitStart (GROctava* o)
{
if (fDone) return;
fOctava = o->getOctava();
}
44 changes: 44 additions & 0 deletions src/engine/operations/GRPitchYVisitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
GUIDO Library
Copyright (C) 2023 D. Fober
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef ___GRPitchYVisitor___
#define ___GRPitchYVisitor___

#include "defines.h"
#include "GUIDOEngine.h"
#include "GRVisitor.h"
#include "NVPoint.h"

class GRPitchYVisitor : public GRVisitor
{
int fBasePitch;
int fBaseLine;
int fBaseOct;
int fOctava;
float fLastX;
TYPE_TIMEPOSITION fLastDate = 0;
TYPE_TIMEPOSITION fTargetDate = 0;
bool fDone = false;

public:
GRPitchYVisitor() {}
virtual ~GRPitchYVisitor() {}

NVPoint getPitchPos (GRStaff* staff, int pitch, TYPE_TIMEPOSITION date);

virtual bool voiceMode () { return true; }

virtual void visitStart (GRClef* o);
virtual void visitStart (GROctava* o);
virtual void visitStart (GRSingleNote* o);
virtual void visitStart (GRSingleRest* o);
virtual void visitStart (GREmpty* o);
};

#endif

0 comments on commit 161cac9

Please sign in to comment.