libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmDspSys.c 69KB

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