libcm is a C development framework with an emphasis on audio signal processing applications.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cmDspKr.c 119KB

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