libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmScore.c 47KB

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