Skip to content

Commit

Permalink
Refactor PAE Duration handling
Browse files Browse the repository at this point in the history
This change extracts the PAE duration handling into a static method that
can be used to convert durations to their PAE representations.

It also adds a new feature to the feature extractor that includes the note duration
on the PAE note.
  • Loading branch information
ahankinson committed Mar 13, 2024
1 parent 74cbd1f commit dcbdf2e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
1 change: 1 addition & 0 deletions include/vrv/featureextractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class FeatureExtractor {
std::list<const Note *> m_previousNotes;

jsonxx::Array m_pitchesChromatic;
jsonxx::Array m_pitchesChromaticWithDuration;
jsonxx::Array m_pitchesDiatonic;
jsonxx::Array m_pitchesIds;

Expand Down
4 changes: 4 additions & 0 deletions include/vrv/iopae.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class PAEOutput : public Output {
*/
bool WriteObjectEnd(Object *object) override;

/**
* Helper method to return a string representation of the PAE duration.
*/
static std::string GetPaeDur(data_DURATION dur, int ndots);
private:
/**
* @name Methods for writing containers (measures, staff, etc) scoreDef and related.
Expand Down
13 changes: 12 additions & 1 deletion src/featureextractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "chord.h"
#include "doc.h"
#include "gracegrp.h"
#include "iopae.h"
#include "layer.h"
#include "mdiv.h"
#include "measure.h"
Expand Down Expand Up @@ -70,10 +71,16 @@ void FeatureExtractor::Extract(const Object *object)
}

std::stringstream pitch;
std::stringstream pitchWithDuration;

pitchWithDuration << PAEOutput::GetPaeDur(note->GetDur(), note->GetDots());

data_OCTAVE oct = note->GetOct();
char octSign = (oct > 3) ? '\'' : ',';
int signCount = (oct > 3) ? (oct - 3) : (4 - oct);
pitch << std::string(signCount, octSign);
std::string octaves = std::string(signCount, octSign);
pitch << octaves;
pitchWithDuration << octaves;

const Accid *accid = vrv_cast<const Accid *>(note->FindDescendantByType(ACCID));
if (accid) {
Expand All @@ -98,13 +105,16 @@ void FeatureExtractor::Extract(const Object *object)
default: accidStr = accidStrWritten;
}
pitch << accidStr;
pitchWithDuration << accidStr;
}

std::string pname = note->AttPitch::PitchnameToStr(note->GetPname());
std::transform(pname.begin(), pname.end(), pname.begin(), ::toupper);
pitch << pname;
pitchWithDuration << pname;

m_pitchesChromatic << pitch.str();
m_pitchesChromaticWithDuration << pitchWithDuration.str();
m_pitchesDiatonic << pname;
jsonxx::Array pitchesIds;
pitchesIds << note->GetID();
Expand Down Expand Up @@ -145,6 +155,7 @@ void FeatureExtractor::ToJson(std::string &output)
{
jsonxx::Object o;

o << "pitchesChromaticWithDuration" << m_pitchesChromaticWithDuration;
o << "pitchesChromatic" << m_pitchesChromatic;
o << "pitchesDiatonic" << m_pitchesDiatonic;
o << "pitchesIds" << m_pitchesIds;
Expand Down
57 changes: 33 additions & 24 deletions src/iopae.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,38 @@ void PAEOutput::WriteTupletEnd(Tuplet *tuplet)
m_streamStringOutput << ";" << tuplet->GetNum() << ")";
}

std::string PAEOutput::GetPaeDur(data_DURATION ndur, int ndots)
{
std::string dur;
switch (ndur) {
case (DURATION_long): dur = "0"; break;
case (DURATION_breve): dur = "9"; break;
case (DURATION_1): dur = "1"; break;
case (DURATION_2): dur = "2"; break;
case (DURATION_4): dur = "4"; break;
case (DURATION_8): dur = "8"; break;
case (DURATION_16): dur = "6"; break;
case (DURATION_32): dur = "3"; break;
case (DURATION_64): dur = "5"; break;
case (DURATION_128): dur = "7"; break;
case (DURATION_maxima): dur = "0"; break;
case (DURATION_longa): dur = "0"; break;
case (DURATION_brevis): dur = "9"; break;
case (DURATION_semibrevis): dur = "1"; break;
case (DURATION_minima): dur = "2"; break;
case (DURATION_semiminima): dur = "4"; break;
case (DURATION_fusa): dur = "8"; break;
case (DURATION_semifusa): dur = "6"; break;
default: LogWarning("Unsupported duration"); dur = "4";
}

if (ndots > 0) {
dur += std::string(ndots, '.');
}

return dur;
}

void PAEOutput::WriteDur(DurationInterface *interface)
{
assert(interface);
Expand All @@ -550,30 +582,7 @@ void PAEOutput::WriteDur(DurationInterface *interface)
if ((interface->GetDur() != m_currentDur) || (ndots != m_currentDots)) {
m_currentDur = interface->GetDur();
m_currentDots = ndots;
std::string dur;
switch (m_currentDur) {
case (DURATION_long): dur = "0"; break;
case (DURATION_breve): dur = "9"; break;
case (DURATION_1): dur = "1"; break;
case (DURATION_2): dur = "2"; break;
case (DURATION_4): dur = "4"; break;
case (DURATION_8): dur = "8"; break;
case (DURATION_16): dur = "6"; break;
case (DURATION_32): dur = "3"; break;
case (DURATION_64): dur = "5"; break;
case (DURATION_128): dur = "7"; break;
case (DURATION_maxima): dur = "0"; break;
case (DURATION_longa): dur = "0"; break;
case (DURATION_brevis): dur = "9"; break;
case (DURATION_semibrevis): dur = "1"; break;
case (DURATION_minima): dur = "2"; break;
case (DURATION_semiminima): dur = "4"; break;
case (DURATION_fusa): dur = "8"; break;
case (DURATION_semifusa): dur = "6"; break;
default: LogWarning("Unsupported duration"); dur = "4";
}
m_streamStringOutput << dur;
m_streamStringOutput << std::string(m_currentDots, '.');
m_streamStringOutput << GetPaeDur(interface->GetDur(), m_currentDots);
}
}

Expand Down

0 comments on commit dcbdf2e

Please sign in to comment.