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.

cmDspKr.c 116KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmComplexTypes.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLinkedHeap.h"
  11. #include "cmFile.h"
  12. #include "cmSymTbl.h"
  13. #include "cmJson.h"
  14. #include "cmText.h"
  15. #include "cmPrefs.h"
  16. #include "cmDspValue.h"
  17. #include "cmMsgProtocol.h"
  18. #include "cmThread.h"
  19. #include "cmUdpPort.h"
  20. #include "cmUdpNet.h"
  21. #include "cmTime.h"
  22. #include "cmAudioSys.h"
  23. #include "cmDspCtx.h"
  24. #include "cmDspClass.h"
  25. #include "cmDspStore.h"
  26. #include "cmDspUi.h"
  27. #include "cmDspSys.h"
  28. #include "cmMath.h"
  29. #include "cmAudioFile.h"
  30. #include "cmFileSys.h"
  31. #include "cmProcObj.h"
  32. #include "cmProcTemplateMain.h"
  33. #include "cmProc.h"
  34. #include "cmMidi.h"
  35. #include "cmProc2.h"
  36. #include "cmVectOpsTemplateMain.h"
  37. #include "cmAudioFile.h"
  38. #include "cmMidiFile.h"
  39. #include "cmTimeLine.h"
  40. #include "cmScore.h"
  41. #include "cmProc4.h"
  42. #include "cmProc5.h"
  43. #include "cmSyncRecd.h"
  44. #include "cmTakeSeqBldr.h"
  45. enum
  46. {
  47. kWndSmpCntKrId,
  48. kHopFactKrId,
  49. kModeKrId,
  50. kThreshKrId,
  51. kLwrSlopeKrId,
  52. kUprSlopeKrId,
  53. kOffsetKrId,
  54. kInvertKrId,
  55. kBypassKrId,
  56. kWetKrId,
  57. kAudioInKrId,
  58. kAudioOutKrId
  59. };
  60. typedef struct
  61. {
  62. cmDspInst_t inst;
  63. cmCtx* ctx;
  64. cmSpecDist_t* sdp;
  65. } cmDspKr_t;
  66. cmDspClass_t _cmKrDC;
  67. //==========================================================================================================================================
  68. cmDspInst_t* _cmDspKrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  69. {
  70. cmDspVarArg_t args[] =
  71. {
  72. { "wndn", kWndSmpCntKrId, 0, 0, kInDsvFl | kUIntDsvFl | kReqArgDsvFl, "Window sample count" },
  73. { "hopf", kHopFactKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Hop factor" },
  74. { "mode", kModeKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Mode 0=bypass 1=basic 2=spec cnt 3=amp env" },
  75. { "thrh", kThreshKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Threshold" },
  76. { "lwrs", kLwrSlopeKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Lower Slope"},
  77. { "uprs", kUprSlopeKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Upper Slope"},
  78. { "offs", kOffsetKrId, 0, 0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Offset"},
  79. { "invt", kInvertKrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Invert"},
  80. { "bypass", kBypassKrId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Bypass enable flag." },
  81. { "wet", kWetKrId, 0, 0, kInDsvFl | kSampleDsvFl, "Wet mix level."},
  82. { "in", kAudioInKrId, 0, 0, kInDsvFl | kAudioBufDsvFl, "Audio Input" },
  83. { "out", kAudioOutKrId, 0, 1, kOutDsvFl | kAudioBufDsvFl, "Audio Output" },
  84. { NULL, 0, 0, 0, 0 }
  85. };
  86. cmDspKr_t* p = cmDspInstAlloc(cmDspKr_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  87. unsigned defWndSmpCnt = cmDspDefaultUInt(&p->inst,kWndSmpCntKrId);
  88. unsigned wndSmpCnt = cmNextPowerOfTwo( defWndSmpCnt );
  89. cmDspSetDefaultUInt( ctx,&p->inst, kWndSmpCntKrId, defWndSmpCnt, wndSmpCnt );
  90. cmDspSetDefaultUInt( ctx,&p->inst, kHopFactKrId, 0, 4 );
  91. cmDspSetDefaultUInt( ctx,&p->inst, kModeKrId, 0, kBasicModeSdId );
  92. cmDspSetDefaultDouble( ctx,&p->inst, kThreshKrId, 0, 60.0 );
  93. cmDspSetDefaultDouble( ctx,&p->inst, kLwrSlopeKrId, 0, 2.0 );
  94. cmDspSetDefaultDouble( ctx,&p->inst, kUprSlopeKrId, 0, 0.0 );
  95. cmDspSetDefaultDouble( ctx,&p->inst, kOffsetKrId, 0, 30.0);
  96. cmDspSetDefaultUInt( ctx,&p->inst, kInvertKrId, 0, 0 );
  97. cmDspSetDefaultUInt( ctx,&p->inst, kBypassKrId, 0, 0 );
  98. cmDspSetDefaultSample( ctx,&p->inst, kWetKrId, 0, 1.0);
  99. //_cmDspKrCmInit(ctx,p); // initialize the cm library
  100. p->ctx = cmCtxAlloc(NULL,ctx->rpt,ctx->lhH,ctx->stH);
  101. return &p->inst;
  102. }
  103. cmDspRC_t _cmDspKrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  104. {
  105. cmDspRC_t rc = kOkDspRC;
  106. cmDspKr_t* p = (cmDspKr_t*)inst;
  107. cmSpecDistFree(&p->sdp);
  108. cmCtxFree(&p->ctx);
  109. //_cmDspKrCmFinal(ctx,p); // finalize the cm library
  110. return rc;
  111. }
  112. cmDspRC_t _cmDspKrSetup(cmDspCtx_t* ctx, cmDspKr_t* p )
  113. {
  114. cmDspRC_t rc = kOkDspRC;
  115. unsigned wndSmpCnt = cmDspUInt(&p->inst,kWndSmpCntKrId);
  116. unsigned hopFact = cmDspUInt(&p->inst,kHopFactKrId);
  117. unsigned olaWndTypeId =kHannWndId;
  118. cmSpecDistFree(&p->sdp);
  119. p->sdp = cmSpecDistAlloc(p->ctx, NULL, cmDspSamplesPerCycle(ctx), cmDspSampleRate(ctx), wndSmpCnt, hopFact, olaWndTypeId);
  120. assert(p->sdp != NULL );
  121. if((rc = cmDspZeroAudioBuf(ctx,&p->inst,kAudioOutKrId)) != kOkDspRC )
  122. return rc;
  123. return rc;
  124. }
  125. cmDspRC_t _cmDspKrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  126. {
  127. cmDspKr_t* p = (cmDspKr_t*)inst;
  128. cmDspRC_t rc;
  129. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  130. return rc;
  131. return _cmDspKrSetup(ctx,p);
  132. }
  133. cmDspRC_t _cmDspKrExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  134. {
  135. cmDspKr_t* p = (cmDspKr_t*)inst;
  136. cmDspRC_t rc = kOkDspRC;
  137. unsigned iChIdx = 0;
  138. const cmSample_t* ip = cmDspAudioBuf(ctx,inst,kAudioInKrId,iChIdx);
  139. unsigned iSmpCnt = cmDspVarRows(inst,kAudioInKrId);
  140. // if no connected
  141. if( iSmpCnt == 0 )
  142. return rc;
  143. unsigned oChIdx = 0;
  144. cmSample_t* op = cmDspAudioBuf(ctx,inst,kAudioOutKrId,oChIdx);
  145. unsigned oSmpCnt = cmDspVarRows(inst,kAudioOutKrId);
  146. const cmSample_t* sp;
  147. cmSample_t wet = cmDspSample(inst,kWetKrId);
  148. cmSpecDistExec(p->sdp,ip,iSmpCnt);
  149. if((sp = cmSpecDistOut(p->sdp)) != NULL )
  150. {
  151. cmVOS_MultVVS(op,oSmpCnt,sp,wet);
  152. }
  153. if( wet<1.0 )
  154. cmVOS_MultSumVVS(op,oSmpCnt,ip,1.0-wet);
  155. return rc;
  156. }
  157. cmDspRC_t _cmDspKrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  158. {
  159. cmDspKr_t* p = (cmDspKr_t*)inst;
  160. cmDspRC_t rc = kOkDspRC;
  161. cmDspSetEvent(ctx,inst,evt);
  162. switch( evt->dstVarId )
  163. {
  164. case kWndSmpCntKrId:
  165. case kHopFactKrId:
  166. _cmDspKrSetup(ctx,p);
  167. // THIS IS A HACK
  168. // WHEN WND OR HOP CHANGE THE RESULTING CHANGES
  169. // SHOULD BE ISOLATED IN cmSpecDist() AND THE
  170. // CURRENT STATE OF THE PARAMETERS SHOULD NOT BE
  171. // LOST - IF THE CHANGES WERE ISOLATED WITHIN PVANL
  172. // AND PVSYN IT MIGHT BE POSSIBLE TO DO WITH
  173. // MINIMAL AUDIO INTERUPTION.
  174. p->sdp->mode = cmDspUInt(inst,kModeKrId);
  175. p->sdp->thresh = cmDspDouble(inst,kThreshKrId);
  176. p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKrId);
  177. p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKrId);
  178. p->sdp->offset = cmDspDouble(inst,kOffsetKrId);
  179. p->sdp->invertFl = cmDspUInt(inst,kInvertKrId)!=0;
  180. printf("wsn:%i hsn:%i\n",p->sdp->wndSmpCnt,p->sdp->hopSmpCnt);
  181. break;
  182. case kModeKrId:
  183. p->sdp->mode = cmDspUInt(inst,kModeKrId);
  184. printf("mode:%i\n",p->sdp->mode);
  185. break;
  186. case kThreshKrId:
  187. p->sdp->thresh = cmDspDouble(inst,kThreshKrId);
  188. //printf("thr:p:%p sdp:%p %f\n",p,p->sdp,p->sdp->thresh);
  189. break;
  190. case kUprSlopeKrId:
  191. p->sdp->uprSlope = cmDspDouble(inst,kUprSlopeKrId);
  192. //printf("upr slope:%f\n",p->sdp->uprSlope);
  193. break;
  194. case kLwrSlopeKrId:
  195. p->sdp->lwrSlope = cmDspDouble(inst,kLwrSlopeKrId);
  196. //printf("upr slope:%f\n",p->sdp->lwrSlope);
  197. break;
  198. case kOffsetKrId:
  199. p->sdp->offset = cmDspDouble(inst,kOffsetKrId);
  200. break;
  201. case kInvertKrId:
  202. p->sdp->invertFl = cmDspUInt(inst,kInvertKrId)!=0;
  203. break;
  204. case kWetKrId:
  205. break;
  206. default:
  207. { assert(0); }
  208. }
  209. return rc;
  210. }
  211. struct cmDspClass_str* cmKrClassCons( cmDspCtx_t* ctx )
  212. {
  213. cmDspClassSetup(&_cmKrDC,ctx,"Kr",
  214. NULL,
  215. _cmDspKrAlloc,
  216. _cmDspKrFree,
  217. _cmDspKrReset,
  218. _cmDspKrExec,
  219. _cmDspKrRecv,
  220. NULL,NULL,
  221. "Fourier based non-linear transformer.");
  222. return &_cmKrDC;
  223. }
  224. //==========================================================================================================================================
  225. // Time Line UI Object
  226. enum
  227. {
  228. kTlFileTlId,
  229. kPrefixPathTlId,
  230. kSelTlId,
  231. kCursTlId,
  232. kResetTlId,
  233. kAudFnTlId,
  234. kAudLblTlId,
  235. kMidiFnTlId,
  236. kMidiLblTlId,
  237. kBegAudSmpIdxTlId,
  238. kEndAudSmpIdxTlId,
  239. kBegMidiSmpIdxTlId,
  240. kEndMidiSmpIdxTlId
  241. };
  242. cmDspClass_t _cmTimeLineDC;
  243. typedef struct
  244. {
  245. cmDspInst_t inst;
  246. cmTlH_t tlH;
  247. unsigned afIdx;
  248. } cmDspTimeLine_t;
  249. cmDspInst_t* _cmDspTimeLineAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  250. {
  251. cmDspVarArg_t args[] =
  252. {
  253. { "tlfile", kTlFileTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Time line file." },
  254. { "path", kPrefixPathTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Time line data file prefix path" },
  255. { "sel", kSelTlId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected marker id."},
  256. { "curs", kCursTlId, 0, 0, kInDsvFl | kUIntDsvFl, "Current audio file index."},
  257. { "reset", kResetTlId, 0, 0, kInDsvFl | kSymDsvFl, "Resend all outputs." },
  258. { "afn", kAudFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected Audio file." },
  259. { "albl", kAudLblTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Select Audio file time line label."},
  260. { "mfn", kMidiFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected MIDI file." },
  261. { "mlbl", kMidiLblTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Select MIDI file time line label."},
  262. { "absi", kBegAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin audio sample index."},
  263. { "aesi", kEndAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End audio sample index."},
  264. { "mbsi", kBegMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin MIDI sample index."},
  265. { "mesi", kEndMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End MIDI sample index."},
  266. { NULL, 0, 0, 0, 0 }
  267. };
  268. cmDspTimeLine_t* p = cmDspInstAlloc(cmDspTimeLine_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  269. cmDspSetDefaultUInt( ctx, &p->inst, kSelTlId, 0, cmInvalidId);
  270. cmDspSetDefaultUInt( ctx, &p->inst, kCursTlId, 0, 0);
  271. cmDspSetDefaultStrcz(ctx, &p->inst, kAudFnTlId, NULL, "");
  272. cmDspSetDefaultStrcz(ctx, &p->inst, kAudLblTlId, NULL, "");
  273. cmDspSetDefaultStrcz(ctx, &p->inst, kMidiFnTlId, NULL, "");
  274. cmDspSetDefaultStrcz(ctx, &p->inst, kMidiLblTlId, NULL, "");
  275. cmDspSetDefaultInt( ctx, &p->inst, kBegAudSmpIdxTlId, 0, cmInvalidIdx);
  276. cmDspSetDefaultInt( ctx, &p->inst, kEndAudSmpIdxTlId, 0, cmInvalidIdx);
  277. cmDspSetDefaultInt( ctx, &p->inst, kBegMidiSmpIdxTlId, 0, cmInvalidIdx);
  278. cmDspSetDefaultInt( ctx, &p->inst, kEndMidiSmpIdxTlId, 0, cmInvalidIdx);
  279. // create the UI control
  280. cmDspUiTimeLineCreate(ctx,&p->inst,kTlFileTlId,kPrefixPathTlId,kSelTlId,kCursTlId);
  281. p->tlH = cmTimeLineNullHandle;
  282. return &p->inst;
  283. }
  284. cmDspRC_t _cmDspTimeLineFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  285. {
  286. cmDspRC_t rc = kOkDspRC;
  287. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  288. if( cmTimeLineFinalize(&p->tlH) != kOkTlRC )
  289. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Time-line finalize failed.");
  290. return rc;
  291. }
  292. cmDspRC_t _cmDspTimeLineReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  293. {
  294. cmDspRC_t rc = kOkDspRC;
  295. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  296. cmDspApplyAllDefaults(ctx,inst);
  297. const cmChar_t* tlFn;
  298. const cmChar_t* tlPrePath = cmDspStrcz(inst,kPrefixPathTlId);
  299. if((tlFn = cmDspStrcz(inst, kTlFileTlId )) != NULL )
  300. if( cmTimeLineInitializeFromFile(ctx->cmCtx, &p->tlH, NULL, NULL, tlFn, tlPrePath ) != kOkTlRC )
  301. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Time-line file open failed.");
  302. return rc;
  303. }
  304. cmDspRC_t _cmDspTimeLineRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  305. {
  306. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  307. switch( evt->dstVarId )
  308. {
  309. case kPrefixPathTlId:
  310. cmDspSetEvent(ctx,inst,evt);
  311. break;
  312. case kCursTlId:
  313. cmDspSetEvent(ctx,inst,evt);
  314. break;
  315. case kResetTlId:
  316. case kSelTlId:
  317. {
  318. unsigned markerId;
  319. cmDspSetEvent(ctx,inst,evt);
  320. // get the id of the selected marker
  321. if((markerId = cmDspUInt(inst,kSelTlId)) != cmInvalidId )
  322. {
  323. // get the marker object
  324. cmTlObj_t* op;
  325. if((op = cmTimeLineIdToObj(p->tlH, cmInvalidId, markerId )) != NULL )
  326. {
  327. assert(op->typeId == kMarkerTlId);
  328. p->afIdx = op->begSmpIdx;
  329. cmDspSetInt(ctx, inst, kBegAudSmpIdxTlId, op->begSmpIdx );
  330. cmDspSetInt(ctx, inst, kEndAudSmpIdxTlId, op->begSmpIdx + op->durSmpCnt );
  331. // locate the audio file assoc'd with the marker
  332. cmTlAudioFile_t* afp;
  333. if((afp = cmTimeLineAudioFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL)
  334. {
  335. cmDspSetStrcz(ctx, inst, kAudFnTlId, afp->fn );
  336. cmDspSetStrcz(ctx, inst, kAudLblTlId, afp->obj.name );
  337. }
  338. // locate the midi file assoc'd with the marker
  339. cmTlMidiFile_t* mfp;
  340. if((mfp = cmTimeLineMidiFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL )
  341. {
  342. cmDspSetInt(ctx, inst, kBegMidiSmpIdxTlId, op->seqSmpIdx - mfp->obj.seqSmpIdx );
  343. cmDspSetInt(ctx, inst, kEndMidiSmpIdxTlId, op->seqSmpIdx + op->durSmpCnt - mfp->obj.seqSmpIdx );
  344. cmDspSetStrcz(ctx, inst, kMidiFnTlId, mfp->fn );
  345. cmDspSetStrcz(ctx, inst, kMidiLblTlId,mfp->obj.name );
  346. }
  347. }
  348. }
  349. }
  350. break;
  351. default:
  352. {assert(0);}
  353. }
  354. return kOkDspRC;
  355. }
  356. struct cmDspClass_str* cmTimeLineClassCons( cmDspCtx_t* ctx )
  357. {
  358. cmDspClassSetup(&_cmTimeLineDC,ctx,"TimeLine",
  359. NULL,
  360. _cmDspTimeLineAlloc,
  361. _cmDspTimeLineFree,
  362. _cmDspTimeLineReset,
  363. NULL,
  364. _cmDspTimeLineRecv,
  365. NULL,NULL,
  366. "Time Line control.");
  367. return &_cmTimeLineDC;
  368. }
  369. //==========================================================================================================================================
  370. // Score UI Object
  371. enum
  372. {
  373. kFnScId,
  374. kSelScId,
  375. kSendScId,
  376. kStatusScId,
  377. kD0ScId,
  378. kD1ScId,
  379. kSmpIdxScId,
  380. kLocIdxScId,
  381. kCmdScId,
  382. kEvtIdxScId,
  383. kDynScId,
  384. kValTypeScId,
  385. kValueScId
  386. };
  387. cmDspClass_t _cmScoreDC;
  388. typedef struct
  389. {
  390. cmDspInst_t inst;
  391. cmScH_t scH;
  392. cmDspCtx_t* ctx; // temporary ctx ptr used during cmScore callback in _cmDspScoreRecv()
  393. unsigned printSymId;
  394. } cmDspScore_t;
  395. cmDspInst_t* _cmDspScoreAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  396. {
  397. cmDspVarArg_t args[] =
  398. {
  399. { "fn", kFnScId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  400. { "sel", kSelScId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected score element index input."},
  401. { "send", kSendScId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element."},
  402. { "status", kStatusScId, 0, 0, kInDsvFl | kIntDsvFl, "Performed MIDI status value output" },
  403. { "d0", kD0ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 0" },
  404. { "d1", kD1ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 1" },
  405. { "smpidx", kSmpIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDi msg time tag as a sample index." },
  406. { "loc", kLocIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performance score location."},
  407. { "cmd", kCmdScId, 0, 0, kInDsvFl | kSymDsvFl, "cmd: dump "},
  408. { "evtidx", kEvtIdxScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Performed event index of following dynamcis level."},
  409. { "dyn", kDynScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Dynamic level of previous event index."},
  410. { "type", kValTypeScId,0, 0, kOutDsvFl | kUIntDsvFl, "Output variable type."},
  411. { "value", kValueScId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output variable value."},
  412. { NULL, 0, 0, 0, 0 }
  413. };
  414. cmDspScore_t* p = cmDspInstAlloc(cmDspScore_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  415. cmDspSetDefaultUInt( ctx, &p->inst, kSelScId, 0, cmInvalidId);
  416. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"dump");
  417. // create the UI control
  418. cmDspUiScoreCreate(ctx,&p->inst,kFnScId,kSelScId,kSmpIdxScId,kD0ScId,kD1ScId,kLocIdxScId,kEvtIdxScId,kDynScId,kValTypeScId,kValueScId);
  419. p->scH = cmScNullHandle;
  420. return &p->inst;
  421. }
  422. cmDspRC_t _cmDspScoreFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  423. {
  424. cmDspRC_t rc = kOkDspRC;
  425. cmDspScore_t* p = (cmDspScore_t*)inst;
  426. if( cmScoreFinalize(&p->scH) != kOkTlRC )
  427. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Score finalize failed.");
  428. return rc;
  429. }
  430. // Callback from cmScore triggered from _cmDspScoreRecv() during call to cmScoreSetPerfEvent().
  431. void _cmDspScoreCb( void* arg, const void* data, unsigned byteCnt )
  432. {
  433. cmDspInst_t* inst = (cmDspInst_t*)arg;
  434. cmDspScore_t* p = (cmDspScore_t*)inst;
  435. cmScMsg_t m;
  436. if( cmScoreDecode(data,byteCnt,&m) == kOkScRC )
  437. {
  438. switch( m.typeId )
  439. {
  440. case kDynMsgScId:
  441. cmDspSetUInt( p->ctx,inst, kEvtIdxScId, m.u.dyn.evtIdx );
  442. cmDspSetUInt( p->ctx,inst, kDynScId, m.u.dyn.dynLvl );
  443. break;
  444. case kVarMsgScId:
  445. cmDspSetUInt( p->ctx,inst, kValTypeScId, m.u.meas.varId);
  446. cmDspSetDouble(p->ctx,inst, kValueScId, m.u.meas.value);
  447. break;
  448. default:
  449. { assert(0); }
  450. }
  451. }
  452. }
  453. cmDspRC_t _cmDspScoreReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  454. {
  455. cmDspRC_t rc = kOkDspRC;
  456. cmDspScore_t* p = (cmDspScore_t*)inst;
  457. const cmChar_t* tlFn = NULL;
  458. unsigned* dynRefArray = NULL;
  459. unsigned dynRefCnt = 0;
  460. cmDspApplyAllDefaults(ctx,inst);
  461. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  462. {
  463. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  464. goto errLabel;
  465. }
  466. if((tlFn = cmDspStrcz(inst, kFnScId )) != NULL )
  467. {
  468. if( cmScoreInitialize(ctx->cmCtx, &p->scH, tlFn, cmDspSampleRate(ctx), dynRefArray, dynRefCnt, _cmDspScoreCb, p, ctx->stH ) != kOkTlRC )
  469. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Score file open failed.");
  470. //else
  471. // cmScorePrintLoc(p->scH);
  472. }
  473. errLabel:
  474. return rc;
  475. }
  476. cmDspRC_t _cmDspScoreRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  477. {
  478. cmDspScore_t* p = (cmDspScore_t*)inst;
  479. if( evt->dstVarId == kSendScId )
  480. {
  481. unsigned selIdx;
  482. if((selIdx = cmDspUInt(inst,kSelScId)) != cmInvalidIdx )
  483. {
  484. cmDspSetUInt(ctx,inst,kSelScId, selIdx );
  485. cmScoreClearPerfInfo(p->scH);
  486. }
  487. return kOkDspRC;
  488. }
  489. cmDspSetEvent(ctx,inst,evt);
  490. switch( evt->dstVarId )
  491. {
  492. case kSelScId:
  493. cmScoreClearPerfInfo(p->scH);
  494. break;
  495. case kStatusScId:
  496. //printf("st:%x\n",cmDspUInt(inst,kStatusScId));
  497. break;
  498. case kLocIdxScId:
  499. {
  500. assert( cmDspUInt(inst,kStatusScId ) == kNoteOnMdId );
  501. p->ctx = ctx; // setup p->ctx for use in _cmDspScoreCb()
  502. // this call may result in callbacks to _cmDspScoreCb()
  503. cmScoreExecPerfEvent(p->scH, cmDspUInt(inst,kLocIdxScId), cmDspUInt(inst,kSmpIdxScId), cmDspUInt(inst,kD0ScId), cmDspUInt(inst,kD1ScId) );
  504. }
  505. break;
  506. case kCmdScId:
  507. if( cmDspSymbol(inst,kCmdScId) == p->printSymId )
  508. cmScorePrintLoc(p->scH);
  509. break;
  510. }
  511. return kOkDspRC;
  512. }
  513. struct cmDspClass_str* cmScoreClassCons( cmDspCtx_t* ctx )
  514. {
  515. cmDspClassSetup(&_cmScoreDC,ctx,"Score",
  516. NULL,
  517. _cmDspScoreAlloc,
  518. _cmDspScoreFree,
  519. _cmDspScoreReset,
  520. NULL,
  521. _cmDspScoreRecv,
  522. NULL,NULL,
  523. "Score control.");
  524. return &_cmScoreDC;
  525. }
  526. //==========================================================================================================================================
  527. // MIDI File Player
  528. enum
  529. {
  530. kFnMfId,
  531. kSelMfId,
  532. kBsiMfId,
  533. kEsiMfId,
  534. kStatusMfId,
  535. kD0MfId,
  536. kD1MfId,
  537. kSmpIdxMfId,
  538. kIdMfId
  539. };
  540. cmDspClass_t _cmMidiFilePlayDC;
  541. typedef struct
  542. {
  543. cmDspInst_t inst;
  544. cmMidiFileH_t mfH;
  545. unsigned curMsgIdx; // current midi file msg index
  546. int csi; // current sample index
  547. int bsi; // starting sample index
  548. int esi; // ending sample index
  549. unsigned startSymId;
  550. unsigned stopSymId;
  551. unsigned contSymId;
  552. bool errFl;
  553. } cmDspMidiFilePlay_t;
  554. /*
  555. 'bsi' and 'esi' give the starting and ending sample for MIDI file playback.
  556. These indexes are relative to the start of the file.
  557. When the player recieves a 'start' msg it sets the current sample index
  558. 'si' to 'bsi' and begins scanning for the next note to play.
  559. On each call to the _cmDspMidiFilePlayExec() msgs that fall in the interval
  560. si:si+sPc-1 will be transmitted. (where sPc are the number of samples per DSP cycle).
  561. */
  562. cmDspInst_t* _cmDspMidiFilePlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  563. {
  564. cmDspVarArg_t args[] =
  565. {
  566. { "fn", kFnMfId, 0, 0, kInDsvFl | kStrzDsvFl, "File name"},
  567. { "sel", kSelMfId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" },
  568. { "bsi", kBsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Starting sample." },
  569. { "esi", kEsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Ending sample."},
  570. { "status", kStatusMfId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output" },
  571. { "d0", kD0MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" },
  572. { "d1", kD1MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1" },
  573. { "smpidx", kSmpIdxMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index." },
  574. { "id", kIdMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "MIDI file msg unique id."},
  575. { NULL, 0, 0, 0, 0 }
  576. };
  577. cmDspMidiFilePlay_t* p = cmDspInstAlloc(cmDspMidiFilePlay_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  578. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  579. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  580. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  581. p->mfH = cmMidiFileNullHandle;
  582. cmDspSetDefaultStrcz( ctx, &p->inst, kFnMfId, NULL, "");
  583. cmDspSetDefaultSymbol(ctx, &p->inst, kSelMfId, p->stopSymId);
  584. cmDspSetDefaultInt( ctx, &p->inst, kBsiMfId, 0, 0);
  585. cmDspSetDefaultInt( ctx, &p->inst, kEsiMfId, 0, 0);
  586. cmDspSetDefaultUInt( ctx, &p->inst, kStatusMfId, 0, 0);
  587. cmDspSetDefaultUInt( ctx, &p->inst, kD0MfId, 0, 0);
  588. cmDspSetDefaultUInt( ctx, &p->inst, kD1MfId, 0, 0);
  589. return &p->inst;
  590. }
  591. cmDspRC_t _cmDspMidiFilePlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  592. {
  593. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  594. if( cmMidiFileClose(&p->mfH) )
  595. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "MIDI file close failed.");
  596. return kOkDspRC;
  597. }
  598. // return the index of the msg following smpIdx
  599. unsigned _cmDspMidiFilePlaySeekMsgIdx( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned smpIdx )
  600. {
  601. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  602. if( cmMidiFileIsValid(p->mfH) == false )
  603. {
  604. cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  605. return cmInvalidIdx;
  606. }
  607. unsigned i;
  608. unsigned n = cmMidiFileMsgCount(p->mfH);
  609. const cmMidiTrackMsg_t** a = cmMidiFileMsgArray(p->mfH);
  610. for(i=0; i<n; ++i)
  611. if( a[i]->dtick > smpIdx )
  612. break;
  613. return i==n ? cmInvalidIdx : i;
  614. }
  615. cmDspRC_t _cmDspMidiFilePlayOpen(cmDspCtx_t* ctx, cmDspInst_t* inst )
  616. {
  617. cmDspRC_t rc = kOkDspRC;
  618. const cmChar_t* fn = cmDspStrcz(inst,kFnMfId);
  619. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  620. p->errFl = false;
  621. if( fn==NULL || strlen(fn)==0 )
  622. return rc;
  623. if( cmMidiFileOpen( fn, &p->mfH, ctx->cmCtx ) != kOkFileRC )
  624. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "MIDI file open failed.");
  625. else
  626. {
  627. p->curMsgIdx = 0;
  628. p->bsi = cmDspInt(inst,kBsiMfId);
  629. p->esi = cmDspInt(inst,kEsiMfId);
  630. p->csi = 0;
  631. // force the first msg to occurr one quarter note into the file
  632. cmMidiFileSetDelay(p->mfH, cmMidiFileTicksPerQN(p->mfH) );
  633. // convert midi msg times to absolute time in samples
  634. cmMidiFileTickToSamples(p->mfH,cmDspSampleRate(ctx),true);
  635. }
  636. return rc;
  637. }
  638. cmDspRC_t _cmDspMidiFilePlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  639. {
  640. cmDspApplyAllDefaults(ctx,inst);
  641. return _cmDspMidiFilePlayOpen(ctx,inst);
  642. }
  643. cmDspRC_t _cmDspMidiFilePlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  644. {
  645. cmDspRC_t rc = kOkDspRC;
  646. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  647. unsigned sPc = cmDspSamplesPerCycle(ctx);
  648. if( cmDspSymbol(inst,kSelMfId) != p->stopSymId )
  649. {
  650. if( cmMidiFileIsValid(p->mfH) == false )
  651. {
  652. if( p->errFl==false )
  653. {
  654. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  655. p->errFl = true;
  656. }
  657. return rc;
  658. }
  659. const cmMidiTrackMsg_t** mpp = cmMidiFileMsgArray(p->mfH);
  660. unsigned msgN = cmMidiFileMsgCount(p->mfH);
  661. for(; p->curMsgIdx < msgN && p->csi <= mpp[p->curMsgIdx]->dtick && mpp[p->curMsgIdx]->dtick < (p->csi + sPc); ++p->curMsgIdx )
  662. {
  663. const cmMidiTrackMsg_t* mp = mpp[p->curMsgIdx];
  664. switch( mp->status )
  665. {
  666. case kNoteOffMdId:
  667. case kNoteOnMdId:
  668. case kCtlMdId:
  669. cmDspSetUInt(ctx,inst, kSmpIdxMfId, mp->dtick);
  670. cmDspSetUInt(ctx,inst, kD1MfId, mp->u.chMsgPtr->d1);
  671. cmDspSetUInt(ctx,inst, kD0MfId, mp->u.chMsgPtr->d0);
  672. cmDspSetUInt(ctx,inst, kStatusMfId, mp->status);
  673. cmDspSetUInt(ctx,inst, kIdMfId, mp->uid);
  674. break;
  675. }
  676. }
  677. }
  678. p->csi += sPc;
  679. return rc;
  680. }
  681. cmDspRC_t _cmDspMidiFilePlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  682. {
  683. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  684. cmDspSetEvent(ctx,inst,evt);
  685. switch(evt->dstVarId)
  686. {
  687. case kFnMfId:
  688. _cmDspMidiFilePlayOpen(ctx, inst );
  689. break;
  690. case kSelMfId:
  691. {
  692. if( cmDspSymbol(inst,kSelMfId)==p->startSymId )
  693. {
  694. p->csi = cmDspInt(inst,kBsiMfId);
  695. p->curMsgIdx = _cmDspMidiFilePlaySeekMsgIdx(ctx, inst, p->csi );
  696. }
  697. break;
  698. }
  699. }
  700. return kOkDspRC;
  701. }
  702. struct cmDspClass_str* cmMidiFilePlayClassCons( cmDspCtx_t* ctx )
  703. {
  704. cmDspClassSetup(&_cmMidiFilePlayDC,ctx,"MidiFilePlay",
  705. NULL,
  706. _cmDspMidiFilePlayAlloc,
  707. _cmDspMidiFilePlayFree,
  708. _cmDspMidiFilePlayReset,
  709. _cmDspMidiFilePlayExec,
  710. _cmDspMidiFilePlayRecv,
  711. NULL,NULL,
  712. "MIDI file player.");
  713. return &_cmMidiFilePlayDC;
  714. }
  715. //==========================================================================================================================================
  716. enum
  717. {
  718. kFnSfId,
  719. kBufCntSfId,
  720. kMaxWndCntSfId,
  721. kMinVelSfId,
  722. kMeasflSfId,
  723. kIndexSfId,
  724. kMuidSfId,
  725. kStatusSfId,
  726. kD0SfId,
  727. kD1SfId,
  728. kSmpIdxSfId,
  729. kCmdSfId,
  730. kOutSfId,
  731. kRecentSfId,
  732. kVlocSfId,
  733. kVtypSfId,
  734. kVvalSfId,
  735. kVcostSfId,
  736. kSymSfId
  737. };
  738. cmDspClass_t _cmScFolDC;
  739. struct cmDspScFol_str;
  740. typedef struct
  741. {
  742. cmDspCtx_t* ctx;
  743. struct cmDspScFol_str* sfp;
  744. } cmDspScFolCbArg_t;
  745. typedef struct cmDspScFol_str
  746. {
  747. cmDspInst_t inst;
  748. cmScMatcher* sfp;
  749. cmScMeas* smp;
  750. cmScH_t scH;
  751. cmDspScFolCbArg_t arg;
  752. unsigned printSymId;
  753. unsigned quietSymId;
  754. unsigned maxScLocIdx;
  755. bool liveFl;
  756. } cmDspScFol_t;
  757. cmDspInst_t* _cmDspScFolAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  758. {
  759. cmDspVarArg_t args[] =
  760. {
  761. { "fn", kFnSfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  762. { "bufcnt",kBufCntSfId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Event buffer element count." },
  763. { "wndcnt",kMaxWndCntSfId,0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Maximum window length."},
  764. { "minvel",kMinVelSfId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Minimum velocity."},
  765. { "measfl",kMeasflSfId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Enable measurements"},
  766. { "index", kIndexSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Tracking start location."},
  767. { "muid", kMuidSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI msg file unique id"},
  768. { "status",kStatusSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI status byte"},
  769. { "d0", kD0SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 0"},
  770. { "d1", kD1SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 1"},
  771. { "smpidx",kSmpIdxSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI time tag as a sample index"},
  772. { "cmd", kCmdSfId, 0, 0, kInDsvFl | kSymDsvFl, "Command input: print | quiet"},
  773. { "out", kOutSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Maximum score location index."},
  774. { "recent",kRecentSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Most recent score location index."},
  775. { "vloc", kVlocSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Score location at which the variable value becomes active."},
  776. { "vtyp", kVtypSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Variable type: 0=even=kEvenVarScId 1=dyn=kDynVarScId 2=tempo=kTempoVarScId."},
  777. { "vval", kVvalSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Variable value."},
  778. { "vcost", kVcostSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Variable match cost value."},
  779. { "sym", kSymSfId, 0, 0, kOutDsvFl| kSymDsvFl, "Symbol associated with a global variable which has changed value."},
  780. { NULL, 0, 0, 0, 0, NULL }
  781. };
  782. cmDspScFol_t* p;
  783. if((p = cmDspInstAlloc(cmDspScFol_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
  784. return NULL;
  785. p->sfp = cmScMatcherAlloc(ctx->cmProcCtx, NULL, 0, cmScNullHandle, 0, 0, NULL, NULL );
  786. p->smp = cmScMeasAlloc( ctx->cmProcCtx, NULL, cmScNullHandle, 0, NULL, 0 );
  787. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"print");
  788. p->quietSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"quiet");
  789. p->maxScLocIdx= cmInvalidIdx;
  790. cmDspSetDefaultUInt( ctx, &p->inst, kBufCntSfId, 0, 7);
  791. cmDspSetDefaultUInt( ctx, &p->inst, kMaxWndCntSfId, 0, 10);
  792. cmDspSetDefaultUInt( ctx, &p->inst, kMinVelSfId, 0, 5);
  793. cmDspSetDefaultBool( ctx, &p->inst, kMeasflSfId, 0, 0);
  794. cmDspSetDefaultUInt( ctx, &p->inst, kIndexSfId, 0, 0);
  795. cmDspSetDefaultUInt( ctx, &p->inst, kOutSfId, 0, 0);
  796. cmDspSetDefaultUInt( ctx, &p->inst, kRecentSfId, 0, 0);
  797. cmDspSetDefaultSymbol(ctx,&p->inst, kCmdSfId, p->quietSymId );
  798. return &p->inst;
  799. }
  800. cmDspRC_t _cmDspScFolFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  801. {
  802. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  803. cmScMatcherFree(&p->sfp);
  804. cmScMeasFree(&p->smp);
  805. cmScoreFinalize(&p->scH);
  806. return kOkDspRC;
  807. }
  808. // This is a callback function from cmScMatcherExec() which is called when
  809. // this cmDspFol object receives a new score location index.
  810. void _cmScFolMatcherCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
  811. {
  812. cmDspScFolCbArg_t* ap = (cmDspScFolCbArg_t*)arg;
  813. if( cmScMeasExec(ap->sfp->smp, rp->mni, rp->locIdx, rp->scEvtIdx, rp->flags, rp->smpIdx, rp->pitch, rp->vel ) == cmOkRC )
  814. {
  815. cmDspInst_t* inst = &(ap->sfp->inst);
  816. // send 'set' values that were calculated on the previous call to cmScMeasExec()
  817. unsigned i;
  818. for(i=ap->sfp->smp->vsi; i<ap->sfp->smp->nsi; ++i)
  819. if(ap->sfp->smp->set[i].value != DBL_MAX )
  820. {
  821. /*
  822. switch( ap->sfp->smp->set[i].sp->varId )
  823. {
  824. case kEvenVarScId:
  825. cmDspSetDouble(ap->ctx,inst,kEvenSfId,ap->sfp->smp->set[i].value);
  826. break;
  827. case kDynVarScId:
  828. cmDspSetDouble(ap->ctx,inst,kDynSfId,ap->sfp->smp->set[i].value);
  829. break;
  830. case kTempoVarScId:
  831. cmDspSetDouble(ap->ctx,inst,kTempoSfId,ap->sfp->smp->set[i].value);
  832. break;
  833. default:
  834. { assert(0); }
  835. }
  836. cmDspSetDouble(ap->ctx,inst,kCostSfId,ap->sfp->smp->set[i].match_cost);
  837. */
  838. // Set the values in the global variable storage
  839. cmDspValue_t vv,cv;
  840. unsigned j;
  841. cmDsvSetDouble(&vv,ap->sfp->smp->set[i].value);
  842. cmDsvSetDouble(&cv,ap->sfp->smp->set[i].match_cost);
  843. for(j=0; j<ap->sfp->smp->set[i].sp->sectCnt; ++j)
  844. {
  845. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->symArray[j], &vv );
  846. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->costSymArray[j], &cv );
  847. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->symArray[j]);
  848. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->costSymArray[j]);
  849. if( cmDspBool(inst,kMeasflSfId) )
  850. {
  851. cmDspSetUInt( ap->ctx, inst, kVlocSfId, ap->sfp->smp->set[i].sp->sectArray[j]->locPtr->index);
  852. cmDspSetDouble( ap->ctx, inst, kVvalSfId, ap->sfp->smp->set[i].value);
  853. cmDspSetDouble( ap->ctx, inst, kVcostSfId, ap->sfp->smp->set[i].match_cost);
  854. cmDspSetUInt( ap->ctx, inst, kVtypSfId, ap->sfp->smp->set[i].sp->varId);
  855. }
  856. }
  857. }
  858. /*
  859. // trigger 'section' starts
  860. for(i=ap->sfp->smp->vsli; i<ap->sfp->smp->nsli; ++i)
  861. {
  862. const cmScoreLoc_t* locPtr = cmScoreLoc(ap->sfp->smp->mp->scH,i);
  863. if( locPtr->begSectPtr != NULL )
  864. cmDspSetUInt(ap->ctx,inst,kSectIndexSfId,locPtr->begSectPtr->index);
  865. }
  866. */
  867. }
  868. }
  869. cmDspRC_t _cmDspScFolOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  870. {
  871. cmDspRC_t rc = kOkDspRC;
  872. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  873. const cmChar_t* fn;
  874. if((fn = cmDspStrcz(inst,kFnSfId)) == NULL || strlen(fn)==0 )
  875. return cmErrMsg(&inst->classPtr->err, kInvalidArgDspRC, "No score file name supplied.");
  876. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  877. return cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  878. if( cmScoreIsValid(p->scH) )
  879. {
  880. unsigned* dynRefArray = NULL;
  881. unsigned dynRefCnt = 0;
  882. // initialize the cmScMatcher
  883. if( cmScMatcherInit(p->sfp, cmDspSampleRate(ctx), p->scH, cmDspUInt(inst,kMaxWndCntSfId), cmDspUInt(inst,kBufCntSfId), _cmScFolMatcherCb, p->smp ) != cmOkRC )
  884. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal score follower allocation failed.");
  885. // read the dynamics reference array
  886. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  887. {
  888. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  889. goto errLabel;
  890. }
  891. // initialize the cmScMeas object.
  892. if( cmScMeasInit(p->smp, p->scH, cmDspSampleRate(ctx), dynRefArray, dynRefCnt ) != cmOkRC )
  893. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal scMeas object initialization failed.");
  894. }
  895. errLabel:
  896. return rc;
  897. }
  898. cmDspRC_t _cmDspScFolReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  899. {
  900. cmDspRC_t rc;
  901. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  902. return rc;
  903. return _cmDspScFolOpenScore(ctx,inst);
  904. }
  905. cmDspRC_t _cmDspScFolRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  906. {
  907. cmDspRC_t rc = kOkDspRC;
  908. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  909. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && p->sfp != NULL )
  910. {
  911. switch( evt->dstVarId )
  912. {
  913. case kIndexSfId:
  914. if( cmScoreIsValid(p->scH) )
  915. {
  916. p->maxScLocIdx = cmInvalidIdx;
  917. if( cmScMeasReset( p->smp ) != cmOkRC )
  918. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score measure unit reset to score index '%i' failed.");
  919. if( cmScMatcherReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC )
  920. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed.");
  921. }
  922. break;
  923. case kStatusSfId:
  924. if( cmScoreIsValid(p->scH))
  925. {
  926. unsigned scLocIdx = cmInvalidIdx;
  927. // setup the cmScMeas() callback arg.
  928. p->arg.ctx = ctx;
  929. p->arg.sfp = p;
  930. p->sfp->cbArg = &p->arg;
  931. // this call may result in a callback to _cmScFolMatcherCb()
  932. if( cmScMatcherExec(p->sfp, cmDspUInt(inst,kSmpIdxSfId), cmDspUInt(inst,kMuidSfId), cmDspUInt(inst,kStatusSfId), cmDspUInt(inst,kD0SfId), cmDspUInt(inst,kD1SfId), &scLocIdx) == cmOkRC )
  933. if( scLocIdx != cmInvalidIdx )
  934. {
  935. // It is possible that the internal score follower may go backwards.
  936. // In this case it will report a given score location multiple times or out of time order.
  937. // The 'out' port will only be updated under the circumstances that no later
  938. // score location has been seen - so the last output from 'out' always reports
  939. // the furthest possible progress in the score. THe 'recent' output simply reports
  940. // the most recent output from the internal score follower which may include
  941. // previously reported or out of order score locations.
  942. cmDspSetUInt(ctx,inst,kRecentSfId,scLocIdx);
  943. if( p->maxScLocIdx==cmInvalidIdx || p->maxScLocIdx < scLocIdx )
  944. {
  945. p->maxScLocIdx = scLocIdx;
  946. cmDspSetUInt(ctx,inst,kOutSfId,scLocIdx);
  947. }
  948. }
  949. }
  950. break;
  951. case kFnSfId:
  952. _cmDspScFolOpenScore(ctx,inst);
  953. break;
  954. case kCmdSfId:
  955. if( cmDspSymbol(inst,kCmdSfId) == p->printSymId )
  956. p->sfp->printFl = true;
  957. else
  958. if( cmDspSymbol(inst,kCmdSfId) == p->quietSymId )
  959. p->sfp->printFl = false;
  960. break;
  961. }
  962. }
  963. return rc;
  964. }
  965. struct cmDspClass_str* cmScFolClassCons( cmDspCtx_t* ctx )
  966. {
  967. cmDspClassSetup(&_cmScFolDC,ctx,"ScFol",
  968. NULL,
  969. _cmDspScFolAlloc,
  970. _cmDspScFolFree,
  971. _cmDspScFolReset,
  972. NULL,
  973. _cmDspScFolRecv,
  974. NULL,NULL,
  975. "Score Follower");
  976. return &_cmScFolDC;
  977. }
  978. //==========================================================================================================================================
  979. enum
  980. {
  981. kScLocIdxMdId,
  982. kResetIdxMdId,
  983. kCmdMdId,
  984. kPostMdId
  985. };
  986. cmDspClass_t _cmModulatorDC;
  987. typedef struct
  988. {
  989. cmDspInst_t inst;
  990. cmScModulator* mp;
  991. cmDspCtx_t* tmp_ctx; // used to temporarily hold the current cmDspCtx during callback
  992. cmChar_t* fn;
  993. cmChar_t* modLabel;
  994. unsigned onSymId;
  995. unsigned offSymId;
  996. unsigned postSymId;
  997. } cmDspScMod_t;
  998. void _cmDspScModCb( void* arg, unsigned varSymId, double value, bool postFl )
  999. {
  1000. cmDspScMod_t* p = (cmDspScMod_t*)arg;
  1001. cmDspVar_t* varPtr;
  1002. if((varPtr = cmDspVarSymbolToPtr( p->tmp_ctx, &p->inst, varSymId, 0 )) == NULL )
  1003. return;
  1004. cmDspSetDouble(p->tmp_ctx,&p->inst,varPtr->constId,value);
  1005. if( postFl )
  1006. cmDspSetSymbol(p->tmp_ctx,&p->inst,kPostMdId,p->postSymId);
  1007. }
  1008. cmDspInst_t* _cmDspScModAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1009. {
  1010. va_list vl1;
  1011. va_copy(vl1,vl);
  1012. cmDspVarArg_t args[] =
  1013. {
  1014. { "index", kScLocIdxMdId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower index input."},
  1015. { "reset", kResetIdxMdId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reset the modulator and go to the score index."},
  1016. { "cmd", kCmdMdId, 0,0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "on | off."},
  1017. { "post", kPostMdId, 0,0, kOutDsvFl | kSymDsvFl, "Sends 'post' symbol after a message transmission if the 'post' flag is set in scMod."},
  1018. { NULL, 0, 0, 0, 0 }
  1019. };
  1020. // validate the argument count
  1021. if( va_cnt != 2 )
  1022. {
  1023. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator requires at least two arguments.");
  1024. return NULL;
  1025. }
  1026. // read the modulator file and label strings
  1027. const cmChar_t* fn = va_arg(vl1,const cmChar_t*);
  1028. const cmChar_t* modLabel = va_arg(vl1,const cmChar_t*);
  1029. va_end(vl1);
  1030. // validate the file
  1031. if( fn==NULL || cmFsIsFile(fn)==false )
  1032. {
  1033. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator file '%s' is not valid.",cmStringNullGuard(fn));
  1034. return NULL;
  1035. }
  1036. // allocate the internal modulator object
  1037. cmScModulator* mp = cmScModulatorAlloc(ctx->cmProcCtx, NULL, ctx->cmCtx, ctx->stH, cmDspSampleRate(ctx), cmDspSamplesPerCycle(ctx), fn, modLabel, _cmDspScModCb, NULL );
  1038. if(mp == NULL )
  1039. {
  1040. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The internal modulator object initialization failed.");
  1041. return NULL;
  1042. }
  1043. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  1044. unsigned argCnt = fixArgCnt + cmScModulatorOutVarCount(mp);
  1045. cmDspVarArg_t a[ argCnt+1 ];
  1046. unsigned i;
  1047. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1048. for(i=fixArgCnt; i<argCnt; ++i)
  1049. {
  1050. unsigned varIdx = i - fixArgCnt;
  1051. const cmScModVar_t* vp = cmScModulatorOutVar(mp,varIdx);
  1052. const cmChar_t* label = cmSymTblLabel( ctx->stH, vp->varSymId );
  1053. const cmChar_t* docStr = cmTsPrintfS("Variable output for %s",label);
  1054. cmDspArgSetup(ctx, a + i, label, cmInvalidId, i, 0, 0, kOutDsvFl | kDoubleDsvFl, docStr );
  1055. }
  1056. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1057. cmDspScMod_t* p = cmDspInstAlloc(cmDspScMod_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1058. p->fn = cmMemAllocStr(fn);
  1059. p->modLabel = cmMemAllocStr(modLabel);
  1060. p->mp = mp;
  1061. p->onSymId = cmSymTblId(ctx->stH,"on");
  1062. p->offSymId = cmSymTblId(ctx->stH,"off");
  1063. p->postSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"post");
  1064. mp->cbArg = p; // set the modulator callback arg
  1065. cmDspSetDefaultUInt(ctx,&p->inst,kScLocIdxMdId,0,0);
  1066. cmDspSetDefaultSymbol(ctx,&p->inst,kCmdMdId,p->offSymId);
  1067. return &p->inst;
  1068. }
  1069. cmDspRC_t _cmDspScModFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1070. {
  1071. cmDspRC_t rc = kOkDspRC;
  1072. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1073. if( cmScModulatorFree(&p->mp) != kOkTlRC )
  1074. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Modulator release failed.");
  1075. cmMemFree(p->fn);
  1076. cmMemFree(p->modLabel);
  1077. return rc;
  1078. }
  1079. cmDspRC_t _cmDspScModReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1080. {
  1081. cmDspRC_t rc = kOkDspRC;
  1082. cmDspApplyAllDefaults(ctx,inst);
  1083. return rc;
  1084. }
  1085. cmDspRC_t _cmDspScModRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1086. {
  1087. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1088. cmDspSetEvent(ctx,inst,evt);
  1089. switch( evt->dstVarId )
  1090. {
  1091. case kResetIdxMdId:
  1092. cmDspSetUInt(ctx,inst,kScLocIdxMdId,cmDspUInt(inst,kResetIdxMdId));
  1093. break;
  1094. case kCmdMdId:
  1095. {
  1096. unsigned symId = cmDspSymbol(inst,kCmdMdId);
  1097. if( symId == p->onSymId )
  1098. cmScModulatorReset(p->mp, ctx->cmCtx, cmDspUInt(inst,kScLocIdxMdId));
  1099. }
  1100. break;
  1101. }
  1102. return kOkDspRC;
  1103. }
  1104. cmDspRC_t _cmDspScModExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1105. {
  1106. cmDspRC_t rc = kOkDspRC;
  1107. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1108. if( cmDspSymbol(inst,kCmdMdId) != p->offSymId )
  1109. {
  1110. p->tmp_ctx = ctx;
  1111. cmScModulatorExec(p->mp,cmDspUInt(inst,kScLocIdxMdId));
  1112. }
  1113. return rc;
  1114. }
  1115. struct cmDspClass_str* cmScModClassCons( cmDspCtx_t* ctx )
  1116. {
  1117. cmDspClassSetup(&_cmModulatorDC,ctx,"ScMod",
  1118. NULL,
  1119. _cmDspScModAlloc,
  1120. _cmDspScModFree,
  1121. _cmDspScModReset,
  1122. _cmDspScModExec,
  1123. _cmDspScModRecv,
  1124. NULL,NULL,
  1125. "Score Driven Variable Modulator.");
  1126. return &_cmModulatorDC;
  1127. }
  1128. //==========================================================================================================================================
  1129. enum
  1130. {
  1131. kInChCntGsId,
  1132. kOutGroupCntGsId,
  1133. kGroupSelIdxGsId,
  1134. kBaseInFloatGsId
  1135. };
  1136. cmDspClass_t _cmGSwitchDC;
  1137. typedef struct
  1138. {
  1139. cmDspInst_t inst;
  1140. unsigned iChCnt;
  1141. unsigned oGroupCnt;
  1142. unsigned baseInFloatGsId;
  1143. unsigned baseInSymGsId;
  1144. unsigned baseInBoolGsId;
  1145. unsigned baseOutFloatGsId;
  1146. unsigned baseOutSymGsId;
  1147. unsigned baseOutBoolGsId;
  1148. } cmDspGSwitch_t;
  1149. cmDspInst_t* _cmDspGSwitchAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1150. {
  1151. va_list vl1;
  1152. va_copy(vl1,vl);
  1153. cmDspVarArg_t args[] =
  1154. {
  1155. { "ichs", kInChCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Input channel count."},
  1156. { "ochs", kOutGroupCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Output group count."},
  1157. { "sel", kGroupSelIdxGsId, 0,0, kInDsvFl | kUIntDsvFl, "Group select index."},
  1158. { NULL, 0, 0, 0, 0 }
  1159. };
  1160. // validate the argument count
  1161. if( va_cnt != 2 )
  1162. {
  1163. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch requires at least two arguments.");
  1164. return NULL;
  1165. }
  1166. // read the input ch and output group count
  1167. unsigned iChCnt = va_arg(vl1,unsigned);
  1168. unsigned oGroupCnt = va_arg(vl1,unsigned);
  1169. va_end(vl1);
  1170. // validate the channel counts
  1171. if( iChCnt == 0 || oGroupCnt==0 )
  1172. {
  1173. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch input channel count and group count must be greater than zero.");
  1174. return NULL;
  1175. }
  1176. unsigned typeCnt = 3; // i.e. float,sym,bool
  1177. unsigned baseInFloatGsId = kBaseInFloatGsId;
  1178. unsigned baseInSymGsId = baseInFloatGsId + iChCnt;
  1179. unsigned baseInBoolGsId = baseInSymGsId + iChCnt;
  1180. unsigned baseOutFloatGsId = baseInBoolGsId + iChCnt;
  1181. unsigned baseOutSymGsId = baseOutFloatGsId + (iChCnt * oGroupCnt);
  1182. unsigned baseOutBoolGsId = baseOutSymGsId + (iChCnt * oGroupCnt);
  1183. unsigned fixArgCnt = 3;
  1184. unsigned varArgCnt = (iChCnt * typeCnt) + (iChCnt * typeCnt * oGroupCnt);
  1185. unsigned argCnt = fixArgCnt + varArgCnt;
  1186. cmDspVarArg_t a[ argCnt+1 ];
  1187. unsigned i;
  1188. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1189. cmDspArgSetupN( ctx, a, argCnt, baseInFloatGsId, iChCnt, "f-in", baseInFloatGsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Float input");
  1190. cmDspArgSetupN( ctx, a, argCnt, baseInSymGsId, iChCnt, "s-in", baseInSymGsId, 0, 0, kInDsvFl | kSymDsvFl, "Symbol input");
  1191. cmDspArgSetupN( ctx, a, argCnt, baseInBoolGsId, iChCnt, "b-in", baseInBoolGsId, 0, 0, kInDsvFl | kBoolDsvFl, "Bool input");
  1192. unsigned labelCharCnt = 63;
  1193. cmChar_t label[labelCharCnt+1];
  1194. label[labelCharCnt] = 0;
  1195. unsigned gsid = baseOutFloatGsId;
  1196. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1197. {
  1198. snprintf(label,labelCharCnt,"f-out-%i",i);
  1199. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kDoubleDsvFl, "Float output");
  1200. }
  1201. gsid = baseOutSymGsId;
  1202. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1203. {
  1204. snprintf(label,labelCharCnt,"s-out-%i",i);
  1205. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kSymDsvFl, "Symbol output");
  1206. }
  1207. gsid = baseOutBoolGsId;
  1208. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1209. {
  1210. snprintf(label,labelCharCnt,"b-out-%i",i);
  1211. cmDspArgSetupN( ctx,a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kBoolDsvFl, "Bool output");
  1212. }
  1213. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1214. cmDspGSwitch_t* p = cmDspInstAlloc(cmDspGSwitch_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1215. p->iChCnt = iChCnt;
  1216. p->oGroupCnt = oGroupCnt;
  1217. p->baseInFloatGsId = baseInFloatGsId;
  1218. p->baseInSymGsId = baseInSymGsId;
  1219. p->baseInBoolGsId = baseInBoolGsId;
  1220. p->baseOutFloatGsId = baseOutFloatGsId;
  1221. p->baseOutSymGsId = baseOutSymGsId;
  1222. p->baseOutBoolGsId = baseOutBoolGsId;
  1223. cmDspSetDefaultUInt(ctx,&p->inst,kGroupSelIdxGsId,0,0);
  1224. return &p->inst;
  1225. }
  1226. cmDspRC_t _cmDspGSwitchReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1227. {
  1228. cmDspRC_t rc = kOkDspRC;
  1229. cmDspApplyAllDefaults(ctx,inst);
  1230. return rc;
  1231. }
  1232. cmDspRC_t _cmDspGSwitchRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1233. {
  1234. cmDspRC_t rc = kOkDspRC;
  1235. cmDspGSwitch_t* p = (cmDspGSwitch_t*)inst;
  1236. // if this is the group selector
  1237. if( evt->dstVarId == kGroupSelIdxGsId )
  1238. {
  1239. unsigned idx;
  1240. if( (idx = cmDsvGetUInt(evt->valuePtr)) > p->oGroupCnt )
  1241. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The GSwitch group select index %i is out of range %i.",idx,p->oGroupCnt);
  1242. else
  1243. cmDspSetEvent(ctx,inst,evt);
  1244. return rc;
  1245. }
  1246. // get the group selector
  1247. unsigned groupIdx = cmDspUInt(inst,kGroupSelIdxGsId);
  1248. assert( groupIdx < p->oGroupCnt);
  1249. // if this is a float input
  1250. if( p->baseInFloatGsId <= evt->dstVarId && evt->dstVarId < p->baseInFloatGsId + p->iChCnt )
  1251. {
  1252. unsigned outVarId = p->baseOutFloatGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInFloatGsId);
  1253. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1254. return rc;
  1255. }
  1256. // if this is a symbol input
  1257. if( p->baseInSymGsId <= evt->dstVarId && evt->dstVarId < p->baseInSymGsId + p->iChCnt )
  1258. {
  1259. unsigned outVarId = p->baseOutSymGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInSymGsId);
  1260. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1261. return rc;
  1262. }
  1263. // if this is a bool input
  1264. if( p->baseInBoolGsId <= evt->dstVarId && evt->dstVarId < p->baseInBoolGsId + p->iChCnt )
  1265. {
  1266. unsigned outVarId = p->baseOutBoolGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInBoolGsId);
  1267. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1268. return rc;
  1269. }
  1270. return rc;
  1271. }
  1272. struct cmDspClass_str* cmGSwitchClassCons( cmDspCtx_t* ctx )
  1273. {
  1274. cmDspClassSetup(&_cmGSwitchDC,ctx,"GSwitch",
  1275. NULL,
  1276. _cmDspGSwitchAlloc,
  1277. NULL,
  1278. _cmDspGSwitchReset,
  1279. NULL,
  1280. _cmDspGSwitchRecv,
  1281. NULL,NULL,
  1282. "Ganged switch.");
  1283. return &_cmGSwitchDC;
  1284. }
  1285. //==========================================================================================================================================
  1286. enum
  1287. {
  1288. kMinInSrId,
  1289. kMaxInSrId,
  1290. kMinOutSrId,
  1291. kMaxOutSrId,
  1292. kValInSrId,
  1293. kValOutSrId,
  1294. };
  1295. cmDspClass_t _cmScaleRangeDC;
  1296. typedef struct
  1297. {
  1298. cmDspInst_t inst;
  1299. } cmDspScaleRange_t;
  1300. cmDspInst_t* _cmDspScaleRangeAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1301. {
  1302. va_list vl1;
  1303. va_copy(vl1,vl);
  1304. cmDspVarArg_t args[] =
  1305. {
  1306. { "min_in", kMinInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1307. { "max_in", kMaxInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1308. { "min_out", kMinOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1309. { "max_out", kMaxOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1310. { "val_in", kValInSrId, 0,0, kInDsvFl | kDoubleDsvFl, "Input value."},
  1311. { "val_out", kValOutSrId, 0,0, kOutDsvFl | kDoubleDsvFl, "Output value"},
  1312. { NULL, 0, 0, 0, 0 }
  1313. };
  1314. cmDspScaleRange_t* p = cmDspInstAlloc(cmDspScaleRange_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1315. cmDspSetDefaultDouble(ctx,&p->inst,kMinInSrId,0,0);
  1316. cmDspSetDefaultDouble(ctx,&p->inst,kMaxInSrId,0,1.0);
  1317. cmDspSetDefaultDouble(ctx,&p->inst,kMinOutSrId,0,0);
  1318. cmDspSetDefaultDouble(ctx,&p->inst,kMaxOutSrId,0,1.0);
  1319. return &p->inst;
  1320. }
  1321. cmDspRC_t _cmDspScaleRangeReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1322. {
  1323. cmDspRC_t rc = kOkDspRC;
  1324. cmDspApplyAllDefaults(ctx,inst);
  1325. return rc;
  1326. }
  1327. cmDspRC_t _cmDspScaleRangeRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1328. {
  1329. cmDspRC_t rc = kOkDspRC;
  1330. //cmDspScaleRange_t* p = (cmDspScaleRange_t*)inst;
  1331. cmDspSetEvent(ctx,inst,evt);
  1332. if( evt->dstVarId == kValInSrId )
  1333. {
  1334. double val = cmDspDouble(inst,kValInSrId);
  1335. double min_in = cmDspDouble(inst,kMinInSrId);
  1336. double max_in = cmDspDouble(inst,kMaxInSrId);
  1337. double min_out = cmDspDouble(inst,kMinOutSrId);
  1338. double max_out = cmDspDouble(inst,kMaxOutSrId);
  1339. double x = cmMax(min_in,cmMin(max_in,val));
  1340. x = (x - min_in)/(max_in - min_in);
  1341. x = min_out + x * (max_out - min_out);
  1342. cmDspSetDouble(ctx,inst,kValOutSrId, x );
  1343. //printf("%f (%f %f) : (%f %f) %f\n",val,min_in,max_in,min_out,max_out,x);
  1344. }
  1345. return rc;
  1346. }
  1347. struct cmDspClass_str* cmScaleRangeClassCons( cmDspCtx_t* ctx )
  1348. {
  1349. cmDspClassSetup(&_cmScaleRangeDC,ctx,"ScaleRange",
  1350. NULL,
  1351. _cmDspScaleRangeAlloc,
  1352. NULL,
  1353. _cmDspScaleRangeReset,
  1354. NULL,
  1355. _cmDspScaleRangeRecv,
  1356. NULL,NULL,
  1357. "Scale a value inside an input range to a value in the output range.");
  1358. return &_cmScaleRangeDC;
  1359. }
  1360. //==========================================================================================================================================
  1361. enum
  1362. {
  1363. kCntAmId,
  1364. kSflocAmId,
  1365. kLocAmId,
  1366. kTypeAmId,
  1367. kValueAmId,
  1368. kCstAmId,
  1369. kCmdAmId,
  1370. kScLocAmId,
  1371. kEvenAmId,
  1372. kDynAmId,
  1373. kTempoAmId,
  1374. kCostAmId
  1375. };
  1376. cmDspClass_t _cmActiveMeasDC;
  1377. typedef struct cmDspAmRecd_str
  1378. {
  1379. unsigned loc;
  1380. unsigned type;
  1381. double value;
  1382. double cost;
  1383. struct cmDspAmRecd_str* link;
  1384. } cmDspAmRecd_t;
  1385. /*
  1386. int cmDspActiveMeasRecdCompare(const void * p0, const void * p1)
  1387. {
  1388. return ((int)((cmDspActiveMeasRecd_t*)p0)->loc) - (int)(((cmDspActiveMeasRecd_t*)p1)->loc);
  1389. }
  1390. */
  1391. typedef struct
  1392. {
  1393. cmDspInst_t inst;
  1394. unsigned addSymId;
  1395. unsigned clearSymId;
  1396. unsigned printSymId;
  1397. unsigned rewindSymId;
  1398. cmDspAmRecd_t* array; // array[cnt]
  1399. unsigned cnt;
  1400. cmDspAmRecd_t* list; // first recd in list sorted on 'loc'.
  1401. cmDspAmRecd_t* avail; // next empty recd
  1402. cmDspAmRecd_t* sent; // last recd sent
  1403. } cmDspActiveMeas_t;
  1404. void _cmDspAmAllocList( cmDspActiveMeas_t* p, unsigned cnt )
  1405. {
  1406. assert(p->array == NULL );
  1407. cmDspAmRecd_t* r = cmMemAllocZ(cmDspAmRecd_t,cnt);
  1408. p->cnt = cnt;
  1409. p->array = r;
  1410. p->list = NULL;
  1411. p->avail = r;
  1412. p->sent = NULL;
  1413. }
  1414. cmDspRC_t _cmDspActiveMeasAdd( cmDspCtx_t* ctx, cmDspActiveMeas_t* p, unsigned loc, unsigned type, double value, double cost)
  1415. {
  1416. assert( type != kInvalidVarScId );
  1417. cmDspAmRecd_t* rp = p->list;
  1418. cmDspAmRecd_t* pp = NULL;
  1419. // search for the location to add the new record
  1420. for(; rp!=NULL; rp=rp->link)
  1421. {
  1422. // if this loc and type already exists then replace the value and cost fields
  1423. if( rp->loc==loc && rp->type==type )
  1424. goto foundLabel;
  1425. // if this loc should be inserted before rp
  1426. if( loc < rp->loc )
  1427. break;
  1428. pp = rp;
  1429. }
  1430. // if the pre-allocated list is full
  1431. if( p->avail >= p->array+p->cnt )
  1432. return cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"Unable to store new measurement record. All preallocated active measurement slots are in use.");
  1433. // if prepending to the list
  1434. if( pp == NULL )
  1435. {
  1436. rp = p->avail;
  1437. rp->link = p->list;
  1438. p->list = rp;
  1439. }
  1440. else
  1441. {
  1442. // if appending to the list after pp
  1443. if( rp == NULL )
  1444. {
  1445. // nothing to do
  1446. }
  1447. else // if inserting between pp and rp
  1448. {
  1449. p->avail->link = rp;
  1450. }
  1451. rp = p->avail;
  1452. pp->link = rp;
  1453. }
  1454. p->avail += 1;
  1455. foundLabel:
  1456. rp->loc = loc;
  1457. rp->type = type;
  1458. rp->value = value;
  1459. rp->cost = cost;
  1460. return kOkDspRC;
  1461. }
  1462. cmDspInst_t* _cmDspActiveMeasAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1463. {
  1464. cmDspVarArg_t args[] =
  1465. {
  1466. { "cnt", kCntAmId, 0,0, kInDsvFl | kUIntDsvFl, "Maximum count of active measurements."},
  1467. { "sfloc", kSflocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location input." },
  1468. { "loc", kLocAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. location." },
  1469. { "type", kTypeAmId, 0,0, kInDsvFl | kUIntDsvFl, "Meas. Type. (even,dyn,...)" },
  1470. { "val", kValueAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Value."},
  1471. { "cst", kCstAmId, 0,0, kInDsvFl | kDoubleDsvFl, "Meas. Cost."},
  1472. { "cmd", kCmdAmId, 0,0, kInDsvFl | kSymDsvFl, "Commands:add | clear | dump | rewind"},
  1473. { "scloc", kScLocAmId, 0,0, kOutDsvFl | kUIntDsvFl, "Score location"},
  1474. { "even", kEvenAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Even out"},
  1475. { "dyn", kDynAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Dyn out"},
  1476. { "tempo", kTempoAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Tempo out"},
  1477. { "cost", kCostAmId, 0,0, kOutDsvFl | kDoubleDsvFl, "Cost out"},
  1478. { NULL, 0, 0, 0, 0 }
  1479. };
  1480. cmDspActiveMeas_t* p = cmDspInstAlloc(cmDspActiveMeas_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1481. p->addSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"add");
  1482. p->clearSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"clear");
  1483. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"dump");
  1484. p->rewindSymId= cmSymTblRegisterStaticSymbol(ctx->stH,"rewind");
  1485. cmDspSetDefaultUInt( ctx,&p->inst,kCntAmId, 0,256);
  1486. cmDspSetDefaultUInt( ctx,&p->inst,kScLocAmId,0,0);
  1487. cmDspSetDefaultDouble(ctx,&p->inst,kEvenAmId, 0,0);
  1488. cmDspSetDefaultDouble(ctx,&p->inst,kDynAmId, 0,0);
  1489. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1490. cmDspSetDefaultDouble(ctx,&p->inst,kTempoAmId,0,0);
  1491. return &p->inst;
  1492. }
  1493. cmDspRC_t _cmDspActiveMeasPrint(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1494. {
  1495. cmDspAmRecd_t* rp = p->list;
  1496. for(; rp!=NULL; rp=rp->link)
  1497. {
  1498. const cmChar_t* label = "<null>";
  1499. switch( rp->type )
  1500. {
  1501. case kEvenVarScId: label="even "; break;
  1502. case kDynVarScId: label="dyn "; break;
  1503. case kTempoVarScId: label="tempo"; break;
  1504. default:
  1505. { assert(0); }
  1506. }
  1507. cmRptPrintf(ctx->rpt,"loc:%i %s %f %f\n",rp->loc,label,rp->value,rp->cost);
  1508. }
  1509. return kOkDspRC;
  1510. }
  1511. cmDspRC_t _cmDspActiveMeasClear(cmDspCtx_t* ctx, cmDspActiveMeas_t* p )
  1512. {
  1513. memset(p->array,0,sizeof(p->array[0])*p->cnt);
  1514. p->avail = p->array;
  1515. p->list = NULL;
  1516. p->avail = p->array;
  1517. return kOkDspRC;
  1518. }
  1519. cmDspRC_t _cmDspActiveMeasFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1520. {
  1521. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1522. cmMemPtrFree(&p->array);
  1523. return kOkDspRC;
  1524. }
  1525. cmDspRC_t _cmDspActiveMeasReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1526. {
  1527. cmDspRC_t rc = kOkDspRC;
  1528. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1529. cmDspApplyAllDefaults(ctx,inst);
  1530. unsigned cnt = cmMax(100,cmDspUInt(inst,kCntAmId));
  1531. _cmDspActiveMeasFree(ctx,inst,evt);
  1532. _cmDspAmAllocList(p,cnt);
  1533. return rc;
  1534. }
  1535. cmDspRC_t _cmDspActiveMeasRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1536. {
  1537. cmDspRC_t rc = kOkDspRC;
  1538. cmDspActiveMeas_t* p = (cmDspActiveMeas_t*)inst;
  1539. cmDspSetEvent(ctx,inst,evt);
  1540. switch( evt->dstVarId )
  1541. {
  1542. case kSflocAmId:
  1543. {
  1544. unsigned sfloc = cmDspUInt(inst,kSflocAmId); // get the recv'd score location
  1545. cmDspAmRecd_t* rp = p->sent==NULL ? p->list : p->sent->link; // get the next recd to send
  1546. bool fl = false;
  1547. for(; rp!=NULL; rp=rp->link)
  1548. if( rp->loc <= sfloc )
  1549. {
  1550. // deterimine the records type
  1551. unsigned varId = cmInvalidId;
  1552. switch( rp->type )
  1553. {
  1554. case kEvenVarScId: varId = kEvenAmId; break;
  1555. case kDynVarScId: varId = kDynAmId; break;
  1556. case kTempoVarScId: varId = kTempoAmId; break;
  1557. default:
  1558. { assert(0); }
  1559. }
  1560. // Sending the location triggers the avail-ch to switch - so the location should only
  1561. // be sent once.
  1562. if( !fl )
  1563. {
  1564. cmDspSetUInt(ctx,inst,kScLocAmId,rp->loc);
  1565. fl = true;
  1566. }
  1567. // transmit the records value and cost
  1568. cmDspSetDouble(ctx,inst,varId,rp->value);
  1569. cmDspSetDouble(ctx,inst,kCostAmId,rp->cost);
  1570. p->sent = rp;
  1571. }
  1572. }
  1573. break;
  1574. case kCmdAmId:
  1575. {
  1576. unsigned cmdSymId = cmDspSymbol(inst,kCmdAmId);
  1577. if( cmdSymId == p->addSymId )
  1578. rc = _cmDspActiveMeasAdd(ctx,p,cmDspUInt(inst,kLocAmId),cmDspUInt(inst,kTypeAmId),cmDspDouble(inst,kValueAmId),cmDspDouble(inst,kCstAmId));
  1579. else
  1580. if( cmdSymId == p->clearSymId )
  1581. rc = _cmDspActiveMeasClear(ctx,p);
  1582. else
  1583. if( cmdSymId == p->printSymId )
  1584. rc = _cmDspActiveMeasPrint(ctx,p);
  1585. else
  1586. if(cmdSymId == p->rewindSymId )
  1587. p->sent = NULL;
  1588. }
  1589. break;
  1590. }
  1591. /*
  1592. switch( evt->dstVarId )
  1593. {
  1594. case kSflocAmId:
  1595. if( p->nextFullIdx != cmInvalidIdx )
  1596. {
  1597. // get the recv'd score location
  1598. unsigned sflocIdx = cmDspUInt(inst,kSflocAmId);
  1599. unsigned prvLoc = cmInvalidIdx;
  1600. // for each remaining avail record
  1601. for(; p->nextFullIdx < p->nextEmptyIdx; p->nextFullIdx++)
  1602. {
  1603. cmDspActiveMeasRecd_t* r = p->array + p->nextFullIdx;
  1604. // if this records score location is after the recv'd score loc then we're done
  1605. if( r->loc > sflocIdx )
  1606. break;
  1607. // deterimine the records type
  1608. unsigned varId = cmInvalidId;
  1609. switch( r->type )
  1610. {
  1611. case kEvenVarScId: varId = kEvenAmId; break;
  1612. case kDynVarScId: varId = kDynAmId; break;
  1613. case kTempoVarScId: varId = kTempoAmId; break;
  1614. default:
  1615. { assert(0); }
  1616. }
  1617. // if this score location has not yet been sent then send it now
  1618. if( prvLoc != r->loc )
  1619. cmDspSetUInt(ctx,inst,kScLocAmId,r->loc);
  1620. // transmit the records value and cost
  1621. cmDspSetDouble(ctx,inst,varId,r->value);
  1622. cmDspSetDouble(ctx,inst,kCostAmId,r->cost);
  1623. prvLoc = r->loc;
  1624. }
  1625. }
  1626. break;
  1627. case kCmdAmId:
  1628. {
  1629. unsigned cmdSymId = cmDspSymbol(inst,kCmdAmId);
  1630. if( cmdSymId == p->addSymId )
  1631. {
  1632. if( p->nextEmptyIdx >= p->cnt )
  1633. cmDspInstErr(ctx,inst,kProcFailDspRC,"The active measurement list is full cnt=%i.",p->cnt);
  1634. else
  1635. {
  1636. cmDspActiveMeasRecd_t* r = p->array + p->nextEmptyIdx;
  1637. r->loc = cmDspUInt( inst,kLocAmId);
  1638. r->type = cmDspUInt( inst,kTypeAmId);
  1639. r->value = cmDspDouble(inst,kValueAmId);
  1640. r->cost = cmDspDouble(inst,kCstAmId);
  1641. p->nextEmptyIdx += 1;
  1642. qsort(p->array,p->nextEmptyIdx,sizeof(p->array[0]),cmDspActiveMeasRecdCompare);
  1643. if( p->nextEmptyIdx == 1 && p->nextFullIdx == cmInvalidIdx )
  1644. p->nextFullIdx = 0;
  1645. }
  1646. }
  1647. if( cmdSymId == p->clearSymId )
  1648. rc = _cmDspActiveMeasClear(ctx,p);
  1649. else
  1650. if( cmdSymId == p->printSymId )
  1651. rc = _cmDspActiveMeasPrint(ctx,p);
  1652. else
  1653. if(cmdSymId == p->rewindSymId )
  1654. p->nextFullIdx = 0;
  1655. }
  1656. break;
  1657. }
  1658. */
  1659. return rc;
  1660. }
  1661. struct cmDspClass_str* cmActiveMeasClassCons( cmDspCtx_t* ctx )
  1662. {
  1663. cmDspClassSetup(&_cmActiveMeasDC,ctx,"ActiveMeas",
  1664. NULL,
  1665. _cmDspActiveMeasAlloc,
  1666. _cmDspActiveMeasFree,
  1667. _cmDspActiveMeasReset,
  1668. NULL,
  1669. _cmDspActiveMeasRecv,
  1670. NULL,NULL,
  1671. "Scale a value inside an input range to a value in the output range.");
  1672. return &_cmActiveMeasDC;
  1673. }
  1674. //==========================================================================================================================================
  1675. // Audio MIDI Sync
  1676. /*
  1677. Usage:
  1678. 1) In the program resource file setup a list of sync points.
  1679. 'asmp' refers to a sample offset into the audio file 'af'
  1680. which should match to the midi event index 'mid' in the
  1681. midi file 'mf'.
  1682. amSync :
  1683. [
  1684. { af:"af-16" asmp:34735276 mf:"mf-10" mid:350 }
  1685. { af:"af-16" asmp:71802194 mf:"mf-10" mid:787 }
  1686. ]
  1687. 2) Feed the 'fidx' output from a wave table loaded with 'af' into the 'asmp' input port of this amSync object.
  1688. Feed the 'id' output from the MIDI file player loaded with 'mf' into the 'mid' input port of this amSync object.
  1689. 3) Run the players.
  1690. 4) When the run is complete send any message to the 'sel' port of this amSync object.
  1691. The 'frm:' field of the printed output gives the difference in samples between
  1692. MIDI and audio sync points.
  1693. If the value is positive then the MIDI point is after the Audio point.
  1694. If the value is negative then the MIDI point is before the audio point.
  1695. */
  1696. enum
  1697. {
  1698. kSelAmId,
  1699. kAFnAmId,
  1700. kASmpAmId,
  1701. kMFnAmId,
  1702. kMIdAmId,
  1703. };
  1704. enum
  1705. {
  1706. kAfnAmFl = 0x01,
  1707. kMfnAmFl = 0x02,
  1708. kAsmpAmFl = 0x04,
  1709. kMidAmFl = 0x08,
  1710. };
  1711. cmDspClass_t _cmAmSyncDC;
  1712. typedef struct cmDspAmSyncEntry_str
  1713. {
  1714. const cmChar_t* afn; // audio file name
  1715. const cmChar_t* mfn; // midi file name
  1716. unsigned asmp; // Audio sample index to sync to MIDI event
  1717. unsigned mid; // MIDI event unique id (cmMidiTrackMsg_t.uid)
  1718. int afi; // closest DSP system cycle index to the reference audio sample index (asmp).
  1719. int mfi; // DSP system cycle on which the reference MIDI event (mid) arrived.
  1720. unsigned state; // as incoming msg match this record the state is updated with kXXXAmFl flags
  1721. } cmDspAmSyncEntry_t;
  1722. typedef struct
  1723. {
  1724. cmDspInst_t inst;
  1725. cmDspAmSyncEntry_t* array;
  1726. unsigned arrayCnt;
  1727. cmDspAmSyncEntry_t* acur;
  1728. cmDspAmSyncEntry_t* mcur;
  1729. } cmDspAmSync_t;
  1730. cmDspInst_t* _cmDspAmSyncAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1731. {
  1732. cmDspVarArg_t args[] =
  1733. {
  1734. { "sel", kSelAmId, 0, 0, kInDsvFl | kTypeDsvMask, "Print and reset" },
  1735. { "afn", kAFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "Audio File name"},
  1736. { "asmp", kASmpAmId, 0, 0, kInDsvFl | kIntDsvFl, "Audio sample index"},
  1737. { "mfn", kMFnAmId, 0, 0, kInDsvFl | kStrzDsvFl, "MIDI File name"},
  1738. { "mid", kMIdAmId, 0, 0, kInDsvFl | kIntDsvFl, "MIDI Event Unique Id"},
  1739. { NULL, 0, 0, 0, 0 }
  1740. };
  1741. cmDspAmSync_t* p = cmDspInstAlloc(cmDspAmSync_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1742. return &p->inst;
  1743. }
  1744. cmDspRC_t _cmDspAmSyncFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1745. {
  1746. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  1747. cmMemFree(p->array);
  1748. return kOkDspRC;
  1749. }
  1750. cmDspRC_t _cmDspAmSyncReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1751. {
  1752. cmDspRC_t rc = kOkDspRC;
  1753. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  1754. cmDspApplyAllDefaults(ctx,inst);
  1755. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(ctx->dspH);
  1756. cmJsonNode_t* np;
  1757. const cmChar_t* errLabelPtr;
  1758. unsigned i;
  1759. cmJsRC_t jsRC;
  1760. if((np = cmJsonFindValue(jsH, "amSync", NULL, kArrayTId)) == NULL )
  1761. {
  1762. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The AUDIO MIDI Sync cfg. record was not found.");
  1763. goto errLabel;
  1764. }
  1765. p->arrayCnt = cmJsonChildCount(np);
  1766. p->array = cmMemResizeZ(cmDspAmSyncEntry_t,p->array,p->arrayCnt);
  1767. for(i=0; i<p->arrayCnt; ++i)
  1768. {
  1769. cmJsonNode_t* anp = cmJsonArrayElement(np,i);
  1770. cmDspAmSyncEntry_t* r = p->array + i;
  1771. if( (jsRC = cmJsonMemberValues(anp,&errLabelPtr,
  1772. "af", kStringTId,&r->afn,
  1773. "asmp",kIntTId, &r->asmp,
  1774. "mf", kStringTId,&r->mfn,
  1775. "mid", kIntTId, &r->mid,
  1776. NULL)) != kOkJsRC )
  1777. {
  1778. if( jsRC == kNodeNotFoundJsRC )
  1779. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The Audio-MIDI Sync cfg. field '%s' was missing in the cfg. record at index %i.",errLabelPtr,i);
  1780. else
  1781. rc = cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The AUDIO MIDI Sync cfg. parse failed on the record at index %i.",i);
  1782. break;
  1783. }
  1784. r->afi = cmInvalidIdx;
  1785. r->mfi = cmInvalidIdx;
  1786. r->state = 0;
  1787. }
  1788. errLabel:
  1789. return rc;
  1790. }
  1791. cmDspRC_t _cmDspAmSyncRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1792. {
  1793. cmDspAmSync_t* p = (cmDspAmSync_t*)inst;
  1794. unsigned i;
  1795. if( evt->dstVarId != kSelAmId )
  1796. cmDspSetEvent(ctx,inst,evt);
  1797. switch(evt->dstVarId)
  1798. {
  1799. case kSelAmId:
  1800. {
  1801. double srate = cmDspSysSampleRate(ctx->dspH);
  1802. int fpc = cmDspSamplesPerCycle(ctx);
  1803. for(i=0; i<p->arrayCnt; ++i)
  1804. {
  1805. cmDspAmSyncEntry_t* r = p->array + i;
  1806. int dframes = r->mfi-r->afi;
  1807. cmRptPrintf(ctx->rpt,"0x%x : %s %i %i - %s %i %i : frm:%i smp:%i sec:%f\n",
  1808. r->state,
  1809. r->afn,
  1810. r->asmp,
  1811. r->afi,
  1812. r->mfn,
  1813. r->mid,
  1814. r->mfi,
  1815. dframes,
  1816. dframes*fpc,dframes*fpc/srate);
  1817. r->afi = cmInvalidIdx;
  1818. r->mfi = cmInvalidIdx;
  1819. r->state = 0;
  1820. }
  1821. p->acur = NULL;
  1822. p->mcur = NULL;
  1823. }
  1824. break;
  1825. case kAFnAmId:
  1826. {
  1827. // an audio file name just arrived - set p->acur to point to it
  1828. const cmChar_t* fn = cmDspStrcz(inst, kAFnAmId );
  1829. for(i=0; i<p->arrayCnt; ++i)
  1830. if( strcmp(fn,p->array[i].afn) == 0 )
  1831. {
  1832. p->array[i].state = cmSetFlag(p->array[i].state,kAfnAmFl);
  1833. p->acur = p->array + i;
  1834. }
  1835. }
  1836. break;
  1837. case kMFnAmId:
  1838. {
  1839. // a midi file name just arrived - set p->mcur to point to it
  1840. const cmChar_t* fn = cmDspStrcz(inst, kMFnAmId );
  1841. for(i=0; i<p->arrayCnt; ++i)
  1842. if( strcmp(fn,p->array[i].mfn) == 0 )
  1843. {
  1844. p->array[i].state = cmSetFlag(p->array[i].state,kMfnAmFl);
  1845. p->mcur = p->array + i;
  1846. }
  1847. }
  1848. break;
  1849. case kASmpAmId:
  1850. {
  1851. // a audio file sample index has just arrived
  1852. int v = cmDspInt(inst,kASmpAmId);
  1853. // if a valid audio file has been set
  1854. if( p->acur != NULL )
  1855. for(i=0; i<p->arrayCnt; ++i)
  1856. {
  1857. // if the audio sync point is before or on the new audio file sample index then
  1858. // this is the closest audio file index to the audio sync point - record the
  1859. // associated cycleCnt
  1860. cmDspAmSyncEntry_t* r = p->array + i;
  1861. if( cmIsNotFlag(r->state,kAsmpAmFl) && r->asmp <= v && strcmp(p->acur->afn,r->afn)==0 )
  1862. {
  1863. r->afi = ctx->cycleCnt;
  1864. r->state = cmSetFlag(r->state,kAsmpAmFl);
  1865. break;
  1866. }
  1867. }
  1868. }
  1869. break;
  1870. case kMIdAmId:
  1871. {
  1872. // a new MIDI event was received
  1873. int v = cmDspInt(inst,kMIdAmId);
  1874. if( p->mcur != NULL )
  1875. for(i=0; i<p->arrayCnt; ++i)
  1876. {
  1877. // if the new MIDI event matched the MIDI sync point then record the
  1878. // current cycleCnt.
  1879. cmDspAmSyncEntry_t* r = p->array + i;
  1880. if( cmIsNotFlag(r->state,kMidAmFl) && r->mid == v && strcmp(p->mcur->mfn,r->mfn)==0 )
  1881. {
  1882. r->mfi = ctx->cycleCnt;
  1883. r->state = cmSetFlag(r->state,kMidAmFl);
  1884. break;
  1885. }
  1886. }
  1887. }
  1888. break;
  1889. }
  1890. return kOkDspRC;
  1891. }
  1892. struct cmDspClass_str* cmAmSyncClassCons( cmDspCtx_t* ctx )
  1893. {
  1894. cmDspClassSetup(&_cmAmSyncDC,ctx,"AmSync",
  1895. NULL,
  1896. _cmDspAmSyncAlloc,
  1897. _cmDspAmSyncFree,
  1898. _cmDspAmSyncReset,
  1899. NULL,
  1900. _cmDspAmSyncRecv,
  1901. NULL,NULL,
  1902. "Audio - MIDI Sync Object.");
  1903. return &_cmAmSyncDC;
  1904. }
  1905. //==========================================================================================================================================
  1906. enum
  1907. {
  1908. kPgmNmId,
  1909. kStatusNmId,
  1910. kD0NmId,
  1911. kD1NmId,
  1912. kThruNmId
  1913. };
  1914. cmDspClass_t _cmNanoMapDC;
  1915. typedef struct
  1916. {
  1917. cmDspInst_t inst;
  1918. } cmDspNanoMap_t;
  1919. cmDspRC_t _cmDspNanoMapSend( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned st, unsigned d0, unsigned d1 )
  1920. {
  1921. cmDspSetUInt(ctx,inst,kD1NmId,d1);
  1922. cmDspSetUInt(ctx,inst,kD0NmId,d0);
  1923. cmDspSetUInt(ctx,inst,kStatusNmId,st);
  1924. return kOkDspRC;
  1925. }
  1926. void _cmDspNanoMapPgm( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned pgm )
  1927. {
  1928. //cmDspNanoMap_t* p = (cmDspNanoMap_t*)inst;
  1929. unsigned i;
  1930. for(i=0; i<kMidiChCnt; ++i)
  1931. {
  1932. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,121,0); // reset all controllers
  1933. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,123,0); // turn all notes off
  1934. _cmDspNanoMapSend(ctx,inst,kCtlMdId+i,0,0); // switch to bank 0
  1935. _cmDspNanoMapSend(ctx,inst,kPgmMdId+i,pgm,0); // send pgm change
  1936. cmSleepMs(15);
  1937. }
  1938. }
  1939. cmDspInst_t* _cmDspNanoMapAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1940. {
  1941. cmDspVarArg_t args[] =
  1942. {
  1943. { "pgm", kPgmNmId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reprogram all channels to this pgm." },
  1944. { "status", kStatusNmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI status" },
  1945. { "d0", kD0NmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d0" },
  1946. { "d1", kD1NmId, 0, 0, kOutDsvFl | kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "MIDI channel message d1" },
  1947. { "thru", kThruNmId, 0, 0, kInDsvFl | kBoolDsvFl | kOptArgDsvFl, "Enable pass through."},
  1948. { NULL, 0, 0, 0, 0 }
  1949. };
  1950. cmDspNanoMap_t* p = cmDspInstAlloc(cmDspNanoMap_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1951. cmDspSetDefaultUInt(ctx,&p->inst, kPgmNmId, 0, 0 );
  1952. return &p->inst;
  1953. }
  1954. cmDspRC_t _cmDspNanoMapReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1955. {
  1956. cmDspRC_t rc = kOkDspRC;
  1957. cmDspApplyAllDefaults(ctx,inst);
  1958. _cmDspNanoMapPgm(ctx,inst,cmDspUInt(inst,kPgmNmId));
  1959. return rc;
  1960. }
  1961. cmDspRC_t _cmDspNanoMapRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1962. {
  1963. //cmDspNanoMap_t* p = (cmDspNanoMap_t*)inst;
  1964. switch( evt->dstVarId )
  1965. {
  1966. case kPgmNmId:
  1967. cmDspSetEvent(ctx,inst,evt);
  1968. _cmDspNanoMapPgm(ctx,inst,cmDspUInt(inst,kPgmNmId));
  1969. break;
  1970. case kStatusNmId:
  1971. {
  1972. unsigned status = cmDsvGetUInt(evt->valuePtr);
  1973. unsigned stat_no_ch = status & 0xf0;
  1974. if( stat_no_ch == kNoteOnMdId || stat_no_ch == kNoteOffMdId || stat_no_ch == kCtlMdId )
  1975. {
  1976. //unsigned d0 = cmDspUInt(inst,kD0NmId);
  1977. unsigned ch = 0; //d0 % 8;
  1978. status = (status & 0xf0) + ch;
  1979. cmDspSetUInt(ctx,inst,kStatusNmId,status);
  1980. }
  1981. }
  1982. break;
  1983. default:
  1984. cmDspSetEvent(ctx,inst,evt);
  1985. break;
  1986. }
  1987. return kOkDspRC;
  1988. }
  1989. struct cmDspClass_str* cmNanoMapClassCons( cmDspCtx_t* ctx )
  1990. {
  1991. cmDspClassSetup(&_cmNanoMapDC,ctx,"NanoMap",
  1992. NULL,
  1993. _cmDspNanoMapAlloc,
  1994. NULL,
  1995. _cmDspNanoMapReset,
  1996. NULL,
  1997. _cmDspNanoMapRecv,
  1998. NULL,
  1999. NULL,
  2000. "Nanosynth Mapper");
  2001. return &_cmNanoMapDC;
  2002. }
  2003. //==========================================================================================================================================
  2004. enum
  2005. {
  2006. kChCntPrId,
  2007. kFnPrId,
  2008. kSecsPrId,
  2009. kMaxLaSecsPrId,
  2010. kCurLaSecsPrId,
  2011. kFadeRatePrId,
  2012. kScInitLocIdxPrId,
  2013. kScLocIdxPrId,
  2014. kCmdPrId,
  2015. kInAudioBasePrId
  2016. };
  2017. cmDspClass_t _cmRecdPlayDC;
  2018. typedef struct
  2019. {
  2020. cmDspInst_t inst;
  2021. cmRecdPlay* rcdply;
  2022. cmScH_t scH;
  2023. unsigned onSymId;
  2024. unsigned offSymId;
  2025. unsigned audioOutBaseId;
  2026. unsigned chCnt;
  2027. //unsigned scLocIdx;
  2028. } cmDspRecdPlay_t;
  2029. cmDspRC_t _cmDspRecdPlayParseRsrc( cmDspCtx_t* ctx, cmDspInst_t* inst, cmRecdPlay* rcdply )
  2030. {
  2031. cmDspRC_t rc = kOkDspRC;
  2032. const cmChar_t* path = NULL;
  2033. // read the 'recdplay' audio file path
  2034. if( cmDspRsrcString( ctx->dspH, &path, "recdPlayPath", NULL ) != kOkDspRC )
  2035. {
  2036. cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The 'recdPlayPath' resource string was not found.");
  2037. }
  2038. if( path == NULL )
  2039. path = "";
  2040. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(ctx->dspH);
  2041. cmJsonNode_t* jnp = cmJsonFindValue(jsH,"recdPlay",NULL, kArrayTId);
  2042. if( jnp == NULL || cmJsonIsArray(jnp)==false )
  2043. {
  2044. // this is really a warning - the object does not require preloaded segments.
  2045. cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"Warning: The 'recdPlay' resource used to define pre-loaded segments was not found.");
  2046. return kOkDspRC;
  2047. }
  2048. unsigned n = cmJsonChildCount(jnp);
  2049. unsigned i;
  2050. // for each 'recdplay' segment record
  2051. for(i=0; i<n && rc==kOkDspRC; ++i)
  2052. {
  2053. cmJsonNode_t* cnp = cmJsonArrayElement(jnp,i);
  2054. const cmChar_t* label = NULL;
  2055. unsigned segSymId = cmInvalidId;
  2056. const cmChar_t* errLabel = NULL;
  2057. const cmChar_t* fn = NULL;
  2058. // read the ith segment record
  2059. if( cmJsonMemberValues(cnp,&errLabel,
  2060. "label", kStringTId, &label,
  2061. "file", kStringTId, &fn,
  2062. NULL) != kOkJsRC )
  2063. {
  2064. rc = cmDspInstErr(ctx,inst,kRsrcNotFoundDspRC,"The record at index %i in the 'recdPlay' pre-loaded segment list could not be parsed.",i);
  2065. goto errLabel;
  2066. }
  2067. // find or generate the symbol id for the segment label symbol
  2068. if((segSymId = cmSymTblRegisterSymbol(ctx->stH,label)) == cmInvalidId )
  2069. {
  2070. rc = cmDspInstErr(ctx,inst,kSymNotFoundDspRC,"The 'recdPlay' pre-load segment symbol '%s' could not be found or registered.",cmStringNullGuard(label));
  2071. goto errLabel;
  2072. }
  2073. // create the full path name for the segment audio file
  2074. if((fn = cmFsMakeFn( path, fn, NULL, NULL )) == NULL )
  2075. {
  2076. rc = cmDspInstErr(ctx,inst,kFileSysFailDspRC,"The 'recdPlay' file name '%s/%s' could not be generated.",cmStringNullGuard(path),cmStringNullGuard(fn));
  2077. goto errLabel;
  2078. }
  2079. // pre-load the segment
  2080. if( cmRecdPlayInsertRecord(rcdply,segSymId,fn) != cmOkRC )
  2081. rc = cmDspInstErr(ctx,inst,kSubSysFailDspRC,"The 'recdPlay' segment label:'%s' file:'%s' could not be loaded.",cmStringNullGuard(label),cmStringNullGuard(fn));
  2082. cmFsFreeFn(fn);
  2083. }
  2084. errLabel:
  2085. return rc;
  2086. }
  2087. cmDspRC_t _cmDspRecdPlayOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2088. {
  2089. cmDspRC_t rc =kOkDspRC;
  2090. const cmChar_t* fn;
  2091. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2092. //p->scLocIdx = 0;
  2093. if((fn = cmDspStrcz(inst,kFnPrId)) == NULL || strlen(fn)==0 )
  2094. return cmDspInstErr(ctx,inst, kInvalidArgDspRC, "No score file name supplied.");
  2095. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  2096. return cmDspInstErr(ctx,inst, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  2097. if( cmScoreIsValid(p->scH) )
  2098. {
  2099. unsigned i;
  2100. unsigned markerCnt = cmScoreMarkerLabelCount(p->scH);
  2101. double initFragSecs = cmDspDouble(inst,kSecsPrId);
  2102. double maxLaSecs = cmDspDouble(inst,kMaxLaSecsPrId);
  2103. double curLaSecs = cmDspDouble(inst,kCurLaSecsPrId);
  2104. printf("2 max la secs:%f\n",cmDspDouble(inst,kMaxLaSecsPrId));
  2105. if((p->rcdply = cmRecdPlayAlloc(ctx->cmProcCtx, NULL, cmDspSampleRate(ctx), markerCnt, p->chCnt, initFragSecs, maxLaSecs, curLaSecs)) == NULL)
  2106. return cmErrMsg(&inst->classPtr->err,kSubSysFailDspRC,"Unable to create the internal recorder-player object.");
  2107. for(i=0; i<markerCnt; ++i)
  2108. cmRecdPlayRegisterFrag(p->rcdply,i, cmScoreMarkerLabelSymbolId(p->scH,i ));
  2109. if((rc = _cmDspRecdPlayParseRsrc(ctx,inst,p->rcdply)) != kOkDspRC )
  2110. rc = cmDspInstErr(ctx,inst,kInstResetFailDspRC,"The 'recdplay' segment pre-load failed.");
  2111. //p->scLocIdx = cmDspUInt(inst,kScInitLocIdxPrId);
  2112. }
  2113. return rc;
  2114. }
  2115. cmDspInst_t* _cmDspRecdPlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2116. {
  2117. if( va_cnt < 1 )
  2118. {
  2119. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'RecdPlay' constructor must have a count of input ports.");
  2120. return NULL;
  2121. }
  2122. va_list vl1;
  2123. va_copy(vl1,vl);
  2124. int chCnt = va_arg(vl,int);
  2125. unsigned audioOutBase = kInAudioBasePrId + chCnt;
  2126. cmDspRecdPlay_t* p = cmDspInstAllocV(cmDspRecdPlay_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl1,
  2127. 1, "chs", kChCntPrId, 0,0, kUIntDsvFl | kReqArgDsvFl, "channel count.",
  2128. 1, "fn", kFnPrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." ,
  2129. 1, "secs", kSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Initial fragment allocation in seconds.",
  2130. 1, "maxla", kMaxLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kReqArgDsvFl, "Maximum look-ahead buffer in seconds.",
  2131. 1, "curla", kCurLaSecsPrId, 0,0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Current look-head buffer in seconds.",
  2132. 1, "frate", kFadeRatePrId, 0,0, kInDsvFl | kDoubleDsvFl | kOptArgDsvFl, "Fade rate in dB per second.",
  2133. 1, "initIdx",kScInitLocIdxPrId,0,0,kInDsvFl | kUIntDsvFl, "Score search start location.",
  2134. 1, "index", kScLocIdxPrId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower location index.",
  2135. 1, "cmd", kCmdPrId, 0,0, kInDsvFl | kSymDsvFl, "on=reset off=stop.",
  2136. chCnt, "in", kInAudioBasePrId,0,1, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2137. chCnt, "out", audioOutBase, 0,1, kOutDsvFl | kAudioBufDsvFl, "Audio output",
  2138. 0 );
  2139. va_end(vl1);
  2140. p->onSymId = cmSymTblId(ctx->stH,"on");
  2141. p->offSymId = cmSymTblId(ctx->stH,"off");
  2142. p->audioOutBaseId = audioOutBase;
  2143. p->chCnt = chCnt;
  2144. //p->scLocIdx = 0;
  2145. printf("0 max la secs:%f\n",cmDspDouble(&p->inst,kMaxLaSecsPrId));
  2146. cmDspSetDefaultDouble(ctx,&p->inst, kSecsPrId, 0.0, 10.0 );
  2147. cmDspSetDefaultDouble(ctx,&p->inst, kMaxLaSecsPrId,0.0, 2.0);
  2148. cmDspSetDefaultDouble(ctx,&p->inst, kCurLaSecsPrId,0.0, 0.1);
  2149. cmDspSetDefaultDouble(ctx,&p->inst, kFadeRatePrId, 0.0, 1.0);
  2150. cmDspSetDefaultUInt( ctx,&p->inst, kScInitLocIdxPrId,0,0);
  2151. printf("1 max la secs:%f\n",cmDspDouble(&p->inst,kMaxLaSecsPrId));
  2152. return &p->inst;
  2153. }
  2154. cmDspRC_t _cmDspRecdPlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2155. {
  2156. cmDspRC_t rc = kOkDspRC;
  2157. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2158. cmRecdPlayFree(&p->rcdply);
  2159. cmScoreFinalize(&p->scH);
  2160. return rc;
  2161. }
  2162. cmDspRC_t _cmDspRecdPlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2163. {
  2164. cmDspRC_t rc;
  2165. if((rc = _cmDspRecdPlayOpenScore(ctx,inst)) == kOkDspRC )
  2166. cmDspApplyAllDefaults(ctx,inst);
  2167. return rc;
  2168. }
  2169. cmDspRC_t _cmDspRecdPlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2170. {
  2171. cmDspRC_t rc = kOkDspRC;
  2172. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2173. const cmSample_t* x[ p->chCnt ];
  2174. cmSample_t* y[ p->chCnt ];
  2175. unsigned n = 0;
  2176. unsigned i;
  2177. unsigned actChCnt = 0;
  2178. for(i=0; i<p->chCnt; ++i)
  2179. {
  2180. if( cmDspIsAudioInputConnected(ctx,inst,kInAudioBasePrId+i) == false )
  2181. {
  2182. x[i] = NULL;
  2183. y[i] = NULL;
  2184. }
  2185. else
  2186. {
  2187. if( i==0 )
  2188. n = cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0);
  2189. else
  2190. {
  2191. assert( n == cmDspAudioBufSmpCount(ctx,inst,kInAudioBasePrId+i,0));
  2192. }
  2193. x[i] = cmDspAudioBuf(ctx,inst,kInAudioBasePrId+i,0);
  2194. if( x[i] != NULL )
  2195. {
  2196. y[i] = cmDspAudioBuf(ctx,inst,p->audioOutBaseId+i,0);
  2197. if( y[i] != NULL )
  2198. {
  2199. assert( n == cmDspAudioBufSmpCount(ctx,inst,p->audioOutBaseId+i,0));
  2200. cmVOS_Zero(y[i],n);
  2201. actChCnt += 1;
  2202. }
  2203. }
  2204. }
  2205. }
  2206. cmRecdPlayExec(p->rcdply,x,y,actChCnt,n);
  2207. return rc;
  2208. }
  2209. cmDspRC_t _cmDspRecdPlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2210. {
  2211. cmDspRecdPlay_t* p = (cmDspRecdPlay_t*)inst;
  2212. if( p->rcdply == NULL )
  2213. return kOkDspRC;
  2214. cmDspSetEvent(ctx,inst,evt);
  2215. switch( evt->dstVarId )
  2216. {
  2217. case kCmdPrId:
  2218. if( cmDspSymbol(inst,kCmdPrId) == p->onSymId )
  2219. {
  2220. printf("rewind\n");
  2221. cmRecdPlayRewind(p->rcdply);
  2222. //p->scLocIdx = cmDspUInt(inst,kScInitLocIdxPrId);
  2223. }
  2224. else
  2225. if( cmDspSymbol(inst,kCmdPrId) == p->offSymId )
  2226. {
  2227. }
  2228. break;
  2229. case kCurLaSecsPrId:
  2230. cmRecdPlaySetLaSecs(p->rcdply, cmDspDouble(inst,kCurLaSecsPrId));
  2231. break;
  2232. case kScInitLocIdxPrId:
  2233. printf("init-idx:%i\n",cmDspUInt(inst,kScInitLocIdxPrId));
  2234. break;
  2235. case kScLocIdxPrId:
  2236. {
  2237. unsigned endScLocIdx = cmDspUInt(inst,kScLocIdxPrId);
  2238. if( endScLocIdx < cmDspUInt(inst,kScInitLocIdxPrId) )
  2239. break;
  2240. cmScoreLoc_t* loc = cmScoreLoc(p->scH, endScLocIdx );
  2241. if( loc == NULL )
  2242. break;
  2243. cmScoreMarker_t* mp = loc->markList;
  2244. for(; mp!=NULL; mp=mp->link)
  2245. switch( mp->markTypeId )
  2246. {
  2247. case kRecdBegScMId:
  2248. printf("recd-beg %s\n",cmSymTblLabel(ctx->stH,mp->labelSymId));
  2249. cmRecdPlayBeginRecord(p->rcdply, mp->labelSymId );
  2250. break;
  2251. case kRecdEndScMId:
  2252. printf("recd-end %s\n",cmSymTblLabel(ctx->stH,mp->labelSymId));
  2253. cmRecdPlayEndRecord(p->rcdply, mp->labelSymId );
  2254. break;
  2255. case kPlayBegScMId:
  2256. printf("play-beg\n");
  2257. cmRecdPlayBeginPlay(p->rcdply, mp->labelSymId );
  2258. break;
  2259. case kPlayEndScMId:
  2260. printf("play-end\n");
  2261. cmRecdPlayEndPlay(p->rcdply, mp->labelSymId );
  2262. break;
  2263. case kFadeScMId:
  2264. printf("fade-beg\n");
  2265. cmRecdPlayBeginFade(p->rcdply, mp->labelSymId, cmDspDouble(inst,kFadeRatePrId) );
  2266. break;
  2267. default:
  2268. break;
  2269. }
  2270. //p->scLocIdx = endScLocIdx+1;
  2271. }
  2272. break;
  2273. }
  2274. return kOkDspRC;
  2275. }
  2276. struct cmDspClass_str* cmRecdPlayClassCons( cmDspCtx_t* ctx )
  2277. {
  2278. cmDspClassSetup(&_cmRecdPlayDC,ctx,"RecdPlay",
  2279. NULL,
  2280. _cmDspRecdPlayAlloc,
  2281. _cmDspRecdPlayFree,
  2282. _cmDspRecdPlayReset,
  2283. _cmDspRecdPlayExec,
  2284. _cmDspRecdPlayRecv,
  2285. NULL,
  2286. NULL,
  2287. "Score controlled live recorder/player");
  2288. return &_cmRecdPlayDC;
  2289. }
  2290. //==========================================================================================================================================
  2291. enum
  2292. {
  2293. kHopFactGrId,
  2294. kInGrId,
  2295. kHzBaseGrId,
  2296. };
  2297. cmDspClass_t _cmGoertzelDC;
  2298. typedef struct
  2299. {
  2300. cmDspInst_t inst;
  2301. cmGoertzel* g;
  2302. double outPhs;
  2303. unsigned outBaseGrId;
  2304. unsigned chCnt;
  2305. } cmDspGoertzel_t;
  2306. cmDspInst_t* _cmDspGoertzelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2307. {
  2308. if( va_cnt !=3 )
  2309. {
  2310. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'Goertzel' constructor must have two arguments: a channel count and frequency array.");
  2311. return NULL;
  2312. }
  2313. va_list vl1;
  2314. va_copy(vl1,vl);
  2315. unsigned hopFact = va_arg(vl,unsigned);
  2316. int chCnt = va_arg(vl,int);
  2317. double* hzV = va_arg(vl,double*);
  2318. unsigned outBaseGrId = kHzBaseGrId + chCnt;
  2319. unsigned i;
  2320. cmDspGoertzel_t* p = cmDspInstAllocV(cmDspGoertzel_t,ctx,classPtr,instSymId,id,storeSymId,1,vl1,
  2321. 1, "hop", kHopFactGrId, 0,0, kInDsvFl | kDoubleDsvFl, "Hop factor",
  2322. 1, "in", kInGrId, 0,1, kInDsvFl | kAudioBufDsvFl, "Audio input",
  2323. chCnt, "hz", kHzBaseGrId, 0,0, kInDsvFl | kDoubleDsvFl, "Hz input.",
  2324. chCnt, "out", outBaseGrId, 0,1, kOutDsvFl | kDoubleDsvFl, "Detector output",
  2325. 0 );
  2326. va_end(vl1);
  2327. p->outBaseGrId = outBaseGrId;
  2328. p->chCnt = chCnt;
  2329. p->g = cmGoertzelAlloc(ctx->cmProcCtx, NULL, 0, NULL, 0,0,0,0 );
  2330. cmDspSetDefaultUInt(ctx,&p->inst, kHopFactGrId, 0, cmMax(hopFact,1));
  2331. for(i=0; i<chCnt; ++i)
  2332. cmDspSetDefaultDouble(ctx,&p->inst, kHzBaseGrId+i, 0.0, hzV[i] );
  2333. return &p->inst;
  2334. }
  2335. cmDspRC_t _cmDspGoertzelSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2336. {
  2337. cmDspRC_t rc = kOkDspRC;
  2338. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2339. unsigned hopFact = cmDspUInt(inst,kHopFactGrId);
  2340. unsigned procSmpCnt = cmDspAudioBufSmpCount(ctx,inst,kInGrId,0);
  2341. unsigned wndSmpCnt = procSmpCnt * hopFact;
  2342. double fcHzV[ p->chCnt ];
  2343. unsigned i;
  2344. for(i=0; i<p->chCnt; ++i)
  2345. {
  2346. double hz;
  2347. if( p->g->ch == NULL || p->g->ch[i].hz == 0 )
  2348. hz = cmDspDouble(inst,kHzBaseGrId);
  2349. else
  2350. hz = p->g->ch[i].hz;
  2351. fcHzV[i] = hz;
  2352. }
  2353. if( cmGoertzelInit(p->g,cmDspSysSampleRate(ctx->dspH),fcHzV,p->chCnt,procSmpCnt,procSmpCnt,wndSmpCnt) != cmOkRC )
  2354. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal Goertzel detector.");
  2355. return rc;
  2356. }
  2357. cmDspRC_t _cmDspGoertzelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2358. {
  2359. cmDspRC_t rc = kOkDspRC;
  2360. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2361. cmGoertzelFree(&p->g);
  2362. return rc;
  2363. }
  2364. cmDspRC_t _cmDspGoertzelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2365. {
  2366. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2367. cmDspApplyAllDefaults(ctx,inst);
  2368. p->outPhs = 0;
  2369. return _cmDspGoertzelSetup(ctx, inst );
  2370. }
  2371. cmDspRC_t _cmDspGoertzelExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2372. {
  2373. cmDspRC_t rc = kOkDspRC;
  2374. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2375. const cmSample_t* x = cmDspAudioBuf(ctx,inst,kInGrId,0);
  2376. unsigned n = cmDspAudioBufSmpCount(ctx,inst,kInGrId,0);
  2377. double outMs = 50.0;
  2378. double outPhsMax = outMs * cmDspSysSampleRate(ctx->dspH) / 1000.0;
  2379. double outV[ p->chCnt ];
  2380. unsigned i;
  2381. if( x != NULL )
  2382. {
  2383. cmGoertzelExec(p->g,x,n,outV,p->chCnt);
  2384. p->outPhs += n;
  2385. if( p->outPhs > outPhsMax )
  2386. {
  2387. while( p->outPhs > outPhsMax )
  2388. p->outPhs -= outPhsMax;
  2389. for(i=0; i<p->chCnt; ++i)
  2390. {
  2391. cmDspSetDouble(ctx,inst,p->outBaseGrId+i,outV[i]);
  2392. //printf("%f ",outV[i]);
  2393. }
  2394. //printf("\n");
  2395. }
  2396. }
  2397. return rc;
  2398. }
  2399. cmDspRC_t _cmDspGoertzelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2400. {
  2401. cmDspGoertzel_t* p = (cmDspGoertzel_t*)inst;
  2402. cmDspSetEvent(ctx,inst,evt);
  2403. if( kHzBaseGrId <= evt->dstVarId && evt->dstVarId < kHzBaseGrId+p->chCnt )
  2404. cmGoertzelSetFcHz(p->g, evt->dstVarId - kHzBaseGrId, cmDspDouble(inst,evt->dstVarId));
  2405. else
  2406. {
  2407. if( evt->dstVarId==kHopFactGrId )
  2408. {
  2409. _cmDspGoertzelSetup(ctx,inst);
  2410. }
  2411. }
  2412. return kOkDspRC;
  2413. }
  2414. struct cmDspClass_str* cmGoertzelClassCons( cmDspCtx_t* ctx )
  2415. {
  2416. cmDspClassSetup(&_cmGoertzelDC,ctx,"Goertzel",
  2417. NULL,
  2418. _cmDspGoertzelAlloc,
  2419. _cmDspGoertzelFree,
  2420. _cmDspGoertzelReset,
  2421. _cmDspGoertzelExec,
  2422. _cmDspGoertzelRecv,
  2423. NULL,
  2424. NULL,
  2425. "Goertzel Tone Detector Filter");
  2426. return &_cmGoertzelDC;
  2427. }
  2428. //==========================================================================================================================================
  2429. enum
  2430. {
  2431. kRecdDirSrId,
  2432. kSrFnSrId,
  2433. kAfSrId,
  2434. kBitsSrId,
  2435. kCmdSrId,
  2436. kStatusSrId,
  2437. kD0SrId,
  2438. kD1SrId,
  2439. kSecSrId,
  2440. kNSecSrId,
  2441. kAinBaseSrId
  2442. };
  2443. cmDspClass_t _cmSyncRecdDC;
  2444. typedef struct
  2445. {
  2446. cmDspInst_t inst;
  2447. unsigned chCnt;
  2448. cmTimeSpec_t ats;
  2449. cmSyncRecdH_t srH;
  2450. unsigned openSymId;
  2451. unsigned closeSymId;
  2452. const cmChar_t* aFn;
  2453. const cmChar_t* srFn;
  2454. unsigned smpIdx;
  2455. } cmDspSyncRecd_t;
  2456. cmDspRC_t _cmDspSyncRecdCreateFile( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2457. {
  2458. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2459. const cmChar_t* aFn = cmDspStrcz(inst,kAfSrId);
  2460. const cmChar_t* srFn = cmDspStrcz(inst,kSrFnSrId);
  2461. const cmChar_t* dir = cmDspStrcz(inst,kRecdDirSrId);
  2462. if( !cmFsIsDir(dir) )
  2463. return cmDspInstErr(ctx,&p->inst,kInvalidArgDspRC,"'%s' is not a valid directory.",cmStringNullGuard(dir));
  2464. cmMemPtrFree(&p->aFn);
  2465. if( cmFsGenFn(dir,aFn,"aiff",&p->aFn) != kOkFsRC )
  2466. return cmDspInstErr(ctx,&p->inst,kFileSysFailDspRC,"Audio file name generation failed for dir='%s' and prefix='%s'.",cmStringNullGuard(dir),cmStringNullGuard(aFn));
  2467. cmMemPtrFree(&p->srFn);
  2468. if( cmFsGenFn(dir,srFn,"sr",&p->srFn) != kOkFsRC )
  2469. return cmDspInstErr(ctx,&p->inst,kFileSysFailDspRC,"Sync-recd file name generation failed for dir='%s' and prefix='%s'.",cmStringNullGuard(dir),cmStringNullGuard(srFn));
  2470. unsigned bits = cmDspUInt(inst,kBitsSrId);
  2471. if( cmSyncRecdCreate( ctx->cmCtx, &p->srH, p->srFn, p->aFn, cmDspSampleRate(ctx), p->chCnt, bits ) != kOkSyRC )
  2472. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd file create failed for '%s'.",p->srFn);
  2473. p->smpIdx = 0;
  2474. return kOkDspRC;
  2475. }
  2476. cmDspInst_t* _cmDspSyncRecdAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2477. {
  2478. cmDspSyncRecd_t* p = cmDspInstAllocV(cmDspSyncRecd_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2479. 1, "dir", kRecdDirSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Recording directory.",
  2480. 1, "srfn", kSrFnSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "SyncRecd file prefix.",
  2481. 1, "afn", kAfSrId, 0,0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Audio file prefix.",
  2482. 1, "bits", kBitsSrId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Audio file bits per sample.",
  2483. 1, "cmd", kCmdSrId, 0,0, kInDsvFl | kSymDsvFl, "Command: open | close",
  2484. 1, "status", kStatusSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI status",
  2485. 1, "d0", kD0SrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI d0",
  2486. 1, "d1", kD1SrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI d1",
  2487. 1, "sec", kSecSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI Timestamp Seconds",
  2488. 1, "nsec", kNSecSrId, 0,0, kInDsvFl | kUIntDsvFl, "MIDI Timestamp Nanoseconds",
  2489. 2, "ain", kAinBaseSrId, 0,1, kInDsvFl | kAudioBufDsvFl, "Audio Input",
  2490. 0 );
  2491. p->chCnt = 2;
  2492. p->openSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"open");
  2493. p->closeSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"close");
  2494. cmDspSetDefaultUInt(ctx,&p->inst,kBitsSrId,0,16);
  2495. return &p->inst;
  2496. }
  2497. cmDspRC_t _cmDspSyncRecdFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2498. {
  2499. cmDspRC_t rc = kOkDspRC;
  2500. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2501. cmMemPtrFree(&p->aFn);
  2502. cmMemPtrFree(&p->srFn);
  2503. cmSyncRecdFinal(&p->srH);
  2504. return rc;
  2505. }
  2506. cmDspRC_t _cmDspSyncRecdReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2507. {
  2508. cmDspRC_t rc = kOkDspRC;
  2509. cmDspApplyAllDefaults(ctx,inst);
  2510. return rc;
  2511. }
  2512. cmDspRC_t _cmDspSyncRecdExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2513. {
  2514. cmDspRC_t rc = kOkDspRC;
  2515. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2516. const cmSample_t* x[ p->chCnt ];
  2517. unsigned n = 0;
  2518. unsigned i;
  2519. //const cmTimeSpec_t* ts = &ctx->ctx->oTimeStamp;
  2520. //printf("SR: %ld %ld\n",ts->tv_sec,ts->tv_nsec);
  2521. p->ats = ctx->ctx->iTimeStamp;
  2522. for(i=0; i<p->chCnt; ++i)
  2523. {
  2524. if( i==0 )
  2525. n = cmDspAudioBufSmpCount(ctx,inst,kAinBaseSrId+i,0);
  2526. else
  2527. { assert( n == cmDspAudioBufSmpCount(ctx,inst,kAinBaseSrId+i,0)); }
  2528. x[i] = cmDspAudioBuf(ctx,inst,kAinBaseSrId+i,0);
  2529. }
  2530. if( n>0 && cmSyncRecdIsValid(p->srH ) )
  2531. if( cmSyncRecdAudioWrite( p->srH, &ctx->ctx->iTimeStamp, p->smpIdx, x, p->chCnt, n ) != kOkSyRC )
  2532. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd audio update failed.");
  2533. p->smpIdx += n;
  2534. return rc;
  2535. }
  2536. cmDspRC_t _cmDspSyncRecdRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2537. {
  2538. cmDspRC_t rc = kOkDspRC;
  2539. cmDspSyncRecd_t* p = (cmDspSyncRecd_t*)inst;
  2540. cmDspSetEvent(ctx,inst,evt);
  2541. switch( evt->dstVarId )
  2542. {
  2543. case kStatusSrId:
  2544. if(cmMidiIsChStatus( cmDspUInt(inst,kStatusSrId) ) )
  2545. {
  2546. cmTimeSpec_t ts;
  2547. ts.tv_sec = cmDspUInt(inst,kSecSrId);
  2548. ts.tv_nsec = cmDspUInt(inst,kNSecSrId);
  2549. //printf("%i %i\n",cmDspUInt(inst,kD1SrId),cmTimeElapsedMicros(&ts,&p->ats));
  2550. if( cmSyncRecdIsValid(p->srH ) )
  2551. if( cmSyncRecdMidiWrite(p->srH, &ts, cmDspUInt(inst,kStatusSrId), cmDspUInt(inst,kD0SrId), cmDspUInt(inst,kD1SrId) ) != kOkSyRC )
  2552. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Sync-recd MIDI update failed.");
  2553. }
  2554. break;
  2555. case kCmdSrId:
  2556. {
  2557. unsigned cmdId = cmDspSymbol(inst,kCmdSrId);
  2558. if( cmdId == p->openSymId )
  2559. rc = _cmDspSyncRecdCreateFile(ctx,inst);
  2560. else
  2561. if( cmdId == p->closeSymId && cmSyncRecdIsValid(p->srH))
  2562. {
  2563. cmSyncRecdFinal(&p->srH);
  2564. //cmSyncRecdTest(ctx->cmCtx);
  2565. }
  2566. }
  2567. break;
  2568. }
  2569. return rc;
  2570. }
  2571. struct cmDspClass_str* cmSyncRecdClassCons( cmDspCtx_t* ctx )
  2572. {
  2573. cmDspClassSetup(&_cmSyncRecdDC,ctx,"SyncRecd",
  2574. NULL,
  2575. _cmDspSyncRecdAlloc,
  2576. _cmDspSyncRecdFree,
  2577. _cmDspSyncRecdReset,
  2578. _cmDspSyncRecdExec,
  2579. _cmDspSyncRecdRecv,
  2580. NULL,
  2581. NULL,
  2582. "Synchronized Audio and MIDI recorder.");
  2583. return &_cmSyncRecdDC;
  2584. }
  2585. //==========================================================================================================================================
  2586. enum
  2587. {
  2588. kFnTsbId,
  2589. kBldrTsbId,
  2590. kSelTsbId,
  2591. kRefreshTsbId,
  2592. kSendTsbId
  2593. };
  2594. cmDspClass_t _cmTakeSeqBldrDC;
  2595. typedef struct
  2596. {
  2597. cmDspInst_t inst;
  2598. cmTakeSeqBldrH_t h;
  2599. bool errFl;
  2600. } cmDspTakeSeqBldr_t;
  2601. cmDspInst_t* _cmDspTakeSeqBldrAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2602. {
  2603. cmDspTakeSeqBldr_t* p = cmDspInstAllocV(cmDspTakeSeqBldr_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2604. 1, "fn", kFnTsbId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score Tracking file.",
  2605. 1, "bldr", kBldrTsbId, 0, 0, kOutDsvFl | kPtrDsvFl, "Bldr Ref",
  2606. 1, "sel", kSelTsbId, 0, 0, kOutDsvFl | kUIntDsvFl, "Selected score element location index.",
  2607. 1, "refresh",kRefreshTsbId, 0, 0, kOutDsvFl | kUIntDsvFl, "Refresh",
  2608. 1, "send", kSendTsbId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element location.",
  2609. 0 );
  2610. p->errFl = false;
  2611. cmDspSetDefaultInt( ctx, &p->inst, kRefreshTsbId, 0, 0);
  2612. if( cmTakeSeqBldrAlloc(ctx->cmCtx, &p->h ) != kOkTsbRC )
  2613. cmErrMsg(&p->inst.classPtr->err, kSubSysFailDspRC, "Allocate TaskSeqBldr object.");
  2614. else
  2615. {
  2616. cmDspUiTakeSeqBldrCreate(ctx,&p->inst,kFnTsbId,kBldrTsbId,kSelTsbId,kRefreshTsbId);
  2617. }
  2618. return &p->inst;
  2619. }
  2620. cmDspRC_t _cmDspTakeSeqBldrSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2621. {
  2622. cmDspRC_t rc = kOkDspRC;
  2623. cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  2624. if( cmTakeSeqBldrInitialize(p->h, cmDspStrcz(inst,kFnTsbId) ) != kOkTsbRC )
  2625. {
  2626. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal TakeSeqBldr object with %s.",cmStringNullGuard(cmDspStrcz(inst,kFnTsbId)));
  2627. }
  2628. else
  2629. {
  2630. cmDspSetPtr(ctx,inst,kBldrTsbId,p->h.h);
  2631. p->errFl = false;
  2632. }
  2633. return rc;
  2634. }
  2635. cmDspRC_t _cmDspTakeSeqBldrFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2636. {
  2637. cmDspRC_t rc = kOkDspRC;
  2638. cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  2639. cmTakeSeqBldrFree(&p->h);
  2640. return rc;
  2641. }
  2642. cmDspRC_t _cmDspTakeSeqBldrReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2643. {
  2644. //cmDspTakeSeqBldr_t* p = (cmDspTakeSeqBldr_t*)inst;
  2645. cmDspApplyAllDefaults(ctx,inst);
  2646. return _cmDspTakeSeqBldrSetup(ctx, inst );
  2647. }
  2648. cmDspRC_t _cmDspTakeSeqBldrExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2649. {
  2650. cmDspRC_t rc = kOkDspRC;
  2651. return rc;
  2652. }
  2653. cmDspRC_t _cmDspTakeSeqBldrRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2654. {
  2655. // not matter what arrives at the 'send' input ....
  2656. if( evt->dstVarId == kSendTsbId )
  2657. {
  2658. // send the last score loc
  2659. unsigned selIdx;
  2660. if((selIdx = cmDspUInt(inst,kSelTsbId)) != cmInvalidIdx )
  2661. cmDspSetUInt(ctx,inst,kSelTsbId, selIdx );
  2662. return kOkDspRC;
  2663. }
  2664. cmDspSetEvent(ctx,inst,evt);
  2665. switch(evt->dstVarId)
  2666. {
  2667. case kFnTsbId:
  2668. _cmDspMidiFilePlayOpen(ctx, inst );
  2669. break;
  2670. }
  2671. return kOkDspRC;
  2672. }
  2673. struct cmDspClass_str* cmTakeSeqBldrClassCons( cmDspCtx_t* ctx )
  2674. {
  2675. cmDspClassSetup(&_cmTakeSeqBldrDC,ctx,"TakeSeqBldr",
  2676. NULL,
  2677. _cmDspTakeSeqBldrAlloc,
  2678. _cmDspTakeSeqBldrFree,
  2679. _cmDspTakeSeqBldrReset,
  2680. _cmDspTakeSeqBldrExec,
  2681. _cmDspTakeSeqBldrRecv,
  2682. NULL,
  2683. NULL,
  2684. "TakeSeqBldr");
  2685. return &_cmTakeSeqBldrDC;
  2686. }
  2687. //==========================================================================================================================================
  2688. enum
  2689. {
  2690. kBldrTsrId,
  2691. kRefreshTsrId,
  2692. kCmdTsrId,
  2693. kSelTsrId,
  2694. kStatusTsrId,
  2695. kD0TsrId,
  2696. kD1TsrId,
  2697. kSmpIdxTsrId
  2698. };
  2699. cmDspClass_t _cmTakeSeqRendDC;
  2700. typedef struct
  2701. {
  2702. cmDspInst_t inst;
  2703. cmTakeSeqBldrH_t h;
  2704. unsigned startSymId;
  2705. unsigned stopSymId;
  2706. unsigned contSymId;
  2707. unsigned onSymId;
  2708. unsigned offSymId;
  2709. bool errFl;
  2710. } cmDspTakeSeqRend_t;
  2711. cmDspInst_t* _cmDspTakeSeqRendAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2712. {
  2713. cmDspTakeSeqRend_t* p = cmDspInstAllocV(cmDspTakeSeqRend_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2714. 1, "bldr", kBldrTsrId, 0, 0, kInDsvFl | kPtrDsvFl | kOptArgDsvFl, "Take Sequene Builder Ref",
  2715. 1, "refresh",kRefreshTsrId, 0, 0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Refresh",
  2716. 1, "cmd", kCmdTsrId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" ,
  2717. 1, "sel", kSelTsrId, 0, 0, kInDsvFl | kUIntDsvFl, "Selected score element location index input.",
  2718. 1, "status", kStatusTsrId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output",
  2719. 1, "d0", kD0TsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" ,
  2720. 1, "d1", kD1TsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1",
  2721. 1, "smpidx", kSmpIdxTsrId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index.",
  2722. 0 );
  2723. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  2724. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  2725. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  2726. p->onSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"on");
  2727. p->offSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"off");
  2728. p->errFl = false;
  2729. cmDspSetDefaultInt( ctx, &p->inst, kRefreshTsrId, 0, 0);
  2730. cmDspSetDefaultSymbol(ctx, &p->inst, kCmdTsrId, p->stopSymId);
  2731. cmDspSetDefaultInt( ctx, &p->inst, kSmpIdxTsrId, 0, 0);
  2732. cmDspSetDefaultUInt( ctx, &p->inst, kStatusTsrId, 0, 0);
  2733. cmDspSetDefaultUInt( ctx, &p->inst, kD0TsrId, 0, 0);
  2734. cmDspSetDefaultUInt( ctx, &p->inst, kD1TsrId, 0, 0);
  2735. cmDspUiTakeSeqRendCreate(ctx,&p->inst,kBldrTsrId,kRefreshTsrId,kSelTsrId);
  2736. return &p->inst;
  2737. }
  2738. cmDspRC_t _cmDspTakeSeqRendFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2739. {
  2740. cmDspRC_t rc = kOkDspRC;
  2741. return rc;
  2742. }
  2743. cmDspRC_t _cmDspTakeSeqRendReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2744. {
  2745. //cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  2746. cmDspApplyAllDefaults(ctx,inst);
  2747. return kOkDspRC;
  2748. }
  2749. typedef struct
  2750. {
  2751. cmDspCtx_t* ctx;
  2752. cmDspInst_t* inst;
  2753. } _cmDspTakeSeqRendCbArg_t;
  2754. // Called from cmDspTakeSeqRendExec() -> cmTakeSeqRendPlayExec() to
  2755. // deliver MIDI messages which need to be transmitted.
  2756. void _cmDspTakeSeqRendMidiCb( void* arg, const cmTksbEvent_t* e )
  2757. {
  2758. _cmDspTakeSeqRendCbArg_t* a = (_cmDspTakeSeqRendCbArg_t*)arg;
  2759. switch( e->status )
  2760. {
  2761. case kNoteOffMdId:
  2762. case kNoteOnMdId:
  2763. case kCtlMdId:
  2764. //if( !cmMidiIsPedal(e->status,e->d0))
  2765. {
  2766. cmDspSetUInt(a->ctx,a->inst, kSmpIdxTsrId, e->smpIdx);
  2767. cmDspSetUInt(a->ctx,a->inst, kD1TsrId, e->d1);
  2768. cmDspSetUInt(a->ctx,a->inst, kD0TsrId, e->d0);
  2769. cmDspSetUInt(a->ctx,a->inst, kStatusTsrId, e->status);
  2770. }
  2771. break;
  2772. }
  2773. }
  2774. void _cmDspTakeSeqRendPedalsUp( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2775. {
  2776. _cmDspTakeSeqRendCbArg_t a;
  2777. a.ctx = ctx;
  2778. a.inst = inst;
  2779. cmTksbEvent_t e[] =
  2780. {
  2781. { 0, kCtlMdId, kSustainCtlMdId, 0 },
  2782. { 0, kCtlMdId, kPortamentoCtlMdId,0 },
  2783. { 0, kCtlMdId, kSostenutoCtlMdId, 0 },
  2784. { 0, kCtlMdId, kSoftPedalCtlMdId, 0 },
  2785. { 0, kCtlMdId, kLegatoCtlMdId, 0 }
  2786. };
  2787. unsigned n = sizeof(e)/sizeof(e[0]);
  2788. unsigned i;
  2789. for(i=0; i<n; ++i)
  2790. _cmDspTakeSeqRendMidiCb(&a,e+i);
  2791. }
  2792. cmDspRC_t _cmDspTakeSeqRendExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2793. {
  2794. cmDspRC_t rc = kOkDspRC;
  2795. cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  2796. if( cmDspSymbol(inst,kCmdTsrId) != p->stopSymId )
  2797. {
  2798. if( cmTakeSeqBldrIsValid(p->h) == false )
  2799. {
  2800. if( p->errFl==false )
  2801. {
  2802. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The Take Sequence Builder not been given a valid file.");
  2803. p->errFl = true;
  2804. }
  2805. return rc;
  2806. }
  2807. unsigned sPc = cmDspSamplesPerCycle(ctx);
  2808. _cmDspTakeSeqRendCbArg_t arg;
  2809. arg.inst = inst;
  2810. arg.ctx = ctx;
  2811. // This call may result in multiple callbacks
  2812. // to _cmDspTakeSeqRendMidiCb() from within the function.
  2813. cmTakeSeqBldrPlayExec(p->h,sPc,_cmDspTakeSeqRendMidiCb, &arg );
  2814. }
  2815. return rc;
  2816. }
  2817. cmDspRC_t _cmDspTakeSeqRendRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2818. {
  2819. cmDspTakeSeqRend_t* p = (cmDspTakeSeqRend_t*)inst;
  2820. printf("Recv:%s\n",cmStringNullGuard(cmDspVarLabel(ctx, inst, evt->dstVarId)) );
  2821. cmDspSetEvent(ctx,inst,evt);
  2822. switch(evt->dstVarId)
  2823. {
  2824. case kBldrTsrId:
  2825. p->h.h = cmDspPtr(inst,kBldrTsrId);
  2826. break;
  2827. case kCmdTsrId:
  2828. {
  2829. unsigned symId = cmDspSymbol(inst,kCmdTsrId);
  2830. if( symId == p->onSymId )
  2831. cmDspSetSymbol( ctx, inst, kCmdTsrId, p->startSymId );
  2832. else
  2833. if( symId == p->offSymId )
  2834. cmDspSetSymbol( ctx, inst, kCmdTsrId, p->stopSymId );
  2835. if( cmTakeSeqBldrIsValid(p->h) && cmDspSymbol(inst,kCmdTsrId)==p->startSymId )
  2836. {
  2837. _cmDspTakeSeqRendPedalsUp( ctx, inst );
  2838. cmTakeSeqBldrPlaySeekLoc(p->h, cmInvalidId );
  2839. }
  2840. }
  2841. break;
  2842. case kSelTsrId:
  2843. {
  2844. // seek the playback position to the scLocIdx.
  2845. unsigned scLocIdx = cmDspUInt(inst,kSelTsrId);
  2846. if( cmTakeSeqBldrIsValid(p->h) && cmTakeSeqBldrPlaySeekLoc(p->h, scLocIdx ) != kOkTsbRC )
  2847. return cmDspInstErr(ctx,&p->inst,kSubSysFailDspRC,"Take Sequence Bldr Seek failed on score location index %i.", scLocIdx);
  2848. }
  2849. break;
  2850. }
  2851. return kOkDspRC;
  2852. }
  2853. struct cmDspClass_str* cmTakeSeqRendClassCons( cmDspCtx_t* ctx )
  2854. {
  2855. cmDspClassSetup(&_cmTakeSeqRendDC,ctx,"TakeSeqRend",
  2856. NULL,
  2857. _cmDspTakeSeqRendAlloc,
  2858. _cmDspTakeSeqRendFree,
  2859. _cmDspTakeSeqRendReset,
  2860. _cmDspTakeSeqRendExec,
  2861. _cmDspTakeSeqRendRecv,
  2862. NULL,
  2863. NULL,
  2864. "TakeSeqRend");
  2865. return &_cmTakeSeqRendDC;
  2866. }
  2867. //==========================================================================================================================================
  2868. enum
  2869. {
  2870. kLfsrN_RcId,
  2871. kMlsCoeff0RcId,
  2872. kMlsCoeff1RcId,
  2873. kSmpPerChipRcId,
  2874. kRcosBetaRcId,
  2875. kRcosOS_RcId,
  2876. kCarrierHzRcId,
  2877. kAtkDcyMsRcId,
  2878. kPhatAlphaRcId,
  2879. kPhatMultRcId,
  2880. kInRcId,
  2881. kOutRcId,
  2882. };
  2883. cmDspClass_t _cmReflectCalcDC;
  2884. typedef struct
  2885. {
  2886. cmDspInst_t inst;
  2887. cmReflectCalc_t* r;
  2888. } cmDspReflectCalc_t;
  2889. cmDspInst_t* _cmDspReflectCalcAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  2890. {
  2891. /*
  2892. if( va_cnt !=3 )
  2893. {
  2894. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ReflectCalc' constructor must have two arguments: a channel count and frequency array.");
  2895. return NULL;
  2896. }
  2897. */
  2898. cmDspReflectCalc_t* p = cmDspInstAllocV(cmDspReflectCalc_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  2899. 1, "lfsrN", kLfsrN_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Gold code generator LFSR length",
  2900. 1, "mlsc0", kMlsCoeff0RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0",
  2901. 1, "mlsc1", kMlsCoeff1RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0",
  2902. 1, "spchip",kSmpPerChipRcId,0,0, kInDsvFl | kUIntDsvFl, "Samples per spreading code bit.",
  2903. 1, "rcosb", kRcosBetaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Raised cosine beta",
  2904. 1, "rcosos",kRcosOS_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Raised cosine oversample factor.",
  2905. 1, "carhz", kCarrierHzRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Carrier frequency in Hertz.",
  2906. 1, "envms", kAtkDcyMsRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Signal Attack/Decay milliseconds.",
  2907. 1, "alpha", kPhatAlphaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "PHAT alpha coefficient.",
  2908. 1, "mult", kPhatMultRcId, 0,0, kInDsvFl | kUIntDsvFl, "PHAT multiplier coefficient.",
  2909. 1, "in", kInRcId, 0,1, kInDsvFl | kAudioBufDsvFl,"Audio input",
  2910. 1, "out", kOutRcId, 0,1, kOutDsvFl | kAudioBufDsvFl,"Audio output",
  2911. 0 );
  2912. p->r = cmReflectCalcAlloc(ctx->cmProcCtx, NULL, NULL, 0, 0 );
  2913. cmDspSetDefaultUInt( ctx, &p->inst, kLfsrN_RcId, 0, 8);
  2914. cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff0RcId, 0, 0x8e);
  2915. cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff1RcId, 0, 0x96);
  2916. cmDspSetDefaultUInt( ctx, &p->inst, kSmpPerChipRcId, 0, 64);
  2917. cmDspSetDefaultDouble( ctx, &p->inst, kRcosBetaRcId, 0, 0.5);
  2918. cmDspSetDefaultUInt( ctx, &p->inst, kRcosOS_RcId, 0, 4);
  2919. cmDspSetDefaultDouble( ctx, &p->inst, kCarrierHzRcId, 0, 2500.0);
  2920. cmDspSetDefaultDouble( ctx, &p->inst, kAtkDcyMsRcId, 0, 50.0);
  2921. cmDspSetDefaultDouble( ctx, &p->inst, kPhatAlphaRcId, 0, 0.5);
  2922. cmDspSetDefaultUInt( ctx, &p->inst, kPhatMultRcId, 0, 1);
  2923. return &p->inst;
  2924. }
  2925. cmDspRC_t _cmDspReflectCalcSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  2926. {
  2927. cmDspRC_t rc = kOkDspRC;
  2928. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  2929. cmGoldSigArg_t gsa;
  2930. gsa.chN = 1;
  2931. gsa.srate = cmDspSampleRate(ctx);
  2932. gsa.lfsrN = cmDspUInt(inst,kLfsrN_RcId);
  2933. gsa.mlsCoeff0 = cmDspUInt(inst,kMlsCoeff0RcId);
  2934. gsa.mlsCoeff1 = cmDspUInt(inst,kMlsCoeff1RcId);
  2935. gsa.samplesPerChip = cmDspUInt(inst,kSmpPerChipRcId);
  2936. gsa.rcosBeta = cmDspDouble(inst,kRcosBetaRcId);
  2937. gsa.rcosOSFact = cmDspUInt(inst,kRcosOS_RcId);
  2938. gsa.carrierHz = cmDspDouble(inst,kCarrierHzRcId);
  2939. gsa.envMs = cmDspDouble(inst,kAtkDcyMsRcId);
  2940. double phatAlpha = cmDspDouble(inst,kPhatAlphaRcId);
  2941. unsigned phatMult = cmDspUInt(inst,kPhatMultRcId);
  2942. if( cmReflectCalcInit(p->r,&gsa,phatAlpha,phatMult) != cmOkRC )
  2943. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal ReflectCalc detector.");
  2944. return rc;
  2945. }
  2946. cmDspRC_t _cmDspReflectCalcFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2947. {
  2948. cmDspRC_t rc = kOkDspRC;
  2949. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  2950. cmReflectCalcFree(&p->r);
  2951. return rc;
  2952. }
  2953. cmDspRC_t _cmDspReflectCalcReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2954. {
  2955. //cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  2956. cmDspApplyAllDefaults(ctx,inst);
  2957. return _cmDspReflectCalcSetup(ctx, inst );
  2958. }
  2959. cmDspRC_t _cmDspReflectCalcExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2960. {
  2961. cmDspRC_t rc = kOkDspRC;
  2962. cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  2963. const cmSample_t* xV = cmDspAudioBuf(ctx,inst,kInRcId,0);
  2964. unsigned xN = cmDspAudioBufSmpCount(ctx,inst,kInRcId,0);
  2965. cmSample_t* yV = cmDspAudioBuf(ctx,inst,kOutRcId,0);
  2966. unsigned yN = cmDspAudioBufSmpCount(ctx,inst,kOutRcId,0);
  2967. if( xV != NULL && yV != NULL )
  2968. {
  2969. assert( xN == yN );
  2970. cmReflectCalcExec(p->r,xV,yV,xN);
  2971. }
  2972. return rc;
  2973. }
  2974. cmDspRC_t _cmDspReflectCalcRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  2975. {
  2976. //cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst;
  2977. cmDspSetEvent(ctx,inst,evt);
  2978. return kOkDspRC;
  2979. }
  2980. struct cmDspClass_str* cmReflectCalcClassCons( cmDspCtx_t* ctx )
  2981. {
  2982. cmDspClassSetup(&_cmReflectCalcDC,ctx,"ReflectCalc",
  2983. NULL,
  2984. _cmDspReflectCalcAlloc,
  2985. _cmDspReflectCalcFree,
  2986. _cmDspReflectCalcReset,
  2987. _cmDspReflectCalcExec,
  2988. _cmDspReflectCalcRecv,
  2989. NULL,
  2990. NULL,
  2991. "Reflecttion time calculator");
  2992. return &_cmReflectCalcDC;
  2993. }
  2994. //==========================================================================================================================================
  2995. enum
  2996. {
  2997. kMuEcId,
  2998. kImpRespN_EcId,
  2999. kDelayN_EcId,
  3000. kUnfiltInEcId,
  3001. kFiltInEcId,
  3002. kOutEcId
  3003. };
  3004. cmDspClass_t _cmEchoCancelDC;
  3005. typedef struct
  3006. {
  3007. cmDspInst_t inst;
  3008. cmNlmsEc_t* r;
  3009. } cmDspEchoCancel_t;
  3010. cmDspInst_t* _cmDspEchoCancelAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  3011. {
  3012. /*
  3013. if( va_cnt !=3 )
  3014. {
  3015. cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'EchoCancel' constructor must have two arguments: a channel count and frequency array.");
  3016. return NULL;
  3017. }
  3018. */
  3019. cmDspEchoCancel_t* p = cmDspInstAllocV(cmDspEchoCancel_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl,
  3020. 1, "mu", kMuEcId, 0,0, kInDsvFl | kDoubleDsvFl, "NLSM mu coefficient.",
  3021. 1, "irN", kImpRespN_EcId, 0,0, kInDsvFl | kUIntDsvFl, "Filter impulse response length in samples.",
  3022. 1, "delayN", kDelayN_EcId, 0,0, kInDsvFl | kUIntDsvFl, "Fixed feedback delay in samples.",
  3023. 1, "uf_in", kUnfiltInEcId, 0,1, kInDsvFl | kAudioBufDsvFl,"Unfiltered audio input",
  3024. 1, "f_in", kFiltInEcId, 0,1, kInDsvFl | kAudioBufDsvFl,"Filtered audio input",
  3025. 1, "out", kOutEcId, 0,1, kOutDsvFl | kAudioBufDsvFl,"Audio output",
  3026. 0 );
  3027. p->r = cmNlmsEcAlloc(ctx->cmProcCtx, NULL, 0,0,0 );
  3028. cmDspSetDefaultDouble( ctx, &p->inst, kMuEcId, 0, 0.1);
  3029. cmDspSetDefaultUInt( ctx, &p->inst, kImpRespN_EcId, 0, 256);
  3030. cmDspSetDefaultUInt( ctx, &p->inst, kDelayN_EcId, 0, 2048);
  3031. return &p->inst;
  3032. }
  3033. cmDspRC_t _cmDspEchoCancelSetup( cmDspCtx_t* ctx, cmDspInst_t* inst )
  3034. {
  3035. cmDspRC_t rc = kOkDspRC;
  3036. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3037. double mu = cmDspDouble(inst,kMuEcId);
  3038. unsigned hN = cmDspUInt(inst,kImpRespN_EcId);
  3039. unsigned delayN = cmDspUInt(inst,kDelayN_EcId);
  3040. if( cmNlmsEcInit(p->r,mu,hN,delayN) != cmOkRC )
  3041. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal echo canceller.");
  3042. return rc;
  3043. }
  3044. cmDspRC_t _cmDspEchoCancelFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3045. {
  3046. cmDspRC_t rc = kOkDspRC;
  3047. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3048. cmNlmsEcFree(&p->r);
  3049. return rc;
  3050. }
  3051. cmDspRC_t _cmDspEchoCancelReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3052. {
  3053. //cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3054. cmDspApplyAllDefaults(ctx,inst);
  3055. return _cmDspEchoCancelSetup(ctx, inst );
  3056. }
  3057. cmDspRC_t _cmDspEchoCancelExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3058. {
  3059. cmDspRC_t rc = kOkDspRC;
  3060. cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3061. const cmSample_t* fV = cmDspAudioBuf(ctx,inst,kFiltInEcId,0);
  3062. unsigned fN = cmDspAudioBufSmpCount(ctx,inst,kFiltInEcId,0);
  3063. const cmSample_t* uV = cmDspAudioBuf(ctx,inst,kUnfiltInEcId,0);
  3064. unsigned uN = cmDspAudioBufSmpCount(ctx,inst,kUnfiltInEcId,0);
  3065. cmSample_t* yV = cmDspAudioBuf(ctx,inst,kOutEcId,0);
  3066. unsigned yN = cmDspAudioBufSmpCount(ctx,inst,kOutEcId,0);
  3067. if( fV !=NULL && uV != NULL && yV != NULL )
  3068. {
  3069. assert( uN == yN && fN == yN );
  3070. cmNlmsEcExec(p->r,uV,fV,yV,yN);
  3071. }
  3072. return rc;
  3073. }
  3074. cmDspRC_t _cmDspEchoCancelRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  3075. {
  3076. //cmDspEchoCancel_t* p = (cmDspEchoCancel_t*)inst;
  3077. cmDspSetEvent(ctx,inst,evt);
  3078. return kOkDspRC;
  3079. }
  3080. struct cmDspClass_str* cmEchoCancelClassCons( cmDspCtx_t* ctx )
  3081. {
  3082. cmDspClassSetup(&_cmEchoCancelDC,ctx,"EchoCancel",
  3083. NULL,
  3084. _cmDspEchoCancelAlloc,
  3085. _cmDspEchoCancelFree,
  3086. _cmDspEchoCancelReset,
  3087. _cmDspEchoCancelExec,
  3088. _cmDspEchoCancelRecv,
  3089. NULL,
  3090. NULL,
  3091. "Echo canceller");
  3092. return &_cmEchoCancelDC;
  3093. }