Browse Source

cmProc4.c/h Added to repository

master
kpl 11 years ago
parent
commit
dac4199f36
2 changed files with 383 additions and 0 deletions
  1. 319
    0
      cmProc4.c
  2. 64
    0
      cmProc4.h

+ 319
- 0
cmProc4.c View File

@@ -0,0 +1,319 @@
1
+#include "cmPrefix.h"
2
+#include "cmGlobal.h"
3
+#include "cmRpt.h"
4
+#include "cmErr.h"
5
+#include "cmCtx.h"
6
+#include "cmMem.h"
7
+#include "cmMallocDebug.h"
8
+#include "cmLinkedHeap.h"
9
+#include "cmFloatTypes.h"
10
+#include "cmComplexTypes.h"
11
+#include "cmFileSys.h"
12
+#include "cmJson.h"
13
+#include "cmSymTbl.h"
14
+#include "cmAudioFile.h"
15
+#include "cmProcObj.h"
16
+#include "cmProcTemplate.h"
17
+#include "cmMath.h"
18
+#include "cmProc.h"
19
+#include "cmVectOps.h"
20
+#include "cmMidi.h"
21
+#include "cmMidiFile.h"
22
+#include "cmTimeLine.h"
23
+#include "cmScore.h"
24
+#include "cmProc4.h"
25
+
26
+
27
+
28
+cmScFol* cmScFolAlloc( cmCtx* c, cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH )
29
+{
30
+  cmScFol* op = cmObjAlloc(cmScFol,c,p);
31
+  if( srate != 0 )
32
+    if( cmScFolInit(op,srate,wndN,wndMs,scH) != cmOkRC )
33
+      cmScFolFree(&op);
34
+  return op;
35
+}
36
+
37
+cmRC_t   cmScFolFree(  cmScFol** pp )
38
+{
39
+  cmRC_t rc = cmOkRC;
40
+  if( pp==NULL || *pp==NULL )
41
+    return rc;
42
+
43
+  cmScFol* p = *pp;
44
+  if((rc = cmScFolFinal(p)) != cmOkRC )
45
+    return rc;
46
+
47
+  unsigned i;
48
+  for(i=0; i<p->locN; ++i)
49
+    cmMemFree(p->loc[i].pitchV);
50
+
51
+  cmMemFree(p->loc);
52
+  cmObjFree(pp);
53
+  return rc;
54
+}
55
+
56
+
57
+cmRC_t   cmScFolFinal( cmScFol* p )
58
+{ return cmOkRC; }
59
+
60
+void _cmScFolPrint( cmScFol* p )
61
+{
62
+  int i,j;
63
+  for(i=0; i<p->locN; ++i)
64
+  {
65
+    printf("%2i %5i ",p->loc[i].barNumb,p->loc[i].evtIdx);
66
+    for(j=0; j<p->loc[i].pitchCnt; ++j)
67
+      printf("%s ",cmMidiToSciPitch(p->loc[i].pitchV[j],NULL,0));
68
+    printf("\n");
69
+  }
70
+}
71
+
72
+cmRC_t   cmScFolInit(  cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH )
73
+{
74
+  cmRC_t rc;
75
+  if((rc = cmScFolFinal(p)) != cmOkRC )
76
+    return rc;
77
+
78
+  p->srate          = srate;
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);
83
+  p->locN           = cmScoreEvtCount(scH);
84
+  p->loc            = cmMemResizeZ(cmScFolLoc_t,p->loc,p->locN);
85
+  p->sri            = cmInvalidIdx;
86
+  p->sbi            = cmInvalidIdx;
87
+  p->sei            = cmInvalidIdx;
88
+  p->edWndMtx       = cmVOU_LevEditDistAllocMtx(p->wndN);
89
+  p->evalWndN       = 5;
90
+  p->allowedMissCnt = 1;
91
+
92
+  assert(p->evalWndN<p->wndN);
93
+
94
+  int i,n;
95
+  double        maxDSecs = 0;   // max time between events to be considered simultaneous
96
+  cmScoreEvt_t* e0p      = NULL;
97
+  int           j0       = 0;
98
+
99
+  // for each score event
100
+  for(i=0,n=0; i<p->locN; ++i)
101
+  {
102
+    cmScoreEvt_t* ep = cmScoreEvt(scH,i);
103
+
104
+    // if the event is not a note then ignore it
105
+    if( ep->type == kNonEvtScId )
106
+    {
107
+      assert( j0+n < p->locN );
108
+
109
+      p->loc[j0+n].evtIdx  = i;
110
+      p->loc[j0+n].barNumb = ep->barNumb;
111
+
112
+      // if the first event has not yet been selected
113
+      if( e0p == NULL )
114
+      {
115
+        e0p = ep;
116
+        n   = 1;
117
+      }
118
+      else
119
+      {
120
+        // time can never reverse
121
+        assert( ep->secs >= e0p->secs );  
122
+
123
+        // calc seconds between first event and current event
124
+        double dsecs = ep->secs - e0p->secs;
125
+
126
+        // if the first event and current event are simultaneous...
127
+        if( dsecs <= maxDSecs )
128
+          ++n;   // ... incr. the count of simultaneous events
129
+        else
130
+        {
131
+          int k;
132
+          //  ... a complete set of simultaneous events have been located
133
+          // duplicate all the events at each of their respective time locations
134
+          for(k=0; k<n; ++k)
135
+          {
136
+            int m;
137
+            assert( j0+k < p->locN );
138
+
139
+            p->loc[j0+k].pitchCnt = n;
140
+            p->loc[j0+k].pitchV   = cmMemAllocZ(unsigned,n);
141
+
142
+            for(m=0; m<n; ++m)
143
+            {
144
+              cmScoreEvt_t* tp = cmScoreEvt(scH,p->loc[j0+m].evtIdx);
145
+              assert(tp!=NULL);
146
+              p->loc[j0+k].pitchV[m] = tp->pitch;
147
+            }
148
+          }
149
+          
150
+          e0p = ep;
151
+          j0 += n;
152
+          n   = 1;
153
+        }
154
+      }
155
+    }
156
+  }
157
+
158
+  p->locN = j0;
159
+
160
+  //_cmScFolPrint(p);
161
+
162
+  return rc;
163
+}
164
+
165
+cmRC_t   cmScFolReset(   cmScFol* p, unsigned scoreIndex )
166
+{
167
+  int i;
168
+
169
+  // zero the event index
170
+  memset(p->wndV,0,sizeof(cmScFolWndEle_t)*p->wndN);
171
+
172
+  // 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;
175
+
176
+  // locate the score element in svV[] that is closest to,
177
+  // and possibly after, scoreIndex.
178
+  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) ) 
194
+      p->sbi = i;
195
+    else
196
+      break;
197
+  
198
+  dBar = 3;
199
+
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) )
203
+      p->sei = i;
204
+    else
205
+      break;
206
+
207
+  return cmOkRC;
208
+}
209
+
210
+bool  _cmScFolIsMatch( const cmScFolLoc_t* loc, unsigned pitch )
211
+{
212
+  unsigned i;
213
+  for(i=0; i<loc->pitchCnt; ++i)
214
+    if( loc->pitchV[i] == pitch )
215
+      return true;
216
+  return false;
217
+}
218
+
219
+int _cmScFolDist(unsigned mtxMaxN, unsigned* m, const unsigned* s1, const cmScFolLoc_t* s0, int n )
220
+{
221
+  mtxMaxN += 1;
222
+
223
+  assert( n < mtxMaxN );
224
+  
225
+  int			  v		= 0;
226
+  unsigned  i;
227
+  // Note that m[maxN,maxN] is not oriented in column major order like most 'cm' matrices.
228
+
229
+  for(i=1; i<n+1; ++i)
230
+  {
231
+    unsigned ii 	= i  * mtxMaxN;	// current row
232
+    unsigned i_1 	= ii - mtxMaxN;	// previous row
233
+    unsigned j;	
234
+    for( j=1; j<n+1; ++j)
235
+    {
236
+      //int cost = s0[i-1] == s1[j-1] ? 0 : 1;
237
+      int cost = _cmScFolIsMatch(s0 + i-1, s1[j-1]) ? 0 : 1;
238
+
239
+      //m[i][j] = min( m[i-1][j] + 1, min( m[i][j-1] + 1, m[i-1][j-1] + cost ) );
240
+					
241
+      m[ ii + j ] = v = cmMin( m[ i_1 + j] + 1, cmMin( m[ ii + j - 1] + 1, m[ i_1 + j - 1 ] + cost ) );
242
+    }
243
+  }	
244
+  return v;		
245
+}
246
+
247
+
248
+unsigned   cmScFolExec(  cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 )
249
+{
250
+  assert( p->sri != cmInvalidIdx );
251
+
252
+  unsigned ret_idx = cmInvalidIdx;
253
+  unsigned ewnd[ p->wndN ];
254
+
255
+  if( status != kNoteOnMdId && d1>0 )
256
+    return ret_idx;
257
+
258
+  // left shift wndV[] to make the right-most element available - then copy in the new element
259
+  memmove(p->wndV, p->wndV+1, sizeof(cmScFolWndEle_t)*(p->wndN-1));
260
+  p->wndV[ p->wndN-1 ].smpIdx = smpIdx;
261
+  p->wndV[ p->wndN-1 ].val    = d0;
262
+  p->wndV[ p->wndN-1 ].validFl= true;
263
+
264
+  // fill in ewnd[] with the valid values in wndV[]
265
+  int i = p->wndN-1;
266
+  int en = 0;
267
+  for(; i>=0; --i,++en)
268
+  {
269
+    if( p->wndV[i].validFl /*&& ((smpIdx-p->wnd[i].smpIdx)<=maxWndSmp)*/)
270
+      ewnd[i] = p->wndV[i].val;
271
+    else
272
+      break;
273
+  }
274
+  ++i; // increment i to the first valid element in ewnd[].
275
+
276
+  int k;
277
+  printf("en:%i sbi:%i sei:%i pitch:%s : ",en,p->sbi,p->sei,cmMidiToSciPitch(d0,NULL,0));
278
+  for(k=i; k<p->wndN; ++k)
279
+    printf("%s ", cmMidiToSciPitch(ewnd[k],NULL,0));
280
+  printf("\n");
281
+
282
+  // en is the count of valid elements in ewnd[].
283
+  // ewnd[i] is the first valid element
284
+
285
+  int    j       = 0;
286
+  int    dist;
287
+  int    minDist = INT_MAX;
288
+  int    minIdx  = cmInvalidIdx;
289
+  for(j=0; p->sbi+en+j <= p->sei; ++j)
290
+    if((dist = _cmScFolDist(p->wndN, p->edWndMtx, ewnd+i, p->loc + p->sbi+j, en )) < minDist )
291
+    {
292
+      minDist = dist;
293
+      minIdx  = j;
294
+    }
295
+
296
+  // The best fit is on the score window: p->loc[sbi+minIdx : sbi+minIdx+en-1 ]
297
+   
298
+  int evalWndN = cmMin(en,p->evalWndN);
299
+
300
+  assert(evalWndN<p->wndN);
301
+  
302
+  j = p->sbi+minIdx+en - evalWndN;
303
+
304
+  // Determine how many of the last evalWndN elements match
305
+  dist =  _cmScFolDist(p->wndN, p->edWndMtx, ewnd+p->wndN-evalWndN, p->loc+j, evalWndN );
306
+  
307
+  // a successful match has <= allowedMissCnt and an exact match on the last element  
308
+  //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]))
310
+  {
311
+    p->sbi  = p->sbi + minIdx;
312
+    p->sei  = cmMin(p->sei+minIdx,p->locN-1);
313
+    ret_idx = p->sbi+minIdx+en-1;
314
+  }
315
+
316
+  printf("minDist:%i minIdx:%i evalDist:%i sbi:%i sei:%i\n",minDist,minIdx,dist,p->sbi,p->sei);
317
+
318
+  return ret_idx;
319
+}

+ 64
- 0
cmProc4.h View File

@@ -0,0 +1,64 @@
1
+#ifndef cmProc4_h
2
+#define cmProc4_h
3
+
4
+#ifdef __cplusplus
5
+extern "C" {
6
+#endif
7
+
8
+
9
+
10
+typedef struct
11
+{
12
+  unsigned     smpIdx;  // time tag sample index for val
13
+  cmMidiByte_t val;     //
14
+  bool         validFl; //
15
+  
16
+} cmScFolWndEle_t;
17
+
18
+typedef struct
19
+{
20
+  unsigned  pitchCnt; // 
21
+  unsigned* pitchV;   // pitchV[pitchCnt]
22
+  unsigned  evtIdx;   // index of the score event (into cmScoreEvt[]) at this location 
23
+  int       barNumb;  // bar number of this location
24
+} cmScFolLoc_t;
25
+
26
+
27
+typedef struct
28
+{
29
+  cmObj            obj;
30
+  cmReal_t         srate;
31
+  cmScH_t          scH;
32
+  unsigned         maxWndSmp;
33
+  unsigned         wndN;
34
+  cmScFolWndEle_t* wndV;     // wnd[wndN]
35
+  int              wni;     // oldest value in wnd[]
36
+  int              locN;
37
+  cmScFolLoc_t*    loc;
38
+  unsigned         sri;     // last reset score index
39
+  unsigned         sbi;     // first (oldest) score index
40
+  unsigned         sei;     // last (newest) score index
41
+
42
+  unsigned         evalWndN; // (dflt:5) count of elements to use for refined match window
43
+  unsigned         allowedMissCnt; // (dflt:1) count of non-match elements in refined match where a match is still signaled
44
+
45
+  unsigned*        edWndMtx;
46
+  
47
+} cmScFol;
48
+
49
+// wndN = max count of elements in the  score following window.
50
+  // wndMs     = max length of the score following window in time
51
+
52
+cmScFol* cmScFolAlloc( cmCtx* ctx, cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH );
53
+cmRC_t   cmScFolFree(  cmScFol** pp );
54
+cmRC_t   cmScFolInit(  cmScFol* p, cmReal_t srate, unsigned wndN, cmReal_t wndMs, cmScH_t scH );
55
+cmRC_t   cmScFolFinal( cmScFol* p );
56
+cmRC_t   cmScFolReset( cmScFol* p, unsigned scoreIndex );
57
+unsigned cmScFolExec(  cmScFol* p, unsigned smpIdx, unsigned status, cmMidiByte_t d0, cmMidiByte_t d1 );
58
+  
59
+
60
+#ifdef __cplusplus
61
+}
62
+#endif
63
+
64
+#endif

Loading…
Cancel
Save