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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmRpt.h"
  5. #include "cmErr.h"
  6. #include "cmCtx.h"
  7. #include "cmMem.h"
  8. #include "cmMallocDebug.h"
  9. #include "cmLinkedHeap.h"
  10. #include "cmText.h"
  11. #include "cmFileSys.h"
  12. #include "cmSymTbl.h"
  13. #include "cmMidi.h"
  14. #include "cmJson.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 "cmProcObj.h"
  23. #include "cmDspCtx.h"
  24. #include "cmDspClass.h"
  25. #include "cmDspStore.h"
  26. #include "cmDspSys.h"
  27. #include "cmDspBuiltIn.h"
  28. #include "cmDspPgm.h"
  29. #include "cmDspPreset.h"
  30. #include "cmDspNet.h"
  31. #include "cmTime.h"
  32. cmDspSysH_t cmDspNullHandle = cmSTATIC_NULL_HANDLE;
  33. #define kDspSysLabelCharCnt (127)
  34. cmChar_t _cmDspSysLabel0[ kDspSysLabelCharCnt + 1 ];
  35. cmChar_t _cmDspSysLabel1[ kDspSysLabelCharCnt + 1 ];
  36. const cmChar_t* cmDspSysPrintLabel( const cmChar_t* label, unsigned i )
  37. {
  38. snprintf(_cmDspSysLabel0,kDspSysLabelCharCnt,"%s-%i",label,i);
  39. assert( strlen(_cmDspSysLabel0 ) <= kDspSysLabelCharCnt );
  40. return _cmDspSysLabel0;
  41. }
  42. const cmChar_t* cmDspSysPrintLabel2( const cmChar_t* label, unsigned i )
  43. {
  44. snprintf(_cmDspSysLabel1,kDspSysLabelCharCnt,"%s-%i",label,i);
  45. assert( strlen(_cmDspSysLabel1 ) <= kDspSysLabelCharCnt );
  46. return _cmDspSysLabel1;
  47. }
  48. ///============================================================================================
  49. cmDsp_t* _cmDspHandleToPtr( cmDspSysH_t h )
  50. {
  51. assert(h.h != NULL);
  52. return (cmDsp_t*)h.h;
  53. }
  54. cmDspInst_t* _cmDspSysInstSymIdToPtr( cmDsp_t* p, unsigned instSymId )
  55. {
  56. _cmDspInst_t* ip = p->instList;
  57. for(; ip!=NULL; ip = ip->linkPtr )
  58. if( ip->instPtr->symId == instSymId )
  59. return ip->instPtr;
  60. return NULL;
  61. }
  62. // free a single instance
  63. cmDspRC_t _cmDspInstFree( cmDspCtx_t* ctx, cmDspInst_t* inst )
  64. {
  65. cmDspRC_t rc = kOkDspRC;
  66. if( inst->freeFunc != NULL )
  67. if((rc = inst->freeFunc( ctx, inst, NULL )) != kOkDspRC )
  68. return rc;
  69. cmLHeapFree(ctx->lhH,inst);
  70. return kOkDspRC;
  71. }
  72. cmDspRC_t _cmDspSysFreeInst( cmDsp_t* p, unsigned instId )
  73. {
  74. cmDspRC_t rc = kOkDspRC;
  75. _cmDspInst_t* ip = p->instList;
  76. _cmDspInst_t* pp = NULL;
  77. // locate the requested instance
  78. for(; ip!=NULL; ip=ip->linkPtr)
  79. {
  80. if( ip->instPtr->id == instId )
  81. break;
  82. pp = ip;
  83. }
  84. // verify the instance was located
  85. if( ip == NULL )
  86. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The DSP instance %i could not be found.",instId);
  87. // instruct the instance to release its resources
  88. if( _cmDspInstFree(&p->ctx,ip->instPtr ) != kOkDspRC )
  89. return cmErrMsg(&p->err,kInstFinalFailDspRC,"An attempt to free DSP instance '%s' id:%i failed.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  90. // remove the instance from the linked list
  91. if( pp == NULL )
  92. p->instList = NULL;
  93. else
  94. pp->linkPtr = ip->linkPtr;
  95. return rc;
  96. }
  97. cmDspRC_t _cmDspSysFinalize( cmDsp_t* p )
  98. {
  99. cmDspRC_t rc = kOkDspRC;
  100. _cmDspClass_t* cp = p->classList;
  101. _cmDspSysNetFree(p);
  102. // finalize the classes - no need to free the class ctl
  103. // recd's because they were allocated in the linked heap
  104. for(; cp!=NULL; cp=cp->linkPtr)
  105. {
  106. if( cp->classPtr->finalClassFunc != NULL )
  107. if( cp->classPtr->finalClassFunc(&p->ctx,cp->classPtr) != kOkDspRC )
  108. rc = cmErrMsg(&p->err,kClassFinalFailDspRC,"Class finalization failed on class '%s'.",cp->classPtr->labelStr);
  109. }
  110. if( p->ctx.cmProcCtx != NULL )
  111. if( cmCtxFree(&p->ctx.cmProcCtx) != cmOkRC )
  112. rc = cmErrMsg(&p->err,kProcFailDspRC,"The proc context finalizatoin failed.");
  113. cmDspStoreFree(&p->dsH);
  114. if( cmSymTblIsValid(p->stH) )
  115. cmSymTblDestroy(&p->stH);
  116. if( cmJsonIsValid(p->jsH) )
  117. if( cmJsonFinalize(&p->jsH) != kOkJsRC )
  118. rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON finalization failed.");
  119. if( cmLHeapIsValid(p->lhH) )
  120. cmLHeapDestroy(&p->lhH);
  121. if( rc == kOkJsRC )
  122. {
  123. cmMemFree(p);
  124. // clear the cmAudioSysCtx_t
  125. memset(&p->ctx.ctx,0,sizeof(p->ctx.ctx));
  126. }
  127. return rc;
  128. }
  129. cmDspRC_t cmDspSysInitialize( cmCtx_t* ctx, cmDspSysH_t* hp, cmUdpNetH_t netH )
  130. {
  131. unsigned i;
  132. cmDspRC_t rc = kOkDspRC;
  133. if((rc = cmDspSysFinalize(hp )) != kOkDspRC )
  134. return rc;
  135. cmDsp_t* p = cmMemAllocZ( cmDsp_t, 1 );
  136. cmErrSetup(&p->err,&ctx->rpt,"DSP System");
  137. //p->ctx.ctx = asCtx;
  138. p->cmCtx = *ctx;
  139. p->netH = netH;
  140. p->pgmIdx = cmInvalidIdx;
  141. // create the DSP class linked heap
  142. if(cmLHeapIsValid( p->lhH = cmLHeapCreate(1024,ctx)) == false)
  143. return cmErrMsg(&p->err,kLHeapFailDspRC,"Linked heap intiialization failed.");
  144. // initialize the DSP class JSON object
  145. if( cmJsonInitialize(&p->jsH,ctx) != kOkJsRC )
  146. {
  147. rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON initialization failed.");
  148. goto errLabel;
  149. }
  150. // intialize the DSP class symbol table
  151. if( cmSymTblIsValid( p->stH = cmSymTblCreate(cmSymTblNullHandle,1,ctx)) == false )
  152. {
  153. rc = cmErrMsg(&p->err,kSymTblFailDspRC,"Symbol table initialization failed.");
  154. goto errLabel;
  155. }
  156. // allocate the DSP system variable storage object
  157. if( cmDspStoreAlloc(ctx,&p->dsH,10,10) != kOkDspRC )
  158. {
  159. rc = cmErrMsg(&p->err,kDspStoreFailDspRC,"DSP store allocation failed.");
  160. goto errLabel;
  161. }
  162. // initialize the proc context
  163. if( (p->ctx.cmProcCtx = cmCtxAlloc(NULL,&ctx->rpt,p->lhH,p->stH)) == NULL )
  164. {
  165. rc = cmErrMsg(&p->err,kProcFailDspRC,"cmProc context initialization failed.");
  166. goto errLabel;
  167. }
  168. // allocate the the preset mgr
  169. _cmDspPresetAlloc(&p->pm);
  170. // initialize the networking compenents
  171. if((rc = _cmDspSysNetAlloc(p)) != kOkDspRC )
  172. goto errLabel;
  173. // set the DSP ctx to use the system lHeap and sym. tbl so that the
  174. // DSP class instantions use them
  175. p->ctx.lhH = p->lhH;
  176. p->ctx.jsH = p->jsH;
  177. p->ctx.stH = p->stH;
  178. p->ctx.dsH = p->dsH;
  179. p->ctx.rsrcJsH = cmJsonNullHandle;
  180. p->ctx.rpt = &ctx->rpt;
  181. p->ctx.cmCtx = &p->cmCtx;
  182. p->ctx.dspH.h = p;
  183. _cmDspClass_t* pp = NULL;
  184. cmDspClassConsFunc_t classConsFunc = NULL;
  185. // Get each DSP class construction function
  186. for(i=0; (classConsFunc = cmDspClassGetBuiltIn(i))!=NULL; ++i)
  187. {
  188. // allocate the class ctl recd
  189. _cmDspClass_t* cp = cmLhAllocZ(p->ctx.lhH,_cmDspClass_t,1);
  190. // call the class constructor func
  191. cp->classPtr = classConsFunc(&p->ctx);
  192. // set the class symbol id
  193. cp->symId = cmSymTblRegister(p->ctx.stH,cp->classPtr->labelStr,true);
  194. // link in the class ctl recd
  195. if( pp == NULL )
  196. p->classList = cp;
  197. else
  198. pp->linkPtr = cp;
  199. pp = cp;
  200. }
  201. hp->h = p;
  202. p->ctx.lhH = cmLHeapNullHandle;
  203. p->ctx.jsH = cmJsonNullHandle;
  204. p->ctx.stH = cmSymTblNullHandle;
  205. errLabel:
  206. if( rc != kOkDspRC )
  207. _cmDspSysFinalize(p);
  208. return rc;
  209. }
  210. cmDspRC_t cmDspSysFinalize( cmDspSysH_t* hp )
  211. {
  212. cmDspRC_t rc = kOkDspRC;
  213. if( hp==NULL || cmDspSysIsValid(*hp)==false )
  214. return kOkDspRC;
  215. if((rc = cmDspSysUnload(*hp)) != kOkDspRC )
  216. return rc;
  217. cmDsp_t* p = _cmDspHandleToPtr(*hp);
  218. if((rc = _cmDspSysFinalize(p)) == kOkDspRC )
  219. hp->h = NULL;
  220. return rc;
  221. }
  222. unsigned cmDspSysPgmCount( cmDspSysH_t h )
  223. {
  224. _cmDspSysPgm_t* arr = _cmDspSysPgmArrayBase();
  225. unsigned i = 0;
  226. while( arr != NULL && arr[i].label != NULL )
  227. ++i;
  228. return i;
  229. }
  230. const cmChar_t* cmDspPgmLabel( cmDspSysH_t h, unsigned idx )
  231. {
  232. if( idx < cmDspSysPgmCount(h) )
  233. {
  234. _cmDspSysPgm_t* arr = _cmDspSysPgmArrayBase();
  235. return arr[idx].label;
  236. }
  237. return NULL;
  238. }
  239. bool cmDspSysIsValid( cmDspSysH_t h )
  240. { return h.h != NULL; }
  241. cmDspRC_t cmDspSysLastRC( cmDspSysH_t h )
  242. {
  243. cmDsp_t* p = _cmDspHandleToPtr(h);
  244. return cmErrLastRC(&p->err);
  245. }
  246. cmDspRC_t cmDspSysLoad( cmDspSysH_t h, cmAudioSysCtx_t* asCtx, unsigned pgmIdx )
  247. {
  248. cmDspRC_t rc;
  249. cmDsp_t* p = _cmDspHandleToPtr(h);
  250. p->pgmIdx = cmInvalidIdx;
  251. if( pgmIdx >= cmDspSysPgmCount(h) )
  252. return cmErrMsg(&p->err,kInvalidPgmIdxDspRC,"%i is not a valid program index.",pgmIdx);
  253. // unload the previously loaded DSP program
  254. if((rc = cmDspSysUnload(h)) != kOkDspRC )
  255. return rc;
  256. // assign the cmAudioSysCtx_t context recd
  257. p->ctx.ctx = asCtx;
  258. _cmDspSysPgm_t* arr = _cmDspSysPgmArrayBase();
  259. // form the resource file name
  260. if((p->rsrcFn = cmFsMakeFn(cmFsPrefsDir(),arr[pgmIdx].label,"js",NULL)) == NULL )
  261. {
  262. rc = cmErrMsg(&p->err,kFileSysFailDspRC,"Resource file name formation failed.");
  263. goto errLabel;
  264. }
  265. // if the pgm specific resource file already exists
  266. if( cmFsIsFile(p->rsrcFn) )
  267. {
  268. // open and parse the resource file
  269. if( cmJsonInitializeFromFile(&p->ctx.rsrcJsH,p->rsrcFn,&p->cmCtx) != kOkJsRC )
  270. {
  271. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Resource file open failed.");
  272. goto errLabel;
  273. }
  274. }
  275. else // ... the resource file does not currently exists - create it
  276. {
  277. cmJsonNode_t* rootPtr;
  278. if( cmJsonInitialize(&p->ctx.rsrcJsH,&p->cmCtx) != kOkJsRC )
  279. {
  280. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Resource object allocate failed.");
  281. goto errLabel;
  282. }
  283. if((rootPtr = cmJsonCreateObject(p->ctx.rsrcJsH, NULL )) == NULL )
  284. {
  285. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Resource object root allocate failed.");
  286. goto errLabel;
  287. }
  288. }
  289. // create a linked heap for use by the DSP instances
  290. if(cmLHeapIsValid( p->ctx.lhH = cmLHeapCreate(1024,&p->cmCtx)) == false)
  291. return cmErrMsg(&p->err,kLHeapFailDspRC,"DSP instance linked heap intiialization failed.");
  292. // initialize a JSON object for use by the DSP instances
  293. if( cmJsonInitialize(&p->ctx.jsH,&p->cmCtx) != kOkJsRC )
  294. {
  295. rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON initialization failed.");
  296. goto errLabel;
  297. }
  298. // create a symbol table for use by the DSP instance (note that it uses the system sym. tbl. as it's parent)
  299. if( cmSymTblIsValid( p->ctx.stH = cmSymTblCreate(p->stH, cmDspSys_PARENT_SYM_TBL_BASE_ID, &p->cmCtx)) == false )
  300. {
  301. rc = cmErrMsg(&p->err,kSymTblFailDspRC,"DSP instance symbol table initialization failed.");
  302. goto errLabel;
  303. }
  304. // load the preset mgr
  305. if((rc = _cmDspPresetLoad(&p->pm,&p->cmCtx,&p->err,p->ctx.lhH,p->ctx.stH,arr[pgmIdx].label)) != kOkDspRC )
  306. {
  307. rc = cmErrMsg(&p->err,rc,"Preset manager load failed.");
  308. goto errLabel;
  309. }
  310. // setup the DSP network components
  311. if((rc = _cmDspSysNetPreLoad(p)) != kOkDspRC )
  312. goto errLabel;
  313. p->ctx.cycleCnt = 0;
  314. p->ctx._disableSymId = cmSymTblRegisterStaticSymbol(p->ctx.stH,"_disable");
  315. p->ctx._enableSymId = cmSymTblRegisterStaticSymbol(p->ctx.stH,"_enable");
  316. void** vpp = &arr[ pgmIdx ].userPtr;
  317. cmDspPgmFunc_t pgm = arr[ pgmIdx ].loadFunc;
  318. // allocate and connect user defined signal processing network
  319. if((rc = pgm(h,vpp)) == kOkDspRC )
  320. if((rc = cmErrLastRC(&p->err)) != kOkDspRC )
  321. {
  322. cmErrMsg(&p->err,rc,"User DSP system load failed.");
  323. goto errLabel;
  324. }
  325. p->pgmIdx = pgmIdx;
  326. // enter sync mode
  327. if((rc = _cmDspSysNetSync(p)) != kOkDspRC )
  328. {
  329. cmErrMsg(&p->err,rc,"DSP system sync failed.");
  330. goto errLabel;
  331. }
  332. errLabel:
  333. if( rc != kOkDspRC )
  334. cmDspSysUnload(h);
  335. return rc;
  336. }
  337. cmDspRC_t cmDspSysUnload( cmDspSysH_t h )
  338. {
  339. if( cmDspSysIsValid(h) == false )
  340. return kOkDspRC;
  341. cmDspRC_t rc = kOkDspRC;
  342. cmDsp_t* p = _cmDspHandleToPtr(h);
  343. _cmDspInst_t* ip = p->instList;
  344. // call the DSP network unload function - this function is provided
  345. // by the client network to release any resources obtained when
  346. // the network was created
  347. if( p->pgmIdx != cmInvalidIdx )
  348. {
  349. _cmDspSysPgm_t* arr = _cmDspSysPgmArrayBase();
  350. void** vpp = &arr[ p->pgmIdx ].userPtr;
  351. cmDspPgmFunc_t pgm = arr[ p->pgmIdx ].unloadFunc;
  352. if( pgm != NULL )
  353. if((rc = pgm(h,vpp)) == kOkDspRC )
  354. if((rc = cmErrLastRC(&p->err)) != kOkDspRC )
  355. cmErrMsg(&p->err,rc,"User DSP system unload failed.");
  356. }
  357. p->pgmIdx = cmInvalidIdx;
  358. // unload the networking components
  359. _cmDspSysNetUnload(p);
  360. // free the DSP instances
  361. for(; ip!=NULL; ip=ip->linkPtr)
  362. if((rc = _cmDspInstFree(&p->ctx,ip->instPtr)) != kOkDspRC )
  363. rc = cmErrMsg(&p->err,kInstFinalFailDspRC,"An attempt to free DSP instance '%s' id:%i failed.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  364. p->instList = NULL;
  365. // unload the preset manager
  366. if((rc = _cmDspPresetUnload(&p->pm,&p->cmCtx)) != kOkDspRC)
  367. rc = cmErrMsg(&p->err,rc,"Preset manager 'unload' failed.");
  368. // finalize the pgm specific JSON tree
  369. if( cmJsonIsValid(p->ctx.rsrcJsH))
  370. {
  371. //if( cmJsonWrite(p->ctx.rsrcJsH, cmJsonRoot(p->ctx.rsrcJsH), p->rsrcFn ) != kOkJsRC )
  372. // rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON file write failed on '%s'.",cmStringNullGuard(p->rsrcFn));
  373. //else
  374. //{
  375. if( cmJsonFinalize(&p->ctx.rsrcJsH) != kOkJsRC )
  376. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Resource JSON finalization failed.");
  377. //}
  378. }
  379. // release the JSON file name
  380. if( p->rsrcFn != NULL )
  381. {
  382. cmFsFreeFn(p->rsrcFn); // free the resource file name
  383. p->rsrcFn = NULL;
  384. }
  385. // tell the DSP net system that the pgm has been unloaded
  386. _cmDspSysNetUnload(p);
  387. // elimnate the linked heap, system table, and json object used by the DSP instance objects
  388. if( cmLHeapIsValid(p->ctx.lhH) )
  389. cmLHeapDestroy(&p->ctx.lhH);
  390. if( cmJsonIsValid(p->ctx.jsH) )
  391. if( cmJsonFinalize(&p->ctx.jsH) != kOkJsRC )
  392. rc = cmErrMsg(&p->err,kJsonFailDspRC,"DSP instance JSON object finalization failed.");
  393. if( cmSymTblIsValid(p->ctx.stH) )
  394. cmSymTblDestroy(&p->ctx.stH);
  395. return rc;
  396. }
  397. cmDspRC_t cmDspSysReset( cmDspSysH_t h )
  398. {
  399. cmDsp_t* p = _cmDspHandleToPtr(h);
  400. cmDspRC_t rc = kOkDspRC;
  401. // call reset on each of the instances
  402. _cmDspInst_t* ip = p->instList;
  403. for(; ip != NULL; ip = ip->linkPtr )
  404. if( ip->instPtr->resetFunc != NULL )
  405. {
  406. if( ip->instPtr->resetFunc(&p->ctx,ip->instPtr,NULL) != kOkDspRC )
  407. rc = cmErrMsg(&p->err,kInstResetFailDspRC,"Reset failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  408. ip->instPtr->flags = 0;
  409. }
  410. // send the _reset symbol to every instance which has the _reset attribute symbol
  411. if( rc == kOkDspRC )
  412. {
  413. cmDspValue_t v;
  414. unsigned resetSymId = cmDspSysRegisterStaticSymbol(h,"_reset");
  415. cmDsvSetSymbol(&v,resetSymId);
  416. rc = cmDspSysBroadcastValue(h, resetSymId, &v);
  417. }
  418. return rc;
  419. }
  420. // Handle msg's arriving from the UI
  421. cmDspRC_t _cmDspSysHandleUiMsg(cmDsp_t* p, _cmDspInst_t* ip, const void* msgPtr, unsigned msgByteCnt)
  422. {
  423. cmDspRC_t rc = kOkDspRC;
  424. cmDspUiHdr_t* h = (cmDspUiHdr_t*)msgPtr;
  425. cmDspEvt_t e;
  426. assert( h->uiId == kUiSelAsId );
  427. // the only source of msg's (at the time of this writing) is the UI so:
  428. // Mark the msg as a UI event and set the kUiEchoDspFl if the UI has requested duplex operation.
  429. e.flags = kUiDspFl | (cmIsFlag(h->flags,kDuplexDuiFl) ? kUiEchoDspFl : 0);
  430. e.srcInstPtr = NULL; // the UI has no source instance
  431. e.srcVarId = h->selId; // set the event srcVarId to the UI selId
  432. e.dstVarId = h->instVarId; // identify the dst inst variable
  433. e.dstDataPtr = NULL; // UI msg's don't have custom data (yet)
  434. e.valuePtr = &h->value; //
  435. // if the value associated with this msg is a mtx then set
  436. // its mtx data area pointer to just after the msg header.
  437. if( cmDsvIsMtx(&h->value) )
  438. h->value.u.m.u.vp = ((char*)msgPtr) + sizeof(cmDspUiHdr_t);
  439. else
  440. rc = cmDsvDeserializeInPlace(&h->value,msgByteCnt-sizeof(cmDspUiHdr_t));
  441. if( rc != kOkDsvRC )
  442. cmErrMsg(&p->err,kSerializeUiMsgFailDspRC,"DSP system receive Deserialize failed.");
  443. // find the dest. DSP instance for this msg
  444. for(; ip != NULL; ip = ip->linkPtr )
  445. if( ip->instPtr->id == h->instId )
  446. {
  447. // send the msg to the DSP dst. instance
  448. if( ip->instPtr->recvFunc != NULL )
  449. if( ip->instPtr->recvFunc(&p->ctx,ip->instPtr,&e) != kOkDspRC )
  450. rc = cmErrMsg(&p->err,kInstMsgRcvFailDspRC,"Message recv failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  451. break;
  452. }
  453. if( ip == NULL )
  454. rc = cmErrMsg(&p->err,kInstNotFoundDspRC,"No DSP instance with id %i was found during message delivery.",h->instId);
  455. return rc;
  456. }
  457. // Recv msg's arriving from the audio engine.
  458. cmDspRC_t cmDspSysRcvMsg( cmDspSysH_t h, cmAudioSysCtx_t* asCtx, const void* msgPtr, unsigned msgByteCnt, unsigned srcNetNodeId )
  459. {
  460. cmDsp_t* p = _cmDspHandleToPtr(h);
  461. _cmDspInst_t* ip = p->instList;
  462. if( msgByteCnt == 0 && asCtx->audioRateFl==true)
  463. {
  464. cmTimeSpec_t t0,t1;
  465. cmTimeGet(&t0);
  466. for(; ip != NULL; ip = ip->linkPtr )
  467. if( ip->instPtr->execFunc != NULL && cmIsFlag(ip->instPtr->flags,kDisableExecInstFl)==false )
  468. {
  469. if( ip->instPtr->execFunc(&p->ctx,ip->instPtr,NULL) != kOkDspRC )
  470. cmErrMsg(&p->err,kInstExecFailDspRC,"Execution failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  471. //printf("%i %s\n",p->ctx.cycleCnt,ip->instPtr->classPtr->labelStr);
  472. }
  473. cmTimeGet(&t1);
  474. p->ctx.execDurUsecs = cmTimeElapsedMicros(&t0,&t1);
  475. ++p->ctx.cycleCnt;
  476. }
  477. else
  478. {
  479. unsigned* hdr = (unsigned*)msgPtr;
  480. // the msg selector is the second field in the data packet (following the audio system sub-system id)
  481. //const unsigned msgTypeSelId = ((const unsigned*)msgPtr)[1];
  482. switch( hdr[1] )
  483. {
  484. case kUiSelAsId:
  485. _cmDspSysHandleUiMsg(p,ip,msgPtr,msgByteCnt);
  486. break;
  487. case kNetSyncSelAsId:
  488. _cmDspSysNetRecv(p, (const cmDspNetMsg_t*)msgPtr, msgByteCnt, srcNetNodeId );
  489. break;
  490. case kMidiMsgArraySelAsId:
  491. {
  492. cmMidiPacket_t pkt;
  493. cmDspValue_t v;
  494. pkt.cbDataPtr = NULL;
  495. pkt.devIdx = hdr[2];
  496. pkt.portIdx = hdr[3];
  497. pkt.msgCnt = hdr[4];
  498. pkt.msgArray = (cmMidiMsg*)(hdr + 5);
  499. unsigned midiSymId = cmDspSysRegisterStaticSymbol(h,"_midi");
  500. v.u.m.u.vp = &pkt;
  501. cmDspSysBroadcastValue(h, midiSymId, &v);
  502. /*
  503. // data format for MIDI messages
  504. { kMidiMsgArraytSelAsId, devIdx, portIdx, msgCnt, msgArray[msgCnt] }
  505. where each msgArray[] record is a cmMidiMsg record.
  506. */
  507. }
  508. break;
  509. }
  510. }
  511. // report any error - this should stop further calls to the DSP process
  512. return cmErrLastRC(&p->err);
  513. }
  514. unsigned cmDspSysSyncState( cmDspSysH_t h )
  515. {
  516. cmDsp_t* p = _cmDspHandleToPtr(h);
  517. return p->syncState;
  518. }
  519. unsigned cmDspSysPresetGroupCount( cmDspSysH_t h )
  520. {
  521. cmDsp_t* p = _cmDspHandleToPtr(h);
  522. return _cmDspPresetGroupCount(&p->pm);
  523. }
  524. unsigned cmDspSysPresetGroupSymId( cmDspSysH_t h, unsigned groupIdx )
  525. {
  526. cmDsp_t* p = _cmDspHandleToPtr(h);
  527. return _cmDspPresetGroupSymId(&p->pm,groupIdx);
  528. }
  529. const cmChar_t* cmDspSysPresetGroupLabel( cmDspSysH_t h, unsigned groupIdx )
  530. {
  531. cmDsp_t* p = _cmDspHandleToPtr(h);
  532. return _cmDspPresetGroupLabel(&p->pm,groupIdx);
  533. }
  534. cmDspRC_t cmDspSysPresetGroupJsonList( cmDspSysH_t h, cmJsonH_t* jsHPtr )
  535. {
  536. cmDsp_t* p = _cmDspHandleToPtr(h);
  537. return _cmDspPresetGroupJsonList(&p->pm,jsHPtr);
  538. }
  539. unsigned cmDspSysPresetPresetCount( cmDspSysH_t h, unsigned groupIdx )
  540. {
  541. cmDsp_t* p = _cmDspHandleToPtr(h);
  542. return _cmDspPresetPresetCount(&p->pm,groupIdx);
  543. }
  544. unsigned cmDspSysPresetPresetSymId( cmDspSysH_t h, unsigned groupIdx, unsigned presetIdx )
  545. {
  546. cmDsp_t* p = _cmDspHandleToPtr(h);
  547. return _cmDspPresetPresetSymId(&p->pm,groupIdx,presetIdx);
  548. }
  549. const cmChar_t* cmDspSysPresetPresetLabel( cmDspSysH_t h, unsigned groupIdx, unsigned presetIdx )
  550. {
  551. cmDsp_t* p = _cmDspHandleToPtr(h);
  552. return _cmDspPresetPresetLabel(&p->pm,groupIdx,presetIdx);
  553. }
  554. cmDspRC_t cmDspSysPresetPresetJsonList( cmDspSysH_t h, unsigned groupSymId, cmJsonH_t* jsHPtr )
  555. {
  556. cmDsp_t* p = _cmDspHandleToPtr(h);
  557. return _cmDspPresetPresetJsonList(&p->pm,jsHPtr,groupSymId);
  558. }
  559. unsigned cmDspSysPresetRegisterGroup( cmDspSysH_t h, const cmChar_t* groupLabel )
  560. {
  561. cmDsp_t* p = _cmDspHandleToPtr(h);
  562. assert( groupLabel != NULL );
  563. unsigned grpSymId;
  564. if((grpSymId = cmSymTblRegisterSymbol(p->ctx.stH,groupLabel)) == cmInvalidIdx )
  565. cmErrMsg(&p->err,kSymTblFailDspRC,"Registration of the group label:'%s' failed.",cmStringNullGuard(groupLabel));
  566. return grpSymId;
  567. }
  568. cmDspRC_t cmDspSysPresetCreate( cmDspSysH_t h, const cmChar_t* groupLabel, const cmChar_t* presetLabel )
  569. {
  570. cmDspRC_t rc;
  571. cmDsp_t* p = _cmDspHandleToPtr(h);
  572. // create a new preset and make it active
  573. if((rc = _cmDspPresetCreatePreset(&p->pm, groupLabel, presetLabel )) != kOkDspRC )
  574. return rc;
  575. assert( p->pm.gp != NULL );
  576. // call store on each of the instances
  577. _cmDspInst_t* ip = p->instList;
  578. for(; ip != NULL; ip = ip->linkPtr )
  579. if( ip->instPtr->symId != cmInvalidId && ip->instPtr->presetGroupSymId == p->pm.gp->symId && ip->instPtr->storeFunc != NULL )
  580. {
  581. // create an instance preset and make it active
  582. if((rc = _cmDspPresetCreateInstance(&p->pm, ip->instPtr->symId )) != kOkDspRC )
  583. return rc;
  584. // call the store function on each instance
  585. if( ip->instPtr->storeFunc(&p->ctx,ip->instPtr,true) != kOkDspRC )
  586. rc = cmErrMsg(&p->err,kInstStoreFailDspRC,"Save failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  587. }
  588. return rc;
  589. }
  590. cmDspRC_t cmDspSysPresetRecall( cmDspSysH_t h, const cmChar_t* groupLabel, const cmChar_t* presetLabel )
  591. {
  592. cmDspRC_t rc;
  593. cmDsp_t* p = _cmDspHandleToPtr(h);
  594. // make the given group/preset active
  595. if((rc = _cmDspPresetRecallPreset(&p->pm, groupLabel, presetLabel )) != kOkDspRC )
  596. return rc;
  597. assert( p->pm.gp != NULL );
  598. // call store on each of the instances
  599. _cmDspInst_t* ip = p->instList;
  600. for(; ip != NULL; ip = ip->linkPtr )
  601. if( ip->instPtr->symId != cmInvalidId && ip->instPtr->storeFunc != NULL && ip->instPtr->presetGroupSymId == p->pm.gp->symId )
  602. {
  603. // make the instance active
  604. if((rc = _cmDspPresetRecallInstance(&p->pm, ip->instPtr->symId )) != kOkDspRC )
  605. {
  606. if( rc == kPresetInstNotFoundDspRC )
  607. {
  608. cmErrWarnMsg(&p->err,kOkDspRC,"Assuming a new instance was added to the preset - continuing with preset load.");
  609. continue;
  610. }
  611. }
  612. // call the store function on each instance
  613. if( ip->instPtr->storeFunc(&p->ctx,ip->instPtr,false) != kOkDspRC )
  614. rc = cmErrMsg(&p->err,kInstStoreFailDspRC,"Restore failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  615. }
  616. return rc;
  617. }
  618. cmDspRC_t cmDspSysPresetWriteValue( cmDspSysH_t h, unsigned varSymId, const cmDspValue_t* valPtr )
  619. {
  620. cmDsp_t* p = _cmDspHandleToPtr(h);
  621. return _cmDspPresetCreateVar(&p->pm,varSymId,valPtr);
  622. }
  623. cmDspRC_t cmDspSysPresetReadValue( cmDspSysH_t h, unsigned varSymId, cmDspValue_t* valPtr )
  624. {
  625. cmDsp_t* p = _cmDspHandleToPtr(h);
  626. return _cmDspPresetRecallVar(&p->pm,varSymId,valPtr);
  627. }
  628. // allocate an instance given the class symbol id
  629. cmDspInst_t* _cmDspSysAllocateInst( cmDsp_t* p, unsigned classSymId, unsigned storeSymId, unsigned instSymId, unsigned va_cnt, va_list vl )
  630. {
  631. _cmDspClass_t* cp = p->classList;
  632. cmDspInst_t* instPtr = NULL;
  633. // locate the DSP class
  634. for(; cp != NULL; cp=cp->linkPtr )
  635. if( cp->symId == classSymId )
  636. break;
  637. // if the DSP class was not found
  638. if( cp == NULL )
  639. {
  640. cmErrMsg(&p->err,kClassNotFoundDspRC,"The DSP class '%s' could not be found.",cmSymTblLabel(p->ctx.stH,classSymId));
  641. return NULL;
  642. }
  643. // allocate the instance
  644. if((instPtr = cp->classPtr->allocFunc( &p->ctx, cp->classPtr, storeSymId, instSymId, p->nextInstId, va_cnt, vl )) == NULL )
  645. {
  646. cmErrMsg(&p->err,kAllocInstFailDspRC,"DSP instance allocation failed for class '%s'.",cp->classPtr->labelStr);
  647. return NULL;
  648. }
  649. // assign the symbol attributes
  650. cmDspInstSymId_t* ip = p->symIdList;
  651. for(; ip!=NULL; ip=ip->link)
  652. if( ip->symId != cmInvalidId )
  653. if( cmDspInstRegisterAttrSym(&p->ctx, instPtr, ip->symId ) != kOkDspRC )
  654. {
  655. cmErrMsg(&p->err,kAllocInstFailDspRC,"The DSP instance failed due to attribute symbol registration failure.");
  656. return NULL;
  657. }
  658. // allocate the instance ctl recd
  659. _cmDspInst_t* instCtlPtr = cmLhAllocZ( p->ctx.lhH, _cmDspInst_t, 1 );
  660. assert( instCtlPtr != NULL );
  661. // setup the inst ctl recd
  662. instCtlPtr->instPtr = instPtr;
  663. instCtlPtr->linkPtr = NULL;
  664. // link the inst ctl recd onto the end of the instance list
  665. if( p->instList == NULL )
  666. p->instList = instCtlPtr;
  667. else
  668. {
  669. _cmDspInst_t* ip = p->instList;
  670. while( ip->linkPtr != NULL )
  671. ip = ip->linkPtr;
  672. ip->linkPtr = instCtlPtr;
  673. }
  674. ++p->nextInstId;
  675. return instPtr;
  676. }
  677. // allocate a an instance from the class label - this function calls _cmDspSysAllocateInst()
  678. cmDspInst_t* cmDspSysAllocInstSV( cmDspSysH_t h, const cmChar_t* classLabelStr, unsigned storeSymId, const cmChar_t* instLabelStr, unsigned va_cnt, va_list vl)
  679. {
  680. cmDsp_t* p = _cmDspHandleToPtr(h);
  681. unsigned instSymId = cmInvalidId;
  682. cmDspInst_t* newInstPtr;
  683. unsigned classSymId;
  684. // get the class symbold
  685. if((classSymId = cmSymTblId(p->ctx.stH,classLabelStr)) == cmInvalidId )
  686. {
  687. cmErrMsg(&p->err,kSymNotFoundDspRC,"The symbol id associated with the class '%s' could not be found.",classLabelStr);
  688. return NULL;
  689. }
  690. if( instLabelStr != NULL )
  691. {
  692. // get the symbold id assoc'd with the instance label
  693. if((instSymId = cmSymTblRegisterSymbol(p->ctx.stH,instLabelStr)) == cmInvalidId )
  694. {
  695. cmErrMsg(&p->err,kSymTblFailDspRC,"The DSP instance symbol '%s' failed to register with the symbol table.",instLabelStr);
  696. return NULL;
  697. }
  698. // do not allow instance labels to be re-used
  699. if( _cmDspSysInstSymIdToPtr(p,instSymId) != NULL )
  700. {
  701. cmErrMsg(&p->err,kDuplInstSymIdDspRC,"The DSP instance label '%s' was reused (class:'%s').",cmStringNullGuard(instLabelStr),cmStringNullGuard(classLabelStr));
  702. return NULL;
  703. }
  704. }
  705. // allocate the instance
  706. newInstPtr = _cmDspSysAllocateInst(p, classSymId, storeSymId, instSymId, va_cnt, vl );
  707. return newInstPtr;
  708. }
  709. cmDspInst_t* cmDspSysAllocInstS( cmDspSysH_t h, const cmChar_t* classLabelStr, unsigned storeSymId, const cmChar_t* instLabelStr, unsigned va_cnt, ... )
  710. {
  711. cmDspInst_t* p;
  712. va_list vl;
  713. va_start(vl,va_cnt);
  714. p = cmDspSysAllocInstSV(h, classLabelStr, storeSymId, instLabelStr, va_cnt, vl);
  715. va_end(vl);
  716. return p;
  717. }
  718. cmDspInst_t* cmDspSysAllocInst( cmDspSysH_t h, const cmChar_t* classLabelStr, const cmChar_t* instLabelStr, unsigned va_cnt, ... )
  719. {
  720. cmDspInst_t* p;
  721. va_list vl;
  722. va_start(vl,va_cnt);
  723. p = cmDspSysAllocInstSV(h, classLabelStr, cmInvalidId, instLabelStr, va_cnt, vl);
  724. va_end(vl);
  725. return p;
  726. }
  727. cmDspInst_t** cmDspSysAllocInstArraySV( cmDspSysH_t h, unsigned cnt, unsigned presetGroupSymId, const cmChar_t* classLabelStr, const cmChar_t* instLabelStr, cmDspSysLabelFunc_t labelFunc, unsigned va_cnt, va_list vl )
  728. {
  729. cmDsp_t* p = _cmDspHandleToPtr(h);
  730. cmDspInst_t** a = cmLhAllocZ( p->lhH, cmDspInst_t*, cnt );
  731. unsigned i;
  732. for(i=0; i<cnt; ++i)
  733. {
  734. const cmChar_t* label = instLabelStr;
  735. va_list vl1;
  736. va_copy(vl1,vl);
  737. if( labelFunc != NULL )
  738. label = labelFunc(h,i);
  739. else
  740. if( instLabelStr != NULL )
  741. label = cmDspSysPrintLabel(instLabelStr,i);
  742. a[i] = cmDspSysAllocInstSV(h,classLabelStr,presetGroupSymId,label,va_cnt,vl1);
  743. }
  744. return a;
  745. }
  746. cmDspInst_t** cmDspSysAllocInstArrayS( cmDspSysH_t h, unsigned cnt, unsigned presetGroupSymId, const cmChar_t* classLabelStr, const cmChar_t* instLabelStr, cmDspSysLabelFunc_t labelFunc, unsigned va_cnt, ... )
  747. {
  748. va_list vl;
  749. va_start(vl,va_cnt);
  750. cmDspInst_t** a = cmDspSysAllocInstArraySV(h,cnt,presetGroupSymId,classLabelStr,instLabelStr,labelFunc,va_cnt,vl);
  751. va_end(vl);
  752. return a;
  753. }
  754. cmDspInst_t** cmDspSysAllocInstArray( cmDspSysH_t h, unsigned cnt, const cmChar_t* classLabelStr, const cmChar_t* instLabelStr, cmDspSysLabelFunc_t labelFunc, unsigned va_cnt, ... )
  755. {
  756. va_list vl;
  757. va_start(vl,va_cnt);
  758. cmDspInst_t** a = cmDspSysAllocInstArraySV(h,cnt,cmInvalidId,classLabelStr,instLabelStr,labelFunc,va_cnt,vl);
  759. va_end(vl);
  760. return a;
  761. }
  762. cmDspRC_t cmDspSysNewColumn( cmDspSysH_t h, unsigned colW )
  763. { return cmDspUiNewColumn(&_cmDspHandleToPtr(h)->ctx,colW); }
  764. cmDspRC_t cmDspSysInsertHorzBorder( cmDspSysH_t h )
  765. { return cmDspUiInsertHorzBorder(&_cmDspHandleToPtr(h)->ctx); }
  766. cmDspRC_t cmDspSysNewPage( cmDspSysH_t h, const cmChar_t* title )
  767. { return cmDspUiNewPage(&_cmDspHandleToPtr(h)->ctx,title); }
  768. cmDspRC_t _cmDspSysConnAudio( cmDsp_t* p, cmDspInst_t* srcInstPtr, unsigned srcVarSymId, cmDspInst_t* dstInstPtr, unsigned dstVarSymId )
  769. {
  770. cmDspVar_t* srcVarPtr;
  771. cmDspVar_t* dstVarPtr;
  772. //cmSample_t* srcDataPtr;
  773. if( srcInstPtr == NULL )
  774. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The audio connection process was passed a NULL source processor instance pointer for connection to '%s'.",cmStringNullGuard(dstInstPtr->classPtr->labelStr));
  775. if( dstInstPtr == NULL )
  776. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The audio connection process was passed a NULL destination processor instance pointer for connection from '%s'.",cmStringNullGuard(srcInstPtr->classPtr->labelStr));
  777. // locate the src audio variable
  778. if((srcVarPtr = cmDspVarSymbolToPtr( &p->ctx, srcInstPtr, srcVarSymId, kOutDsvFl )) == NULL )
  779. return cmErrMsg(&p->err,kVarNotFoundDspRC,"Audio connection failed. The source variable '%s' could not be found in DSP instance '%s' id=%i.", cmStringNullGuard(cmSymTblLabel(p->ctx.stH,srcVarSymId)), cmStringNullGuard(srcInstPtr->classPtr->labelStr),srcInstPtr->id);
  780. // validate the src variable type
  781. if( cmIsFlag(srcVarPtr->flags,kAudioBufDsvFl) == false )
  782. return cmErrMsg(&p->err,kVarTypeErrDspRC,"Audio connection failed. The source variable '%s' in '%s' id=%i was not an audio variable.", cmStringNullGuard(cmSymTblLabel(p->ctx.stH,srcVarSymId)), cmStringNullGuard(srcInstPtr->classPtr->labelStr),srcInstPtr->id);
  783. // locate the dst audio variable
  784. if((dstVarPtr = cmDspVarSymbolToPtr( &p->ctx, dstInstPtr, dstVarSymId, kInDsvFl )) == NULL )
  785. return cmErrMsg(&p->err,kVarNotFoundDspRC,"Audio connection failed. The destination variable '%s' could not be found in DSP instance '%s' id=%i.", cmStringNullGuard(cmSymTblLabel(p->ctx.stH,dstVarSymId)), cmStringNullGuard(dstInstPtr->classPtr->labelStr),dstInstPtr->id);
  786. // validate the dst variable type
  787. if( cmIsFlag(dstVarPtr->flags,kAudioBufDsvFl) == false )
  788. return cmErrMsg(&p->err,kVarTypeErrDspRC,"Audio connection failed. The destination variable '%s' in '%s' id=%i was not an audio variable.", cmStringNullGuard(cmSymTblLabel(p->ctx.stH,dstVarSymId)), cmStringNullGuard(dstInstPtr->classPtr->labelStr),dstInstPtr->id);
  789. // get a pointer to the source sample buffer
  790. //if( (srcDataPtr = cmDsvSampleMtx( &srcVarPtr->value )) == NULL )
  791. // return cmErrMsg(&p->err,kVarNotValidDspRC,"Audio connection failed. The audio source variable '%s' in '%s' id=%i has not been allocated.",cmSymTblLabel(p->ctx.stH,srcVarSymId), srcInstPtr->classPtr->labelStr,srcInstPtr->id);
  792. // set the destination sample buffer to point to the source sample buffer.
  793. //cmDsvSetSampleMtx( &dstVarPtr->value, srcDataPtr, cmDsvRows(&srcVarPtr->value), cmDsvCols(&srcVarPtr->value));
  794. cmDsvSetProxy( &dstVarPtr->value, &srcVarPtr->value );
  795. return kOkDspRC;
  796. }
  797. cmDspRC_t cmDspSysConnectAudio( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLbl, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLbl )
  798. {
  799. cmDsp_t* p = _cmDspHandleToPtr(h);
  800. unsigned srcSymId, dstSymId;
  801. if( srcInstPtr == NULL && dstInstPtr == NULL )
  802. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The audio connection process was passed a NULL source and destination processor instance.");
  803. if( srcInstPtr == NULL )
  804. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The audio connection process was passed a NULL source processor instance pointer for connection to '%s'.",cmStringNullGuard(dstInstPtr->classPtr->labelStr));
  805. if( dstInstPtr == NULL )
  806. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The audio connection process was passed a NULL destination processor instance pointer for connection from '%s'.",cmStringNullGuard(srcInstPtr->classPtr->labelStr));
  807. if((srcSymId = cmSymTblId(p->ctx.stH,srcVarLbl)) == cmInvalidId )
  808. return cmErrMsg(&p->err,kSymNotFoundDspRC,"Audio connection failed. The source variable symbol '%s' for source DSP instance '%s' id=%i could not be found.",cmStringNullGuard(srcVarLbl),cmStringNullGuard(srcInstPtr->classPtr->labelStr),srcInstPtr->id);
  809. if((dstSymId = cmSymTblId(p->ctx.stH,dstVarLbl)) == cmInvalidId )
  810. return cmErrMsg(&p->err,kSymNotFoundDspRC,"Audio connection failed. The destination variable symbol '%s' for source DSP instance '%s' id=%i could not be found.",cmStringNullGuard(dstVarLbl),cmStringNullGuard(dstInstPtr->classPtr->labelStr),dstInstPtr->id);
  811. return _cmDspSysConnAudio(p,srcInstPtr,srcSymId,dstInstPtr,dstSymId);
  812. }
  813. cmDspRC_t cmDspSysConnectAudioN11N( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  814. {
  815. cmDspRC_t rc = kOkDspRC;
  816. unsigned i;
  817. for(i=0; i<n; ++i)
  818. {
  819. const cmChar_t* dstVarStr = dstVarPrefixStr;
  820. if( n >= 2 )
  821. dstVarStr = cmDspSysPrintLabel(dstVarPrefixStr,i);
  822. if((rc = cmDspSysConnectAudio( h, srcInstArray[i], srcVarLabel, dstInstPtr, dstVarStr )) != kOkDspRC )
  823. break;
  824. }
  825. return rc;
  826. }
  827. cmDspRC_t cmDspSysConnectAudio1NN1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  828. {
  829. cmDspRC_t rc = kOkDspRC;
  830. unsigned i;
  831. for(i=0; i<n; ++i)
  832. {
  833. const cmChar_t* label = srcVarPrefixStr;
  834. if( n >= 2 )
  835. label = cmDspSysPrintLabel(srcVarPrefixStr,i);
  836. if((rc = cmDspSysConnectAudio( h, srcInstPtr, label, dstInstArray[i], dstVarLabel )) != kOkDspRC )
  837. break;
  838. }
  839. return rc;
  840. }
  841. cmDspRC_t cmDspSysConnectAudio1N1N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  842. {
  843. cmDspRC_t rc = kOkDspRC;
  844. unsigned i;
  845. for(i=0; i<n; ++i)
  846. {
  847. const cmChar_t* label0 = cmDspSysPrintLabel(srcVarPrefixStr,i);
  848. const cmChar_t* label1 = cmDspSysPrintLabel2(dstVarPrefixStr,i);
  849. if((rc = cmDspSysConnectAudio( h, srcInstPtr, label0, dstInstPtr, label1 )) != kOkDspRC )
  850. break;
  851. }
  852. return rc;
  853. }
  854. cmDspRC_t cmDspSysConnectAudioN1N1( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  855. {
  856. cmDspRC_t rc = kOkDspRC;
  857. unsigned i;
  858. for(i=0; i<n; ++i)
  859. {
  860. if((rc = cmDspSysConnectAudio( h, srcInstArray[i], srcVarLabel, dstInstArray[i], dstVarLabel )) != kOkDspRC )
  861. break;
  862. }
  863. return rc;
  864. }
  865. cmDspRC_t cmDspSysConnectAudio11N1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[],const cmChar_t* dstVarLabel, unsigned n )
  866. {
  867. cmDspRC_t rc = kOkDspRC;
  868. unsigned i;
  869. for(i=0; i<n; ++i)
  870. {
  871. if((rc = cmDspSysConnectAudio( h, srcInstPtr, srcVarLabel, dstInstArray[i], dstVarLabel )) != kOkDspRC )
  872. break;
  873. }
  874. return rc;
  875. }
  876. cmDspRC_t cmDspSysConnectAudio111N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, unsigned n )
  877. {
  878. cmDspRC_t rc = kOkDspRC;
  879. unsigned i;
  880. for(i=0; i<n; ++i)
  881. {
  882. const cmChar_t* label1 = cmDspSysPrintLabel(dstVarLabel,i);
  883. const cmChar_t* label = n>1 ? label1 : dstVarLabel;
  884. if((rc = cmDspSysConnectAudio( h, srcInstPtr, srcVarLabel, dstInstPtr, label )) != kOkDspRC )
  885. break;
  886. }
  887. return rc;
  888. }
  889. cmDspRC_t cmDspSysConnectAudioN11NM( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, unsigned srcCnt, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned dstCnt, const unsigned* map )
  890. {
  891. cmDsp_t* p = _cmDspHandleToPtr(h);
  892. cmDspRC_t rc = kOkDspRC;
  893. unsigned i;
  894. for(i=0; i<dstCnt; ++i)
  895. {
  896. const cmChar_t* dstVarStr = dstVarPrefixStr;
  897. if( dstCnt >= 2 )
  898. dstVarStr = cmDspSysPrintLabel(dstVarPrefixStr,i);
  899. if( map[i] >= srcCnt )
  900. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The source instance map connection index %i is of range %i.",i,srcCnt);
  901. if((rc = cmDspSysConnectAudio( h, srcInstArray[ map[i] ], srcVarLabel, dstInstPtr, dstVarStr )) != kOkDspRC )
  902. break;
  903. }
  904. return rc;
  905. }
  906. cmDspRC_t cmDspSysInstallCb( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, void* dstCbDataPtr)
  907. {
  908. cmDsp_t* p = _cmDspHandleToPtr(h);
  909. return cmDspInstallCb(&p->ctx,srcInstPtr,srcVarLabel,dstInstPtr,dstVarLabel,dstCbDataPtr);
  910. }
  911. cmDspRC_t cmDspSysInstallCbN11N( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  912. {
  913. cmDspRC_t rc = kOkDspRC;
  914. unsigned i;
  915. for(i=0; i<n; ++i)
  916. {
  917. const cmChar_t* dstVarStr = dstVarPrefixStr;
  918. if( n >= 2 )
  919. dstVarStr = cmDspSysPrintLabel(dstVarPrefixStr,i);
  920. if((rc = cmDspSysInstallCb( h, srcInstArray[i], srcVarLabel, dstInstPtr, dstVarStr, NULL )) != kOkDspRC )
  921. break;
  922. }
  923. return rc;
  924. }
  925. cmDspRC_t cmDspSysInstallCb1NN1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  926. {
  927. cmDspRC_t rc = kOkDspRC;
  928. unsigned i;
  929. for(i=0; i<n; ++i)
  930. {
  931. const cmChar_t* srcVarStr = srcVarPrefixStr;
  932. if( n >= 2 )
  933. srcVarStr = cmDspSysPrintLabel(srcVarPrefixStr,i);
  934. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  935. break;
  936. }
  937. return rc;
  938. }
  939. cmDspRC_t cmDspSysInstallCb1N1N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  940. {
  941. cmDspRC_t rc = kOkDspRC;
  942. unsigned i;
  943. for(i=0; i<n; ++i)
  944. {
  945. const cmChar_t* label0 = srcVarPrefixStr;
  946. const cmChar_t* label1 = dstVarPrefixStr;
  947. if( n >= 2 )
  948. {
  949. label0 = cmDspSysPrintLabel(srcVarPrefixStr,i);
  950. label1 = cmDspSysPrintLabel2(dstVarPrefixStr,i);
  951. }
  952. if((rc = cmDspSysInstallCb( h, srcInstPtr, label0, dstInstPtr, label1, NULL )) != kOkDspRC )
  953. break;
  954. }
  955. return rc;
  956. }
  957. cmDspRC_t cmDspSysInstallCbN1N1( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  958. {
  959. cmDspRC_t rc = kOkDspRC;
  960. unsigned i;
  961. for(i=0; i<n; ++i)
  962. {
  963. if((rc = cmDspSysInstallCb( h, srcInstArray[i], srcVarLabel, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  964. break;
  965. }
  966. return rc;
  967. }
  968. cmDspRC_t cmDspSysInstallCb11N1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  969. {
  970. cmDspRC_t rc = kOkDspRC;
  971. unsigned i;
  972. for(i=0; i<n; ++i)
  973. {
  974. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarLabel, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  975. break;
  976. }
  977. return rc;
  978. }
  979. cmDspRC_t cmDspSysInstallCb111N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixLabel, unsigned n )
  980. {
  981. cmDspRC_t rc = kOkDspRC;
  982. unsigned i;
  983. for(i=0; i<n; ++i)
  984. {
  985. const cmChar_t* dstVarStr = dstVarPrefixLabel;
  986. if( n >= 2 )
  987. dstVarStr = cmDspSysPrintLabel(dstVarPrefixLabel,i);
  988. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarLabel, dstInstPtr, dstVarStr, NULL )) != kOkDspRC )
  989. break;
  990. }
  991. return rc;
  992. }
  993. cmDspRC_t cmDspSysInstallCbN111( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, unsigned n )
  994. {
  995. cmDspRC_t rc = kOkDspRC;
  996. unsigned i;
  997. for(i=0; i<n; ++i)
  998. {
  999. if((rc = cmDspSysInstallCb( h, srcInstArray[i], srcVarLabel, dstInstPtr, dstVarLabel, NULL )) != kOkDspRC )
  1000. break;
  1001. }
  1002. return rc;
  1003. }
  1004. cmDspRC_t cmDspSysInstallCb1N11( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, unsigned n )
  1005. {
  1006. cmDspRC_t rc = kOkDspRC;
  1007. unsigned i;
  1008. for(i=0; i<n; ++i)
  1009. {
  1010. const cmChar_t* srcVarStr = srcVarPrefixLabel;
  1011. if( n >= 2 )
  1012. srcVarStr = cmDspSysPrintLabel(srcVarPrefixLabel,i);
  1013. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstPtr, dstVarLabel, NULL )) != kOkDspRC )
  1014. break;
  1015. }
  1016. return rc;
  1017. }
  1018. cmDspRC_t cmDspSysInstallCb1N1NM( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, unsigned srcCnt, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned dstCnt, const unsigned* map )
  1019. {
  1020. cmDsp_t* p = _cmDspHandleToPtr(h);
  1021. cmDspRC_t rc = kOkDspRC;
  1022. unsigned i;
  1023. for(i=0; i<dstCnt; ++i)
  1024. {
  1025. const cmChar_t* label0 = srcVarPrefixStr;
  1026. const cmChar_t* label1 = dstVarPrefixStr;
  1027. if( map[i] >= srcCnt )
  1028. {
  1029. if( map[i] >= srcCnt )
  1030. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The source instance map connection index %i is of range %i.",i,srcCnt);
  1031. }
  1032. if( srcCnt >= 2 )
  1033. label0 = cmDspSysPrintLabel(srcVarPrefixStr,map[i]);
  1034. if( dstCnt >= 2 )
  1035. label1 = cmDspSysPrintLabel2(dstVarPrefixStr,i);
  1036. if((rc = cmDspSysInstallCb( h, srcInstPtr, label0, dstInstPtr, label1, NULL )) != kOkDspRC )
  1037. break;
  1038. }
  1039. return rc;
  1040. }
  1041. cmDspRC_t cmDspSysInstallCb1NN1M( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, unsigned srcCnt, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned dstCnt, const unsigned* map )
  1042. {
  1043. cmDsp_t* p = _cmDspHandleToPtr(h);
  1044. cmDspRC_t rc = kOkDspRC;
  1045. unsigned i;
  1046. for(i=0; i<dstCnt; ++i)
  1047. {
  1048. if( map[i] >= srcCnt )
  1049. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The source instance map connection index %i is of range %i.",i,srcCnt);
  1050. const cmChar_t* srcVarStr = srcVarPrefixStr;
  1051. if( srcCnt >= 2 )
  1052. srcVarStr = cmDspSysPrintLabel(srcVarPrefixStr,map[i]);
  1053. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  1054. break;
  1055. }
  1056. return rc;
  1057. }
  1058. cmDspRC_t cmDspSysInstallCb1NN1M2( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, unsigned srcCnt, const unsigned* map, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned dstCnt )
  1059. {
  1060. cmDsp_t* p = _cmDspHandleToPtr(h);
  1061. cmDspRC_t rc = kOkDspRC;
  1062. unsigned i;
  1063. for(i=0; i<srcCnt; ++i)
  1064. {
  1065. if( map[i] >= dstCnt )
  1066. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The dest. instance map connection index %i is of range %i.",map[i],dstCnt);
  1067. const cmChar_t* srcVarStr = srcVarPrefixStr;
  1068. if( srcCnt >= 2 )
  1069. srcVarStr = cmDspSysPrintLabel(srcVarPrefixStr,i);
  1070. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstArray[ map[i] ], dstVarLabel, NULL )) != kOkDspRC )
  1071. break;
  1072. }
  1073. return rc;
  1074. }
  1075. double cmDspSysSampleRate( cmDspSysH_t h )
  1076. {
  1077. cmDsp_t* p = _cmDspHandleToPtr(h);
  1078. return p->ctx.ctx->ss->args.srate;
  1079. }
  1080. cmJsonH_t cmDspSysPgmRsrcHandle( cmDspSysH_t h )
  1081. {
  1082. cmDsp_t* p = _cmDspHandleToPtr(h);
  1083. return p->ctx.rsrcJsH;
  1084. }
  1085. cmSymTblH_t cmDspSysSymbolTable( cmDspSysH_t h )
  1086. {
  1087. cmDsp_t* p = _cmDspHandleToPtr(h);
  1088. return p->ctx.stH;
  1089. }
  1090. unsigned cmDspSysRegisterStaticSymbol( cmDspSysH_t h, const cmChar_t* symLabel )
  1091. { return cmSymTblRegisterStaticSymbol( cmDspSysSymbolTable(h), symLabel ); }
  1092. unsigned cmDspSysRegisterSymbol( cmDspSysH_t h, const cmChar_t* symLabel )
  1093. { return cmSymTblRegisterSymbol( cmDspSysSymbolTable(h), symLabel); }
  1094. cmCtx_t* cmDspSysPgmCtx( cmDspSysH_t h )
  1095. {
  1096. cmDsp_t* p = _cmDspHandleToPtr(h);
  1097. return &p->cmCtx;
  1098. }
  1099. cmLHeapH_t cmDspSysLHeap( cmDspSysH_t h )
  1100. {
  1101. cmDsp_t* p = _cmDspHandleToPtr(h);
  1102. return p->lhH;
  1103. }
  1104. unsigned cmDspSysNetNodeId( cmDspSysH_t h )
  1105. {
  1106. cmDsp_t* p = _cmDspHandleToPtr(h);
  1107. return cmUdpNetLocalNodeId(p->netH);
  1108. }
  1109. const cmChar_t* cmDspSysNetNodeLabel( cmDspSysH_t h )
  1110. {
  1111. cmDsp_t* p = _cmDspHandleToPtr(h);
  1112. return cmUdpNetLocalNodeLabel(p->netH);
  1113. }
  1114. const cmChar_t* cmDspSysNetNodeIdToLabel( cmDspSysH_t h, unsigned netNodeId )
  1115. {
  1116. cmDsp_t* p = _cmDspHandleToPtr(h);
  1117. return cmUdpNetNodeIdToLabel(p->netH,netNodeId);
  1118. }
  1119. unsigned cmDspSysNetNodeLabelToId( cmDspSysH_t h, const cmChar_t* netNodeLabel )
  1120. {
  1121. cmDsp_t* p = _cmDspHandleToPtr(h);
  1122. return cmUdpNetNodeLabelToId(p->netH,netNodeLabel);
  1123. }
  1124. cmDspInstSymId_t* _cmDspSysFindInstSymId( cmDsp_t* p, unsigned symId )
  1125. {
  1126. cmDspInstSymId_t* ip = p->symIdList;
  1127. for(; ip != NULL; ip=ip->link)
  1128. if( ip->symId == symId )
  1129. return ip;
  1130. return NULL;
  1131. }
  1132. unsigned cmDspSysRegisterInstAttrSymbol( cmDspSysH_t h, unsigned symId )
  1133. {
  1134. cmDsp_t* p = _cmDspHandleToPtr(h);
  1135. cmDspInstSymId_t* ip;
  1136. // if this symbol is already registered
  1137. if((ip = _cmDspSysFindInstSymId(p,symId)) != NULL )
  1138. return ip->symId;
  1139. // try to find an unused symbol
  1140. ip = p->symIdList;
  1141. for(; ip != NULL; ip=ip->link)
  1142. if( ip->symId == cmInvalidId )
  1143. break;
  1144. // if no unused symbols were found then allocate a new one
  1145. if( ip == NULL )
  1146. {
  1147. ip = cmLhAllocZ(p->ctx.lhH, cmDspInstSymId_t, 1 );
  1148. ip->link = p->symIdList;
  1149. p->symIdList = ip;
  1150. }
  1151. ip->symId = symId;
  1152. return ip->symId;
  1153. }
  1154. unsigned cmDspSysRegisterInstAttrSymbolStr( cmDspSysH_t h, const cmChar_t* symLabel )
  1155. {
  1156. cmDsp_t* p = _cmDspHandleToPtr(h);
  1157. unsigned symId;
  1158. if((symId = cmSymTblRegisterSymbol(p->ctx.stH,symLabel)) == cmInvalidId )
  1159. return cmErrMsg(&p->err,kSymTblFailDspRC,"The instance attribute symbol '%s' could not be registered.",cmStringNullGuard(symLabel));
  1160. return cmDspSysRegisterInstAttrSymbol(h,symId);
  1161. }
  1162. cmDspRC_t cmDspSysAssignInstAttrSymbol( cmDspSysH_t h, cmDspInst_t* inst, unsigned symId )
  1163. {
  1164. cmDsp_t* p = _cmDspHandleToPtr(h);
  1165. return cmDspInstRegisterAttrSym(&p->ctx, inst, symId) != kOkDspRC;
  1166. }
  1167. unsigned cmDspSysAssignInstAttrSymbolStr( cmDspSysH_t h, cmDspInst_t* inst, const cmChar_t* symLabel )
  1168. {
  1169. unsigned symId = cmDspSysRegisterSymbol(h,symLabel);
  1170. if( cmDspSysAssignInstAttrSymbol( h, inst, symId) != kOkDspRC )
  1171. return cmInvalidId;
  1172. return symId;
  1173. }
  1174. cmDspRC_t cmDspSysRemoveInstAttrSymbol( cmDspSysH_t h, unsigned symId )
  1175. {
  1176. cmDsp_t* p = _cmDspHandleToPtr(h);
  1177. cmDspInstSymId_t* ip;
  1178. // if this symbol is already registered
  1179. if((ip = _cmDspSysFindInstSymId(p,symId)) == NULL )
  1180. return cmErrMsg(&p->err,kOkDspRC,"The instance attribute symbol '%s' is could not be found to be removed.",cmStringNullGuard(cmSymTblLabel(p->ctx.stH,symId)));
  1181. // mark a symbol recd as inactive by setting the symbol id to cmInalidId
  1182. ip->symId = cmInvalidId;
  1183. return kOkDspRC;
  1184. }
  1185. cmDspRC_t cmDspSysRemoveInstAttrSymbolStr( cmDspSysH_t h, const cmChar_t* symLabel )
  1186. {
  1187. cmDsp_t* p = _cmDspHandleToPtr(h);
  1188. unsigned symId;
  1189. if( (symId = cmSymTblId(p->ctx.stH, symLabel )) == cmInvalidId )
  1190. return cmErrMsg(&p->err,kSymNotFoundDspRC,"The instance attribute symbol '%s' does not exist and therefore cannot be removed.",cmStringNullGuard(symLabel));
  1191. return cmDspSysRemoveInstAttrSymbol(h, symId );
  1192. }
  1193. cmDspRC_t cmDspSysBroadcastValue( cmDspSysH_t h, unsigned instAttrSymId, const cmDspValue_t* valuePtr )
  1194. {
  1195. cmDsp_t* p = _cmDspHandleToPtr(h);
  1196. _cmDspInst_t* ip = p->instList;
  1197. for(; ip != NULL; ip = ip->linkPtr )
  1198. if( (instAttrSymId==cmInvalidId) || cmDspInstHasAttrSym(ip->instPtr,instAttrSymId))
  1199. ip->instPtr->sysRecvFunc(&p->ctx,ip->instPtr,instAttrSymId,valuePtr);
  1200. return kOkDspRC;
  1201. }
  1202. cmDspInst_t* cmDspSysAllocLabel( cmDspSysH_t h, const cmChar_t* label, unsigned alignId )
  1203. {
  1204. return cmDspSysAllocInst(h,"Label", NULL, 2, label, alignId);
  1205. }
  1206. cmDspRC_t _cmDspGetRsrcReal( cmDspSysH_t h, const cmChar_t* label, const cmChar_t* rsrcPath, cmReal_t* valPtr )
  1207. {
  1208. const cmChar_t* errLabelPtr = NULL;
  1209. if( cmJsonPathValues( cmDspSysPgmRsrcHandle(h),NULL,NULL,&errLabelPtr, rsrcPath, kRealTId, valPtr, NULL ) != kOkJsRC )
  1210. {
  1211. cmDsp_t* p = _cmDspHandleToPtr(h);
  1212. return cmErrMsg(&p->err,kRsrcNotFoundDspRC,"DSP instance allocation failed for '%s' and the initial value resource path '%s'.", cmStringNullGuard(label),cmStringNullGuard(rsrcPath));
  1213. }
  1214. return kOkDspRC;
  1215. }
  1216. cmDspInst_t* _cmDspSysAllocScalar( cmDspSysH_t h, unsigned presetGrpSymId, const cmChar_t* instLabel, cmReal_t min, cmReal_t max, cmReal_t step, cmReal_t val, const cmChar_t* label )
  1217. {
  1218. cmDspInst_t* inst;
  1219. if((inst = cmDspSysAllocInstS(h,"Scalar", presetGrpSymId, instLabel, 6, kNumberDuiId, min, max, step, val, label )) == NULL)
  1220. {
  1221. cmDsp_t* p = _cmDspHandleToPtr(h);
  1222. cmErrMsg(&p->err,kAllocInstFailDspRC,"Scalar UI control allocation failed for '%s'.", cmStringNullGuard(instLabel));
  1223. }
  1224. return inst;
  1225. }
  1226. cmDspInst_t* cmDspSysAllocScalar( cmDspSysH_t h, const cmChar_t* label, cmReal_t min, cmReal_t max, cmReal_t step, cmReal_t val )
  1227. { return _cmDspSysAllocScalar(h,cmInvalidId,label,min,max,step,val,label); }
  1228. cmChar_t* _cmDspSysFormLabel( cmDspSysH_t h, const cmChar_t* prefixLabel, const cmChar_t* label )
  1229. { return cmTsPrintfH(cmDspSysLHeap(h),"%s-%s",prefixLabel,label); }
  1230. cmDspInst_t* cmDspSysAllocScalarP( cmDspSysH_t h, unsigned presetGrpSymId, const cmChar_t* prefixLabel, const cmChar_t* label, cmReal_t min, cmReal_t max, cmReal_t step, cmReal_t val )
  1231. {
  1232. return _cmDspSysAllocScalar(h,presetGrpSymId,_cmDspSysFormLabel(h,prefixLabel,label),min,max,step,val,label);
  1233. }
  1234. cmDspInst_t* cmDspSysAllocScalarRsrc( cmDspSysH_t h, const cmChar_t* label, cmReal_t min, cmReal_t max, cmReal_t step, const cmChar_t* rsrcPath )
  1235. {
  1236. cmReal_t val;
  1237. if(_cmDspGetRsrcReal(h,label,rsrcPath,&val ) != kOkDspRC )
  1238. return NULL;
  1239. return cmDspSysAllocScalar(h,label,min,max,step,val);
  1240. }
  1241. cmDspInst_t** cmDspSysAllocScalarA( cmDspSysH_t h, unsigned cnt, unsigned presetGroupSymId, const cmChar_t* prefixLabel, const cmChar_t* label, cmReal_t min, cmReal_t max, cmReal_t step, cmReal_t val )
  1242. {
  1243. cmChar_t* lbl = cmTsPrintfH( cmDspSysLHeap(h), "%s-%s",prefixLabel,label);
  1244. return cmDspSysAllocInstArrayS( h, cnt, presetGroupSymId, "Scalar", lbl, NULL, 5, kNumberDuiId, min, max, step, val);
  1245. }
  1246. cmDspInst_t* _cmDspSysAllocButton( cmDspSysH_t h, unsigned btnTypeId, unsigned presetGroupSymId, const cmChar_t* instLabel, const cmChar_t* label, cmReal_t val, unsigned valSymId )
  1247. {
  1248. cmDspInst_t* inst;
  1249. if((inst = cmDspSysAllocInstS( h, "Button", presetGroupSymId, instLabel, 4, btnTypeId, val, valSymId, label)) == NULL )
  1250. {
  1251. cmDsp_t* p = _cmDspHandleToPtr(h);
  1252. cmErrMsg(&p->err,kAllocInstFailDspRC,"Button UI control allocation failed for '%s'.", cmStringNullGuard(label));
  1253. }
  1254. return inst;
  1255. }
  1256. cmDspInst_t* cmDspSysAllocButtonP( cmDspSysH_t h, const cmChar_t* prefixLabel, const cmChar_t* label, cmReal_t val )
  1257. {
  1258. return cmDspSysAllocButton(h,_cmDspSysFormLabel(h,prefixLabel,label),val);
  1259. }
  1260. cmDspInst_t* _cmDspSysAllocButtonRsrc( cmDspSysH_t h, unsigned typeId, unsigned presetGroupSymId, const cmChar_t* label, const cmChar_t* rsrcPath )
  1261. {
  1262. cmReal_t val;
  1263. if(_cmDspGetRsrcReal(h,label,rsrcPath,&val) != kOkDspRC )
  1264. return NULL;
  1265. return _cmDspSysAllocButton(h,typeId,presetGroupSymId,label,label,val,cmInvalidId);
  1266. }
  1267. cmDspInst_t* cmDspSysAllocButton( cmDspSysH_t h, const cmChar_t* label, cmReal_t val )
  1268. { return _cmDspSysAllocButton(h,kButtonDuiId,cmInvalidId,label,label,val,cmInvalidId); }
  1269. cmDspInst_t* cmDspSysAllocButtonRsrc( cmDspSysH_t h, const cmChar_t* label, const cmChar_t* rsrcPath )
  1270. { return _cmDspSysAllocButtonRsrc( h,kButtonDuiId,cmInvalidId,label,rsrcPath); }
  1271. cmDspInst_t* _cmDspSysAllocCheck( cmDspSysH_t h, unsigned presetGroupSymId, const cmChar_t* instLabel, const cmChar_t* label, cmReal_t val )
  1272. { return _cmDspSysAllocButton(h,kCheckDuiId,presetGroupSymId,instLabel,label,val,cmInvalidId); }
  1273. cmDspInst_t* cmDspSysAllocCheck( cmDspSysH_t h, const cmChar_t* label, cmReal_t val )
  1274. { return _cmDspSysAllocCheck(h,cmInvalidId,label,label,val); }
  1275. cmDspInst_t* cmDspSysAllocCheckP( cmDspSysH_t h, unsigned presetGroupSymId, const cmChar_t* prefixLabel, const cmChar_t* label, cmReal_t val )
  1276. { return _cmDspSysAllocCheck(h,presetGroupSymId,_cmDspSysFormLabel(h,prefixLabel,label),label,val); }
  1277. cmDspInst_t* cmDspSysAllocCheckRsrc( cmDspSysH_t h, const cmChar_t* label, const cmChar_t* rsrcPath )
  1278. { return _cmDspSysAllocButtonRsrc( h,kCheckDuiId, cmInvalidId, label,rsrcPath); }
  1279. cmDspInst_t* cmDspSysAllocMsgList( cmDspSysH_t h, const cmChar_t* fn, const cmChar_t* rsrcLabel, unsigned initSelIdx )
  1280. { return cmDspSysAllocInst( h, "MsgList", NULL, 3, rsrcLabel, fn, initSelIdx); }
  1281. cmDspInst_t* cmDspSysAllocMsgListP( cmDspSysH_t h, unsigned presetGroupSymId, const cmChar_t* preLabel, const cmChar_t* label, const cmChar_t* fn, const cmChar_t* rsrcLabel, unsigned initSelIdx )
  1282. {
  1283. cmDspInst_t* inst;
  1284. cmChar_t* lbl;
  1285. if((inst = cmDspSysAllocInstS( h, "MsgList", presetGroupSymId, lbl = _cmDspSysFormLabel(h,preLabel,label), 3, rsrcLabel, fn, initSelIdx)) == NULL )
  1286. {
  1287. cmDsp_t* p = _cmDspHandleToPtr(h);
  1288. cmErrMsg(&p->err,kAllocInstFailDspRC,"Msg List UI control allocation failed for '%s'.", cmStringNullGuard(lbl));
  1289. }
  1290. return inst;
  1291. }
  1292. cmDspInst_t* cmDspSysAllocAudioIn( cmDspSysH_t h, unsigned chIdx, cmReal_t gain )
  1293. { return cmDspSysAllocInst( h, "AudioIn", NULL, 2, chIdx, gain ); }
  1294. const cmJsonNode_t* _cmDspSysFindAudioChMap( cmDspSysH_t h, const cmChar_t* rsrcLabel )
  1295. {
  1296. cmDsp_t* p = _cmDspHandleToPtr(h);
  1297. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(h);
  1298. assert( cmJsonIsValid(jsH) );
  1299. const cmJsonNode_t* np = NULL;
  1300. if(( np = cmJsonFindValue( jsH, rsrcLabel, cmJsonRoot(jsH), kArrayTId )) == NULL )
  1301. {
  1302. cmErrMsg(&p->err,kRsrcNotFoundDspRC,"The audio channel map resource '%s' was not found.", cmStringNullGuard(rsrcLabel));
  1303. return NULL;
  1304. }
  1305. unsigned chCnt = cmJsonChildCount(np);
  1306. if( chCnt == 0 )
  1307. {
  1308. cmErrMsg(&p->err,kInvalidArgDspRC,"The audio channel map resource '%s' is empty.",cmStringNullGuard(rsrcLabel));
  1309. return NULL;
  1310. }
  1311. return np;
  1312. }
  1313. cmDspRC_t _cmDspSysReadAudioChMap( cmDspSysH_t h, const cmChar_t* rsrcLabel, const cmJsonNode_t* mapNodePtr, unsigned* map, unsigned mapCnt )
  1314. {
  1315. cmDsp_t* p = _cmDspHandleToPtr(h);
  1316. unsigned i;
  1317. for(i=0; i<mapCnt; ++i)
  1318. {
  1319. const cmJsonNode_t* np;
  1320. if((np = cmJsonArrayElementC(mapNodePtr,i)) == NULL )
  1321. return cmErrMsg(&p->err,kInvalidArgDspRC,"The audio map element at index %i could not be accessed in the audio map:'%s'.",i,cmStringNullGuard(rsrcLabel));
  1322. if( cmJsonUIntValue(np,map + i ) != kOkJsRC )
  1323. return cmErrMsg(&p->err,kInvalidArgDspRC,"The audio map element at index %i could not be read as an integer in the audio map:'%s'..",i,cmStringNullGuard(rsrcLabel));
  1324. }
  1325. return kOkDspRC;
  1326. }
  1327. cmDspInst_t** _cmDspSysAllocAudioRsrc( cmDspSysH_t h, const cmChar_t* classLabel, const cmChar_t* rsrcLabel, cmReal_t gain, unsigned* retChCntPtr )
  1328. {
  1329. cmDsp_t* p = _cmDspHandleToPtr(h);
  1330. const cmJsonNode_t* np;
  1331. if( retChCntPtr != NULL )
  1332. *retChCntPtr = 0;
  1333. //
  1334. if( rsrcLabel==NULL )
  1335. rsrcLabel = "audioInMap";
  1336. // find the map resource
  1337. if((np = _cmDspSysFindAudioChMap(h,rsrcLabel)) == NULL )
  1338. return NULL;
  1339. unsigned chCnt = cmJsonChildCount(np);
  1340. cmDspInst_t** a = cmLhAllocZ( p->lhH, cmDspInst_t*, chCnt );
  1341. unsigned chMap[ chCnt ];
  1342. // fill in the channel map - maps virtual channels to physical channels
  1343. if( _cmDspSysReadAudioChMap(h,rsrcLabel,np,chMap,chCnt) != kOkDspRC )
  1344. return NULL;
  1345. unsigned i;
  1346. for(i=0; i<chCnt; ++i)
  1347. if((a[i] = cmDspSysAllocInst( h, classLabel, NULL, 2, chMap[i], gain )) == NULL )
  1348. break;
  1349. if( i==chCnt )
  1350. {
  1351. if( retChCntPtr != NULL )
  1352. *retChCntPtr = chCnt;
  1353. return a;
  1354. }
  1355. return NULL;
  1356. }
  1357. cmDspInst_t** cmDspSysAllocAudioInAR( cmDspSysH_t h, const cmChar_t* rsrcLabel, cmReal_t gain, unsigned* retChCntPtr )
  1358. { return _cmDspSysAllocAudioRsrc(h,"AudioIn",rsrcLabel,gain,retChCntPtr); }
  1359. cmDspInst_t* cmDspSysAllocAudioOut(cmDspSysH_t h, unsigned chIdx, cmReal_t gain )
  1360. { return cmDspSysAllocInst( h, "AudioOut", NULL, 2, chIdx, gain ); }
  1361. cmDspInst_t** cmDspSysAllocAudioOutAR( cmDspSysH_t h, const cmChar_t* rsrcLabel, cmReal_t gain, unsigned* retChCntPtr )
  1362. { return _cmDspSysAllocAudioRsrc(h,"AudioOut",rsrcLabel,gain,retChCntPtr); }
  1363. cmDspRC_t cmDspSysInstallNetCb( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, const cmChar_t* dstNetNodeLabel, const cmChar_t* dstInstLabel, const cmChar_t* dstVarLabel )
  1364. {
  1365. cmDsp_t* p = _cmDspHandleToPtr(h);
  1366. cmDspRC_t rc = kOkDspRC;
  1367. cmDspInst_t* netSendInstPtr;
  1368. unsigned dstNetNodeId;
  1369. _cmDspSrcConn_t* srcConnPtr = NULL;
  1370. // get the dest. network node id
  1371. if( (dstNetNodeId = cmUdpNetNodeLabelToId(p->netH, dstNetNodeLabel )) == cmInvalidId )
  1372. return cmErrMsg(&p->err,kNetNodeNotFoundDspRC,"The destination network node label '%s' was not found.",cmStringNullGuard(dstNetNodeLabel));
  1373. if((srcConnPtr = _cmDspSysNetCreateSrcConn( p, dstNetNodeId, dstInstLabel, dstVarLabel)) == NULL )
  1374. return rc;
  1375. // allocate a network sender
  1376. if((netSendInstPtr = cmDspSysAllocInst(h, "NetSend", NULL, 1, srcConnPtr )) == NULL )
  1377. return cmErrMsg(&p->err,kNetSendAllocFailDspRC,"The network sender DSP instance allocation failed.");
  1378. return cmDspSysInstallCb(h, srcInstPtr, srcVarLabel, netSendInstPtr, "in", NULL );
  1379. }
  1380. cmDspRC_t cmDspSysInstallNetCb1N1N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, const cmChar_t* dstNetNodeLabel, const cmChar_t* dstInstLabel, const cmChar_t* dstVarPrefixStr, unsigned dstOffs, unsigned n )
  1381. {
  1382. cmDspRC_t rc = kOkDspRC;
  1383. unsigned i;
  1384. for(i=0; i<n; ++i)
  1385. {
  1386. const cmChar_t* label0 = srcVarPrefixStr;
  1387. const cmChar_t* label1 = dstVarPrefixStr;
  1388. if( n >= 2 )
  1389. {
  1390. label0 = cmDspSysPrintLabel( srcVarPrefixStr,i);
  1391. label1 = cmDspSysPrintLabel2(dstVarPrefixStr,dstOffs + i);
  1392. }
  1393. if((rc = cmDspSysInstallNetCb(h,srcInstPtr,label0,dstNetNodeLabel,dstInstLabel,label1)) != kOkDspRC )
  1394. break;
  1395. }
  1396. return rc;
  1397. }
  1398. cmDspRC_t cmDspSysInstallNetCb1N1NM(cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, unsigned srcCnt, const cmChar_t* dstNetNodeLabel, const cmChar_t* dstInstLabel, const cmChar_t* dstVarPrefixStr, unsigned dstCnt, const unsigned* map )
  1399. {
  1400. cmDspRC_t rc = kOkDspRC;
  1401. unsigned i;
  1402. for(i=0; i<dstCnt; ++i)
  1403. {
  1404. assert( map[i] < srcCnt );
  1405. const cmChar_t* label0 = cmDspSysPrintLabel( srcVarPrefixStr, map[i] );
  1406. const cmChar_t* label1 = cmDspSysPrintLabel2(dstVarPrefixStr, i );
  1407. if((rc = cmDspSysInstallNetCb(h,srcInstPtr,label0,dstNetNodeLabel,dstInstLabel,label1)) != kOkDspRC )
  1408. break;
  1409. }
  1410. return rc;
  1411. }
  1412. cmDspRC_t _cmDspRsrcPath( cmDspSysH_t h, cmChar_t** pathPtr, va_list vl )
  1413. {
  1414. unsigned i;
  1415. cmDspRC_t rc = kOkDspRC;;
  1416. cmDsp_t* p = _cmDspHandleToPtr(h);
  1417. cmChar_t* path = NULL;
  1418. unsigned maxArgCnt = 10;
  1419. *pathPtr = NULL;
  1420. for(i=0; i<maxArgCnt; ++i )
  1421. {
  1422. cmChar_t* str;
  1423. if((str = va_arg(vl,cmChar_t* )) == NULL )
  1424. break;
  1425. if( path != NULL )
  1426. path = cmTextAppendSS(path,"/");
  1427. path = cmTextAppendSS(path,str);
  1428. }
  1429. if( i >= maxArgCnt )
  1430. {
  1431. rc = cmErrMsg(&p->err,kJsonFailDspRC,"A resource path, beginning with '%25s', does not contain a terminating NULL.", cmStringNullGuard(path) );
  1432. goto errLabel;
  1433. }
  1434. // duplicate the string onto the program linked heap
  1435. *pathPtr = cmLhAllocStr( cmDspSysLHeap(h), path);
  1436. errLabel:
  1437. cmMemFree(path);
  1438. return rc;
  1439. }
  1440. cmDspRC_t cmDspRsrcBoolV( cmDspSysH_t h, bool* vp, va_list vl )
  1441. {
  1442. assert(vp != NULL);
  1443. cmDsp_t* p = _cmDspHandleToPtr(h);
  1444. cmChar_t* path = NULL;
  1445. cmDspRC_t rc;
  1446. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1447. return rc;
  1448. if((rc = cmJsonPathToBool( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1449. return cmErrMsg(&p->err,kJsonFailDspRC,"'bool' resource not found at path:'%s'.",cmStringNullGuard(path));
  1450. return rc;
  1451. }
  1452. cmDspRC_t cmDspRsrcIntV( cmDspSysH_t h, int* vp, va_list vl )
  1453. {
  1454. assert(vp != NULL);
  1455. cmDsp_t* p = _cmDspHandleToPtr(h);
  1456. cmChar_t* path = NULL;
  1457. cmDspRC_t rc;
  1458. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1459. return rc;
  1460. if((rc = cmJsonPathToInt( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1461. return cmErrMsg(&p->err,kJsonFailDspRC,"'int' resource not found at path:'%s'.",cmStringNullGuard(path));
  1462. return rc;
  1463. }
  1464. cmDspRC_t cmDspRsrcUIntV( cmDspSysH_t h, unsigned* vp, va_list vl )
  1465. {
  1466. assert(vp != NULL);
  1467. cmDsp_t* p = _cmDspHandleToPtr(h);
  1468. cmChar_t* path = NULL;
  1469. cmDspRC_t rc;
  1470. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1471. return rc;
  1472. if((rc = cmJsonPathToUInt( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1473. return cmErrMsg(&p->err,kJsonFailDspRC,"'uint' resource not found at path:'%s'.",cmStringNullGuard(path));
  1474. return rc;
  1475. }
  1476. cmDspRC_t cmDspRsrcDblV( cmDspSysH_t h, double* vp, va_list vl )
  1477. {
  1478. assert(vp != NULL);
  1479. cmDsp_t* p = _cmDspHandleToPtr(h);
  1480. cmChar_t* path = NULL;
  1481. cmDspRC_t rc;
  1482. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1483. return rc;
  1484. if((rc = cmJsonPathToReal( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1485. return cmErrMsg(&p->err,kJsonFailDspRC,"'real' resource not found at path:'%s'.",cmStringNullGuard(path));
  1486. return rc;
  1487. }
  1488. cmDspRC_t cmDspRsrcRealV( cmDspSysH_t h, cmReal_t* vp, va_list vl )
  1489. {
  1490. assert(vp != NULL);
  1491. cmDsp_t* p = _cmDspHandleToPtr(h);
  1492. cmChar_t* path = NULL;
  1493. cmDspRC_t rc;
  1494. double dval;
  1495. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1496. return rc;
  1497. if((rc = cmJsonPathToReal( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, &dval )) != kOkJsRC )
  1498. return cmErrMsg(&p->err,kJsonFailDspRC,"'real' resource not found at path:'%s'.",cmStringNullGuard(path));
  1499. *vp = dval;
  1500. return rc;
  1501. }
  1502. cmDspRC_t cmDspRsrcStringV( cmDspSysH_t h, const cmChar_t** vp, va_list vl )
  1503. {
  1504. assert(vp != NULL);
  1505. cmDsp_t* p = _cmDspHandleToPtr(h);
  1506. cmChar_t* path = NULL;
  1507. cmDspRC_t rc;
  1508. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1509. return rc;
  1510. if((rc = cmJsonPathToString( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1511. return cmErrMsg(&p->err,kJsonFailDspRC,"'string' resource not found at path:'%s'.",cmStringNullGuard(path));
  1512. return rc;
  1513. }
  1514. cmDspRC_t cmDspRsrcArrayCountV( cmDspSysH_t h, unsigned *n, va_list vl )
  1515. {
  1516. assert(n != NULL );
  1517. cmDsp_t* p = _cmDspHandleToPtr(h);
  1518. cmChar_t* path = NULL;
  1519. cmJsonNode_t* np = NULL;
  1520. cmDspRC_t rc;
  1521. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1522. return rc;
  1523. if((rc = cmJsonPathToArray( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, &np )) != kOkJsRC )
  1524. return cmErrMsg(&p->err,kJsonFailDspRC,"'array' resource not found at path:'%s'.",cmStringNullGuard(path));
  1525. *n = cmJsonChildCount(np);
  1526. return rc;
  1527. }
  1528. cmDspRC_t _cmDspRsrcArrayV( cmDspSysH_t h, unsigned* np, cmJsonNode_t** npp, cmChar_t** pathPtrPtr, va_list vl )
  1529. {
  1530. cmDsp_t* p = _cmDspHandleToPtr(h);
  1531. cmChar_t* path = NULL;
  1532. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(h);
  1533. cmDspRC_t rc;
  1534. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1535. return rc;
  1536. if((rc = cmJsonPathToArray( jsH, NULL, NULL, path, npp )) != kOkJsRC )
  1537. return cmErrMsg(&p->err,kJsonFailDspRC,"'array' resource not found at path:'%s'.",cmStringNullGuard(path));
  1538. if((*np = cmJsonChildCount(*npp)) == 0 )
  1539. return rc;
  1540. *pathPtrPtr = path;
  1541. return rc;
  1542. }
  1543. cmDspRC_t cmDspRsrcBoolArrayV( cmDspSysH_t h, unsigned* np, bool** vpp, va_list vl )
  1544. {
  1545. assert(vpp!=NULL);
  1546. cmDsp_t* p = _cmDspHandleToPtr(h);
  1547. cmJsonNode_t* nodePtr = NULL;
  1548. unsigned n = 0;
  1549. cmChar_t* path = NULL;
  1550. unsigned i;
  1551. cmDspRC_t rc;
  1552. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1553. return rc;
  1554. bool* vp = *vpp;
  1555. vp = cmLhResizeN(p->ctx.lhH,bool,vp,n);
  1556. for(i=0; i<n && rc == kOkDspRC; ++i)
  1557. if( cmJsonBoolValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1558. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1559. *vpp = vp;
  1560. *np = n;
  1561. return rc;
  1562. }
  1563. cmDspRC_t cmDspRsrcIntArrayV( cmDspSysH_t h, unsigned* np, int** vpp, va_list vl )
  1564. {
  1565. assert(vpp!=NULL);
  1566. cmDsp_t* p = _cmDspHandleToPtr(h);
  1567. cmJsonNode_t* nodePtr = NULL;
  1568. unsigned n = 0;
  1569. cmChar_t* path = NULL;
  1570. unsigned i;
  1571. cmDspRC_t rc;
  1572. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1573. return rc;
  1574. int* vp = *vpp;
  1575. vp = cmLhResizeN(p->ctx.lhH,int,vp,n);
  1576. for(i=0; i<n && rc == kOkDspRC; ++i)
  1577. if( cmJsonIntValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1578. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1579. *vpp = vp;
  1580. *np = n;
  1581. return rc;
  1582. }
  1583. cmDspRC_t cmDspRsrcUIntArrayV( cmDspSysH_t h, unsigned* np, unsigned** vpp, va_list vl )
  1584. {
  1585. assert(vpp!=NULL);
  1586. cmDsp_t* p = _cmDspHandleToPtr(h);
  1587. cmJsonNode_t* nodePtr = NULL;
  1588. unsigned n = 0;
  1589. cmChar_t* path = NULL;
  1590. unsigned i;
  1591. cmDspRC_t rc;
  1592. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1593. return rc;
  1594. unsigned* vp = *vpp;
  1595. vp = cmLhResizeN(p->ctx.lhH,unsigned,vp,n);
  1596. for(i=0; i<n && rc == kOkDspRC; ++i)
  1597. if( cmJsonUIntValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1598. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1599. *vpp = vp;
  1600. *np = n;
  1601. return rc;
  1602. }
  1603. cmDspRC_t cmDspRsrcDblArrayV( cmDspSysH_t h, unsigned* np, double** vpp, va_list vl )
  1604. {
  1605. assert(vpp!=NULL);
  1606. cmDsp_t* p = _cmDspHandleToPtr(h);
  1607. cmJsonNode_t* nodePtr = NULL;
  1608. unsigned n = 0;
  1609. cmChar_t* path = NULL;
  1610. unsigned i;
  1611. cmDspRC_t rc;
  1612. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1613. return rc;
  1614. double* vp = *vpp;
  1615. vp = cmLhResizeN(p->ctx.lhH,double,vp,n);
  1616. for(i=0; i<n && rc == kOkDspRC; ++i)
  1617. if( cmJsonRealValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1618. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1619. *vpp = vp;
  1620. *np = n;
  1621. return rc;
  1622. }
  1623. cmDspRC_t cmDspRsrcRealArrayV( cmDspSysH_t h, unsigned* np, cmReal_t** vpp, va_list vl )
  1624. {
  1625. assert(vpp!=NULL);
  1626. cmDsp_t* p = _cmDspHandleToPtr(h);
  1627. cmJsonNode_t* nodePtr = NULL;
  1628. unsigned n = 0;
  1629. cmChar_t* path = NULL;
  1630. unsigned i;
  1631. cmDspRC_t rc;
  1632. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1633. return rc;
  1634. cmReal_t* vp = *vpp;
  1635. vp = cmLhResizeN(p->ctx.lhH,cmReal_t,vp,n);
  1636. for(i=0; i<n && rc == kOkDspRC; ++i)
  1637. {
  1638. double v;
  1639. if( cmJsonRealValue( cmJsonArrayElementC(nodePtr,i), &v ) != kOkJsRC )
  1640. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1641. vp[i] = v;
  1642. }
  1643. *vpp = vp;
  1644. *np = n;
  1645. return rc;
  1646. }
  1647. cmDspRC_t cmDspRsrcStringArrayV(cmDspSysH_t h, unsigned* np, const cmChar_t*** vpp, va_list vl )
  1648. {
  1649. assert(vpp!=NULL);
  1650. cmDsp_t* p = _cmDspHandleToPtr(h);
  1651. cmJsonNode_t* nodePtr = NULL;
  1652. unsigned n = 0;
  1653. cmChar_t* path = NULL;
  1654. unsigned i;
  1655. cmDspRC_t rc;
  1656. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1657. return rc;
  1658. const cmChar_t** vp = *vpp;
  1659. vp = cmLhResizeN(p->ctx.lhH,const cmChar_t*,vp,n);
  1660. for(i=0; i<n && rc == kOkDspRC; ++i)
  1661. if( cmJsonStringValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1662. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1663. *vpp = vp;
  1664. *np = n;
  1665. return rc;
  1666. }
  1667. cmDspRC_t cmDspRsrcInt( cmDspSysH_t h, int* vp, ... )
  1668. {
  1669. va_list vl;
  1670. va_start(vl,vp);
  1671. cmDspRC_t rc = cmDspRsrcIntV(h,vp,vl);
  1672. va_end(vl);
  1673. return rc;
  1674. }
  1675. cmDspRC_t cmDspRsrcUInt( cmDspSysH_t h, unsigned* vp, ... )
  1676. {
  1677. va_list vl;
  1678. va_start(vl,vp);
  1679. cmDspRC_t rc = cmDspRsrcUIntV(h,vp,vl);
  1680. va_end(vl);
  1681. return rc;
  1682. }
  1683. cmDspRC_t cmDspRsrcDbl( cmDspSysH_t h, double* vp, ... )
  1684. {
  1685. va_list vl;
  1686. va_start(vl,vp);
  1687. cmDspRC_t rc = cmDspRsrcDblV(h,vp,vl);
  1688. va_end(vl);
  1689. return rc;
  1690. }
  1691. cmDspRC_t cmDspRsrcReal( cmDspSysH_t h, cmReal_t* vp, ... )
  1692. {
  1693. va_list vl;
  1694. va_start(vl,vp);
  1695. cmDspRC_t rc = cmDspRsrcRealV(h,vp,vl);
  1696. va_end(vl);
  1697. return rc;
  1698. }
  1699. cmDspRC_t cmDspRsrcString( cmDspSysH_t h, const cmChar_t** vp, ... )
  1700. {
  1701. va_list vl;
  1702. va_start(vl,vp);
  1703. cmDspRC_t rc = cmDspRsrcStringV(h,vp,vl);
  1704. va_end(vl);
  1705. return rc;
  1706. }
  1707. cmDspRC_t cmDspRsrcArrayCount( cmDspSysH_t h, unsigned *np, ... )
  1708. {
  1709. va_list vl;
  1710. va_start(vl,np);
  1711. cmDspRC_t rc = cmDspRsrcArrayCountV(h,np,vl);
  1712. va_end(vl);
  1713. return rc;
  1714. }
  1715. cmDspRC_t cmDspRsrcBoolArray( cmDspSysH_t h, unsigned* np, bool** vpp, ... )
  1716. {
  1717. va_list vl;
  1718. va_start(vl,vpp);
  1719. cmDspRC_t rc = cmDspRsrcBoolArrayV(h,np,vpp,vl);
  1720. va_end(vl);
  1721. return rc;
  1722. }
  1723. cmDspRC_t cmDspRsrcIntArray( cmDspSysH_t h, unsigned* np, int** vpp, ... )
  1724. {
  1725. va_list vl;
  1726. va_start(vl,vpp);
  1727. cmDspRC_t rc = cmDspRsrcIntArrayV(h,np,vpp,vl);
  1728. va_end(vl);
  1729. return rc;
  1730. }
  1731. cmDspRC_t cmDspRsrcUIntArray( cmDspSysH_t h, unsigned* np, unsigned** vpp, ... )
  1732. {
  1733. va_list vl;
  1734. va_start(vl,vpp);
  1735. cmDspRC_t rc = cmDspRsrcUIntArrayV(h,np,vpp,vl);
  1736. va_end(vl);
  1737. return rc;
  1738. }
  1739. cmDspRC_t cmDspRsrcDblArray( cmDspSysH_t h, unsigned* np, double** vpp, ... )
  1740. {
  1741. va_list vl;
  1742. va_start(vl,vpp);
  1743. cmDspRC_t rc = cmDspRsrcDblArrayV(h,np,vpp,vl);
  1744. va_end(vl);
  1745. return rc;
  1746. }
  1747. cmDspRC_t cmDspRsrcRealArray( cmDspSysH_t h, unsigned* np, cmReal_t** vpp, ... )
  1748. {
  1749. va_list vl;
  1750. va_start(vl,vpp);
  1751. cmDspRC_t rc = cmDspRsrcRealArrayV(h,np,vpp,vl);
  1752. va_end(vl);
  1753. return rc;
  1754. }
  1755. cmDspRC_t cmDspRsrcStringArray( cmDspSysH_t h, unsigned* np, const cmChar_t*** vpp, ... )
  1756. {
  1757. va_list vl;
  1758. va_start(vl,vpp);
  1759. cmDspRC_t rc = cmDspRsrcStringArrayV(h,np,vpp,vl);
  1760. va_end(vl);
  1761. return rc;
  1762. }