libcm is a C development framework with an emphasis on audio signal processing applications.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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