cwScore.h,cpp : Updates to base object on score_parse.

This commit is contained in:
kevin 2023-08-20 17:11:53 -04:00
parent 1572e23910
commit 722c845187
2 changed files with 207 additions and 238 deletions

View File

@ -6,12 +6,13 @@
#include "cwObject.h" #include "cwObject.h"
#include "cwMidi.h" #include "cwMidi.h"
#include "cwFileSys.h" #include "cwFileSys.h"
#include "cwDynRefTbl.h"
#include "cwScoreParse.h"
#include "cwSfScore.h" #include "cwSfScore.h"
#include "cwCsv.h" #include "cwCsv.h"
#include "cwNumericConvert.h" #include "cwNumericConvert.h"
#include "cwTime.h" #include "cwTime.h"
#include "cwVectOps.h" #include "cwVectOps.h"
#include "cwSfScoreParser.h"
#include "cwMidi.h" #include "cwMidi.h"
namespace cw namespace cw
@ -21,9 +22,13 @@ namespace cw
typedef struct sfscore_str typedef struct sfscore_str
{ {
parser::handle_t parserH; bool deleteParserH_Fl;
score_parse::handle_t parserH;
double srate;
event_t* eventA; event_t* eventA;
unsigned eventAllocN;
unsigned eventN; unsigned eventN;
set_t* setA; set_t* setA;
@ -42,7 +47,7 @@ namespace cw
event_t* event; event_t* event;
loc_t* loc; loc_t* loc;
section_t* section; section_t* section;
set_t* setA[ kScVarCnt ]; set_t* setA[ score_parse::kVarCnt ];
} rpt_event_t; } rpt_event_t;
@ -85,52 +90,86 @@ namespace cw
mem::release(p->eventA); mem::release(p->eventA);
if( p->deleteParserH_Fl )
destroy(p->parserH); destroy(p->parserH);
mem::release(p); mem::release(p);
return rc; return rc;
} }
event_t* _hash_to_event( sfscore_t* p, unsigned hash )
{
for(unsigned i=0; i<p->eventN; ++i)
if( p->eventA[i].hash == hash )
return p->eventA + i;
return nullptr;
}
double _calc_frac( double rval, unsigned dot_cnt )
{
double mult = 1.0;
if(dot_cnt > 0)
{
for(unsigned i=0; i<dot_cnt; ++i)
mult += 1.0 / (1 << i);
}
return mult/rval;
}
rc_t _create_event_array( sfscore_t* p ) rc_t _create_event_array( sfscore_t* p )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
const parser::p_event_t* pe_array = parser::event_array(p->parserH); const score_parse::event_t* pe_array = score_parse::event_array(p->parserH);
p->eventN = parser::event_count(p->parserH); p->eventAllocN = score_parse::event_count(p->parserH);
p->eventN = 0;
if( pe_array == nullptr || p->eventN == 0 ) if( pe_array == nullptr || p->eventAllocN == 0 )
{ {
rc = cwLogError(kInvalidStateRC,"No events were found."); rc = cwLogError(kInvalidStateRC,"No events were found.");
goto errLabel; goto errLabel;
} }
p->eventA = mem::allocZ<event_t>(p->eventN); p->eventA = mem::allocZ<event_t>(p->eventAllocN);
p->locN = 0; p->locN = 0;
for(unsigned i=0; i<p->eventN; ++i) for(unsigned i=0; i<p->eventAllocN; ++i)
{ {
event_t* e = p->eventA + i; event_t* e = p->eventA + p->eventN;
const parser::p_event_t* pe = pe_array + i; const score_parse::event_t* pe = pe_array + i;
e->type = pe->typeId; if( cwIsFlag(pe->flags,score_parse::kOnsetFl) )
e->secs = pe->secs; {
e->index = pe->index; e->type = pe->opId;
e->locIdx = pe->locIdx; e->secs = pe->sec;
e->pitch = pe->pitch; e->index = p->eventN;
e->vel = pe->vel; e->locIdx = pe->oloc;
e->flags = pe->flags; e->pitch = pe->d0;
e->dynVal = pe->dynVal; e->vel = pe->d1;
e->frac = pe->t_frac; e->flags = 0;
e->dynVal = pe->dynLevel;
e->frac = _calc_frac(pe->rval, pe->dotCnt);
e->barNumb = pe->barNumb; e->barNumb = pe->barNumb;
e->barNoteIdx = pe->barNoteIdx; e->barNoteIdx = pe->barPitchIdx;
e->csvRowNumb = pe->csvRowNumb; e->csvRowNumb = pe->csvRowNumb;
e->line = pe->line; e->line = pe->csvRowNumb;
e->csvEventId = pe->csvEventId; e->csvEventId = kInvalidId; // pe->csvId;
e->hash = pe->hash;
for(unsigned i = score_parse::kMinVarIdx; i<score_parse::kVarCnt; ++i)
{
e->varA[i] = pe->varA[i].flags;
e->flags |= pe->varA[i].flags;
}
if( e->locIdx > p->locN ) if( e->locIdx > p->locN )
p->locN = e->locIdx; p->locN = e->locIdx;
p->eventN += 1;
}
} }
p->locN += 1; // add one to convert locN from index to count p->locN += 1; // add one to convert locN from index to count
@ -190,7 +229,7 @@ namespace cw
rc_t _create_section_array( sfscore_t* p ) rc_t _create_section_array( sfscore_t* p )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
p->sectionN = parser::section_count(p->parserH); p->sectionN = score_parse::section_count(p->parserH);
// the location array must have already been created. // the location array must have already been created.
assert( p->locA != nullptr ); assert( p->locA != nullptr );
@ -204,18 +243,29 @@ namespace cw
{ {
p->sectionA = mem::allocZ<section_t>(p->sectionN); p->sectionA = mem::allocZ<section_t>(p->sectionN);
const parser::p_section_t* ps = parser::section_list(p->parserH); const score_parse::section_t* ps = score_parse::section_list(p->parserH);
for(unsigned i=0; i<p->sectionN; ++i) for(unsigned i=0; i<p->sectionN; ++i)
{
if( ps->begEvent != nullptr )
{ {
section_t* section = p->sectionA + i; section_t* section = p->sectionA + i;
event_t* begEvt = _hash_to_event(p,ps->begEvent->hash);
if( begEvt == nullptr )
{
rc = cwLogError(kInvalidStateRC,"The section '%s' does not have a 'begin' event with hash:%x.",cwStringNullGuard(ps->label),ps->begEvent->hash);
goto errLabel;
}
section->label = mem::duplStr(ps->label); section->label = mem::duplStr(ps->label);
section->index = i; section->index = i;
section->begEvtIndex = ps->begEvtIdx; section->begEvtIndex = begEvt->index;
section->locPtr = p->locA + p->eventA[ps->begEvtIdx].locIdx; section->locPtr = p->locA + p->eventA[begEvt->index].locIdx;
section->locPtr->begSectPtr = section; section->locPtr->begSectPtr = section;
for(unsigned j=0; j<kScVarCnt; ++j) for(unsigned j=0; j<score_parse::kVarCnt; ++j)
section->vars[j] = DBL_MAX; section->vars[j] = DBL_MAX;
}
ps = ps->link; ps = ps->link;
} }
@ -237,9 +287,9 @@ namespace cw
rc_t _create_set_array( sfscore_t* p ) rc_t _create_set_array( sfscore_t* p )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
const parser::p_set_t* ps = set_list(p->parserH); const score_parse::set_t* ps = set_list(p->parserH);
p->setN = parser::set_count(p->parserH); p->setN = score_parse::set_count(p->parserH);
if( p->setN == 0 ) if( p->setN == 0 )
{ {
rc = cwLogError(kInvalidStateRC,"No sets were found."); rc = cwLogError(kInvalidStateRC,"No sets were found.");
@ -263,7 +313,15 @@ namespace cw
set->eleCnt = ps->eventN; set->eleCnt = ps->eventN;
set->eleArray = mem::allocZ<event_t*>(set->eleCnt); set->eleArray = mem::allocZ<event_t*>(set->eleCnt);
for(unsigned j=0; j<set->eleCnt; ++j) for(unsigned j=0; j<set->eleCnt; ++j)
set->eleArray[j] = p->eventA + ps->eventA[j]->index; {
set->eleArray[j] = _hash_to_event(p, ps->eventA[j]->hash );
if( set->eleArray[j] == nullptr )
{
rc = cwLogError(kInvalidStateRC,"The '%s' set event in measure:%i with hash %x (CSV Row:%i) could not be found.",score_parse::var_index_to_char(ps->varTypeId),ps->eventA[j]->barNumb,ps->eventA[j]->hash,ps->eventA[j]->csvRowNumb);
goto errLabel;
}
}
// add this set to the setList for the set's end loc // add this set to the setList for the set's end loc
if( set->eleCnt > 0 ) if( set->eleCnt > 0 )
@ -274,9 +332,9 @@ namespace cw
} }
// set the target-section related fields fro this set // set the target-section related fields fro this set
if( ps->target_section != nullptr ) if( ps->targetSection != nullptr )
{ {
if((section = _label_to_section(p,ps->target_section->label)) == nullptr ) if((section = _label_to_section(p,ps->targetSection->label)) == nullptr )
{ {
rc = cwLogError(kInvalidIdRC,"The section '%s' was not found."); rc = cwLogError(kInvalidIdRC,"The section '%s' was not found.");
goto errLabel; goto errLabel;
@ -297,6 +355,46 @@ namespace cw
return rc; return rc;
} }
rc_t _create( handle_t& hRef,
score_parse::handle_t spH,
bool deleteParserH_Fl )
{
rc_t rc;
if((rc = destroy(hRef)) != kOkRC )
return rc;
sfscore_t* p = mem::allocZ<sfscore_t>();
p->deleteParserH_Fl = deleteParserH_Fl;
p->parserH = spH;
if((rc = _create_event_array( p )) != kOkRC )
goto errLabel;
if((rc = _create_loc_array( p )) != kOkRC )
goto errLabel;
if((rc = _create_section_array( p )) != kOkRC )
goto errLabel;
if((rc = _create_set_array( p )) != kOkRC )
goto errLabel;
hRef.set(p);
errLabel:
if( rc != kOkRC )
{
rc = cwLogError(rc,"sfscore create failed.");
_destroy(p);
}
return rc;
}
void _report_print( sfscore_t* p, rpt_event_t* rptA, unsigned rptN ) void _report_print( sfscore_t* p, rpt_event_t* rptA, unsigned rptN )
{ {
unsigned bar0 = 0; unsigned bar0 = 0;
@ -305,7 +403,7 @@ namespace cw
const char* sec_str = "S:"; const char* sec_str = "S:";
const char* bar_str = "B:"; const char* bar_str = "B:";
printf("e idx loc secs op sectn sdx bar bdx scip vel frac\n"); printf("e idx oloc secs op sectn sdx bar bdx scip vel frac\n");
printf("----- ----- ------- --- ------ --- ----- --- ---- --- -----\n"); printf("----- ----- ------- --- ------ --- ----- --- ---- --- -----\n");
@ -327,7 +425,7 @@ namespace cw
e->index, e->index,
e->locIdx, e->locIdx,
e->secs, e->secs,
opcode_id_to_label(e->type), score_parse::opcode_id_to_label(e->type),
d_sec_str, d_sec_str,
section==nullptr ? " " : cwStringNullGuard(section->label), section==nullptr ? " " : cwStringNullGuard(section->label),
section->index, section->index,
@ -338,17 +436,15 @@ namespace cw
e->vel, e->vel,
e->frac ); e->frac );
for(unsigned refVarTypeId=kMinVarScId; refVarTypeId<kScVarCnt; ++refVarTypeId) for(unsigned vi=score_parse::kMinVarIdx; vi<score_parse::kVarCnt; ++vi)
{ {
set_t* set = r->setA[refVarTypeId]; set_t* set = r->setA[vi];
if( set == nullptr || set->sectCnt==0 ) if( set == nullptr || set->sectCnt==0 )
printf(" "); printf(" ");
else else
{ {
unsigned varRefFlags = var_type_id_to_mask(refVarTypeId);
const char* sect_label = set->sectArray[0]==nullptr ? "****" : set->sectArray[0]->label; const char* sect_label = set->sectArray[0]==nullptr ? "****" : set->sectArray[0]->label;
printf("%c-%03i-%s ",var_type_flag_to_char(e->flags & varRefFlags), set->id, sect_label); printf("%s-%03i-%s ",score_parse::var_flags_to_char(e->varA[vi]), set->id, sect_label);
} }
} }
@ -424,47 +520,34 @@ namespace cw
} }
} }
cw::rc_t cw::sfscore::create( handle_t& hRef,
score_parse::handle_t spH )
{
return _create(hRef,spH,false);
}
cw::rc_t cw::sfscore::create( handle_t& hRef, cw::rc_t cw::sfscore::create( handle_t& hRef,
const char* fname, const char* fname,
double srate, double srate,
const dyn_ref_t* dynRefA, dyn_ref_tbl::handle_t dynRefH)
unsigned dynRefN )
{ {
rc_t rc; rc_t rc;
if((rc = destroy(hRef)) != kOkRC ) score_parse::handle_t spH;
return rc;
sfscore_t* p = mem::allocZ<sfscore_t>(); if((rc = score_parse::create(spH,fname,srate,dynRefH)) != kOkRC )
if((rc = parser::create(p->parserH,fname,dynRefA,dynRefN)) != kOkRC )
{ {
rc = cwLogError(rc,"sfscore parse failed."); rc = cwLogError(rc,"sfscore parse failed.");
goto errLabel; goto errLabel;
} }
if((rc = _create_event_array( p )) != kOkRC )
goto errLabel;
if((rc = _create_loc_array( p )) != kOkRC ) rc = _create(hRef,spH,true);
goto errLabel;
if((rc = _create_section_array( p )) != kOkRC )
goto errLabel;
if((rc = _create_set_array( p )) != kOkRC )
goto errLabel;
hRef.set(p);
errLabel: errLabel:
if( rc != kOkRC )
{
rc = cwLogError(rc,"sfscore create failed.");
_destroy(p);
}
return rc; return rc;
} }
cw::rc_t cw::sfscore::destroy( handle_t& hRef ) cw::rc_t cw::sfscore::destroy( handle_t& hRef )
@ -488,6 +571,14 @@ cw::rc_t cw::sfscore::destroy( handle_t& hRef )
} }
double cw::sfscore::sample_rate( handle_t& h )
{
sfscore_t* p = _handleToPtr(h);
return sample_rate(p->parserH);
}
unsigned cw::sfscore::event_count( handle_t h ) unsigned cw::sfscore::event_count( handle_t h )
{ {
sfscore_t* p = _handleToPtr(h); sfscore_t* p = _handleToPtr(h);
@ -500,6 +591,16 @@ cw::sfscore::event_t* cw::sfscore::event( handle_t h, unsigned idx )
return p->eventA + idx; return p->eventA + idx;
} }
cw::sfscore::event_t* cw::sfscore::hash_to_event( handle_t h, unsigned hash )
{
sfscore_t* p = _handleToPtr(h);
for(unsigned i=0; p->eventN; ++i)
if( p->eventA[i].hash == hash )
return p->eventA + i;
return nullptr;
}
unsigned cw::sfscore::loc_count( handle_t h ) unsigned cw::sfscore::loc_count( handle_t h )
{ {
sfscore_t* p = _handleToPtr(h); sfscore_t* p = _handleToPtr(h);
@ -512,108 +613,16 @@ cw::sfscore::loc_t* cw::sfscore::loc( handle_t h, unsigned idx )
return p->locA + idx; return p->locA + idx;
} }
cw::rc_t cw::sfscore::parse_dyn_ref_cfg( const object_t* cfg, dyn_ref_t*& refArrayRef, unsigned& refArrayNRef) void cw::sfscore::report( handle_t h, const char* out_fname )
{
rc_t rc = kOkRC;
dyn_ref_t* dynRefA = nullptr;
refArrayRef = nullptr;
refArrayNRef = 0;
// parse the dynamics ref. array
unsigned dynRefN = cfg->child_count();
if( dynRefN == 0 )
cwLogWarning("The dynamic reference array cfg. is empty.");
else
{
dynRefA = mem::allocZ<dyn_ref_t>(dynRefN);
for(unsigned i=0; i<dynRefN; ++i)
{
const object_t* pair = cfg->child_ele(i);
if( !pair->is_pair() || pair->pair_label()==nullptr || pair->pair_value()==nullptr || pair->pair_value()->value( dynRefA[i].vel)!=kOkRC )
{
rc = cwLogError(kSyntaxErrorRC,"Error parsing the dynamics reference array.");
goto errLabel;
}
dynRefA[i].label = pair->pair_label();
}
}
errLabel:
if( rc != kOkRC )
mem::release(dynRefA);
else
{
refArrayRef = dynRefA;
refArrayNRef = dynRefN;
}
return rc;
}
void cw::sfscore::report( handle_t h )
{ {
sfscore_t* p = _handleToPtr(h); sfscore_t* p = _handleToPtr(h);
if( p->parserH.isValid() )
{
//printf("Score Parser Report\n");
//report(p->parserH);
}
printf("Score Report\n"); printf("Score Report\n");
_report(p); _report(p);
} }
cw::rc_t cw::sfscore::test( const object_t* cfg ) void cw::sfscore::parse_report( handle_t h )
{ {
rc_t rc = kOkRC; sfscore_t* p = _handleToPtr(h);
const char* cm_score_fname = nullptr; report(p->parserH);
const object_t* dynArrayNode = nullptr;
dyn_ref_t* dynRefA = nullptr;
unsigned dynRefN = 0;
double srate = 0;
handle_t h;
time::spec_t t0;
// parse the test cfg
if((rc = cfg->getv( "cm_score_fname", cm_score_fname,
"srate", srate,
"dyn_ref", dynArrayNode )) != kOkRC )
{
rc = cwLogError(rc,"sfscore test parse params failed on.");
goto errLabel;
} }
if((rc = parse_dyn_ref_cfg( dynArrayNode, dynRefA, dynRefN )) != kOkRC )
{
rc = cwLogError(rc,"The reference dynamics array parse failed.");
goto errLabel;
}
time::get(t0);
if((rc = create(h,cm_score_fname,srate,dynRefA,dynRefN)) != kOkRC )
{
rc = cwLogError(rc,"Score test create failed.");
goto errLabel;
}
report(h);
printf("%i events %i ms\n",event_count(h), time::elapsedMs(t0) );
errLabel:
destroy(h);
mem::release(dynRefA);
return rc;
}

View File

@ -6,55 +6,6 @@ namespace cw
namespace sfscore namespace sfscore
{ {
enum
{
kInvalidEvtScId = 0,
kTimeSigEvtScId,
kKeySigEvtScId,
kTempoEvtScId,
kTrackEvtScId,
kTextEvtScId,
kNameEvtScId,
kEOTrackEvtScId,
kCopyEvtScId,
kBlankEvtScId,
kBarEvtScId,
kPgmEvtScId,
kCtlEvtScId,
kNonEvtScId,
kPedalEvtScId
};
// Flags used by event_t.flags
enum
{
kEvenScFl = 0x001, // This note is marked for evenness measurement
kDynScFl = 0x002, // This note is marked for dynamics measurement
kTempoScFl = 0x004, // This note is marked for tempo measurement
//kSkipScFl = 0x008, // This isn't a real event (e.g. tied note) skip over it
kGraceScFl = 0x010, // This is a grace note
kInvalidScFl = 0x020, // This note has a calculated time
//kPedalDnScFl = 0x040, // This is a pedal down event (pitch holds the pedal id and durSecs holds the time the pedal will remain down.)
//kPedalUpScFl = 0x080, // This is a pedal up event (pitch holds the pedal id)
};
// Id's used by set_t.varId and as indexes into
// section_t.vars[].
enum
{
kInvalidVarScId = 0, // 0
kMinVarScId = 1,
kEvenVarScId = kMinVarScId, // 1
kDynVarScId = 2, // 2
kTempoVarScId = 3, // 3
kScVarCnt = 4
};
enum : uint8_t {
kInvalidDynVel = 0
};
struct loc_str; struct loc_str;
struct set_str; struct set_str;
@ -67,16 +18,16 @@ namespace cw
unsigned begEvtIndex; // score element index where this section starts unsigned begEvtIndex; // score element index where this section starts
unsigned setCnt; // Count of elements in setArray[] unsigned setCnt; // Count of elements in setArray[]
struct set_str** setArray; // Ptrs to sets which are applied to this section. struct set_str** setArray; // Ptrs to sets which are applied to this section.
double vars[ kScVarCnt ]; // Set to DBL_MAX by default. double vars[ score_parse::kVarCnt ]; // Set to DBL_MAX by default.
} section_t; } section_t;
typedef struct event_str typedef struct event_str
{ {
unsigned type; // Event type unsigned type; // See score_parse ???TId
double secs; // Time location in seconds double secs; // Time location in seconds
double durSecs; // Duration in seconds double durSecs; // Duration in seconds
unsigned index; // Index of this event in the event array. unsigned index; // Index of this event in the event array.
unsigned locIdx; // Index of the location containing this event unsigned locIdx; // Index of the onset location (oloc) containing this event
midi::byte_t pitch; // MIDI pitch of this note or the MIDI pedal id of pedal down/up msg (64=sustain 65=sostenuto 66=soft) midi::byte_t pitch; // MIDI pitch of this note or the MIDI pedal id of pedal down/up msg (64=sustain 65=sostenuto 66=soft)
midi::byte_t vel; // MIDI velocity of this note midi::byte_t vel; // MIDI velocity of this note
unsigned flags; // Attribute flags for this event unsigned flags; // Attribute flags for this event
@ -90,10 +41,12 @@ namespace cw
unsigned perfDynLvl; // Index into dynamic level ref. array assoc'd with perfVel unsigned perfDynLvl; // Index into dynamic level ref. array assoc'd with perfVel
unsigned line; // Line number of this event in the score file. unsigned line; // Line number of this event in the score file.
unsigned csvEventId; // EventId from CSV 'evt' column. unsigned csvEventId; // EventId from CSV 'evt' column.
unsigned hash; // unique hash id for this note
unsigned varA[ score_parse::kVarCnt ];
} event_t; } event_t;
// A 'set' is a collection of events that are grouped in time and all marked with a given attribute. // A 'set' is a collection of events that are grouped in time and all marked with a given attribute.
// (e.g. eveness, tempo, dynamcs ... ) // (e.g. eveness, tempo, dynamcs ... )o
typedef struct set_str typedef struct set_str
{ {
unsigned id; // Unique id for this set unsigned id; // Unique id for this set
@ -143,29 +96,36 @@ namespace cw
marker_t* markList; // List of markers assigned to this location marker_t* markList; // List of markers assigned to this location
} loc_t; } loc_t;
typedef struct typedef dyn_ref_tbl::dyn_ref_t dyn_ref_t;
{
const char* label;
uint8_t vel;
} dyn_ref_t;
typedef handle<struct sfscore_str> handle_t; typedef handle<struct sfscore_str> handle_t;
rc_t create( handle_t& h, const char* fname, double srate, const dyn_ref_t* dynRefA=nullptr, unsigned dynRefN=0 ); // Create the score from a provided score parser
rc_t create( handle_t& h,
score_parse::handle_t spH );
// Create an internal score parser.
rc_t create( handle_t& h,
const char* fname,
double srate,
dyn_ref_tbl::handle_t dynRefH);
rc_t destroy( handle_t& h ); rc_t destroy( handle_t& h );
double sample_rate( handle_t& h );
unsigned event_count( handle_t h ); unsigned event_count( handle_t h );
event_t* event( handle_t h, unsigned idx ); event_t* event( handle_t h, unsigned idx );
event_t* hash_to_event( handle_t h, unsigned hash );
unsigned loc_count( handle_t h ); unsigned loc_count( handle_t h );
loc_t* loc( handle_t h, unsigned idx ); loc_t* loc( handle_t h, unsigned idx );
rc_t parse_dyn_ref_cfg( const object_t* cfg, dyn_ref_t*& refArrayRef, unsigned& refArrayNRef);
void report( handle_t h ); void report( handle_t h, const char* out_fname=nullptr );
void parse_report( handle_t h );
// see score_test::test() for testing this object
rc_t test( const object_t* cfg );
} }
} }