libcm is a C development framework with an emphasis on audio signal processing applications.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cmDspKr.c 49KB

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