libcm is a C development framework with an emphasis on audio signal processing applications.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cmDspKr.c 127KB

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