Browse Source

Rewrote pitch tracker

master
kevin 11 years ago
parent
commit
68696bce20
6 changed files with 165 additions and 55 deletions
  1. 127
    44
      cmProc4.c
  2. 30
    7
      cmProc4.h
  3. 2
    1
      dsp/cmDspBuiltIn.c
  4. 4
    2
      dsp/cmDspKr.c
  5. 1
    0
      dsp/cmDspKr.h
  6. 1
    1
      dsp/cmDspPgmKr.c

+ 127
- 44
cmProc4.c View File

@@ -25,11 +25,11 @@
25 25
 
26 26
 
27 27
 
28
-cmScFol* cmScFolAlloc( cmCtx* c, cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH )
28
+cmScFol* cmScFolAlloc( cmCtx* c, cmScFol* p, cmReal_t srate, unsigned bufN, cmReal_t wndMs, cmScH_t scH )
29 29
 {
30 30
   cmScFol* op = cmObjAlloc(cmScFol,c,p);
31 31
   if( srate != 0 )
32
-    if( cmScFolInit(op,srate,wndN,wndMs,scH) != cmOkRC )
32
+    if( cmScFolInit(op,srate,bufN,wndMs,scH) != cmOkRC )
33 33
       cmScFolFree(&op);
34 34
   return op;
35 35
 }
@@ -62,14 +62,14 @@ void _cmScFolPrint( cmScFol* p )
62 62
   int i,j;
63 63
   for(i=0; i<p->locN; ++i)
64 64
   {
65
-    printf("%2i %5i ",p->loc[i].barNumb,p->loc[i].evtIdx);
65
+    printf("%2i %5i ",p->loc[i].barNumb,p->loc[i].scIdx);
66 66
     for(j=0; j<p->loc[i].pitchCnt; ++j)
67 67
       printf("%s ",cmMidiToSciPitch(p->loc[i].pitchV[j],NULL,0));
68 68
     printf("\n");
69 69
   }
70 70
 }
71 71
 
72
-cmRC_t   cmScFolInit(  cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH )
72
+cmRC_t   cmScFolInit(  cmScFol* p, cmReal_t srate, unsigned bufN, cmReal_t wndMs, cmScH_t scH )
73 73
 {
74 74
   cmRC_t rc;
75 75
   if((rc = cmScFolFinal(p)) != cmOkRC )
@@ -77,22 +77,19 @@ cmRC_t   cmScFolInit(  cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs
77 77
 
78 78
   p->srate          = srate;
79 79
   p->scH            = scH;
80
-  p->maxWndSmp      = floor(wndMs * srate / 1000.0);
81
-  p->wndN           = wndN;
82
-  p->wndV           = cmMemResizeZ(cmScFolWndEle_t,p->wndV,wndN);
80
+  p->bufN           = bufN;
81
+  p->bufV           = cmMemResizeZ(cmScFolBufEle_t,p->bufV,bufN);
83 82
   p->locN           = cmScoreEvtCount(scH);
84 83
   p->loc            = cmMemResizeZ(cmScFolLoc_t,p->loc,p->locN);
85
-  p->sri            = cmInvalidIdx;
86 84
   p->sbi            = cmInvalidIdx;
87 85
   p->sei            = cmInvalidIdx;
88
-  p->edWndMtx       = cmVOU_LevEditDistAllocMtx(p->wndN);
89
-  p->evalWndN       = 5;
90
-  p->allowedMissCnt = 1;
86
+  p->msln           = 7;
87
+  p->mswn           = 30;
88
+  p->edWndMtx       = cmVOU_LevEditDistAllocMtx(p->bufN);
91 89
 
92
-  assert(p->evalWndN<p->wndN);
93 90
 
94 91
   int i,n;
95
-  double        maxDSecs = 0;   // max time between events to be considered simultaneous
92
+  double        maxDSecs = 0;   // max time between score entries to be considered simultaneous
96 93
   cmScoreEvt_t* e0p      = NULL;
97 94
   int           j0       = 0;
98 95
 
@@ -106,7 +103,7 @@ cmRC_t   cmScFolInit(  cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs
106 103
     {
107 104
       assert( j0+n < p->locN );
108 105
 
109
-      p->loc[j0+n].evtIdx  = i;
106
+      p->loc[j0+n].scIdx  = i;
110 107
       p->loc[j0+n].barNumb = ep->barNumb;
111 108
 
112 109
       // 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
141 138
 
142 139
             for(m=0; m<n; ++m)
143 140
             {
144
-              cmScoreEvt_t* tp = cmScoreEvt(scH,p->loc[j0+m].evtIdx);
141
+              cmScoreEvt_t* tp = cmScoreEvt(scH,p->loc[j0+m].scIdx);
145 142
               assert(tp!=NULL);
146 143
               p->loc[j0+k].pitchV[m] = tp->pitch;
147 144
             }
@@ -166,43 +163,34 @@ cmRC_t   cmScFolReset(   cmScFol* p, unsigned scoreIndex )
166 163
 {
167 164
   int i;
168 165
 
169
-  // zero the event index
170
-  memset(p->wndV,0,sizeof(cmScFolWndEle_t)*p->wndN);
166
+
167
+  // empty the event buffer
168
+  memset(p->bufV,0,sizeof(cmScFolBufEle_t)*p->bufN);
171 169
 
172 170
   // don't allow the score index to be prior to the first note
173
-  if( scoreIndex < p->loc[0].evtIdx )
174
-    scoreIndex = p->loc[0].evtIdx;
171
+  if( scoreIndex < p->loc[0].scIdx )
172
+    scoreIndex = p->loc[0].scIdx;
173
+
174
+  p->sei = cmInvalidIdx;
175
+  p->sbi = cmInvalidIdx;
175 176
 
176 177
   // locate the score element in svV[] that is closest to,
177 178
   // and possibly after, scoreIndex.
178 179
   for(i=0; i<p->locN-1; ++i)
179
-    if( p->loc[i].evtIdx <= scoreIndex && scoreIndex < p->loc[i+1].evtIdx  )
180
-      break;
181
-
182
-  // force scEvtIndex to be valid
183
-  assert( i<p->locN );
184
-
185
-  p->sri = i;
186
-  p->sbi = i;
187
-
188
-  // score event window is dBar bars before and after scEvtIndex;
189
-  int dBar = 1;
190
-
191
-  // backup dBar bars from the 'scoreIndex'
192
-  for(; i>=0; --i)
193
-    if( p->loc[i].barNumb >= (p->loc[p->sri].barNumb-dBar) ) 
180
+    if( p->loc[i].scIdx <= scoreIndex && scoreIndex < p->loc[i+1].scIdx  )
181
+    {
194 182
       p->sbi = i;
195
-    else
196 183
       break;
197
-  
198
-  dBar = 3;
184
+    }
199 185
 
200
-  // move forward dBar bars from 'scoreIndex'
201
-  for(i=p->sri; i<p->locN; ++i)
202
-    if( p->loc[i].barNumb <= (p->loc[p->sri].barNumb+dBar) )
186
+  // locate the score element at the end of the look-ahead region
187
+  for(; i<p->locN-1; ++i)
188
+    if( p->loc[i].scIdx <= scoreIndex + p->msln && scoreIndex + p->msln < p->loc[i+1].scIdx )
189
+    {
203 190
       p->sei = i;
204
-    else
205 191
       break;
192
+    }
193
+
206 194
 
207 195
   return cmOkRC;
208 196
 }
@@ -244,7 +232,7 @@ int _cmScFolDist(unsigned mtxMaxN, unsigned* m, const unsigned* s1, const cmScFo
244 232
   return v;		
245 233
 }
246 234
 
247
-
235
+/*
248 236
 unsigned   cmScFolExec(  cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
249 237
 {
250 238
   assert( p->sri != cmInvalidIdx );
@@ -266,7 +254,8 @@ unsigned   cmScFolExec(  cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByt
266 254
   int en = 0;
267 255
   for(; i>=0; --i,++en)
268 256
   {
269
-    if( p->wndV[i].validFl /*&& ((smpIdx-p->wnd[i].smpIdx)<=maxWndSmp)*/)
257
+    //if( p->wndV[i].validFl && ((smpIdx-p->wnd[i].smpIdx)<=maxWndSmp))
258
+    if( p->wndV[i].validFl )
270 259
       ewnd[i] = p->wndV[i].val;
271 260
     else
272 261
       break;
@@ -306,7 +295,8 @@ unsigned   cmScFolExec(  cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByt
306 295
   
307 296
   // a successful match has <= allowedMissCnt and an exact match on the last element  
308 297
   //if( dist <= p->allowedMissCnt && ewnd[p->wndN-1] == p->loc[p->sbi+minIdx+en-1] )
309
-  if( /*dist <= p->allowedMissCnt &&*/ _cmScFolIsMatch(p->loc+(p->sbi+minIdx+en-1),ewnd[p->wndN-1]))
298
+  //if( dist <= p->allowedMissCnt && _cmScFolIsMatch(p->loc+(p->sbi+minIdx+en-1),ewnd[p->wndN-1]))
299
+  if(  _cmScFolIsMatch(p->loc+(p->sbi+minIdx+en-1),ewnd[p->wndN-1]))
310 300
   {
311 301
     p->sbi  = p->sbi + minIdx;
312 302
     p->sei  = cmMin(p->sei+minIdx,p->locN-1);
@@ -317,3 +307,96 @@ unsigned   cmScFolExec(  cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByt
317 307
 
318 308
   return ret_idx;
319 309
 }
310
+*/
311
+
312
+unsigned   cmScFolExec(  cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
313
+{
314
+
315
+  unsigned ret_idx = cmInvalidIdx;
316
+  unsigned ebuf[ p->bufN ];
317
+
318
+  if( status != kNoteOnMdId && d1>0 )
319
+    return ret_idx;
320
+
321
+  // left shift bufV[] to make the right-most element available - then copy in the new element
322
+  memmove(p->bufV, p->bufV+1, sizeof(cmScFolBufEle_t)*(p->bufN-1));
323
+  p->bufV[ p->bufN-1 ].smpIdx = smpIdx;
324
+  p->bufV[ p->bufN-1 ].val    = d0;
325
+  p->bufV[ p->bufN-1 ].validFl= true;
326
+
327
+  // fill in ebuf[] with the valid values in bufV[]
328
+  int i = p->bufN-1;
329
+  int en = 0;
330
+  for(; i>=0; --i,++en)
331
+  {
332
+    if( p->bufV[i].validFl)
333
+      ebuf[i] = p->bufV[i].val;
334
+    else
335
+      break;
336
+  }
337
+  ++i;  // increment i to the first valid element in ebuf[].
338
+
339
+
340
+  // en is the count of valid elements in ebuf[].
341
+  // ebuf[p->boi] is the first valid element
342
+
343
+  int    j       = 0;
344
+  int    minDist = INT_MAX;
345
+  int    minIdx  = cmInvalidIdx;
346
+  int    dist;
347
+
348
+  // the score wnd must always be as long as the buffer n
349
+  // at the end of the score this may not be the case 
350
+  // (once sei hits locN - at this point we must begin
351
+  // shrinking ewnd[] to contain only the last p->sei-p->sbi+1 elements)
352
+  assert( p->sei-p->sbi+1 >= en );
353
+
354
+  for(j=0; p->sbi+en+j <= p->sei; ++j)
355
+    if((dist = _cmScFolDist(p->bufN, p->edWndMtx, ebuf+i, p->loc + p->sbi+j, en )) < minDist )
356
+    {
357
+      minDist = dist;
358
+      minIdx  = j;
359
+    }
360
+
361
+  // The best fit is on the score window: p->loc[sbi+minIdx : sbi+minIdx+en-1 ]
362
+
363
+  // if a perfect match occurred
364
+  if( minDist == 0 )
365
+  {
366
+    // we had a perfect match - shrink the window to it's minumum size
367
+    p->sbi += (en==p->bufN) ? minIdx + 1 : 0;  // move wnd begin forward to just past first match
368
+    p->sei  = p->sbi + minIdx + en + p->msln;  // move wnd end forward to just past last match
369
+    ret_idx = p->sbi + minIdx + en - 1;       
370
+    // BUG BUG BUG - is the window length valid - 
371
+    //  - sbi and sei must be inside 0:locN
372
+    //  - sei-sbi + 1 must be >= en
373
+  }
374
+  else
375
+  {
376
+    // if the last event matched - then return the match location as the current score location
377
+    if( _cmScFolIsMatch(p->loc+(p->sbi+minIdx+en-1),ebuf[p->bufN-1]) )
378
+      ret_idx = p->sbi + minIdx + en - 1;
379
+
380
+    // even though we didn't match move the end of the score window forward
381
+    // this will enlarge the score window by one
382
+     p->sei += 1;
383
+
384
+    assert( p->sei > p->sbi );
385
+
386
+    // if the score window length surpasses the max score window size 
387
+    // move the beginning index forward 
388
+    if( p->sei - p->sbi + 1 > p->mswn && p->sei > p->mswn )
389
+      p->sbi = p->sei - p->mswn + 1;
390
+
391
+    // BUG BUG BUG - is the window length valid - 
392
+    //  - sbi and sei must be inside 0:locN
393
+    //  - sei-sbi + 1 must be >= en
394
+
395
+  }
396
+
397
+  // BUG BUG BUG - this is not currently guarded against
398
+  assert( p->sei < p->locN );
399
+  assert( p->sbi < p->locN );
400
+    
401
+  return ret_idx;
402
+}

+ 30
- 7
cmProc4.h View File

@@ -13,17 +13,17 @@ typedef struct
13 13
   cmMidiByte_t val;     //
14 14
   bool         validFl; //
15 15
   
16
-} cmScFolWndEle_t;
16
+} cmScFolBufEle_t;
17 17
 
18 18
 typedef struct
19 19
 {
20 20
   unsigned  pitchCnt; // 
21 21
   unsigned* pitchV;   // pitchV[pitchCnt]
22
-  unsigned  evtIdx;   // index of the score event (into cmScoreEvt[]) at this location 
22
+  unsigned  scIdx;   // index of the score event (into cmScoreEvt[]) at this location 
23 23
   int       barNumb;  // bar number of this location
24 24
 } cmScFolLoc_t;
25 25
 
26
-
26
+  /*
27 27
 typedef struct
28 28
 {
29 29
   cmObj            obj;
@@ -31,7 +31,7 @@ typedef struct
31 31
   cmScH_t          scH;
32 32
   unsigned         maxWndSmp;
33 33
   unsigned         wndN;
34
-  cmScFolWndEle_t* wndV;     // wnd[wndN]
34
+  cmScFolWndEle_t* wndV;    // wnd[wndN]
35 35
   int              wni;     // oldest value in wnd[]
36 36
   int              locN;
37 37
   cmScFolLoc_t*    loc;
@@ -44,14 +44,37 @@ typedef struct
44 44
 
45 45
   unsigned*        edWndMtx;
46 46
   
47
+} cmScFol1;
48
+  */
49
+
50
+typedef struct
51
+{
52
+  cmObj            obj;
53
+  cmReal_t         srate;   //
54
+  cmScH_t          scH;     // score handle
55
+  unsigned         bufN;    // event buffer count 
56
+  cmScFolBufEle_t* bufV;    // event buffer bufV[bufN] - bufV[bufN-1] newest event, bufV[boi] oldest event
57
+  int              locN;    // count of score locations
58
+  cmScFolLoc_t*    loc;     // score loc[locN]
59
+  unsigned         sbi;    // oldest score window index
60
+  unsigned         sei;    // newest score window index
61
+  unsigned         msln;    // minimum score look ahead count
62
+  unsigned         mswn;    // maximum score window length
63
+
64
+  //unsigned         evalWndN; // (dflt:5) count of elements to use for refined match window
65
+  //unsigned         allowedMissCnt; // (dflt:1) count of non-match elements in refined match where a match is still signaled
66
+
67
+  unsigned*        edWndMtx;
68
+  
47 69
 } cmScFol;
48 70
 
71
+
49 72
 // wndN = max count of elements in the  score following window.
50
-  // wndMs     = max length of the score following window in time
73
+// wndMs     = max length of the score following window in time
51 74
 
52
-cmScFol* cmScFolAlloc( cmCtx* ctx, cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH );
75
+cmScFol* cmScFolAlloc( cmCtx* ctx, cmScFol* p, cmReal_t srate, unsigned bufN, cmReal_t wndMs, cmScH_t scH );
53 76
 cmRC_t   cmScFolFree(  cmScFol** pp );
54
-cmRC_t   cmScFolInit(  cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH );
77
+cmRC_t   cmScFolInit(  cmScFol* p, cmReal_t srate, unsigned bufN, cmReal_t wndMs, cmScH_t scH );
55 78
 cmRC_t   cmScFolFinal( cmScFol* p );
56 79
 cmRC_t   cmScFolReset( cmScFol* p, unsigned scoreIndex );
57 80
 unsigned cmScFolExec(  cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 );

+ 2
- 1
dsp/cmDspBuiltIn.c View File

@@ -4992,7 +4992,8 @@ cmDspClassConsFunc_t _cmDspClassBuiltInArray[] =
4992 4992
   
4993 4993
   cmTimeLineClassCons,
4994 4994
   cmMidiFilePlayClassCons,
4995
-  
4995
+  cmScFolClassCons,
4996
+
4996 4997
   NULL,
4997 4998
 };
4998 4999
 

+ 4
- 2
dsp/cmDspKr.c View File

@@ -686,11 +686,13 @@ cmDspRC_t _cmDspScFolRecv(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t*
686 686
     switch( evt->dstVarId )
687 687
     {
688 688
       case kIndexSfId:
689
-        if( cmScFolReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC )
690
-          cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed.");
689
+        if( cmScoreIsValid(p->scH) )
690
+          if( cmScFolReset( p->sfp, cmDspUInt(inst,kIndexSfId) ) != cmOkRC )
691
+            cmErrMsg(&inst->classPtr->err, kSubSysFailDspRC, "Score follower reset to score index '%i' failed.");
691 692
         break;
692 693
 
693 694
       case kStatusSfId:
695
+        if( cmScoreIsValid(p->scH))
694 696
         {
695 697
           unsigned idx = cmScFolExec(p->sfp, ctx->cycleCnt, cmDspUInt(inst,kStatusSfId), cmDspUInt(inst,kD0SfId), cmDspUInt(inst,kD1SfId));
696 698
           if( idx != cmInvalidIdx )

+ 1
- 0
dsp/cmDspKr.h View File

@@ -8,6 +8,7 @@ extern "C" {
8 8
   struct cmDspClass_str* cmKrClassCons( cmDspCtx_t* ctx );
9 9
   struct cmDspClass_str* cmTimeLineClassCons( cmDspCtx_t* ctx );
10 10
   struct cmDspClass_str* cmMidiFilePlayClassCons( cmDspCtx_t* ctx );
11
+  struct cmDspClass_str* cmScFolClassCons( cmDspCtx_t* ctx );
11 12
 
12 13
 #ifdef __cplusplus
13 14
 }

+ 1
- 1
dsp/cmDspPgmKr.c View File

@@ -39,7 +39,7 @@ cmDspRC_t _cmDspSysPgm_TimeLine(cmDspSysH_t h, void** userPtrPtr )
39 39
   cmDspRC_t       rc      = kOkDspRC;
40 40
   const cmChar_t* tlFn    = "/home/kevin/src/cmgv/src/gv/data/tl7.js";
41 41
   const cmChar_t* audPath = "/home/kevin/media/audio/20110723-Kriesberg/Audio Files";
42
-  const cmChar_t* scFn    = "/home/kevin/src/cmgv/src/gv/data/mod2.csv";
42
+  const cmChar_t* scFn    = "/home/kevin/src/cmgv/src/gv/data/mod2b.txt";
43 43
 
44 44
   cmDspInst_t* sci =  cmDspSysAllocInst(h,"Scalar", "ScIdx",  5, kNumberDuiId, 0.0,  10000.0, 1.0,  0.0);
45 45
 

Loading…
Cancel
Save