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

cmProc4.c 100KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077
  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 "cmFile.h"
  12. #include "cmFileSys.h"
  13. #include "cmJson.h"
  14. #include "cmSymTbl.h"
  15. #include "cmAudioFile.h"
  16. #include "cmText.h"
  17. #include "cmProcObj.h"
  18. #include "cmProcTemplate.h"
  19. #include "cmMath.h"
  20. #include "cmTime.h"
  21. #include "cmMidi.h"
  22. #include "cmMidiFile.h"
  23. #include "cmProc.h"
  24. #include "cmProc2.h"
  25. #include "cmVectOps.h"
  26. #include "cmTimeLine.h"
  27. #include "cmScore.h"
  28. #include "cmProc4.h"
  29. //=======================================================================================================================
  30. //----------------------------------------------------------------------------------------
  31. void ed_print_mtx( ed_r* r)
  32. {
  33. unsigned i,j,k;
  34. for(i=0; i<r->rn; ++i)
  35. {
  36. for(j=0; j<r->cn; ++j)
  37. {
  38. printf("(");
  39. const ed_val* vp = r->m + i + (j*r->rn);
  40. for(k=0; k<kEdCnt; ++k)
  41. {
  42. printf("%i",vp->v[k]);
  43. if( k<kEdCnt-1)
  44. printf(", ");
  45. else
  46. printf(" ");
  47. }
  48. printf("%c)",vp->transFl?'t':' ');
  49. }
  50. printf("\n");
  51. }
  52. }
  53. void ed_init( ed_r* r, const char* s0, const char* s1 )
  54. {
  55. unsigned i,j,k;
  56. r->rn = strlen(s0)+1;
  57. r->cn = strlen(s1)+1;
  58. r->m = cmMemAllocZ(ed_val, r->rn*r->cn );
  59. r->pn = r->rn + r->cn;
  60. r->p_mem = cmMemAllocZ(ed_path, 2*r->pn );
  61. r->p_avl = r->p_mem;
  62. r->p_cur = NULL;
  63. r->p_opt = r->p_mem + r->pn;
  64. r->s_opt = DBL_MAX;
  65. r->s0 = s0;
  66. r->s1 = s1;
  67. for(i=0; i<r->rn; ++i)
  68. for(j=0; j<r->cn; ++j)
  69. {
  70. unsigned v[] = {0,0,0,0};
  71. if( i == 0 )
  72. {
  73. v[kEdMinIdx] = j;
  74. v[kEdInsIdx] = j;
  75. }
  76. else
  77. if( j == 0 )
  78. {
  79. v[kEdMinIdx] = i;
  80. v[kEdDelIdx] = i;
  81. }
  82. for(k=0; k<kEdCnt; ++k)
  83. r->m[ i + (j*r->rn) ].v[k] = v[k];
  84. }
  85. // put pn path records on the available list
  86. for(i=0; i<r->pn; ++i)
  87. r->p_mem[i].next = i<r->pn-1 ? r->p_mem + i + 1 : NULL;
  88. }
  89. unsigned _ed_min( ed_r* r, unsigned i, unsigned j )
  90. {
  91. assert( i<r->rn && j<r->cn );
  92. return r->m[ i + (j*r->rn) ].v[kEdMinIdx];
  93. }
  94. bool _ed_is_trans( ed_r* r, const ed_val* v1p, unsigned i, unsigned j )
  95. {
  96. bool fl = false;
  97. ed_val* v0p = r->m + i + (j*r->rn);
  98. if( i>=1 && j>=1 &&
  99. v1p->v[kEdMinIdx] == v1p->v[kEdSubIdx]
  100. && v1p->matchFl == false
  101. && v0p->v[kEdMinIdx] == v0p->v[kEdSubIdx]
  102. && v0p->matchFl == false )
  103. {
  104. char c00 = r->s0[i-1];
  105. char c01 = r->s0[i ];
  106. char c10 = r->s1[j-1];
  107. char c11 = r->s1[j ];
  108. fl = c00==c11 && c01==c10;
  109. }
  110. return fl;
  111. }
  112. void ed_calc_mtx( ed_r* r )
  113. {
  114. unsigned i,j;
  115. for(i=1; i<r->rn; ++i)
  116. for(j=1; j<r->cn; ++j)
  117. {
  118. ed_val* vp = r->m + i + (j*r->rn);
  119. vp->matchFl = r->s0[i-1] == r->s1[j-1];
  120. unsigned cost = vp->matchFl ? 0 : 1;
  121. vp->v[kEdSubIdx] = _ed_min(r,i-1,j-1) + cost;
  122. vp->v[kEdDelIdx] = _ed_min(r,i-1,j ) + 1;
  123. vp->v[kEdInsIdx] = _ed_min(r,i, j-1) + 1;
  124. vp->v[kEdMinIdx] = cmMin( vp->v[kEdSubIdx], cmMin(vp->v[kEdDelIdx],vp->v[kEdInsIdx]));
  125. vp->transFl = _ed_is_trans(r,vp,i-1,j-1);
  126. }
  127. }
  128. void ed_path_push( ed_r* r, unsigned code, unsigned ri, unsigned ci, bool matchFl, bool transFl )
  129. {
  130. assert(r->p_avl != NULL );
  131. ed_path* p = r->p_avl;
  132. r->p_avl = r->p_avl->next;
  133. p->code = code;
  134. p->ri = ri;
  135. p->ci = ci;
  136. p->matchFl = matchFl;
  137. p->transFl = transFl;
  138. p->next = r->p_cur;
  139. r->p_cur = p;
  140. }
  141. void ed_path_pop( ed_r* r )
  142. {
  143. assert( r->p_cur != NULL );
  144. ed_path* tp = r->p_cur->next;
  145. r->p_cur->next = r->p_avl;
  146. r->p_avl = r->p_cur;
  147. r->p_cur = tp;
  148. }
  149. double ed_score_candidate( ed_r* r )
  150. {
  151. ed_path* cp = r->p_cur;
  152. ed_path* bp = r->p_cur;
  153. ed_path* ep = NULL;
  154. for(; cp!=NULL; cp=cp->next)
  155. if( cp->code != kEdInsIdx )
  156. {
  157. bp = cp;
  158. break;
  159. }
  160. for(; cp!=NULL; cp=cp->next)
  161. if( cp->code!=kEdInsIdx )
  162. ep = cp;
  163. assert( ep!=NULL && bp!=ep);
  164. unsigned n=1;
  165. for(cp=bp; cp!=ep; cp=cp->next)
  166. ++n;
  167. double gapCnt = 0;
  168. double penalty = 0;
  169. bool pfl = bp->matchFl;
  170. unsigned i;
  171. cp = bp;
  172. for(i=0; i<n; ++i,cp=cp->next)
  173. {
  174. // a gap is a transition from a matching subst. to an insert or deletion
  175. //if( pc != cp->code && cp->code != kEdSubIdx && pc==kEdSubIdx && pfl==true )
  176. if( pfl==true && cp->matchFl==false )
  177. ++gapCnt;
  178. //
  179. switch( cp->code )
  180. {
  181. case kEdSubIdx:
  182. penalty += cp->matchFl ? 0 : 1;
  183. penalty -= cp->transFl ? 1 : 0;
  184. break;
  185. case kEdDelIdx:
  186. penalty += 1;
  187. break;
  188. case kEdInsIdx:
  189. penalty += 1;
  190. break;
  191. }
  192. pfl = cp->matchFl;
  193. }
  194. double score = gapCnt/n + penalty;
  195. printf("n:%i gaps:%f gap_score:%f penalty:%f score:%f\n",n,gapCnt,gapCnt/n,penalty,score);
  196. return score;
  197. }
  198. void ed_eval_candidate( ed_r* r, double score )
  199. {
  200. if( r->s_opt == DBL_MAX || r->s_opt > score)
  201. {
  202. // copy the p_cur to p_opt[]
  203. ed_path* cp = r->p_cur;
  204. unsigned i;
  205. for(i=0; cp!=NULL && i<r->pn; cp=cp->next,++i)
  206. {
  207. r->p_opt[i].code = cp->code;
  208. r->p_opt[i].ri = cp->ri;
  209. r->p_opt[i].ci = cp->ci;
  210. r->p_opt[i].matchFl = cp->matchFl;
  211. r->p_opt[i].transFl = cp->transFl;
  212. }
  213. assert( i < r->pn );
  214. r->p_opt[i].code = 0; // terminate with code=0
  215. r->s_opt = score;
  216. }
  217. }
  218. void ed_print_opt( ed_r* r )
  219. {
  220. unsigned i;
  221. for(i=0; r->p_opt[i].code!=0; ++i)
  222. {
  223. ed_path* cp = r->p_opt + i;
  224. char c0 = cp->matchFl ? 'm' : ' ';
  225. char c1 = cp->transFl ? 't' : ' ';
  226. printf("%2i code:%i ri:%2i ci:%2i %c%c\n",i,cp->code,cp->ri,cp->ci,c0,c1);
  227. }
  228. printf("score:%f\n",r->s_opt);
  229. }
  230. void ed_print_candidate( ed_r* r )
  231. {
  232. ed_path* cp = r->p_cur;
  233. unsigned pn = r->pn;
  234. unsigned i;
  235. char s0[pn+1];
  236. char s1[pn+1];
  237. char s2[pn+1];
  238. char s3[pn+1];
  239. s0[pn] = 0;
  240. s1[pn] = 0;
  241. s2[pn] = 0;
  242. s3[pn] = 0;
  243. for(i=0; i<pn && cp!=NULL; ++i,cp=cp->next)
  244. {
  245. switch(cp->code)
  246. {
  247. case kEdSubIdx: // subst
  248. assert( 0 <= cp->ri && cp->ri <= r->rn );
  249. assert( 0 <= cp->ci && cp->ci <= r->cn );
  250. s0[i] = r->s0[cp->ri];
  251. s1[i] = r->s1[cp->ci];
  252. s2[i] = 's';
  253. s3[i] = cp->matchFl ? 'm' : ' ';
  254. break;
  255. case kEdDelIdx: // delete
  256. assert( 0 <= cp->ri && cp->ri <= r->rn );
  257. s0[i] = r->s0[cp->ri];
  258. s1[i] = ' ';
  259. s2[i] = 'd';
  260. s3[i] = ' ';
  261. break;
  262. case kEdInsIdx: // insert
  263. assert( 0 <= cp->ci && cp->ci <= r->cn );
  264. s0[i] = ' ';
  265. s1[i] = r->s1[cp->ci];
  266. s2[i] = 'i';
  267. s3[i] = ' ';
  268. break;
  269. }
  270. }
  271. if( i < pn )
  272. {
  273. s0[i] = 0;
  274. s1[i] = 0;
  275. s2[i] = 0;
  276. s3[i] = 0;
  277. }
  278. printf("\ns0:%s\n",s0);
  279. printf("s1:%s\n",s1);
  280. printf("s2:%s\n",s2);
  281. printf("s3:%s\n",s3);
  282. }
  283. // traverse the solution matrix from the lower-right to
  284. // the upper-left.
  285. void ed_node( ed_r* r, int i, int j )
  286. {
  287. unsigned m;
  288. // stop when the upper-right is encountered
  289. if( i==0 && j==0 )
  290. {
  291. ed_print_candidate(r);
  292. ed_eval_candidate(r, ed_score_candidate(r) );
  293. return;
  294. }
  295. ed_val* vp = r->m + i + (j*r->rn);
  296. // for each possible dir: up,left,up-left
  297. for(m=1; m<kEdCnt; ++m)
  298. if( vp->v[m] == vp->v[kEdMinIdx] )
  299. {
  300. unsigned ii = i-1;
  301. unsigned jj = j-1;
  302. switch(m)
  303. {
  304. case kEdSubIdx:
  305. break;
  306. case kEdDelIdx:
  307. jj = j;
  308. break;
  309. case kEdInsIdx:
  310. ii = i;
  311. break;
  312. }
  313. // prepend to the current candidate path: r->p_cur
  314. ed_path_push(r,m,ii,jj,vp->matchFl,vp->transFl);
  315. // recurse!
  316. ed_node(r,ii,jj);
  317. // remove the first element from the current path
  318. ed_path_pop(r);
  319. }
  320. }
  321. void ed_align( ed_r* r )
  322. {
  323. int i = r->rn-1;
  324. int j = r->cn-1;
  325. unsigned m = r->m[i + (j*r->rn)].v[kEdMinIdx];
  326. if( m==cmMax(r->rn,r->cn) )
  327. printf("Edit distance is at max: %i. No Match.\n",m);
  328. else
  329. ed_node(r,i,j);
  330. }
  331. void ed_free( ed_r* r )
  332. {
  333. cmMemFree(r->m);
  334. cmMemFree(r->p_mem);
  335. }
  336. void ed_main()
  337. {
  338. const char* s0 = "YHCQPGK";
  339. const char* s1 = "LAHYQQKPGKA";
  340. s0 = "ABCDE";
  341. s1 = "ABDCE";
  342. //s1 = "FGHIJK";
  343. ed_r r;
  344. ed_init(&r,s0,s1);
  345. ed_calc_mtx(&r);
  346. ed_print_mtx(&r);
  347. ed_align(&r);
  348. ed_print_opt(&r);
  349. ed_free(&r);
  350. }
  351. //=======================================================================================================================
  352. cmScMatch* cmScMatchAlloc( cmCtx* c, cmScMatch* p, cmScH_t scH, unsigned maxScWndN, unsigned maxMidiWndN )
  353. {
  354. cmScMatch* op = cmObjAlloc(cmScMatch,c,p);
  355. if( cmScoreIsValid(scH) )
  356. if( cmScMatchInit(op,scH,maxScWndN,maxMidiWndN) != cmOkRC )
  357. cmScMatchFree(&op);
  358. return op;
  359. }
  360. cmRC_t cmScMatchFree( cmScMatch** pp )
  361. {
  362. cmRC_t rc = cmOkRC;
  363. if( pp==NULL || *pp==NULL )
  364. return rc;
  365. cmScMatch* p = *pp;
  366. if((rc = cmScMatchFinal(p)) != cmOkRC )
  367. return rc;
  368. cmMemFree(p->loc);
  369. cmMemFree(p->m);
  370. cmMemFree(p->p_mem);
  371. cmObjFree(pp);
  372. return rc;
  373. }
  374. void _cmScMatchInitLoc( cmScMatch* p )
  375. {
  376. unsigned li,ei;
  377. p->locN = cmScoreEvtCount(p->scH);
  378. p->loc = cmMemResizeZ(cmScMatchLoc_t,p->loc,p->locN);
  379. // for each score location
  380. for(li=0,ei=0; li<cmScoreLocCount(p->scH); ++li)
  381. {
  382. unsigned i,n;
  383. const cmScoreLoc_t* lp = cmScoreLoc(p->scH,li);
  384. // count the number of note events at location li
  385. for(n=0,i=0; i<lp->evtCnt; ++i)
  386. if( lp->evtArray[i]->type == kNonEvtScId )
  387. ++n;
  388. assert( ei+n <= p->locN );
  389. // duplicate each note at location li n times
  390. for(i=0; i<n; ++i)
  391. {
  392. unsigned j,k;
  393. p->loc[ei+i].evtCnt = n;
  394. p->loc[ei+i].evtV = cmMemAllocZ(cmScMatchEvt_t,n);
  395. p->loc[ei+i].scLocIdx = li;
  396. p->loc[ei+i].barNumb = lp->barNumb;
  397. for(j=0,k=0; j<lp->evtCnt; ++j)
  398. if( lp->evtArray[j]->type == kNonEvtScId )
  399. {
  400. p->loc[ei+i].evtV[k].pitch = lp->evtArray[j]->pitch;
  401. p->loc[ei+i].evtV[k].scEvtIdx = lp->evtArray[j]->index;
  402. ++k;
  403. }
  404. }
  405. ei += n;
  406. }
  407. assert(ei<=p->locN);
  408. p->locN = ei;
  409. }
  410. cmRC_t cmScMatchInit( cmScMatch* p, cmScH_t scH, unsigned maxScWndN, unsigned maxMidiWndN )
  411. {
  412. unsigned i;
  413. cmRC_t rc;
  414. if((rc = cmScMatchFinal(p)) != cmOkRC )
  415. return rc;
  416. p->scH = scH;
  417. p->mrn = maxMidiWndN + 1;
  418. p->mcn = maxScWndN + 1;
  419. p->mmn = maxMidiWndN;
  420. p->msn = maxScWndN;
  421. _cmScMatchInitLoc(p);
  422. p->m = cmMemResizeZ(cmScMatchVal_t, p->m, p->mrn*p->mcn );
  423. p->pn = p->mrn + p->mcn;
  424. p->p_mem = cmMemResizeZ(cmScMatchPath_t, p->p_mem, 2*p->pn );
  425. p->p_avl = p->p_mem;
  426. p->p_cur = NULL;
  427. p->p_opt = p->p_mem + p->pn;
  428. // put pn path records on the available list
  429. for(i=0; i<p->pn; ++i)
  430. {
  431. p->p_mem[i].next = i<p->pn-1 ? p->p_mem + i + 1 : NULL;
  432. p->p_opt[i].next = i<p->pn-1 ? p->p_opt + i + 1 : NULL;
  433. }
  434. return rc;
  435. }
  436. cmRC_t cmScMatchFinal( cmScMatch* p )
  437. {
  438. unsigned i;
  439. if( p != NULL )
  440. for(i=0; i<p->locN; ++i)
  441. cmMemPtrFree(&p->loc[i].evtV);
  442. return cmOkRC;
  443. }
  444. cmRC_t _cmScMatchInitMtx( cmScMatch* p, unsigned rn, unsigned cn )
  445. {
  446. //if( rn >p->mrn && cn > p->mcn )
  447. if( rn*cn > p->mrn*p->mcn )
  448. {
  449. 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);
  450. }
  451. // if the size of the mtx is not changing then there is nothing to do
  452. if( rn == p->rn && cn == p->cn )
  453. return cmOkRC;
  454. // update the mtx size
  455. p->rn = rn;
  456. p->cn = cn;
  457. // fill in the default values for the first row
  458. // and column of the DP matrix
  459. unsigned i,j,k;
  460. for(i=0; i<rn; ++i)
  461. for(j=0; j<cn; ++j)
  462. {
  463. unsigned v[] = {0,0,0,0};
  464. if( i == 0 )
  465. {
  466. v[kSmMinIdx] = j;
  467. v[kSmInsIdx] = j;
  468. }
  469. else
  470. if( j == 0 )
  471. {
  472. v[kSmMinIdx] = i;
  473. v[kSmDelIdx] = i;
  474. }
  475. // zero the value field
  476. for(k=0; k<kSmCnt; ++k)
  477. p->m[ i + (j*rn) ].v[k] = v[k];
  478. }
  479. return cmOkRC;
  480. }
  481. cmScMatchVal_t* _cmScMatchValPtr( cmScMatch* p, unsigned i, unsigned j, unsigned rn, unsigned cn )
  482. {
  483. assert( i < rn && j < cn );
  484. return p->m + i + (j*rn);
  485. }
  486. unsigned _cmScMatchIsMatchIndex( const cmScMatchLoc_t* loc, unsigned pitch )
  487. {
  488. unsigned i;
  489. for(i=0; i<loc->evtCnt; ++i)
  490. if( loc->evtV[i].pitch == pitch )
  491. return i;
  492. return cmInvalidIdx;
  493. }
  494. bool _cmScMatchIsMatch( const cmScMatchLoc_t* loc, unsigned pitch )
  495. { return _cmScMatchIsMatchIndex(loc,pitch) != cmInvalidIdx; }
  496. bool _cmScMatchIsTrans( cmScMatch* p, const cmScMatchMidi_t* midiV, const cmScMatchVal_t* v1p, unsigned bsi, unsigned i, unsigned j, unsigned rn, unsigned cn )
  497. {
  498. bool fl = false;
  499. cmScMatchVal_t* v0p = _cmScMatchValPtr(p,i,j,rn,cn);
  500. if( i>=1 && j>=1
  501. && v1p->v[kSmMinIdx] == v1p->v[kSmSubIdx]
  502. && cmIsNotFlag(v1p->flags,kSmMatchFl)
  503. && v0p->v[kSmMinIdx] == v0p->v[kSmSubIdx]
  504. && cmIsNotFlag(v0p->flags,kSmMatchFl)
  505. )
  506. {
  507. unsigned c00 = midiV[i-1].pitch;
  508. unsigned c01 = midiV[i ].pitch;
  509. cmScMatchLoc_t* c10 = p->loc + bsi + j - 1;
  510. cmScMatchLoc_t* c11 = p->loc + bsi + j;
  511. fl = _cmScMatchIsMatch(c11,c00) && _cmScMatchIsMatch(c10,c01);
  512. }
  513. return fl;
  514. }
  515. unsigned _cmScMatchMin( cmScMatch* p, unsigned i, unsigned j, unsigned rn, unsigned cn )
  516. {
  517. return _cmScMatchValPtr(p,i,j,rn,cn)->v[kSmMinIdx];
  518. }
  519. // Return false if bsi + cn > p->locN
  520. // pitchV[rn-1]
  521. bool _cmScMatchCalcMtx( cmScMatch* p, unsigned bsi, const cmScMatchMidi_t* midiV, unsigned rn, unsigned cn )
  522. {
  523. // loc[begScanLocIdx:begScanLocIdx+cn-1] must be valid
  524. if( bsi + cn > p->locN )
  525. return false;
  526. unsigned i,j;
  527. for(j=1; j<cn; ++j)
  528. for(i=1; i<rn; ++i)
  529. {
  530. cmScMatchLoc_t* loc = p->loc + bsi + j - 1;
  531. unsigned pitch = midiV[i-1].pitch;
  532. cmScMatchVal_t* vp = _cmScMatchValPtr(p,i,j,rn,cn);
  533. unsigned idx = _cmScMatchIsMatchIndex(loc,pitch);
  534. vp->flags = idx==cmInvalidIdx ? 0 : kSmMatchFl;
  535. vp->scEvtIdx = idx==cmInvalidIdx ? cmInvalidIdx : loc->evtV[idx].scEvtIdx;
  536. unsigned cost = cmIsFlag(vp->flags,kSmMatchFl) ? 0 : 1;
  537. vp->v[kSmSubIdx] = _cmScMatchMin(p,i-1,j-1, rn, cn) + cost;
  538. vp->v[kSmDelIdx] = _cmScMatchMin(p,i-1,j , rn, cn) + 1;
  539. vp->v[kSmInsIdx] = _cmScMatchMin(p,i, j-1, rn, cn) + 1;
  540. vp->v[kSmMinIdx] = cmMin( vp->v[kSmSubIdx], cmMin(vp->v[kSmDelIdx],vp->v[kSmInsIdx]));
  541. vp->flags |= _cmScMatchIsTrans(p,midiV,vp,bsi,i-1,j-1,rn,cn) ? kSmTransFl : 0;
  542. }
  543. return true;
  544. }
  545. void _cmScMatchPrintMtx( cmScMatch* r, unsigned rn, unsigned cn)
  546. {
  547. unsigned i,j,k;
  548. for(i=0; i<rn; ++i)
  549. {
  550. for(j=0; j<cn; ++j)
  551. {
  552. printf("(");
  553. const cmScMatchVal_t* vp = _cmScMatchValPtr(r,i,j,rn,cn);
  554. for(k=0; k<kSmCnt; ++k)
  555. {
  556. printf("%i",vp->v[k]);
  557. if( k<kSmCnt-1)
  558. printf(", ");
  559. else
  560. printf(" ");
  561. }
  562. printf("%c%c)",cmIsFlag(vp->flags,kSmMatchFl)?'m':' ',cmIsFlag(vp->flags,kSmTransFl)?'t':' ');
  563. }
  564. printf("\n");
  565. }
  566. }
  567. void _cmScMatchPathPush( cmScMatch* r, unsigned code, unsigned ri, unsigned ci, unsigned flags, unsigned scEvtIdx )
  568. {
  569. assert(r->p_avl != NULL );
  570. cmScMatchPath_t* p = r->p_avl;
  571. r->p_avl = r->p_avl->next;
  572. p->code = code;
  573. p->ri = ri;
  574. p->ci = ci;
  575. p->flags = code==kSmSubIdx && cmIsFlag(flags,kSmMatchFl) ? kSmMatchFl : 0;
  576. p->flags |= cmIsFlag(flags,kSmTransFl) ? kSmTransFl : 0;
  577. p->scEvtIdx= scEvtIdx;
  578. p->next = r->p_cur;
  579. r->p_cur = p;
  580. }
  581. void _cmScMatchPathPop( cmScMatch* r )
  582. {
  583. assert( r->p_cur != NULL );
  584. cmScMatchPath_t* tp = r->p_cur->next;
  585. r->p_cur->next = r->p_avl;
  586. r->p_avl = r->p_cur;
  587. r->p_cur = tp;
  588. }
  589. double _cmScMatchCalcCandidateCost( cmScMatch* r )
  590. {
  591. cmScMatchPath_t* cp = r->p_cur;
  592. cmScMatchPath_t* bp = r->p_cur;
  593. cmScMatchPath_t* ep = NULL;
  594. // skip leading inserts
  595. for(; cp!=NULL; cp=cp->next)
  596. if( cp->code != kSmInsIdx )
  597. {
  598. bp = cp;
  599. break;
  600. }
  601. // skip to trailing inserts
  602. for(; cp!=NULL; cp=cp->next)
  603. if( cp->code!=kSmInsIdx )
  604. ep = cp;
  605. // count remaining path length
  606. assert( ep!=NULL );
  607. unsigned n=1;
  608. for(cp=bp; cp!=ep; cp=cp->next)
  609. ++n;
  610. double gapCnt = 0;
  611. double penalty = 0;
  612. bool pfl = cmIsFlag(bp->flags,kSmMatchFl);
  613. unsigned i;
  614. cp = bp;
  615. for(i=0; i<n; ++i,cp=cp->next)
  616. {
  617. // a gap is a transition from a matching subst. to an insert or deletion
  618. //if( pc != cp->code && cp->code != kSmSubIdx && pc==kSmSubIdx && pfl==true )
  619. if( pfl==true && cmIsFlag(cp->flags,kSmMatchFl)==false )
  620. ++gapCnt;
  621. //
  622. switch( cp->code )
  623. {
  624. case kSmSubIdx:
  625. penalty += cmIsFlag(cp->flags,kSmMatchFl) ? 0 : 1;
  626. penalty -= cmIsFlag(cp->flags,kSmTransFl) ? 1 : 0;
  627. break;
  628. case kSmDelIdx:
  629. penalty += 1;
  630. break;
  631. case kSmInsIdx:
  632. penalty += 1;
  633. break;
  634. }
  635. pfl = cmIsFlag(cp->flags,kSmMatchFl);
  636. }
  637. double cost = gapCnt/n + penalty;
  638. //printf("n:%i gaps:%f gap_score:%f penalty:%f score:%f\n",n,gapCnt,gapCnt/n,penalty,score);
  639. return cost;
  640. }
  641. double _cmScMatchEvalCandidate( cmScMatch* r, double min_cost, double cost )
  642. {
  643. if( min_cost == DBL_MAX || cost < min_cost)
  644. {
  645. // copy the p_cur to p_opt[]
  646. cmScMatchPath_t* cp = r->p_cur;
  647. unsigned i;
  648. for(i=0; cp!=NULL && i<r->pn; cp=cp->next,++i)
  649. {
  650. r->p_opt[i].code = cp->code;
  651. r->p_opt[i].ri = cp->ri;
  652. r->p_opt[i].ci = cp->ci;
  653. r->p_opt[i].flags = cp->flags;
  654. r->p_opt[i].scEvtIdx= cp->scEvtIdx;
  655. r->p_opt[i].next = cp->next==NULL ? NULL : r->p_opt + i + 1;
  656. }
  657. assert( i < r->pn );
  658. r->p_opt[i].code = 0; // terminate with code=0
  659. min_cost = cost;
  660. }
  661. return min_cost;
  662. }
  663. // NOTE: IF THE COST CALCULATION WAS BUILT INTO THE RECURSION THEN
  664. // THIS FUNCTION COULD BE MADE MORE EFFICIENT BECAUSE PATHS WHICH
  665. // EXCEEDED THE min_cost COULD BE SHORT CIRCUITED.
  666. //
  667. // traverse the solution matrix from the lower-right to
  668. // the upper-left.
  669. double _cmScMatchGenPaths( cmScMatch* r, int i, int j, unsigned rn, unsigned cn, double min_cost )
  670. {
  671. unsigned m;
  672. // stop when the upper-right is encountered
  673. if( i==0 && j==0 )
  674. return _cmScMatchEvalCandidate(r, min_cost, _cmScMatchCalcCandidateCost(r) );
  675. cmScMatchVal_t* vp = _cmScMatchValPtr(r,i,j,rn,cn);
  676. // for each possible dir: up,left,up-left
  677. for(m=1; m<kSmCnt; ++m)
  678. if( vp->v[m] == vp->v[kSmMinIdx] )
  679. {
  680. // prepend to the current candidate path: r->p_cur
  681. _cmScMatchPathPush(r,m,i,j,vp->flags,vp->scEvtIdx);
  682. int ii = i-1;
  683. int jj = j-1;
  684. switch(m)
  685. {
  686. case kSmSubIdx:
  687. break;
  688. case kSmDelIdx:
  689. jj = j;
  690. break;
  691. case kSmInsIdx:
  692. ii = i;
  693. break;
  694. default:
  695. { assert(0); }
  696. }
  697. // recurse!
  698. min_cost = _cmScMatchGenPaths(r,ii,jj,rn,cn,min_cost);
  699. // remove the first element from the current path
  700. _cmScMatchPathPop(r);
  701. }
  702. return min_cost;
  703. }
  704. double _cmScMatchAlign( cmScMatch* p, unsigned rn, unsigned cn, double min_cost )
  705. {
  706. int i = rn-1;
  707. int j = cn-1;
  708. unsigned m = _cmScMatchMin(p,i,j,rn,cn);
  709. if( m==cmMax(rn,cn) )
  710. printf("Edit distance is at max: %i. No Match.\n",m);
  711. else
  712. min_cost = _cmScMatchGenPaths(p,i,j,rn,cn,min_cost);
  713. return min_cost;
  714. }
  715. cmRC_t cmScMatchExec( cmScMatch* p, unsigned locIdx, unsigned locN, const cmScMatchMidi_t* midiV, unsigned midiN, double min_cost )
  716. {
  717. cmRC_t rc;
  718. unsigned rn = midiN + 1;
  719. unsigned cn = locN + 1;
  720. // set the DP matrix default values
  721. if((rc = _cmScMatchInitMtx(p, rn, cn )) != cmOkRC )
  722. return rc;
  723. // _cmScMatchCalcMtx() returns false if the score window exceeds the length of the score
  724. if(!_cmScMatchCalcMtx(p,locIdx, midiV, rn, cn) )
  725. return cmEofRC;
  726. //_cmScMatchPrintMtx(p,rn,cn);
  727. // locate the path through the DP matrix with the lowest edit distance (cost)
  728. p->opt_cost = _cmScMatchAlign(p, rn, cn, min_cost);
  729. return rc;
  730. }
  731. // Traverse the least cost path and:
  732. // 1) Return, esi, the score location index of the last MIDI note
  733. // which has a positive match with the score and assign
  734. // the internal score index to cp->locIdx.
  735. //
  736. // 2) Set cmScAlignPath_t.locIdx - index into p->loc[] associated
  737. // with each path element that is a 'substitute' or an 'insert'.
  738. //
  739. // 3) Set *missCnPtr: the count of trailing non-positive matches.
  740. //
  741. // i_opt is index into p->loc[] of p->p_opt.
  742. unsigned cmScMatchDoSync( cmScMatch* p, unsigned i_opt, cmScMatchMidi_t* midiBuf, unsigned midiN, unsigned* missCntPtr )
  743. {
  744. cmScMatchPath_t* cp = p->p_opt;
  745. unsigned missCnt = 0;
  746. unsigned esi = cmInvalidIdx;
  747. unsigned i;
  748. for(i=0; cp!=NULL; cp=cp->next)
  749. {
  750. // there is no MIDI note associated with 'inserts'
  751. if( cp->code != kSmInsIdx )
  752. {
  753. assert( cp->ri > 0 );
  754. midiBuf[ cp->ri-1 ].locIdx = cmInvalidIdx;
  755. }
  756. switch( cp->code )
  757. {
  758. case kSmSubIdx:
  759. midiBuf[ cp->ri-1 ].locIdx = i_opt + i;
  760. midiBuf[ cp->ri-1 ].scEvtIdx = cp->scEvtIdx;
  761. if( cmIsFlag(cp->flags,kSmMatchFl) )
  762. {
  763. esi = i_opt + i;
  764. missCnt = 0;
  765. }
  766. else
  767. {
  768. ++missCnt;
  769. }
  770. // fall through
  771. case kSmInsIdx:
  772. cp->locIdx = i_opt + i;
  773. ++i;
  774. break;
  775. case kSmDelIdx:
  776. cp->locIdx = cmInvalidIdx;
  777. ++missCnt;
  778. break;
  779. }
  780. }
  781. if( missCntPtr != NULL )
  782. *missCntPtr = missCnt;
  783. return esi;
  784. }
  785. void _cmScMatchMidiEvtFlags( cmScMatch* p, const cmScMatchLoc_t* lp, unsigned evtIdx, char* s, unsigned sn )
  786. {
  787. const cmScoreLoc_t* slp = cmScoreLoc(p->scH,lp->scLocIdx);
  788. assert( evtIdx < slp->evtCnt );
  789. const cmScoreEvt_t* ep = slp->evtArray[evtIdx];
  790. unsigned i = 0;
  791. s[0] = 0;
  792. if( cmIsFlag(ep->flags,kEvenScFl) )
  793. s[i++] = 'e';
  794. if( cmIsFlag(ep->flags,kTempoScFl) )
  795. s[i++] = 't';
  796. if( cmIsFlag(ep->flags,kDynScFl) )
  797. s[i++] = 'd';
  798. if( cmIsFlag(ep->flags,kGraceScFl) )
  799. s[i++] = 'g';
  800. s[i++] = 0;
  801. assert( i <= sn );
  802. }
  803. void _cmScMatchPrintPath( cmScMatch* p, cmScMatchPath_t* cp, unsigned bsi, const cmScMatchMidi_t* midiV )
  804. {
  805. assert( bsi != cmInvalidIdx );
  806. cmScMatchPath_t* pp = cp;
  807. int polyN = 0;
  808. int i;
  809. printf("loc: ");
  810. // get the polyphony count for the score window
  811. for(i=0; pp!=NULL; pp=pp->next)
  812. {
  813. cmScMatchLoc_t* lp = p->loc + bsi + pp->ci;
  814. if( pp->code!=kSmDelIdx )
  815. {
  816. if(lp->evtCnt > polyN)
  817. polyN = lp->evtCnt;
  818. printf("%4i%4s ",bsi+i," ");
  819. ++i;
  820. }
  821. else
  822. printf("%4s%4s "," "," ");
  823. }
  824. printf("\n");
  825. // print the score notes
  826. for(i=polyN; i>0; --i)
  827. {
  828. printf("%3i: ",i);
  829. for(pp=cp; pp!=NULL; pp=pp->next)
  830. {
  831. if( pp->code!=kSmDelIdx )
  832. {
  833. int locIdx = bsi + pp->ci - 1;
  834. assert(0 <= locIdx && locIdx <= p->locN);
  835. cmScMatchLoc_t* lp = p->loc + locIdx;
  836. if( lp->evtCnt >= i )
  837. {
  838. unsigned sn = 6;
  839. char s[sn];
  840. _cmScMatchMidiEvtFlags(p,lp,i-1,s,sn );
  841. printf("%4s%-4s ",cmMidiToSciPitch(lp->evtV[i-1].pitch,NULL,0),s);
  842. }
  843. else
  844. printf("%4s%4s "," "," ");
  845. }
  846. else
  847. printf("%4s%4s ", (pp->code==kSmDelIdx? "-" : " ")," ");
  848. /*
  849. int locIdx = bsi + pp->ci - 1;
  850. assert(0 <= locIdx && locIdx <= p->locN);
  851. cmScMatchLoc_t* lp = p->loc + locIdx;
  852. if( pp->code!=kSmDelIdx && lp->evtCnt >= i )
  853. printf("%4s ",cmMidiToSciPitch(lp->evtV[i-1].pitch,NULL,0));
  854. else
  855. printf("%4s ", pp->code==kSmDelIdx? "-" : " ");
  856. */
  857. }
  858. printf("\n");
  859. }
  860. printf("mid: ");
  861. // print the MIDI buffer
  862. for(pp=cp; pp!=NULL; pp=pp->next)
  863. {
  864. if( pp->code!=kSmInsIdx )
  865. printf("%4s%4s ",cmMidiToSciPitch(midiV[pp->ri-1].pitch,NULL,0)," ");
  866. else
  867. printf("%4s%4s ",pp->code==kSmInsIdx?"-":" "," ");
  868. }
  869. printf("\nvel: ");
  870. // print the MIDI velocity
  871. for(pp=cp; pp!=NULL; pp=pp->next)
  872. {
  873. if( pp->code!=kSmInsIdx )
  874. printf("%4i%4s ",midiV[pp->ri-1].vel," ");
  875. else
  876. printf("%4s%4s ",pp->code==kSmInsIdx?"-":" "," ");
  877. }
  878. printf("\nmni: ");
  879. // print the MIDI buffer index (mni)
  880. for(pp=cp; pp!=NULL; pp=pp->next)
  881. {
  882. if( pp->code!=kSmInsIdx )
  883. printf("%4i%4s ",midiV[pp->ri-1].mni," ");
  884. else
  885. printf("%4s%4s ",pp->code==kSmInsIdx?"-":" "," ");
  886. }
  887. printf("\n op: ");
  888. // print the substitute/insert/delete operation
  889. for(pp=cp; pp!=NULL; pp=pp->next)
  890. {
  891. char c = ' ';
  892. switch( pp->code )
  893. {
  894. case kSmSubIdx: c = 's'; break;
  895. case kSmDelIdx: c = 'd'; break;
  896. case kSmInsIdx: c = 'i'; break;
  897. default:
  898. { assert(0); }
  899. }
  900. printf("%4c%4s ",c," ");
  901. }
  902. printf("\n ");
  903. // give substitute attribute (match or transpose)
  904. for(pp=cp; pp!=NULL; pp=pp->next)
  905. {
  906. cmChar_t s[3];
  907. int k = 0;
  908. if( cmIsFlag(pp->flags,kSmMatchFl) )
  909. s[k++] = 'm';
  910. if( cmIsFlag(pp->flags,kSmTransFl) )
  911. s[k++] = 't';
  912. s[k] = 0;
  913. printf("%4s%4s ",s," ");
  914. }
  915. printf("\nscl: ");
  916. // print the stored location index
  917. for(pp=cp; pp!=NULL; pp=pp->next)
  918. {
  919. if( pp->locIdx == cmInvalidIdx )
  920. printf("%4s%4s "," "," ");
  921. else
  922. printf("%4i%4s ",p->loc[pp->locIdx].scLocIdx," ");
  923. }
  924. printf("\nbar: ");
  925. // print the stored location index
  926. for(pp=cp; pp!=NULL; pp=pp->next)
  927. {
  928. if( pp->locIdx==cmInvalidIdx || pp->scEvtIdx==cmInvalidIdx )
  929. printf("%4s%4s "," "," ");
  930. else
  931. {
  932. const cmScoreEvt_t* ep = cmScoreEvt(p->scH, pp->scEvtIdx );
  933. printf("%4i%4s ",ep->barNumb," ");
  934. }
  935. }
  936. printf("\nsec: ");
  937. // print seconds
  938. unsigned begSmpIdx = cmInvalidIdx;
  939. for(pp=cp; pp!=NULL; pp=pp->next)
  940. {
  941. if( pp->code!=kSmInsIdx )
  942. {
  943. if( begSmpIdx == cmInvalidIdx )
  944. begSmpIdx = midiV[pp->ri-1].smpIdx;
  945. printf("%2.2f%4s ", (double)(midiV[pp->ri-1].smpIdx - begSmpIdx)/96000.0," ");
  946. }
  947. else
  948. printf("%4s%4s ",pp->code==kSmInsIdx?"-":" "," ");
  949. }
  950. printf("\n\n");
  951. }
  952. //=======================================================================================================================
  953. cmScMatcher* cmScMatcherAlloc( cmCtx* c, cmScMatcher* p, double srate, cmScH_t scH, unsigned scWndN, unsigned midiWndN, cmScMatcherCb_t cbFunc, void* cbArg )
  954. {
  955. cmScMatcher* op = cmObjAlloc(cmScMatcher,c,p);
  956. if( op != NULL )
  957. op->mp = cmScMatchAlloc(c,NULL,cmScNullHandle,0,0);
  958. if( srate != 0 )
  959. {
  960. if( cmScMatcherInit(op,srate,scH,scWndN,midiWndN,cbFunc,cbArg) != cmOkRC )
  961. cmScMatcherFree(&op);
  962. }
  963. return op;
  964. }
  965. cmRC_t cmScMatcherFree( cmScMatcher** pp )
  966. {
  967. cmRC_t rc = cmOkRC;
  968. if( pp==NULL || *pp==NULL )
  969. return rc;
  970. cmScMatcher* p = *pp;
  971. if((rc = cmScMatcherFinal(p)) != cmOkRC )
  972. return rc;
  973. cmScMatchFree(&p->mp);
  974. cmMemFree(p->midiBuf);
  975. cmMemFree(p->res);
  976. cmObjFree(pp);
  977. return rc;
  978. }
  979. cmRC_t cmScMatcherInit( cmScMatcher* p, double srate, cmScH_t scH, unsigned scWndN, unsigned midiWndN, cmScMatcherCb_t cbFunc, void* cbArg )
  980. {
  981. cmRC_t rc;
  982. if((rc = cmScMatcherFinal(p)) != cmOkRC )
  983. return rc;
  984. if( midiWndN > scWndN )
  985. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The score alignment MIDI event buffer length (%i) must be less than the score window length (%i).",midiWndN,scWndN);
  986. if(( rc = cmScMatchInit(p->mp,scH,scWndN,midiWndN)) != cmOkRC )
  987. return rc;
  988. p->cbFunc = cbFunc;
  989. p->cbArg = cbArg;
  990. p->mn = midiWndN;
  991. p->midiBuf = cmMemResizeZ(cmScMatchMidi_t,p->midiBuf,p->mn);
  992. p->initHopCnt = 50;
  993. p->stepCnt = 3;
  994. p->maxMissCnt = p->stepCnt+1;
  995. p->rn = 2 * cmScoreEvtCount(scH);
  996. p->res = cmMemResizeZ(cmScMatcherResult_t,p->res,p->rn);
  997. p->printFl = false;
  998. cmScMatcherReset(p,0);
  999. return rc;
  1000. }
  1001. cmRC_t cmScMatcherFinal( cmScMatcher* p )
  1002. {
  1003. return cmScMatchFinal(p->mp);
  1004. }
  1005. cmRC_t cmScMatcherReset( cmScMatcher* p, unsigned scLocIdx )
  1006. {
  1007. p->mbi = p->mp->mmn;
  1008. p->mni = 0;
  1009. p->begSyncLocIdx = cmInvalidIdx;
  1010. p->s_opt = DBL_MAX;
  1011. p->missCnt = 0;
  1012. p->scanCnt = 0;
  1013. p->ri = 0;
  1014. p->eli = cmInvalidIdx;
  1015. p->ili = 0;
  1016. // convert scLocIdx to an index into p->mp->loc[]
  1017. unsigned i = 0;
  1018. while(1)
  1019. {
  1020. for(i=0; i<p->mp->locN; ++i)
  1021. if( p->mp->loc[i].scLocIdx == scLocIdx )
  1022. {
  1023. p->ili = i;
  1024. break;
  1025. }
  1026. assert(p->mp->locN>0);
  1027. if( i!=p->mp->locN || scLocIdx==p->mp->loc[p->mp->locN-1].scLocIdx)
  1028. break;
  1029. scLocIdx += 1;
  1030. }
  1031. if( i==p->mp->locN)
  1032. return cmCtxRtCondition( &p->obj, cmSubSysFailRC, "Score matcher reset failed.");
  1033. return cmOkRC;
  1034. }
  1035. bool cmScMatcherInputMidi( cmScMatcher* p, unsigned smpIdx, unsigned muid, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
  1036. {
  1037. if( (status&0xf0) != kNoteOnMdId)
  1038. return false;
  1039. if( d1 == 0 )
  1040. return false;
  1041. unsigned mi = p->mn-1;
  1042. //printf("%3i %4s\n",p->mni,cmMidiToSciPitch(d0,NULL,0));
  1043. // shift the new MIDI event onto the end of the MIDI buffer
  1044. memmove(p->midiBuf, p->midiBuf+1, sizeof(cmScMatchMidi_t)*mi);
  1045. p->midiBuf[mi].locIdx = cmInvalidIdx;
  1046. p->midiBuf[mi].scEvtIdx = cmInvalidIdx;
  1047. p->midiBuf[mi].mni = p->mni++;
  1048. p->midiBuf[mi].smpIdx = smpIdx;
  1049. p->midiBuf[mi].muid = muid;
  1050. p->midiBuf[mi].pitch = d0;
  1051. p->midiBuf[mi].vel = d1;
  1052. if( p->mbi > 0 )
  1053. --p->mbi;
  1054. return true;
  1055. }
  1056. void _cmScMatcherStoreResult( cmScMatcher* p, unsigned locIdx, unsigned scEvtIdx, unsigned flags, const cmScMatchMidi_t* mp )
  1057. {
  1058. // don't store missed score note results
  1059. assert( mp != NULL );
  1060. bool matchFl = cmIsFlag(flags,kSmMatchFl);
  1061. bool tpFl = locIdx!=cmInvalidIdx && matchFl;
  1062. bool fpFl = locIdx==cmInvalidIdx || matchFl==false;
  1063. cmScMatcherResult_t * rp = NULL;
  1064. unsigned i;
  1065. cmScMatcherResult_t r;
  1066. assert( tpFl==false || (tpFl==true && locIdx != cmInvalidIdx ) );
  1067. // it is possible that the same MIDI event is reported more than once
  1068. // (due to step->scan back tracking) - try to find previous result records
  1069. // associated with this MIDI event
  1070. for(i=0; i<p->ri; ++i)
  1071. if( p->res[i].mni == mp->mni )
  1072. {
  1073. // if this is not the first time this note was reported and it is a true positive
  1074. if( tpFl )
  1075. {
  1076. rp = p->res + i;
  1077. break;
  1078. }
  1079. // a match was found but this was not a true-pos so ignore it
  1080. return;
  1081. }
  1082. if( rp == NULL )
  1083. {
  1084. if( p->ri >= p->rn )
  1085. {
  1086. rp = &r;
  1087. memset(rp,0,sizeof(r));
  1088. }
  1089. else
  1090. {
  1091. rp = p->res + p->ri;
  1092. ++p->ri;
  1093. }
  1094. }
  1095. rp->locIdx = locIdx;
  1096. rp->scEvtIdx = scEvtIdx;
  1097. rp->mni = mp->mni;
  1098. rp->muid = mp->muid;
  1099. rp->smpIdx = mp->smpIdx;
  1100. rp->pitch = mp->pitch;
  1101. rp->vel = mp->vel;
  1102. rp->flags = flags | (tpFl ? kSmTruePosFl : 0) | (fpFl ? kSmFalsePosFl : 0);
  1103. if( p->cbFunc != NULL )
  1104. p->cbFunc(p,p->cbArg,rp);
  1105. }
  1106. void cmScMatcherPrintPath( cmScMatcher* p )
  1107. {
  1108. _cmScMatchPrintPath(p->mp, p->mp->p_opt, p->begSyncLocIdx, p->midiBuf );
  1109. }
  1110. unsigned cmScMatcherScan( cmScMatcher* p, unsigned bli, unsigned hopCnt )
  1111. {
  1112. assert( p->mp != NULL && p->mp->mmn > 0 );
  1113. unsigned i_opt = cmInvalidIdx;
  1114. double s_opt = DBL_MAX;
  1115. cmRC_t rc = cmOkRC;
  1116. unsigned i;
  1117. // initialize the internal values set by this function
  1118. p->missCnt = 0;
  1119. p->eli = cmInvalidIdx;
  1120. p->s_opt = DBL_MAX;
  1121. // if the MIDI buf is not full
  1122. if( p->mbi != 0 )
  1123. return cmInvalidIdx;
  1124. // calc the edit distance from pitchV[] to a sliding score window
  1125. for(i=0; rc==cmOkRC && (hopCnt==cmInvalidCnt || i<hopCnt); ++i)
  1126. {
  1127. rc = cmScMatchExec(p->mp, bli + i, p->mp->msn, p->midiBuf, p->mp->mmn, s_opt );
  1128. switch(rc)
  1129. {
  1130. case cmOkRC: // normal result
  1131. if( p->mp->opt_cost < s_opt )
  1132. {
  1133. s_opt = p->mp->opt_cost;
  1134. i_opt = bli + i;
  1135. }
  1136. break;
  1137. case cmEofRC: // score window encountered the end of the score
  1138. break;
  1139. default: // error state
  1140. return cmInvalidIdx;
  1141. }
  1142. }
  1143. // store the cost assoc'd with i_opt
  1144. p->s_opt = s_opt;
  1145. if( i_opt == cmInvalidIdx )
  1146. return cmInvalidIdx;
  1147. // set the locIdx field in midiBuf[], trailing miss count and
  1148. // return the latest positive-match locIdx
  1149. p->eli = cmScMatchDoSync(p->mp,i_opt,p->midiBuf,p->mp->mmn,&p->missCnt);
  1150. // if no positive matches were found
  1151. if( p->eli == cmInvalidIdx )
  1152. i_opt = cmInvalidIdx;
  1153. else
  1154. {
  1155. cmScMatchPath_t* cp;
  1156. // record result
  1157. for(cp=p->mp->p_opt; cp!=NULL; cp=cp->next)
  1158. if( cp->code != kSmInsIdx )
  1159. _cmScMatcherStoreResult(p, cp->locIdx, cp->scEvtIdx, cp->flags, p->midiBuf + cp->ri - 1);
  1160. }
  1161. return i_opt;
  1162. }
  1163. cmRC_t cmScMatcherStep( cmScMatcher* p )
  1164. {
  1165. int i;
  1166. unsigned pitch = p->midiBuf[ p->mn-1 ].pitch;
  1167. unsigned locIdx = cmInvalidIdx;
  1168. unsigned pidx = cmInvalidIdx;
  1169. // the tracker must be sync'd to step
  1170. if( p->eli == cmInvalidIdx )
  1171. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The p->eli value must be valid to perform a step operation.");
  1172. // if the end of the score has been reached
  1173. if( p->eli + 1 >= p->mp->locN )
  1174. return cmEofRC;
  1175. // attempt to match to next location first
  1176. if( (pidx = _cmScMatchIsMatchIndex(p->mp->loc + p->eli + 1, pitch)) != cmInvalidIdx )
  1177. {
  1178. locIdx = p->eli + 1;
  1179. }
  1180. else
  1181. {
  1182. //
  1183. for(i=2; i<p->stepCnt; ++i)
  1184. {
  1185. // go forward
  1186. if( p->eli+i < p->mp->locN && (pidx=_cmScMatchIsMatchIndex(p->mp->loc + p->eli + i, pitch))!=cmInvalidIdx )
  1187. {
  1188. locIdx = p->eli + i;
  1189. break;
  1190. }
  1191. // go backward
  1192. if( p->eli >= (i-1) && (pidx=_cmScMatchIsMatchIndex(p->mp->loc + p->eli - (i-1), pitch))!=cmInvalidIdx )
  1193. {
  1194. locIdx = p->eli - (i-1);
  1195. break;
  1196. }
  1197. }
  1198. }
  1199. unsigned scEvtIdx = locIdx==cmInvalidIdx ? cmInvalidIdx : p->mp->loc[locIdx].evtV[pidx].scEvtIdx;
  1200. p->midiBuf[ p->mn-1 ].locIdx = locIdx;
  1201. p->midiBuf[ p->mn-1 ].scEvtIdx = scEvtIdx;
  1202. if( locIdx == cmInvalidIdx )
  1203. ++p->missCnt;
  1204. else
  1205. {
  1206. p->missCnt = 0;
  1207. p->eli = locIdx;
  1208. }
  1209. // store the result
  1210. _cmScMatcherStoreResult(p, locIdx, scEvtIdx, locIdx!=cmInvalidIdx ? kSmMatchFl : 0, p->midiBuf + p->mn - 1);
  1211. if( p->missCnt >= p->maxMissCnt )
  1212. {
  1213. unsigned begScanLocIdx = p->eli > p->mn ? p->eli - p->mn : 0;
  1214. p->s_opt = DBL_MAX;
  1215. unsigned bli = cmScMatcherScan(p,begScanLocIdx,p->mn*2);
  1216. ++p->scanCnt;
  1217. // if the scan failed find a match
  1218. if( bli == cmInvalidIdx )
  1219. return cmCtxRtCondition( &p->obj, cmSubSysFailRC, "Scan resync. failed.");
  1220. }
  1221. return cmOkRC;
  1222. }
  1223. cmRC_t cmScMatcherExec( cmScMatcher* p, unsigned smpIdx, unsigned muid, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1, unsigned* scLocIdxPtr )
  1224. {
  1225. bool fl = p->mbi > 0;
  1226. cmRC_t rc = cmOkRC;
  1227. unsigned org_eli = p->eli;
  1228. if( scLocIdxPtr != NULL )
  1229. *scLocIdxPtr = cmInvalidIdx;
  1230. // update the MIDI buffer with the incoming note
  1231. if( cmScMatcherInputMidi(p,smpIdx,muid,status,d0,d1) == false )
  1232. return rc;
  1233. // if the MIDI buffer transitioned to full then perform an initial scan sync.
  1234. if( fl && p->mbi == 0 )
  1235. {
  1236. if( (p->begSyncLocIdx = cmScMatcherScan(p,p->ili,p->initHopCnt)) == cmInvalidIdx )
  1237. {
  1238. rc = cmInvalidArgRC; // signal init. scan sync. fail
  1239. }
  1240. else
  1241. {
  1242. //cmScMatcherPrintPath(p);
  1243. }
  1244. }
  1245. else
  1246. {
  1247. // if the MIDI buffer is full then perform a step sync.
  1248. if( !fl && p->mbi == 0 )
  1249. rc = cmScMatcherStep(p);
  1250. }
  1251. // if we lost sync
  1252. if( p->eli == cmInvalidIdx )
  1253. {
  1254. // IF WE LOST SYNC THEN WE BETTER DO SOMETHING - LIKE INCREASE THE SCAN HOPS
  1255. // ON THE NEXT EVENT.
  1256. p->eli = org_eli;
  1257. }
  1258. else
  1259. {
  1260. if( scLocIdxPtr!=NULL && p->eli != org_eli )
  1261. {
  1262. //printf("LOC:%i bar:%i\n",p->eli,p->mp->loc[p->eli].barNumb);
  1263. *scLocIdxPtr = p->mp->loc[p->eli].scLocIdx;
  1264. }
  1265. }
  1266. return rc;
  1267. }
  1268. double cmScMatcherFMeas( cmScMatcher* p )
  1269. {
  1270. unsigned bli = p->mp->locN;
  1271. unsigned eli = 0;
  1272. unsigned scNoteCnt = 0; // total count of score notes
  1273. unsigned matchCnt = 0; // count of matched notes (true positives)
  1274. unsigned wrongCnt = 0; // count of incorrect notes (false positives)
  1275. unsigned missCnt = 0; // count of missed score notes (false negatives)
  1276. unsigned i;
  1277. for(i=0; i<p->ri; ++i)
  1278. if( p->res[i].locIdx != cmInvalidIdx )
  1279. {
  1280. bli = cmMin(bli,p->res[i].locIdx);
  1281. eli = cmMax(eli,p->res[i].locIdx);
  1282. if( cmIsFlag(p->res[i].flags,kSmTruePosFl) )
  1283. ++matchCnt;
  1284. if( cmIsFlag(p->res[i].flags,kSmFalsePosFl) )
  1285. ++wrongCnt;
  1286. }
  1287. scNoteCnt = eli - bli + 1;
  1288. missCnt = scNoteCnt - matchCnt;
  1289. double prec = (double)2.0 * matchCnt / (matchCnt + wrongCnt);
  1290. double rcal = (double)2.0 * matchCnt / (matchCnt + missCnt);
  1291. double fmeas = prec * rcal / (prec + rcal);
  1292. //printf("total:%i match:%i wrong:%i miss:%i\n",scNoteCnt,matchCnt,wrongCnt,missCnt);
  1293. return fmeas;
  1294. }
  1295. typedef struct cmScMatcherPrint_str
  1296. {
  1297. unsigned flags;
  1298. unsigned scLocIdx;
  1299. unsigned mni;
  1300. unsigned pitch;
  1301. unsigned vel;
  1302. unsigned barNumb;
  1303. } cmScMatcherPrint_t;
  1304. void _cmScMatcherInsertPrint(cmScMatcherPrint_t* a, unsigned i, unsigned* anp, unsigned aan, const cmScMatcherResult_t* rp, unsigned scLocIdx )
  1305. {
  1306. assert( *anp + 1 <= aan );
  1307. memmove(a + i + 1, a + i, (*anp-i)*sizeof(cmScMatcherPrint_t));
  1308. memset( a + i, 0, sizeof(cmScMatcherPrint_t));
  1309. *anp += 1;
  1310. a[i].flags = rp->flags;
  1311. a[i].scLocIdx = scLocIdx;
  1312. a[i].mni = rp->mni;
  1313. a[i].pitch = rp->pitch;
  1314. a[i].vel = rp->vel;
  1315. }
  1316. void cmScMatcherPrint( cmScMatcher* p )
  1317. {
  1318. unsigned bsli = cmScoreEvtCount(p->mp->scH);
  1319. unsigned esli = 0;
  1320. unsigned i,j,k;
  1321. // get first/last scLocIdx from res[] - this is the range of
  1322. // score events that the score matcher has identified
  1323. for(i=0; i<p->ri; ++i)
  1324. if( p->res[i].locIdx != cmInvalidIdx )
  1325. {
  1326. bsli = cmMin(bsli,p->mp->loc[p->res[i].locIdx].scLocIdx);
  1327. esli = cmMax(esli,p->mp->loc[p->res[i].locIdx].scLocIdx);
  1328. }
  1329. unsigned an = 0;
  1330. unsigned aan = p->ri;
  1331. // calc the count of score events between bsli and esli.
  1332. for(i=bsli; i<=esli; ++i)
  1333. {
  1334. cmScoreLoc_t* lp = cmScoreLoc(p->mp->scH, i);
  1335. assert(lp != NULL);
  1336. aan += lp->evtCnt;
  1337. }
  1338. // allocate an array of 'aan' print records
  1339. cmScMatcherPrint_t* a = cmMemAllocZ(cmScMatcherPrint_t,aan);
  1340. // fill the cmScMatcherPrint_t array with note and bar events from the score
  1341. for(i=bsli; i<=esli; ++i)
  1342. {
  1343. unsigned scLocIdx = i;
  1344. cmScoreLoc_t* lp = cmScoreLoc(p->mp->scH, scLocIdx );
  1345. // for each score event which occurs at this location
  1346. for(j=0; j<lp->evtCnt; ++j)
  1347. {
  1348. assert( an < aan );
  1349. cmScoreEvt_t* ep = lp->evtArray[j];
  1350. cmScMatcherPrint_t* pp = a + an;
  1351. an += 1;
  1352. switch( ep->type )
  1353. {
  1354. case kBarEvtScId:
  1355. pp->flags = kSmBarFl;
  1356. break;
  1357. case kNonEvtScId:
  1358. pp->flags = kSmNoteFl;
  1359. break;
  1360. }
  1361. pp->scLocIdx = scLocIdx;
  1362. pp->mni = cmInvalidIdx;
  1363. pp->pitch = ep->pitch;
  1364. pp->vel = kInvalidMidiVelocity;
  1365. pp->barNumb = ep->barNumb;
  1366. }
  1367. }
  1368. //
  1369. // a[an] now contains a record for each note and bar event in the
  1370. // time range associated with the score matcher's result array.
  1371. //
  1372. // for each result record
  1373. for(i=0; i<p->ri; ++i)
  1374. {
  1375. cmScMatcherResult_t* rp = p->res + i;
  1376. // if this result recd matched a score event
  1377. if( cmIsFlag(rp->flags,kSmTruePosFl) )
  1378. {
  1379. // locate the matching score event in a[an]
  1380. for(k=0; k<an; ++k)
  1381. if( a[k].scLocIdx==p->mp->loc[rp->locIdx].scLocIdx && a[k].pitch==rp->pitch )
  1382. {
  1383. a[k].mni = rp->mni;
  1384. a[k].vel = rp->vel;
  1385. a[k].flags |= kSmMatchFl;
  1386. break;
  1387. }
  1388. }
  1389. // if this result did not match a score event
  1390. if( cmIsFlag(rp->flags,kSmFalsePosFl) )
  1391. {
  1392. unsigned d_min = 0;
  1393. cmScMatcherPrint_t* dp = NULL;
  1394. unsigned scLocIdx = cmInvalidIdx;
  1395. // if this result does not have a valid locIdx
  1396. // (e.g. errant MIDI notes: scan:'delete' note or a step:mis-match note)
  1397. if( rp->locIdx == cmInvalidIdx )
  1398. {
  1399. // find the print recd with the closet 'mni'
  1400. for(k=0; k<an; ++k)
  1401. if( a[k].mni != cmInvalidIdx )
  1402. {
  1403. unsigned d;
  1404. if( a[k].mni > rp->mni )
  1405. d = a[k].mni - rp->mni;
  1406. else
  1407. d = rp->mni - a[k].mni;
  1408. if( dp==NULL || d < d_min )
  1409. {
  1410. dp = a + k;
  1411. d_min = d;
  1412. }
  1413. }
  1414. k = dp - a;
  1415. assert( k < an );
  1416. scLocIdx = p->mp->loc[k].scLocIdx;
  1417. if( a[k].mni < rp->mni )
  1418. ++k;
  1419. }
  1420. else // result w/ a valid locIdx (e.g. scan 'substitute' with no match)
  1421. {
  1422. scLocIdx = p->mp->loc[rp->locIdx].scLocIdx;
  1423. // find the print recd with the closest scIdx
  1424. for(k=0; k<an; ++k)
  1425. if( a[k].scLocIdx != cmInvalidIdx )
  1426. {
  1427. unsigned d;
  1428. if( a[k].scLocIdx > scLocIdx )
  1429. d = a[k].scLocIdx - scLocIdx;
  1430. else
  1431. d = scLocIdx - a[k].scLocIdx;
  1432. if( dp==NULL || d < d_min )
  1433. {
  1434. dp = a + k;
  1435. d_min = d;
  1436. }
  1437. }
  1438. k = dp - a;
  1439. assert( k < an );
  1440. if( a[k].scLocIdx < scLocIdx )
  1441. ++k;
  1442. }
  1443. // create a new print recd to represent the false-positive result recd
  1444. assert( dp != NULL );
  1445. _cmScMatcherInsertPrint(a, k, &an,aan,rp,scLocIdx);
  1446. }
  1447. }
  1448. printf("sloc bar mni ptch flag\n");
  1449. printf("---- ---- ---- ---- ----\n");
  1450. for(i=0; i<an; ++i)
  1451. {
  1452. printf("%4i %4i %4i %4s %c%c%c\n",a[i].scLocIdx,a[i].barNumb,a[i].mni,
  1453. cmIsFlag(a[i].flags,kSmBarFl) ? "|" : cmMidiToSciPitch(a[i].pitch,NULL,0),
  1454. cmIsFlag(a[i].flags,kSmNoteFl) ? 'n' : ' ',
  1455. cmIsFlag(a[i].flags,kSmMatchFl) ? 'm' : (cmIsFlag(a[i].flags,kSmTransFl) ? 't' : ' '),
  1456. cmIsFlag(a[i].flags,kSmFalsePosFl) ? '*' : ' '
  1457. );
  1458. }
  1459. cmMemFree(a);
  1460. }
  1461. //=======================================================================================================================
  1462. cmScMeas* cmScMeasAlloc( cmCtx* c, cmScMeas* p, cmScH_t scH, double srate, const unsigned* dynRefArray, unsigned dynRefCnt )
  1463. {
  1464. cmScMeas* op = cmObjAlloc(cmScMeas,c,p);
  1465. op->mp = cmScMatchAlloc( c, NULL, cmScNullHandle, 0, 0 );
  1466. if( cmScoreIsValid(scH) )
  1467. if( cmScMeasInit(op,scH,srate,dynRefArray,dynRefCnt) != cmOkRC )
  1468. cmScMeasFree(&op);
  1469. return op;
  1470. }
  1471. cmRC_t cmScMeasFree( cmScMeas** pp )
  1472. {
  1473. cmRC_t rc = cmOkRC;
  1474. if( pp==NULL || *pp==NULL )
  1475. return rc;
  1476. cmScMeas* p = *pp;
  1477. if((rc = cmScMeasFinal(p)) != cmOkRC )
  1478. return rc;
  1479. cmScMatchFree(&p->mp);
  1480. cmMemFree(p->midiBuf);
  1481. cmMemFree(p->set);
  1482. cmMemFree(p->dynRef);
  1483. cmObjFree(pp);
  1484. return rc;
  1485. }
  1486. void _cmScMeasPrint( cmScMeas* p )
  1487. {
  1488. unsigned i;
  1489. for(i=0; i<p->sn; ++i)
  1490. {
  1491. cmScMeasSet_t* sp = p->set + i;
  1492. printf("%4i: sli:%4i %4i li:%4i %4i\n", i, sp->bsli, sp->esli, sp->bli, sp->eli );
  1493. }
  1494. }
  1495. int _cmScMeasSortFunc( const void* p0, const void* p1 )
  1496. {
  1497. const cmScMeasSet_t* s0 = (const cmScMeasSet_t*)p0;
  1498. const cmScMeasSet_t* s1 = (const cmScMeasSet_t*)p1;
  1499. return s0->esli - s1->esli;
  1500. }
  1501. cmRC_t cmScMeasInit( cmScMeas* p, cmScH_t scH, double srate, const unsigned* dynRefArray, unsigned dynRefCnt )
  1502. {
  1503. cmRC_t rc;
  1504. unsigned i,j;
  1505. unsigned si;
  1506. unsigned maxScWndN = 0;
  1507. if((rc = cmScMeasFinal(p)) != cmOkRC )
  1508. return rc;
  1509. p->mii = 0;
  1510. p->mn = 2 * cmScoreEvtCount(scH);
  1511. p->midiBuf = cmMemResizeZ(cmScMatchMidi_t,p->midiBuf,p->mn);
  1512. p->sn = cmScoreSetCount(scH);
  1513. p->set = cmMemResizeZ(cmScMeasSet_t,p->set,p->sn);
  1514. p->dynRef = cmMemResizeZ(unsigned,p->dynRef,dynRefCnt);
  1515. p->dn = dynRefCnt;
  1516. p->srate = srate;
  1517. memcpy(p->dynRef,dynRefArray,sizeof(dynRefArray[0])*dynRefCnt);
  1518. unsigned n = cmScoreLocCount(scH);
  1519. // for each score location
  1520. for(i=0,si=0; i<n; ++i)
  1521. {
  1522. cmScoreLoc_t* lp = cmScoreLoc(scH,i);
  1523. cmScoreSet_t* sp = lp->setList;
  1524. // for each set that ends on this score location
  1525. for(; sp!=NULL; sp=sp->llink,++si)
  1526. {
  1527. assert(si < p->sn);
  1528. cmScMeasSet_t* msp = p->set + si;
  1529. msp->sp = sp;
  1530. msp->bsli = cmScoreLocCount(scH);
  1531. msp->esli = 0;
  1532. msp->bsei = cmScoreEvtCount(scH);
  1533. msp->esei = 0;
  1534. msp->bli = cmInvalidIdx;
  1535. msp->eli = cmInvalidIdx;
  1536. for(j=0; j<sp->eleCnt; ++j)
  1537. {
  1538. msp->bsli = cmMin(msp->bsli,sp->eleArray[j]->locIdx);
  1539. msp->esli = cmMax(msp->esli,sp->eleArray[j]->locIdx);
  1540. msp->bsei = cmMin(msp->bsei,sp->eleArray[j]->index);
  1541. msp->esei = cmMax(msp->esei,sp->eleArray[j]->index);
  1542. }
  1543. }
  1544. }
  1545. // initialize p->mp so that mp->loc[] is loaded - use dummy scWndN and midiN
  1546. if((rc = cmScMatchInit(p->mp, scH, 11, 10 )) != cmOkRC )
  1547. return rc;
  1548. // assign set[].bli and set[].eli
  1549. // for each measurment set
  1550. for(j=0; j<p->sn; ++j)
  1551. {
  1552. cmScMeasSet_t* msp = p->set + j;
  1553. // for each score location
  1554. for(i=0; i<p->mp->locN; ++i)
  1555. {
  1556. // if this set starts on this score location
  1557. if( msp->bli==cmInvalidIdx && msp->bsli==p->mp->loc[i].scLocIdx )
  1558. msp->bli = i; // store the index of the score location
  1559. // if this set end on this score location
  1560. if( msp->esli==p->mp->loc[i].scLocIdx )
  1561. msp->eli = i; // store the index of the score location
  1562. }
  1563. assert( msp->eli > msp->bli );
  1564. // track the longest set.
  1565. maxScWndN = cmMax( maxScWndN, msp->eli - msp->bli + 1 );
  1566. }
  1567. // setup the match
  1568. if((rc = cmScMatchInit(p->mp, scH, 2*maxScWndN+1, 2*maxScWndN )) != cmOkRC )
  1569. return rc;
  1570. // sort set[] on cmScMeasSet_t.esli
  1571. qsort(p->set, p->sn, sizeof(cmScMeasSet_t), _cmScMeasSortFunc );
  1572. //_cmScMeasPrint(p);
  1573. cmScMeasReset(p);
  1574. return rc;
  1575. }
  1576. cmRC_t cmScMeasFinal( cmScMeas* p )
  1577. { return cmScMatchFinal(p->mp); }
  1578. cmRC_t cmScMeasReset( cmScMeas* p )
  1579. {
  1580. cmRC_t rc = cmOkRC;
  1581. p->mii = 0;
  1582. p->nsi = cmInvalidIdx;
  1583. p->vsi = cmInvalidIdx;
  1584. p->nsli = cmInvalidIdx;
  1585. unsigned i;
  1586. for(i=0; i<p->sn; ++i)
  1587. {
  1588. p->set[i].value = DBL_MAX;
  1589. p->set[i].tempo = 0;
  1590. p->set[i].match_cost = 0;
  1591. }
  1592. return rc;
  1593. }
  1594. typedef struct
  1595. {
  1596. unsigned scLocIdx; // score loc index
  1597. double frac; // score based fraction of beat
  1598. unsigned smpIdx; // time of assoc'd MIDI event
  1599. unsigned cnt; //
  1600. double val; //
  1601. unsigned pitch;
  1602. } _cmScMeasTimeEle_t;
  1603. typedef struct
  1604. {
  1605. unsigned setN; // set length
  1606. unsigned midiN; // count of MIDI events to match to score
  1607. unsigned alignN; // count of score events in the alignment (<= setN)
  1608. unsigned matchN; // count of positive matches
  1609. double tempo;
  1610. double value;
  1611. } _cmScMeasResult_t;
  1612. double _cmScMeasCalcTempo( const _cmScMeasTimeEle_t* b, unsigned bn, double srate )
  1613. {
  1614. assert( bn >= 2 );
  1615. assert( b[bn-1].smpIdx != cmInvalidIdx );
  1616. assert( b[0].smpIdx != cmInvalidIdx );
  1617. double durSmpCnt = b[bn-1].smpIdx - b[0].smpIdx;
  1618. double beats = 0;
  1619. unsigned i;
  1620. for(i=0; i<bn; ++i)
  1621. beats += b[i].frac;
  1622. assert(beats != 0);
  1623. return beats / (durSmpCnt / (srate * 60.0));
  1624. }
  1625. // Note: On successful completion (return!=0) the first
  1626. // and last record returned in c[cn] will be matched records.
  1627. unsigned _cmScMeasTimeAlign( cmScMeas* p, cmScMeasSet_t* sp, cmScMatchMidi_t* m, unsigned mn, _cmScMeasTimeEle_t* c, unsigned cn, _cmScMeasResult_t* rp )
  1628. {
  1629. int i,j,k;
  1630. int an = sp->sp->eleCnt;
  1631. _cmScMeasTimeEle_t* b = NULL;
  1632. int bn = 0;
  1633. bool tempoFl = false;
  1634. unsigned matchN = 0;
  1635. assert( an!=0);
  1636. // alloc a 'score set' element array
  1637. _cmScMeasTimeEle_t a[an];
  1638. // get the scLocIdx of each set element from the score
  1639. for(i=0,j=0; i<an; ++i)
  1640. if( i==0 || sp->sp->eleArray[i-1]->locIdx != sp->sp->eleArray[i]->locIdx )
  1641. {
  1642. assert( sp->sp->eleArray[i]->locIdx != cmInvalidIdx );
  1643. a[j].scLocIdx = sp->sp->eleArray[i]->locIdx;
  1644. a[j].frac = sp->sp->eleArray[i]->frac;
  1645. a[j].smpIdx = cmInvalidIdx;
  1646. a[j].cnt = 0;
  1647. ++j;
  1648. }
  1649. an = j; // set the count of unique score locations (new length of a[])
  1650. // collect the 'smpIdx' for each MIDI event which matches a set element
  1651. for(i=0; i<mn; ++i)
  1652. if( m[i].locIdx != cmInvalidIdx )
  1653. {
  1654. for(j=0; j<an; ++j)
  1655. if( p->mp->loc[m[i].locIdx].scLocIdx == a[j].scLocIdx )
  1656. {
  1657. a[j].smpIdx += m[i].smpIdx;
  1658. a[j].cnt += 1;
  1659. a[j].pitch = m[i].pitch;
  1660. if( a[j].cnt == 1 )
  1661. matchN += 1; // only cnt one match per sc loc.
  1662. break;
  1663. }
  1664. }
  1665. // remove leading missing values
  1666. for(i=0; i<an; ++i)
  1667. if( a[i].smpIdx != cmInvalidIdx )
  1668. {
  1669. b = a + i;
  1670. bn = an - i;
  1671. break;
  1672. }
  1673. // remove trailing missing values
  1674. for(i=bn-1; i>=0; --i,--bn)
  1675. if( b[i].smpIdx != cmInvalidIdx )
  1676. break;
  1677. // can't measure evenness against less than 2 values
  1678. if( bn < 2 )
  1679. {
  1680. return 0;
  1681. }
  1682. assert(b[0].smpIdx != cmInvalidIdx && b[bn-1].smpIdx != cmInvalidIdx);
  1683. // calc avg. smpIdx, insert missing values, and convert b[].smpIdx to delta smp index
  1684. for(i=0,j=0; i<bn; ++i)
  1685. {
  1686. if( b[i].cnt > 1 )
  1687. b[i].smpIdx /= b[i].cnt;
  1688. if( b[i].smpIdx == cmInvalidIdx )
  1689. ++j; // incr missing value count
  1690. else
  1691. {
  1692. if( i > 0 )
  1693. {
  1694. // fill in missing values
  1695. ++j;
  1696. unsigned d = (b[i].smpIdx - b[i-j].smpIdx)/j;
  1697. for(k=0; k<j; ++k)
  1698. b[i-j+k].val = d;
  1699. }
  1700. j=0;
  1701. }
  1702. if( b[i].frac != 0 )
  1703. tempoFl = true;
  1704. }
  1705. rp->setN = an;
  1706. rp->midiN = mn;
  1707. rp->alignN = bn;
  1708. rp->matchN = matchN;
  1709. rp->tempo = 0;
  1710. rp->value = 0;
  1711. // calculate tempo
  1712. if( tempoFl )
  1713. rp->tempo = _cmScMeasCalcTempo(b,bn,p->srate);
  1714. assert(bn<=cn);
  1715. // TODO: this copy should be eliminated
  1716. // copy to output
  1717. for(i=0; i<bn && i<cn; ++i)
  1718. c[i] = b[i];
  1719. return bn;
  1720. }
  1721. double _cmScMeasEven( cmScMeas* p, cmScMeasSet_t* sp, cmScMatchMidi_t* m, unsigned mn, _cmScMeasResult_t* rp )
  1722. {
  1723. unsigned bn = sp->sp->eleCnt;
  1724. _cmScMeasTimeEle_t b[bn];
  1725. unsigned i;
  1726. if((bn = _cmScMeasTimeAlign(p,sp,m,mn,b,bn,rp)) == 0 )
  1727. return DBL_MAX;
  1728. // calc avg. delta time
  1729. double d_avg = 0;
  1730. for(i=0; i<bn-1; ++i)
  1731. {
  1732. d_avg += b[i].val;
  1733. //printf("loc:%i %f\n",b[i].scLocIdx,b[i].val);
  1734. }
  1735. d_avg /= (bn-1);
  1736. // calc std-dev of delta time
  1737. double d_sd = 0;
  1738. for(i=0; i<bn-1; ++i)
  1739. d_sd += (b[i].val-d_avg) * (b[i].val-d_avg);
  1740. // if there is no deviation then we can't compute a z-score
  1741. // (this will happen if we fill in all the missing values based on 2 values)
  1742. if( d_sd == 0 )
  1743. return 1.0;
  1744. d_sd = sqrt(d_sd/(bn-1));
  1745. // calc avg. z-score
  1746. double z = 0;
  1747. for(i=0; i<bn-1; ++i)
  1748. z += fabs(b[i].val - d_avg)/d_sd;
  1749. double val = z / (bn-1);
  1750. rp->value = val;
  1751. return val;
  1752. }
  1753. // return Tempo estimation in BPM
  1754. double _cmScMeasTempo( cmScMeas* p, cmScMeasSet_t* sp, cmScMatchMidi_t* m, unsigned mn, _cmScMeasResult_t* rp )
  1755. {
  1756. unsigned bn = sp->sp->eleCnt;
  1757. _cmScMeasTimeEle_t b[bn];
  1758. if((bn= _cmScMeasTimeAlign(p,sp,m,mn,b,bn,rp)) == 0 )
  1759. return DBL_MAX;
  1760. return rp->tempo;
  1761. }
  1762. double _cmScMeasDyn( cmScMeas* p, cmScMeasSet_t* sp, cmScMatchMidi_t* m, unsigned mn, _cmScMeasResult_t* rp )
  1763. {
  1764. typedef struct
  1765. {
  1766. unsigned scEvtIdx;
  1767. unsigned vel;
  1768. double val;
  1769. } ele_t;
  1770. int i,j;
  1771. int n = sp->sp->eleCnt;
  1772. double vv = 0;
  1773. unsigned vn = 0;
  1774. unsigned matchN = 0;
  1775. unsigned alignN = 0;
  1776. assert( n!=0);
  1777. ele_t a[n];
  1778. // get the scEvtIdx of each set element
  1779. for(i=0; i<n; ++i)
  1780. {
  1781. cmScoreEvt_t* ep = cmScoreEvt( p->mp->scH, sp->sp->eleArray[i]->index );
  1782. assert( ep != NULL );
  1783. a[i].scEvtIdx = sp->sp->eleArray[i]->index;
  1784. a[i].vel = 0;
  1785. a[i].val = ep->dynVal;
  1786. }
  1787. // set the performed vel. of each note in the set
  1788. // (if a note was not played it's a[].vel is left at 0)
  1789. for(i=0; i<mn; ++i)
  1790. if( m[i].scEvtIdx != cmInvalidIdx )
  1791. {
  1792. alignN += 1;
  1793. for(j=0; j<n; ++j)
  1794. if( m[i].scEvtIdx == a[j].scEvtIdx )
  1795. {
  1796. matchN += 1;
  1797. a[j].vel = m[i].vel;
  1798. break;
  1799. }
  1800. }
  1801. // assign a dynamics category to each note in the set
  1802. for(i=0; i<n; ++i)
  1803. if( a[i].vel > 0 )
  1804. {
  1805. unsigned mnv = 0; // lower bound for first dyn's category
  1806. for(j=0; j<p->dn; ++j)
  1807. {
  1808. if( mnv <= a[i].vel && a[i].vel < p->dynRef[j] )
  1809. {
  1810. // accum. the diff. between the ref. and performed dyn. category
  1811. vv += fabs(a[i].val - j);
  1812. vn += 1;
  1813. break;
  1814. }
  1815. mnv = p->dynRef[j];
  1816. }
  1817. assert(j<p->dn);
  1818. }
  1819. rp->setN = n;
  1820. rp->midiN = mn;
  1821. rp->alignN = alignN;
  1822. rp->matchN = matchN;
  1823. rp->tempo = 0;
  1824. rp->value = vn == 0 ? DBL_MAX : vv/vn;
  1825. return rp->value;
  1826. }
  1827. unsigned MEAS_MATCH_CNT = 0;
  1828. void _cmScMeasPrintResult( cmScMeas* p, cmScMeasSet_t* sp, _cmScMeasResult_t* rp, unsigned bli, const cmScMatchMidi_t* mb )
  1829. {
  1830. const char* label = "<none>";
  1831. switch( sp->sp->varId )
  1832. {
  1833. case kEvenVarScId:
  1834. label = "even";
  1835. break;
  1836. case kDynVarScId:
  1837. label = "dyn";
  1838. break;
  1839. case kTempoVarScId:
  1840. label = "tempo";
  1841. break;
  1842. }
  1843. const cmChar_t* msg = "";
  1844. if( rp->value == DBL_MAX )
  1845. {
  1846. msg = "Measure FAILED.";
  1847. sp->value = 0;
  1848. }
  1849. printf("%i set:%i %s bsli:%i esli:%i [set:%i match:%i] cost:%f val:%f %s",MEAS_MATCH_CNT, p->nsi, label, sp->bsli, sp->esli, rp->setN, rp->matchN, p->mp->opt_cost, sp->value, msg);
  1850. if( rp->tempo != 0 )
  1851. printf(" tempo:%f ",rp->tempo);
  1852. printf("\n");
  1853. _cmScMatchPrintPath(p->mp, p->mp->p_opt, bli, mb );
  1854. }
  1855. void _cmScMeasCalcVal( cmScMeas* p, cmScMeasSet_t* sp, int n_mii )
  1856. {
  1857. unsigned mn = 0;
  1858. int i,k = cmInvalidIdx;
  1859. if( n_mii == 0 )
  1860. return;
  1861. // Determine the count of MIDI notes to match to the set score
  1862. // by searching from the MIDI note just recieved (midiBuf[n_mii]
  1863. // back toward the beginning until a MIDI event which occurs just
  1864. // prior to the set's begScLocIdx.
  1865. for(i=n_mii; i>=0; --i)
  1866. {
  1867. if( p->midiBuf[i].locIdx != cmInvalidIdx )
  1868. {
  1869. k = i;
  1870. unsigned scLocIdx = p->mp->loc[ p->midiBuf[i].locIdx ].scLocIdx;
  1871. if( scLocIdx < sp->bsli )
  1872. break;
  1873. }
  1874. }
  1875. assert(k != cmInvalidIdx);
  1876. mn = n_mii - k + 1;
  1877. i = k;
  1878. assert(i>=0);
  1879. assert(mn>0);
  1880. // Create a copy of the the MIDI buffer to prevent the
  1881. // p->midiBuf[].locIdx from being overwritten by cmScMatchDoSync().
  1882. cmScMatchMidi_t mb[ mn ];
  1883. unsigned j;
  1884. for(j=0; j<mn; ++j)
  1885. {
  1886. mb[j] = p->midiBuf[i+j];
  1887. mb[j].locIdx = cmInvalidIdx;
  1888. }
  1889. // In general the first and last MIDI event should be assigned
  1890. // to a score location - it's possible however that no MIDI
  1891. // event's prior to the one at p->midiBuf[n_mii] were assigned.
  1892. assert( (i==0 || p->midiBuf[i].locIdx!=cmInvalidIdx) && p->midiBuf[i+mn-1].locIdx != cmInvalidIdx);
  1893. unsigned l0i = cmMin(p->midiBuf[i].locIdx,p->midiBuf[i+mn-1].locIdx);
  1894. unsigned l1i = cmMax(p->midiBuf[i].locIdx,p->midiBuf[i+mn-1].locIdx);
  1895. unsigned bli = l0i;
  1896. unsigned ln = l1i - bli + 1;
  1897. double min_cost = DBL_MAX;
  1898. _cmScMeasResult_t r;
  1899. memset(&r,0,sizeof(r));
  1900. // match MIDI to score
  1901. if( cmScMatchExec(p->mp, bli, ln, mb, mn, min_cost ) != cmOkRC )
  1902. return;
  1903. // sync the score and MIDI based on the match information
  1904. if( cmScMatchDoSync(p->mp, bli, mb, mn, NULL ) == cmInvalidIdx )
  1905. return;
  1906. if( p->mp->opt_cost != DBL_MAX )
  1907. sp->match_cost = p->mp->opt_cost / sp->sp->eleCnt;
  1908. switch( sp->sp->varId )
  1909. {
  1910. case kEvenVarScId:
  1911. sp->value = _cmScMeasEven(p, sp, mb, mn, &r );
  1912. break;
  1913. case kDynVarScId:
  1914. sp->value = _cmScMeasDyn(p, sp, mb, mn, &r );
  1915. break;
  1916. case kTempoVarScId:
  1917. sp->value = _cmScMeasTempo(p, sp, mb, mn, &r );
  1918. break;
  1919. default:
  1920. { assert(0); }
  1921. }
  1922. sp->tempo = r.tempo;
  1923. // print the result
  1924. //_cmScMeasPrintResult(p, sp, &r, bli, mb );
  1925. MEAS_MATCH_CNT++;
  1926. }
  1927. cmRC_t cmScMeasExec( cmScMeas* p, unsigned mni, unsigned locIdx, unsigned scEvtIdx, unsigned flags, unsigned smpIdx, unsigned pitch, unsigned vel )
  1928. {
  1929. cmRC_t rc = cmOkRC;
  1930. // if the midi buffer is full
  1931. if( p->mii >= p->mn )
  1932. return cmCtxRtCondition( &p->obj, cmEofRC, "The MIDI buffer is full.");
  1933. int n_mii = cmInvalidIdx;
  1934. // locate the MIDI event assoc'd with 'mni' ...
  1935. if( p->mii>0 && mni <= p->midiBuf[p->mii-1].mni )
  1936. {
  1937. if( locIdx != cmInvalidIdx )
  1938. {
  1939. for(n_mii=p->mii-1; n_mii>=0; --n_mii)
  1940. if( p->midiBuf[n_mii].mni == mni )
  1941. break;
  1942. if( n_mii<0 )
  1943. n_mii = cmInvalidIdx;
  1944. }
  1945. }
  1946. else // ... or push a new record onto p->midiBuf[]
  1947. {
  1948. n_mii = p->mii;
  1949. ++p->mii;
  1950. }
  1951. // store the MIDI event
  1952. p->midiBuf[n_mii].mni = mni;
  1953. p->midiBuf[n_mii].locIdx = locIdx;
  1954. p->midiBuf[n_mii].scEvtIdx = scEvtIdx;
  1955. p->midiBuf[n_mii].smpIdx = smpIdx;
  1956. p->midiBuf[n_mii].pitch = pitch;
  1957. p->midiBuf[n_mii].vel = vel;
  1958. // setting vsi=nsi and vsli=nsli will indicate to the calling
  1959. // program that no new sets are ready.
  1960. p->vsi = p->nsi;
  1961. p->vsli = p->nsli;
  1962. if( locIdx == cmInvalidIdx )
  1963. return cmOkRC;
  1964. //
  1965. unsigned scLocIdx = p->mp->loc[ locIdx ].scLocIdx;
  1966. unsigned maxScLocIdx = cmScoreLocCount(p->mp->scH)-1;
  1967. // if this cmScMeas object has not yet synchronized to the cmScMatcher
  1968. // (if p->nsli is not valid)
  1969. if( p->nsli == cmInvalidIdx )
  1970. {
  1971. unsigned i;
  1972. for(i=0; i<p->sn; ++i)
  1973. if( p->set[i].esli+1 == scLocIdx )
  1974. {
  1975. p->nsli = scLocIdx;
  1976. p->nsi = i;
  1977. break;
  1978. }
  1979. if(i==p->sn)
  1980. return rc;
  1981. }
  1982. p->vsi = p->nsi;
  1983. p->vsli = p->nsli;
  1984. // for each cmScore location between p->nsli and scLocIdx
  1985. for(; p->nsli<=scLocIdx && p->nsi < p->sn; ++p->nsli)
  1986. {
  1987. // if this score location index (p->nsli) is one score location
  1988. // ahead of the next sets ending location.
  1989. while( cmMin(maxScLocIdx,p->set[p->nsi].esli+1) == p->nsli )
  1990. {
  1991. // calculate the value assoc'd with p->set[p->nsi]
  1992. _cmScMeasCalcVal(p, p->set + p->nsi, n_mii );
  1993. // advance the set index
  1994. ++p->nsi;
  1995. }
  1996. }
  1997. return rc;
  1998. }
  1999. //=======================================================================================================================
  2000. cmRC_t cmScAlignScanToTimeLineEvent( cmScMatcher* p, cmTlH_t tlH, cmTlObj_t* top, unsigned endSmpIdx )
  2001. {
  2002. assert( top != NULL );
  2003. cmTlMidiEvt_t* mep = NULL;
  2004. cmRC_t rc = cmOkRC;
  2005. // as long as more MIDI events are available get the next MIDI msg
  2006. while( rc==cmOkRC && (mep = cmTlNextMidiEvtObjPtr(tlH, top, top->seqId )) != NULL )
  2007. {
  2008. top = &mep->obj;
  2009. // if the msg falls after the end of the marker then we are through
  2010. if( mep->obj.seqSmpIdx != cmInvalidIdx && mep->obj.seqSmpIdx > endSmpIdx )
  2011. break;
  2012. // if the time line MIDI msg a note-on
  2013. if( (mep->msg->status&0xf0) == kNoteOnMdId )
  2014. {
  2015. rc = cmScMatcherExec(p, mep->obj.seqSmpIdx, mep->msg->uid, mep->msg->status, mep->msg->u.chMsgPtr->d0, mep->msg->u.chMsgPtr->d1, NULL );
  2016. switch( rc )
  2017. {
  2018. case cmOkRC: // continue processing MIDI events
  2019. break;
  2020. case cmEofRC: // end of the score was encountered
  2021. break;
  2022. case cmInvalidArgRC: // p->eli was not set correctly
  2023. break;
  2024. case cmSubSysFailRC: // scan resync failed
  2025. break;
  2026. default:
  2027. { assert(0); }
  2028. }
  2029. }
  2030. }
  2031. if( rc == cmEofRC )
  2032. rc = cmOkRC;
  2033. return rc;
  2034. }
  2035. // This callback connects/feeds the cmScMeas object from the cmScMatcher object.
  2036. // (See _cmScMatcherStoreResult().)
  2037. void cmScMatcherCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
  2038. {
  2039. cmScMeas* mp = (cmScMeas*)arg;
  2040. cmScMeasExec(mp, rp->mni, rp->locIdx, rp->scEvtIdx, rp->flags, rp->smpIdx, rp->pitch, rp->vel );
  2041. }
  2042. void cmScAlignScanMarkers( cmRpt_t* rpt, cmTlH_t tlH, cmScH_t scH )
  2043. {
  2044. unsigned i;
  2045. double srate = cmTimeLineSampleRate(tlH);
  2046. unsigned midiN = 7;
  2047. unsigned scWndN = 10;
  2048. unsigned markN = 291;
  2049. unsigned dynRefArray[] = { 14, 28, 42, 56, 71, 85, 99, 113,128 };
  2050. unsigned dynRefCnt = sizeof(dynRefArray)/sizeof(dynRefArray[0]);
  2051. cmCtx* ctx = cmCtxAlloc(NULL, rpt, cmLHeapNullHandle, cmSymTblNullHandle );
  2052. cmScMeas* mp = cmScMeasAlloc(ctx,NULL,scH,srate,dynRefArray,dynRefCnt);
  2053. cmScMatcher* p = cmScMatcherAlloc(ctx,NULL,srate,scH,scWndN,midiN,cmScMatcherCb,mp);
  2054. double scoreThresh = 0.5;
  2055. unsigned candCnt = 0;
  2056. unsigned initFailCnt = 0;
  2057. unsigned otherFailCnt = 0;
  2058. unsigned scoreFailCnt = 0;
  2059. bool printFl = false;
  2060. unsigned markCharCnt = 31;
  2061. cmChar_t markText[ markCharCnt+1 ];
  2062. cmTimeSpec_t t0,t1;
  2063. cmTimeGet(&t0);
  2064. // for each marker
  2065. for(i=0; i<markN; ++i)
  2066. {
  2067. // form the marker text
  2068. snprintf(markText,markCharCnt,"Mark %i",i);
  2069. // locate the marker
  2070. cmTlMarker_t* tlmp = cmTimeLineMarkerFind( tlH, markText );
  2071. if( tlmp == NULL )
  2072. {
  2073. if( printFl )
  2074. printf("The marker '%s' was not found.\n\n",markText);
  2075. continue;
  2076. }
  2077. // skip markers which do not contain text
  2078. if( cmTextIsEmpty(tlmp->text) )
  2079. {
  2080. if( printFl )
  2081. printf("The marker '%s' is being skipped because it has no text.\n\n",markText);
  2082. continue;
  2083. }
  2084. printf("=================== MARKER:%s ===================\n",markText);
  2085. cmScMatcherReset(p,0); // reset the score follower to the beginnig of the score
  2086. cmScMeasReset(mp);
  2087. ++candCnt;
  2088. // scan to the beginning of the marker
  2089. cmRC_t rc = cmScAlignScanToTimeLineEvent(p,tlH,&tlmp->obj,tlmp->obj.seqSmpIdx+tlmp->obj.durSmpCnt);
  2090. bool pfl = true;
  2091. if( rc != cmOkRC || p->begSyncLocIdx==cmInvalidIdx)
  2092. {
  2093. bool fl = printFl;
  2094. printFl = true;
  2095. // if a no alignment was found
  2096. if( p->begSyncLocIdx == cmInvalidIdx )
  2097. rc = cmInvalidArgRC;
  2098. if( p->mni == 0 )
  2099. {
  2100. if( printFl )
  2101. printf("mark:%i midi:%i Not enough MIDI notes to fill the scan buffer.\n",i,p->mni);
  2102. pfl = false;
  2103. }
  2104. else
  2105. {
  2106. switch(rc)
  2107. {
  2108. case cmInvalidArgRC:
  2109. if( printFl )
  2110. printf("mark:%i INITIAL SYNC FAIL\n",i);
  2111. ++initFailCnt;
  2112. pfl = false;
  2113. break;
  2114. case cmSubSysFailRC:
  2115. if( printFl )
  2116. printf("mark:%i SCAN RESYNC FAIL\n",i);
  2117. ++otherFailCnt;
  2118. break;
  2119. default:
  2120. if( printFl )
  2121. printf("mark:%i UNKNOWN FAIL\n",i);
  2122. ++otherFailCnt;
  2123. }
  2124. }
  2125. printFl = fl;
  2126. }
  2127. if( pfl )
  2128. {
  2129. double fmeas = cmScMatcherFMeas(p);
  2130. if( printFl )
  2131. 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,tlmp->text);
  2132. if( fmeas < scoreThresh )
  2133. ++scoreFailCnt;
  2134. }
  2135. //print score and match for entire marker
  2136. //cmScMatcherPrint(p);
  2137. // ONLY USE ONE MARKER DURING TESTING
  2138. // break;
  2139. if( printFl )
  2140. printf("\n");
  2141. }
  2142. printf("cand:%i fail:%i - init:%i score:%i other:%i\n\n",candCnt,initFailCnt+scoreFailCnt+otherFailCnt,initFailCnt,scoreFailCnt,otherFailCnt);
  2143. cmTimeGet(&t1);
  2144. printf("elapsed:%f\n", (double)cmTimeElapsedMicros(&t0,&t1)/1000000.0 );
  2145. cmScMatcherFree(&p);
  2146. cmScMeasFree(&mp);
  2147. cmCtxFree(&ctx);
  2148. }
  2149. //=======================================================================================================================
  2150. cmScModulator* cmScModulatorAlloc( cmCtx* c, cmScModulator* p, cmCtx_t* ctx, cmSymTblH_t stH, double srate, unsigned samplesPerCycle, const cmChar_t* fn, const cmChar_t* modLabel, cmScModCb_t cbFunc, void* cbArg )
  2151. {
  2152. cmScModulator* op = cmObjAlloc(cmScModulator,c,p);
  2153. if( ctx != NULL )
  2154. if( cmScModulatorInit(op,ctx,stH,srate,samplesPerCycle,fn,modLabel,cbFunc,cbArg) != cmOkRC )
  2155. cmScModulatorFree(&op);
  2156. return op;
  2157. }
  2158. cmRC_t cmScModulatorFree( cmScModulator** pp )
  2159. {
  2160. cmRC_t rc = cmOkRC;
  2161. if( pp==NULL || *pp==NULL )
  2162. return rc;
  2163. cmScModulator* p = *pp;
  2164. if((rc = cmScModulatorFinal(p)) != cmOkRC )
  2165. return rc;
  2166. //cmMemFree(p->earray);
  2167. //cmMemFree(p->xlist);
  2168. cmObjFree(pp);
  2169. return rc;
  2170. }
  2171. typedef struct
  2172. {
  2173. unsigned typeId;
  2174. unsigned minArgCnt;
  2175. const cmChar_t* label;
  2176. } _cmScModTypeMap_t;
  2177. _cmScModTypeMap_t _cmScModTypeArray[] =
  2178. {
  2179. { kDeclModTId, 0, "decl" },
  2180. { kSetModTId, 1, "set" },
  2181. { kLineModTId, 2, "line" },
  2182. { kSetLineModTId, 3, "sline" },
  2183. { kPostModTId, 4, "post" },
  2184. { kExecModTId, 5, "exec" },
  2185. { kInputModTId, 6, "input" },
  2186. { kCrossModTId, 7, "cross" },
  2187. { kInvalidModTId, 0, "<invalid>"}
  2188. };
  2189. const _cmScModTypeMap_t* _cmScModTypeLabelToMap( const cmChar_t* label )
  2190. {
  2191. unsigned i;
  2192. for(i=0; _cmScModTypeArray[i].typeId!=kInvalidModTId; ++i)
  2193. if( strcmp(_cmScModTypeArray[i].label,label) == 0 )
  2194. return _cmScModTypeArray + i;
  2195. return NULL;
  2196. }
  2197. const _cmScModTypeMap_t* _cmScModTypeIdToMap( unsigned typeId )
  2198. {
  2199. unsigned i;
  2200. for(i=0; _cmScModTypeArray[i].typeId!=kInvalidModTId; ++i)
  2201. if( _cmScModTypeArray[i].typeId == typeId )
  2202. return _cmScModTypeArray + i;
  2203. return NULL;
  2204. }
  2205. cmScModVar_t* _cmScModSymToVar( cmScModulator* p, unsigned varSymId )
  2206. {
  2207. cmScModVar_t* vp = p->vlist;
  2208. for(; vp!=NULL; vp=vp->vlink)
  2209. if( varSymId == vp->varSymId )
  2210. return vp;
  2211. return NULL;
  2212. }
  2213. cmScModVar_t* _cmScModulatorInsertVar( cmScModulator* p, unsigned varSymId, unsigned flags )
  2214. {
  2215. cmScModVar_t* vp = _cmScModSymToVar(p,varSymId);
  2216. // if the specified variable was not found then create one
  2217. if( vp == NULL )
  2218. {
  2219. vp = cmMemAllocZ(cmScModVar_t,1);
  2220. vp->varSymId = varSymId;
  2221. vp->varId = cmInvalidId;
  2222. vp->vlink = p->vlist;
  2223. p->vlist = vp;
  2224. }
  2225. vp->flags = flags;
  2226. vp->value = DBL_MAX;
  2227. vp->min = DBL_MAX;
  2228. vp->max = DBL_MAX;
  2229. vp->rate = DBL_MAX;
  2230. vp->phase = 0;
  2231. vp->entry = NULL;
  2232. vp->alink = NULL;
  2233. return vp;
  2234. }
  2235. cmScModEntry_t* _cmScModulatorInsertEntry(cmScModulator* p, cmScModEntryGroup_t* g, unsigned idx, unsigned scLocIdx, unsigned varSymId, unsigned typeId, unsigned paramCnt )
  2236. {
  2237. assert( idx < g->en );
  2238. g->earray[idx].scLocIdx = scLocIdx;
  2239. g->earray[idx].typeId = typeId;
  2240. return g->earray + idx;
  2241. }
  2242. /*
  2243. {
  2244. [
  2245. { loc:123, mod:modlabel, var:varlabel, param:[ ] }
  2246. ]
  2247. }
  2248. */
  2249. // Parameter values are found as values of the 'data','min' or 'max' fields.
  2250. // A parameter value may be either a symbol identifier (mapped to a variable)
  2251. // or a literal number. This function determines which form the paramter
  2252. // value takes and parses it accordingly.
  2253. cmRC_t _cmScModulatorParseParam( cmScModulator* p, cmSymTblH_t stH, cmJsonNode_t* np, cmScModParam_t* pp )
  2254. {
  2255. cmRC_t rc = cmOkRC;
  2256. switch( np->typeId )
  2257. {
  2258. case kIntTId:
  2259. case kRealTId:
  2260. if( cmJsonRealValue(np, &pp->val ) != kOkJsRC )
  2261. {
  2262. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Error parsing in Modulator literal value." );
  2263. goto errLabel;
  2264. }
  2265. pp->pid = kLiteralModPId;
  2266. break;
  2267. case kStringTId:
  2268. {
  2269. const cmChar_t* label = NULL;
  2270. if( cmJsonStringValue(np, &label) != kOkJsRC )
  2271. {
  2272. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Error parsing in Modulator symbol label." );
  2273. goto errLabel;
  2274. }
  2275. pp->symId = cmSymTblRegisterSymbol(stH,label);
  2276. pp->pid = kSymbolModPId;
  2277. }
  2278. break;
  2279. default:
  2280. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Parameter value is not a number or identifier." );
  2281. goto errLabel;
  2282. break;
  2283. }
  2284. errLabel:
  2285. return rc;
  2286. }
  2287. // If the requested parameter has a value then return it in *valPtr.
  2288. // If it does not then do nothing. This function applies scaling to RHS values.
  2289. cmRC_t _cmScModGetParam( cmScModulator* p, const cmScModParam_t* pp, double* valPtr )
  2290. {
  2291. cmRC_t rc = cmOkRC;
  2292. switch( pp->pid )
  2293. {
  2294. case kInvalidModPId:
  2295. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "An invalid parameter was encountered.");
  2296. goto errLabel;
  2297. break;
  2298. case kLiteralModPId:
  2299. *valPtr = pp->val;
  2300. break;
  2301. case kSymbolModPId:
  2302. {
  2303. cmScModVar_t* vp;
  2304. // get a pointer to the parameter variable
  2305. if((vp = _cmScModSymToVar(p, pp->symId )) == NULL )
  2306. {
  2307. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Variable '%s' not found.",cmSymTblLabel(p->stH,pp->symId));
  2308. goto errLabel;
  2309. }
  2310. // if this is not a 'calculated' paramter then scale it here.
  2311. if( cmIsFlag(vp->flags,kCalcModFl ) && vp->min!=DBL_MAX && vp->max!=DBL_MAX )
  2312. *valPtr = (vp->value - vp->min)/(vp->max-vp->min);
  2313. else
  2314. *valPtr = vp->value;
  2315. }
  2316. break;
  2317. default:
  2318. { assert(0); }
  2319. }
  2320. errLabel:
  2321. return rc;
  2322. }
  2323. void _cmScModDumpParam( cmScModulator* p, const cmChar_t* label, const cmScModParam_t* pp )
  2324. {
  2325. printf("%s: ",label);
  2326. switch( pp->pid )
  2327. {
  2328. case kInvalidModPId:
  2329. printf("<invalid>");
  2330. break;
  2331. case kLiteralModPId:
  2332. if( pp->val == DBL_MAX )
  2333. printf("<max> ");
  2334. else
  2335. printf("%f ",pp->val);
  2336. break;
  2337. case kSymbolModPId:
  2338. printf("%s ",cmSymTblLabel(p->stH,pp->symId));
  2339. break;
  2340. default:
  2341. { assert(0); }
  2342. }
  2343. }
  2344. void _cmScModDumpVal( cmChar_t* label, double val )
  2345. {
  2346. printf("%s:",label);
  2347. if( val == DBL_MAX )
  2348. printf("<max> " );
  2349. else
  2350. printf("%f ",val);
  2351. }
  2352. void _cmScModDumpVar( cmScModulator* p, const cmScModVar_t* vp )
  2353. {
  2354. printf("%7s %3i fl:0x%x entry:%p alink:%p %s",cmSymTblLabel(p->stH,vp->varSymId),vp->varId,vp->flags,vp->entry,vp->alink, cmIsFlag(vp->flags,kInputModFl)?"input":"");
  2355. _cmScModDumpVal("val",vp->value);
  2356. _cmScModDumpVal("min",vp->min);
  2357. _cmScModDumpVal("max",vp->max);
  2358. _cmScModDumpVal("rate",vp->rate);
  2359. _cmScModDumpVal("v0",vp->v0);
  2360. }
  2361. void _cmScModDumpEntry( cmScModulator* p, const cmScModEntry_t* ep)
  2362. {
  2363. const _cmScModTypeMap_t* tm = _cmScModTypeIdToMap( ep->typeId );
  2364. printf("%10i ",ep->scLocIdx);
  2365. printf("%5s %7s", tm==NULL ? "invld" : tm->label, cmSymTblLabel(p->stH,ep->varPtr->varSymId));
  2366. _cmScModDumpParam(p," beg", &ep->beg);
  2367. _cmScModDumpParam(p," end", &ep->end);
  2368. _cmScModDumpParam(p," min", &ep->min);
  2369. _cmScModDumpParam(p," max", &ep->max);
  2370. _cmScModDumpParam(p," rate",&ep->rate);
  2371. printf("\n");
  2372. }
  2373. cmRC_t _cmScModParseEntryGroup( cmScModulator* p, cmCtx_t* ctx, cmJsonH_t jsH, cmSymTblH_t stH, cmJsonNode_t* jnp, cmScModEntryGroup_t* g, const cmChar_t* fn )
  2374. {
  2375. cmRC_t rc = cmOkRC;
  2376. unsigned prvScLocIdx = cmInvalidIdx;
  2377. const cmChar_t* prvVarLabel = "<dummy>";
  2378. const cmChar_t* prvTypeLabel = NULL;
  2379. unsigned i;
  2380. // allocate the entry array
  2381. unsigned entryCnt = cmJsonChildCount(jnp);
  2382. g->earray = cmMemResizeZ(cmScModEntry_t,g->earray,entryCnt);
  2383. g->en = entryCnt;
  2384. for(i=0; i<entryCnt; ++i)
  2385. {
  2386. cmJsRC_t jsRC = kOkJsRC;
  2387. const char* errLabelPtr = NULL;
  2388. unsigned scLocIdx = cmInvalidIdx;
  2389. const cmChar_t* varLabel = NULL;
  2390. const cmChar_t* typeLabel = NULL;
  2391. cmJsonNode_t* onp = cmJsonArrayElement(jnp,i);
  2392. cmJsonNode_t* dnp = NULL;
  2393. const _cmScModTypeMap_t* map = NULL;
  2394. if((jsRC = cmJsonMemberValues( onp, &errLabelPtr,
  2395. "var", kStringTId | kOptArgJsFl, &varLabel,
  2396. "type",kStringTId | kOptArgJsFl, &typeLabel,
  2397. "loc", kIntTId | kOptArgJsFl, &scLocIdx,
  2398. NULL )) != kOkJsRC )
  2399. {
  2400. if( errLabelPtr == NULL )
  2401. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Error:%s on record at index %i in file:%s",errLabelPtr,i,cmStringNullGuard(fn) );
  2402. else
  2403. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Synax error in Modulator record at index %i in file:%s",i,cmStringNullGuard(fn) );
  2404. goto errLabel;
  2405. }
  2406. // if the score location was not given use the previous score location
  2407. if( scLocIdx == cmInvalidIdx )
  2408. scLocIdx = prvScLocIdx;
  2409. else
  2410. prvScLocIdx = scLocIdx;
  2411. // if the var label was not given use the previous one
  2412. if( varLabel == NULL )
  2413. varLabel = prvVarLabel;
  2414. else
  2415. prvVarLabel = varLabel;
  2416. if( varLabel == NULL )
  2417. {
  2418. rc = cmCtxRtCondition(&p->obj, cmInvalidArgRC, "No 'var' label has been set in mod file '%s'.",cmStringNullGuard(fn));
  2419. goto errLabel;
  2420. }
  2421. // if the type label was not given use the previous one
  2422. if( typeLabel == NULL )
  2423. typeLabel = prvTypeLabel;
  2424. else
  2425. prvTypeLabel = typeLabel;
  2426. if( typeLabel == NULL )
  2427. {
  2428. rc = cmCtxRtCondition(&p->obj, cmInvalidArgRC, "No 'type' label has been set in mod file '%s'.",cmStringNullGuard(fn));
  2429. goto errLabel;
  2430. }
  2431. // validate the entry type label
  2432. if((map = _cmScModTypeLabelToMap(typeLabel)) == NULL )
  2433. {
  2434. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Unknown entry type '%s' in Modulator record at index %i in file:%s",cmStringNullGuard(typeLabel),i,cmStringNullGuard(fn) );
  2435. goto errLabel;
  2436. }
  2437. unsigned varSymId = cmSymTblRegisterSymbol(stH,varLabel);
  2438. // get the count of the elmenets in the data array
  2439. unsigned paramCnt = cmJsonChildCount(onp);
  2440. // fill the entry record and find or create the target var
  2441. cmScModEntry_t* ep = _cmScModulatorInsertEntry(p,g,i,scLocIdx,varSymId,map->typeId,paramCnt);
  2442. typedef struct
  2443. {
  2444. const cmChar_t* label;
  2445. cmScModParam_t* param;
  2446. } map_t;
  2447. // parse the var and parameter records
  2448. map_t mapArray[] =
  2449. {
  2450. { "min", &ep->min },
  2451. { "max", &ep->max },
  2452. { "rate",&ep->rate },
  2453. { "val", &ep->beg },
  2454. { "end", &ep->end },
  2455. { "dur", &ep->dur },
  2456. { "arg", &ep->arg },
  2457. { NULL, NULL }
  2458. };
  2459. unsigned j=0;
  2460. for(j=0; mapArray[j].param!=NULL; ++j)
  2461. if((dnp = cmJsonFindValue(jsH,mapArray[j].label, onp, kInvalidTId )) != NULL )
  2462. if((rc = _cmScModulatorParseParam(p,stH,dnp,mapArray[j].param)) != cmOkRC )
  2463. goto errLabel;
  2464. // create the variable associated with this entry
  2465. ep->varPtr = _cmScModulatorInsertVar(p,varSymId, ep->typeId==kInputModTId ? kInputModFl : 0);
  2466. // set the variable id value
  2467. if( ep->varPtr->varId == cmInvalidIdx )
  2468. {
  2469. if( ep->typeId != kInputModTId )
  2470. ep->varPtr->varId = p->outVarCnt++;
  2471. else
  2472. ep->varPtr->varId = p->inVarCnt++;
  2473. }
  2474. }
  2475. errLabel:
  2476. return rc;
  2477. }
  2478. cmRC_t _cmScModulatorParse( cmScModulator* p, cmCtx_t* ctx, cmSymTblH_t stH, const cmChar_t* fn )
  2479. {
  2480. cmRC_t rc = cmOkRC;
  2481. cmJsonNode_t* jnp = NULL;
  2482. cmJsonH_t jsH = cmJsonNullHandle;
  2483. unsigned i = cmInvalidIdx;
  2484. cmScModEntryGroup_t* g0 = NULL;
  2485. cmScModEntryGroup_t* g1 = p->glist;
  2486. // read the JSON file
  2487. if( cmJsonInitializeFromFile(&jsH, fn, ctx ) != kOkJsRC )
  2488. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "JSON file parse failed on the modulator file: %s.",cmStringNullGuard(fn) );
  2489. jnp = cmJsonRoot(jsH);
  2490. unsigned groupCnt = cmJsonChildCount(jnp);
  2491. // for each entry group
  2492. for(i=0; i<groupCnt; ++i)
  2493. {
  2494. // get the entry group record
  2495. if( g1 == NULL )
  2496. {
  2497. g1 = cmMemAllocZ(cmScModEntryGroup_t,1);
  2498. if( g0 == NULL )
  2499. p->glist = g1;
  2500. else
  2501. g0->link = g1;
  2502. }
  2503. // get the entry group pair node
  2504. cmJsonNode_t* gnp = cmJsonMemberAtIndex(jnp,i);
  2505. if( gnp == NULL || !cmJsonIsPair(gnp) )
  2506. {
  2507. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Invalid entry group at index %i in score modulator file:%s",i,cmStringNullGuard(fn) );
  2508. goto errLabel;
  2509. }
  2510. // store the group label
  2511. g1->symId = cmSymTblRegisterSymbol(stH,cmJsonPairLabel(gnp));
  2512. // get and validate the group array value
  2513. if( (gnp = cmJsonPairValue(gnp))==NULL || cmJsonIsArray(gnp)==false )
  2514. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Group entry '%s' does not contain an array of entries in file:%s",cmStringNullGuard(cmJsonPairLabel(gnp)),cmStringNullGuard(fn) );
  2515. // parse the entry group
  2516. if((rc = _cmScModParseEntryGroup( p, ctx, jsH, stH, gnp, g1, fn )) != cmOkRC )
  2517. {
  2518. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Syntax error in entry group at index %i in score modulator file:%s",i,cmStringNullGuard(fn) );
  2519. goto errLabel;
  2520. }
  2521. g0 = g1;
  2522. g1 = g1->link;
  2523. }
  2524. errLabel:
  2525. // release the JSON tree
  2526. if( cmJsonIsValid(jsH) )
  2527. cmJsonFinalize(&jsH);
  2528. return rc;
  2529. }
  2530. cmRC_t _cmScModulatorReset( cmScModulator* p, cmCtx_t* ctx, unsigned scLocIdx, unsigned entryGroupSymId )
  2531. {
  2532. cmRC_t rc = cmOkRC;
  2533. p->alist = NULL;
  2534. p->elist = NULL;
  2535. p->nei = 0;
  2536. p->outVarCnt = 0;
  2537. p->inVarCnt = 0;
  2538. // reload the file
  2539. if((rc = _cmScModulatorParse(p,ctx,p->stH,p->fn)) != cmOkRC )
  2540. goto errLabel;
  2541. // select the entry group to execute
  2542. if( entryGroupSymId != cmInvalidId )
  2543. {
  2544. cmScModEntryGroup_t* g = p->glist;
  2545. for(; g!=NULL; g=g->link)
  2546. if( g->symId == entryGroupSymId )
  2547. {
  2548. cmCtxPrint(p->obj.ctx,"%s selected.\n",cmSymTblLabel(p->stH,entryGroupSymId));
  2549. p->xlist = g;
  2550. break;
  2551. }
  2552. }
  2553. // if an active entry group has not been set - set it to the first entry group
  2554. if( p->xlist == NULL )
  2555. {
  2556. if( entryGroupSymId != cmInvalidId )
  2557. cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The modulator entry group '%s' was not found. The active entry group was set to the first group in the file.",cmSymTblLabel(p->stH,entryGroupSymId) );
  2558. p->xlist = p->glist;
  2559. }
  2560. // clear the active flag on all variables
  2561. cmScModVar_t* vp = p->vlist;
  2562. for(; vp!=NULL; vp=vp->vlink)
  2563. {
  2564. vp->flags = cmClrFlag(vp->flags,kActiveModFl);
  2565. vp->alink = NULL;
  2566. }
  2567. errLabel:
  2568. return rc;
  2569. }
  2570. cmRC_t cmScModulatorInit( cmScModulator* p, cmCtx_t* ctx, cmSymTblH_t stH, double srate, unsigned samplesPerCycle, const cmChar_t* fn, const cmChar_t* modLabel, cmScModCb_t cbFunc, void* cbArg )
  2571. {
  2572. cmRC_t rc;
  2573. if((rc = cmScModulatorFinal(p)) != cmOkRC )
  2574. return rc;
  2575. p->fn = cmMemAllocStr(fn);
  2576. p->stH = stH;
  2577. p->modSymId = cmSymTblRegisterSymbol(stH,modLabel);
  2578. p->cbFunc = cbFunc;
  2579. p->cbArg = cbArg;
  2580. p->samplesPerCycle = samplesPerCycle;
  2581. p->srate = srate;
  2582. if( rc != cmOkRC )
  2583. cmScModulatorFinal(p);
  2584. else
  2585. _cmScModulatorReset(p,ctx,0,cmInvalidId);
  2586. return rc;
  2587. }
  2588. cmRC_t cmScModulatorFinal( cmScModulator* p )
  2589. {
  2590. cmMemFree(p->fn);
  2591. // release each var record
  2592. cmScModVar_t* vp = p->vlist;
  2593. while( vp!=NULL )
  2594. {
  2595. cmScModVar_t* np = vp->vlink;
  2596. cmMemFree(vp);
  2597. vp=np;
  2598. }
  2599. // release each group record
  2600. cmScModEntryGroup_t* g = p->glist;
  2601. while( g != NULL )
  2602. {
  2603. cmScModEntryGroup_t* g0 = g->link;
  2604. cmMemFree(g->earray);
  2605. cmMemFree(g);
  2606. g = g0;
  2607. }
  2608. p->glist = NULL;
  2609. return cmOkRC;
  2610. }
  2611. unsigned cmScModulatorOutVarCount( cmScModulator* p )
  2612. { return p->outVarCnt; }
  2613. cmScModVar_t* cmScModulatorOutVar( cmScModulator* p, unsigned idx )
  2614. {
  2615. unsigned i;
  2616. cmScModEntryGroup_t* g = p->glist;
  2617. for(; g!=NULL; g=g->link)
  2618. for(i=0; i<g->en; ++i)
  2619. if( cmIsNotFlag(g->earray[i].varPtr->flags,kInputModFl) && g->earray[i].varPtr->varId == idx )
  2620. return g->earray[i].varPtr;
  2621. return NULL;
  2622. }
  2623. unsigned cmScModulatorInVarCount( cmScModulator* p )
  2624. { return p->inVarCnt; }
  2625. cmScModVar_t* cmScModulatorInVar( cmScModulator* p, unsigned idx )
  2626. {
  2627. unsigned i;
  2628. cmScModEntryGroup_t* g = p->glist;
  2629. for(; g!=NULL; g=g->link)
  2630. for(i=0; i<g->en; ++i)
  2631. if( cmIsFlag(g->earray[i].varPtr->flags,kInputModFl) && g->earray[i].varPtr->varId == idx )
  2632. return g->earray[i].varPtr;
  2633. return NULL;
  2634. }
  2635. cmScModVar_t* _cmScModSetValuePrefix( cmScModulator* p, unsigned varSymId )
  2636. {
  2637. cmScModVar_t* vp;
  2638. // if the var does not exist ....
  2639. if((vp = _cmScModSymToVar(p, varSymId )) == NULL )
  2640. {
  2641. // ... then create it
  2642. vp = _cmScModulatorInsertVar(p,varSymId,kCalcModFl);
  2643. assert(vp!=NULL);
  2644. }
  2645. return vp;
  2646. }
  2647. cmRC_t cmScModulatorSetValueMinMax( cmScModulator* p, unsigned varSymId, double value, double min, double max )
  2648. {
  2649. cmScModVar_t* vp = _cmScModSetValuePrefix(p, varSymId );
  2650. assert( min <= max);
  2651. vp->min = min;
  2652. vp->max = max;
  2653. vp->value = value;
  2654. return cmOkRC;
  2655. }
  2656. cmRC_t cmScModulatorSetValue( cmScModulator* p, unsigned varSymId, double value )
  2657. {
  2658. cmScModVar_t* vp = _cmScModSetValuePrefix(p, varSymId );
  2659. vp->value = value;
  2660. return cmOkRC;
  2661. }
  2662. cmRC_t cmScModulatorReset( cmScModulator* p, cmCtx_t* ctx, unsigned scLocIdx, unsigned entryGroupSymId )
  2663. {
  2664. _cmScModulatorReset(p,ctx,scLocIdx,entryGroupSymId);
  2665. return cmScModulatorExec(p,scLocIdx);
  2666. }
  2667. void _cmScModUnlinkActive( cmScModulator* p, cmScModVar_t* vp, cmScModVar_t* pp )
  2668. {
  2669. // if vp is the first link on the chain
  2670. if( vp == p->alist )
  2671. p->alist = vp->alink;
  2672. // if vp is the last link on the chain
  2673. if( vp == p->elist )
  2674. p->elist = pp;
  2675. if( pp != NULL )
  2676. pp->alink = vp->alink;
  2677. vp->flags = cmClrFlag(vp->flags,kActiveModFl);
  2678. vp->alink = NULL;
  2679. vp->entry = NULL;
  2680. }
  2681. cmRC_t _cmScModActivateEntries( cmScModulator* p, cmScModEntry_t* earray, unsigned* idxRef, unsigned cnt, unsigned scLocIdx );
  2682. cmRC_t _cmScModActivateGroup( cmScModulator* p, cmScModEntry_t* ep )
  2683. {
  2684. cmScModEntryGroup_t* g = p->glist;
  2685. for(; g!=NULL; g=g->link)
  2686. if( g->symId == ep->beg.symId )
  2687. {
  2688. unsigned idx = 0;
  2689. return _cmScModActivateEntries( p, g->earray, &idx, g->en, ep->beg.symId );
  2690. }
  2691. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Entry group '%s' not found.",cmSymTblLabel(p->stH,ep->beg.symId));
  2692. }
  2693. cmRC_t _cmScModGetCrossParam( cmScModulator* p, cmScModEntry_t* ep, cmScModParam_t* pp, const cmChar_t* label, double* valRef )
  2694. {
  2695. cmRC_t rc;
  2696. if((rc = _cmScModGetParam(p, pp, valRef )) != cmOkRC )
  2697. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Invalid %s parameter for cross variable:%s",label,cmSymTblLabel(p->stH,ep->varPtr->varSymId) );
  2698. return rc;
  2699. }
  2700. cmRC_t _cmScModExecCross( cmScModulator* p, cmScModEntry_t* ep )
  2701. {
  2702. cmRC_t rc = cmOkRC;
  2703. double x = 0.0;
  2704. double y0 = 0.0, y1 = 0.0;
  2705. if((rc = _cmScModGetCrossParam(p, ep, &ep->arg, "src var", &x )) != cmOkRC )
  2706. return rc;
  2707. if((rc = _cmScModGetCrossParam(p, ep, &ep->min, "dst min", &y0 )) != cmOkRC )
  2708. return rc;
  2709. if((rc = _cmScModGetCrossParam(p, ep, &ep->max, "dst max", &y1 )) != cmOkRC )
  2710. return rc;
  2711. //printf("%s x:%f y0:%f y1:%f\n",__FUNCTION__,x,y0,y1);
  2712. ep->varPtr->value = y0 + x * (y1-y0);
  2713. return rc;
  2714. }
  2715. // Type specific variable activation -
  2716. cmRC_t _cmScModActivate(cmScModulator* p, cmScModEntry_t* ep )
  2717. {
  2718. cmRC_t rc = cmOkRC;
  2719. cmScModVar_t* vp = ep->varPtr;
  2720. // optionally update the min/max/rate values in the target var
  2721. if( ep->min.pid != kInvalidModPId )
  2722. if((rc = _cmScModGetParam(p,&ep->min,&vp->min)) != cmOkRC )
  2723. goto errLabel;
  2724. if( ep->max.pid != kInvalidModPId )
  2725. if((rc = _cmScModGetParam(p,&ep->max,&vp->max)) != cmOkRC )
  2726. goto errLabel;
  2727. if( ep->rate.pid != kInvalidModPId )
  2728. if((rc = _cmScModGetParam(p,&ep->rate,&vp->rate)) != cmOkRC )
  2729. goto errLabel;
  2730. switch( ep->typeId )
  2731. {
  2732. case kDeclModTId:
  2733. case kSetModTId:
  2734. break;
  2735. case kLineModTId:
  2736. vp->v0 = vp->value;
  2737. vp->phase = 0;
  2738. break;
  2739. case kSetLineModTId:
  2740. _cmScModGetParam(p,&ep->beg,&vp->value); // starting value
  2741. vp->v0 = vp->value; // set initial value
  2742. vp->phase = 0; // reset phase
  2743. break;
  2744. case kPostModTId:
  2745. p->postFl = vp->value;
  2746. break;
  2747. case kExecModTId:
  2748. rc = _cmScModActivateGroup(p,ep);
  2749. break;
  2750. case kInputModTId:
  2751. break;
  2752. case kCrossModTId:
  2753. break;
  2754. default:
  2755. { assert(0); }
  2756. }
  2757. errLabel:
  2758. return rc;
  2759. }
  2760. // Callback the application with a new variable value.
  2761. cmRC_t _cmScModExecSendValue( cmScModulator* p, cmScModVar_t* vp )
  2762. {
  2763. cmRC_t rc = cmOkRC;
  2764. bool sendFl = true;
  2765. double v = vp->value;
  2766. // scale the output value - this is equiv to scaling the LHS
  2767. if( cmIsFlag(vp->flags,kCalcModFl) && vp->min!=DBL_MAX && vp->max!=DBL_MAX )
  2768. v = vp->min + v * (vp->max - vp->min);
  2769. // if an output rate throttle is in effect ....
  2770. if( vp->rate!=DBL_MAX && vp->phase!=0 )
  2771. sendFl = remainder(vp->phase*p->samplesPerCycle, p->srate*vp->rate/1000 ) < p->samplesPerCycle;
  2772. if(sendFl)
  2773. p->cbFunc(p->cbArg,vp->varSymId,v,p->postFl);
  2774. return rc;
  2775. }
  2776. // Execute a variable.
  2777. // Return true if vp should be deactivated otherwise return false.
  2778. bool _cmScModExec( cmScModulator* p, cmScModVar_t* vp )
  2779. {
  2780. cmRC_t rc = cmOkRC;
  2781. bool fl = false;
  2782. bool sendFl = true;
  2783. switch( vp->entry->typeId )
  2784. {
  2785. case kDeclModTId:
  2786. sendFl = false;
  2787. fl = true;
  2788. break;
  2789. case kSetModTId:
  2790. {
  2791. // Get a new value for the variable *vp.
  2792. if((rc = _cmScModGetParam(p,&vp->entry->beg,&vp->value)) != cmOkRC )
  2793. goto errLabel;
  2794. vp->phase = 0; // force the value to be sent
  2795. fl = true;
  2796. }
  2797. break;
  2798. case kSetLineModTId:
  2799. case kLineModTId:
  2800. {
  2801. double v1=0, td=0;
  2802. // get the target value
  2803. if((rc = _cmScModGetParam(p,&vp->entry->end,&v1)) != cmOkRC)
  2804. goto errLabel;
  2805. // get the time duration
  2806. if((rc = _cmScModGetParam(p,&vp->entry->dur,&td)) != cmOkRC)
  2807. goto errLabel;
  2808. // update the value of the var *vp
  2809. double v = vp->v0 + (v1-vp->v0) * (vp->phase * p->samplesPerCycle) / (p->srate * td);
  2810. if((fl = (vp->value <= v1 && v >= v1) || (vp->value >= v1 && v <= v1 )) == true )
  2811. v = v1;
  2812. vp->value = v;
  2813. }
  2814. break;
  2815. case kPostModTId:
  2816. sendFl = false;
  2817. break;
  2818. case kExecModTId:
  2819. sendFl = false;
  2820. break;
  2821. case kInputModTId:
  2822. sendFl = false;
  2823. break;
  2824. case kCrossModTId:
  2825. _cmScModExecCross(p,vp->entry);
  2826. vp->phase = 0; // force the value to be sent
  2827. fl = true;
  2828. break;
  2829. default:
  2830. { assert(0); }
  2831. }
  2832. // notify the application that a new variable value has been generated
  2833. if(sendFl)
  2834. {
  2835. rc = _cmScModExecSendValue(p,vp);
  2836. // increment the phase - after send because send notices when phase is zero
  2837. vp->phase += 1;
  2838. }
  2839. errLabel:
  2840. if( rc != cmOkRC )
  2841. fl = true;
  2842. return fl;
  2843. }
  2844. // Execute the entries in xparray[] begining with entry xparray[idx] and continuing until:
  2845. // 1) cnt - idx entries have been executed
  2846. // 2) an entry is located whose scLocIdx != scLocIdx and also not -1
  2847. cmRC_t _cmScModActivateEntries( cmScModulator* p, cmScModEntry_t* earray, unsigned* idxRef, unsigned cnt, unsigned scLocIdx )
  2848. {
  2849. assert( idxRef != NULL );
  2850. cmRC_t trc;
  2851. cmRC_t rc = cmOkRC;
  2852. unsigned idx = *idxRef;
  2853. // trigger entries that have expired since the last call to this function
  2854. for(; idx<cnt && (earray[idx].scLocIdx==-1 || earray[idx].scLocIdx<=scLocIdx); ++idx)
  2855. {
  2856. cmScModEntry_t* ep = earray + idx;
  2857. // if the variable assoc'd with this entry is not on the active list ...
  2858. if( cmIsFlag(ep->varPtr->flags,kActiveModFl) == false )
  2859. {
  2860. // ... then append it to the end of the active list ...
  2861. ep->varPtr->flags |= kActiveModFl;
  2862. if( p->elist == NULL )
  2863. p->elist = ep->varPtr;
  2864. else
  2865. {
  2866. p->elist->alink = ep->varPtr;
  2867. p->elist = ep->varPtr;
  2868. }
  2869. p->elist->alink = NULL;
  2870. if( p->alist == NULL )
  2871. p->alist = ep->varPtr;
  2872. }
  2873. // do type specific activation
  2874. if((trc = _cmScModActivate(p,ep)) != cmOkRC )
  2875. rc = trc;
  2876. ep->varPtr->entry = ep;
  2877. }
  2878. *idxRef = idx;
  2879. return rc;
  2880. }
  2881. cmRC_t cmScModulatorExec( cmScModulator* p, unsigned scLocIdx )
  2882. {
  2883. cmRC_t rc = cmOkRC;
  2884. rc = _cmScModActivateEntries(p, p->xlist->earray, &p->nei, p->xlist->en, scLocIdx );
  2885. // Update the active variables
  2886. cmScModVar_t* pp = NULL;
  2887. cmScModVar_t* vp = p->alist;
  2888. for(; vp!=NULL; vp=vp->alink)
  2889. {
  2890. if( _cmScModExec(p,vp) )
  2891. _cmScModUnlinkActive(p,vp,pp);
  2892. else
  2893. pp = vp;
  2894. }
  2895. return rc;
  2896. }
  2897. cmRC_t cmScModulatorDump( cmScModulator* p )
  2898. {
  2899. cmRC_t rc = cmOkRC;
  2900. printf("MOD:\n");
  2901. printf("nei:%i alist:%p outVarCnt:%i inVarCnt:%i\n",p->nei,p->alist,p->outVarCnt,p->inVarCnt);
  2902. printf("ENTRIES:\n");
  2903. cmScModEntryGroup_t* g = p->glist;
  2904. for(; g!=NULL; g=g->link)
  2905. {
  2906. printf("%s\n",cmSymTblLabel(p->stH,g->symId));
  2907. unsigned i;
  2908. for(i=0; i<g->en; ++i)
  2909. {
  2910. cmScModEntry_t* ep = g->earray + i;
  2911. printf("%3i ",i);
  2912. _cmScModDumpEntry(p,ep);
  2913. }
  2914. }
  2915. printf("VARIABLES\n");
  2916. cmScModVar_t* vp = p->vlist;
  2917. for(; vp!=NULL; vp=vp->vlink)
  2918. {
  2919. _cmScModDumpVar(p,vp);
  2920. printf("\n");
  2921. }
  2922. return rc;
  2923. }
  2924. //=======================================================================================================================
  2925. cmRecdPlay* cmRecdPlayAlloc( cmCtx* c, cmRecdPlay* p, double srate, unsigned fragCnt, unsigned chCnt, double initFragSecs, double maxLaSecs, double curLaSecs )
  2926. {
  2927. cmRecdPlay* op = cmObjAlloc(cmRecdPlay,c,p);
  2928. if( cmRecdPlayInit(op,srate,fragCnt,chCnt,initFragSecs,maxLaSecs,curLaSecs) != cmOkRC )
  2929. cmRecdPlayFree(&op);
  2930. return op;
  2931. }
  2932. cmRC_t cmRecdPlayFree( cmRecdPlay** pp )
  2933. {
  2934. cmRC_t rc = cmOkRC;
  2935. if( pp==NULL || *pp==NULL )
  2936. return rc;
  2937. cmRecdPlay* p = *pp;
  2938. if((rc = cmRecdPlayFinal(p)) != cmOkRC )
  2939. return rc;
  2940. cmObjFree(pp);
  2941. return rc;
  2942. }
  2943. cmRC_t cmRecdPlayInit( cmRecdPlay* p, double srate, unsigned fragCnt, unsigned chCnt, double initFragSecs, double maxLaSecs, double curLaSecs )
  2944. {
  2945. cmRC_t rc;
  2946. unsigned i;
  2947. if( curLaSecs > maxLaSecs )
  2948. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The initial look-ahead time %f is greater than the maximum look-ahead time %f.",curLaSecs,maxLaSecs);
  2949. if((rc = cmRecdPlayFinal(p)) != cmOkRC )
  2950. return rc;
  2951. if( chCnt == 0 )
  2952. return cmOkRC;
  2953. p->frags = cmMemAllocZ(cmRecdPlayFrag,fragCnt);
  2954. p->fragCnt = fragCnt;
  2955. p->srate = srate;
  2956. p->chCnt = chCnt;
  2957. p->initFragSecs = initFragSecs;
  2958. p->maxLaSmpCnt = floor(maxLaSecs*srate);
  2959. p->curLaSmpCnt = floor(curLaSecs*srate);
  2960. p->laChs = cmMemAllocZ(cmSample_t*,chCnt);
  2961. p->laSmpIdx = 0;
  2962. for(i=0; i<chCnt; ++i)
  2963. p->laChs[i] = cmMemAllocZ(cmSample_t,p->maxLaSmpCnt);
  2964. return rc;
  2965. }
  2966. cmRC_t cmRecdPlayFinal( cmRecdPlay* p )
  2967. {
  2968. unsigned i,j;
  2969. // free the fragments
  2970. for(i=0; i<p->fragCnt; ++i)
  2971. {
  2972. for(j=0; j<p->chCnt; ++j)
  2973. cmMemFree(p->frags[i].chArray[j]);
  2974. cmMemFree(p->frags[i].chArray);
  2975. }
  2976. // free the look-ahead buffers
  2977. for(i=0; i<p->chCnt; ++i)
  2978. cmMemFree(p->laChs[i]);
  2979. cmMemPtrFree(&p->laChs);
  2980. cmMemPtrFree(&p->frags);
  2981. p->fragCnt = 0;
  2982. p->chCnt = 0;
  2983. p->rlist = NULL;
  2984. p->plist = NULL;
  2985. return cmOkRC;
  2986. }
  2987. cmRC_t cmRecdPlayRegisterFrag( cmRecdPlay* p, unsigned fragIdx, unsigned labelSymId )
  2988. {
  2989. assert( fragIdx < p->fragCnt );
  2990. unsigned i;
  2991. p->frags[ fragIdx ].labelSymId = labelSymId;
  2992. p->frags[ fragIdx ].chArray = cmMemResizeZ(cmSample_t*,p->frags[fragIdx].chArray,p->chCnt);
  2993. for(i=0; i<p->chCnt; ++i)
  2994. {
  2995. p->frags[ fragIdx ].allocCnt = floor(p->initFragSecs * p->srate);
  2996. p->frags[ fragIdx ].chArray[i] = cmMemResizeZ(cmSample_t,p->frags[ fragIdx ].chArray[i],p->frags[fragIdx].allocCnt);
  2997. }
  2998. return cmOkRC;
  2999. }
  3000. cmRC_t cmRecdPlaySetLaSecs( cmRecdPlay* p, double curLaSecs )
  3001. {
  3002. p->curLaSmpCnt = floor(curLaSecs*p->srate);
  3003. return cmOkRC;
  3004. }
  3005. cmRC_t cmRecdPlayRewind( cmRecdPlay* p )
  3006. {
  3007. unsigned i;
  3008. // zero the look-ahead buffers
  3009. p->laSmpIdx = 0;
  3010. for(i=0; i<p->chCnt; ++i)
  3011. cmVOS_Zero(p->laChs[i],p->maxLaSmpCnt);
  3012. // remove all the active players
  3013. while( p->plist != NULL )
  3014. cmRecdPlayEndPlay(p,p->plist->labelSymId);
  3015. // remove all the active recorders
  3016. while( p->rlist != NULL )
  3017. cmRecdPlayEndRecord(p,p->rlist->labelSymId);
  3018. // rewind all the fragments play posn.
  3019. for(i=0; i<p->fragCnt; ++i)
  3020. p->frags[i].playIdx = 0;
  3021. return cmOkRC;
  3022. }
  3023. cmRC_t cmRecdPlayBeginRecord( cmRecdPlay* p, unsigned labelSymId )
  3024. {
  3025. unsigned i;
  3026. for(i=0; i<p->fragCnt; ++i)
  3027. if( p->frags[i].labelSymId == labelSymId )
  3028. {
  3029. // if the frag is not already on the recd list
  3030. if( p->frags[i].rlink == NULL )
  3031. {
  3032. p->frags[i].recdIdx = 0;
  3033. p->frags[i].playIdx = 0;
  3034. p->frags[i].rlink = p->rlist;
  3035. p->rlist = p->frags + i;
  3036. // handle LA buf longer than frag buf.
  3037. int cpyCnt = cmMin(p->curLaSmpCnt,p->frags[i].allocCnt);
  3038. // go backwards in LA buf from newest sample to find init src offset
  3039. int srcOffs = p->laSmpIdx - cpyCnt;
  3040. // if the src is before the first sample in the LA buf then wrap to end of buf
  3041. if( srcOffs < 0 )
  3042. srcOffs += p->maxLaSmpCnt;
  3043. assert( 0 <= srcOffs && srcOffs < p->maxLaSmpCnt );
  3044. // cnt of samples to copy from LA buf (limited by end of LA buf)
  3045. int n0 = cmMin(cpyCnt,p->maxLaSmpCnt - srcOffs);
  3046. // if necessary wrap to begin of LA buf for remaining samples
  3047. int n1 = cpyCnt>n0 ? n1 = cpyCnt-n0 : 0;
  3048. int j;
  3049. assert(n0+n1 == cpyCnt );
  3050. for(j=0; j<p->chCnt; ++j)
  3051. cmVOS_Copy(p->frags[i].chArray[j],n0,p->laChs[j]+srcOffs);
  3052. if( n1 > 0 )
  3053. {
  3054. for(j=0; j<p->chCnt; ++j)
  3055. cmVOS_Copy(p->frags[i].chArray[j]+n0,n1,p->laChs[j]);
  3056. }
  3057. p->frags[i].recdIdx = cpyCnt;
  3058. p->frags[i].playIdx = 0;
  3059. }
  3060. return cmOkRC;
  3061. }
  3062. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'begin record'.",labelSymId);
  3063. }
  3064. cmRC_t cmRecdPlayEndRecord( cmRecdPlay* p, unsigned labelSymId )
  3065. {
  3066. cmRecdPlayFrag* fp = p->rlist;
  3067. cmRecdPlayFrag* pp = NULL;
  3068. for(; fp != NULL; fp=fp->rlink )
  3069. {
  3070. if( fp->labelSymId == labelSymId )
  3071. {
  3072. if( pp == NULL )
  3073. p->rlist = fp->rlink;
  3074. else
  3075. pp->rlink = fp->rlink;
  3076. fp->rlink = NULL;
  3077. return cmOkRC;
  3078. }
  3079. pp = fp;
  3080. }
  3081. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'end record'.",labelSymId);
  3082. }
  3083. cmRC_t cmRecdPlayInsertRecord(cmRecdPlay* p, unsigned labelSymId, const cmChar_t* wavFn )
  3084. {
  3085. cmRC_t rc = cmOkRC;
  3086. unsigned i;
  3087. for(i=0; i<p->fragCnt; ++i)
  3088. if( p->frags[i].labelSymId == labelSymId )
  3089. {
  3090. cmAudioFileH_t afH = cmNullAudioFileH;
  3091. cmAudioFileInfo_t afInfo;
  3092. cmRC_t afRC = kOkAfRC;
  3093. // open the audio file
  3094. if( cmAudioFileIsValid( afH = cmAudioFileNewOpen(wavFn, &afInfo, &afRC, p->obj.err.rpt )) == false )
  3095. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The audio file '%s' could not be opened'.",cmStringNullGuard(wavFn));
  3096. // ignore blank
  3097. if( afInfo.frameCnt == 0 )
  3098. return cmOkRC;
  3099. // allocate buffer space
  3100. unsigned j;
  3101. for(j=0; j<p->chCnt; ++j)
  3102. p->frags[i].chArray[j] = cmMemResize(cmSample_t,p->frags[i].chArray[j],afInfo.frameCnt);
  3103. p->frags[i].allocCnt = afInfo.frameCnt;
  3104. // read samples into the buffer space
  3105. unsigned chIdx = 0;
  3106. unsigned chCnt = cmMin(p->chCnt,afInfo.chCnt);
  3107. unsigned actFrmCnt = 0;
  3108. if( cmAudioFileReadSample(afH,afInfo.frameCnt,chIdx,chCnt,p->frags[i].chArray, &actFrmCnt) != kOkAfRC )
  3109. return cmCtxRtCondition(&p->obj, cmSubSysFailRC, "Read failed on the audio file '%s'.",cmStringNullGuard(wavFn));
  3110. p->frags[i].recdIdx = actFrmCnt;
  3111. return rc;
  3112. }
  3113. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'begin record'.",labelSymId);
  3114. }
  3115. cmRC_t cmRecdPlayBeginPlay( cmRecdPlay* p, unsigned labelSymId )
  3116. {
  3117. unsigned i;
  3118. for(i=0; i<p->fragCnt; ++i)
  3119. if( p->frags[i].labelSymId == labelSymId )
  3120. {
  3121. // if the frag is not already on the play list
  3122. if( p->frags[i].plink == NULL )
  3123. {
  3124. p->frags[i].playIdx = 0;
  3125. p->frags[i].fadeSmpIdx = 0;
  3126. p->frags[i].fadeDbPerSec = 0.0;
  3127. p->frags[i].plink = p->plist;
  3128. p->plist = p->frags + i;
  3129. }
  3130. return cmOkRC;
  3131. }
  3132. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'begin play'.",labelSymId);
  3133. }
  3134. cmRC_t cmRecdPlayEndPlay( cmRecdPlay* p, unsigned labelSymId )
  3135. {
  3136. cmRecdPlayFrag* fp = p->plist;
  3137. cmRecdPlayFrag* pp = NULL;
  3138. for(; fp != NULL; fp=fp->plink )
  3139. {
  3140. if( fp->labelSymId == labelSymId )
  3141. {
  3142. if( pp == NULL )
  3143. p->plist = fp->plink;
  3144. else
  3145. pp->plink = fp->plink;
  3146. fp->plink = NULL;
  3147. return cmOkRC;
  3148. }
  3149. pp = fp;
  3150. }
  3151. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'end play'.",labelSymId);
  3152. }
  3153. cmRC_t cmRecdPlayBeginFade( cmRecdPlay* p, unsigned labelSymId, double fadeDbPerSec )
  3154. {
  3155. cmRecdPlayFrag* fp = p->plist;
  3156. for(; fp != NULL; fp=fp->plink )
  3157. if( fp->labelSymId == labelSymId )
  3158. {
  3159. fp->fadeDbPerSec = -fabs(fadeDbPerSec);
  3160. return cmOkRC;
  3161. }
  3162. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'fade begin'.",labelSymId);
  3163. }
  3164. cmRC_t cmRecdPlayExec( cmRecdPlay* p, const cmSample_t** iChs, cmSample_t** oChs, unsigned chCnt, unsigned smpCnt )
  3165. {
  3166. unsigned i;
  3167. chCnt = cmMin(chCnt, p->chCnt);
  3168. //-------------------------------------------------------------------
  3169. // copy incoming audio into the look-ahead buffers
  3170. //
  3171. // if the number of incoming samples is longer than the look-head buffer
  3172. // then copy exactly maxLaSmpCnt samples from the end of the incoming sample
  3173. // buffer to the look-ahead buffer.
  3174. unsigned srcOffs = 0;
  3175. unsigned srcSmpCnt = smpCnt;
  3176. if( srcSmpCnt > p->maxLaSmpCnt )
  3177. {
  3178. // advance incoming sample buffer so that there are maxLaSmpCnt samples remaining
  3179. srcOffs = srcSmpCnt-p->maxLaSmpCnt;
  3180. srcSmpCnt = p->maxLaSmpCnt; // decrease the total samples to copy
  3181. }
  3182. // count of samples from cur posn to end of the LA buffer.
  3183. unsigned n0 = cmMin(srcSmpCnt, p->maxLaSmpCnt - p->laSmpIdx );
  3184. // count of samples past the end of the LA buffer to be wrapped into begin of buffer
  3185. unsigned n1 = srcSmpCnt>n0 ? srcSmpCnt-n0 : 0;
  3186. assert(n0+n1 == srcSmpCnt);
  3187. // copy first block to end of LA buffer
  3188. for(i=0; i<chCnt; ++i)
  3189. if( iChs[i] == NULL )
  3190. cmVOS_Zero(p->laChs[i]+p->laSmpIdx,n0);
  3191. else
  3192. cmVOS_Copy(p->laChs[i]+p->laSmpIdx,n0,iChs[i] + srcOffs);
  3193. p->laSmpIdx += n0;
  3194. if( n1!=0)
  3195. {
  3196. // copy second block to begin of LA buffer
  3197. for(i=0; i<chCnt; ++i)
  3198. if( iChs[i] == NULL )
  3199. cmVOS_Zero(p->laChs[i],n1);
  3200. else
  3201. cmVOS_Copy(p->laChs[i],n1,iChs[i] + srcOffs + n0);
  3202. p->laSmpIdx = n1;
  3203. }
  3204. //-------------------------------------------------------------------
  3205. // copy incoming audio into the active record buffers
  3206. //
  3207. cmRecdPlayFrag* fp = p->rlist;
  3208. for(; fp!=NULL; fp=fp->rlink)
  3209. {
  3210. assert( fp->recdIdx <= fp->allocCnt);
  3211. unsigned n = cmMin(fp->allocCnt - fp->recdIdx,smpCnt);
  3212. unsigned i;
  3213. for(i=0; i<p->chCnt; ++i)
  3214. if( iChs[i] == NULL )
  3215. cmVOS_Zero(fp->chArray[i] + fp->recdIdx, n );
  3216. else
  3217. cmVOS_Copy(fp->chArray[i] + fp->recdIdx, n, iChs[i] );
  3218. fp->recdIdx += n;
  3219. }
  3220. //-------------------------------------------------------------------
  3221. // copy outgoing audio out of the active play buffers
  3222. //
  3223. fp = p->plist;
  3224. for(; fp!=NULL; fp=fp->rlink)
  3225. {
  3226. assert( fp->playIdx <= fp->recdIdx);
  3227. double gain = pow(10.0,((fp->fadeSmpIdx / p->srate) * fp->fadeDbPerSec)/20.0);
  3228. unsigned n = cmMin(fp->recdIdx - fp->playIdx,smpCnt);
  3229. unsigned i;
  3230. for(i=0; i<p->chCnt; ++i)
  3231. if( oChs[i] != NULL )
  3232. cmVOS_MultVVS(oChs[i],n,fp->chArray[i] + fp->playIdx,gain);
  3233. fp->playIdx += n;
  3234. // if a fade rate has been set then advance the fade phase
  3235. if(fp->fadeDbPerSec!=0.0)
  3236. fp->fadeSmpIdx += smpCnt;
  3237. }
  3238. return cmOkRC;
  3239. }