libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmProc4.c 98KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962
  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. } _cmScMeasTimeEle_t;
  1602. typedef struct
  1603. {
  1604. unsigned setN; // set length
  1605. unsigned midiN; // count of MIDI events to match to score
  1606. unsigned alignN; // count of score events in the alignment (<= setN)
  1607. unsigned matchN; // count of positive matches
  1608. double tempo;
  1609. double value;
  1610. } _cmScMeasResult_t;
  1611. double _cmScMeasCalcTempo( const _cmScMeasTimeEle_t* b, unsigned bn, double srate )
  1612. {
  1613. assert( bn >= 2 );
  1614. assert( b[bn-1].smpIdx != cmInvalidIdx );
  1615. assert( b[0].smpIdx != cmInvalidIdx );
  1616. double durSmpCnt = b[bn-1].smpIdx - b[0].smpIdx;
  1617. double beats = 0;
  1618. unsigned i;
  1619. for(i=0; i<bn; ++i)
  1620. beats += b[i].frac;
  1621. assert(beats != 0);
  1622. return beats / (durSmpCnt / (srate * 60.0));
  1623. }
  1624. // Note: On successful completion (return!=0) the first
  1625. // and last record returned in c[cn] will be matched records.
  1626. unsigned _cmScMeasTimeAlign( cmScMeas* p, cmScMeasSet_t* sp, cmScMatchMidi_t* m, unsigned mn, _cmScMeasTimeEle_t* c, unsigned cn, _cmScMeasResult_t* rp )
  1627. {
  1628. int i,j,k;
  1629. int an = sp->sp->eleCnt;
  1630. _cmScMeasTimeEle_t* b = NULL;
  1631. int bn = 0;
  1632. bool tempoFl = false;
  1633. unsigned matchN = 0;
  1634. assert( an!=0);
  1635. // alloc a 'score set' element array
  1636. _cmScMeasTimeEle_t a[an];
  1637. // get the scLocIdx of each set element from the score
  1638. for(i=0,j=0; i<an; ++i)
  1639. if( i==0 || sp->sp->eleArray[i-1]->locIdx != sp->sp->eleArray[i]->locIdx )
  1640. {
  1641. assert( sp->sp->eleArray[i]->locIdx != cmInvalidIdx );
  1642. a[j].scLocIdx = sp->sp->eleArray[i]->locIdx;
  1643. a[j].frac = sp->sp->eleArray[i]->frac;
  1644. a[j].smpIdx = cmInvalidIdx;
  1645. a[j].cnt = 0;
  1646. ++j;
  1647. }
  1648. an = j; // set the count of unique score locations (new length of a[])
  1649. // collect the 'smpIdx' for each MIDI event which matches a set element
  1650. for(i=0; i<mn; ++i)
  1651. if( m[i].locIdx != cmInvalidIdx )
  1652. {
  1653. for(j=0; j<an; ++j)
  1654. if( p->mp->loc[m[i].locIdx].scLocIdx == a[j].scLocIdx )
  1655. {
  1656. a[j].smpIdx += m[i].smpIdx;
  1657. a[j].cnt += 1;
  1658. if( a[j].cnt == 1 )
  1659. matchN += 1; // only cnt one match per sc loc.
  1660. break;
  1661. }
  1662. }
  1663. // remove leading missing values
  1664. for(i=0; i<an; ++i)
  1665. if( a[i].smpIdx != cmInvalidIdx )
  1666. {
  1667. b = a + i;
  1668. bn = an - i;
  1669. break;
  1670. }
  1671. // remove trailing missing values
  1672. for(i=bn-1; i>=0; --i,--bn)
  1673. if( b[i].smpIdx != cmInvalidIdx )
  1674. break;
  1675. // can't measure evenness against less than 2 values
  1676. if( bn < 2 )
  1677. {
  1678. return 0;
  1679. }
  1680. assert(b[0].smpIdx != cmInvalidIdx && b[bn-1].smpIdx != cmInvalidIdx);
  1681. // calc avg. smpIdx, insert missing values, and convert b[].smpIdx to delta smp index
  1682. for(i=0,j=0; i<bn; ++i)
  1683. {
  1684. if( b[i].cnt > 1 )
  1685. b[i].smpIdx /= b[i].cnt;
  1686. if( b[i].smpIdx == cmInvalidIdx )
  1687. ++j; // incr missing value count
  1688. else
  1689. {
  1690. if( i > 0 )
  1691. {
  1692. // fill in missing values
  1693. ++j;
  1694. unsigned d = (b[i].smpIdx - b[i-j].smpIdx)/j;
  1695. for(k=0; k<j; ++k)
  1696. b[i-j+k].val = d;
  1697. }
  1698. j=0;
  1699. }
  1700. if( b[i].frac != 0 )
  1701. tempoFl = true;
  1702. }
  1703. rp->setN = an;
  1704. rp->midiN = mn;
  1705. rp->alignN = bn;
  1706. rp->matchN = matchN;
  1707. rp->tempo = 0;
  1708. rp->value = 0;
  1709. // calculate tempo
  1710. if( tempoFl )
  1711. rp->tempo = _cmScMeasCalcTempo(b,bn,p->srate);
  1712. assert(bn<=cn);
  1713. // TODO: this copy should be eliminated
  1714. // copy to output
  1715. for(i=0; i<bn && i<cn; ++i)
  1716. c[i] = b[i];
  1717. return bn;
  1718. }
  1719. double _cmScMeasEven( cmScMeas* p, cmScMeasSet_t* sp, cmScMatchMidi_t* m, unsigned mn, _cmScMeasResult_t* rp )
  1720. {
  1721. unsigned bn = sp->sp->eleCnt;
  1722. _cmScMeasTimeEle_t b[bn];
  1723. unsigned i;
  1724. if((bn = _cmScMeasTimeAlign(p,sp,m,mn,b,bn,rp)) == 0 )
  1725. return DBL_MAX;
  1726. // calc avg. delta time
  1727. double d_avg = 0;
  1728. for(i=0; i<bn-1; ++i)
  1729. d_avg += b[i].val;
  1730. d_avg /= (bn-1);
  1731. // calc std-dev of delta time
  1732. double d_sd = 0;
  1733. for(i=0; i<bn-1; ++i)
  1734. d_sd += (b[i].val-d_avg) * (b[i].val-d_avg);
  1735. // if there is no deviation then we can't compute a z-score
  1736. // (this will happen if we fill in all the missing values based on 2 values)
  1737. if( d_sd == 0 )
  1738. return 1.0;
  1739. d_sd = sqrt(d_sd/(bn-1));
  1740. // calc avg. z-score
  1741. double z = 0;
  1742. for(i=0; i<bn-1; ++i)
  1743. z += fabs(b[i].val - d_avg)/d_sd;
  1744. double val = z / (bn-1);
  1745. rp->value = val;
  1746. return val;
  1747. }
  1748. // return Tempo estimation in BPM
  1749. double _cmScMeasTempo( cmScMeas* p, cmScMeasSet_t* sp, cmScMatchMidi_t* m, unsigned mn, _cmScMeasResult_t* rp )
  1750. {
  1751. unsigned bn = sp->sp->eleCnt;
  1752. _cmScMeasTimeEle_t b[bn];
  1753. if((bn= _cmScMeasTimeAlign(p,sp,m,mn,b,bn,rp)) == 0 )
  1754. return DBL_MAX;
  1755. return rp->tempo;
  1756. }
  1757. double _cmScMeasDyn( cmScMeas* p, cmScMeasSet_t* sp, cmScMatchMidi_t* m, unsigned mn, _cmScMeasResult_t* rp )
  1758. {
  1759. typedef struct
  1760. {
  1761. unsigned scEvtIdx;
  1762. unsigned vel;
  1763. double val;
  1764. } ele_t;
  1765. int i,j;
  1766. int n = sp->sp->eleCnt;
  1767. double vv = 0;
  1768. unsigned vn = 0;
  1769. unsigned matchN = 0;
  1770. unsigned alignN = 0;
  1771. assert( n!=0);
  1772. ele_t a[n];
  1773. // get the scEvtIdx of each set element
  1774. for(i=0; i<n; ++i)
  1775. {
  1776. cmScoreEvt_t* ep = cmScoreEvt( p->mp->scH, sp->sp->eleArray[i]->index );
  1777. assert( ep != NULL );
  1778. a[i].scEvtIdx = sp->sp->eleArray[i]->index;
  1779. a[i].vel = 0;
  1780. a[i].val = ep->dynVal;
  1781. }
  1782. // set the performed vel. of each note in the set
  1783. // (if a note was not played it's a[].vel is left at 0)
  1784. for(i=0; i<mn; ++i)
  1785. if( m[i].scEvtIdx != cmInvalidIdx )
  1786. {
  1787. alignN += 1;
  1788. for(j=0; j<n; ++j)
  1789. if( m[i].scEvtIdx == a[j].scEvtIdx )
  1790. {
  1791. matchN += 1;
  1792. a[j].vel = m[i].vel;
  1793. break;
  1794. }
  1795. }
  1796. // assign a dynamics category to each note in the set
  1797. for(i=0; i<n; ++i)
  1798. if( a[i].vel > 0 )
  1799. {
  1800. unsigned mnv = 0; // lower bound for first dyn's category
  1801. for(j=0; j<p->dn; ++j)
  1802. {
  1803. if( mnv <= a[i].vel && a[i].vel < p->dynRef[j] )
  1804. {
  1805. // accum. the diff. between the ref. and performed dyn. category
  1806. vv += fabs(a[i].val - j);
  1807. vn += 1;
  1808. break;
  1809. }
  1810. mnv = p->dynRef[j];
  1811. }
  1812. assert(j<p->dn);
  1813. }
  1814. rp->setN = n;
  1815. rp->midiN = mn;
  1816. rp->alignN = alignN;
  1817. rp->matchN = matchN;
  1818. rp->tempo = 0;
  1819. rp->value = vn == 0 ? DBL_MAX : vv/vn;
  1820. return rp->value;
  1821. }
  1822. unsigned MEAS_MATCH_CNT = 0;
  1823. void _cmScMeasPrintResult( cmScMeas* p, cmScMeasSet_t* sp, _cmScMeasResult_t* rp, unsigned bli, const cmScMatchMidi_t* mb )
  1824. {
  1825. const char* label = "<none>";
  1826. switch( sp->sp->varId )
  1827. {
  1828. case kEvenVarScId:
  1829. label = "even";
  1830. break;
  1831. case kDynVarScId:
  1832. label = "dyn";
  1833. break;
  1834. case kTempoVarScId:
  1835. label = "tempo";
  1836. break;
  1837. }
  1838. const cmChar_t* msg = "";
  1839. if( rp->value == DBL_MAX )
  1840. {
  1841. msg = "Measure FAILED.";
  1842. sp->value = 0;
  1843. }
  1844. 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);
  1845. if( rp->tempo != 0 )
  1846. printf(" tempo:%f ",rp->tempo);
  1847. printf("\n");
  1848. _cmScMatchPrintPath(p->mp, p->mp->p_opt, bli, mb );
  1849. }
  1850. void _cmScMeasCalcVal( cmScMeas* p, cmScMeasSet_t* sp, int n_mii )
  1851. {
  1852. unsigned mn = 0;
  1853. int i,k = cmInvalidIdx;
  1854. if( n_mii == 0 )
  1855. return;
  1856. // Determine the count of MIDI notes to match to the set score
  1857. // by searching from the MIDI note just recieved (midiBuf[n_mii]
  1858. // back toward the beginning until a MIDI event which occurs just
  1859. // prior to the set's begScLocIdx.
  1860. for(i=n_mii; i>=0; --i)
  1861. {
  1862. if( p->midiBuf[i].locIdx != cmInvalidIdx )
  1863. {
  1864. k = i;
  1865. unsigned scLocIdx = p->mp->loc[ p->midiBuf[i].locIdx ].scLocIdx;
  1866. if( scLocIdx < sp->bsli )
  1867. break;
  1868. }
  1869. }
  1870. assert(k != cmInvalidIdx);
  1871. mn = n_mii - k + 1;
  1872. i = k;
  1873. assert(i>=0);
  1874. assert(mn>0);
  1875. // Create a copy of the the MIDI buffer to prevent the
  1876. // p->midiBuf[].locIdx from being overwritten by cmScMatchDoSync().
  1877. cmScMatchMidi_t mb[ mn ];
  1878. unsigned j;
  1879. for(j=0; j<mn; ++j)
  1880. {
  1881. mb[j] = p->midiBuf[i+j];
  1882. mb[j].locIdx = cmInvalidIdx;
  1883. }
  1884. // In general the first and last MIDI event should be assigned
  1885. // to a score location - it's possible however that no MIDI
  1886. // event's prior to the one at p->midiBuf[n_mii] were assigned.
  1887. assert( (i==0 || p->midiBuf[i].locIdx!=cmInvalidIdx) && p->midiBuf[i+mn-1].locIdx != cmInvalidIdx);
  1888. unsigned l0i = cmMin(p->midiBuf[i].locIdx,p->midiBuf[i+mn-1].locIdx);
  1889. unsigned l1i = cmMax(p->midiBuf[i].locIdx,p->midiBuf[i+mn-1].locIdx);
  1890. unsigned bli = l0i;
  1891. unsigned ln = l1i - bli + 1;
  1892. double min_cost = DBL_MAX;
  1893. _cmScMeasResult_t r;
  1894. memset(&r,0,sizeof(r));
  1895. // match MIDI to score
  1896. if( cmScMatchExec(p->mp, bli, ln, mb, mn, min_cost ) != cmOkRC )
  1897. return;
  1898. // sync the score and MIDI based on the match information
  1899. if( cmScMatchDoSync(p->mp, bli, mb, mn, NULL ) == cmInvalidIdx )
  1900. return;
  1901. if( p->mp->opt_cost != DBL_MAX )
  1902. sp->match_cost = p->mp->opt_cost / sp->sp->eleCnt;
  1903. switch( sp->sp->varId )
  1904. {
  1905. case kEvenVarScId:
  1906. sp->value = _cmScMeasEven(p, sp, mb, mn, &r );
  1907. break;
  1908. case kDynVarScId:
  1909. sp->value = _cmScMeasDyn(p, sp, mb, mn, &r );
  1910. break;
  1911. case kTempoVarScId:
  1912. sp->value = _cmScMeasTempo(p, sp, mb, mn, &r );
  1913. break;
  1914. default:
  1915. { assert(0); }
  1916. }
  1917. sp->tempo = r.tempo;
  1918. // print the result
  1919. //_cmScMeasPrintResult(p, sp, &r, bli, mb );
  1920. MEAS_MATCH_CNT++;
  1921. }
  1922. cmRC_t cmScMeasExec( cmScMeas* p, unsigned mni, unsigned locIdx, unsigned scEvtIdx, unsigned flags, unsigned smpIdx, unsigned pitch, unsigned vel )
  1923. {
  1924. cmRC_t rc = cmOkRC;
  1925. // if the midi buffer is full
  1926. if( p->mii >= p->mn )
  1927. return cmCtxRtCondition( &p->obj, cmEofRC, "The MIDI buffer is full.");
  1928. int n_mii = cmInvalidIdx;
  1929. // locate the MIDI event assoc'd with 'mni' ...
  1930. if( p->mii>0 && mni <= p->midiBuf[p->mii-1].mni )
  1931. {
  1932. if( locIdx != cmInvalidIdx )
  1933. {
  1934. for(n_mii=p->mii-1; n_mii>=0; --n_mii)
  1935. if( p->midiBuf[n_mii].mni == mni )
  1936. break;
  1937. if( n_mii<0 )
  1938. n_mii = cmInvalidIdx;
  1939. }
  1940. }
  1941. else // ... or push a new record onto p->midiBuf[]
  1942. {
  1943. n_mii = p->mii;
  1944. ++p->mii;
  1945. }
  1946. // store the MIDI event
  1947. p->midiBuf[n_mii].mni = mni;
  1948. p->midiBuf[n_mii].locIdx = locIdx;
  1949. p->midiBuf[n_mii].scEvtIdx = scEvtIdx;
  1950. p->midiBuf[n_mii].smpIdx = smpIdx;
  1951. p->midiBuf[n_mii].pitch = pitch;
  1952. p->midiBuf[n_mii].vel = vel;
  1953. // setting vsi=nsi and vsli=nsli will indicate to the calling
  1954. // program that no new sets are ready.
  1955. p->vsi = p->nsi;
  1956. p->vsli = p->nsli;
  1957. if( locIdx == cmInvalidIdx )
  1958. return cmOkRC;
  1959. //
  1960. unsigned scLocIdx = p->mp->loc[ locIdx ].scLocIdx;
  1961. unsigned maxScLocIdx = cmScoreLocCount(p->mp->scH)-1;
  1962. // if this cmScMeas object has not yet synchronized to the cmScMatcher
  1963. // (if p->nsli is not valid)
  1964. if( p->nsli == cmInvalidIdx )
  1965. {
  1966. unsigned i;
  1967. for(i=0; i<p->sn; ++i)
  1968. if( p->set[i].esli+1 == scLocIdx )
  1969. {
  1970. p->nsli = scLocIdx;
  1971. p->nsi = i;
  1972. break;
  1973. }
  1974. if(i==p->sn)
  1975. return rc;
  1976. }
  1977. p->vsi = p->nsi;
  1978. p->vsli = p->nsli;
  1979. // for each cmScore location between p->nsli and scLocIdx
  1980. for(; p->nsli<=scLocIdx && p->nsi < p->sn; ++p->nsli)
  1981. {
  1982. // if this score location index (p->nsli) is one score location
  1983. // ahead of the next sets ending location.
  1984. while( cmMin(maxScLocIdx,p->set[p->nsi].esli+1) == p->nsli )
  1985. {
  1986. // calculate the value assoc'd with p->set[p->nsi]
  1987. _cmScMeasCalcVal(p, p->set + p->nsi, n_mii );
  1988. // advance the set index
  1989. ++p->nsi;
  1990. }
  1991. }
  1992. return rc;
  1993. }
  1994. //=======================================================================================================================
  1995. cmRC_t cmScAlignScanToTimeLineEvent( cmScMatcher* p, cmTlH_t tlH, cmTlObj_t* top, unsigned endSmpIdx )
  1996. {
  1997. assert( top != NULL );
  1998. cmTlMidiEvt_t* mep = NULL;
  1999. cmRC_t rc = cmOkRC;
  2000. // as long as more MIDI events are available get the next MIDI msg
  2001. while( rc==cmOkRC && (mep = cmTlNextMidiEvtObjPtr(tlH, top, top->seqId )) != NULL )
  2002. {
  2003. top = &mep->obj;
  2004. // if the msg falls after the end of the marker then we are through
  2005. if( mep->obj.seqSmpIdx != cmInvalidIdx && mep->obj.seqSmpIdx > endSmpIdx )
  2006. break;
  2007. // if the time line MIDI msg a note-on
  2008. if( (mep->msg->status&0xf0) == kNoteOnMdId )
  2009. {
  2010. rc = cmScMatcherExec(p, mep->obj.seqSmpIdx, mep->msg->uid, mep->msg->status, mep->msg->u.chMsgPtr->d0, mep->msg->u.chMsgPtr->d1, NULL );
  2011. switch( rc )
  2012. {
  2013. case cmOkRC: // continue processing MIDI events
  2014. break;
  2015. case cmEofRC: // end of the score was encountered
  2016. break;
  2017. case cmInvalidArgRC: // p->eli was not set correctly
  2018. break;
  2019. case cmSubSysFailRC: // scan resync failed
  2020. break;
  2021. default:
  2022. { assert(0); }
  2023. }
  2024. }
  2025. }
  2026. if( rc == cmEofRC )
  2027. rc = cmOkRC;
  2028. return rc;
  2029. }
  2030. // This callback connects/feeds the cmScMeas object from the cmScMatcher object.
  2031. // (See _cmScMatcherStoreResult().)
  2032. void cmScMatcherCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
  2033. {
  2034. cmScMeas* mp = (cmScMeas*)arg;
  2035. cmScMeasExec(mp, rp->mni, rp->locIdx, rp->scEvtIdx, rp->flags, rp->smpIdx, rp->pitch, rp->vel );
  2036. }
  2037. void cmScAlignScanMarkers( cmRpt_t* rpt, cmTlH_t tlH, cmScH_t scH )
  2038. {
  2039. unsigned i;
  2040. double srate = cmTimeLineSampleRate(tlH);
  2041. unsigned midiN = 7;
  2042. unsigned scWndN = 10;
  2043. unsigned markN = 291;
  2044. unsigned dynRefArray[] = { 14, 28, 42, 56, 71, 85, 99, 113,128 };
  2045. unsigned dynRefCnt = sizeof(dynRefArray)/sizeof(dynRefArray[0]);
  2046. cmCtx* ctx = cmCtxAlloc(NULL, rpt, cmLHeapNullHandle, cmSymTblNullHandle );
  2047. cmScMeas* mp = cmScMeasAlloc(ctx,NULL,scH,srate,dynRefArray,dynRefCnt);
  2048. cmScMatcher* p = cmScMatcherAlloc(ctx,NULL,srate,scH,scWndN,midiN,cmScMatcherCb,mp);
  2049. double scoreThresh = 0.5;
  2050. unsigned candCnt = 0;
  2051. unsigned initFailCnt = 0;
  2052. unsigned otherFailCnt = 0;
  2053. unsigned scoreFailCnt = 0;
  2054. bool printFl = false;
  2055. unsigned markCharCnt = 31;
  2056. cmChar_t markText[ markCharCnt+1 ];
  2057. cmTimeSpec_t t0,t1;
  2058. cmTimeGet(&t0);
  2059. // for each marker
  2060. for(i=0; i<markN; ++i)
  2061. {
  2062. // form the marker text
  2063. snprintf(markText,markCharCnt,"Mark %i",i);
  2064. // locate the marker
  2065. cmTlMarker_t* tlmp = cmTimeLineMarkerFind( tlH, markText );
  2066. if( tlmp == NULL )
  2067. {
  2068. if( printFl )
  2069. printf("The marker '%s' was not found.\n\n",markText);
  2070. continue;
  2071. }
  2072. // skip markers which do not contain text
  2073. if( cmTextIsEmpty(tlmp->text) )
  2074. {
  2075. if( printFl )
  2076. printf("The marker '%s' is being skipped because it has no text.\n\n",markText);
  2077. continue;
  2078. }
  2079. printf("=================== MARKER:%s ===================\n",markText);
  2080. cmScMatcherReset(p,0); // reset the score follower to the beginnig of the score
  2081. cmScMeasReset(mp);
  2082. ++candCnt;
  2083. // scan to the beginning of the marker
  2084. cmRC_t rc = cmScAlignScanToTimeLineEvent(p,tlH,&tlmp->obj,tlmp->obj.seqSmpIdx+tlmp->obj.durSmpCnt);
  2085. bool pfl = true;
  2086. if( rc != cmOkRC || p->begSyncLocIdx==cmInvalidIdx)
  2087. {
  2088. bool fl = printFl;
  2089. printFl = true;
  2090. // if a no alignment was found
  2091. if( p->begSyncLocIdx == cmInvalidIdx )
  2092. rc = cmInvalidArgRC;
  2093. if( p->mni == 0 )
  2094. {
  2095. if( printFl )
  2096. printf("mark:%i midi:%i Not enough MIDI notes to fill the scan buffer.\n",i,p->mni);
  2097. pfl = false;
  2098. }
  2099. else
  2100. {
  2101. switch(rc)
  2102. {
  2103. case cmInvalidArgRC:
  2104. if( printFl )
  2105. printf("mark:%i INITIAL SYNC FAIL\n",i);
  2106. ++initFailCnt;
  2107. pfl = false;
  2108. break;
  2109. case cmSubSysFailRC:
  2110. if( printFl )
  2111. printf("mark:%i SCAN RESYNC FAIL\n",i);
  2112. ++otherFailCnt;
  2113. break;
  2114. default:
  2115. if( printFl )
  2116. printf("mark:%i UNKNOWN FAIL\n",i);
  2117. ++otherFailCnt;
  2118. }
  2119. }
  2120. printFl = fl;
  2121. }
  2122. if( pfl )
  2123. {
  2124. double fmeas = cmScMatcherFMeas(p);
  2125. if( printFl )
  2126. 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);
  2127. if( fmeas < scoreThresh )
  2128. ++scoreFailCnt;
  2129. }
  2130. //print score and match for entire marker
  2131. //cmScMatcherPrint(p);
  2132. // ONLY USE ONE MARKER DURING TESTING
  2133. // break;
  2134. if( printFl )
  2135. printf("\n");
  2136. }
  2137. printf("cand:%i fail:%i - init:%i score:%i other:%i\n\n",candCnt,initFailCnt+scoreFailCnt+otherFailCnt,initFailCnt,scoreFailCnt,otherFailCnt);
  2138. cmTimeGet(&t1);
  2139. printf("elapsed:%f\n", (double)cmTimeElapsedMicros(&t0,&t1)/1000000.0 );
  2140. cmScMatcherFree(&p);
  2141. cmScMeasFree(&mp);
  2142. cmCtxFree(&ctx);
  2143. }
  2144. //=======================================================================================================================
  2145. 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 )
  2146. {
  2147. cmScModulator* op = cmObjAlloc(cmScModulator,c,p);
  2148. if( ctx != NULL )
  2149. if( cmScModulatorInit(op,ctx,stH,srate,samplesPerCycle,fn,modLabel,cbFunc,cbArg) != cmOkRC )
  2150. cmScModulatorFree(&op);
  2151. return op;
  2152. }
  2153. cmRC_t cmScModulatorFree( cmScModulator** pp )
  2154. {
  2155. cmRC_t rc = cmOkRC;
  2156. if( pp==NULL || *pp==NULL )
  2157. return rc;
  2158. cmScModulator* p = *pp;
  2159. if((rc = cmScModulatorFinal(p)) != cmOkRC )
  2160. return rc;
  2161. //cmMemFree(p->earray);
  2162. //cmMemFree(p->xlist);
  2163. cmObjFree(pp);
  2164. return rc;
  2165. }
  2166. typedef struct
  2167. {
  2168. unsigned typeId;
  2169. unsigned minArgCnt;
  2170. const cmChar_t* label;
  2171. } _cmScModTypeMap_t;
  2172. _cmScModTypeMap_t _cmScModTypeArray[] =
  2173. {
  2174. { kDeclModTId, 0, "decl" },
  2175. { kSetModTId, 1, "set" },
  2176. { kLineModTId, 2, "line" },
  2177. { kSetLineModTId, 3, "sline" },
  2178. { kPostModTId, 4, "post" },
  2179. { kExecModTId, 5, "exec" },
  2180. { kInvalidModTId, 0, "<invalid>"}
  2181. };
  2182. const _cmScModTypeMap_t* _cmScModTypeLabelToMap( const cmChar_t* label )
  2183. {
  2184. unsigned i;
  2185. for(i=0; _cmScModTypeArray[i].typeId!=kInvalidModTId; ++i)
  2186. if( strcmp(_cmScModTypeArray[i].label,label) == 0 )
  2187. return _cmScModTypeArray + i;
  2188. return NULL;
  2189. }
  2190. const _cmScModTypeMap_t* _cmScModTypeIdToMap( unsigned typeId )
  2191. {
  2192. unsigned i;
  2193. for(i=0; _cmScModTypeArray[i].typeId!=kInvalidModTId; ++i)
  2194. if( _cmScModTypeArray[i].typeId == typeId )
  2195. return _cmScModTypeArray + i;
  2196. return NULL;
  2197. }
  2198. cmScModVar_t* _cmScModSymToVar( cmScModulator* p, unsigned varSymId )
  2199. {
  2200. cmScModVar_t* vp = p->vlist;
  2201. for(; vp!=NULL; vp=vp->vlink)
  2202. if( varSymId == vp->varSymId )
  2203. return vp;
  2204. return NULL;
  2205. }
  2206. cmScModVar_t* _cmScModulatorInsertVar( cmScModulator* p, unsigned varSymId, unsigned flags )
  2207. {
  2208. cmScModVar_t* vp = _cmScModSymToVar(p,varSymId);
  2209. // if the specified variable was not found then create one
  2210. if( vp == NULL )
  2211. {
  2212. vp = cmMemAllocZ(cmScModVar_t,1);
  2213. vp->varSymId = varSymId;
  2214. vp->outVarId = cmInvalidId;
  2215. vp->vlink = p->vlist;
  2216. p->vlist = vp;
  2217. }
  2218. vp->flags = flags;
  2219. vp->value = DBL_MAX;
  2220. vp->min = DBL_MAX;
  2221. vp->max = DBL_MAX;
  2222. vp->rate = DBL_MAX;
  2223. vp->phase = 0;
  2224. vp->entry = NULL;
  2225. vp->alink = NULL;
  2226. return vp;
  2227. }
  2228. cmScModEntry_t* _cmScModulatorInsertEntry(cmScModulator* p, cmScModEntryGroup_t* g, unsigned idx, unsigned scLocIdx, unsigned varSymId, unsigned typeId, unsigned paramCnt )
  2229. {
  2230. assert( idx < g->en );
  2231. g->earray[idx].scLocIdx = scLocIdx;
  2232. g->earray[idx].typeId = typeId;
  2233. g->earray[idx].varPtr = _cmScModulatorInsertVar(p,varSymId,0);
  2234. if( g->earray[idx].varPtr->outVarId == cmInvalidIdx )
  2235. g->earray[idx].varPtr->outVarId = p->outVarCnt++;
  2236. return g->earray + idx;
  2237. }
  2238. /*
  2239. {
  2240. [
  2241. { loc:123, mod:modlabel, var:varlabel, param:[ ] }
  2242. ]
  2243. }
  2244. */
  2245. // Parameter values are found as values of the 'data','min' or 'max' fields.
  2246. // A parameter value may be either a symbol identifier (mapped to a variable)
  2247. // or a literal number. This function determines which form the paramter
  2248. // value takes and parses it accordingly.
  2249. cmRC_t _cmScModulatorParseParam( cmScModulator* p, cmSymTblH_t stH, cmJsonNode_t* np, cmScModParam_t* pp )
  2250. {
  2251. cmRC_t rc = cmOkRC;
  2252. switch( np->typeId )
  2253. {
  2254. case kIntTId:
  2255. case kRealTId:
  2256. if( cmJsonRealValue(np, &pp->val ) != kOkJsRC )
  2257. {
  2258. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Error parsing in Modulator literal value." );
  2259. goto errLabel;
  2260. }
  2261. pp->pid = kLiteralModPId;
  2262. break;
  2263. case kStringTId:
  2264. {
  2265. const cmChar_t* label = NULL;
  2266. if( cmJsonStringValue(np, &label) != kOkJsRC )
  2267. {
  2268. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Error parsing in Modulator symbol label." );
  2269. goto errLabel;
  2270. }
  2271. pp->symId = cmSymTblRegisterSymbol(stH,label);
  2272. pp->pid = kSymbolModPId;
  2273. }
  2274. break;
  2275. default:
  2276. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Parameter value is not a number or identifier." );
  2277. goto errLabel;
  2278. break;
  2279. }
  2280. errLabel:
  2281. return rc;
  2282. }
  2283. cmRC_t _cmScModParseEntryGroup( cmScModulator* p, cmCtx_t* ctx, cmJsonH_t jsH, cmSymTblH_t stH, cmJsonNode_t* jnp, cmScModEntryGroup_t* g, const cmChar_t* fn )
  2284. {
  2285. cmRC_t rc = cmOkRC;
  2286. unsigned prvScLocIdx = cmInvalidIdx;
  2287. const cmChar_t* prvVarLabel = "<dummy>";
  2288. const cmChar_t* prvTypeLabel = NULL;
  2289. unsigned i;
  2290. // allocate the entry array
  2291. unsigned entryCnt = cmJsonChildCount(jnp);
  2292. g->earray = cmMemResizeZ(cmScModEntry_t,g->earray,entryCnt);
  2293. g->en = entryCnt;
  2294. for(i=0; i<entryCnt; ++i)
  2295. {
  2296. cmJsRC_t jsRC = kOkJsRC;
  2297. const char* errLabelPtr = NULL;
  2298. unsigned scLocIdx = cmInvalidIdx;
  2299. const cmChar_t* varLabel = NULL;
  2300. const cmChar_t* typeLabel = NULL;
  2301. cmJsonNode_t* onp = cmJsonArrayElement(jnp,i);
  2302. cmJsonNode_t* dnp = NULL;
  2303. const _cmScModTypeMap_t* map = NULL;
  2304. if((jsRC = cmJsonMemberValues( onp, &errLabelPtr,
  2305. "var", kStringTId | kOptArgJsFl, &varLabel,
  2306. "type",kStringTId | kOptArgJsFl, &typeLabel,
  2307. "loc", kIntTId | kOptArgJsFl, &scLocIdx,
  2308. NULL )) != kOkJsRC )
  2309. {
  2310. if( errLabelPtr == NULL )
  2311. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Error:%s on record at index %i in file:%s",errLabelPtr,i,cmStringNullGuard(fn) );
  2312. else
  2313. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Synax error in Modulator record at index %i in file:%s",i,cmStringNullGuard(fn) );
  2314. goto errLabel;
  2315. }
  2316. // if the score location was not given use the previous score location
  2317. if( scLocIdx == cmInvalidIdx )
  2318. scLocIdx = prvScLocIdx;
  2319. else
  2320. prvScLocIdx = scLocIdx;
  2321. // if the var label was not given use the previous one
  2322. if( varLabel == NULL )
  2323. varLabel = prvVarLabel;
  2324. else
  2325. prvVarLabel = varLabel;
  2326. if( varLabel == NULL )
  2327. {
  2328. rc = cmCtxRtCondition(&p->obj, cmInvalidArgRC, "No 'var' label has been set in mod file '%s'.",cmStringNullGuard(fn));
  2329. goto errLabel;
  2330. }
  2331. // if the type label was not given use the previous one
  2332. if( typeLabel == NULL )
  2333. typeLabel = prvTypeLabel;
  2334. else
  2335. prvTypeLabel = typeLabel;
  2336. if( typeLabel == NULL )
  2337. {
  2338. rc = cmCtxRtCondition(&p->obj, cmInvalidArgRC, "No 'type' label has been set in mod file '%s'.",cmStringNullGuard(fn));
  2339. goto errLabel;
  2340. }
  2341. // validate the entry type label
  2342. if((map = _cmScModTypeLabelToMap(typeLabel)) == NULL )
  2343. {
  2344. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Unknown entry type '%s' in Modulator record at index %i in file:%s",cmStringNullGuard(typeLabel),i,cmStringNullGuard(fn) );
  2345. goto errLabel;
  2346. }
  2347. unsigned varSymId = cmSymTblRegisterSymbol(stH,varLabel);
  2348. // get the count of the elmenets in the data array
  2349. unsigned paramCnt = cmJsonChildCount(onp);
  2350. // fill the entry record and find or create the target var
  2351. cmScModEntry_t* ep = _cmScModulatorInsertEntry(p,g,i,scLocIdx,varSymId,map->typeId,paramCnt);
  2352. typedef struct
  2353. {
  2354. const cmChar_t* label;
  2355. cmScModParam_t* param;
  2356. } map_t;
  2357. // parse the var and parameter records
  2358. map_t mapArray[] =
  2359. {
  2360. { "min", &ep->min },
  2361. { "max", &ep->max },
  2362. { "rate",&ep->rate },
  2363. { "val", &ep->beg },
  2364. { "end", &ep->end },
  2365. { "dur", &ep->dur },
  2366. { NULL, NULL }
  2367. };
  2368. unsigned j=0;
  2369. for(j=0; mapArray[j].param!=NULL; ++j)
  2370. if((dnp = cmJsonFindValue(jsH,mapArray[j].label, onp, kInvalidTId )) != NULL )
  2371. if((rc = _cmScModulatorParseParam(p,stH,dnp,mapArray[j].param)) != cmOkRC )
  2372. goto errLabel;
  2373. }
  2374. errLabel:
  2375. return rc;
  2376. }
  2377. cmRC_t _cmScModulatorParse( cmScModulator* p, cmCtx_t* ctx, cmSymTblH_t stH, const cmChar_t* fn )
  2378. {
  2379. cmRC_t rc = cmOkRC;
  2380. cmJsonNode_t* jnp = NULL;
  2381. cmJsonH_t jsH = cmJsonNullHandle;
  2382. unsigned i = cmInvalidIdx;
  2383. cmScModEntryGroup_t* g0 = NULL;
  2384. cmScModEntryGroup_t* g1 = p->glist;
  2385. // read the JSON file
  2386. if( cmJsonInitializeFromFile(&jsH, fn, ctx ) != kOkJsRC )
  2387. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "JSON file parse failed on the modulator file: %s.",cmStringNullGuard(fn) );
  2388. jnp = cmJsonRoot(jsH);
  2389. unsigned groupCnt = cmJsonChildCount(jnp);
  2390. // for each entry group
  2391. for(i=0; i<groupCnt; ++i)
  2392. {
  2393. // get the entry group record
  2394. if( g1 == NULL )
  2395. {
  2396. g1 = cmMemAllocZ(cmScModEntryGroup_t,1);
  2397. if( g0 == NULL )
  2398. p->glist = g1;
  2399. else
  2400. g0->link = g1;
  2401. }
  2402. // get the entry group pair node
  2403. cmJsonNode_t* gnp = cmJsonMemberAtIndex(jnp,i);
  2404. if( gnp == NULL || !cmJsonIsPair(gnp) )
  2405. {
  2406. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Invalid entry group at index %i in score modulator file:%s",i,cmStringNullGuard(fn) );
  2407. goto errLabel;
  2408. }
  2409. // store the group label
  2410. g1->symId = cmSymTblRegisterSymbol(stH,cmJsonPairLabel(gnp));
  2411. // get and validate the group array value
  2412. if( (gnp = cmJsonPairValue(gnp))==NULL || cmJsonIsArray(gnp)==false )
  2413. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Group entry '%s' does not contain an array of entries in file:%s",cmStringNullGuard(cmJsonPairLabel(gnp)),cmStringNullGuard(fn) );
  2414. // parse the entry group
  2415. if((rc = _cmScModParseEntryGroup( p, ctx, jsH, stH, gnp, g1, fn )) != cmOkRC )
  2416. {
  2417. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Syntax error in entry group at index %i in score modulator file:%s",i,cmStringNullGuard(fn) );
  2418. goto errLabel;
  2419. }
  2420. g0 = g1;
  2421. g1 = g1->link;
  2422. }
  2423. errLabel:
  2424. // release the JSON tree
  2425. if( cmJsonIsValid(jsH) )
  2426. cmJsonFinalize(&jsH);
  2427. return rc;
  2428. }
  2429. cmRC_t _cmScModulatorReset( cmScModulator* p, cmCtx_t* ctx, unsigned scLocIdx, unsigned entryGroupSymId )
  2430. {
  2431. cmRC_t rc = cmOkRC;
  2432. p->alist = NULL;
  2433. p->elist = NULL;
  2434. p->nei = 0;
  2435. p->outVarCnt = 0;
  2436. // reload the file
  2437. if((rc = _cmScModulatorParse(p,ctx,p->stH,p->fn)) != cmOkRC )
  2438. goto errLabel;
  2439. // select the entry group to execute
  2440. if( entryGroupSymId != cmInvalidId )
  2441. {
  2442. cmScModEntryGroup_t* g = p->glist;
  2443. for(; g!=NULL; g=g->link)
  2444. if( g->symId == entryGroupSymId )
  2445. {
  2446. cmCtxPrint(p->obj.ctx,"%s selected.\n",cmSymTblLabel(p->stH,entryGroupSymId));
  2447. p->xlist = p->glist;
  2448. break;
  2449. }
  2450. }
  2451. // if an active entry group has not been set - set it to the first entry group
  2452. if( p->xlist == NULL )
  2453. {
  2454. if( entryGroupSymId != cmInvalidId )
  2455. 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) );
  2456. p->xlist = p->glist;
  2457. }
  2458. // clear the active flag on all variables
  2459. cmScModVar_t* vp = p->vlist;
  2460. for(; vp!=NULL; vp=vp->vlink)
  2461. {
  2462. vp->flags = cmClrFlag(vp->flags,kActiveModFl);
  2463. vp->alink = NULL;
  2464. }
  2465. errLabel:
  2466. return rc;
  2467. }
  2468. 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 )
  2469. {
  2470. cmRC_t rc;
  2471. if((rc = cmScModulatorFinal(p)) != cmOkRC )
  2472. return rc;
  2473. p->fn = cmMemAllocStr(fn);
  2474. p->stH = stH;
  2475. p->modSymId = cmSymTblRegisterSymbol(stH,modLabel);
  2476. p->cbFunc = cbFunc;
  2477. p->cbArg = cbArg;
  2478. p->samplesPerCycle = samplesPerCycle;
  2479. p->srate = srate;
  2480. if( rc != cmOkRC )
  2481. cmScModulatorFinal(p);
  2482. else
  2483. _cmScModulatorReset(p,ctx,0,cmInvalidId);
  2484. return rc;
  2485. }
  2486. cmRC_t cmScModulatorFinal( cmScModulator* p )
  2487. {
  2488. cmMemFree(p->fn);
  2489. // release each var record
  2490. cmScModVar_t* vp = p->vlist;
  2491. while( vp!=NULL )
  2492. {
  2493. cmScModVar_t* np = vp->vlink;
  2494. cmMemFree(vp);
  2495. vp=np;
  2496. }
  2497. // release each group record
  2498. cmScModEntryGroup_t* g = p->glist;
  2499. while( g != NULL )
  2500. {
  2501. cmScModEntryGroup_t* g0 = g->link;
  2502. cmMemFree(g->earray);
  2503. cmMemFree(g);
  2504. g = g0;
  2505. }
  2506. p->glist = NULL;
  2507. return cmOkRC;
  2508. }
  2509. unsigned cmScModulatorOutVarCount( cmScModulator* p )
  2510. { return p->outVarCnt; }
  2511. cmScModVar_t* cmScModulatorOutVar( cmScModulator* p, unsigned idx )
  2512. {
  2513. unsigned i;
  2514. cmScModEntryGroup_t* g = p->glist;
  2515. for(; g!=NULL; g=g->link)
  2516. for(i=0; i<g->en; ++i)
  2517. if( g->earray[i].varPtr->outVarId == idx )
  2518. return g->earray[i].varPtr;
  2519. return NULL;
  2520. }
  2521. cmRC_t cmScModulatorSetValue( cmScModulator* p, unsigned varSymId, double value, double min, double max )
  2522. {
  2523. cmScModVar_t* vp;
  2524. // if the var does not exist ....
  2525. if((vp = _cmScModSymToVar(p, varSymId )) == NULL )
  2526. {
  2527. // ... then create it
  2528. vp = _cmScModulatorInsertVar(p,varSymId,kCalcModFl);
  2529. assert(vp!=NULL);
  2530. }
  2531. assert( min <= max);
  2532. vp->min = min;
  2533. vp->max = max;
  2534. vp->value = value;
  2535. return cmOkRC;
  2536. }
  2537. cmRC_t cmScModulatorReset( cmScModulator* p, cmCtx_t* ctx, unsigned scLocIdx, unsigned entryGroupSymId )
  2538. {
  2539. _cmScModulatorReset(p,ctx,scLocIdx,entryGroupSymId);
  2540. return cmScModulatorExec(p,scLocIdx);
  2541. }
  2542. void _cmScModUnlinkActive( cmScModulator* p, cmScModVar_t* vp, cmScModVar_t* pp )
  2543. {
  2544. // if vp is the first link on the chain
  2545. if( vp == p->alist )
  2546. p->alist = vp->alink;
  2547. // if vp is the last link on the chain
  2548. if( vp == p->elist )
  2549. p->elist = pp;
  2550. if( pp != NULL )
  2551. pp->alink = vp->alink;
  2552. vp->flags = cmClrFlag(vp->flags,kActiveModFl);
  2553. vp->alink = NULL;
  2554. vp->entry = NULL;
  2555. }
  2556. // If the requested parameter has a value then return it in *valPtr.
  2557. // If it does not then do nothing. This function applies scaling to RHS values.
  2558. cmRC_t _cmScModGetParam( cmScModulator* p, const cmScModParam_t* pp, double* valPtr )
  2559. {
  2560. cmRC_t rc = cmOkRC;
  2561. switch( pp->pid )
  2562. {
  2563. case kInvalidModPId:
  2564. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "An invalid parameter was encountered.");
  2565. goto errLabel;
  2566. break;
  2567. case kLiteralModPId:
  2568. *valPtr = pp->val;
  2569. break;
  2570. case kSymbolModPId:
  2571. {
  2572. cmScModVar_t* vp;
  2573. // get a pointer to the parameter variable
  2574. if((vp = _cmScModSymToVar(p, pp->symId )) == NULL )
  2575. {
  2576. rc = cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Variable '%s' not found.",cmSymTblLabel(p->stH,pp->symId));
  2577. goto errLabel;
  2578. }
  2579. // if this is not a 'calculated' paramter then scale it here.
  2580. if( cmIsFlag(vp->flags,kCalcModFl ) && vp->min!=DBL_MAX && vp->max!=DBL_MAX )
  2581. *valPtr = (vp->value - vp->min)/(vp->max-vp->min);
  2582. else
  2583. *valPtr = vp->value;
  2584. }
  2585. break;
  2586. default:
  2587. { assert(0); }
  2588. }
  2589. errLabel:
  2590. return rc;
  2591. }
  2592. cmRC_t _cmScModActivateEntries( cmScModulator* p, cmScModEntry_t* earray, unsigned* idxRef, unsigned cnt, unsigned scLocIdx );
  2593. cmRC_t _cmScModActivateGroup( cmScModulator* p, cmScModEntry_t* ep )
  2594. {
  2595. cmScModEntryGroup_t* g = p->glist;
  2596. for(; g!=NULL; g=g->link)
  2597. if( g->symId == ep->beg.symId )
  2598. {
  2599. unsigned idx = 0;
  2600. return _cmScModActivateEntries( p, g->earray, &idx, g->en, ep->beg.symId );
  2601. }
  2602. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "Entry group '%s' not found.",cmSymTblLabel(p->stH,ep->beg.symId));
  2603. }
  2604. // Type specific variable activation -
  2605. cmRC_t _cmScModActivate(cmScModulator* p, cmScModEntry_t* ep )
  2606. {
  2607. cmRC_t rc = cmOkRC;
  2608. cmScModVar_t* vp = ep->varPtr;
  2609. // optionally update the min/max/rate values in the target var
  2610. if( ep->min.pid != kInvalidModPId )
  2611. if((rc = _cmScModGetParam(p,&ep->min,&vp->min)) != cmOkRC )
  2612. goto errLabel;
  2613. if( ep->max.pid != kInvalidModPId )
  2614. if((rc = _cmScModGetParam(p,&ep->max,&vp->max)) != cmOkRC )
  2615. goto errLabel;
  2616. if( ep->rate.pid != kInvalidModPId )
  2617. if((rc = _cmScModGetParam(p,&ep->rate,&vp->rate)) != cmOkRC )
  2618. goto errLabel;
  2619. switch( ep->typeId )
  2620. {
  2621. case kDeclModTId:
  2622. case kSetModTId:
  2623. break;
  2624. case kLineModTId:
  2625. vp->v0 = vp->value;
  2626. vp->phase = 0;
  2627. break;
  2628. case kSetLineModTId:
  2629. _cmScModGetParam(p,&ep->beg,&vp->value); // starting value
  2630. vp->v0 = vp->value; // set initial value
  2631. vp->phase = 0; // reset phase
  2632. break;
  2633. case kPostModTId:
  2634. p->postFl = vp->value;
  2635. break;
  2636. case kExecModTId:
  2637. rc = _cmScModActivateGroup(p,ep);
  2638. break;
  2639. default:
  2640. { assert(0); }
  2641. }
  2642. errLabel:
  2643. return rc;
  2644. }
  2645. // Callback the application with a new variable value.
  2646. cmRC_t _cmScModExecSendValue( cmScModulator* p, cmScModVar_t* vp )
  2647. {
  2648. cmRC_t rc = cmOkRC;
  2649. bool sendFl = true;
  2650. double v = vp->value;
  2651. // scale the output value - this is equiv to scaling the LHS
  2652. if( cmIsFlag(vp->flags,kCalcModFl) && vp->min!=DBL_MAX && vp->max!=DBL_MAX )
  2653. v = vp->min + v * (vp->max - vp->min);
  2654. // if an output rate throttle is in effect ....
  2655. if( vp->rate!=DBL_MAX && vp->phase!=0 )
  2656. sendFl = remainder(vp->phase*p->samplesPerCycle, p->srate*vp->rate/1000 ) < p->samplesPerCycle;
  2657. if(sendFl)
  2658. p->cbFunc(p->cbArg,vp->varSymId,v,p->postFl);
  2659. return rc;
  2660. }
  2661. // Return true if vp should be deactivated otherwise return false.
  2662. bool _cmScModExec( cmScModulator* p, cmScModVar_t* vp )
  2663. {
  2664. cmRC_t rc = cmOkRC;
  2665. bool fl = false;
  2666. bool sendFl = true;
  2667. switch( vp->entry->typeId )
  2668. {
  2669. case kDeclModTId:
  2670. sendFl = false;
  2671. fl = true;
  2672. break;
  2673. case kSetModTId:
  2674. {
  2675. if((rc = _cmScModGetParam(p,&vp->entry->beg,&vp->value)) != cmOkRC )
  2676. goto errLabel;
  2677. vp->phase = 0; // force the value to be sent
  2678. fl = true;
  2679. }
  2680. break;
  2681. case kSetLineModTId:
  2682. case kLineModTId:
  2683. {
  2684. double v1=0, td=0;
  2685. // get the target value
  2686. if((rc = _cmScModGetParam(p,&vp->entry->end,&v1)) != cmOkRC)
  2687. goto errLabel;
  2688. // get the time duration
  2689. if((rc = _cmScModGetParam(p,&vp->entry->dur,&td)) != cmOkRC)
  2690. goto errLabel;
  2691. double v = vp->v0 + (v1-vp->v0) * (vp->phase * p->samplesPerCycle) / (p->srate * td);
  2692. if((fl = (vp->value <= v1 && v >= v1) || (vp->value >= v1 && v <= v1 )) == true )
  2693. v = v1;
  2694. vp->value = v;
  2695. }
  2696. break;
  2697. case kPostModTId:
  2698. sendFl = false;
  2699. break;
  2700. case kExecModTId:
  2701. sendFl = false;
  2702. break;
  2703. default:
  2704. { assert(0); }
  2705. }
  2706. // notify the application that a new variable value has been generated
  2707. if(sendFl)
  2708. {
  2709. rc = _cmScModExecSendValue(p,vp);
  2710. // increment the phase - after send because send notices when phase is zero
  2711. vp->phase += 1;
  2712. }
  2713. errLabel:
  2714. if( rc != cmOkRC )
  2715. fl = true;
  2716. return fl;
  2717. }
  2718. // Execute the entries in xparray[] begining with entry xparray[idx] and continuing until:
  2719. // 1) cnt - idx entries have been executed
  2720. // 2) an entry is located whose scLocIdx != scLocIdx and also not -1
  2721. cmRC_t _cmScModActivateEntries( cmScModulator* p, cmScModEntry_t* earray, unsigned* idxRef, unsigned cnt, unsigned scLocIdx )
  2722. {
  2723. assert( idxRef != NULL );
  2724. cmRC_t trc;
  2725. cmRC_t rc = cmOkRC;
  2726. unsigned idx = *idxRef;
  2727. // trigger entries that have expired since the last call to this function
  2728. for(; idx<cnt && (earray[idx].scLocIdx==-1 || earray[idx].scLocIdx<=scLocIdx); ++idx)
  2729. {
  2730. cmScModEntry_t* ep = earray + idx;
  2731. // if the variable assoc'd with this entry is not on the active list ...
  2732. if( cmIsFlag(ep->varPtr->flags,kActiveModFl) == false )
  2733. {
  2734. // ... then append it to the end of the active list ...
  2735. ep->varPtr->flags |= kActiveModFl;
  2736. if( p->elist == NULL )
  2737. p->elist = ep->varPtr;
  2738. else
  2739. {
  2740. p->elist->alink = ep->varPtr;
  2741. p->elist = ep->varPtr;
  2742. }
  2743. p->elist->alink = NULL;
  2744. if( p->alist == NULL )
  2745. p->alist = ep->varPtr;
  2746. }
  2747. // do type specific activation
  2748. if((trc = _cmScModActivate(p,ep)) != cmOkRC )
  2749. rc = trc;
  2750. ep->varPtr->entry = ep;
  2751. }
  2752. *idxRef = idx;
  2753. return rc;
  2754. }
  2755. cmRC_t cmScModulatorExec( cmScModulator* p, unsigned scLocIdx )
  2756. {
  2757. cmRC_t rc = cmOkRC;
  2758. rc = _cmScModActivateEntries(p, p->xlist->earray, &p->nei, p->xlist->en, scLocIdx );
  2759. // Update the active variables
  2760. cmScModVar_t* pp = NULL;
  2761. cmScModVar_t* vp = p->alist;
  2762. for(; vp!=NULL; vp=vp->alink)
  2763. {
  2764. if( _cmScModExec(p,vp) )
  2765. _cmScModUnlinkActive(p,vp,pp);
  2766. else
  2767. pp = vp;
  2768. }
  2769. return rc;
  2770. }
  2771. void _cmScModDumpParam( cmScModulator* p, const cmChar_t* label, cmScModParam_t* pp )
  2772. {
  2773. printf("%s: ",label);
  2774. switch( pp->pid )
  2775. {
  2776. case kInvalidModPId:
  2777. printf("<invalid>");
  2778. break;
  2779. case kLiteralModPId:
  2780. if( pp->val == DBL_MAX )
  2781. printf("<max> ");
  2782. else
  2783. printf("%f ",pp->val);
  2784. break;
  2785. case kSymbolModPId:
  2786. printf("%s ",cmSymTblLabel(p->stH,pp->symId));
  2787. break;
  2788. default:
  2789. { assert(0); }
  2790. }
  2791. }
  2792. void _cmScModDumpVal( cmChar_t* label, double val )
  2793. {
  2794. printf("%s:",label);
  2795. if( val == DBL_MAX )
  2796. printf("<max> " );
  2797. else
  2798. printf("%f ",val);
  2799. }
  2800. void _cmScModDumpVar( cmScModulator* p, const cmScModVar_t* vp )
  2801. {
  2802. printf("%7s %3i fl:0x%x entry:%p alink:%p ",cmSymTblLabel(p->stH,vp->varSymId),vp->outVarId,vp->flags,vp->entry,vp->alink);
  2803. _cmScModDumpVal("val",vp->value);
  2804. _cmScModDumpVal("min",vp->min);
  2805. _cmScModDumpVal("max",vp->max);
  2806. _cmScModDumpVal("rate",vp->rate);
  2807. _cmScModDumpVal("v0",vp->v0);
  2808. }
  2809. cmRC_t cmScModulatorDump( cmScModulator* p )
  2810. {
  2811. cmRC_t rc = cmOkRC;
  2812. printf("MOD:\n");
  2813. printf("nei:%i alist:%p outVarCnt:%i\n",p->nei,p->alist,p->outVarCnt);
  2814. printf("ENTRIES:\n");
  2815. cmScModEntryGroup_t* g = p->glist;
  2816. for(; g!=NULL; g=g->link)
  2817. {
  2818. printf("%s\n",cmSymTblLabel(p->stH,g->symId));
  2819. unsigned i;
  2820. for(i=0; i<g->en; ++i)
  2821. {
  2822. cmScModEntry_t* ep = g->earray + i;
  2823. const _cmScModTypeMap_t* tm = _cmScModTypeIdToMap( ep->typeId );
  2824. printf("%3i ",i);
  2825. printf("%10i ",ep->scLocIdx);
  2826. printf("%5s %7s", tm==NULL ? "invld" : tm->label, cmSymTblLabel(p->stH,ep->varPtr->varSymId));
  2827. _cmScModDumpParam(p," beg", &ep->beg);
  2828. _cmScModDumpParam(p," end", &ep->end);
  2829. _cmScModDumpParam(p," min", &ep->min);
  2830. _cmScModDumpParam(p," max", &ep->max);
  2831. _cmScModDumpParam(p," rate",&ep->rate);
  2832. printf("\n");
  2833. }
  2834. }
  2835. printf("VARIABLES\n");
  2836. cmScModVar_t* vp = p->vlist;
  2837. for(; vp!=NULL; vp=vp->vlink)
  2838. {
  2839. _cmScModDumpVar(p,vp);
  2840. printf("\n");
  2841. }
  2842. return rc;
  2843. }
  2844. //=======================================================================================================================
  2845. cmRecdPlay* cmRecdPlayAlloc( cmCtx* c, cmRecdPlay* p, double srate, unsigned fragCnt, unsigned chCnt, double initFragSecs, double maxLaSecs, double curLaSecs )
  2846. {
  2847. cmRecdPlay* op = cmObjAlloc(cmRecdPlay,c,p);
  2848. if( cmRecdPlayInit(op,srate,fragCnt,chCnt,initFragSecs,maxLaSecs,curLaSecs) != cmOkRC )
  2849. cmRecdPlayFree(&op);
  2850. return op;
  2851. }
  2852. cmRC_t cmRecdPlayFree( cmRecdPlay** pp )
  2853. {
  2854. cmRC_t rc = cmOkRC;
  2855. if( pp==NULL || *pp==NULL )
  2856. return rc;
  2857. cmRecdPlay* p = *pp;
  2858. if((rc = cmRecdPlayFinal(p)) != cmOkRC )
  2859. return rc;
  2860. cmObjFree(pp);
  2861. return rc;
  2862. }
  2863. cmRC_t cmRecdPlayInit( cmRecdPlay* p, double srate, unsigned fragCnt, unsigned chCnt, double initFragSecs, double maxLaSecs, double curLaSecs )
  2864. {
  2865. cmRC_t rc;
  2866. unsigned i;
  2867. if( curLaSecs > maxLaSecs )
  2868. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The initial look-ahead time %f is greater than the maximum look-ahead time %f.",curLaSecs,maxLaSecs);
  2869. if((rc = cmRecdPlayFinal(p)) != cmOkRC )
  2870. return rc;
  2871. if( chCnt == 0 )
  2872. return cmOkRC;
  2873. p->frags = cmMemAllocZ(cmRecdPlayFrag,fragCnt);
  2874. p->fragCnt = fragCnt;
  2875. p->srate = srate;
  2876. p->chCnt = chCnt;
  2877. p->initFragSecs = initFragSecs;
  2878. p->maxLaSmpCnt = floor(maxLaSecs*srate);
  2879. p->curLaSmpCnt = floor(curLaSecs*srate);
  2880. p->laChs = cmMemAllocZ(cmSample_t*,chCnt);
  2881. p->laSmpIdx = 0;
  2882. for(i=0; i<chCnt; ++i)
  2883. p->laChs[i] = cmMemAllocZ(cmSample_t,p->maxLaSmpCnt);
  2884. return rc;
  2885. }
  2886. cmRC_t cmRecdPlayFinal( cmRecdPlay* p )
  2887. {
  2888. unsigned i,j;
  2889. // free the fragments
  2890. for(i=0; i<p->fragCnt; ++i)
  2891. {
  2892. for(j=0; j<p->chCnt; ++j)
  2893. cmMemFree(p->frags[i].chArray[j]);
  2894. cmMemFree(p->frags[i].chArray);
  2895. }
  2896. // free the look-ahead buffers
  2897. for(i=0; i<p->chCnt; ++i)
  2898. cmMemFree(p->laChs[i]);
  2899. cmMemPtrFree(&p->laChs);
  2900. cmMemPtrFree(&p->frags);
  2901. p->fragCnt = 0;
  2902. p->chCnt = 0;
  2903. p->rlist = NULL;
  2904. p->plist = NULL;
  2905. return cmOkRC;
  2906. }
  2907. cmRC_t cmRecdPlayRegisterFrag( cmRecdPlay* p, unsigned fragIdx, unsigned labelSymId )
  2908. {
  2909. assert( fragIdx < p->fragCnt );
  2910. unsigned i;
  2911. p->frags[ fragIdx ].labelSymId = labelSymId;
  2912. p->frags[ fragIdx ].chArray = cmMemResizeZ(cmSample_t*,p->frags[fragIdx].chArray,p->chCnt);
  2913. for(i=0; i<p->chCnt; ++i)
  2914. {
  2915. p->frags[ fragIdx ].allocCnt = floor(p->initFragSecs * p->srate);
  2916. p->frags[ fragIdx ].chArray[i] = cmMemResizeZ(cmSample_t,p->frags[ fragIdx ].chArray[i],p->frags[fragIdx].allocCnt);
  2917. }
  2918. return cmOkRC;
  2919. }
  2920. cmRC_t cmRecdPlaySetLaSecs( cmRecdPlay* p, double curLaSecs )
  2921. {
  2922. p->curLaSmpCnt = floor(curLaSecs*p->srate);
  2923. return cmOkRC;
  2924. }
  2925. cmRC_t cmRecdPlayRewind( cmRecdPlay* p )
  2926. {
  2927. unsigned i;
  2928. // zero the look-ahead buffers
  2929. p->laSmpIdx = 0;
  2930. for(i=0; i<p->chCnt; ++i)
  2931. cmVOS_Zero(p->laChs[i],p->maxLaSmpCnt);
  2932. // remove all the active players
  2933. while( p->plist != NULL )
  2934. cmRecdPlayEndPlay(p,p->plist->labelSymId);
  2935. // remove all the active recorders
  2936. while( p->rlist != NULL )
  2937. cmRecdPlayEndRecord(p,p->rlist->labelSymId);
  2938. // rewind all the fragments play posn.
  2939. for(i=0; i<p->fragCnt; ++i)
  2940. p->frags[i].playIdx = 0;
  2941. return cmOkRC;
  2942. }
  2943. cmRC_t cmRecdPlayBeginRecord( cmRecdPlay* p, unsigned labelSymId )
  2944. {
  2945. unsigned i;
  2946. for(i=0; i<p->fragCnt; ++i)
  2947. if( p->frags[i].labelSymId == labelSymId )
  2948. {
  2949. // if the frag is not already on the recd list
  2950. if( p->frags[i].rlink == NULL )
  2951. {
  2952. p->frags[i].recdIdx = 0;
  2953. p->frags[i].playIdx = 0;
  2954. p->frags[i].rlink = p->rlist;
  2955. p->rlist = p->frags + i;
  2956. // handle LA buf longer than frag buf.
  2957. int cpyCnt = cmMin(p->curLaSmpCnt,p->frags[i].allocCnt);
  2958. // go backwards in LA buf from newest sample to find init src offset
  2959. int srcOffs = p->laSmpIdx - cpyCnt;
  2960. // if the src is before the first sample in the LA buf then wrap to end of buf
  2961. if( srcOffs < 0 )
  2962. srcOffs += p->maxLaSmpCnt;
  2963. assert( 0 <= srcOffs && srcOffs < p->maxLaSmpCnt );
  2964. // cnt of samples to copy from LA buf (limited by end of LA buf)
  2965. int n0 = cmMin(cpyCnt,p->maxLaSmpCnt - srcOffs);
  2966. // if necessary wrap to begin of LA buf for remaining samples
  2967. int n1 = cpyCnt>n0 ? n1 = cpyCnt-n0 : 0;
  2968. int j;
  2969. assert(n0+n1 == cpyCnt );
  2970. for(j=0; j<p->chCnt; ++j)
  2971. cmVOS_Copy(p->frags[i].chArray[j],n0,p->laChs[j]+srcOffs);
  2972. if( n1 > 0 )
  2973. {
  2974. for(j=0; j<p->chCnt; ++j)
  2975. cmVOS_Copy(p->frags[i].chArray[j]+n0,n1,p->laChs[j]);
  2976. }
  2977. p->frags[i].recdIdx = cpyCnt;
  2978. p->frags[i].playIdx = 0;
  2979. }
  2980. return cmOkRC;
  2981. }
  2982. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'begin record'.",labelSymId);
  2983. }
  2984. cmRC_t cmRecdPlayEndRecord( cmRecdPlay* p, unsigned labelSymId )
  2985. {
  2986. cmRecdPlayFrag* fp = p->rlist;
  2987. cmRecdPlayFrag* pp = NULL;
  2988. for(; fp != NULL; fp=fp->rlink )
  2989. {
  2990. if( fp->labelSymId == labelSymId )
  2991. {
  2992. if( pp == NULL )
  2993. p->rlist = fp->rlink;
  2994. else
  2995. pp->rlink = fp->rlink;
  2996. fp->rlink = NULL;
  2997. return cmOkRC;
  2998. }
  2999. pp = fp;
  3000. }
  3001. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'end record'.",labelSymId);
  3002. }
  3003. cmRC_t cmRecdPlayInsertRecord(cmRecdPlay* p, unsigned labelSymId, const cmChar_t* wavFn )
  3004. {
  3005. cmRC_t rc = cmOkRC;
  3006. unsigned i;
  3007. for(i=0; i<p->fragCnt; ++i)
  3008. if( p->frags[i].labelSymId == labelSymId )
  3009. {
  3010. cmAudioFileH_t afH = cmNullAudioFileH;
  3011. cmAudioFileInfo_t afInfo;
  3012. cmRC_t afRC = kOkAfRC;
  3013. // open the audio file
  3014. if( cmAudioFileIsValid( afH = cmAudioFileNewOpen(wavFn, &afInfo, &afRC, p->obj.err.rpt )) == false )
  3015. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The audio file '%s' could not be opened'.",cmStringNullGuard(wavFn));
  3016. // ignore blank
  3017. if( afInfo.frameCnt == 0 )
  3018. return cmOkRC;
  3019. // allocate buffer space
  3020. unsigned j;
  3021. for(j=0; j<p->chCnt; ++j)
  3022. p->frags[i].chArray[j] = cmMemResize(cmSample_t,p->frags[i].chArray[j],afInfo.frameCnt);
  3023. p->frags[i].allocCnt = afInfo.frameCnt;
  3024. // read samples into the buffer space
  3025. unsigned chIdx = 0;
  3026. unsigned chCnt = cmMin(p->chCnt,afInfo.chCnt);
  3027. unsigned actFrmCnt = 0;
  3028. if( cmAudioFileReadSample(afH,afInfo.frameCnt,chIdx,chCnt,p->frags[i].chArray, &actFrmCnt) != kOkAfRC )
  3029. return cmCtxRtCondition(&p->obj, cmSubSysFailRC, "Read failed on the audio file '%s'.",cmStringNullGuard(wavFn));
  3030. p->frags[i].recdIdx = actFrmCnt;
  3031. return rc;
  3032. }
  3033. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'begin record'.",labelSymId);
  3034. }
  3035. cmRC_t cmRecdPlayBeginPlay( cmRecdPlay* p, unsigned labelSymId )
  3036. {
  3037. unsigned i;
  3038. for(i=0; i<p->fragCnt; ++i)
  3039. if( p->frags[i].labelSymId == labelSymId )
  3040. {
  3041. // if the frag is not already on the play list
  3042. if( p->frags[i].plink == NULL )
  3043. {
  3044. p->frags[i].playIdx = 0;
  3045. p->frags[i].fadeSmpIdx = 0;
  3046. p->frags[i].fadeDbPerSec = 0.0;
  3047. p->frags[i].plink = p->plist;
  3048. p->plist = p->frags + i;
  3049. }
  3050. return cmOkRC;
  3051. }
  3052. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'begin play'.",labelSymId);
  3053. }
  3054. cmRC_t cmRecdPlayEndPlay( cmRecdPlay* p, unsigned labelSymId )
  3055. {
  3056. cmRecdPlayFrag* fp = p->plist;
  3057. cmRecdPlayFrag* pp = NULL;
  3058. for(; fp != NULL; fp=fp->plink )
  3059. {
  3060. if( fp->labelSymId == labelSymId )
  3061. {
  3062. if( pp == NULL )
  3063. p->plist = fp->plink;
  3064. else
  3065. pp->plink = fp->plink;
  3066. fp->plink = NULL;
  3067. return cmOkRC;
  3068. }
  3069. pp = fp;
  3070. }
  3071. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'end play'.",labelSymId);
  3072. }
  3073. cmRC_t cmRecdPlayBeginFade( cmRecdPlay* p, unsigned labelSymId, double fadeDbPerSec )
  3074. {
  3075. cmRecdPlayFrag* fp = p->plist;
  3076. for(; fp != NULL; fp=fp->plink )
  3077. if( fp->labelSymId == labelSymId )
  3078. {
  3079. fp->fadeDbPerSec = -fabs(fadeDbPerSec);
  3080. return cmOkRC;
  3081. }
  3082. return cmCtxRtCondition( &p->obj, cmInvalidArgRC, "The fragment label symbol id '%i' not found for 'fade begin'.",labelSymId);
  3083. }
  3084. cmRC_t cmRecdPlayExec( cmRecdPlay* p, const cmSample_t** iChs, cmSample_t** oChs, unsigned chCnt, unsigned smpCnt )
  3085. {
  3086. unsigned i;
  3087. chCnt = cmMin(chCnt, p->chCnt);
  3088. //-------------------------------------------------------------------
  3089. // copy incoming audio into the look-ahead buffers
  3090. //
  3091. // if the number of incoming samples is longer than the look-head buffer
  3092. // then copy exactly maxLaSmpCnt samples from the end of the incoming sample
  3093. // buffer to the look-ahead buffer.
  3094. unsigned srcOffs = 0;
  3095. unsigned srcSmpCnt = smpCnt;
  3096. if( srcSmpCnt > p->maxLaSmpCnt )
  3097. {
  3098. // advance incoming sample buffer so that there are maxLaSmpCnt samples remaining
  3099. srcOffs = srcSmpCnt-p->maxLaSmpCnt;
  3100. srcSmpCnt = p->maxLaSmpCnt; // decrease the total samples to copy
  3101. }
  3102. // count of samples from cur posn to end of the LA buffer.
  3103. unsigned n0 = cmMin(srcSmpCnt, p->maxLaSmpCnt - p->laSmpIdx );
  3104. // count of samples past the end of the LA buffer to be wrapped into begin of buffer
  3105. unsigned n1 = srcSmpCnt>n0 ? srcSmpCnt-n0 : 0;
  3106. assert(n0+n1 == srcSmpCnt);
  3107. // copy first block to end of LA buffer
  3108. for(i=0; i<chCnt; ++i)
  3109. if( iChs[i] == NULL )
  3110. cmVOS_Zero(p->laChs[i]+p->laSmpIdx,n0);
  3111. else
  3112. cmVOS_Copy(p->laChs[i]+p->laSmpIdx,n0,iChs[i] + srcOffs);
  3113. p->laSmpIdx += n0;
  3114. if( n1!=0)
  3115. {
  3116. // copy second block to begin of LA buffer
  3117. for(i=0; i<chCnt; ++i)
  3118. if( iChs[i] == NULL )
  3119. cmVOS_Zero(p->laChs[i],n1);
  3120. else
  3121. cmVOS_Copy(p->laChs[i],n1,iChs[i] + srcOffs + n0);
  3122. p->laSmpIdx = n1;
  3123. }
  3124. //-------------------------------------------------------------------
  3125. // copy incoming audio into the active record buffers
  3126. //
  3127. cmRecdPlayFrag* fp = p->rlist;
  3128. for(; fp!=NULL; fp=fp->rlink)
  3129. {
  3130. assert( fp->recdIdx <= fp->allocCnt);
  3131. unsigned n = cmMin(fp->allocCnt - fp->recdIdx,smpCnt);
  3132. unsigned i;
  3133. for(i=0; i<p->chCnt; ++i)
  3134. if( iChs[i] == NULL )
  3135. cmVOS_Zero(fp->chArray[i] + fp->recdIdx, n );
  3136. else
  3137. cmVOS_Copy(fp->chArray[i] + fp->recdIdx, n, iChs[i] );
  3138. fp->recdIdx += n;
  3139. }
  3140. //-------------------------------------------------------------------
  3141. // copy outgoing audio out of the active play buffers
  3142. //
  3143. fp = p->plist;
  3144. for(; fp!=NULL; fp=fp->rlink)
  3145. {
  3146. assert( fp->playIdx <= fp->recdIdx);
  3147. double gain = pow(10.0,((fp->fadeSmpIdx / p->srate) * fp->fadeDbPerSec)/20.0);
  3148. unsigned n = cmMin(fp->recdIdx - fp->playIdx,smpCnt);
  3149. unsigned i;
  3150. for(i=0; i<p->chCnt; ++i)
  3151. if( oChs[i] != NULL )
  3152. cmVOS_MultVVS(oChs[i],n,fp->chArray[i] + fp->playIdx,gain);
  3153. fp->playIdx += n;
  3154. // if a fade rate has been set then advance the fade phase
  3155. if(fp->fadeDbPerSec!=0.0)
  3156. fp->fadeSmpIdx += smpCnt;
  3157. }
  3158. return cmOkRC;
  3159. }