libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmProc4.c 93KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830
  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. #include "cmTime.h"
  27. cmScFol* cmScFolAlloc( cmCtx* c, cmScFol* p, cmReal_t srate, cmScH_t scH, unsigned bufN, unsigned minWndLookAhead, unsigned maxWndCnt, unsigned minVel )
  28. {
  29. cmScFol* op = cmObjAlloc(cmScFol,c,p);
  30. if( srate != 0 )
  31. if( cmScFolInit(op,srate,scH,bufN,minWndLookAhead,maxWndCnt,minVel) != cmOkRC )
  32. cmScFolFree(&op);
  33. return op;
  34. }
  35. cmRC_t cmScFolFree( cmScFol** pp )
  36. {
  37. cmRC_t rc = cmOkRC;
  38. if( pp==NULL || *pp==NULL )
  39. return rc;
  40. cmScFol* p = *pp;
  41. if((rc = cmScFolFinal(p)) != cmOkRC )
  42. return rc;
  43. unsigned i;
  44. for(i=0; i<p->locN; ++i)
  45. cmMemFree(p->loc[i].evtV);
  46. cmMemFree(p->loc);
  47. cmMemFree(p->bufV);
  48. cmObjFree(pp);
  49. return rc;
  50. }
  51. cmRC_t cmScFolFinal( cmScFol* p )
  52. {
  53. cmMemFree(p->edWndMtx);
  54. return cmOkRC;
  55. }
  56. void _cmScFolPrint( cmScFol* p )
  57. {
  58. int i,j;
  59. for(i=0; i<p->locN; ++i)
  60. {
  61. printf("%2i %5i ",p->loc[i].barNumb,p->loc[i].scIdx);
  62. for(j=0; j<p->loc[i].evtCnt; ++j)
  63. printf("%s ",cmMidiToSciPitch(p->loc[i].evtV[j].pitch,NULL,0));
  64. printf("\n");
  65. }
  66. }
  67. unsigned* _cmScFolAllocEditDistMtx(unsigned maxN)
  68. {
  69. maxN += 1;
  70. unsigned* m = cmMemAllocZ(unsigned,maxN*maxN);
  71. unsigned* p = m;
  72. unsigned i;
  73. // initialize the comparison matrix with the default costs in the
  74. // first row and column
  75. // (Note that this matrix is not oriented in column major order like most 'cm' matrices.)
  76. for(i=0; i<maxN; ++i)
  77. {
  78. p[i] = i; // 0th row
  79. p[ i * maxN ] = i; // 0th col
  80. }
  81. return m;
  82. }
  83. cmRC_t cmScFolInit( cmScFol* p, cmReal_t srate, cmScH_t scH, unsigned bufN, unsigned minWndLookAhead, unsigned maxWndCnt, unsigned minVel )
  84. {
  85. cmRC_t rc;
  86. if((rc = cmScFolFinal(p)) != cmOkRC )
  87. return rc;
  88. if( bufN > maxWndCnt )
  89. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The score follower buffer count (%i) must be less than the max. window length (%i).",bufN,maxWndCnt );
  90. if( minWndLookAhead > maxWndCnt )
  91. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The score follower look-ahead count (%i) must be less than the max. window length (%i).",minWndLookAhead,maxWndCnt);
  92. p->srate = srate;
  93. p->scH = scH;
  94. p->bufN = bufN;
  95. p->bufV = cmMemResizeZ(cmScFolBufEle_t,p->bufV,bufN);
  96. p->locN = cmScoreEvtCount(scH);
  97. p->loc = cmMemResizeZ(cmScFolLoc_t,p->loc,p->locN);
  98. p->sbi = cmInvalidIdx;
  99. p->sei = cmInvalidIdx;
  100. p->msln = minWndLookAhead;
  101. p->mswn = maxWndCnt;
  102. p->forwardCnt = 2;
  103. p->maxDist = 4;
  104. p->edWndMtx = _cmScFolAllocEditDistMtx(p->bufN);
  105. p->minVel = minVel;
  106. p->printFl = true;
  107. p->noBackFl = true;
  108. p->missCnt = 0;
  109. p->matchCnt = 0;
  110. p->eventIdx = 0;
  111. p->skipCnt = 0;
  112. p->ret_idx = cmInvalidIdx;
  113. // for each score location
  114. unsigned li,ei;
  115. for(li=0,ei=0; li<cmScoreLocCount(p->scH); ++li)
  116. {
  117. unsigned i,n;
  118. const cmScoreLoc_t* lp = cmScoreLoc(p->scH,li);
  119. // count the number of note events at location li
  120. for(n=0,i=0; i<lp->evtCnt; ++i)
  121. if( lp->evtArray[i]->type == kNonEvtScId )
  122. ++n;
  123. assert( ei+n <= p->locN );
  124. // duplicate each note at location li n times
  125. for(i=0; i<n; ++i)
  126. {
  127. unsigned j,k;
  128. p->loc[ei+i].evtCnt = n;
  129. p->loc[ei+i].evtV = cmMemAllocZ(cmScFolEvt_t,n);
  130. p->loc[ei+i].scIdx = li;
  131. p->loc[ei+i].barNumb = lp->barNumb;
  132. for(j=0,k=0; j<lp->evtCnt; ++j)
  133. if( lp->evtArray[j]->type == kNonEvtScId )
  134. {
  135. p->loc[ei+i].evtV[k].pitch = lp->evtArray[j]->pitch;
  136. p->loc[ei+i].evtV[k].scEvtIdx = lp->evtArray[j]->index;
  137. ++k;
  138. }
  139. }
  140. ei += n;
  141. }
  142. p->locN = ei;
  143. //_cmScFolPrint(p);
  144. return rc;
  145. }
  146. cmRC_t cmScFolReset( cmScFol* p, unsigned scEvtIdx )
  147. {
  148. int i,j;
  149. // empty the event buffer
  150. memset(p->bufV,0,sizeof(cmScFolBufEle_t)*p->bufN);
  151. // don't allow the score index to be prior to the first note
  152. //if( scEvtIdx < p->loc[0].scIdx )
  153. // scEvtIdx = p->loc[0].scIdx;
  154. p->sei = cmInvalidIdx;
  155. p->sbi = cmInvalidIdx;
  156. p->missCnt = 0;
  157. p->matchCnt = 0;
  158. p->eventIdx = 0;
  159. p->skipCnt = 0;
  160. p->ret_idx = cmInvalidIdx;
  161. // locate the score element in svV[] that is closest to,
  162. // and possibly after, scEvtIdx.
  163. for(i=0; i<p->locN-1; ++i)
  164. {
  165. for(j=0; j<p->loc[i].evtCnt; ++j)
  166. if( p->loc[i].evtV[j].scEvtIdx <= scEvtIdx )
  167. p->sbi = i;
  168. else
  169. break;
  170. }
  171. // locate the score element at the end of the look-ahead region
  172. for(; i<p->locN; ++i)
  173. {
  174. for(j=0; j<p->loc[i].evtCnt; ++j)
  175. if( p->loc[i].evtV[j].scEvtIdx <= scEvtIdx + p->msln )
  176. p->sei = i;
  177. }
  178. return cmOkRC;
  179. }
  180. bool _cmScFolIsMatch( const cmScFolLoc_t* loc, unsigned pitch )
  181. {
  182. unsigned i;
  183. for(i=0; i<loc->evtCnt; ++i)
  184. if( loc->evtV[i].pitch == pitch )
  185. return true;
  186. return false;
  187. }
  188. int _cmScFolMatchCost( const cmScFolLoc_t* loc, unsigned li, const cmScFolBufEle_t* pitch, unsigned pi )
  189. {
  190. if( _cmScFolIsMatch(loc+li,pitch[pi].val) )
  191. return 0;
  192. if( li>0 && pi>0 )
  193. if( _cmScFolIsMatch(loc+li-1,pitch[pi].val) && _cmScFolIsMatch(loc+li,pitch[pi-1].val) )
  194. return 0;
  195. return 1;
  196. }
  197. int _cmScFolDist(unsigned mtxMaxN, unsigned* m, const cmScFolBufEle_t* s1, const cmScFolLoc_t* s0, int n )
  198. {
  199. mtxMaxN += 1;
  200. assert( n < mtxMaxN );
  201. int v = 0;
  202. unsigned i;
  203. // Note that m[maxN,maxN] is not oriented in column major order like most 'cm' matrices.
  204. for(i=1; i<n+1; ++i)
  205. {
  206. unsigned ii = i * mtxMaxN; // current row
  207. unsigned i_1 = ii - mtxMaxN; // previous row
  208. unsigned j;
  209. for( j=1; j<n+1; ++j)
  210. {
  211. //int cost = s0[i-1] == s1[j-1] ? 0 : 1;
  212. //int cost = _cmScFolIsMatch(s0 + i-1, s1[j-1]) ? 0 : 1;
  213. int cost = _cmScFolMatchCost(s0,i-1,s1,j-1);
  214. //m[i][j] = min( m[i-1][j] + 1, min( m[i][j-1] + 1, m[i-1][j-1] + cost ) );
  215. m[ ii + j ] = v = cmMin( m[ i_1 + j] + 1, cmMin( m[ ii + j - 1] + 1, m[ i_1 + j - 1 ] + cost ) );
  216. }
  217. }
  218. return v;
  219. }
  220. void _cmScFolRpt0( cmScFol* p, unsigned locIdx, unsigned locN, const cmScFolBufEle_t* b, unsigned bn, unsigned min_idx )
  221. {
  222. unsigned i;
  223. int n;
  224. printf("--------------- event:%i ------------- \n",p->eventIdx);
  225. printf("loc: ");
  226. for(i=0; i<locN; ++i)
  227. printf("%4i ",i+locIdx);
  228. printf("\n");
  229. for(n=0,i=0; i<locN; ++i)
  230. if( p->loc[locIdx+i].evtCnt > n )
  231. n = p->loc[locIdx+i].evtCnt;
  232. --n;
  233. for(; n>=0; --n)
  234. {
  235. printf("sc%1i: ",n);
  236. for(i=0; i<locN; ++i)
  237. {
  238. if( n < p->loc[locIdx+i].evtCnt )
  239. printf("%4s ",cmMidiToSciPitch(p->loc[locIdx+i].evtV[n].pitch,NULL,0));
  240. else
  241. printf(" ");
  242. }
  243. printf("\n");
  244. }
  245. printf("perf:");
  246. for(i=0; i<min_idx; ++i)
  247. printf(" ");
  248. for(i=0; i<bn; ++i)
  249. printf("%4s ",cmMidiToSciPitch(b[i].val,NULL,0));
  250. printf("\n");
  251. }
  252. void _cmScFolRpt1( cmScFol*p, unsigned minDist, unsigned ret_idx, unsigned d1, unsigned missCnt, unsigned matchCnt )
  253. {
  254. printf("dist:%i miss:%i match:%i skip:%i vel:%i ",minDist,missCnt,matchCnt,p->skipCnt,d1);
  255. if( ret_idx != cmInvalidIdx )
  256. printf("ret_idx:%i ",ret_idx);
  257. printf("\n");
  258. }
  259. unsigned cmScFolExec( cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
  260. {
  261. unsigned ret_idx = cmInvalidIdx;
  262. if( p->sbi == cmInvalidIdx )
  263. {
  264. cmCtxRtCondition( &p->obj, cmInvalidArgRC, "An initial score search location has not been set." );
  265. return ret_idx;
  266. }
  267. if( status != kNoteOnMdId )
  268. return ret_idx;
  269. ++p->eventIdx;
  270. // reject notes with very low velocity
  271. if( d1 < p->minVel )
  272. {
  273. ++p->skipCnt;
  274. return ret_idx;
  275. }
  276. // left shift bufV[] to make the right-most element available - then copy in the new element
  277. memmove(p->bufV, p->bufV+1, sizeof(cmScFolBufEle_t)*(p->bufN-1));
  278. p->bufV[ p->bufN-1 ].smpIdx = smpIdx;
  279. p->bufV[ p->bufN-1 ].val = d0;
  280. p->bufV[ p->bufN-1 ].validFl= true;
  281. // fill in ebuf[] with the valid values in bufV[]
  282. int en = cmMin(p->eventIdx,p->bufN);
  283. int bbi = p->eventIdx>=p->bufN ? 0 : p->bufN-p->eventIdx;
  284. // en is the count of valid elements in ebuf[].
  285. // ebuf[p->boi] is the first valid element
  286. int j = 0;
  287. int minDist = INT_MAX;
  288. int minIdx = cmInvalidIdx;
  289. int dist;
  290. // the score wnd must always be as long as the buffer n
  291. // at the end of the score this may not be the case
  292. // (once sei hits locN - at this point we must begin
  293. // shrinking ewnd[] to contain only the last p->sei-p->sbi+1 elements)
  294. assert( p->sei-p->sbi+1 >= en );
  295. for(j=0; p->sbi+en+j-1 <= p->sei; ++j)
  296. {
  297. // use <= minDist to choose the latest window with the lowest match
  298. if((dist = _cmScFolDist(p->bufN, p->edWndMtx, p->bufV+bbi, p->loc + p->sbi+j, en )) < minDist )
  299. {
  300. // only make an eql match if the posn is greater than the last location
  301. if( dist==minDist && p->ret_idx != cmInvalidId && p->ret_idx >= p->sbi+minIdx+en-1 )
  302. continue;
  303. minDist = dist;
  304. minIdx = j;
  305. }
  306. }
  307. // The best fit is on the score window: p->loc[sbi+minIdx : sbi+minIdx+en-1 ]
  308. if( p->printFl )
  309. _cmScFolRpt0( p, p->sbi, p->sei-p->sbi+1, p->bufV+bbi, en, minIdx );
  310. // save current missCnt for later printing
  311. unsigned missCnt = p->missCnt;
  312. // if a perfect match occurred
  313. if( minDist == 0 )
  314. {
  315. ret_idx = p->sbi + minIdx + en - 1;
  316. p->missCnt = 0;
  317. // we had a perfect match - shrink the window to it's minumum size
  318. p->sbi += (en==p->bufN) ? minIdx + 1 : 0; // move wnd begin forward to just past first match
  319. p->sei = p->sbi + minIdx + en + p->msln; // move wnd end forward to lead by the min look-ahead
  320. }
  321. else
  322. {
  323. if( minDist > p->maxDist )
  324. ret_idx = cmInvalidIdx;
  325. else
  326. // if the last event matched - then return the match location as the current score location
  327. if( _cmScFolIsMatch(p->loc+(p->sbi+minIdx+en-1),p->bufV[p->bufN-1].val) )
  328. {
  329. ret_idx = p->sbi + minIdx + en - 1;
  330. p->missCnt = 0;
  331. // this is probably a pretty good match reduce the part of the window prior to
  332. // the first match (bring the end of the window almost up to the end of the
  333. // buffers sync position)
  334. if( en >= p->bufN-1 && (en+2) <= ret_idx )
  335. p->sbi = ret_idx - (en+2);
  336. }
  337. else // the last event does not match based on the optimal edit-distance alignment
  338. {
  339. // Look backward from the closest match location for a match to the current pitch.
  340. // The backward search scope is limited by the current value of 'missCnt'.
  341. unsigned i;
  342. j = p->sbi+minIdx+en-2;
  343. for(i=1; i+1 <= p->bufN && j>=p->sbi && i<=p->missCnt; ++i,--j)
  344. {
  345. // if this look-back location already matched then stop the backward search
  346. if(_cmScFolIsMatch(p->loc+j,p->bufV[p->bufN-1-i].val))
  347. break;
  348. // does this look-back location match the current pitch
  349. if(_cmScFolIsMatch(p->loc+j,p->bufV[p->bufN-1].val))
  350. {
  351. ret_idx = j;
  352. p->missCnt = i; // set missCnt to the cnt of steps backward necessary for a match
  353. break;
  354. }
  355. }
  356. // If the backward search did not find a match - look forward
  357. if( ret_idx == cmInvalidIdx )
  358. {
  359. unsigned i;
  360. j = p->sbi+minIdx+en;
  361. for(i=0; j<=p->sei && i<p->forwardCnt; ++i,++j)
  362. if( _cmScFolIsMatch(p->loc+j,p->bufV[p->bufN-1].val) )
  363. {
  364. ret_idx = j;
  365. break;
  366. }
  367. p->missCnt = ret_idx == cmInvalidIdx ? p->missCnt + 1 : 0;
  368. }
  369. }
  370. // Adjust the end window position (sei) based on the match location
  371. if( ret_idx == cmInvalidIdx )
  372. {
  373. // even though we didn't match move the end of the score window forward
  374. // this will enlarge the score window by one
  375. p->sei += 1;
  376. }
  377. else
  378. {
  379. assert( p->sei>=ret_idx);
  380. // force sei to lead by min look-ahead
  381. if( p->sei - ret_idx < p->msln )
  382. p->sei = ret_idx + p->msln;
  383. }
  384. assert( p->sei > p->sbi );
  385. // Adjust the begin window position
  386. if( p->noBackFl && ret_idx != cmInvalidIdx && en>=p->bufN && p->sbi > p->bufN )
  387. p->sbi = ret_idx - p->bufN;
  388. // if the score window length surpasses the max score window size
  389. // move the beginning index forward
  390. if( p->sei - p->sbi + 1 > p->mswn && p->sei > p->mswn )
  391. p->sbi = p->sei - p->mswn + 1;
  392. }
  393. if( p->printFl )
  394. _cmScFolRpt1(p, minDist, ret_idx, d1, missCnt, p->matchCnt );
  395. // don't allow the returned location to repeat or go backwards
  396. if( p->noBackFl && p->ret_idx != cmInvalidIdx && ret_idx <= p->ret_idx )
  397. ret_idx = cmInvalidIdx;
  398. // track the number of consecutive matches
  399. if( ret_idx == cmInvalidIdx )
  400. p->matchCnt = 0;
  401. else
  402. {
  403. ++p->matchCnt;
  404. p->ret_idx = ret_idx;
  405. }
  406. // Force the window to remain valid when it is at the end of the score
  407. // - sbi and sei must be inside 0:locN
  408. // - sei-sbi + 1 must be >= en
  409. if( p->sei >= p->locN )
  410. {
  411. p->sei = p->locN - 1;
  412. p->sbi = p->sei - p->bufN + 1;
  413. }
  414. if( ret_idx != cmInvalidIdx )
  415. ret_idx = p->loc[ret_idx].scIdx;
  416. return ret_idx;
  417. }
  418. //=======================================================================================================================
  419. cmScTrk* cmScTrkAlloc( cmCtx* c, cmScTrk* p, cmReal_t srate, cmScH_t scH, unsigned bufN, unsigned minWndLookAhead, unsigned maxWndCnt, unsigned minVel )
  420. {
  421. cmScTrk* op = cmObjAlloc(cmScTrk,c,p);
  422. op->sfp = cmScFolAlloc(c,NULL,srate,scH,bufN,minWndLookAhead,maxWndCnt,minVel);
  423. if( srate != 0 )
  424. if( cmScTrkInit(op,srate,scH,bufN,minWndLookAhead,maxWndCnt,minVel) != cmOkRC )
  425. cmScTrkFree(&op);
  426. return op;
  427. }
  428. cmRC_t cmScTrkFree( cmScTrk** pp )
  429. {
  430. cmRC_t rc = cmOkRC;
  431. if( pp==NULL || *pp==NULL )
  432. return rc;
  433. cmScTrk* p = *pp;
  434. if((rc = cmScTrkFinal(p)) != cmOkRC )
  435. return rc;
  436. cmScFolFree(&p->sfp);
  437. cmObjFree(pp);
  438. return rc;
  439. }
  440. void _cmScTrkPrint( cmScTrk* p )
  441. {
  442. int i,j;
  443. for(i=0; i<p->locN; ++i)
  444. {
  445. printf("%2i %5i ",p->loc[i].barNumb,p->loc[i].scIdx);
  446. for(j=0; j<p->loc[i].evtCnt; ++j)
  447. printf("%s ",cmMidiToSciPitch(p->loc[i].evtV[j].pitch,NULL,0));
  448. printf("\n");
  449. }
  450. }
  451. cmRC_t cmScTrkInit( cmScTrk* p, cmReal_t srate, cmScH_t scH, unsigned bufN, unsigned minWndLookAhead, unsigned maxWndCnt, unsigned minVel )
  452. {
  453. cmRC_t rc;
  454. if((rc = cmScTrkFinal(p)) != cmOkRC )
  455. return rc;
  456. if( minWndLookAhead > maxWndCnt )
  457. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The score follower look-ahead count (%i) must be less than the max. window length (%i).",minWndLookAhead,maxWndCnt);
  458. if((rc = cmScFolInit(p->sfp,srate,scH,bufN,minWndLookAhead,maxWndCnt,minVel)) != cmOkRC )
  459. return rc;
  460. p->srate = srate;
  461. p->scH = scH;
  462. p->locN = cmScoreLocCount(scH);
  463. p->loc = cmMemResizeZ(cmScTrkLoc_t,p->loc,p->locN);
  464. p->minVel = minVel;
  465. p->maxWndCnt = maxWndCnt;
  466. p->minWndLookAhead= 4; //minWndLookAhead;
  467. p->printFl = true;
  468. p->curLocIdx = cmInvalidIdx;
  469. p->evtIndex = 0;
  470. // for each score location
  471. unsigned li;
  472. for(li=0; li<cmScoreLocCount(p->scH); ++li)
  473. {
  474. unsigned i,j,k,n;
  475. const cmScoreLoc_t* lp = cmScoreLoc(p->scH,li);
  476. // count the number of note events at location li
  477. for(n=0,i=0; i<lp->evtCnt; ++i)
  478. if( lp->evtArray[i]->type == kNonEvtScId )
  479. ++n;
  480. p->loc[li].evtCnt = n;
  481. p->loc[li].evtV = cmMemAllocZ(cmScTrkEvt_t,n);
  482. p->loc[li].scIdx = li;
  483. p->loc[li].barNumb = lp->barNumb;
  484. for(j=0,k=0; j<lp->evtCnt; ++j)
  485. if( lp->evtArray[j]->type == kNonEvtScId )
  486. {
  487. p->loc[li].evtV[k].pitch = lp->evtArray[j]->pitch;
  488. p->loc[li].evtV[k].scEvtIdx = lp->evtArray[j]->index;
  489. ++k;
  490. }
  491. }
  492. //_cmScTrkPrint(p);
  493. return rc;
  494. }
  495. cmRC_t cmScTrkFinal( cmScTrk* p )
  496. {
  497. unsigned i;
  498. for(i=0; i<p->locN; ++i)
  499. cmMemPtrFree(&p->loc[i].evtV);
  500. return cmOkRC;
  501. }
  502. cmRC_t cmScTrkReset( cmScTrk* p, unsigned scEvtIdx )
  503. {
  504. unsigned i;
  505. cmScFolReset(p->sfp,scEvtIdx);
  506. p->curLocIdx = cmInvalidIdx;
  507. p->evtIndex = 0;
  508. // locate the score element in svV[] that is closest to,
  509. // and possibly after, scEvtIdx.
  510. for(i=0; i<p->locN; ++i)
  511. {
  512. unsigned j;
  513. for(j=0; j<p->loc[i].evtCnt; ++j)
  514. {
  515. p->loc[i].evtV[j].matchFl = false;
  516. // it is possible that scEvtIdx is before the first event included in p->loc[0]
  517. // using the p->curLocIdx==cmInvalidIdx forces the first evt in p->loc[0] to be
  518. // selected in this case
  519. if( p->loc[i].evtV[j].scEvtIdx <= scEvtIdx || p->curLocIdx==cmInvalidIdx )
  520. p->curLocIdx = i;
  521. }
  522. }
  523. if( p->curLocIdx == cmInvalidIdx )
  524. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The initial score search location event %i was not found.", scEvtIdx );
  525. return cmOkRC;
  526. }
  527. unsigned _cmScTrkIsMatch(cmScTrk* p, int d, unsigned pitch )
  528. {
  529. if( 0 <= p->curLocIdx + d && p->curLocIdx+1 < p->locN )
  530. {
  531. unsigned i;
  532. const cmScTrkLoc_t* lp = p->loc + p->curLocIdx + d;
  533. for(i=0; i<lp->evtCnt; ++i)
  534. if( lp->evtV[i].pitch == pitch && lp->evtV[i].matchFl==false)
  535. return i;
  536. }
  537. return cmInvalidIdx;
  538. }
  539. void _cmScTrkRpt0( cmScTrk* p, unsigned pitch, unsigned vel, unsigned nli, unsigned nei )
  540. {
  541. bool missFl = nli==cmInvalidIdx || nei==cmInvalidIdx;
  542. printf("------- event:%i %s vel:%i cur:%i new:%i %s-------\n",p->evtIndex,cmMidiToSciPitch(pitch,NULL,0),vel,p->curLocIdx,nli,missFl?"MISS ":"");
  543. int bi = p->curLocIdx < p->minWndLookAhead ? 0 : p->curLocIdx - p->minWndLookAhead;
  544. int ei = cmMin(p->locN-1,p->curLocIdx+p->minWndLookAhead);
  545. unsigned i,n=0;
  546. for(i=bi; i<=ei; ++i)
  547. if( p->loc[i].evtCnt>n )
  548. n = p->loc[i].evtCnt;
  549. printf("loc ");
  550. for(i=bi; i<=ei; ++i)
  551. printf("%4i ",i);
  552. printf("\n");
  553. for(i=0; i<n; ++i)
  554. {
  555. unsigned j;
  556. printf("sc%2i ",i);
  557. for(j=bi; j<=ei; ++j)
  558. {
  559. if( i < p->loc[j].evtCnt )
  560. {
  561. char* X = p->loc[j].evtV[i].matchFl ? "__" : " ";
  562. if( nli==j && nei==i)
  563. {
  564. X = "**";
  565. assert( p->loc[j].evtV[i].pitch == pitch );
  566. }
  567. printf("%4s%s ",cmMidiToSciPitch(p->loc[j].evtV[i].pitch,NULL,0),X);
  568. }
  569. else
  570. printf(" ");
  571. }
  572. printf("\n");
  573. }
  574. }
  575. unsigned cmScTrkExec( cmScTrk* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
  576. {
  577. unsigned ret_idx = cmInvalidIdx;
  578. //cmScFolExec(p->sfp, smpIdx, status, d0, d1);
  579. if( status != kNoteOnMdId )
  580. return cmInvalidIdx;
  581. if( p->curLocIdx == cmInvalidIdx )
  582. {
  583. cmCtxRtCondition( &p->obj, cmInvalidArgRC, "An initial score search location has not been set." );
  584. return cmInvalidIdx;
  585. }
  586. int i,nei,nli=cmInvalidIdx;
  587. // try to match curLocIdx first
  588. if((nei = _cmScTrkIsMatch(p,0,d0)) != cmInvalidIdx )
  589. nli = p->curLocIdx;
  590. for(i=1; nei==cmInvalidIdx && i<p->minWndLookAhead; ++i)
  591. {
  592. // go forward
  593. if((nei = _cmScTrkIsMatch(p,i,d0)) != cmInvalidIdx )
  594. nli = p->curLocIdx + i;
  595. else
  596. // go backward
  597. if((nei = _cmScTrkIsMatch(p,-i,d0)) != cmInvalidIdx )
  598. nli = p->curLocIdx - i;
  599. }
  600. if( p->printFl )
  601. {
  602. _cmScTrkRpt0(p, d0, d1, nli, nei );
  603. }
  604. if( nli != cmInvalidIdx )
  605. {
  606. p->loc[nli].evtV[nei].matchFl = true;
  607. ret_idx = p->loc[nli].scIdx;
  608. if( nli > p->curLocIdx )
  609. p->curLocIdx = nli;
  610. }
  611. ++p->evtIndex;
  612. return ret_idx;
  613. }
  614. //=======================================================================================================================
  615. //----------------------------------------------------------------------------------------
  616. void ed_print_mtx( ed_r* r)
  617. {
  618. unsigned i,j,k;
  619. for(i=0; i<r->rn; ++i)
  620. {
  621. for(j=0; j<r->cn; ++j)
  622. {
  623. printf("(");
  624. const ed_val* vp = r->m + i + (j*r->rn);
  625. for(k=0; k<kEdCnt; ++k)
  626. {
  627. printf("%i",vp->v[k]);
  628. if( k<kEdCnt-1)
  629. printf(", ");
  630. else
  631. printf(" ");
  632. }
  633. printf("%c)",vp->transFl?'t':' ');
  634. }
  635. printf("\n");
  636. }
  637. }
  638. void ed_init( ed_r* r, const char* s0, const char* s1 )
  639. {
  640. unsigned i,j,k;
  641. r->rn = strlen(s0)+1;
  642. r->cn = strlen(s1)+1;
  643. r->m = cmMemAllocZ(ed_val, r->rn*r->cn );
  644. r->pn = r->rn + r->cn;
  645. r->p_mem = cmMemAllocZ(ed_path, 2*r->pn );
  646. r->p_avl = r->p_mem;
  647. r->p_cur = NULL;
  648. r->p_opt = r->p_mem + r->pn;
  649. r->s_opt = DBL_MAX;
  650. r->s0 = s0;
  651. r->s1 = s1;
  652. for(i=0; i<r->rn; ++i)
  653. for(j=0; j<r->cn; ++j)
  654. {
  655. unsigned v[] = {0,0,0,0};
  656. if( i == 0 )
  657. {
  658. v[kEdMinIdx] = j;
  659. v[kEdInsIdx] = j;
  660. }
  661. else
  662. if( j == 0 )
  663. {
  664. v[kEdMinIdx] = i;
  665. v[kEdDelIdx] = i;
  666. }
  667. for(k=0; k<kEdCnt; ++k)
  668. r->m[ i + (j*r->rn) ].v[k] = v[k];
  669. }
  670. // put pn path records on the available list
  671. for(i=0; i<r->pn; ++i)
  672. r->p_mem[i].next = i<r->pn-1 ? r->p_mem + i + 1 : NULL;
  673. }
  674. unsigned _ed_min( ed_r* r, unsigned i, unsigned j )
  675. {
  676. assert( i<r->rn && j<r->cn );
  677. return r->m[ i + (j*r->rn) ].v[kEdMinIdx];
  678. }
  679. bool _ed_is_trans( ed_r* r, const ed_val* v1p, unsigned i, unsigned j )
  680. {
  681. bool fl = false;
  682. ed_val* v0p = r->m + i + (j*r->rn);
  683. if( i>=1 && j>=1 &&
  684. v1p->v[kEdMinIdx] == v1p->v[kEdSubIdx]
  685. && v1p->matchFl == false
  686. && v0p->v[kEdMinIdx] == v0p->v[kEdSubIdx]
  687. && v0p->matchFl == false )
  688. {
  689. char c00 = r->s0[i-1];
  690. char c01 = r->s0[i ];
  691. char c10 = r->s1[j-1];
  692. char c11 = r->s1[j ];
  693. fl = c00==c11 && c01==c10;
  694. }
  695. return fl;
  696. }
  697. void ed_calc_mtx( ed_r* r )
  698. {
  699. unsigned i,j;
  700. for(i=1; i<r->rn; ++i)
  701. for(j=1; j<r->cn; ++j)
  702. {
  703. ed_val* vp = r->m + i + (j*r->rn);
  704. vp->matchFl = r->s0[i-1] == r->s1[j-1];
  705. unsigned cost = vp->matchFl ? 0 : 1;
  706. vp->v[kEdSubIdx] = _ed_min(r,i-1,j-1) + cost;
  707. vp->v[kEdDelIdx] = _ed_min(r,i-1,j ) + 1;
  708. vp->v[kEdInsIdx] = _ed_min(r,i, j-1) + 1;
  709. vp->v[kEdMinIdx] = cmMin( vp->v[kEdSubIdx], cmMin(vp->v[kEdDelIdx],vp->v[kEdInsIdx]));
  710. vp->transFl = _ed_is_trans(r,vp,i-1,j-1);
  711. }
  712. }
  713. void ed_path_push( ed_r* r, unsigned code, unsigned ri, unsigned ci, bool matchFl, bool transFl )
  714. {
  715. assert(r->p_avl != NULL );
  716. ed_path* p = r->p_avl;
  717. r->p_avl = r->p_avl->next;
  718. p->code = code;
  719. p->ri = ri;
  720. p->ci = ci;
  721. p->matchFl = matchFl;
  722. p->transFl = transFl;
  723. p->next = r->p_cur;
  724. r->p_cur = p;
  725. }
  726. void ed_path_pop( ed_r* r )
  727. {
  728. assert( r->p_cur != NULL );
  729. ed_path* tp = r->p_cur->next;
  730. r->p_cur->next = r->p_avl;
  731. r->p_avl = r->p_cur;
  732. r->p_cur = tp;
  733. }
  734. double ed_score_candidate( ed_r* r )
  735. {
  736. ed_path* cp = r->p_cur;
  737. ed_path* bp = r->p_cur;
  738. ed_path* ep = NULL;
  739. for(; cp!=NULL; cp=cp->next)
  740. if( cp->code != kEdInsIdx )
  741. {
  742. bp = cp;
  743. break;
  744. }
  745. for(; cp!=NULL; cp=cp->next)
  746. if( cp->code!=kEdInsIdx )
  747. ep = cp;
  748. assert( ep!=NULL && bp!=ep);
  749. unsigned n=1;
  750. for(cp=bp; cp!=ep; cp=cp->next)
  751. ++n;
  752. double gapCnt = 0;
  753. double penalty = 0;
  754. bool pfl = bp->matchFl;
  755. unsigned i;
  756. cp = bp;
  757. for(i=0; i<n; ++i,cp=cp->next)
  758. {
  759. // a gap is a transition from a matching subst. to an insert or deletion
  760. //if( pc != cp->code && cp->code != kEdSubIdx && pc==kEdSubIdx && pfl==true )
  761. if( pfl==true && cp->matchFl==false )
  762. ++gapCnt;
  763. //
  764. switch( cp->code )
  765. {
  766. case kEdSubIdx:
  767. penalty += cp->matchFl ? 0 : 1;
  768. penalty -= cp->transFl ? 1 : 0;
  769. break;
  770. case kEdDelIdx:
  771. penalty += 1;
  772. break;
  773. case kEdInsIdx:
  774. penalty += 1;
  775. break;
  776. }
  777. pfl = cp->matchFl;
  778. }
  779. double score = gapCnt/n + penalty;
  780. printf("n:%i gaps:%f gap_score:%f penalty:%f score:%f\n",n,gapCnt,gapCnt/n,penalty,score);
  781. return score;
  782. }
  783. void ed_eval_candidate( ed_r* r, double score )
  784. {
  785. if( r->s_opt == DBL_MAX || r->s_opt > score)
  786. {
  787. // copy the p_cur to p_opt[]
  788. ed_path* cp = r->p_cur;
  789. unsigned i;
  790. for(i=0; cp!=NULL && i<r->pn; cp=cp->next,++i)
  791. {
  792. r->p_opt[i].code = cp->code;
  793. r->p_opt[i].ri = cp->ri;
  794. r->p_opt[i].ci = cp->ci;
  795. r->p_opt[i].matchFl = cp->matchFl;
  796. r->p_opt[i].transFl = cp->transFl;
  797. }
  798. assert( i < r->pn );
  799. r->p_opt[i].code = 0; // terminate with code=0
  800. r->s_opt = score;
  801. }
  802. }
  803. void ed_print_opt( ed_r* r )
  804. {
  805. unsigned i;
  806. for(i=0; r->p_opt[i].code!=0; ++i)
  807. {
  808. ed_path* cp = r->p_opt + i;
  809. char c0 = cp->matchFl ? 'm' : ' ';
  810. char c1 = cp->transFl ? 't' : ' ';
  811. printf("%2i code:%i ri:%2i ci:%2i %c%c\n",i,cp->code,cp->ri,cp->ci,c0,c1);
  812. }
  813. printf("score:%f\n",r->s_opt);
  814. }
  815. void ed_print_candidate( ed_r* r )
  816. {
  817. ed_path* cp = r->p_cur;
  818. unsigned pn = r->pn;
  819. unsigned i;
  820. char s0[pn+1];
  821. char s1[pn+1];
  822. char s2[pn+1];
  823. char s3[pn+1];
  824. s0[pn] = 0;
  825. s1[pn] = 0;
  826. s2[pn] = 0;
  827. s3[pn] = 0;
  828. for(i=0; i<pn && cp!=NULL; ++i,cp=cp->next)
  829. {
  830. switch(cp->code)
  831. {
  832. case kEdSubIdx: // subst
  833. assert( 0 <= cp->ri && cp->ri <= r->rn );
  834. assert( 0 <= cp->ci && cp->ci <= r->cn );
  835. s0[i] = r->s0[cp->ri];
  836. s1[i] = r->s1[cp->ci];
  837. s2[i] = 's';
  838. s3[i] = cp->matchFl ? 'm' : ' ';
  839. break;
  840. case kEdDelIdx: // delete
  841. assert( 0 <= cp->ri && cp->ri <= r->rn );
  842. s0[i] = r->s0[cp->ri];
  843. s1[i] = ' ';
  844. s2[i] = 'd';
  845. s3[i] = ' ';
  846. break;
  847. case kEdInsIdx: // insert
  848. assert( 0 <= cp->ci && cp->ci <= r->cn );
  849. s0[i] = ' ';
  850. s1[i] = r->s1[cp->ci];
  851. s2[i] = 'i';
  852. s3[i] = ' ';
  853. break;
  854. }
  855. }
  856. if( i < pn )
  857. {
  858. s0[i] = 0;
  859. s1[i] = 0;
  860. s2[i] = 0;
  861. s3[i] = 0;
  862. }
  863. printf("\ns0:%s\n",s0);
  864. printf("s1:%s\n",s1);
  865. printf("s2:%s\n",s2);
  866. printf("s3:%s\n",s3);
  867. }
  868. // traverse the solution matrix from the lower-right to
  869. // the upper-left.
  870. void ed_node( ed_r* r, int i, int j )
  871. {
  872. unsigned m;
  873. // stop when the upper-right is encountered
  874. if( i==0 && j==0 )
  875. {
  876. ed_print_candidate(r);
  877. ed_eval_candidate(r, ed_score_candidate(r) );
  878. return;
  879. }
  880. ed_val* vp = r->m + i + (j*r->rn);
  881. // for each possible dir: up,left,up-left
  882. for(m=1; m<kEdCnt; ++m)
  883. if( vp->v[m] == vp->v[kEdMinIdx] )
  884. {
  885. unsigned ii = i-1;
  886. unsigned jj = j-1;
  887. switch(m)
  888. {
  889. case kEdSubIdx:
  890. break;
  891. case kEdDelIdx:
  892. jj = j;
  893. break;
  894. case kEdInsIdx:
  895. ii = i;
  896. break;
  897. }
  898. // prepend to the current candidate path: r->p_cur
  899. ed_path_push(r,m,ii,jj,vp->matchFl,vp->transFl);
  900. // recurse!
  901. ed_node(r,ii,jj);
  902. // remove the first element from the current path
  903. ed_path_pop(r);
  904. }
  905. }
  906. void ed_align( ed_r* r )
  907. {
  908. int i = r->rn-1;
  909. int j = r->cn-1;
  910. unsigned m = r->m[i + (j*r->rn)].v[kEdMinIdx];
  911. if( m==cmMax(r->rn,r->cn) )
  912. printf("Edit distance is at max: %i. No Match.\n",m);
  913. else
  914. ed_node(r,i,j);
  915. }
  916. void ed_free( ed_r* r )
  917. {
  918. cmMemFree(r->m);
  919. cmMemFree(r->p_mem);
  920. }
  921. void ed_main()
  922. {
  923. const char* s0 = "YHCQPGK";
  924. const char* s1 = "LAHYQQKPGKA";
  925. s0 = "ABCDE";
  926. s1 = "ABDCE";
  927. //s1 = "FGHIJK";
  928. ed_r r;
  929. ed_init(&r,s0,s1);
  930. ed_calc_mtx(&r);
  931. ed_print_mtx(&r);
  932. ed_align(&r);
  933. ed_print_opt(&r);
  934. ed_free(&r);
  935. }
  936. //=======================================================================================================================
  937. cmScMatch* cmScMatchAlloc( cmCtx* c, cmScMatch* p, cmScH_t scH, unsigned maxScWndN, unsigned maxMidiWndN )
  938. {
  939. cmScMatch* op = cmObjAlloc(cmScMatch,c,p);
  940. if( cmScoreIsValid(scH) )
  941. if( cmScMatchInit(op,scH,maxScWndN,maxMidiWndN) != cmOkRC )
  942. cmScMatchFree(&op);
  943. return op;
  944. }
  945. cmRC_t cmScMatchFree( cmScMatch** pp )
  946. {
  947. cmRC_t rc = cmOkRC;
  948. if( pp==NULL || *pp==NULL )
  949. return rc;
  950. cmScMatch* p = *pp;
  951. if((rc = cmScMatchFinal(p)) != cmOkRC )
  952. return rc;
  953. cmMemFree(p->loc);
  954. cmMemFree(p->m);
  955. cmMemFree(p->p_mem);
  956. cmObjFree(pp);
  957. return rc;
  958. }
  959. void _cmScMatchInitLoc( cmScMatch* p )
  960. {
  961. unsigned li,ei;
  962. p->locN = cmScoreEvtCount(p->scH);
  963. p->loc = cmMemResizeZ(cmScMatchLoc_t,p->loc,p->locN);
  964. // for each score location
  965. for(li=0,ei=0; li<cmScoreLocCount(p->scH); ++li)
  966. {
  967. unsigned i,n;
  968. const cmScoreLoc_t* lp = cmScoreLoc(p->scH,li);
  969. // count the number of note events at location li
  970. for(n=0,i=0; i<lp->evtCnt; ++i)
  971. if( lp->evtArray[i]->type == kNonEvtScId )
  972. ++n;
  973. assert( ei+n <= p->locN );
  974. // duplicate each note at location li n times
  975. for(i=0; i<n; ++i)
  976. {
  977. unsigned j,k;
  978. p->loc[ei+i].evtCnt = n;
  979. p->loc[ei+i].evtV = cmMemAllocZ(cmScMatchEvt_t,n);
  980. p->loc[ei+i].scLocIdx = li;
  981. p->loc[ei+i].barNumb = lp->barNumb;
  982. for(j=0,k=0; j<lp->evtCnt; ++j)
  983. if( lp->evtArray[j]->type == kNonEvtScId )
  984. {
  985. p->loc[ei+i].evtV[k].pitch = lp->evtArray[j]->pitch;
  986. ++k;
  987. }
  988. }
  989. ei += n;
  990. }
  991. assert(ei<=p->locN);
  992. p->locN = ei;
  993. }
  994. cmRC_t cmScMatchInit( cmScMatch* p, cmScH_t scH, unsigned maxScWndN, unsigned maxMidiWndN )
  995. {
  996. unsigned i;
  997. cmRC_t rc;
  998. if((rc = cmScMatchFinal(p)) != cmOkRC )
  999. return rc;
  1000. p->scH = scH;
  1001. p->mrn = maxMidiWndN + 1;
  1002. p->mcn = maxScWndN + 1;
  1003. p->mmn = maxMidiWndN;
  1004. p->msn = maxScWndN;
  1005. _cmScMatchInitLoc(p);
  1006. p->m = cmMemResizeZ(cmScMatchVal_t, p->m, p->mrn*p->mcn );
  1007. p->pn = p->mrn + p->mcn;
  1008. p->p_mem = cmMemResizeZ(cmScMatchPath_t, p->p_mem, 2*p->pn );
  1009. p->p_avl = p->p_mem;
  1010. p->p_cur = NULL;
  1011. p->p_opt = p->p_mem + p->pn;
  1012. // put pn path records on the available list
  1013. for(i=0; i<p->pn; ++i)
  1014. {
  1015. p->p_mem[i].next = i<p->pn-1 ? p->p_mem + i + 1 : NULL;
  1016. p->p_opt[i].next = i<p->pn-1 ? p->p_opt + i + 1 : NULL;
  1017. }
  1018. return rc;
  1019. }
  1020. cmRC_t cmScMatchFinal( cmScMatch* p )
  1021. {
  1022. unsigned i;
  1023. if( p != NULL )
  1024. for(i=0; i<p->locN; ++i)
  1025. cmMemPtrFree(&p->loc[i].evtV);
  1026. return cmOkRC;
  1027. }
  1028. cmRC_t _cmScMatchInitMtx( cmScMatch* p, unsigned rn, unsigned cn )
  1029. {
  1030. if( rn >p->mrn && cn > p->mcn )
  1031. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "MIDI sequence length must be less than %i. Score sequence length must be less than %i.",p->mmn,p->msn);
  1032. // if the size of the mtx is not changing then there is nothing to do
  1033. if( rn == p->rn && cn == p->cn )
  1034. return cmOkRC;
  1035. // update the mtx size
  1036. p->rn = rn;
  1037. p->cn = cn;
  1038. // fill in the default values for the first row
  1039. // and column of the DP matrix
  1040. unsigned i,j,k;
  1041. for(i=0; i<rn; ++i)
  1042. for(j=0; j<cn; ++j)
  1043. {
  1044. unsigned v[] = {0,0,0,0};
  1045. if( i == 0 )
  1046. {
  1047. v[kSmMinIdx] = j;
  1048. v[kSmInsIdx] = j;
  1049. }
  1050. else
  1051. if( j == 0 )
  1052. {
  1053. v[kSmMinIdx] = i;
  1054. v[kSmDelIdx] = i;
  1055. }
  1056. for(k=0; k<kSmCnt; ++k)
  1057. p->m[ i + (j*rn) ].v[k] = v[k];
  1058. }
  1059. return cmOkRC;
  1060. }
  1061. cmScMatchVal_t* _cmScMatchValPtr( cmScMatch* p, unsigned i, unsigned j, unsigned rn, unsigned cn )
  1062. {
  1063. assert( i < rn && j < cn );
  1064. return p->m + i + (j*rn);
  1065. }
  1066. bool _cmScMatchIsMatch( const cmScMatchLoc_t* loc, unsigned pitch )
  1067. {
  1068. unsigned i;
  1069. for(i=0; i<loc->evtCnt; ++i)
  1070. if( loc->evtV[i].pitch == pitch )
  1071. return true;
  1072. return false;
  1073. }
  1074. bool _cmScMatchIsTrans( cmScMatch* p, const unsigned* pitchV, const cmScMatchVal_t* v1p, unsigned bsi, unsigned i, unsigned j, unsigned rn, unsigned cn )
  1075. {
  1076. bool fl = false;
  1077. cmScMatchVal_t* v0p = _cmScMatchValPtr(p,i,j,rn,cn);
  1078. if( i>=1 && j>=1
  1079. && v1p->v[kSmMinIdx] == v1p->v[kSmSubIdx]
  1080. && cmIsNotFlag(v1p->flags,kSmMatchFl)
  1081. && v0p->v[kSmMinIdx] == v0p->v[kSmSubIdx]
  1082. && cmIsNotFlag(v0p->flags,kSmMatchFl)
  1083. )
  1084. {
  1085. unsigned c00 = pitchV[i-1];
  1086. unsigned c01 = pitchV[i ];
  1087. cmScMatchLoc_t* c10 = p->loc + bsi + j - 1;
  1088. cmScMatchLoc_t* c11 = p->loc + bsi + j;
  1089. fl = _cmScMatchIsMatch(c11,c00) && _cmScMatchIsMatch(c10,c01);
  1090. }
  1091. return fl;
  1092. }
  1093. unsigned _cmScMatchMin( cmScMatch* p, unsigned i, unsigned j, unsigned rn, unsigned cn )
  1094. {
  1095. return _cmScMatchValPtr(p,i,j,rn,cn)->v[kSmMinIdx];
  1096. }
  1097. // Return false if bsi + cn > p->locN
  1098. // pitchV[rn-1]
  1099. bool _cmScMatchCalcMtx( cmScMatch* p, unsigned bsi, const unsigned* pitchV, unsigned rn, unsigned cn )
  1100. {
  1101. // loc[begScanLocIdx:begScanLocIdx+cn-1] must be valid
  1102. if( bsi + cn > p->locN )
  1103. return false;
  1104. unsigned i,j;
  1105. for(j=1; j<cn; ++j)
  1106. for(i=1; i<rn; ++i)
  1107. {
  1108. cmScMatchLoc_t* loc = p->loc + bsi + j - 1;
  1109. unsigned pitch = pitchV[i-1];
  1110. cmScMatchVal_t* vp = _cmScMatchValPtr(p,i,j,rn,cn);
  1111. vp->flags = _cmScMatchIsMatch(loc,pitch) ? kSmMatchFl : 0;
  1112. unsigned cost = cmIsFlag(vp->flags,kSmMatchFl) ? 0 : 1;
  1113. vp->v[kSmSubIdx] = _cmScMatchMin(p,i-1,j-1, rn, cn) + cost;
  1114. vp->v[kSmDelIdx] = _cmScMatchMin(p,i-1,j , rn, cn) + 1;
  1115. vp->v[kSmInsIdx] = _cmScMatchMin(p,i, j-1, rn, cn) + 1;
  1116. vp->v[kSmMinIdx] = cmMin( vp->v[kSmSubIdx], cmMin(vp->v[kSmDelIdx],vp->v[kSmInsIdx]));
  1117. vp->flags |= _cmScMatchIsTrans(p,pitchV,vp,bsi,i-1,j-1,rn,cn) ? kSmTransFl : 0;
  1118. }
  1119. return true;
  1120. }
  1121. void _cmScMatchPrintMtx( cmScMatch* r, unsigned rn, unsigned cn)
  1122. {
  1123. unsigned i,j,k;
  1124. for(i=0; i<rn; ++i)
  1125. {
  1126. for(j=0; j<cn; ++j)
  1127. {
  1128. printf("(");
  1129. const cmScMatchVal_t* vp = _cmScMatchValPtr(r,i,j,rn,cn);
  1130. for(k=0; k<kSmCnt; ++k)
  1131. {
  1132. printf("%i",vp->v[k]);
  1133. if( k<kSmCnt-1)
  1134. printf(", ");
  1135. else
  1136. printf(" ");
  1137. }
  1138. printf("%c%c)",cmIsFlag(vp->flags,kSmMatchFl)?'m':' ',cmIsFlag(vp->flags,kSmTransFl)?'t':' ');
  1139. }
  1140. printf("\n");
  1141. }
  1142. }
  1143. void _cmScMatchPathPush( cmScMatch* r, unsigned code, unsigned ri, unsigned ci, unsigned flags )
  1144. {
  1145. assert(r->p_avl != NULL );
  1146. cmScMatchPath_t* p = r->p_avl;
  1147. r->p_avl = r->p_avl->next;
  1148. p->code = code;
  1149. p->ri = ri;
  1150. p->ci = ci;
  1151. p->flags = code==kSmSubIdx && cmIsFlag(flags,kSmMatchFl) ? kSmMatchFl : 0;
  1152. p->flags |= cmIsFlag(flags,kSmTransFl) ? kSmTransFl : 0;
  1153. p->next = r->p_cur;
  1154. r->p_cur = p;
  1155. }
  1156. void _cmScMatchPathPop( cmScMatch* r )
  1157. {
  1158. assert( r->p_cur != NULL );
  1159. cmScMatchPath_t* tp = r->p_cur->next;
  1160. r->p_cur->next = r->p_avl;
  1161. r->p_avl = r->p_cur;
  1162. r->p_cur = tp;
  1163. }
  1164. double _cmScMatchCalcCandidateCost( cmScMatch* r )
  1165. {
  1166. cmScMatchPath_t* cp = r->p_cur;
  1167. cmScMatchPath_t* bp = r->p_cur;
  1168. cmScMatchPath_t* ep = NULL;
  1169. // skip leading inserts
  1170. for(; cp!=NULL; cp=cp->next)
  1171. if( cp->code != kSmInsIdx )
  1172. {
  1173. bp = cp;
  1174. break;
  1175. }
  1176. // skip to trailing inserts
  1177. for(; cp!=NULL; cp=cp->next)
  1178. if( cp->code!=kSmInsIdx )
  1179. ep = cp;
  1180. // count remaining path length
  1181. assert( ep!=NULL && bp!=ep);
  1182. unsigned n=1;
  1183. for(cp=bp; cp!=ep; cp=cp->next)
  1184. ++n;
  1185. double gapCnt = 0;
  1186. double penalty = 0;
  1187. bool pfl = cmIsFlag(bp->flags,kSmMatchFl);
  1188. unsigned i;
  1189. cp = bp;
  1190. for(i=0; i<n; ++i,cp=cp->next)
  1191. {
  1192. // a gap is a transition from a matching subst. to an insert or deletion
  1193. //if( pc != cp->code && cp->code != kSmSubIdx && pc==kSmSubIdx && pfl==true )
  1194. if( pfl==true && cmIsFlag(cp->flags,kSmMatchFl)==false )
  1195. ++gapCnt;
  1196. //
  1197. switch( cp->code )
  1198. {
  1199. case kSmSubIdx:
  1200. penalty += cmIsFlag(cp->flags,kSmMatchFl) ? 0 : 1;
  1201. penalty -= cmIsFlag(cp->flags,kSmTransFl) ? 1 : 0;
  1202. break;
  1203. case kSmDelIdx:
  1204. penalty += 1;
  1205. break;
  1206. case kSmInsIdx:
  1207. penalty += 1;
  1208. break;
  1209. }
  1210. pfl = cmIsFlag(cp->flags,kSmMatchFl);
  1211. }
  1212. double cost = gapCnt/n + penalty;
  1213. //printf("n:%i gaps:%f gap_score:%f penalty:%f score:%f\n",n,gapCnt,gapCnt/n,penalty,score);
  1214. return cost;
  1215. }
  1216. double _cmScMatchEvalCandidate( cmScMatch* r, double min_cost, double cost )
  1217. {
  1218. if( min_cost == DBL_MAX || cost < min_cost)
  1219. {
  1220. // copy the p_cur to p_opt[]
  1221. cmScMatchPath_t* cp = r->p_cur;
  1222. unsigned i;
  1223. for(i=0; cp!=NULL && i<r->pn; cp=cp->next,++i)
  1224. {
  1225. r->p_opt[i].code = cp->code;
  1226. r->p_opt[i].ri = cp->ri;
  1227. r->p_opt[i].ci = cp->ci;
  1228. r->p_opt[i].flags = cp->flags;
  1229. r->p_opt[i].next = cp->next==NULL ? NULL : r->p_opt + i + 1;
  1230. }
  1231. assert( i < r->pn );
  1232. r->p_opt[i].code = 0; // terminate with code=0
  1233. min_cost = cost;
  1234. }
  1235. return min_cost;
  1236. }
  1237. // NOTE: IF THE COST CALCULATION WAS BUILT INTO THE RECURSION THEN
  1238. // THIS FUNCTION COULD BE MADE MORE EFFICIENT BECAUSE PATHS WHICH
  1239. // EXCEEDED THE min_cost COULD BE SHORT CIRCUITED.
  1240. //
  1241. // traverse the solution matrix from the lower-right to
  1242. // the upper-left.
  1243. double _cmScMatchGenPaths( cmScMatch* r, int i, int j, unsigned rn, unsigned cn, double min_cost )
  1244. {
  1245. unsigned m;
  1246. // stop when the upper-right is encountered
  1247. if( i==0 && j==0 )
  1248. return _cmScMatchEvalCandidate(r, min_cost, _cmScMatchCalcCandidateCost(r) );
  1249. cmScMatchVal_t* vp = _cmScMatchValPtr(r,i,j,rn,cn);
  1250. // for each possible dir: up,left,up-left
  1251. for(m=1; m<kSmCnt; ++m)
  1252. if( vp->v[m] == vp->v[kSmMinIdx] )
  1253. {
  1254. // prepend to the current candidate path: r->p_cur
  1255. _cmScMatchPathPush(r,m,i,j,vp->flags);
  1256. int ii = i-1;
  1257. int jj = j-1;
  1258. switch(m)
  1259. {
  1260. case kSmSubIdx:
  1261. break;
  1262. case kSmDelIdx:
  1263. jj = j;
  1264. break;
  1265. case kSmInsIdx:
  1266. ii = i;
  1267. break;
  1268. default:
  1269. { assert(0); }
  1270. }
  1271. // recurse!
  1272. min_cost = _cmScMatchGenPaths(r,ii,jj,rn,cn,min_cost);
  1273. // remove the first element from the current path
  1274. _cmScMatchPathPop(r);
  1275. }
  1276. return min_cost;
  1277. }
  1278. double _cmScMatchAlign( cmScMatch* p, unsigned rn, unsigned cn, double min_cost )
  1279. {
  1280. int i = rn-1;
  1281. int j = cn-1;
  1282. unsigned m = _cmScMatchMin(p,i,j,rn,cn);
  1283. if( m==cmMax(rn,cn) )
  1284. printf("Edit distance is at max: %i. No Match.\n",m);
  1285. else
  1286. min_cost = _cmScMatchGenPaths(p,i,j,rn,cn,min_cost);
  1287. return min_cost;
  1288. }
  1289. cmRC_t cmScMatchExec( cmScMatch* p, unsigned scLocIdx, unsigned locN, const unsigned* midiPitchV, unsigned midiPitchN, double min_cost )
  1290. {
  1291. cmRC_t rc;
  1292. unsigned rn = midiPitchN + 1;
  1293. unsigned cn = locN + 1;
  1294. // set the DP matrix default values
  1295. if((rc = _cmScMatchInitMtx(p, rn, cn )) != cmOkRC )
  1296. return rc;
  1297. // _cmScMatchCalcMtx() returns false if the score window exceeds the length of the score
  1298. if(!_cmScMatchCalcMtx(p,scLocIdx,midiPitchV, rn, cn) )
  1299. return cmEofRC;
  1300. //_cmScMatchPrintMtx(p,rn,cn);
  1301. // locate the path through the DP matrix with the lowest edit distance (cost)
  1302. p->opt_cost = _cmScMatchAlign(p, rn, cn, min_cost);
  1303. return rc;
  1304. }
  1305. void _cmScMatchPrintPath( cmScMatch* p, cmScMatchPath_t* cp, unsigned bsi, const unsigned* pitchV, const unsigned* mniV )
  1306. {
  1307. assert( bsi != cmInvalidIdx );
  1308. cmScMatchPath_t* pp = cp;
  1309. int polyN = 0;
  1310. int i;
  1311. printf("loc: ");
  1312. // get the polyphony count for the score window
  1313. for(i=0; pp!=NULL; pp=pp->next)
  1314. {
  1315. cmScMatchLoc_t* lp = p->loc + bsi + pp->ci;
  1316. if( pp->code!=kSmDelIdx )
  1317. {
  1318. if(lp->evtCnt > polyN)
  1319. polyN = lp->evtCnt;
  1320. printf("%4i ",bsi+i);
  1321. ++i;
  1322. }
  1323. else
  1324. printf("%4s "," ");
  1325. }
  1326. printf("\n");
  1327. // print the score notes
  1328. for(i=polyN; i>0; --i)
  1329. {
  1330. printf("%3i: ",i);
  1331. for(pp=cp; pp!=NULL; pp=pp->next)
  1332. {
  1333. int locIdx = bsi + pp->ci - 1;
  1334. assert(0 <= locIdx && locIdx <= p->locN);
  1335. cmScMatchLoc_t* lp = p->loc + locIdx;
  1336. if( pp->code!=kSmDelIdx && lp->evtCnt >= i )
  1337. printf("%4s ",cmMidiToSciPitch(lp->evtV[i-1].pitch,NULL,0));
  1338. else
  1339. printf("%4s ", pp->code==kSmDelIdx? "-" : " ");
  1340. }
  1341. printf("\n");
  1342. }
  1343. printf("mid: ");
  1344. // print the MIDI buffer
  1345. for(pp=cp; pp!=NULL; pp=pp->next)
  1346. {
  1347. if( pp->code!=kSmInsIdx )
  1348. printf("%4s ",cmMidiToSciPitch(pitchV[pp->ri-1],NULL,0));
  1349. else
  1350. printf("%4s ",pp->code==kSmInsIdx?"-":" ");
  1351. }
  1352. printf("\nmni: ");
  1353. // print the MIDI buffer index (mni)
  1354. for(pp=cp; pp!=NULL; pp=pp->next)
  1355. {
  1356. if( pp->code!=kSmInsIdx )
  1357. printf("%4i ",mniV[pp->ri-1]);
  1358. else
  1359. printf("%4s ",pp->code==kSmInsIdx?"-":" ");
  1360. }
  1361. printf("\n op: ");
  1362. // print the substitute/insert/delete operation
  1363. for(pp=cp; pp!=NULL; pp=pp->next)
  1364. {
  1365. char c = ' ';
  1366. switch( pp->code )
  1367. {
  1368. case kSmSubIdx: c = 's'; break;
  1369. case kSmDelIdx: c = 'd'; break;
  1370. case kSmInsIdx: c = 'i'; break;
  1371. default:
  1372. { assert(0); }
  1373. }
  1374. printf("%4c ",c);
  1375. }
  1376. printf("\n ");
  1377. // give substitute attribute (match or transpose)
  1378. for(pp=cp; pp!=NULL; pp=pp->next)
  1379. {
  1380. cmChar_t s[3];
  1381. int k = 0;
  1382. if( cmIsFlag(pp->flags,kSmMatchFl) )
  1383. s[k++] = 'm';
  1384. if( cmIsFlag(pp->flags,kSmTransFl) )
  1385. s[k++] = 't';
  1386. s[k] = 0;
  1387. printf("%4s ",s);
  1388. }
  1389. printf("\nscl: ");
  1390. // print the stored location index
  1391. for(pp=cp; pp!=NULL; pp=pp->next)
  1392. {
  1393. if( pp->locIdx == cmInvalidIdx )
  1394. printf("%4s "," ");
  1395. else
  1396. printf("%4i ",p->loc[pp->locIdx].scLocIdx);
  1397. }
  1398. printf("\n\n");
  1399. }
  1400. //=======================================================================================================================
  1401. cmScMatcher* cmScMatcherAlloc( cmCtx* c, cmScMatcher* p, double srate, cmScH_t scH, unsigned scWndN, unsigned midiWndN )
  1402. {
  1403. cmScMatcher* op = cmObjAlloc(cmScMatcher,c,p);
  1404. if( op != NULL )
  1405. op->mp = cmScMatchAlloc(c,NULL,cmScNullHandle,0,0);
  1406. if( srate != 0 )
  1407. {
  1408. if( cmScMatcherInit(op,srate,scH,scWndN,midiWndN) != cmOkRC )
  1409. cmScMatcherFree(&op);
  1410. }
  1411. return op;
  1412. }
  1413. cmRC_t cmScMatcherFree( cmScMatcher** pp )
  1414. {
  1415. cmRC_t rc = cmOkRC;
  1416. if( pp==NULL || *pp==NULL )
  1417. return rc;
  1418. cmScMatcher* p = *pp;
  1419. if((rc = cmScMatcherFinal(p)) != cmOkRC )
  1420. return rc;
  1421. cmScMatchFree(&p->mp);
  1422. cmMemFree(p->midiBuf);
  1423. cmMemFree(p->res);
  1424. cmObjFree(pp);
  1425. return rc;
  1426. }
  1427. cmRC_t cmScMatcherInit( cmScMatcher* p, double srate, cmScH_t scH, unsigned scWndN, unsigned midiWndN )
  1428. {
  1429. cmRC_t rc;
  1430. if((rc = cmScMatcherFinal(p)) != cmOkRC )
  1431. return rc;
  1432. if( midiWndN > scWndN )
  1433. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The score alignment MIDI event buffer length (%i) must be less than the score window length (%i).",midiWndN,scWndN);
  1434. if(( rc = cmScMatchInit(p->mp,scH,scWndN,midiWndN)) != cmOkRC )
  1435. return rc;
  1436. p->mn = midiWndN;
  1437. p->midiBuf = cmMemResize(cmScMatcherMidi_t,p->midiBuf,p->mn);
  1438. p->stepCnt = 3;
  1439. p->maxMissCnt = p->stepCnt+1;
  1440. p->rn = 2 * cmScoreEvtCount(scH);
  1441. p->res = cmMemResizeZ(cmScMatcherResult_t,p->res,p->rn);
  1442. cmScMatcherReset(p);
  1443. return rc;
  1444. }
  1445. cmRC_t cmScMatcherFinal( cmScMatcher* p )
  1446. {
  1447. return cmScMatchFinal(p->mp);
  1448. }
  1449. void cmScMatcherReset( cmScMatcher* p )
  1450. {
  1451. p->mbi = p->mp->mmn;
  1452. p->mni = 0;
  1453. p->begSyncLocIdx = cmInvalidIdx;
  1454. p->s_opt = DBL_MAX;
  1455. p->missCnt = 0;
  1456. p->scanCnt = 0;
  1457. p->ri = 0;
  1458. }
  1459. bool cmScMatcherInputMidi( cmScMatcher* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
  1460. {
  1461. if( status != kNoteOnMdId )
  1462. return false;
  1463. unsigned mi = p->mn-1;
  1464. //printf("%3i %5.2f %4s\n",p->mni,(double)smpIdx/p->srate,cmMidiToSciPitch(d0,NULL,0));
  1465. // shift the new MIDI event onto the end of the MIDI buffer
  1466. memmove(p->midiBuf, p->midiBuf+1, sizeof(cmScMatcherMidi_t)*mi);
  1467. p->midiBuf[mi].locIdx = cmInvalidIdx;
  1468. p->midiBuf[mi].cbCnt = 0;
  1469. p->midiBuf[mi].mni = p->mni++;
  1470. p->midiBuf[mi].smpIdx = smpIdx;
  1471. p->midiBuf[mi].pitch = d0;
  1472. p->midiBuf[mi].vel = d1;
  1473. if( p->mbi > 0 )
  1474. --p->mbi;
  1475. return true;
  1476. }
  1477. void _cmScMatcherStoreResult( cmScMatcher* p, unsigned locIdx, unsigned flags, const cmScMatcherMidi_t* mp )
  1478. {
  1479. // don't store missed score note results
  1480. assert( mp != NULL );
  1481. bool matchFl = cmIsFlag(flags,kSmMatchFl);
  1482. bool tpFl = locIdx!=cmInvalidIdx && matchFl;
  1483. bool fpFl = locIdx==cmInvalidIdx || matchFl==false;
  1484. cmScMatcherResult_t * rp = NULL;
  1485. unsigned i;
  1486. assert( tpFl==false || (tpFl==true && locIdx != cmInvalidIdx ) );
  1487. // it is possible that the same MIDI event is reported more than once
  1488. // (due to step->scan back tracking) - try to find previous result records
  1489. // associated with this MIDI event
  1490. for(i=0; i<p->ri; ++i)
  1491. if( p->res[i].mni == mp->mni )
  1492. {
  1493. // if this is not the first time this note was reported and it is a true positive
  1494. if( tpFl )
  1495. {
  1496. rp = p->res + i;
  1497. break;
  1498. }
  1499. // a match was found but this was not a true-pos so ignore it
  1500. return;
  1501. }
  1502. if( rp == NULL )
  1503. {
  1504. rp = p->res + p->ri;
  1505. ++p->ri;
  1506. }
  1507. rp->locIdx = locIdx;
  1508. rp->mni = mp->mni;
  1509. rp->pitch = mp->pitch;
  1510. rp->vel = mp->vel;
  1511. rp->flags = flags | (tpFl ? kSmTruePosFl : 0) | (fpFl ? kSmFalsePosFl : 0);
  1512. //rp->tpFl = tpFl;
  1513. //rp->fpFl = fpFl;
  1514. }
  1515. void cmScMatcherPrintPath( cmScMatcher* p )
  1516. {
  1517. unsigned pitchV[ p->mn ];
  1518. unsigned mniV[ p->mn ];
  1519. unsigned i;
  1520. for(i=0; i<p->mn; ++i)
  1521. {
  1522. pitchV[i] = p->midiBuf[i].pitch;
  1523. mniV[i] = p->midiBuf[i].mni;
  1524. }
  1525. _cmScMatchPrintPath(p->mp, p->mp->p_opt, p->begSyncLocIdx, pitchV, mniV );
  1526. }
  1527. unsigned cmScMatcherScan( cmScMatcher* p, unsigned bsi, unsigned scanCnt )
  1528. {
  1529. assert( p->mp != NULL && p->mp->mmn > 0 );
  1530. unsigned i_opt = cmInvalidIdx;
  1531. double s_opt = DBL_MAX;
  1532. cmRC_t rc = cmOkRC;
  1533. unsigned i;
  1534. // initialize the internal values set by this function
  1535. p->missCnt = 0;
  1536. p->esi = cmInvalidIdx;
  1537. p->s_opt = DBL_MAX;
  1538. // if the MIDI buf is not full
  1539. if( p->mbi != 0 )
  1540. return cmInvalidIdx;
  1541. // load a temporary MIDI pitch buffer for use by cmScMatch.
  1542. unsigned pitchV[p->mp->mmn];
  1543. for(i=0; i<p->mp->mmn; ++i)
  1544. pitchV[i] = p->midiBuf[i].pitch;
  1545. // calc the edit distance from pitchV[] to a sliding score window
  1546. for(i=0; rc==cmOkRC && (scanCnt==cmInvalidCnt || i<scanCnt); ++i)
  1547. {
  1548. rc = cmScMatchExec(p->mp, bsi + i, p->mp->msn, pitchV, p->mp->mmn, s_opt );
  1549. switch(rc)
  1550. {
  1551. case cmOkRC: // normal result
  1552. if( p->mp->opt_cost < s_opt )
  1553. {
  1554. s_opt = p->mp->opt_cost;
  1555. i_opt = bsi + i;
  1556. }
  1557. break;
  1558. case cmEofRC: // score window encountered the end of the score
  1559. break;
  1560. default: // error state
  1561. return cmInvalidIdx;
  1562. }
  1563. }
  1564. // store the cost assoc'd with i_opt
  1565. p->s_opt = s_opt;
  1566. if( i_opt == cmInvalidIdx )
  1567. return cmInvalidIdx;
  1568. // Traverse the least cost path and:
  1569. // 1) Set p->esi to the score location index of the last MIDI note
  1570. // which has a positive match with the score and assign
  1571. // the internal score index to cp->locIdx.
  1572. //
  1573. // 2) Set cmScAlignPath_t.locIdx - index into p->loc[] associated
  1574. // with each path element that is a 'substitute' or an 'insert'.
  1575. //
  1576. // 3) Set p->missCnt: the count of trailing non-positive matches.
  1577. // p->missCnt is eventually used in cmScAlignStep() to track the number
  1578. // of consecutive trailing missed notes.
  1579. //
  1580. cmScMatchPath_t* cp = p->mp->p_opt;
  1581. for(i=0; cp!=NULL; cp=cp->next)
  1582. {
  1583. if( cp->code != kSmInsIdx )
  1584. {
  1585. assert( cp->ri > 0 );
  1586. p->midiBuf[ cp->ri-1 ].locIdx = cmInvalidIdx;
  1587. }
  1588. switch( cp->code )
  1589. {
  1590. case kSmSubIdx:
  1591. if( cmIsFlag(cp->flags,kSmMatchFl) || cmIsFlag(cp->flags,kSmTransFl))
  1592. {
  1593. p->esi = i_opt + i;
  1594. p->missCnt = 0;
  1595. if( cmIsFlag(cp->flags,kSmMatchFl) )
  1596. p->midiBuf[ cp->ri-1 ].locIdx = i_opt + i;
  1597. }
  1598. else
  1599. {
  1600. ++p->missCnt;
  1601. }
  1602. // fall through
  1603. case kSmInsIdx:
  1604. cp->locIdx = i_opt + i;
  1605. ++i;
  1606. break;
  1607. case kSmDelIdx:
  1608. cp->locIdx = cmInvalidIdx;
  1609. ++p->missCnt;
  1610. break;
  1611. }
  1612. }
  1613. // if no positive matches were found
  1614. if( p->esi == cmInvalidIdx )
  1615. i_opt = cmInvalidIdx;
  1616. else
  1617. {
  1618. // record result
  1619. for(cp=p->mp->p_opt; cp!=NULL; cp=cp->next)
  1620. if( cp->code != kSmInsIdx )
  1621. _cmScMatcherStoreResult(p, cp->locIdx, cp->flags, p->midiBuf + cp->ri - 1);
  1622. }
  1623. return i_opt;
  1624. }
  1625. cmRC_t cmScMatcherStep( cmScMatcher* p )
  1626. {
  1627. int i;
  1628. unsigned pitch = p->midiBuf[ p->mn-1 ].pitch;
  1629. unsigned locIdx = cmInvalidIdx;
  1630. // the tracker must be sync'd to step
  1631. if( p->esi == cmInvalidIdx )
  1632. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The p->esi value must be valid to perform a step operation.");
  1633. // if the end of the score has been reached
  1634. if( p->esi + 1 >= p->mp->locN )
  1635. return cmEofRC;
  1636. // attempt to match to next location first
  1637. if( _cmScMatchIsMatch(p->mp->loc + p->esi + 1, pitch) )
  1638. {
  1639. locIdx = p->esi + 1;
  1640. }
  1641. else
  1642. {
  1643. //
  1644. for(i=2; i<p->stepCnt; ++i)
  1645. {
  1646. // go forward
  1647. if( p->esi+i < p->mp->locN && _cmScMatchIsMatch(p->mp->loc + p->esi + i, pitch) )
  1648. {
  1649. locIdx = p->esi + i;
  1650. break;
  1651. }
  1652. // go backward
  1653. if( p->esi >= (i-1) && _cmScMatchIsMatch(p->mp->loc + p->esi - (i-1), pitch) )
  1654. {
  1655. locIdx = p->esi - (i-1);
  1656. break;
  1657. }
  1658. }
  1659. }
  1660. p->midiBuf[ p->mn-1 ].locIdx = locIdx;
  1661. if( locIdx == cmInvalidIdx )
  1662. ++p->missCnt;
  1663. else
  1664. {
  1665. p->missCnt = 0;
  1666. p->esi = locIdx;
  1667. }
  1668. // store the result
  1669. _cmScMatcherStoreResult(p, locIdx, locIdx!=cmInvalidIdx ? kSmMatchFl : 0, p->midiBuf + p->mn - 1);
  1670. if( p->missCnt >= p->maxMissCnt )
  1671. {
  1672. unsigned begScanLocIdx = p->esi > p->mn ? p->esi - p->mn : 0;
  1673. p->s_opt = DBL_MAX;
  1674. unsigned bsi = cmScMatcherScan(p,begScanLocIdx,p->mn*2);
  1675. ++p->scanCnt;
  1676. // if the scan failed find a match
  1677. if( bsi == cmInvalidIdx )
  1678. return cmCtxRtCondition( &p->obj, cmSubSysFailRC, "Scan resync. failed.");
  1679. }
  1680. return cmOkRC;
  1681. }
  1682. cmRC_t cmScMatcherExec( cmScMatcher* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
  1683. {
  1684. bool fl = p->mbi > 0;
  1685. cmRC_t rc = cmOkRC;
  1686. // update the MIDI buffer with the incoming note
  1687. cmScMatcherInputMidi(p,smpIdx,status,d0,d1);
  1688. // if the MIDI buffer transitioned to full then perform an initial scan sync.
  1689. if( fl && p->mbi == 0 )
  1690. {
  1691. if( (p->begSyncLocIdx = cmScMatcherScan(p,0,cmInvalidCnt)) == cmInvalidIdx )
  1692. rc = cmInvalidArgRC; // signal init. scan sync. fail
  1693. else
  1694. {
  1695. // cmScMatcherPrintPath(p);
  1696. }
  1697. }
  1698. else
  1699. {
  1700. // if the MIDI buffer is full then perform a step sync.
  1701. if( !fl && p->mbi == 0 )
  1702. rc = cmScMatcherStep(p);
  1703. }
  1704. return rc;
  1705. }
  1706. double cmScMatcherFMeas( cmScMatcher* p )
  1707. {
  1708. unsigned bli = p->mp->locN;
  1709. unsigned eli = 0;
  1710. unsigned scNoteCnt = 0; // total count of score notes
  1711. unsigned matchCnt = 0; // count of matched notes (true positives)
  1712. unsigned wrongCnt = 0; // count of incorrect notes (false positives)
  1713. unsigned missCnt = 0; // count of missed score notes (false negatives)
  1714. unsigned i;
  1715. for(i=0; i<p->ri; ++i)
  1716. if( p->res[i].locIdx != cmInvalidIdx )
  1717. {
  1718. bli = cmMin(bli,p->res[i].locIdx);
  1719. eli = cmMax(eli,p->res[i].locIdx);
  1720. if( cmIsFlag(p->res[i].flags,kSmTruePosFl) )
  1721. ++matchCnt;
  1722. if( cmIsFlag(p->res[i].flags,kSmFalsePosFl) )
  1723. ++wrongCnt;
  1724. }
  1725. scNoteCnt = eli - bli + 1;
  1726. missCnt = scNoteCnt - matchCnt;
  1727. double prec = (double)2.0 * matchCnt / (matchCnt + wrongCnt);
  1728. double rcal = (double)2.0 * matchCnt / (matchCnt + missCnt);
  1729. double fmeas = prec * rcal / (prec + rcal);
  1730. //printf("total:%i match:%i wrong:%i miss:%i\n",scNoteCnt,matchCnt,wrongCnt,missCnt);
  1731. return fmeas;
  1732. }
  1733. typedef struct cmScMatcherPrint_str
  1734. {
  1735. unsigned flags;
  1736. unsigned scLocIdx;
  1737. unsigned mni;
  1738. unsigned pitch;
  1739. unsigned vel;
  1740. } cmScMatcherPrint_t;
  1741. void _cmScMatcherInsertPrint(cmScMatcherPrint_t* a, unsigned i, unsigned* anp, unsigned aan, const cmScMatcherResult_t* rp, unsigned scLocIdx )
  1742. {
  1743. assert( *anp + 1 <= aan );
  1744. memmove(a + i + 1, a + i, (*anp-i)*sizeof(cmScMatcherPrint_t));
  1745. memset( a + i, 0, sizeof(cmScMatcherPrint_t));
  1746. *anp += 1;
  1747. a[i].flags = rp->flags;
  1748. a[i].scLocIdx = scLocIdx;
  1749. a[i].mni = rp->mni;
  1750. a[i].pitch = rp->pitch;
  1751. a[i].vel = rp->vel;
  1752. }
  1753. void cmScMatcherPrint( cmScMatcher* p )
  1754. {
  1755. unsigned bsli = cmScoreEvtCount(p->mp->scH);
  1756. unsigned esli = 0;
  1757. unsigned i,j,k;
  1758. // get first/last scLocIdx from res[]
  1759. for(i=0; i<p->ri; ++i)
  1760. if( p->res[i].locIdx != cmInvalidIdx )
  1761. {
  1762. bsli = cmMin(bsli,p->mp->loc[p->res[i].locIdx].scLocIdx);
  1763. esli = cmMax(esli,p->mp->loc[p->res[i].locIdx].scLocIdx);
  1764. }
  1765. unsigned an = 0;
  1766. unsigned aan = p->ri;
  1767. // calc the count of score events between bsli and esli.
  1768. for(i=bsli; i<=esli; ++i)
  1769. {
  1770. cmScoreLoc_t* lp = cmScoreLoc(p->mp->scH, i);
  1771. assert(lp != NULL);
  1772. aan += lp->evtCnt;
  1773. }
  1774. // allocate an array off 'aan' print records
  1775. cmScMatcherPrint_t* a = cmMemAllocZ(cmScMatcherPrint_t,aan);
  1776. // fill a[] with score note and bar events
  1777. for(i=bsli; i<=esli; ++i)
  1778. {
  1779. unsigned scLocIdx = i;
  1780. cmScoreLoc_t* lp = cmScoreLoc(p->mp->scH, scLocIdx );
  1781. for(j=0; j<lp->evtCnt; ++j)
  1782. {
  1783. assert( an < aan );
  1784. cmScoreEvt_t* ep = lp->evtArray[j];
  1785. cmScMatcherPrint_t* pp = a + an;
  1786. an += 1;
  1787. switch( ep->type )
  1788. {
  1789. case kBarEvtScId:
  1790. pp->flags = kSmBarFl;
  1791. break;
  1792. case kNonEvtScId:
  1793. pp->flags = kSmNoteFl;
  1794. break;
  1795. }
  1796. pp->scLocIdx = scLocIdx;
  1797. pp->mni = cmInvalidIdx;
  1798. pp->pitch = ep->pitch;
  1799. pp->vel = kInvalidMidiVelocity;
  1800. }
  1801. }
  1802. // for each result record
  1803. for(i=0; i<p->ri; ++i)
  1804. {
  1805. cmScMatcherResult_t* rp = p->res + i;
  1806. // if this result recd matched a score event
  1807. if( cmIsFlag(rp->flags,kSmTruePosFl) )
  1808. {
  1809. // locate the matching score event
  1810. for(k=0; k<an; ++k)
  1811. if( a[k].scLocIdx==p->mp->loc[rp->locIdx].scLocIdx && a[k].pitch==rp->pitch )
  1812. {
  1813. a[k].mni = rp->mni;
  1814. a[k].vel = rp->vel;
  1815. a[k].flags |= kSmMatchFl;
  1816. break;
  1817. }
  1818. }
  1819. // if this result did not match a score event
  1820. if( cmIsFlag(rp->flags,kSmFalsePosFl) )
  1821. {
  1822. unsigned d_min;
  1823. cmScMatcherPrint_t* dp = NULL;
  1824. unsigned scLocIdx = cmInvalidIdx;
  1825. // if this result does not have a valid locIdx
  1826. // (e.g. errant MIDI notes: scan:'delete' note or a step:mis-match note)
  1827. if( rp->locIdx == cmInvalidIdx )
  1828. {
  1829. // find the print recd with the closet 'mni'
  1830. for(k=0; k<an; ++k)
  1831. if( a[k].mni != cmInvalidIdx )
  1832. {
  1833. unsigned d;
  1834. if( a[k].mni > rp->mni )
  1835. d = a[k].mni - rp->mni;
  1836. else
  1837. d = rp->mni - a[k].mni;
  1838. if( dp==NULL || d < d_min )
  1839. {
  1840. dp = a + k;
  1841. d_min = d;
  1842. }
  1843. }
  1844. k = dp - a;
  1845. assert( k < an );
  1846. scLocIdx = p->mp->loc[k].scLocIdx;
  1847. if( a[k].mni < rp->mni )
  1848. ++k;
  1849. }
  1850. else // result w/ a valid locIdx (e.g. scan 'substitute' with no match)
  1851. {
  1852. scLocIdx = p->mp->loc[rp->locIdx].scLocIdx;
  1853. // find the print recd with the closest scIdx
  1854. for(k=0; k<an; ++k)
  1855. if( a[k].scLocIdx != cmInvalidIdx )
  1856. {
  1857. unsigned d;
  1858. if( a[k].scLocIdx > scLocIdx )
  1859. d = a[k].scLocIdx - scLocIdx;
  1860. else
  1861. d = scLocIdx - a[k].scLocIdx;
  1862. if( dp==NULL || d < d_min )
  1863. {
  1864. dp = a + k;
  1865. d_min = d;
  1866. }
  1867. }
  1868. k = dp - a;
  1869. assert( k < an );
  1870. if( a[k].scLocIdx < scLocIdx )
  1871. ++k;
  1872. }
  1873. // create a new print recd to represent the false-positive result recd
  1874. assert( dp != NULL );
  1875. _cmScMatcherInsertPrint(a, k, &an,aan,rp,scLocIdx);
  1876. }
  1877. }
  1878. for(i=0; i<an; ++i)
  1879. {
  1880. printf("%4i %4i %4s %c%c%c\n",a[i].scLocIdx,a[i].mni,
  1881. cmIsFlag(a[i].flags,kSmBarFl) ? "|" : cmMidiToSciPitch(a[i].pitch,NULL,0),
  1882. cmIsFlag(a[i].flags,kSmNoteFl) ? 'n' : ' ',
  1883. cmIsFlag(a[i].flags,kSmMatchFl) ? 'm' : (cmIsFlag(a[i].flags,kSmTransFl) ? 't' : ' '),
  1884. cmIsFlag(a[i].flags,kSmFalsePosFl) ? '*' : ' '
  1885. );
  1886. }
  1887. }
  1888. //=======================================================================================================================
  1889. cmScAlign* cmScAlignAlloc( cmCtx* c, cmScAlign* p, cmScAlignCb_t cbFunc, void* cbArg, cmReal_t srate, cmScH_t scH, unsigned midiN, unsigned scWndN )
  1890. {
  1891. cmScAlign* op = cmObjAlloc(cmScAlign,c,p);
  1892. if( srate != 0 )
  1893. if( cmScAlignInit(op,cbFunc,cbArg,srate,scH,midiN,scWndN) != cmOkRC )
  1894. cmScAlignFree(&op);
  1895. return op;
  1896. }
  1897. cmRC_t cmScAlignFree( cmScAlign** pp )
  1898. {
  1899. cmRC_t rc = cmOkRC;
  1900. if( pp==NULL || *pp==NULL )
  1901. return rc;
  1902. cmScAlign* p = *pp;
  1903. if((rc = cmScAlignFinal(p)) != cmOkRC )
  1904. return rc;
  1905. cmMemFree(p->loc);
  1906. cmMemFree(p->midiBuf);
  1907. cmMemFree(p->m);
  1908. cmMemFree(p->p_mem);
  1909. cmMemFree(p->res);
  1910. cmObjFree(pp);
  1911. return rc;
  1912. }
  1913. void _cmScAlignPrint( cmScAlign* p )
  1914. {
  1915. int i,j;
  1916. for(i=0; i<p->locN; ++i)
  1917. {
  1918. printf("%2i %5i ",p->loc[i].barNumb,p->loc[i].scLocIdx);
  1919. for(j=0; j<p->loc[i].evtCnt; ++j)
  1920. printf("%s ",cmMidiToSciPitch(p->loc[i].evtV[j].pitch,NULL,0));
  1921. printf("\n");
  1922. }
  1923. }
  1924. cmRC_t cmScAlignInit( cmScAlign* p, cmScAlignCb_t cbFunc, void* cbArg, cmReal_t srate, cmScH_t scH, unsigned midiN, unsigned scWndN )
  1925. {
  1926. cmRC_t rc;
  1927. if((rc = cmScAlignFinal(p)) != cmOkRC )
  1928. return rc;
  1929. if( midiN > scWndN )
  1930. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The score alignment MIDI event buffer length (%i) must be less than the score window length (%i).",midiN,scWndN);
  1931. p->cbFunc = cbFunc;
  1932. p->cbArg = cbArg;
  1933. p->srate = srate;
  1934. p->scH = scH;
  1935. p->locN = cmScoreEvtCount(scH);
  1936. p->loc = cmMemResizeZ(cmScAlignLoc_t,p->loc,p->locN);
  1937. p->mn = midiN;
  1938. p->midiBuf = cmMemResizeZ(cmScAlignMidiEvt_t,p->midiBuf,midiN);
  1939. p->mbi = midiN;
  1940. p->printFl = true;
  1941. // Setup score structures
  1942. // for each score location
  1943. unsigned li,ei;
  1944. for(li=0,ei=0; li<cmScoreLocCount(p->scH); ++li)
  1945. {
  1946. unsigned i,n;
  1947. const cmScoreLoc_t* lp = cmScoreLoc(p->scH,li);
  1948. // count the number of note events at location li
  1949. for(n=0,i=0; i<lp->evtCnt; ++i)
  1950. if( lp->evtArray[i]->type == kNonEvtScId )
  1951. ++n;
  1952. assert( ei+n <= p->locN );
  1953. // duplicate each note at location li n times
  1954. for(i=0; i<n; ++i)
  1955. {
  1956. unsigned j,k;
  1957. p->loc[ei+i].evtCnt = n;
  1958. p->loc[ei+i].evtV = cmMemAllocZ(cmScAlignScEvt_t,n);
  1959. p->loc[ei+i].scLocIdx = li;
  1960. p->loc[ei+i].barNumb = lp->barNumb;
  1961. for(j=0,k=0; j<lp->evtCnt; ++j)
  1962. if( lp->evtArray[j]->type == kNonEvtScId )
  1963. {
  1964. p->loc[ei+i].evtV[k].pitch = lp->evtArray[j]->pitch;
  1965. ++k;
  1966. }
  1967. }
  1968. ei += n;
  1969. }
  1970. assert(ei<=p->locN);
  1971. p->locN = ei;
  1972. // setup edit distance structures
  1973. p->rn = midiN+1;
  1974. p->cn = scWndN+1;
  1975. p->m = cmMemResizeZ(cmScAlignVal_t, p->m, p->rn*p->cn );
  1976. p->pn = p->rn + p->cn;
  1977. p->p_mem = cmMemResizeZ(cmScAlignPath_t, p->p_mem, 2*p->pn );
  1978. p->p_avl = p->p_mem;
  1979. p->p_cur = NULL;
  1980. p->p_opt = p->p_mem + p->pn;
  1981. p->s_opt = DBL_MAX;
  1982. p->resN = 2 * cmScoreEvtCount(scH); // make twice as many result records as there are score events
  1983. p->res = cmMemResizeZ(cmScAlignResult_t, p->res, p->resN);
  1984. p->stepCnt = 3;
  1985. p->maxStepMissCnt = 4;
  1986. // fill in the default values for the first row
  1987. // and column of the DP matrix
  1988. unsigned i,j,k;
  1989. for(i=0; i<p->rn; ++i)
  1990. for(j=0; j<p->cn; ++j)
  1991. {
  1992. unsigned v[] = {0,0,0,0};
  1993. if( i == 0 )
  1994. {
  1995. v[kSaMinIdx] = j;
  1996. v[kSaInsIdx] = j;
  1997. }
  1998. else
  1999. if( j == 0 )
  2000. {
  2001. v[kSaMinIdx] = i;
  2002. v[kSaDelIdx] = i;
  2003. }
  2004. for(k=0; k<kSaCnt; ++k)
  2005. p->m[ i + (j*p->rn) ].v[k] = v[k];
  2006. }
  2007. // put pn path records on the available list
  2008. for(i=0; i<p->pn; ++i)
  2009. {
  2010. p->p_mem[i].next = i<p->pn-1 ? p->p_mem + i + 1 : NULL;
  2011. p->p_opt[i].next = i<p->pn-1 ? p->p_opt + i + 1 : NULL;
  2012. }
  2013. //_cmScAlignPrint(p);
  2014. cmScAlignReset(p,0);
  2015. return rc;
  2016. }
  2017. cmRC_t cmScAlignFinal( cmScAlign* p )
  2018. {
  2019. unsigned i;
  2020. for(i=0; i<p->locN; ++i)
  2021. cmMemPtrFree(&p->loc[i].evtV);
  2022. return cmOkRC;
  2023. }
  2024. void cmScAlignReset( cmScAlign* p, unsigned begScanLocIdx )
  2025. {
  2026. assert( begScanLocIdx < p->locN );
  2027. p->mbi = p->mn;
  2028. p->mni = 0;
  2029. p->begScanLocIdx = begScanLocIdx;
  2030. p->begSyncLocIdx = cmInvalidIdx;
  2031. p->s_opt = DBL_MAX;
  2032. p->esi = cmInvalidIdx;
  2033. p->missCnt = 0;
  2034. p->scanCnt = 0;
  2035. p->ri = 0;
  2036. }
  2037. cmScAlignVal_t* _cmScAlignValPtr( cmScAlign* p, unsigned i, unsigned j )
  2038. {
  2039. assert( i < p->rn );
  2040. assert( j < p->cn );
  2041. return p->m + i + (j*p->rn);
  2042. }
  2043. bool _cmScAlignIsMatch( const cmScAlignLoc_t* loc, unsigned pitch )
  2044. {
  2045. unsigned i;
  2046. for(i=0; i<loc->evtCnt; ++i)
  2047. if( loc->evtV[i].pitch == pitch )
  2048. return true;
  2049. return false;
  2050. }
  2051. bool _cmScAlignIsTrans( cmScAlign* p, const cmScAlignVal_t* v1p, unsigned i, unsigned j )
  2052. {
  2053. bool fl = false;
  2054. cmScAlignVal_t* v0p = _cmScAlignValPtr(p,i,j);
  2055. if( i>=1 && j>=1
  2056. && v1p->v[kSaMinIdx] == v1p->v[kSaSubIdx]
  2057. && v1p->matchFl == false
  2058. && v0p->v[kSaMinIdx] == v0p->v[kSaSubIdx]
  2059. && v0p->matchFl == false )
  2060. {
  2061. unsigned c00 = p->midiBuf[i-1].pitch;
  2062. unsigned c01 = p->midiBuf[i ].pitch;
  2063. cmScAlignLoc_t* c10 = p->loc + p->begScanLocIdx + j - 1;
  2064. cmScAlignLoc_t* c11 = p->loc + p->begScanLocIdx + j;
  2065. fl = _cmScAlignIsMatch(c11,c00) && _cmScAlignIsMatch(c10,c01);
  2066. }
  2067. return fl;
  2068. }
  2069. unsigned _cmScAlignMin( cmScAlign* p, unsigned i, unsigned j )
  2070. {
  2071. assert( i<p->rn && j<p->cn );
  2072. //return p->m[ i + (j*p->rn) ].v[kSaMinIdx];
  2073. return _cmScAlignValPtr(p,i,j)->v[kSaMinIdx];
  2074. }
  2075. // Returns 'false' if the score window goes past the end of the score
  2076. // (i.e. p->begScanLocIdx + p->cn > p->locN )
  2077. bool _cmScAlignCalcMtx( cmScAlign* p )
  2078. {
  2079. // the midi buffer must be full
  2080. assert( p->mbi == 0 );
  2081. // loc[begScanLocIdx:begScanLocIdx+p->cn-1] must be valid
  2082. if( p->begScanLocIdx + p->cn > p->locN )
  2083. return false;
  2084. unsigned i,j;
  2085. for(j=1; j<p->cn; ++j)
  2086. for(i=1; i<p->rn; ++i)
  2087. {
  2088. cmScAlignLoc_t* loc = p->loc + p->begScanLocIdx + j - 1;
  2089. unsigned pitch = p->midiBuf[i-1].pitch;
  2090. cmScAlignVal_t* vp = _cmScAlignValPtr(p,i,j);
  2091. vp->matchFl = _cmScAlignIsMatch(loc,pitch);
  2092. unsigned cost = vp->matchFl ? 0 : 1;
  2093. vp->v[kSaSubIdx] = _cmScAlignMin(p,i-1,j-1) + cost;
  2094. vp->v[kSaDelIdx] = _cmScAlignMin(p,i-1,j ) + 1;
  2095. vp->v[kSaInsIdx] = _cmScAlignMin(p,i, j-1) + 1;
  2096. vp->v[kSaMinIdx] = cmMin( vp->v[kSaSubIdx], cmMin(vp->v[kSaDelIdx],vp->v[kSaInsIdx]));
  2097. vp->transFl = _cmScAlignIsTrans(p,vp,i-1,j-1);
  2098. }
  2099. return true;
  2100. }
  2101. void _cmScAlignPathPush( cmScAlign* r, unsigned code, unsigned ri, unsigned ci, bool matchFl, bool transFl )
  2102. {
  2103. assert(r->p_avl != NULL );
  2104. cmScAlignPath_t* p = r->p_avl;
  2105. r->p_avl = r->p_avl->next;
  2106. p->code = code;
  2107. p->ri = ri;
  2108. p->ci = ci;
  2109. p->matchFl = code==kSaSubIdx ? matchFl : false;
  2110. p->transFl = transFl;
  2111. p->next = r->p_cur;
  2112. r->p_cur = p;
  2113. }
  2114. void _cmScAlignPathPop( cmScAlign* r )
  2115. {
  2116. assert( r->p_cur != NULL );
  2117. cmScAlignPath_t* tp = r->p_cur->next;
  2118. r->p_cur->next = r->p_avl;
  2119. r->p_avl = r->p_cur;
  2120. r->p_cur = tp;
  2121. }
  2122. double _cmScAlignScoreCandidate( cmScAlign* r )
  2123. {
  2124. cmScAlignPath_t* cp = r->p_cur;
  2125. cmScAlignPath_t* bp = r->p_cur;
  2126. cmScAlignPath_t* ep = NULL;
  2127. for(; cp!=NULL; cp=cp->next)
  2128. if( cp->code != kSaInsIdx )
  2129. {
  2130. bp = cp;
  2131. break;
  2132. }
  2133. for(; cp!=NULL; cp=cp->next)
  2134. if( cp->code!=kSaInsIdx )
  2135. ep = cp;
  2136. assert( ep!=NULL && bp!=ep);
  2137. unsigned n=1;
  2138. for(cp=bp; cp!=ep; cp=cp->next)
  2139. ++n;
  2140. double gapCnt = 0;
  2141. double penalty = 0;
  2142. bool pfl = bp->matchFl;
  2143. unsigned i;
  2144. cp = bp;
  2145. for(i=0; i<n; ++i,cp=cp->next)
  2146. {
  2147. // a gap is a transition from a matching subst. to an insert or deletion
  2148. //if( pc != cp->code && cp->code != kSaSubIdx && pc==kSaSubIdx && pfl==true )
  2149. if( pfl==true && cp->matchFl==false )
  2150. ++gapCnt;
  2151. //
  2152. switch( cp->code )
  2153. {
  2154. case kSaSubIdx:
  2155. penalty += cp->matchFl ? 0 : 1;
  2156. penalty -= cp->transFl ? 1 : 0;
  2157. break;
  2158. case kSaDelIdx:
  2159. penalty += 1;
  2160. break;
  2161. case kSaInsIdx:
  2162. penalty += 1;
  2163. break;
  2164. }
  2165. pfl = cp->matchFl;
  2166. }
  2167. double score = gapCnt/n + penalty;
  2168. //printf("n:%i gaps:%f gap_score:%f penalty:%f score:%f\n",n,gapCnt,gapCnt/n,penalty,score);
  2169. return score;
  2170. }
  2171. void _cmScAlignEvalCandidate( cmScAlign* r, double score )
  2172. {
  2173. if( r->s_opt == DBL_MAX || score < r->s_opt)
  2174. {
  2175. // copy the p_cur to p_opt[]
  2176. cmScAlignPath_t* cp = r->p_cur;
  2177. unsigned i;
  2178. for(i=0; cp!=NULL && i<r->pn; cp=cp->next,++i)
  2179. {
  2180. r->p_opt[i].code = cp->code;
  2181. r->p_opt[i].ri = cp->ri;
  2182. r->p_opt[i].ci = cp->ci;
  2183. r->p_opt[i].matchFl = cp->matchFl;
  2184. r->p_opt[i].transFl = cp->transFl;
  2185. r->p_opt[i].next = cp->next==NULL ? NULL : r->p_opt + i + 1;
  2186. }
  2187. assert( i < r->pn );
  2188. r->p_opt[i].code = 0; // terminate with code=0
  2189. r->s_opt = score;
  2190. }
  2191. }
  2192. // traverse the solution matrix from the lower-right to
  2193. // the upper-left.
  2194. void _cmScAlignGenPaths( cmScAlign* r, int i, int j )
  2195. {
  2196. unsigned m;
  2197. // stop when the upper-right is encountered
  2198. if( i==0 && j==0 )
  2199. {
  2200. _cmScAlignEvalCandidate(r, _cmScAlignScoreCandidate(r) );
  2201. return;
  2202. }
  2203. cmScAlignVal_t* vp = _cmScAlignValPtr(r,i,j);
  2204. // for each possible dir: up,left,up-left
  2205. for(m=1; m<kSaCnt; ++m)
  2206. if( vp->v[m] == vp->v[kSaMinIdx] )
  2207. {
  2208. // prepend to the current candidate path: r->p_cur
  2209. _cmScAlignPathPush(r,m,i,j,vp->matchFl,vp->transFl);
  2210. int ii = i-1;
  2211. int jj = j-1;
  2212. switch(m)
  2213. {
  2214. case kSaSubIdx:
  2215. break;
  2216. case kSaDelIdx:
  2217. jj = j;
  2218. break;
  2219. case kSaInsIdx:
  2220. ii = i;
  2221. break;
  2222. }
  2223. // recurse!
  2224. _cmScAlignGenPaths(r,ii,jj);
  2225. // remove the first element from the current path
  2226. _cmScAlignPathPop(r);
  2227. }
  2228. }
  2229. double _cmScAlign( cmScAlign* p )
  2230. {
  2231. int i = p->rn-1;
  2232. int j = p->cn-1;
  2233. unsigned m = _cmScAlignMin(p,i,j); //p->m[i + (j*p->rn)].v[kSaMinIdx];
  2234. if( m==cmMax(p->rn,p->cn) )
  2235. printf("Edit distance is at max: %i. No Match.\n",m);
  2236. else
  2237. _cmScAlignGenPaths(p,i,j);
  2238. return p->s_opt;
  2239. }
  2240. cmRC_t cmScAlignExec( cmScAlign* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
  2241. {
  2242. bool fl = p->mbi > 0;
  2243. cmRC_t rc = cmOkRC;
  2244. // update the MIDI buffer with the incoming note
  2245. cmScAlignInputMidi(p,smpIdx,status,d0,d1);
  2246. // if the MIDI buffer transitioned to full then perform an initial scan sync.
  2247. if( fl && p->mbi == 0 )
  2248. {
  2249. if( (p->begSyncLocIdx = cmScAlignScan(p,cmInvalidCnt)) == cmInvalidIdx )
  2250. rc = cmInvalidArgRC; // signal init. scan sync. fail
  2251. }
  2252. else
  2253. {
  2254. // if the MIDI buffer is full then perform a step sync.
  2255. if( !fl && p->mbi == 0 )
  2256. rc = cmScAlignStep(p);
  2257. }
  2258. return rc;
  2259. }
  2260. bool cmScAlignInputMidi( cmScAlign* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
  2261. {
  2262. if( status != kNoteOnMdId )
  2263. return false;
  2264. unsigned mi = p->mn-1;
  2265. //printf("%3i %5.2f %4s\n",p->mni,(double)smpIdx/p->srate,cmMidiToSciPitch(d0,NULL,0));
  2266. // shift the new MIDI event onto the end of the MIDI buffer
  2267. memmove(p->midiBuf,p->midiBuf+1,sizeof(cmScAlignMidiEvt_t)*mi);
  2268. p->midiBuf[mi].locIdx = cmInvalidIdx;
  2269. p->midiBuf[mi].cbCnt = 0;
  2270. p->midiBuf[mi].mni = p->mni++;
  2271. p->midiBuf[mi].smpIdx = smpIdx;
  2272. p->midiBuf[mi].pitch = d0;
  2273. p->midiBuf[mi].vel = d1;
  2274. if( p->mbi > 0 )
  2275. --p->mbi;
  2276. return true;
  2277. }
  2278. // If mep==NULL then the identified score location was not matched (this is an 'insert')
  2279. // these only occurr during 'scan' not 'step'.
  2280. //
  2281. // If locIdx == cmInvalidIdx then the MIDI event did not match a score location
  2282. // When this occurrs during a scan then this is a 'deleted' MIDI note otherwise
  2283. // the note was not found inside loc[esi-stepCnt:esi+stepCnt].
  2284. //
  2285. // If mep!=NULL && scLocIdx!=cmInvalidIdx but matchFl==false then this is a
  2286. // 'substitute' with a mismatch. These only occur during 'scan'.
  2287. void _cmScAlignCb( cmScAlign* p, unsigned locIdx, cmScAlignMidiEvt_t* mep, bool matchFl, bool transFl )
  2288. {
  2289. // verify that the result buffer is not full
  2290. if( p->ri >= p->resN )
  2291. {
  2292. cmCtxRtCondition( &p->obj, cmArgAssertRC, "The score alignment result buffer is full.");
  2293. return;
  2294. }
  2295. // don't report unmatched score locations
  2296. if( mep == NULL )
  2297. return;
  2298. ++mep->cbCnt;
  2299. cmScAlignResult_t* rp = NULL;
  2300. // if this is the first time this MIDI event has generated a callback ...
  2301. if( mep->cbCnt == 1 )
  2302. rp = p->res + p->ri++; // ... then create a new record in p->res[] ...
  2303. else
  2304. if( mep->cbCnt > 1 && matchFl ) // ... otherwise if it was matched ...
  2305. {
  2306. unsigned i;
  2307. for(i=0; i<p->ri; ++i)
  2308. if(p->res[i].mni == mep->mni )
  2309. {
  2310. if( p->res[i].matchFl == false ) // ... and it's previous recd was not matched then update the record with the match info.
  2311. rp = p->res + i;
  2312. }
  2313. }
  2314. if(rp == NULL )
  2315. return;
  2316. assert( locIdx != cmInvalidIdx || mep != NULL );
  2317. rp->locIdx = locIdx;
  2318. rp->smpIdx = mep==NULL ? cmInvalidIdx : mep->smpIdx;
  2319. rp->mni = mep==NULL ? cmInvalidIdx : mep->mni;
  2320. rp->pitch = mep==NULL ? kInvalidMidiPitch : mep->pitch;
  2321. rp->vel = mep==NULL ? kInvalidMidiVelocity : mep->vel;
  2322. rp->matchFl = mep==NULL ? false : matchFl;
  2323. rp->transFl = mep==NULL ? false : transFl;
  2324. }
  2325. void _cmScAlignPrintPath( cmScAlign* p, cmScAlignPath_t* cp, unsigned bsi )
  2326. {
  2327. assert( bsi != cmInvalidIdx );
  2328. cmScAlignPath_t* pp = cp;
  2329. int polyN = 0;
  2330. int i;
  2331. printf("loc: ");
  2332. // get the polyphony count for the score window
  2333. for(i=0; pp!=NULL; pp=pp->next)
  2334. {
  2335. cmScAlignLoc_t* lp = p->loc + bsi + pp->ci;
  2336. if( pp->code!=kSaDelIdx )
  2337. {
  2338. if(lp->evtCnt > polyN)
  2339. polyN = lp->evtCnt;
  2340. printf("%4i ",bsi+i);
  2341. ++i;
  2342. }
  2343. else
  2344. printf("%4s "," ");
  2345. }
  2346. printf("\n");
  2347. // print the score notes
  2348. for(i=polyN; i>0; --i)
  2349. {
  2350. printf("%3i: ",i);
  2351. for(pp=cp; pp!=NULL; pp=pp->next)
  2352. {
  2353. int locIdx = bsi + pp->ci - 1;
  2354. assert(0 <= locIdx && locIdx <= p->locN);
  2355. cmScAlignLoc_t* lp = p->loc + locIdx;
  2356. if( pp->code!=kSaDelIdx && lp->evtCnt >= i )
  2357. printf("%4s ",cmMidiToSciPitch(lp->evtV[i-1].pitch,NULL,0));
  2358. else
  2359. printf("%4s ", pp->code==kSaDelIdx? "-" : " ");
  2360. }
  2361. printf("\n");
  2362. }
  2363. printf("mid: ");
  2364. // print the MIDI buffer
  2365. for(pp=cp; pp!=NULL; pp=pp->next)
  2366. {
  2367. if( pp->code!=kSaInsIdx )
  2368. printf("%4s ",cmMidiToSciPitch(p->midiBuf[pp->ri-1].pitch,NULL,0));
  2369. else
  2370. printf("%4s ",pp->code==kSaInsIdx?"-":" ");
  2371. }
  2372. printf("\nmni: ");
  2373. // print the MIDI buffer index (mni)
  2374. for(pp=cp; pp!=NULL; pp=pp->next)
  2375. {
  2376. if( pp->code!=kSaInsIdx )
  2377. printf("%4i ",p->midiBuf[pp->ri-1].mni);
  2378. else
  2379. printf("%4s ",pp->code==kSaInsIdx?"-":" ");
  2380. }
  2381. printf("\n op: ");
  2382. // print the substitute/insert/delete operation
  2383. for(pp=cp; pp!=NULL; pp=pp->next)
  2384. {
  2385. char c = ' ';
  2386. switch( pp->code )
  2387. {
  2388. case kSaSubIdx: c = 's'; break;
  2389. case kSaDelIdx: c = 'd'; break;
  2390. case kSaInsIdx: c = 'i'; break;
  2391. default:
  2392. { assert(0); }
  2393. }
  2394. printf("%4c ",c);
  2395. }
  2396. printf("\n ");
  2397. // give substitute attribute (match or transpose)
  2398. for(pp=cp; pp!=NULL; pp=pp->next)
  2399. {
  2400. cmChar_t s[3];
  2401. int k = 0;
  2402. if( pp->matchFl )
  2403. s[k++] = 'm';
  2404. if( pp->transFl )
  2405. s[k++] = 't';
  2406. s[k] = 0;
  2407. printf("%4s ",s);
  2408. }
  2409. printf("\nscl: ");
  2410. // print the stored location index
  2411. for(pp=cp; pp!=NULL; pp=pp->next)
  2412. {
  2413. if( pp->locIdx == cmInvalidIdx )
  2414. printf("%4s "," ");
  2415. else
  2416. printf("%4i ",p->loc[pp->locIdx].scLocIdx);
  2417. }
  2418. printf("\n\n");
  2419. }
  2420. // Returns the p->loc[] index at the start of the min cost score window
  2421. // based on the current MIDI buffer.
  2422. // scanCnt is the number of time the score window will be shifted one
  2423. // location to the left
  2424. unsigned cmScAlignScan( cmScAlign* p, unsigned scanCnt )
  2425. {
  2426. unsigned bsi = cmInvalidIdx;
  2427. assert( p->mbi == 0 );
  2428. // if the MIDI buf is full
  2429. if( p->mbi == 0 )
  2430. {
  2431. double s_opt = DBL_MAX;
  2432. unsigned i;
  2433. // Loop as long as the score window is inside the score.
  2434. // Fill the Dyn Pgm matrix: MIDI_buf to score[begScanLocIdx:begScanLocIdx+scWndN-1].
  2435. for(i=0; _cmScAlignCalcMtx(p) && (scanCnt==cmInvalidCnt || i<scanCnt); ++i)
  2436. {
  2437. // locate the path through the DP matrix with the lowest edit distance (cost)
  2438. double cost = _cmScAlign(p);
  2439. // if it is less than any previous score window
  2440. if(cost < s_opt)
  2441. {
  2442. s_opt = cost;
  2443. bsi = p->begScanLocIdx;
  2444. }
  2445. // increment the score window
  2446. p->begScanLocIdx += 1;
  2447. }
  2448. // store the cost assoc'd with bsi
  2449. p->s_opt = s_opt;
  2450. }
  2451. assert( bsi != cmInvalidIdx );
  2452. // Traverse the least cost path and:
  2453. // 1) Set p->esi to the score location index of the last MIDI note
  2454. // which has a positive match with the score and assign
  2455. // the internal score index to cp->locIdx.
  2456. //
  2457. // 2) Set cmScAlignPath_t.locIdx - index into p->loc[] associated
  2458. // with each path element that is a 'substitute' or an 'insert'.
  2459. //
  2460. // 3) Set p->missCnt: the count of trailing non-positive matches.
  2461. // p->missCnt is eventually used in cmScAlignStep() to track the number
  2462. // of consecutive trailing missed notes.
  2463. //
  2464. cmScAlignPath_t* cp = p->p_opt;
  2465. unsigned i = bsi;
  2466. p->missCnt = 0;
  2467. p->esi = cmInvalidIdx;
  2468. for(i=0; cp!=NULL; cp=cp->next)
  2469. {
  2470. if( cp->code != kSaInsIdx )
  2471. {
  2472. assert( cp->ri > 0 );
  2473. p->midiBuf[ cp->ri-1 ].locIdx = cmInvalidIdx;
  2474. }
  2475. switch( cp->code )
  2476. {
  2477. case kSaSubIdx:
  2478. if( cp->matchFl || cp->transFl)
  2479. {
  2480. p->esi = bsi + i;
  2481. p->missCnt = 0;
  2482. if( cp->matchFl )
  2483. p->midiBuf[ cp->ri-1 ].locIdx = bsi + i;
  2484. }
  2485. else
  2486. {
  2487. ++p->missCnt;
  2488. }
  2489. cp->locIdx = bsi + i;
  2490. ++i;
  2491. break;
  2492. case kSaInsIdx:
  2493. cp->locIdx = bsi + i;
  2494. ++i;
  2495. break;
  2496. case kSaDelIdx:
  2497. cp->locIdx = cmInvalidIdx;
  2498. ++p->missCnt;
  2499. break;
  2500. }
  2501. }
  2502. // if no positive matches were found
  2503. if( p->esi == cmInvalidIdx )
  2504. bsi = cmInvalidIdx;
  2505. else
  2506. {
  2507. // report matches
  2508. for(cp=p->p_opt; cp!=NULL; cp=cp->next)
  2509. {
  2510. unsigned locIdx = cp->locIdx;
  2511. cmScAlignMidiEvt_t* mep = NULL;
  2512. if( cp->code != kSaInsIdx )
  2513. mep = p->midiBuf + cp->ri - 1;
  2514. _cmScAlignCb(p,locIdx,mep,cp->matchFl,cp->transFl);
  2515. }
  2516. }
  2517. return bsi;
  2518. }
  2519. cmRC_t cmScAlignStep( cmScAlign* p )
  2520. {
  2521. int i;
  2522. unsigned pitch = p->midiBuf[ p->mn-1 ].pitch;
  2523. unsigned locIdx = cmInvalidIdx;
  2524. // the tracker must be sync'd to step
  2525. if( p->esi == cmInvalidIdx )
  2526. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The p->esi value must be valid to perform a step operation.");
  2527. // if the end of the score has been reached
  2528. if( p->esi + 1 >= p->locN )
  2529. return cmEofRC;
  2530. // attempt to match to next location first
  2531. if( _cmScAlignIsMatch(p->loc + p->esi + 1, pitch) )
  2532. {
  2533. locIdx = p->esi + 1;
  2534. }
  2535. else
  2536. {
  2537. //
  2538. for(i=2; i<p->stepCnt; ++i)
  2539. {
  2540. // go forward
  2541. if( p->esi+i < p->locN && _cmScAlignIsMatch(p->loc + p->esi + i, pitch) )
  2542. {
  2543. locIdx = p->esi + i;
  2544. break;
  2545. }
  2546. // go backward
  2547. if( p->esi >= (i-1) && _cmScAlignIsMatch(p->loc + p->esi - (i-1), pitch) )
  2548. {
  2549. locIdx = p->esi - (i-1);
  2550. break;
  2551. }
  2552. }
  2553. }
  2554. p->midiBuf[ p->mn-1 ].locIdx = locIdx;
  2555. if( locIdx == cmInvalidIdx )
  2556. ++p->missCnt;
  2557. else
  2558. {
  2559. p->missCnt = 0;
  2560. p->esi = locIdx;
  2561. _cmScAlignCb(p,locIdx, p->midiBuf + p->mn - 1,true,false);
  2562. }
  2563. if( p->missCnt >= p->maxStepMissCnt )
  2564. {
  2565. p->begScanLocIdx = p->esi > p->rn ? p->esi - p->rn : 0;
  2566. p->s_opt = DBL_MAX;
  2567. unsigned bsi = cmScAlignScan(p,p->rn*2);
  2568. ++p->scanCnt;
  2569. // if the scan failed find a match
  2570. if( bsi == cmInvalidIdx )
  2571. return cmCtxRtCondition( &p->obj, cmSubSysFailRC, "Scan resync. failed.");
  2572. //if( bsi != cmInvalidIdx )
  2573. // _cmScAlignPrintPath(p, p->p_opt, bsi );
  2574. }
  2575. return cmOkRC;
  2576. }
  2577. void _cmScAlignPrintMtx( cmScAlign* r)
  2578. {
  2579. unsigned i,j,k;
  2580. for(i=0; i<r->rn; ++i)
  2581. {
  2582. for(j=0; j<r->cn; ++j)
  2583. {
  2584. printf("(");
  2585. const cmScAlignVal_t* vp = _cmScAlignValPtr(r,i,j);
  2586. for(k=0; k<kSaCnt; ++k)
  2587. {
  2588. printf("%i",vp->v[k]);
  2589. if( k<kSaCnt-1)
  2590. printf(", ");
  2591. else
  2592. printf(" ");
  2593. }
  2594. printf("%c)",vp->transFl?'t':' ');
  2595. }
  2596. printf("\n");
  2597. }
  2598. }
  2599. void cmScAlignPrintOpt( cmScAlign* p )
  2600. {
  2601. unsigned i;
  2602. for(i=0; p->p_opt[i].code!=0; ++i)
  2603. {
  2604. cmScAlignPath_t* cp = p->p_opt + i;
  2605. char c0 = cp->matchFl ? 'm' : ' ';
  2606. char c1 = cp->transFl ? 't' : ' ';
  2607. printf("%2i code:%i ri:%2i ci:%2i %c%c\n",i,cp->code,cp->ri,cp->ci,c0,c1);
  2608. }
  2609. printf("score:%f\n",p->s_opt);
  2610. }
  2611. enum
  2612. {
  2613. kBarSaFl = 0x01, // this is a score bar
  2614. kScNoteSaFl = 0x02, // this is a score reference note (if mni != cmInvalidIdx then it was matched)
  2615. kSubsErrSaFl = 0x04, // 'subs' mismatch midi note
  2616. kMidiErrSaFl = 0x08, // 'deleted' Midi note
  2617. };
  2618. typedef struct cmScAlignPrint_str
  2619. {
  2620. unsigned flags;
  2621. unsigned scLocIdx;
  2622. unsigned smpIdx;
  2623. unsigned pitch;
  2624. unsigned vel;
  2625. unsigned mni;
  2626. bool matchFl;
  2627. bool transFl;
  2628. } cmScAlignPrint_t;
  2629. void _cmScAlignPrintList( cmScAlignPrint_t* a, unsigned an )
  2630. {
  2631. cmScAlignPrint_t* pp;
  2632. unsigned i;
  2633. printf("----------------------------------------------------\n");
  2634. printf("idx scl mni pit flg \n");
  2635. for(i=0; i<an; ++i)
  2636. {
  2637. pp = a + i;
  2638. printf("%3i %3i %3i %4s 0x%x\n",i,pp->scLocIdx,pp->mni,
  2639. pp->pitch==kInvalidMidiPitch ? " " : cmMidiToSciPitch(pp->pitch,NULL,0),
  2640. pp->flags);
  2641. }
  2642. printf("\n");
  2643. }
  2644. // insert a black record at a[i]
  2645. unsigned _cmScAlignPrintExpand( cmScAlignPrint_t* a, unsigned aan, unsigned i, unsigned an )
  2646. {
  2647. assert( an < aan );
  2648. memmove( a + i + 1, a + i, (an-i)*sizeof(cmScAlignPrint_t));
  2649. memset( a + i, 0, sizeof(cmScAlignPrint_t));
  2650. return an + 1;
  2651. }
  2652. void _cmScAlignPrintOutResult( cmScAlign* p, cmScAlignResult_t* rp, const cmChar_t* label )
  2653. {
  2654. printf("loc:%4i scloc:%4i smp:%10i mni:%4i %4s %c %c %s\n",
  2655. rp->locIdx,
  2656. rp->locIdx==cmInvalidIdx ? -1 : p->loc[rp->locIdx].scLocIdx,
  2657. rp->smpIdx,
  2658. rp->mni,
  2659. rp->pitch<=127 ? cmMidiToSciPitch(rp->pitch,NULL,0) : " ",
  2660. rp->matchFl ? 'm' : ' ',
  2661. rp->transFl ? 't' : ' ',
  2662. label);
  2663. }
  2664. void _cmScAlignPrintSet( cmScAlignPrint_t* pp, const cmScAlignResult_t* rp, unsigned flags, unsigned scLocIdx )
  2665. {
  2666. pp->scLocIdx = scLocIdx;
  2667. pp->flags = flags;
  2668. pp->smpIdx = rp->smpIdx;
  2669. pp->pitch = rp->pitch;
  2670. pp->vel = rp->vel;
  2671. pp->mni = rp->mni;
  2672. assert( pp->scLocIdx!=cmInvalidIdx || pp->mni != cmInvalidIdx );
  2673. }
  2674. unsigned _cmScAlignPrintPoly( cmScAlignPrint_t* a, unsigned an, unsigned scLocIdx )
  2675. {
  2676. unsigned polyN = 0;
  2677. unsigned i;
  2678. for(i=0; i<an; ++i)
  2679. if( a[i].scLocIdx == scLocIdx )
  2680. break;
  2681. if( i < an )
  2682. {
  2683. for(; i<an; ++i,++polyN)
  2684. if( a[i].scLocIdx != scLocIdx )
  2685. break;
  2686. // identical scLocIdx values must be consecutive
  2687. for(; i<an; ++i)
  2688. {
  2689. if( a[i].scLocIdx == scLocIdx )
  2690. _cmScAlignPrintList(a,an);
  2691. assert( a[i].scLocIdx != scLocIdx );
  2692. }
  2693. }
  2694. return polyN;
  2695. }
  2696. cmScAlignPrint_t* _cmScAlignPrintRecd(cmScAlignPrint_t* a, unsigned an, unsigned scLocIdx, unsigned polyIdx )
  2697. {
  2698. unsigned i,j;
  2699. for(i=0; i<an; ++i)
  2700. {
  2701. if( a[i].scLocIdx == scLocIdx )
  2702. for(j=0; i<an; ++j,++i)
  2703. {
  2704. if( a[i].scLocIdx != scLocIdx )
  2705. break;
  2706. if( j == polyIdx )
  2707. return a + i;
  2708. }
  2709. }
  2710. return NULL;
  2711. }
  2712. void _cmScAlignPrintReport( cmScAlign* p, cmScAlignPrint_t* a, unsigned an, unsigned bsi, unsigned esi )
  2713. {
  2714. unsigned colN = 5;
  2715. unsigned bli = bsi;
  2716. bool titleFl = true;
  2717. while( bli < esi )
  2718. {
  2719. unsigned i,j;
  2720. // get ending scLocIdx
  2721. unsigned eli = cmMin(bli+colN, esi);
  2722. // get the max poly count
  2723. unsigned polyN = 0;
  2724. for(i=bli; i<eli; ++i)
  2725. {
  2726. unsigned pn = _cmScAlignPrintPoly(a,an,i);
  2727. if( pn > polyN )
  2728. polyN = pn;
  2729. }
  2730. // print titles
  2731. if( titleFl )
  2732. {
  2733. printf(" ");
  2734. for(j=bli; j<eli; ++j)
  2735. printf("| %4s %4s %3s %1s ","mni"," ","vel"," ");
  2736. printf("\n");
  2737. titleFl = false;
  2738. }
  2739. // print 'loc' index line
  2740. printf("scl: ");
  2741. for(j=bli; j<eli; ++j)
  2742. printf("| %4i %4s %3s %1s ",j," "," "," ");
  2743. printf("\n");
  2744. for(i=polyN; i>0; --i)
  2745. {
  2746. printf("%3i: ",i);
  2747. for(j=bli; j<eli; ++j)
  2748. {
  2749. cmScAlignPrint_t* pp;
  2750. if((pp = _cmScAlignPrintRecd(a,an,j,i-1)) == NULL )
  2751. printf("| %4s %4s %3s %1s "," "," "," "," ");
  2752. else
  2753. {
  2754. if( pp->mni == cmInvalidIdx && cmIsNotFlag(pp->flags,kBarSaFl) )
  2755. printf("| %4s %4s %3s %1s "," ",cmMidiToSciPitch(pp->pitch,NULL,0)," "," ");
  2756. else
  2757. {
  2758. if( cmIsFlag(pp->flags,kBarSaFl) )
  2759. printf("| %4s %4s %3i %1s "," "," | ",pp->pitch,"b");
  2760. else
  2761. {
  2762. const cmChar_t* op = cmIsFlag(pp->flags,kMidiErrSaFl) ? "d" : " ";
  2763. op = cmIsFlag(pp->flags,kSubsErrSaFl) ? "s" : op;
  2764. printf("| %4i %4s %3i %1s ",pp->mni,cmMidiToSciPitch(pp->pitch,NULL,0),pp->vel,op);
  2765. }
  2766. }
  2767. }
  2768. }
  2769. printf("\n");
  2770. }
  2771. printf("\n");
  2772. bli = eli;
  2773. }
  2774. }
  2775. // The goal of this function is to create a cmScAlignPrint_t array containing
  2776. // one record for each score bar, score note and errant MIDI note.
  2777. // The function works by first creating a record for each score bar and note
  2778. // and then scanning the cmScAlignResult_t array (p->res[]) for each result
  2779. // record create by an earlier call to _cmScAlignCb(). A result record can
  2780. // uniquely indicates one of the following result states based on receiving
  2781. // a MIDI event.
  2782. // Match - locIdx!=cmInvalidIdx matchFl==true mni!=cmInvalidIdx
  2783. // Mis-match - locIdx!=cmInvalidIdx matchFl==false mni!=cmInvalidIdx
  2784. // Delete - locIdx==cmInvalidIdx matchFl==false mni!=cmInvalidIdx
  2785. // Insert - locIdx==cmInvalidIdx matchFl==false mni==cmInvalidIdx
  2786. //
  2787. // This is made slightly more complicated by the fact that a given MIDI event
  2788. // may generate more than one result record. This can occur when the
  2789. // tracker is in 'step' mode and generates a result record with a given state
  2790. // as a result of a given MIDI note and then reconsiders that MIDI note
  2791. // while during a subsequent 'scan' mode resync. operation. For example
  2792. // a MIDI note which generate a 'delete' result during a step operation
  2793. // may later generate a match result during a scan.
  2794. double _cmScAlignPrintResult( cmScAlign* p )
  2795. {
  2796. // determine the scH score begin and end indexes
  2797. unsigned bsi = cmScoreLocCount(p->scH);
  2798. unsigned esi = 0;
  2799. unsigned i,j;
  2800. for(i=0; i<p->ri; ++i)
  2801. {
  2802. cmScAlignResult_t* rp = p->res + i;
  2803. assert( rp->locIdx==cmInvalidIdx || rp->locIdx<p->locN);
  2804. if( rp->locIdx != cmInvalidIdx )
  2805. {
  2806. bsi = cmMin(bsi,p->loc[ rp->locIdx ].scLocIdx);
  2807. esi = cmMax(esi,p->loc[ rp->locIdx ].scLocIdx);
  2808. }
  2809. }
  2810. // get a count of MIDI events + score events
  2811. unsigned aan = p->ri;
  2812. for(i=bsi; i<=esi; ++i)
  2813. {
  2814. cmScoreLoc_t* lp = cmScoreLoc( p->scH, i);
  2815. aan += lp->evtCnt;
  2816. }
  2817. cmScAlignPrint_t* a = cmMemAllocZ(cmScAlignPrint_t,aan);
  2818. unsigned an = 0;
  2819. unsigned scNoteCnt = 0; // notes in the score
  2820. unsigned matchCnt = 0; // matched score notes
  2821. unsigned wrongCnt = 0; // errant midi notes
  2822. unsigned skipCnt = 0; // skipped score events
  2823. // create a record for each score event
  2824. for(i=bsi; i<=esi; ++i)
  2825. {
  2826. cmScoreLoc_t* lp = cmScoreLoc( p->scH, i);
  2827. for(j=0; j<lp->evtCnt; ++j,++an)
  2828. {
  2829. assert( an < aan );
  2830. cmScAlignPrint_t* pp = a + an;
  2831. assert( lp->index != cmInvalidIdx );
  2832. pp->scLocIdx = lp->index;
  2833. pp->mni = cmInvalidIdx;
  2834. pp->pitch = kInvalidMidiPitch;
  2835. pp->vel = cmInvalidIdx;
  2836. switch( lp->evtArray[j]->type )
  2837. {
  2838. case kBarEvtScId:
  2839. pp->flags = kBarSaFl;
  2840. pp->pitch = lp->evtArray[j]->barNumb;
  2841. pp->mni = cmInvalidIdx;
  2842. break;
  2843. case kNonEvtScId:
  2844. pp->flags = kScNoteSaFl;
  2845. pp->pitch = lp->evtArray[j]->pitch;
  2846. ++scNoteCnt;
  2847. break;
  2848. }
  2849. }
  2850. }
  2851. //_cmScAlignPrintList(a,an);
  2852. // Update the score with matching MIDI notes
  2853. // for each result record ...
  2854. for(i=0; i<p->ri; ++i)
  2855. {
  2856. cmScAlignResult_t* rp = p->res + i;
  2857. rp->foundFl = false;
  2858. // ... if this is not an errant MIDI note (delete)
  2859. if( rp->locIdx != cmInvalidIdx )
  2860. {
  2861. assert( rp->locIdx != cmInvalidIdx && rp->locIdx < p->locN );
  2862. unsigned scLocIdx = p->loc[rp->locIdx].scLocIdx;
  2863. cmScAlignPrint_t* pp;
  2864. // ... find the score location matching the result record score location
  2865. for(j=0; j<an; ++j)
  2866. {
  2867. pp = a + j;
  2868. // if this score location matches the result score location
  2869. if( scLocIdx == pp->scLocIdx )
  2870. {
  2871. // if this is a matching midi node
  2872. if( rp->matchFl && cmIsFlag(pp->flags,kScNoteSaFl) && pp->pitch == rp->pitch )
  2873. {
  2874. //_cmScAlignPrintOutResult(p,rp,"match");
  2875. rp->foundFl = true;
  2876. _cmScAlignPrintSet(pp, rp, pp->flags, pp->scLocIdx );
  2877. ++matchCnt;
  2878. break;
  2879. }
  2880. // if this is a 'substitute' non-matching note
  2881. if( rp->matchFl == false && rp->mni != cmInvalidIdx )
  2882. {
  2883. //_cmScAlignPrintOutResult(p,rp,"mis-match");
  2884. ++j; // insert after the a[j]
  2885. an = _cmScAlignPrintExpand(a,aan,j,an);
  2886. _cmScAlignPrintSet(a + j, rp, kSubsErrSaFl, scLocIdx );
  2887. rp->foundFl = true;
  2888. ++wrongCnt;
  2889. break;
  2890. }
  2891. // if this is a 'skipped' score note ('insert') alert
  2892. if( rp->mni == cmInvalidIdx )
  2893. {
  2894. //_cmScAlignPrintOutResult(p,rp,"skip");
  2895. rp->foundFl = true;
  2896. break;
  2897. }
  2898. }
  2899. }
  2900. }
  2901. if( rp->foundFl == false )
  2902. {
  2903. // _cmScAlignPrintOutResult(p,rp,"not-found");
  2904. }
  2905. }
  2906. //_cmScAlignPrintList(a,an);
  2907. // Insert records into the print record array (a[](
  2908. // to represent errant MIDI notes. (Notes which
  2909. // were played but do not match any notes in the score.)
  2910. // for each result record ...
  2911. for(i=0; i<p->ri; ++i)
  2912. {
  2913. cmScAlignResult_t* rp = p->res + i;
  2914. cmScAlignPrint_t* pp = NULL;
  2915. cmScAlignPrint_t* dpp = NULL;
  2916. unsigned dmin;
  2917. // if this result did not have a matching score event
  2918. if(rp->foundFl)
  2919. continue;
  2920. // find the print recd with the closest mni
  2921. for(j=0; j<an; ++j)
  2922. {
  2923. pp = a + j;
  2924. if( pp->mni!=cmInvalidId )
  2925. {
  2926. unsigned d;
  2927. if( pp->mni > rp->mni )
  2928. d = pp->mni - rp->mni;
  2929. else
  2930. d = rp->mni - pp->mni;
  2931. if( dpp == NULL || d < dmin )
  2932. {
  2933. dpp = pp;
  2934. dmin = d;
  2935. }
  2936. }
  2937. }
  2938. assert( dpp != NULL );
  2939. j = dpp - a;
  2940. if( rp->mni > dpp->mni )
  2941. ++j;
  2942. assert( rp->locIdx == cmInvalidIdx );
  2943. // insert a print recd before or after the closest print recd
  2944. an = _cmScAlignPrintExpand(a,aan,j,an);
  2945. _cmScAlignPrintSet(a + j, rp, kMidiErrSaFl, dpp->scLocIdx );
  2946. ++wrongCnt;
  2947. }
  2948. for(i=0; i<an; ++i)
  2949. if( cmIsFlag(a[i].flags,kScNoteSaFl) && (a[i].mni == cmInvalidIdx || cmIsFlag(a[i].flags,kSubsErrSaFl)))
  2950. ++skipCnt;
  2951. //_cmScAlignPrintList(a,an);
  2952. //_cmScAlignPrintReport(p,a,an,bsi,esi);
  2953. double prec = (double)2.0 * matchCnt / (matchCnt + wrongCnt);
  2954. double rcal = (double)2.0 * matchCnt / (matchCnt + skipCnt);
  2955. double fmeas = prec * rcal / (prec + rcal);
  2956. 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);
  2957. cmMemFree(a);
  2958. return fmeas;
  2959. }
  2960. cmRC_t cmScAlignScanToTimeLineEvent( cmScMatcher* p, cmTlH_t tlH, cmTlObj_t* top, unsigned endSmpIdx )
  2961. {
  2962. assert( top != NULL );
  2963. cmTlMidiEvt_t* mep = NULL;
  2964. cmRC_t rc = cmOkRC;
  2965. // as long as more MIDI events are available get the next MIDI msg
  2966. while( rc==cmOkRC && (mep = cmTlNextMidiEvtObjPtr(tlH, top, top->seqId )) != NULL )
  2967. {
  2968. top = &mep->obj;
  2969. // if the msg falls after the end of the marker then we are through
  2970. if( mep->obj.seqSmpIdx != cmInvalidIdx && mep->obj.seqSmpIdx > endSmpIdx )
  2971. break;
  2972. // if the time line MIDI msg a note-on
  2973. if( mep->msg->status == kNoteOnMdId )
  2974. {
  2975. rc = cmScMatcherExec(p, mep->obj.seqSmpIdx, mep->msg->status, mep->msg->u.chMsgPtr->d0, mep->msg->u.chMsgPtr->d1 );
  2976. switch( rc )
  2977. {
  2978. case cmOkRC: // continue processing MIDI events
  2979. break;
  2980. case cmEofRC: // end of the score was encountered
  2981. break;
  2982. case cmInvalidArgRC: // p->esi was not set correctly
  2983. break;
  2984. case cmSubSysFailRC: // scan resync failed
  2985. break;
  2986. }
  2987. }
  2988. }
  2989. if( rc == cmEofRC )
  2990. rc = cmOkRC;
  2991. return rc;
  2992. }
  2993. void cmScAlignCb( void* cbArg, unsigned scLocIdx, unsigned mni, unsigned pitch, unsigned vel )
  2994. {
  2995. //cmScAlign* p = (cmScAlign*)cbArg;
  2996. }
  2997. void cmScAlignScanMarkers( cmRpt_t* rpt, cmTlH_t tlH, cmScH_t scH )
  2998. {
  2999. unsigned i;
  3000. double srate = 96000;
  3001. unsigned midiN = 7;
  3002. unsigned scWndN = 10;
  3003. unsigned markN = 291;
  3004. cmCtx* ctx = cmCtxAlloc(NULL, rpt, cmLHeapNullHandle, cmSymTblNullHandle );
  3005. cmScMatcher* p = cmScMatcherAlloc(ctx,NULL,srate,scH,scWndN,midiN);
  3006. unsigned markCharCnt = 31;
  3007. cmChar_t markText[ markCharCnt+1 ];
  3008. double scoreThresh = 0.5;
  3009. unsigned candCnt = 0;
  3010. unsigned initFailCnt = 0;
  3011. unsigned otherFailCnt = 0;
  3012. unsigned scoreFailCnt = 0;
  3013. bool printFl = false;
  3014. cmTimeSpec_t t0,t1;
  3015. cmTimeGet(&t0);
  3016. //p->cbArg = p; // set the callback arg.
  3017. // for each marker
  3018. for(i=0; i<markN; ++i)
  3019. {
  3020. // form the marker text
  3021. snprintf(markText,markCharCnt,"Mark %i",i);
  3022. // locate the marker
  3023. cmTlMarker_t* mp = cmTimeLineMarkerFind( tlH, markText );
  3024. if( mp == NULL )
  3025. {
  3026. if( printFl )
  3027. printf("The marker '%s' was not found.\n\n",markText);
  3028. continue;
  3029. }
  3030. // skip markers which do not contain text
  3031. if( cmTextIsEmpty(mp->text) )
  3032. {
  3033. if( printFl )
  3034. printf("The marker '%s' is being skipped because it has no text.\n\n",markText);
  3035. continue;
  3036. }
  3037. // reset the score follower to the beginnig of the score
  3038. cmScMatcherReset(p);
  3039. ++candCnt;
  3040. // scan to the beginning of the marker
  3041. cmRC_t rc = cmScAlignScanToTimeLineEvent(p,tlH,&mp->obj,mp->obj.seqSmpIdx+mp->obj.durSmpCnt);
  3042. bool pfl = true;
  3043. if( rc != cmOkRC || p->begSyncLocIdx==cmInvalidIdx)
  3044. {
  3045. if( p->begSyncLocIdx == cmInvalidIdx )
  3046. rc = cmInvalidArgRC;
  3047. if( p->mni == 0 )
  3048. {
  3049. if( printFl )
  3050. printf("mark:%i midi:%i Not enough MIDI notes to fill the scan buffer.\n",i,p->mni);
  3051. pfl = false;
  3052. }
  3053. else
  3054. {
  3055. switch(rc)
  3056. {
  3057. case cmInvalidArgRC:
  3058. if( printFl )
  3059. printf("mark:%i INITIAL SYNC FAIL\n",i);
  3060. ++initFailCnt;
  3061. pfl = false;
  3062. break;
  3063. case cmSubSysFailRC:
  3064. if( printFl )
  3065. printf("mark:%i SCAN RESYNC FAIL\n",i);
  3066. ++otherFailCnt;
  3067. break;
  3068. default:
  3069. if( printFl )
  3070. printf("mark:%i UNKNOWN FAIL\n",i);
  3071. ++otherFailCnt;
  3072. }
  3073. }
  3074. }
  3075. if( pfl )
  3076. {
  3077. double fmeas = cmScMatcherFMeas(p);
  3078. if( printFl )
  3079. printf("mark:%i midi:%i loc:%i bar:%i cost:%f f-meas:%f text:%s\n",i,p->mni,p->begSyncLocIdx,p->mp->loc[p->begSyncLocIdx].barNumb,p->s_opt,fmeas,mp->text);
  3080. if( fmeas < scoreThresh )
  3081. ++scoreFailCnt;
  3082. }
  3083. //cmScMatcherPrint(p);
  3084. //break; // ONLY USE ONE MARKER DURING TESTING
  3085. if( printFl )
  3086. printf("\n");
  3087. }
  3088. printf("cand:%i fail:%i - init:%i score:%i other:%i\n\n",candCnt,initFailCnt+scoreFailCnt+otherFailCnt,initFailCnt,scoreFailCnt,otherFailCnt);
  3089. cmTimeGet(&t1);
  3090. printf("elapsed:%f\n", (double)cmTimeElapsedMicros(&t0,&t1)/1000000.0 );
  3091. cmScMatcherFree(&p);
  3092. cmCtxFree(&ctx);
  3093. }