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.

cmDspSys.c 73KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393
  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 the JSON tree has been modified.
  372. if( cmJsonIsModified(p->ctx.rsrcJsH) )
  373. if( cmJsonWrite(p->ctx.rsrcJsH, cmJsonRoot(p->ctx.rsrcJsH), p->rsrcFn ) != kOkJsRC )
  374. rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON resource file write failed on '%s'.",cmStringNullGuard(p->rsrcFn));
  375. if( cmJsonFinalize(&p->ctx.rsrcJsH) != kOkJsRC )
  376. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Resource JSON finalization failed.");
  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. unsigned* hdr = (unsigned*)msgPtr;
  479. // the msg selector is the second field in the data packet (following the audio system sub-system id)
  480. //const unsigned msgTypeSelId = ((const unsigned*)msgPtr)[1];
  481. switch( hdr[1] )
  482. {
  483. case kUiSelAsId:
  484. _cmDspSysHandleUiMsg(p,ip,msgPtr,msgByteCnt);
  485. break;
  486. case kNetSyncSelAsId:
  487. _cmDspSysNetRecv(p, (const cmDspNetMsg_t*)msgPtr, msgByteCnt, srcNetNodeId );
  488. break;
  489. case kMidiMsgArraySelAsId:
  490. {
  491. cmMidiPacket_t pkt;
  492. cmDspValue_t v;
  493. pkt.cbDataPtr = NULL;
  494. pkt.devIdx = hdr[2];
  495. pkt.portIdx = hdr[3];
  496. pkt.msgCnt = hdr[4];
  497. pkt.msgArray = (cmMidiMsg*)(hdr + 5);
  498. unsigned midiSymId = cmDspSysRegisterStaticSymbol(h,"_midi");
  499. v.u.m.u.vp = &pkt;
  500. cmDspSysBroadcastValue(h, midiSymId, &v);
  501. /*
  502. // data format for MIDI messages
  503. { kMidiMsgArraytSelAsId, devIdx, portIdx, msgCnt, msgArray[msgCnt] }
  504. where each msgArray[] record is a cmMidiMsg record.
  505. */
  506. }
  507. break;
  508. }
  509. }
  510. // report any error - this should stop further calls to the DSP process
  511. return cmErrLastRC(&p->err);
  512. }
  513. unsigned cmDspSysSyncState( cmDspSysH_t h )
  514. {
  515. cmDsp_t* p = _cmDspHandleToPtr(h);
  516. return p->syncState;
  517. }
  518. unsigned cmDspSysPresetGroupCount( cmDspSysH_t h )
  519. {
  520. cmDsp_t* p = _cmDspHandleToPtr(h);
  521. return _cmDspPresetGroupCount(&p->pm);
  522. }
  523. unsigned cmDspSysPresetGroupSymId( cmDspSysH_t h, unsigned groupIdx )
  524. {
  525. cmDsp_t* p = _cmDspHandleToPtr(h);
  526. return _cmDspPresetGroupSymId(&p->pm,groupIdx);
  527. }
  528. const cmChar_t* cmDspSysPresetGroupLabel( cmDspSysH_t h, unsigned groupIdx )
  529. {
  530. cmDsp_t* p = _cmDspHandleToPtr(h);
  531. return _cmDspPresetGroupLabel(&p->pm,groupIdx);
  532. }
  533. cmDspRC_t cmDspSysPresetGroupJsonList( cmDspSysH_t h, cmJsonH_t* jsHPtr )
  534. {
  535. cmDsp_t* p = _cmDspHandleToPtr(h);
  536. return _cmDspPresetGroupJsonList(&p->pm,jsHPtr);
  537. }
  538. unsigned cmDspSysPresetPresetCount( cmDspSysH_t h, unsigned groupIdx )
  539. {
  540. cmDsp_t* p = _cmDspHandleToPtr(h);
  541. return _cmDspPresetPresetCount(&p->pm,groupIdx);
  542. }
  543. unsigned cmDspSysPresetPresetSymId( cmDspSysH_t h, unsigned groupIdx, unsigned presetIdx )
  544. {
  545. cmDsp_t* p = _cmDspHandleToPtr(h);
  546. return _cmDspPresetPresetSymId(&p->pm,groupIdx,presetIdx);
  547. }
  548. const cmChar_t* cmDspSysPresetPresetLabel( cmDspSysH_t h, unsigned groupIdx, unsigned presetIdx )
  549. {
  550. cmDsp_t* p = _cmDspHandleToPtr(h);
  551. return _cmDspPresetPresetLabel(&p->pm,groupIdx,presetIdx);
  552. }
  553. cmDspRC_t cmDspSysPresetPresetJsonList( cmDspSysH_t h, unsigned groupSymId, cmJsonH_t* jsHPtr )
  554. {
  555. cmDsp_t* p = _cmDspHandleToPtr(h);
  556. return _cmDspPresetPresetJsonList(&p->pm,jsHPtr,groupSymId);
  557. }
  558. unsigned cmDspSysPresetRegisterGroup( cmDspSysH_t h, const cmChar_t* groupLabel )
  559. {
  560. cmDsp_t* p = _cmDspHandleToPtr(h);
  561. assert( groupLabel != NULL );
  562. unsigned grpSymId;
  563. if((grpSymId = cmSymTblRegisterSymbol(p->ctx.stH,groupLabel)) == cmInvalidIdx )
  564. cmErrMsg(&p->err,kSymTblFailDspRC,"Registration of the group label:'%s' failed.",cmStringNullGuard(groupLabel));
  565. return grpSymId;
  566. }
  567. cmDspRC_t cmDspSysPresetCreate( cmDspSysH_t h, const cmChar_t* groupLabel, const cmChar_t* presetLabel )
  568. {
  569. cmDspRC_t rc;
  570. cmDsp_t* p = _cmDspHandleToPtr(h);
  571. // create a new preset and make it active
  572. if((rc = _cmDspPresetCreatePreset(&p->pm, groupLabel, presetLabel )) != kOkDspRC )
  573. return rc;
  574. assert( p->pm.gp != NULL );
  575. // call store on each of the instances
  576. _cmDspInst_t* ip = p->instList;
  577. for(; ip != NULL; ip = ip->linkPtr )
  578. if( ip->instPtr->symId != cmInvalidId && ip->instPtr->presetGroupSymId == p->pm.gp->symId && ip->instPtr->storeFunc != NULL )
  579. {
  580. // create an instance preset and make it active
  581. if((rc = _cmDspPresetCreateInstance(&p->pm, ip->instPtr->symId )) != kOkDspRC )
  582. return rc;
  583. // call the store function on each instance
  584. if( ip->instPtr->storeFunc(&p->ctx,ip->instPtr,true) != kOkDspRC )
  585. rc = cmErrMsg(&p->err,kInstStoreFailDspRC,"Save failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  586. }
  587. return rc;
  588. }
  589. cmDspRC_t cmDspSysPresetRecall( cmDspSysH_t h, const cmChar_t* groupLabel, const cmChar_t* presetLabel )
  590. {
  591. cmDspRC_t rc;
  592. cmDsp_t* p = _cmDspHandleToPtr(h);
  593. // make the given group/preset active
  594. if((rc = _cmDspPresetRecallPreset(&p->pm, groupLabel, presetLabel )) != kOkDspRC )
  595. return rc;
  596. assert( p->pm.gp != NULL );
  597. // call store on each of the instances
  598. _cmDspInst_t* ip = p->instList;
  599. for(; ip != NULL; ip = ip->linkPtr )
  600. if( ip->instPtr->symId != cmInvalidId && ip->instPtr->storeFunc != NULL && ip->instPtr->presetGroupSymId == p->pm.gp->symId )
  601. {
  602. // make the instance active
  603. if((rc = _cmDspPresetRecallInstance(&p->pm, ip->instPtr->symId )) != kOkDspRC )
  604. {
  605. if( rc == kPresetInstNotFoundDspRC )
  606. {
  607. cmErrWarnMsg(&p->err,kOkDspRC,"Assuming a new instance was added to the preset - continuing with preset load.");
  608. continue;
  609. }
  610. }
  611. // call the store function on each instance
  612. if( ip->instPtr->storeFunc(&p->ctx,ip->instPtr,false) != kOkDspRC )
  613. rc = cmErrMsg(&p->err,kInstStoreFailDspRC,"Restore failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  614. }
  615. return rc;
  616. }
  617. cmDspRC_t cmDspSysPresetWriteValue( cmDspSysH_t h, unsigned varSymId, const cmDspValue_t* valPtr )
  618. {
  619. cmDsp_t* p = _cmDspHandleToPtr(h);
  620. return _cmDspPresetCreateVar(&p->pm,varSymId,valPtr);
  621. }
  622. cmDspRC_t cmDspSysPresetReadValue( cmDspSysH_t h, unsigned varSymId, cmDspValue_t* valPtr )
  623. {
  624. cmDsp_t* p = _cmDspHandleToPtr(h);
  625. return _cmDspPresetRecallVar(&p->pm,varSymId,valPtr);
  626. }
  627. // allocate an instance given the class symbol id
  628. cmDspInst_t* _cmDspSysAllocateInst( cmDsp_t* p, unsigned classSymId, unsigned storeSymId, unsigned instSymId, unsigned va_cnt, va_list vl )
  629. {
  630. _cmDspClass_t* cp = p->classList;
  631. cmDspInst_t* instPtr = NULL;
  632. // locate the DSP class
  633. for(; cp != NULL; cp=cp->linkPtr )
  634. if( cp->symId == classSymId )
  635. break;
  636. // if the DSP class was not found
  637. if( cp == NULL )
  638. {
  639. cmErrMsg(&p->err,kClassNotFoundDspRC,"The DSP class '%s' could not be found.",cmSymTblLabel(p->ctx.stH,classSymId));
  640. return NULL;
  641. }
  642. // allocate the instance
  643. if((instPtr = cp->classPtr->allocFunc( &p->ctx, cp->classPtr, storeSymId, instSymId, p->nextInstId, va_cnt, vl )) == NULL )
  644. {
  645. cmErrMsg(&p->err,kAllocInstFailDspRC,"DSP instance allocation failed for class '%s'.",cp->classPtr->labelStr);
  646. return NULL;
  647. }
  648. // assign the symbol attributes
  649. cmDspInstSymId_t* ip = p->symIdList;
  650. for(; ip!=NULL; ip=ip->link)
  651. if( ip->symId != cmInvalidId )
  652. if( cmDspInstRegisterAttrSym(&p->ctx, instPtr, ip->symId ) != kOkDspRC )
  653. {
  654. cmErrMsg(&p->err,kAllocInstFailDspRC,"The DSP instance failed due to attribute symbol registration failure.");
  655. return NULL;
  656. }
  657. // allocate the instance ctl recd
  658. _cmDspInst_t* instCtlPtr = cmLhAllocZ( p->ctx.lhH, _cmDspInst_t, 1 );
  659. assert( instCtlPtr != NULL );
  660. // setup the inst ctl recd
  661. instCtlPtr->instPtr = instPtr;
  662. instCtlPtr->linkPtr = NULL;
  663. // link the inst ctl recd onto the end of the instance list
  664. if( p->instList == NULL )
  665. p->instList = instCtlPtr;
  666. else
  667. {
  668. _cmDspInst_t* ip = p->instList;
  669. while( ip->linkPtr != NULL )
  670. ip = ip->linkPtr;
  671. ip->linkPtr = instCtlPtr;
  672. }
  673. ++p->nextInstId;
  674. return instPtr;
  675. }
  676. // allocate a an instance from the class label - this function calls _cmDspSysAllocateInst()
  677. cmDspInst_t* cmDspSysAllocInstSV( cmDspSysH_t h, const cmChar_t* classLabelStr, unsigned storeSymId, const cmChar_t* instLabelStr, unsigned va_cnt, va_list vl)
  678. {
  679. cmDsp_t* p = _cmDspHandleToPtr(h);
  680. unsigned instSymId = cmInvalidId;
  681. cmDspInst_t* newInstPtr;
  682. unsigned classSymId;
  683. // get the class symbold
  684. if((classSymId = cmSymTblId(p->ctx.stH,classLabelStr)) == cmInvalidId )
  685. {
  686. cmErrMsg(&p->err,kSymNotFoundDspRC,"The symbol id associated with the class '%s' could not be found.",classLabelStr);
  687. return NULL;
  688. }
  689. if( instLabelStr != NULL )
  690. {
  691. // get the symbold id assoc'd with the instance label
  692. if((instSymId = cmSymTblRegisterSymbol(p->ctx.stH,instLabelStr)) == cmInvalidId )
  693. {
  694. cmErrMsg(&p->err,kSymTblFailDspRC,"The DSP instance symbol '%s' failed to register with the symbol table.",instLabelStr);
  695. return NULL;
  696. }
  697. // do not allow instance labels to be re-used
  698. if( _cmDspSysInstSymIdToPtr(p,instSymId) != NULL )
  699. {
  700. cmErrMsg(&p->err,kDuplInstSymIdDspRC,"The DSP instance label '%s' was reused (class:'%s').",cmStringNullGuard(instLabelStr),cmStringNullGuard(classLabelStr));
  701. return NULL;
  702. }
  703. }
  704. // allocate the instance
  705. newInstPtr = _cmDspSysAllocateInst(p, classSymId, storeSymId, instSymId, va_cnt, vl );
  706. return newInstPtr;
  707. }
  708. cmDspInst_t* cmDspSysAllocInstS( cmDspSysH_t h, const cmChar_t* classLabelStr, unsigned storeSymId, const cmChar_t* instLabelStr, unsigned va_cnt, ... )
  709. {
  710. cmDspInst_t* p;
  711. va_list vl;
  712. va_start(vl,va_cnt);
  713. p = cmDspSysAllocInstSV(h, classLabelStr, storeSymId, instLabelStr, va_cnt, vl);
  714. va_end(vl);
  715. return p;
  716. }
  717. cmDspInst_t* cmDspSysAllocInst( cmDspSysH_t h, const cmChar_t* classLabelStr, const cmChar_t* instLabelStr, unsigned va_cnt, ... )
  718. {
  719. cmDspInst_t* p;
  720. va_list vl;
  721. va_start(vl,va_cnt);
  722. p = cmDspSysAllocInstSV(h, classLabelStr, cmInvalidId, instLabelStr, va_cnt, vl);
  723. va_end(vl);
  724. return p;
  725. }
  726. 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 )
  727. {
  728. cmDsp_t* p = _cmDspHandleToPtr(h);
  729. cmDspInst_t** a = cmLhAllocZ( p->lhH, cmDspInst_t*, cnt );
  730. unsigned i;
  731. for(i=0; i<cnt; ++i)
  732. {
  733. const cmChar_t* label = instLabelStr;
  734. va_list vl1;
  735. va_copy(vl1,vl);
  736. if( labelFunc != NULL )
  737. label = labelFunc(h,i);
  738. else
  739. if( instLabelStr != NULL )
  740. label = cmDspSysPrintLabel(instLabelStr,i);
  741. a[i] = cmDspSysAllocInstSV(h,classLabelStr,presetGroupSymId,label,va_cnt,vl1);
  742. }
  743. return a;
  744. }
  745. cmDspInst_t** cmDspSysAllocInstArrayS( cmDspSysH_t h, unsigned cnt, unsigned presetGroupSymId, const cmChar_t* classLabelStr, const cmChar_t* instLabelStr, cmDspSysLabelFunc_t labelFunc, unsigned va_cnt, ... )
  746. {
  747. va_list vl;
  748. va_start(vl,va_cnt);
  749. cmDspInst_t** a = cmDspSysAllocInstArraySV(h,cnt,presetGroupSymId,classLabelStr,instLabelStr,labelFunc,va_cnt,vl);
  750. va_end(vl);
  751. return a;
  752. }
  753. cmDspInst_t** cmDspSysAllocInstArray( cmDspSysH_t h, unsigned cnt, const cmChar_t* classLabelStr, const cmChar_t* instLabelStr, cmDspSysLabelFunc_t labelFunc, unsigned va_cnt, ... )
  754. {
  755. va_list vl;
  756. va_start(vl,va_cnt);
  757. cmDspInst_t** a = cmDspSysAllocInstArraySV(h,cnt,cmInvalidId,classLabelStr,instLabelStr,labelFunc,va_cnt,vl);
  758. va_end(vl);
  759. return a;
  760. }
  761. cmDspRC_t cmDspSysNewColumn( cmDspSysH_t h, unsigned colW )
  762. { return cmDspUiNewColumn(&_cmDspHandleToPtr(h)->ctx,colW); }
  763. cmDspRC_t cmDspSysInsertHorzBorder( cmDspSysH_t h )
  764. { return cmDspUiInsertHorzBorder(&_cmDspHandleToPtr(h)->ctx); }
  765. cmDspRC_t cmDspSysNewPage( cmDspSysH_t h, const cmChar_t* title )
  766. { return cmDspUiNewPage(&_cmDspHandleToPtr(h)->ctx,title); }
  767. cmDspRC_t _cmDspSysConnAudio( cmDsp_t* p, cmDspInst_t* srcInstPtr, unsigned srcVarSymId, cmDspInst_t* dstInstPtr, unsigned dstVarSymId )
  768. {
  769. cmDspVar_t* srcVarPtr;
  770. cmDspVar_t* dstVarPtr;
  771. //cmSample_t* srcDataPtr;
  772. if( srcInstPtr == NULL )
  773. 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));
  774. if( dstInstPtr == NULL )
  775. 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));
  776. // locate the src audio variable
  777. if((srcVarPtr = cmDspVarSymbolToPtr( &p->ctx, srcInstPtr, srcVarSymId, kOutDsvFl )) == NULL )
  778. 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);
  779. // validate the src variable type
  780. if( cmIsFlag(srcVarPtr->flags,kAudioBufDsvFl) == false )
  781. 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);
  782. // locate the dst audio variable
  783. if((dstVarPtr = cmDspVarSymbolToPtr( &p->ctx, dstInstPtr, dstVarSymId, kInDsvFl )) == NULL )
  784. 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);
  785. // validate the dst variable type
  786. if( cmIsFlag(dstVarPtr->flags,kAudioBufDsvFl) == false )
  787. 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);
  788. // get a pointer to the source sample buffer
  789. //if( (srcDataPtr = cmDsvSampleMtx( &srcVarPtr->value )) == NULL )
  790. // 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);
  791. // set the destination sample buffer to point to the source sample buffer.
  792. //cmDsvSetSampleMtx( &dstVarPtr->value, srcDataPtr, cmDsvRows(&srcVarPtr->value), cmDsvCols(&srcVarPtr->value));
  793. cmDsvSetProxy( &dstVarPtr->value, &srcVarPtr->value );
  794. return kOkDspRC;
  795. }
  796. cmDspRC_t cmDspSysConnectAudio( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLbl, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLbl )
  797. {
  798. cmDsp_t* p = _cmDspHandleToPtr(h);
  799. unsigned srcSymId, dstSymId;
  800. if( srcInstPtr == NULL && dstInstPtr == NULL )
  801. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The audio connection process was passed a NULL source and destination processor instance.");
  802. if( srcInstPtr == NULL )
  803. 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));
  804. if( dstInstPtr == NULL )
  805. 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));
  806. if((srcSymId = cmSymTblId(p->ctx.stH,srcVarLbl)) == cmInvalidId )
  807. 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);
  808. if((dstSymId = cmSymTblId(p->ctx.stH,dstVarLbl)) == cmInvalidId )
  809. 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);
  810. return _cmDspSysConnAudio(p,srcInstPtr,srcSymId,dstInstPtr,dstSymId);
  811. }
  812. cmDspRC_t cmDspSysConnectAudioN11N( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  813. {
  814. cmDspRC_t rc = kOkDspRC;
  815. unsigned i;
  816. for(i=0; i<n; ++i)
  817. {
  818. const cmChar_t* dstVarStr = dstVarPrefixStr;
  819. if( n >= 2 )
  820. dstVarStr = cmDspSysPrintLabel(dstVarPrefixStr,i);
  821. if((rc = cmDspSysConnectAudio( h, srcInstArray[i], srcVarLabel, dstInstPtr, dstVarStr )) != kOkDspRC )
  822. break;
  823. }
  824. return rc;
  825. }
  826. cmDspRC_t cmDspSysConnectAudio1NN1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  827. {
  828. cmDspRC_t rc = kOkDspRC;
  829. unsigned i;
  830. for(i=0; i<n; ++i)
  831. {
  832. const cmChar_t* label = srcVarPrefixStr;
  833. if( n >= 2 )
  834. label = cmDspSysPrintLabel(srcVarPrefixStr,i);
  835. if((rc = cmDspSysConnectAudio( h, srcInstPtr, label, dstInstArray[i], dstVarLabel )) != kOkDspRC )
  836. break;
  837. }
  838. return rc;
  839. }
  840. cmDspRC_t cmDspSysConnectAudio1N1N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  841. {
  842. cmDspRC_t rc = kOkDspRC;
  843. unsigned i;
  844. for(i=0; i<n; ++i)
  845. {
  846. const cmChar_t* label0 = cmDspSysPrintLabel(srcVarPrefixStr,i);
  847. const cmChar_t* label1 = cmDspSysPrintLabel2(dstVarPrefixStr,i);
  848. if((rc = cmDspSysConnectAudio( h, srcInstPtr, label0, dstInstPtr, label1 )) != kOkDspRC )
  849. break;
  850. }
  851. return rc;
  852. }
  853. cmDspRC_t cmDspSysConnectAudioN1N1( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  854. {
  855. cmDspRC_t rc = kOkDspRC;
  856. unsigned i;
  857. for(i=0; i<n; ++i)
  858. {
  859. if((rc = cmDspSysConnectAudio( h, srcInstArray[i], srcVarLabel, dstInstArray[i], dstVarLabel )) != kOkDspRC )
  860. break;
  861. }
  862. return rc;
  863. }
  864. cmDspRC_t cmDspSysConnectAudio11N1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[],const cmChar_t* dstVarLabel, unsigned n )
  865. {
  866. cmDspRC_t rc = kOkDspRC;
  867. unsigned i;
  868. for(i=0; i<n; ++i)
  869. {
  870. if((rc = cmDspSysConnectAudio( h, srcInstPtr, srcVarLabel, dstInstArray[i], dstVarLabel )) != kOkDspRC )
  871. break;
  872. }
  873. return rc;
  874. }
  875. cmDspRC_t cmDspSysConnectAudio111N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, unsigned n )
  876. {
  877. cmDspRC_t rc = kOkDspRC;
  878. unsigned i;
  879. for(i=0; i<n; ++i)
  880. {
  881. const cmChar_t* label1 = cmDspSysPrintLabel(dstVarLabel,i);
  882. const cmChar_t* label = n>1 ? label1 : dstVarLabel;
  883. if((rc = cmDspSysConnectAudio( h, srcInstPtr, srcVarLabel, dstInstPtr, label )) != kOkDspRC )
  884. break;
  885. }
  886. return rc;
  887. }
  888. 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 )
  889. {
  890. cmDsp_t* p = _cmDspHandleToPtr(h);
  891. cmDspRC_t rc = kOkDspRC;
  892. unsigned i;
  893. for(i=0; i<dstCnt; ++i)
  894. {
  895. const cmChar_t* dstVarStr = dstVarPrefixStr;
  896. if( dstCnt >= 2 )
  897. dstVarStr = cmDspSysPrintLabel(dstVarPrefixStr,i);
  898. if( map[i] >= srcCnt )
  899. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The source instance map connection index %i is of range %i.",i,srcCnt);
  900. if((rc = cmDspSysConnectAudio( h, srcInstArray[ map[i] ], srcVarLabel, dstInstPtr, dstVarStr )) != kOkDspRC )
  901. break;
  902. }
  903. return rc;
  904. }
  905. cmDspRC_t cmDspSysInstallCb( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, void* dstCbDataPtr)
  906. {
  907. cmDsp_t* p = _cmDspHandleToPtr(h);
  908. return cmDspInstallCb(&p->ctx,srcInstPtr,srcVarLabel,dstInstPtr,dstVarLabel,dstCbDataPtr);
  909. }
  910. cmDspRC_t cmDspSysInstallCbN11N( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  911. {
  912. cmDspRC_t rc = kOkDspRC;
  913. unsigned i;
  914. for(i=0; i<n; ++i)
  915. {
  916. const cmChar_t* dstVarStr = dstVarPrefixStr;
  917. if( n >= 2 )
  918. dstVarStr = cmDspSysPrintLabel(dstVarPrefixStr,i);
  919. if((rc = cmDspSysInstallCb( h, srcInstArray[i], srcVarLabel, dstInstPtr, dstVarStr, NULL )) != kOkDspRC )
  920. break;
  921. }
  922. return rc;
  923. }
  924. cmDspRC_t cmDspSysInstallCb1NN1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  925. {
  926. cmDspRC_t rc = kOkDspRC;
  927. unsigned i;
  928. for(i=0; i<n; ++i)
  929. {
  930. const cmChar_t* srcVarStr = srcVarPrefixStr;
  931. if( n >= 2 )
  932. srcVarStr = cmDspSysPrintLabel(srcVarPrefixStr,i);
  933. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  934. break;
  935. }
  936. return rc;
  937. }
  938. cmDspRC_t cmDspSysInstallCb1N1N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  939. {
  940. cmDspRC_t rc = kOkDspRC;
  941. unsigned i;
  942. for(i=0; i<n; ++i)
  943. {
  944. const cmChar_t* label0 = srcVarPrefixStr;
  945. const cmChar_t* label1 = dstVarPrefixStr;
  946. if( n >= 2 )
  947. {
  948. label0 = cmDspSysPrintLabel(srcVarPrefixStr,i);
  949. label1 = cmDspSysPrintLabel2(dstVarPrefixStr,i);
  950. }
  951. if((rc = cmDspSysInstallCb( h, srcInstPtr, label0, dstInstPtr, label1, NULL )) != kOkDspRC )
  952. break;
  953. }
  954. return rc;
  955. }
  956. cmDspRC_t cmDspSysInstallCbN1N1( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  957. {
  958. cmDspRC_t rc = kOkDspRC;
  959. unsigned i;
  960. for(i=0; i<n; ++i)
  961. {
  962. if((rc = cmDspSysInstallCb( h, srcInstArray[i], srcVarLabel, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  963. break;
  964. }
  965. return rc;
  966. }
  967. cmDspRC_t cmDspSysInstallCb11N1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  968. {
  969. cmDspRC_t rc = kOkDspRC;
  970. unsigned i;
  971. for(i=0; i<n; ++i)
  972. {
  973. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarLabel, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  974. break;
  975. }
  976. return rc;
  977. }
  978. cmDspRC_t cmDspSysInstallCb111N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixLabel, unsigned n )
  979. {
  980. cmDspRC_t rc = kOkDspRC;
  981. unsigned i;
  982. for(i=0; i<n; ++i)
  983. {
  984. const cmChar_t* dstVarStr = dstVarPrefixLabel;
  985. if( n >= 2 )
  986. dstVarStr = cmDspSysPrintLabel(dstVarPrefixLabel,i);
  987. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarLabel, dstInstPtr, dstVarStr, NULL )) != kOkDspRC )
  988. break;
  989. }
  990. return rc;
  991. }
  992. cmDspRC_t cmDspSysInstallCbN111( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, unsigned n )
  993. {
  994. cmDspRC_t rc = kOkDspRC;
  995. unsigned i;
  996. for(i=0; i<n; ++i)
  997. {
  998. if((rc = cmDspSysInstallCb( h, srcInstArray[i], srcVarLabel, dstInstPtr, dstVarLabel, NULL )) != kOkDspRC )
  999. break;
  1000. }
  1001. return rc;
  1002. }
  1003. cmDspRC_t cmDspSysInstallCb1N11( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, unsigned n )
  1004. {
  1005. cmDspRC_t rc = kOkDspRC;
  1006. unsigned i;
  1007. for(i=0; i<n; ++i)
  1008. {
  1009. const cmChar_t* srcVarStr = srcVarPrefixLabel;
  1010. if( n >= 2 )
  1011. srcVarStr = cmDspSysPrintLabel(srcVarPrefixLabel,i);
  1012. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstPtr, dstVarLabel, NULL )) != kOkDspRC )
  1013. break;
  1014. }
  1015. return rc;
  1016. }
  1017. 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 )
  1018. {
  1019. cmDsp_t* p = _cmDspHandleToPtr(h);
  1020. cmDspRC_t rc = kOkDspRC;
  1021. unsigned i;
  1022. for(i=0; i<dstCnt; ++i)
  1023. {
  1024. const cmChar_t* label0 = srcVarPrefixStr;
  1025. const cmChar_t* label1 = dstVarPrefixStr;
  1026. if( map[i] >= srcCnt )
  1027. {
  1028. if( map[i] >= srcCnt )
  1029. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The source instance map connection index %i is of range %i.",i,srcCnt);
  1030. }
  1031. if( srcCnt >= 2 )
  1032. label0 = cmDspSysPrintLabel(srcVarPrefixStr,map[i]);
  1033. if( dstCnt >= 2 )
  1034. label1 = cmDspSysPrintLabel2(dstVarPrefixStr,i);
  1035. if((rc = cmDspSysInstallCb( h, srcInstPtr, label0, dstInstPtr, label1, NULL )) != kOkDspRC )
  1036. break;
  1037. }
  1038. return rc;
  1039. }
  1040. 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 )
  1041. {
  1042. cmDsp_t* p = _cmDspHandleToPtr(h);
  1043. cmDspRC_t rc = kOkDspRC;
  1044. unsigned i;
  1045. for(i=0; i<dstCnt; ++i)
  1046. {
  1047. if( map[i] >= srcCnt )
  1048. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The source instance map connection index %i is of range %i.",i,srcCnt);
  1049. const cmChar_t* srcVarStr = srcVarPrefixStr;
  1050. if( srcCnt >= 2 )
  1051. srcVarStr = cmDspSysPrintLabel(srcVarPrefixStr,map[i]);
  1052. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  1053. break;
  1054. }
  1055. return rc;
  1056. }
  1057. 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 )
  1058. {
  1059. cmDsp_t* p = _cmDspHandleToPtr(h);
  1060. cmDspRC_t rc = kOkDspRC;
  1061. unsigned i;
  1062. for(i=0; i<srcCnt; ++i)
  1063. {
  1064. if( map[i] >= dstCnt )
  1065. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The dest. instance map connection index %i is of range %i.",map[i],dstCnt);
  1066. const cmChar_t* srcVarStr = srcVarPrefixStr;
  1067. if( srcCnt >= 2 )
  1068. srcVarStr = cmDspSysPrintLabel(srcVarPrefixStr,i);
  1069. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstArray[ map[i] ], dstVarLabel, NULL )) != kOkDspRC )
  1070. break;
  1071. }
  1072. return rc;
  1073. }
  1074. double cmDspSysSampleRate( cmDspSysH_t h )
  1075. {
  1076. cmDsp_t* p = _cmDspHandleToPtr(h);
  1077. return p->ctx.ctx->ss->args.srate;
  1078. }
  1079. cmJsonH_t cmDspSysPgmRsrcHandle( cmDspSysH_t h )
  1080. {
  1081. cmDsp_t* p = _cmDspHandleToPtr(h);
  1082. return p->ctx.rsrcJsH;
  1083. }
  1084. cmSymTblH_t cmDspSysSymbolTable( cmDspSysH_t h )
  1085. {
  1086. cmDsp_t* p = _cmDspHandleToPtr(h);
  1087. return p->ctx.stH;
  1088. }
  1089. unsigned cmDspSysRegisterStaticSymbol( cmDspSysH_t h, const cmChar_t* symLabel )
  1090. { return cmSymTblRegisterStaticSymbol( cmDspSysSymbolTable(h), symLabel ); }
  1091. unsigned cmDspSysRegisterSymbol( cmDspSysH_t h, const cmChar_t* symLabel )
  1092. { return cmSymTblRegisterSymbol( cmDspSysSymbolTable(h), symLabel); }
  1093. cmCtx_t* cmDspSysPgmCtx( cmDspSysH_t h )
  1094. {
  1095. cmDsp_t* p = _cmDspHandleToPtr(h);
  1096. return &p->cmCtx;
  1097. }
  1098. cmLHeapH_t cmDspSysLHeap( cmDspSysH_t h )
  1099. {
  1100. cmDsp_t* p = _cmDspHandleToPtr(h);
  1101. return p->lhH;
  1102. }
  1103. unsigned cmDspSysNetNodeId( cmDspSysH_t h )
  1104. {
  1105. cmDsp_t* p = _cmDspHandleToPtr(h);
  1106. return cmUdpNetLocalNodeId(p->netH);
  1107. }
  1108. const cmChar_t* cmDspSysNetNodeLabel( cmDspSysH_t h )
  1109. {
  1110. cmDsp_t* p = _cmDspHandleToPtr(h);
  1111. return cmUdpNetLocalNodeLabel(p->netH);
  1112. }
  1113. const cmChar_t* cmDspSysNetNodeIdToLabel( cmDspSysH_t h, unsigned netNodeId )
  1114. {
  1115. cmDsp_t* p = _cmDspHandleToPtr(h);
  1116. return cmUdpNetNodeIdToLabel(p->netH,netNodeId);
  1117. }
  1118. unsigned cmDspSysNetNodeLabelToId( cmDspSysH_t h, const cmChar_t* netNodeLabel )
  1119. {
  1120. cmDsp_t* p = _cmDspHandleToPtr(h);
  1121. return cmUdpNetNodeLabelToId(p->netH,netNodeLabel);
  1122. }
  1123. cmDspInstSymId_t* _cmDspSysFindInstSymId( cmDsp_t* p, unsigned symId )
  1124. {
  1125. cmDspInstSymId_t* ip = p->symIdList;
  1126. for(; ip != NULL; ip=ip->link)
  1127. if( ip->symId == symId )
  1128. return ip;
  1129. return NULL;
  1130. }
  1131. unsigned cmDspSysRegisterInstAttrSymbol( cmDspSysH_t h, unsigned symId )
  1132. {
  1133. cmDsp_t* p = _cmDspHandleToPtr(h);
  1134. cmDspInstSymId_t* ip;
  1135. // if this symbol is already registered
  1136. if((ip = _cmDspSysFindInstSymId(p,symId)) != NULL )
  1137. return ip->symId;
  1138. // try to find an unused symbol
  1139. ip = p->symIdList;
  1140. for(; ip != NULL; ip=ip->link)
  1141. if( ip->symId == cmInvalidId )
  1142. break;
  1143. // if no unused symbols were found then allocate a new one
  1144. if( ip == NULL )
  1145. {
  1146. ip = cmLhAllocZ(p->ctx.lhH, cmDspInstSymId_t, 1 );
  1147. ip->link = p->symIdList;
  1148. p->symIdList = ip;
  1149. }
  1150. ip->symId = symId;
  1151. return ip->symId;
  1152. }
  1153. unsigned cmDspSysRegisterInstAttrSymbolStr( cmDspSysH_t h, const cmChar_t* symLabel )
  1154. {
  1155. cmDsp_t* p = _cmDspHandleToPtr(h);
  1156. unsigned symId;
  1157. if((symId = cmSymTblRegisterSymbol(p->ctx.stH,symLabel)) == cmInvalidId )
  1158. return cmErrMsg(&p->err,kSymTblFailDspRC,"The instance attribute symbol '%s' could not be registered.",cmStringNullGuard(symLabel));
  1159. return cmDspSysRegisterInstAttrSymbol(h,symId);
  1160. }
  1161. cmDspRC_t cmDspSysAssignInstAttrSymbol( cmDspSysH_t h, cmDspInst_t* inst, unsigned symId )
  1162. {
  1163. cmDsp_t* p = _cmDspHandleToPtr(h);
  1164. return cmDspInstRegisterAttrSym(&p->ctx, inst, symId) != kOkDspRC;
  1165. }
  1166. unsigned cmDspSysAssignInstAttrSymbolStr( cmDspSysH_t h, cmDspInst_t* inst, const cmChar_t* symLabel )
  1167. {
  1168. unsigned symId = cmDspSysRegisterSymbol(h,symLabel);
  1169. if( cmDspSysAssignInstAttrSymbol( h, inst, symId) != kOkDspRC )
  1170. return cmInvalidId;
  1171. return symId;
  1172. }
  1173. cmDspRC_t cmDspSysRemoveInstAttrSymbol( cmDspSysH_t h, unsigned symId )
  1174. {
  1175. cmDsp_t* p = _cmDspHandleToPtr(h);
  1176. cmDspInstSymId_t* ip;
  1177. // if this symbol is already registered
  1178. if((ip = _cmDspSysFindInstSymId(p,symId)) == NULL )
  1179. return cmErrMsg(&p->err,kOkDspRC,"The instance attribute symbol '%s' is could not be found to be removed.",cmStringNullGuard(cmSymTblLabel(p->ctx.stH,symId)));
  1180. // mark a symbol recd as inactive by setting the symbol id to cmInalidId
  1181. ip->symId = cmInvalidId;
  1182. return kOkDspRC;
  1183. }
  1184. cmDspRC_t cmDspSysRemoveInstAttrSymbolStr( cmDspSysH_t h, const cmChar_t* symLabel )
  1185. {
  1186. cmDsp_t* p = _cmDspHandleToPtr(h);
  1187. unsigned symId;
  1188. if( (symId = cmSymTblId(p->ctx.stH, symLabel )) == cmInvalidId )
  1189. return cmErrMsg(&p->err,kSymNotFoundDspRC,"The instance attribute symbol '%s' does not exist and therefore cannot be removed.",cmStringNullGuard(symLabel));
  1190. return cmDspSysRemoveInstAttrSymbol(h, symId );
  1191. }
  1192. cmDspRC_t cmDspSysBroadcastValue( cmDspSysH_t h, unsigned instAttrSymId, const cmDspValue_t* valuePtr )
  1193. {
  1194. cmDsp_t* p = _cmDspHandleToPtr(h);
  1195. _cmDspInst_t* ip = p->instList;
  1196. for(; ip != NULL; ip = ip->linkPtr )
  1197. if( (instAttrSymId==cmInvalidId) || cmDspInstHasAttrSym(ip->instPtr,instAttrSymId))
  1198. ip->instPtr->sysRecvFunc(&p->ctx,ip->instPtr,instAttrSymId,valuePtr);
  1199. return kOkDspRC;
  1200. }
  1201. cmDspInst_t* cmDspSysAllocLabel( cmDspSysH_t h, const cmChar_t* label, unsigned alignId )
  1202. {
  1203. return cmDspSysAllocInst(h,"Label", NULL, 2, label, alignId);
  1204. }
  1205. cmDspRC_t _cmDspGetRsrcReal( cmDspSysH_t h, const cmChar_t* label, const cmChar_t* rsrcPath, cmReal_t* valPtr )
  1206. {
  1207. const cmChar_t* errLabelPtr = NULL;
  1208. if( cmJsonPathValues( cmDspSysPgmRsrcHandle(h),NULL,NULL,&errLabelPtr, rsrcPath, kRealTId, valPtr, NULL ) != kOkJsRC )
  1209. {
  1210. cmDsp_t* p = _cmDspHandleToPtr(h);
  1211. return cmErrMsg(&p->err,kRsrcNotFoundDspRC,"DSP instance allocation failed for '%s' and the initial value resource path '%s'.", cmStringNullGuard(label),cmStringNullGuard(rsrcPath));
  1212. }
  1213. return kOkDspRC;
  1214. }
  1215. 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 )
  1216. {
  1217. cmDspInst_t* inst;
  1218. if((inst = cmDspSysAllocInstS(h,"Scalar", presetGrpSymId, instLabel, 6, kNumberDuiId, min, max, step, val, label )) == NULL)
  1219. {
  1220. cmDsp_t* p = _cmDspHandleToPtr(h);
  1221. cmErrMsg(&p->err,kAllocInstFailDspRC,"Scalar UI control allocation failed for '%s'.", cmStringNullGuard(instLabel));
  1222. }
  1223. return inst;
  1224. }
  1225. cmDspInst_t* cmDspSysAllocScalar( cmDspSysH_t h, const cmChar_t* label, cmReal_t min, cmReal_t max, cmReal_t step, cmReal_t val )
  1226. { return _cmDspSysAllocScalar(h,cmInvalidId,label,min,max,step,val,label); }
  1227. cmChar_t* _cmDspSysFormLabel( cmDspSysH_t h, const cmChar_t* prefixLabel, const cmChar_t* label )
  1228. { return cmTsPrintfH(cmDspSysLHeap(h),"%s-%s",prefixLabel,label); }
  1229. 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 )
  1230. {
  1231. return _cmDspSysAllocScalar(h,presetGrpSymId,_cmDspSysFormLabel(h,prefixLabel,label),min,max,step,val,label);
  1232. }
  1233. cmDspInst_t* cmDspSysAllocScalarRsrc( cmDspSysH_t h, const cmChar_t* label, cmReal_t min, cmReal_t max, cmReal_t step, const cmChar_t* rsrcPath )
  1234. {
  1235. cmReal_t val;
  1236. if(_cmDspGetRsrcReal(h,label,rsrcPath,&val ) != kOkDspRC )
  1237. return NULL;
  1238. return cmDspSysAllocScalar(h,label,min,max,step,val);
  1239. }
  1240. 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 )
  1241. {
  1242. cmChar_t* lbl = cmTsPrintfH( cmDspSysLHeap(h), "%s-%s",prefixLabel,label);
  1243. return cmDspSysAllocInstArrayS( h, cnt, presetGroupSymId, "Scalar", lbl, NULL, 5, kNumberDuiId, min, max, step, val);
  1244. }
  1245. cmDspInst_t* _cmDspSysAllocButton( cmDspSysH_t h, unsigned btnTypeId, unsigned presetGroupSymId, const cmChar_t* instLabel, const cmChar_t* label, cmReal_t val, unsigned valSymId )
  1246. {
  1247. cmDspInst_t* inst;
  1248. if((inst = cmDspSysAllocInstS( h, "Button", presetGroupSymId, instLabel, 4, btnTypeId, val, valSymId, label)) == NULL )
  1249. {
  1250. cmDsp_t* p = _cmDspHandleToPtr(h);
  1251. cmErrMsg(&p->err,kAllocInstFailDspRC,"Button UI control allocation failed for '%s'.", cmStringNullGuard(label));
  1252. }
  1253. return inst;
  1254. }
  1255. cmDspInst_t* cmDspSysAllocButtonP( cmDspSysH_t h, const cmChar_t* prefixLabel, const cmChar_t* label, cmReal_t val )
  1256. {
  1257. return cmDspSysAllocButton(h,_cmDspSysFormLabel(h,prefixLabel,label),val);
  1258. }
  1259. cmDspInst_t* _cmDspSysAllocButtonRsrc( cmDspSysH_t h, unsigned typeId, unsigned presetGroupSymId, const cmChar_t* label, const cmChar_t* rsrcPath )
  1260. {
  1261. cmReal_t val;
  1262. if(_cmDspGetRsrcReal(h,label,rsrcPath,&val) != kOkDspRC )
  1263. return NULL;
  1264. return _cmDspSysAllocButton(h,typeId,presetGroupSymId,label,label,val,cmInvalidId);
  1265. }
  1266. cmDspInst_t* cmDspSysAllocButton( cmDspSysH_t h, const cmChar_t* label, cmReal_t val )
  1267. { return _cmDspSysAllocButton(h,kButtonDuiId,cmInvalidId,label,label,val,cmInvalidId); }
  1268. cmDspInst_t* cmDspSysAllocButtonRsrc( cmDspSysH_t h, const cmChar_t* label, const cmChar_t* rsrcPath )
  1269. { return _cmDspSysAllocButtonRsrc( h,kButtonDuiId,cmInvalidId,label,rsrcPath); }
  1270. cmDspInst_t* _cmDspSysAllocCheck( cmDspSysH_t h, unsigned presetGroupSymId, const cmChar_t* instLabel, const cmChar_t* label, cmReal_t val )
  1271. { return _cmDspSysAllocButton(h,kCheckDuiId,presetGroupSymId,instLabel,label,val,cmInvalidId); }
  1272. cmDspInst_t* cmDspSysAllocCheck( cmDspSysH_t h, const cmChar_t* label, cmReal_t val )
  1273. { return _cmDspSysAllocCheck(h,cmInvalidId,label,label,val); }
  1274. cmDspInst_t* cmDspSysAllocCheckP( cmDspSysH_t h, unsigned presetGroupSymId, const cmChar_t* prefixLabel, const cmChar_t* label, cmReal_t val )
  1275. { return _cmDspSysAllocCheck(h,presetGroupSymId,_cmDspSysFormLabel(h,prefixLabel,label),label,val); }
  1276. cmDspInst_t* cmDspSysAllocCheckRsrc( cmDspSysH_t h, const cmChar_t* label, const cmChar_t* rsrcPath )
  1277. { return _cmDspSysAllocButtonRsrc( h,kCheckDuiId, cmInvalidId, label,rsrcPath); }
  1278. cmDspInst_t* cmDspSysAllocMsgList( cmDspSysH_t h, const cmChar_t* fn, const cmChar_t* rsrcLabel, unsigned initSelIdx )
  1279. { return cmDspSysAllocInst( h, "MsgList", NULL, 3, rsrcLabel, fn, initSelIdx); }
  1280. 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 )
  1281. {
  1282. cmDspInst_t* inst;
  1283. cmChar_t* lbl;
  1284. if((inst = cmDspSysAllocInstS( h, "MsgList", presetGroupSymId, lbl = _cmDspSysFormLabel(h,preLabel,label), 3, rsrcLabel, fn, initSelIdx)) == NULL )
  1285. {
  1286. cmDsp_t* p = _cmDspHandleToPtr(h);
  1287. cmErrMsg(&p->err,kAllocInstFailDspRC,"Msg List UI control allocation failed for '%s'.", cmStringNullGuard(lbl));
  1288. }
  1289. return inst;
  1290. }
  1291. cmDspInst_t* cmDspSysAllocAudioIn( cmDspSysH_t h, unsigned chIdx, cmReal_t gain )
  1292. { return cmDspSysAllocInst( h, "AudioIn", NULL, 2, chIdx, gain ); }
  1293. const cmJsonNode_t* _cmDspSysFindAudioChMap( cmDspSysH_t h, const cmChar_t* rsrcLabel )
  1294. {
  1295. cmDsp_t* p = _cmDspHandleToPtr(h);
  1296. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(h);
  1297. assert( cmJsonIsValid(jsH) );
  1298. const cmJsonNode_t* np = NULL;
  1299. if(( np = cmJsonFindValue( jsH, rsrcLabel, cmJsonRoot(jsH), kArrayTId )) == NULL )
  1300. {
  1301. cmErrMsg(&p->err,kRsrcNotFoundDspRC,"The audio channel map resource '%s' was not found.", cmStringNullGuard(rsrcLabel));
  1302. return NULL;
  1303. }
  1304. unsigned chCnt = cmJsonChildCount(np);
  1305. if( chCnt == 0 )
  1306. {
  1307. cmErrMsg(&p->err,kInvalidArgDspRC,"The audio channel map resource '%s' is empty.",cmStringNullGuard(rsrcLabel));
  1308. return NULL;
  1309. }
  1310. return np;
  1311. }
  1312. cmDspRC_t _cmDspSysReadAudioChMap( cmDspSysH_t h, const cmChar_t* rsrcLabel, const cmJsonNode_t* mapNodePtr, unsigned* map, unsigned mapCnt )
  1313. {
  1314. cmDsp_t* p = _cmDspHandleToPtr(h);
  1315. unsigned i;
  1316. for(i=0; i<mapCnt; ++i)
  1317. {
  1318. const cmJsonNode_t* np;
  1319. if((np = cmJsonArrayElementC(mapNodePtr,i)) == NULL )
  1320. return cmErrMsg(&p->err,kInvalidArgDspRC,"The audio map element at index %i could not be accessed in the audio map:'%s'.",i,cmStringNullGuard(rsrcLabel));
  1321. if( cmJsonUIntValue(np,map + i ) != kOkJsRC )
  1322. 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));
  1323. }
  1324. return kOkDspRC;
  1325. }
  1326. cmDspInst_t** _cmDspSysAllocAudioRsrc( cmDspSysH_t h, const cmChar_t* classLabel, const cmChar_t* rsrcLabel, cmReal_t gain, unsigned* retChCntPtr )
  1327. {
  1328. cmDsp_t* p = _cmDspHandleToPtr(h);
  1329. const cmJsonNode_t* np;
  1330. if( retChCntPtr != NULL )
  1331. *retChCntPtr = 0;
  1332. //
  1333. if( rsrcLabel==NULL )
  1334. rsrcLabel = "audioInMap";
  1335. // find the map resource
  1336. if((np = _cmDspSysFindAudioChMap(h,rsrcLabel)) == NULL )
  1337. return NULL;
  1338. unsigned chCnt = cmJsonChildCount(np);
  1339. cmDspInst_t** a = cmLhAllocZ( p->lhH, cmDspInst_t*, chCnt );
  1340. unsigned chMap[ chCnt ];
  1341. // fill in the channel map - maps virtual channels to physical channels
  1342. if( _cmDspSysReadAudioChMap(h,rsrcLabel,np,chMap,chCnt) != kOkDspRC )
  1343. return NULL;
  1344. unsigned i;
  1345. for(i=0; i<chCnt; ++i)
  1346. if((a[i] = cmDspSysAllocInst( h, classLabel, NULL, 2, chMap[i], gain )) == NULL )
  1347. break;
  1348. if( i==chCnt )
  1349. {
  1350. if( retChCntPtr != NULL )
  1351. *retChCntPtr = chCnt;
  1352. return a;
  1353. }
  1354. return NULL;
  1355. }
  1356. cmDspInst_t** cmDspSysAllocAudioInAR( cmDspSysH_t h, const cmChar_t* rsrcLabel, cmReal_t gain, unsigned* retChCntPtr )
  1357. { return _cmDspSysAllocAudioRsrc(h,"AudioIn",rsrcLabel,gain,retChCntPtr); }
  1358. cmDspInst_t* cmDspSysAllocAudioOut(cmDspSysH_t h, unsigned chIdx, cmReal_t gain )
  1359. { return cmDspSysAllocInst( h, "AudioOut", NULL, 2, chIdx, gain ); }
  1360. cmDspInst_t** cmDspSysAllocAudioOutAR( cmDspSysH_t h, const cmChar_t* rsrcLabel, cmReal_t gain, unsigned* retChCntPtr )
  1361. { return _cmDspSysAllocAudioRsrc(h,"AudioOut",rsrcLabel,gain,retChCntPtr); }
  1362. 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 )
  1363. {
  1364. cmDsp_t* p = _cmDspHandleToPtr(h);
  1365. cmDspRC_t rc = kOkDspRC;
  1366. cmDspInst_t* netSendInstPtr;
  1367. unsigned dstNetNodeId;
  1368. _cmDspSrcConn_t* srcConnPtr = NULL;
  1369. // get the dest. network node id
  1370. if( (dstNetNodeId = cmUdpNetNodeLabelToId(p->netH, dstNetNodeLabel )) == cmInvalidId )
  1371. return cmErrMsg(&p->err,kNetNodeNotFoundDspRC,"The destination network node label '%s' was not found.",cmStringNullGuard(dstNetNodeLabel));
  1372. if((srcConnPtr = _cmDspSysNetCreateSrcConn( p, dstNetNodeId, dstInstLabel, dstVarLabel)) == NULL )
  1373. return rc;
  1374. // allocate a network sender
  1375. if((netSendInstPtr = cmDspSysAllocInst(h, "NetSend", NULL, 1, srcConnPtr )) == NULL )
  1376. return cmErrMsg(&p->err,kNetSendAllocFailDspRC,"The network sender DSP instance allocation failed.");
  1377. return cmDspSysInstallCb(h, srcInstPtr, srcVarLabel, netSendInstPtr, "in", NULL );
  1378. }
  1379. 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 )
  1380. {
  1381. cmDspRC_t rc = kOkDspRC;
  1382. unsigned i;
  1383. for(i=0; i<n; ++i)
  1384. {
  1385. const cmChar_t* label0 = srcVarPrefixStr;
  1386. const cmChar_t* label1 = dstVarPrefixStr;
  1387. if( n >= 2 )
  1388. {
  1389. label0 = cmDspSysPrintLabel( srcVarPrefixStr,i);
  1390. label1 = cmDspSysPrintLabel2(dstVarPrefixStr,dstOffs + i);
  1391. }
  1392. if((rc = cmDspSysInstallNetCb(h,srcInstPtr,label0,dstNetNodeLabel,dstInstLabel,label1)) != kOkDspRC )
  1393. break;
  1394. }
  1395. return rc;
  1396. }
  1397. 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 )
  1398. {
  1399. cmDspRC_t rc = kOkDspRC;
  1400. unsigned i;
  1401. for(i=0; i<dstCnt; ++i)
  1402. {
  1403. assert( map[i] < srcCnt );
  1404. const cmChar_t* label0 = cmDspSysPrintLabel( srcVarPrefixStr, map[i] );
  1405. const cmChar_t* label1 = cmDspSysPrintLabel2(dstVarPrefixStr, i );
  1406. if((rc = cmDspSysInstallNetCb(h,srcInstPtr,label0,dstNetNodeLabel,dstInstLabel,label1)) != kOkDspRC )
  1407. break;
  1408. }
  1409. return rc;
  1410. }
  1411. cmDspRC_t _cmDspRsrcPath( cmDspSysH_t h, cmChar_t** pathPtr, va_list vl )
  1412. {
  1413. unsigned i;
  1414. cmDspRC_t rc = kOkDspRC;;
  1415. cmDsp_t* p = _cmDspHandleToPtr(h);
  1416. cmChar_t* path = NULL;
  1417. unsigned maxArgCnt = 10;
  1418. *pathPtr = NULL;
  1419. for(i=0; i<maxArgCnt; ++i )
  1420. {
  1421. cmChar_t* str;
  1422. if((str = va_arg(vl,cmChar_t* )) == NULL )
  1423. break;
  1424. if( path != NULL )
  1425. path = cmTextAppendSS(path,"/");
  1426. path = cmTextAppendSS(path,str);
  1427. }
  1428. if( i >= maxArgCnt )
  1429. {
  1430. rc = cmErrMsg(&p->err,kJsonFailDspRC,"A resource path, beginning with '%25s', does not contain a terminating NULL.", cmStringNullGuard(path) );
  1431. goto errLabel;
  1432. }
  1433. // duplicate the string onto the program linked heap
  1434. *pathPtr = cmLhAllocStr( cmDspSysLHeap(h), path);
  1435. errLabel:
  1436. cmMemFree(path);
  1437. return rc;
  1438. }
  1439. cmDspRC_t _cmDspRsrcWritePath( cmDspSysH_t h, cmChar_t** pathStrRef, cmChar_t** varStrRef, va_list vl )
  1440. {
  1441. unsigned i;
  1442. cmDspRC_t rc = kOkDspRC;;
  1443. cmDsp_t* p = _cmDspHandleToPtr(h);
  1444. cmChar_t* path = NULL;
  1445. unsigned maxArgCnt = 10;
  1446. cmChar_t* prevStr = NULL;
  1447. *pathStrRef = NULL;
  1448. *varStrRef = NULL;
  1449. for(i=0; i<maxArgCnt; ++i )
  1450. {
  1451. cmChar_t* str;
  1452. if((str = va_arg(vl,cmChar_t* )) == NULL )
  1453. break;
  1454. if( prevStr != NULL )
  1455. {
  1456. if( path != NULL )
  1457. path = cmTextAppendSS(path,"/");
  1458. path = cmTextAppendSS(path,prevStr);
  1459. }
  1460. prevStr = str;
  1461. }
  1462. if( i >= maxArgCnt )
  1463. {
  1464. rc = cmErrMsg(&p->err,kJsonFailDspRC,"A resource path, beginning with '%25s', does not contain a terminating NULL.", cmStringNullGuard(path) );
  1465. goto errLabel;
  1466. }
  1467. // duplicate the string onto the program linked heap
  1468. if( path != NULL )
  1469. *pathStrRef = cmLhAllocStr( cmDspSysLHeap(h), path);
  1470. *varStrRef = prevStr;
  1471. errLabel:
  1472. cmMemFree(path);
  1473. return rc;
  1474. }
  1475. cmDspRC_t cmDspRsrcBoolV( cmDspSysH_t h, bool* vp, va_list vl )
  1476. {
  1477. assert(vp != NULL);
  1478. cmDsp_t* p = _cmDspHandleToPtr(h);
  1479. cmChar_t* path = NULL;
  1480. cmDspRC_t rc;
  1481. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1482. return rc;
  1483. if((rc = cmJsonPathToBool( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1484. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'bool' resource not found at path:'%s'.",cmStringNullGuard(path));
  1485. cmLhFree(cmDspSysLHeap(h),path);
  1486. return rc;
  1487. }
  1488. cmDspRC_t cmDspRsrcIntV( cmDspSysH_t h, int* 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 = cmJsonPathToInt( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1497. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'int' resource not found at path:'%s'.",cmStringNullGuard(path));
  1498. cmLhFree(cmDspSysLHeap(h),path);
  1499. return rc;
  1500. }
  1501. cmDspRC_t cmDspRsrcUIntV( cmDspSysH_t h, unsigned* vp, va_list vl )
  1502. {
  1503. assert(vp != NULL);
  1504. cmDsp_t* p = _cmDspHandleToPtr(h);
  1505. cmChar_t* path = NULL;
  1506. cmDspRC_t rc;
  1507. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1508. return rc;
  1509. if((rc = cmJsonPathToUInt( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1510. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'uint' resource not found at path:'%s'.",cmStringNullGuard(path));
  1511. cmLhFree(cmDspSysLHeap(h),path);
  1512. return rc;
  1513. }
  1514. cmDspRC_t cmDspRsrcDblV( cmDspSysH_t h, double* vp, va_list vl )
  1515. {
  1516. assert(vp != NULL);
  1517. cmDsp_t* p = _cmDspHandleToPtr(h);
  1518. cmChar_t* path = NULL;
  1519. cmDspRC_t rc;
  1520. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1521. return rc;
  1522. if((rc = cmJsonPathToReal( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1523. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'real' resource not found at path:'%s'.",cmStringNullGuard(path));
  1524. cmLhFree(cmDspSysLHeap(h),path);
  1525. return rc;
  1526. }
  1527. cmDspRC_t cmDspRsrcRealV( cmDspSysH_t h, cmReal_t* vp, va_list vl )
  1528. {
  1529. assert(vp != NULL);
  1530. cmDsp_t* p = _cmDspHandleToPtr(h);
  1531. cmChar_t* path = NULL;
  1532. cmDspRC_t rc;
  1533. double dval;
  1534. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1535. return rc;
  1536. if((rc = cmJsonPathToReal( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, &dval )) != kOkJsRC )
  1537. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'real' resource not found at path:'%s'.",cmStringNullGuard(path));
  1538. else
  1539. *vp = dval;
  1540. cmLhFree(cmDspSysLHeap(h),path);
  1541. return rc;
  1542. }
  1543. cmDspRC_t cmDspRsrcStringV( cmDspSysH_t h, const cmChar_t** vp, va_list vl )
  1544. {
  1545. assert(vp != NULL);
  1546. cmDsp_t* p = _cmDspHandleToPtr(h);
  1547. cmChar_t* path = NULL;
  1548. cmDspRC_t rc;
  1549. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1550. return rc;
  1551. if((rc = cmJsonPathToString( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1552. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'string' resource not found at path:'%s'.",cmStringNullGuard(path));
  1553. cmLhFree(cmDspSysLHeap(h),path);
  1554. return rc;
  1555. }
  1556. cmDspRC_t cmDspRsrcArrayCountV( cmDspSysH_t h, unsigned *n, va_list vl )
  1557. {
  1558. assert(n != NULL );
  1559. cmDsp_t* p = _cmDspHandleToPtr(h);
  1560. cmChar_t* path = NULL;
  1561. cmJsonNode_t* np = NULL;
  1562. cmDspRC_t rc;
  1563. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1564. return rc;
  1565. if((rc = cmJsonPathToArray( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, &np )) != kOkJsRC )
  1566. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'array' resource not found at path:'%s'.",cmStringNullGuard(path));
  1567. else
  1568. *n = cmJsonChildCount(np);
  1569. cmLhFree(cmDspSysLHeap(h),path);
  1570. return rc;
  1571. }
  1572. cmDspRC_t _cmDspRsrcArrayV( cmDspSysH_t h, unsigned* np, cmJsonNode_t** npp, cmChar_t** pathPtrPtr, va_list vl )
  1573. {
  1574. cmDsp_t* p = _cmDspHandleToPtr(h);
  1575. cmChar_t* path = NULL;
  1576. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(h);
  1577. cmDspRC_t rc;
  1578. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1579. return rc;
  1580. if((rc = cmJsonPathToArray( jsH, NULL, NULL, path, npp )) != kOkJsRC )
  1581. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'array' resource not found at path:'%s'.",cmStringNullGuard(path));
  1582. else
  1583. {
  1584. if((*np = cmJsonChildCount(*npp)) != 0 )
  1585. *pathPtrPtr = path;
  1586. }
  1587. cmLhFree(cmDspSysLHeap(h),path);
  1588. return rc;
  1589. }
  1590. cmDspRC_t cmDspRsrcBoolArrayV( cmDspSysH_t h, unsigned* np, bool** vpp, va_list vl )
  1591. {
  1592. assert(vpp!=NULL);
  1593. cmDsp_t* p = _cmDspHandleToPtr(h);
  1594. cmJsonNode_t* nodePtr = NULL;
  1595. unsigned n = 0;
  1596. cmChar_t* path = NULL;
  1597. unsigned i;
  1598. cmDspRC_t rc;
  1599. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1600. return rc;
  1601. bool* vp = *vpp;
  1602. vp = cmLhResizeN(p->ctx.lhH,bool,vp,n);
  1603. for(i=0; i<n && rc == kOkDspRC; ++i)
  1604. if( cmJsonBoolValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1605. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1606. *vpp = vp;
  1607. *np = n;
  1608. cmLhFree(cmDspSysLHeap(h),path);
  1609. return rc;
  1610. }
  1611. cmDspRC_t cmDspRsrcIntArrayV( cmDspSysH_t h, unsigned* np, int** vpp, va_list vl )
  1612. {
  1613. assert(vpp!=NULL);
  1614. cmDsp_t* p = _cmDspHandleToPtr(h);
  1615. cmJsonNode_t* nodePtr = NULL;
  1616. unsigned n = 0;
  1617. cmChar_t* path = NULL;
  1618. unsigned i;
  1619. cmDspRC_t rc;
  1620. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1621. return rc;
  1622. int* vp = *vpp;
  1623. vp = cmLhResizeN(p->ctx.lhH,int,vp,n);
  1624. for(i=0; i<n && rc == kOkDspRC; ++i)
  1625. if( cmJsonIntValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1626. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1627. *vpp = vp;
  1628. *np = n;
  1629. cmLhFree(cmDspSysLHeap(h),path);
  1630. return rc;
  1631. }
  1632. cmDspRC_t cmDspRsrcUIntArrayV( cmDspSysH_t h, unsigned* np, unsigned** vpp, va_list vl )
  1633. {
  1634. assert(vpp!=NULL);
  1635. cmDsp_t* p = _cmDspHandleToPtr(h);
  1636. cmJsonNode_t* nodePtr = NULL;
  1637. unsigned n = 0;
  1638. cmChar_t* path = NULL;
  1639. unsigned i;
  1640. cmDspRC_t rc;
  1641. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1642. return rc;
  1643. unsigned* vp = *vpp;
  1644. vp = cmLhResizeN(p->ctx.lhH,unsigned,vp,n);
  1645. for(i=0; i<n && rc == kOkDspRC; ++i)
  1646. if( cmJsonUIntValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1647. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1648. *vpp = vp;
  1649. *np = n;
  1650. cmLhFree(cmDspSysLHeap(h),path);
  1651. return rc;
  1652. }
  1653. cmDspRC_t cmDspRsrcDblArrayV( cmDspSysH_t h, unsigned* np, double** vpp, va_list vl )
  1654. {
  1655. assert(vpp!=NULL);
  1656. cmDsp_t* p = _cmDspHandleToPtr(h);
  1657. cmJsonNode_t* nodePtr = NULL;
  1658. unsigned n = 0;
  1659. cmChar_t* path = NULL;
  1660. unsigned i;
  1661. cmDspRC_t rc;
  1662. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1663. return rc;
  1664. double* vp = *vpp;
  1665. vp = cmLhResizeN(p->ctx.lhH,double,vp,n);
  1666. for(i=0; i<n && rc == kOkDspRC; ++i)
  1667. if( cmJsonRealValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1668. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1669. *vpp = vp;
  1670. *np = n;
  1671. cmLhFree(cmDspSysLHeap(h),path);
  1672. return rc;
  1673. }
  1674. cmDspRC_t cmDspRsrcRealArrayV( cmDspSysH_t h, unsigned* np, cmReal_t** vpp, va_list vl )
  1675. {
  1676. assert(vpp!=NULL);
  1677. cmDsp_t* p = _cmDspHandleToPtr(h);
  1678. cmJsonNode_t* nodePtr = NULL;
  1679. unsigned n = 0;
  1680. cmChar_t* path = NULL;
  1681. unsigned i;
  1682. cmDspRC_t rc;
  1683. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1684. return rc;
  1685. cmReal_t* vp = *vpp;
  1686. vp = cmLhResizeN(p->ctx.lhH,cmReal_t,vp,n);
  1687. for(i=0; i<n && rc == kOkDspRC; ++i)
  1688. {
  1689. double v;
  1690. if( cmJsonRealValue( cmJsonArrayElementC(nodePtr,i), &v ) != kOkJsRC )
  1691. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1692. vp[i] = v;
  1693. }
  1694. *vpp = vp;
  1695. *np = n;
  1696. cmLhFree(cmDspSysLHeap(h),path);
  1697. return rc;
  1698. }
  1699. cmDspRC_t cmDspRsrcStringArrayV(cmDspSysH_t h, unsigned* np, const cmChar_t*** vpp, va_list vl )
  1700. {
  1701. assert(vpp!=NULL);
  1702. cmDsp_t* p = _cmDspHandleToPtr(h);
  1703. cmJsonNode_t* nodePtr = NULL;
  1704. unsigned n = 0;
  1705. cmChar_t* path = NULL;
  1706. unsigned i;
  1707. cmDspRC_t rc;
  1708. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1709. return rc;
  1710. const cmChar_t** vp = *vpp;
  1711. vp = cmLhResizeN(p->ctx.lhH,const cmChar_t*,vp,n);
  1712. for(i=0; i<n && rc == kOkDspRC; ++i)
  1713. if( cmJsonStringValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1714. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1715. *vpp = vp;
  1716. *np = n;
  1717. cmLhFree(cmDspSysLHeap(h),path);
  1718. return rc;
  1719. }
  1720. cmDspRC_t cmDspRsrcInt( cmDspSysH_t h, int* vp, ... )
  1721. {
  1722. va_list vl;
  1723. va_start(vl,vp);
  1724. cmDspRC_t rc = cmDspRsrcIntV(h,vp,vl);
  1725. va_end(vl);
  1726. return rc;
  1727. }
  1728. cmDspRC_t cmDspRsrcUInt( cmDspSysH_t h, unsigned* vp, ... )
  1729. {
  1730. va_list vl;
  1731. va_start(vl,vp);
  1732. cmDspRC_t rc = cmDspRsrcUIntV(h,vp,vl);
  1733. va_end(vl);
  1734. return rc;
  1735. }
  1736. cmDspRC_t cmDspRsrcDbl( cmDspSysH_t h, double* vp, ... )
  1737. {
  1738. va_list vl;
  1739. va_start(vl,vp);
  1740. cmDspRC_t rc = cmDspRsrcDblV(h,vp,vl);
  1741. va_end(vl);
  1742. return rc;
  1743. }
  1744. cmDspRC_t cmDspRsrcReal( cmDspSysH_t h, cmReal_t* vp, ... )
  1745. {
  1746. va_list vl;
  1747. va_start(vl,vp);
  1748. cmDspRC_t rc = cmDspRsrcRealV(h,vp,vl);
  1749. va_end(vl);
  1750. return rc;
  1751. }
  1752. cmDspRC_t cmDspRsrcString( cmDspSysH_t h, const cmChar_t** vp, ... )
  1753. {
  1754. va_list vl;
  1755. va_start(vl,vp);
  1756. cmDspRC_t rc = cmDspRsrcStringV(h,vp,vl);
  1757. va_end(vl);
  1758. return rc;
  1759. }
  1760. cmDspRC_t cmDspRsrcArrayCount( cmDspSysH_t h, unsigned *np, ... )
  1761. {
  1762. va_list vl;
  1763. va_start(vl,np);
  1764. cmDspRC_t rc = cmDspRsrcArrayCountV(h,np,vl);
  1765. va_end(vl);
  1766. return rc;
  1767. }
  1768. cmDspRC_t cmDspRsrcBoolArray( cmDspSysH_t h, unsigned* np, bool** vpp, ... )
  1769. {
  1770. va_list vl;
  1771. va_start(vl,vpp);
  1772. cmDspRC_t rc = cmDspRsrcBoolArrayV(h,np,vpp,vl);
  1773. va_end(vl);
  1774. return rc;
  1775. }
  1776. cmDspRC_t cmDspRsrcIntArray( cmDspSysH_t h, unsigned* np, int** vpp, ... )
  1777. {
  1778. va_list vl;
  1779. va_start(vl,vpp);
  1780. cmDspRC_t rc = cmDspRsrcIntArrayV(h,np,vpp,vl);
  1781. va_end(vl);
  1782. return rc;
  1783. }
  1784. cmDspRC_t cmDspRsrcUIntArray( cmDspSysH_t h, unsigned* np, unsigned** vpp, ... )
  1785. {
  1786. va_list vl;
  1787. va_start(vl,vpp);
  1788. cmDspRC_t rc = cmDspRsrcUIntArrayV(h,np,vpp,vl);
  1789. va_end(vl);
  1790. return rc;
  1791. }
  1792. cmDspRC_t cmDspRsrcDblArray( cmDspSysH_t h, unsigned* np, double** vpp, ... )
  1793. {
  1794. va_list vl;
  1795. va_start(vl,vpp);
  1796. cmDspRC_t rc = cmDspRsrcDblArrayV(h,np,vpp,vl);
  1797. va_end(vl);
  1798. return rc;
  1799. }
  1800. cmDspRC_t cmDspRsrcRealArray( cmDspSysH_t h, unsigned* np, cmReal_t** vpp, ... )
  1801. {
  1802. va_list vl;
  1803. va_start(vl,vpp);
  1804. cmDspRC_t rc = cmDspRsrcRealArrayV(h,np,vpp,vl);
  1805. va_end(vl);
  1806. return rc;
  1807. }
  1808. cmDspRC_t cmDspRsrcStringArray( cmDspSysH_t h, unsigned* np, const cmChar_t*** vpp, ... )
  1809. {
  1810. va_list vl;
  1811. va_start(vl,vpp);
  1812. cmDspRC_t rc = cmDspRsrcStringArrayV(h,np,vpp,vl);
  1813. va_end(vl);
  1814. return rc;
  1815. }
  1816. cmDspRC_t _cmDspWritePathAndParent( cmDspSysH_t h, cmChar_t** pathRef, cmChar_t** varLabelRef, cmJsonNode_t** parentRef, va_list vl )
  1817. {
  1818. cmDsp_t* p = _cmDspHandleToPtr(h);
  1819. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(h);
  1820. cmDspRC_t rc;
  1821. if((rc = _cmDspRsrcWritePath(h,pathRef,varLabelRef,vl)) != kOkDspRC )
  1822. goto errLabel;
  1823. if( *pathRef == NULL )
  1824. *parentRef = cmJsonRoot(jsH);
  1825. else
  1826. *parentRef = cmJsonFindPathValue( jsH, *pathRef, cmJsonRoot(jsH), kPairTId );
  1827. if( *parentRef == NULL )
  1828. {
  1829. rc = cmErrMsg(&p->err,kJsonFailDspRC,"The parent object '%s' for variable '%s' could not be found.",cmStringNullGuard(*pathRef),cmStringNullGuard(*varLabelRef));
  1830. goto errLabel;
  1831. }
  1832. errLabel:
  1833. return rc;
  1834. }
  1835. cmDspRC_t cmDspRsrcWriteStringV( cmDspSysH_t h, const cmChar_t* v, va_list vl )
  1836. {
  1837. cmDsp_t* p = _cmDspHandleToPtr(h);
  1838. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(h);
  1839. cmChar_t* varStr = NULL;
  1840. cmChar_t* pathStr = NULL;
  1841. cmJsonNode_t* parentObjNodePtr = NULL;
  1842. cmDspRC_t rc;
  1843. if((rc = _cmDspWritePathAndParent(h, &pathStr, &varStr, &parentObjNodePtr, vl )) != kOkDspRC )
  1844. goto errLabel;
  1845. if( cmJsonInsertOrReplacePairString( jsH, parentObjNodePtr, varStr, kStringTId, v ) != kOkJsRC )
  1846. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Write 'string' resource value failed for path '%s' and variable '%s'",cmStringNullGuard(pathStr),cmStringNullGuard(varStr));
  1847. errLabel:
  1848. if( pathStr != NULL )
  1849. cmLhFree(cmDspSysLHeap(h),pathStr);
  1850. return rc;
  1851. }
  1852. cmDspRC_t cmDspRsrcWriteString( cmDspSysH_t h, const cmChar_t* v, ... )
  1853. {
  1854. va_list vl;
  1855. va_start(vl,v);
  1856. cmDspRC_t rc = cmDspRsrcWriteStringV(h,v,vl);
  1857. va_end(vl);
  1858. return rc;
  1859. }