libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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. kAudPathTlId,
  226. kSelTlId,
  227. kCursTlId,
  228. kResetTlId,
  229. kAudFnTlId,
  230. kMidiFnTlId,
  231. kBegAudSmpIdxTlId,
  232. kEndAudSmpIdxTlId,
  233. kBegMidiSmpIdxTlId,
  234. kEndMidiSmpIdxTlId
  235. };
  236. cmDspClass_t _cmTimeLineDC;
  237. typedef struct
  238. {
  239. cmDspInst_t inst;
  240. cmTlH_t tlH;
  241. unsigned afIdx;
  242. } cmDspTimeLine_t;
  243. cmDspInst_t* _cmDspTimeLineAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  244. {
  245. cmDspVarArg_t args[] =
  246. {
  247. { "tlfile", kTlFileTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Time line file." },
  248. { "path", kAudPathTlId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Audio path" },
  249. { "sel", kSelTlId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected marker id."},
  250. { "curs", kCursTlId, 0, 0, kInDsvFl | kUIntDsvFl, "Current audio file index."},
  251. { "reset", kResetTlId, 0, 0, kInDsvFl | kSymDsvFl, "Resend all outputs." },
  252. { "afn", kAudFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected Audio file." },
  253. { "mfn", kMidiFnTlId, 0, 0, kOutDsvFl | kStrzDsvFl, "Selected MIDI file." },
  254. { "absi", kBegAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin audio sample index."},
  255. { "aesi", kEndAudSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End audio sample index."},
  256. { "mbsi", kBegMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "Begin MIDI sample index."},
  257. { "mesi", kEndMidiSmpIdxTlId, 0, 0, kOutDsvFl | kIntDsvFl, "End MIDI sample index."},
  258. { NULL, 0, 0, 0, 0 }
  259. };
  260. cmDspTimeLine_t* p = cmDspInstAlloc(cmDspTimeLine_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  261. cmDspSetDefaultUInt( ctx, &p->inst, kSelTlId, 0, cmInvalidId);
  262. cmDspSetDefaultUInt( ctx, &p->inst, kCursTlId, 0, 0);
  263. cmDspSetDefaultStrcz(ctx, &p->inst, kAudFnTlId, NULL, "");
  264. cmDspSetDefaultStrcz(ctx, &p->inst, kMidiFnTlId, NULL, "");
  265. cmDspSetDefaultInt( ctx, &p->inst, kBegAudSmpIdxTlId, 0, cmInvalidIdx);
  266. cmDspSetDefaultInt( ctx, &p->inst, kEndAudSmpIdxTlId, 0, cmInvalidIdx);
  267. cmDspSetDefaultInt( ctx, &p->inst, kBegMidiSmpIdxTlId, 0, cmInvalidIdx);
  268. cmDspSetDefaultInt( ctx, &p->inst, kEndMidiSmpIdxTlId, 0, cmInvalidIdx);
  269. // create the UI control
  270. cmDspUiTimeLineCreate(ctx,&p->inst,kTlFileTlId,kAudPathTlId,kSelTlId,kCursTlId);
  271. p->tlH = cmTimeLineNullHandle;
  272. return &p->inst;
  273. }
  274. cmDspRC_t _cmDspTimeLineFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  275. {
  276. cmDspRC_t rc = kOkDspRC;
  277. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  278. if( cmTimeLineFinalize(&p->tlH) != kOkTlRC )
  279. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Time-line finalize failed.");
  280. return rc;
  281. }
  282. cmDspRC_t _cmDspTimeLineReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  283. {
  284. cmDspRC_t rc = kOkDspRC;
  285. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  286. cmDspApplyAllDefaults(ctx,inst);
  287. const cmChar_t* tlFn;
  288. if((tlFn = cmDspStrcz(inst, kTlFileTlId )) != NULL )
  289. if( cmTimeLineInitializeFromFile(ctx->cmCtx, &p->tlH, NULL, NULL, tlFn ) != kOkTlRC )
  290. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Time-line file open failed.");
  291. return rc;
  292. }
  293. cmDspRC_t _cmDspTimeLineRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  294. {
  295. cmDspTimeLine_t* p = (cmDspTimeLine_t*)inst;
  296. switch( evt->dstVarId )
  297. {
  298. case kAudPathTlId:
  299. cmDspSetEvent(ctx,inst,evt);
  300. break;
  301. case kCursTlId:
  302. cmDspSetEvent(ctx,inst,evt);
  303. break;
  304. case kResetTlId:
  305. case kSelTlId:
  306. {
  307. unsigned markerId;
  308. cmDspSetEvent(ctx,inst,evt);
  309. // get the id of the selected marker
  310. if((markerId = cmDspUInt(inst,kSelTlId)) != cmInvalidId )
  311. {
  312. // get the marker object
  313. cmTlObj_t* op;
  314. if((op = cmTimeLineIdToObj(p->tlH, cmInvalidId, markerId )) != NULL )
  315. {
  316. assert(op->typeId == kMarkerTlId);
  317. p->afIdx = op->begSmpIdx;
  318. cmDspSetInt(ctx, inst, kBegAudSmpIdxTlId, op->begSmpIdx );
  319. cmDspSetInt(ctx, inst, kEndAudSmpIdxTlId, op->begSmpIdx + op->durSmpCnt );
  320. // locate the audio file assoc'd with the marker
  321. cmTlAudioFile_t* afp;
  322. if((afp = cmTimeLineAudioFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL)
  323. cmDspSetStrcz(ctx, inst, kAudFnTlId, afp->fn );
  324. // locate the midi file assoc'd with the marker
  325. cmTlMidiFile_t* mfp;
  326. if((mfp = cmTimeLineMidiFileAtTime(p->tlH,op->seqId,op->seqSmpIdx)) != NULL )
  327. {
  328. cmDspSetInt(ctx, inst, kBegMidiSmpIdxTlId, op->seqSmpIdx - mfp->obj.seqSmpIdx );
  329. cmDspSetInt(ctx, inst, kEndMidiSmpIdxTlId, op->seqSmpIdx + op->durSmpCnt - mfp->obj.seqSmpIdx );
  330. cmDspSetStrcz(ctx, inst, kMidiFnTlId, mfp->fn );
  331. }
  332. }
  333. }
  334. }
  335. break;
  336. default:
  337. {assert(0);}
  338. }
  339. return kOkDspRC;
  340. }
  341. struct cmDspClass_str* cmTimeLineClassCons( cmDspCtx_t* ctx )
  342. {
  343. cmDspClassSetup(&_cmTimeLineDC,ctx,"TimeLine",
  344. NULL,
  345. _cmDspTimeLineAlloc,
  346. _cmDspTimeLineFree,
  347. _cmDspTimeLineReset,
  348. NULL,
  349. _cmDspTimeLineRecv,
  350. NULL,NULL,
  351. "Time Line control.");
  352. return &_cmTimeLineDC;
  353. }
  354. //==========================================================================================================================================
  355. // Score UI Object
  356. enum
  357. {
  358. kFnScId,
  359. kSelScId,
  360. kSendScId,
  361. kStatusScId,
  362. kD0ScId,
  363. kD1ScId,
  364. kSmpIdxScId,
  365. kLocIdxScId,
  366. kEvtIdxScId,
  367. kDynScId,
  368. kValTypeScId,
  369. kValueScId
  370. };
  371. cmDspClass_t _cmScoreDC;
  372. typedef struct
  373. {
  374. cmDspInst_t inst;
  375. cmScH_t scH;
  376. cmDspCtx_t* ctx; // temporary ctx ptr used during cmScore callback in _cmDspScoreRecv()
  377. } cmDspScore_t;
  378. cmDspInst_t* _cmDspScoreAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  379. {
  380. cmDspVarArg_t args[] =
  381. {
  382. { "fn", kFnScId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  383. { "sel", kSelScId, 0, 0, kInDsvFl | kOutDsvFl | kUIntDsvFl, "Selected score element index input."},
  384. { "send", kSendScId, 0, 0, kInDsvFl | kTypeDsvMask, "Resend last selected score element."},
  385. { "status", kStatusScId, 0, 0, kInDsvFl | kIntDsvFl, "Performed MIDI status value output" },
  386. { "d0", kD0ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 0" },
  387. { "d1", kD1ScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDI msg data byte 1" },
  388. { "smpidx", kSmpIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performed MIDi msg time tag as a sample index." },
  389. { "loc", kLocIdxScId, 0, 0, kInDsvFl | kUIntDsvFl, "Performance score location."},
  390. { "evtidx", kEvtIdxScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Performed event index of following dynamcis level."},
  391. { "dyn", kDynScId, 0, 0, kOutDsvFl | kUIntDsvFl, "Dynamic level of previous event index."},
  392. { "type", kValTypeScId,0, 0, kOutDsvFl | kUIntDsvFl, "Output variable type."},
  393. { "value", kValueScId, 0, 0, kOutDsvFl | kDoubleDsvFl, "Output variable value."},
  394. { NULL, 0, 0, 0, 0 }
  395. };
  396. cmDspScore_t* p = cmDspInstAlloc(cmDspScore_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  397. cmDspSetDefaultUInt( ctx, &p->inst, kSelScId, 0, cmInvalidId);
  398. // create the UI control
  399. cmDspUiScoreCreate(ctx,&p->inst,kFnScId,kSelScId,kSmpIdxScId,kD0ScId,kD1ScId,kLocIdxScId,kEvtIdxScId,kDynScId,kValTypeScId,kValueScId);
  400. p->scH = cmScNullHandle;
  401. return &p->inst;
  402. }
  403. cmDspRC_t _cmDspScoreFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  404. {
  405. cmDspRC_t rc = kOkDspRC;
  406. cmDspScore_t* p = (cmDspScore_t*)inst;
  407. if( cmScoreFinalize(&p->scH) != kOkTlRC )
  408. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Score finalize failed.");
  409. return rc;
  410. }
  411. // Callback from cmScore triggered from _cmDspScoreRecv() during call to cmScoreSetPerfEvent().
  412. void _cmDspScoreCb( void* arg, const void* data, unsigned byteCnt )
  413. {
  414. cmDspInst_t* inst = (cmDspInst_t*)arg;
  415. cmDspScore_t* p = (cmDspScore_t*)inst;
  416. cmScMsg_t m;
  417. if( cmScoreDecode(data,byteCnt,&m) == kOkScRC )
  418. {
  419. switch( m.typeId )
  420. {
  421. case kDynMsgScId:
  422. cmDspSetUInt( p->ctx,inst, kEvtIdxScId, m.u.dyn.evtIdx );
  423. cmDspSetUInt( p->ctx,inst, kDynScId, m.u.dyn.dynLvl );
  424. break;
  425. case kVarMsgScId:
  426. cmDspSetUInt( p->ctx,inst, kValTypeScId, m.u.meas.varId);
  427. cmDspSetDouble(p->ctx,inst, kValueScId, m.u.meas.value);
  428. break;
  429. default:
  430. { assert(0); }
  431. }
  432. }
  433. }
  434. cmDspRC_t _cmDspScoreReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  435. {
  436. cmDspRC_t rc = kOkDspRC;
  437. cmDspScore_t* p = (cmDspScore_t*)inst;
  438. const cmChar_t* tlFn = NULL;
  439. unsigned* dynRefArray = NULL;
  440. unsigned dynRefCnt = 0;
  441. cmDspApplyAllDefaults(ctx,inst);
  442. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  443. {
  444. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  445. goto errLabel;
  446. }
  447. if((tlFn = cmDspStrcz(inst, kFnScId )) != NULL )
  448. if( cmScoreInitialize(ctx->cmCtx, &p->scH, tlFn, cmDspSampleRate(ctx), dynRefArray, dynRefCnt, _cmDspScoreCb, p, cmSymTblNullHandle ) != kOkTlRC )
  449. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "Score file open failed.");
  450. errLabel:
  451. return rc;
  452. }
  453. cmDspRC_t _cmDspScoreRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  454. {
  455. cmDspScore_t* p = (cmDspScore_t*)inst;
  456. if( evt->dstVarId == kSendScId )
  457. {
  458. unsigned selIdx;
  459. if((selIdx = cmDspUInt(inst,kSelScId)) != cmInvalidIdx )
  460. {
  461. cmDspSetUInt(ctx,inst,kSelScId, selIdx );
  462. cmScoreClearPerfInfo(p->scH);
  463. }
  464. return kOkDspRC;
  465. }
  466. cmDspSetEvent(ctx,inst,evt);
  467. switch( evt->dstVarId )
  468. {
  469. case kSelScId:
  470. cmScoreClearPerfInfo(p->scH);
  471. break;
  472. case kStatusScId:
  473. //printf("st:%x\n",cmDspUInt(inst,kStatusScId));
  474. break;
  475. case kLocIdxScId:
  476. {
  477. assert( cmDspUInt(inst,kStatusScId ) == kNoteOnMdId );
  478. p->ctx = ctx; // setup p->ctx for use in _cmDspScoreCb()
  479. // this call may result in callbacks to _cmDspScoreCb()
  480. cmScoreExecPerfEvent(p->scH, cmDspUInt(inst,kLocIdxScId), cmDspUInt(inst,kSmpIdxScId), cmDspUInt(inst,kD0ScId), cmDspUInt(inst,kD1ScId) );
  481. }
  482. break;
  483. }
  484. return kOkDspRC;
  485. }
  486. struct cmDspClass_str* cmScoreClassCons( cmDspCtx_t* ctx )
  487. {
  488. cmDspClassSetup(&_cmScoreDC,ctx,"Score",
  489. NULL,
  490. _cmDspScoreAlloc,
  491. _cmDspScoreFree,
  492. _cmDspScoreReset,
  493. NULL,
  494. _cmDspScoreRecv,
  495. NULL,NULL,
  496. "Score control.");
  497. return &_cmScoreDC;
  498. }
  499. //==========================================================================================================================================
  500. // MIDI File Player
  501. enum
  502. {
  503. kFnMfId,
  504. kSelMfId,
  505. kBsiMfId,
  506. kEsiMfId,
  507. kStatusMfId,
  508. kD0MfId,
  509. kD1MfId,
  510. kSmpIdxMfId
  511. };
  512. cmDspClass_t _cmMidiFilePlayDC;
  513. typedef struct
  514. {
  515. cmDspInst_t inst;
  516. cmMidiFileH_t mfH;
  517. unsigned curMsgIdx; // current midi file msg index
  518. int csi; // current sample index
  519. int bsi; // starting sample index
  520. int esi; // ending sample index
  521. unsigned startSymId;
  522. unsigned stopSymId;
  523. unsigned contSymId;
  524. bool errFl;
  525. } cmDspMidiFilePlay_t;
  526. /*
  527. 'bsi' and 'esi' give the starting and ending sample for MIDI file playback.
  528. These indexes are relative to the start of the file.
  529. When the player recieves a 'start' msg it sets the current sample index
  530. 'si' to 'bsi' and begins scanning for the next note to play.
  531. On each call to the _cmDspMidiFilePlayExec() msgs that fall in the interval
  532. si:si+sPc-1 will be transmitted. (where sPc are the number of samples per DSP cycle).
  533. */
  534. cmDspInst_t* _cmDspMidiFilePlayAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  535. {
  536. cmDspVarArg_t args[] =
  537. {
  538. { "fn", kFnMfId, 0, 0, kInDsvFl | kStrzDsvFl, "File name"},
  539. { "sel", kSelMfId, 0, 0, kInDsvFl | kSymDsvFl, "start | stop | continue" },
  540. { "bsi", kBsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Starting sample." },
  541. { "esi", kEsiMfId, 0, 0, kInDsvFl | kIntDsvFl, "Ending sample."},
  542. { "status", kStatusMfId, 0, 0, kOutDsvFl | kIntDsvFl, "Status value output" },
  543. { "d0", kD0MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 0" },
  544. { "d1", kD1MfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Data byte 1" },
  545. { "smpidx", kSmpIdxMfId, 0, 0, kOutDsvFl | kUIntDsvFl, "Msg time tag as a sample index." },
  546. { NULL, 0, 0, 0, 0 }
  547. };
  548. cmDspMidiFilePlay_t* p = cmDspInstAlloc(cmDspMidiFilePlay_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  549. p->startSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"start");
  550. p->stopSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"stop");
  551. p->contSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"continue");
  552. p->mfH = cmMidiFileNullHandle;
  553. cmDspSetDefaultStrcz( ctx, &p->inst, kFnMfId, NULL, "");
  554. cmDspSetDefaultSymbol(ctx, &p->inst, kSelMfId, p->stopSymId);
  555. cmDspSetDefaultInt( ctx, &p->inst, kBsiMfId, 0, 0);
  556. cmDspSetDefaultInt( ctx, &p->inst, kEsiMfId, 0, 0);
  557. cmDspSetDefaultUInt( ctx, &p->inst, kStatusMfId, 0, 0);
  558. cmDspSetDefaultUInt( ctx, &p->inst, kD0MfId, 0, 0);
  559. cmDspSetDefaultUInt( ctx, &p->inst, kD1MfId, 0, 0);
  560. return &p->inst;
  561. }
  562. cmDspRC_t _cmDspMidiFilePlayFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  563. {
  564. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  565. if( cmMidiFileClose(&p->mfH) )
  566. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "MIDI file close failed.");
  567. return kOkDspRC;
  568. }
  569. // return the index of the msg following smpIdx
  570. unsigned _cmDspMidiFilePlaySeekMsgIdx( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned smpIdx )
  571. {
  572. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  573. if( cmMidiFileIsValid(p->mfH) == false )
  574. {
  575. cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  576. return cmInvalidIdx;
  577. }
  578. unsigned i;
  579. unsigned n = cmMidiFileMsgCount(p->mfH);
  580. const cmMidiTrackMsg_t** a = cmMidiFileMsgArray(p->mfH);
  581. for(i=0; i<n; ++i)
  582. if( a[i]->dtick > smpIdx )
  583. break;
  584. return i==n ? cmInvalidIdx : i;
  585. }
  586. cmDspRC_t _cmDspMidiFilePlayOpen(cmDspCtx_t* ctx, cmDspInst_t* inst )
  587. {
  588. cmDspRC_t rc = kOkDspRC;
  589. const cmChar_t* fn = cmDspStrcz(inst,kFnMfId);
  590. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  591. p->errFl = false;
  592. if( fn==NULL || strlen(fn)==0 )
  593. return rc;
  594. if( cmMidiFileOpen( fn, &p->mfH, ctx->cmCtx ) != kOkFileRC )
  595. rc = cmErrMsg(&inst->classPtr->err, kInstResetFailDspRC, "MIDI file open failed.");
  596. else
  597. {
  598. p->curMsgIdx = 0;
  599. p->bsi = cmDspInt(inst,kBsiMfId);
  600. p->esi = cmDspInt(inst,kEsiMfId);
  601. p->csi = 0;
  602. // force the first msg to occurr one quarter note into the file
  603. cmMidiFileSetDelay(p->mfH, cmMidiFileTicksPerQN(p->mfH) );
  604. // convert midi msg times to absolute time in samples
  605. cmMidiFileTickToSamples(p->mfH,cmDspSampleRate(ctx),true);
  606. }
  607. return rc;
  608. }
  609. cmDspRC_t _cmDspMidiFilePlayReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  610. {
  611. cmDspApplyAllDefaults(ctx,inst);
  612. return _cmDspMidiFilePlayOpen(ctx,inst);
  613. }
  614. cmDspRC_t _cmDspMidiFilePlayExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  615. {
  616. cmDspRC_t rc = kOkDspRC;
  617. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  618. unsigned sPc = cmDspSamplesPerCycle(ctx);
  619. if( cmDspSymbol(inst,kSelMfId) != p->stopSymId )
  620. {
  621. if( cmMidiFileIsValid(p->mfH) == false )
  622. {
  623. if( p->errFl==false )
  624. {
  625. rc = cmErrMsg(&inst->classPtr->err, kInvalidStateDspRC,"The MIDI file player has not been given a valid MIDI file.");
  626. p->errFl = true;
  627. }
  628. return rc;
  629. }
  630. const cmMidiTrackMsg_t** mpp = cmMidiFileMsgArray(p->mfH);
  631. unsigned msgN = cmMidiFileMsgCount(p->mfH);
  632. for(; p->curMsgIdx < msgN && p->csi <= mpp[p->curMsgIdx]->dtick && mpp[p->curMsgIdx]->dtick < (p->csi + sPc); ++p->curMsgIdx )
  633. {
  634. const cmMidiTrackMsg_t* mp = mpp[p->curMsgIdx];
  635. switch( mp->status )
  636. {
  637. case kNoteOnMdId:
  638. case kCtlMdId:
  639. cmDspSetUInt(ctx,inst, kSmpIdxMfId, mp->dtick);
  640. cmDspSetUInt(ctx,inst, kD1MfId, mp->u.chMsgPtr->d1);
  641. cmDspSetUInt(ctx,inst, kD0MfId, mp->u.chMsgPtr->d0);
  642. cmDspSetUInt(ctx,inst, kStatusMfId, mp->status);
  643. break;
  644. }
  645. }
  646. }
  647. p->csi += sPc;
  648. return rc;
  649. }
  650. cmDspRC_t _cmDspMidiFilePlayRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  651. {
  652. cmDspMidiFilePlay_t* p = (cmDspMidiFilePlay_t*)inst;
  653. cmDspSetEvent(ctx,inst,evt);
  654. switch(evt->dstVarId)
  655. {
  656. case kFnMfId:
  657. _cmDspMidiFilePlayOpen(ctx, inst );
  658. break;
  659. case kSelMfId:
  660. {
  661. if( cmDspSymbol(inst,kSelMfId)==p->startSymId )
  662. {
  663. p->csi = cmDspInt(inst,kBsiMfId);
  664. p->curMsgIdx = _cmDspMidiFilePlaySeekMsgIdx(ctx, inst, p->csi );
  665. }
  666. break;
  667. }
  668. }
  669. return kOkDspRC;
  670. }
  671. struct cmDspClass_str* cmMidiFilePlayClassCons( cmDspCtx_t* ctx )
  672. {
  673. cmDspClassSetup(&_cmMidiFilePlayDC,ctx,"MidiFilePlay",
  674. NULL,
  675. _cmDspMidiFilePlayAlloc,
  676. _cmDspMidiFilePlayFree,
  677. _cmDspMidiFilePlayReset,
  678. _cmDspMidiFilePlayExec,
  679. _cmDspMidiFilePlayRecv,
  680. NULL,NULL,
  681. "Time tagged text file.");
  682. return &_cmMidiFilePlayDC;
  683. }
  684. //==========================================================================================================================================
  685. enum
  686. {
  687. kFnSfId,
  688. kBufCntSfId,
  689. kMinLkAhdSfId,
  690. kMaxWndCntSfId,
  691. kMinVelSfId,
  692. kIndexSfId,
  693. kStatusSfId,
  694. kD0SfId,
  695. kD1SfId,
  696. kSmpIdxSfId,
  697. kCmdSfId,
  698. kOutSfId,
  699. kDynSfId,
  700. kEvenSfId,
  701. kTempoSfId,
  702. kCostSfId,
  703. kSymSfId
  704. };
  705. cmDspClass_t _cmScFolDC;
  706. struct cmDspScFol_str;
  707. typedef struct
  708. {
  709. cmDspCtx_t* ctx;
  710. struct cmDspScFol_str* sfp;
  711. } cmDspScFolCbArg_t;
  712. typedef struct cmDspScFol_str
  713. {
  714. cmDspInst_t inst;
  715. cmScMatcher* sfp;
  716. cmScMeas* smp;
  717. cmScH_t scH;
  718. cmDspScFolCbArg_t arg;
  719. unsigned printSymId;
  720. unsigned quietSymId;
  721. } cmDspScFol_t;
  722. cmDspInst_t* _cmDspScFolAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  723. {
  724. cmDspVarArg_t args[] =
  725. {
  726. { "fn", kFnSfId, 0, 0, kInDsvFl | kStrzDsvFl | kReqArgDsvFl, "Score file." },
  727. { "bufcnt",kBufCntSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Event buffer element count." },
  728. { "lkahd", kMinLkAhdSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Minimum window look-ahead."},
  729. { "wndcnt",kMaxWndCntSfId,0, 0, kInDsvFl | kUIntDsvFl, "Maximum window length."},
  730. { "minvel",kMinVelSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Minimum velocity."},
  731. { "index", kIndexSfId, 0, 0, kInDsvFl | kUIntDsvFl, "Tracking start location."},
  732. { "status",kStatusSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI status byte"},
  733. { "d0", kD0SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 0"},
  734. { "d1", kD1SfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI data byte 1"},
  735. { "smpidx",kSmpIdxSfId, 0, 0, kInDsvFl | kUIntDsvFl, "MIDI time tag as a sample index"},
  736. { "cmd", kCmdSfId, 0, 0, kInDsvFl | kSymDsvFl, "Command input: print | quiet"},
  737. { "out", kOutSfId, 0, 0, kOutDsvFl| kUIntDsvFl, "Current score index."},
  738. { "dyn", kDynSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Dynamic value."},
  739. { "even", kEvenSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Evenness value."},
  740. { "tempo", kTempoSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Tempo value."},
  741. { "cost", kCostSfId, 0, 0, kOutDsvFl| kDoubleDsvFl, "Match cost value."},
  742. { "sym", kSymSfId, 0, 0, kOutDsvFl| kSymDsvFl, "Symbol associated with a global variable which has changed value."},
  743. { NULL, 0, 0, 0, 0, NULL }
  744. };
  745. cmDspScFol_t* p;
  746. if((p = cmDspInstAlloc(cmDspScFol_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl)) == NULL )
  747. return NULL;
  748. p->sfp = cmScMatcherAlloc(ctx->cmProcCtx, NULL, 0, cmScNullHandle, 0, 0, NULL, NULL );
  749. p->smp = cmScMeasAlloc( ctx->cmProcCtx, NULL, cmScNullHandle, 0, NULL, 0 );
  750. p->printSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"print");
  751. p->quietSymId = cmSymTblRegisterStaticSymbol(ctx->stH,"quiet");
  752. cmDspSetDefaultUInt( ctx, &p->inst, kBufCntSfId, 0, 7);
  753. cmDspSetDefaultUInt( ctx, &p->inst, kMaxWndCntSfId, 0, 10);
  754. cmDspSetDefaultUInt( ctx, &p->inst, kMinLkAhdSfId, 0, 3);
  755. cmDspSetDefaultUInt( ctx, &p->inst, kMinVelSfId, 0, 5);
  756. cmDspSetDefaultUInt( ctx, &p->inst, kIndexSfId, 0, 0);
  757. cmDspSetDefaultUInt( ctx, &p->inst, kOutSfId, 0, 0);
  758. cmDspSetDefaultDouble( ctx, &p->inst, kDynSfId, 0, 0);
  759. cmDspSetDefaultDouble( ctx, &p->inst, kEvenSfId, 0, 0);
  760. cmDspSetDefaultDouble( ctx, &p->inst, kTempoSfId, 0, 0);
  761. cmDspSetDefaultDouble( ctx, &p->inst, kCostSfId, 0, 0);
  762. cmDspSetDefaultSymbol(ctx,&p->inst, kCmdSfId, p->quietSymId );
  763. return &p->inst;
  764. }
  765. cmDspRC_t _cmDspScFolFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  766. {
  767. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  768. cmScMatcherFree(&p->sfp);
  769. cmScMeasFree(&p->smp);
  770. cmScoreFinalize(&p->scH);
  771. return kOkDspRC;
  772. }
  773. // This is a callback function from cmScMatcherExec() which is called when
  774. // this cmDspFol object receives a new score location index.
  775. void _cmScFolMatcherCb( cmScMatcher* p, void* arg, cmScMatcherResult_t* rp )
  776. {
  777. cmDspScFolCbArg_t* ap = (cmDspScFolCbArg_t*)arg;
  778. if( cmScMeasExec(ap->sfp->smp, rp->mni, rp->locIdx, rp->scEvtIdx, rp->flags, rp->smpIdx, rp->pitch, rp->vel ) == cmOkRC )
  779. {
  780. cmDspInst_t* inst = &(ap->sfp->inst);
  781. // send 'set' values that were calculated on the previous call to cmScMeasExec()
  782. unsigned i;
  783. for(i=ap->sfp->smp->vsi; i<ap->sfp->smp->nsi; ++i)
  784. if(ap->sfp->smp->set[i].value != DBL_MAX )
  785. {
  786. switch( ap->sfp->smp->set[i].sp->varId )
  787. {
  788. case kEvenVarScId:
  789. cmDspSetDouble(ap->ctx,inst,kEvenSfId,ap->sfp->smp->set[i].value);
  790. break;
  791. case kDynVarScId:
  792. cmDspSetDouble(ap->ctx,inst,kDynSfId,ap->sfp->smp->set[i].value);
  793. break;
  794. case kTempoVarScId:
  795. cmDspSetDouble(ap->ctx,inst,kTempoSfId,ap->sfp->smp->set[i].value);
  796. break;
  797. default:
  798. { assert(0); }
  799. }
  800. cmDspSetDouble(ap->ctx,inst,kCostSfId,ap->sfp->smp->set[i].match_cost);
  801. // Set the values in the global variable storage
  802. cmDspValue_t vv,cv;
  803. unsigned j;
  804. cmDsvSetDouble(&vv,ap->sfp->smp->set[i].value);
  805. cmDsvSetDouble(&cv,ap->sfp->smp->set[i].match_cost);
  806. for(j=0; j<ap->sfp->smp->set[i].sp->sectCnt; ++j)
  807. {
  808. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->symArray[j], &vv );
  809. cmDspStoreSetValueViaSym(ap->ctx->dsH, ap->sfp->smp->set[i].sp->costSymArray[j], &cv );
  810. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->symArray[j]);
  811. cmDspSetSymbol(ap->ctx,inst,kSymSfId,ap->sfp->smp->set[i].sp->costSymArray[j]);
  812. }
  813. }
  814. /*
  815. // trigger 'section' starts
  816. for(i=ap->sfp->smp->vsli; i<ap->sfp->smp->nsli; ++i)
  817. {
  818. const cmScoreLoc_t* locPtr = cmScoreLoc(ap->sfp->smp->mp->scH,i);
  819. if( locPtr->begSectPtr != NULL )
  820. cmDspSetUInt(ap->ctx,inst,kSectIndexSfId,locPtr->begSectPtr->index);
  821. }
  822. */
  823. }
  824. }
  825. cmDspRC_t _cmDspScFolOpenScore( cmDspCtx_t* ctx, cmDspInst_t* inst )
  826. {
  827. cmDspRC_t rc = kOkDspRC;
  828. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  829. const cmChar_t* fn;
  830. if((fn = cmDspStrcz(inst,kFnSfId)) == NULL || strlen(fn)==0 )
  831. return cmErrMsg(&inst->classPtr->err, kInvalidArgDspRC, "No score file name supplied.");
  832. if( cmScoreInitialize(ctx->cmCtx, &p->scH, fn, cmDspSampleRate(ctx), NULL, 0, NULL, NULL, ctx->stH ) != kOkScRC )
  833. return cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to open the score '%s'.",fn);
  834. if( cmScoreIsValid(p->scH) )
  835. {
  836. unsigned* dynRefArray = NULL;
  837. unsigned dynRefCnt = 0;
  838. // initialize the cmScMatcher
  839. if( cmScMatcherInit(p->sfp, cmDspSampleRate(ctx), p->scH, cmDspUInt(inst,kMaxWndCntSfId), cmDspUInt(inst,kBufCntSfId), _cmScFolMatcherCb, p->smp ) != cmOkRC )
  840. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal score follower allocation failed.");
  841. // read the dynamics reference array
  842. if( cmDspRsrcUIntArray(ctx->dspH, &dynRefCnt, &dynRefArray, "dynRef", NULL ) != kOkDspRC )
  843. {
  844. rc = cmErrMsg(&inst->classPtr->err, kRsrcNotFoundDspRC, "The dynamics reference array resource was not found.");
  845. goto errLabel;
  846. }
  847. // initialize the cmScMeas object.
  848. if( cmScMeasInit(p->smp, p->scH, cmDspSampleRate(ctx), dynRefArray, dynRefCnt ) != cmOkRC )
  849. rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Internal scMeas object initialization failed.");
  850. }
  851. errLabel:
  852. return rc;
  853. }
  854. cmDspRC_t _cmDspScFolReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  855. {
  856. cmDspRC_t rc;
  857. if((rc = cmDspApplyAllDefaults(ctx,inst)) != kOkDspRC )
  858. return rc;
  859. return _cmDspScFolOpenScore(ctx,inst);
  860. }
  861. cmDspRC_t _cmDspScFolRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  862. {
  863. cmDspRC_t rc = kOkDspRC;
  864. cmDspScFol_t* p = (cmDspScFol_t*)inst;
  865. if((rc = cmDspSetEvent(ctx,inst,evt)) == kOkDspRC && p->sfp != NULL )
  866. {
  867. switch( evt->dstVarId )
  868. {
  869. case kIndexSfId:
  870. if( cmScoreIsValid(p->scH) )
  871. {
  872. if( cmScMeasReset( p->smp ) != cmOkRC )
  873. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score measure unit reset to score index '%i' failed.");
  874. if( cmScMatcherReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC )
  875. cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed.");
  876. }
  877. break;
  878. case kStatusSfId:
  879. if( cmScoreIsValid(p->scH))
  880. {
  881. unsigned scLocIdx = cmInvalidIdx;
  882. // setup the cmScMeas() callback arg.
  883. p->arg.ctx = ctx;
  884. p->arg.sfp = p;
  885. p->sfp->cbArg = &p->arg;
  886. // this call may result in a callback to _cmScFolMatcherCb()
  887. if( cmScMatcherExec(p->sfp, cmDspUInt(inst,kSmpIdxSfId), cmDspUInt(inst,kStatusSfId), cmDspUInt(inst,kD0SfId), cmDspUInt(inst,kD1SfId), &scLocIdx) == cmOkRC )
  888. if( scLocIdx != cmInvalidIdx )
  889. cmDspSetUInt(ctx,inst,kOutSfId,scLocIdx);
  890. }
  891. break;
  892. case kFnSfId:
  893. _cmDspScFolOpenScore(ctx,inst);
  894. break;
  895. case kCmdSfId:
  896. if( cmDspSymbol(inst,kCmdSfId) == p->printSymId )
  897. p->sfp->printFl = true;
  898. else
  899. if( cmDspSymbol(inst,kCmdSfId) == p->quietSymId )
  900. p->sfp->printFl = false;
  901. break;
  902. }
  903. }
  904. return rc;
  905. }
  906. struct cmDspClass_str* cmScFolClassCons( cmDspCtx_t* ctx )
  907. {
  908. cmDspClassSetup(&_cmScFolDC,ctx,"ScFol",
  909. NULL,
  910. _cmDspScFolAlloc,
  911. _cmDspScFolFree,
  912. _cmDspScFolReset,
  913. NULL,
  914. _cmDspScFolRecv,
  915. NULL,NULL,
  916. "Score Follower");
  917. return &_cmScFolDC;
  918. }
  919. //==========================================================================================================================================
  920. enum
  921. {
  922. kScLocIdxMdId,
  923. kResetIdxMdId,
  924. kCmdMdId
  925. };
  926. cmDspClass_t _cmModulatorDC;
  927. typedef struct
  928. {
  929. cmDspInst_t inst;
  930. cmScModulator* mp;
  931. cmDspCtx_t* tmp_ctx; // used to temporarily hold the current cmDspCtx during callback
  932. cmChar_t* fn;
  933. cmChar_t* modLabel;
  934. unsigned onSymId;
  935. unsigned offSymId;
  936. } cmDspScMod_t;
  937. void _cmDspScModCb( void* arg, unsigned varSymId, double value )
  938. {
  939. cmDspScMod_t* p = (cmDspScMod_t*)arg;
  940. cmDspVar_t* varPtr;
  941. if((varPtr = cmDspVarSymbolToPtr( p->tmp_ctx, &p->inst, varSymId, 0 )) == NULL )
  942. return;
  943. cmDspSetDouble(p->tmp_ctx,&p->inst,varPtr->constId,value);
  944. }
  945. cmDspInst_t* _cmDspScModAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  946. {
  947. va_list vl1;
  948. va_copy(vl1,vl);
  949. cmDspVarArg_t args[] =
  950. {
  951. { "index", kScLocIdxMdId, 0,0, kInDsvFl | kUIntDsvFl, "Score follower index input."},
  952. { "reset", kResetIdxMdId, 0,0, kInDsvFl | kUIntDsvFl | kOptArgDsvFl, "Reset the modulator and go to the score index."},
  953. { "cmd", kCmdMdId, 0,0, kInDsvFl | kSymDsvFl | kOptArgDsvFl, "on | off."},
  954. { NULL, 0, 0, 0, 0 }
  955. };
  956. // validate the argument count
  957. if( va_cnt != 2 )
  958. {
  959. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator requires at least two arguments.");
  960. return NULL;
  961. }
  962. // read the modulator file and label strings
  963. const cmChar_t* fn = va_arg(vl1,const cmChar_t*);
  964. const cmChar_t* modLabel = va_arg(vl1,const cmChar_t*);
  965. va_end(vl1);
  966. // validate the file
  967. if( fn==NULL || cmFsIsFile(fn)==false )
  968. {
  969. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The Modulator file '%s' is not valid.",cmStringNullGuard(fn));
  970. return NULL;
  971. }
  972. // allocate the internal modulator object
  973. cmScModulator* mp = cmScModulatorAlloc(ctx->cmProcCtx, NULL, ctx->cmCtx, ctx->stH, cmDspSampleRate(ctx), cmDspSamplesPerCycle(ctx), fn, modLabel, _cmDspScModCb, NULL );
  974. if(mp == NULL )
  975. {
  976. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The internal modulator object initialization failed.");
  977. return NULL;
  978. }
  979. unsigned fixArgCnt = sizeof(args)/sizeof(args[0]) - 1;
  980. unsigned argCnt = fixArgCnt + cmScModulatorOutVarCount(mp);
  981. cmDspVarArg_t a[ argCnt+1 ];
  982. unsigned i;
  983. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  984. for(i=fixArgCnt; i<argCnt; ++i)
  985. {
  986. unsigned varIdx = i - fixArgCnt;
  987. const cmScModVar_t* vp = cmScModulatorOutVar(mp,varIdx);
  988. const cmChar_t* label = cmSymTblLabel( ctx->stH, vp->varSymId );
  989. const cmChar_t* docStr = cmTsPrintfS("Variable output for %s",label);
  990. cmDspArgSetup(ctx, a + i, label, cmInvalidId, i, 0, 0, kOutDsvFl | kDoubleDsvFl, docStr );
  991. }
  992. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  993. cmDspScMod_t* p = cmDspInstAlloc(cmDspScMod_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  994. p->fn = cmMemAllocStr(fn);
  995. p->modLabel = cmMemAllocStr(modLabel);
  996. p->mp = mp;
  997. p->onSymId = cmSymTblId(ctx->stH,"on");
  998. p->offSymId = cmSymTblId(ctx->stH,"off");
  999. mp->cbArg = p; // set the modulator callback arg
  1000. cmDspSetDefaultUInt(ctx,&p->inst,kScLocIdxMdId,0,0);
  1001. cmDspSetDefaultSymbol(ctx,&p->inst,kCmdMdId,p->offSymId);
  1002. return &p->inst;
  1003. }
  1004. cmDspRC_t _cmDspScModFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1005. {
  1006. cmDspRC_t rc = kOkDspRC;
  1007. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1008. if( cmScModulatorFree(&p->mp) != kOkTlRC )
  1009. return cmErrMsg(&inst->classPtr->err, kInstFinalFailDspRC, "Modulator release failed.");
  1010. cmMemFree(p->fn);
  1011. cmMemFree(p->modLabel);
  1012. return rc;
  1013. }
  1014. cmDspRC_t _cmDspScModReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1015. {
  1016. cmDspRC_t rc = kOkDspRC;
  1017. cmDspApplyAllDefaults(ctx,inst);
  1018. return rc;
  1019. }
  1020. cmDspRC_t _cmDspScModRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1021. {
  1022. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1023. cmDspSetEvent(ctx,inst,evt);
  1024. switch( evt->dstVarId )
  1025. {
  1026. case kResetIdxMdId:
  1027. cmDspSetUInt(ctx,inst,kScLocIdxMdId,cmDspUInt(inst,kResetIdxMdId));
  1028. break;
  1029. case kCmdMdId:
  1030. {
  1031. unsigned symId = cmDspSymbol(inst,kCmdMdId);
  1032. if( symId == p->onSymId )
  1033. cmScModulatorReset(p->mp, ctx->cmCtx, cmDspUInt(inst,kScLocIdxMdId));
  1034. }
  1035. break;
  1036. }
  1037. return kOkDspRC;
  1038. }
  1039. cmDspRC_t _cmDspScModExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1040. {
  1041. cmDspRC_t rc = kOkDspRC;
  1042. cmDspScMod_t* p = (cmDspScMod_t*)inst;
  1043. if( cmDspSymbol(inst,kCmdMdId) != p->offSymId )
  1044. {
  1045. p->tmp_ctx = ctx;
  1046. cmScModulatorExec(p->mp,cmDspUInt(inst,kScLocIdxMdId));
  1047. }
  1048. return rc;
  1049. }
  1050. struct cmDspClass_str* cmScModClassCons( cmDspCtx_t* ctx )
  1051. {
  1052. cmDspClassSetup(&_cmModulatorDC,ctx,"ScMod",
  1053. NULL,
  1054. _cmDspScModAlloc,
  1055. _cmDspScModFree,
  1056. _cmDspScModReset,
  1057. _cmDspScModExec,
  1058. _cmDspScModRecv,
  1059. NULL,NULL,
  1060. "Score Driven Variable Modulator.");
  1061. return &_cmModulatorDC;
  1062. }
  1063. //==========================================================================================================================================
  1064. enum
  1065. {
  1066. kInChCntGsId,
  1067. kOutGroupCntGsId,
  1068. kGroupSelIdxGsId,
  1069. kBaseInFloatGsId
  1070. };
  1071. cmDspClass_t _cmGSwitchDC;
  1072. typedef struct
  1073. {
  1074. cmDspInst_t inst;
  1075. unsigned iChCnt;
  1076. unsigned oGroupCnt;
  1077. unsigned baseInFloatGsId;
  1078. unsigned baseInSymGsId;
  1079. unsigned baseInBoolGsId;
  1080. unsigned baseOutFloatGsId;
  1081. unsigned baseOutSymGsId;
  1082. unsigned baseOutBoolGsId;
  1083. } cmDspGSwitch_t;
  1084. cmDspInst_t* _cmDspGSwitchAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1085. {
  1086. va_list vl1;
  1087. va_copy(vl1,vl);
  1088. cmDspVarArg_t args[] =
  1089. {
  1090. { "ichs", kInChCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Input channel count."},
  1091. { "ochs", kOutGroupCntGsId, 0,0, kUIntDsvFl | kReqArgDsvFl, "Output group count."},
  1092. { "sel", kGroupSelIdxGsId, 0,0, kInDsvFl | kUIntDsvFl, "Group select index."},
  1093. { NULL, 0, 0, 0, 0 }
  1094. };
  1095. // validate the argument count
  1096. if( va_cnt != 2 )
  1097. {
  1098. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch requires at least two arguments.");
  1099. return NULL;
  1100. }
  1101. // read the input ch and output group count
  1102. unsigned iChCnt = va_arg(vl1,unsigned);
  1103. unsigned oGroupCnt = va_arg(vl1,unsigned);
  1104. va_end(vl1);
  1105. // validate the channel counts
  1106. if( iChCnt == 0 || oGroupCnt==0 )
  1107. {
  1108. cmDspClassErr(ctx,classPtr,kInvalidArgDspRC,"The GSwitch input channel count and group count must be greater than zero.");
  1109. return NULL;
  1110. }
  1111. unsigned typeCnt = 3; // i.e. float,sym,bool
  1112. unsigned baseInFloatGsId = kBaseInFloatGsId;
  1113. unsigned baseInSymGsId = baseInFloatGsId + iChCnt;
  1114. unsigned baseInBoolGsId = baseInSymGsId + iChCnt;
  1115. unsigned baseOutFloatGsId = baseInBoolGsId + iChCnt;
  1116. unsigned baseOutSymGsId = baseOutFloatGsId + (iChCnt * oGroupCnt);
  1117. unsigned baseOutBoolGsId = baseOutSymGsId + (iChCnt * oGroupCnt);
  1118. unsigned fixArgCnt = 3;
  1119. unsigned varArgCnt = (iChCnt * typeCnt) + (iChCnt * typeCnt * oGroupCnt);
  1120. unsigned argCnt = fixArgCnt + varArgCnt;
  1121. cmDspVarArg_t a[ argCnt+1 ];
  1122. unsigned i;
  1123. cmDspArgCopy( a, argCnt, 0, args, fixArgCnt );
  1124. cmDspArgSetupN( ctx, a, argCnt, baseInFloatGsId, iChCnt, "f-in", baseInFloatGsId, 0, 0, kInDsvFl | kDoubleDsvFl, "Float input");
  1125. cmDspArgSetupN( ctx, a, argCnt, baseInSymGsId, iChCnt, "s-in", baseInSymGsId, 0, 0, kInDsvFl | kSymDsvFl, "Symbol input");
  1126. cmDspArgSetupN( ctx, a, argCnt, baseInBoolGsId, iChCnt, "b-in", baseInBoolGsId, 0, 0, kInDsvFl | kBoolDsvFl, "Bool input");
  1127. unsigned labelCharCnt = 63;
  1128. cmChar_t label[labelCharCnt+1];
  1129. label[labelCharCnt] = 0;
  1130. unsigned gsid = baseOutFloatGsId;
  1131. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1132. {
  1133. snprintf(label,labelCharCnt,"f-out-%i",i);
  1134. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kDoubleDsvFl, "Float output");
  1135. }
  1136. gsid = baseOutSymGsId;
  1137. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1138. {
  1139. snprintf(label,labelCharCnt,"s-out-%i",i);
  1140. cmDspArgSetupN( ctx, a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kSymDsvFl, "Symbol output");
  1141. }
  1142. gsid = baseOutBoolGsId;
  1143. for(i=0; i<oGroupCnt; ++i, gsid+=iChCnt)
  1144. {
  1145. snprintf(label,labelCharCnt,"b-out-%i",i);
  1146. cmDspArgSetupN( ctx,a, argCnt, gsid, iChCnt, label, gsid, 0, 0, kInDsvFl | kBoolDsvFl, "Bool output");
  1147. }
  1148. cmDspArgSetupNull(a+argCnt); // set terminating arg. flags
  1149. cmDspGSwitch_t* p = cmDspInstAlloc(cmDspGSwitch_t,ctx,classPtr,a,instSymId,id,storeSymId,va_cnt,vl);
  1150. p->iChCnt = iChCnt;
  1151. p->oGroupCnt = oGroupCnt;
  1152. p->baseInFloatGsId = baseInFloatGsId;
  1153. p->baseInSymGsId = baseInSymGsId;
  1154. p->baseInBoolGsId = baseInBoolGsId;
  1155. p->baseOutFloatGsId = baseOutFloatGsId;
  1156. p->baseOutSymGsId = baseOutSymGsId;
  1157. p->baseOutBoolGsId = baseOutBoolGsId;
  1158. cmDspSetDefaultUInt(ctx,&p->inst,kGroupSelIdxGsId,0,0);
  1159. return &p->inst;
  1160. }
  1161. cmDspRC_t _cmDspGSwitchReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1162. {
  1163. cmDspRC_t rc = kOkDspRC;
  1164. cmDspApplyAllDefaults(ctx,inst);
  1165. return rc;
  1166. }
  1167. cmDspRC_t _cmDspGSwitchRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1168. {
  1169. cmDspRC_t rc = kOkDspRC;
  1170. cmDspGSwitch_t* p = (cmDspGSwitch_t*)inst;
  1171. // if this is the group selector
  1172. if( evt->dstVarId == kGroupSelIdxGsId )
  1173. {
  1174. unsigned idx;
  1175. if( (idx = cmDsvGetUInt(evt->valuePtr)) > p->oGroupCnt )
  1176. cmDspInstErr(ctx,inst,kInvalidArgDspRC,"The GSwitch group select index %i is out of range %i.",idx,p->oGroupCnt);
  1177. else
  1178. cmDspSetEvent(ctx,inst,evt);
  1179. return rc;
  1180. }
  1181. // get the group selector
  1182. unsigned groupIdx = cmDspUInt(inst,kGroupSelIdxGsId);
  1183. assert( groupIdx < p->oGroupCnt);
  1184. // if this is a float input
  1185. if( p->baseInFloatGsId <= evt->dstVarId && evt->dstVarId < p->baseInFloatGsId + p->iChCnt )
  1186. {
  1187. unsigned outVarId = p->baseOutFloatGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInFloatGsId);
  1188. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1189. return rc;
  1190. }
  1191. // if this is a symbol input
  1192. if( p->baseInSymGsId <= evt->dstVarId && evt->dstVarId < p->baseInSymGsId + p->iChCnt )
  1193. {
  1194. unsigned outVarId = p->baseOutSymGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInSymGsId);
  1195. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1196. return rc;
  1197. }
  1198. // if this is a bool input
  1199. if( p->baseInBoolGsId <= evt->dstVarId && evt->dstVarId < p->baseInBoolGsId + p->iChCnt )
  1200. {
  1201. unsigned outVarId = p->baseOutBoolGsId + (groupIdx * p->iChCnt) + (evt->dstVarId - p->baseInBoolGsId);
  1202. cmDspValueSet(ctx, inst, outVarId, evt->valuePtr, 0 );
  1203. return rc;
  1204. }
  1205. return rc;
  1206. }
  1207. struct cmDspClass_str* cmGSwitchClassCons( cmDspCtx_t* ctx )
  1208. {
  1209. cmDspClassSetup(&_cmGSwitchDC,ctx,"GSwitch",
  1210. NULL,
  1211. _cmDspGSwitchAlloc,
  1212. NULL,
  1213. _cmDspGSwitchReset,
  1214. NULL,
  1215. _cmDspGSwitchRecv,
  1216. NULL,NULL,
  1217. "Ganged switch.");
  1218. return &_cmGSwitchDC;
  1219. }
  1220. //==========================================================================================================================================
  1221. enum
  1222. {
  1223. kMinInSrId,
  1224. kMaxInSrId,
  1225. kMinOutSrId,
  1226. kMaxOutSrId,
  1227. kValInSrId,
  1228. kValOutSrId,
  1229. };
  1230. cmDspClass_t _cmScaleRangeDC;
  1231. typedef struct
  1232. {
  1233. cmDspInst_t inst;
  1234. } cmDspScaleRange_t;
  1235. cmDspInst_t* _cmDspScaleRangeAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl )
  1236. {
  1237. va_list vl1;
  1238. va_copy(vl1,vl);
  1239. cmDspVarArg_t args[] =
  1240. {
  1241. { "min_in", kMinInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1242. { "max_in", kMaxInSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1243. { "min_out", kMinOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1244. { "max_out", kMaxOutSrId, 0,0, kInDsvFl | kDoubleDsvFl , "Min Input value."},
  1245. { "val_in", kValInSrId, 0,0, kInDsvFl | kDoubleDsvFl, "Input value."},
  1246. { "val_out", kValOutSrId, 0,0, kOutDsvFl | kDoubleDsvFl, "Output value"},
  1247. { NULL, 0, 0, 0, 0 }
  1248. };
  1249. cmDspScaleRange_t* p = cmDspInstAlloc(cmDspScaleRange_t,ctx,classPtr,args,instSymId,id,storeSymId,va_cnt,vl);
  1250. cmDspSetDefaultDouble(ctx,&p->inst,kMinInSrId,0,0);
  1251. cmDspSetDefaultDouble(ctx,&p->inst,kMaxInSrId,0,1.0);
  1252. cmDspSetDefaultDouble(ctx,&p->inst,kMinOutSrId,0,0);
  1253. cmDspSetDefaultDouble(ctx,&p->inst,kMaxOutSrId,0,1.0);
  1254. return &p->inst;
  1255. }
  1256. cmDspRC_t _cmDspScaleRangeReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1257. {
  1258. cmDspRC_t rc = kOkDspRC;
  1259. cmDspApplyAllDefaults(ctx,inst);
  1260. return rc;
  1261. }
  1262. cmDspRC_t _cmDspScaleRangeRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  1263. {
  1264. cmDspRC_t rc = kOkDspRC;
  1265. //cmDspScaleRange_t* p = (cmDspScaleRange_t*)inst;
  1266. cmDspSetEvent(ctx,inst,evt);
  1267. if( evt->dstVarId == kValInSrId )
  1268. {
  1269. double val = cmDspDouble(inst,kValInSrId);
  1270. double min_in = cmDspDouble(inst,kMinInSrId);
  1271. double max_in = cmDspDouble(inst,kMaxInSrId);
  1272. double min_out = cmDspDouble(inst,kMinOutSrId);
  1273. double max_out = cmDspDouble(inst,kMaxOutSrId);
  1274. double x = cmMax(min_in,cmMin(max_in,val));
  1275. x = (x - min_in)/(max_in - min_in);
  1276. x = min_out + x * (max_out - min_out);
  1277. cmDspSetDouble(ctx,inst,kValOutSrId, x );
  1278. //printf("%f (%f %f) : (%f %f) %f\n",val,min_in,max_in,min_out,max_out,x);
  1279. }
  1280. return rc;
  1281. }
  1282. struct cmDspClass_str* cmScaleRangeClassCons( cmDspCtx_t* ctx )
  1283. {
  1284. cmDspClassSetup(&_cmScaleRangeDC,ctx,"ScaleRange",
  1285. NULL,
  1286. _cmDspScaleRangeAlloc,
  1287. NULL,
  1288. _cmDspScaleRangeReset,
  1289. NULL,
  1290. _cmDspScaleRangeRecv,
  1291. NULL,NULL,
  1292. "Scale a value inside an input range to a value in the output range.");
  1293. return &_cmScaleRangeDC;
  1294. }