libcm is a C development framework with an emphasis on audio signal processing applications.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  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. cmScRC_t cmScoreSeqNotify( cmScH_t h )
  1079. {
  1080. cmScRC_t rc = kOkScRC;
  1081. cmSc_t* p = _cmScHandleToPtr(h);
  1082. cmScMsg_t m;
  1083. unsigned i;
  1084. if( p->cbFunc != NULL )
  1085. {
  1086. memset(&m.u.evt,0,sizeof(m.u.evt));
  1087. m.typeId = kBeginMsgScId;
  1088. p->cbFunc(p->cbArg,&m,sizeof(m));
  1089. m.typeId = kEventMsgScId;
  1090. for(i=0; i<p->cnt; ++i)
  1091. {
  1092. m.u.evt = p->array[i];
  1093. p->cbFunc(p->cbArg,&m,sizeof(m));
  1094. }
  1095. m.typeId = kSectionMsgScId;
  1096. for(i=0; i<p->sectCnt; ++i)
  1097. {
  1098. m.u.sect = p->sect[i];
  1099. p->cbFunc(p->cbArg,&m,sizeof(m));
  1100. }
  1101. memset(&m.u.evt,0,sizeof(m.u.evt));
  1102. m.typeId = kEndMsgScId;
  1103. p->cbFunc(p->cbArg,&m,sizeof(m));
  1104. }
  1105. return rc;
  1106. }
  1107. void cmScoreClearPerfInfo( cmScH_t h )
  1108. {
  1109. cmSc_t* p = _cmScHandleToPtr(h);
  1110. unsigned i;
  1111. for(i=0; i<p->cnt; ++i)
  1112. {
  1113. p->array[i].perfSmpIdx = cmInvalidIdx;
  1114. p->array[i].perfVel = 0;
  1115. p->array[i].perfDynLvl = 0;
  1116. }
  1117. for(i=0; i<p->locCnt; ++i)
  1118. {
  1119. cmScoreSet_t* sp = p->loc[i].setList;
  1120. for(; sp!=NULL; sp=sp->llink)
  1121. sp->doneFl = false;
  1122. }
  1123. for(i=0; i<p->sectCnt; ++i)
  1124. {
  1125. unsigned j;
  1126. for(j=0; j<kScVarCnt; ++j)
  1127. p->sect[i].vars[j] = DBL_MAX;
  1128. }
  1129. p->nxtLocIdx = 0;
  1130. p->minSetLocIdx = cmInvalidIdx;
  1131. p->maxSetLocIdx = cmInvalidIdx;
  1132. }
  1133. bool _cmScIsSetPerfDone( cmScoreSet_t* sp )
  1134. {
  1135. unsigned i = 0;
  1136. for(i=0; i<sp->eleCnt; ++i)
  1137. if( sp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1138. return false;
  1139. return true;
  1140. }
  1141. void _cmScPerfSortTimes( unsigned *v, unsigned n )
  1142. {
  1143. unsigned i;
  1144. bool fl = true;
  1145. while(fl && n)
  1146. {
  1147. fl = false;
  1148. for(i=1; i<n; ++i)
  1149. {
  1150. if( v[i-1] > v[i] )
  1151. {
  1152. unsigned t = v[i-1];
  1153. v[i-1] = v[i];
  1154. v[i] = t;
  1155. fl = true;
  1156. }
  1157. }
  1158. --n;
  1159. }
  1160. }
  1161. bool _cmScPerfEven(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1162. {
  1163. unsigned i = 0;
  1164. double u = 0;
  1165. double x = 0;
  1166. bool sortFl = false;
  1167. bool printFl = true;
  1168. unsigned v[ stp->eleCnt ];
  1169. unsigned d[ stp->eleCnt - 1];
  1170. assert( stp->eleCnt > 1 );
  1171. // calculate the sum of the time between events
  1172. for(i=0; i<stp->eleCnt; ++i)
  1173. {
  1174. // if this event was not received - then the set is not valid
  1175. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1176. {
  1177. if( printFl && printMissFl)
  1178. printf("EVENESS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1179. return false;
  1180. }
  1181. // load v[i]
  1182. v[i] = stp->eleArray[i]->perfSmpIdx;
  1183. // check for out of order elements
  1184. if( i> 0 )
  1185. if( v[i] < v[i-1] )
  1186. sortFl = true;
  1187. }
  1188. // sort the times in ascending order
  1189. if( sortFl )
  1190. _cmScPerfSortTimes(v,stp->eleCnt);
  1191. // calc the sum of time differences
  1192. for(i=1; i<stp->eleCnt; ++i)
  1193. u += d[i-1] = v[i] - v[i-1];
  1194. // calculate the average time between events
  1195. u /= stp->eleCnt-1;
  1196. // calculate the std-dev of the time between events
  1197. for(i=0; i<stp->eleCnt-1; ++i)
  1198. x += (d[i]-u)*(d[i]-u);
  1199. double sd = sqrt(x/(stp->eleCnt-1));
  1200. // compute the average z-score
  1201. double c = 0;
  1202. for(i=0; i<stp->eleCnt-1; ++i)
  1203. c += fabs(d[i]-u)/sd;
  1204. stp->value = c/(stp->eleCnt-1);
  1205. stp->doneFl = true;
  1206. if(printFl)
  1207. {
  1208. /*
  1209. for(i=0; i<stp->eleCnt; ++i)
  1210. {
  1211. printf("%i %i ",i,v[i]);
  1212. if( i > 0 )
  1213. printf("%i ", d[i-1]);
  1214. printf("\n");
  1215. }
  1216. */
  1217. printf("%s EVENESS:%f\n",sortFl?"SORTED ":"",stp->value);
  1218. }
  1219. return true;
  1220. }
  1221. bool _cmScPerfDyn( cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1222. {
  1223. double a = 0;
  1224. unsigned i = 0;
  1225. bool printFl = true;
  1226. for(i=0; i<stp->eleCnt; ++i)
  1227. {
  1228. unsigned j;
  1229. // if this event was not received - then the set is not valid
  1230. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1231. {
  1232. if( printFl && printMissFl )
  1233. printf("DYNAMICS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1234. return false;
  1235. }
  1236. unsigned m = 0; // lower bound for the first dyn. category
  1237. // determine the dynamic category for the performed velocity of each event
  1238. for(j=0; j<p->dynRefCnt; ++j)
  1239. {
  1240. // if the vel fall's into the jth dyn. category
  1241. if( m <= stp->eleArray[i]->perfVel && stp->eleArray[i]->perfVel < p->dynRefArray[j] )
  1242. break;
  1243. // store the min vel for the next dyn category
  1244. m = p->dynRefArray[j];
  1245. }
  1246. assert( j < p->dynRefCnt );
  1247. stp->eleArray[i]->perfDynLvl = j+1;
  1248. a += abs((j+1) - stp->eleArray[i]->dynVal);
  1249. if( p->cbFunc != NULL )
  1250. {
  1251. cmScMsg_t m;
  1252. m.typeId = kDynMsgScId;
  1253. m.u.dyn.evtIdx = stp->eleArray[i]->index;
  1254. m.u.dyn.dynLvl = stp->eleArray[i]->perfDynLvl;
  1255. p->cbFunc(p->cbArg,&m,sizeof(m));
  1256. }
  1257. }
  1258. stp->value = a / stp->eleCnt;
  1259. stp->doneFl = true;
  1260. if( printFl )
  1261. printf("DYNAMICS:%f\n",stp->value);
  1262. return true;
  1263. }
  1264. bool _cmScPerfTempo1(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1265. {
  1266. bool printFl = true;
  1267. unsigned durSmpCnt = 0;
  1268. double durBeats = 0;
  1269. int i;
  1270. for(i=0; i<stp->eleCnt; ++i)
  1271. {
  1272. // if this event was not received - then the set is not valid
  1273. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1274. {
  1275. if( printFl && printMissFl )
  1276. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1277. return false;
  1278. }
  1279. if( i > 0 )
  1280. durSmpCnt += stp->eleArray[i]->perfSmpIdx - stp->eleArray[i-1]->perfSmpIdx;
  1281. durBeats += stp->eleArray[i]->frac;
  1282. }
  1283. stp->value = durBeats / (durSmpCnt / p->srate*60.0 );
  1284. stp->doneFl = true;
  1285. if( printFl )
  1286. printf("TEMPO:%f\n",stp->value);
  1287. return true;
  1288. }
  1289. bool _cmScPerfTempo(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1290. {
  1291. bool printFl = true;
  1292. unsigned durSmpCnt = 0;
  1293. double durBeats = 0;
  1294. int i;
  1295. unsigned bi = cmInvalidIdx;
  1296. unsigned ei = cmInvalidIdx;
  1297. unsigned missCnt = 0;
  1298. for(i=0; i<stp->eleCnt; ++i)
  1299. {
  1300. // if this event was not received - then the set is not valid
  1301. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1302. {
  1303. ++missCnt;
  1304. if( printFl && printMissFl )
  1305. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1306. }
  1307. else
  1308. {
  1309. if( bi == cmInvalidIdx )
  1310. bi = i;
  1311. ei = i;
  1312. }
  1313. }
  1314. if( ei > bi )
  1315. {
  1316. for(i=bi; i<=ei; ++i)
  1317. durBeats += stp->eleArray[i]->frac;
  1318. durSmpCnt = stp->eleArray[ei]->perfSmpIdx - stp->eleArray[bi]->perfSmpIdx;
  1319. stp->value = durBeats / (durSmpCnt / (p->srate*60.0) );
  1320. stp->doneFl = true;
  1321. }
  1322. if( printFl )
  1323. printf("TEMPO:%f bi:%i ei:%i secs:%f bts:%f\n",stp->value,bi,ei,durSmpCnt/p->srate,durBeats);
  1324. return true;
  1325. }
  1326. void _cmScPerfExec( cmSc_t* p, cmScoreSet_t* sp, bool printMissFl )
  1327. {
  1328. if( sp->doneFl == false )
  1329. {
  1330. switch( sp->varId )
  1331. {
  1332. case kEvenVarScId:
  1333. _cmScPerfEven(p,sp,printMissFl);
  1334. break;
  1335. case kDynVarScId:
  1336. _cmScPerfDyn(p,sp,printMissFl);
  1337. break;
  1338. case kTempoVarScId:
  1339. _cmScPerfTempo(p,sp,printMissFl);
  1340. break;
  1341. default:
  1342. { assert(0); }
  1343. }
  1344. }
  1345. }
  1346. void _cmScPerfExecRange( cmSc_t* p )
  1347. {
  1348. if( p->minSetLocIdx == cmInvalidIdx || p->maxSetLocIdx==cmInvalidIdx )
  1349. return;
  1350. unsigned i = p->minSetLocIdx;
  1351. for(; i<=p->maxSetLocIdx; ++i)
  1352. {
  1353. cmScoreSet_t* sp = p->loc[i].setList;
  1354. for(; sp!=NULL; sp=sp->llink)
  1355. _cmScPerfExec(p,sp,true);
  1356. }
  1357. }
  1358. bool _cmScSetPerfEvent( cmSc_t* p, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1359. {
  1360. assert(locIdx < p->locCnt );
  1361. cmScoreLoc_t* lp = p->loc + locIdx;
  1362. bool foundFl = false;
  1363. bool doneFl = true;
  1364. unsigned i;
  1365. // locate the event at the loc[locIdx]
  1366. for(i=0; i<lp->evtCnt; ++i)
  1367. {
  1368. cmScoreEvt_t* ep = lp->evtArray[i];
  1369. if( ep->type == kNonEvtScId )
  1370. {
  1371. if( ep->pitch == pitch )
  1372. {
  1373. assert( ep->perfSmpIdx == cmInvalidIdx );
  1374. ep->perfSmpIdx = smpIdx;
  1375. ep->perfVel = vel;
  1376. foundFl = true;
  1377. }
  1378. // check if all notes have arrived for this location
  1379. if( ep->perfSmpIdx == cmInvalidIdx )
  1380. doneFl = false;
  1381. }
  1382. }
  1383. // the event must always be found
  1384. assert( foundFl );
  1385. return doneFl;
  1386. }
  1387. bool cmScoreSetPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1388. {
  1389. cmSc_t* p = _cmScHandleToPtr(h);
  1390. return _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1391. }
  1392. void cmScoreExecPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1393. {
  1394. unsigned i;
  1395. cmSc_t* p = _cmScHandleToPtr(h);
  1396. bool doneFl = _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1397. unsigned printLvl = 1;
  1398. cmScoreLoc_t* lp = p->loc + locIdx;
  1399. // all events for a location must be complete to trigger attached events
  1400. if( doneFl == false )
  1401. return;
  1402. if( p->loc[locIdx].setList != NULL )
  1403. {
  1404. // set idx of most recent loc w/ a set end event
  1405. p->maxSetLocIdx = locIdx;
  1406. if( p->minSetLocIdx == cmInvalidIdx )
  1407. p->minSetLocIdx = locIdx;
  1408. }
  1409. // attempt to calculate all sets between loc[p->minSetLocIdx] and loc[p->maxSetLocIdx]
  1410. _cmScPerfExecRange(p);
  1411. // prevent event retriggering or going backwards
  1412. if( printLvl && locIdx < p->nxtLocIdx )
  1413. {
  1414. printf("----- BACK ----- \n");
  1415. return;
  1416. }
  1417. if( printLvl && locIdx > p->nxtLocIdx )
  1418. {
  1419. printf("----- SKIP ----- \n");
  1420. }
  1421. // for each location between the current and previous location
  1422. for(; p->nxtLocIdx<=locIdx; ++p->nxtLocIdx)
  1423. {
  1424. lp = p->loc + p->nxtLocIdx;
  1425. // if this location is the start of a new section - then apply
  1426. // sets that are assigned to this section
  1427. if( lp->begSectPtr != NULL && lp->begSectPtr->setCnt > 0 )
  1428. {
  1429. // notice the location of the oldest section start - once we cross this point
  1430. // it is too late to notice set completions - so incr p->inSetLocIdx
  1431. if( lp->begSectPtr->setCnt )
  1432. p->minSetLocIdx = p->nxtLocIdx+1;
  1433. for(i=0; i<lp->begSectPtr->setCnt; ++i)
  1434. {
  1435. cmScoreSet_t* stp = lp->begSectPtr->setArray[i];
  1436. // temporarily commented out for testing purposes
  1437. // if( stp->doneFl == false )
  1438. // _cmScPerfExec(p, stp, printLvl>0 );
  1439. if( stp->doneFl )
  1440. {
  1441. assert( stp->varId < kScVarCnt );
  1442. lp->begSectPtr->vars[ stp->varId ] = stp->value;
  1443. if( p->cbFunc != NULL )
  1444. {
  1445. cmScMsg_t m;
  1446. m.typeId = kVarMsgScId;
  1447. m.u.meas.varId = stp->varId;
  1448. m.u.meas.value = stp->value;
  1449. p->cbFunc(p->cbArg,&m,sizeof(m));
  1450. }
  1451. }
  1452. }
  1453. }
  1454. }
  1455. }
  1456. void cmScoreSetPerfValue( cmScH_t h, unsigned locIdx, unsigned varId, double value )
  1457. {
  1458. cmSc_t* p = _cmScHandleToPtr(h);
  1459. int li = locIdx;
  1460. for(; li>=0; --li)
  1461. if( p->loc[li].begSectPtr != NULL )
  1462. {
  1463. assert( varId < kScVarCnt );
  1464. p->loc[li].begSectPtr->vars[varId] = value;
  1465. break;
  1466. }
  1467. assert( li>=0);
  1468. }
  1469. void cmScoreSetPerfDynLevel( cmScH_t h, unsigned evtIdx, unsigned dynLvl )
  1470. {
  1471. cmSc_t* p = _cmScHandleToPtr(h);
  1472. assert(evtIdx < p->cnt );
  1473. p->array[ evtIdx ].perfDynLvl = dynLvl;
  1474. }
  1475. cmScRC_t cmScoreDecode( const void* msg, unsigned msgByteCnt, cmScMsg_t* m)
  1476. {
  1477. cmScMsg_t* mp = (cmScMsg_t*)msg;
  1478. *m = *mp;
  1479. return kOkScRC;
  1480. }
  1481. void cmScorePrint( cmScH_t h, cmRpt_t* rpt )
  1482. {
  1483. cmSc_t* p = _cmScHandleToPtr(h);
  1484. unsigned i;
  1485. for(i=0; i<20 /*p->cnt*/; ++i)
  1486. {
  1487. cmScoreEvt_t* r = p->array + i;
  1488. switch(r->type)
  1489. {
  1490. case kNonEvtScId:
  1491. cmRptPrintf(rpt,"%5i %3i %3i %s 0x%2x %c%c%c %s\n",
  1492. i,
  1493. r->barNumb,
  1494. r->barNoteIdx,
  1495. cmScEvtTypeIdToLabel(r->type),
  1496. r->pitch,
  1497. cmIsFlag(r->flags,kEvenScFl) ? 'e' : ' ',
  1498. cmIsFlag(r->flags,kTempoScFl) ? 't' : ' ',
  1499. cmIsFlag(r->flags,kDynScFl) ? 'd' : ' ',
  1500. cmIsFlag(r->flags,kDynScFl) ? cmScDynIdToLabel(r->dynVal) : "");
  1501. break;
  1502. default:
  1503. break;
  1504. }
  1505. }
  1506. }
  1507. void cmScoreTest( cmCtx_t* ctx, const cmChar_t* fn )
  1508. {
  1509. cmScH_t h = cmScNullHandle;
  1510. if( cmScoreInitialize(ctx,&h,fn,0,NULL,0,NULL,NULL) != kOkScRC )
  1511. return;
  1512. cmScorePrint(h,&ctx->rpt);
  1513. cmScoreFinalize(&h);
  1514. }
  1515. // 1. Fix absolute message time which was incorrect on original score file.
  1516. // 2.
  1517. void cmScoreFix( cmCtx_t* ctx )
  1518. {
  1519. const cmChar_t* mfn = "/home/kevin/src/cmgv/src/gv/data/ImaginaryThemes.mid";
  1520. const cmChar_t* crfn = "/home/kevin/src/cmgv/src/gv/data/mod0a.txt";
  1521. const cmChar_t* cwfn = "/home/kevin/src/cmgv/src/gv/data/mod1.csv";
  1522. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  1523. cmCsvH_t csvH = cmCsvNullHandle;
  1524. const cmMidiTrackMsg_t** msg = NULL;
  1525. double secs = 0.0;
  1526. int ci,mi,crn,mn;
  1527. bool errFl = true;
  1528. unsigned handCnt = 0;
  1529. unsigned midiMissCnt = 0;
  1530. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  1531. goto errLabel;
  1532. if( cmCsvLexRegisterMatcher(csvH, cmCsvLexNextAvailId(csvH), _cmScLexSciPitchMatcher ) != kOkCsvRC )
  1533. goto errLabel;
  1534. if( cmCsvParseFile(csvH, crfn, 0 ) != kOkCsvRC )
  1535. goto errLabel;
  1536. if( cmMidiFileOpen(mfn,&mfH,ctx) != kOkMfRC )
  1537. goto errLabel;
  1538. cmMidiFileTickToMicros(mfH);
  1539. cmMidiFileCalcNoteDurations(mfH);
  1540. mn = cmMidiFileMsgCount(mfH);
  1541. msg = cmMidiFileMsgArray(mfH);
  1542. crn = cmCsvRowCount(csvH);
  1543. // for each row in the score file
  1544. for(ci=1,mi=0; ci<crn && cmCsvLastRC(csvH)==kOkCsvRC; ++ci)
  1545. {
  1546. unsigned id;
  1547. // zero the duration column
  1548. if( cmCsvCellPtr(csvH, ci, kDSecsColScIdx ) != NULL )
  1549. cmCsvSetCellUInt( csvH, ci, kDSecsColScIdx, 0 );
  1550. // get the MIDI file event id for this row
  1551. if((id = cmCsvCellUInt(csvH,ci,kMidiFileIdColScIdx)) == UINT_MAX)
  1552. {
  1553. // this is a hand-entered event - so it has no event id
  1554. ++handCnt;
  1555. }
  1556. else
  1557. {
  1558. for(; mi<mn; ++mi)
  1559. {
  1560. const cmMidiTrackMsg_t* m = msg[mi];
  1561. assert( mi+1 <= id );
  1562. secs += m->dtick/1000000.0;
  1563. if( mi+1 != id )
  1564. {
  1565. if( m->status == kNoteOnMdId && m->u.chMsgPtr->d1>0 )
  1566. {
  1567. // this MIDI note-on does not have a corresponding score event
  1568. ++midiMissCnt;
  1569. }
  1570. }
  1571. else
  1572. {
  1573. cmCsvSetCellDouble( csvH, ci, kSecsColScIdx, secs );
  1574. ++mi;
  1575. if( m->status == kNoteOnMdId )
  1576. cmCsvSetCellDouble( csvH, ci, kDSecsColScIdx, m->u.chMsgPtr->durTicks/1000000.0 );
  1577. break;
  1578. }
  1579. }
  1580. if( mi==mn)
  1581. printf("done on row:%i\n",ci);
  1582. }
  1583. }
  1584. if( cmCsvLastRC(csvH) != kOkCsvRC )
  1585. goto errLabel;
  1586. if( cmCsvWrite(csvH,cwfn) != kOkCsvRC )
  1587. goto errLabel;
  1588. errFl = false;
  1589. errLabel:
  1590. if( errFl )
  1591. printf("Score fix failed.\n");
  1592. else
  1593. printf("Score fix done! hand:%i miss:%i\n",handCnt,midiMissCnt);
  1594. cmMidiFileClose(&mfH);
  1595. cmCsvFinalize(&csvH);
  1596. }