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 78KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573
  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. // This function is assigns a unique symbol id (cmDspInst_t.symId)
  247. // to all DSP instances whose symId is set to cmInvalidId.
  248. cmDspRC_t _cmDspSysAssignUniqueInstSymId( cmDsp_t* p )
  249. {
  250. typedef struct class_str
  251. {
  252. const cmChar_t* label;
  253. unsigned cnt;
  254. struct class_str* link;
  255. } class_t;
  256. cmDspRC_t rc = kOkDspRC;
  257. class_t* map = NULL;
  258. _cmDspInst_t* dip = p->instList;
  259. for(; dip!=NULL; dip=dip->linkPtr)
  260. {
  261. cmDspInst_t* ip = dip->instPtr;
  262. // if this instance does not have a valid symbol id then create one
  263. if( ip->symId == cmInvalidId )
  264. {
  265. // look for the instance class in map[]
  266. class_t* mp = map;
  267. for(; mp!=NULL; mp=mp->link)
  268. if( strcmp(ip->classPtr->labelStr,mp->label) == 0 )
  269. break;
  270. // if map[] does not yet have a recd for this class ....
  271. if( mp == NULL )
  272. {
  273. mp = cmMemAllocZ(class_t,1); // ... then make one
  274. mp->label = ip->classPtr->labelStr;
  275. mp->link = map;
  276. map = mp;
  277. }
  278. // generate a new unique symbol
  279. while( ip->symId==cmInvalidId && mp->cnt != cmInvalidId )
  280. {
  281. // increment the instance count for this class
  282. mp->cnt += 1;
  283. // form a symbol label
  284. cmChar_t* idStr = cmTsPrintfP(NULL,"%s-%i",ip->classPtr->labelStr,mp->cnt);
  285. // register the new symbol
  286. unsigned symId = cmSymTblRegisterSymbol(p->ctx.stH,idStr);
  287. cmMemFree(idStr); // the symbol label is no longer need
  288. // if the symbol has not yet been used then it must be unique
  289. if( _cmDspSysInstSymIdToPtr(p,symId) == NULL )
  290. ip->symId = symId;
  291. else
  292. cmSymTblRemove(p->ctx.stH,symId); // ... otherwise remove the symbol and try again
  293. }
  294. // check for the very unlikely case that no unique symbol could be generated
  295. if(mp->cnt == cmInvalidId )
  296. rc = cmErrMsg(&p->err,kSymNotFoundDspRC,"All DSP instances were not assigned a unique symbol id.");
  297. }
  298. }
  299. // free the class list
  300. class_t* mp = map;
  301. while( mp != NULL )
  302. {
  303. class_t* np = mp->link;
  304. cmMemFree(mp);
  305. mp = np;
  306. }
  307. return rc;
  308. }
  309. cmDspRC_t cmDspSysLoad( cmDspSysH_t h, cmAudioSysCtx_t* asCtx, unsigned pgmIdx )
  310. {
  311. cmDspRC_t rc;
  312. cmDsp_t* p = _cmDspHandleToPtr(h);
  313. p->pgmIdx = cmInvalidIdx;
  314. if( pgmIdx >= cmDspSysPgmCount(h) )
  315. return cmErrMsg(&p->err,kInvalidPgmIdxDspRC,"%i is not a valid program index.",pgmIdx);
  316. // unload the previously loaded DSP program
  317. if((rc = cmDspSysUnload(h)) != kOkDspRC )
  318. return rc;
  319. // assign the cmAudioSysCtx_t context recd
  320. p->ctx.ctx = asCtx;
  321. _cmDspSysPgm_t* arr = _cmDspSysPgmArrayBase();
  322. // form the resource file name
  323. if((p->rsrcFn = cmFsMakeFn(cmFsPrefsDir(),arr[pgmIdx].label,"js",NULL)) == NULL )
  324. {
  325. rc = cmErrMsg(&p->err,kFileSysFailDspRC,"Resource file name formation failed.");
  326. goto errLabel;
  327. }
  328. // if the pgm specific resource file already exists
  329. if( cmFsIsFile(p->rsrcFn) )
  330. {
  331. // open and parse the resource file
  332. if( cmJsonInitializeFromFile(&p->ctx.rsrcJsH,p->rsrcFn,&p->cmCtx) != kOkJsRC )
  333. {
  334. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Resource file open failed.");
  335. goto errLabel;
  336. }
  337. }
  338. else // ... the resource file does not currently exists - create it
  339. {
  340. cmJsonNode_t* rootPtr;
  341. if( cmJsonInitialize(&p->ctx.rsrcJsH,&p->cmCtx) != kOkJsRC )
  342. {
  343. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Resource object allocate failed.");
  344. goto errLabel;
  345. }
  346. if((rootPtr = cmJsonCreateObject(p->ctx.rsrcJsH, NULL )) == NULL )
  347. {
  348. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Resource object root allocate failed.");
  349. goto errLabel;
  350. }
  351. }
  352. // create a linked heap for use by the DSP instances
  353. if(cmLHeapIsValid( p->ctx.lhH = cmLHeapCreate(1024,&p->cmCtx)) == false)
  354. return cmErrMsg(&p->err,kLHeapFailDspRC,"DSP instance linked heap intiialization failed.");
  355. // initialize a JSON object for use by the DSP instances
  356. if( cmJsonInitialize(&p->ctx.jsH,&p->cmCtx) != kOkJsRC )
  357. {
  358. rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON initialization failed.");
  359. goto errLabel;
  360. }
  361. // create a symbol table for use by the DSP instance (note that it uses the system sym. tbl. as it's parent)
  362. if( cmSymTblIsValid( p->ctx.stH = cmSymTblCreate(p->stH, cmDspSys_PARENT_SYM_TBL_BASE_ID, &p->cmCtx)) == false )
  363. {
  364. rc = cmErrMsg(&p->err,kSymTblFailDspRC,"DSP instance symbol table initialization failed.");
  365. goto errLabel;
  366. }
  367. // load the preset mgr
  368. if((rc = _cmDspPresetLoad(&p->pm,&p->cmCtx,&p->err,p->ctx.lhH,p->ctx.stH,arr[pgmIdx].label)) != kOkDspRC )
  369. {
  370. rc = cmErrMsg(&p->err,rc,"Preset manager load failed.");
  371. goto errLabel;
  372. }
  373. // setup the DSP network components
  374. if((rc = _cmDspSysNetPreLoad(p)) != kOkDspRC )
  375. goto errLabel;
  376. p->ctx.cycleCnt = 0;
  377. p->ctx._disableSymId = cmSymTblRegisterStaticSymbol(p->ctx.stH,"_disable");
  378. p->ctx._enableSymId = cmSymTblRegisterStaticSymbol(p->ctx.stH,"_enable");
  379. void** vpp = &arr[ pgmIdx ].userPtr;
  380. cmDspPgmFunc_t pgm = arr[ pgmIdx ].loadFunc;
  381. // allocate and connect user defined signal processing network
  382. if((rc = pgm(h,vpp)) == kOkDspRC )
  383. if((rc = cmErrLastRC(&p->err)) != kOkDspRC )
  384. {
  385. cmErrMsg(&p->err,rc,"User DSP system load failed.");
  386. goto errLabel;
  387. }
  388. p->pgmIdx = pgmIdx;
  389. // enter sync mode
  390. if((rc = _cmDspSysNetSync(p)) != kOkDspRC )
  391. {
  392. cmErrMsg(&p->err,rc,"DSP system sync failed.");
  393. goto errLabel;
  394. }
  395. rc = _cmDspSysAssignUniqueInstSymId(p);
  396. errLabel:
  397. if( rc != kOkDspRC )
  398. cmDspSysUnload(h);
  399. return rc;
  400. }
  401. cmDspRC_t cmDspSysUnload( cmDspSysH_t h )
  402. {
  403. if( cmDspSysIsValid(h) == false )
  404. return kOkDspRC;
  405. cmDspRC_t rc = kOkDspRC;
  406. cmDsp_t* p = _cmDspHandleToPtr(h);
  407. _cmDspInst_t* ip = p->instList;
  408. // call the DSP network unload function - this function is provided
  409. // by the client network to release any resources obtained when
  410. // the network was created
  411. if( p->pgmIdx != cmInvalidIdx )
  412. {
  413. _cmDspSysPgm_t* arr = _cmDspSysPgmArrayBase();
  414. void** vpp = &arr[ p->pgmIdx ].userPtr;
  415. cmDspPgmFunc_t pgm = arr[ p->pgmIdx ].unloadFunc;
  416. if( pgm != NULL )
  417. if((rc = pgm(h,vpp)) == kOkDspRC )
  418. if((rc = cmErrLastRC(&p->err)) != kOkDspRC )
  419. cmErrMsg(&p->err,rc,"User DSP system unload failed.");
  420. }
  421. p->pgmIdx = cmInvalidIdx;
  422. // unload the networking components
  423. _cmDspSysNetUnload(p);
  424. // free the DSP instances
  425. for(; ip!=NULL; ip=ip->linkPtr)
  426. if((rc = _cmDspInstFree(&p->ctx,ip->instPtr)) != kOkDspRC )
  427. rc = cmErrMsg(&p->err,kInstFinalFailDspRC,"An attempt to free DSP instance '%s' id:%i failed.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  428. p->instList = NULL;
  429. // unload the preset manager
  430. if((rc = _cmDspPresetUnload(&p->pm,&p->cmCtx)) != kOkDspRC)
  431. rc = cmErrMsg(&p->err,rc,"Preset manager 'unload' failed.");
  432. // finalize the pgm specific JSON tree
  433. if( cmJsonIsValid(p->ctx.rsrcJsH))
  434. {
  435. // if the JSON tree has been modified.
  436. if( cmJsonIsModified(p->ctx.rsrcJsH) )
  437. if( cmJsonWrite(p->ctx.rsrcJsH, cmJsonRoot(p->ctx.rsrcJsH), p->rsrcFn ) != kOkJsRC )
  438. rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON resource file write failed on '%s'.",cmStringNullGuard(p->rsrcFn));
  439. if( cmJsonFinalize(&p->ctx.rsrcJsH) != kOkJsRC )
  440. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Resource JSON finalization failed.");
  441. }
  442. // release the JSON file name
  443. if( p->rsrcFn != NULL )
  444. {
  445. cmFsFreeFn(p->rsrcFn); // free the resource file name
  446. p->rsrcFn = NULL;
  447. }
  448. // tell the DSP net system that the pgm has been unloaded
  449. _cmDspSysNetUnload(p);
  450. // elimnate the linked heap, system table, and json object used by the DSP instance objects
  451. if( cmLHeapIsValid(p->ctx.lhH) )
  452. cmLHeapDestroy(&p->ctx.lhH);
  453. if( cmJsonIsValid(p->ctx.jsH) )
  454. if( cmJsonFinalize(&p->ctx.jsH) != kOkJsRC )
  455. rc = cmErrMsg(&p->err,kJsonFailDspRC,"DSP instance JSON object finalization failed.");
  456. if( cmSymTblIsValid(p->ctx.stH) )
  457. cmSymTblDestroy(&p->ctx.stH);
  458. return rc;
  459. }
  460. cmDspRC_t cmDspSysReset( cmDspSysH_t h )
  461. {
  462. cmDsp_t* p = _cmDspHandleToPtr(h);
  463. cmDspRC_t rc = kOkDspRC;
  464. // call reset on each of the instances
  465. _cmDspInst_t* ip = p->instList;
  466. for(; ip != NULL; ip = ip->linkPtr )
  467. if( ip->instPtr->resetFunc != NULL )
  468. {
  469. if( ip->instPtr->resetFunc(&p->ctx,ip->instPtr,NULL) != kOkDspRC )
  470. rc = cmErrMsg(&p->err,kInstResetFailDspRC,"Reset failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  471. ip->instPtr->flags = 0;
  472. }
  473. // send the _reset symbol to every instance which has the _reset attribute symbol
  474. if( rc == kOkDspRC )
  475. {
  476. cmDspValue_t v;
  477. unsigned resetSymId = cmDspSysRegisterStaticSymbol(h,"_reset");
  478. cmDsvSetSymbol(&v,resetSymId);
  479. rc = cmDspSysBroadcastValue(h, resetSymId, &v);
  480. }
  481. return rc;
  482. }
  483. // Handle msg's arriving from the UI
  484. cmDspRC_t _cmDspSysHandleUiMsg(cmDsp_t* p, _cmDspInst_t* ip, const void* msgPtr, unsigned msgByteCnt)
  485. {
  486. cmDspRC_t rc = kOkDspRC;
  487. cmDspUiHdr_t* h = (cmDspUiHdr_t*)msgPtr;
  488. cmDspEvt_t e;
  489. assert( h->uiId == kUiSelAsId );
  490. // the only source of msg's (at the time of this writing) is the UI so:
  491. // Mark the msg as a UI event and set the kUiEchoDspFl if the UI has requested duplex operation.
  492. e.flags = kUiDspFl | (cmIsFlag(h->flags,kDuplexDuiFl) ? kUiEchoDspFl : 0);
  493. e.srcInstPtr = NULL; // the UI has no source instance
  494. e.srcVarId = h->selId; // set the event srcVarId to the UI selId
  495. e.dstVarId = h->instVarId; // identify the dst inst variable
  496. e.dstDataPtr = NULL; // UI msg's don't have custom data (yet)
  497. e.valuePtr = &h->value; //
  498. // if the value associated with this msg is a mtx then set
  499. // its mtx data area pointer to just after the msg header.
  500. if( cmDsvIsMtx(&h->value) )
  501. h->value.u.m.u.vp = ((char*)msgPtr) + sizeof(cmDspUiHdr_t);
  502. else
  503. rc = cmDsvDeserializeInPlace(&h->value,msgByteCnt-sizeof(cmDspUiHdr_t));
  504. if( rc != kOkDsvRC )
  505. cmErrMsg(&p->err,kSerializeUiMsgFailDspRC,"DSP system receive Deserialize failed.");
  506. // find the dest. DSP instance for this msg
  507. for(; ip != NULL; ip = ip->linkPtr )
  508. if( ip->instPtr->id == h->instId )
  509. {
  510. // send the msg to the DSP dst. instance
  511. if( ip->instPtr->recvFunc != NULL )
  512. if( ip->instPtr->recvFunc(&p->ctx,ip->instPtr,&e) != kOkDspRC )
  513. rc = cmErrMsg(&p->err,kInstMsgRcvFailDspRC,"Message recv failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  514. break;
  515. }
  516. if( ip == NULL )
  517. rc = cmErrMsg(&p->err,kInstNotFoundDspRC,"No DSP instance with id %i was found during message delivery.",h->instId);
  518. return rc;
  519. }
  520. // Recv msg's arriving from the audio engine.
  521. cmDspRC_t cmDspSysRcvMsg( cmDspSysH_t h, cmAudioSysCtx_t* asCtx, const void* msgPtr, unsigned msgByteCnt, unsigned srcNetNodeId )
  522. {
  523. cmDsp_t* p = _cmDspHandleToPtr(h);
  524. _cmDspInst_t* ip = p->instList;
  525. if( msgByteCnt == 0 && asCtx->audioRateFl==true)
  526. {
  527. cmTimeSpec_t t0,t1;
  528. cmTimeGet(&t0);
  529. for(; ip != NULL; ip = ip->linkPtr )
  530. if( ip->instPtr->execFunc != NULL && cmIsFlag(ip->instPtr->flags,kDisableExecInstFl)==false )
  531. {
  532. if( ip->instPtr->execFunc(&p->ctx,ip->instPtr,NULL) != kOkDspRC )
  533. cmErrMsg(&p->err,kInstExecFailDspRC,"Execution failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  534. //printf("%i %s\n",p->ctx.cycleCnt,ip->instPtr->classPtr->labelStr);
  535. }
  536. cmTimeGet(&t1);
  537. p->ctx.execDurUsecs = cmTimeElapsedMicros(&t0,&t1);
  538. ++p->ctx.cycleCnt;
  539. }
  540. else
  541. {
  542. unsigned* hdr = (unsigned*)msgPtr;
  543. // the msg selector is the second field in the data packet (following the audio system sub-system id)
  544. //const unsigned msgTypeSelId = ((const unsigned*)msgPtr)[1];
  545. switch( hdr[1] )
  546. {
  547. case kUiSelAsId:
  548. _cmDspSysHandleUiMsg(p,ip,msgPtr,msgByteCnt);
  549. break;
  550. case kNetSyncSelAsId:
  551. _cmDspSysNetRecv(p, (const cmDspNetMsg_t*)msgPtr, msgByteCnt, srcNetNodeId );
  552. break;
  553. case kMidiMsgArraySelAsId:
  554. {
  555. cmMidiPacket_t pkt;
  556. cmDspValue_t v;
  557. pkt.cbDataPtr = NULL;
  558. pkt.devIdx = hdr[2];
  559. pkt.portIdx = hdr[3];
  560. pkt.msgCnt = hdr[4];
  561. pkt.msgArray = (cmMidiMsg*)(hdr + 5);
  562. unsigned midiSymId = cmDspSysRegisterStaticSymbol(h,"_midi");
  563. v.u.m.u.vp = &pkt;
  564. cmDspSysBroadcastValue(h, midiSymId, &v);
  565. /*
  566. // data format for MIDI messages
  567. { kMidiMsgArraytSelAsId, devIdx, portIdx, msgCnt, msgArray[msgCnt] }
  568. where each msgArray[] record is a cmMidiMsg record.
  569. */
  570. }
  571. break;
  572. }
  573. }
  574. // report any error - this should stop further calls to the DSP process
  575. return cmErrLastRC(&p->err);
  576. }
  577. unsigned cmDspSysSyncState( cmDspSysH_t h )
  578. {
  579. cmDsp_t* p = _cmDspHandleToPtr(h);
  580. return p->syncState;
  581. }
  582. cmDspRC_t cmDspSysPrintPgm( cmDspSysH_t h, const cmChar_t* outFn )
  583. {
  584. cmDsp_t* p = _cmDspHandleToPtr(h);
  585. cmDspRC_t rc = kOkDspRC;
  586. cmJsonH_t jsH = cmJsonNullHandle;
  587. if( cmJsonInitialize(&jsH, &p->cmCtx ) != kOkJsRC )
  588. return cmErrMsg(&p->err,kJsonFailDspRC,"JSON output object create failed.");
  589. // create the root object
  590. cmJsonNode_t* onp = cmJsonCreateObject(jsH,NULL);
  591. assert( onp != NULL );
  592. // create the instance array
  593. cmJsonNode_t* iap = cmJsonInsertPairArray(jsH, cmJsonRoot(jsH), "inst_array" );
  594. assert( iap != NULL );
  595. // create the connection array
  596. cmJsonNode_t* cap = cmJsonInsertPairArray(jsH, cmJsonRoot(jsH), "conn_array" );
  597. assert( cap != NULL );
  598. _cmDspInst_t* dip = p->instList;
  599. for(; dip!=NULL; dip=dip->linkPtr)
  600. {
  601. cmDspInst_t* ip = dip->instPtr;
  602. onp = cmJsonCreateObject(jsH,iap);
  603. if( cmJsonInsertPairs(jsH, onp,
  604. "class", kStringTId, ip->classPtr->labelStr,
  605. "label", kStringTId, cmSymTblLabel(p->ctx.stH,ip->symId),
  606. "id", kIntTId, ip->symId,
  607. NULL ) != kOkJsRC )
  608. {
  609. rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON DSP instance create failed.");
  610. goto errLabel;
  611. }
  612. unsigned i;
  613. for(i=0; i<ip->varCnt; ++i)
  614. {
  615. const cmDspVar_t* v = ip->varArray + i;
  616. cmDspCb_t* cp = v->cbList;
  617. for(; cp!=NULL; cp=cp->linkPtr)
  618. {
  619. const cmDspVar_t* dvar = cmDspVarIdToCPtr(cp->dstInstPtr, cp->dstVarId );
  620. onp = cmJsonCreateObject(jsH,cap);
  621. assert(dvar != NULL);
  622. if(cmJsonInsertPairs(jsH,onp,
  623. "sid", kStringTId, cmSymTblLabel(p->ctx.stH,ip->symId),
  624. "svar", kStringTId, cmSymTblLabel(p->ctx.stH,v->symId),
  625. "did", kStringTId, cmSymTblLabel(p->ctx.stH,cp->dstInstPtr->symId),
  626. "dvar", kStringTId, cmSymTblLabel(p->ctx.stH,dvar->symId),
  627. NULL ) != kOkJsRC )
  628. {
  629. rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON DSP connect create failed.");
  630. goto errLabel;
  631. }
  632. }
  633. }
  634. }
  635. if( cmJsonWrite(jsH,NULL,outFn) != kOkJsRC )
  636. {
  637. rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON file write failed on '%s.",cmStringNullGuard(outFn));
  638. goto errLabel;
  639. }
  640. if( cmJsonFinalize(&jsH) != kOkJsRC )
  641. rc = cmErrMsg(&p->err,kJsonFailDspRC,"JSON tree release failed.");
  642. errLabel:
  643. return rc;
  644. }
  645. unsigned cmDspSysPresetGroupCount( cmDspSysH_t h )
  646. {
  647. cmDsp_t* p = _cmDspHandleToPtr(h);
  648. return _cmDspPresetGroupCount(&p->pm);
  649. }
  650. unsigned cmDspSysPresetGroupSymId( cmDspSysH_t h, unsigned groupIdx )
  651. {
  652. cmDsp_t* p = _cmDspHandleToPtr(h);
  653. return _cmDspPresetGroupSymId(&p->pm,groupIdx);
  654. }
  655. const cmChar_t* cmDspSysPresetGroupLabel( cmDspSysH_t h, unsigned groupIdx )
  656. {
  657. cmDsp_t* p = _cmDspHandleToPtr(h);
  658. return _cmDspPresetGroupLabel(&p->pm,groupIdx);
  659. }
  660. cmDspRC_t cmDspSysPresetGroupJsonList( cmDspSysH_t h, cmJsonH_t* jsHPtr )
  661. {
  662. cmDsp_t* p = _cmDspHandleToPtr(h);
  663. return _cmDspPresetGroupJsonList(&p->pm,jsHPtr);
  664. }
  665. unsigned cmDspSysPresetPresetCount( cmDspSysH_t h, unsigned groupIdx )
  666. {
  667. cmDsp_t* p = _cmDspHandleToPtr(h);
  668. return _cmDspPresetPresetCount(&p->pm,groupIdx);
  669. }
  670. unsigned cmDspSysPresetPresetSymId( cmDspSysH_t h, unsigned groupIdx, unsigned presetIdx )
  671. {
  672. cmDsp_t* p = _cmDspHandleToPtr(h);
  673. return _cmDspPresetPresetSymId(&p->pm,groupIdx,presetIdx);
  674. }
  675. const cmChar_t* cmDspSysPresetPresetLabel( cmDspSysH_t h, unsigned groupIdx, unsigned presetIdx )
  676. {
  677. cmDsp_t* p = _cmDspHandleToPtr(h);
  678. return _cmDspPresetPresetLabel(&p->pm,groupIdx,presetIdx);
  679. }
  680. cmDspRC_t cmDspSysPresetPresetJsonList( cmDspSysH_t h, unsigned groupSymId, cmJsonH_t* jsHPtr )
  681. {
  682. cmDsp_t* p = _cmDspHandleToPtr(h);
  683. return _cmDspPresetPresetJsonList(&p->pm,jsHPtr,groupSymId);
  684. }
  685. unsigned cmDspSysPresetRegisterGroup( cmDspSysH_t h, const cmChar_t* groupLabel )
  686. {
  687. cmDsp_t* p = _cmDspHandleToPtr(h);
  688. assert( groupLabel != NULL );
  689. unsigned grpSymId;
  690. if((grpSymId = cmSymTblRegisterSymbol(p->ctx.stH,groupLabel)) == cmInvalidIdx )
  691. cmErrMsg(&p->err,kSymTblFailDspRC,"Registration of the group label:'%s' failed.",cmStringNullGuard(groupLabel));
  692. return grpSymId;
  693. }
  694. cmDspRC_t cmDspSysPresetCreate( cmDspSysH_t h, const cmChar_t* groupLabel, const cmChar_t* presetLabel )
  695. {
  696. cmDspRC_t rc;
  697. cmDsp_t* p = _cmDspHandleToPtr(h);
  698. // create a new preset and make it active
  699. if((rc = _cmDspPresetCreatePreset(&p->pm, groupLabel, presetLabel )) != kOkDspRC )
  700. return rc;
  701. assert( p->pm.gp != NULL );
  702. // call store on each of the instances
  703. _cmDspInst_t* ip = p->instList;
  704. for(; ip != NULL; ip = ip->linkPtr )
  705. if( ip->instPtr->symId != cmInvalidId && ip->instPtr->presetGroupSymId == p->pm.gp->symId && ip->instPtr->storeFunc != NULL )
  706. {
  707. // create an instance preset and make it active
  708. if((rc = _cmDspPresetCreateInstance(&p->pm, ip->instPtr->symId )) != kOkDspRC )
  709. return rc;
  710. // call the store function on each instance
  711. if( ip->instPtr->storeFunc(&p->ctx,ip->instPtr,true) != kOkDspRC )
  712. rc = cmErrMsg(&p->err,kInstStoreFailDspRC,"Save failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  713. }
  714. return rc;
  715. }
  716. cmDspRC_t cmDspSysPresetRecall( cmDspSysH_t h, const cmChar_t* groupLabel, const cmChar_t* presetLabel )
  717. {
  718. cmDspRC_t rc;
  719. cmDsp_t* p = _cmDspHandleToPtr(h);
  720. // make the given group/preset active
  721. if((rc = _cmDspPresetRecallPreset(&p->pm, groupLabel, presetLabel )) != kOkDspRC )
  722. return rc;
  723. assert( p->pm.gp != NULL );
  724. // call store on each of the instances
  725. _cmDspInst_t* ip = p->instList;
  726. for(; ip != NULL; ip = ip->linkPtr )
  727. if( ip->instPtr->symId != cmInvalidId && ip->instPtr->storeFunc != NULL && ip->instPtr->presetGroupSymId == p->pm.gp->symId )
  728. {
  729. // make the instance active
  730. if((rc = _cmDspPresetRecallInstance(&p->pm, ip->instPtr->symId )) != kOkDspRC )
  731. {
  732. if( rc == kPresetInstNotFoundDspRC )
  733. {
  734. cmErrWarnMsg(&p->err,kOkDspRC,"Assuming a new instance was added to the preset - continuing with preset load.");
  735. continue;
  736. }
  737. }
  738. // call the store function on each instance
  739. if( ip->instPtr->storeFunc(&p->ctx,ip->instPtr,false) != kOkDspRC )
  740. rc = cmErrMsg(&p->err,kInstStoreFailDspRC,"Restore failed on DSP instance '%s' id:%i.",ip->instPtr->classPtr->labelStr,ip->instPtr->id);
  741. }
  742. return rc;
  743. }
  744. cmDspRC_t cmDspSysPresetWriteValue( cmDspSysH_t h, unsigned varSymId, const cmDspValue_t* valPtr )
  745. {
  746. cmDsp_t* p = _cmDspHandleToPtr(h);
  747. return _cmDspPresetCreateVar(&p->pm,varSymId,valPtr);
  748. }
  749. cmDspRC_t cmDspSysPresetReadValue( cmDspSysH_t h, unsigned varSymId, cmDspValue_t* valPtr )
  750. {
  751. cmDsp_t* p = _cmDspHandleToPtr(h);
  752. return _cmDspPresetRecallVar(&p->pm,varSymId,valPtr);
  753. }
  754. // allocate an instance given the class symbol id
  755. cmDspInst_t* _cmDspSysAllocateInst( cmDsp_t* p, unsigned classSymId, unsigned storeSymId, unsigned instSymId, unsigned va_cnt, va_list vl )
  756. {
  757. _cmDspClass_t* cp = p->classList;
  758. cmDspInst_t* instPtr = NULL;
  759. // locate the DSP class
  760. for(; cp != NULL; cp=cp->linkPtr )
  761. if( cp->symId == classSymId )
  762. break;
  763. // if the DSP class was not found
  764. if( cp == NULL )
  765. {
  766. cmErrMsg(&p->err,kClassNotFoundDspRC,"The DSP class '%s' could not be found.",cmSymTblLabel(p->ctx.stH,classSymId));
  767. return NULL;
  768. }
  769. // allocate the instance
  770. if((instPtr = cp->classPtr->allocFunc( &p->ctx, cp->classPtr, storeSymId, instSymId, p->nextInstId, va_cnt, vl )) == NULL )
  771. {
  772. cmErrMsg(&p->err,kAllocInstFailDspRC,"DSP instance allocation failed for class '%s'.",cp->classPtr->labelStr);
  773. return NULL;
  774. }
  775. // assign the symbol attributes
  776. cmDspInstSymId_t* ip = p->symIdList;
  777. for(; ip!=NULL; ip=ip->link)
  778. if( ip->symId != cmInvalidId )
  779. if( cmDspInstRegisterAttrSym(&p->ctx, instPtr, ip->symId ) != kOkDspRC )
  780. {
  781. cmErrMsg(&p->err,kAllocInstFailDspRC,"The DSP instance failed due to attribute symbol registration failure.");
  782. return NULL;
  783. }
  784. // allocate the instance ctl recd
  785. _cmDspInst_t* instCtlPtr = cmLhAllocZ( p->ctx.lhH, _cmDspInst_t, 1 );
  786. assert( instCtlPtr != NULL );
  787. // setup the inst ctl recd
  788. instCtlPtr->instPtr = instPtr;
  789. instCtlPtr->linkPtr = NULL;
  790. // link the inst ctl recd onto the end of the instance list
  791. if( p->instList == NULL )
  792. p->instList = instCtlPtr;
  793. else
  794. {
  795. _cmDspInst_t* ip = p->instList;
  796. while( ip->linkPtr != NULL )
  797. ip = ip->linkPtr;
  798. ip->linkPtr = instCtlPtr;
  799. }
  800. ++p->nextInstId;
  801. return instPtr;
  802. }
  803. // allocate a an instance from the class label - this function calls _cmDspSysAllocateInst()
  804. cmDspInst_t* cmDspSysAllocInstSV( cmDspSysH_t h, const cmChar_t* classLabelStr, unsigned storeSymId, const cmChar_t* instLabelStr, unsigned va_cnt, va_list vl)
  805. {
  806. cmDsp_t* p = _cmDspHandleToPtr(h);
  807. unsigned instSymId = cmInvalidId;
  808. cmDspInst_t* newInstPtr;
  809. unsigned classSymId;
  810. // get the class symbold
  811. if((classSymId = cmSymTblId(p->ctx.stH,classLabelStr)) == cmInvalidId )
  812. {
  813. cmErrMsg(&p->err,kSymNotFoundDspRC,"The symbol id associated with the class '%s' could not be found.",classLabelStr);
  814. return NULL;
  815. }
  816. if( instLabelStr != NULL )
  817. {
  818. // get the symbold id assoc'd with the instance label
  819. if((instSymId = cmSymTblRegisterSymbol(p->ctx.stH,instLabelStr)) == cmInvalidId )
  820. {
  821. cmErrMsg(&p->err,kSymTblFailDspRC,"The DSP instance symbol '%s' failed to register with the symbol table.",instLabelStr);
  822. return NULL;
  823. }
  824. // do not allow instance labels to be re-used
  825. if( _cmDspSysInstSymIdToPtr(p,instSymId) != NULL )
  826. {
  827. cmErrMsg(&p->err,kDuplInstSymIdDspRC,"The DSP instance label '%s' was reused (class:'%s').",cmStringNullGuard(instLabelStr),cmStringNullGuard(classLabelStr));
  828. return NULL;
  829. }
  830. }
  831. // allocate the instance
  832. newInstPtr = _cmDspSysAllocateInst(p, classSymId, storeSymId, instSymId, va_cnt, vl );
  833. return newInstPtr;
  834. }
  835. cmDspInst_t* cmDspSysAllocInstS( cmDspSysH_t h, const cmChar_t* classLabelStr, unsigned storeSymId, const cmChar_t* instLabelStr, unsigned va_cnt, ... )
  836. {
  837. cmDspInst_t* p;
  838. va_list vl;
  839. va_start(vl,va_cnt);
  840. p = cmDspSysAllocInstSV(h, classLabelStr, storeSymId, instLabelStr, va_cnt, vl);
  841. va_end(vl);
  842. return p;
  843. }
  844. cmDspInst_t* cmDspSysAllocInst( cmDspSysH_t h, const cmChar_t* classLabelStr, const cmChar_t* instLabelStr, unsigned va_cnt, ... )
  845. {
  846. cmDspInst_t* p;
  847. va_list vl;
  848. va_start(vl,va_cnt);
  849. p = cmDspSysAllocInstSV(h, classLabelStr, cmInvalidId, instLabelStr, va_cnt, vl);
  850. va_end(vl);
  851. return p;
  852. }
  853. 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 )
  854. {
  855. cmDsp_t* p = _cmDspHandleToPtr(h);
  856. cmDspInst_t** a = cmLhAllocZ( p->lhH, cmDspInst_t*, cnt );
  857. unsigned i;
  858. for(i=0; i<cnt; ++i)
  859. {
  860. const cmChar_t* label = instLabelStr;
  861. va_list vl1;
  862. va_copy(vl1,vl);
  863. if( labelFunc != NULL )
  864. label = labelFunc(h,i);
  865. else
  866. if( instLabelStr != NULL )
  867. label = cmDspSysPrintLabel(instLabelStr,i);
  868. a[i] = cmDspSysAllocInstSV(h,classLabelStr,presetGroupSymId,label,va_cnt,vl1);
  869. }
  870. return a;
  871. }
  872. cmDspInst_t** cmDspSysAllocInstArrayS( cmDspSysH_t h, unsigned cnt, unsigned presetGroupSymId, const cmChar_t* classLabelStr, const cmChar_t* instLabelStr, cmDspSysLabelFunc_t labelFunc, unsigned va_cnt, ... )
  873. {
  874. va_list vl;
  875. va_start(vl,va_cnt);
  876. cmDspInst_t** a = cmDspSysAllocInstArraySV(h,cnt,presetGroupSymId,classLabelStr,instLabelStr,labelFunc,va_cnt,vl);
  877. va_end(vl);
  878. return a;
  879. }
  880. cmDspInst_t** cmDspSysAllocInstArray( cmDspSysH_t h, unsigned cnt, const cmChar_t* classLabelStr, const cmChar_t* instLabelStr, cmDspSysLabelFunc_t labelFunc, unsigned va_cnt, ... )
  881. {
  882. va_list vl;
  883. va_start(vl,va_cnt);
  884. cmDspInst_t** a = cmDspSysAllocInstArraySV(h,cnt,cmInvalidId,classLabelStr,instLabelStr,labelFunc,va_cnt,vl);
  885. va_end(vl);
  886. return a;
  887. }
  888. cmDspRC_t cmDspSysNewColumn( cmDspSysH_t h, unsigned colW )
  889. { return cmDspUiNewColumn(&_cmDspHandleToPtr(h)->ctx,colW); }
  890. cmDspRC_t cmDspSysInsertHorzBorder( cmDspSysH_t h )
  891. { return cmDspUiInsertHorzBorder(&_cmDspHandleToPtr(h)->ctx); }
  892. cmDspRC_t cmDspSysNewPage( cmDspSysH_t h, const cmChar_t* title )
  893. { return cmDspUiNewPage(&_cmDspHandleToPtr(h)->ctx,title); }
  894. cmDspRC_t _cmDspSysConnAudio( cmDsp_t* p, cmDspInst_t* srcInstPtr, unsigned srcVarSymId, cmDspInst_t* dstInstPtr, unsigned dstVarSymId )
  895. {
  896. cmDspVar_t* srcVarPtr;
  897. cmDspVar_t* dstVarPtr;
  898. //cmSample_t* srcDataPtr;
  899. if( srcInstPtr == NULL )
  900. 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));
  901. if( dstInstPtr == NULL )
  902. 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));
  903. // locate the src audio variable
  904. if((srcVarPtr = cmDspVarSymbolToPtr( &p->ctx, srcInstPtr, srcVarSymId, kOutDsvFl )) == NULL )
  905. 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);
  906. // validate the src variable type
  907. if( cmIsFlag(srcVarPtr->flags,kAudioBufDsvFl) == false )
  908. 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);
  909. // locate the dst audio variable
  910. if((dstVarPtr = cmDspVarSymbolToPtr( &p->ctx, dstInstPtr, dstVarSymId, kInDsvFl )) == NULL )
  911. 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);
  912. // validate the dst variable type
  913. if( cmIsFlag(dstVarPtr->flags,kAudioBufDsvFl) == false )
  914. 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);
  915. // get a pointer to the source sample buffer
  916. //if( (srcDataPtr = cmDsvSampleMtx( &srcVarPtr->value )) == NULL )
  917. // 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);
  918. // set the destination sample buffer to point to the source sample buffer.
  919. //cmDsvSetSampleMtx( &dstVarPtr->value, srcDataPtr, cmDsvRows(&srcVarPtr->value), cmDsvCols(&srcVarPtr->value));
  920. cmDsvSetProxy( &dstVarPtr->value, &srcVarPtr->value );
  921. return kOkDspRC;
  922. }
  923. cmDspRC_t cmDspSysConnectAudio( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLbl, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLbl )
  924. {
  925. cmDsp_t* p = _cmDspHandleToPtr(h);
  926. unsigned srcSymId, dstSymId;
  927. if( srcInstPtr == NULL && dstInstPtr == NULL )
  928. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The audio connection process was passed a NULL source and destination processor instance.");
  929. if( srcInstPtr == NULL )
  930. 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));
  931. if( dstInstPtr == NULL )
  932. 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));
  933. if((srcSymId = cmSymTblId(p->ctx.stH,srcVarLbl)) == cmInvalidId )
  934. 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);
  935. if((dstSymId = cmSymTblId(p->ctx.stH,dstVarLbl)) == cmInvalidId )
  936. 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);
  937. return _cmDspSysConnAudio(p,srcInstPtr,srcSymId,dstInstPtr,dstSymId);
  938. }
  939. cmDspRC_t cmDspSysConnectAudioN11N( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  940. {
  941. cmDspRC_t rc = kOkDspRC;
  942. unsigned i;
  943. for(i=0; i<n; ++i)
  944. {
  945. const cmChar_t* dstVarStr = dstVarPrefixStr;
  946. if( n >= 2 )
  947. dstVarStr = cmDspSysPrintLabel(dstVarPrefixStr,i);
  948. if((rc = cmDspSysConnectAudio( h, srcInstArray[i], srcVarLabel, dstInstPtr, dstVarStr )) != kOkDspRC )
  949. break;
  950. }
  951. return rc;
  952. }
  953. cmDspRC_t cmDspSysConnectAudio1NN1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  954. {
  955. cmDspRC_t rc = kOkDspRC;
  956. unsigned i;
  957. for(i=0; i<n; ++i)
  958. {
  959. const cmChar_t* label = srcVarPrefixStr;
  960. if( n >= 2 )
  961. label = cmDspSysPrintLabel(srcVarPrefixStr,i);
  962. if((rc = cmDspSysConnectAudio( h, srcInstPtr, label, dstInstArray[i], dstVarLabel )) != kOkDspRC )
  963. break;
  964. }
  965. return rc;
  966. }
  967. cmDspRC_t cmDspSysConnectAudio1N1N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  968. {
  969. cmDspRC_t rc = kOkDspRC;
  970. unsigned i;
  971. for(i=0; i<n; ++i)
  972. {
  973. const cmChar_t* label0 = cmDspSysPrintLabel(srcVarPrefixStr,i);
  974. const cmChar_t* label1 = cmDspSysPrintLabel2(dstVarPrefixStr,i);
  975. if((rc = cmDspSysConnectAudio( h, srcInstPtr, label0, dstInstPtr, label1 )) != kOkDspRC )
  976. break;
  977. }
  978. return rc;
  979. }
  980. cmDspRC_t cmDspSysConnectAudioN1N1( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  981. {
  982. cmDspRC_t rc = kOkDspRC;
  983. unsigned i;
  984. for(i=0; i<n; ++i)
  985. {
  986. if((rc = cmDspSysConnectAudio( h, srcInstArray[i], srcVarLabel, dstInstArray[i], dstVarLabel )) != kOkDspRC )
  987. break;
  988. }
  989. return rc;
  990. }
  991. cmDspRC_t cmDspSysConnectAudio11N1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[],const cmChar_t* dstVarLabel, unsigned n )
  992. {
  993. cmDspRC_t rc = kOkDspRC;
  994. unsigned i;
  995. for(i=0; i<n; ++i)
  996. {
  997. if((rc = cmDspSysConnectAudio( h, srcInstPtr, srcVarLabel, dstInstArray[i], dstVarLabel )) != kOkDspRC )
  998. break;
  999. }
  1000. return rc;
  1001. }
  1002. cmDspRC_t cmDspSysConnectAudio111N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, unsigned n )
  1003. {
  1004. cmDspRC_t rc = kOkDspRC;
  1005. unsigned i;
  1006. for(i=0; i<n; ++i)
  1007. {
  1008. const cmChar_t* label1 = cmDspSysPrintLabel(dstVarLabel,i);
  1009. const cmChar_t* label = n>1 ? label1 : dstVarLabel;
  1010. if((rc = cmDspSysConnectAudio( h, srcInstPtr, srcVarLabel, dstInstPtr, label )) != kOkDspRC )
  1011. break;
  1012. }
  1013. return rc;
  1014. }
  1015. 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 )
  1016. {
  1017. cmDsp_t* p = _cmDspHandleToPtr(h);
  1018. cmDspRC_t rc = kOkDspRC;
  1019. unsigned i;
  1020. for(i=0; i<dstCnt; ++i)
  1021. {
  1022. const cmChar_t* dstVarStr = dstVarPrefixStr;
  1023. if( dstCnt >= 2 )
  1024. dstVarStr = cmDspSysPrintLabel(dstVarPrefixStr,i);
  1025. if( map[i] >= srcCnt )
  1026. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The source instance map connection index %i is of range %i.",i,srcCnt);
  1027. if((rc = cmDspSysConnectAudio( h, srcInstArray[ map[i] ], srcVarLabel, dstInstPtr, dstVarStr )) != kOkDspRC )
  1028. break;
  1029. }
  1030. return rc;
  1031. }
  1032. cmDspRC_t cmDspSysInstallCb( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, void* dstCbDataPtr)
  1033. {
  1034. cmDsp_t* p = _cmDspHandleToPtr(h);
  1035. return cmDspInstallCb(&p->ctx,srcInstPtr,srcVarLabel,dstInstPtr,dstVarLabel,dstCbDataPtr);
  1036. }
  1037. cmDspRC_t cmDspSysInstallCbN11N( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  1038. {
  1039. cmDspRC_t rc = kOkDspRC;
  1040. unsigned i;
  1041. for(i=0; i<n; ++i)
  1042. {
  1043. const cmChar_t* dstVarStr = dstVarPrefixStr;
  1044. if( n >= 2 )
  1045. dstVarStr = cmDspSysPrintLabel(dstVarPrefixStr,i);
  1046. if((rc = cmDspSysInstallCb( h, srcInstArray[i], srcVarLabel, dstInstPtr, dstVarStr, NULL )) != kOkDspRC )
  1047. break;
  1048. }
  1049. return rc;
  1050. }
  1051. cmDspRC_t cmDspSysInstallCb1NN1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  1052. {
  1053. cmDspRC_t rc = kOkDspRC;
  1054. unsigned i;
  1055. for(i=0; i<n; ++i)
  1056. {
  1057. const cmChar_t* srcVarStr = srcVarPrefixStr;
  1058. if( n >= 2 )
  1059. srcVarStr = cmDspSysPrintLabel(srcVarPrefixStr,i);
  1060. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  1061. break;
  1062. }
  1063. return rc;
  1064. }
  1065. cmDspRC_t cmDspSysInstallCb1N1N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixStr, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixStr, unsigned n )
  1066. {
  1067. cmDspRC_t rc = kOkDspRC;
  1068. unsigned i;
  1069. for(i=0; i<n; ++i)
  1070. {
  1071. const cmChar_t* label0 = srcVarPrefixStr;
  1072. const cmChar_t* label1 = dstVarPrefixStr;
  1073. if( n >= 2 )
  1074. {
  1075. label0 = cmDspSysPrintLabel(srcVarPrefixStr,i);
  1076. label1 = cmDspSysPrintLabel2(dstVarPrefixStr,i);
  1077. }
  1078. if((rc = cmDspSysInstallCb( h, srcInstPtr, label0, dstInstPtr, label1, NULL )) != kOkDspRC )
  1079. break;
  1080. }
  1081. return rc;
  1082. }
  1083. cmDspRC_t cmDspSysInstallCbN1N1( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  1084. {
  1085. cmDspRC_t rc = kOkDspRC;
  1086. unsigned i;
  1087. for(i=0; i<n; ++i)
  1088. {
  1089. if((rc = cmDspSysInstallCb( h, srcInstArray[i], srcVarLabel, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  1090. break;
  1091. }
  1092. return rc;
  1093. }
  1094. cmDspRC_t cmDspSysInstallCb11N1( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstArray[], const cmChar_t* dstVarLabel, unsigned n )
  1095. {
  1096. cmDspRC_t rc = kOkDspRC;
  1097. unsigned i;
  1098. for(i=0; i<n; ++i)
  1099. {
  1100. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarLabel, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  1101. break;
  1102. }
  1103. return rc;
  1104. }
  1105. cmDspRC_t cmDspSysInstallCb111N( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarPrefixLabel, unsigned n )
  1106. {
  1107. cmDspRC_t rc = kOkDspRC;
  1108. unsigned i;
  1109. for(i=0; i<n; ++i)
  1110. {
  1111. const cmChar_t* dstVarStr = dstVarPrefixLabel;
  1112. if( n >= 2 )
  1113. dstVarStr = cmDspSysPrintLabel(dstVarPrefixLabel,i);
  1114. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarLabel, dstInstPtr, dstVarStr, NULL )) != kOkDspRC )
  1115. break;
  1116. }
  1117. return rc;
  1118. }
  1119. cmDspRC_t cmDspSysInstallCbN111( cmDspSysH_t h, cmDspInst_t* srcInstArray[], const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, unsigned n )
  1120. {
  1121. cmDspRC_t rc = kOkDspRC;
  1122. unsigned i;
  1123. for(i=0; i<n; ++i)
  1124. {
  1125. if((rc = cmDspSysInstallCb( h, srcInstArray[i], srcVarLabel, dstInstPtr, dstVarLabel, NULL )) != kOkDspRC )
  1126. break;
  1127. }
  1128. return rc;
  1129. }
  1130. cmDspRC_t cmDspSysInstallCb1N11( cmDspSysH_t h, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarPrefixLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, unsigned n )
  1131. {
  1132. cmDspRC_t rc = kOkDspRC;
  1133. unsigned i;
  1134. for(i=0; i<n; ++i)
  1135. {
  1136. const cmChar_t* srcVarStr = srcVarPrefixLabel;
  1137. if( n >= 2 )
  1138. srcVarStr = cmDspSysPrintLabel(srcVarPrefixLabel,i);
  1139. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstPtr, dstVarLabel, NULL )) != kOkDspRC )
  1140. break;
  1141. }
  1142. return rc;
  1143. }
  1144. 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 )
  1145. {
  1146. cmDsp_t* p = _cmDspHandleToPtr(h);
  1147. cmDspRC_t rc = kOkDspRC;
  1148. unsigned i;
  1149. for(i=0; i<dstCnt; ++i)
  1150. {
  1151. const cmChar_t* label0 = srcVarPrefixStr;
  1152. const cmChar_t* label1 = dstVarPrefixStr;
  1153. if( map[i] >= srcCnt )
  1154. {
  1155. if( map[i] >= srcCnt )
  1156. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The source instance map connection index %i is of range %i.",i,srcCnt);
  1157. }
  1158. if( srcCnt >= 2 )
  1159. label0 = cmDspSysPrintLabel(srcVarPrefixStr,map[i]);
  1160. if( dstCnt >= 2 )
  1161. label1 = cmDspSysPrintLabel2(dstVarPrefixStr,i);
  1162. if((rc = cmDspSysInstallCb( h, srcInstPtr, label0, dstInstPtr, label1, NULL )) != kOkDspRC )
  1163. break;
  1164. }
  1165. return rc;
  1166. }
  1167. 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 )
  1168. {
  1169. cmDsp_t* p = _cmDspHandleToPtr(h);
  1170. cmDspRC_t rc = kOkDspRC;
  1171. unsigned i;
  1172. for(i=0; i<dstCnt; ++i)
  1173. {
  1174. if( map[i] >= srcCnt )
  1175. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The source instance map connection index %i is of range %i.",i,srcCnt);
  1176. const cmChar_t* srcVarStr = srcVarPrefixStr;
  1177. if( srcCnt >= 2 )
  1178. srcVarStr = cmDspSysPrintLabel(srcVarPrefixStr,map[i]);
  1179. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstArray[i], dstVarLabel, NULL )) != kOkDspRC )
  1180. break;
  1181. }
  1182. return rc;
  1183. }
  1184. 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 )
  1185. {
  1186. cmDsp_t* p = _cmDspHandleToPtr(h);
  1187. cmDspRC_t rc = kOkDspRC;
  1188. unsigned i;
  1189. for(i=0; i<srcCnt; ++i)
  1190. {
  1191. if( map[i] >= dstCnt )
  1192. return cmErrMsg(&p->err,kInstNotFoundDspRC,"The dest. instance map connection index %i is of range %i.",map[i],dstCnt);
  1193. const cmChar_t* srcVarStr = srcVarPrefixStr;
  1194. if( srcCnt >= 2 )
  1195. srcVarStr = cmDspSysPrintLabel(srcVarPrefixStr,i);
  1196. if((rc = cmDspSysInstallCb( h, srcInstPtr, srcVarStr, dstInstArray[ map[i] ], dstVarLabel, NULL )) != kOkDspRC )
  1197. break;
  1198. }
  1199. return rc;
  1200. }
  1201. double cmDspSysSampleRate( cmDspSysH_t h )
  1202. {
  1203. cmDsp_t* p = _cmDspHandleToPtr(h);
  1204. return p->ctx.ctx->ss->args.srate;
  1205. }
  1206. cmJsonH_t cmDspSysPgmRsrcHandle( cmDspSysH_t h )
  1207. {
  1208. cmDsp_t* p = _cmDspHandleToPtr(h);
  1209. return p->ctx.rsrcJsH;
  1210. }
  1211. cmSymTblH_t cmDspSysSymbolTable( cmDspSysH_t h )
  1212. {
  1213. cmDsp_t* p = _cmDspHandleToPtr(h);
  1214. return p->ctx.stH;
  1215. }
  1216. unsigned cmDspSysRegisterStaticSymbol( cmDspSysH_t h, const cmChar_t* symLabel )
  1217. { return cmSymTblRegisterStaticSymbol( cmDspSysSymbolTable(h), symLabel ); }
  1218. unsigned cmDspSysRegisterSymbol( cmDspSysH_t h, const cmChar_t* symLabel )
  1219. { return cmSymTblRegisterSymbol( cmDspSysSymbolTable(h), symLabel); }
  1220. cmCtx_t* cmDspSysPgmCtx( cmDspSysH_t h )
  1221. {
  1222. cmDsp_t* p = _cmDspHandleToPtr(h);
  1223. return &p->cmCtx;
  1224. }
  1225. cmLHeapH_t cmDspSysLHeap( cmDspSysH_t h )
  1226. {
  1227. cmDsp_t* p = _cmDspHandleToPtr(h);
  1228. return p->lhH;
  1229. }
  1230. unsigned cmDspSysNetNodeId( cmDspSysH_t h )
  1231. {
  1232. cmDsp_t* p = _cmDspHandleToPtr(h);
  1233. return cmUdpNetLocalNodeId(p->netH);
  1234. }
  1235. const cmChar_t* cmDspSysNetNodeLabel( cmDspSysH_t h )
  1236. {
  1237. cmDsp_t* p = _cmDspHandleToPtr(h);
  1238. return cmUdpNetLocalNodeLabel(p->netH);
  1239. }
  1240. const cmChar_t* cmDspSysNetNodeIdToLabel( cmDspSysH_t h, unsigned netNodeId )
  1241. {
  1242. cmDsp_t* p = _cmDspHandleToPtr(h);
  1243. return cmUdpNetNodeIdToLabel(p->netH,netNodeId);
  1244. }
  1245. unsigned cmDspSysNetNodeLabelToId( cmDspSysH_t h, const cmChar_t* netNodeLabel )
  1246. {
  1247. cmDsp_t* p = _cmDspHandleToPtr(h);
  1248. return cmUdpNetNodeLabelToId(p->netH,netNodeLabel);
  1249. }
  1250. cmDspInstSymId_t* _cmDspSysFindInstSymId( cmDsp_t* p, unsigned symId )
  1251. {
  1252. cmDspInstSymId_t* ip = p->symIdList;
  1253. for(; ip != NULL; ip=ip->link)
  1254. if( ip->symId == symId )
  1255. return ip;
  1256. return NULL;
  1257. }
  1258. unsigned cmDspSysRegisterInstAttrSymbol( cmDspSysH_t h, unsigned symId )
  1259. {
  1260. cmDsp_t* p = _cmDspHandleToPtr(h);
  1261. cmDspInstSymId_t* ip;
  1262. // if this symbol is already registered
  1263. if((ip = _cmDspSysFindInstSymId(p,symId)) != NULL )
  1264. return ip->symId;
  1265. // try to find an unused symbol
  1266. ip = p->symIdList;
  1267. for(; ip != NULL; ip=ip->link)
  1268. if( ip->symId == cmInvalidId )
  1269. break;
  1270. // if no unused symbols were found then allocate a new one
  1271. if( ip == NULL )
  1272. {
  1273. ip = cmLhAllocZ(p->ctx.lhH, cmDspInstSymId_t, 1 );
  1274. ip->link = p->symIdList;
  1275. p->symIdList = ip;
  1276. }
  1277. ip->symId = symId;
  1278. return ip->symId;
  1279. }
  1280. unsigned cmDspSysRegisterInstAttrSymbolStr( cmDspSysH_t h, const cmChar_t* symLabel )
  1281. {
  1282. cmDsp_t* p = _cmDspHandleToPtr(h);
  1283. unsigned symId;
  1284. if((symId = cmSymTblRegisterSymbol(p->ctx.stH,symLabel)) == cmInvalidId )
  1285. return cmErrMsg(&p->err,kSymTblFailDspRC,"The instance attribute symbol '%s' could not be registered.",cmStringNullGuard(symLabel));
  1286. return cmDspSysRegisterInstAttrSymbol(h,symId);
  1287. }
  1288. cmDspRC_t cmDspSysAssignInstAttrSymbol( cmDspSysH_t h, cmDspInst_t* inst, unsigned symId )
  1289. {
  1290. cmDsp_t* p = _cmDspHandleToPtr(h);
  1291. return cmDspInstRegisterAttrSym(&p->ctx, inst, symId) != kOkDspRC;
  1292. }
  1293. unsigned cmDspSysAssignInstAttrSymbolStr( cmDspSysH_t h, cmDspInst_t* inst, const cmChar_t* symLabel )
  1294. {
  1295. unsigned symId = cmDspSysRegisterSymbol(h,symLabel);
  1296. if( cmDspSysAssignInstAttrSymbol( h, inst, symId) != kOkDspRC )
  1297. return cmInvalidId;
  1298. return symId;
  1299. }
  1300. cmDspRC_t cmDspSysRemoveInstAttrSymbol( cmDspSysH_t h, unsigned symId )
  1301. {
  1302. cmDsp_t* p = _cmDspHandleToPtr(h);
  1303. cmDspInstSymId_t* ip;
  1304. // if this symbol is already registered
  1305. if((ip = _cmDspSysFindInstSymId(p,symId)) == NULL )
  1306. return cmErrMsg(&p->err,kOkDspRC,"The instance attribute symbol '%s' is could not be found to be removed.",cmStringNullGuard(cmSymTblLabel(p->ctx.stH,symId)));
  1307. // mark a symbol recd as inactive by setting the symbol id to cmInalidId
  1308. ip->symId = cmInvalidId;
  1309. return kOkDspRC;
  1310. }
  1311. cmDspRC_t cmDspSysRemoveInstAttrSymbolStr( cmDspSysH_t h, const cmChar_t* symLabel )
  1312. {
  1313. cmDsp_t* p = _cmDspHandleToPtr(h);
  1314. unsigned symId;
  1315. if( (symId = cmSymTblId(p->ctx.stH, symLabel )) == cmInvalidId )
  1316. return cmErrMsg(&p->err,kSymNotFoundDspRC,"The instance attribute symbol '%s' does not exist and therefore cannot be removed.",cmStringNullGuard(symLabel));
  1317. return cmDspSysRemoveInstAttrSymbol(h, symId );
  1318. }
  1319. cmDspRC_t cmDspSysBroadcastValue( cmDspSysH_t h, unsigned instAttrSymId, const cmDspValue_t* valuePtr )
  1320. {
  1321. cmDsp_t* p = _cmDspHandleToPtr(h);
  1322. _cmDspInst_t* ip = p->instList;
  1323. for(; ip != NULL; ip = ip->linkPtr )
  1324. if( (instAttrSymId==cmInvalidId) || cmDspInstHasAttrSym(ip->instPtr,instAttrSymId))
  1325. ip->instPtr->sysRecvFunc(&p->ctx,ip->instPtr,instAttrSymId,valuePtr);
  1326. return kOkDspRC;
  1327. }
  1328. cmDspInst_t* cmDspSysAllocLabel( cmDspSysH_t h, const cmChar_t* label, unsigned alignId )
  1329. {
  1330. return cmDspSysAllocInst(h,"Label", NULL, 2, label, alignId);
  1331. }
  1332. cmDspRC_t _cmDspGetRsrcReal( cmDspSysH_t h, const cmChar_t* label, const cmChar_t* rsrcPath, cmReal_t* valPtr )
  1333. {
  1334. const cmChar_t* errLabelPtr = NULL;
  1335. if( cmJsonPathValues( cmDspSysPgmRsrcHandle(h),NULL,NULL,&errLabelPtr, rsrcPath, kRealTId, valPtr, NULL ) != kOkJsRC )
  1336. {
  1337. cmDsp_t* p = _cmDspHandleToPtr(h);
  1338. return cmErrMsg(&p->err,kRsrcNotFoundDspRC,"DSP instance allocation failed for '%s' and the initial value resource path '%s'.", cmStringNullGuard(label),cmStringNullGuard(rsrcPath));
  1339. }
  1340. return kOkDspRC;
  1341. }
  1342. 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 )
  1343. {
  1344. cmDspInst_t* inst;
  1345. if((inst = cmDspSysAllocInstS(h,"Scalar", presetGrpSymId, instLabel, 6, kNumberDuiId, min, max, step, val, label )) == NULL)
  1346. {
  1347. cmDsp_t* p = _cmDspHandleToPtr(h);
  1348. cmErrMsg(&p->err,kAllocInstFailDspRC,"Scalar UI control allocation failed for '%s'.", cmStringNullGuard(instLabel));
  1349. }
  1350. return inst;
  1351. }
  1352. cmDspInst_t* cmDspSysAllocScalar( cmDspSysH_t h, const cmChar_t* label, cmReal_t min, cmReal_t max, cmReal_t step, cmReal_t val )
  1353. { return _cmDspSysAllocScalar(h,cmInvalidId,label,min,max,step,val,label); }
  1354. cmChar_t* _cmDspSysFormLabel( cmDspSysH_t h, unsigned presetGrpSymId, const cmChar_t* prefixLabel, const cmChar_t* label )
  1355. {
  1356. cmDsp_t* p = _cmDspHandleToPtr(h);
  1357. if( prefixLabel == NULL )
  1358. {
  1359. if(( prefixLabel = cmSymTblLabel(p->ctx.stH,presetGrpSymId)) == NULL )
  1360. cmErrMsg(&p->err,kInvalidArgDspRC,"The prefix label was not given for the DSP instance '%s'.",cmStringNullGuard(label));
  1361. }
  1362. if( label == NULL )
  1363. cmErrMsg(&p->err,kInvalidArgDspRC,"NULL was passed where a DSP instance label was expected.");
  1364. return cmTsPrintfH(cmDspSysLHeap(h),"%s-%s",cmStringNullGuard(prefixLabel),cmStringNullGuard(label));
  1365. }
  1366. 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 )
  1367. {
  1368. return _cmDspSysAllocScalar(h,presetGrpSymId,_cmDspSysFormLabel(h,presetGrpSymId,prefixLabel,label),min,max,step,val,label);
  1369. }
  1370. cmDspInst_t* cmDspSysAllocScalarRsrc( cmDspSysH_t h, const cmChar_t* label, cmReal_t min, cmReal_t max, cmReal_t step, const cmChar_t* rsrcPath )
  1371. {
  1372. cmReal_t val;
  1373. if(_cmDspGetRsrcReal(h,label,rsrcPath,&val ) != kOkDspRC )
  1374. return NULL;
  1375. return cmDspSysAllocScalar(h,label,min,max,step,val);
  1376. }
  1377. 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 )
  1378. {
  1379. cmChar_t* lbl = cmTsPrintfH( cmDspSysLHeap(h), "%s-%s",prefixLabel,label);
  1380. return cmDspSysAllocInstArrayS( h, cnt, presetGroupSymId, "Scalar", lbl, NULL, 5, kNumberDuiId, min, max, step, val);
  1381. }
  1382. cmDspInst_t* _cmDspSysAllocButton( cmDspSysH_t h, unsigned btnTypeId, unsigned presetGroupSymId, const cmChar_t* instLabel, const cmChar_t* label, cmReal_t val, unsigned valSymId )
  1383. {
  1384. cmDspInst_t* inst;
  1385. if((inst = cmDspSysAllocInstS( h, "Button", presetGroupSymId, instLabel, 4, btnTypeId, val, valSymId, label)) == NULL )
  1386. {
  1387. cmDsp_t* p = _cmDspHandleToPtr(h);
  1388. cmErrMsg(&p->err,kAllocInstFailDspRC,"Button UI control allocation failed for '%s'.", cmStringNullGuard(label));
  1389. }
  1390. return inst;
  1391. }
  1392. cmDspInst_t* cmDspSysAllocButtonP( cmDspSysH_t h, const cmChar_t* prefixLabel, const cmChar_t* label, cmReal_t val )
  1393. {
  1394. return cmDspSysAllocButton(h,_cmDspSysFormLabel(h,cmInvalidId,prefixLabel,label),val);
  1395. }
  1396. cmDspInst_t* _cmDspSysAllocButtonRsrc( cmDspSysH_t h, unsigned typeId, unsigned presetGroupSymId, const cmChar_t* label, const cmChar_t* rsrcPath )
  1397. {
  1398. cmReal_t val;
  1399. if(_cmDspGetRsrcReal(h,label,rsrcPath,&val) != kOkDspRC )
  1400. return NULL;
  1401. return _cmDspSysAllocButton(h,typeId,presetGroupSymId,label,label,val,cmInvalidId);
  1402. }
  1403. cmDspInst_t* cmDspSysAllocButton( cmDspSysH_t h, const cmChar_t* label, cmReal_t val )
  1404. { return _cmDspSysAllocButton(h,kButtonDuiId,cmInvalidId,label,label,val,cmInvalidId); }
  1405. cmDspInst_t* cmDspSysAllocButtonRsrc( cmDspSysH_t h, const cmChar_t* label, const cmChar_t* rsrcPath )
  1406. { return _cmDspSysAllocButtonRsrc( h,kButtonDuiId,cmInvalidId,label,rsrcPath); }
  1407. cmDspInst_t* _cmDspSysAllocCheck( cmDspSysH_t h, unsigned presetGroupSymId, const cmChar_t* instLabel, const cmChar_t* label, cmReal_t val )
  1408. { return _cmDspSysAllocButton(h,kCheckDuiId,presetGroupSymId,instLabel,label,val,cmInvalidId); }
  1409. cmDspInst_t* cmDspSysAllocCheck( cmDspSysH_t h, const cmChar_t* label, cmReal_t val )
  1410. { return _cmDspSysAllocCheck(h,cmInvalidId,label,label,val); }
  1411. cmDspInst_t* cmDspSysAllocCheckP( cmDspSysH_t h, unsigned presetGroupSymId, const cmChar_t* prefixLabel, const cmChar_t* label, cmReal_t val )
  1412. { return _cmDspSysAllocCheck(h,presetGroupSymId,_cmDspSysFormLabel(h,presetGroupSymId,prefixLabel,label),label,val); }
  1413. cmDspInst_t* cmDspSysAllocCheckRsrc( cmDspSysH_t h, const cmChar_t* label, const cmChar_t* rsrcPath )
  1414. { return _cmDspSysAllocButtonRsrc( h,kCheckDuiId, cmInvalidId, label,rsrcPath); }
  1415. cmDspInst_t* cmDspSysAllocMsgList( cmDspSysH_t h, const cmChar_t* fn, const cmChar_t* rsrcLabel, unsigned initSelIdx )
  1416. { return cmDspSysAllocInst( h, "MsgList", NULL, 3, rsrcLabel, fn, initSelIdx); }
  1417. 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 )
  1418. {
  1419. cmDspInst_t* inst;
  1420. cmChar_t* lbl;
  1421. if((inst = cmDspSysAllocInstS( h, "MsgList", presetGroupSymId, lbl = _cmDspSysFormLabel(h,presetGroupSymId,preLabel,label), 3, rsrcLabel, fn, initSelIdx)) == NULL )
  1422. {
  1423. cmDsp_t* p = _cmDspHandleToPtr(h);
  1424. cmErrMsg(&p->err,kAllocInstFailDspRC,"Msg List UI control allocation failed for '%s'.", cmStringNullGuard(lbl));
  1425. }
  1426. return inst;
  1427. }
  1428. cmDspInst_t* cmDspSysAllocAudioIn( cmDspSysH_t h, unsigned chIdx, cmReal_t gain )
  1429. { return cmDspSysAllocInst( h, "AudioIn", NULL, 2, chIdx, gain ); }
  1430. const cmJsonNode_t* _cmDspSysFindAudioChMap( cmDspSysH_t h, const cmChar_t* rsrcLabel )
  1431. {
  1432. cmDsp_t* p = _cmDspHandleToPtr(h);
  1433. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(h);
  1434. assert( cmJsonIsValid(jsH) );
  1435. const cmJsonNode_t* np = NULL;
  1436. if(( np = cmJsonFindValue( jsH, rsrcLabel, cmJsonRoot(jsH), kArrayTId )) == NULL )
  1437. {
  1438. cmErrMsg(&p->err,kRsrcNotFoundDspRC,"The audio channel map resource '%s' was not found.", cmStringNullGuard(rsrcLabel));
  1439. return NULL;
  1440. }
  1441. unsigned chCnt = cmJsonChildCount(np);
  1442. if( chCnt == 0 )
  1443. {
  1444. cmErrMsg(&p->err,kInvalidArgDspRC,"The audio channel map resource '%s' is empty.",cmStringNullGuard(rsrcLabel));
  1445. return NULL;
  1446. }
  1447. return np;
  1448. }
  1449. cmDspRC_t _cmDspSysReadAudioChMap( cmDspSysH_t h, const cmChar_t* rsrcLabel, const cmJsonNode_t* mapNodePtr, unsigned* map, unsigned mapCnt )
  1450. {
  1451. cmDsp_t* p = _cmDspHandleToPtr(h);
  1452. unsigned i;
  1453. for(i=0; i<mapCnt; ++i)
  1454. {
  1455. const cmJsonNode_t* np;
  1456. if((np = cmJsonArrayElementC(mapNodePtr,i)) == NULL )
  1457. return cmErrMsg(&p->err,kInvalidArgDspRC,"The audio map element at index %i could not be accessed in the audio map:'%s'.",i,cmStringNullGuard(rsrcLabel));
  1458. if( cmJsonUIntValue(np,map + i ) != kOkJsRC )
  1459. 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));
  1460. }
  1461. return kOkDspRC;
  1462. }
  1463. cmDspInst_t** _cmDspSysAllocAudioRsrc( cmDspSysH_t h, const cmChar_t* classLabel, const cmChar_t* rsrcLabel, cmReal_t gain, unsigned* retChCntPtr )
  1464. {
  1465. cmDsp_t* p = _cmDspHandleToPtr(h);
  1466. const cmJsonNode_t* np;
  1467. if( retChCntPtr != NULL )
  1468. *retChCntPtr = 0;
  1469. //
  1470. if( rsrcLabel==NULL )
  1471. rsrcLabel = "audioInMap";
  1472. // find the map resource
  1473. if((np = _cmDspSysFindAudioChMap(h,rsrcLabel)) == NULL )
  1474. return NULL;
  1475. unsigned chCnt = cmJsonChildCount(np);
  1476. cmDspInst_t** a = cmLhAllocZ( p->lhH, cmDspInst_t*, chCnt );
  1477. unsigned chMap[ chCnt ];
  1478. // fill in the channel map - maps virtual channels to physical channels
  1479. if( _cmDspSysReadAudioChMap(h,rsrcLabel,np,chMap,chCnt) != kOkDspRC )
  1480. return NULL;
  1481. unsigned i;
  1482. for(i=0; i<chCnt; ++i)
  1483. if((a[i] = cmDspSysAllocInst( h, classLabel, NULL, 2, chMap[i], gain )) == NULL )
  1484. break;
  1485. if( i==chCnt )
  1486. {
  1487. if( retChCntPtr != NULL )
  1488. *retChCntPtr = chCnt;
  1489. return a;
  1490. }
  1491. return NULL;
  1492. }
  1493. cmDspInst_t** cmDspSysAllocAudioInAR( cmDspSysH_t h, const cmChar_t* rsrcLabel, cmReal_t gain, unsigned* retChCntPtr )
  1494. { return _cmDspSysAllocAudioRsrc(h,"AudioIn",rsrcLabel,gain,retChCntPtr); }
  1495. cmDspInst_t* cmDspSysAllocAudioOut(cmDspSysH_t h, unsigned chIdx, cmReal_t gain )
  1496. { return cmDspSysAllocInst( h, "AudioOut", NULL, 2, chIdx, gain ); }
  1497. cmDspInst_t** cmDspSysAllocAudioOutAR( cmDspSysH_t h, const cmChar_t* rsrcLabel, cmReal_t gain, unsigned* retChCntPtr )
  1498. { return _cmDspSysAllocAudioRsrc(h,"AudioOut",rsrcLabel,gain,retChCntPtr); }
  1499. 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 )
  1500. {
  1501. cmDsp_t* p = _cmDspHandleToPtr(h);
  1502. cmDspRC_t rc = kOkDspRC;
  1503. cmDspInst_t* netSendInstPtr;
  1504. unsigned dstNetNodeId;
  1505. _cmDspSrcConn_t* srcConnPtr = NULL;
  1506. // get the dest. network node id
  1507. if( (dstNetNodeId = cmUdpNetNodeLabelToId(p->netH, dstNetNodeLabel )) == cmInvalidId )
  1508. return cmErrMsg(&p->err,kNetNodeNotFoundDspRC,"The destination network node label '%s' was not found.",cmStringNullGuard(dstNetNodeLabel));
  1509. if((srcConnPtr = _cmDspSysNetCreateSrcConn( p, dstNetNodeId, dstInstLabel, dstVarLabel)) == NULL )
  1510. return rc;
  1511. // allocate a network sender
  1512. if((netSendInstPtr = cmDspSysAllocInst(h, "NetSend", NULL, 1, srcConnPtr )) == NULL )
  1513. return cmErrMsg(&p->err,kNetSendAllocFailDspRC,"The network sender DSP instance allocation failed.");
  1514. return cmDspSysInstallCb(h, srcInstPtr, srcVarLabel, netSendInstPtr, "in", NULL );
  1515. }
  1516. 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 )
  1517. {
  1518. cmDspRC_t rc = kOkDspRC;
  1519. unsigned i;
  1520. for(i=0; i<n; ++i)
  1521. {
  1522. const cmChar_t* label0 = srcVarPrefixStr;
  1523. const cmChar_t* label1 = dstVarPrefixStr;
  1524. if( n >= 2 )
  1525. {
  1526. label0 = cmDspSysPrintLabel( srcVarPrefixStr,i);
  1527. label1 = cmDspSysPrintLabel2(dstVarPrefixStr,dstOffs + i);
  1528. }
  1529. if((rc = cmDspSysInstallNetCb(h,srcInstPtr,label0,dstNetNodeLabel,dstInstLabel,label1)) != kOkDspRC )
  1530. break;
  1531. }
  1532. return rc;
  1533. }
  1534. 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 )
  1535. {
  1536. cmDspRC_t rc = kOkDspRC;
  1537. unsigned i;
  1538. for(i=0; i<dstCnt; ++i)
  1539. {
  1540. assert( map[i] < srcCnt );
  1541. const cmChar_t* label0 = cmDspSysPrintLabel( srcVarPrefixStr, map[i] );
  1542. const cmChar_t* label1 = cmDspSysPrintLabel2(dstVarPrefixStr, i );
  1543. if((rc = cmDspSysInstallNetCb(h,srcInstPtr,label0,dstNetNodeLabel,dstInstLabel,label1)) != kOkDspRC )
  1544. break;
  1545. }
  1546. return rc;
  1547. }
  1548. cmDspRC_t _cmDspRsrcPath( cmDspSysH_t h, cmChar_t** pathPtr, va_list vl )
  1549. {
  1550. unsigned i;
  1551. cmDspRC_t rc = kOkDspRC;;
  1552. cmDsp_t* p = _cmDspHandleToPtr(h);
  1553. cmChar_t* path = NULL;
  1554. unsigned maxArgCnt = 10;
  1555. *pathPtr = NULL;
  1556. for(i=0; i<maxArgCnt; ++i )
  1557. {
  1558. cmChar_t* str;
  1559. if((str = va_arg(vl,cmChar_t* )) == NULL )
  1560. break;
  1561. if( path != NULL )
  1562. path = cmTextAppendSS(path,"/");
  1563. path = cmTextAppendSS(path,str);
  1564. }
  1565. if( i >= maxArgCnt )
  1566. {
  1567. rc = cmErrMsg(&p->err,kJsonFailDspRC,"A resource path, beginning with '%25s', does not contain a terminating NULL.", cmStringNullGuard(path) );
  1568. goto errLabel;
  1569. }
  1570. // duplicate the string onto the program linked heap
  1571. *pathPtr = cmLhAllocStr( cmDspSysLHeap(h), path);
  1572. errLabel:
  1573. cmMemFree(path);
  1574. return rc;
  1575. }
  1576. cmDspRC_t _cmDspRsrcWritePath( cmDspSysH_t h, cmChar_t** pathStrRef, cmChar_t** varStrRef, va_list vl )
  1577. {
  1578. unsigned i;
  1579. cmDspRC_t rc = kOkDspRC;;
  1580. cmDsp_t* p = _cmDspHandleToPtr(h);
  1581. cmChar_t* path = NULL;
  1582. unsigned maxArgCnt = 10;
  1583. cmChar_t* prevStr = NULL;
  1584. *pathStrRef = NULL;
  1585. *varStrRef = NULL;
  1586. for(i=0; i<maxArgCnt; ++i )
  1587. {
  1588. cmChar_t* str;
  1589. if((str = va_arg(vl,cmChar_t* )) == NULL )
  1590. break;
  1591. if( prevStr != NULL )
  1592. {
  1593. if( path != NULL )
  1594. path = cmTextAppendSS(path,"/");
  1595. path = cmTextAppendSS(path,prevStr);
  1596. }
  1597. prevStr = str;
  1598. }
  1599. if( i >= maxArgCnt )
  1600. {
  1601. rc = cmErrMsg(&p->err,kJsonFailDspRC,"A resource path, beginning with '%25s', does not contain a terminating NULL.", cmStringNullGuard(path) );
  1602. goto errLabel;
  1603. }
  1604. // duplicate the string onto the program linked heap
  1605. if( path != NULL )
  1606. *pathStrRef = cmLhAllocStr( cmDspSysLHeap(h), path);
  1607. *varStrRef = prevStr;
  1608. errLabel:
  1609. cmMemFree(path);
  1610. return rc;
  1611. }
  1612. cmDspRC_t cmDspRsrcBoolV( cmDspSysH_t h, bool* vp, va_list vl )
  1613. {
  1614. assert(vp != NULL);
  1615. cmDsp_t* p = _cmDspHandleToPtr(h);
  1616. cmChar_t* path = NULL;
  1617. cmDspRC_t rc;
  1618. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1619. return rc;
  1620. if((rc = cmJsonPathToBool( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1621. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'bool' resource not found at path:'%s'.",cmStringNullGuard(path));
  1622. cmLhFree(cmDspSysLHeap(h),path);
  1623. return rc;
  1624. }
  1625. cmDspRC_t cmDspRsrcIntV( cmDspSysH_t h, int* vp, va_list vl )
  1626. {
  1627. assert(vp != NULL);
  1628. cmDsp_t* p = _cmDspHandleToPtr(h);
  1629. cmChar_t* path = NULL;
  1630. cmDspRC_t rc;
  1631. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1632. return rc;
  1633. if((rc = cmJsonPathToInt( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1634. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'int' resource not found at path:'%s'.",cmStringNullGuard(path));
  1635. cmLhFree(cmDspSysLHeap(h),path);
  1636. return rc;
  1637. }
  1638. cmDspRC_t cmDspRsrcUIntV( cmDspSysH_t h, unsigned* vp, va_list vl )
  1639. {
  1640. assert(vp != NULL);
  1641. cmDsp_t* p = _cmDspHandleToPtr(h);
  1642. cmChar_t* path = NULL;
  1643. cmDspRC_t rc;
  1644. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1645. return rc;
  1646. if((rc = cmJsonPathToUInt( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1647. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'uint' resource not found at path:'%s'.",cmStringNullGuard(path));
  1648. cmLhFree(cmDspSysLHeap(h),path);
  1649. return rc;
  1650. }
  1651. cmDspRC_t cmDspRsrcDblV( cmDspSysH_t h, double* vp, va_list vl )
  1652. {
  1653. assert(vp != NULL);
  1654. cmDsp_t* p = _cmDspHandleToPtr(h);
  1655. cmChar_t* path = NULL;
  1656. cmDspRC_t rc;
  1657. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1658. return rc;
  1659. if((rc = cmJsonPathToReal( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1660. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'real' resource not found at path:'%s'.",cmStringNullGuard(path));
  1661. cmLhFree(cmDspSysLHeap(h),path);
  1662. return rc;
  1663. }
  1664. cmDspRC_t cmDspRsrcRealV( cmDspSysH_t h, cmReal_t* vp, va_list vl )
  1665. {
  1666. assert(vp != NULL);
  1667. cmDsp_t* p = _cmDspHandleToPtr(h);
  1668. cmChar_t* path = NULL;
  1669. cmDspRC_t rc;
  1670. double dval;
  1671. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1672. return rc;
  1673. if((rc = cmJsonPathToReal( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, &dval )) != kOkJsRC )
  1674. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'real' resource not found at path:'%s'.",cmStringNullGuard(path));
  1675. else
  1676. *vp = dval;
  1677. cmLhFree(cmDspSysLHeap(h),path);
  1678. return rc;
  1679. }
  1680. cmDspRC_t cmDspRsrcStringV( cmDspSysH_t h, const cmChar_t** vp, va_list vl )
  1681. {
  1682. assert(vp != NULL);
  1683. cmDsp_t* p = _cmDspHandleToPtr(h);
  1684. cmChar_t* path = NULL;
  1685. cmDspRC_t rc;
  1686. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1687. return rc;
  1688. if((rc = cmJsonPathToString( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, vp )) != kOkJsRC )
  1689. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'string' resource not found at path:'%s'.",cmStringNullGuard(path));
  1690. cmLhFree(cmDspSysLHeap(h),path);
  1691. return rc;
  1692. }
  1693. cmDspRC_t cmDspRsrcArrayCountV( cmDspSysH_t h, unsigned *n, va_list vl )
  1694. {
  1695. assert(n != NULL );
  1696. cmDsp_t* p = _cmDspHandleToPtr(h);
  1697. cmChar_t* path = NULL;
  1698. cmJsonNode_t* np = NULL;
  1699. cmDspRC_t rc;
  1700. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1701. return rc;
  1702. if((rc = cmJsonPathToArray( cmDspSysPgmRsrcHandle(h), NULL, NULL, path, &np )) != kOkJsRC )
  1703. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'array' resource not found at path:'%s'.",cmStringNullGuard(path));
  1704. else
  1705. *n = cmJsonChildCount(np);
  1706. cmLhFree(cmDspSysLHeap(h),path);
  1707. return rc;
  1708. }
  1709. cmDspRC_t _cmDspRsrcArrayV( cmDspSysH_t h, unsigned* np, cmJsonNode_t** npp, cmChar_t** pathPtrPtr, va_list vl )
  1710. {
  1711. cmDsp_t* p = _cmDspHandleToPtr(h);
  1712. cmChar_t* path = NULL;
  1713. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(h);
  1714. cmDspRC_t rc;
  1715. if((rc = _cmDspRsrcPath(h,&path,vl)) != kOkDspRC )
  1716. return rc;
  1717. if((rc = cmJsonPathToArray( jsH, NULL, NULL, path, npp )) != kOkJsRC )
  1718. rc = cmErrMsg(&p->err,kJsonFailDspRC,"'array' resource not found at path:'%s'.",cmStringNullGuard(path));
  1719. else
  1720. {
  1721. if((*np = cmJsonChildCount(*npp)) != 0 )
  1722. *pathPtrPtr = path;
  1723. }
  1724. cmLhFree(cmDspSysLHeap(h),path);
  1725. return rc;
  1726. }
  1727. cmDspRC_t cmDspRsrcBoolArrayV( cmDspSysH_t h, unsigned* np, bool** vpp, va_list vl )
  1728. {
  1729. assert(vpp!=NULL);
  1730. cmDsp_t* p = _cmDspHandleToPtr(h);
  1731. cmJsonNode_t* nodePtr = NULL;
  1732. unsigned n = 0;
  1733. cmChar_t* path = NULL;
  1734. unsigned i;
  1735. cmDspRC_t rc;
  1736. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1737. return rc;
  1738. bool* vp = *vpp;
  1739. vp = cmLhResizeN(p->ctx.lhH,bool,vp,n);
  1740. for(i=0; i<n && rc == kOkDspRC; ++i)
  1741. if( cmJsonBoolValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1742. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1743. *vpp = vp;
  1744. *np = n;
  1745. cmLhFree(cmDspSysLHeap(h),path);
  1746. return rc;
  1747. }
  1748. cmDspRC_t cmDspRsrcIntArrayV( cmDspSysH_t h, unsigned* np, int** vpp, va_list vl )
  1749. {
  1750. assert(vpp!=NULL);
  1751. cmDsp_t* p = _cmDspHandleToPtr(h);
  1752. cmJsonNode_t* nodePtr = NULL;
  1753. unsigned n = 0;
  1754. cmChar_t* path = NULL;
  1755. unsigned i;
  1756. cmDspRC_t rc;
  1757. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1758. return rc;
  1759. int* vp = *vpp;
  1760. vp = cmLhResizeN(p->ctx.lhH,int,vp,n);
  1761. for(i=0; i<n && rc == kOkDspRC; ++i)
  1762. if( cmJsonIntValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1763. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1764. *vpp = vp;
  1765. *np = n;
  1766. cmLhFree(cmDspSysLHeap(h),path);
  1767. return rc;
  1768. }
  1769. cmDspRC_t cmDspRsrcUIntArrayV( cmDspSysH_t h, unsigned* np, unsigned** vpp, va_list vl )
  1770. {
  1771. assert(vpp!=NULL);
  1772. cmDsp_t* p = _cmDspHandleToPtr(h);
  1773. cmJsonNode_t* nodePtr = NULL;
  1774. unsigned n = 0;
  1775. cmChar_t* path = NULL;
  1776. unsigned i;
  1777. cmDspRC_t rc;
  1778. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1779. return rc;
  1780. unsigned* vp = *vpp;
  1781. vp = cmLhResizeN(p->ctx.lhH,unsigned,vp,n);
  1782. for(i=0; i<n && rc == kOkDspRC; ++i)
  1783. if( cmJsonUIntValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1784. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1785. *vpp = vp;
  1786. *np = n;
  1787. cmLhFree(cmDspSysLHeap(h),path);
  1788. return rc;
  1789. }
  1790. cmDspRC_t cmDspRsrcDblArrayV( cmDspSysH_t h, unsigned* np, double** vpp, va_list vl )
  1791. {
  1792. assert(vpp!=NULL);
  1793. cmDsp_t* p = _cmDspHandleToPtr(h);
  1794. cmJsonNode_t* nodePtr = NULL;
  1795. unsigned n = 0;
  1796. cmChar_t* path = NULL;
  1797. unsigned i;
  1798. cmDspRC_t rc;
  1799. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1800. return rc;
  1801. double* vp = *vpp;
  1802. vp = cmLhResizeN(p->ctx.lhH,double,vp,n);
  1803. for(i=0; i<n && rc == kOkDspRC; ++i)
  1804. if( cmJsonRealValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1805. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1806. *vpp = vp;
  1807. *np = n;
  1808. cmLhFree(cmDspSysLHeap(h),path);
  1809. return rc;
  1810. }
  1811. cmDspRC_t cmDspRsrcRealArrayV( cmDspSysH_t h, unsigned* np, cmReal_t** vpp, va_list vl )
  1812. {
  1813. assert(vpp!=NULL);
  1814. cmDsp_t* p = _cmDspHandleToPtr(h);
  1815. cmJsonNode_t* nodePtr = NULL;
  1816. unsigned n = 0;
  1817. cmChar_t* path = NULL;
  1818. unsigned i;
  1819. cmDspRC_t rc;
  1820. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1821. return rc;
  1822. cmReal_t* vp = *vpp;
  1823. vp = cmLhResizeN(p->ctx.lhH,cmReal_t,vp,n);
  1824. for(i=0; i<n && rc == kOkDspRC; ++i)
  1825. {
  1826. double v;
  1827. if( cmJsonRealValue( cmJsonArrayElementC(nodePtr,i), &v ) != kOkJsRC )
  1828. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1829. vp[i] = v;
  1830. }
  1831. *vpp = vp;
  1832. *np = n;
  1833. cmLhFree(cmDspSysLHeap(h),path);
  1834. return rc;
  1835. }
  1836. cmDspRC_t cmDspRsrcStringArrayV(cmDspSysH_t h, unsigned* np, const cmChar_t*** vpp, va_list vl )
  1837. {
  1838. assert(vpp!=NULL);
  1839. cmDsp_t* p = _cmDspHandleToPtr(h);
  1840. cmJsonNode_t* nodePtr = NULL;
  1841. unsigned n = 0;
  1842. cmChar_t* path = NULL;
  1843. unsigned i;
  1844. cmDspRC_t rc;
  1845. if(( rc = _cmDspRsrcArrayV(h,&n,&nodePtr,&path,vl)) != kOkDspRC )
  1846. return rc;
  1847. const cmChar_t** vp = *vpp;
  1848. vp = cmLhResizeN(p->ctx.lhH,const cmChar_t*,vp,n);
  1849. for(i=0; i<n && rc == kOkDspRC; ++i)
  1850. if( cmJsonStringValue( cmJsonArrayElementC(nodePtr,i), vp + i ) != kOkJsRC )
  1851. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Element at index '%i' in resource '%s' is invalid.",i,cmStringNullGuard(path));
  1852. *vpp = vp;
  1853. *np = n;
  1854. cmLhFree(cmDspSysLHeap(h),path);
  1855. return rc;
  1856. }
  1857. cmDspRC_t cmDspRsrcInt( cmDspSysH_t h, int* vp, ... )
  1858. {
  1859. va_list vl;
  1860. va_start(vl,vp);
  1861. cmDspRC_t rc = cmDspRsrcIntV(h,vp,vl);
  1862. va_end(vl);
  1863. return rc;
  1864. }
  1865. cmDspRC_t cmDspRsrcUInt( cmDspSysH_t h, unsigned* vp, ... )
  1866. {
  1867. va_list vl;
  1868. va_start(vl,vp);
  1869. cmDspRC_t rc = cmDspRsrcUIntV(h,vp,vl);
  1870. va_end(vl);
  1871. return rc;
  1872. }
  1873. cmDspRC_t cmDspRsrcDbl( cmDspSysH_t h, double* vp, ... )
  1874. {
  1875. va_list vl;
  1876. va_start(vl,vp);
  1877. cmDspRC_t rc = cmDspRsrcDblV(h,vp,vl);
  1878. va_end(vl);
  1879. return rc;
  1880. }
  1881. cmDspRC_t cmDspRsrcReal( cmDspSysH_t h, cmReal_t* vp, ... )
  1882. {
  1883. va_list vl;
  1884. va_start(vl,vp);
  1885. cmDspRC_t rc = cmDspRsrcRealV(h,vp,vl);
  1886. va_end(vl);
  1887. return rc;
  1888. }
  1889. cmDspRC_t cmDspRsrcString( cmDspSysH_t h, const cmChar_t** vp, ... )
  1890. {
  1891. va_list vl;
  1892. va_start(vl,vp);
  1893. cmDspRC_t rc = cmDspRsrcStringV(h,vp,vl);
  1894. va_end(vl);
  1895. return rc;
  1896. }
  1897. cmDspRC_t cmDspRsrcArrayCount( cmDspSysH_t h, unsigned *np, ... )
  1898. {
  1899. va_list vl;
  1900. va_start(vl,np);
  1901. cmDspRC_t rc = cmDspRsrcArrayCountV(h,np,vl);
  1902. va_end(vl);
  1903. return rc;
  1904. }
  1905. cmDspRC_t cmDspRsrcBoolArray( cmDspSysH_t h, unsigned* np, bool** vpp, ... )
  1906. {
  1907. va_list vl;
  1908. va_start(vl,vpp);
  1909. cmDspRC_t rc = cmDspRsrcBoolArrayV(h,np,vpp,vl);
  1910. va_end(vl);
  1911. return rc;
  1912. }
  1913. cmDspRC_t cmDspRsrcIntArray( cmDspSysH_t h, unsigned* np, int** vpp, ... )
  1914. {
  1915. va_list vl;
  1916. va_start(vl,vpp);
  1917. cmDspRC_t rc = cmDspRsrcIntArrayV(h,np,vpp,vl);
  1918. va_end(vl);
  1919. return rc;
  1920. }
  1921. cmDspRC_t cmDspRsrcUIntArray( cmDspSysH_t h, unsigned* np, unsigned** vpp, ... )
  1922. {
  1923. va_list vl;
  1924. va_start(vl,vpp);
  1925. cmDspRC_t rc = cmDspRsrcUIntArrayV(h,np,vpp,vl);
  1926. va_end(vl);
  1927. return rc;
  1928. }
  1929. cmDspRC_t cmDspRsrcDblArray( cmDspSysH_t h, unsigned* np, double** vpp, ... )
  1930. {
  1931. va_list vl;
  1932. va_start(vl,vpp);
  1933. cmDspRC_t rc = cmDspRsrcDblArrayV(h,np,vpp,vl);
  1934. va_end(vl);
  1935. return rc;
  1936. }
  1937. cmDspRC_t cmDspRsrcRealArray( cmDspSysH_t h, unsigned* np, cmReal_t** vpp, ... )
  1938. {
  1939. va_list vl;
  1940. va_start(vl,vpp);
  1941. cmDspRC_t rc = cmDspRsrcRealArrayV(h,np,vpp,vl);
  1942. va_end(vl);
  1943. return rc;
  1944. }
  1945. cmDspRC_t cmDspRsrcStringArray( cmDspSysH_t h, unsigned* np, const cmChar_t*** vpp, ... )
  1946. {
  1947. va_list vl;
  1948. va_start(vl,vpp);
  1949. cmDspRC_t rc = cmDspRsrcStringArrayV(h,np,vpp,vl);
  1950. va_end(vl);
  1951. return rc;
  1952. }
  1953. cmDspRC_t _cmDspWritePathAndParent( cmDspSysH_t h, cmChar_t** pathRef, cmChar_t** varLabelRef, cmJsonNode_t** parentRef, va_list vl )
  1954. {
  1955. cmDsp_t* p = _cmDspHandleToPtr(h);
  1956. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(h);
  1957. cmDspRC_t rc;
  1958. if((rc = _cmDspRsrcWritePath(h,pathRef,varLabelRef,vl)) != kOkDspRC )
  1959. goto errLabel;
  1960. if( *pathRef == NULL )
  1961. *parentRef = cmJsonRoot(jsH);
  1962. else
  1963. *parentRef = cmJsonFindPathValue( jsH, *pathRef, cmJsonRoot(jsH), kPairTId );
  1964. if( *parentRef == NULL )
  1965. {
  1966. rc = cmErrMsg(&p->err,kJsonFailDspRC,"The parent object '%s' for variable '%s' could not be found.",cmStringNullGuard(*pathRef),cmStringNullGuard(*varLabelRef));
  1967. goto errLabel;
  1968. }
  1969. errLabel:
  1970. return rc;
  1971. }
  1972. cmDspRC_t cmDspRsrcWriteStringV( cmDspSysH_t h, const cmChar_t* v, va_list vl )
  1973. {
  1974. cmDsp_t* p = _cmDspHandleToPtr(h);
  1975. cmJsonH_t jsH = cmDspSysPgmRsrcHandle(h);
  1976. cmChar_t* varStr = NULL;
  1977. cmChar_t* pathStr = NULL;
  1978. cmJsonNode_t* parentObjNodePtr = NULL;
  1979. cmDspRC_t rc;
  1980. if((rc = _cmDspWritePathAndParent(h, &pathStr, &varStr, &parentObjNodePtr, vl )) != kOkDspRC )
  1981. goto errLabel;
  1982. if( cmJsonInsertOrReplacePairString( jsH, parentObjNodePtr, varStr, kStringTId, v ) != kOkJsRC )
  1983. rc = cmErrMsg(&p->err,kJsonFailDspRC,"Write 'string' resource value failed for path '%s' and variable '%s'",cmStringNullGuard(pathStr),cmStringNullGuard(varStr));
  1984. errLabel:
  1985. if( pathStr != NULL )
  1986. cmLhFree(cmDspSysLHeap(h),pathStr);
  1987. return rc;
  1988. }
  1989. cmDspRC_t cmDspRsrcWriteString( cmDspSysH_t h, const cmChar_t* v, ... )
  1990. {
  1991. va_list vl;
  1992. va_start(vl,v);
  1993. cmDspRC_t rc = cmDspRsrcWriteStringV(h,v,vl);
  1994. va_end(vl);
  1995. return rc;
  1996. }