libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmDspKr.c 96KB

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