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.

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