Browse Source

cmScore.c/h Added score display to gv. Many changes.

master
kevin 11 years ago
parent
commit
b096ef30d7
2 changed files with 118 additions and 15 deletions
  1. 86
    12
      app/cmScore.c
  2. 32
    3
      app/cmScore.h

+ 86
- 12
app/cmScore.c View File

@@ -60,6 +60,9 @@ typedef struct
60 60
   cmScoreEvt_t* array;
61 61
   unsigned      cnt;
62 62
   cmCsvH_t      cH;
63
+  cmScCb_t      cbFunc;
64
+  void*         cbArg;
65
+  cmChar_t*     fn;
63 66
 } cmSc_t;
64 67
 
65 68
 cmScEvtRef_t _cmScEvtRefArray[] = 
@@ -111,7 +114,7 @@ unsigned _cmScEvtTypeLabelToId( const cmChar_t* label )
111 114
   return kInvalidEvtScId;
112 115
 }
113 116
 
114
-const cmChar_t* _cmScEvtTypeIdToLabel( unsigned id )
117
+const cmChar_t* cmScEvtTypeIdToLabel( unsigned id )
115 118
 {
116 119
   cmScEvtRef_t* r = _cmScEvtRefArray;
117 120
   for(; r->id != kInvalidEvtScId; ++r )
@@ -129,7 +132,7 @@ unsigned _cmScDynLabelToId( const cmChar_t* label )
129 132
   return kInvalidDynScId;
130 133
 }
131 134
 
132
-const cmChar_t* _cmScDynIdToLabel( unsigned id )
135
+const cmChar_t* cmScDynIdToLabel( unsigned id )
133 136
 {
134 137
   cmScEvtRef_t* r = _cmScDynRefArray;
135 138
   for(; r->id != kInvalidDynScId; ++r )
@@ -173,15 +176,20 @@ cmScRC_t _cmScFinalize( cmSc_t* p )
173 176
   if( cmCsvFinalize(&p->cH) != kOkCsvRC )
174 177
     return rc;
175 178
 
179
+  cmMemFree(p->fn);
176 180
   cmMemFree(p->array);
177 181
   cmMemFree(p);
178 182
   return rc;
179 183
 }
180 184
 
181
-cmScRC_t _cmScParseBar( cmSc_t* p, unsigned rowIdx, int* barNumb )
185
+cmScRC_t _cmScParseBar( cmSc_t* p, unsigned rowIdx, cmScoreEvt_t* s, int* barNumb )
182 186
 {
183 187
   if((*barNumb = cmCsvCellInt(p->cH,rowIdx,kBarColScIdx)) == INT_MAX )
184 188
     return cmErrMsg(&p->err,kSyntaxErrScRC,"Unable to parse the bar number.");
189
+
190
+  s->type    = kBarEvtScId;
191
+  s->dsecs   = 0;
192
+  s->barNumb = *barNumb;
185 193
   return kOkScRC;
186 194
 }
187 195
 
@@ -223,6 +231,7 @@ cmScRC_t _cmScParseNoteOn( cmSc_t* p, unsigned rowIdx, cmScoreEvt_t* s, int barN
223 231
   }
224 232
 
225 233
   s->type       = kNonEvtScId;
234
+  s->dsecs      = dsecs;
226 235
   s->pitch      = midiPitch;
227 236
   s->flags      = flags;
228 237
   s->dynVal     = dynVal; 
@@ -235,8 +244,10 @@ cmScRC_t _cmScParseNoteOn( cmSc_t* p, unsigned rowIdx, cmScoreEvt_t* s, int barN
235 244
 cmScRC_t _cmScParseFile( cmSc_t* p, cmCtx_t* ctx, const cmChar_t* fn )
236 245
 {
237 246
   cmScRC_t rc = kOkScRC;
238
-  unsigned barNoteIdx;
239
-  int      barNumb;
247
+  unsigned barNoteIdx = 0;
248
+  int      barEvtIdx = cmInvalidIdx;
249
+  int      barNumb   = 0;
250
+  double   secs;
240 251
 
241 252
   if( cmCsvInitialize(&p->cH, ctx ) != kOkCsvRC )
242 253
   {
@@ -280,19 +291,25 @@ cmScRC_t _cmScParseFile( cmSc_t* p, cmCtx_t* ctx, const cmChar_t* fn )
280 291
       break;
281 292
     }
282 293
 
283
-    
294
+    secs = DBL_MAX;
295
+
284 296
     switch(tid)
285 297
     {
286 298
       case kBarEvtScId:
287 299
         // parse bar lines
288
-        if((rc = _cmScParseBar(p,i,&barNumb)) == kOkScRC )
300
+        if((rc = _cmScParseBar(p,i,p->array+j,&barNumb)) == kOkScRC )
301
+        {
289 302
           barNoteIdx = 0;
303
+          barEvtIdx  = j;
304
+          ++j;
305
+        }
290 306
         break;
291 307
 
292 308
       case kNonEvtScId:
293 309
         // parse note-on events
294 310
         if((rc =  _cmScParseNoteOn(p, i, p->array + j, barNumb, barNoteIdx )) == kOkScRC )
295 311
         {
312
+          secs = p->array[j].dsecs;
296 313
           if( cmIsFlag(p->array[j].flags,kSkipScFl) == false )
297 314
             ++j;
298 315
 
@@ -301,9 +318,20 @@ cmScRC_t _cmScParseFile( cmSc_t* p, cmCtx_t* ctx, const cmChar_t* fn )
301 318
         break;
302 319
 
303 320
       default:
321
+        // Returns DBL_MAX on error.
322
+        secs =  cmCsvCellDouble(p->cH, i, kDSecsColScIdx );
304 323
         break;
305 324
     }
306 325
     
326
+    // the bar lines don't have times so set the time of the bar line to the
327
+    // time of the first event in the bar.
328
+    if( barEvtIdx != cmInvalidIdx && secs != DBL_MAX )
329
+    {
330
+      assert( p->array[ barEvtIdx ].type == kBarEvtScId );
331
+      p->array[ barEvtIdx ].dsecs = secs;
332
+      barEvtIdx = cmInvalidIdx;
333
+    }
334
+    
307 335
   }
308 336
 
309 337
   if( rc == kSyntaxErrScRC )
@@ -319,7 +347,7 @@ cmScRC_t _cmScParseFile( cmSc_t* p, cmCtx_t* ctx, const cmChar_t* fn )
319 347
   return rc;
320 348
 }
321 349
 
322
-cmScRC_t cmScoreInitialize( cmCtx_t* ctx, cmScH_t* hp, const cmChar_t* fn )
350
+cmScRC_t cmScoreInitialize( cmCtx_t* ctx, cmScH_t* hp, const cmChar_t* fn, cmScCb_t cbFunc, void* cbArg )
323 351
 {
324 352
   cmScRC_t rc = kOkScRC;
325 353
   if((rc = cmScoreFinalize(hp)) != kOkScRC )
@@ -332,6 +360,10 @@ cmScRC_t cmScoreInitialize( cmCtx_t* ctx, cmScH_t* hp, const cmChar_t* fn )
332 360
   if((rc = _cmScParseFile(p,ctx,fn)) != kOkScRC )
333 361
     goto errLabel;
334 362
 
363
+  p->cbFunc = cbFunc;
364
+  p->cbArg  = cbArg;
365
+  p->fn     = cmMemAllocStr(fn);
366
+
335 367
   hp->h = p;
336 368
 
337 369
  errLabel:
@@ -358,6 +390,12 @@ cmScRC_t cmScoreFinalize( cmScH_t* hp )
358 390
   return rc;
359 391
 }
360 392
 
393
+const cmChar_t* cmScoreFileName( cmScH_t h )
394
+{
395
+  cmSc_t* p = _cmScHandleToPtr(h);
396
+  return p->fn;
397
+}
398
+
361 399
 bool     cmScoreIsValid( cmScH_t h )
362 400
 { return h.h != NULL; }
363 401
 
@@ -378,6 +416,42 @@ cmScoreEvt_t* cmScoreEvt( cmScH_t h, unsigned idx )
378 416
   return p->array + idx;
379 417
 }
380 418
 
419
+cmScRC_t      cmScoreSeqNotify( cmScH_t h )
420
+{
421
+  cmScRC_t  rc = kOkScRC;
422
+  cmSc_t*   p  = _cmScHandleToPtr(h);
423
+  cmScMsg_t m;
424
+  unsigned  i;
425
+
426
+  if( p->cbFunc != NULL )
427
+  {
428
+    memset(&m.evt,0,sizeof(m.evt));
429
+    m.typeId = kBeginMsgScId;
430
+    p->cbFunc(p->cbArg,&m,sizeof(m));
431
+
432
+    m.typeId = kEventMsgScId;
433
+    for(i=0; i<p->cnt; ++i)
434
+    {
435
+      m.evt = p->array[i];
436
+      p->cbFunc(p->cbArg,&m,sizeof(m));
437
+    }
438
+
439
+    memset(&m.evt,0,sizeof(m.evt));
440
+    m.typeId = kEndMsgScId;
441
+    p->cbFunc(p->cbArg,&m,sizeof(m));
442
+
443
+  }
444
+  return rc;
445
+}
446
+
447
+cmScRC_t      cmScoreDecode( const void* msg, unsigned msgByteCnt, cmScMsg_t* m)
448
+{
449
+  cmScMsg_t* mp = (cmScMsg_t*)msg;
450
+  *m = *mp;
451
+  return kOkScRC;
452
+}
453
+
454
+
381 455
 void cmScorePrint( cmScH_t h, cmRpt_t* rpt )
382 456
 {
383 457
   cmSc_t* p = _cmScHandleToPtr(h);
@@ -392,12 +466,12 @@ void cmScorePrint( cmScH_t h, cmRpt_t* rpt )
392 466
           i,
393 467
           r->barNumb,
394 468
           r->barNoteIdx,
395
-          _cmScEvtTypeIdToLabel(r->type),
469
+          cmScEvtTypeIdToLabel(r->type),
396 470
           r->pitch,
397 471
           cmIsFlag(r->flags,kEvenScFl)  ? 'e' : ' ',
398 472
           cmIsFlag(r->flags,kTempoScFl) ? 't' : ' ',
399 473
           cmIsFlag(r->flags,kDynScFl)   ? 'd' : ' ',
400
-          cmIsFlag(r->flags,kDynScFl)   ? _cmScDynIdToLabel(r->dynVal) : "");          
474
+          cmIsFlag(r->flags,kDynScFl)   ? cmScDynIdToLabel(r->dynVal) : "");          
401 475
         break;
402 476
 
403 477
       default:
@@ -603,7 +677,7 @@ cmScRC_t cmScoreSyncTimeLineTest( cmCtx_t* ctx,  const cmChar_t* timeLineJsFn, c
603 677
 
604 678
   if(1)
605 679
   {
606
-    if((rc = cmScoreInitialize(ctx,&scH,scoreCsvFn))  != kOkScRC )
680
+    if((rc = cmScoreInitialize(ctx,&scH,scoreCsvFn,NULL,NULL))  != kOkScRC )
607 681
       goto errLabel;
608 682
 
609 683
 
@@ -626,7 +700,7 @@ cmScRC_t cmScoreSyncTimeLineTest( cmCtx_t* ctx,  const cmChar_t* timeLineJsFn, c
626 700
 void cmScoreTest( cmCtx_t* ctx, const cmChar_t* fn )
627 701
 {
628 702
   cmScH_t h = cmScNullHandle;
629
-  if( cmScoreInitialize(ctx,&h,fn) != kOkScRC )
703
+  if( cmScoreInitialize(ctx,&h,fn,NULL,NULL) != kOkScRC )
630 704
     return;
631 705
 
632 706
   cmScorePrint(h,&ctx->rpt);

+ 32
- 3
app/cmScore.h View File

@@ -51,15 +51,25 @@ extern "C" {
51 51
     unsigned     barNoteIdx;   // index of this note in this bar
52 52
   } cmScoreEvt_t;
53 53
 
54
+  typedef void (*cmScCb_t)( void* arg, const void* data, unsigned byteCnt );
54 55
 
55 56
   typedef cmRC_t     cmScRC_t;
56 57
   typedef cmHandle_t cmScH_t;
58
+
59
+  typedef void (*cmScCb_t)( void* arg, const void* data, unsigned byteCnt );
57 60
   
58 61
   extern cmScH_t cmScNullHandle;
59 62
 
63
+  const cmChar_t* cmScEvtTypeIdToLabel( unsigned id );
64
+  const cmChar_t* cmScDynIdToLabel( unsigned id );
65
+
66
+
60 67
   // Initialize a score object from a CSV File generated from a score spreadsheet.
61
-  cmScRC_t      cmScoreInitialize( cmCtx_t* ctx, cmScH_t* hp, const cmChar_t* fn );
62
-  cmScRC_t      cmScoreFinalize( cmScH_t* hp );
68
+  cmScRC_t      cmScoreInitialize( cmCtx_t* ctx, cmScH_t* hp, const cmChar_t* fn, cmScCb_t cbFunc, void* cbArg );
69
+  cmScRC_t      cmScoreFinalize(   cmScH_t* hp );
70
+
71
+  // Filename of last successfuly loaded score file.
72
+  const cmChar_t* cmScoreFileName( cmScH_t h );
63 73
 
64 74
   bool          cmScoreIsValid( cmScH_t h );
65 75
 
@@ -67,13 +77,32 @@ extern "C" {
67 77
   unsigned      cmScoreEvtCount( cmScH_t h );
68 78
   cmScoreEvt_t* cmScoreEvt( cmScH_t h, unsigned idx );
69 79
 
80
+
81
+  cmScRC_t      cmScoreSeqNotify( cmScH_t h );
82
+  
83
+  typedef enum
84
+  {
85
+    kInvalidMsgScId,
86
+    kBeginMsgScId,
87
+    kEventMsgScId,
88
+    kEndMsgScId
89
+  } cmScMsgTypeId_t;
90
+
91
+  typedef struct
92
+  {
93
+    cmScMsgTypeId_t typeId;
94
+    cmScoreEvt_t    evt;    // only used when typeId == kEventMsgScId
95
+  } cmScMsg_t;
96
+
97
+  cmScRC_t      cmScoreDecode( const void* msg, unsigned msgByteCnt, cmScMsg_t* );
98
+
70 99
   void          cmScorePrint( cmScH_t h, cmRpt_t* rpt );
71 100
 
72 101
   cmScRC_t      cmScoreSyncTimeLine( cmScH_t scH, cmTlH_t tlH, unsigned editDistWndCnt, cmReal_t maxNoteOffsetSecs );
73 102
 
74 103
   cmScRC_t      cmScoreSyncTimeLineTest( cmCtx_t* ctx,  const cmChar_t* timeLineJsFn, const cmChar_t* scoreCsvFn );
75 104
 
76
-  void           cmScoreTest( cmCtx_t* ctx, const cmChar_t* fn );
105
+  void          cmScoreTest( cmCtx_t* ctx, const cmChar_t* fn );
77 106
 
78 107
 #ifdef __cplusplus
79 108
 }

Loading…
Cancel
Save