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.

cmProc4.c 64KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.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 "cmFloatTypes.h"
  10. #include "cmComplexTypes.h"
  11. #include "cmFileSys.h"
  12. #include "cmJson.h"
  13. #include "cmSymTbl.h"
  14. #include "cmAudioFile.h"
  15. #include "cmText.h"
  16. #include "cmProcObj.h"
  17. #include "cmProcTemplate.h"
  18. #include "cmMath.h"
  19. #include "cmProc.h"
  20. #include "cmVectOps.h"
  21. #include "cmMidi.h"
  22. #include "cmMidiFile.h"
  23. #include "cmTimeLine.h"
  24. #include "cmScore.h"
  25. #include "cmProc4.h"
  26. cmScFol* cmScFolAlloc( cmCtx* c, cmScFol* p, cmReal_t srate, cmScH_t scH, unsigned bufN, unsigned minWndLookAhead, unsigned maxWndCnt, unsigned minVel )
  27. {
  28. cmScFol* op = cmObjAlloc(cmScFol,c,p);
  29. if( srate != 0 )
  30. if( cmScFolInit(op,srate,scH,bufN,minWndLookAhead,maxWndCnt,minVel) != cmOkRC )
  31. cmScFolFree(&op);
  32. return op;
  33. }
  34. cmRC_t cmScFolFree( cmScFol** pp )
  35. {
  36. cmRC_t rc = cmOkRC;
  37. if( pp==NULL || *pp==NULL )
  38. return rc;
  39. cmScFol* p = *pp;
  40. if((rc = cmScFolFinal(p)) != cmOkRC )
  41. return rc;
  42. unsigned i;
  43. for(i=0; i<p->locN; ++i)
  44. cmMemFree(p->loc[i].evtV);
  45. cmMemFree(p->loc);
  46. cmMemFree(p->bufV);
  47. cmObjFree(pp);
  48. return rc;
  49. }
  50. cmRC_t cmScFolFinal( cmScFol* p )
  51. {
  52. cmMemFree(p->edWndMtx);
  53. return cmOkRC;
  54. }
  55. void _cmScFolPrint( cmScFol* p )
  56. {
  57. int i,j;
  58. for(i=0; i<p->locN; ++i)
  59. {
  60. printf("%2i %5i ",p->loc[i].barNumb,p->loc[i].scIdx);
  61. for(j=0; j<p->loc[i].evtCnt; ++j)
  62. printf("%s ",cmMidiToSciPitch(p->loc[i].evtV[j].pitch,NULL,0));
  63. printf("\n");
  64. }
  65. }
  66. unsigned* _cmScFolAllocEditDistMtx(unsigned maxN)
  67. {
  68. maxN += 1;
  69. unsigned* m = cmMemAllocZ(unsigned,maxN*maxN);
  70. unsigned* p = m;
  71. unsigned i;
  72. // initialize the comparison matrix with the default costs in the
  73. // first row and column
  74. // (Note that this matrix is not oriented in column major order like most 'cm' matrices.)
  75. for(i=0; i<maxN; ++i)
  76. {
  77. p[i] = i; // 0th row
  78. p[ i * maxN ] = i; // 0th col
  79. }
  80. return m;
  81. }
  82. cmRC_t cmScFolInit( cmScFol* p, cmReal_t srate, cmScH_t scH, unsigned bufN, unsigned minWndLookAhead, unsigned maxWndCnt, unsigned minVel )
  83. {
  84. cmRC_t rc;
  85. if((rc = cmScFolFinal(p)) != cmOkRC )
  86. return rc;
  87. if( bufN > maxWndCnt )
  88. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The score follower buffer count (%i) must be less than the max. window length (%i).",bufN,maxWndCnt );
  89. if( minWndLookAhead > maxWndCnt )
  90. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The score follower look-ahead count (%i) must be less than the max. window length (%i).",minWndLookAhead,maxWndCnt);
  91. p->srate = srate;
  92. p->scH = scH;
  93. p->bufN = bufN;
  94. p->bufV = cmMemResizeZ(cmScFolBufEle_t,p->bufV,bufN);
  95. p->locN = cmScoreEvtCount(scH);
  96. p->loc = cmMemResizeZ(cmScFolLoc_t,p->loc,p->locN);
  97. p->sbi = cmInvalidIdx;
  98. p->sei = cmInvalidIdx;
  99. p->msln = minWndLookAhead;
  100. p->mswn = maxWndCnt;
  101. p->forwardCnt = 2;
  102. p->maxDist = 4;
  103. p->edWndMtx = _cmScFolAllocEditDistMtx(p->bufN);
  104. p->minVel = minVel;
  105. p->printFl = true;
  106. p->noBackFl = true;
  107. p->missCnt = 0;
  108. p->matchCnt = 0;
  109. p->eventIdx = 0;
  110. p->skipCnt = 0;
  111. p->ret_idx = cmInvalidIdx;
  112. // for each score location
  113. unsigned li,ei;
  114. for(li=0,ei=0; li<cmScoreLocCount(p->scH); ++li)
  115. {
  116. unsigned i,n;
  117. const cmScoreLoc_t* lp = cmScoreLoc(p->scH,li);
  118. // count the number of note events at location li
  119. for(n=0,i=0; i<lp->evtCnt; ++i)
  120. if( lp->evtArray[i]->type == kNonEvtScId )
  121. ++n;
  122. assert( ei+n <= p->locN );
  123. // duplicate each note at location li n times
  124. for(i=0; i<n; ++i)
  125. {
  126. unsigned j,k;
  127. p->loc[ei+i].evtCnt = n;
  128. p->loc[ei+i].evtV = cmMemAllocZ(cmScFolEvt_t,n);
  129. p->loc[ei+i].scIdx = li;
  130. p->loc[ei+i].barNumb = lp->barNumb;
  131. for(j=0,k=0; j<lp->evtCnt; ++j)
  132. if( lp->evtArray[j]->type == kNonEvtScId )
  133. {
  134. p->loc[ei+i].evtV[k].pitch = lp->evtArray[j]->pitch;
  135. p->loc[ei+i].evtV[k].scEvtIdx = lp->evtArray[j]->index;
  136. ++k;
  137. }
  138. }
  139. ei += n;
  140. }
  141. p->locN = ei;
  142. //_cmScFolPrint(p);
  143. return rc;
  144. }
  145. cmRC_t cmScFolReset( cmScFol* p, unsigned scEvtIdx )
  146. {
  147. int i,j;
  148. // empty the event buffer
  149. memset(p->bufV,0,sizeof(cmScFolBufEle_t)*p->bufN);
  150. // don't allow the score index to be prior to the first note
  151. //if( scEvtIdx < p->loc[0].scIdx )
  152. // scEvtIdx = p->loc[0].scIdx;
  153. p->sei = cmInvalidIdx;
  154. p->sbi = cmInvalidIdx;
  155. p->missCnt = 0;
  156. p->matchCnt = 0;
  157. p->eventIdx = 0;
  158. p->skipCnt = 0;
  159. p->ret_idx = cmInvalidIdx;
  160. // locate the score element in svV[] that is closest to,
  161. // and possibly after, scEvtIdx.
  162. for(i=0; i<p->locN-1; ++i)
  163. {
  164. for(j=0; j<p->loc[i].evtCnt; ++j)
  165. if( p->loc[i].evtV[j].scEvtIdx <= scEvtIdx )
  166. p->sbi = i;
  167. else
  168. break;
  169. }
  170. // locate the score element at the end of the look-ahead region
  171. for(; i<p->locN; ++i)
  172. {
  173. for(j=0; j<p->loc[i].evtCnt; ++j)
  174. if( p->loc[i].evtV[j].scEvtIdx <= scEvtIdx + p->msln )
  175. p->sei = i;
  176. }
  177. return cmOkRC;
  178. }
  179. bool _cmScFolIsMatch( const cmScFolLoc_t* loc, unsigned pitch )
  180. {
  181. unsigned i;
  182. for(i=0; i<loc->evtCnt; ++i)
  183. if( loc->evtV[i].pitch == pitch )
  184. return true;
  185. return false;
  186. }
  187. int _cmScFolMatchCost( const cmScFolLoc_t* loc, unsigned li, const cmScFolBufEle_t* pitch, unsigned pi )
  188. {
  189. if( _cmScFolIsMatch(loc+li,pitch[pi].val) )
  190. return 0;
  191. if( li>0 && pi>0 )
  192. if( _cmScFolIsMatch(loc+li-1,pitch[pi].val) && _cmScFolIsMatch(loc+li,pitch[pi-1].val) )
  193. return 0;
  194. return 1;
  195. }
  196. int _cmScFolDist(unsigned mtxMaxN, unsigned* m, const cmScFolBufEle_t* s1, const cmScFolLoc_t* s0, int n )
  197. {
  198. mtxMaxN += 1;
  199. assert( n < mtxMaxN );
  200. int v = 0;
  201. unsigned i;
  202. // Note that m[maxN,maxN] is not oriented in column major order like most 'cm' matrices.
  203. for(i=1; i<n+1; ++i)
  204. {
  205. unsigned ii = i * mtxMaxN; // current row
  206. unsigned i_1 = ii - mtxMaxN; // previous row
  207. unsigned j;
  208. for( j=1; j<n+1; ++j)
  209. {
  210. //int cost = s0[i-1] == s1[j-1] ? 0 : 1;
  211. //int cost = _cmScFolIsMatch(s0 + i-1, s1[j-1]) ? 0 : 1;
  212. int cost = _cmScFolMatchCost(s0,i-1,s1,j-1);
  213. //m[i][j] = min( m[i-1][j] + 1, min( m[i][j-1] + 1, m[i-1][j-1] + cost ) );
  214. m[ ii + j ] = v = cmMin( m[ i_1 + j] + 1, cmMin( m[ ii + j - 1] + 1, m[ i_1 + j - 1 ] + cost ) );
  215. }
  216. }
  217. return v;
  218. }
  219. void _cmScFolRpt0( cmScFol* p, unsigned locIdx, unsigned locN, const cmScFolBufEle_t* b, unsigned bn, unsigned min_idx )
  220. {
  221. unsigned i;
  222. int n;
  223. printf("--------------- event:%i ------------- \n",p->eventIdx);
  224. printf("loc: ");
  225. for(i=0; i<locN; ++i)
  226. printf("%4i ",i+locIdx);
  227. printf("\n");
  228. for(n=0,i=0; i<locN; ++i)
  229. if( p->loc[locIdx+i].evtCnt > n )
  230. n = p->loc[locIdx+i].evtCnt;
  231. --n;
  232. for(; n>=0; --n)
  233. {
  234. printf("sc%1i: ",n);
  235. for(i=0; i<locN; ++i)
  236. {
  237. if( n < p->loc[locIdx+i].evtCnt )
  238. printf("%4s ",cmMidiToSciPitch(p->loc[locIdx+i].evtV[n].pitch,NULL,0));
  239. else
  240. printf(" ");
  241. }
  242. printf("\n");
  243. }
  244. printf("perf:");
  245. for(i=0; i<min_idx; ++i)
  246. printf(" ");
  247. for(i=0; i<bn; ++i)
  248. printf("%4s ",cmMidiToSciPitch(b[i].val,NULL,0));
  249. printf("\n");
  250. }
  251. void _cmScFolRpt1( cmScFol*p, unsigned minDist, unsigned ret_idx, unsigned d1, unsigned missCnt, unsigned matchCnt )
  252. {
  253. printf("dist:%i miss:%i match:%i skip:%i vel:%i ",minDist,missCnt,matchCnt,p->skipCnt,d1);
  254. if( ret_idx != cmInvalidIdx )
  255. printf("ret_idx:%i ",ret_idx);
  256. printf("\n");
  257. }
  258. unsigned cmScFolExec( cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
  259. {
  260. unsigned ret_idx = cmInvalidIdx;
  261. if( p->sbi == cmInvalidIdx )
  262. {
  263. cmCtxRtCondition( &p->obj, cmInvalidArgRC, "An initial score search location has not been set." );
  264. return ret_idx;
  265. }
  266. if( status != kNoteOnMdId )
  267. return ret_idx;
  268. ++p->eventIdx;
  269. // reject notes with very low velocity
  270. if( d1 < p->minVel )
  271. {
  272. ++p->skipCnt;
  273. return ret_idx;
  274. }
  275. // left shift bufV[] to make the right-most element available - then copy in the new element
  276. memmove(p->bufV, p->bufV+1, sizeof(cmScFolBufEle_t)*(p->bufN-1));
  277. p->bufV[ p->bufN-1 ].smpIdx = smpIdx;
  278. p->bufV[ p->bufN-1 ].val = d0;
  279. p->bufV[ p->bufN-1 ].validFl= true;
  280. // fill in ebuf[] with the valid values in bufV[]
  281. int en = cmMin(p->eventIdx,p->bufN);
  282. int bbi = p->eventIdx>=p->bufN ? 0 : p->bufN-p->eventIdx;
  283. // en is the count of valid elements in ebuf[].
  284. // ebuf[p->boi] is the first valid element
  285. int j = 0;
  286. int minDist = INT_MAX;
  287. int minIdx = cmInvalidIdx;
  288. int dist;
  289. // the score wnd must always be as long as the buffer n
  290. // at the end of the score this may not be the case
  291. // (once sei hits locN - at this point we must begin
  292. // shrinking ewnd[] to contain only the last p->sei-p->sbi+1 elements)
  293. assert( p->sei-p->sbi+1 >= en );
  294. for(j=0; p->sbi+en+j-1 <= p->sei; ++j)
  295. {
  296. // use <= minDist to choose the latest window with the lowest match
  297. if((dist = _cmScFolDist(p->bufN, p->edWndMtx, p->bufV+bbi, p->loc + p->sbi+j, en )) < minDist )
  298. {
  299. // only make an eql match if the posn is greater than the last location
  300. if( dist==minDist && p->ret_idx != cmInvalidId && p->ret_idx >= p->sbi+minIdx+en-1 )
  301. continue;
  302. minDist = dist;
  303. minIdx = j;
  304. }
  305. }
  306. // The best fit is on the score window: p->loc[sbi+minIdx : sbi+minIdx+en-1 ]
  307. if( p->printFl )
  308. _cmScFolRpt0( p, p->sbi, p->sei-p->sbi+1, p->bufV+bbi, en, minIdx );
  309. // save current missCnt for later printing
  310. unsigned missCnt = p->missCnt;
  311. // if a perfect match occurred
  312. if( minDist == 0 )
  313. {
  314. ret_idx = p->sbi + minIdx + en - 1;
  315. p->missCnt = 0;
  316. // we had a perfect match - shrink the window to it's minumum size
  317. p->sbi += (en==p->bufN) ? minIdx + 1 : 0; // move wnd begin forward to just past first match
  318. p->sei = p->sbi + minIdx + en + p->msln; // move wnd end forward to lead by the min look-ahead
  319. }
  320. else
  321. {
  322. if( minDist > p->maxDist )
  323. ret_idx = cmInvalidIdx;
  324. else
  325. // if the last event matched - then return the match location as the current score location
  326. if( _cmScFolIsMatch(p->loc+(p->sbi+minIdx+en-1),p->bufV[p->bufN-1].val) )
  327. {
  328. ret_idx = p->sbi + minIdx + en - 1;
  329. p->missCnt = 0;
  330. // this is probably a pretty good match reduce the part of the window prior to
  331. // the first match (bring the end of the window almost up to the end of the
  332. // buffers sync position)
  333. if( en >= p->bufN-1 && (en+2) <= ret_idx )
  334. p->sbi = ret_idx - (en+2);
  335. }
  336. else // the last event does not match based on the optimal edit-distance alignment
  337. {
  338. // Look backward from the closest match location for a match to the current pitch.
  339. // The backward search scope is limited by the current value of 'missCnt'.
  340. unsigned i;
  341. j = p->sbi+minIdx+en-2;
  342. for(i=1; i+1 <= p->bufN && j>=p->sbi && i<=p->missCnt; ++i,--j)
  343. {
  344. // if this look-back location already matched then stop the backward search
  345. if(_cmScFolIsMatch(p->loc+j,p->bufV[p->bufN-1-i].val))
  346. break;
  347. // does this look-back location match the current pitch
  348. if(_cmScFolIsMatch(p->loc+j,p->bufV[p->bufN-1].val))
  349. {
  350. ret_idx = j;
  351. p->missCnt = i; // set missCnt to the cnt of steps backward necessary for a match
  352. break;
  353. }
  354. }
  355. // If the backward search did not find a match - look forward
  356. if( ret_idx == cmInvalidIdx )
  357. {
  358. unsigned i;
  359. j = p->sbi+minIdx+en;
  360. for(i=0; j<=p->sei && i<p->forwardCnt; ++i,++j)
  361. if( _cmScFolIsMatch(p->loc+j,p->bufV[p->bufN-1].val) )
  362. {
  363. ret_idx = j;
  364. break;
  365. }
  366. p->missCnt = ret_idx == cmInvalidIdx ? p->missCnt + 1 : 0;
  367. }
  368. }
  369. // Adjust the end window position (sei) based on the match location
  370. if( ret_idx == cmInvalidIdx )
  371. {
  372. // even though we didn't match move the end of the score window forward
  373. // this will enlarge the score window by one
  374. p->sei += 1;
  375. }
  376. else
  377. {
  378. assert( p->sei>=ret_idx);
  379. // force sei to lead by min look-ahead
  380. if( p->sei - ret_idx < p->msln )
  381. p->sei = ret_idx + p->msln;
  382. }
  383. assert( p->sei > p->sbi );
  384. // Adjust the begin window position
  385. if( p->noBackFl && ret_idx != cmInvalidIdx && en>=p->bufN && p->sbi > p->bufN )
  386. p->sbi = ret_idx - p->bufN;
  387. // if the score window length surpasses the max score window size
  388. // move the beginning index forward
  389. if( p->sei - p->sbi + 1 > p->mswn && p->sei > p->mswn )
  390. p->sbi = p->sei - p->mswn + 1;
  391. }
  392. if( p->printFl )
  393. _cmScFolRpt1(p, minDist, ret_idx, d1, missCnt, p->matchCnt );
  394. // don't allow the returned location to repeat or go backwards
  395. if( p->noBackFl && p->ret_idx != cmInvalidIdx && ret_idx <= p->ret_idx )
  396. ret_idx = cmInvalidIdx;
  397. // track the number of consecutive matches
  398. if( ret_idx == cmInvalidIdx )
  399. p->matchCnt = 0;
  400. else
  401. {
  402. ++p->matchCnt;
  403. p->ret_idx = ret_idx;
  404. }
  405. // Force the window to remain valid when it is at the end of the score
  406. // - sbi and sei must be inside 0:locN
  407. // - sei-sbi + 1 must be >= en
  408. if( p->sei >= p->locN )
  409. {
  410. p->sei = p->locN - 1;
  411. p->sbi = p->sei - p->bufN + 1;
  412. }
  413. if( ret_idx != cmInvalidIdx )
  414. ret_idx = p->loc[ret_idx].scIdx;
  415. return ret_idx;
  416. }
  417. //=======================================================================================================================
  418. cmScTrk* cmScTrkAlloc( cmCtx* c, cmScTrk* p, cmReal_t srate, cmScH_t scH, unsigned bufN, unsigned minWndLookAhead, unsigned maxWndCnt, unsigned minVel )
  419. {
  420. cmScTrk* op = cmObjAlloc(cmScTrk,c,p);
  421. op->sfp = cmScFolAlloc(c,NULL,srate,scH,bufN,minWndLookAhead,maxWndCnt,minVel);
  422. if( srate != 0 )
  423. if( cmScTrkInit(op,srate,scH,bufN,minWndLookAhead,maxWndCnt,minVel) != cmOkRC )
  424. cmScTrkFree(&op);
  425. return op;
  426. }
  427. cmRC_t cmScTrkFree( cmScTrk** pp )
  428. {
  429. cmRC_t rc = cmOkRC;
  430. if( pp==NULL || *pp==NULL )
  431. return rc;
  432. cmScTrk* p = *pp;
  433. if((rc = cmScTrkFinal(p)) != cmOkRC )
  434. return rc;
  435. cmScFolFree(&p->sfp);
  436. cmObjFree(pp);
  437. return rc;
  438. }
  439. void _cmScTrkPrint( cmScTrk* p )
  440. {
  441. int i,j;
  442. for(i=0; i<p->locN; ++i)
  443. {
  444. printf("%2i %5i ",p->loc[i].barNumb,p->loc[i].scIdx);
  445. for(j=0; j<p->loc[i].evtCnt; ++j)
  446. printf("%s ",cmMidiToSciPitch(p->loc[i].evtV[j].pitch,NULL,0));
  447. printf("\n");
  448. }
  449. }
  450. cmRC_t cmScTrkInit( cmScTrk* p, cmReal_t srate, cmScH_t scH, unsigned bufN, unsigned minWndLookAhead, unsigned maxWndCnt, unsigned minVel )
  451. {
  452. cmRC_t rc;
  453. if((rc = cmScTrkFinal(p)) != cmOkRC )
  454. return rc;
  455. if( minWndLookAhead > maxWndCnt )
  456. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The score follower look-ahead count (%i) must be less than the max. window length (%i).",minWndLookAhead,maxWndCnt);
  457. if((rc = cmScFolInit(p->sfp,srate,scH,bufN,minWndLookAhead,maxWndCnt,minVel)) != cmOkRC )
  458. return rc;
  459. p->srate = srate;
  460. p->scH = scH;
  461. p->locN = cmScoreLocCount(scH);
  462. p->loc = cmMemResizeZ(cmScTrkLoc_t,p->loc,p->locN);
  463. p->minVel = minVel;
  464. p->maxWndCnt = maxWndCnt;
  465. p->minWndLookAhead= 4; //minWndLookAhead;
  466. p->printFl = true;
  467. p->curLocIdx = cmInvalidIdx;
  468. p->evtIndex = 0;
  469. // for each score location
  470. unsigned li;
  471. for(li=0; li<cmScoreLocCount(p->scH); ++li)
  472. {
  473. unsigned i,j,k,n;
  474. const cmScoreLoc_t* lp = cmScoreLoc(p->scH,li);
  475. // count the number of note events at location li
  476. for(n=0,i=0; i<lp->evtCnt; ++i)
  477. if( lp->evtArray[i]->type == kNonEvtScId )
  478. ++n;
  479. p->loc[li].evtCnt = n;
  480. p->loc[li].evtV = cmMemAllocZ(cmScTrkEvt_t,n);
  481. p->loc[li].scIdx = li;
  482. p->loc[li].barNumb = lp->barNumb;
  483. for(j=0,k=0; j<lp->evtCnt; ++j)
  484. if( lp->evtArray[j]->type == kNonEvtScId )
  485. {
  486. p->loc[li].evtV[k].pitch = lp->evtArray[j]->pitch;
  487. p->loc[li].evtV[k].scEvtIdx = lp->evtArray[j]->index;
  488. ++k;
  489. }
  490. }
  491. //_cmScTrkPrint(p);
  492. return rc;
  493. }
  494. cmRC_t cmScTrkFinal( cmScTrk* p )
  495. {
  496. unsigned i;
  497. for(i=0; i<p->locN; ++i)
  498. cmMemPtrFree(&p->loc[i].evtV);
  499. return cmOkRC;
  500. }
  501. cmRC_t cmScTrkReset( cmScTrk* p, unsigned scEvtIdx )
  502. {
  503. unsigned i;
  504. cmScFolReset(p->sfp,scEvtIdx);
  505. p->curLocIdx = cmInvalidIdx;
  506. p->evtIndex = 0;
  507. // locate the score element in svV[] that is closest to,
  508. // and possibly after, scEvtIdx.
  509. for(i=0; i<p->locN; ++i)
  510. {
  511. unsigned j;
  512. for(j=0; j<p->loc[i].evtCnt; ++j)
  513. {
  514. p->loc[i].evtV[j].matchFl = false;
  515. // it is possible that scEvtIdx is before the first event included in p->loc[0]
  516. // using the p->curLocIdx==cmInvalidIdx forces the first evt in p->loc[0] to be
  517. // selected in this case
  518. if( p->loc[i].evtV[j].scEvtIdx <= scEvtIdx || p->curLocIdx==cmInvalidIdx )
  519. p->curLocIdx = i;
  520. }
  521. }
  522. if( p->curLocIdx == cmInvalidIdx )
  523. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The initial score search location event %i was not found.", scEvtIdx );
  524. return cmOkRC;
  525. }
  526. unsigned _cmScTrkIsMatch(cmScTrk* p, int d, unsigned pitch )
  527. {
  528. if( 0 <= p->curLocIdx + d && p->curLocIdx+1 < p->locN )
  529. {
  530. unsigned i;
  531. const cmScTrkLoc_t* lp = p->loc + p->curLocIdx + d;
  532. for(i=0; i<lp->evtCnt; ++i)
  533. if( lp->evtV[i].pitch == pitch && lp->evtV[i].matchFl==false)
  534. return i;
  535. }
  536. return cmInvalidIdx;
  537. }
  538. void _cmScTrkRpt0( cmScTrk* p, unsigned pitch, unsigned vel, unsigned nli, unsigned nei )
  539. {
  540. bool missFl = nli==cmInvalidIdx || nei==cmInvalidIdx;
  541. printf("------- event:%i %s vel:%i cur:%i new:%i %s-------\n",p->evtIndex,cmMidiToSciPitch(pitch,NULL,0),vel,p->curLocIdx,nli,missFl?"MISS ":"");
  542. int bi = p->curLocIdx < p->minWndLookAhead ? 0 : p->curLocIdx - p->minWndLookAhead;
  543. int ei = cmMin(p->locN-1,p->curLocIdx+p->minWndLookAhead);
  544. unsigned i,n=0;
  545. for(i=bi; i<=ei; ++i)
  546. if( p->loc[i].evtCnt>n )
  547. n = p->loc[i].evtCnt;
  548. printf("loc ");
  549. for(i=bi; i<=ei; ++i)
  550. printf("%4i ",i);
  551. printf("\n");
  552. for(i=0; i<n; ++i)
  553. {
  554. unsigned j;
  555. printf("sc%2i ",i);
  556. for(j=bi; j<=ei; ++j)
  557. {
  558. if( i < p->loc[j].evtCnt )
  559. {
  560. char* X = p->loc[j].evtV[i].matchFl ? "__" : " ";
  561. if( nli==j && nei==i)
  562. {
  563. X = "**";
  564. assert( p->loc[j].evtV[i].pitch == pitch );
  565. }
  566. printf("%4s%s ",cmMidiToSciPitch(p->loc[j].evtV[i].pitch,NULL,0),X);
  567. }
  568. else
  569. printf(" ");
  570. }
  571. printf("\n");
  572. }
  573. }
  574. unsigned cmScTrkExec( cmScTrk* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
  575. {
  576. unsigned ret_idx = cmInvalidIdx;
  577. //cmScFolExec(p->sfp, smpIdx, status, d0, d1);
  578. if( status != kNoteOnMdId )
  579. return cmInvalidIdx;
  580. if( p->curLocIdx == cmInvalidIdx )
  581. {
  582. cmCtxRtCondition( &p->obj, cmInvalidArgRC, "An initial score search location has not been set." );
  583. return cmInvalidIdx;
  584. }
  585. int i,nei,nli=cmInvalidIdx;
  586. // try to match curLocIdx first
  587. if((nei = _cmScTrkIsMatch(p,0,d0)) != cmInvalidIdx )
  588. nli = p->curLocIdx;
  589. for(i=1; nei==cmInvalidIdx && i<p->minWndLookAhead; ++i)
  590. {
  591. // go forward
  592. if((nei = _cmScTrkIsMatch(p,i,d0)) != cmInvalidIdx )
  593. nli = p->curLocIdx + i;
  594. else
  595. // go backward
  596. if((nei = _cmScTrkIsMatch(p,-i,d0)) != cmInvalidIdx )
  597. nli = p->curLocIdx - i;
  598. }
  599. if( p->printFl )
  600. {
  601. _cmScTrkRpt0(p, d0, d1, nli, nei );
  602. }
  603. if( nli != cmInvalidIdx )
  604. {
  605. p->loc[nli].evtV[nei].matchFl = true;
  606. ret_idx = p->loc[nli].scIdx;
  607. if( nli > p->curLocIdx )
  608. p->curLocIdx = nli;
  609. }
  610. ++p->evtIndex;
  611. return ret_idx;
  612. }
  613. //=======================================================================================================================
  614. //----------------------------------------------------------------------------------------
  615. void ed_print_mtx( ed_r* r)
  616. {
  617. unsigned i,j,k;
  618. for(i=0; i<r->rn; ++i)
  619. {
  620. for(j=0; j<r->cn; ++j)
  621. {
  622. printf("(");
  623. const ed_val* vp = r->m + i + (j*r->rn);
  624. for(k=0; k<kEdCnt; ++k)
  625. {
  626. printf("%i",vp->v[k]);
  627. if( k<kEdCnt-1)
  628. printf(", ");
  629. else
  630. printf(" ");
  631. }
  632. printf("%c)",vp->transFl?'t':' ');
  633. }
  634. printf("\n");
  635. }
  636. }
  637. void ed_init( ed_r* r, const char* s0, const char* s1 )
  638. {
  639. unsigned i,j,k;
  640. r->rn = strlen(s0)+1;
  641. r->cn = strlen(s1)+1;
  642. r->m = cmMemAllocZ(ed_val, r->rn*r->cn );
  643. r->pn = r->rn + r->cn;
  644. r->p_mem = cmMemAllocZ(ed_path, 2*r->pn );
  645. r->p_avl = r->p_mem;
  646. r->p_cur = NULL;
  647. r->p_opt = r->p_mem + r->pn;
  648. r->s_opt = DBL_MAX;
  649. r->s0 = s0;
  650. r->s1 = s1;
  651. for(i=0; i<r->rn; ++i)
  652. for(j=0; j<r->cn; ++j)
  653. {
  654. unsigned v[] = {0,0,0,0};
  655. if( i == 0 )
  656. {
  657. v[kEdMinIdx] = j;
  658. v[kEdInsIdx] = j;
  659. }
  660. else
  661. if( j == 0 )
  662. {
  663. v[kEdMinIdx] = i;
  664. v[kEdDelIdx] = i;
  665. }
  666. for(k=0; k<kEdCnt; ++k)
  667. r->m[ i + (j*r->rn) ].v[k] = v[k];
  668. }
  669. // put pn path records on the available list
  670. for(i=0; i<r->pn; ++i)
  671. r->p_mem[i].next = i<r->pn-1 ? r->p_mem + i + 1 : NULL;
  672. }
  673. unsigned _ed_min( ed_r* r, unsigned i, unsigned j )
  674. {
  675. assert( i<r->rn && j<r->cn );
  676. return r->m[ i + (j*r->rn) ].v[kEdMinIdx];
  677. }
  678. bool _ed_is_trans( ed_r* r, const ed_val* v1p, unsigned i, unsigned j )
  679. {
  680. bool fl = false;
  681. ed_val* v0p = r->m + i + (j*r->rn);
  682. if( i>=1 && j>=1 &&
  683. v1p->v[kEdMinIdx] == v1p->v[kEdSubIdx]
  684. && v1p->matchFl == false
  685. && v0p->v[kEdMinIdx] == v0p->v[kEdSubIdx]
  686. && v0p->matchFl == false )
  687. {
  688. char c00 = r->s0[i-1];
  689. char c01 = r->s0[i ];
  690. char c10 = r->s1[j-1];
  691. char c11 = r->s1[j ];
  692. fl = c00==c11 && c01==c10;
  693. }
  694. return fl;
  695. }
  696. void ed_calc_mtx( ed_r* r )
  697. {
  698. unsigned i,j;
  699. for(i=1; i<r->rn; ++i)
  700. for(j=1; j<r->cn; ++j)
  701. {
  702. ed_val* vp = r->m + i + (j*r->rn);
  703. vp->matchFl = r->s0[i-1] == r->s1[j-1];
  704. unsigned cost = vp->matchFl ? 0 : 1;
  705. vp->v[kEdSubIdx] = _ed_min(r,i-1,j-1) + cost;
  706. vp->v[kEdDelIdx] = _ed_min(r,i-1,j ) + 1;
  707. vp->v[kEdInsIdx] = _ed_min(r,i, j-1) + 1;
  708. vp->v[kEdMinIdx] = cmMin( vp->v[kEdSubIdx], cmMin(vp->v[kEdDelIdx],vp->v[kEdInsIdx]));
  709. vp->transFl = _ed_is_trans(r,vp,i-1,j-1);
  710. }
  711. }
  712. void ed_path_push( ed_r* r, unsigned code, unsigned ri, unsigned ci, bool matchFl, bool transFl )
  713. {
  714. assert(r->p_avl != NULL );
  715. ed_path* p = r->p_avl;
  716. r->p_avl = r->p_avl->next;
  717. p->code = code;
  718. p->ri = ri;
  719. p->ci = ci;
  720. p->matchFl = matchFl;
  721. p->transFl = transFl;
  722. p->next = r->p_cur;
  723. r->p_cur = p;
  724. }
  725. void ed_path_pop( ed_r* r )
  726. {
  727. assert( r->p_cur != NULL );
  728. ed_path* tp = r->p_cur->next;
  729. r->p_cur->next = r->p_avl;
  730. r->p_avl = r->p_cur;
  731. r->p_cur = tp;
  732. }
  733. double ed_score_candidate( ed_r* r )
  734. {
  735. ed_path* cp = r->p_cur;
  736. ed_path* bp = r->p_cur;
  737. ed_path* ep = NULL;
  738. for(; cp!=NULL; cp=cp->next)
  739. if( cp->code != kEdInsIdx )
  740. {
  741. bp = cp;
  742. break;
  743. }
  744. for(; cp!=NULL; cp=cp->next)
  745. if( cp->code!=kEdInsIdx )
  746. ep = cp;
  747. assert( ep!=NULL && bp!=ep);
  748. unsigned n=1;
  749. for(cp=bp; cp!=ep; cp=cp->next)
  750. ++n;
  751. double gapCnt = 0;
  752. double penalty = 0;
  753. bool pfl = bp->matchFl;
  754. unsigned i;
  755. cp = bp;
  756. for(i=0; i<n; ++i,cp=cp->next)
  757. {
  758. // a gap is a transition from a matching subst. to an insert or deletion
  759. //if( pc != cp->code && cp->code != kEdSubIdx && pc==kEdSubIdx && pfl==true )
  760. if( pfl==true && cp->matchFl==false )
  761. ++gapCnt;
  762. //
  763. switch( cp->code )
  764. {
  765. case kEdSubIdx:
  766. penalty += cp->matchFl ? 0 : 1;
  767. penalty -= cp->transFl ? 1 : 0;
  768. break;
  769. case kEdDelIdx:
  770. penalty += 1;
  771. break;
  772. case kEdInsIdx:
  773. penalty += 1;
  774. break;
  775. }
  776. pfl = cp->matchFl;
  777. }
  778. double score = gapCnt/n + penalty;
  779. printf("n:%i gaps:%f gap_score:%f penalty:%f score:%f\n",n,gapCnt,gapCnt/n,penalty,score);
  780. return score;
  781. }
  782. void ed_eval_candidate( ed_r* r, double score )
  783. {
  784. if( r->s_opt == DBL_MAX || r->s_opt > score)
  785. {
  786. // copy the p_cur to p_opt[]
  787. ed_path* cp = r->p_cur;
  788. unsigned i;
  789. for(i=0; cp!=NULL && i<r->pn; cp=cp->next,++i)
  790. {
  791. r->p_opt[i].code = cp->code;
  792. r->p_opt[i].ri = cp->ri;
  793. r->p_opt[i].ci = cp->ci;
  794. r->p_opt[i].matchFl = cp->matchFl;
  795. r->p_opt[i].transFl = cp->transFl;
  796. }
  797. assert( i < r->pn );
  798. r->p_opt[i].code = 0; // terminate with code=0
  799. r->s_opt = score;
  800. }
  801. }
  802. void ed_print_opt( ed_r* r )
  803. {
  804. unsigned i;
  805. for(i=0; r->p_opt[i].code!=0; ++i)
  806. {
  807. ed_path* cp = r->p_opt + i;
  808. char c0 = cp->matchFl ? 'm' : ' ';
  809. char c1 = cp->transFl ? 't' : ' ';
  810. printf("%2i code:%i ri:%2i ci:%2i %c%c\n",i,cp->code,cp->ri,cp->ci,c0,c1);
  811. }
  812. printf("score:%f\n",r->s_opt);
  813. }
  814. void ed_print_candidate( ed_r* r )
  815. {
  816. ed_path* cp = r->p_cur;
  817. unsigned pn = r->pn;
  818. unsigned i;
  819. char s0[pn+1];
  820. char s1[pn+1];
  821. char s2[pn+1];
  822. char s3[pn+1];
  823. s0[pn] = 0;
  824. s1[pn] = 0;
  825. s2[pn] = 0;
  826. s3[pn] = 0;
  827. for(i=0; i<pn && cp!=NULL; ++i,cp=cp->next)
  828. {
  829. switch(cp->code)
  830. {
  831. case kEdSubIdx: // subst
  832. assert( 0 <= cp->ri && cp->ri <= r->rn );
  833. assert( 0 <= cp->ci && cp->ci <= r->cn );
  834. s0[i] = r->s0[cp->ri];
  835. s1[i] = r->s1[cp->ci];
  836. s2[i] = 's';
  837. s3[i] = cp->matchFl ? 'm' : ' ';
  838. break;
  839. case kEdDelIdx: // delete
  840. assert( 0 <= cp->ri && cp->ri <= r->rn );
  841. s0[i] = r->s0[cp->ri];
  842. s1[i] = ' ';
  843. s2[i] = 'd';
  844. s3[i] = ' ';
  845. break;
  846. case kEdInsIdx: // insert
  847. assert( 0 <= cp->ci && cp->ci <= r->cn );
  848. s0[i] = ' ';
  849. s1[i] = r->s1[cp->ci];
  850. s2[i] = 'i';
  851. s3[i] = ' ';
  852. break;
  853. }
  854. }
  855. if( i < pn )
  856. {
  857. s0[i] = 0;
  858. s1[i] = 0;
  859. s2[i] = 0;
  860. s3[i] = 0;
  861. }
  862. printf("\ns0:%s\n",s0);
  863. printf("s1:%s\n",s1);
  864. printf("s2:%s\n",s2);
  865. printf("s3:%s\n",s3);
  866. }
  867. // traverse the solution matrix from the lower-right to
  868. // the upper-left.
  869. void ed_node( ed_r* r, int i, int j )
  870. {
  871. unsigned m;
  872. // stop when the upper-right is encountered
  873. if( i==0 && j==0 )
  874. {
  875. ed_print_candidate(r);
  876. ed_eval_candidate(r, ed_score_candidate(r) );
  877. return;
  878. }
  879. ed_val* vp = r->m + i + (j*r->rn);
  880. // for each possible dir: up,left,up-left
  881. for(m=1; m<kEdCnt; ++m)
  882. if( vp->v[m] == vp->v[kEdMinIdx] )
  883. {
  884. unsigned ii = i-1;
  885. unsigned jj = j-1;
  886. switch(m)
  887. {
  888. case kEdSubIdx:
  889. break;
  890. case kEdDelIdx:
  891. jj = j;
  892. break;
  893. case kEdInsIdx:
  894. ii = i;
  895. break;
  896. }
  897. // prepend to the current candidate path: r->p_cur
  898. ed_path_push(r,m,ii,jj,vp->matchFl,vp->transFl);
  899. // recurse!
  900. ed_node(r,ii,jj);
  901. // remove the first element from the current path
  902. ed_path_pop(r);
  903. }
  904. }
  905. void ed_align( ed_r* r )
  906. {
  907. int i = r->rn-1;
  908. int j = r->cn-1;
  909. unsigned m = r->m[i + (j*r->rn)].v[kEdMinIdx];
  910. if( m==cmMax(r->rn,r->cn) )
  911. printf("Edit distance is at max: %i. No Match.\n",m);
  912. else
  913. ed_node(r,i,j);
  914. }
  915. void ed_free( ed_r* r )
  916. {
  917. cmMemFree(r->m);
  918. cmMemFree(r->p_mem);
  919. }
  920. void ed_main()
  921. {
  922. const char* s0 = "YHCQPGK";
  923. const char* s1 = "LAHYQQKPGKA";
  924. s0 = "ABCDE";
  925. s1 = "ABDCE";
  926. //s1 = "FGHIJK";
  927. ed_r r;
  928. ed_init(&r,s0,s1);
  929. ed_calc_mtx(&r);
  930. ed_print_mtx(&r);
  931. ed_align(&r);
  932. ed_print_opt(&r);
  933. ed_free(&r);
  934. }
  935. //=======================================================================================================================
  936. cmScAlign* cmScAlignAlloc( cmCtx* c, cmScAlign* p, cmScAlignCb_t cbFunc, void* cbArg, cmReal_t srate, cmScH_t scH, unsigned midiN, unsigned scWndN )
  937. {
  938. cmScAlign* op = cmObjAlloc(cmScAlign,c,p);
  939. if( srate != 0 )
  940. if( cmScAlignInit(op,cbFunc,cbArg,srate,scH,midiN,scWndN) != cmOkRC )
  941. cmScAlignFree(&op);
  942. return op;
  943. }
  944. cmRC_t cmScAlignFree( cmScAlign** pp )
  945. {
  946. cmRC_t rc = cmOkRC;
  947. if( pp==NULL || *pp==NULL )
  948. return rc;
  949. cmScAlign* p = *pp;
  950. if((rc = cmScAlignFinal(p)) != cmOkRC )
  951. return rc;
  952. cmMemFree(p->loc);
  953. cmMemFree(p->midiBuf);
  954. cmMemFree(p->m);
  955. cmMemFree(p->p_mem);
  956. cmMemFree(p->res);
  957. cmObjFree(pp);
  958. return rc;
  959. }
  960. void _cmScAlignPrint( cmScAlign* p )
  961. {
  962. int i,j;
  963. for(i=0; i<p->locN; ++i)
  964. {
  965. printf("%2i %5i ",p->loc[i].barNumb,p->loc[i].scLocIdx);
  966. for(j=0; j<p->loc[i].evtCnt; ++j)
  967. printf("%s ",cmMidiToSciPitch(p->loc[i].evtV[j].pitch,NULL,0));
  968. printf("\n");
  969. }
  970. }
  971. cmRC_t cmScAlignInit( cmScAlign* p, cmScAlignCb_t cbFunc, void* cbArg, cmReal_t srate, cmScH_t scH, unsigned midiN, unsigned scWndN )
  972. {
  973. cmRC_t rc;
  974. if((rc = cmScAlignFinal(p)) != cmOkRC )
  975. return rc;
  976. if( midiN > scWndN )
  977. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The score alignment MIDI event buffer length (%i) must be less than the score window length (%i).",midiN,scWndN);
  978. p->cbFunc = cbFunc;
  979. p->cbArg = cbArg;
  980. p->srate = srate;
  981. p->scH = scH;
  982. p->locN = cmScoreEvtCount(scH);
  983. p->loc = cmMemResizeZ(cmScAlignLoc_t,p->loc,p->locN);
  984. p->mn = midiN;
  985. p->midiBuf = cmMemResizeZ(cmScAlignMidiEvt_t,p->midiBuf,midiN);
  986. p->mbi = midiN;
  987. p->printFl = true;
  988. // Setup score structures
  989. // for each score location
  990. unsigned li,ei;
  991. for(li=0,ei=0; li<cmScoreLocCount(p->scH); ++li)
  992. {
  993. unsigned i,n;
  994. const cmScoreLoc_t* lp = cmScoreLoc(p->scH,li);
  995. // count the number of note events at location li
  996. for(n=0,i=0; i<lp->evtCnt; ++i)
  997. if( lp->evtArray[i]->type == kNonEvtScId )
  998. ++n;
  999. assert( ei+n <= p->locN );
  1000. // duplicate each note at location li n times
  1001. for(i=0; i<n; ++i)
  1002. {
  1003. unsigned j,k;
  1004. p->loc[ei+i].evtCnt = n;
  1005. p->loc[ei+i].evtV = cmMemAllocZ(cmScAlignScEvt_t,n);
  1006. p->loc[ei+i].scLocIdx = li;
  1007. p->loc[ei+i].barNumb = lp->barNumb;
  1008. for(j=0,k=0; j<lp->evtCnt; ++j)
  1009. if( lp->evtArray[j]->type == kNonEvtScId )
  1010. {
  1011. p->loc[ei+i].evtV[k].pitch = lp->evtArray[j]->pitch;
  1012. ++k;
  1013. }
  1014. }
  1015. ei += n;
  1016. }
  1017. assert(ei<=p->locN);
  1018. p->locN = ei;
  1019. // setup edit distance structures
  1020. p->rn = midiN+1;
  1021. p->cn = scWndN+1;
  1022. p->m = cmMemResizeZ(cmScAlignVal_t, p->m, p->rn*p->cn );
  1023. p->pn = p->rn + p->cn;
  1024. p->p_mem = cmMemResizeZ(cmScAlignPath_t, p->p_mem, 2*p->pn );
  1025. p->p_avl = p->p_mem;
  1026. p->p_cur = NULL;
  1027. p->p_opt = p->p_mem + p->pn;
  1028. p->s_opt = DBL_MAX;
  1029. p->resN = 2 * cmScoreEvtCount(scH); // make twice as many result records as there are score events
  1030. p->res = cmMemResizeZ(cmScAlignResult_t, p->res, p->resN);
  1031. p->stepCnt = 3;
  1032. p->maxStepMissCnt = 4;
  1033. // fill in the default values for the first row
  1034. // and column of the DP matrix
  1035. unsigned i,j,k;
  1036. for(i=0; i<p->rn; ++i)
  1037. for(j=0; j<p->cn; ++j)
  1038. {
  1039. unsigned v[] = {0,0,0,0};
  1040. if( i == 0 )
  1041. {
  1042. v[kSaMinIdx] = j;
  1043. v[kSaInsIdx] = j;
  1044. }
  1045. else
  1046. if( j == 0 )
  1047. {
  1048. v[kSaMinIdx] = i;
  1049. v[kSaDelIdx] = i;
  1050. }
  1051. for(k=0; k<kSaCnt; ++k)
  1052. p->m[ i + (j*p->rn) ].v[k] = v[k];
  1053. }
  1054. // put pn path records on the available list
  1055. for(i=0; i<p->pn; ++i)
  1056. {
  1057. p->p_mem[i].next = i<p->pn-1 ? p->p_mem + i + 1 : NULL;
  1058. p->p_opt[i].next = i<p->pn-1 ? p->p_opt + i + 1 : NULL;
  1059. }
  1060. //_cmScAlignPrint(p);
  1061. cmScAlignReset(p,0);
  1062. return rc;
  1063. }
  1064. cmRC_t cmScAlignFinal( cmScAlign* p )
  1065. {
  1066. unsigned i;
  1067. for(i=0; i<p->locN; ++i)
  1068. cmMemPtrFree(&p->loc[i].evtV);
  1069. return cmOkRC;
  1070. }
  1071. void cmScAlignReset( cmScAlign* p, unsigned begScanLocIdx )
  1072. {
  1073. assert( begScanLocIdx < p->locN );
  1074. p->mbi = p->mn;
  1075. p->mni = 0;
  1076. p->begScanLocIdx = begScanLocIdx;
  1077. p->begSyncLocIdx = cmInvalidIdx;
  1078. p->s_opt = DBL_MAX;
  1079. p->esi = cmInvalidIdx;
  1080. p->missCnt = 0;
  1081. p->scanCnt = 0;
  1082. p->ri = 0;
  1083. }
  1084. cmScAlignVal_t* _cmScAlignValPtr( cmScAlign* p, unsigned i, unsigned j )
  1085. {
  1086. assert( i < p->rn );
  1087. assert( j < p->cn );
  1088. return p->m + i + (j*p->rn);
  1089. }
  1090. bool _cmScAlignIsMatch( const cmScAlignLoc_t* loc, unsigned pitch )
  1091. {
  1092. unsigned i;
  1093. for(i=0; i<loc->evtCnt; ++i)
  1094. if( loc->evtV[i].pitch == pitch )
  1095. return true;
  1096. return false;
  1097. }
  1098. bool _cmScAlignIsTrans( cmScAlign* p, const cmScAlignVal_t* v1p, unsigned i, unsigned j )
  1099. {
  1100. bool fl = false;
  1101. cmScAlignVal_t* v0p = _cmScAlignValPtr(p,i,j);
  1102. if( i>=1 && j>=1
  1103. && v1p->v[kSaMinIdx] == v1p->v[kSaSubIdx]
  1104. && v1p->matchFl == false
  1105. && v0p->v[kSaMinIdx] == v0p->v[kSaSubIdx]
  1106. && v0p->matchFl == false )
  1107. {
  1108. unsigned c00 = p->midiBuf[i-1].pitch;
  1109. unsigned c01 = p->midiBuf[i ].pitch;
  1110. cmScAlignLoc_t* c10 = p->loc + p->begScanLocIdx + j - 1;
  1111. cmScAlignLoc_t* c11 = p->loc + p->begScanLocIdx + j;
  1112. fl = _cmScAlignIsMatch(c11,c00) && _cmScAlignIsMatch(c10,c01);
  1113. }
  1114. return fl;
  1115. }
  1116. unsigned _cmScAlignMin( cmScAlign* p, unsigned i, unsigned j )
  1117. {
  1118. assert( i<p->rn && j<p->cn );
  1119. //return p->m[ i + (j*p->rn) ].v[kSaMinIdx];
  1120. return _cmScAlignValPtr(p,i,j)->v[kSaMinIdx];
  1121. }
  1122. // Returns 'false' if the score window goes past the end of the score
  1123. // (i.e. p->begScanLocIdx + p->cn > p->locN )
  1124. bool _cmScAlignCalcMtx( cmScAlign* p )
  1125. {
  1126. // the midi buffer must be full
  1127. assert( p->mbi == 0 );
  1128. // loc[begScanLocIdx:begScanLocIdx+p->cn-1] must be valid
  1129. if( p->begScanLocIdx + p->cn > p->locN )
  1130. return false;
  1131. unsigned i,j;
  1132. for(j=1; j<p->cn; ++j)
  1133. for(i=1; i<p->rn; ++i)
  1134. {
  1135. cmScAlignLoc_t* loc = p->loc + p->begScanLocIdx + j - 1;
  1136. unsigned pitch = p->midiBuf[i-1].pitch;
  1137. cmScAlignVal_t* vp = _cmScAlignValPtr(p,i,j);
  1138. vp->matchFl = _cmScAlignIsMatch(loc,pitch);
  1139. unsigned cost = vp->matchFl ? 0 : 1;
  1140. vp->v[kSaSubIdx] = _cmScAlignMin(p,i-1,j-1) + cost;
  1141. vp->v[kSaDelIdx] = _cmScAlignMin(p,i-1,j ) + 1;
  1142. vp->v[kSaInsIdx] = _cmScAlignMin(p,i, j-1) + 1;
  1143. vp->v[kSaMinIdx] = cmMin( vp->v[kSaSubIdx], cmMin(vp->v[kSaDelIdx],vp->v[kSaInsIdx]));
  1144. vp->transFl = _cmScAlignIsTrans(p,vp,i-1,j-1);
  1145. }
  1146. return true;
  1147. }
  1148. void _cmScAlignPathPush( cmScAlign* r, unsigned code, unsigned ri, unsigned ci, bool matchFl, bool transFl )
  1149. {
  1150. assert(r->p_avl != NULL );
  1151. cmScAlignPath_t* p = r->p_avl;
  1152. r->p_avl = r->p_avl->next;
  1153. p->code = code;
  1154. p->ri = ri;
  1155. p->ci = ci;
  1156. p->matchFl = code==kSaSubIdx ? matchFl : false;
  1157. p->transFl = transFl;
  1158. p->next = r->p_cur;
  1159. r->p_cur = p;
  1160. }
  1161. void _cmScAlignPathPop( cmScAlign* r )
  1162. {
  1163. assert( r->p_cur != NULL );
  1164. cmScAlignPath_t* tp = r->p_cur->next;
  1165. r->p_cur->next = r->p_avl;
  1166. r->p_avl = r->p_cur;
  1167. r->p_cur = tp;
  1168. }
  1169. double _cmScAlignScoreCandidate( cmScAlign* r )
  1170. {
  1171. cmScAlignPath_t* cp = r->p_cur;
  1172. cmScAlignPath_t* bp = r->p_cur;
  1173. cmScAlignPath_t* ep = NULL;
  1174. for(; cp!=NULL; cp=cp->next)
  1175. if( cp->code != kSaInsIdx )
  1176. {
  1177. bp = cp;
  1178. break;
  1179. }
  1180. for(; cp!=NULL; cp=cp->next)
  1181. if( cp->code!=kSaInsIdx )
  1182. ep = cp;
  1183. assert( ep!=NULL && bp!=ep);
  1184. unsigned n=1;
  1185. for(cp=bp; cp!=ep; cp=cp->next)
  1186. ++n;
  1187. double gapCnt = 0;
  1188. double penalty = 0;
  1189. bool pfl = bp->matchFl;
  1190. unsigned i;
  1191. cp = bp;
  1192. for(i=0; i<n; ++i,cp=cp->next)
  1193. {
  1194. // a gap is a transition from a matching subst. to an insert or deletion
  1195. //if( pc != cp->code && cp->code != kSaSubIdx && pc==kSaSubIdx && pfl==true )
  1196. if( pfl==true && cp->matchFl==false )
  1197. ++gapCnt;
  1198. //
  1199. switch( cp->code )
  1200. {
  1201. case kSaSubIdx:
  1202. penalty += cp->matchFl ? 0 : 1;
  1203. penalty -= cp->transFl ? 1 : 0;
  1204. break;
  1205. case kSaDelIdx:
  1206. penalty += 1;
  1207. break;
  1208. case kSaInsIdx:
  1209. penalty += 1;
  1210. break;
  1211. }
  1212. pfl = cp->matchFl;
  1213. }
  1214. double score = gapCnt/n + penalty;
  1215. //printf("n:%i gaps:%f gap_score:%f penalty:%f score:%f\n",n,gapCnt,gapCnt/n,penalty,score);
  1216. return score;
  1217. }
  1218. void _cmScAlignEvalCandidate( cmScAlign* r, double score )
  1219. {
  1220. if( r->s_opt == DBL_MAX || score < r->s_opt)
  1221. {
  1222. // copy the p_cur to p_opt[]
  1223. cmScAlignPath_t* cp = r->p_cur;
  1224. unsigned i;
  1225. for(i=0; cp!=NULL && i<r->pn; cp=cp->next,++i)
  1226. {
  1227. r->p_opt[i].code = cp->code;
  1228. r->p_opt[i].ri = cp->ri;
  1229. r->p_opt[i].ci = cp->ci;
  1230. r->p_opt[i].matchFl = cp->matchFl;
  1231. r->p_opt[i].transFl = cp->transFl;
  1232. r->p_opt[i].next = cp->next==NULL ? NULL : r->p_opt + i + 1;
  1233. }
  1234. assert( i < r->pn );
  1235. r->p_opt[i].code = 0; // terminate with code=0
  1236. r->s_opt = score;
  1237. }
  1238. }
  1239. // traverse the solution matrix from the lower-right to
  1240. // the upper-left.
  1241. void _cmScAlignGenPaths( cmScAlign* r, int i, int j )
  1242. {
  1243. unsigned m;
  1244. // stop when the upper-right is encountered
  1245. if( i==0 && j==0 )
  1246. {
  1247. _cmScAlignEvalCandidate(r, _cmScAlignScoreCandidate(r) );
  1248. return;
  1249. }
  1250. cmScAlignVal_t* vp = _cmScAlignValPtr(r,i,j);
  1251. // for each possible dir: up,left,up-left
  1252. for(m=1; m<kSaCnt; ++m)
  1253. if( vp->v[m] == vp->v[kSaMinIdx] )
  1254. {
  1255. // prepend to the current candidate path: r->p_cur
  1256. _cmScAlignPathPush(r,m,i,j,vp->matchFl,vp->transFl);
  1257. int ii = i-1;
  1258. int jj = j-1;
  1259. switch(m)
  1260. {
  1261. case kSaSubIdx:
  1262. break;
  1263. case kSaDelIdx:
  1264. jj = j;
  1265. break;
  1266. case kSaInsIdx:
  1267. ii = i;
  1268. break;
  1269. }
  1270. // recurse!
  1271. _cmScAlignGenPaths(r,ii,jj);
  1272. // remove the first element from the current path
  1273. _cmScAlignPathPop(r);
  1274. }
  1275. }
  1276. double _cmScAlign( cmScAlign* p )
  1277. {
  1278. int i = p->rn-1;
  1279. int j = p->cn-1;
  1280. unsigned m = _cmScAlignMin(p,i,j); //p->m[i + (j*p->rn)].v[kSaMinIdx];
  1281. if( m==cmMax(p->rn,p->cn) )
  1282. printf("Edit distance is at max: %i. No Match.\n",m);
  1283. else
  1284. _cmScAlignGenPaths(p,i,j);
  1285. return p->s_opt;
  1286. }
  1287. cmRC_t cmScAlignExec( cmScAlign* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
  1288. {
  1289. bool fl = p->mbi > 0;
  1290. cmRC_t rc = cmOkRC;
  1291. // update the MIDI buffer with the incoming note
  1292. cmScAlignInputMidi(p,smpIdx,status,d0,d1);
  1293. // if the MIDI buffer transitioned to full then perform an initial scan sync.
  1294. if( fl && p->mbi == 0 )
  1295. {
  1296. if( (p->begSyncLocIdx = cmScAlignScan(p,cmInvalidCnt)) == cmInvalidIdx )
  1297. rc = cmInvalidArgRC; // signal init. scan sync. fail
  1298. }
  1299. else
  1300. {
  1301. // if the MIDI buffer is full then perform a step sync.
  1302. if( !fl && p->mbi == 0 )
  1303. rc = cmScAlignStep(p);
  1304. }
  1305. return rc;
  1306. }
  1307. bool cmScAlignInputMidi( cmScAlign* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
  1308. {
  1309. if( status != kNoteOnMdId )
  1310. return false;
  1311. unsigned mi = p->mn-1;
  1312. //printf("%3i %5.2f %4s\n",p->mni,(double)smpIdx/p->srate,cmMidiToSciPitch(d0,NULL,0));
  1313. // shift the new MIDI event onto the end of the MIDI buffer
  1314. memmove(p->midiBuf,p->midiBuf+1,sizeof(cmScAlignMidiEvt_t)*mi);
  1315. p->midiBuf[mi].locIdx = cmInvalidIdx;
  1316. p->midiBuf[mi].cbCnt = 0;
  1317. p->midiBuf[mi].mni = p->mni++;
  1318. p->midiBuf[mi].smpIdx = smpIdx;
  1319. p->midiBuf[mi].pitch = d0;
  1320. p->midiBuf[mi].vel = d1;
  1321. if( p->mbi > 0 )
  1322. --p->mbi;
  1323. return true;
  1324. }
  1325. // If mep==NULL then the identified score location was not matched (this is an 'insert')
  1326. // these only occurr during 'scan' not 'step'.
  1327. //
  1328. // If locIdx == cmInvalidIdx then the MIDI event did not match a score location
  1329. // When this occurrs during a scan then this is a 'deleted' MIDI note otherwise
  1330. // the note was not found inside loc[esi-stepCnt:esi+stepCnt].
  1331. //
  1332. // If mep!=NULL && scLocIdx!=cmInvalidIdx but matchFl==false then this is a
  1333. // 'substitute' with a mismatch. These only occur during 'scan'.
  1334. void _cmScAlignCb( cmScAlign* p, unsigned locIdx, cmScAlignMidiEvt_t* mep, bool matchFl, bool transFl )
  1335. {
  1336. // verify that the result buffer is not full
  1337. if( p->ri >= p->resN )
  1338. {
  1339. cmCtxRtCondition( &p->obj, cmArgAssertRC, "The score alignment result buffer is full.");
  1340. return;
  1341. }
  1342. // don't report unmatched score locations
  1343. if( mep == NULL )
  1344. return;
  1345. ++mep->cbCnt;
  1346. cmScAlignResult_t* rp = NULL;
  1347. // if this is the first time this MIDI event has generated a callback ...
  1348. if( mep->cbCnt == 1 )
  1349. rp = p->res + p->ri++; // ... then create a new record in p->res[] ...
  1350. else
  1351. if( mep->cbCnt > 1 && matchFl ) // ... otherwise if it was matched ...
  1352. {
  1353. unsigned i;
  1354. for(i=0; i<p->ri; ++i)
  1355. if(p->res[i].mni == mep->mni )
  1356. {
  1357. if( p->res[i].matchFl == false ) // ... and it's previous recd was not matched then update the record with the match info.
  1358. rp = p->res + i;
  1359. }
  1360. }
  1361. if(rp == NULL )
  1362. return;
  1363. assert( locIdx != cmInvalidIdx || mep != NULL );
  1364. rp->locIdx = locIdx;
  1365. rp->smpIdx = mep==NULL ? cmInvalidIdx : mep->smpIdx;
  1366. rp->mni = mep==NULL ? cmInvalidIdx : mep->mni;
  1367. rp->pitch = mep==NULL ? kInvalidMidiPitch : mep->pitch;
  1368. rp->vel = mep==NULL ? kInvalidMidiVelocity : mep->vel;
  1369. rp->matchFl = mep==NULL ? false : matchFl;
  1370. rp->transFl = mep==NULL ? false : transFl;
  1371. }
  1372. void _cmScAlignPrintPath( cmScAlign* p, cmScAlignPath_t* cp, unsigned bsi )
  1373. {
  1374. assert( bsi != cmInvalidIdx );
  1375. cmScAlignPath_t* pp = cp;
  1376. int polyN = 0;
  1377. int i;
  1378. printf("loc: ");
  1379. // get the polyphony count for the score window
  1380. for(i=0; pp!=NULL; pp=pp->next)
  1381. {
  1382. cmScAlignLoc_t* lp = p->loc + bsi + pp->ci;
  1383. if( pp->code!=kSaDelIdx )
  1384. {
  1385. if(lp->evtCnt > polyN)
  1386. polyN = lp->evtCnt;
  1387. printf("%4i ",bsi+i);
  1388. ++i;
  1389. }
  1390. else
  1391. printf("%4s "," ");
  1392. }
  1393. printf("\n");
  1394. // print the score notes
  1395. for(i=polyN; i>0; --i)
  1396. {
  1397. printf("%3i: ",i);
  1398. for(pp=cp; pp!=NULL; pp=pp->next)
  1399. {
  1400. int locIdx = bsi + pp->ci - 1;
  1401. assert(0 <= locIdx && locIdx <= p->locN);
  1402. cmScAlignLoc_t* lp = p->loc + locIdx;
  1403. if( pp->code!=kSaDelIdx && lp->evtCnt >= i )
  1404. printf("%4s ",cmMidiToSciPitch(lp->evtV[i-1].pitch,NULL,0));
  1405. else
  1406. printf("%4s ", pp->code==kSaDelIdx? "-" : " ");
  1407. }
  1408. printf("\n");
  1409. }
  1410. printf("mid: ");
  1411. // print the MIDI buffer
  1412. for(pp=cp; pp!=NULL; pp=pp->next)
  1413. {
  1414. if( pp->code!=kSaInsIdx )
  1415. printf("%4s ",cmMidiToSciPitch(p->midiBuf[pp->ri-1].pitch,NULL,0));
  1416. else
  1417. printf("%4s ",pp->code==kSaInsIdx?"-":" ");
  1418. }
  1419. printf("\nmni: ");
  1420. // print the MIDI buffer index (mni)
  1421. for(pp=cp; pp!=NULL; pp=pp->next)
  1422. {
  1423. if( pp->code!=kSaInsIdx )
  1424. printf("%4i ",p->midiBuf[pp->ri-1].mni);
  1425. else
  1426. printf("%4s ",pp->code==kSaInsIdx?"-":" ");
  1427. }
  1428. printf("\n op: ");
  1429. // print the substitute/insert/delete operation
  1430. for(pp=cp; pp!=NULL; pp=pp->next)
  1431. {
  1432. char c = ' ';
  1433. switch( pp->code )
  1434. {
  1435. case kSaSubIdx: c = 's'; break;
  1436. case kSaDelIdx: c = 'd'; break;
  1437. case kSaInsIdx: c = 'i'; break;
  1438. default:
  1439. { assert(0); }
  1440. }
  1441. printf("%4c ",c);
  1442. }
  1443. printf("\n ");
  1444. // give substitute attribute (match or transpose)
  1445. for(pp=cp; pp!=NULL; pp=pp->next)
  1446. {
  1447. cmChar_t s[3];
  1448. int k = 0;
  1449. if( pp->matchFl )
  1450. s[k++] = 'm';
  1451. if( pp->transFl )
  1452. s[k++] = 't';
  1453. s[k] = 0;
  1454. printf("%4s ",s);
  1455. }
  1456. printf("\nscl: ");
  1457. // print the stored location index
  1458. for(pp=cp; pp!=NULL; pp=pp->next)
  1459. {
  1460. if( pp->locIdx == cmInvalidIdx )
  1461. printf("%4s "," ");
  1462. else
  1463. printf("%4i ",p->loc[pp->locIdx].scLocIdx);
  1464. }
  1465. printf("\n\n");
  1466. }
  1467. // Returns the p->loc[] index at the start of the min cost score window
  1468. // based on the current MIDI buffer.
  1469. // scanCnt is the number of time the score window will be shifted one
  1470. // location to the left
  1471. unsigned cmScAlignScan( cmScAlign* p, unsigned scanCnt )
  1472. {
  1473. unsigned bsi = cmInvalidIdx;
  1474. assert( p->mbi == 0 );
  1475. // if the MIDI buf is full
  1476. if( p->mbi == 0 )
  1477. {
  1478. double s_opt = DBL_MAX;
  1479. unsigned i;
  1480. // Loop as long as the score window is inside the score.
  1481. // Fill the Dyn Pgm matrix: MIDI_buf to score[begScanLocIdx:begScanLocIdx+scWndN-1].
  1482. for(i=0; _cmScAlignCalcMtx(p) && (scanCnt==cmInvalidCnt || i<scanCnt); ++i)
  1483. {
  1484. // locate the path through the DP matrix with the lowest edit distance (cost)
  1485. double cost = _cmScAlign(p);
  1486. // if it is less than any previous score window
  1487. if(cost < s_opt)
  1488. {
  1489. s_opt = cost;
  1490. bsi = p->begScanLocIdx;
  1491. }
  1492. // increment the score window
  1493. p->begScanLocIdx += 1;
  1494. }
  1495. // store the cost assoc'd with bsi
  1496. p->s_opt = s_opt;
  1497. }
  1498. assert( bsi != cmInvalidIdx );
  1499. // Traverse the least cost path and:
  1500. // 1) Set p->esi to the score location index of the last MIDI note
  1501. // which has a positive match with the score and assign
  1502. // the internal score index to cp->locIdx.
  1503. // 2) Set cmScAlignPath_t.locIdx - index into p->loc[] associated
  1504. // with each path element that is a 'substitute' or an 'insert'.
  1505. // 3) Set p->missCnt: the count of trailing non-positive matches.
  1506. // p->missCnt is eventually used in cmScAlignStep() to track the number
  1507. // of consecutive trailing missed notes.
  1508. cmScAlignPath_t* cp = p->p_opt;
  1509. unsigned i = bsi;
  1510. p->missCnt = 0;
  1511. p->esi = cmInvalidIdx;
  1512. for(i=0; cp!=NULL; cp=cp->next)
  1513. {
  1514. if( cp->code != kSaInsIdx )
  1515. {
  1516. assert( cp->ri > 0 );
  1517. p->midiBuf[ cp->ri-1 ].locIdx = cmInvalidIdx;
  1518. }
  1519. switch( cp->code )
  1520. {
  1521. case kSaSubIdx:
  1522. if( cp->matchFl || cp->transFl)
  1523. {
  1524. p->esi = bsi + i;
  1525. p->missCnt = 0;
  1526. if( cp->matchFl )
  1527. p->midiBuf[ cp->ri-1 ].locIdx = bsi + i;
  1528. }
  1529. else
  1530. {
  1531. ++p->missCnt;
  1532. }
  1533. cp->locIdx = bsi + i;
  1534. ++i;
  1535. break;
  1536. case kSaInsIdx:
  1537. cp->locIdx = bsi + i;
  1538. ++i;
  1539. break;
  1540. case kSaDelIdx:
  1541. cp->locIdx = cmInvalidIdx;
  1542. ++p->missCnt;
  1543. break;
  1544. }
  1545. }
  1546. // if no positive matches were found
  1547. if( p->esi == cmInvalidIdx )
  1548. bsi = cmInvalidIdx;
  1549. else
  1550. {
  1551. // report matches
  1552. for(cp=p->p_opt; cp!=NULL; cp=cp->next)
  1553. {
  1554. unsigned locIdx = cp->locIdx;
  1555. cmScAlignMidiEvt_t* mep = NULL;
  1556. if( cp->code != kSaInsIdx )
  1557. mep = p->midiBuf + cp->ri - 1;
  1558. _cmScAlignCb(p,locIdx,mep,cp->matchFl,cp->transFl);
  1559. }
  1560. }
  1561. return bsi;
  1562. }
  1563. cmRC_t cmScAlignStep( cmScAlign* p )
  1564. {
  1565. int i;
  1566. unsigned pitch = p->midiBuf[ p->mn-1 ].pitch;
  1567. unsigned locIdx = cmInvalidIdx;
  1568. // the tracker must be sync'd to step
  1569. if( p->esi == cmInvalidIdx )
  1570. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The p->esi value must be valid to perform a step operation.");
  1571. // if the end of the score has been reached
  1572. if( p->esi + 1 >= p->locN )
  1573. return cmEofRC;
  1574. // attempt to match to next location first
  1575. if( _cmScAlignIsMatch(p->loc + p->esi + 1, pitch) )
  1576. {
  1577. locIdx = p->esi + 1;
  1578. }
  1579. else
  1580. {
  1581. //
  1582. for(i=2; i<p->stepCnt; ++i)
  1583. {
  1584. // go forward
  1585. if( p->esi+i < p->locN && _cmScAlignIsMatch(p->loc + p->esi + i, pitch) )
  1586. {
  1587. locIdx = p->esi + i;
  1588. break;
  1589. }
  1590. // go backward
  1591. if( p->esi >= (i-1) && _cmScAlignIsMatch(p->loc + p->esi - (i-1), pitch) )
  1592. {
  1593. locIdx = p->esi - (i-1);
  1594. break;
  1595. }
  1596. }
  1597. }
  1598. p->midiBuf[ p->mn-1 ].locIdx = locIdx;
  1599. if( locIdx == cmInvalidIdx )
  1600. ++p->missCnt;
  1601. else
  1602. {
  1603. p->missCnt = 0;
  1604. p->esi = locIdx;
  1605. _cmScAlignCb(p,locIdx, p->midiBuf + p->mn - 1,true,false);
  1606. }
  1607. if( p->missCnt >= p->maxStepMissCnt )
  1608. {
  1609. p->begScanLocIdx = p->esi > p->rn ? p->esi - p->rn : 0;
  1610. p->s_opt = DBL_MAX;
  1611. unsigned bsi = cmScAlignScan(p,p->rn*2);
  1612. ++p->scanCnt;
  1613. // if the scan failed find a match
  1614. if( bsi == cmInvalidIdx )
  1615. return cmCtxRtCondition( &p->obj, cmSubSysFailRC, "Scan resync. failed.");
  1616. //if( bsi != cmInvalidIdx )
  1617. // _cmScAlignPrintPath(p, p->p_opt, bsi );
  1618. }
  1619. return cmOkRC;
  1620. }
  1621. void _cmScAlignPrintMtx( cmScAlign* r)
  1622. {
  1623. unsigned i,j,k;
  1624. for(i=0; i<r->rn; ++i)
  1625. {
  1626. for(j=0; j<r->cn; ++j)
  1627. {
  1628. printf("(");
  1629. const cmScAlignVal_t* vp = _cmScAlignValPtr(r,i,j);
  1630. for(k=0; k<kSaCnt; ++k)
  1631. {
  1632. printf("%i",vp->v[k]);
  1633. if( k<kSaCnt-1)
  1634. printf(", ");
  1635. else
  1636. printf(" ");
  1637. }
  1638. printf("%c)",vp->transFl?'t':' ');
  1639. }
  1640. printf("\n");
  1641. }
  1642. }
  1643. void cmScAlignPrintOpt( cmScAlign* p )
  1644. {
  1645. unsigned i;
  1646. for(i=0; p->p_opt[i].code!=0; ++i)
  1647. {
  1648. cmScAlignPath_t* cp = p->p_opt + i;
  1649. char c0 = cp->matchFl ? 'm' : ' ';
  1650. char c1 = cp->transFl ? 't' : ' ';
  1651. printf("%2i code:%i ri:%2i ci:%2i %c%c\n",i,cp->code,cp->ri,cp->ci,c0,c1);
  1652. }
  1653. printf("score:%f\n",p->s_opt);
  1654. }
  1655. enum
  1656. {
  1657. kBarSaFl = 0x01, // this is a score bar
  1658. kScNoteSaFl = 0x02, // this is a score reference note (if mni != cmInvalidIdx then it was matched)
  1659. kSubsErrSaFl = 0x04, // 'subs' mismatch midi note
  1660. kMidiErrSaFl = 0x08, // 'deleted' Midi note
  1661. };
  1662. typedef struct cmScAlignPrint_str
  1663. {
  1664. unsigned flags;
  1665. unsigned scLocIdx;
  1666. unsigned smpIdx;
  1667. unsigned pitch;
  1668. unsigned vel;
  1669. unsigned mni;
  1670. bool matchFl;
  1671. bool transFl;
  1672. } cmScAlignPrint_t;
  1673. void _cmScAlignPrintList( cmScAlignPrint_t* a, unsigned an )
  1674. {
  1675. cmScAlignPrint_t* pp;
  1676. unsigned i;
  1677. printf("----------------------------------------------------\n");
  1678. printf("idx scl mni pit flg \n");
  1679. for(i=0; i<an; ++i)
  1680. {
  1681. pp = a + i;
  1682. printf("%3i %3i %3i %4s 0x%x\n",i,pp->scLocIdx,pp->mni,
  1683. pp->pitch==kInvalidMidiPitch ? " " : cmMidiToSciPitch(pp->pitch,NULL,0),
  1684. pp->flags);
  1685. }
  1686. printf("\n");
  1687. }
  1688. // insert a black record at a[i]
  1689. unsigned _cmScAlignPrintExpand( cmScAlignPrint_t* a, unsigned aan, unsigned i, unsigned an )
  1690. {
  1691. assert( an < aan );
  1692. memmove( a + i + 1, a + i, (an-i)*sizeof(cmScAlignPrint_t));
  1693. memset( a + i, 0, sizeof(cmScAlignPrint_t));
  1694. return an + 1;
  1695. }
  1696. void _cmScAlignPrintOutResult( cmScAlign* p, cmScAlignResult_t* rp, const cmChar_t* label )
  1697. {
  1698. printf("loc:%4i scloc:%4i smp:%10i mni:%4i %4s %c %c %s\n",
  1699. rp->locIdx,
  1700. rp->locIdx==cmInvalidIdx ? -1 : p->loc[rp->locIdx].scLocIdx,
  1701. rp->smpIdx,
  1702. rp->mni,
  1703. rp->pitch<=127 ? cmMidiToSciPitch(rp->pitch,NULL,0) : " ",
  1704. rp->matchFl ? 'm' : ' ',
  1705. rp->transFl ? 't' : ' ',
  1706. label);
  1707. }
  1708. void _cmScAlignPrintSet( cmScAlignPrint_t* pp, const cmScAlignResult_t* rp, unsigned flags, unsigned scLocIdx )
  1709. {
  1710. pp->scLocIdx = scLocIdx;
  1711. pp->flags = flags;
  1712. pp->smpIdx = rp->smpIdx;
  1713. pp->pitch = rp->pitch;
  1714. pp->vel = rp->vel;
  1715. pp->mni = rp->mni;
  1716. assert( pp->scLocIdx!=cmInvalidIdx || pp->mni != cmInvalidIdx );
  1717. }
  1718. unsigned _cmScAlignPrintPoly( cmScAlignPrint_t* a, unsigned an, unsigned scLocIdx )
  1719. {
  1720. unsigned polyN = 0;
  1721. unsigned i;
  1722. for(i=0; i<an; ++i)
  1723. if( a[i].scLocIdx == scLocIdx )
  1724. break;
  1725. if( i < an )
  1726. {
  1727. for(; i<an; ++i,++polyN)
  1728. if( a[i].scLocIdx != scLocIdx )
  1729. break;
  1730. // identical scLocIdx values must be consecutive
  1731. for(; i<an; ++i)
  1732. {
  1733. if( a[i].scLocIdx == scLocIdx )
  1734. _cmScAlignPrintList(a,an);
  1735. assert( a[i].scLocIdx != scLocIdx );
  1736. }
  1737. }
  1738. return polyN;
  1739. }
  1740. cmScAlignPrint_t* _cmScAlignPrintRecd(cmScAlignPrint_t* a, unsigned an, unsigned scLocIdx, unsigned polyIdx )
  1741. {
  1742. unsigned i,j;
  1743. for(i=0; i<an; ++i)
  1744. {
  1745. if( a[i].scLocIdx == scLocIdx )
  1746. for(j=0; i<an; ++j,++i)
  1747. {
  1748. if( a[i].scLocIdx != scLocIdx )
  1749. break;
  1750. if( j == polyIdx )
  1751. return a + i;
  1752. }
  1753. }
  1754. return NULL;
  1755. }
  1756. void _cmScAlignPrintReport( cmScAlign* p, cmScAlignPrint_t* a, unsigned an, unsigned bsi, unsigned esi )
  1757. {
  1758. unsigned colN = 5;
  1759. unsigned bli = bsi;
  1760. bool titleFl = true;
  1761. while( bli < esi )
  1762. {
  1763. unsigned i,j;
  1764. // get ending scLocIdx
  1765. unsigned eli = cmMin(bli+colN, esi);
  1766. // get the max poly count
  1767. unsigned polyN = 0;
  1768. for(i=bli; i<eli; ++i)
  1769. {
  1770. unsigned pn = _cmScAlignPrintPoly(a,an,i);
  1771. if( pn > polyN )
  1772. polyN = pn;
  1773. }
  1774. // print titles
  1775. if( titleFl )
  1776. {
  1777. printf(" ");
  1778. for(j=bli; j<eli; ++j)
  1779. printf("| %4s %4s %3s %1s ","mni"," ","vel"," ");
  1780. printf("\n");
  1781. titleFl = false;
  1782. }
  1783. // print 'loc' index line
  1784. printf("scl: ");
  1785. for(j=bli; j<eli; ++j)
  1786. printf("| %4i %4s %3s %1s ",j," "," "," ");
  1787. printf("\n");
  1788. for(i=polyN; i>0; --i)
  1789. {
  1790. printf("%3i: ",i);
  1791. for(j=bli; j<eli; ++j)
  1792. {
  1793. cmScAlignPrint_t* pp;
  1794. if((pp = _cmScAlignPrintRecd(a,an,j,i-1)) == NULL )
  1795. printf("| %4s %4s %3s %1s "," "," "," "," ");
  1796. else
  1797. {
  1798. if( pp->mni == cmInvalidIdx && cmIsNotFlag(pp->flags,kBarSaFl) )
  1799. printf("| %4s %4s %3s %1s "," ",cmMidiToSciPitch(pp->pitch,NULL,0)," "," ");
  1800. else
  1801. {
  1802. if( cmIsFlag(pp->flags,kBarSaFl) )
  1803. printf("| %4s %4s %3i %1s "," "," | ",pp->pitch,"b");
  1804. else
  1805. {
  1806. const cmChar_t* op = cmIsFlag(pp->flags,kMidiErrSaFl) ? "d" : " ";
  1807. op = cmIsFlag(pp->flags,kSubsErrSaFl) ? "s" : op;
  1808. printf("| %4i %4s %3i %1s ",pp->mni,cmMidiToSciPitch(pp->pitch,NULL,0),pp->vel,op);
  1809. }
  1810. }
  1811. }
  1812. }
  1813. printf("\n");
  1814. }
  1815. printf("\n");
  1816. bli = eli;
  1817. }
  1818. }
  1819. // The goal of this function is to create a cmScAlignPrint_t array containing
  1820. // one record for each score bar, score note and errant MIDI note.
  1821. // The function works by first creating a record for each score bar and note
  1822. // and then scanning the cmScAlignResult_t array (p->res[]) for each result
  1823. // record create by an earlier call to _cmScAlignCb(). A result record can
  1824. // uniquely indicates one of the following result states based on receiving
  1825. // a MIDI event.
  1826. // Match - locIdx!=cmInvalidIdx matchFl==true mni!=cmInvalidIdx
  1827. // Mis-match - locIdx!=cmInvalidIdx matchFl==false mni!=cmInvalidIdx
  1828. // Delete - locIdx==cmInvalidIdx matchFl==false mni!=cmInvalidIdx
  1829. // Insert - locIdx==cmInvalidIdx matchFl==false mni==cmInvalidIdx
  1830. //
  1831. // This is made slightly more complicated by the fact that a given MIDI event
  1832. // may generate more than one result record. This can occur when the
  1833. // tracker is in 'step' mode and generates a result record with a given state
  1834. // as a result of a given MIDI note and then reconsiders that MIDI note
  1835. // while during a subsequent 'scan' mode resync. operation. For example
  1836. // a MIDI note which generate a 'delete' result during a step operation
  1837. // may later generate a match result during a scan.
  1838. double _cmScAlignPrintResult( cmScAlign* p )
  1839. {
  1840. // determine the scH score begin and end indexes
  1841. unsigned bsi = cmScoreLocCount(p->scH);
  1842. unsigned esi = 0;
  1843. unsigned i,j;
  1844. for(i=0; i<p->ri; ++i)
  1845. {
  1846. cmScAlignResult_t* rp = p->res + i;
  1847. assert( rp->locIdx==cmInvalidIdx || rp->locIdx<p->locN);
  1848. if( rp->locIdx != cmInvalidIdx )
  1849. {
  1850. bsi = cmMin(bsi,p->loc[ rp->locIdx ].scLocIdx);
  1851. esi = cmMax(esi,p->loc[ rp->locIdx ].scLocIdx);
  1852. }
  1853. }
  1854. // get a count of MIDI events + score events
  1855. unsigned aan = p->ri;
  1856. for(i=bsi; i<=esi; ++i)
  1857. {
  1858. cmScoreLoc_t* lp = cmScoreLoc( p->scH, i);
  1859. aan += lp->evtCnt;
  1860. }
  1861. cmScAlignPrint_t* a = cmMemAllocZ(cmScAlignPrint_t,aan);
  1862. unsigned an = 0;
  1863. unsigned scNoteCnt = 0; // notes in the score
  1864. unsigned matchCnt = 0; // matched score notes
  1865. unsigned wrongCnt = 0; // errant midi notes
  1866. unsigned skipCnt = 0; // skipped score events
  1867. // create a record for each score event
  1868. for(i=bsi; i<=esi; ++i)
  1869. {
  1870. cmScoreLoc_t* lp = cmScoreLoc( p->scH, i);
  1871. for(j=0; j<lp->evtCnt; ++j,++an)
  1872. {
  1873. assert( an < aan );
  1874. cmScAlignPrint_t* pp = a + an;
  1875. assert( lp->index != cmInvalidIdx );
  1876. pp->scLocIdx = lp->index;
  1877. pp->mni = cmInvalidIdx;
  1878. pp->pitch = kInvalidMidiPitch;
  1879. pp->vel = cmInvalidIdx;
  1880. switch( lp->evtArray[j]->type )
  1881. {
  1882. case kBarEvtScId:
  1883. pp->flags = kBarSaFl;
  1884. pp->pitch = lp->evtArray[j]->barNumb;
  1885. pp->mni = cmInvalidIdx;
  1886. break;
  1887. case kNonEvtScId:
  1888. pp->flags = kScNoteSaFl;
  1889. pp->pitch = lp->evtArray[j]->pitch;
  1890. ++scNoteCnt;
  1891. break;
  1892. }
  1893. }
  1894. }
  1895. //_cmScAlignPrintList(a,an);
  1896. // Update the score with matching MIDI notes
  1897. // for each result record ...
  1898. for(i=0; i<p->ri; ++i)
  1899. {
  1900. cmScAlignResult_t* rp = p->res + i;
  1901. rp->foundFl = false;
  1902. // ... if this is not an errant MIDI note (delete)
  1903. if( rp->locIdx != cmInvalidIdx )
  1904. {
  1905. assert( rp->locIdx != cmInvalidIdx && rp->locIdx < p->locN );
  1906. unsigned scLocIdx = p->loc[rp->locIdx].scLocIdx;
  1907. cmScAlignPrint_t* pp;
  1908. // ... find the score location matching the result record score location
  1909. for(j=0; j<an; ++j)
  1910. {
  1911. pp = a + j;
  1912. // if this score location matches the result score location
  1913. if( scLocIdx == pp->scLocIdx )
  1914. {
  1915. // if this is a matching midi node
  1916. if( rp->matchFl && cmIsFlag(pp->flags,kScNoteSaFl) && pp->pitch == rp->pitch )
  1917. {
  1918. //_cmScAlignPrintOutResult(p,rp,"match");
  1919. rp->foundFl = true;
  1920. _cmScAlignPrintSet(pp, rp, pp->flags, pp->scLocIdx );
  1921. ++matchCnt;
  1922. break;
  1923. }
  1924. // if this is a 'substitute' non-matching note
  1925. if( rp->matchFl == false && rp->mni != cmInvalidIdx )
  1926. {
  1927. //_cmScAlignPrintOutResult(p,rp,"mis-match");
  1928. ++j; // insert after the a[j]
  1929. an = _cmScAlignPrintExpand(a,aan,j,an);
  1930. _cmScAlignPrintSet(a + j, rp, kSubsErrSaFl, scLocIdx );
  1931. rp->foundFl = true;
  1932. ++wrongCnt;
  1933. break;
  1934. }
  1935. // if this is a 'skipped' score note ('insert') alert
  1936. if( rp->mni == cmInvalidIdx )
  1937. {
  1938. //_cmScAlignPrintOutResult(p,rp,"skip");
  1939. rp->foundFl = true;
  1940. break;
  1941. }
  1942. }
  1943. }
  1944. }
  1945. if( rp->foundFl == false )
  1946. {
  1947. // _cmScAlignPrintOutResult(p,rp,"not-found");
  1948. }
  1949. }
  1950. //_cmScAlignPrintList(a,an);
  1951. // Insert records into the print record array (a[](
  1952. // to represent errant MIDI notes. (Notes which
  1953. // were played but do not match any notes in the score.)
  1954. // for each result record ...
  1955. for(i=0; i<p->ri; ++i)
  1956. {
  1957. cmScAlignResult_t* rp = p->res + i;
  1958. cmScAlignPrint_t* pp = NULL;
  1959. cmScAlignPrint_t* dpp = NULL;
  1960. unsigned dmin;
  1961. // if this result did not have a matching score event
  1962. if(rp->foundFl)
  1963. continue;
  1964. // find the print recd with the closest mni
  1965. for(j=0; j<an; ++j)
  1966. {
  1967. pp = a + j;
  1968. if( pp->mni!=cmInvalidId )
  1969. {
  1970. unsigned d;
  1971. if( pp->mni > rp->mni )
  1972. d = pp->mni - rp->mni;
  1973. else
  1974. d = rp->mni - pp->mni;
  1975. if( dpp == NULL || d < dmin )
  1976. {
  1977. dpp = pp;
  1978. dmin = d;
  1979. }
  1980. }
  1981. }
  1982. assert( dpp != NULL );
  1983. j = dpp - a;
  1984. if( rp->mni > dpp->mni )
  1985. ++j;
  1986. assert( rp->locIdx == cmInvalidIdx );
  1987. // insert a print recd before or after the closest print recd
  1988. an = _cmScAlignPrintExpand(a,aan,j,an);
  1989. _cmScAlignPrintSet(a + j, rp, kMidiErrSaFl, dpp->scLocIdx );
  1990. ++wrongCnt;
  1991. }
  1992. for(i=0; i<an; ++i)
  1993. if( cmIsFlag(a[i].flags,kScNoteSaFl) && (a[i].mni == cmInvalidIdx || cmIsFlag(a[i].flags,kSubsErrSaFl)))
  1994. ++skipCnt;
  1995. //_cmScAlignPrintList(a,an);
  1996. //_cmScAlignPrintReport(p,a,an,bsi,esi);
  1997. double prec = (double)2.0 * matchCnt / (matchCnt + wrongCnt);
  1998. double rcal = (double)2.0 * matchCnt / (matchCnt + skipCnt);
  1999. double fmeas = prec * rcal / (prec + rcal);
  2000. printf("midi:%i scans:%i score notes:%i match:%i skip:%i wrong:%i : %f\n",p->mni,p->scanCnt,scNoteCnt,matchCnt,skipCnt,wrongCnt,fmeas);
  2001. cmMemFree(a);
  2002. return fmeas;
  2003. }
  2004. cmRC_t cmScAlignScanToTimeLineEvent( cmScAlign* p, cmTlH_t tlH, cmTlObj_t* top, unsigned endSmpIdx )
  2005. {
  2006. assert( top != NULL );
  2007. cmTlMidiEvt_t* mep = NULL;
  2008. cmRC_t rc = cmOkRC;
  2009. // as long as more MIDI events are available get the next MIDI msg
  2010. while( rc==cmOkRC && (mep = cmTlNextMidiEvtObjPtr(tlH, top, top->seqId )) != NULL )
  2011. {
  2012. top = &mep->obj;
  2013. // if the msg falls after the end of the marker then we are through
  2014. if( mep->obj.seqSmpIdx != cmInvalidIdx && mep->obj.seqSmpIdx > endSmpIdx )
  2015. break;
  2016. // if the time line MIDI msg a note-on
  2017. if( mep->msg->status == kNoteOnMdId )
  2018. {
  2019. rc = cmScAlignExec(p, mep->obj.seqSmpIdx, mep->msg->status, mep->msg->u.chMsgPtr->d0, mep->msg->u.chMsgPtr->d1 );
  2020. switch( rc )
  2021. {
  2022. case cmOkRC: // continue processing MIDI events
  2023. break;
  2024. case cmEofRC: // end of the score was encountered
  2025. break;
  2026. case cmInvalidArgRC: // p->esi was not set correctly
  2027. break;
  2028. case cmSubSysFailRC: // scan resync failed
  2029. break;
  2030. }
  2031. }
  2032. }
  2033. if( rc == cmEofRC )
  2034. rc = cmOkRC;
  2035. return rc;
  2036. }
  2037. void cmScAlignCb( void* cbArg, unsigned scLocIdx, unsigned mni, unsigned pitch, unsigned vel )
  2038. {
  2039. //cmScAlign* p = (cmScAlign*)cbArg;
  2040. }
  2041. void cmScAlignScanMarkers( cmRpt_t* rpt, cmTlH_t tlH, cmScH_t scH )
  2042. {
  2043. unsigned i;
  2044. double srate = 96000;
  2045. unsigned midiN = 7;
  2046. unsigned scWndN = 10;
  2047. unsigned markN = 291;
  2048. cmCtx* ctx = cmCtxAlloc(NULL, rpt, cmLHeapNullHandle, cmSymTblNullHandle );
  2049. cmScAlign* p = cmScAlignAlloc(ctx,NULL,cmScAlignCb,NULL,srate,scH,midiN,scWndN);
  2050. unsigned markCharCnt = 31;
  2051. cmChar_t markText[ markCharCnt+1 ];
  2052. double scoreThresh = 0.5;
  2053. unsigned candCnt = 0;
  2054. unsigned initFailCnt = 0;
  2055. unsigned otherFailCnt = 0;
  2056. unsigned scoreFailCnt = 0;
  2057. p->cbArg = p; // set the callback arg.
  2058. // for each marker
  2059. for(i=0; i<markN; ++i)
  2060. {
  2061. // form the marker text
  2062. snprintf(markText,markCharCnt,"Mark %i",i);
  2063. // locate the marker
  2064. cmTlMarker_t* mp = cmTimeLineMarkerFind( tlH, markText );
  2065. if( mp == NULL )
  2066. {
  2067. printf("The marker '%s' was not found.\n\n",markText);
  2068. continue;
  2069. }
  2070. // skip markers which do not contain text
  2071. if( cmTextIsEmpty(mp->text) )
  2072. {
  2073. printf("The marker '%s' is being skipped because it has no text.\n\n",markText);
  2074. continue;
  2075. }
  2076. // reset the score follower to the beginnig of the score
  2077. cmScAlignReset(p,0);
  2078. ++candCnt;
  2079. // scan to the beginning of the marker
  2080. cmRC_t rc = cmScAlignScanToTimeLineEvent(p,tlH,&mp->obj,mp->obj.seqSmpIdx+mp->obj.durSmpCnt);
  2081. bool pfl = true;
  2082. if( rc != cmOkRC || p->begSyncLocIdx==cmInvalidIdx)
  2083. {
  2084. if( p->begSyncLocIdx == cmInvalidIdx )
  2085. rc = cmInvalidArgRC;
  2086. if( p->mni == 0 )
  2087. {
  2088. printf("mark:%i midi:%i Not enough MIDI notes to fill the scan buffer.\n",i,p->mni);
  2089. pfl = false;
  2090. }
  2091. else
  2092. {
  2093. switch(rc)
  2094. {
  2095. case cmInvalidArgRC:
  2096. printf("mark:%i INITIAL SYNC FAIL\n",i);
  2097. ++initFailCnt;
  2098. pfl = false;
  2099. break;
  2100. case cmSubSysFailRC:
  2101. printf("mark:%i SCAN RESYNC FAIL\n",i);
  2102. ++otherFailCnt;
  2103. break;
  2104. default:
  2105. printf("mark:%i UNKNOWN FAIL\n",i);
  2106. ++otherFailCnt;
  2107. }
  2108. }
  2109. }
  2110. if( pfl )
  2111. {
  2112. //_cmScAlignPrintMtx(p);
  2113. printf("mark:%i scans:%4i loc:%4i bar:%4i score:%5.2f miss:%i text:'%s'\n",i,p->scanCnt,p->begSyncLocIdx,p->loc[p->begSyncLocIdx].barNumb,p->s_opt,p->missCnt,mp->text);
  2114. //_cmScAlignPrintPath(p, p->p_opt, bsi );
  2115. //printf("mark:%i scans:%i midi:%i text:'%s'\n",i,p->scanCnt,p->mni,mp->text);
  2116. if( _cmScAlignPrintResult(p) < scoreThresh )
  2117. ++scoreFailCnt;
  2118. }
  2119. //break; // ONLY USE ONE MARKER DURING TESTING
  2120. printf("\n");
  2121. }
  2122. printf("cand:%i fail:%i - init:%i score:%i other:%i\n\n",candCnt,initFailCnt+scoreFailCnt+otherFailCnt,initFailCnt,scoreFailCnt,otherFailCnt);
  2123. cmScAlignFree(&p);
  2124. cmCtxFree(&ctx);
  2125. }