libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmScore.c 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmRpt.h"
  5. #include "cmErr.h"
  6. #include "cmCtx.h"
  7. #include "cmMem.h"
  8. #include "cmMallocDebug.h"
  9. #include "cmMidi.h"
  10. #include "cmLex.h"
  11. #include "cmCsv.h"
  12. #include "cmMidiFile.h"
  13. #include "cmAudioFile.h"
  14. #include "cmTimeLine.h"
  15. #include "cmScore.h"
  16. #include "cmVectOpsTemplateMain.h"
  17. cmScH_t cmScNullHandle = cmSTATIC_NULL_HANDLE;
  18. enum
  19. {
  20. kLabelCharCnt = 7,
  21. kInvalidDynScId = 0,
  22. };
  23. enum
  24. {
  25. kMidiFileIdColScIdx= 0,
  26. kTypeLabelColScIdx = 3,
  27. kDSecsColScIdx = 4,
  28. kSecsColScIdx = 5,
  29. kPitchColScIdx = 11,
  30. kBarColScIdx = 13,
  31. kSkipColScIdx = 14,
  32. kEvenColScIdx = 15,
  33. kGraceColScIdx = 16,
  34. kTempoColScIdx = 17,
  35. kFracColScIdx = 18,
  36. kDynColScIdx = 19,
  37. kSectionColScIdx = 20,
  38. kRemarkColScIdx = 21
  39. };
  40. typedef struct
  41. {
  42. unsigned id;
  43. unsigned flag;
  44. cmChar_t label[ kLabelCharCnt + 1 ];
  45. } cmScEvtRef_t;
  46. typedef struct cmScSect_str
  47. {
  48. const cmChar_t* label;
  49. unsigned startIdx;
  50. struct cmScSect_str* link;
  51. } cmScSect_t;
  52. typedef struct cmScSetEle_str
  53. {
  54. cmChar_t* label;
  55. unsigned eleIdx;
  56. struct cmScSetEle_str* link;
  57. } cmScSetEle_t;
  58. typedef struct cmScSet_str
  59. {
  60. unsigned typeFl; // type of this set
  61. cmScSetEle_t* eles; // indexes of set elements
  62. cmScSetEle_t* sects; // application section labels
  63. bool inFl; // true if currently accepting elements
  64. struct cmScSet_str* link; //
  65. } cmScSet_t;
  66. typedef struct
  67. {
  68. cmErr_t err;
  69. cmCsvH_t cH;
  70. cmScCb_t cbFunc;
  71. void* cbArg;
  72. cmChar_t* fn;
  73. double srate;
  74. cmScoreEvt_t* array;
  75. unsigned cnt;
  76. cmScoreLoc_t* loc;
  77. unsigned locCnt;
  78. cmScoreSection_t* sect;
  79. unsigned sectCnt;
  80. unsigned sciPitchLexTId; // sci pitch and section id lexer token id's
  81. unsigned sectionLexTId;
  82. cmScSect_t* sectList; // lists used during parsing
  83. cmScSet_t* setList;
  84. cmScoreSet_t* sets;
  85. unsigned setCnt;
  86. unsigned* dynRefArray;
  87. unsigned dynRefCnt;
  88. unsigned nxtLocIdx;
  89. unsigned minSetLocIdx;
  90. unsigned maxSetLocIdx;
  91. } cmSc_t;
  92. cmScEvtRef_t _cmScEvtRefArray[] =
  93. {
  94. { kTimeSigEvtScId, 0, "tsg" },
  95. { kKeySigEvtScId, 0, "ksg" },
  96. { kTempoEvtScId, 0, "tmp" },
  97. { kTrackEvtScId, 0, "trk" },
  98. { kTextEvtScId, 0, "txt" },
  99. { kEOTrackEvtScId, 0, "eot" },
  100. { kCopyEvtScId, 0, "cpy" },
  101. { kBlankEvtScId, 0, "blk" },
  102. { kBarEvtScId, 0, "bar" },
  103. { kPgmEvtScId, 0, "pgm" },
  104. { kCtlEvtScId, 0, "ctl" },
  105. { kNonEvtScId, 0, "non" },
  106. { kInvalidEvtScId, 0, "***" }
  107. };
  108. cmScEvtRef_t _cmScDynRefArray[] =
  109. {
  110. { 1, 0, "pppp" },
  111. { 2, 0, "ppp" },
  112. { 3, 0, "pp" },
  113. { 4, 0, "p" },
  114. { 5, 0, "mp" },
  115. { 6, 0, "mf" },
  116. { 7, 0, "f" },
  117. { 8, 0, "ff" },
  118. { 9,0, "fff" },
  119. { kInvalidDynScId,0, "***" },
  120. };
  121. cmScEvtRef_t _cmScVarRefArray[] =
  122. {
  123. { kEvenVarScId, kEvenScFl, "e"},
  124. { kDynVarScId, kDynScFl, "d"},
  125. { kTempoVarScId,kTempoScFl,"t"},
  126. { cmInvalidId, 0, "@"}
  127. };
  128. cmSc_t* _cmScHandleToPtr( cmScH_t h )
  129. {
  130. cmSc_t* p = (cmSc_t*)h.h;
  131. assert( p != NULL );
  132. return p;
  133. }
  134. unsigned _cmScEvtTypeLabelToId( const cmChar_t* label )
  135. {
  136. cmScEvtRef_t* r = _cmScEvtRefArray;
  137. for(; r->id != kInvalidEvtScId; ++r )
  138. if( strcmp(label,r->label) == 0 )
  139. return r->id;
  140. return kInvalidEvtScId;
  141. }
  142. const cmChar_t* cmScEvtTypeIdToLabel( unsigned id )
  143. {
  144. cmScEvtRef_t* r = _cmScEvtRefArray;
  145. for(; r->id != kInvalidEvtScId; ++r )
  146. if( r->id == id )
  147. return r->label;
  148. return NULL;
  149. }
  150. unsigned _cmScDynLabelToId( const cmChar_t* label )
  151. {
  152. cmScEvtRef_t* r = _cmScDynRefArray;
  153. for(; r->id != kInvalidEvtScId; ++r )
  154. if( strncmp(label,r->label,strlen(r->label)) == 0 )
  155. return r->id;
  156. return kInvalidDynScId;
  157. }
  158. // return the count of dynamic label/id pairs
  159. unsigned _cmScDynLabelCount( )
  160. {
  161. unsigned n = 0;
  162. cmScEvtRef_t* r = _cmScDynRefArray;
  163. for(; r->id != kInvalidEvtScId; ++r )
  164. ++n;
  165. return n;
  166. }
  167. const cmChar_t* cmScDynIdToLabel( unsigned id )
  168. {
  169. cmScEvtRef_t* r = _cmScDynRefArray;
  170. for(; r->id != kInvalidDynScId; ++r )
  171. if( r->id == id )
  172. return r->label;
  173. return NULL;
  174. }
  175. char _cmScVarFlagToChar( unsigned flags )
  176. {
  177. unsigned i;
  178. for(i=0; _cmScVarRefArray[i].id!=cmInvalidId; ++i)
  179. if( _cmScVarRefArray[i].flag == flags )
  180. return _cmScVarRefArray[i].label[0];
  181. assert(0);
  182. return ' ';
  183. }
  184. char _cmScVarIdToChar( unsigned id )
  185. {
  186. unsigned i;
  187. for(i=0; _cmScVarRefArray[i].id!=cmInvalidId; ++i)
  188. if( _cmScVarRefArray[i].id == id )
  189. return _cmScVarRefArray[i].label[0];
  190. assert(0);
  191. return ' ';
  192. }
  193. unsigned _cmScVarFlagToId( unsigned flags )
  194. {
  195. unsigned i;
  196. for(i=0; _cmScVarRefArray[i].id!=cmInvalidId; ++i)
  197. if( _cmScVarRefArray[i].flag == flags )
  198. return _cmScVarRefArray[i].id;
  199. assert(0);
  200. return cmInvalidId;
  201. }
  202. const char* _cmScFlagsToStr( unsigned flags, char* buf, int bufCharCnt )
  203. {
  204. unsigned i=0;
  205. if( cmIsFlag(flags,kEvenScFl) )
  206. {
  207. assert(i<bufCharCnt);
  208. buf[i] = 'e';
  209. ++i;
  210. }
  211. if( cmIsFlag(flags,kDynScFl) )
  212. {
  213. assert(i<bufCharCnt);
  214. buf[i] = 'd';
  215. ++i;
  216. }
  217. if( cmIsFlag(flags,kTempoScFl ))
  218. {
  219. assert(i<bufCharCnt);
  220. buf[i] = 't';
  221. ++i;
  222. }
  223. assert(i<bufCharCnt);
  224. buf[i] = 0;
  225. return buf;
  226. }
  227. unsigned _cmScLexSciPitchMatcher( const cmChar_t* cp, unsigned cn )
  228. {
  229. if( cp==NULL || cn < 2 )
  230. return 0;
  231. // first char must be "A-G"
  232. if( strspn(cp,"ABCDEFG") != 1 )
  233. return 0;
  234. unsigned i = 1;
  235. // next char could be accidental
  236. if( cp[i] == '#' || cp[i] == 'b' )
  237. ++i; // i==2
  238. // the 2nd or 3rd char must be a digit
  239. if( i>=cn || isdigit(cp[i]) == false )
  240. return 0;
  241. ++i; // i==2 or i==3
  242. // the 3rd or 4th char must be a digit or EOS
  243. if( i>=cn || isdigit(cp[i]) == false )
  244. return i;
  245. ++i;
  246. return i;
  247. }
  248. unsigned _cmScLexSectionIdMatcher( const cmChar_t* cp, unsigned cn )
  249. {
  250. if( cp==NULL || cn < 2 )
  251. return 0;
  252. // first char must be a number
  253. if( !isdigit(cp[0]) )
  254. return 0;
  255. // if 2nd char is a char then terminate
  256. if( 'a'<=cp[1] && cp[1]<='z' )
  257. return 2;
  258. // if 2nd char is digit and 3rd char is char then terminate
  259. if( isdigit(cp[1]) && cn>2 && 'a'<=cp[2] && cp[2]<='z' )
  260. return 3;
  261. return 0;
  262. }
  263. void _cmScFreeSetList( cmScSet_t* setList )
  264. {
  265. cmScSet_t* tp = setList;
  266. cmScSet_t* ntp = NULL;
  267. while(tp!=NULL)
  268. {
  269. ntp = tp->link;
  270. cmScSetEle_t* ep = tp->eles;
  271. while( ep != NULL )
  272. {
  273. cmScSetEle_t* nep = ep->link;
  274. cmMemFree(ep);
  275. ep = nep;
  276. }
  277. ep = tp->sects;
  278. while( ep != NULL )
  279. {
  280. cmScSetEle_t* nep = ep->link;
  281. cmMemFree(ep->label);
  282. cmMemFree(ep);
  283. ep = nep;
  284. }
  285. cmMemFree(tp);
  286. tp = ntp;
  287. }
  288. }
  289. cmScRC_t _cmScFinalize( cmSc_t* p )
  290. {
  291. cmScRC_t rc = kOkScRC;
  292. unsigned i;
  293. if( cmCsvFinalize(&p->cH) != kOkCsvRC )
  294. return rc;
  295. if( p->sets != NULL )
  296. {
  297. for(i=0; i<p->setCnt; ++i)
  298. {
  299. cmMemFree(p->sets[i].eleArray);
  300. //cmMemFree(p->sets[i].sectArray);
  301. }
  302. cmMemFree(p->sets);
  303. }
  304. _cmScFreeSetList(p->setList);
  305. if( p->loc != NULL )
  306. {
  307. for(i=0; i<p->locCnt; ++i)
  308. {
  309. cmMemFree(p->loc[i].evtArray);
  310. if( p->loc[i].begSectPtr != NULL )
  311. cmMemFree(p->loc[i].begSectPtr->setArray);
  312. }
  313. cmMemFree(p->loc);
  314. }
  315. cmMemPtrFree(&p->dynRefArray);
  316. cmMemFree(p->sect);
  317. cmMemFree(p->fn);
  318. cmMemFree(p->array);
  319. cmMemFree(p);
  320. return rc;
  321. }
  322. cmScRC_t _cmScParseBar( cmSc_t* p, unsigned rowIdx, cmScoreEvt_t* s, int* barNumb )
  323. {
  324. if((*barNumb = cmCsvCellInt(p->cH,rowIdx,kBarColScIdx)) == INT_MAX )
  325. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unable to parse the bar number.");
  326. s->type = kBarEvtScId;
  327. s->secs = 0;
  328. s->barNumb = *barNumb;
  329. s->csvRowNumb = rowIdx + 1;
  330. return kOkScRC;
  331. }
  332. cmScSet_t* _cmScNewSet( cmSc_t* p, unsigned typeFl )
  333. {
  334. // create a new set record
  335. cmScSet_t* nsp = cmMemAllocZ(cmScSet_t,1);
  336. nsp->inFl = true;
  337. nsp->typeFl = typeFl;
  338. if( p->setList == NULL )
  339. p->setList = nsp;
  340. else
  341. {
  342. // go to the end of the the set list
  343. cmScSet_t* sp = p->setList;
  344. assert(sp!=NULL);
  345. while( sp->link != NULL )
  346. sp = sp->link;
  347. sp->link = nsp;
  348. }
  349. return nsp;
  350. }
  351. cmScSet_t* _cmScFindSet( cmSc_t* p, unsigned typeFl )
  352. {
  353. // locate the set currently accepting ele's for this type
  354. cmScSet_t* sp = p->setList;
  355. for(; sp != NULL; sp=sp->link )
  356. if( sp->typeFl == typeFl && sp->inFl )
  357. break;
  358. return sp;
  359. }
  360. void _cmScSetDone(cmSc_t* p, unsigned typeFl)
  361. {
  362. cmScSet_t* sp = _cmScFindSet(p,typeFl);
  363. assert( sp != NULL );
  364. sp->inFl = false;
  365. }
  366. // This function is called during parsing to
  367. // insert a set element or set section into a cmScSet_t
  368. // element or section linked list. Either the scoreIdx
  369. // or the label is valid but not both.
  370. cmScSet_t* _cmScInsertSetEle(cmSc_t* p, unsigned scoreIdx, unsigned typeFl, const cmChar_t* label, unsigned labelCharCnt)
  371. {
  372. assert( scoreIdx!=cmInvalidId || (scoreIdx==cmInvalidIdx && label!=NULL && labelCharCnt>0));
  373. cmScSet_t* sp = _cmScFindSet(p,typeFl);
  374. if( sp == NULL )
  375. sp = _cmScNewSet(p,typeFl);
  376. // allocate a new set element record
  377. cmScSetEle_t* nep = cmMemAllocZ(cmScSetEle_t,1);
  378. cmScSetEle_t** list = NULL;
  379. nep->eleIdx = scoreIdx;
  380. if( label == NULL )
  381. {
  382. // all elements must be of the same type
  383. assert( sp->typeFl == typeFl );
  384. sp->typeFl = typeFl;
  385. list = &sp->eles;
  386. }
  387. else
  388. {
  389. nep->label = cmMemAllocStrN(label,labelCharCnt);
  390. list = &sp->sects;
  391. }
  392. // *list refers to sp->sects or sp->ele's depending on the type of ele
  393. if( *list == NULL )
  394. *list = nep;
  395. else
  396. {
  397. // got to the last element in the set
  398. cmScSetEle_t* ep = *list;
  399. while( ep->link != NULL )
  400. ep = ep->link;
  401. // append the new element to the end of the list
  402. ep->link = nep;
  403. }
  404. return sp;
  405. }
  406. // Extract the next attribute section identifier.
  407. const cmChar_t* _cmScParseOneSetSection( cmSc_t* p, unsigned typeFl, const cmChar_t* c0p )
  408. {
  409. // advance white space
  410. while( *c0p && (isspace(*c0p) || *c0p==',') )
  411. ++c0p;
  412. if( *c0p==0 )
  413. return c0p;
  414. // c0p now points to a section id or an asterisk
  415. const cmChar_t* c1p = c0p;
  416. // advance past section id
  417. while( *c1p && (!isspace(*c1p) && (isdigit(*c1p) || isalpha(*c1p))))
  418. ++c1p;
  419. // if c0p pointed to an asterisk then c1p is still equal to c0p
  420. if( c1p > c0p )
  421. _cmScInsertSetEle(p,cmInvalidIdx,typeFl,c0p,c1p-c0p);
  422. return c1p;
  423. }
  424. // Parse an attribute string to extract the section
  425. // identifiers which may follow the attribute token (e,t,mf,ff,...)
  426. cmScRC_t _cmScParseAttr(cmSc_t* p, unsigned scoreIdx, const cmChar_t* text, unsigned typeFl)
  427. {
  428. const cmChar_t* cp = text;
  429. // insert a set element - all attribute's produce one element record
  430. _cmScInsertSetEle(p,scoreIdx,typeFl,NULL,0);
  431. // advance past the attribute type marking (e,t,(p,mf,f,fff,etc)) in search
  432. // of section identifiers
  433. while( *cp && !isspace(*cp) )
  434. ++cp;
  435. if( *cp )
  436. {
  437. // search for the first section identifier
  438. if((cp =_cmScParseOneSetSection(p,typeFl,cp)) != NULL )
  439. {
  440. bool asteriskFl = false;
  441. // search for the second section identifier
  442. if((cp = _cmScParseOneSetSection(p,typeFl,cp)) != NULL && *cp!=0 )
  443. asteriskFl = *cp == '*';
  444. _cmScSetDone(p,typeFl);
  445. // if the attr just parsed ended with an asterisk then it is both
  446. // the last element of the previous set and the first ele of the
  447. // next set
  448. if( asteriskFl )
  449. {
  450. // if the attr just parsed had a section id then it was the last
  451. // element in the set - create a new set record to hold the next set
  452. _cmScNewSet(p,typeFl);
  453. _cmScInsertSetEle(p,scoreIdx,typeFl,NULL,0);
  454. }
  455. }
  456. }
  457. return kOkScRC;
  458. }
  459. void _cmScPrintSets( const cmChar_t* label, cmScSet_t* setList )
  460. {
  461. printf("%s\n",label);
  462. const cmScSet_t* sp = setList;
  463. for(; sp != NULL; sp=sp->link )
  464. {
  465. const cmScSetEle_t* ep = sp->eles;
  466. for(; ep!=NULL; ep=ep->link)
  467. printf("%i ",ep->eleIdx);
  468. printf(" : ");
  469. for(ep=sp->sects; ep!=NULL; ep=ep->link)
  470. printf("%s ",cmStringNullGuard(ep->label));
  471. printf("\n");
  472. }
  473. }
  474. cmScRC_t _cmScParseNoteOn( cmSc_t* p, unsigned rowIdx, cmScoreEvt_t* s, unsigned scoreIdx, int barNumb, unsigned barNoteIdx )
  475. {
  476. cmScRC_t rc = kOkScRC;
  477. unsigned flags = 0;
  478. unsigned dynVal = kInvalidDynScId;
  479. const cmChar_t* sciPitch;
  480. cmMidiByte_t midiPitch;
  481. const cmChar_t* attr;
  482. double secs;
  483. double durSecs;
  484. const cmCsvCell_t* cell;
  485. s += scoreIdx;
  486. // verify the scientific pitch cell was formatted correcly
  487. if((cell = cmCsvCellPtr(p->cH,rowIdx,kPitchColScIdx)) == NULL || cell->lexTId != p->sciPitchLexTId )
  488. return cmErrMsg(&p->err,kSyntaxErrScRC,"Pitch column format error.");
  489. if((sciPitch = cmCsvCellText(p->cH,rowIdx,kPitchColScIdx)) == NULL )
  490. return cmErrMsg(&p->err,kSyntaxErrScRC,"Expected a scientific pitch value");
  491. if((midiPitch = cmSciPitchToMidi(sciPitch)) == kInvalidMidiPitch)
  492. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unable to convert the scientific pitch '%s' to a MIDI value. ");
  493. // it is possible that note delta-secs field is empty - so default to 0
  494. if((secs = cmCsvCellDouble(p->cH, rowIdx, kSecsColScIdx )) == DBL_MAX) // Returns DBL_MAX on error.
  495. flags += kInvalidScFl;
  496. // skip attribute
  497. if((attr = cmCsvCellText(p->cH,rowIdx,kSkipColScIdx)) != NULL && *attr == 's' )
  498. flags += kSkipScFl;
  499. // evenness attribute
  500. if((attr = cmCsvCellText(p->cH,rowIdx,kEvenColScIdx)) != NULL && *attr == 'e' )
  501. {
  502. flags += kEvenScFl;
  503. _cmScParseAttr(p,scoreIdx,attr,kEvenScFl);
  504. }
  505. // grace attribute
  506. if((attr = cmCsvCellText(p->cH,rowIdx,kGraceColScIdx)) != NULL && *attr == 'g' )
  507. {
  508. flags += kGraceScFl;
  509. if( cmIsNotFlag(flags,kEvenScFl) )
  510. return cmErrMsg(&p->err,kSyntaxErrScRC,"All 'grace' notes should also be 'even' notes.");
  511. }
  512. // tempo attribute
  513. if((attr = cmCsvCellText(p->cH,rowIdx,kTempoColScIdx)) != NULL && *attr == 't' )
  514. {
  515. flags += kTempoScFl;
  516. _cmScParseAttr(p,scoreIdx,attr,kTempoScFl);
  517. }
  518. // dynamics attribute
  519. if((attr = cmCsvCellText(p->cH,rowIdx,kDynColScIdx)) != NULL )
  520. {
  521. if((dynVal = _cmScDynLabelToId(attr)) == kInvalidDynScId )
  522. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unknown dynamic label '%s'.",cmStringNullGuard(attr));
  523. flags += kDynScFl;
  524. _cmScParseAttr(p,scoreIdx,attr,kDynScFl);
  525. }
  526. // tempo/non-grace rythmic value
  527. if( cmIsFlag(flags,kTempoScFl) || (cmIsFlag(flags,kEvenScFl) && cmIsNotFlag(flags,kGraceScFl)) )
  528. {
  529. double frac = cmCsvCellDouble(p->cH,rowIdx,kFracColScIdx);
  530. // no 'frac' value is given for the last note of the set we must accept error
  531. // values here and validate the actual values later
  532. if( frac>0 && frac!=DBL_MAX )
  533. s->frac = frac;
  534. }
  535. // Returns DBL_MAX on error.
  536. if((durSecs = cmCsvCellDouble(p->cH, rowIdx, kDSecsColScIdx )) == DBL_MAX)
  537. durSecs = 0.25;
  538. s->type = kNonEvtScId;
  539. s->secs = secs;
  540. s->pitch = midiPitch;
  541. s->flags = flags;
  542. s->dynVal = dynVal;
  543. s->barNumb = barNumb;
  544. s->barNoteIdx = barNoteIdx;
  545. s->durSecs = durSecs;
  546. s->csvRowNumb = rowIdx+1;
  547. return rc;
  548. }
  549. cmScRC_t _cmScParseSectionColumn( cmSc_t* p, unsigned rowIdx, unsigned evtIdx, cmScSect_t* sectList )
  550. {
  551. const cmCsvCell_t* cell;
  552. cmScSect_t* sect;
  553. const cmChar_t* label;
  554. // most rows don't have section labels
  555. if( (cell = cmCsvCellPtr( p->cH,rowIdx,kSectionColScIdx)) == NULL
  556. || (label = cmCsvCellText(p->cH,rowIdx,kSectionColScIdx)) == NULL)
  557. return kOkScRC;
  558. // verify the section id type
  559. if( cell->lexTId != p->sectionLexTId && cell->lexTId != kIntLexTId )
  560. return cmErrMsg(&p->err,kSyntaxErrScRC,"'section' column format error.");
  561. sect = cmMemAllocZ(cmScSect_t,1);
  562. sect->label = label;
  563. sect->startIdx = evtIdx;
  564. //printf("section:%s\n",label);
  565. cmScSect_t* sp = sectList;
  566. assert( sp != NULL );
  567. while( sp->link != NULL )
  568. sp = sp->link;
  569. sp->link = sect;
  570. return kOkScRC;
  571. }
  572. cmScoreSection_t* _cmScLabelToSection( cmSc_t* p, const cmChar_t* label )
  573. {
  574. int i;
  575. for(i=0; i<p->sectCnt; ++i)
  576. if( strcmp(p->sect[i].label,label) == 0 )
  577. return p->sect + i;
  578. return NULL;
  579. }
  580. // Calculate the total number of all types of sets and
  581. // then convert each of the cmScSet_t linked list's to
  582. // a single linear cmScoreSet_t list (p->sets[]).
  583. cmScRC_t _cmScProcSets( cmSc_t* p )
  584. {
  585. cmScRC_t rc = kOkScRC;
  586. // calculate the count of all sets
  587. unsigned i;
  588. unsigned n = 0;
  589. cmScSet_t* sp = p->setList;
  590. for(n=0; sp!=NULL; sp=sp->link)
  591. if( sp->eles != NULL )
  592. ++n;
  593. // allocate the linear set array
  594. p->sets = cmMemAllocZ(cmScoreSet_t,n);
  595. p->setCnt = n;
  596. // fill in the linear set array
  597. sp = p->setList;
  598. for(i=0; sp!=NULL; sp=sp->link)
  599. if( sp->eles != NULL )
  600. {
  601. unsigned j;
  602. unsigned en;
  603. unsigned rowNumb = 0;
  604. assert( i<n );
  605. // get the count of elements assoc'd with this set
  606. cmScSetEle_t* ep = sp->eles;
  607. for(en=0; ep!=NULL; ep=ep->link)
  608. ++en;
  609. assert( en > 0 );
  610. // allocate the element array
  611. p->sets[i].eleCnt = en;
  612. p->sets[i].eleArray = cmMemAllocZ(cmScoreEvt_t*,en);
  613. // fill in the element array
  614. ep = sp->eles;
  615. unsigned graceCnt = 0;
  616. for(j=0; ep!=NULL; ep=ep->link,++j)
  617. {
  618. assert(ep->eleIdx != cmInvalidIdx && ep->eleIdx<p->cnt);
  619. p->sets[i].eleArray[j] = p->array + ep->eleIdx;
  620. assert( cmIsFlag( p->sets[i].eleArray[j]->flags, sp->typeFl) );
  621. rowNumb = p->array[ep->eleIdx].csvRowNumb;
  622. unsigned flags = p->array[ep->eleIdx].flags;
  623. // count grace notes
  624. if( cmIsFlag(flags,kGraceScFl) )
  625. ++graceCnt;
  626. // validate the 'frac' field - all but the last note in
  627. // tempo and non-grace evenness sets must have a non-zero 'frac' value.
  628. if( en>0 && j<en-1 && (sp->typeFl==kTempoScFl || (sp->typeFl==kEvenScFl && graceCnt==0)) && p->array[ep->eleIdx].frac==0)
  629. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The note on row number %i must have a non-zero 'frac' value.",p->array[ep->eleIdx].csvRowNumb);
  630. }
  631. // get the count of sections assoc'd with this set
  632. ep = sp->sects;
  633. for(en=0; ep!=NULL; ep=ep->link)
  634. ++en;
  635. // allocate the section array
  636. p->sets[i].varId = _cmScVarFlagToId(sp->typeFl);
  637. //p->sets[i].sectCnt = en;
  638. //p->sets[i].sectArray = cmMemAllocZ(cmScoreSection_t*,en);
  639. // fill in the section array with sections which this set will be applied to
  640. ep = sp->sects;
  641. for(j=0; ep!=NULL; ep=ep->link,++j)
  642. {
  643. cmScoreSection_t* sp;
  644. assert(ep->label != NULL);
  645. if((sp = _cmScLabelToSection(p, ep->label )) == NULL )
  646. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The section labelled '%s' could not be found for the set which includes row number %i.",ep->label,rowNumb);
  647. else
  648. {
  649. //= p->sets[i].sectArray[j];
  650. sp->setArray = cmMemResizeP(cmScoreSet_t*,sp->setArray,++sp->setCnt);
  651. sp->setArray[sp->setCnt-1] = p->sets + i;
  652. }
  653. }
  654. ++i;
  655. }
  656. assert(i==n);
  657. // assign each set to the location which contains it's last element.
  658. // (this is earliest point in the score location where all the
  659. // performance information contained in the set may be valid)
  660. for(i=0; i<p->setCnt; ++i)
  661. {
  662. assert( p->sets[i].eleCnt >= 1 );
  663. // get a ptr to the last element for the ith set
  664. const cmScoreEvt_t* ep = p->sets[i].eleArray[ p->sets[i].eleCnt-1 ];
  665. unsigned j,k;
  666. // find the location which contains the last element
  667. for(j=0; j<p->locCnt; ++j)
  668. {
  669. for(k=0; k<p->loc[j].evtCnt; ++k)
  670. if( p->loc[j].evtArray[k] == ep )
  671. break;
  672. if(k<p->loc[j].evtCnt)
  673. break;
  674. }
  675. assert( j<p->locCnt );
  676. // assign the ith set to the location which contains it's last element
  677. p->sets[i].llink = p->loc[j].setList;
  678. p->loc[j].setList = p->sets + i;
  679. }
  680. return rc;
  681. }
  682. cmScRC_t _cmScProcSections( cmSc_t* p, cmScSect_t* sectList )
  683. {
  684. cmScRC_t rc = kOkScRC;
  685. unsigned i;
  686. // count the sections
  687. cmScSect_t* sp = sectList;
  688. p->sectCnt = 0;
  689. for(; sp!=NULL; sp=sp->link)
  690. if( sp->label != NULL )
  691. ++p->sectCnt;
  692. // alloc a section array
  693. p->sect = cmMemAllocZ(cmScoreSection_t,p->sectCnt);
  694. // fill the section array
  695. sp = sectList;
  696. for(i=0; sp!=NULL; sp=sp->link)
  697. if( sp->label != NULL )
  698. {
  699. p->sect[i].label = sp->label;
  700. p->sect[i].index = i;
  701. p->sect[i].begEvtIndex = sp->startIdx;
  702. ++i;
  703. }
  704. // assign the begSectPtr to each section
  705. for(i=0; i<p->sectCnt; ++i)
  706. {
  707. assert( p->sect[i].begEvtIndex < p->cnt );
  708. unsigned j,k;
  709. const cmScoreEvt_t* ep = p->array + p->sect[i].begEvtIndex;
  710. for(j=0; j<p->locCnt; ++j)
  711. {
  712. for(k=0; k<p->loc[j].evtCnt; ++k)
  713. if( p->loc[j].evtArray[k] == ep )
  714. {
  715. p->loc[j].begSectPtr = p->sect + i;
  716. p->sect[i].locPtr = p->loc + j;
  717. break;
  718. }
  719. if( k<p->loc[j].evtCnt)
  720. break;
  721. }
  722. }
  723. // release the section linked list
  724. sp = sectList;
  725. cmScSect_t* np = NULL;
  726. while(sp!=NULL)
  727. {
  728. np = sp->link;
  729. cmMemFree(sp);
  730. sp = np;
  731. }
  732. //_cmScPrintSets("Sets",p->setList );
  733. _cmScProcSets(p);
  734. return rc;
  735. }
  736. cmScRC_t _cmScParseFile( cmSc_t* p, cmCtx_t* ctx, const cmChar_t* fn )
  737. {
  738. cmScRC_t rc = kOkScRC;
  739. unsigned barNoteIdx = 0;
  740. int barEvtIdx = cmInvalidIdx;
  741. int barNumb = 0;
  742. double secs;
  743. double cur_secs = 0;
  744. p->sectList = cmMemAllocZ(cmScSect_t,1); // section zero
  745. //_cmScNewSet(p); // preallocate the first set
  746. // initialize the CSV file parser
  747. if( cmCsvInitialize(&p->cH, ctx ) != kOkCsvRC )
  748. {
  749. rc = cmErrMsg(&p->err,kCsvFailScRC,"Score file initialization failed.");
  750. goto errLabel;
  751. }
  752. // register custom lex token matchers for sci-pitch and section id's
  753. if( cmCsvLexRegisterMatcher(p->cH, p->sciPitchLexTId = cmCsvLexNextAvailId(p->cH)+0, _cmScLexSciPitchMatcher ) != kOkCsvRC
  754. ||cmCsvLexRegisterMatcher(p->cH, p->sectionLexTId = cmCsvLexNextAvailId(p->cH)+1, _cmScLexSectionIdMatcher) != kOkCsvRC )
  755. {
  756. rc = cmErrMsg(&p->err,kCsvFailScRC,"CSV token matcher registration failed.");
  757. goto errLabel;
  758. }
  759. // parse the CSV file
  760. if( cmCsvParseFile(p->cH, fn, 0 ) != kOkCsvRC )
  761. {
  762. rc = cmErrMsg(&p->err,kCsvFailScRC,"CSV file parsing failed on the file '%s'.",cmStringNullGuard(fn));
  763. goto errLabel;
  764. }
  765. // allocate the score event array
  766. p->cnt = cmCsvRowCount(p->cH);
  767. p->array = cmMemAllocZ(cmScoreEvt_t,p->cnt);
  768. unsigned i,j;
  769. // skip labels line - start on line 1
  770. for(i=1,j=0; i<p->cnt && rc==kOkScRC; ++i)
  771. {
  772. // get the row 'type' label
  773. const char* typeLabel;
  774. if((typeLabel = cmCsvCellText(p->cH,i,kTypeLabelColScIdx)) == NULL )
  775. {
  776. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"No type label.");
  777. break;
  778. }
  779. // convert the row 'type' label to an id
  780. unsigned tid;
  781. if((tid = _cmScEvtTypeLabelToId(typeLabel)) == kInvalidEvtScId)
  782. {
  783. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"Unknown type '%s'.",cmStringNullGuard(typeLabel));
  784. break;
  785. }
  786. secs = DBL_MAX;
  787. switch(tid)
  788. {
  789. case kBarEvtScId: // parse bar lines
  790. if((rc = _cmScParseBar(p,i,p->array+j,&barNumb)) == kOkScRC )
  791. {
  792. barNoteIdx = 0;
  793. barEvtIdx = j;
  794. p->array[j].index = j;
  795. ++j;
  796. }
  797. break;
  798. case kNonEvtScId: // parse note-on events
  799. if((rc = _cmScParseNoteOn(p, i, p->array, j, barNumb, barNoteIdx )) == kOkScRC )
  800. {
  801. secs = p->array[j].secs;
  802. if( p->array[j].secs == DBL_MAX )
  803. p->array[j].secs = cur_secs;
  804. if( cmIsFlag(p->array[j].flags,kSkipScFl) == false )
  805. {
  806. p->array[j].index = j;
  807. ++j;
  808. }
  809. ++barNoteIdx;
  810. }
  811. break;
  812. default:
  813. // Returns DBL_MAX on error.
  814. secs = cmCsvCellDouble(p->cH, i, kSecsColScIdx );
  815. break;
  816. }
  817. if( secs != DBL_MAX )
  818. cur_secs = secs;
  819. // form the section list
  820. if( j > 0 )
  821. if((rc = _cmScParseSectionColumn(p,i,j-1,p->sectList)) != kOkScRC )
  822. break;
  823. // the bar lines don't have times so set the time of the bar line to the
  824. // time of the first event in the bar.
  825. if( barEvtIdx != cmInvalidIdx && secs != DBL_MAX )
  826. {
  827. assert( p->array[ barEvtIdx ].type == kBarEvtScId );
  828. p->array[ barEvtIdx ].secs = secs;
  829. // handle the case where the previous bar had no events
  830. // BUG BUG BUG this is a hack which will fail if the first bar does not have events.
  831. if( barEvtIdx>=1 && p->array[ barEvtIdx-1].type == kBarEvtScId )
  832. p->array[ barEvtIdx-1].secs = secs;
  833. barEvtIdx = cmInvalidIdx;
  834. }
  835. }
  836. if( rc == kSyntaxErrScRC )
  837. {
  838. cmErrMsg(&p->err,rc,"Syntax error on line %i in '%s'.",i+1,cmStringNullGuard(fn));
  839. goto errLabel;
  840. }
  841. p->cnt = j;
  842. errLabel:
  843. return rc;
  844. }
  845. cmScRC_t _cmScInitLocArray( cmSc_t* p )
  846. {
  847. cmScRC_t rc = kOkScRC;
  848. double maxDSecs = 0; // max time between events that are considered simultaneous
  849. unsigned barNumb = 0;
  850. int i,j,k;
  851. if( p->cnt==0)
  852. return rc;
  853. p->locCnt = 1;
  854. // count the number of unique time locations in the score
  855. p->locCnt = 1;
  856. for(i=1; i<p->cnt; ++i )
  857. {
  858. if( p->array[i].secs < p->array[i-1].secs )
  859. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The time associated with the score entry on line %i is less than the previous line.",p->array[i].csvRowNumb);
  860. if( (p->array[i].secs - p->array[i-1].secs) > maxDSecs )
  861. ++p->locCnt;
  862. }
  863. if( rc != kOkScRC )
  864. return rc;
  865. // allocate the loc. array
  866. p->loc = cmMemAllocZ(cmScoreLoc_t,p->locCnt);
  867. // fill in the location array
  868. for(i=0,k=0; i<p->cnt; ++k)
  869. {
  870. j = i+1;
  871. // get the count of events at this location
  872. while( j<p->cnt && p->array[j].secs - p->array[j-1].secs <= maxDSecs )
  873. ++j;
  874. assert(k<p->locCnt);
  875. p->loc[k].index = k;
  876. p->loc[k].evtCnt = j-i;
  877. p->loc[k].evtArray = cmMemAllocZ(cmScoreEvt_t*,p->loc[k].evtCnt);
  878. // fill in the location record event pointers
  879. for(j=0; j<p->loc[k].evtCnt; ++j)
  880. {
  881. p->loc[k].evtArray[j] = p->array + (i + j);
  882. p->loc[k].evtArray[j]->locIdx = k;
  883. if( p->array[i+j].type == kBarEvtScId )
  884. barNumb = p->array[i+j].barNumb;
  885. }
  886. // fill in the location record
  887. p->loc[k].secs = p->array[i].secs;
  888. p->loc[k].barNumb = barNumb;
  889. i += p->loc[k].evtCnt;
  890. }
  891. assert( p->locCnt == k );
  892. return rc;
  893. }
  894. cmScRC_t cmScoreInitialize( cmCtx_t* ctx, cmScH_t* hp, const cmChar_t* fn, double srate, const unsigned* dynRefArray, unsigned dynRefCnt, cmScCb_t cbFunc, void* cbArg )
  895. {
  896. cmScRC_t rc = kOkScRC;
  897. if((rc = cmScoreFinalize(hp)) != kOkScRC )
  898. return rc;
  899. cmSc_t* p = cmMemAllocZ(cmSc_t,1);
  900. cmErrSetup(&p->err,&ctx->rpt,"Score");
  901. if((rc = _cmScParseFile(p,ctx,fn)) != kOkScRC )
  902. goto errLabel;
  903. if((rc = _cmScInitLocArray(p)) != kOkScRC )
  904. goto errLabel;
  905. if((rc = _cmScProcSections(p,p->sectList)) != kOkScRC )
  906. goto errLabel;
  907. // load the dynamic reference array
  908. if( dynRefArray != NULL && dynRefCnt > 0)
  909. {
  910. unsigned n = _cmScDynLabelCount();
  911. if( dynRefCnt != n )
  912. {
  913. rc = cmErrMsg(&p->err,kInvalidDynRefCntScRC,"The count of dynamics labels must be %i not %i.",n,dynRefCnt);
  914. goto errLabel;
  915. }
  916. p->dynRefArray = cmMemAllocZ(unsigned,dynRefCnt);
  917. memcpy(p->dynRefArray,dynRefArray,sizeof(unsigned)*dynRefCnt);
  918. p->dynRefCnt = dynRefCnt;
  919. }
  920. p->srate = srate;
  921. p->cbFunc = cbFunc;
  922. p->cbArg = cbArg;
  923. p->fn = cmMemAllocStr(fn);
  924. p->nxtLocIdx = 0;
  925. p->minSetLocIdx = cmInvalidIdx;
  926. p->maxSetLocIdx = cmInvalidIdx;
  927. hp->h = p;
  928. cmScoreClearPerfInfo(*hp);
  929. //cmScorePrintLoc(*hp);
  930. errLabel:
  931. if( rc != kOkScRC )
  932. _cmScFinalize(p);
  933. return rc;
  934. }
  935. cmScRC_t cmScoreFinalize( cmScH_t* hp )
  936. {
  937. cmScRC_t rc = kOkScRC;
  938. if( hp == NULL || cmScoreIsValid(*hp) == false )
  939. return kOkScRC;
  940. cmSc_t* p = _cmScHandleToPtr(*hp);
  941. if((rc = _cmScFinalize(p)) != kOkScRC )
  942. return rc;
  943. hp->h = NULL;
  944. return rc;
  945. }
  946. const cmChar_t* cmScoreFileName( cmScH_t h )
  947. {
  948. cmSc_t* p = _cmScHandleToPtr(h);
  949. return p->fn;
  950. }
  951. double cmScoreSampleRate( cmScH_t h )
  952. {
  953. cmSc_t* p = _cmScHandleToPtr(h);
  954. return p->srate;
  955. }
  956. bool cmScoreIsValid( cmScH_t h )
  957. { return h.h != NULL; }
  958. unsigned cmScoreEvtCount( cmScH_t h )
  959. {
  960. cmSc_t* p = _cmScHandleToPtr(h);
  961. return p->cnt;
  962. }
  963. cmScoreEvt_t* cmScoreEvt( cmScH_t h, unsigned idx )
  964. {
  965. cmSc_t* p = _cmScHandleToPtr(h);
  966. if( idx >= p->cnt )
  967. {
  968. cmErrMsg(&p->err,kInvalidIdxScRC,"%i is an invalid index for %i records.",idx,p->cnt);
  969. return NULL;
  970. }
  971. return p->array + idx;
  972. }
  973. unsigned cmScoreSectionCount( cmScH_t h )
  974. {
  975. cmSc_t* p = _cmScHandleToPtr(h);
  976. return p->sectCnt;
  977. }
  978. cmScoreSection_t* cmScoreSection( cmScH_t h, unsigned idx )
  979. {
  980. cmSc_t* p = _cmScHandleToPtr(h);
  981. assert( idx < p->sectCnt);
  982. return p->sect + idx;
  983. }
  984. unsigned cmScoreLocCount( cmScH_t h )
  985. {
  986. cmSc_t* p = _cmScHandleToPtr(h);
  987. return p->locCnt;
  988. }
  989. cmScoreLoc_t* cmScoreLoc( cmScH_t h, unsigned idx )
  990. {
  991. cmSc_t* p = _cmScHandleToPtr(h);
  992. if( idx >= p->locCnt )
  993. {
  994. cmErrMsg(&p->err,kInvalidIdxScRC,"%i is an invalid index for %i location records.",idx,p->locCnt);
  995. return NULL;
  996. }
  997. return p->loc + idx;
  998. }
  999. void cmScorePrintLoc( cmScH_t h )
  1000. {
  1001. unsigned i = 0;
  1002. cmSc_t* p = _cmScHandleToPtr(h);
  1003. unsigned colCnt = 10;
  1004. int bufCharCnt = 4;
  1005. char buf[ bufCharCnt ];
  1006. const char* emptyStr = " ";
  1007. // for each set of 'colCnt' columns
  1008. for(i=0; i<p->locCnt; i+=colCnt )
  1009. {
  1010. // print the location 'index' line
  1011. unsigned c,j,k;
  1012. printf("index: ");
  1013. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1014. printf("%7i ",j);
  1015. printf("\n");
  1016. // print the 'sectn' label line
  1017. printf("sectn: ");
  1018. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1019. if( p->loc[j].begSectPtr==NULL )
  1020. printf("%s",emptyStr);
  1021. else
  1022. printf("%7s ",p->loc[j].begSectPtr->label);
  1023. printf("\n");
  1024. // calculate the max number of simultan. events at any one location
  1025. // for this set of 'colCnt' columns.
  1026. unsigned n=0;
  1027. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1028. if( p->loc[j].evtCnt > n )
  1029. n = p->loc[j].evtCnt;
  1030. // for each 'sco' line
  1031. for(k=0; k<n; ++k)
  1032. {
  1033. printf("sco%2i: ",k);
  1034. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1035. if( k >= p->loc[j].evtCnt )
  1036. printf("%s",emptyStr);
  1037. else
  1038. {
  1039. switch(p->loc[j].evtArray[k]->type)
  1040. {
  1041. case kBarEvtScId:
  1042. printf(" |%3i ",p->loc[j].evtArray[k]->barNumb);
  1043. break;
  1044. case kNonEvtScId:
  1045. {
  1046. int bn=16;
  1047. char b[bn];
  1048. strcpy(b,cmMidiToSciPitch(p->loc[j].evtArray[k]->pitch,NULL,0));
  1049. strcat(b,_cmScFlagsToStr( p->loc[j].evtArray[k]->flags,buf,bufCharCnt));
  1050. assert(strlen(b)<bn);
  1051. printf("%7s ",b);
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. printf("\n");
  1057. }
  1058. // calc the max number of set triggers which will occur on
  1059. // any one location for this set of 'colCnt' columns.
  1060. n=0;
  1061. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1062. if(p->loc[j].begSectPtr != NULL && p->loc[j].begSectPtr->setCnt > n )
  1063. n = p->loc[j].begSectPtr->setCnt;
  1064. for(k=0; k<n; ++k)
  1065. {
  1066. printf("trig%1i: ",k);
  1067. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1068. {
  1069. if( p->loc[j].begSectPtr != NULL && k<p->loc[j].begSectPtr->setCnt )
  1070. {
  1071. const cmScoreSet_t* sp = p->loc[j].begSectPtr->setArray[k];
  1072. printf(" %3s-%c ",p->loc[j].begSectPtr->label,_cmScVarIdToChar(sp->varId) );
  1073. }
  1074. else
  1075. {
  1076. printf("%s",emptyStr);
  1077. }
  1078. }
  1079. printf("\n");
  1080. }
  1081. printf("\n");
  1082. }
  1083. }
  1084. unsigned cmScoreSetCount( cmScH_t h )
  1085. {
  1086. cmSc_t* p = _cmScHandleToPtr(h);
  1087. return p->setCnt;
  1088. }
  1089. cmScRC_t cmScoreSeqNotify( cmScH_t h )
  1090. {
  1091. cmScRC_t rc = kOkScRC;
  1092. cmSc_t* p = _cmScHandleToPtr(h);
  1093. cmScMsg_t m;
  1094. unsigned i;
  1095. if( p->cbFunc != NULL )
  1096. {
  1097. memset(&m.u.evt,0,sizeof(m.u.evt));
  1098. m.typeId = kBeginMsgScId;
  1099. p->cbFunc(p->cbArg,&m,sizeof(m));
  1100. m.typeId = kEventMsgScId;
  1101. for(i=0; i<p->cnt; ++i)
  1102. {
  1103. m.u.evt = p->array[i];
  1104. p->cbFunc(p->cbArg,&m,sizeof(m));
  1105. }
  1106. m.typeId = kSectionMsgScId;
  1107. for(i=0; i<p->sectCnt; ++i)
  1108. {
  1109. m.u.sect = p->sect[i];
  1110. p->cbFunc(p->cbArg,&m,sizeof(m));
  1111. }
  1112. memset(&m.u.evt,0,sizeof(m.u.evt));
  1113. m.typeId = kEndMsgScId;
  1114. p->cbFunc(p->cbArg,&m,sizeof(m));
  1115. }
  1116. return rc;
  1117. }
  1118. void cmScoreClearPerfInfo( cmScH_t h )
  1119. {
  1120. cmSc_t* p = _cmScHandleToPtr(h);
  1121. unsigned i;
  1122. for(i=0; i<p->cnt; ++i)
  1123. {
  1124. p->array[i].perfSmpIdx = cmInvalidIdx;
  1125. p->array[i].perfVel = 0;
  1126. p->array[i].perfDynLvl = 0;
  1127. }
  1128. for(i=0; i<p->locCnt; ++i)
  1129. {
  1130. cmScoreSet_t* sp = p->loc[i].setList;
  1131. for(; sp!=NULL; sp=sp->llink)
  1132. sp->doneFl = false;
  1133. }
  1134. for(i=0; i<p->sectCnt; ++i)
  1135. {
  1136. unsigned j;
  1137. for(j=0; j<kScVarCnt; ++j)
  1138. p->sect[i].vars[j] = DBL_MAX;
  1139. }
  1140. p->nxtLocIdx = 0;
  1141. p->minSetLocIdx = cmInvalidIdx;
  1142. p->maxSetLocIdx = cmInvalidIdx;
  1143. }
  1144. bool _cmScIsSetPerfDone( cmScoreSet_t* sp )
  1145. {
  1146. unsigned i = 0;
  1147. for(i=0; i<sp->eleCnt; ++i)
  1148. if( sp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1149. return false;
  1150. return true;
  1151. }
  1152. void _cmScPerfSortTimes( unsigned *v, unsigned n )
  1153. {
  1154. unsigned i;
  1155. bool fl = true;
  1156. while(fl && n)
  1157. {
  1158. fl = false;
  1159. for(i=1; i<n; ++i)
  1160. {
  1161. if( v[i-1] > v[i] )
  1162. {
  1163. unsigned t = v[i-1];
  1164. v[i-1] = v[i];
  1165. v[i] = t;
  1166. fl = true;
  1167. }
  1168. }
  1169. --n;
  1170. }
  1171. }
  1172. bool _cmScPerfEven(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1173. {
  1174. unsigned i = 0;
  1175. double u = 0;
  1176. double x = 0;
  1177. bool sortFl = false;
  1178. bool printFl = true;
  1179. unsigned v[ stp->eleCnt ];
  1180. unsigned d[ stp->eleCnt - 1];
  1181. assert( stp->eleCnt > 1 );
  1182. // calculate the sum of the time between events
  1183. for(i=0; i<stp->eleCnt; ++i)
  1184. {
  1185. // if this event was not received - then the set is not valid
  1186. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1187. {
  1188. if( printFl && printMissFl)
  1189. printf("EVENESS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1190. return false;
  1191. }
  1192. // load v[i]
  1193. v[i] = stp->eleArray[i]->perfSmpIdx;
  1194. // check for out of order elements
  1195. if( i> 0 )
  1196. if( v[i] < v[i-1] )
  1197. sortFl = true;
  1198. }
  1199. // sort the times in ascending order
  1200. if( sortFl )
  1201. _cmScPerfSortTimes(v,stp->eleCnt);
  1202. // calc the sum of time differences
  1203. for(i=1; i<stp->eleCnt; ++i)
  1204. u += d[i-1] = v[i] - v[i-1];
  1205. // calculate the average time between events
  1206. u /= stp->eleCnt-1;
  1207. // calculate the std-dev of the time between events
  1208. for(i=0; i<stp->eleCnt-1; ++i)
  1209. x += (d[i]-u)*(d[i]-u);
  1210. double sd = sqrt(x/(stp->eleCnt-1));
  1211. // compute the average z-score
  1212. double c = 0;
  1213. for(i=0; i<stp->eleCnt-1; ++i)
  1214. c += fabs(d[i]-u)/sd;
  1215. stp->value = c/(stp->eleCnt-1);
  1216. stp->doneFl = true;
  1217. if(printFl)
  1218. {
  1219. /*
  1220. for(i=0; i<stp->eleCnt; ++i)
  1221. {
  1222. printf("%i %i ",i,v[i]);
  1223. if( i > 0 )
  1224. printf("%i ", d[i-1]);
  1225. printf("\n");
  1226. }
  1227. */
  1228. printf("%s EVENESS:%f\n",sortFl?"SORTED ":"",stp->value);
  1229. }
  1230. return true;
  1231. }
  1232. bool _cmScPerfDyn( cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1233. {
  1234. double a = 0;
  1235. unsigned i = 0;
  1236. bool printFl = true;
  1237. for(i=0; i<stp->eleCnt; ++i)
  1238. {
  1239. unsigned j;
  1240. // if this event was not received - then the set is not valid
  1241. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1242. {
  1243. if( printFl && printMissFl )
  1244. printf("DYNAMICS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1245. return false;
  1246. }
  1247. unsigned m = 0; // lower bound for the first dyn. category
  1248. // determine the dynamic category for the performed velocity of each event
  1249. for(j=0; j<p->dynRefCnt; ++j)
  1250. {
  1251. // if the vel fall's into the jth dyn. category
  1252. if( m <= stp->eleArray[i]->perfVel && stp->eleArray[i]->perfVel < p->dynRefArray[j] )
  1253. break;
  1254. // store the min vel for the next dyn category
  1255. m = p->dynRefArray[j];
  1256. }
  1257. assert( j < p->dynRefCnt );
  1258. stp->eleArray[i]->perfDynLvl = j+1;
  1259. a += abs((j+1) - stp->eleArray[i]->dynVal);
  1260. if( p->cbFunc != NULL )
  1261. {
  1262. cmScMsg_t m;
  1263. m.typeId = kDynMsgScId;
  1264. m.u.dyn.evtIdx = stp->eleArray[i]->index;
  1265. m.u.dyn.dynLvl = stp->eleArray[i]->perfDynLvl;
  1266. p->cbFunc(p->cbArg,&m,sizeof(m));
  1267. }
  1268. }
  1269. stp->value = a / stp->eleCnt;
  1270. stp->doneFl = true;
  1271. if( printFl )
  1272. printf("DYNAMICS:%f\n",stp->value);
  1273. return true;
  1274. }
  1275. bool _cmScPerfTempo1(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1276. {
  1277. bool printFl = true;
  1278. unsigned durSmpCnt = 0;
  1279. double durBeats = 0;
  1280. int i;
  1281. for(i=0; i<stp->eleCnt; ++i)
  1282. {
  1283. // if this event was not received - then the set is not valid
  1284. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1285. {
  1286. if( printFl && printMissFl )
  1287. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1288. return false;
  1289. }
  1290. if( i > 0 )
  1291. durSmpCnt += stp->eleArray[i]->perfSmpIdx - stp->eleArray[i-1]->perfSmpIdx;
  1292. durBeats += stp->eleArray[i]->frac;
  1293. }
  1294. stp->value = durBeats / (durSmpCnt / p->srate*60.0 );
  1295. stp->doneFl = true;
  1296. if( printFl )
  1297. printf("TEMPO:%f\n",stp->value);
  1298. return true;
  1299. }
  1300. bool _cmScPerfTempo(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1301. {
  1302. bool printFl = true;
  1303. unsigned durSmpCnt = 0;
  1304. double durBeats = 0;
  1305. int i;
  1306. unsigned bi = cmInvalidIdx;
  1307. unsigned ei = cmInvalidIdx;
  1308. unsigned missCnt = 0;
  1309. for(i=0; i<stp->eleCnt; ++i)
  1310. {
  1311. // if this event was not received - then the set is not valid
  1312. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1313. {
  1314. ++missCnt;
  1315. if( printFl && printMissFl )
  1316. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1317. }
  1318. else
  1319. {
  1320. if( bi == cmInvalidIdx )
  1321. bi = i;
  1322. ei = i;
  1323. }
  1324. }
  1325. if( ei > bi )
  1326. {
  1327. for(i=bi; i<=ei; ++i)
  1328. durBeats += stp->eleArray[i]->frac;
  1329. durSmpCnt = stp->eleArray[ei]->perfSmpIdx - stp->eleArray[bi]->perfSmpIdx;
  1330. stp->value = durBeats / (durSmpCnt / (p->srate*60.0) );
  1331. stp->doneFl = true;
  1332. }
  1333. if( printFl )
  1334. printf("TEMPO:%f bi:%i ei:%i secs:%f bts:%f\n",stp->value,bi,ei,durSmpCnt/p->srate,durBeats);
  1335. return true;
  1336. }
  1337. void _cmScPerfExec( cmSc_t* p, cmScoreSet_t* sp, bool printMissFl )
  1338. {
  1339. if( sp->doneFl == false )
  1340. {
  1341. switch( sp->varId )
  1342. {
  1343. case kEvenVarScId:
  1344. _cmScPerfEven(p,sp,printMissFl);
  1345. break;
  1346. case kDynVarScId:
  1347. _cmScPerfDyn(p,sp,printMissFl);
  1348. break;
  1349. case kTempoVarScId:
  1350. _cmScPerfTempo(p,sp,printMissFl);
  1351. break;
  1352. default:
  1353. { assert(0); }
  1354. }
  1355. }
  1356. }
  1357. void _cmScPerfExecRange( cmSc_t* p )
  1358. {
  1359. if( p->minSetLocIdx == cmInvalidIdx || p->maxSetLocIdx==cmInvalidIdx )
  1360. return;
  1361. unsigned i = p->minSetLocIdx;
  1362. for(; i<=p->maxSetLocIdx; ++i)
  1363. {
  1364. cmScoreSet_t* sp = p->loc[i].setList;
  1365. for(; sp!=NULL; sp=sp->llink)
  1366. _cmScPerfExec(p,sp,true);
  1367. }
  1368. }
  1369. bool _cmScSetPerfEvent( cmSc_t* p, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1370. {
  1371. assert(locIdx < p->locCnt );
  1372. cmScoreLoc_t* lp = p->loc + locIdx;
  1373. bool foundFl = false;
  1374. bool doneFl = true;
  1375. unsigned i;
  1376. // locate the event at the loc[locIdx]
  1377. for(i=0; i<lp->evtCnt; ++i)
  1378. {
  1379. cmScoreEvt_t* ep = lp->evtArray[i];
  1380. if( ep->type == kNonEvtScId )
  1381. {
  1382. if( ep->pitch == pitch )
  1383. {
  1384. assert( ep->perfSmpIdx == cmInvalidIdx );
  1385. ep->perfSmpIdx = smpIdx;
  1386. ep->perfVel = vel;
  1387. foundFl = true;
  1388. }
  1389. // check if all notes have arrived for this location
  1390. if( ep->perfSmpIdx == cmInvalidIdx )
  1391. doneFl = false;
  1392. }
  1393. }
  1394. // the event must always be found
  1395. assert( foundFl );
  1396. return doneFl;
  1397. }
  1398. bool cmScoreSetPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1399. {
  1400. cmSc_t* p = _cmScHandleToPtr(h);
  1401. return _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1402. }
  1403. void cmScoreExecPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1404. {
  1405. unsigned i;
  1406. cmSc_t* p = _cmScHandleToPtr(h);
  1407. bool doneFl = _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1408. unsigned printLvl = 1;
  1409. cmScoreLoc_t* lp = p->loc + locIdx;
  1410. // all events for a location must be complete to trigger attached events
  1411. if( doneFl == false )
  1412. return;
  1413. if( p->loc[locIdx].setList != NULL )
  1414. {
  1415. // set idx of most recent loc w/ a set end event
  1416. p->maxSetLocIdx = locIdx;
  1417. if( p->minSetLocIdx == cmInvalidIdx )
  1418. p->minSetLocIdx = locIdx;
  1419. }
  1420. // attempt to calculate all sets between loc[p->minSetLocIdx] and loc[p->maxSetLocIdx]
  1421. _cmScPerfExecRange(p);
  1422. // prevent event retriggering or going backwards
  1423. if( printLvl && locIdx < p->nxtLocIdx )
  1424. {
  1425. printf("----- BACK ----- \n");
  1426. return;
  1427. }
  1428. if( printLvl && locIdx > p->nxtLocIdx )
  1429. {
  1430. printf("----- SKIP ----- \n");
  1431. }
  1432. // for each location between the current and previous location
  1433. for(; p->nxtLocIdx<=locIdx; ++p->nxtLocIdx)
  1434. {
  1435. lp = p->loc + p->nxtLocIdx;
  1436. // if this location is the start of a new section - then apply
  1437. // sets that are assigned to this section
  1438. if( lp->begSectPtr != NULL && lp->begSectPtr->setCnt > 0 )
  1439. {
  1440. // notice the location of the oldest section start - once we cross this point
  1441. // it is too late to notice set completions - so incr p->inSetLocIdx
  1442. if( lp->begSectPtr->setCnt )
  1443. p->minSetLocIdx = p->nxtLocIdx+1;
  1444. for(i=0; i<lp->begSectPtr->setCnt; ++i)
  1445. {
  1446. cmScoreSet_t* stp = lp->begSectPtr->setArray[i];
  1447. // temporarily commented out for testing purposes
  1448. // if( stp->doneFl == false )
  1449. // _cmScPerfExec(p, stp, printLvl>0 );
  1450. if( stp->doneFl )
  1451. {
  1452. assert( stp->varId < kScVarCnt );
  1453. lp->begSectPtr->vars[ stp->varId ] = stp->value;
  1454. if( p->cbFunc != NULL )
  1455. {
  1456. cmScMsg_t m;
  1457. m.typeId = kVarMsgScId;
  1458. m.u.meas.varId = stp->varId;
  1459. m.u.meas.value = stp->value;
  1460. p->cbFunc(p->cbArg,&m,sizeof(m));
  1461. }
  1462. }
  1463. }
  1464. }
  1465. }
  1466. }
  1467. void cmScoreSetPerfValue( cmScH_t h, unsigned locIdx, unsigned varId, double value )
  1468. {
  1469. cmSc_t* p = _cmScHandleToPtr(h);
  1470. int li = locIdx;
  1471. for(; li>=0; --li)
  1472. if( p->loc[li].begSectPtr != NULL )
  1473. {
  1474. assert( varId < kScVarCnt );
  1475. p->loc[li].begSectPtr->vars[varId] = value;
  1476. break;
  1477. }
  1478. assert( li>=0);
  1479. }
  1480. void cmScoreSetPerfDynLevel( cmScH_t h, unsigned evtIdx, unsigned dynLvl )
  1481. {
  1482. cmSc_t* p = _cmScHandleToPtr(h);
  1483. assert(evtIdx < p->cnt );
  1484. p->array[ evtIdx ].perfDynLvl = dynLvl;
  1485. }
  1486. cmScRC_t cmScoreDecode( const void* msg, unsigned msgByteCnt, cmScMsg_t* m)
  1487. {
  1488. cmScMsg_t* mp = (cmScMsg_t*)msg;
  1489. *m = *mp;
  1490. return kOkScRC;
  1491. }
  1492. void cmScorePrint( cmScH_t h, cmRpt_t* rpt )
  1493. {
  1494. cmSc_t* p = _cmScHandleToPtr(h);
  1495. unsigned i;
  1496. for(i=0; i<20 /*p->cnt*/; ++i)
  1497. {
  1498. cmScoreEvt_t* r = p->array + i;
  1499. switch(r->type)
  1500. {
  1501. case kNonEvtScId:
  1502. cmRptPrintf(rpt,"%5i %3i %3i %s 0x%2x %c%c%c %s\n",
  1503. i,
  1504. r->barNumb,
  1505. r->barNoteIdx,
  1506. cmScEvtTypeIdToLabel(r->type),
  1507. r->pitch,
  1508. cmIsFlag(r->flags,kEvenScFl) ? 'e' : ' ',
  1509. cmIsFlag(r->flags,kTempoScFl) ? 't' : ' ',
  1510. cmIsFlag(r->flags,kDynScFl) ? 'd' : ' ',
  1511. cmIsFlag(r->flags,kDynScFl) ? cmScDynIdToLabel(r->dynVal) : "");
  1512. break;
  1513. default:
  1514. break;
  1515. }
  1516. }
  1517. }
  1518. void cmScoreTest( cmCtx_t* ctx, const cmChar_t* fn )
  1519. {
  1520. cmScH_t h = cmScNullHandle;
  1521. if( cmScoreInitialize(ctx,&h,fn,0,NULL,0,NULL,NULL) != kOkScRC )
  1522. return;
  1523. cmScorePrint(h,&ctx->rpt);
  1524. cmScoreFinalize(&h);
  1525. }
  1526. // 1. Fix absolute message time which was incorrect on original score file.
  1527. // 2.
  1528. void cmScoreFix( cmCtx_t* ctx )
  1529. {
  1530. const cmChar_t* mfn = "/home/kevin/src/cmgv/src/gv/data/ImaginaryThemes.mid";
  1531. const cmChar_t* crfn = "/home/kevin/src/cmgv/src/gv/data/mod0a.txt";
  1532. const cmChar_t* cwfn = "/home/kevin/src/cmgv/src/gv/data/mod1.csv";
  1533. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  1534. cmCsvH_t csvH = cmCsvNullHandle;
  1535. const cmMidiTrackMsg_t** msg = NULL;
  1536. double secs = 0.0;
  1537. int ci,mi,crn,mn;
  1538. bool errFl = true;
  1539. unsigned handCnt = 0;
  1540. unsigned midiMissCnt = 0;
  1541. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  1542. goto errLabel;
  1543. if( cmCsvLexRegisterMatcher(csvH, cmCsvLexNextAvailId(csvH), _cmScLexSciPitchMatcher ) != kOkCsvRC )
  1544. goto errLabel;
  1545. if( cmCsvParseFile(csvH, crfn, 0 ) != kOkCsvRC )
  1546. goto errLabel;
  1547. if( cmMidiFileOpen(mfn,&mfH,ctx) != kOkMfRC )
  1548. goto errLabel;
  1549. cmMidiFileTickToMicros(mfH);
  1550. cmMidiFileCalcNoteDurations(mfH);
  1551. mn = cmMidiFileMsgCount(mfH);
  1552. msg = cmMidiFileMsgArray(mfH);
  1553. crn = cmCsvRowCount(csvH);
  1554. // for each row in the score file
  1555. for(ci=1,mi=0; ci<crn && cmCsvLastRC(csvH)==kOkCsvRC; ++ci)
  1556. {
  1557. unsigned id;
  1558. // zero the duration column
  1559. if( cmCsvCellPtr(csvH, ci, kDSecsColScIdx ) != NULL )
  1560. cmCsvSetCellUInt( csvH, ci, kDSecsColScIdx, 0 );
  1561. // get the MIDI file event id for this row
  1562. if((id = cmCsvCellUInt(csvH,ci,kMidiFileIdColScIdx)) == UINT_MAX)
  1563. {
  1564. // this is a hand-entered event - so it has no event id
  1565. ++handCnt;
  1566. }
  1567. else
  1568. {
  1569. for(; mi<mn; ++mi)
  1570. {
  1571. const cmMidiTrackMsg_t* m = msg[mi];
  1572. assert( mi+1 <= id );
  1573. secs += m->dtick/1000000.0;
  1574. if( mi+1 != id )
  1575. {
  1576. if( m->status == kNoteOnMdId && m->u.chMsgPtr->d1>0 )
  1577. {
  1578. // this MIDI note-on does not have a corresponding score event
  1579. ++midiMissCnt;
  1580. }
  1581. }
  1582. else
  1583. {
  1584. cmCsvSetCellDouble( csvH, ci, kSecsColScIdx, secs );
  1585. ++mi;
  1586. if( m->status == kNoteOnMdId )
  1587. cmCsvSetCellDouble( csvH, ci, kDSecsColScIdx, m->u.chMsgPtr->durTicks/1000000.0 );
  1588. break;
  1589. }
  1590. }
  1591. if( mi==mn)
  1592. printf("done on row:%i\n",ci);
  1593. }
  1594. }
  1595. if( cmCsvLastRC(csvH) != kOkCsvRC )
  1596. goto errLabel;
  1597. if( cmCsvWrite(csvH,cwfn) != kOkCsvRC )
  1598. goto errLabel;
  1599. errFl = false;
  1600. errLabel:
  1601. if( errFl )
  1602. printf("Score fix failed.\n");
  1603. else
  1604. printf("Score fix done! hand:%i miss:%i\n",handCnt,midiMissCnt);
  1605. cmMidiFileClose(&mfH);
  1606. cmCsvFinalize(&csvH);
  1607. }