|
@@ -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
|
+}
|