From da34a3e4a514bfb171ee559a386103fd5dfb8bd1 Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 6 Aug 2015 18:18:15 -0400 Subject: [PATCH 1/6] cmProc5.h/c : Added cmReflectCalcWrite(). --- cmProc5.c | 28 +++++++++++++++++++++++++++- cmProc5.h | 8 ++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cmProc5.c b/cmProc5.c index a0f6449..af156db 100644 --- a/cmProc5.c +++ b/cmProc5.c @@ -694,6 +694,8 @@ cmRC_t cmReflectCalcFree( cmReflectCalc_t** pp ) cmReflectCalc_t* p = *pp; + cmReflectCalcWrite(p,"/Users/kevin/temp/cmkc"); + if((rc = cmReflectCalcFinal(p)) != cmOkRC ) return rc; @@ -755,9 +757,12 @@ cmRC_t cmReflectCalcFinal( cmReflectCalc_t* p ) return cmOkRC; } -cmRC_t cmReflectCalcExec( cmReflectCalc_t* p, const cmSample_t xV, cmSample_t* yV, unsigned xyN ) +cmRC_t cmReflectCalcExec( cmReflectCalc_t* p, const cmSample_t* xV, cmSample_t* yV, unsigned xyN ) { unsigned i; + + // feed audio into the PHAT's buffer + cmPhatExec(p->phat,xV,xyN); for(i=0; ixi) { @@ -766,12 +771,16 @@ cmRC_t cmReflectCalcExec( cmReflectCalc_t* p, const cmSample_t xV, cmSample_t* y else yV[i] = 0; + // if the PHAT has a complete buffer if( p->xi == p->phat->fhN ) { p->xi = 0; + // execute the correlation cmPhatChExec(p->phat,0,0,0); + // p->phat->xV now holds the correlation result + if( p->va != NULL ) cmVectArrayAppendS(p->va,p->phat->xV,p->phat->fhN ); } @@ -781,3 +790,20 @@ cmRC_t cmReflectCalcExec( cmReflectCalc_t* p, const cmSample_t xV, cmSample_t* y return cmOkRC; } + +cmRC_t cmReflectCalcWrite( cmReflectCalc_t* p, const char* dirStr ) +{ + cmRC_t rc = cmOkRC; + + if( p->va != NULL) + { + const char* path = NULL; + + if((rc = cmVectArrayWrite(p->va, path = cmFsMakeFn(path,"reflect_calc","va",dirStr,NULL) )) != cmOkRC ) + rc = cmCtxRtCondition(&p->obj,cmSubSysFailRC,"Reflect calc file write failed."); + + cmFsFreeFn(path); + } + + return rc; +} diff --git a/cmProc5.h b/cmProc5.h index 84719b5..34f367c 100644 --- a/cmProc5.h +++ b/cmProc5.h @@ -208,11 +208,11 @@ extern "C" { cmReflectCalc_t* cmReflectCalcAlloc( cmCtx* ctx, cmReflectCalc_t* p, const cmGoldSigArg_t* gsa, float phat_alpha, unsigned phat_mult ); - cmRC_t cmReflectCalcFree( cmReflectCalc_t** pp ); - cmRC_t cmReflectCalcInit( cmReflectCalc_t* p, const cmGoldSigArg_t* gsa, float phat_alpha, unsigned phat_mult ); + cmRC_t cmReflectCalcFree( cmReflectCalc_t** pp ); + cmRC_t cmReflectCalcInit( cmReflectCalc_t* p, const cmGoldSigArg_t* gsa, float phat_alpha, unsigned phat_mult ); cmRC_t cmReflectCalcFinal( cmReflectCalc_t* p ); - cmRC_t cmReflectCalcExec( cmReflectCalc_t* p, const cmSample_t xV, cmSample_t* yV, unsigned xyN ); - + cmRC_t cmReflectCalcExec( cmReflectCalc_t* p, const cmSample_t* xV, cmSample_t* yV, unsigned xyN ); + cmRC_t cmReflectCalcWrite( cmReflectCalc_t* p, const char* dirStr ); #ifdef __cplusplus From 537ad58c392d94dbb589b2746b8383ba6d716c62 Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 6 Aug 2015 18:19:01 -0400 Subject: [PATCH 2/6] cmDspKr.h/c : Added initial implementation of cmReflectCalc. --- dsp/cmDspKr.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++ dsp/cmDspKr.h | 1 + 2 files changed, 158 insertions(+) diff --git a/dsp/cmDspKr.c b/dsp/cmDspKr.c index 3b0ff10..325bf63 100644 --- a/dsp/cmDspKr.c +++ b/dsp/cmDspKr.c @@ -3587,3 +3587,160 @@ struct cmDspClass_str* cmTakeSeqRendClassCons( cmDspCtx_t* ctx ) return &_cmTakeSeqRendDC; } + +//========================================================================================================================================== +enum +{ + kLfsrN_RcId, + kMlsCoeff0RcId, + kMlsCoeff1RcId, + kSmpPerChipRcId, + kRcosBetaRcId, + kRcosOS_RcId, + kCarrierHzRcId, + kAtkDcyMsRcId, + kPhatAlphaRcId, + kPhatMultRcId, + kInRcId, + kOutRcId, +}; + +cmDspClass_t _cmReflectCalcDC; + +typedef struct +{ + cmDspInst_t inst; + cmReflectCalc_t* r; +} cmDspReflectCalc_t; + + +cmDspInst_t* _cmDspReflectCalcAlloc(cmDspCtx_t* ctx, cmDspClass_t* classPtr, unsigned storeSymId, unsigned instSymId, unsigned id, unsigned va_cnt, va_list vl ) +{ + + /* + if( va_cnt !=3 ) + { + cmDspClassErr(ctx,classPtr,kVarArgParseFailDspRC,"The 'ReflectCalc' constructor must have two arguments: a channel count and frequency array."); + return NULL; + } + */ + + cmDspReflectCalc_t* p = cmDspInstAllocV(cmDspReflectCalc_t,ctx,classPtr,instSymId,id,storeSymId,va_cnt,vl, + 1, "lfsrN", kLfsrN_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Gold code generator LFSR length", + 1, "mlsc0", kMlsCoeff0RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0", + 1, "mlsc1", kMlsCoeff1RcId, 0,0, kInDsvFl | kUIntDsvFl, "LFSR coefficient 0", + 1, "spchip",kSmpPerChipRcId,0,0, kInDsvFl | kUIntDsvFl, "Samples per spreading code bit.", + 1, "rcosb", kRcosBetaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Raised cosine beta", + 1, "rcosos",kRcosOS_RcId, 0,0, kInDsvFl | kUIntDsvFl, "Raised cosine oversample factor.", + 1, "carhz", kCarrierHzRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Carrier frequency in Hertz.", + 1, "envms", kAtkDcyMsRcId, 0,0, kInDsvFl | kDoubleDsvFl, "Signal Attack/Decay milliseconds.", + 1, "alpha", kPhatAlphaRcId, 0,0, kInDsvFl | kDoubleDsvFl, "PHAT alpha coefficient.", + 1, "mult", kPhatMultRcId, 0,0, kInDsvFl | kUIntDsvFl, "PHAT multiplier coefficient.", + 1, "in", kInRcId, 0,1, kInDsvFl | kAudioBufDsvFl,"Audio input", + 1, "out", kOutRcId, 0,1, kOutDsvFl | kAudioBufDsvFl,"Audio output", + 0 ); + + +p->r = cmReflectCalcAlloc(ctx->cmProcCtx, NULL, NULL, 0, 0 ); + + cmDspSetDefaultUInt( ctx, &p->inst, kLfsrN_RcId, 0, 8); + cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff0RcId, 0, 0x8e); + cmDspSetDefaultUInt( ctx, &p->inst, kMlsCoeff1RcId, 0, 0x96); + cmDspSetDefaultUInt( ctx, &p->inst, kSmpPerChipRcId, 0, 64); + cmDspSetDefaultDouble( ctx, &p->inst, kRcosBetaRcId, 0, 0.5); + cmDspSetDefaultUInt( ctx, &p->inst, kRcosOS_RcId, 0, 4); + cmDspSetDefaultDouble( ctx, &p->inst, kCarrierHzRcId, 0, 2500.0); + cmDspSetDefaultDouble( ctx, &p->inst, kAtkDcyMsRcId, 0, 50.0); + cmDspSetDefaultDouble( ctx, &p->inst, kPhatAlphaRcId, 0, 0.5); + cmDspSetDefaultUInt( ctx, &p->inst, kPhatMultRcId, 0, 4); + + return &p->inst; +} + +cmDspRC_t _cmDspReflectCalcSetup( cmDspCtx_t* ctx, cmDspInst_t* inst ) +{ + cmDspRC_t rc = kOkDspRC; + cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst; + cmGoldSigArg_t gsa; + + gsa.chN = 1; + gsa.srate = cmDspSampleRate(ctx); + gsa.lfsrN = cmDspUInt(inst,kLfsrN_RcId); + gsa.mlsCoeff0 = cmDspUInt(inst,kMlsCoeff0RcId); + gsa.mlsCoeff1 = cmDspUInt(inst,kMlsCoeff1RcId); + gsa.samplesPerChip = cmDspUInt(inst,kSmpPerChipRcId); + gsa.rcosBeta = cmDspDouble(inst,kRcosBetaRcId); + gsa.rcosOSFact = cmDspUInt(inst,kRcosOS_RcId); + gsa.carrierHz = cmDspDouble(inst,kCarrierHzRcId); + gsa.envMs = cmDspDouble(inst,kAtkDcyMsRcId); + + double phatAlpha = cmDspDouble(inst,kPhatAlphaRcId); + unsigned phatMult = cmDspUInt(inst,kPhatMultRcId); + + if( cmReflectCalcInit(p->r,&gsa,phatAlpha,phatMult) != cmOkRC ) + rc = cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Unable to initialize the internal ReflectCalc detector."); + + return rc; +} + +cmDspRC_t _cmDspReflectCalcFree(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt ) +{ + cmDspRC_t rc = kOkDspRC; + cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst; + + cmReflectCalcFree(&p->r); + + return rc; +} + +cmDspRC_t _cmDspReflectCalcReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt ) +{ +//cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst; + + cmDspApplyAllDefaults(ctx,inst); + + return _cmDspReflectCalcSetup(ctx, inst ); +} + +cmDspRC_t _cmDspReflectCalcExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt ) +{ + cmDspRC_t rc = kOkDspRC; + cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst; + const cmSample_t* xV = cmDspAudioBuf(ctx,inst,kInRcId,0); + unsigned xN = cmDspAudioBufSmpCount(ctx,inst,kInRcId,0); + cmSample_t* yV = cmDspAudioBuf(ctx,inst,kOutRcId,0); + unsigned yN = cmDspAudioBufSmpCount(ctx,inst,kOutRcId,0); + + if( xV != NULL && yV != NULL ) + { + assert( xN == yN ); + cmReflectCalcExec(p->r,xV,yV,xN); + } + + return rc; +} + +cmDspRC_t _cmDspReflectCalcRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt ) +{ +//cmDspReflectCalc_t* p = (cmDspReflectCalc_t*)inst; + + cmDspSetEvent(ctx,inst,evt); + + return kOkDspRC; +} + +struct cmDspClass_str* cmReflectCalcClassCons( cmDspCtx_t* ctx ) +{ + cmDspClassSetup(&_cmReflectCalcDC,ctx,"ReflectCalc", + NULL, + _cmDspReflectCalcAlloc, + _cmDspReflectCalcFree, + _cmDspReflectCalcReset, + _cmDspReflectCalcExec, + _cmDspReflectCalcRecv, + NULL, + NULL, + "Reflecttion time calculator"); + + return &_cmReflectCalcDC; +} diff --git a/dsp/cmDspKr.h b/dsp/cmDspKr.h index 13459be..e8ac4e6 100644 --- a/dsp/cmDspKr.h +++ b/dsp/cmDspKr.h @@ -21,6 +21,7 @@ extern "C" { struct cmDspClass_str* cmSyncRecdClassCons( cmDspCtx_t* ctx ); struct cmDspClass_str* cmTakeSeqBldrClassCons( cmDspCtx_t* ctx ); struct cmDspClass_str* cmTakeSeqRendClassCons( cmDspCtx_t* ctx ); + struct cmDspClass_str* cmReflectCalcClassCons( cmDspCtx_t* ctx ); #ifdef __cplusplus } From b371733c9a2a396dabeb284c4599c516c20dfa1e Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 6 Aug 2015 18:19:35 -0400 Subject: [PATCH 3/6] cmDspBuiltIn.c : Registered cmReflectCalcClassCons. --- dsp/cmDspBuiltIn.c | 1 + 1 file changed, 1 insertion(+) diff --git a/dsp/cmDspBuiltIn.c b/dsp/cmDspBuiltIn.c index 37e3680..0ef37b2 100644 --- a/dsp/cmDspBuiltIn.c +++ b/dsp/cmDspBuiltIn.c @@ -5837,6 +5837,7 @@ cmDspClassConsFunc_t _cmDspClassBuiltInArray[] = cmSyncRecdClassCons, cmTakeSeqBldrClassCons, cmTakeSeqRendClassCons, + cmReflectCalcClassCons, NULL, }; From a23c26f46bea4e678880edee26386eb2693190bd Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 6 Aug 2015 18:20:38 -0400 Subject: [PATCH 4/6] cmDspPgm.c : Added _cmDspSysPgm_ReflectCalc(). --- dsp/cmDspPgm.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/dsp/cmDspPgm.c b/dsp/cmDspPgm.c index 0a67855..904db83 100644 --- a/dsp/cmDspPgm.c +++ b/dsp/cmDspPgm.c @@ -26,6 +26,44 @@ #include "cmDspPgmPP.h" #include "cmDspPgmKr.h" + +cmDspRC_t _cmDspSysPgm_ReflectCalc( cmDspSysH_t h, void** userPtrPtr ) +{ + + // audio inputs + cmDspInst_t* ai0 = cmDspSysAllocInst(h,"AudioIn", NULL, 1, 0 ); + cmDspInst_t* ai1 = cmDspSysAllocInst(h,"AudioIn", NULL, 1, 1 ); + + // audio outputs + cmDspInst_t* ao0 = cmDspSysAllocInst(h,"AudioOut",NULL, 1, 0 ); + cmDspInst_t* ao1 = cmDspSysAllocInst(h,"AudioOut",NULL, 1, 1 ); + + // input meters + cmDspInst_t* im0 = cmDspSysAllocInst(h,"AMeter","In 0", 0); + cmDspInst_t* im1 = cmDspSysAllocInst(h,"AMeter","In 1", 0); + + // output meters + //cmDspInst_t* om0 = cmDspSysAllocInst(h,"AMeter","Out 0", 0); + //cmDspInst_t* om1 = cmDspSysAllocInst(h,"AMeter","Out 1",0); + + // gain controls + cmDspInst_t* igain = cmDspSysAllocInst( h,"Scalar", "In Gain", 5, kNumberDuiId, 0.0, 4.0, 0.01, 1.0); + cmDspInst_t* ogain = cmDspSysAllocInst( h,"Scalar", "Out Gain", 5, kNumberDuiId, 0.0, 4.0, 0.01, 1.0); + + cmDspSysConnectAudio(h, ai0, "out", im0, "in"); // ain0 -> imtr0 + cmDspSysConnectAudio(h, ai1, "out", im1, "in"); // ain1 -> imtr1 + + cmDspSysInstallCb( h, igain,"val", ai0, "gain", NULL); // igain -> ain0 + cmDspSysInstallCb( h, igain,"val", ai1, "gain", NULL); // igain -> ain0 + + cmDspSysInstallCb( h, ogain,"val", ao0, "gain", NULL); // igain -> ain0 + cmDspSysInstallCb( h, ogain,"val", ao1, "gain", NULL); // igain -> ain0 + + cmDspSysConnectAudio(h, ai0,"out", ao0, "in" ); // ain0 -> aout0 + cmDspSysConnectAudio(h, ai1,"out", ao1, "in" ); // ain1 -> aout1 + return kOkDspRC; +} + cmDspRC_t _cmDspSysPgm_SyncRecd( cmDspSysH_t h, void** userPtrPtr ) { cmDspRC_t rc = kOkDspRC; @@ -2834,7 +2872,7 @@ cmDspRC_t _cmDspSysPgm_BinEnc( cmDspSysH_t h, void** userPtrPtr ) double maxDurMs = 60000.0; double azimBeg = 0.0; double azimEnd = 360.0; - const char* fn1 = "media/audio/sourcetone/Jazz/Ella & Louis - Under A Blanket Of Blue"; + //const char* fn1 = "media/audio/sourcetone/Jazz/Ella & Louis - Under A Blanket Of Blue"; const char* fn0 = "temp/comhear/drw/monty.wav"; cmDspInst_t* twod = cmDspSysAllocInst( h, "twod", NULL, 0); @@ -2901,8 +2939,7 @@ cmDspRC_t _cmDspSysPgm_BinEnc( cmDspSysH_t h, void** userPtrPtr ) _cmDspSysPgm_t _cmDspSysPgmArray[] = { - { "two-d", _cmDspSysPgm_TwoD, NULL, NULL }, - { "bin-enc", _cmDspSysPgm_BinEnc, NULL, NULL }, + { "reflect", _cmDspSysPgm_ReflectCalc, NULL, NULL }, { "tksb", _cmDspSysPgm_Tksb, NULL, NULL }, { "time_line", _cmDspSysPgm_TimeLine, NULL, NULL }, { "seq-bldr", _cmDspSysPgm_TakeSeqBldr, NULL, NULL }, @@ -2951,6 +2988,8 @@ _cmDspSysPgm_t _cmDspSysPgmArray[] = { "scalar op", _cmDspSysPgm_ScalarOp, NULL, NULL }, { "seg_line", _cmDspSysPgm_SegLine, NULL, NULL }, { "avail_ch", _cmDspSysPgm_AvailCh, NULL, NULL }, + { "two-d", _cmDspSysPgm_TwoD, NULL, NULL }, + { "bin-enc", _cmDspSysPgm_BinEnc, NULL, NULL }, { NULL , NULL, NULL, NULL } }; From 77edcafc7677c682db56a2446ebb7abfe61e9d23 Mon Sep 17 00:00:00 2001 From: Kevin Larke Date: Fri, 7 Aug 2015 13:24:05 -0700 Subject: [PATCH 5/6] cmProc5.c : Fixed bugs in cmReflectCalcAlloc(). --- cmProc5.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmProc5.c b/cmProc5.c index af156db..36fc158 100644 --- a/cmProc5.c +++ b/cmProc5.c @@ -656,14 +656,14 @@ cmReflectCalc_t* cmReflectCalcAlloc( cmCtx* ctx, cmReflectCalc_t* p, const cmGol cmRC_t rc = cmOkRC; // allocate the Gold code signal generator - if( (p->gs = cmGoldSigAlloc(ctx,NULL,NULL)) == NULL ) + if( (op->gs = cmGoldSigAlloc(ctx,NULL,NULL)) == NULL ) { rc = cmCtxRtCondition(&p->obj,cmSubSysFailRC,"Gold sig allocate failed."); goto errLabel; } // allocate the PHAT object - if( (p->phat = cmPhatAlloc(ctx,NULL,0,0,0,0,0)) == NULL ) + if( (op->phat = cmPhatAlloc(ctx,NULL,0,0,0,0,0)) == NULL ) { rc = cmCtxRtCondition(&p->obj,cmSubSysFailRC,"PHAT allocate failed."); goto errLabel; @@ -694,7 +694,7 @@ cmRC_t cmReflectCalcFree( cmReflectCalc_t** pp ) cmReflectCalc_t* p = *pp; - cmReflectCalcWrite(p,"/Users/kevin/temp/cmkc"); + cmReflectCalcWrite(p,"/home/kevin/temp/cmkc"); if((rc = cmReflectCalcFinal(p)) != cmOkRC ) return rc; From 1953a676acf2b79d3895516f6a98646219559eb1 Mon Sep 17 00:00:00 2001 From: Kevin Larke Date: Fri, 7 Aug 2015 13:24:55 -0700 Subject: [PATCH 6/6] cmDspPgm.c : Changed _cmDspSysPgm_ReflectCalc() to actually use cmReflectCalc. --- dsp/cmDspPgm.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/dsp/cmDspPgm.c b/dsp/cmDspPgm.c index 904db83..9397504 100644 --- a/dsp/cmDspPgm.c +++ b/dsp/cmDspPgm.c @@ -34,6 +34,9 @@ cmDspRC_t _cmDspSysPgm_ReflectCalc( cmDspSysH_t h, void** userPtrPtr ) cmDspInst_t* ai0 = cmDspSysAllocInst(h,"AudioIn", NULL, 1, 0 ); cmDspInst_t* ai1 = cmDspSysAllocInst(h,"AudioIn", NULL, 1, 1 ); + cmDspInst_t* rc0 = cmDspSysAllocInst(h,"ReflectCalc", NULL, 0 ); + //cmDspInst_t* rc1 = cmDspSysAllocInst(h,"ReflectCalc", NULL, 0 ); + // audio outputs cmDspInst_t* ao0 = cmDspSysAllocInst(h,"AudioOut",NULL, 1, 0 ); cmDspInst_t* ao1 = cmDspSysAllocInst(h,"AudioOut",NULL, 1, 1 ); @@ -59,8 +62,12 @@ cmDspRC_t _cmDspSysPgm_ReflectCalc( cmDspSysH_t h, void** userPtrPtr ) cmDspSysInstallCb( h, ogain,"val", ao0, "gain", NULL); // igain -> ain0 cmDspSysInstallCb( h, ogain,"val", ao1, "gain", NULL); // igain -> ain0 - cmDspSysConnectAudio(h, ai0,"out", ao0, "in" ); // ain0 -> aout0 - cmDspSysConnectAudio(h, ai1,"out", ao1, "in" ); // ain1 -> aout1 + + cmDspSysConnectAudio(h, ai0,"out", rc0, "in" ); + cmDspSysConnectAudio(h, rc0,"out", ao0, "in" ); + + cmDspSysConnectAudio(h, ai1,"out", ao1, "in" ); + return kOkDspRC; }