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 119KB

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