diff --git a/cmProc4.c b/cmProc4.c index db959a6..da283fc 100644 --- a/cmProc4.c +++ b/cmProc4.c @@ -25,11 +25,11 @@ -cmScFol* cmScFolAlloc( cmCtx* c, cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH ) +cmScFol* cmScFolAlloc( cmCtx* c, cmScFol* p, cmReal_t srate, unsigned bufN, cmReal_t wndMs, cmScH_t scH ) { cmScFol* op = cmObjAlloc(cmScFol,c,p); if( srate != 0 ) - if( cmScFolInit(op,srate,wndN,wndMs,scH) != cmOkRC ) + if( cmScFolInit(op,srate,bufN,wndMs,scH) != cmOkRC ) cmScFolFree(&op); return op; } @@ -62,14 +62,14 @@ void _cmScFolPrint( cmScFol* p ) int i,j; for(i=0; ilocN; ++i) { - printf("%2i %5i ",p->loc[i].barNumb,p->loc[i].evtIdx); + printf("%2i %5i ",p->loc[i].barNumb,p->loc[i].scIdx); for(j=0; jloc[i].pitchCnt; ++j) printf("%s ",cmMidiToSciPitch(p->loc[i].pitchV[j],NULL,0)); printf("\n"); } } -cmRC_t cmScFolInit( cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH ) +cmRC_t cmScFolInit( cmScFol* p, cmReal_t srate, unsigned bufN, cmReal_t wndMs, cmScH_t scH ) { cmRC_t rc; if((rc = cmScFolFinal(p)) != cmOkRC ) @@ -77,22 +77,19 @@ cmRC_t cmScFolInit( cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs p->srate = srate; p->scH = scH; - p->maxWndSmp = floor(wndMs * srate / 1000.0); - p->wndN = wndN; - p->wndV = cmMemResizeZ(cmScFolWndEle_t,p->wndV,wndN); + p->bufN = bufN; + p->bufV = cmMemResizeZ(cmScFolBufEle_t,p->bufV,bufN); p->locN = cmScoreEvtCount(scH); p->loc = cmMemResizeZ(cmScFolLoc_t,p->loc,p->locN); - p->sri = cmInvalidIdx; p->sbi = cmInvalidIdx; p->sei = cmInvalidIdx; - p->edWndMtx = cmVOU_LevEditDistAllocMtx(p->wndN); - p->evalWndN = 5; - p->allowedMissCnt = 1; + p->msln = 7; + p->mswn = 30; + p->edWndMtx = cmVOU_LevEditDistAllocMtx(p->bufN); - assert(p->evalWndNwndN); int i,n; - double maxDSecs = 0; // max time between events to be considered simultaneous + double maxDSecs = 0; // max time between score entries to be considered simultaneous cmScoreEvt_t* e0p = NULL; int j0 = 0; @@ -106,7 +103,7 @@ cmRC_t cmScFolInit( cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs { assert( j0+n < p->locN ); - p->loc[j0+n].evtIdx = i; + p->loc[j0+n].scIdx = i; p->loc[j0+n].barNumb = ep->barNumb; // if the first event has not yet been selected @@ -141,7 +138,7 @@ cmRC_t cmScFolInit( cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs for(m=0; mloc[j0+m].evtIdx); + cmScoreEvt_t* tp = cmScoreEvt(scH,p->loc[j0+m].scIdx); assert(tp!=NULL); p->loc[j0+k].pitchV[m] = tp->pitch; } @@ -166,43 +163,34 @@ cmRC_t cmScFolReset( cmScFol* p, unsigned scoreIndex ) { int i; - // zero the event index - memset(p->wndV,0,sizeof(cmScFolWndEle_t)*p->wndN); + + // empty the event buffer + memset(p->bufV,0,sizeof(cmScFolBufEle_t)*p->bufN); // don't allow the score index to be prior to the first note - if( scoreIndex < p->loc[0].evtIdx ) - scoreIndex = p->loc[0].evtIdx; + if( scoreIndex < p->loc[0].scIdx ) + scoreIndex = p->loc[0].scIdx; + + p->sei = cmInvalidIdx; + p->sbi = cmInvalidIdx; // locate the score element in svV[] that is closest to, // and possibly after, scoreIndex. for(i=0; ilocN-1; ++i) - if( p->loc[i].evtIdx <= scoreIndex && scoreIndex < p->loc[i+1].evtIdx ) - break; - - // force scEvtIndex to be valid - assert( ilocN ); - - p->sri = i; - p->sbi = i; - - // score event window is dBar bars before and after scEvtIndex; - int dBar = 1; - - // backup dBar bars from the 'scoreIndex' - for(; i>=0; --i) - if( p->loc[i].barNumb >= (p->loc[p->sri].barNumb-dBar) ) + if( p->loc[i].scIdx <= scoreIndex && scoreIndex < p->loc[i+1].scIdx ) + { p->sbi = i; - else break; - - dBar = 3; + } - // move forward dBar bars from 'scoreIndex' - for(i=p->sri; ilocN; ++i) - if( p->loc[i].barNumb <= (p->loc[p->sri].barNumb+dBar) ) + // locate the score element at the end of the look-ahead region + for(; ilocN-1; ++i) + if( p->loc[i].scIdx <= scoreIndex + p->msln && scoreIndex + p->msln < p->loc[i+1].scIdx ) + { p->sei = i; - else break; + } + return cmOkRC; } @@ -244,7 +232,7 @@ int _cmScFolDist(unsigned mtxMaxN, unsigned* m, const unsigned* s1, const cmScFo return v; } - +/* unsigned cmScFolExec( cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 ) { assert( p->sri != cmInvalidIdx ); @@ -266,7 +254,8 @@ unsigned cmScFolExec( cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByt int en = 0; for(; i>=0; --i,++en) { - if( p->wndV[i].validFl /*&& ((smpIdx-p->wnd[i].smpIdx)<=maxWndSmp)*/) + //if( p->wndV[i].validFl && ((smpIdx-p->wnd[i].smpIdx)<=maxWndSmp)) + if( p->wndV[i].validFl ) ewnd[i] = p->wndV[i].val; else break; @@ -306,7 +295,8 @@ unsigned cmScFolExec( cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByt // a successful match has <= allowedMissCnt and an exact match on the last element //if( dist <= p->allowedMissCnt && ewnd[p->wndN-1] == p->loc[p->sbi+minIdx+en-1] ) - if( /*dist <= p->allowedMissCnt &&*/ _cmScFolIsMatch(p->loc+(p->sbi+minIdx+en-1),ewnd[p->wndN-1])) + //if( dist <= p->allowedMissCnt && _cmScFolIsMatch(p->loc+(p->sbi+minIdx+en-1),ewnd[p->wndN-1])) + if( _cmScFolIsMatch(p->loc+(p->sbi+minIdx+en-1),ewnd[p->wndN-1])) { p->sbi = p->sbi + minIdx; p->sei = cmMin(p->sei+minIdx,p->locN-1); @@ -317,3 +307,96 @@ unsigned cmScFolExec( cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByt return ret_idx; } +*/ + +unsigned cmScFolExec( cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 ) +{ + + unsigned ret_idx = cmInvalidIdx; + unsigned ebuf[ p->bufN ]; + + if( status != kNoteOnMdId && d1>0 ) + return ret_idx; + + // left shift bufV[] to make the right-most element available - then copy in the new element + memmove(p->bufV, p->bufV+1, sizeof(cmScFolBufEle_t)*(p->bufN-1)); + p->bufV[ p->bufN-1 ].smpIdx = smpIdx; + p->bufV[ p->bufN-1 ].val = d0; + p->bufV[ p->bufN-1 ].validFl= true; + + // fill in ebuf[] with the valid values in bufV[] + int i = p->bufN-1; + int en = 0; + for(; i>=0; --i,++en) + { + if( p->bufV[i].validFl) + ebuf[i] = p->bufV[i].val; + else + break; + } + ++i; // increment i to the first valid element in ebuf[]. + + + // en is the count of valid elements in ebuf[]. + // ebuf[p->boi] is the first valid element + + int j = 0; + int minDist = INT_MAX; + int minIdx = cmInvalidIdx; + int dist; + + // the score wnd must always be as long as the buffer n + // at the end of the score this may not be the case + // (once sei hits locN - at this point we must begin + // shrinking ewnd[] to contain only the last p->sei-p->sbi+1 elements) + assert( p->sei-p->sbi+1 >= en ); + + for(j=0; p->sbi+en+j <= p->sei; ++j) + if((dist = _cmScFolDist(p->bufN, p->edWndMtx, ebuf+i, p->loc + p->sbi+j, en )) < minDist ) + { + minDist = dist; + minIdx = j; + } + + // The best fit is on the score window: p->loc[sbi+minIdx : sbi+minIdx+en-1 ] + + // if a perfect match occurred + if( minDist == 0 ) + { + // we had a perfect match - shrink the window to it's minumum size + p->sbi += (en==p->bufN) ? minIdx + 1 : 0; // move wnd begin forward to just past first match + p->sei = p->sbi + minIdx + en + p->msln; // move wnd end forward to just past last match + ret_idx = p->sbi + minIdx + en - 1; + // BUG BUG BUG - is the window length valid - + // - sbi and sei must be inside 0:locN + // - sei-sbi + 1 must be >= en + } + else + { + // if the last event matched - then return the match location as the current score location + if( _cmScFolIsMatch(p->loc+(p->sbi+minIdx+en-1),ebuf[p->bufN-1]) ) + ret_idx = p->sbi + minIdx + en - 1; + + // even though we didn't match move the end of the score window forward + // this will enlarge the score window by one + p->sei += 1; + + assert( p->sei > p->sbi ); + + // if the score window length surpasses the max score window size + // move the beginning index forward + if( p->sei - p->sbi + 1 > p->mswn && p->sei > p->mswn ) + p->sbi = p->sei - p->mswn + 1; + + // BUG BUG BUG - is the window length valid - + // - sbi and sei must be inside 0:locN + // - sei-sbi + 1 must be >= en + + } + + // BUG BUG BUG - this is not currently guarded against + assert( p->sei < p->locN ); + assert( p->sbi < p->locN ); + + return ret_idx; +} diff --git a/cmProc4.h b/cmProc4.h index a2d1748..b0ece31 100644 --- a/cmProc4.h +++ b/cmProc4.h @@ -13,17 +13,17 @@ typedef struct cmMidiByte_t val; // bool validFl; // -} cmScFolWndEle_t; +} cmScFolBufEle_t; typedef struct { unsigned pitchCnt; // unsigned* pitchV; // pitchV[pitchCnt] - unsigned evtIdx; // index of the score event (into cmScoreEvt[]) at this location + unsigned scIdx; // index of the score event (into cmScoreEvt[]) at this location int barNumb; // bar number of this location } cmScFolLoc_t; - + /* typedef struct { cmObj obj; @@ -31,7 +31,7 @@ typedef struct cmScH_t scH; unsigned maxWndSmp; unsigned wndN; - cmScFolWndEle_t* wndV; // wnd[wndN] + cmScFolWndEle_t* wndV; // wnd[wndN] int wni; // oldest value in wnd[] int locN; cmScFolLoc_t* loc; @@ -44,14 +44,37 @@ typedef struct unsigned* edWndMtx; +} cmScFol1; + */ + +typedef struct +{ + cmObj obj; + cmReal_t srate; // + cmScH_t scH; // score handle + unsigned bufN; // event buffer count + cmScFolBufEle_t* bufV; // event buffer bufV[bufN] - bufV[bufN-1] newest event, bufV[boi] oldest event + int locN; // count of score locations + cmScFolLoc_t* loc; // score loc[locN] + unsigned sbi; // oldest score window index + unsigned sei; // newest score window index + unsigned msln; // minimum score look ahead count + unsigned mswn; // maximum score window length + + //unsigned evalWndN; // (dflt:5) count of elements to use for refined match window + //unsigned allowedMissCnt; // (dflt:1) count of non-match elements in refined match where a match is still signaled + + unsigned* edWndMtx; + } cmScFol; -// wndN = max count of elements in the score following window. - // wndMs = max length of the score following window in time -cmScFol* cmScFolAlloc( cmCtx* ctx, cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH ); +// wndN = max count of elements in the score following window. +// wndMs = max length of the score following window in time + +cmScFol* cmScFolAlloc( cmCtx* ctx, cmScFol* p, cmReal_t srate, unsigned bufN, cmReal_t wndMs, cmScH_t scH ); cmRC_t cmScFolFree( cmScFol** pp ); -cmRC_t cmScFolInit( cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH ); +cmRC_t cmScFolInit( cmScFol* p, cmReal_t srate, unsigned bufN, cmReal_t wndMs, cmScH_t scH ); cmRC_t cmScFolFinal( cmScFol* p ); cmRC_t cmScFolReset( cmScFol* p, unsigned scoreIndex ); unsigned cmScFolExec( cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 ); diff --git a/dsp/cmDspBuiltIn.c b/dsp/cmDspBuiltIn.c index b09365b..b9c1f83 100644 --- a/dsp/cmDspBuiltIn.c +++ b/dsp/cmDspBuiltIn.c @@ -4992,7 +4992,8 @@ cmDspClassConsFunc_t _cmDspClassBuiltInArray[] = cmTimeLineClassCons, cmMidiFilePlayClassCons, - + cmScFolClassCons, + NULL, }; diff --git a/dsp/cmDspKr.c b/dsp/cmDspKr.c index 9223764..fdbc6f6 100644 --- a/dsp/cmDspKr.c +++ b/dsp/cmDspKr.c @@ -686,11 +686,13 @@ cmDspRC_t _cmDspScFolRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* switch( evt->dstVarId ) { case kIndexSfId: - if( cmScFolReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC ) - cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed."); + if( cmScoreIsValid(p->scH) ) + if( cmScFolReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC ) + cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed."); break; case kStatusSfId: + if( cmScoreIsValid(p->scH)) { unsigned idx = cmScFolExec(p->sfp, ctx->cycleCnt, cmDspUInt(inst,kStatusSfId), cmDspUInt(inst,kD0SfId), cmDspUInt(inst,kD1SfId)); if( idx != cmInvalidIdx ) diff --git a/dsp/cmDspKr.h b/dsp/cmDspKr.h index c03fabb..09d372a 100644 --- a/dsp/cmDspKr.h +++ b/dsp/cmDspKr.h @@ -8,6 +8,7 @@ extern "C" { struct cmDspClass_str* cmKrClassCons( cmDspCtx_t* ctx ); struct cmDspClass_str* cmTimeLineClassCons( cmDspCtx_t* ctx ); struct cmDspClass_str* cmMidiFilePlayClassCons( cmDspCtx_t* ctx ); + struct cmDspClass_str* cmScFolClassCons( cmDspCtx_t* ctx ); #ifdef __cplusplus } diff --git a/dsp/cmDspPgmKr.c b/dsp/cmDspPgmKr.c index e1fbe0e..f5e9144 100644 --- a/dsp/cmDspPgmKr.c +++ b/dsp/cmDspPgmKr.c @@ -39,7 +39,7 @@ cmDspRC_t _cmDspSysPgm_TimeLine(cmDspSysH_t h, void** userPtrPtr ) cmDspRC_t rc = kOkDspRC; const cmChar_t* tlFn = "/home/kevin/src/cmgv/src/gv/data/tl7.js"; const cmChar_t* audPath = "/home/kevin/media/audio/20110723-Kriesberg/Audio Files"; - const cmChar_t* scFn = "/home/kevin/src/cmgv/src/gv/data/mod2.csv"; + const cmChar_t* scFn = "/home/kevin/src/cmgv/src/gv/data/mod2b.txt"; cmDspInst_t* sci = cmDspSysAllocInst(h,"Scalar", "ScIdx", 5, kNumberDuiId, 0.0, 10000.0, 1.0, 0.0);