libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmScoreProc.c 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. #include "cmGlobal.h"
  2. #include "cmFloatTypes.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmLinkedHeap.h"
  9. #include "cmSymTbl.h"
  10. #include "cmJson.h"
  11. #include "cmFile.h"
  12. #include "cmTime.h"
  13. #include "cmMidi.h"
  14. #include "cmMidiFile.h"
  15. #include "cmAudioFile.h"
  16. #include "cmScore.h"
  17. #include "cmTimeLine.h"
  18. #include "cmScoreProc.h"
  19. #include "cmProcObj.h"
  20. #include "cmProc4.h"
  21. typedef enum
  22. {
  23. kBeginTakeSpId, // tlObjPtr points to a cmTlMarker_t object.
  24. kEndTakeSpId, // tlObjPtr is NULL.
  25. kNoteOnSpId, // tlObjPtr points to a cmTlMidiEvt_t note-on object.
  26. kFailSpId // tlObjPtr points to a cmTlMarker_t object (This takes score tracking failed.)
  27. } cmScoreProcSelId_t;
  28. struct cmSp_str;
  29. typedef cmSpRC_t (*cmScoreProcCb_t)( void* arg, struct cmSp_str* p, cmScoreProcSelId_t id, cmTlObj_t* tlObjPtr );
  30. typedef struct cmSp_str
  31. {
  32. cmErr_t err; // score proc object error state
  33. cmCtx* ctx; // application context
  34. cmScH_t scH; // score object
  35. cmTlH_t tlH; // time-line object
  36. cmJsonH_t jsH; //
  37. unsigned* dynArray; // dynArray[dynCnt] dynamics reference array
  38. unsigned dynCnt; //
  39. double srate; //
  40. cmScMatcher* match; // score follower
  41. cmScoreProcCb_t procCb; // score processor callback - called whenever a new 'marker' take or note-on is about to be processed
  42. cmScMatcherCb_t matchCb; // score follower callback - called whenever the score follower detects a matched event
  43. void* cbArg; // callback arg. for both matchCb and procCb.
  44. } cmSp_t;
  45. // read the dynamics reference array from the time-line project file.
  46. cmSpRC_t _cmJsonReadDynArray( cmJsonH_t jsH, unsigned** dynArray, unsigned* dynCnt )
  47. {
  48. cmJsonNode_t* np;
  49. int i;
  50. if( cmJsonPathToArray(jsH, NULL, NULL, "dynRef", &np ) != kOkJsRC )
  51. return kJsonFailSpRC;
  52. *dynCnt = cmJsonChildCount(np);
  53. *dynArray = cmMemAllocZ(unsigned,*dynCnt);
  54. for(i=0; i<*dynCnt; ++i)
  55. if( cmJsonUIntValue( cmJsonArrayElement(np,i), (*dynArray)+i ) != kOkJsRC )
  56. return kJsonFailSpRC;
  57. return kOkSpRC;
  58. }
  59. cmSpRC_t _cmScoreProcInit(
  60. cmCtx_t* ctx,
  61. cmSp_t* p,
  62. const cmChar_t* rsrcFn,
  63. cmScoreProcCb_t procCb,
  64. cmScMatcherCb_t matchCb,
  65. void* cbArg )
  66. {
  67. cmSpRC_t rc = kOkSpRC;
  68. const cmChar_t* scFn = NULL;
  69. const cmChar_t* tlFn = NULL;
  70. const cmChar_t* tlPrefixPath = NULL;
  71. cmErrSetup(&p->err,&ctx->rpt,"ScoreProc");
  72. // open the resource file
  73. if( cmJsonInitializeFromFile( &p->jsH, rsrcFn, ctx ) != kOkJsRC )
  74. {
  75. rc = cmErrMsg(&p->err,kJsonFailSpRC,"Unable to load the main resource file:%s.",cmStringNullGuard(rsrcFn));
  76. goto errLabel;
  77. }
  78. // get the time line fn
  79. if( cmJsonPathToString( p->jsH, NULL, NULL, "timeLineFn", &tlFn ) != kOkJsRC )
  80. {
  81. rc = cmErrMsg(&p->err,kJsonFailSpRC,"Unable to locate the time line file name in the main resource file:%s",cmStringNullGuard(rsrcFn));
  82. goto errLabel;
  83. }
  84. // get the score file name
  85. if( cmJsonPathToString( p->jsH, NULL, NULL, "scoreFn", &scFn ) != kOkJsRC )
  86. {
  87. rc = cmErrMsg(&p->err,kJsonFailSpRC,"Unable to locate the score file name in the main resource file:%s",cmStringNullGuard(rsrcFn));
  88. goto errLabel;
  89. }
  90. // get the time line data file prefix path
  91. if( cmJsonPathToString( p->jsH, NULL, NULL, "tlPrefixPath", &tlPrefixPath ) != kOkJsRC )
  92. {
  93. rc = cmErrMsg(&p->err,kJsonFailSpRC,"Unable to locate the time line data file prefix path in the main resource file:%s",cmStringNullGuard(rsrcFn));
  94. goto errLabel;
  95. }
  96. // read the dynamics reference array
  97. if((rc = _cmJsonReadDynArray( p->jsH, &p->dynArray, &p->dynCnt )) != kOkSpRC )
  98. {
  99. rc = cmErrMsg(&p->err,rc,"Unable to read dynamics reference array resource from the main resource file:%s",cmStringNullGuard(rsrcFn));
  100. goto errLabel;
  101. }
  102. // load the time-line file
  103. if( cmTimeLineInitializeFromFile(ctx, &p->tlH, NULL, NULL, tlFn, tlPrefixPath ) != kOkTlRC )
  104. {
  105. rc = cmErrMsg(&p->err,kTimeLineFailSpRC,"Time line load failed for time line file:%s.",cmStringNullGuard(tlFn));
  106. goto errLabel;
  107. }
  108. p->srate = cmTimeLineSampleRate(p->tlH);
  109. // load the score file
  110. if( cmScoreInitialize(ctx, &p->scH, scFn, p->srate, NULL, 0, NULL, NULL, cmSymTblNullHandle ) != kOkScRC )
  111. {
  112. rc = cmErrMsg(&p->err,kScoreFailSpRC,"Score load failed for score file:%s.",cmStringNullGuard(scFn));
  113. goto errLabel;
  114. }
  115. p->ctx = cmCtxAlloc(NULL, &ctx->rpt, cmLHeapNullHandle, cmSymTblNullHandle );
  116. p->matchCb = matchCb;
  117. p->procCb = procCb;
  118. p->cbArg = cbArg;
  119. errLabel:
  120. return rc;
  121. }
  122. // This function iterates through each sequence and advances
  123. // to each 'begin-marker' position.
  124. cmSpRC_t _cmScoreProcProcess(cmCtx_t* ctx, cmSp_t* sp)
  125. {
  126. cmSpRC_t rc = kOkSpRC;
  127. unsigned midiN = 7;
  128. unsigned scWndN = 10;
  129. unsigned seqN = cmTimeLineSeqCount(sp->tlH);
  130. double srate = cmTimeLineSampleRate(sp->tlH);
  131. unsigned seqId;
  132. assert( sp->srate == srate);
  133. // allocate the score matcher
  134. sp->match = cmScMatcherAlloc(sp->ctx,NULL,sp->srate,sp->scH,scWndN,midiN,sp->matchCb,sp->cbArg);
  135. assert(sp->match != NULL );
  136. // for each time line sequence
  137. for(seqId=0; seqId<seqN; ++seqId)
  138. {
  139. cmTlObj_t* o0p = NULL;
  140. // for each 'marker' in this time line sequence
  141. while( (o0p = cmTimeLineNextTypeObj(sp->tlH, o0p, seqId, kMarkerTlId)) != NULL )
  142. {
  143. // get the 'marker' recd
  144. cmTlMarker_t* markPtr = cmTimeLineMarkerObjPtr(sp->tlH,o0p);
  145. assert( markPtr != NULL );
  146. // if the marker does not have a valid start bar location
  147. if( markPtr->bar == 0 )
  148. continue;
  149. // get the end-of-marker time as a sample index
  150. unsigned markEndSmpIdx = markPtr->obj.seqSmpIdx + markPtr->obj.durSmpCnt;
  151. // get the score event associated with the marker's bar number.
  152. const cmScoreEvt_t* evtPtr = cmScoreBarEvt(sp->scH,markPtr->bar);
  153. assert( evtPtr != NULL );
  154. // get the score location associated with the markers bar score event
  155. const cmScoreLoc_t* locPtr = cmScoreEvtLoc(sp->scH,evtPtr);
  156. assert( locPtr != NULL );
  157. cmRptPrintf(&ctx->rpt,"Processing loc:%i seq:%i %s %s\n",locPtr->index,seqId,cmStringNullGuard(markPtr->obj.name),cmStringNullGuard(markPtr->text));
  158. // reset the score matcher to begin searching at the bar location
  159. if( cmScMatcherReset(sp->match, locPtr->index ) != cmOkRC )
  160. {
  161. cmErrMsg(&sp->err,kScoreMatchFailSpRC,"The score matcher reset failed on location: %i.",locPtr->index);
  162. continue;
  163. }
  164. // inform the score processor that we are about to start a new take
  165. if( sp->procCb( sp->cbArg, sp, kBeginTakeSpId, o0p ) != kOkSpRC )
  166. {
  167. cmErrMsg(&sp->err,kProcFailSpRC,"The score process object failed on reset.");
  168. continue;
  169. }
  170. cmTlObj_t* o1p = o0p;
  171. bool errFl = false;
  172. // as long as more MIDI events are available get the next MIDI msg
  173. while( (rc == kOkSpRC) && (o1p = cmTimeLineNextTypeObj(sp->tlH, o1p, seqId, kMidiEvtTlId )) != NULL )
  174. {
  175. cmTlMidiEvt_t* mep = cmTimeLineMidiEvtObjPtr(sp->tlH,o1p);
  176. assert(mep != NULL );
  177. // if the msg falls after the end of the marker then we are done
  178. if( mep->obj.seqSmpIdx != cmInvalidIdx && mep->obj.seqSmpIdx > markEndSmpIdx )
  179. break;
  180. // if the time line MIDI msg is a note-on
  181. if( mep->msg->status == kNoteOnMdId )
  182. {
  183. sp->procCb( sp->cbArg, sp, kNoteOnSpId, o1p );
  184. cmRC_t cmRC = cmScMatcherExec(sp->match, mep->obj.seqSmpIdx, mep->msg->uid, mep->msg->status, mep->msg->u.chMsgPtr->d0, mep->msg->u.chMsgPtr->d1, NULL );
  185. switch( cmRC )
  186. {
  187. case cmOkRC: // continue processing MIDI events
  188. break;
  189. case cmEofRC: // end of the score was encountered
  190. break;
  191. case cmInvalidArgRC: // p->eli was not set correctly
  192. rc = cmErrMsg(&sp->err,kScoreMatchFailSpRC,"The score matcher failed due to an invalid argument.");
  193. errFl = true;
  194. break;
  195. case cmSubSysFailRC: // scan resync failed
  196. rc = cmErrMsg(&sp->err,kScoreMatchFailSpRC,"The score matcher failed on resync.");
  197. sp->procCb( sp->cbArg, sp, kFailSpId, o0p );
  198. //cmScMatcherPrint(sp->match);
  199. //goto errLabel;
  200. break;
  201. default:
  202. { assert(0); }
  203. }
  204. }
  205. }
  206. // inform the score processor that we done processing a take
  207. if( sp->procCb( sp->cbArg, sp, kEndTakeSpId, NULL ) != kOkSpRC )
  208. cmErrMsg(&sp->err,kProcFailSpRC,"The score process object failed on reset.");
  209. // error flag is used to break out of the loop after the 'end-take' is called
  210. // so that the user defined processes has a chance to clean-up
  211. if( errFl )
  212. goto errLabel;
  213. rc = kOkSpRC;
  214. }
  215. }
  216. errLabel:
  217. if( cmScMatcherFree(&sp->match) != cmOkRC )
  218. cmErrMsg(&sp->err,kScoreMatchFailSpRC,"The score matcher release failed.");
  219. return rc;
  220. }
  221. cmSpRC_t _cmScoreProcFinal( cmSp_t* p )
  222. {
  223. cmSpRC_t rc = kOkSpRC;
  224. cmCtxFree(&p->ctx);
  225. if( cmScoreFinalize(&p->scH) != kOkScRC )
  226. cmErrMsg(&p->err,kScoreFailSpRC,"Score finalize failed.");
  227. if( cmTimeLineFinalize(&p->tlH) != kOkTlRC )
  228. cmErrMsg(&p->err,kTimeLineFailSpRC,"Time line finalize failed.");
  229. if( cmJsonFinalize(&p->jsH) != kOkJsRC )
  230. cmErrMsg(&p->err,kJsonFailSpRC,"JSON finalize failed.");
  231. cmMemFree(p->dynArray);
  232. return rc;
  233. }
  234. //==================================================================================================
  235. typedef struct _cmSpMeas_t
  236. {
  237. cmTlMarker_t* markPtr; // time-line marker in which this 'set' exists
  238. cmScoreSet_t* setPtr; // score set on which this measurment is based
  239. double value; // the value of the measurement
  240. double cost; // the quality of the perf->score match
  241. struct _cmSpMeas_t* link;
  242. } _cmSpMeas_t;
  243. typedef struct
  244. {
  245. struct cmSp_str* sp;
  246. cmScMeas* meas; // performance analyzer
  247. cmTlMarker_t* curMarkPtr; //
  248. _cmSpMeas_t* list_beg; //
  249. _cmSpMeas_t* list_end; //
  250. _cmSpMeas_t* slist_beg; //
  251. } _cmSpMeasProc_t;
  252. typedef struct
  253. {
  254. unsigned srcSeqId;
  255. const cmChar_t* srcMarkNameStr;
  256. unsigned srcTypeId;
  257. const cmChar_t* srcTypeLabelStr;
  258. unsigned dstScLocIdx;
  259. unsigned dstEvtIdx;
  260. const cmChar_t* dstSectLabelStr;
  261. double value;
  262. double cost;
  263. } _cmSpMeasSect_t;
  264. unsigned _cmSpMeasSectCount( _cmSpMeasProc_t* m )
  265. {
  266. const _cmSpMeas_t* mp = m->list_beg;
  267. unsigned n = 0;
  268. for(; mp != NULL; mp=mp->link)
  269. n += mp->setPtr->sectCnt;
  270. return n;
  271. }
  272. int _cmSpMeasSectCompare( const void* p0, const void* p1 )
  273. {
  274. _cmSpMeasSect_t* m0 = (_cmSpMeasSect_t*)p0;
  275. _cmSpMeasSect_t* m1 = (_cmSpMeasSect_t*)p1;
  276. return (int)m0->dstScLocIdx - (int)m1->dstScLocIdx;
  277. }
  278. cmSpRC_t _cmScWriteMeasFile( cmCtx_t* ctx, cmSp_t* sp, _cmSpMeasProc_t* m, const cmChar_t* outFn )
  279. {
  280. cmFileH_t fH = cmFileNullHandle;
  281. cmSpRC_t rc = kOkSpRC;
  282. unsigned i,j,k;
  283. _cmSpMeas_t* mp = m->list_beg;
  284. unsigned scnt = _cmSpMeasSectCount(m);
  285. _cmSpMeasSect_t sarray[ scnt ];
  286. for(i=0,k=0; k<scnt && mp!=NULL; ++i,mp=mp->link)
  287. {
  288. const cmChar_t* typeLabel = NULL;
  289. switch(mp->setPtr->varId)
  290. {
  291. case kEvenVarScId: typeLabel="even"; break;
  292. case kDynVarScId: typeLabel="dyn"; break;
  293. case kTempoVarScId:typeLabel="tempo";break;
  294. default:
  295. { assert(0); }
  296. }
  297. for(j=0; j<mp->setPtr->sectCnt; ++j,++k)
  298. {
  299. _cmSpMeasSect_t* r = sarray + k;
  300. r->srcSeqId = mp->markPtr->obj.seqId,
  301. r->srcMarkNameStr = cmStringNullGuard(mp->markPtr->obj.name),
  302. r->srcTypeId = mp->setPtr->varId,
  303. r->srcTypeLabelStr = typeLabel,
  304. r->dstScLocIdx = mp->setPtr->sectArray[j]->locPtr->index,
  305. r->dstEvtIdx = mp->setPtr->sectArray[j]->begEvtIndex,
  306. r->dstSectLabelStr = cmStringNullGuard(mp->setPtr->sectArray[j]->label),
  307. r->value = mp->value,
  308. r->cost = mp->cost;
  309. }
  310. }
  311. assert(mp==NULL && k==scnt);
  312. qsort(sarray,scnt,sizeof(sarray[0]),_cmSpMeasSectCompare);
  313. if( cmFileOpen(&fH,outFn,kWriteFileFl,&ctx->rpt) != kOkFileRC )
  314. {
  315. rc = cmErrMsg(&sp->err,kFileFailSpRC,"Unable to create the output file '%s'.",cmStringNullGuard(outFn));
  316. goto errLabel;
  317. }
  318. cmFilePrintf(fH,"{\n meas : \n[\n[ \"sec\" \"typeLabel\" \"val\" \"cost\" \"loc\" \"evt\" \"seq\" \"mark\" \"typeId\" ]\n");
  319. for(i=0; i<scnt; ++i)
  320. {
  321. _cmSpMeasSect_t* r = sarray + i;
  322. cmFilePrintf(fH,"[ \"%s\" \"%s\" %f %f %i %i %i \"%s\" %i ]\n",
  323. r->dstSectLabelStr,
  324. r->srcTypeLabelStr,
  325. r->value,
  326. r->cost,
  327. r->dstScLocIdx,
  328. r->dstEvtIdx,
  329. r->srcSeqId,
  330. r->srcMarkNameStr,
  331. r->srcTypeId
  332. );
  333. }
  334. /*
  335. mp = sp->list_beg;
  336. for(; mp!=NULL; mp=mp->link)
  337. {
  338. for(i=0; i<mp->setPtr->sectCnt; ++i)
  339. {
  340. cmFilePrintf(fH,"[ %i \"%s\" %i \"%s\" %i %i \"%s\" %f %f ]\n",
  341. mp->markPtr->obj.seqId,
  342. cmStringNullGuard(mp->markPtr->obj.name),
  343. mp->setPtr->varId,
  344. typeLabel,
  345. mp->setPtr->sectArray[i]->locPtr->index,
  346. mp->setPtr->sectArray[i]->begEvtIndex,
  347. cmStringNullGuard(mp->setPtr->sectArray[i]->label),
  348. mp->value,
  349. mp->cost );
  350. }
  351. }
  352. */
  353. cmFilePrintf(fH,"\n]\n}\n");
  354. errLabel:
  355. if( cmFileClose(&fH) != kOkFileRC )
  356. cmErrMsg(&sp->err,kFileFailSpRC,"The output file close failed on '%s'.",cmStringNullGuard(outFn));
  357. return rc;
  358. }
  359. // score matcher callback
  360. void _cmSpMatchMeasCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
  361. {
  362. _cmSpMeasProc_t* m = (_cmSpMeasProc_t*)arg;
  363. cmScMeas* sm = m->meas;
  364. if( cmScMeasExec(sm, rp->mni, rp->locIdx, rp->scEvtIdx, rp->flags, rp->smpIdx, rp->pitch, rp->vel ) == cmOkRC )
  365. {
  366. unsigned i;
  367. for(i=sm->vsi; i<sm->nsi; ++i)
  368. // ignore set's which did not produce a valid value
  369. if(sm->set[i].value != DBL_MAX )
  370. {
  371. _cmSpMeas_t* r = cmMemAllocZ(_cmSpMeas_t,1);
  372. r->markPtr = m->curMarkPtr;
  373. r->setPtr = sm->set[i].sp;
  374. r->value = sm->set[i].value;
  375. r->cost = sm->set[i].match_cost;
  376. if( m->list_beg == NULL )
  377. {
  378. m->list_beg = r;
  379. m->list_end = r;
  380. }
  381. else
  382. {
  383. m->list_end->link = r;
  384. m->list_end = r;
  385. }
  386. }
  387. }
  388. }
  389. // measurement proc callback
  390. cmSpRC_t _cmSpProcMeasCb( void* arg, cmSp_t* sp, cmScoreProcSelId_t id, cmTlObj_t* tlObjPtr )
  391. {
  392. cmSpRC_t rc = kOkSpRC;
  393. _cmSpMeasProc_t* m = (_cmSpMeasProc_t*)arg;
  394. switch( id )
  395. {
  396. case kBeginTakeSpId:
  397. // reset the performance evaluation object
  398. if( cmScMeasReset(m->meas) != cmOkRC )
  399. rc = cmErrMsg(&sp->err,kScoreMatchFailSpRC,"The score performance evaluation object failed on reset.");
  400. m->curMarkPtr = cmTimeLineMarkerObjPtr(sp->tlH,tlObjPtr);
  401. break;
  402. case kNoteOnSpId:
  403. break;
  404. case kEndTakeSpId:
  405. break;
  406. case kFailSpId:
  407. break;
  408. }
  409. return rc;
  410. }
  411. cmSpRC_t _cmScoreProcGenAllMeasurementsMain(cmCtx_t* ctx)
  412. {
  413. const cmChar_t* rsrcFn = "/home/kevin/.kc/time_line.js";
  414. const cmChar_t* outFn = "/home/kevin/src/cmkc/src/kc/data/meas0.js";
  415. cmSpRC_t rc = kOkSpRC;
  416. _cmSpMeasProc_t* m = cmMemAllocZ(_cmSpMeasProc_t,1);
  417. cmSp_t s;
  418. cmSp_t* sp = &s;
  419. memset(sp,0,sizeof(s));
  420. cmRptPrintf(&ctx->rpt,"Score Performance Evaluation Start\n");
  421. // initialize the score processor
  422. if((rc = _cmScoreProcInit(ctx,sp,rsrcFn,_cmSpProcMeasCb,_cmSpMatchMeasCb,m)) != kOkSpRC )
  423. goto errLabel;
  424. // allocate the performance evaluation measurement object
  425. m->meas = cmScMeasAlloc( sp->ctx, NULL, sp->scH, sp->srate, sp->dynArray, sp->dynCnt );
  426. m->sp = sp;
  427. // run the score processor
  428. _cmScoreProcProcess(ctx,sp);
  429. // write the results of the performance evaluation
  430. if((rc = _cmScWriteMeasFile(ctx, sp, m, outFn )) != kOkSpRC )
  431. cmErrMsg(&sp->err,kFileFailSpRC,"The measurement output did not complete without errors.");
  432. // free the measurement linked list
  433. _cmSpMeas_t* mp = m->list_beg;
  434. while(mp!=NULL)
  435. {
  436. _cmSpMeas_t* np = mp->link;
  437. cmMemFree(mp);
  438. mp = np;
  439. }
  440. // free the performance evaluation object
  441. if( cmScMeasFree(&m->meas) != cmOkRC )
  442. cmErrMsg(&sp->err,kScoreMatchFailSpRC,"The performance evaluation object failed.");
  443. //cmScorePrint(sp.scH,&ctx->rpt);
  444. //cmScorePrintLoc(sp.scH);
  445. errLabel:
  446. _cmScoreProcFinal(sp);
  447. cmMemFree(m);
  448. cmRptPrintf(&ctx->rpt,"Score Proc End\n");
  449. return rc;
  450. }
  451. //==================================================================================================
  452. typedef struct cmSpAssoc_str
  453. {
  454. unsigned scEvtIdx; // score event index
  455. unsigned tlUid; // time-line MIDI note-on object id
  456. struct cmSpAssoc_str* link;
  457. } cmSpAssoc_t;
  458. typedef struct cmSpNoteMap_str
  459. {
  460. unsigned tlUid; // time-line MIDI note-on object id
  461. unsigned mni; // assocated 'mni' returned in a cmScMatcherResult_t record
  462. unsigned muid; // MIDI file msg unique id for this event (see cmMidiTrackMsg_t.uid)
  463. struct cmSpNoteMap_str* link;
  464. } cmSpNoteMap_t;
  465. typedef struct
  466. {
  467. cmCtx_t* ctx;
  468. cmSp_t* sp;
  469. unsigned mni;
  470. bool failFl;
  471. cmJsonH_t jsH;
  472. cmJsonNode_t* takeArray;
  473. cmJsonNode_t* takeObj;
  474. cmJsonNode_t* array;
  475. cmSpAssoc_t* bap;
  476. cmSpAssoc_t* eap;
  477. cmSpNoteMap_t* bmp;
  478. cmSpNoteMap_t* emp;
  479. } cmSpAssocProc_t;
  480. void _cmSpMatchAssocCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
  481. {
  482. cmSpAssocProc_t* m = (cmSpAssocProc_t*)arg;
  483. if( cmJsonCreateFilledObject(m->jsH, m->array,
  484. "mni", kIntTId, rp->mni,
  485. "muid", kIntTId, rp->muid,
  486. "scEvtIdx", kIntTId, rp->scEvtIdx,
  487. "flags", kIntTId, rp->flags,
  488. NULL ) == NULL )
  489. {
  490. cmErrMsg(&m->ctx->err,kJsonFailSpRC,"JSON association record create failed.");
  491. }
  492. //cmScoreEvt_t* sep = rp->scEvtIdx == -1 ? NULL : cmScoreEvt( m->sp->scH, rp->scEvtIdx );
  493. //printf("%3i loc:%4i pitch=%3i %3i flags=0x%x\n",rp->mni,rp->locIdx,rp->pitch,sep==NULL ? -1 : sep->pitch,rp->flags );
  494. }
  495. cmSpRC_t _cmSpProcAssocCb( void* arg, cmSp_t* sp, cmScoreProcSelId_t id, cmTlObj_t* tlObjPtr )
  496. {
  497. cmSpRC_t rc = kOkSpRC;
  498. cmSpAssocProc_t* m = (cmSpAssocProc_t*)arg;
  499. switch( id )
  500. {
  501. case kBeginTakeSpId:
  502. {
  503. cmTlMarker_t* markPtr = cmTimeLineMarkerObjPtr( sp->tlH, tlObjPtr );
  504. assert( markPtr != NULL );
  505. m->mni = 0;
  506. m->failFl = false;
  507. // insert a take object
  508. if((m->takeObj = cmJsonCreateObject(m->jsH, m->takeArray )) == NULL )
  509. {
  510. rc = cmErrMsg(&m->ctx->err,kJsonFailSpRC,"Take insert failed on seq:%i '%s' : '%s'.", tlObjPtr->seqId, cmStringNullGuard(tlObjPtr->text),cmStringNullGuard(markPtr->text));
  511. goto errLabel;
  512. }
  513. // set the section time-line UID
  514. if( cmJsonInsertPairInt(m->jsH, m->takeObj,"markerUid", tlObjPtr->uid ) != kOkJsRC )
  515. {
  516. rc = cmErrMsg(&m->ctx->err,kJsonFailSpRC,"Marker uid field insert failed on seq:%i '%s' : '%s'.", tlObjPtr->seqId, cmStringNullGuard(tlObjPtr->text),cmStringNullGuard(markPtr->text));
  517. goto errLabel;
  518. }
  519. // create an array to hold the assoc results
  520. if(( m->array = cmJsonInsertPairArray(m->jsH, m->takeObj, "array")) == NULL )
  521. {
  522. rc = cmErrMsg(&m->ctx->err,kJsonFailSpRC,"Marker array field insert failed on seq:%i '%s' : '%s'.", tlObjPtr->seqId, cmStringNullGuard(tlObjPtr->text),cmStringNullGuard(markPtr->text));
  523. goto errLabel;
  524. }
  525. }
  526. break;
  527. case kEndTakeSpId:
  528. {
  529. while( m->bmp != NULL )
  530. {
  531. cmSpNoteMap_t* nmp = m->bmp->link;
  532. cmMemFree(m->bmp);
  533. m->bmp = nmp;
  534. }
  535. m->bmp = NULL;
  536. m->emp = NULL;
  537. if( cmJsonInsertPairInt( m->jsH, m->takeObj, "failFl", m->failFl ) != kOkJsRC )
  538. {
  539. rc = cmErrMsg(&m->ctx->err,kJsonFailSpRC,"JSON fail flag insert failed.");
  540. goto errLabel;
  541. }
  542. }
  543. break;
  544. case kNoteOnSpId:
  545. {
  546. // create a cmSpNoteMap_t record ...
  547. cmSpNoteMap_t* map = cmMemAllocZ(cmSpNoteMap_t,1);
  548. map->tlUid = tlObjPtr->uid;
  549. map->mni = m->mni;
  550. // .. and insert it in the note-map list
  551. if( m->emp == NULL )
  552. {
  553. m->bmp = map;
  554. m->emp = map;
  555. }
  556. else
  557. {
  558. m->emp->link = map;
  559. m->emp = map;
  560. }
  561. m->mni += 1;
  562. }
  563. break;
  564. case kFailSpId:
  565. m->failFl = true;
  566. break;
  567. }
  568. errLabel:
  569. return rc;
  570. }
  571. cmSpRC_t _cmScoreProcGenAssocMain(cmCtx_t* ctx)
  572. {
  573. const cmChar_t* rsrcFn = "/home/kevin/.kc/time_line.js";
  574. const cmChar_t* outFn = "/home/kevin/src/cmkc/src/kc/data/takeSeqBldr0.js";
  575. cmSpRC_t rc = kOkSpRC;
  576. cmSpAssocProc_t* m = cmMemAllocZ(cmSpAssocProc_t,1);
  577. cmSp_t s;
  578. cmSp_t* sp = &s;
  579. memset(sp,0,sizeof(s));
  580. m->ctx = ctx;
  581. cmRptPrintf(&ctx->rpt,"Score Association Start\n");
  582. // create a JSON object to hold the results
  583. if( cmJsonInitialize(&m->jsH, ctx ) != kOkJsRC )
  584. {
  585. cmErrMsg(&m->ctx->err,kJsonFailSpRC,"Score association JSON output object create failed.");
  586. goto errLabel;
  587. }
  588. // create the JSON root object
  589. if( cmJsonCreateObject(m->jsH, NULL ) == NULL )
  590. {
  591. cmErrMsg(&m->ctx->err,kJsonFailSpRC,"Create JSON root object.");
  592. goto errLabel;
  593. }
  594. // initialize the score processor
  595. if((rc = _cmScoreProcInit(ctx,sp,rsrcFn,_cmSpProcAssocCb,_cmSpMatchAssocCb, m)) != kOkSpRC )
  596. goto errLabel;
  597. m->sp = sp;
  598. // store the time-line and score file name
  599. if( cmJsonInsertPairs(m->jsH, cmJsonRoot(m->jsH),
  600. "timeLineFn", kStringTId, cmTimeLineFileName( sp->tlH),
  601. "scoreFn", kStringTId, cmScoreFileName( sp->scH ),
  602. "tlPrefixPath", kStringTId, cmTimeLinePrefixPath( sp->tlH ),
  603. NULL ) != kOkJsRC )
  604. {
  605. cmErrMsg(&m->ctx->err,kJsonFailSpRC,"File name JSON field insertion failed.");
  606. goto errLabel;
  607. }
  608. // create an array to hold each take
  609. if((m->takeArray = cmJsonInsertPairArray(m->jsH, cmJsonRoot(m->jsH), "takeArray" )) == NULL )
  610. {
  611. cmErrMsg(&m->ctx->err,kJsonFailSpRC,"JSON take-array create failed.");
  612. goto errLabel;
  613. }
  614. // run the score processor
  615. _cmScoreProcProcess(ctx,sp);
  616. cmRptPrintf(&ctx->rpt,"Writing results to '%s'.",outFn);
  617. // write the results to a JSON file
  618. if(cmJsonWrite(m->jsH, NULL, outFn ) != kOkJsRC )
  619. {
  620. cmErrMsg(&m->ctx->err,kJsonFailSpRC,"Score association output file write failed.");
  621. goto errLabel;
  622. }
  623. errLabel:
  624. if( cmJsonFinalize(&m->jsH) != kOkJsRC )
  625. {
  626. cmErrMsg(&m->ctx->err,kJsonFailSpRC,"JSON finalize failed.");
  627. }
  628. _cmScoreProcFinal(sp);
  629. cmMemFree(m);
  630. cmRptPrintf(&ctx->rpt,"Score Proc End\n");
  631. return rc;
  632. }
  633. //==================================================================================================
  634. cmSpRC_t cmScoreProc(cmCtx_t* ctx)
  635. {
  636. cmSpRC_t rc = kOkSpRC;
  637. //_cmScoreProcGenAllMeasurementsMain(ctx);
  638. _cmScoreProcGenAssocMain(ctx);
  639. return rc;
  640. }