libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmDspClass.c 43KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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 "cmSymTbl.h"
  11. #include "cmPrefs.h"
  12. #include "cmJson.h"
  13. #include "cmDspValue.h"
  14. #include "cmMsgProtocol.h"
  15. #include "cmThread.h"
  16. #include "cmUdpPort.h"
  17. #include "cmUdpNet.h"
  18. #include "cmAudioSys.h"
  19. #include "cmProcObj.h"
  20. #include "cmDspCtx.h"
  21. #include "cmDspClass.h"
  22. #include "cmDspSys.h"
  23. cmDspRC_t cmDspClassSetup(
  24. cmDspClass_t* classPtr,
  25. cmDspCtx_t* ctx,
  26. const cmChar_t* classLabel,
  27. cmDspClassFunc_t finalClassFunc,
  28. cmDspConsFunc_t allocFunc,
  29. cmDspFunc_t freeFunc,
  30. cmDspFunc_t resetFunc,
  31. cmDspFunc_t execFunc,
  32. cmDspFunc_t recvFunc,
  33. cmDspStoreFunc_t storeFunc,
  34. cmDspAttrSymFunc_t sysRecvFunc,
  35. const cmChar_t* doc )
  36. {
  37. classPtr->labelStr = classLabel;
  38. classPtr->doc = doc;
  39. classPtr->finalClassFunc = finalClassFunc;
  40. classPtr->allocFunc = allocFunc;
  41. classPtr->freeFunc = freeFunc;
  42. classPtr->resetFunc = resetFunc;
  43. classPtr->execFunc = execFunc;
  44. classPtr->recvFunc = recvFunc;
  45. classPtr->storeFunc = storeFunc;
  46. classPtr->sysRecvFunc = sysRecvFunc;
  47. cmErrSetup( &classPtr->err, ctx->rpt, classLabel);
  48. return kOkDspRC;
  49. }
  50. // default DSP instance system recv function
  51. cmDspRC_t _cmDspInstSysRecvFunc( cmDspCtx_t* ctx, struct cmDspInst_str* inst, unsigned attrSymId, const cmDspValue_t* value )
  52. {
  53. if( cmDsvIsSymbol(value) )
  54. {
  55. unsigned msgSymId = cmDsvSymbol(value);
  56. if( msgSymId == ctx->_disableSymId )
  57. {
  58. inst->flags = cmSetFlag(inst->flags,kDisableExecInstFl );
  59. //printf("%s disabled\n",cmStringNullGuard(cmDspInstLabel(ctx,inst)));
  60. }
  61. else
  62. {
  63. if( msgSymId == ctx->_enableSymId )
  64. {
  65. inst->flags = cmClrFlag(inst->flags,kDisableExecInstFl );
  66. //printf("%s enabled\n",cmStringNullGuard(cmDspInstLabel(ctx,inst)));
  67. }
  68. }
  69. }
  70. if( inst->classPtr->sysRecvFunc != NULL )
  71. return inst->classPtr->sysRecvFunc(ctx,inst,attrSymId,value);
  72. return kOkDspRC;
  73. }
  74. void* cmDspInstAllocate(
  75. cmDspCtx_t* ctx,
  76. cmDspClass_t* classPtr,
  77. const cmDspVarArg_t* argV,
  78. unsigned instByteCnt,
  79. unsigned instSymId,
  80. unsigned instId,
  81. unsigned storeSymId,
  82. unsigned va_cnt,
  83. va_list vl )
  84. {
  85. unsigned i;
  86. unsigned varCnt = 0;
  87. unsigned varDataByteCnt = 0;
  88. unsigned va_idx = 0;
  89. unsigned reqArgCnt = 0;
  90. // check for duplicate variable names
  91. for(i=0; argV[i].label != NULL; ++i)
  92. if( argV[i].label != NULL )
  93. {
  94. unsigned j = 0;
  95. for(j=0; argV[j].label != NULL; ++j)
  96. if( i != j && argV[j].label != NULL && strcmp(argV[i].label,argV[j].label) == 0 )
  97. {
  98. // two variables have the same name
  99. unsigned mask = kInDsvFl | kOutDsvFl;
  100. // we allow two variables to have the same name as long as they are not both inputs or outputs
  101. if( (argV[j].flags & mask) == (argV[i].flags & mask))
  102. cmDspClassErr(ctx,classPtr,kVarDuplicateDspRC,"The variable label '%s' is used by multiple variables.",argV[i].label);
  103. }
  104. }
  105. // determine the count and size of the instance variables
  106. for(i=0; argV[i].label != NULL; ++i,++varCnt)
  107. {
  108. unsigned rn = argV[i].rn;
  109. unsigned flags = argV[i].flags;
  110. // audio buffer sample count is determined automatically
  111. if( cmAllFlags( argV[i].flags, kAudioBufDsvFl | kOutDsvFl ) )
  112. {
  113. rn = ctx->ctx->ss->args.dspFramesPerCycle;
  114. flags = kSampleDsvFl | kMtxDsvFl;
  115. }
  116. // determine the space needed for matrices of known size
  117. if( cmIsFlag(flags,kMtxDsvFl) && (rn*argV[i].cn) )
  118. {
  119. unsigned n = cmDsvByteCount( flags, rn, argV[i].cn );
  120. varDataByteCnt += n;
  121. }
  122. // determine the number of required arguments
  123. reqArgCnt += cmIsFlag(flags,kReqArgDsvFl) ? 1 : 0;
  124. }
  125. unsigned varRecdByteCnt = varCnt * sizeof(cmDspVar_t);
  126. unsigned ttlByteCnt = instByteCnt + varRecdByteCnt + varDataByteCnt;
  127. char* p = cmLHeapAllocZ(ctx->lhH, ttlByteCnt); // allocate the instance memory
  128. char* ep = p + ttlByteCnt; // record the end of the alloc'd memory
  129. cmDspInst_t* ip = (cmDspInst_t*)p; // set the instance ptr
  130. cmDspVar_t* varArray = (cmDspVar_t*)(p + instByteCnt); // set the instance variable recd array
  131. char* varDataPtr = (char*)(varArray + varCnt); // set the base of variable data buffer
  132. // setup the instance record
  133. ip->classPtr = classPtr;
  134. ip->symId = instSymId;
  135. ip->id = instId;
  136. ip->flags = kDisableExecInstFl;
  137. ip->presetGroupSymId = storeSymId;
  138. ip->varArray = varArray;
  139. ip->varCnt = varCnt;
  140. ip->freeFunc = classPtr->freeFunc;
  141. ip->resetFunc = classPtr->resetFunc;
  142. ip->execFunc = classPtr->execFunc;
  143. ip->recvFunc = classPtr->recvFunc;
  144. ip->storeFunc = classPtr->storeFunc;
  145. ip->sysRecvFunc = _cmDspInstSysRecvFunc;
  146. if( reqArgCnt > va_cnt )
  147. cmDspInstErr(ctx,ip,kVarArgParseFailDspRC,"The class requires %i arguments but only %i were given.",reqArgCnt,va_cnt);
  148. // setup the variable records
  149. for(i=0; i<varCnt; ++i)
  150. {
  151. unsigned flags = argV[i].flags;
  152. unsigned rn = argV[i].rn;
  153. // handle requests for audio output buffers
  154. if( cmAllFlags(argV[i].flags, kAudioBufDsvFl | kOutDsvFl ) )
  155. {
  156. // remove the audio buffer flag and reconstitute as a generic cmDspValue sample mtx type.
  157. //flags = cmClrFlag( argV[i].flags, kAudioBufDsvFl | kTypeDsvMask );
  158. flags = kSampleDsvFl | kMtxDsvFl;
  159. rn = ctx->ctx->ss->args.dspFramesPerCycle;
  160. }
  161. // the constId must be the same as the var index
  162. assert( i == argV[i].constId );
  163. bool reqArgFl = cmIsFlag(argV[i].flags,kReqArgDsvFl);
  164. bool optArgFl = cmIsFlag(argV[i].flags,kOptArgDsvFl);
  165. // verify that the required arg. exists
  166. if( reqArgFl && i>va_cnt )
  167. cmDspInstErr(ctx,ip,kVarArgParseFailDspRC,"The required argument '%s' is missing",argV[i].label);
  168. ip->varArray[i].flags = argV[i].flags; // store the original flag set as the var flags
  169. ip->varArray[i].constId = argV[i].constId;
  170. ip->varArray[i].symId = cmSymTblRegisterSymbol(ctx->stH,argV[i].label);
  171. ip->varArray[i].doc = argV[i].doc;
  172. ip->varArray[i].value.flags = argV[i].flags & kTypeDsvMask;
  173. // if this is a required or optional constructor arg. then assign it as the
  174. // default value for this variable
  175. if( va_idx < va_cnt && (reqArgFl || optArgFl) )
  176. {
  177. cmDspValue_t* vp = &ip->varArray[i].value;
  178. switch(ip->varArray[i].value.flags)
  179. {
  180. case kBoolDsvFl: cmDsvSetBool( vp, va_arg(vl,int )); break;
  181. case kCharDsvFl: cmDsvSetChar( vp, va_arg(vl,int )); break;
  182. case kUCharDsvFl: cmDsvSetUChar( vp, va_arg(vl,int )); break;
  183. case kShortDsvFl: cmDsvSetShort( vp, va_arg(vl,int )); break;
  184. case kUShortDsvFl: cmDsvSetUShort( vp, va_arg(vl,int )); break;
  185. case kLongDsvFl: cmDsvSetLong( vp, va_arg(vl,long )); break;
  186. case kULongDsvFl: cmDsvSetULong( vp, va_arg(vl,unsigned long )); break;
  187. case kIntDsvFl: cmDsvSetInt( vp, va_arg(vl,int )); break;
  188. case kUIntDsvFl: cmDsvSetUInt( vp, va_arg(vl,unsigned )); break;
  189. case kFloatDsvFl: cmDsvSetFloat( vp, va_arg(vl,double )); break;
  190. case kDoubleDsvFl: cmDsvSetDouble( vp, va_arg(vl,double )); break;
  191. case kSampleDsvFl: cmDsvSetSample( vp, va_arg(vl,double )); break;
  192. case kRealDsvFl: cmDsvSetReal( vp, va_arg(vl,double )); break;
  193. case kStrzDsvFl: cmDsvSetStrz( vp, va_arg(vl,cmChar_t* )); break;
  194. case kSymDsvFl: cmDsvSetSymbol( vp, va_arg(vl,unsigned )); break;
  195. default:
  196. cmDspInstErr(ctx,ip,kVarArgParseFailDspRC,"A var-args parse error occurred while parsing the variable '%s'.",argV[i].label);
  197. }
  198. // set the default value from the initial value
  199. cmDspValueSet(ctx, ip, i, vp, kSetDefaultDspFl );
  200. // store the fact that the default value was explicitely set.
  201. ip->varArray[i].flags = cmSetFlag(ip->varArray[i].flags,kDfltSetDsvFl);
  202. // track the number of va_list arg's read
  203. ++va_idx;
  204. }
  205. // assign memory to the matrix types of known size
  206. if( cmIsFlag(flags,kMtxDsvFl) && (rn*argV[i].cn) )
  207. {
  208. cmDsvSetMtx( &ip->varArray[i].value, flags, varDataPtr, rn, argV[i].cn );
  209. unsigned n = cmDsvByteCount( flags, rn, argV[i].cn );
  210. varDataPtr += n;
  211. }
  212. }
  213. assert( varDataPtr == ep );
  214. return p;
  215. }
  216. #ifdef OS_OSX
  217. va_list _cmDspParseArgV( cmDspVarArg_t* a, va_list vl )
  218. #else
  219. void _cmDspParseArgV( cmDspVarArg_t* a, va_list vl )
  220. #endif
  221. {
  222. a->label = va_arg(vl,const char*);
  223. a->constId = va_arg(vl,unsigned);
  224. a->rn = va_arg(vl,unsigned);
  225. a->cn = va_arg(vl,unsigned);
  226. a->flags = va_arg(vl,unsigned);
  227. a->doc = va_arg(vl,const char*);
  228. #ifdef OS_OSX
  229. return vl;
  230. #endif
  231. }
  232. void* cmDspInstAllocateV(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned instByteCnt, unsigned instSymId, unsigned instId, unsigned storeSymId, unsigned va_cnt, va_list vl0, ... )
  233. {
  234. va_list vl1,vl2;
  235. va_start(vl1,vl0);
  236. va_copy(vl2,vl1);
  237. unsigned argCnt = 0;
  238. int repeatCnt;
  239. while( (repeatCnt = va_arg(vl1,int)) != 0 )
  240. {
  241. cmDspVarArg_t a;
  242. // do some value checking to possibly catch problems with the var_args format
  243. assert( repeatCnt > 0 && repeatCnt < 255 );
  244. argCnt += repeatCnt;
  245. #ifdef OS_OSX
  246. vl1 =
  247. #endif
  248. _cmDspParseArgV(&a,vl1);
  249. }
  250. cmDspVarArg_t aa[ argCnt+1 ];
  251. unsigned j=0;
  252. while( (repeatCnt = va_arg(vl2,int)) != 0 )
  253. {
  254. cmDspVarArg_t a;
  255. #ifdef OS_OSX
  256. vl2 =
  257. #endif
  258. _cmDspParseArgV(&a,vl2);
  259. cmDspArgSetupN(ctx,aa,argCnt,j,repeatCnt,a.label,a.constId,a.rn, a.cn, a.flags, a.doc );
  260. j += repeatCnt;
  261. }
  262. cmDspArgSetupNull(aa + argCnt);
  263. va_end(vl1);
  264. return cmDspInstAllocate(ctx,classPtr,aa,instByteCnt,instSymId,instId,storeSymId,va_cnt,vl0);
  265. }
  266. cmDspInstSymId_t* _cmDspInstFindAttrSymId( cmDspInst_t* inst, unsigned symId )
  267. {
  268. cmDspInstSymId_t* ip = inst->symIdList;
  269. for(; ip != NULL; ip=ip->link)
  270. if( ip->symId == symId )
  271. return ip;
  272. return NULL;
  273. }
  274. bool cmDspInstHasAttrSym( cmDspInst_t* inst, unsigned attrSymId )
  275. { return _cmDspInstFindAttrSymId(inst,attrSymId) != NULL; }
  276. cmDspRC_t cmDspInstRegisterAttrSym( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned symId )
  277. {
  278. cmDspInstSymId_t* ip;
  279. // if this symbol is already registered
  280. if((ip = _cmDspInstFindAttrSymId(inst,symId)) != NULL )
  281. return cmDspInstErr(ctx,inst,kOkDspRC,"The symbol '%s' is already registered with the instance.",cmStringNullGuard(cmSymTblLabel(ctx->stH,symId)));
  282. // try to find an unused symbol
  283. ip = inst->symIdList;
  284. for(; ip != NULL; ip=ip->link)
  285. if( ip->symId == cmInvalidId )
  286. break;
  287. // if no unused symbols were found then allocate a new one
  288. if( ip == NULL )
  289. {
  290. ip = cmLhAllocZ(ctx->lhH, cmDspInstSymId_t, 1 );
  291. ip->link = inst->symIdList;
  292. inst->symIdList = ip;
  293. }
  294. ip->symId = symId;
  295. return kOkDspRC;
  296. }
  297. cmDspRC_t cmDspInstRemoveAttrSym( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned symId )
  298. {
  299. cmDspInstSymId_t* ip;
  300. // if this symbol is already registered
  301. if((ip = _cmDspInstFindAttrSymId(inst,symId)) == NULL )
  302. return cmDspInstErr(ctx,inst,kOkDspRC,"The symbol '%s' is could not be found to be removed from the instance.",cmStringNullGuard(cmSymTblLabel(ctx->stH,symId)));
  303. // mark a symbol recd as inactive by setting the symbol id to cmInalidId
  304. ip->symId = cmInvalidId;
  305. return kOkDspRC;
  306. }
  307. cmDspRC_t _cmDspClassErrV( cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned instId, cmDspRC_t rc, const cmChar_t* fmt, va_list vl )
  308. {
  309. va_list vl2;
  310. va_copy(vl2,vl);
  311. unsigned n = vsnprintf(NULL,0,fmt,vl2)+1;
  312. cmChar_t buf[n+1];
  313. vsnprintf(buf,n,fmt,vl);
  314. if( instId == cmInvalidId )
  315. cmErrMsg( &classPtr->err, rc, "%s DSP Class:%s",buf, classPtr->labelStr);
  316. else
  317. cmErrMsg( &classPtr->err, rc, "%s DSP Class:%s Inst:%i",buf, classPtr->labelStr,instId);
  318. return rc;
  319. }
  320. cmDspRC_t cmDspClassErr( cmDspCtx_t* ctx, cmDspClass_t* classPtr, cmDspRC_t rc, const cmChar_t* fmt, ... )
  321. {
  322. va_list vl;
  323. va_start(vl,fmt);
  324. rc = _cmDspClassErrV(ctx,classPtr,cmInvalidId,rc,fmt,vl);
  325. va_end(vl);
  326. return rc;
  327. }
  328. cmDspRC_t cmDspInstErr( cmDspCtx_t* ctx, cmDspInst_t* inst, cmDspRC_t rc, const cmChar_t* fmt, ... )
  329. {
  330. va_list vl;
  331. va_start(vl,fmt);
  332. rc = _cmDspClassErrV(ctx,inst->classPtr,inst->id,rc,fmt,vl);
  333. va_end(vl);
  334. return rc;
  335. }
  336. void cmDspArgSetup(
  337. cmDspCtx_t* ctx,
  338. cmDspVarArg_t* arg,
  339. const cmChar_t* labelPrefix,
  340. unsigned labelId,
  341. unsigned constId,
  342. unsigned rn,
  343. unsigned cn,
  344. unsigned flags,
  345. const cmChar_t* docStr )
  346. {
  347. unsigned labelIdCharCnt = (unsigned)floor( labelId==0 ? 1.0 : log10(abs(labelId)) + 1.0 );
  348. int labelCharCnt = strlen(labelPrefix) + 1 + labelIdCharCnt + 1;
  349. cmChar_t label[ labelCharCnt + 1 ];
  350. const cmChar_t* lp;
  351. if( labelId == cmInvalidId )
  352. lp = labelPrefix;
  353. else
  354. {
  355. lp = label;
  356. label[labelCharCnt] = 0;
  357. snprintf(label,labelCharCnt,"%s-%i",labelPrefix,labelId);
  358. }
  359. // use the symbol table to hold the label string
  360. arg->label = cmSymTblLabel(ctx->stH,cmSymTblRegisterSymbol(ctx->stH,lp));
  361. arg->constId = constId;
  362. arg->rn = rn;
  363. arg->cn = cn;
  364. arg->flags = flags;
  365. arg->doc = cmLhAllocStr(ctx->lhH,docStr);;
  366. }
  367. unsigned cmDspArgCopy( cmDspVarArg_t* argArray, unsigned argN, unsigned dstIdx, const cmDspVarArg_t* s, unsigned sn )
  368. {
  369. assert( dstIdx + sn <= argN );
  370. unsigned i;
  371. for(i=0; i<sn; ++i)
  372. argArray[dstIdx+i] = s[i];
  373. return dstIdx + i;
  374. }
  375. unsigned cmDspArgSetupN(
  376. cmDspCtx_t* ctx,
  377. cmDspVarArg_t* arg,
  378. unsigned argN,
  379. unsigned dstIdx,
  380. unsigned cnt,
  381. const cmChar_t* labelPrefix,
  382. unsigned baseConstId,
  383. unsigned rn,
  384. unsigned cn,
  385. unsigned flags,
  386. const cmChar_t* staticDocStr )
  387. {
  388. assert( dstIdx + cnt <= argN );
  389. unsigned i;
  390. for(i=0; i<cnt; ++i)
  391. cmDspArgSetup(ctx,arg + dstIdx + i,labelPrefix, cnt==1 ? cmInvalidId : i, baseConstId + i, rn, cn, flags, staticDocStr );
  392. return dstIdx + i;
  393. }
  394. void cmDspArgSetupNull( cmDspVarArg_t* arg )
  395. { memset(arg,0,sizeof(*arg)); }
  396. const cmChar_t* cmDspInstLabel( cmDspCtx_t* ctx, cmDspInst_t* inst )
  397. {
  398. const char* noInstLbl = "<no_inst_lbl>";
  399. const char* lbl = noInstLbl;
  400. if( inst->symId == cmInvalidId )
  401. return lbl;
  402. if((lbl = cmSymTblLabel(ctx->stH,inst->symId)) == NULL )
  403. lbl = noInstLbl;
  404. return lbl;
  405. }
  406. cmDspRC_t _cmDspVarPtr( const cmDspInst_t* inst, unsigned varId, cmDspVar_t** retVarPtrPtr )
  407. {
  408. if( varId >= inst->varCnt )
  409. {
  410. *retVarPtrPtr = NULL;
  411. return cmErrMsg(&inst->classPtr->err,kVarNotFoundDspRC,"The variable id %i is invalid for DSP instance '%s' (id:%i) .", varId, inst->classPtr->labelStr, inst->id );
  412. }
  413. assert( inst->varArray[varId].constId == varId );
  414. *retVarPtrPtr = inst->varArray + varId;
  415. return kOkDspRC;
  416. }
  417. cmDspRC_t _cmDspVarAndValuePtr( cmDspInst_t* inst, unsigned varId, cmDspVar_t** varPtrPtr, cmDspValue_t** valPtrPtr )
  418. {
  419. cmDspRC_t rc;
  420. if((rc = _cmDspVarPtr(inst,varId,varPtrPtr)) != kOkDspRC )
  421. *valPtrPtr = NULL;
  422. else
  423. *valPtrPtr = cmDsvValuePtr(&(*varPtrPtr)->value);
  424. return rc;
  425. }
  426. cmDspRC_t _cmDspVarValuePtr( cmDspInst_t* inst, unsigned varId, cmDspValue_t** retValPtrPtr )
  427. {
  428. cmDspVar_t* varPtr = NULL;
  429. cmDspRC_t rc;
  430. if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
  431. *retValPtrPtr = NULL;
  432. else
  433. *retValPtrPtr = cmDsvValuePtr(&varPtr->value);
  434. return rc;
  435. }
  436. cmDspRC_t _cmDspVarDefaultPtr( cmDspInst_t* inst, unsigned varId, cmDspValue_t** retValPtrPtr )
  437. {
  438. cmDspVar_t* varPtr = NULL;
  439. cmDspRC_t rc;
  440. if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
  441. *retValPtrPtr = NULL;
  442. else
  443. *retValPtrPtr = &varPtr->dflt;
  444. return rc;
  445. }
  446. void _cmDspSendEvt( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, unsigned srcVarId, const cmDspVar_t* varPtr )
  447. {
  448. cmDspEvt_t e;
  449. cmDspCb_t* cbp = varPtr->cbList;
  450. if( cbp == NULL )
  451. return;
  452. e.flags = kDfltEvtDspFlags;
  453. e.srcInstPtr = srcInstPtr;
  454. e.srcVarId = srcVarId;
  455. e.valuePtr = &varPtr->value;
  456. for(; cbp!=NULL; cbp=cbp->linkPtr)
  457. {
  458. e.dstVarId = cbp->dstVarId;
  459. e.dstDataPtr = cbp->dstDataPtr;
  460. if( cbp->dstInstPtr->recvFunc!=NULL)
  461. cbp->dstInstPtr->recvFunc(ctx,cbp->dstInstPtr,&e);
  462. }
  463. }
  464. cmDspVar_t* cmDspVarSymbolToPtr( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varSymId, unsigned flags )
  465. {
  466. unsigned i;
  467. for(i=0; i<inst->varCnt; ++i)
  468. if( inst->varArray[i].symId == varSymId )
  469. {
  470. if( flags==0 || cmAllFlags(inst->varArray[i].flags,flags) )
  471. return inst->varArray + i;
  472. }
  473. cmDspInstErr(ctx,inst,kVarNotFoundDspRC,"The variable '%s' could not be found in the instance '%s' (id:%i).",
  474. cmSymTblLabel( ctx->stH, varSymId), inst->classPtr->labelStr, inst->id );
  475. return NULL;
  476. }
  477. const cmDspVar_t* cmDspVarIdToCPtr( const cmDspInst_t* inst, unsigned varId )
  478. {
  479. cmDspVar_t* varPtr;
  480. if( _cmDspVarPtr(inst, varId, &varPtr) != kOkDspRC )
  481. return NULL;
  482. return varPtr;
  483. }
  484. cmDspVar_t* cmDspVarIdToPtr( cmDspInst_t* inst, unsigned varId )
  485. {
  486. cmDspVar_t* varPtr;
  487. if( _cmDspVarPtr(inst, varId, &varPtr) != kOkDspRC )
  488. return NULL;
  489. return varPtr;
  490. }
  491. const cmChar_t* cmDspVarLabel( cmDspCtx_t* ctx, const cmDspInst_t* inst, unsigned varId )
  492. {
  493. const cmDspVar_t* varPtr;
  494. if(( varPtr = cmDspVarIdToCPtr(inst,varId) ) == NULL )
  495. return NULL;
  496. return cmSymTblLabel( ctx->stH, varPtr->symId);
  497. }
  498. cmDspRC_t cmDspOutputEvent( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  499. {
  500. cmDspRC_t rc;
  501. cmDspVar_t* varPtr;
  502. if((rc = _cmDspVarPtr(inst, varId, &varPtr )) != kOkDspRC )
  503. return rc;
  504. _cmDspSendEvt(ctx, inst, varId, varPtr );
  505. return rc;
  506. }
  507. cmDspRC_t cmDspVarPresetWrite( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  508. {
  509. cmDspRC_t rc;
  510. const cmDspVar_t* varPtr;
  511. if((varPtr = cmDspVarIdToCPtr(inst,varId)) == NULL )
  512. return cmDspInstErr(ctx,inst,kVarNotFoundDspRC,"Var with id %i not found during preset store.");
  513. if((rc = cmDspSysPresetWriteValue(ctx->dspH, varPtr->symId, &varPtr->value )) != kOkDspRC )
  514. return cmDspInstErr(ctx,inst,kSubSysFailDspRC,"Var preset storage failed on var:'%s'.",cmStringNullGuard(cmSymTblLabel(ctx->stH,varPtr->symId)));
  515. return rc;
  516. }
  517. cmDspRC_t cmDspVarPresetRead( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  518. {
  519. cmDspRC_t rc;
  520. cmDspVar_t* varPtr;
  521. cmDspValue_t value;
  522. if((varPtr = cmDspVarIdToPtr(inst,varId)) == NULL )
  523. return cmDspInstErr(ctx,inst,kVarNotFoundDspRC,"Var with id %i not found during preset recall.");
  524. if((rc = cmDspSysPresetReadValue(ctx->dspH, varPtr->symId, &value )) != kOkDspRC )
  525. return cmDspInstErr(ctx,inst,kSubSysFailDspRC,"Var preset recall failed on var:'%s'.",cmStringNullGuard(cmSymTblLabel(ctx->stH,varPtr->symId)));
  526. return cmDspValueSet(ctx,inst,varId,&value,0);
  527. }
  528. cmDspRC_t cmDspVarPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, bool storeFl )
  529. {
  530. cmDspRC_t rc;
  531. if( storeFl )
  532. rc = cmDspVarPresetWrite(ctx,inst,varId);
  533. else
  534. rc = cmDspVarPresetRead(ctx,inst,varId);
  535. return rc;
  536. }
  537. double cmDspSampleRate( cmDspCtx_t* ctx )
  538. { return ctx->ctx->ss->args.srate; }
  539. unsigned cmDspSamplesPerCycle( cmDspCtx_t * ctx )
  540. { return ctx->ctx->ss->args.dspFramesPerCycle; }
  541. unsigned cmDspTypeFlags(const cmDspInst_t* inst, unsigned varId )
  542. {
  543. cmDspVar_t* varPtr;
  544. unsigned flags;
  545. if( _cmDspVarPtr(inst,varId,&varPtr) != kOkDspRC )
  546. return 0;
  547. cmDspValue_t* valPtr = cmDsvValuePtr(&varPtr->value);
  548. if( valPtr->flags != 0 )
  549. flags = valPtr->flags;
  550. else
  551. flags = varPtr->flags;
  552. return flags & kTypeDsvMask;
  553. }
  554. bool cmDspIsBool( const cmDspInst_t* inst, unsigned varId )
  555. { return cmIsFlag(cmDspTypeFlags(inst,varId),kBoolDsvFl); }
  556. bool cmDspIsInt( const cmDspInst_t* inst, unsigned varId )
  557. { return cmIsFlag(cmDspTypeFlags(inst,varId),kIntDsvFl); }
  558. bool cmDspIsUInt( const cmDspInst_t* inst, unsigned varId )
  559. { return cmIsFlag(cmDspTypeFlags(inst,varId),kUIntDsvFl); }
  560. bool cmDspIsDouble( const cmDspInst_t* inst, unsigned varId )
  561. { return cmIsFlag(cmDspTypeFlags(inst,varId),kDoubleDsvFl); }
  562. bool cmDspIsStrz( const cmDspInst_t* inst, unsigned varId )
  563. { return cmIsFlag(cmDspTypeFlags(inst,varId),kStrzDsvFl); }
  564. bool cmDspIsSymbol( const cmDspInst_t* inst, unsigned varId )
  565. { return cmIsFlag(cmDspTypeFlags(inst,varId),kSymDsvFl); }
  566. bool cmDspIsJson( const cmDspInst_t* inst, unsigned varId )
  567. { return cmIsFlag(cmDspTypeFlags(inst,varId),kJsonDsvFl); }
  568. bool cmDspBool( cmDspInst_t* inst, unsigned varId )
  569. {
  570. cmDspValue_t* vp;
  571. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  572. return 0;
  573. return cmDsvGetBool(vp);
  574. }
  575. int cmDspInt( cmDspInst_t* inst, unsigned varId )
  576. {
  577. cmDspValue_t* vp;
  578. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  579. return 0;
  580. return cmDsvGetInt(vp);
  581. }
  582. unsigned cmDspUInt( cmDspInst_t* inst, unsigned varId )
  583. {
  584. cmDspValue_t* vp;
  585. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  586. return 0;
  587. return cmDsvGetUInt(vp);
  588. }
  589. double cmDspDouble( cmDspInst_t* inst, unsigned varId )
  590. {
  591. cmDspValue_t* vp;
  592. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  593. return 0;
  594. return cmDsvGetDouble(vp);
  595. }
  596. cmSample_t cmDspSample( cmDspInst_t* inst, unsigned varId )
  597. {
  598. cmDspValue_t* vp;
  599. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  600. return 0;
  601. return cmDsvGetSample(vp);
  602. }
  603. cmReal_t cmDspReal( cmDspInst_t* inst, unsigned varId )
  604. {
  605. cmDspValue_t* vp;
  606. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  607. return 0;
  608. return cmDsvGetReal(vp);
  609. }
  610. const cmChar_t* cmDspStrcz( cmDspInst_t* inst, unsigned varId )
  611. {
  612. cmDspValue_t* vp;
  613. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  614. return NULL;
  615. return cmDsvStrcz(vp);
  616. }
  617. unsigned cmDspSymbol( cmDspInst_t* inst, unsigned varId )
  618. {
  619. cmDspValue_t* vp;
  620. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  621. return cmInvalidId;
  622. return cmDsvSymbol(vp);
  623. }
  624. cmJsonNode_t* cmDspJson( cmDspInst_t* inst, unsigned varId )
  625. {
  626. cmDspValue_t* vp;
  627. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  628. return NULL;
  629. return cmDsvJson(vp);
  630. }
  631. bool cmDspDefaultBool( cmDspInst_t* inst, unsigned varId )
  632. {
  633. cmDspValue_t* vp;
  634. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  635. return 0;
  636. return cmDsvGetBool(vp);
  637. }
  638. int cmDspDefaultInt( cmDspInst_t* inst, unsigned varId )
  639. {
  640. cmDspValue_t* vp;
  641. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  642. return 0;
  643. return cmDsvGetInt(vp);
  644. }
  645. unsigned cmDspDefaultUInt( cmDspInst_t* inst, unsigned varId )
  646. {
  647. cmDspValue_t* vp;
  648. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  649. return 0;
  650. return cmDsvGetUInt(vp);
  651. }
  652. double cmDspDefaultDouble( cmDspInst_t* inst, unsigned varId )
  653. {
  654. cmDspValue_t* vp;
  655. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  656. return 0;
  657. return cmDsvGetDouble(vp);
  658. }
  659. cmSample_t cmDspDefaultSample( cmDspInst_t* inst, unsigned varId )
  660. {
  661. cmDspValue_t* vp;
  662. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  663. return 0;
  664. return cmDsvGetSample(vp);
  665. }
  666. cmReal_t cmDspDefaultReal( cmDspInst_t* inst, unsigned varId )
  667. {
  668. cmDspValue_t* vp;
  669. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  670. return 0;
  671. return cmDsvGetReal(vp);
  672. }
  673. const cmChar_t* cmDspDefaultStrcz( cmDspInst_t* inst, unsigned varId )
  674. {
  675. cmDspValue_t* vp;
  676. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  677. return NULL;
  678. return cmDsvGetStrcz(vp);
  679. }
  680. unsigned cmDspDefaultSymbol( cmDspInst_t* inst, unsigned varId )
  681. {
  682. cmDspValue_t* vp;
  683. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  684. return cmInvalidId;
  685. return cmDsvGetSymbol(vp);
  686. }
  687. cmJsonNode_t* cmDspDefaultJson( cmDspInst_t* inst, unsigned varId )
  688. {
  689. cmDspValue_t* vp;
  690. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  691. return NULL;
  692. return cmDsvGetJson(vp);
  693. }
  694. cmDspRC_t cmDspValueSet( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmDspValue_t* svp, unsigned flags )
  695. {
  696. cmDspRC_t rc;
  697. cmDspVar_t* varPtr;
  698. // get a pointer to the var recd
  699. if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
  700. return rc;
  701. // get a pointer to the target value.
  702. cmDspValue_t* dvp = cmIsFlag(flags,kSetDefaultDspFl) ? &varPtr->dflt : cmDsvValuePtr(&varPtr->value);
  703. // store a pointer to the dst string memory in case we need to delete it later
  704. cmChar_t* rlsPtr = cmAllFlags(dvp->flags,kStrzDsvFl | kDynDsvFl) ? dvp->u.z : NULL;
  705. // set the dest type to the type shared with the source
  706. // ( this assumes that the dst may have multiple types but the source has one type)
  707. unsigned typeFlags = (varPtr->flags & svp->flags) & kTypeDsvMask;
  708. // if svp->flags is set to kNullDsvFl then use just the dst type
  709. if( typeFlags == 0 )
  710. typeFlags = varPtr->flags & kTypeDsvMask;
  711. // convert the source to the var type
  712. switch(typeFlags)
  713. {
  714. case kBoolDsvFl:
  715. cmDsvSetBool( dvp, cmDsvGetBool(svp));
  716. break;
  717. case kIntDsvFl:
  718. cmDsvSetInt( dvp, cmDsvGetInt(svp));
  719. break;
  720. case kUIntDsvFl:
  721. cmDsvSetUInt( dvp, cmDsvGetUInt(svp));
  722. break;
  723. case kDoubleDsvFl:
  724. cmDsvSetDouble(dvp, cmDsvGetDouble(svp));
  725. break;
  726. case kSampleDsvFl:
  727. cmDsvSetSample(dvp, cmDsvGetSample(svp));
  728. break;
  729. case kRealDsvFl:
  730. cmDsvSetReal(dvp, cmDsvGetReal(svp));
  731. break;
  732. case kSymDsvFl:
  733. cmDsvSetSymbol(dvp, cmDsvGetSymbol(svp));
  734. break;
  735. case kJsonDsvFl:
  736. cmDsvSetJson( dvp, cmDsvJson(svp));
  737. break;
  738. case kStrzDsvFl:
  739. {
  740. const cmChar_t* sp;
  741. if( cmDsvIsSymbol(svp) )
  742. sp = cmSymTblLabel(ctx->stH,cmDsvSymbol(svp));
  743. else
  744. sp = cmDsvGetStrcz(svp);
  745. // don't copy over myself
  746. if( sp == dvp->u.z )
  747. rlsPtr = NULL;
  748. else
  749. {
  750. // if the source == NULL then set the dst to NULL
  751. // (NULL should always be a legitimate value)
  752. if( sp == NULL )
  753. cmDsvSetStrcz(dvp,sp);
  754. else
  755. {
  756. // if the source should not be copied into internal memory
  757. if( cmIsFlag(flags,kNoAllocDspFl) )
  758. cmDsvSetStrcz(dvp,sp);
  759. else
  760. {
  761. // allocate memory to hold the new string
  762. unsigned n = strlen(sp)+1;
  763. cmChar_t* dp = cmLhResizeN(ctx->lhH,cmChar_t,rlsPtr,n);
  764. strncpy(dp,sp,n);
  765. cmDsvSetStrz(dvp, dp);
  766. dvp->flags = cmSetFlag(dvp->flags,kDynDsvFl);
  767. rlsPtr = NULL;
  768. }
  769. }
  770. }
  771. }
  772. break;
  773. default:
  774. { assert(0); }
  775. }
  776. // if the dst contained a dynamically alloc'd string prior to being
  777. // set with a new value - then release the memory used by the original
  778. // string here
  779. if( rlsPtr != NULL )
  780. cmLHeapFree(ctx->lhH,rlsPtr);
  781. // notify listeners of the change of value
  782. if( cmIsFlag(flags,kNoSendDspFl) == false )
  783. _cmDspSendEvt( ctx, inst, varId, varPtr );
  784. // notify the UI of the change of value
  785. if( cmIsFlag(varPtr->flags,kUiDsvFl) && cmIsFlag(flags,kNoUpdateUiDspFl)==false )
  786. {
  787. cmDspUiSendVar( ctx, inst, varPtr );
  788. }
  789. return rc;
  790. }
  791. cmDspRC_t cmDspApplyDefault( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  792. {
  793. cmDspRC_t rc;
  794. cmDspVar_t* varPtr;
  795. // get a pointer to the var recd
  796. if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
  797. return rc;
  798. if( cmIsFlag(varPtr->flags,kDfltSetDsvFl) )
  799. {
  800. // unless the var has kSendDfltDsvFl set we don't transmit the default values
  801. unsigned flags = cmIsFlag(varPtr->flags,kSendDfltDsvFl) ? 0 : kNoSendDspFl;
  802. return cmDspValueSet(ctx, inst, varId, &varPtr->dflt, flags );
  803. }
  804. return kOkDspRC;
  805. }
  806. cmDspRC_t cmDspApplyAllDefaults( cmDspCtx_t* ctx, cmDspInst_t* inst )
  807. {
  808. cmDspRC_t rc = kOkDspRC;
  809. unsigned i;
  810. for(i=0; i<inst->varCnt; ++i)
  811. if((rc = cmDspApplyDefault(ctx,inst,inst->varArray[i].constId)) != kOkDspRC )
  812. return rc;
  813. return rc;
  814. }
  815. cmDspRC_t cmDspSetDefault( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmDspValue_t* valPtr )
  816. {
  817. cmDspRC_t rc;
  818. cmDspVar_t* varPtr;
  819. // get a pointer to the var recd
  820. if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
  821. return rc;
  822. if(( cmDspValueSet( ctx, inst, varId, valPtr, kSetDefaultDspFl )) == kOkDspRC )
  823. {
  824. varPtr->flags = cmSetFlag(varPtr->flags, kDfltSetDsvFl);
  825. }
  826. return rc;
  827. }
  828. cmDspRC_t _cmDspDefaultSet( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmDspValue_t* valPtr, bool nonInitValIsEqualFl )
  829. {
  830. cmDspRC_t rc;
  831. cmDspVar_t* varPtr;
  832. // get a pointer to the var recd
  833. if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
  834. return rc;
  835. // if the dflt value was previuosly set and the default value is
  836. // not equal to the non-intialized value for this var then the default value
  837. // has already been set to a legal value
  838. if( cmIsFlag(varPtr->flags,kDfltSetDsvFl) && nonInitValIsEqualFl==false )
  839. return kOkDspRC;
  840. return cmDspSetDefault(ctx,inst,varId,valPtr);
  841. }
  842. cmDspRC_t cmDspSetDefaultBool( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, bool nonInitVal, bool val )
  843. {
  844. cmDspValue_t v;
  845. double dv = cmDspDefaultBool(inst,varId);
  846. cmDsvSetBool(&v,val);
  847. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  848. }
  849. cmDspRC_t cmDspSetDefaultInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, int nonInitVal, int val )
  850. {
  851. cmDspValue_t v;
  852. int dv = cmDspDefaultInt(inst,varId);
  853. cmDsvSetInt(&v,val);
  854. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  855. }
  856. cmDspRC_t cmDspSetDefaultUInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned nonInitVal, unsigned val )
  857. {
  858. cmDspValue_t v;
  859. unsigned dv = cmDspDefaultUInt(inst,varId);
  860. cmDsvSetUInt(&v,val);
  861. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  862. }
  863. cmDspRC_t cmDspSetDefaultDouble( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, double nonInitVal, double val )
  864. {
  865. cmDspValue_t v;
  866. double dv = cmDspDefaultDouble(inst,varId);
  867. cmDsvSetDouble(&v,val);
  868. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  869. }
  870. cmDspRC_t cmDspSetDefaultSample( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmSample_t nonInitVal, cmSample_t val )
  871. {
  872. cmDspValue_t v;
  873. cmSample_t dv = cmDspDefaultSample(inst,varId);
  874. cmDsvSetSample(&v,val);
  875. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  876. }
  877. cmDspRC_t cmDspSetDefaultReal( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmReal_t nonInitVal, cmReal_t val )
  878. {
  879. cmDspValue_t v;
  880. cmReal_t dv = cmDspDefaultReal(inst,varId);
  881. cmDsvSetReal(&v,val);
  882. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  883. }
  884. cmDspRC_t cmDspSetDefaultSymbol( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned val )
  885. {
  886. cmDspValue_t v;
  887. unsigned dv = cmDspDefaultSymbol(inst,varId);
  888. cmDsvSetSymbol(&v,val);
  889. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==cmInvalidId);
  890. }
  891. cmDspRC_t cmDspSetDefaultStrcz( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmChar_t* nonInitVal, const cmChar_t* val )
  892. {
  893. cmDspValue_t v;
  894. const cmChar_t* dv= cmDspDefaultStrcz(inst,varId);
  895. cmDsvSetStrcz(&v,val);
  896. bool fl = false; // assume the default and non-init value are not equal
  897. // if the pointers are the same then the strings must match
  898. if( dv == nonInitVal )
  899. fl = true;
  900. else
  901. // if both strings exist - then test if they are equal
  902. if( dv!=NULL && nonInitVal!=NULL)
  903. fl = strcmp(dv,nonInitVal)==0;
  904. return _cmDspDefaultSet(ctx,inst,varId,&v,fl);
  905. }
  906. cmDspRC_t cmDspSetDefaultJson( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmJsonNode_t* nonInitVal, cmJsonNode_t* val )
  907. {
  908. cmDspValue_t v;
  909. cmJsonNode_t* dv= cmDspDefaultJson(inst,varId);
  910. cmDsvSetJson(&v,val);
  911. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  912. }
  913. cmDspRC_t cmDspSetBool( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, bool val )
  914. {
  915. cmDspValue_t v;
  916. cmDsvSetBool(&v,val);
  917. return cmDspValueSet(ctx, inst, varId, &v, 0);
  918. }
  919. cmDspRC_t cmDspSetInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, int val )
  920. {
  921. cmDspValue_t v;
  922. cmDsvSetInt(&v,val);
  923. return cmDspValueSet(ctx, inst, varId, &v, 0);
  924. }
  925. cmDspRC_t cmDspSetUInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned val )
  926. {
  927. cmDspValue_t v;
  928. cmDsvSetUInt(&v,val);
  929. return cmDspValueSet(ctx, inst, varId, &v, 0);
  930. }
  931. cmDspRC_t cmDspSetDouble( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, double val )
  932. {
  933. cmDspValue_t v;
  934. cmDsvSetDouble(&v,val);
  935. return cmDspValueSet(ctx, inst, varId, &v, 0);
  936. }
  937. cmDspRC_t cmDspSetSample( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmSample_t val )
  938. {
  939. cmDspValue_t v;
  940. cmDsvSetSample(&v,val);
  941. return cmDspValueSet(ctx, inst, varId, &v, 0);
  942. }
  943. cmDspRC_t cmDspSetReal( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmReal_t val )
  944. {
  945. cmDspValue_t v;
  946. cmDsvSetReal(&v,val);
  947. return cmDspValueSet(ctx, inst, varId, &v, 0);
  948. }
  949. cmDspRC_t cmDspSetStrcz( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmChar_t* val )
  950. {
  951. cmDspValue_t v;
  952. cmDsvSetStrcz(&v,val);
  953. return cmDspValueSet(ctx, inst, varId, &v, 0);
  954. }
  955. cmDspRC_t cmDspSetSymbol( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned val )
  956. {
  957. cmDspValue_t v;
  958. cmDsvSetSymbol(&v,val);
  959. return cmDspValueSet(ctx, inst, varId, &v, 0);
  960. }
  961. cmDspRC_t cmDspSetJson( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmJsonNode_t* val )
  962. {
  963. cmDspValue_t v;
  964. cmDsvSetJson(&v,val);
  965. return cmDspValueSet(ctx, inst, varId, &v, 0);
  966. }
  967. cmDspRC_t cmDspSetEvent( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  968. {
  969. unsigned flags = kUpdateUiDspFl;
  970. // We will assume that all values should be reflected to the UI - unless
  971. // this event is known to have been generated by the UI (evt->kUiDspFl is set)
  972. // and evt->kUiEchoDspFl is not set.
  973. if( cmIsFlag(evt->flags,kUiDspFl)==true && cmIsFlag(evt->flags,kUiEchoDspFl)==false )
  974. flags = kNoUpdateUiDspFl;
  975. // Note: If the event target variable is not a UI variable
  976. // (i.e. cmDspVar_t.kUiDsvFl is not set) then calling cmDspValueSet()
  977. // with the kUpdateUiDspFl set has no effect.
  978. return cmDspValueSet(ctx,inst,evt->dstVarId, evt->valuePtr, flags);
  979. }
  980. cmDspRC_t cmDspSetEventUi( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  981. { return cmDspValueSet(ctx,inst,evt->dstVarId, evt->valuePtr, kUpdateUiDspFl); }
  982. cmDspRC_t cmDspSetEventUiId( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt, unsigned varId )
  983. {
  984. cmDspEvt_t e;
  985. cmDspEvtCopy(&e,evt);
  986. e.dstVarId = varId;
  987. evt = &e;
  988. return cmDspSetEventUi(ctx,inst,evt);
  989. }
  990. unsigned cmDspVarRows( cmDspInst_t* inst, unsigned varId )
  991. {
  992. cmDspValue_t* vp;
  993. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  994. return 0;
  995. return cmDsvRows(vp);
  996. }
  997. unsigned cmDspVarCols( cmDspInst_t* inst, unsigned varId )
  998. {
  999. cmDspValue_t* vp;
  1000. if( _cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  1001. return 0;
  1002. return cmDsvCols(vp);
  1003. }
  1004. bool cmDspIsAudioInputConnected( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  1005. {
  1006. cmDspValue_t* vp;
  1007. if( _cmDspVarValuePtr(inst,varId,&vp ) != kOkDspRC )
  1008. return false;
  1009. if( cmAllFlags(vp->flags, kSampleDsvFl | kMtxDsvFl ))
  1010. return true;
  1011. return false;
  1012. }
  1013. cmDspRC_t cmDspZeroAudioBuf( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  1014. {
  1015. cmDspValue_t* valPtr;
  1016. cmDspVar_t* varPtr;
  1017. cmDspRC_t rc;
  1018. if((rc = _cmDspVarAndValuePtr(inst,varId,&varPtr,&valPtr)) != kOkDspRC )
  1019. return rc;
  1020. bool fl0 = cmAllFlags(valPtr->flags, kSampleDsvFl | kMtxDsvFl );
  1021. bool fl1 = cmIsFlag(varPtr->flags,kOutDsvFl);
  1022. if( fl0==false || fl1==false )
  1023. return cmDspInstErr(ctx,inst,kVarTypeErrDspRC,"The variable '%s' of DSP instance %s (id:%i) is not an output audio buffer.", cmDspVarLabel(ctx,inst,varId),inst->classPtr->labelStr,inst->id);
  1024. memset(cmDsvSampleMtx(valPtr), 0, cmDsvEleCount(valPtr)*sizeof(cmSample_t) );
  1025. return rc;
  1026. }
  1027. cmSample_t* cmDspAudioBuf( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned chIdx )
  1028. {
  1029. cmDspValue_t* vp;
  1030. cmDspVar_t* varPtr;
  1031. cmDspRC_t rc;
  1032. if((rc = _cmDspVarAndValuePtr(inst,varId,&varPtr,&vp )) != kOkDspRC )
  1033. return NULL;
  1034. if( !cmAllFlags(vp->flags, kSampleDsvFl | kMtxDsvFl ))
  1035. {
  1036. // this is an unconnected audio input
  1037. if( cmAllFlags(varPtr->flags,kInDsvFl | kAudioBufDsvFl ) )
  1038. return NULL;
  1039. cmDspInstErr(ctx,inst,kVarTypeErrDspRC,"The variable '%s' of DSP instance '%s' (id:%i) is not an audio buffer.", cmDspVarLabel(ctx,inst,varId),inst->classPtr->labelStr,inst->id);
  1040. return NULL;
  1041. }
  1042. if( chIdx >= cmDsvCols(vp) )
  1043. {
  1044. cmDspInstErr(ctx,inst,kVarTypeErrDspRC,"Channel index %i of audio buffer variable '%s' in DSP instance '%s' (id:%i) is outside the channel count (%i).",
  1045. chIdx,cmDspVarLabel(ctx,inst,varId),inst->classPtr->labelStr,inst->id,cmDsvCols(vp));
  1046. }
  1047. return cmDsvSampleMtx(vp) + (chIdx * cmDsvRows(vp));
  1048. }
  1049. unsigned cmDspAudioBufSmpCount( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned chIdx )
  1050. {
  1051. cmDspValue_t* vp;
  1052. cmDspRC_t rc;
  1053. if((rc = _cmDspVarValuePtr(inst,varId,&vp)) != kOkDspRC )
  1054. return 0;
  1055. if( !cmAllFlags(vp->flags, kSampleDsvFl | kMtxDsvFl ))
  1056. {
  1057. cmDspInstErr(ctx,inst,kVarTypeErrDspRC,"The variable '%s' of DSP instance '%s' (id:%i) is not an audio buffer or is an unconnected input audio buffer.", cmDspVarLabel(ctx,inst,varId),inst->classPtr->labelStr,inst->id);
  1058. return 0;
  1059. }
  1060. return cmDsvRows(vp);
  1061. }
  1062. cmDspRC_t cmDspInstInstallCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, unsigned srcVarSymId, cmDspInst_t* dstInstPtr, unsigned dstVarSymId, void* dstDataPtr)
  1063. {
  1064. cmDspVar_t* srcVarPtr;
  1065. cmDspVar_t* dstVarPtr;
  1066. // get the src and dst var ptrs
  1067. if((srcVarPtr = cmDspVarSymbolToPtr(ctx,srcInstPtr,srcVarSymId,kOutDsvFl)) == NULL || (dstVarPtr = cmDspVarSymbolToPtr(ctx,dstInstPtr,dstVarSymId,kInDsvFl)) == NULL )
  1068. return cmDspInstErr(ctx,srcInstPtr,kInstCbInstallFailDspRC,"Callback installation failed for connection from '%s' (id:%i) var:'%s' to '%s' (id:%i) var:'%s'.",
  1069. srcInstPtr->classPtr->labelStr, srcInstPtr->id, cmSymTblLabel(ctx->stH,srcVarSymId), dstInstPtr->classPtr->labelStr, dstInstPtr->id, cmSymTblLabel(ctx->stH,dstVarSymId) );
  1070. // check for strict type conversion - this will fail for vars which allow multiple types
  1071. // (e.g. numbers and strings)
  1072. if( cmDsvCanConvertFlags(dstVarPtr->value.flags,srcVarPtr->value.flags) == false )
  1073. {
  1074. // if all the flags in the src are set in the dst then the match is ok
  1075. // (this will cover dst types which admit multiple types)
  1076. if( cmAllFlags(dstVarPtr->flags & kTypeDsvMask,srcVarPtr->flags & kTypeDsvMask) == false )
  1077. cmDspInstErr(ctx,srcInstPtr,kOkDspRC,"Data types may not be compatible for connection to '%s' id:%i var:'%s' from '%s'.",dstInstPtr->classPtr->labelStr, dstInstPtr->id, cmSymTblLabel(ctx->stH,dstVarSymId), cmSymTblLabel(ctx->stH,srcVarSymId));
  1078. }
  1079. if( cmIsFlag(srcVarPtr->flags, kAudioBufDsvFl) )
  1080. cmDspInstErr(ctx,srcInstPtr,kOkDspRC,"An audio source is being used as an event callback connection.");
  1081. if( cmIsFlag(dstVarPtr->flags, kAudioBufDsvFl ) )
  1082. cmDspInstErr(ctx,dstInstPtr,kOkDspRC,"An audio destination is the target of a callback connection.");
  1083. cmDspCb_t* r = cmLhAllocZ( ctx->lhH, cmDspCb_t, 1 );
  1084. r->srcVarSymId = srcVarSymId;
  1085. r->dstInstPtr = dstInstPtr;
  1086. r->dstVarId = dstVarPtr->constId;
  1087. r->dstDataPtr = dstDataPtr;
  1088. r->linkPtr = NULL;
  1089. cmDspCb_t* cp = srcVarPtr->cbList;
  1090. cmDspCb_t* pp = NULL;
  1091. for(; cp != NULL; cp = cp->linkPtr )
  1092. pp = cp;
  1093. if( pp == NULL )
  1094. srcVarPtr->cbList = r;
  1095. else
  1096. pp->linkPtr = r;
  1097. return kOkDspRC;
  1098. }
  1099. cmDspRC_t cmDspInstallCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, void* dstCbDataPtr)
  1100. {
  1101. unsigned srcVarSymId,dstVarSymId;
  1102. if( srcInstPtr == NULL && dstInstPtr == NULL )
  1103. return cmErrMsg(&ctx->cmCtx->err,kInstNotFoundDspRC,"The callback installation was passed a NULL source and destination processor instance pointer.");
  1104. if( srcInstPtr == NULL )
  1105. return cmErrMsg(&ctx->cmCtx->err,kInstNotFoundDspRC,"The callback installation was passed a NULL source processor instance pointer for connection to '%s'.",cmStringNullGuard(dstInstPtr->classPtr->labelStr));
  1106. if( dstInstPtr == NULL )
  1107. return cmErrMsg(&ctx->cmCtx->err,kInstNotFoundDspRC,"The callback installation was passed a NULL destination processor instance pointer for connection from '%s'.",cmStringNullGuard(srcInstPtr->classPtr->labelStr));
  1108. if((srcVarSymId = cmSymTblId( ctx->stH, srcVarLabel )) == cmInvalidId )
  1109. return cmDspInstErr(ctx,srcInstPtr,kSrcVarNotFoundDspRC,"Connection failed. The source variable '%s' could not found in '%s' (id:%i) for registration by '%s' (id:%i).",
  1110. srcVarLabel,srcInstPtr->classPtr->labelStr,srcInstPtr->id,dstInstPtr->classPtr->labelStr,dstInstPtr->id);
  1111. if((dstVarSymId = cmSymTblId( ctx->stH, dstVarLabel )) == cmInvalidId )
  1112. return cmDspInstErr(ctx,dstInstPtr,kSrcVarNotFoundDspRC,"Connection failed. The destination variable '%s' for the DSP instance '%s' (id:%i).",
  1113. dstVarLabel,dstInstPtr->classPtr->labelStr,dstInstPtr->id);
  1114. return cmDspInstInstallCb(ctx, srcInstPtr, srcVarSymId, dstInstPtr, dstVarSymId, dstCbDataPtr);
  1115. }
  1116. cmDspRC_t cmDspInstRemoveCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, unsigned srcVarSymId, cmDspInst_t* dstInstPtr, unsigned dstVarId )
  1117. {
  1118. cmDspVar_t* varPtr;
  1119. if((varPtr = cmDspVarSymbolToPtr(ctx,srcInstPtr,srcVarSymId,kOutDsvFl)) == NULL )
  1120. return cmDspInstErr(ctx,srcInstPtr,kInstCbInstallFailDspRC,"Callback removal failed for instance '%s' (id:%i).",
  1121. srcInstPtr->classPtr->labelStr, srcInstPtr->id );
  1122. cmDspCb_t* cp = varPtr->cbList;
  1123. cmDspCb_t* pp = NULL;
  1124. for(; cp != NULL; cp = cp->linkPtr )
  1125. {
  1126. if( cp->srcVarSymId == srcVarSymId && cp->dstInstPtr == dstInstPtr && cp->dstVarId == dstVarId )
  1127. break;
  1128. pp = cp;
  1129. }
  1130. if( pp == NULL )
  1131. varPtr->cbList = cp->linkPtr;
  1132. else
  1133. pp->linkPtr = cp->linkPtr;
  1134. return kOkDspRC;
  1135. }
  1136. cmDspRC_t cmDspRemoveCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, unsigned dstVarId)
  1137. {
  1138. unsigned srcVarSymId;
  1139. if((srcVarSymId = cmSymTblId( ctx->stH, srcVarLabel )) == cmInvalidId )
  1140. return cmDspInstErr(ctx,srcInstPtr,kSrcVarNotFoundDspRC,"The variable '%s' could not found in '%s' (id:%i) do de-register for '%s' (id:%i).",
  1141. srcVarLabel,srcInstPtr->classPtr->labelStr,srcInstPtr->id,dstInstPtr->classPtr->labelStr,dstInstPtr->id);
  1142. return cmDspInstRemoveCb(ctx, srcInstPtr, srcVarSymId, dstInstPtr, dstVarId );
  1143. }
  1144. cmDspRC_t cmDspInstVarSetFlags( cmDspCtx_t* ctx, cmDspInst_t* instPtr, unsigned varId, unsigned flags )
  1145. {
  1146. cmDspVar_t* varPtr;
  1147. if(_cmDspVarPtr(instPtr, varId, &varPtr ) != kOkDspRC )
  1148. return cmDspInstErr(ctx,instPtr,kInstSetFlagsFailDspRC,"Set flags failed for DSP instance %s (id:%i).",instPtr->classPtr->labelStr,instPtr->id);
  1149. varPtr->flags = cmSetFlag(varPtr->flags,flags);
  1150. return kOkDspRC;
  1151. }