libcm is a C development framework with an emphasis on audio signal processing applications.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cmDspClass.c 43KB

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