diff --git a/Makefile.am b/Makefile.am index 07902f9..7ed550d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -39,8 +39,8 @@ cmHDR += src/libcm/cmGr.h src/libcm/cmGrDevCtx.h src/libcm/cmGrPage.h src/libcm/ cmHDR += src/libcm/dsp/cmDspSys.h src/libcm/dsp/cmDspClass.h src/libcm/dsp/cmDspValue.h src/libcm/dsp/cmDspUi.h src/libcm/dsp/cmDspPreset.h src/libcm/dsp/cmDspNet.h cmSRC += src/libcm/dsp/cmDspSys.c src/libcm/dsp/cmDspClass.c src/libcm/dsp/cmDspValue.c src/libcm/dsp/cmDspUi.c src/libcm/dsp/cmDspPreset.c src/libcm/dsp/cmDspNet.c -cmHDR += src/libcm/dsp/cmDspBuiltIn.h src/libcm/dsp/cmDspFx.h -cmSRC += src/libcm/dsp/cmDspBuiltIn.c src/libcm/dsp/cmDspFx.c +cmHDR += src/libcm/dsp/cmDspStore.h src/libcm/dsp/cmDspBuiltIn.h src/libcm/dsp/cmDspFx.h +cmSRC += src/libcm/dsp/cmDspStore.c src/libcm/dsp/cmDspBuiltIn.c src/libcm/dsp/cmDspFx.c cmHDR += src/libcm/dsp/cmDspPgm.h src/libcm/dsp/cmDspPgmPP.h src/libcm/dsp/cmDspPgmPPMain.h cmSRC += src/libcm/dsp/cmDspPgm.c src/libcm/dsp/cmDspPgmPP.c src/libcm/dsp/cmDspPgmPPMain.c diff --git a/dsp/cmDspCtx.h b/dsp/cmDspCtx.h index 8a49b53..45e618c 100644 --- a/dsp/cmDspCtx.h +++ b/dsp/cmDspCtx.h @@ -6,8 +6,11 @@ extern "C" { #endif typedef cmHandle_t cmDspSysH_t; + typedef cmHandle_t cmDspStoreH_t; + struct cmAudioSysCtx_str; + struct cmDspGlobalVar_str; // DSP system context passed to many DSP instance functions typedef struct @@ -20,9 +23,11 @@ extern "C" { cmLHeapH_t lhH; cmJsonH_t jsH; cmSymTblH_t stH; + cmDspStoreH_t dsH; cmJsonH_t rsrcJsH; unsigned cycleCnt; // count of DSP execution cycles (multiply by cmDspSamplesPerCycle() to get time since start of DSP system in samples) + unsigned _disableSymId; unsigned _enableSymId; diff --git a/dsp/cmDspNet.c b/dsp/cmDspNet.c index cfa1f44..49a5e0a 100644 --- a/dsp/cmDspNet.c +++ b/dsp/cmDspNet.c @@ -20,6 +20,7 @@ #include "cmProcObj.h" #include "cmDspCtx.h" #include "cmDspClass.h" +#include "cmDspStore.h" #include "cmDspSys.h" #include "cmDspPreset.h" #include "cmDspNet.h" diff --git a/dsp/cmDspNet.h b/dsp/cmDspNet.h index 70e2026..dfa7d17 100644 --- a/dsp/cmDspNet.h +++ b/dsp/cmDspNet.h @@ -62,6 +62,7 @@ extern "C" { cmDspCtx_t ctx; cmLHeapH_t lhH; // DSP system lHeap used for system memory (DSP instance memory uses ctx->lhH so that it can be removed independent of the DSP system memory) cmSymTblH_t stH; // DSP system symbol table (holds class based symbols) (DSP instances use ctx->stH) + cmDspStoreH_t dsH; // DSP system global variable storate table cmJsonH_t jsH; // DSP json for use by the system const cmChar_t* rsrcFn; // name of the JSON file containing resource specific resource _cmDspClass_t* classList; diff --git a/dsp/cmDspStore.c b/dsp/cmDspStore.c new file mode 100644 index 0000000..2019cdd --- /dev/null +++ b/dsp/cmDspStore.c @@ -0,0 +1,179 @@ +#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 "cmJson.h" +#include "cmDspValue.h" +#include "cmDspCtx.h" +#include "cmDspClass.h" +#include "cmDspStore.h" + +typedef struct +{ + unsigned symId; + cmDspValue_t value; +} cmDspStoreVar_t; + +typedef struct +{ + cmErr_t err; + cmDspStoreVar_t* array; + unsigned allocCnt; + unsigned growCnt; + unsigned curCnt; +} cmDspStore_t; + + +cmDspStoreH_t cmDspStoreNullHandle = cmSTATIC_NULL_HANDLE; + +cmDspStore_t* _cmDspStoreHandleToPtr( cmDspStoreH_t h ) +{ + cmDspStore_t* p = (cmDspStore_t*)h.h; + assert( p != NULL ); + return p; +} + +cmDspRC_t _cmDspStoreFree( cmDspStore_t* p ) +{ + cmMemFree(p->array); + cmMemFree(p); + return kOkDspRC; +} + + +cmDspRC_t cmDspStoreAlloc( cmCtx_t* ctx, cmDspStoreH_t* hp, unsigned initStoreCnt, unsigned growStoreCnt ) +{ + cmDspRC_t rc; + if((rc = cmDspStoreFree(hp)) != kOkDspRC ) + return rc; + + cmDspStore_t* p = cmMemAllocZ(cmDspStore_t,1); + + cmErrSetup(&p->err,&ctx->rpt,"cmDspStore"); + + p->array = cmMemAllocZ(cmDspStoreVar_t,initStoreCnt); + p->allocCnt = initStoreCnt; + p->curCnt = 0; + p->growCnt = growStoreCnt; + + hp->h = p; + + return rc; +} + +cmDspRC_t cmDspStoreFree( cmDspStoreH_t *hp ) +{ + cmDspRC_t rc = kOkDspRC; + if(hp==NULL || cmDspStoreIsValid(*hp)==false ) + return rc; + + cmDspStore_t* p = _cmDspStoreHandleToPtr(*hp); + + if((rc = _cmDspStoreFree(p)) != kOkDspRC ) + return rc; + + hp->h = NULL; + + return rc; +} + +bool cmDspStoreIsValid( cmDspStoreH_t h ) +{ return h.h; } + + +cmDspStoreVar_t* _cmDspStoreSymToPtr( cmDspStore_t* p, unsigned symId ) +{ + cmDspStoreVar_t* vp = p->array; + cmDspStoreVar_t* ep = p->array + p->curCnt; + for(; vpsymId == symId ) + return vp; + return NULL; +} + +cmDspStoreVar_t* _cmDspStoreIdToPtr( cmDspStore_t* p, unsigned id ) +{ + if( id < p->curCnt ) + return p->array + id; + + return NULL; +} + +cmDspStoreVar_t* _cmDspStoreAppend( cmDspStore_t* p ) +{ + cmDspStoreVar_t* vp = NULL; + + if( p->curCnt >= p->allocCnt ) + { + p->allocCnt += p->growCnt; + p->array = cmMemResizePZ(cmDspStoreVar_t,p->array,p->allocCnt); + } + + vp = p->array + p->curCnt; + + p->curCnt += 1; + + return vp; +} + +unsigned cmDspStoreSymToId( cmDspStoreH_t h, unsigned symId ) +{ + cmDspStore_t* p = _cmDspStoreHandleToPtr(h); + const cmDspStoreVar_t* vp; + if((vp = _cmDspStoreSymToPtr(p,symId)) == NULL ) + return cmInvalidId; + return vp - p->array; +} + +unsigned cmDspStoreIdToSym( cmDspStoreH_t h, unsigned id ) +{ + cmDspStore_t* p = _cmDspStoreHandleToPtr(h); + const cmDspStoreVar_t* vp; + if((vp = _cmDspStoreIdToPtr(p,id)) == NULL ) + return cmInvalidId; + return vp->symId; +} + +const cmDspValue_t* cmDspStoreIdToValue( cmDspStoreH_t h, unsigned id ) +{ + cmDspStore_t* p = _cmDspStoreHandleToPtr(h); + const cmDspStoreVar_t* vp; + if((vp = _cmDspStoreIdToPtr(p,id)) == NULL ) + return NULL; + return &vp->value; +} + +cmDspRC_t cmDspStoreSetValueViaId( cmDspStoreH_t h, unsigned id, const cmDspValue_t* val ) +{ + cmDspRC_t rc = kOkDspRC; + cmDspStore_t* p = _cmDspStoreHandleToPtr(h); + cmDspStoreVar_t* vp = NULL; + + if((vp = _cmDspStoreIdToPtr(p,id)) == NULL ) + return cmErrMsg(&p->err,kVarNotFoundDspRC,"There is not global variable at with id:%i\n",id); + + cmDsvCopy(&vp->value,val); + return rc; +} + +unsigned cmDspStoreSetValueViaSym( cmDspStoreH_t h, unsigned symId, const cmDspValue_t* val ) +{ + cmDspStore_t* p = _cmDspStoreHandleToPtr(h); + cmDspStoreVar_t* vp = NULL; + + if((vp = _cmDspStoreSymToPtr(p,symId)) == NULL ) + vp = _cmDspStoreAppend(p); + + assert(vp != NULL ); + + cmDsvCopy(&vp->value,val); + vp->symId = symId; + + return vp - p->array; +} diff --git a/dsp/cmDspStore.h b/dsp/cmDspStore.h new file mode 100644 index 0000000..292c047 --- /dev/null +++ b/dsp/cmDspStore.h @@ -0,0 +1,30 @@ +#ifndef cmDspStore_h +#define cmDspStore_h + +#ifdef __cplusplus +extern "C" { +#endif + + extern cmDspStoreH_t cmDspStoreNullHandle; + + cmDspRC_t cmDspStoreAlloc( cmCtx_t* ctx, cmDspStoreH_t* hp, unsigned initStoreCnt, unsigned growStoreCnt ); + + cmDspRC_t cmDspStoreFree( cmDspStoreH_t *hp ); + + bool cmDspStoreIsValid( cmDspStoreH_t h ); + + unsigned cmDspStoreSymToId( cmDspStoreH_t h, unsigned symId ); + unsigned cmDspStoreIdToSym( cmDspStoreH_t h, unsigned id ); + const cmDspValue_t* cmDspStoreIdToValue( cmDspStoreH_t h, unsigned id ); + + cmDspRC_t cmDspStoreSetValueViaId( cmDspStoreH_t h, unsigned id, const cmDspValue_t* val ); + + // Sets the variable to the value (and creates it if it does not exist). + // Returns the 'id' of the variable. + unsigned cmDspStoreSetValueViaSym( cmDspStoreH_t h, unsigned symId, const cmDspValue_t* val ); + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/dsp/cmDspSys.c b/dsp/cmDspSys.c index 06ee2a3..5d3fc73 100644 --- a/dsp/cmDspSys.c +++ b/dsp/cmDspSys.c @@ -21,6 +21,7 @@ #include "cmProcObj.h" #include "cmDspCtx.h" #include "cmDspClass.h" +#include "cmDspStore.h" #include "cmDspSys.h" #include "cmDspBuiltIn.h" #include "cmDspPgm.h" @@ -140,6 +141,8 @@ cmDspRC_t _cmDspSysFinalize( cmDsp_t* p ) if( cmCtxFree(&p->ctx.cmProcCtx) != cmOkRC ) rc = cmErrMsg(&p->err,kProcFailDspRC,"The proc context finalizatoin failed."); + cmDspStoreFree(&p->dsH); + if( cmSymTblIsValid(p->stH) ) cmSymTblDestroy(&p->stH); @@ -195,6 +198,13 @@ cmDspRC_t cmDspSysInitialize( cmCtx_t* ctx, cmDspSysH_t* hp, cmUdpNetH_t netH ) goto errLabel; } + // allocate the DSP system variable storage object + if( cmDspStoreAlloc(ctx,&p->dsH,10,10) != kOkDspRC ) + { + rc = cmErrMsg(&p->err,kDspStoreFailDspRC,"DSP store allocation failed."); + goto errLabel; + } + // initialize the proc context if( (p->ctx.cmProcCtx = cmCtxAlloc(NULL,&ctx->rpt,p->lhH,p->stH)) == NULL ) { @@ -214,6 +224,7 @@ cmDspRC_t cmDspSysInitialize( cmCtx_t* ctx, cmDspSysH_t* hp, cmUdpNetH_t netH ) p->ctx.lhH = p->lhH; p->ctx.jsH = p->jsH; p->ctx.stH = p->stH; + p->ctx.dsH = p->dsH; p->ctx.rsrcJsH = cmJsonNullHandle; p->ctx.rpt = &ctx->rpt; p->ctx.cmCtx = &p->cmCtx;