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

cmScore.c 47KB

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