libcm is a C development framework with an emphasis on audio signal processing applications.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cmDspKr.c 82KB

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