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

cmDspKr.c 97KB

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