12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457 |
- #include "cmPrefix.h"
- #include "cmGlobal.h"
- #include "cmFloatTypes.h"
- #include "cmRpt.h"
- #include "cmErr.h"
- #include "cmCtx.h"
- #include "cmMem.h"
- #include "cmMallocDebug.h"
- #include "cmLinkedHeap.h"
- #include "cmSymTbl.h"
- #include "cmPrefs.h"
- #include "cmJson.h"
- #include "cmDspValue.h"
- #include "cmMsgProtocol.h"
- #include "cmThread.h"
- #include "cmUdpPort.h"
- #include "cmUdpNet.h"
- #include "cmAudioSys.h"
- #include "cmProcObj.h"
- #include "cmDspCtx.h"
- #include "cmDspClass.h"
- #include "cmDspSys.h"
-
- cmDspRC_t cmDspClassSetup(
- cmDspClass_t* classPtr,
- cmDspCtx_t* ctx,
- const cmChar_t* classLabel,
- cmDspClassFunc_t finalClassFunc,
- cmDspConsFunc_t allocFunc,
- cmDspFunc_t freeFunc,
- cmDspFunc_t resetFunc,
- cmDspFunc_t execFunc,
- cmDspFunc_t recvFunc,
- cmDspStoreFunc_t storeFunc,
- cmDspAttrSymFunc_t sysRecvFunc,
- const cmChar_t* doc )
- {
- classPtr->labelStr = classLabel;
- classPtr->doc = doc;
-
- classPtr->finalClassFunc = finalClassFunc;
- classPtr->allocFunc = allocFunc;
- classPtr->freeFunc = freeFunc;
- classPtr->resetFunc = resetFunc;
- classPtr->execFunc = execFunc;
- classPtr->recvFunc = recvFunc;
- classPtr->storeFunc = storeFunc;
- classPtr->sysRecvFunc = sysRecvFunc;
- cmErrSetup( &classPtr->err, ctx->rpt, classLabel);
-
- return kOkDspRC;
- }
-
- // default DSP instance system recv function
- cmDspRC_t _cmDspInstSysRecvFunc( cmDspCtx_t* ctx, struct cmDspInst_str* inst, unsigned attrSymId, const cmDspValue_t* value )
- {
- if( cmDsvIsSymbol(value) )
- {
- unsigned msgSymId = cmDsvSymbol(value);
-
- if( msgSymId == ctx->_disableSymId )
- {
- inst->flags = cmSetFlag(inst->flags,kDisableExecInstFl );
- //printf("%s disabled\n",cmStringNullGuard(cmDspInstLabel(ctx,inst)));
- }
- else
- {
- if( msgSymId == ctx->_enableSymId )
- {
- inst->flags = cmClrFlag(inst->flags,kDisableExecInstFl );
- //printf("%s enabled\n",cmStringNullGuard(cmDspInstLabel(ctx,inst)));
- }
- }
- }
-
- if( inst->classPtr->sysRecvFunc != NULL )
- return inst->classPtr->sysRecvFunc(ctx,inst,attrSymId,value);
-
- return kOkDspRC;
- }
-
- void* cmDspInstAllocate(
- cmDspCtx_t* ctx,
- cmDspClass_t* classPtr,
- const cmDspVarArg_t* argV,
- unsigned instByteCnt,
- unsigned instSymId,
- unsigned instId,
- unsigned storeSymId,
- unsigned va_cnt,
- va_list vl )
- {
- unsigned i;
- unsigned varCnt = 0;
- unsigned varDataByteCnt = 0;
- unsigned va_idx = 0;
- unsigned reqArgCnt = 0;
-
- // check for duplicate variable names
- for(i=0; argV[i].label != NULL; ++i)
- if( argV[i].label != NULL )
- {
- unsigned j = 0;
- for(j=0; argV[j].label != NULL; ++j)
- if( i != j && argV[j].label != NULL && strcmp(argV[i].label,argV[j].label) == 0 )
- {
- // two variables have the same name
- unsigned mask = kInDsvFl | kOutDsvFl;
-
- // we allow two variables to have the same name as long as they are not both inputs or outputs
- if( (argV[j].flags & mask) == (argV[i].flags & mask))
- cmDspClassErr(ctx,classPtr,kVarDuplicateDspRC,"The variable label '%s' is used by multiple variables.",argV[i].label);
- }
- }
-
- // determine the count and size of the instance variables
- for(i=0; argV[i].label != NULL; ++i,++varCnt)
- {
- unsigned rn = argV[i].rn;
- unsigned flags = argV[i].flags;
-
- // audio buffer sample count is determined automatically
- if( cmAllFlags( argV[i].flags, kAudioBufDsvFl | kOutDsvFl ) )
- {
- rn = ctx->ctx->ss->args.dspFramesPerCycle;
- flags = kSampleDsvFl | kMtxDsvFl;
- }
-
- // determine the space needed for matrices of known size
- if( cmIsFlag(flags,kMtxDsvFl) && (rn*argV[i].cn) )
- {
- unsigned n = cmDsvByteCount( flags, rn, argV[i].cn );
- varDataByteCnt += n;
- }
-
- // determine the number of required arguments
- reqArgCnt += cmIsFlag(flags,kReqArgDsvFl) ? 1 : 0;
- }
-
-
- unsigned varRecdByteCnt = varCnt * sizeof(cmDspVar_t);
- unsigned ttlByteCnt = instByteCnt + varRecdByteCnt + varDataByteCnt;
-
- char* p = cmLHeapAllocZ(ctx->lhH, ttlByteCnt); // allocate the instance memory
- char* ep = p + ttlByteCnt; // record the end of the alloc'd memory
- cmDspInst_t* ip = (cmDspInst_t*)p; // set the instance ptr
- cmDspVar_t* varArray = (cmDspVar_t*)(p + instByteCnt); // set the instance variable recd array
- char* varDataPtr = (char*)(varArray + varCnt); // set the base of variable data buffer
-
-
- // setup the instance record
- ip->classPtr = classPtr;
- ip->symId = instSymId;
- ip->id = instId;
- ip->flags = kDisableExecInstFl;
- ip->presetGroupSymId = storeSymId;
- ip->varArray = varArray;
- ip->varCnt = varCnt;
-
- ip->freeFunc = classPtr->freeFunc;
- ip->resetFunc = classPtr->resetFunc;
- ip->execFunc = classPtr->execFunc;
- ip->recvFunc = classPtr->recvFunc;
- ip->storeFunc = classPtr->storeFunc;
- ip->sysRecvFunc = _cmDspInstSysRecvFunc;
-
- if( reqArgCnt > va_cnt )
- cmDspInstErr(ctx,ip,kVarArgParseFailDspRC,"The class requires %i arguments but only %i were given.",reqArgCnt,va_cnt);
-
- // setup the variable records
- for(i=0; i<varCnt; ++i)
- {
- unsigned flags = argV[i].flags;
- unsigned rn = argV[i].rn;
-
- // handle requests for audio output buffers
- if( cmAllFlags(argV[i].flags, kAudioBufDsvFl | kOutDsvFl ) )
- {
- // remove the audio buffer flag and reconstitute as a generic cmDspValue sample mtx type.
- //flags = cmClrFlag( argV[i].flags, kAudioBufDsvFl | kTypeDsvMask );
-
- flags = kSampleDsvFl | kMtxDsvFl;
- rn = ctx->ctx->ss->args.dspFramesPerCycle;
- }
-
- // the constId must be the same as the var index
- assert( i == argV[i].constId );
-
- bool reqArgFl = cmIsFlag(argV[i].flags,kReqArgDsvFl);
- bool optArgFl = cmIsFlag(argV[i].flags,kOptArgDsvFl);
-
- // verify that the required arg. exists
- if( reqArgFl && i>va_cnt )
- cmDspInstErr(ctx,ip,kVarArgParseFailDspRC,"The required argument '%s' is missing",argV[i].label);
-
- ip->varArray[i].flags = argV[i].flags; // store the original flag set as the var flags
- ip->varArray[i].constId = argV[i].constId;
- ip->varArray[i].symId = cmSymTblRegisterSymbol(ctx->stH,argV[i].label);
- ip->varArray[i].doc = argV[i].doc;
- ip->varArray[i].value.flags = argV[i].flags & kTypeDsvMask;
-
- // if this is a required or optional constructor arg. then assign it as the
- // default value for this variable
- if( va_idx < va_cnt && (reqArgFl || optArgFl) )
- {
- cmDspValue_t* vp = &ip->varArray[i].value;
-
- switch(ip->varArray[i].value.flags)
- {
- case kBoolDsvFl: cmDsvSetBool( vp, va_arg(vl,int )); break;
- case kCharDsvFl: cmDsvSetChar( vp, va_arg(vl,int )); break;
- case kUCharDsvFl: cmDsvSetUChar( vp, va_arg(vl,int )); break;
- case kShortDsvFl: cmDsvSetShort( vp, va_arg(vl,int )); break;
- case kUShortDsvFl: cmDsvSetUShort( vp, va_arg(vl,int )); break;
- case kLongDsvFl: cmDsvSetLong( vp, va_arg(vl,long )); break;
- case kULongDsvFl: cmDsvSetULong( vp, va_arg(vl,unsigned long )); break;
- case kIntDsvFl: cmDsvSetInt( vp, va_arg(vl,int )); break;
- case kUIntDsvFl: cmDsvSetUInt( vp, va_arg(vl,unsigned )); break;
- case kFloatDsvFl: cmDsvSetFloat( vp, va_arg(vl,double )); break;
- case kDoubleDsvFl: cmDsvSetDouble( vp, va_arg(vl,double )); break;
- case kSampleDsvFl: cmDsvSetSample( vp, va_arg(vl,double )); break;
- case kRealDsvFl: cmDsvSetReal( vp, va_arg(vl,double )); break;
- case kStrzDsvFl: cmDsvSetStrz( vp, va_arg(vl,cmChar_t* )); break;
- case kSymDsvFl: cmDsvSetSymbol( vp, va_arg(vl,unsigned )); break;
- default:
- cmDspInstErr(ctx,ip,kVarArgParseFailDspRC,"A var-args parse error occurred while parsing the variable '%s'.",argV[i].label);
- }
-
- // set the default value from the initial value
- cmDspValueSet(ctx, ip, i, vp, kSetDefaultDspFl );
-
- // store the fact that the default value was explicitely set.
- ip->varArray[i].flags = cmSetFlag(ip->varArray[i].flags,kDfltSetDsvFl);
-
- // track the number of va_list arg's read
- ++va_idx;
- }
-
- // assign memory to the matrix types of known size
- if( cmIsFlag(flags,kMtxDsvFl) && (rn*argV[i].cn) )
- {
- cmDsvSetMtx( &ip->varArray[i].value, flags, varDataPtr, rn, argV[i].cn );
- unsigned n = cmDsvByteCount( flags, rn, argV[i].cn );
- varDataPtr += n;
- }
- }
-
- assert( varDataPtr == ep );
-
- return p;
- }
-
- #ifdef OSX_VER_10_5
- va_list _cmDspParseArgV( cmDspVarArg_t* a, va_list vl )
- #else
- void _cmDspParseArgV( cmDspVarArg_t* a, va_list vl )
- #endif
- {
- a->label = va_arg(vl,const char*);
- a->constId = va_arg(vl,unsigned);
- a->rn = va_arg(vl,unsigned);
- a->cn = va_arg(vl,unsigned);
- a->flags = va_arg(vl,unsigned);
- a->doc = va_arg(vl,const char*);
- #ifdef OSX_VER_10_5
- return vl;
- #endif
-
- }
-
- void* cmDspInstAllocateV(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned instByteCnt, unsigned instSymId, unsigned instId, unsigned storeSymId, unsigned va_cnt, va_list vl0, ... )
- {
- va_list vl1,vl2;
- va_start(vl1,vl0);
- va_copy(vl2,vl1);
-
- unsigned argCnt = 0;
- int repeatCnt;
-
- while( (repeatCnt = va_arg(vl1,int)) != 0 )
- {
- cmDspVarArg_t a;
-
- // do some value checking to possibly catch problems with the var_args format
- assert( repeatCnt > 0 && repeatCnt < 255 );
-
- argCnt += repeatCnt;
-
- #ifdef OSX_VER_10_5
- vl1 =
- #endif
- _cmDspParseArgV(&a,vl1);
- }
-
- va_end(vl1);
-
- cmDspVarArg_t aa[ argCnt+1 ];
- unsigned j=0;
- while( (repeatCnt = va_arg(vl2,int)) != 0 )
- {
- cmDspVarArg_t a;
-
- #ifdef OSX_VER_10_5
- vl2 =
- #endif
- _cmDspParseArgV(&a,vl2);
-
- cmDspArgSetupN(ctx,aa,argCnt,j,repeatCnt,a.label,a.constId,a.rn, a.cn, a.flags, a.doc );
- j += repeatCnt;
-
- }
- cmDspArgSetupNull(aa + argCnt);
-
- va_end(vl2);
-
- return cmDspInstAllocate(ctx,classPtr,aa,instByteCnt,instSymId,instId,storeSymId,va_cnt,vl0);
- }
-
- cmDspInstSymId_t* _cmDspInstFindAttrSymId( cmDspInst_t* inst, unsigned symId )
- {
- cmDspInstSymId_t* ip = inst->symIdList;
- for(; ip != NULL; ip=ip->link)
- if( ip->symId == symId )
- return ip;
- return NULL;
- }
-
- bool cmDspInstHasAttrSym( cmDspInst_t* inst, unsigned attrSymId )
- { return _cmDspInstFindAttrSymId(inst,attrSymId) != NULL; }
-
-
- cmDspRC_t cmDspInstRegisterAttrSym( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned symId )
- {
- cmDspInstSymId_t* ip;
-
- // if this symbol is already registered
- if((ip = _cmDspInstFindAttrSymId(inst,symId)) != NULL )
- return cmDspInstErr(ctx,inst,kOkDspRC,"The symbol '%s' is already registered with the instance.",cmStringNullGuard(cmSymTblLabel(ctx->stH,symId)));
-
- // try to find an unused symbol
- ip = inst->symIdList;
- for(; ip != NULL; ip=ip->link)
- if( ip->symId == cmInvalidId )
- break;
-
- // if no unused symbols were found then allocate a new one
- if( ip == NULL )
- {
- ip = cmLhAllocZ(ctx->lhH, cmDspInstSymId_t, 1 );
- ip->link = inst->symIdList;
- inst->symIdList = ip;
-
- }
-
- ip->symId = symId;
-
- return kOkDspRC;
- }
-
- cmDspRC_t cmDspInstRemoveAttrSym( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned symId )
- {
- cmDspInstSymId_t* ip;
-
- // if this symbol is already registered
- if((ip = _cmDspInstFindAttrSymId(inst,symId)) == NULL )
- return cmDspInstErr(ctx,inst,kOkDspRC,"The symbol '%s' is could not be found to be removed from the instance.",cmStringNullGuard(cmSymTblLabel(ctx->stH,symId)));
-
- // mark a symbol recd as inactive by setting the symbol id to cmInalidId
- ip->symId = cmInvalidId;
- return kOkDspRC;
- }
-
-
- cmDspRC_t _cmDspClassErrV( cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned instId, cmDspRC_t rc, const cmChar_t* fmt, va_list vl )
- {
- va_list vl2;
- va_copy(vl2,vl);
- unsigned n = vsnprintf(NULL,0,fmt,vl2)+1;
-
- cmChar_t buf[n+1];
- vsnprintf(buf,n,fmt,vl);
- if( instId == cmInvalidId )
- cmErrMsg( &classPtr->err, rc, "%s DSP Class:%s",buf, classPtr->labelStr);
- else
- cmErrMsg( &classPtr->err, rc, "%s DSP Class:%s Inst:%i",buf, classPtr->labelStr,instId);
-
- va_end(vl2);
- return rc;
- }
-
- cmDspRC_t cmDspClassErr( cmDspCtx_t* ctx, cmDspClass_t* classPtr, cmDspRC_t rc, const cmChar_t* fmt, ... )
- {
- va_list vl;
- va_start(vl,fmt);
- rc = _cmDspClassErrV(ctx,classPtr,cmInvalidId,rc,fmt,vl);
- va_end(vl);
- return rc;
- }
-
- cmDspRC_t cmDspInstErr( cmDspCtx_t* ctx, cmDspInst_t* inst, cmDspRC_t rc, const cmChar_t* fmt, ... )
- {
- va_list vl;
- va_start(vl,fmt);
- rc = _cmDspClassErrV(ctx,inst->classPtr,inst->id,rc,fmt,vl);
- va_end(vl);
- return rc;
- }
-
- void cmDspArgSetup(
- cmDspCtx_t* ctx,
- cmDspVarArg_t* arg,
- const cmChar_t* labelPrefix,
- unsigned labelId,
- unsigned constId,
- unsigned rn,
- unsigned cn,
- unsigned flags,
- const cmChar_t* docStr )
- {
- unsigned labelIdCharCnt = (unsigned)floor( labelId==0 ? 1.0 : log10(abs(labelId)) + 1.0 );
- int labelCharCnt = strlen(labelPrefix) + 1 + labelIdCharCnt + 1;
- cmChar_t label[ labelCharCnt + 1 ];
- const cmChar_t* lp;
-
- if( labelId == cmInvalidId )
- lp = labelPrefix;
- else
- {
- lp = label;
- label[labelCharCnt] = 0;
- snprintf(label,labelCharCnt,"%s-%i",labelPrefix,labelId);
- }
-
- // use the symbol table to hold the label string
- arg->label = cmSymTblLabel(ctx->stH,cmSymTblRegisterSymbol(ctx->stH,lp));
- arg->constId = constId;
- arg->rn = rn;
- arg->cn = cn;
- arg->flags = flags;
- arg->doc = cmLhAllocStr(ctx->lhH,docStr);;
- }
-
- unsigned cmDspArgCopy( cmDspVarArg_t* argArray, unsigned argN, unsigned dstIdx, const cmDspVarArg_t* s, unsigned sn )
- {
- assert( dstIdx + sn <= argN );
- unsigned i;
- for(i=0; i<sn; ++i)
- argArray[dstIdx+i] = s[i];
- return dstIdx + i;
- }
-
- unsigned cmDspArgSetupN(
- cmDspCtx_t* ctx,
- cmDspVarArg_t* arg,
- unsigned argN,
- unsigned dstIdx,
- unsigned cnt,
- const cmChar_t* labelPrefix,
- unsigned baseConstId,
- unsigned rn,
- unsigned cn,
- unsigned flags,
- const cmChar_t* staticDocStr )
- {
- assert( dstIdx + cnt <= argN );
- unsigned i;
- for(i=0; i<cnt; ++i)
- cmDspArgSetup(ctx,arg + dstIdx + i,labelPrefix, cnt==1 ? cmInvalidId : i, baseConstId + i, rn, cn, flags, staticDocStr );
- return dstIdx + i;
- }
-
- void cmDspArgSetupNull( cmDspVarArg_t* arg )
- { memset(arg,0,sizeof(*arg)); }
-
-
- const cmChar_t* cmDspInstLabel( cmDspCtx_t* ctx, cmDspInst_t* inst )
- {
- const char* noInstLbl = "<no_inst_lbl>";
- const char* lbl = noInstLbl;
-
- if( inst->symId == cmInvalidId )
- return lbl;
-
- if((lbl = cmSymTblLabel(ctx->stH,inst->symId)) == NULL )
- lbl = noInstLbl;
-
- return lbl;
- }
-
-
- cmDspRC_t _cmDspVarPtr( const cmDspInst_t* inst, unsigned varId, cmDspVar_t** retVarPtrPtr )
- {
- if( varId >= inst->varCnt )
- {
- *retVarPtrPtr = NULL;
- return cmErrMsg(&inst->classPtr->err,kVarNotFoundDspRC,"The variable id %i is invalid for DSP instance '%s' (id:%i) .", varId, inst->classPtr->labelStr, inst->id );
- }
-
- assert( inst->varArray[varId].constId == varId );
-
- *retVarPtrPtr = inst->varArray + varId;
-
- return kOkDspRC;
- }
-
- cmDspRC_t _cmDspVarAndValuePtr( cmDspInst_t* inst, unsigned varId, cmDspVar_t** varPtrPtr, cmDspValue_t** valPtrPtr )
- {
- cmDspRC_t rc;
-
- if((rc = _cmDspVarPtr(inst,varId,varPtrPtr)) != kOkDspRC )
- *valPtrPtr = NULL;
- else
- *valPtrPtr = cmDsvValuePtr(&(*varPtrPtr)->value);
-
- return rc;
- }
-
- cmDspRC_t _cmDspVarValuePtr( cmDspInst_t* inst, unsigned varId, cmDspValue_t** retValPtrPtr )
- {
- cmDspVar_t* varPtr = NULL;
- cmDspRC_t rc;
-
- if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
- *retValPtrPtr = NULL;
- else
- *retValPtrPtr = cmDsvValuePtr(&varPtr->value);
-
- return rc;
- }
-
- cmDspRC_t _cmDspVarDefaultPtr( cmDspInst_t* inst, unsigned varId, cmDspValue_t** retValPtrPtr )
- {
- cmDspVar_t* varPtr = NULL;
- cmDspRC_t rc;
-
- if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
- *retValPtrPtr = NULL;
- else
- *retValPtrPtr = &varPtr->dflt;
-
- return rc;
- }
-
- void _cmDspSendEvt( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, unsigned srcVarId, const cmDspVar_t* varPtr )
- {
- cmDspEvt_t e;
- cmDspCb_t* cbp = varPtr->cbList;
-
- if( cbp == NULL )
- return;
-
- e.flags = kDfltEvtDspFlags;
- e.srcInstPtr = srcInstPtr;
- e.srcVarId = srcVarId;
- e.valuePtr = &varPtr->value;
-
- for(; cbp!=NULL; cbp=cbp->linkPtr)
- {
- e.dstVarId = cbp->dstVarId;
- e.dstDataPtr = cbp->dstDataPtr;
-
- if( cbp->dstInstPtr->recvFunc!=NULL)
- cbp->dstInstPtr->recvFunc(ctx,cbp->dstInstPtr,&e);
- }
- }
-
- cmDspVar_t* cmDspVarSymbolToPtr( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varSymId, unsigned flags )
- {
- unsigned i;
- for(i=0; i<inst->varCnt; ++i)
- if( inst->varArray[i].symId == varSymId )
- {
- if( flags==0 || cmAllFlags(inst->varArray[i].flags,flags) )
- return inst->varArray + i;
- }
-
- cmDspInstErr(ctx,inst,kVarNotFoundDspRC,"The variable '%s' could not be found in the instance '%s' (id:%i).",
- cmSymTblLabel( ctx->stH, varSymId), inst->classPtr->labelStr, inst->id );
-
- return NULL;
- }
-
- const cmDspVar_t* cmDspVarIdToCPtr( const cmDspInst_t* inst, unsigned varId )
- {
- cmDspVar_t* varPtr;
-
- if( _cmDspVarPtr(inst, varId, &varPtr) != kOkDspRC )
- return NULL;
-
- return varPtr;
- }
-
- cmDspVar_t* cmDspVarIdToPtr( cmDspInst_t* inst, unsigned varId )
- {
- cmDspVar_t* varPtr;
-
- if( _cmDspVarPtr(inst, varId, &varPtr) != kOkDspRC )
- return NULL;
-
- return varPtr;
- }
-
- const cmChar_t* cmDspVarLabel( cmDspCtx_t* ctx, const cmDspInst_t* inst, unsigned varId )
- {
- const cmDspVar_t* varPtr;
- if(( varPtr = cmDspVarIdToCPtr(inst,varId) ) == NULL )
- return NULL;
-
- return cmSymTblLabel( ctx->stH, varPtr->symId);
- }
-
- cmDspRC_t cmDspOutputEvent( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
- {
- cmDspRC_t rc;
- cmDspVar_t* varPtr;
-
- if((rc = _cmDspVarPtr(inst, varId, &varPtr )) != kOkDspRC )
- return rc;
-
- _cmDspSendEvt(ctx, inst, varId, varPtr );
-
- return rc;
- }
-
- cmDspRC_t cmDspVarPresetWrite( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
- {
- cmDspRC_t rc;
- const cmDspVar_t* varPtr;
-
- if((varPtr = cmDspVarIdToCPtr(inst,varId)) == NULL )
- return cmDspInstErr(ctx,inst,kVarNotFoundDspRC,"Var with id %i not found during preset store.");
-
- if((rc = cmDspSysPresetWriteValue(ctx->dspH, varPtr->symId, &varPtr->value )) != kOkDspRC )
- return cmDspInstErr(ctx,inst,kSubSysFailDspRC,"Var preset storage failed on var:'%s'.",cmStringNullGuard(cmSymTblLabel(ctx->stH,varPtr->symId)));
-
- return rc;
- }
-
- cmDspRC_t cmDspVarPresetRead( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
- {
- cmDspRC_t rc;
- cmDspVar_t* varPtr;
- cmDspValue_t value;
-
- if((varPtr = cmDspVarIdToPtr(inst,varId)) == NULL )
- return cmDspInstErr(ctx,inst,kVarNotFoundDspRC,"Var with id %i not found during preset recall.");
-
- if((rc = cmDspSysPresetReadValue(ctx->dspH, varPtr->symId, &value )) != kOkDspRC )
- return cmDspInstErr(ctx,inst,kSubSysFailDspRC,"Var preset recall failed on var:'%s'.",cmStringNullGuard(cmSymTblLabel(ctx->stH,varPtr->symId)));
-
- return cmDspValueSet(ctx,inst,varId,&value,0);
- }
-
- cmDspRC_t cmDspVarPresetRdWr( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, bool storeFl )
- {
- cmDspRC_t rc;
-
- if( storeFl )
- rc = cmDspVarPresetWrite(ctx,inst,varId);
- else
- rc = cmDspVarPresetRead(ctx,inst,varId);
-
- return rc;
- }
-
- double cmDspSampleRate( cmDspCtx_t* ctx )
- { return ctx->ctx->ss->args.srate; }
-
- unsigned cmDspSamplesPerCycle( cmDspCtx_t * ctx )
- { return ctx->ctx->ss->args.dspFramesPerCycle; }
-
- unsigned cmDspTypeFlags(const cmDspInst_t* inst, unsigned varId )
- {
- cmDspVar_t* varPtr;
-
- unsigned flags;
- if( _cmDspVarPtr(inst,varId,&varPtr) != kOkDspRC )
- return 0;
-
- cmDspValue_t* valPtr = cmDsvValuePtr(&varPtr->value);
-
- if( valPtr->flags != 0 )
- flags = valPtr->flags;
- else
- flags = varPtr->flags;
-
- return flags & kTypeDsvMask;
- }
-
- bool cmDspIsBool( const cmDspInst_t* inst, unsigned varId )
- { return cmIsFlag(cmDspTypeFlags(inst,varId),kBoolDsvFl); }
-
- bool cmDspIsInt( const cmDspInst_t* inst, unsigned varId )
- { return cmIsFlag(cmDspTypeFlags(inst,varId),kIntDsvFl); }
-
- bool cmDspIsUInt( const cmDspInst_t* inst, unsigned varId )
- { return cmIsFlag(cmDspTypeFlags(inst,varId),kUIntDsvFl); }
-
- bool cmDspIsDouble( const cmDspInst_t* inst, unsigned varId )
- { return cmIsFlag(cmDspTypeFlags(inst,varId),kDoubleDsvFl); }
-
- bool cmDspIsStrz( const cmDspInst_t* inst, unsigned varId )
- { return cmIsFlag(cmDspTypeFlags(inst,varId),kStrzDsvFl); }
-
- bool cmDspIsSymbol( const cmDspInst_t* inst, unsigned varId )
- { return cmIsFlag(cmDspTypeFlags(inst,varId),kSymDsvFl); }
-
- bool cmDspIsJson( const cmDspInst_t* inst, unsigned varId )
- { return cmIsFlag(cmDspTypeFlags(inst,varId),kJsonDsvFl); }
-
- bool cmDspBool( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetBool(vp);
- }
-
- int cmDspInt( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetInt(vp);
- }
-
-
- unsigned cmDspUInt( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetUInt(vp);
- }
-
- double cmDspDouble( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetDouble(vp);
- }
-
- cmSample_t cmDspSample( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetSample(vp);
- }
-
- cmReal_t cmDspReal( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetReal(vp);
- }
-
- const cmChar_t* cmDspStrcz( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
- return NULL;
-
- return cmDsvStrcz(vp);
- }
-
- unsigned cmDspSymbol( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
- return cmInvalidId;
-
- return cmDsvSymbol(vp);
- }
-
- cmJsonNode_t* cmDspJson( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
- return NULL;
-
- return cmDsvJson(vp);
- }
-
- bool cmDspDefaultBool( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetBool(vp);
- }
-
- int cmDspDefaultInt( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetInt(vp);
- }
-
-
- unsigned cmDspDefaultUInt( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetUInt(vp);
- }
-
- double cmDspDefaultDouble( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetDouble(vp);
- }
-
- cmSample_t cmDspDefaultSample( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetSample(vp);
- }
-
- cmReal_t cmDspDefaultReal( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvGetReal(vp);
- }
-
- const cmChar_t* cmDspDefaultStrcz( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
- return NULL;
-
- return cmDsvGetStrcz(vp);
- }
-
- unsigned cmDspDefaultSymbol( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
- return cmInvalidId;
-
- return cmDsvGetSymbol(vp);
- }
-
- cmJsonNode_t* cmDspDefaultJson( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarDefaultPtr(inst,varId,&vp) != kOkDspRC )
- return NULL;
-
- return cmDsvGetJson(vp);
- }
-
-
- cmDspRC_t cmDspValueSet( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmDspValue_t* svp, unsigned flags )
- {
- cmDspRC_t rc;
- cmDspVar_t* varPtr;
-
- // get a pointer to the var recd
- if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
- return rc;
-
- // get a pointer to the target value.
- cmDspValue_t* dvp = cmIsFlag(flags,kSetDefaultDspFl) ? &varPtr->dflt : cmDsvValuePtr(&varPtr->value);
-
- // store a pointer to the dst string memory in case we need to delete it later
- cmChar_t* rlsPtr = cmAllFlags(dvp->flags,kStrzDsvFl | kDynDsvFl) ? dvp->u.z : NULL;
-
- // set the dest type to the type shared with the source
- // ( this assumes that the dst may have multiple types but the source has one type)
- unsigned typeFlags = (varPtr->flags & svp->flags) & kTypeDsvMask;
-
- // if svp->flags is set to kNullDsvFl then use just the dst type
- if( typeFlags == 0 )
- typeFlags = varPtr->flags & kTypeDsvMask;
-
- // convert the source to the var type
- switch(typeFlags)
- {
- case kBoolDsvFl:
- cmDsvSetBool( dvp, cmDsvGetBool(svp));
- break;
-
- case kIntDsvFl:
- cmDsvSetInt( dvp, cmDsvGetInt(svp));
- break;
-
- case kUIntDsvFl:
- cmDsvSetUInt( dvp, cmDsvGetUInt(svp));
- break;
-
- case kDoubleDsvFl:
- cmDsvSetDouble(dvp, cmDsvGetDouble(svp));
- break;
-
- case kSampleDsvFl:
- cmDsvSetSample(dvp, cmDsvGetSample(svp));
- break;
-
- case kRealDsvFl:
- cmDsvSetReal(dvp, cmDsvGetReal(svp));
- break;
-
- case kSymDsvFl:
- cmDsvSetSymbol(dvp, cmDsvGetSymbol(svp));
- break;
-
- case kJsonDsvFl:
- cmDsvSetJson( dvp, cmDsvJson(svp));
- break;
-
- case kStrzDsvFl:
- {
- const cmChar_t* sp;
-
- if( cmDsvIsSymbol(svp) )
- sp = cmSymTblLabel(ctx->stH,cmDsvSymbol(svp));
- else
- sp = cmDsvGetStrcz(svp);
-
- // don't copy over myself
- if( sp == dvp->u.z )
- rlsPtr = NULL;
- else
- {
- // if the source == NULL then set the dst to NULL
- // (NULL should always be a legitimate value)
- if( sp == NULL )
- cmDsvSetStrcz(dvp,sp);
- else
- {
- // if the source should not be copied into internal memory
- if( cmIsFlag(flags,kNoAllocDspFl) )
- cmDsvSetStrcz(dvp,sp);
- else
- {
- // allocate memory to hold the new string
- unsigned n = strlen(sp)+1;
- cmChar_t* dp = cmLhResizeN(ctx->lhH,cmChar_t,rlsPtr,n);
- strncpy(dp,sp,n);
- cmDsvSetStrz(dvp, dp);
- dvp->flags = cmSetFlag(dvp->flags,kDynDsvFl);
- rlsPtr = NULL;
- }
- }
- }
- }
- break;
-
- default:
- { assert(0); }
- }
-
- // if the dst contained a dynamically alloc'd string prior to being
- // set with a new value - then release the memory used by the original
- // string here
- if( rlsPtr != NULL )
- cmLHeapFree(ctx->lhH,rlsPtr);
-
- // notify listeners of the change of value
- if( cmIsFlag(flags,kNoSendDspFl) == false )
- _cmDspSendEvt( ctx, inst, varId, varPtr );
-
- // notify the UI of the change of value
- if( cmIsFlag(varPtr->flags,kUiDsvFl) && cmIsFlag(flags,kNoUpdateUiDspFl)==false )
- {
- cmDspUiSendVar( ctx, inst, varPtr );
- }
-
- return rc;
- }
-
- cmDspRC_t cmDspApplyDefault( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
- {
- cmDspRC_t rc;
- cmDspVar_t* varPtr;
-
- // get a pointer to the var recd
- if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
- return rc;
-
- if( cmIsFlag(varPtr->flags,kDfltSetDsvFl) )
- {
- // unless the var has kSendDfltDsvFl set we don't transmit the default values
- unsigned flags = cmIsFlag(varPtr->flags,kSendDfltDsvFl) ? 0 : kNoSendDspFl;
-
- return cmDspValueSet(ctx, inst, varId, &varPtr->dflt, flags );
- }
-
- return kOkDspRC;
- }
-
- cmDspRC_t cmDspApplyAllDefaults( cmDspCtx_t* ctx, cmDspInst_t* inst )
- {
- cmDspRC_t rc = kOkDspRC;
- unsigned i;
- for(i=0; i<inst->varCnt; ++i)
- if((rc = cmDspApplyDefault(ctx,inst,inst->varArray[i].constId)) != kOkDspRC )
- return rc;
-
- return rc;
- }
-
- cmDspRC_t cmDspSetDefault( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmDspValue_t* valPtr )
- {
- cmDspRC_t rc;
- cmDspVar_t* varPtr;
-
- // get a pointer to the var recd
- if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
- return rc;
-
- if(( cmDspValueSet( ctx, inst, varId, valPtr, kSetDefaultDspFl )) == kOkDspRC )
- {
- varPtr->flags = cmSetFlag(varPtr->flags, kDfltSetDsvFl);
- }
-
- return rc;
- }
-
- cmDspRC_t _cmDspDefaultSet( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmDspValue_t* valPtr, bool nonInitValIsEqualFl )
- {
- cmDspRC_t rc;
- cmDspVar_t* varPtr;
-
- // get a pointer to the var recd
- if((rc = _cmDspVarPtr(inst,varId,&varPtr)) != kOkDspRC )
- return rc;
-
- // if the dflt value was previuosly set and the default value is
- // not equal to the non-intialized value for this var then the default value
- // has already been set to a legal value
- if( cmIsFlag(varPtr->flags,kDfltSetDsvFl) && nonInitValIsEqualFl==false )
- return kOkDspRC;
-
- return cmDspSetDefault(ctx,inst,varId,valPtr);
- }
-
- cmDspRC_t cmDspSetDefaultBool( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, bool nonInitVal, bool val )
- {
- cmDspValue_t v;
- double dv = cmDspDefaultBool(inst,varId);
- cmDsvSetBool(&v,val);
- return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
- }
-
- cmDspRC_t cmDspSetDefaultInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, int nonInitVal, int val )
- {
- cmDspValue_t v;
- int dv = cmDspDefaultInt(inst,varId);
- cmDsvSetInt(&v,val);
- return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
- }
-
- cmDspRC_t cmDspSetDefaultUInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned nonInitVal, unsigned val )
- {
- cmDspValue_t v;
- unsigned dv = cmDspDefaultUInt(inst,varId);
- cmDsvSetUInt(&v,val);
- return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
- }
-
- cmDspRC_t cmDspSetDefaultDouble( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, double nonInitVal, double val )
- {
- cmDspValue_t v;
- double dv = cmDspDefaultDouble(inst,varId);
- cmDsvSetDouble(&v,val);
- return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
- }
-
- cmDspRC_t cmDspSetDefaultSample( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmSample_t nonInitVal, cmSample_t val )
- {
- cmDspValue_t v;
- cmSample_t dv = cmDspDefaultSample(inst,varId);
- cmDsvSetSample(&v,val);
- return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
- }
-
- cmDspRC_t cmDspSetDefaultReal( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmReal_t nonInitVal, cmReal_t val )
- {
- cmDspValue_t v;
- cmReal_t dv = cmDspDefaultReal(inst,varId);
- cmDsvSetReal(&v,val);
- return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
- }
-
- cmDspRC_t cmDspSetDefaultSymbol( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned val )
- {
- cmDspValue_t v;
- unsigned dv = cmDspDefaultSymbol(inst,varId);
- cmDsvSetSymbol(&v,val);
- return _cmDspDefaultSet(ctx,inst,varId,&v,dv==cmInvalidId);
- }
-
- cmDspRC_t cmDspSetDefaultStrcz( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmChar_t* nonInitVal, const cmChar_t* val )
- {
- cmDspValue_t v;
- const cmChar_t* dv= cmDspDefaultStrcz(inst,varId);
- cmDsvSetStrcz(&v,val);
- bool fl = false; // assume the default and non-init value are not equal
-
- // if the pointers are the same then the strings must match
- if( dv == nonInitVal )
- fl = true;
- else
- // if both strings exist - then test if they are equal
- if( dv!=NULL && nonInitVal!=NULL)
- fl = strcmp(dv,nonInitVal)==0;
-
- return _cmDspDefaultSet(ctx,inst,varId,&v,fl);
- }
-
- cmDspRC_t cmDspSetDefaultJson( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmJsonNode_t* nonInitVal, cmJsonNode_t* val )
- {
- cmDspValue_t v;
- cmJsonNode_t* dv= cmDspDefaultJson(inst,varId);
- cmDsvSetJson(&v,val);
- return _cmDspDefaultSet(ctx,inst,varId,&v,dv==nonInitVal);
- }
-
-
- cmDspRC_t cmDspSetBool( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, bool val )
- {
- cmDspValue_t v;
- cmDsvSetBool(&v,val);
- return cmDspValueSet(ctx, inst, varId, &v, 0);
- }
-
- cmDspRC_t cmDspSetInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, int val )
- {
- cmDspValue_t v;
- cmDsvSetInt(&v,val);
- return cmDspValueSet(ctx, inst, varId, &v, 0);
- }
-
- cmDspRC_t cmDspSetUInt( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned val )
- {
- cmDspValue_t v;
- cmDsvSetUInt(&v,val);
- return cmDspValueSet(ctx, inst, varId, &v, 0);
- }
-
- cmDspRC_t cmDspSetDouble( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, double val )
- {
- cmDspValue_t v;
- cmDsvSetDouble(&v,val);
- return cmDspValueSet(ctx, inst, varId, &v, 0);
- }
-
- cmDspRC_t cmDspSetSample( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmSample_t val )
- {
- cmDspValue_t v;
- cmDsvSetSample(&v,val);
- return cmDspValueSet(ctx, inst, varId, &v, 0);
- }
-
- cmDspRC_t cmDspSetReal( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmReal_t val )
- {
- cmDspValue_t v;
- cmDsvSetReal(&v,val);
- return cmDspValueSet(ctx, inst, varId, &v, 0);
- }
-
- cmDspRC_t cmDspSetStrcz( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, const cmChar_t* val )
- {
- cmDspValue_t v;
- cmDsvSetStrcz(&v,val);
- return cmDspValueSet(ctx, inst, varId, &v, 0);
- }
-
- cmDspRC_t cmDspSetSymbol( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned val )
- {
- cmDspValue_t v;
- cmDsvSetSymbol(&v,val);
- return cmDspValueSet(ctx, inst, varId, &v, 0);
- }
-
- cmDspRC_t cmDspSetJson( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, cmJsonNode_t* val )
- {
- cmDspValue_t v;
- cmDsvSetJson(&v,val);
- return cmDspValueSet(ctx, inst, varId, &v, 0);
- }
-
- cmDspRC_t cmDspSetEvent( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- {
- unsigned flags = kUpdateUiDspFl;
-
- // We will assume that all values should be reflected to the UI - unless
- // this event is known to have been generated by the UI (evt->kUiDspFl is set)
- // and evt->kUiEchoDspFl is not set.
-
- if( cmIsFlag(evt->flags,kUiDspFl)==true && cmIsFlag(evt->flags,kUiEchoDspFl)==false )
- flags = kNoUpdateUiDspFl;
-
- // Note: If the event target variable is not a UI variable
- // (i.e. cmDspVar_t.kUiDsvFl is not set) then calling cmDspValueSet()
- // with the kUpdateUiDspFl set has no effect.
-
- return cmDspValueSet(ctx,inst,evt->dstVarId, evt->valuePtr, flags);
- }
-
-
- cmDspRC_t cmDspSetEventUi( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
- { return cmDspValueSet(ctx,inst,evt->dstVarId, evt->valuePtr, kUpdateUiDspFl); }
-
- cmDspRC_t cmDspSetEventUiId( cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt, unsigned varId )
- {
- cmDspEvt_t e;
- cmDspEvtCopy(&e,evt);
- e.dstVarId = varId;
- evt = &e;
- return cmDspSetEventUi(ctx,inst,evt);
- }
-
- unsigned cmDspVarRows( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if(_cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvRows(vp);
- }
-
- unsigned cmDspVarCols( cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
- if( _cmDspVarValuePtr(inst,varId,&vp) != kOkDspRC )
- return 0;
-
- return cmDsvCols(vp);
- }
-
- bool cmDspIsAudioInputConnected( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* vp;
-
- if( _cmDspVarValuePtr(inst,varId,&vp ) != kOkDspRC )
- return false;
-
- if( cmAllFlags(vp->flags, kSampleDsvFl | kMtxDsvFl ))
- return true;
-
- return false;
- }
-
- cmDspRC_t cmDspZeroAudioBuf( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId )
- {
- cmDspValue_t* valPtr;
- cmDspVar_t* varPtr;
- cmDspRC_t rc;
-
- if((rc = _cmDspVarAndValuePtr(inst,varId,&varPtr,&valPtr)) != kOkDspRC )
- return rc;
-
- bool fl0 = cmAllFlags(valPtr->flags, kSampleDsvFl | kMtxDsvFl );
- bool fl1 = cmIsFlag(varPtr->flags,kOutDsvFl);
-
- if( fl0==false || fl1==false )
- 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);
-
- memset(cmDsvSampleMtx(valPtr), 0, cmDsvEleCount(valPtr)*sizeof(cmSample_t) );
-
- return rc;
- }
-
- cmSample_t* cmDspAudioBuf( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned chIdx )
- {
- cmDspValue_t* vp;
- cmDspVar_t* varPtr;
- cmDspRC_t rc;
-
- if((rc = _cmDspVarAndValuePtr(inst,varId,&varPtr,&vp )) != kOkDspRC )
- return NULL;
-
- if( !cmAllFlags(vp->flags, kSampleDsvFl | kMtxDsvFl ))
- {
-
- // this is an unconnected audio input
- if( cmAllFlags(varPtr->flags,kInDsvFl | kAudioBufDsvFl ) )
- return NULL;
-
- 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);
- return NULL;
- }
-
- if( chIdx >= cmDsvCols(vp) )
- {
- cmDspInstErr(ctx,inst,kVarTypeErrDspRC,"Channel index %i of audio buffer variable '%s' in DSP instance '%s' (id:%i) is outside the channel count (%i).",
- chIdx,cmDspVarLabel(ctx,inst,varId),inst->classPtr->labelStr,inst->id,cmDsvCols(vp));
- }
-
- return cmDsvSampleMtx(vp) + (chIdx * cmDsvRows(vp));
- }
-
- unsigned cmDspAudioBufSmpCount( cmDspCtx_t* ctx, cmDspInst_t* inst, unsigned varId, unsigned chIdx )
- {
- cmDspValue_t* vp;
- cmDspRC_t rc;
-
- if((rc = _cmDspVarValuePtr(inst,varId,&vp)) != kOkDspRC )
- return 0;
-
- if( !cmAllFlags(vp->flags, kSampleDsvFl | kMtxDsvFl ))
- {
- 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);
- return 0;
- }
-
- return cmDsvRows(vp);
- }
-
- cmDspRC_t cmDspInstInstallCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, unsigned srcVarSymId, cmDspInst_t* dstInstPtr, unsigned dstVarSymId, void* dstDataPtr)
- {
- cmDspVar_t* srcVarPtr;
- cmDspVar_t* dstVarPtr;
-
- // get the src and dst var ptrs
- if((srcVarPtr = cmDspVarSymbolToPtr(ctx,srcInstPtr,srcVarSymId,kOutDsvFl)) == NULL || (dstVarPtr = cmDspVarSymbolToPtr(ctx,dstInstPtr,dstVarSymId,kInDsvFl)) == NULL )
- return cmDspInstErr(ctx,srcInstPtr,kInstCbInstallFailDspRC,"Callback installation failed for connection from '%s' (id:%i) var:'%s' to '%s' (id:%i) var:'%s'.",
- srcInstPtr->classPtr->labelStr, srcInstPtr->id, cmSymTblLabel(ctx->stH,srcVarSymId), dstInstPtr->classPtr->labelStr, dstInstPtr->id, cmSymTblLabel(ctx->stH,dstVarSymId) );
-
- // check for strict type conversion - this will fail for vars which allow multiple types
- // (e.g. numbers and strings)
- if( cmDsvCanConvertFlags(dstVarPtr->value.flags,srcVarPtr->value.flags) == false )
- {
- // if all the flags in the src are set in the dst then the match is ok
- // (this will cover dst types which admit multiple types)
- if( cmAllFlags(dstVarPtr->flags & kTypeDsvMask,srcVarPtr->flags & kTypeDsvMask) == false )
- 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));
- }
-
- if( cmIsFlag(srcVarPtr->flags, kAudioBufDsvFl) )
- cmDspInstErr(ctx,srcInstPtr,kOkDspRC,"An audio source is being used as an event callback connection.");
-
- if( cmIsFlag(dstVarPtr->flags, kAudioBufDsvFl ) )
- cmDspInstErr(ctx,dstInstPtr,kOkDspRC,"An audio destination is the target of a callback connection.");
-
- cmDspCb_t* r = cmLhAllocZ( ctx->lhH, cmDspCb_t, 1 );
- r->srcVarSymId = srcVarSymId;
- r->dstInstPtr = dstInstPtr;
- r->dstVarId = dstVarPtr->constId;
- r->dstDataPtr = dstDataPtr;
- r->linkPtr = NULL;
-
- cmDspCb_t* cp = srcVarPtr->cbList;
- cmDspCb_t* pp = NULL;
-
- for(; cp != NULL; cp = cp->linkPtr )
- pp = cp;
-
- if( pp == NULL )
- srcVarPtr->cbList = r;
- else
- pp->linkPtr = r;
-
- return kOkDspRC;
- }
-
- cmDspRC_t cmDspInstallCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, const cmChar_t* dstVarLabel, void* dstCbDataPtr)
- {
- unsigned srcVarSymId,dstVarSymId;
-
- if( srcInstPtr == NULL && dstInstPtr == NULL )
- return cmErrMsg(&ctx->cmCtx->err,kInstNotFoundDspRC,"The callback installation was passed a NULL source and destination processor instance pointer.");
-
- if( srcInstPtr == NULL )
- 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));
-
- if( dstInstPtr == NULL )
- 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));
-
- if((srcVarSymId = cmSymTblId( ctx->stH, srcVarLabel )) == cmInvalidId )
- return cmDspInstErr(ctx,srcInstPtr,kSrcVarNotFoundDspRC,"Connection failed. The source variable '%s' could not found in '%s' (id:%i) for registration by '%s' (id:%i).",
- srcVarLabel,srcInstPtr->classPtr->labelStr,srcInstPtr->id,dstInstPtr->classPtr->labelStr,dstInstPtr->id);
-
- if((dstVarSymId = cmSymTblId( ctx->stH, dstVarLabel )) == cmInvalidId )
- return cmDspInstErr(ctx,dstInstPtr,kSrcVarNotFoundDspRC,"Connection failed. The destination variable '%s' for the DSP instance '%s' (id:%i).",
- dstVarLabel,dstInstPtr->classPtr->labelStr,dstInstPtr->id);
-
-
- return cmDspInstInstallCb(ctx, srcInstPtr, srcVarSymId, dstInstPtr, dstVarSymId, dstCbDataPtr);
- }
-
- cmDspRC_t cmDspInstRemoveCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, unsigned srcVarSymId, cmDspInst_t* dstInstPtr, unsigned dstVarId )
- {
- cmDspVar_t* varPtr;
-
- if((varPtr = cmDspVarSymbolToPtr(ctx,srcInstPtr,srcVarSymId,kOutDsvFl)) == NULL )
- return cmDspInstErr(ctx,srcInstPtr,kInstCbInstallFailDspRC,"Callback removal failed for instance '%s' (id:%i).",
- srcInstPtr->classPtr->labelStr, srcInstPtr->id );
-
-
- cmDspCb_t* cp = varPtr->cbList;
- cmDspCb_t* pp = NULL;
-
- for(; cp != NULL; cp = cp->linkPtr )
- {
- if( cp->srcVarSymId == srcVarSymId && cp->dstInstPtr == dstInstPtr && cp->dstVarId == dstVarId )
- break;
-
- pp = cp;
- }
-
- if( pp == NULL )
- varPtr->cbList = cp->linkPtr;
- else
- pp->linkPtr = cp->linkPtr;
-
- return kOkDspRC;
-
- }
-
- cmDspRC_t cmDspRemoveCb( cmDspCtx_t* ctx, cmDspInst_t* srcInstPtr, const cmChar_t* srcVarLabel, cmDspInst_t* dstInstPtr, unsigned dstVarId)
- {
- unsigned srcVarSymId;
-
- if((srcVarSymId = cmSymTblId( ctx->stH, srcVarLabel )) == cmInvalidId )
- return cmDspInstErr(ctx,srcInstPtr,kSrcVarNotFoundDspRC,"The variable '%s' could not found in '%s' (id:%i) do de-register for '%s' (id:%i).",
- srcVarLabel,srcInstPtr->classPtr->labelStr,srcInstPtr->id,dstInstPtr->classPtr->labelStr,dstInstPtr->id);
-
- return cmDspInstRemoveCb(ctx, srcInstPtr, srcVarSymId, dstInstPtr, dstVarId );
- }
-
- cmDspRC_t cmDspInstVarSetFlags( cmDspCtx_t* ctx, cmDspInst_t* instPtr, unsigned varId, unsigned flags )
- {
- cmDspVar_t* varPtr;
-
- if(_cmDspVarPtr(instPtr, varId, &varPtr ) != kOkDspRC )
- return cmDspInstErr(ctx,instPtr,kInstSetFlagsFailDspRC,"Set flags failed for DSP instance %s (id:%i).",instPtr->classPtr->labelStr,instPtr->id);
-
- varPtr->flags = cmSetFlag(varPtr->flags,flags);
-
- return kOkDspRC;
- }
|