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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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. void _cmDspParseArgV( cmDspVarArg_t* a, va_list vl )
  217. {
  218. a->label = va_arg(vl,const char*);
  219. a->constId = va_arg(vl,unsigned);
  220. a->rn = va_arg(vl,unsigned);
  221. a->cn = va_arg(vl,unsigned);
  222. a->flags = va_arg(vl,unsigned);
  223. a->doc = va_arg(vl,const char*);
  224. }
  225. void* cmDspInstAllocateV(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned instByteCnt, unsigned instSymId, unsigned instId, unsigned storeSymId, unsigned va_cnt, va_list vl0, ... )
  226. {
  227. va_list vl1,vl2;
  228. va_start(vl1,vl0);
  229. va_copy(vl2,vl1);
  230. unsigned argCnt = 0;
  231. int repeatCnt;
  232. while( (repeatCnt = va_arg(vl1,int)) != 0 )
  233. {
  234. cmDspVarArg_t a;
  235. // do some value checking to possibly catch problems with the var_args format
  236. assert( repeatCnt > 0 && repeatCnt < 255 );
  237. argCnt += repeatCnt;
  238. _cmDspParseArgV(&a,vl1);
  239. }
  240. cmDspVarArg_t aa[ argCnt+1 ];
  241. unsigned j=0;
  242. while( (repeatCnt = va_arg(vl2,int)) != 0 )
  243. {
  244. cmDspVarArg_t a;
  245. _cmDspParseArgV(&a,vl2);
  246. cmDspArgSetupN(ctx,aa,argCnt,j,repeatCnt,a.label,a.constId,a.rn, a.cn, a.flags, a.doc );
  247. j += repeatCnt;
  248. }
  249. cmDspArgSetupNull(aa + argCnt);
  250. va_end(vl1);
  251. return cmDspInstAllocate(ctx,classPtr,aa,instByteCnt,instSymId,instId,storeSymId,va_cnt,vl0);
  252. }
  253. cmDspInstSymId_t* _cmDspInstFindAttrSymId( cmDspInst_t* inst, unsigned symId )
  254. {
  255. cmDspInstSymId_t* ip = inst->symIdList;
  256. for(; ip != NULL; ip=ip->link)
  257. if( ip->symId == symId )
  258. return ip;
  259. return NULL;
  260. }
  261. bool cmDspInstHasAttrSym( cmDspInst_t* inst, unsigned attrSymId )
  262. { return _cmDspInstFindAttrSymId(inst,attrSymId) != NULL; }
  263. cmDspRC_t cmDspInstRegisterAttrSym( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned symId )
  264. {
  265. cmDspInstSymId_t* ip;
  266. // if this symbol is already registered
  267. if((ip = _cmDspInstFindAttrSymId(inst,symId)) != NULL )
  268. return cmDspInstErr(ctx,inst,kOkDspRC,"The symbol '%s' is already registered with the instance.",cmStringNullGuard(cmSymTblLabel(ctx->stH,symId)));
  269. // try to find an unused symbol
  270. ip = inst->symIdList;
  271. for(; ip != NULL; ip=ip->link)
  272. if( ip->symId == cmInvalidId )
  273. break;
  274. // if no unused symbols were found then allocate a new one
  275. if( ip == NULL )
  276. {
  277. ip = cmLhAllocZ(ctx->lhH, cmDspInstSymId_t, 1 );
  278. ip->link = inst->symIdList;
  279. inst->symIdList = ip;
  280. }
  281. ip->symId = symId;
  282. return kOkDspRC;
  283. }
  284. cmDspRC_t cmDspInstRemoveAttrSym( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned symId )
  285. {
  286. cmDspInstSymId_t* ip;
  287. // if this symbol is already registered
  288. if((ip = _cmDspInstFindAttrSymId(inst,symId)) == NULL )
  289. return cmDspInstErr(ctx,inst,kOkDspRC,"The symbol '%s' is could not be found to be removed from the instance.",cmStringNullGuard(cmSymTblLabel(ctx->stH,symId)));
  290. // mark a symbol recd as inactive by setting the symbol id to cmInalidId
  291. ip->symId = cmInvalidId;
  292. return kOkDspRC;
  293. }
  294. cmDspRC_t _cmDspClassErrV( cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned instId, cmDspRC_t rc, const cmChar_t* fmt, va_list vl )
  295. {
  296. va_list vl2;
  297. va_copy(vl2,vl);
  298. unsigned n = vsnprintf(NULL,0,fmt,vl2)+1;
  299. cmChar_t buf[n+1];
  300. vsnprintf(buf,n,fmt,vl);
  301. if( instId == cmInvalidId )
  302. cmErrMsg( &classPtr->err, rc, "%s DSP Class:%s",buf, classPtr->labelStr);
  303. else
  304. cmErrMsg( &classPtr->err, rc, "%s DSP Class:%s Inst:%i",buf, classPtr->labelStr,instId);
  305. return rc;
  306. }
  307. cmDspRC_t cmDspClassErr( cmDspCtx_t* ctx, cmDspClass_t* classPtr, cmDspRC_t rc, const cmChar_t* fmt, ... )
  308. {
  309. va_list vl;
  310. va_start(vl,fmt);
  311. rc = _cmDspClassErrV(ctx,classPtr,cmInvalidId,rc,fmt,vl);
  312. va_end(vl);
  313. return rc;
  314. }
  315. cmDspRC_t cmDspInstErr( cmDspCtx_t* ctx, cmDspInst_t* inst, cmDspRC_t rc, const cmChar_t* fmt, ... )
  316. {
  317. va_list vl;
  318. va_start(vl,fmt);
  319. rc = _cmDspClassErrV(ctx,inst->classPtr,inst->id,rc,fmt,vl);
  320. va_end(vl);
  321. return rc;
  322. }
  323. void cmDspArgSetup(
  324. cmDspCtx_t* ctx,
  325. cmDspVarArg_t* arg,
  326. const cmChar_t* labelPrefix,
  327. unsigned labelId,
  328. unsigned constId,
  329. unsigned rn,
  330. unsigned cn,
  331. unsigned flags,
  332. const cmChar_t* docStr )
  333. {
  334. unsigned labelIdCharCnt = (unsigned)floor( labelId==0 ? 1.0 : log10(abs(labelId)) + 1.0 );
  335. int labelCharCnt = strlen(labelPrefix) + 1 + labelIdCharCnt + 1;
  336. cmChar_t label[ labelCharCnt + 1 ];
  337. const cmChar_t* lp;
  338. if( labelId == cmInvalidId )
  339. lp = labelPrefix;
  340. else
  341. {
  342. lp = label;
  343. label[labelCharCnt] = 0;
  344. snprintf(label,labelCharCnt,"%s-%i",labelPrefix,labelId);
  345. }
  346. // use the symbol table to hold the label string
  347. arg->label = cmSymTblLabel(ctx->stH,cmSymTblRegisterSymbol(ctx->stH,lp));
  348. arg->constId = constId;
  349. arg->rn = rn;
  350. arg->cn = cn;
  351. arg->flags = flags;
  352. arg->doc = cmLhAllocStr(ctx->lhH,docStr);;
  353. }
  354. unsigned cmDspArgCopy( cmDspVarArg_t* argArray, unsigned argN, unsigned dstIdx, const cmDspVarArg_t* s, unsigned sn )
  355. {
  356. assert( dstIdx + sn <= argN );
  357. unsigned i;
  358. for(i=0; i<sn; ++i)
  359. argArray[dstIdx+i] = s[i];
  360. return dstIdx + i;
  361. }
  362. unsigned cmDspArgSetupN(
  363. cmDspCtx_t* ctx,
  364. cmDspVarArg_t* arg,
  365. unsigned argN,
  366. unsigned dstIdx,
  367. unsigned cnt,
  368. const cmChar_t* labelPrefix,
  369. unsigned baseConstId,
  370. unsigned rn,
  371. unsigned cn,
  372. unsigned flags,
  373. const cmChar_t* staticDocStr )
  374. {
  375. assert( dstIdx + cnt <= argN );
  376. unsigned i;
  377. for(i=0; i<cnt; ++i)
  378. cmDspArgSetup(ctx,arg + dstIdx + i,labelPrefix, cnt==1 ? cmInvalidId : i, baseConstId + i, rn, cn, flags, staticDocStr );
  379. return dstIdx + i;
  380. }
  381. void cmDspArgSetupNull( cmDspVarArg_t* arg )
  382. { memset(arg,0,sizeof(*arg)); }
  383. const cmChar_t* cmDspInstLabel( cmDspCtx_t* ctx, cmDspInst_t* inst )
  384. {
  385. const char* noInstLbl = "<no_inst_lbl>";
  386. const char* lbl = noInstLbl;
  387. if( inst->symId == cmInvalidId )
  388. return lbl;
  389. if((lbl = cmSymTblLabel(ctx->stH,inst->symId)) == NULL )
  390. lbl = noInstLbl;
  391. return lbl;
  392. }
  393. cmDspRC_t _cmDspVarPtr( const cmDspInst_t* inst, unsigned varId, cmDspVar_t** retVarPtrPtr )
  394. {
  395. if( varId >= inst->varCnt )
  396. {
  397. *retVarPtrPtr = NULL;
  398. return cmErrMsg(&inst->classPtr->err,kVarNotFoundDspRC,"The variable id %i is invalid for DSP instance '%s' (id:%i) .", varId, inst->classPtr->labelStr, inst->id );
  399. }
  400. assert( inst->varArray[varId].constId == varId );
  401. *retVarPtrPtr = inst->varArray + varId;
  402. return kOkDspRC;
  403. }
  404. cmDspRC_t _cmDspVarAndValuePtr( cmDspInst_t* inst, unsigned varId, cmDspVar_t** varPtrPtr, cmDspValue_t** valPtrPtr )
  405. {
  406. cmDspRC_t rc;
  407. if((rc = _cmDspVarPtr(inst,varId,varPtrPtr)) != kOkDspRC )
  408. *valPtrPtr = NULL;
  409. else
  410. *valPtrPtr = cmDsvValuePtr(&(*varPtrPtr)->value);
  411. return rc;
  412. }
  413. cmDspRC_t _cmDspVarValuePtr( cmDspInst_t* inst, unsigned varId, cmDspValue_t** retValPtrPtr )
  414. {
  415. cmDspVar_t* varPtr = NULL;
  416. cmDspRC_t rc;
  417. if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
  418. *retValPtrPtr = NULL;
  419. else
  420. *retValPtrPtr = cmDsvValuePtr(&varPtr->value);
  421. return rc;
  422. }
  423. cmDspRC_t _cmDspVarDefaultPtr( cmDspInst_t* inst, unsigned varId, cmDspValue_t** retValPtrPtr )
  424. {
  425. cmDspVar_t* varPtr = NULL;
  426. cmDspRC_t rc;
  427. if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
  428. *retValPtrPtr = NULL;
  429. else
  430. *retValPtrPtr = &varPtr->dflt;
  431. return rc;
  432. }
  433. void _cmDspSendEvt( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, unsigned srcVarId, const cmDspVar_t* varPtr )
  434. {
  435. cmDspEvt_t e;
  436. cmDspCb_t* cbp = varPtr->cbList;
  437. if( cbp == NULL )
  438. return;
  439. e.flags = kDfltEvtDspFlags;
  440. e.srcInstPtr = srcInstPtr;
  441. e.srcVarId = srcVarId;
  442. e.valuePtr = &varPtr->value;
  443. for(; cbp!=NULL; cbp=cbp->linkPtr)
  444. {
  445. e.dstVarId = cbp->dstVarId;
  446. e.dstDataPtr = cbp->dstDataPtr;
  447. if( cbp->dstInstPtr->recvFunc!=NULL)
  448. cbp->dstInstPtr->recvFunc(ctx,cbp->dstInstPtr,&e);
  449. }
  450. }
  451. cmDspVar_t* cmDspVarSymbolToPtr( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varSymId, unsigned flags )
  452. {
  453. unsigned i;
  454. for(i=0; i<inst->varCnt; ++i)
  455. if( inst->varArray[i].symId == varSymId )
  456. {
  457. if( flags==0 || cmAllFlags(inst->varArray[i].flags,flags) )
  458. return inst->varArray + i;
  459. }
  460. cmDspInstErr(ctx,inst,kVarNotFoundDspRC,"The variable '%s' could not be found in the instance '%s' (id:%i).",
  461. cmSymTblLabel( ctx->stH, varSymId), inst->classPtr->labelStr, inst->id );
  462. return NULL;
  463. }
  464. const cmDspVar_t* cmDspVarIdToCPtr( const cmDspInst_t* inst, unsigned varId )
  465. {
  466. cmDspVar_t* varPtr;
  467. if( _cmDspVarPtr(inst, varId, &varPtr) != kOkDspRC )
  468. return NULL;
  469. return varPtr;
  470. }
  471. cmDspVar_t* cmDspVarIdToPtr( cmDspInst_t* inst, unsigned varId )
  472. {
  473. cmDspVar_t* varPtr;
  474. if( _cmDspVarPtr(inst, varId, &varPtr) != kOkDspRC )
  475. return NULL;
  476. return varPtr;
  477. }
  478. const cmChar_t* cmDspVarLabel( cmDspCtx_t* ctx, const cmDspInst_t* inst, unsigned varId )
  479. {
  480. const cmDspVar_t* varPtr;
  481. if(( varPtr = cmDspVarIdToCPtr(inst,varId) ) == NULL )
  482. return NULL;
  483. return cmSymTblLabel( ctx->stH, varPtr->symId);
  484. }
  485. cmDspRC_t cmDspOutputEvent( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  486. {
  487. cmDspRC_t rc;
  488. cmDspVar_t* varPtr;
  489. if((rc = _cmDspVarPtr(inst, varId, &varPtr )) != kOkDspRC )
  490. return rc;
  491. _cmDspSendEvt(ctx, inst, varId, varPtr );
  492. return rc;
  493. }
  494. cmDspRC_t cmDspVarPresetWrite( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  495. {
  496. cmDspRC_t rc;
  497. const cmDspVar_t* varPtr;
  498. if((varPtr = cmDspVarIdToCPtr(inst,varId)) == NULL )
  499. return cmDspInstErr(ctx,inst,kVarNotFoundDspRC,"Var with id %i not found during preset store.");
  500. if((rc = cmDspSysPresetWriteValue(ctx->dspH, varPtr->symId, &varPtr->value )) != kOkDspRC )
  501. return cmDspInstErr(ctx,inst,kSubSysFailDspRC,"Var preset storage failed on var:'%s'.",cmStringNullGuard(cmSymTblLabel(ctx->stH,varPtr->symId)));
  502. return rc;
  503. }
  504. cmDspRC_t cmDspVarPresetRead( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  505. {
  506. cmDspRC_t rc;
  507. cmDspVar_t* varPtr;
  508. cmDspValue_t value;
  509. if((varPtr = cmDspVarIdToPtr(inst,varId)) == NULL )
  510. return cmDspInstErr(ctx,inst,kVarNotFoundDspRC,"Var with id %i not found during preset recall.");
  511. if((rc = cmDspSysPresetReadValue(ctx->dspH, varPtr->symId, &value )) != kOkDspRC )
  512. return cmDspInstErr(ctx,inst,kSubSysFailDspRC,"Var preset recall failed on var:'%s'.",cmStringNullGuard(cmSymTblLabel(ctx->stH,varPtr->symId)));
  513. return cmDspValueSet(ctx,inst,varId,&value,0);
  514. }
  515. cmDspRC_t cmDspVarPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, bool storeFl )
  516. {
  517. cmDspRC_t rc;
  518. if( storeFl )
  519. rc = cmDspVarPresetWrite(ctx,inst,varId);
  520. else
  521. rc = cmDspVarPresetRead(ctx,inst,varId);
  522. return rc;
  523. }
  524. double cmDspSampleRate( cmDspCtx_t* ctx )
  525. { return ctx->ctx->ss->args.srate; }
  526. unsigned cmDspSamplesPerCycle( cmDspCtx_t * ctx )
  527. { return ctx->ctx->ss->args.dspFramesPerCycle; }
  528. unsigned cmDspTypeFlags(const cmDspInst_t* inst, unsigned varId )
  529. {
  530. cmDspVar_t* varPtr;
  531. unsigned flags;
  532. if( _cmDspVarPtr(inst,varId,&varPtr) != kOkDspRC )
  533. return 0;
  534. cmDspValue_t* valPtr = cmDsvValuePtr(&varPtr->value);
  535. if( valPtr->flags != 0 )
  536. flags = valPtr->flags;
  537. else
  538. flags = varPtr->flags;
  539. return flags & kTypeDsvMask;
  540. }
  541. bool cmDspIsBool( const cmDspInst_t* inst, unsigned varId )
  542. { return cmIsFlag(cmDspTypeFlags(inst,varId),kBoolDsvFl); }
  543. bool cmDspIsInt( const cmDspInst_t* inst, unsigned varId )
  544. { return cmIsFlag(cmDspTypeFlags(inst,varId),kIntDsvFl); }
  545. bool cmDspIsUInt( const cmDspInst_t* inst, unsigned varId )
  546. { return cmIsFlag(cmDspTypeFlags(inst,varId),kUIntDsvFl); }
  547. bool cmDspIsDouble( const cmDspInst_t* inst, unsigned varId )
  548. { return cmIsFlag(cmDspTypeFlags(inst,varId),kDoubleDsvFl); }
  549. bool cmDspIsStrz( const cmDspInst_t* inst, unsigned varId )
  550. { return cmIsFlag(cmDspTypeFlags(inst,varId),kStrzDsvFl); }
  551. bool cmDspIsSymbol( const cmDspInst_t* inst, unsigned varId )
  552. { return cmIsFlag(cmDspTypeFlags(inst,varId),kSymDsvFl); }
  553. bool cmDspIsJson( const cmDspInst_t* inst, unsigned varId )
  554. { return cmIsFlag(cmDspTypeFlags(inst,varId),kJsonDsvFl); }
  555. bool cmDspBool( cmDspInst_t* inst, unsigned varId )
  556. {
  557. cmDspValue_t* vp;
  558. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  559. return 0;
  560. return cmDsvGetBool(vp);
  561. }
  562. int cmDspInt( cmDspInst_t* inst, unsigned varId )
  563. {
  564. cmDspValue_t* vp;
  565. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  566. return 0;
  567. return cmDsvGetInt(vp);
  568. }
  569. unsigned cmDspUInt( cmDspInst_t* inst, unsigned varId )
  570. {
  571. cmDspValue_t* vp;
  572. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  573. return 0;
  574. return cmDsvGetUInt(vp);
  575. }
  576. double cmDspDouble( cmDspInst_t* inst, unsigned varId )
  577. {
  578. cmDspValue_t* vp;
  579. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  580. return 0;
  581. return cmDsvGetDouble(vp);
  582. }
  583. cmSample_t cmDspSample( cmDspInst_t* inst, unsigned varId )
  584. {
  585. cmDspValue_t* vp;
  586. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  587. return 0;
  588. return cmDsvGetSample(vp);
  589. }
  590. cmReal_t cmDspReal( cmDspInst_t* inst, unsigned varId )
  591. {
  592. cmDspValue_t* vp;
  593. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  594. return 0;
  595. return cmDsvGetReal(vp);
  596. }
  597. const cmChar_t* cmDspStrcz( cmDspInst_t* inst, unsigned varId )
  598. {
  599. cmDspValue_t* vp;
  600. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  601. return NULL;
  602. return cmDsvStrcz(vp);
  603. }
  604. unsigned cmDspSymbol( cmDspInst_t* inst, unsigned varId )
  605. {
  606. cmDspValue_t* vp;
  607. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  608. return cmInvalidId;
  609. return cmDsvSymbol(vp);
  610. }
  611. cmJsonNode_t* cmDspJson( cmDspInst_t* inst, unsigned varId )
  612. {
  613. cmDspValue_t* vp;
  614. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  615. return NULL;
  616. return cmDsvJson(vp);
  617. }
  618. bool cmDspDefaultBool( cmDspInst_t* inst, unsigned varId )
  619. {
  620. cmDspValue_t* vp;
  621. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  622. return 0;
  623. return cmDsvGetBool(vp);
  624. }
  625. int cmDspDefaultInt( cmDspInst_t* inst, unsigned varId )
  626. {
  627. cmDspValue_t* vp;
  628. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  629. return 0;
  630. return cmDsvGetInt(vp);
  631. }
  632. unsigned cmDspDefaultUInt( cmDspInst_t* inst, unsigned varId )
  633. {
  634. cmDspValue_t* vp;
  635. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  636. return 0;
  637. return cmDsvGetUInt(vp);
  638. }
  639. double cmDspDefaultDouble( cmDspInst_t* inst, unsigned varId )
  640. {
  641. cmDspValue_t* vp;
  642. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  643. return 0;
  644. return cmDsvGetDouble(vp);
  645. }
  646. cmSample_t cmDspDefaultSample( cmDspInst_t* inst, unsigned varId )
  647. {
  648. cmDspValue_t* vp;
  649. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  650. return 0;
  651. return cmDsvGetSample(vp);
  652. }
  653. cmReal_t cmDspDefaultReal( cmDspInst_t* inst, unsigned varId )
  654. {
  655. cmDspValue_t* vp;
  656. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  657. return 0;
  658. return cmDsvGetReal(vp);
  659. }
  660. const cmChar_t* cmDspDefaultStrcz( cmDspInst_t* inst, unsigned varId )
  661. {
  662. cmDspValue_t* vp;
  663. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  664. return NULL;
  665. return cmDsvGetStrcz(vp);
  666. }
  667. unsigned cmDspDefaultSymbol( cmDspInst_t* inst, unsigned varId )
  668. {
  669. cmDspValue_t* vp;
  670. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  671. return cmInvalidId;
  672. return cmDsvGetSymbol(vp);
  673. }
  674. cmJsonNode_t* cmDspDefaultJson( cmDspInst_t* inst, unsigned varId )
  675. {
  676. cmDspValue_t* vp;
  677. if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
  678. return NULL;
  679. return cmDsvGetJson(vp);
  680. }
  681. cmDspRC_t cmDspValueSet( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmDspValue_t* svp, unsigned flags )
  682. {
  683. cmDspRC_t rc;
  684. cmDspVar_t* varPtr;
  685. // get a pointer to the var recd
  686. if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
  687. return rc;
  688. // get a pointer to the target value.
  689. cmDspValue_t* dvp = cmIsFlag(flags,kSetDefaultDspFl) ? &varPtr->dflt : cmDsvValuePtr(&varPtr->value);
  690. // store a pointer to the dst string memory in case we need to delete it later
  691. cmChar_t* rlsPtr = cmAllFlags(dvp->flags,kStrzDsvFl | kDynDsvFl) ? dvp->u.z : NULL;
  692. // set the dest type to the type shared with the source
  693. // ( this assumes that the dst may have multiple types but the source has one type)
  694. unsigned typeFlags = (varPtr->flags & svp->flags) & kTypeDsvMask;
  695. // if svp->flags is set to kNullDsvFl then use just the dst type
  696. if( typeFlags == 0 )
  697. typeFlags = varPtr->flags & kTypeDsvMask;
  698. // convert the source to the var type
  699. switch(typeFlags)
  700. {
  701. case kBoolDsvFl:
  702. cmDsvSetBool( dvp, cmDsvGetBool(svp));
  703. break;
  704. case kIntDsvFl:
  705. cmDsvSetInt( dvp, cmDsvGetInt(svp));
  706. break;
  707. case kUIntDsvFl:
  708. cmDsvSetUInt( dvp, cmDsvGetUInt(svp));
  709. break;
  710. case kDoubleDsvFl:
  711. cmDsvSetDouble(dvp, cmDsvGetDouble(svp));
  712. break;
  713. case kSampleDsvFl:
  714. cmDsvSetSample(dvp, cmDsvGetSample(svp));
  715. break;
  716. case kRealDsvFl:
  717. cmDsvSetReal(dvp, cmDsvGetReal(svp));
  718. break;
  719. case kSymDsvFl:
  720. cmDsvSetSymbol(dvp, cmDsvGetSymbol(svp));
  721. break;
  722. case kJsonDsvFl:
  723. cmDsvSetJson( dvp, cmDsvJson(svp));
  724. break;
  725. case kStrzDsvFl:
  726. {
  727. const cmChar_t* sp;
  728. if( cmDsvIsSymbol(svp) )
  729. sp = cmSymTblLabel(ctx->stH,cmDsvSymbol(svp));
  730. else
  731. sp = cmDsvGetStrcz(svp);
  732. // don't copy over myself
  733. if( sp == dvp->u.z )
  734. rlsPtr = NULL;
  735. else
  736. {
  737. // if the source == NULL then set the dst to NULL
  738. // (NULL should always be a legitimate value)
  739. if( sp == NULL )
  740. cmDsvSetStrcz(dvp,sp);
  741. else
  742. {
  743. // if the source should not be copied into internal memory
  744. if( cmIsFlag(flags,kNoAllocDspFl) )
  745. cmDsvSetStrcz(dvp,sp);
  746. else
  747. {
  748. // allocate memory to hold the new string
  749. unsigned n = strlen(sp)+1;
  750. cmChar_t* dp = cmLhResizeN(ctx->lhH,cmChar_t,rlsPtr,n);
  751. strncpy(dp,sp,n);
  752. cmDsvSetStrz(dvp, dp);
  753. dvp->flags = cmSetFlag(dvp->flags,kDynDsvFl);
  754. rlsPtr = NULL;
  755. }
  756. }
  757. }
  758. }
  759. break;
  760. default:
  761. { assert(0); }
  762. }
  763. // if the dst contained a dynamically alloc'd string prior to being
  764. // set with a new value - then release the memory used by the original
  765. // string here
  766. if( rlsPtr != NULL )
  767. cmLHeapFree(ctx->lhH,rlsPtr);
  768. // notify listeners of the change of value
  769. if( cmIsFlag(flags,kNoSendDspFl) == false )
  770. _cmDspSendEvt( ctx, inst, varId, varPtr );
  771. // notify the UI of the change of value
  772. if( cmIsFlag(varPtr->flags,kUiDsvFl) && cmIsFlag(flags,kNoUpdateUiDspFl)==false )
  773. {
  774. cmDspUiSendVar( ctx, inst, varPtr );
  775. }
  776. return rc;
  777. }
  778. cmDspRC_t cmDspApplyDefault( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  779. {
  780. cmDspRC_t rc;
  781. cmDspVar_t* varPtr;
  782. // get a pointer to the var recd
  783. if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
  784. return rc;
  785. if( cmIsFlag(varPtr->flags,kDfltSetDsvFl) )
  786. {
  787. // unless the var has kSendDfltDsvFl set we don't transmit the default values
  788. unsigned flags = cmIsFlag(varPtr->flags,kSendDfltDsvFl) ? 0 : kNoSendDspFl;
  789. return cmDspValueSet(ctx, inst, varId, &varPtr->dflt, flags );
  790. }
  791. return kOkDspRC;
  792. }
  793. cmDspRC_t cmDspApplyAllDefaults( cmDspCtx_t* ctx, cmDspInst_t* inst )
  794. {
  795. cmDspRC_t rc = kOkDspRC;
  796. unsigned i;
  797. for(i=0; i<inst->varCnt; ++i)
  798. if((rc = cmDspApplyDefault(ctx,inst,inst->varArray[i].constId)) != kOkDspRC )
  799. return rc;
  800. return rc;
  801. }
  802. cmDspRC_t cmDspSetDefault( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmDspValue_t* valPtr )
  803. {
  804. cmDspRC_t rc;
  805. cmDspVar_t* varPtr;
  806. // get a pointer to the var recd
  807. if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
  808. return rc;
  809. if(( cmDspValueSet( ctx, inst, varId, valPtr, kSetDefaultDspFl )) == kOkDspRC )
  810. {
  811. varPtr->flags = cmSetFlag(varPtr->flags, kDfltSetDsvFl);
  812. }
  813. return rc;
  814. }
  815. cmDspRC_t _cmDspDefaultSet( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmDspValue_t* valPtr, bool nonInitValIsEqualFl )
  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 the dflt value was previuosly set and the default value is
  823. // not equal to the non-intialized value for this var then the default value
  824. // has already been set to a legal value
  825. if( cmIsFlag(varPtr->flags,kDfltSetDsvFl) && nonInitValIsEqualFl==false )
  826. return kOkDspRC;
  827. return cmDspSetDefault(ctx,inst,varId,valPtr);
  828. }
  829. cmDspRC_t cmDspSetDefaultBool( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, bool nonInitVal, bool val )
  830. {
  831. cmDspValue_t v;
  832. double dv = cmDspDefaultBool(inst,varId);
  833. cmDsvSetBool(&v,val);
  834. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  835. }
  836. cmDspRC_t cmDspSetDefaultInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, int nonInitVal, int val )
  837. {
  838. cmDspValue_t v;
  839. int dv = cmDspDefaultInt(inst,varId);
  840. cmDsvSetInt(&v,val);
  841. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  842. }
  843. cmDspRC_t cmDspSetDefaultUInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned nonInitVal, unsigned val )
  844. {
  845. cmDspValue_t v;
  846. unsigned dv = cmDspDefaultUInt(inst,varId);
  847. cmDsvSetUInt(&v,val);
  848. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  849. }
  850. cmDspRC_t cmDspSetDefaultDouble( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, double nonInitVal, double val )
  851. {
  852. cmDspValue_t v;
  853. double dv = cmDspDefaultDouble(inst,varId);
  854. cmDsvSetDouble(&v,val);
  855. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  856. }
  857. cmDspRC_t cmDspSetDefaultSample( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmSample_t nonInitVal, cmSample_t val )
  858. {
  859. cmDspValue_t v;
  860. cmSample_t dv = cmDspDefaultSample(inst,varId);
  861. cmDsvSetSample(&v,val);
  862. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  863. }
  864. cmDspRC_t cmDspSetDefaultReal( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmReal_t nonInitVal, cmReal_t val )
  865. {
  866. cmDspValue_t v;
  867. cmReal_t dv = cmDspDefaultReal(inst,varId);
  868. cmDsvSetReal(&v,val);
  869. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  870. }
  871. cmDspRC_t cmDspSetDefaultSymbol( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned val )
  872. {
  873. cmDspValue_t v;
  874. unsigned dv = cmDspDefaultSymbol(inst,varId);
  875. cmDsvSetSymbol(&v,val);
  876. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==cmInvalidId);
  877. }
  878. cmDspRC_t cmDspSetDefaultStrcz( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmChar_t* nonInitVal, const cmChar_t* val )
  879. {
  880. cmDspValue_t v;
  881. const cmChar_t* dv= cmDspDefaultStrcz(inst,varId);
  882. cmDsvSetStrcz(&v,val);
  883. bool fl = false; // assume the default and non-init value are not equal
  884. // if the pointers are the same then the strings must match
  885. if( dv == nonInitVal )
  886. fl = true;
  887. else
  888. // if both strings exist - then test if they are equal
  889. if( dv!=NULL && nonInitVal!=NULL)
  890. fl = strcmp(dv,nonInitVal)==0;
  891. return _cmDspDefaultSet(ctx,inst,varId,&v,fl);
  892. }
  893. cmDspRC_t cmDspSetDefaultJson( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmJsonNode_t* nonInitVal, cmJsonNode_t* val )
  894. {
  895. cmDspValue_t v;
  896. cmJsonNode_t* dv= cmDspDefaultJson(inst,varId);
  897. cmDsvSetJson(&v,val);
  898. return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
  899. }
  900. cmDspRC_t cmDspSetBool( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, bool val )
  901. {
  902. cmDspValue_t v;
  903. cmDsvSetBool(&v,val);
  904. return cmDspValueSet(ctx, inst, varId, &v, 0);
  905. }
  906. cmDspRC_t cmDspSetInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, int val )
  907. {
  908. cmDspValue_t v;
  909. cmDsvSetInt(&v,val);
  910. return cmDspValueSet(ctx, inst, varId, &v, 0);
  911. }
  912. cmDspRC_t cmDspSetUInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned val )
  913. {
  914. cmDspValue_t v;
  915. cmDsvSetUInt(&v,val);
  916. return cmDspValueSet(ctx, inst, varId, &v, 0);
  917. }
  918. cmDspRC_t cmDspSetDouble( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, double val )
  919. {
  920. cmDspValue_t v;
  921. cmDsvSetDouble(&v,val);
  922. return cmDspValueSet(ctx, inst, varId, &v, 0);
  923. }
  924. cmDspRC_t cmDspSetSample( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmSample_t val )
  925. {
  926. cmDspValue_t v;
  927. cmDsvSetSample(&v,val);
  928. return cmDspValueSet(ctx, inst, varId, &v, 0);
  929. }
  930. cmDspRC_t cmDspSetReal( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmReal_t val )
  931. {
  932. cmDspValue_t v;
  933. cmDsvSetReal(&v,val);
  934. return cmDspValueSet(ctx, inst, varId, &v, 0);
  935. }
  936. cmDspRC_t cmDspSetStrcz( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmChar_t* val )
  937. {
  938. cmDspValue_t v;
  939. cmDsvSetStrcz(&v,val);
  940. return cmDspValueSet(ctx, inst, varId, &v, 0);
  941. }
  942. cmDspRC_t cmDspSetSymbol( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned val )
  943. {
  944. cmDspValue_t v;
  945. cmDsvSetSymbol(&v,val);
  946. return cmDspValueSet(ctx, inst, varId, &v, 0);
  947. }
  948. cmDspRC_t cmDspSetJson( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmJsonNode_t* val )
  949. {
  950. cmDspValue_t v;
  951. cmDsvSetJson(&v,val);
  952. return cmDspValueSet(ctx, inst, varId, &v, 0);
  953. }
  954. cmDspRC_t cmDspSetEvent( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  955. {
  956. unsigned flags = kUpdateUiDspFl;
  957. // We will assume that all values should be reflected to the UI - unless
  958. // this event is known to have been generated by the UI (evt->kUiDspFl is set)
  959. // and evt->kUiEchoDspFl is not set.
  960. if( cmIsFlag(evt->flags,kUiDspFl)==true && cmIsFlag(evt->flags,kUiEchoDspFl)==false )
  961. flags = kNoUpdateUiDspFl;
  962. // Note: If the event target variable is not a UI variable
  963. // (i.e. cmDspVar_t.kUiDsvFl is not set) then calling cmDspValueSet()
  964. // with the kUpdateUiDspFl set has no effect.
  965. return cmDspValueSet(ctx,inst,evt->dstVarId, evt->valuePtr, flags);
  966. }
  967. cmDspRC_t cmDspSetEventUi( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
  968. { return cmDspValueSet(ctx,inst,evt->dstVarId, evt->valuePtr, kUpdateUiDspFl); }
  969. cmDspRC_t cmDspSetEventUiId( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt, unsigned varId )
  970. {
  971. cmDspEvt_t e;
  972. cmDspEvtCopy(&e,evt);
  973. e.dstVarId = varId;
  974. evt = &e;
  975. return cmDspSetEventUi(ctx,inst,evt);
  976. }
  977. unsigned cmDspVarRows( cmDspInst_t* inst, unsigned varId )
  978. {
  979. cmDspValue_t* vp;
  980. if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  981. return 0;
  982. return cmDsvRows(vp);
  983. }
  984. unsigned cmDspVarCols( cmDspInst_t* inst, unsigned varId )
  985. {
  986. cmDspValue_t* vp;
  987. if( _cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
  988. return 0;
  989. return cmDsvCols(vp);
  990. }
  991. bool cmDspIsAudioInputConnected( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  992. {
  993. cmDspValue_t* vp;
  994. if( _cmDspVarValuePtr(inst,varId,&vp ) != kOkDspRC )
  995. return false;
  996. if( cmAllFlags(vp->flags, kSampleDsvFl | kMtxDsvFl ))
  997. return true;
  998. return false;
  999. }
  1000. cmDspRC_t cmDspZeroAudioBuf( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
  1001. {
  1002. cmDspValue_t* valPtr;
  1003. cmDspVar_t* varPtr;
  1004. cmDspRC_t rc;
  1005. if((rc = _cmDspVarAndValuePtr(inst,varId,&varPtr,&valPtr)) != kOkDspRC )
  1006. return rc;
  1007. bool fl0 = cmAllFlags(valPtr->flags, kSampleDsvFl | kMtxDsvFl );
  1008. bool fl1 = cmIsFlag(varPtr->flags,kOutDsvFl);
  1009. if( fl0==false || fl1==false )
  1010. 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);
  1011. memset(cmDsvSampleMtx(valPtr), 0, cmDsvEleCount(valPtr)*sizeof(cmSample_t) );
  1012. return rc;
  1013. }
  1014. cmSample_t* cmDspAudioBuf( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned chIdx )
  1015. {
  1016. cmDspValue_t* vp;
  1017. cmDspVar_t* varPtr;
  1018. cmDspRC_t rc;
  1019. if((rc = _cmDspVarAndValuePtr(inst,varId,&varPtr,&vp )) != kOkDspRC )
  1020. return NULL;
  1021. if( !cmAllFlags(vp->flags, kSampleDsvFl | kMtxDsvFl ))
  1022. {
  1023. // this is an unconnected audio input
  1024. if( cmAllFlags(varPtr->flags,kInDsvFl | kAudioBufDsvFl ) )
  1025. return NULL;
  1026. 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);
  1027. return NULL;
  1028. }
  1029. if( chIdx >= cmDsvCols(vp) )
  1030. {
  1031. cmDspInstErr(ctx,inst,kVarTypeErrDspRC,"Channel index %i of audio buffer variable '%s' in DSP instance '%s' (id:%i) is outside the channel count (%i).",
  1032. chIdx,cmDspVarLabel(ctx,inst,varId),inst->classPtr->labelStr,inst->id,cmDsvCols(vp));
  1033. }
  1034. return cmDsvSampleMtx(vp) + (chIdx * cmDsvRows(vp));
  1035. }
  1036. unsigned cmDspAudioBufSmpCount( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned chIdx )
  1037. {
  1038. cmDspValue_t* vp;
  1039. cmDspRC_t rc;
  1040. if((rc = _cmDspVarValuePtr(inst,varId,&vp)) != kOkDspRC )
  1041. return 0;
  1042. if( !cmAllFlags(vp->flags, kSampleDsvFl | kMtxDsvFl ))
  1043. {
  1044. 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);
  1045. return 0;
  1046. }
  1047. return cmDsvRows(vp);
  1048. }
  1049. cmDspRC_t cmDspInstInstallCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, unsigned srcVarSymId, cmDspInst_t* dstInstPtr, unsigned dstVarSymId, void* dstDataPtr)
  1050. {
  1051. cmDspVar_t* srcVarPtr;
  1052. cmDspVar_t* dstVarPtr;
  1053. // get the src and dst var ptrs
  1054. if((srcVarPtr = cmDspVarSymbolToPtr(ctx,srcInstPtr,srcVarSymId,kOutDsvFl)) == NULL || (dstVarPtr = cmDspVarSymbolToPtr(ctx,dstInstPtr,dstVarSymId,kInDsvFl)) == NULL )
  1055. return cmDspInstErr(ctx,srcInstPtr,kInstCbInstallFailDspRC,"Callback installation failed for connection from '%s' (id:%i) var:'%s' to '%s' (id:%i) var:'%s'.",
  1056. srcInstPtr->classPtr->labelStr, srcInstPtr->id, cmSymTblLabel(ctx->stH,srcVarSymId), dstInstPtr->classPtr->labelStr, dstInstPtr->id, cmSymTblLabel(ctx->stH,dstVarSymId) );
  1057. // check for strict type conversion - this will fail for vars which allow multiple types
  1058. // (e.g. numbers and strings)
  1059. if( cmDsvCanConvertFlags(dstVarPtr->value.flags,srcVarPtr->value.flags) == false )
  1060. {
  1061. // if all the flags in the src are set in the dst then the match is ok
  1062. // (this will cover dst types which admit multiple types)
  1063. if( cmAllFlags(dstVarPtr->flags & kTypeDsvMask,srcVarPtr->flags & kTypeDsvMask) == false )
  1064. 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));
  1065. }
  1066. if( cmIsFlag(srcVarPtr->flags, kAudioBufDsvFl) )
  1067. cmDspInstErr(ctx,srcInstPtr,kOkDspRC,"An audio source is being used as an event callback connection.");
  1068. if( cmIsFlag(dstVarPtr->flags, kAudioBufDsvFl ) )
  1069. cmDspInstErr(ctx,dstInstPtr,kOkDspRC,"An audio destination is the target of a callback connection.");
  1070. cmDspCb_t* r = cmLhAllocZ( ctx->lhH, cmDspCb_t, 1 );
  1071. r->srcVarSymId = srcVarSymId;
  1072. r->dstInstPtr = dstInstPtr;
  1073. r->dstVarId = dstVarPtr->constId;
  1074. r->dstDataPtr = dstDataPtr;
  1075. r->linkPtr = NULL;
  1076. cmDspCb_t* cp = srcVarPtr->cbList;
  1077. cmDspCb_t* pp = NULL;
  1078. for(; cp != NULL; cp = cp->linkPtr )
  1079. pp = cp;
  1080. if( pp == NULL )
  1081. srcVarPtr->cbList = r;
  1082. else
  1083. pp->linkPtr = r;
  1084. return kOkDspRC;
  1085. }
  1086. cmDspRC_t cmDspInstallCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, void* dstCbDataPtr)
  1087. {
  1088. unsigned srcVarSymId,dstVarSymId;
  1089. if( srcInstPtr == NULL && dstInstPtr == NULL )
  1090. return cmErrMsg(&ctx->cmCtx->err,kInstNotFoundDspRC,"The callback installation was passed a NULL source and destination processor instance pointer.");
  1091. if( srcInstPtr == NULL )
  1092. 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));
  1093. if( dstInstPtr == NULL )
  1094. 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));
  1095. if((srcVarSymId = cmSymTblId( ctx->stH, srcVarLabel )) == cmInvalidId )
  1096. return cmDspInstErr(ctx,srcInstPtr,kSrcVarNotFoundDspRC,"Connection failed. The source variable '%s' could not found in '%s' (id:%i) for registration by '%s' (id:%i).",
  1097. srcVarLabel,srcInstPtr->classPtr->labelStr,srcInstPtr->id,dstInstPtr->classPtr->labelStr,dstInstPtr->id);
  1098. if((dstVarSymId = cmSymTblId( ctx->stH, dstVarLabel )) == cmInvalidId )
  1099. return cmDspInstErr(ctx,dstInstPtr,kSrcVarNotFoundDspRC,"Connection failed. The destination variable '%s' for the DSP instance '%s' (id:%i).",
  1100. dstVarLabel,dstInstPtr->classPtr->labelStr,dstInstPtr->id);
  1101. return cmDspInstInstallCb(ctx, srcInstPtr, srcVarSymId, dstInstPtr, dstVarSymId, dstCbDataPtr);
  1102. }
  1103. cmDspRC_t cmDspInstRemoveCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, unsigned srcVarSymId, cmDspInst_t* dstInstPtr, unsigned dstVarId )
  1104. {
  1105. cmDspVar_t* varPtr;
  1106. if((varPtr = cmDspVarSymbolToPtr(ctx,srcInstPtr,srcVarSymId,kOutDsvFl)) == NULL )
  1107. return cmDspInstErr(ctx,srcInstPtr,kInstCbInstallFailDspRC,"Callback removal failed for instance '%s' (id:%i).",
  1108. srcInstPtr->classPtr->labelStr, srcInstPtr->id );
  1109. cmDspCb_t* cp = varPtr->cbList;
  1110. cmDspCb_t* pp = NULL;
  1111. for(; cp != NULL; cp = cp->linkPtr )
  1112. {
  1113. if( cp->srcVarSymId == srcVarSymId && cp->dstInstPtr == dstInstPtr && cp->dstVarId == dstVarId )
  1114. break;
  1115. pp = cp;
  1116. }
  1117. if( pp == NULL )
  1118. varPtr->cbList = cp->linkPtr;
  1119. else
  1120. pp->linkPtr = cp->linkPtr;
  1121. return kOkDspRC;
  1122. }
  1123. cmDspRC_t cmDspRemoveCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, unsigned dstVarId)
  1124. {
  1125. unsigned srcVarSymId;
  1126. if((srcVarSymId = cmSymTblId( ctx->stH, srcVarLabel )) == cmInvalidId )
  1127. return cmDspInstErr(ctx,srcInstPtr,kSrcVarNotFoundDspRC,"The variable '%s' could not found in '%s' (id:%i) do de-register for '%s' (id:%i).",
  1128. srcVarLabel,srcInstPtr->classPtr->labelStr,srcInstPtr->id,dstInstPtr->classPtr->labelStr,dstInstPtr->id);
  1129. return cmDspInstRemoveCb(ctx, srcInstPtr, srcVarSymId, dstInstPtr, dstVarId );
  1130. }
  1131. cmDspRC_t cmDspInstVarSetFlags( cmDspCtx_t* ctx, cmDspInst_t* instPtr, unsigned varId, unsigned flags )
  1132. {
  1133. cmDspVar_t* varPtr;
  1134. if(_cmDspVarPtr(instPtr, varId, &varPtr ) != kOkDspRC )
  1135. return cmDspInstErr(ctx,instPtr,kInstSetFlagsFailDspRC,"Set flags failed for DSP instance %s (id:%i).",instPtr->classPtr->labelStr,instPtr->id);
  1136. varPtr->flags = cmSetFlag(varPtr->flags,flags);
  1137. return kOkDspRC;
  1138. }