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 65KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580
  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 "cmLinkedHeap.h"
  10. #include "cmMidi.h"
  11. #include "cmLex.h"
  12. #include "cmCsv.h"
  13. #include "cmSymTbl.h"
  14. #include "cmMidiFile.h"
  15. #include "cmAudioFile.h"
  16. #include "cmTimeLine.h"
  17. #include "cmText.h"
  18. #include "cmScore.h"
  19. #include "cmVectOpsTemplateMain.h"
  20. cmScH_t cmScNullHandle = cmSTATIC_NULL_HANDLE;
  21. enum
  22. {
  23. kLabelCharCnt = 7,
  24. kInvalidDynScId = 0,
  25. };
  26. enum
  27. {
  28. kMidiFileIdColScIdx= 0,
  29. kTypeLabelColScIdx = 3,
  30. kDSecsColScIdx = 4,
  31. kSecsColScIdx = 5,
  32. kPitchColScIdx = 11,
  33. kBarColScIdx = 13,
  34. kSkipColScIdx = 14,
  35. kEvenColScIdx = 15,
  36. kGraceColScIdx = 16,
  37. kTempoColScIdx = 17,
  38. kFracColScIdx = 18,
  39. kDynColScIdx = 19,
  40. kSectionColScIdx = 20,
  41. kRecdPlayColScIdx = 21,
  42. kRemarkColScIdx = 22
  43. };
  44. typedef struct
  45. {
  46. unsigned id;
  47. unsigned flag;
  48. cmChar_t label[ kLabelCharCnt + 1 ];
  49. } cmScEvtRef_t;
  50. typedef struct cmScSect_str
  51. {
  52. const cmChar_t* label;
  53. unsigned startIdx;
  54. struct cmScSect_str* link;
  55. } cmScSect_t;
  56. typedef struct cmScSetEle_str
  57. {
  58. cmChar_t* label;
  59. unsigned eleIdx;
  60. struct cmScSetEle_str* link;
  61. } cmScSetEle_t;
  62. typedef struct cmScSet_str
  63. {
  64. unsigned typeFl; // type of this set
  65. cmScSetEle_t* eles; // indexes of set elements
  66. cmScSetEle_t* sects; // application section labels
  67. bool inFl; // true if currently accepting elements
  68. struct cmScSet_str* link; //
  69. } cmScSet_t;
  70. typedef struct cmScMark_str
  71. {
  72. cmMarkScMId_t cmdId;
  73. unsigned labelSymId;
  74. unsigned scoreIdx;
  75. unsigned csvRowIdx;
  76. struct cmScMark_str* link;
  77. } cmScMark_t;
  78. typedef struct
  79. {
  80. cmErr_t err;
  81. cmCsvH_t cH;
  82. cmSymTblH_t stH;
  83. cmScCb_t cbFunc;
  84. void* cbArg;
  85. cmChar_t* fn;
  86. double srate;
  87. cmScoreEvt_t* array;
  88. unsigned cnt;
  89. cmScoreLoc_t* loc;
  90. unsigned locCnt;
  91. cmScoreSection_t* sect;
  92. unsigned sectCnt;
  93. unsigned* markLabelArray; // one symId per unique cmScoreMarker_t.labelSymId;
  94. unsigned markLabelCnt;
  95. unsigned sciPitchLexTId; // sci pitch and section id lexer token id's
  96. unsigned sectionLexTId;
  97. cmScSect_t* sectList; // lists used during parsing
  98. cmScSet_t* setList;
  99. cmScMark_t* markList;
  100. cmScoreSet_t* sets;
  101. unsigned setCnt;
  102. unsigned* dynRefArray;
  103. unsigned dynRefCnt;
  104. unsigned nxtLocIdx;
  105. unsigned minSetLocIdx;
  106. unsigned maxSetLocIdx;
  107. } cmSc_t;
  108. cmScEvtRef_t _cmScEvtRefArray[] =
  109. {
  110. { kTimeSigEvtScId, 0, "tsg" },
  111. { kKeySigEvtScId, 0, "ksg" },
  112. { kTempoEvtScId, 0, "tmp" },
  113. { kTrackEvtScId, 0, "trk" },
  114. { kTextEvtScId, 0, "txt" },
  115. { kEOTrackEvtScId, 0, "eot" },
  116. { kCopyEvtScId, 0, "cpy" },
  117. { kBlankEvtScId, 0, "blk" },
  118. { kBarEvtScId, 0, "bar" },
  119. { kPgmEvtScId, 0, "pgm" },
  120. { kCtlEvtScId, 0, "ctl" },
  121. { kNonEvtScId, 0, "non" },
  122. { kInvalidEvtScId, 0, "***" }
  123. };
  124. cmScEvtRef_t _cmScDynRefArray[] =
  125. {
  126. { 1, 0, "pppp" },
  127. { 2, 0, "ppp" },
  128. { 3, 0, "pp" },
  129. { 4, 0, "p" },
  130. { 5, 0, "mp" },
  131. { 6, 0, "mf" },
  132. { 7, 0, "f" },
  133. { 8, 0, "ff" },
  134. { 9,0, "fff" },
  135. { kInvalidDynScId,0, "***" },
  136. };
  137. cmScEvtRef_t _cmScVarRefArray[] =
  138. {
  139. { kEvenVarScId, kEvenScFl, "e"},
  140. { kDynVarScId, kDynScFl, "d"},
  141. { kTempoVarScId,kTempoScFl,"t"},
  142. { cmInvalidId, 0, "@"}
  143. };
  144. cmSc_t* _cmScHandleToPtr( cmScH_t h )
  145. {
  146. cmSc_t* p = (cmSc_t*)h.h;
  147. assert( p != NULL );
  148. return p;
  149. }
  150. unsigned _cmScEvtTypeLabelToId( const cmChar_t* label )
  151. {
  152. cmScEvtRef_t* r = _cmScEvtRefArray;
  153. for(; r->id != kInvalidEvtScId; ++r )
  154. if( strcmp(label,r->label) == 0 )
  155. return r->id;
  156. return kInvalidEvtScId;
  157. }
  158. const cmChar_t* cmScEvtTypeIdToLabel( unsigned id )
  159. {
  160. cmScEvtRef_t* r = _cmScEvtRefArray;
  161. for(; r->id != kInvalidEvtScId; ++r )
  162. if( r->id == id )
  163. return r->label;
  164. return NULL;
  165. }
  166. unsigned _cmScDynLabelToId( const cmChar_t* label )
  167. {
  168. cmScEvtRef_t* r = _cmScDynRefArray;
  169. for(; r->id != kInvalidEvtScId; ++r )
  170. if( strncmp(label,r->label,strlen(r->label)) == 0 )
  171. return r->id;
  172. return kInvalidDynScId;
  173. }
  174. // return the count of dynamic label/id pairs
  175. unsigned _cmScDynLabelCount( )
  176. {
  177. unsigned n = 0;
  178. cmScEvtRef_t* r = _cmScDynRefArray;
  179. for(; r->id != kInvalidEvtScId; ++r )
  180. ++n;
  181. return n;
  182. }
  183. const cmChar_t* cmScDynIdToLabel( unsigned id )
  184. {
  185. cmScEvtRef_t* r = _cmScDynRefArray;
  186. for(; r->id != kInvalidDynScId; ++r )
  187. if( r->id == id )
  188. return r->label;
  189. return NULL;
  190. }
  191. char _cmScVarFlagToChar( unsigned flags )
  192. {
  193. unsigned i;
  194. for(i=0; _cmScVarRefArray[i].id!=cmInvalidId; ++i)
  195. if( _cmScVarRefArray[i].flag == flags )
  196. return _cmScVarRefArray[i].label[0];
  197. assert(0);
  198. return ' ';
  199. }
  200. char _cmScVarIdToChar( unsigned id )
  201. {
  202. unsigned i;
  203. for(i=0; _cmScVarRefArray[i].id!=cmInvalidId; ++i)
  204. if( _cmScVarRefArray[i].id == id )
  205. return _cmScVarRefArray[i].label[0];
  206. assert(0);
  207. return ' ';
  208. }
  209. unsigned _cmScVarFlagToId( unsigned flags )
  210. {
  211. unsigned i;
  212. for(i=0; _cmScVarRefArray[i].id!=cmInvalidId; ++i)
  213. if( _cmScVarRefArray[i].flag == flags )
  214. return _cmScVarRefArray[i].id;
  215. assert(0);
  216. return cmInvalidId;
  217. }
  218. const char* _cmScFlagsToStr( unsigned flags, char* buf, int bufCharCnt )
  219. {
  220. unsigned i=0;
  221. if( cmIsFlag(flags,kEvenScFl) )
  222. {
  223. assert(i<bufCharCnt);
  224. buf[i] = 'e';
  225. ++i;
  226. }
  227. if( cmIsFlag(flags,kDynScFl) )
  228. {
  229. assert(i<bufCharCnt);
  230. buf[i] = 'd';
  231. ++i;
  232. }
  233. if( cmIsFlag(flags,kTempoScFl ))
  234. {
  235. assert(i<bufCharCnt);
  236. buf[i] = 't';
  237. ++i;
  238. }
  239. assert(i<bufCharCnt);
  240. buf[i] = 0;
  241. return buf;
  242. }
  243. unsigned _cmScLexSciPitchMatcher( const cmChar_t* cp, unsigned cn )
  244. {
  245. if( cp==NULL || cn < 2 )
  246. return 0;
  247. // first char must be "A-G"
  248. if( strspn(cp,"ABCDEFG") != 1 )
  249. return 0;
  250. unsigned i = 1;
  251. // next char could be accidental
  252. if( cp[i] == '#' || cp[i] == 'b' )
  253. ++i; // i==2
  254. // the 2nd or 3rd char must be a digit
  255. if( i>=cn || isdigit(cp[i]) == false )
  256. return 0;
  257. ++i; // i==2 or i==3
  258. // the 3rd or 4th char must be a digit or EOS
  259. if( i>=cn || isdigit(cp[i]) == false )
  260. return i;
  261. ++i;
  262. return i;
  263. }
  264. unsigned _cmScLexSectionIdMatcher( const cmChar_t* cp, unsigned cn )
  265. {
  266. if( cp==NULL || cn < 2 )
  267. return 0;
  268. // first char must be a number
  269. if( !isdigit(cp[0]) )
  270. return 0;
  271. // if 2nd char is a char then terminate
  272. if( 'a'<=cp[1] && cp[1]<='z' )
  273. return 2;
  274. // if 2nd char is digit and 3rd char is char then terminate
  275. if( isdigit(cp[1]) && cn>2 && 'a'<=cp[2] && cp[2]<='z' )
  276. return 3;
  277. return 0;
  278. }
  279. void _cmScFreeMarkList( cmScMark_t* markList )
  280. {
  281. cmScMark_t* mp = markList;
  282. while( mp!=NULL )
  283. {
  284. cmScMark_t* np = mp->link;
  285. cmMemFree(mp);
  286. mp = np;
  287. }
  288. }
  289. void _cmScFreeSetList( cmScSet_t* setList )
  290. {
  291. cmScSet_t* tp = setList;
  292. cmScSet_t* ntp = NULL;
  293. while(tp!=NULL)
  294. {
  295. ntp = tp->link;
  296. cmScSetEle_t* ep = tp->eles;
  297. while( ep != NULL )
  298. {
  299. cmScSetEle_t* nep = ep->link;
  300. cmMemFree(ep);
  301. ep = nep;
  302. }
  303. ep = tp->sects;
  304. while( ep != NULL )
  305. {
  306. cmScSetEle_t* nep = ep->link;
  307. cmMemFree(ep->label);
  308. cmMemFree(ep);
  309. ep = nep;
  310. }
  311. cmMemFree(tp);
  312. tp = ntp;
  313. }
  314. }
  315. cmScRC_t _cmScFinalize( cmSc_t* p )
  316. {
  317. cmScRC_t rc = kOkScRC;
  318. unsigned i;
  319. if( cmCsvFinalize(&p->cH) != kOkCsvRC )
  320. return rc;
  321. if( p->sets != NULL )
  322. {
  323. for(i=0; i<p->setCnt; ++i)
  324. {
  325. cmMemFree(p->sets[i].eleArray);
  326. cmMemFree(p->sets[i].sectArray);
  327. cmMemFree(p->sets[i].symArray);
  328. cmMemFree(p->sets[i].costSymArray);
  329. }
  330. cmMemFree(p->sets);
  331. }
  332. _cmScFreeSetList(p->setList);
  333. _cmScFreeMarkList(p->markList);
  334. if( p->loc != NULL )
  335. {
  336. for(i=0; i<p->locCnt; ++i)
  337. {
  338. cmMemFree(p->loc[i].evtArray);
  339. if( p->loc[i].begSectPtr != NULL )
  340. cmMemFree(p->loc[i].begSectPtr->setArray);
  341. // free the marker list assoc'd with this location
  342. cmScoreMarker_t* smp = p->loc[i].markList;
  343. while( smp!=NULL )
  344. {
  345. cmScoreMarker_t* np = smp->link;
  346. cmMemFree(smp);
  347. smp = np;
  348. }
  349. }
  350. cmMemFree(p->loc);
  351. }
  352. cmMemPtrFree(&p->dynRefArray);
  353. cmMemFree(p->markLabelArray);
  354. cmMemFree(p->sect);
  355. cmMemFree(p->fn);
  356. cmMemFree(p->array);
  357. cmMemFree(p);
  358. return rc;
  359. }
  360. cmScRC_t _cmScParseBar( cmSc_t* p, unsigned rowIdx, cmScoreEvt_t* s, int* barNumb )
  361. {
  362. if((*barNumb = cmCsvCellInt(p->cH,rowIdx,kBarColScIdx)) == INT_MAX )
  363. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unable to parse the bar number.");
  364. s->type = kBarEvtScId;
  365. s->secs = 0;
  366. s->barNumb = *barNumb;
  367. s->csvRowNumb = rowIdx + 1;
  368. return kOkScRC;
  369. }
  370. cmScSet_t* _cmScNewSet( cmSc_t* p, unsigned typeFl )
  371. {
  372. // create a new set record
  373. cmScSet_t* nsp = cmMemAllocZ(cmScSet_t,1);
  374. nsp->inFl = true;
  375. nsp->typeFl = typeFl;
  376. if( p->setList == NULL )
  377. p->setList = nsp;
  378. else
  379. {
  380. // go to the end of the the set list
  381. cmScSet_t* sp = p->setList;
  382. assert(sp!=NULL);
  383. while( sp->link != NULL )
  384. sp = sp->link;
  385. sp->link = nsp;
  386. }
  387. return nsp;
  388. }
  389. cmScSet_t* _cmScFindSet( cmSc_t* p, unsigned typeFl )
  390. {
  391. // locate the set currently accepting ele's for this type
  392. cmScSet_t* sp = p->setList;
  393. for(; sp != NULL; sp=sp->link )
  394. if( sp->typeFl == typeFl && sp->inFl )
  395. break;
  396. return sp;
  397. }
  398. void _cmScSetDone(cmSc_t* p, unsigned typeFl)
  399. {
  400. cmScSet_t* sp = _cmScFindSet(p,typeFl);
  401. assert( sp != NULL );
  402. sp->inFl = false;
  403. }
  404. // This function is called during parsing to
  405. // insert a set element or set section into a cmScSet_t
  406. // element or section linked list. Either the scoreIdx
  407. // or the label is valid but not both.
  408. cmScSet_t* _cmScInsertSetEle(cmSc_t* p, unsigned scoreIdx, unsigned typeFl, const cmChar_t* label, unsigned labelCharCnt)
  409. {
  410. assert( scoreIdx!=cmInvalidId || (scoreIdx==cmInvalidIdx && label!=NULL && labelCharCnt>0));
  411. cmScSet_t* sp = _cmScFindSet(p,typeFl);
  412. if( sp == NULL )
  413. sp = _cmScNewSet(p,typeFl);
  414. // allocate a new set element record
  415. cmScSetEle_t* nep = cmMemAllocZ(cmScSetEle_t,1);
  416. cmScSetEle_t** list = NULL;
  417. nep->eleIdx = scoreIdx;
  418. if( label == NULL )
  419. {
  420. // all elements must be of the same type
  421. assert( sp->typeFl == typeFl );
  422. sp->typeFl = typeFl;
  423. list = &sp->eles;
  424. }
  425. else
  426. {
  427. nep->label = cmMemAllocStrN(label,labelCharCnt);
  428. list = &sp->sects;
  429. }
  430. // *list refers to sp->sects or sp->ele's depending on the type of ele
  431. if( *list == NULL )
  432. *list = nep;
  433. else
  434. {
  435. // got to the last element in the set
  436. cmScSetEle_t* ep = *list;
  437. while( ep->link != NULL )
  438. ep = ep->link;
  439. // append the new element to the end of the list
  440. ep->link = nep;
  441. }
  442. return sp;
  443. }
  444. // Extract the next attribute section identifier.
  445. const cmChar_t* _cmScParseOneSetSection( cmSc_t* p, unsigned typeFl, const cmChar_t* c0p )
  446. {
  447. // advance white space
  448. while( *c0p && (isspace(*c0p) || *c0p==',') )
  449. ++c0p;
  450. if( *c0p==0 )
  451. return c0p;
  452. // c0p now points to a section id or an asterisk
  453. const cmChar_t* c1p = c0p;
  454. // advance past section id
  455. while( *c1p && (!isspace(*c1p) && (isdigit(*c1p) || isalpha(*c1p))))
  456. ++c1p;
  457. // if c0p pointed to an asterisk then c1p is still equal to c0p
  458. if( c1p > c0p )
  459. _cmScInsertSetEle(p,cmInvalidIdx,typeFl,c0p,c1p-c0p);
  460. return c1p;
  461. }
  462. // Parse an attribute string to extract the section
  463. // identifiers which may follow the attribute token (e,t,mf,ff,...)
  464. cmScRC_t _cmScParseAttr(cmSc_t* p, unsigned scoreIdx, const cmChar_t* text, unsigned typeFl)
  465. {
  466. const cmChar_t* cp = text;
  467. // insert a set element - all attribute's produce one element record
  468. _cmScInsertSetEle(p,scoreIdx,typeFl,NULL,0);
  469. // advance past the attribute type marking (e,t,(p,mf,f,fff,etc)) in search
  470. // of section identifiers
  471. while( *cp && !isspace(*cp) )
  472. ++cp;
  473. if( *cp )
  474. {
  475. // search for the first section identifier
  476. if((cp =_cmScParseOneSetSection(p,typeFl,cp)) != NULL )
  477. {
  478. bool asteriskFl = false;
  479. // search for the second section identifier
  480. if((cp = _cmScParseOneSetSection(p,typeFl,cp)) != NULL && *cp!=0 )
  481. asteriskFl = *cp == '*';
  482. _cmScSetDone(p,typeFl);
  483. // if the attr just parsed ended with an asterisk then it is both
  484. // the last element of the previous set and the first ele of the
  485. // next set
  486. if( asteriskFl )
  487. {
  488. // if the attr just parsed had a section id then it was the last
  489. // element in the set - create a new set record to hold the next set
  490. _cmScNewSet(p,typeFl);
  491. _cmScInsertSetEle(p,scoreIdx,typeFl,NULL,0);
  492. }
  493. }
  494. }
  495. return kOkScRC;
  496. }
  497. // Parse a record/playback string
  498. cmScRC_t _cmScParseMarkers( cmSc_t* p, unsigned scoreIdx, const cmChar_t* text, unsigned rowIdx )
  499. {
  500. const cmChar_t* cp = text;
  501. const cmChar_t* ip;
  502. const cmChar_t* ep;
  503. // if no symbol table has been registered then don't bother storing markers.
  504. // (NOTE - THIS IS A HACK BECAUSE THE SCORE OBJECT USED IN THE cmdIf DOES NOT HAVE
  505. // A SYMBOL TABLE - WE COULD EASILY ADD ONE IF IT EVENTUALLY NEEDS ACCESS TO THE MARKERS
  506. // - OR A SYMBOL TABLE COULD BE ADDED TO THE SCORE ITSELF.)
  507. if( cmSymTblIsValid(p->stH) == false )
  508. return kOkScRC;
  509. // go to command/id space
  510. if((ip = cmTextNextWhiteOrEosC(text)) == NULL )
  511. goto errLabel;
  512. // goto label
  513. if((ip = cmTextNextNonWhiteC(ip)) == NULL )
  514. goto errLabel;
  515. // goto end of label
  516. if((ep = cmTextNextWhiteOrEosC(ip)) == NULL )
  517. goto errLabel;
  518. else
  519. {
  520. unsigned n = (ep-ip)+1;
  521. cmChar_t markTextStr[n+1];
  522. strncpy(markTextStr,ip,n);
  523. // for each command code
  524. // (there may be more than one character)
  525. for(; *cp && !isspace(*cp); ++cp)
  526. {
  527. cmMarkScMId_t cmdId = kInvalidScMId;
  528. switch( *cp )
  529. {
  530. case 'c': cmdId = kRecdBegScMId; break;
  531. case 'e': cmdId = kRecdEndScMId; break;
  532. case 'p': cmdId = kPlayBegScMId; break;
  533. case 'd': cmdId = kPlayEndScMId; break;
  534. case 'f': cmdId = kFadeScMId; break;
  535. default:
  536. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unrecognized marker command character '%c' at row index %i.",*cp,rowIdx);
  537. }
  538. cmScMark_t* mp = cmMemAllocZ(cmScMark_t,1);
  539. mp->cmdId = cmdId;
  540. mp->labelSymId = cmSymTblRegisterSymbol(p->stH,markTextStr);
  541. mp->scoreIdx = scoreIdx;
  542. mp->csvRowIdx = rowIdx;
  543. // insert the new mark at the end of the list
  544. if( p->markList == NULL )
  545. p->markList = mp;
  546. else
  547. {
  548. cmScMark_t* ep = p->markList;
  549. while( ep->link != NULL )
  550. ep = ep->link;
  551. ep->link = mp;
  552. }
  553. }
  554. }
  555. return kOkScRC;
  556. errLabel:
  557. return cmErrMsg(&p->err,kSyntaxErrScRC,"Invalid record/playback field ('%s') on row index:%i.",cmStringNullGuard(text),rowIdx);
  558. }
  559. void _cmScPrintSets( const cmChar_t* label, cmScSet_t* setList )
  560. {
  561. printf("%s\n",label);
  562. const cmScSet_t* sp = setList;
  563. for(; sp != NULL; sp=sp->link )
  564. {
  565. const cmScSetEle_t* ep = sp->eles;
  566. for(; ep!=NULL; ep=ep->link)
  567. printf("%i ",ep->eleIdx);
  568. printf(" : ");
  569. for(ep=sp->sects; ep!=NULL; ep=ep->link)
  570. printf("%s ",cmStringNullGuard(ep->label));
  571. printf("\n");
  572. }
  573. }
  574. cmScRC_t _cmScParseNoteOn( cmSc_t* p, unsigned rowIdx, cmScoreEvt_t* s, unsigned scoreIdx, int barNumb, unsigned barNoteIdx )
  575. {
  576. cmScRC_t rc = kOkScRC;
  577. unsigned flags = 0;
  578. unsigned dynVal = kInvalidDynScId;
  579. const cmChar_t* sciPitch;
  580. cmMidiByte_t midiPitch;
  581. const cmChar_t* attr;
  582. double secs;
  583. double durSecs;
  584. const cmCsvCell_t* cell;
  585. s += scoreIdx;
  586. // verify the scientific pitch cell was formatted correcly
  587. if((cell = cmCsvCellPtr(p->cH,rowIdx,kPitchColScIdx)) == NULL || cell->lexTId != p->sciPitchLexTId )
  588. return cmErrMsg(&p->err,kSyntaxErrScRC,"Pitch column format error.");
  589. if((sciPitch = cmCsvCellText(p->cH,rowIdx,kPitchColScIdx)) == NULL )
  590. return cmErrMsg(&p->err,kSyntaxErrScRC,"Expected a scientific pitch value");
  591. if((midiPitch = cmSciPitchToMidi(sciPitch)) == kInvalidMidiPitch)
  592. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unable to convert the scientific pitch '%s' to a MIDI value. ");
  593. // it is possible that note delta-secs field is empty - so default to 0
  594. if((secs = cmCsvCellDouble(p->cH, rowIdx, kSecsColScIdx )) == DBL_MAX) // Returns DBL_MAX on error.
  595. flags += kInvalidScFl;
  596. // skip attribute
  597. if((attr = cmCsvCellText(p->cH,rowIdx,kSkipColScIdx)) != NULL && *attr == 's' )
  598. flags += kSkipScFl;
  599. // evenness attribute
  600. if((attr = cmCsvCellText(p->cH,rowIdx,kEvenColScIdx)) != NULL && *attr == 'e' )
  601. {
  602. flags += kEvenScFl;
  603. _cmScParseAttr(p,scoreIdx,attr,kEvenScFl);
  604. }
  605. // grace attribute
  606. if((attr = cmCsvCellText(p->cH,rowIdx,kGraceColScIdx)) != NULL && *attr == 'g' )
  607. {
  608. flags += kGraceScFl;
  609. if( cmIsNotFlag(flags,kEvenScFl) )
  610. return cmErrMsg(&p->err,kSyntaxErrScRC,"All 'grace' notes should also be 'even' notes.");
  611. }
  612. // tempo attribute
  613. if((attr = cmCsvCellText(p->cH,rowIdx,kTempoColScIdx)) != NULL && *attr == 't' )
  614. {
  615. flags += kTempoScFl;
  616. _cmScParseAttr(p,scoreIdx,attr,kTempoScFl);
  617. }
  618. // dynamics attribute
  619. if((attr = cmCsvCellText(p->cH,rowIdx,kDynColScIdx)) != NULL )
  620. {
  621. if((dynVal = _cmScDynLabelToId(attr)) == kInvalidDynScId )
  622. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unknown dynamic label '%s'.",cmStringNullGuard(attr));
  623. flags += kDynScFl;
  624. _cmScParseAttr(p,scoreIdx,attr,kDynScFl);
  625. }
  626. // tempo/non-grace rythmic value
  627. if( cmIsFlag(flags,kTempoScFl) || (cmIsFlag(flags,kEvenScFl) && cmIsNotFlag(flags,kGraceScFl)) )
  628. {
  629. double frac = cmCsvCellDouble(p->cH,rowIdx,kFracColScIdx);
  630. // no 'frac' value is given for the last note of the set we must accept error
  631. // values here and validate the actual values later
  632. if( frac>0 && frac!=DBL_MAX )
  633. s->frac = frac;
  634. }
  635. // Returns DBL_MAX on error.
  636. if((durSecs = cmCsvCellDouble(p->cH, rowIdx, kDSecsColScIdx )) == DBL_MAX)
  637. durSecs = 0.25;
  638. // parse the recd/play markers
  639. if((attr = cmCsvCellText(p->cH,rowIdx,kRecdPlayColScIdx)) != NULL )
  640. {
  641. if((rc = _cmScParseMarkers(p,scoreIdx,attr,rowIdx)) != kOkScRC )
  642. return rc;
  643. }
  644. s->type = kNonEvtScId;
  645. s->secs = secs;
  646. s->pitch = midiPitch;
  647. s->flags = flags;
  648. s->dynVal = dynVal;
  649. s->barNumb = barNumb;
  650. s->barNoteIdx = barNoteIdx;
  651. s->durSecs = durSecs;
  652. s->csvRowNumb = rowIdx+1;
  653. return rc;
  654. }
  655. cmScRC_t _cmScParseSectionColumn( cmSc_t* p, unsigned rowIdx, unsigned evtIdx, cmScSect_t* sectList )
  656. {
  657. const cmCsvCell_t* cell;
  658. cmScSect_t* sect;
  659. const cmChar_t* label;
  660. // most rows don't have section labels
  661. if( (cell = cmCsvCellPtr( p->cH,rowIdx,kSectionColScIdx)) == NULL
  662. || (label = cmCsvCellText(p->cH,rowIdx,kSectionColScIdx)) == NULL)
  663. return kOkScRC;
  664. // verify the section id type
  665. if( cell->lexTId != p->sectionLexTId && cell->lexTId != kIntLexTId )
  666. return cmErrMsg(&p->err,kSyntaxErrScRC,"'section' column format error.");
  667. sect = cmMemAllocZ(cmScSect_t,1);
  668. sect->label = label;
  669. sect->startIdx = evtIdx;
  670. //printf("section:%s\n",label);
  671. cmScSect_t* sp = sectList;
  672. assert( sp != NULL );
  673. while( sp->link != NULL )
  674. sp = sp->link;
  675. sp->link = sect;
  676. return kOkScRC;
  677. }
  678. cmScoreSection_t* _cmScLabelToSection( cmSc_t* p, const cmChar_t* label )
  679. {
  680. int i;
  681. for(i=0; i<p->sectCnt; ++i)
  682. if( strcmp(p->sect[i].label,label) == 0 )
  683. return p->sect + i;
  684. return NULL;
  685. }
  686. // Calculate the total number of all types of sets and
  687. // then convert each of the cmScSet_t linked list's to
  688. // a single linear cmScoreSet_t list (p->sets[]).
  689. cmScRC_t _cmScProcSets( cmSc_t* p )
  690. {
  691. cmScRC_t rc = kOkScRC;
  692. // calculate the count of all sets
  693. unsigned i;
  694. unsigned n = 0;
  695. cmScSet_t* sp = p->setList;
  696. for(n=0; sp!=NULL; sp=sp->link)
  697. if( sp->eles != NULL )
  698. ++n;
  699. // allocate the linear set array
  700. p->sets = cmMemAllocZ(cmScoreSet_t,n);
  701. p->setCnt = n;
  702. // fill in the linear set array
  703. sp = p->setList;
  704. for(i=0; sp!=NULL; sp=sp->link)
  705. if( sp->eles != NULL )
  706. {
  707. unsigned j;
  708. unsigned en;
  709. unsigned rowNumb = 0;
  710. assert( i<n );
  711. // get the count of elements assoc'd with this set
  712. cmScSetEle_t* ep = sp->eles;
  713. for(en=0; ep!=NULL; ep=ep->link)
  714. ++en;
  715. assert( en > 0 );
  716. // allocate the element array
  717. p->sets[i].eleCnt = en;
  718. p->sets[i].eleArray = cmMemAllocZ(cmScoreEvt_t*,en);
  719. // fill in the element array
  720. ep = sp->eles;
  721. unsigned graceCnt = 0;
  722. for(j=0; ep!=NULL; ep=ep->link,++j)
  723. {
  724. assert(ep->eleIdx != cmInvalidIdx && ep->eleIdx<p->cnt);
  725. p->sets[i].eleArray[j] = p->array + ep->eleIdx;
  726. assert( cmIsFlag( p->sets[i].eleArray[j]->flags, sp->typeFl) );
  727. rowNumb = p->array[ep->eleIdx].csvRowNumb;
  728. unsigned flags = p->array[ep->eleIdx].flags;
  729. // count grace notes
  730. if( cmIsFlag(flags,kGraceScFl) )
  731. ++graceCnt;
  732. // validate the 'frac' field - all but the last note in
  733. // tempo and non-grace evenness sets must have a non-zero 'frac' value.
  734. if( en>0 && j<en-1 && (sp->typeFl==kTempoScFl || (sp->typeFl==kEvenScFl && graceCnt==0)) && p->array[ep->eleIdx].frac==0)
  735. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The note on row number %i must have a non-zero 'frac' value.",p->array[ep->eleIdx].csvRowNumb);
  736. }
  737. // get the count of sections assoc'd with this set
  738. ep = sp->sects;
  739. for(en=0; ep!=NULL; ep=ep->link)
  740. ++en;
  741. // allocate the section array
  742. p->sets[i].varId = _cmScVarFlagToId(sp->typeFl);
  743. p->sets[i].sectCnt = en;
  744. p->sets[i].sectArray = cmMemAllocZ(cmScoreSection_t*,en);
  745. p->sets[i].symArray = cmMemAllocZ(unsigned,en);
  746. p->sets[i].costSymArray = cmMemAllocZ(unsigned,en);
  747. // fill in the section array with sections which this set will be applied to
  748. ep = sp->sects;
  749. for(j=0; ep!=NULL; ep=ep->link,++j)
  750. {
  751. cmScoreSection_t* sp;
  752. assert(ep->label != NULL);
  753. if((sp = _cmScLabelToSection(p, ep->label )) == NULL )
  754. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The section labelled '%s' could not be found for the set which includes row number %i.",ep->label,rowNumb);
  755. else
  756. {
  757. if( cmSymTblIsValid(p->stH) )
  758. {
  759. // THIS PRINTF PREVENTS CRASH ON OSX ???????
  760. //printf("PROCSET:%i %c %s\n",p->sets[i].varId,_cmScVarIdToChar(p->sets[i].varId),ep->label);
  761. char cc = _cmScVarIdToChar(p->sets[i].varId);
  762. int nn = snprintf(NULL,0,"%c-%s",cc,ep->label);
  763. char b[nn+2];
  764. snprintf(b,nn,"%c-%s",cc,ep->label);
  765. p->sets[i].symArray[j] = cmSymTblRegisterSymbol(p->stH,b);
  766. snprintf(b,nn+1,"c%c-%s",cc,ep->label);
  767. p->sets[i].costSymArray[j] = cmSymTblRegisterSymbol(p->stH,b);
  768. //p->sets[i].symArray[j] = cmSymTblRegisterFmt(p->stH,"%c-%s", cc,ep->label);
  769. //p->sets[i].costSymArray[j] = cmSymTblRegisterFmt(p->stH,"c%c-%s",cc,ep->label);
  770. }
  771. else
  772. {
  773. p->sets[i].symArray[j] = cmInvalidId;
  774. p->sets[i].costSymArray[j] = cmInvalidId;
  775. }
  776. p->sets[i].sectArray[j] = sp;
  777. sp->setArray = cmMemResizeP(cmScoreSet_t*,sp->setArray,++sp->setCnt);
  778. sp->setArray[sp->setCnt-1] = p->sets + i;
  779. }
  780. }
  781. ++i;
  782. }
  783. assert(i==n);
  784. // assign each set to the location which contains it's last element.
  785. // (this is earliest point in the score location where all the
  786. // performance information contained in the set may be valid)
  787. for(i=0; i<p->setCnt; ++i)
  788. {
  789. assert( p->sets[i].eleCnt >= 1 );
  790. // get a ptr to the last element for the ith set
  791. const cmScoreEvt_t* ep = p->sets[i].eleArray[ p->sets[i].eleCnt-1 ];
  792. unsigned j,k;
  793. // find the location which contains the last element
  794. for(j=0; j<p->locCnt; ++j)
  795. {
  796. for(k=0; k<p->loc[j].evtCnt; ++k)
  797. if( p->loc[j].evtArray[k] == ep )
  798. break;
  799. if(k<p->loc[j].evtCnt)
  800. break;
  801. }
  802. assert( j<p->locCnt );
  803. // assign the ith set to the location which contains it's last element
  804. p->sets[i].llink = p->loc[j].setList;
  805. p->loc[j].setList = p->sets + i;
  806. }
  807. return rc;
  808. }
  809. cmScRC_t _cmScProcSections( cmSc_t* p, cmScSect_t* sectList )
  810. {
  811. cmScRC_t rc = kOkScRC;
  812. unsigned i;
  813. // count the sections
  814. cmScSect_t* sp = sectList;
  815. p->sectCnt = 0;
  816. for(; sp!=NULL; sp=sp->link)
  817. if( sp->label != NULL )
  818. ++p->sectCnt;
  819. // alloc a section array
  820. p->sect = cmMemAllocZ(cmScoreSection_t,p->sectCnt);
  821. // fill the section array
  822. sp = sectList;
  823. for(i=0; sp!=NULL; sp=sp->link)
  824. if( sp->label != NULL )
  825. {
  826. p->sect[i].label = sp->label;
  827. p->sect[i].index = i;
  828. p->sect[i].begEvtIndex = sp->startIdx;
  829. ++i;
  830. }
  831. // assign the begSectPtr to each section
  832. for(i=0; i<p->sectCnt; ++i)
  833. {
  834. assert( p->sect[i].begEvtIndex < p->cnt );
  835. unsigned j,k;
  836. const cmScoreEvt_t* ep = p->array + p->sect[i].begEvtIndex;
  837. for(j=0; j<p->locCnt; ++j)
  838. {
  839. for(k=0; k<p->loc[j].evtCnt; ++k)
  840. if( p->loc[j].evtArray[k] == ep )
  841. {
  842. p->loc[j].begSectPtr = p->sect + i;
  843. p->sect[i].locPtr = p->loc + j;
  844. break;
  845. }
  846. if( k<p->loc[j].evtCnt)
  847. break;
  848. }
  849. }
  850. // release the section linked list
  851. sp = sectList;
  852. cmScSect_t* np = NULL;
  853. while(sp!=NULL)
  854. {
  855. np = sp->link;
  856. cmMemFree(sp);
  857. sp = np;
  858. }
  859. //_cmScPrintSets("Sets",p->setList );
  860. _cmScProcSets(p);
  861. return rc;
  862. }
  863. const cmScoreLoc_t* _cmScFindMarkLoc( cmSc_t* p, cmMarkScMId_t cmdId, unsigned labelSymId, const cmScoreMarker_t** markRef )
  864. {
  865. unsigned i;
  866. for(i=0; i<p->locCnt; ++i)
  867. {
  868. cmScoreMarker_t* smp = p->loc[i].markList;
  869. for(; smp!=NULL; smp=smp->link)
  870. {
  871. if( smp->markTypeId==cmdId && smp->labelSymId==labelSymId )
  872. return p->loc + i;
  873. if( markRef != NULL )
  874. *markRef = smp;
  875. }
  876. }
  877. return NULL;
  878. }
  879. unsigned _cmScMarkerLabelIndex( cmSc_t* p, unsigned labelSymId )
  880. {
  881. unsigned i;
  882. for(i=0; i<p->markLabelCnt; ++i)
  883. if( p->markLabelArray[i] == labelSymId )
  884. return i;
  885. return cmInvalidIdx;
  886. }
  887. // Verify that the record/play begin/end and fade markers fall in the correct time order.
  888. // (e.g. 'begin' must be before 'end' and 'fade' must be between and 'begin' and 'end').
  889. cmScRC_t _cmScValidateMarkers( cmSc_t* p )
  890. {
  891. cmScRC_t rc = kOkScRC;
  892. unsigned i;
  893. for(i=0; i<p->locCnt; ++i)
  894. {
  895. cmScoreMarker_t* sm0p = p->loc[i].markList;
  896. for(; sm0p!=NULL; sm0p=sm0p->link)
  897. {
  898. const cmScoreLoc_t* sl0p;
  899. const cmScoreLoc_t* sl1p;
  900. switch( sm0p->markTypeId )
  901. {
  902. case kRecdBegScMId:
  903. if((sl0p = _cmScFindMarkLoc(p,kRecdEndScMId, sm0p->labelSymId, NULL )) == NULL )
  904. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'record begin' marker at CSV row index %i does not have an associated 'record end' marker.",sm0p->csvRowIdx);
  905. else
  906. if( sl0p->index <= p->loc[i].index )
  907. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'record end' marker comes before associated with the 'record begin' marker at CSV row index %i.",sm0p->csvRowIdx);
  908. break;
  909. case kRecdEndScMId:
  910. if((sl0p = _cmScFindMarkLoc(p,kRecdBegScMId, sm0p->labelSymId, NULL )) == NULL )
  911. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'record end' marker at CSV row index %i does not have an associated 'record begin' marker.",sm0p->csvRowIdx);
  912. else
  913. if( sl0p->index > p->loc[i].index )
  914. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'record begin' marker comes after the associated with the 'record end' marker at CSV row index %i.",sm0p->csvRowIdx);
  915. break;
  916. case kFadeScMId:
  917. if((sl0p = _cmScFindMarkLoc(p,kRecdBegScMId, sm0p->labelSymId, NULL )) == NULL )
  918. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'fade' marker at CSV row index %i does not have an associated 'record begin' marker.",sm0p->csvRowIdx);
  919. else
  920. if((sl1p = _cmScFindMarkLoc(p,kRecdEndScMId, sm0p->labelSymId, NULL )) == NULL )
  921. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'fade' marker at CSV row index %i does not have an associated 'record end' marker.",sm0p->csvRowIdx);
  922. else
  923. if( sl0p->index > p->loc[i].index || sl1p->index < p->loc[i].index )
  924. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'fade' marker at CSV row index %i is not between it's associated 'record begin' and 'record end' markers.",sm0p->csvRowIdx);
  925. break;
  926. case kPlayBegScMId:
  927. if((sl0p = _cmScFindMarkLoc(p,kPlayEndScMId, sm0p->labelSymId, NULL )) == NULL )
  928. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'play begin' marker at CSV row index %i does not have an associated 'play end' marker.",sm0p->csvRowIdx);
  929. else
  930. if( sl0p->index <= p->loc[i].index )
  931. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'play end' marker comes before associated with the 'play begin' marker at CSV row index %i.",sm0p->csvRowIdx);
  932. break;
  933. case kPlayEndScMId:
  934. if((sl0p = _cmScFindMarkLoc(p,kPlayBegScMId, sm0p->labelSymId, NULL )) == NULL )
  935. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'play end' marker at CSV row index %i does not have an associated 'play begin' marker.",sm0p->csvRowIdx);
  936. else
  937. if( sl0p->index > p->loc[i].index )
  938. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'play begin' marker comes after the associated with the 'play end' marker at CSV row index %i.",sm0p->csvRowIdx);
  939. break;
  940. default:
  941. break;
  942. }
  943. }
  944. }
  945. return rc;
  946. }
  947. cmScRC_t _cmScProcMarkers( cmSc_t* p )
  948. {
  949. // for each marker in the p->markList
  950. // (p->markList is created by _cmScParseMarkers() during CSV file parsing.)
  951. cmScMark_t* mp = p->markList;
  952. for(; mp!=NULL; mp=mp->link)
  953. {
  954. assert( mp->scoreIdx < p->cnt );
  955. // get the score location assoc'd with this marker
  956. unsigned locIdx = p->array[ mp->scoreIdx ].locIdx;
  957. assert( locIdx < p->locCnt );
  958. cmScoreLoc_t* slp = p->loc + locIdx;
  959. // create a cmScoreMarker record.
  960. cmScoreMarker_t* smp = cmMemAllocZ(cmScoreMarker_t,1);
  961. smp->markTypeId = mp->cmdId;
  962. smp->labelSymId = mp->labelSymId;
  963. smp->csvRowIdx = mp->csvRowIdx;
  964. smp->scoreLocPtr = slp;
  965. // attach the new scoreMarker record to the assoc'd score loc. recd
  966. if( slp->markList == NULL )
  967. slp->markList = smp;
  968. else
  969. {
  970. cmScoreMarker_t* sm0p = slp->markList;
  971. while( sm0p->link != NULL )
  972. sm0p = sm0p->link;
  973. sm0p->link = smp;
  974. }
  975. // if the id represented by this marker
  976. if( _cmScMarkerLabelIndex(p,smp->labelSymId) == cmInvalidIdx )
  977. {
  978. p->markLabelArray = cmMemResizeP(unsigned,p->markLabelArray,p->markLabelCnt+1);
  979. p->markLabelArray[p->markLabelCnt] = smp->labelSymId;
  980. p->markLabelCnt += 1;
  981. }
  982. }
  983. // validate the markers
  984. return _cmScValidateMarkers(p);
  985. }
  986. cmScRC_t _cmScParseFile( cmSc_t* p, cmCtx_t* ctx, const cmChar_t* fn )
  987. {
  988. cmScRC_t rc = kOkScRC;
  989. unsigned barNoteIdx = 0;
  990. int barEvtIdx = cmInvalidIdx;
  991. int barNumb = 0;
  992. double secs;
  993. double cur_secs = 0;
  994. p->sectList = cmMemAllocZ(cmScSect_t,1); // section zero
  995. //_cmScNewSet(p); // preallocate the first set
  996. // initialize the CSV file parser
  997. if( cmCsvInitialize(&p->cH, ctx ) != kOkCsvRC )
  998. {
  999. rc = cmErrMsg(&p->err,kCsvFailScRC,"Score file initialization failed.");
  1000. goto errLabel;
  1001. }
  1002. // register custom lex token matchers for sci-pitch and section id's
  1003. if( cmCsvLexRegisterMatcher(p->cH, p->sciPitchLexTId = cmCsvLexNextAvailId(p->cH)+0, _cmScLexSciPitchMatcher ) != kOkCsvRC
  1004. ||cmCsvLexRegisterMatcher(p->cH, p->sectionLexTId = cmCsvLexNextAvailId(p->cH)+1, _cmScLexSectionIdMatcher) != kOkCsvRC )
  1005. {
  1006. rc = cmErrMsg(&p->err,kCsvFailScRC,"CSV token matcher registration failed.");
  1007. goto errLabel;
  1008. }
  1009. // parse the CSV file
  1010. if( cmCsvParseFile(p->cH, fn, 0 ) != kOkCsvRC )
  1011. {
  1012. rc = cmErrMsg(&p->err,kCsvFailScRC,"CSV file parsing failed on the file '%s'.",cmStringNullGuard(fn));
  1013. goto errLabel;
  1014. }
  1015. // allocate the score event array
  1016. p->cnt = cmCsvRowCount(p->cH);
  1017. p->array = cmMemAllocZ(cmScoreEvt_t,p->cnt);
  1018. unsigned i,j;
  1019. // skip labels line - start on line 1
  1020. for(i=1,j=0; i<p->cnt && rc==kOkScRC; ++i)
  1021. {
  1022. // get the row 'type' label
  1023. const char* typeLabel;
  1024. if((typeLabel = cmCsvCellText(p->cH,i,kTypeLabelColScIdx)) == NULL )
  1025. {
  1026. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"No type label.");
  1027. break;
  1028. }
  1029. // convert the row 'type' label to an id
  1030. unsigned tid;
  1031. if((tid = _cmScEvtTypeLabelToId(typeLabel)) == kInvalidEvtScId)
  1032. {
  1033. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"Unknown type '%s'.",cmStringNullGuard(typeLabel));
  1034. break;
  1035. }
  1036. // set the score file line number
  1037. p->array[j].line = i + 1;
  1038. secs = DBL_MAX;
  1039. switch(tid)
  1040. {
  1041. case kBarEvtScId: // parse bar lines
  1042. if((rc = _cmScParseBar(p,i,p->array+j,&barNumb)) == kOkScRC )
  1043. {
  1044. barNoteIdx = 0;
  1045. barEvtIdx = j;
  1046. p->array[j].index = j;
  1047. ++j;
  1048. }
  1049. break;
  1050. case kNonEvtScId: // parse note-on events
  1051. if((rc = _cmScParseNoteOn(p, i, p->array, j, barNumb, barNoteIdx )) == kOkScRC )
  1052. {
  1053. secs = p->array[j].secs;
  1054. if( p->array[j].secs == DBL_MAX )
  1055. p->array[j].secs = cur_secs;
  1056. if( cmIsFlag(p->array[j].flags,kSkipScFl) == false )
  1057. {
  1058. p->array[j].index = j;
  1059. ++j;
  1060. }
  1061. ++barNoteIdx;
  1062. }
  1063. break;
  1064. default:
  1065. // Returns DBL_MAX on error.
  1066. secs = cmCsvCellDouble(p->cH, i, kSecsColScIdx );
  1067. break;
  1068. }
  1069. if( secs != DBL_MAX )
  1070. cur_secs = secs;
  1071. // form the section list
  1072. if( j > 0 )
  1073. if((rc = _cmScParseSectionColumn(p,i,j-1,p->sectList)) != kOkScRC )
  1074. break;
  1075. // the bar lines don't have times so set the time of the bar line to the
  1076. // time of the first event in the bar.
  1077. if( barEvtIdx != cmInvalidIdx && secs != DBL_MAX )
  1078. {
  1079. assert( p->array[ barEvtIdx ].type == kBarEvtScId );
  1080. p->array[ barEvtIdx ].secs = secs;
  1081. // handle the case where the previous bar had no events
  1082. // BUG BUG BUG this is a hack which will fail if the first bar does not have events.
  1083. if( barEvtIdx>=1 && p->array[ barEvtIdx-1].type == kBarEvtScId )
  1084. p->array[ barEvtIdx-1].secs = secs;
  1085. barEvtIdx = cmInvalidIdx;
  1086. }
  1087. }
  1088. if( rc == kSyntaxErrScRC )
  1089. {
  1090. cmErrMsg(&p->err,rc,"Syntax error on line %i in '%s'.",i+1,cmStringNullGuard(fn));
  1091. goto errLabel;
  1092. }
  1093. p->cnt = j;
  1094. errLabel:
  1095. return rc;
  1096. }
  1097. cmScRC_t _cmScInitLocArray( cmSc_t* p )
  1098. {
  1099. cmScRC_t rc = kOkScRC;
  1100. double maxDSecs = 0; // max time between events that are considered simultaneous
  1101. unsigned barNumb = 0;
  1102. int i,j,k;
  1103. if( p->cnt==0)
  1104. return rc;
  1105. p->locCnt = 1;
  1106. // count the number of unique time locations in the score
  1107. p->locCnt = 1;
  1108. for(i=1; i<p->cnt; ++i )
  1109. {
  1110. if( p->array[i].secs < p->array[i-1].secs )
  1111. 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);
  1112. if( (p->array[i].secs - p->array[i-1].secs) > maxDSecs )
  1113. ++p->locCnt;
  1114. }
  1115. if( rc != kOkScRC )
  1116. return rc;
  1117. // allocate the loc. array
  1118. p->loc = cmMemAllocZ(cmScoreLoc_t,p->locCnt);
  1119. // fill in the location array
  1120. for(i=0,k=0; i<p->cnt; ++k)
  1121. {
  1122. j = i+1;
  1123. // get the count of events at this location
  1124. while( j<p->cnt && p->array[j].secs - p->array[j-1].secs <= maxDSecs )
  1125. ++j;
  1126. assert(k<p->locCnt);
  1127. p->loc[k].index = k;
  1128. p->loc[k].evtCnt = j-i;
  1129. p->loc[k].evtArray = cmMemAllocZ(cmScoreEvt_t*,p->loc[k].evtCnt);
  1130. // fill in the location record event pointers
  1131. for(j=0; j<p->loc[k].evtCnt; ++j)
  1132. {
  1133. p->loc[k].evtArray[j] = p->array + (i + j);
  1134. p->loc[k].evtArray[j]->locIdx = k;
  1135. if( p->array[i+j].type == kBarEvtScId )
  1136. barNumb = p->array[i+j].barNumb;
  1137. }
  1138. // fill in the location record
  1139. p->loc[k].secs = p->array[i].secs;
  1140. p->loc[k].barNumb = barNumb;
  1141. i += p->loc[k].evtCnt;
  1142. }
  1143. assert( p->locCnt == k );
  1144. return rc;
  1145. }
  1146. 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, cmSymTblH_t stH )
  1147. {
  1148. cmScRC_t rc = kOkScRC;
  1149. if((rc = cmScoreFinalize(hp)) != kOkScRC )
  1150. return rc;
  1151. cmSc_t* p = cmMemAllocZ(cmSc_t,1);
  1152. cmErrSetup(&p->err,&ctx->rpt,"Score");
  1153. p->stH = stH;
  1154. if((rc = _cmScParseFile(p,ctx,fn)) != kOkScRC )
  1155. goto errLabel;
  1156. if((rc = _cmScInitLocArray(p)) != kOkScRC )
  1157. goto errLabel;
  1158. if((rc = _cmScProcSections(p,p->sectList)) != kOkScRC )
  1159. goto errLabel;
  1160. if((rc = _cmScProcMarkers(p)) != kOkScRC )
  1161. goto errLabel;
  1162. // load the dynamic reference array
  1163. if( dynRefArray != NULL && dynRefCnt > 0)
  1164. {
  1165. unsigned n = _cmScDynLabelCount();
  1166. if( dynRefCnt != n )
  1167. {
  1168. rc = cmErrMsg(&p->err,kInvalidDynRefCntScRC,"The count of dynamics labels must be %i not %i.",n,dynRefCnt);
  1169. goto errLabel;
  1170. }
  1171. p->dynRefArray = cmMemAllocZ(unsigned,dynRefCnt);
  1172. memcpy(p->dynRefArray,dynRefArray,sizeof(unsigned)*dynRefCnt);
  1173. p->dynRefCnt = dynRefCnt;
  1174. }
  1175. p->srate = srate;
  1176. p->cbFunc = cbFunc;
  1177. p->cbArg = cbArg;
  1178. p->fn = cmMemAllocStr(fn);
  1179. p->nxtLocIdx = 0;
  1180. p->minSetLocIdx = cmInvalidIdx;
  1181. p->maxSetLocIdx = cmInvalidIdx;
  1182. hp->h = p;
  1183. cmScoreClearPerfInfo(*hp);
  1184. //cmScorePrintLoc(*hp);
  1185. errLabel:
  1186. if( rc != kOkScRC )
  1187. _cmScFinalize(p);
  1188. return rc;
  1189. }
  1190. cmScRC_t cmScoreFinalize( cmScH_t* hp )
  1191. {
  1192. cmScRC_t rc = kOkScRC;
  1193. if( hp == NULL || cmScoreIsValid(*hp) == false )
  1194. return kOkScRC;
  1195. cmSc_t* p = _cmScHandleToPtr(*hp);
  1196. if((rc = _cmScFinalize(p)) != kOkScRC )
  1197. return rc;
  1198. hp->h = NULL;
  1199. return rc;
  1200. }
  1201. const cmChar_t* cmScoreFileName( cmScH_t h )
  1202. {
  1203. cmSc_t* p = _cmScHandleToPtr(h);
  1204. return p->fn;
  1205. }
  1206. double cmScoreSampleRate( cmScH_t h )
  1207. {
  1208. cmSc_t* p = _cmScHandleToPtr(h);
  1209. return p->srate;
  1210. }
  1211. bool cmScoreIsValid( cmScH_t h )
  1212. { return h.h != NULL; }
  1213. unsigned cmScoreEvtCount( cmScH_t h )
  1214. {
  1215. cmSc_t* p = _cmScHandleToPtr(h);
  1216. return p->cnt;
  1217. }
  1218. cmScoreEvt_t* cmScoreEvt( cmScH_t h, unsigned idx )
  1219. {
  1220. cmSc_t* p = _cmScHandleToPtr(h);
  1221. if( idx >= p->cnt )
  1222. {
  1223. cmErrMsg(&p->err,kInvalidIdxScRC,"%i is an invalid index for %i records.",idx,p->cnt);
  1224. return NULL;
  1225. }
  1226. return p->array + idx;
  1227. }
  1228. cmScoreEvt_t* cmScoreBarEvt( cmScH_t h, unsigned barNumb )
  1229. {
  1230. cmSc_t* p = _cmScHandleToPtr(h);
  1231. unsigned i = 0;
  1232. for(; i<p->cnt; ++i)
  1233. if( p->array[i].type==kBarEvtScId && p->array[i].barNumb==barNumb )
  1234. return p->array + i;
  1235. return NULL;
  1236. }
  1237. unsigned cmScoreSectionCount( cmScH_t h )
  1238. {
  1239. cmSc_t* p = _cmScHandleToPtr(h);
  1240. return p->sectCnt;
  1241. }
  1242. cmScoreSection_t* cmScoreSection( cmScH_t h, unsigned idx )
  1243. {
  1244. cmSc_t* p = _cmScHandleToPtr(h);
  1245. assert( idx < p->sectCnt);
  1246. return p->sect + idx;
  1247. }
  1248. unsigned cmScoreLocCount( cmScH_t h )
  1249. {
  1250. cmSc_t* p = _cmScHandleToPtr(h);
  1251. return p->locCnt;
  1252. }
  1253. cmScoreLoc_t* cmScoreLoc( cmScH_t h, unsigned idx )
  1254. {
  1255. cmSc_t* p = _cmScHandleToPtr(h);
  1256. if( idx >= p->locCnt )
  1257. {
  1258. cmErrMsg(&p->err,kInvalidIdxScRC,"%i is an invalid index for %i location records.",idx,p->locCnt);
  1259. return NULL;
  1260. }
  1261. return p->loc + idx;
  1262. }
  1263. cmScoreLoc_t* cmScoreEvtLoc( cmScH_t h, const cmScoreEvt_t* evt )
  1264. {
  1265. cmSc_t* p = _cmScHandleToPtr(h);
  1266. unsigned i;
  1267. if( evt != NULL )
  1268. for(i=0; i<p->locCnt; ++i)
  1269. {
  1270. unsigned j;
  1271. for(j=0; j<p->loc[i].evtCnt; ++j)
  1272. if( p->loc[i].evtArray[j] == evt )
  1273. return p->loc + i;
  1274. }
  1275. return NULL;
  1276. }
  1277. void cmScorePrintLoc( cmScH_t h )
  1278. {
  1279. unsigned i = 0;
  1280. cmSc_t* p = _cmScHandleToPtr(h);
  1281. unsigned colCnt = 10;
  1282. int bufCharCnt = 4;
  1283. char buf[ bufCharCnt ];
  1284. const char* emptyStr = " ";
  1285. // for each set of 'colCnt' columns
  1286. for(i=0; i<p->locCnt; i+=colCnt )
  1287. {
  1288. // print the location 'index' line
  1289. unsigned c,j,k;
  1290. printf("index: ");
  1291. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1292. printf("%7i ",j);
  1293. printf("\n");
  1294. // print the 'sectn' label line
  1295. printf("sectn: ");
  1296. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1297. if( p->loc[j].begSectPtr==NULL )
  1298. printf("%s",emptyStr);
  1299. else
  1300. printf("%7s ",p->loc[j].begSectPtr->label);
  1301. printf("\n");
  1302. // calculate the max number of simultan. events at any one location
  1303. // for this set of 'colCnt' columns.
  1304. unsigned n=0;
  1305. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1306. if( p->loc[j].evtCnt > n )
  1307. n = p->loc[j].evtCnt;
  1308. // for each 'sco' line
  1309. for(k=0; k<n; ++k)
  1310. {
  1311. printf("sco%2i: ",k);
  1312. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1313. if( k >= p->loc[j].evtCnt )
  1314. printf("%s",emptyStr);
  1315. else
  1316. {
  1317. switch(p->loc[j].evtArray[k]->type)
  1318. {
  1319. case kBarEvtScId:
  1320. printf(" |%3i ",p->loc[j].evtArray[k]->barNumb);
  1321. break;
  1322. case kNonEvtScId:
  1323. {
  1324. int bn=16;
  1325. char b[bn];
  1326. strcpy(b,cmMidiToSciPitch(p->loc[j].evtArray[k]->pitch,NULL,0));
  1327. strcat(b,_cmScFlagsToStr( p->loc[j].evtArray[k]->flags,buf,bufCharCnt));
  1328. assert(strlen(b)<bn);
  1329. printf("%7s ",b);
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. printf("\n");
  1335. }
  1336. // calc the max number of set triggers which will occur on
  1337. // any one location for this set of 'colCnt' columns.
  1338. n=0;
  1339. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1340. if(p->loc[j].begSectPtr != NULL && p->loc[j].begSectPtr->setCnt > n )
  1341. n = p->loc[j].begSectPtr->setCnt;
  1342. for(k=0; k<n; ++k)
  1343. {
  1344. printf("trig%1i: ",k);
  1345. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1346. {
  1347. if( p->loc[j].begSectPtr != NULL && k<p->loc[j].begSectPtr->setCnt )
  1348. {
  1349. const cmScoreSet_t* sp = p->loc[j].begSectPtr->setArray[k];
  1350. printf(" %3s-%c ",p->loc[j].begSectPtr->label,_cmScVarIdToChar(sp->varId) );
  1351. }
  1352. else
  1353. {
  1354. printf("%s",emptyStr);
  1355. }
  1356. }
  1357. printf("\n");
  1358. }
  1359. printf("\n");
  1360. }
  1361. }
  1362. unsigned cmScoreSetCount( cmScH_t h )
  1363. {
  1364. cmSc_t* p = _cmScHandleToPtr(h);
  1365. return p->setCnt;
  1366. }
  1367. unsigned cmScoreMarkerLabelCount( cmScH_t h )
  1368. {
  1369. cmSc_t* p = _cmScHandleToPtr(h);
  1370. return p->markLabelCnt;
  1371. }
  1372. unsigned cmScoreMarkerLabelSymbolId( cmScH_t h, unsigned idx )
  1373. {
  1374. cmSc_t* p = _cmScHandleToPtr(h);
  1375. assert( idx < p->markLabelCnt );
  1376. return p->markLabelArray[idx];
  1377. }
  1378. const cmScoreMarker_t* cmScoreMarker( cmScH_t h, cmMarkScMId_t markMId, unsigned labelSymId )
  1379. {
  1380. cmSc_t* p = _cmScHandleToPtr(h);
  1381. const cmScoreMarker_t* smp = NULL;
  1382. if( _cmScFindMarkLoc(p, markMId, labelSymId, &smp ) == NULL )
  1383. return NULL;
  1384. return smp;
  1385. }
  1386. cmScRC_t cmScoreSeqNotify( cmScH_t h )
  1387. {
  1388. cmScRC_t rc = kOkScRC;
  1389. cmSc_t* p = _cmScHandleToPtr(h);
  1390. cmScMsg_t m;
  1391. unsigned i;
  1392. if( p->cbFunc != NULL )
  1393. {
  1394. memset(&m.u.evt,0,sizeof(m.u.evt));
  1395. m.typeId = kBeginMsgScId;
  1396. p->cbFunc(p->cbArg,&m,sizeof(m));
  1397. m.typeId = kEventMsgScId;
  1398. for(i=0; i<p->cnt; ++i)
  1399. {
  1400. m.u.evt = p->array[i];
  1401. p->cbFunc(p->cbArg,&m,sizeof(m));
  1402. }
  1403. m.typeId = kSectionMsgScId;
  1404. for(i=0; i<p->sectCnt; ++i)
  1405. {
  1406. m.u.sect = p->sect[i];
  1407. p->cbFunc(p->cbArg,&m,sizeof(m));
  1408. }
  1409. memset(&m.u.evt,0,sizeof(m.u.evt));
  1410. m.typeId = kEndMsgScId;
  1411. p->cbFunc(p->cbArg,&m,sizeof(m));
  1412. }
  1413. return rc;
  1414. }
  1415. void cmScoreClearPerfInfo( cmScH_t h )
  1416. {
  1417. cmSc_t* p = _cmScHandleToPtr(h);
  1418. unsigned i;
  1419. for(i=0; i<p->cnt; ++i)
  1420. {
  1421. p->array[i].perfSmpIdx = cmInvalidIdx;
  1422. p->array[i].perfVel = 0;
  1423. p->array[i].perfDynLvl = 0;
  1424. }
  1425. for(i=0; i<p->locCnt; ++i)
  1426. {
  1427. cmScoreSet_t* sp = p->loc[i].setList;
  1428. for(; sp!=NULL; sp=sp->llink)
  1429. sp->doneFl = false;
  1430. }
  1431. for(i=0; i<p->sectCnt; ++i)
  1432. {
  1433. unsigned j;
  1434. for(j=0; j<kScVarCnt; ++j)
  1435. p->sect[i].vars[j] = DBL_MAX;
  1436. }
  1437. p->nxtLocIdx = 0;
  1438. p->minSetLocIdx = cmInvalidIdx;
  1439. p->maxSetLocIdx = cmInvalidIdx;
  1440. }
  1441. bool _cmScIsSetPerfDone( cmScoreSet_t* sp )
  1442. {
  1443. unsigned i = 0;
  1444. for(i=0; i<sp->eleCnt; ++i)
  1445. if( sp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1446. return false;
  1447. return true;
  1448. }
  1449. void _cmScPerfSortTimes( unsigned *v, unsigned n )
  1450. {
  1451. unsigned i;
  1452. bool fl = true;
  1453. while(fl && n)
  1454. {
  1455. fl = false;
  1456. for(i=1; i<n; ++i)
  1457. {
  1458. if( v[i-1] > v[i] )
  1459. {
  1460. unsigned t = v[i-1];
  1461. v[i-1] = v[i];
  1462. v[i] = t;
  1463. fl = true;
  1464. }
  1465. }
  1466. --n;
  1467. }
  1468. }
  1469. bool _cmScPerfEven(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1470. {
  1471. unsigned i = 0;
  1472. double u = 0;
  1473. double x = 0;
  1474. bool sortFl = false;
  1475. bool printFl = true;
  1476. unsigned v[ stp->eleCnt ];
  1477. unsigned d[ stp->eleCnt - 1];
  1478. assert( stp->eleCnt > 1 );
  1479. // calculate the sum of the time between events
  1480. for(i=0; i<stp->eleCnt; ++i)
  1481. {
  1482. // if this event was not received - then the set is not valid
  1483. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1484. {
  1485. if( printFl && printMissFl)
  1486. printf("EVENESS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1487. return false;
  1488. }
  1489. // load v[i]
  1490. v[i] = stp->eleArray[i]->perfSmpIdx;
  1491. // check for out of order elements
  1492. if( i> 0 )
  1493. if( v[i] < v[i-1] )
  1494. sortFl = true;
  1495. }
  1496. // sort the times in ascending order
  1497. if( sortFl )
  1498. _cmScPerfSortTimes(v,stp->eleCnt);
  1499. // calc the sum of time differences
  1500. for(i=1; i<stp->eleCnt; ++i)
  1501. u += d[i-1] = v[i] - v[i-1];
  1502. // calculate the average time between events
  1503. u /= stp->eleCnt-1;
  1504. // calculate the std-dev of the time between events
  1505. for(i=0; i<stp->eleCnt-1; ++i)
  1506. x += (d[i]-u)*(d[i]-u);
  1507. double sd = sqrt(x/(stp->eleCnt-1));
  1508. // compute the average z-score
  1509. double c = 0;
  1510. for(i=0; i<stp->eleCnt-1; ++i)
  1511. c += fabs(d[i]-u)/sd;
  1512. stp->value = c/(stp->eleCnt-1);
  1513. stp->doneFl = true;
  1514. if(printFl)
  1515. {
  1516. /*
  1517. for(i=0; i<stp->eleCnt; ++i)
  1518. {
  1519. printf("%i %i ",i,v[i]);
  1520. if( i > 0 )
  1521. printf("%i ", d[i-1]);
  1522. printf("\n");
  1523. }
  1524. */
  1525. printf("%s EVENESS:%f\n",sortFl?"SORTED ":"",stp->value);
  1526. }
  1527. return true;
  1528. }
  1529. bool _cmScPerfDyn( cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1530. {
  1531. double a = 0;
  1532. unsigned i = 0;
  1533. bool printFl = true;
  1534. for(i=0; i<stp->eleCnt; ++i)
  1535. {
  1536. unsigned j;
  1537. // if this event was not received - then the set is not valid
  1538. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1539. {
  1540. if( printFl && printMissFl )
  1541. printf("DYNAMICS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1542. return false;
  1543. }
  1544. unsigned m = 0; // lower bound for the first dyn. category
  1545. // determine the dynamic category for the performed velocity of each event
  1546. for(j=0; j<p->dynRefCnt; ++j)
  1547. {
  1548. // if the vel fall's into the jth dyn. category
  1549. if( m <= stp->eleArray[i]->perfVel && stp->eleArray[i]->perfVel < p->dynRefArray[j] )
  1550. break;
  1551. // store the min vel for the next dyn category
  1552. m = p->dynRefArray[j];
  1553. }
  1554. assert( j < p->dynRefCnt );
  1555. stp->eleArray[i]->perfDynLvl = j+1;
  1556. a += abs((j+1) - stp->eleArray[i]->dynVal);
  1557. if( p->cbFunc != NULL )
  1558. {
  1559. cmScMsg_t m;
  1560. m.typeId = kDynMsgScId;
  1561. m.u.dyn.evtIdx = stp->eleArray[i]->index;
  1562. m.u.dyn.dynLvl = stp->eleArray[i]->perfDynLvl;
  1563. p->cbFunc(p->cbArg,&m,sizeof(m));
  1564. }
  1565. }
  1566. stp->value = a / stp->eleCnt;
  1567. stp->doneFl = true;
  1568. if( printFl )
  1569. printf("DYNAMICS:%f\n",stp->value);
  1570. return true;
  1571. }
  1572. bool _cmScPerfTempo1(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1573. {
  1574. bool printFl = true;
  1575. unsigned durSmpCnt = 0;
  1576. double durBeats = 0;
  1577. int i;
  1578. for(i=0; i<stp->eleCnt; ++i)
  1579. {
  1580. // if this event was not received - then the set is not valid
  1581. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1582. {
  1583. if( printFl && printMissFl )
  1584. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1585. return false;
  1586. }
  1587. if( i > 0 )
  1588. durSmpCnt += stp->eleArray[i]->perfSmpIdx - stp->eleArray[i-1]->perfSmpIdx;
  1589. durBeats += stp->eleArray[i]->frac;
  1590. }
  1591. stp->value = durBeats / (durSmpCnt / p->srate*60.0 );
  1592. stp->doneFl = true;
  1593. if( printFl )
  1594. printf("TEMPO:%f\n",stp->value);
  1595. return true;
  1596. }
  1597. bool _cmScPerfTempo(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1598. {
  1599. bool printFl = true;
  1600. unsigned durSmpCnt = 0;
  1601. double durBeats = 0;
  1602. int i;
  1603. unsigned bi = cmInvalidIdx;
  1604. unsigned ei = cmInvalidIdx;
  1605. unsigned missCnt = 0;
  1606. for(i=0; i<stp->eleCnt; ++i)
  1607. {
  1608. // if this event was not received - then the set is not valid
  1609. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1610. {
  1611. ++missCnt;
  1612. if( printFl && printMissFl )
  1613. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1614. }
  1615. else
  1616. {
  1617. if( bi == cmInvalidIdx )
  1618. bi = i;
  1619. ei = i;
  1620. }
  1621. }
  1622. if( ei > bi )
  1623. {
  1624. for(i=bi; i<=ei; ++i)
  1625. durBeats += stp->eleArray[i]->frac;
  1626. durSmpCnt = stp->eleArray[ei]->perfSmpIdx - stp->eleArray[bi]->perfSmpIdx;
  1627. stp->value = durBeats / (durSmpCnt / (p->srate*60.0) );
  1628. stp->doneFl = true;
  1629. }
  1630. if( printFl )
  1631. printf("TEMPO:%f bi:%i ei:%i secs:%f bts:%f\n",stp->value,bi,ei,durSmpCnt/p->srate,durBeats);
  1632. return true;
  1633. }
  1634. void _cmScPerfExec( cmSc_t* p, cmScoreSet_t* sp, bool printMissFl )
  1635. {
  1636. if( sp->doneFl == false )
  1637. {
  1638. switch( sp->varId )
  1639. {
  1640. case kEvenVarScId:
  1641. _cmScPerfEven(p,sp,printMissFl);
  1642. break;
  1643. case kDynVarScId:
  1644. _cmScPerfDyn(p,sp,printMissFl);
  1645. break;
  1646. case kTempoVarScId:
  1647. _cmScPerfTempo(p,sp,printMissFl);
  1648. break;
  1649. default:
  1650. { assert(0); }
  1651. }
  1652. }
  1653. }
  1654. void _cmScPerfExecRange( cmSc_t* p )
  1655. {
  1656. if( p->minSetLocIdx == cmInvalidIdx || p->maxSetLocIdx==cmInvalidIdx )
  1657. return;
  1658. unsigned i = p->minSetLocIdx;
  1659. for(; i<=p->maxSetLocIdx; ++i)
  1660. {
  1661. cmScoreSet_t* sp = p->loc[i].setList;
  1662. for(; sp!=NULL; sp=sp->llink)
  1663. _cmScPerfExec(p,sp,true);
  1664. }
  1665. }
  1666. bool _cmScSetPerfEvent( cmSc_t* p, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1667. {
  1668. assert(locIdx < p->locCnt );
  1669. cmScoreLoc_t* lp = p->loc + locIdx;
  1670. bool doneFl = true;
  1671. unsigned i;
  1672. #ifndef NDEBUG
  1673. bool foundFl = false;
  1674. #endif
  1675. // locate the event at the loc[locIdx]
  1676. for(i=0; i<lp->evtCnt; ++i)
  1677. {
  1678. cmScoreEvt_t* ep = lp->evtArray[i];
  1679. if( ep->type == kNonEvtScId )
  1680. {
  1681. if( ep->pitch == pitch )
  1682. {
  1683. assert( ep->perfSmpIdx == cmInvalidIdx );
  1684. ep->perfSmpIdx = smpIdx;
  1685. ep->perfVel = vel;
  1686. #ifndef NDEBUG
  1687. foundFl = true;
  1688. #endif
  1689. }
  1690. // check if all notes have arrived for this location
  1691. if( ep->perfSmpIdx == cmInvalidIdx )
  1692. doneFl = false;
  1693. }
  1694. }
  1695. // the event must always be found
  1696. assert( foundFl );
  1697. return doneFl;
  1698. }
  1699. bool cmScoreSetPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1700. {
  1701. cmSc_t* p = _cmScHandleToPtr(h);
  1702. return _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1703. }
  1704. void cmScoreExecPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1705. {
  1706. unsigned i;
  1707. cmSc_t* p = _cmScHandleToPtr(h);
  1708. bool doneFl = _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1709. unsigned printLvl = 1;
  1710. cmScoreLoc_t* lp = p->loc + locIdx;
  1711. // all events for a location must be complete to trigger attached events
  1712. if( doneFl == false )
  1713. return;
  1714. if( p->loc[locIdx].setList != NULL )
  1715. {
  1716. // set idx of most recent loc w/ a set end event
  1717. p->maxSetLocIdx = locIdx;
  1718. if( p->minSetLocIdx == cmInvalidIdx )
  1719. p->minSetLocIdx = locIdx;
  1720. }
  1721. // attempt to calculate all sets between loc[p->minSetLocIdx] and loc[p->maxSetLocIdx]
  1722. _cmScPerfExecRange(p);
  1723. // prevent event retriggering or going backwards
  1724. if( printLvl && locIdx < p->nxtLocIdx )
  1725. {
  1726. printf("----- BACK ----- \n");
  1727. return;
  1728. }
  1729. if( printLvl && locIdx > p->nxtLocIdx )
  1730. {
  1731. printf("----- SKIP ----- \n");
  1732. }
  1733. // for each location between the current and previous location
  1734. for(; p->nxtLocIdx<=locIdx; ++p->nxtLocIdx)
  1735. {
  1736. lp = p->loc + p->nxtLocIdx;
  1737. // if this location is the start of a new section - then apply
  1738. // sets that are assigned to this section
  1739. if( lp->begSectPtr != NULL && lp->begSectPtr->setCnt > 0 )
  1740. {
  1741. // notice the location of the oldest section start - once we cross this point
  1742. // it is too late to notice set completions - so incr p->inSetLocIdx
  1743. if( lp->begSectPtr->setCnt )
  1744. p->minSetLocIdx = p->nxtLocIdx+1;
  1745. for(i=0; i<lp->begSectPtr->setCnt; ++i)
  1746. {
  1747. cmScoreSet_t* stp = lp->begSectPtr->setArray[i];
  1748. // temporarily commented out for testing purposes
  1749. // if( stp->doneFl == false )
  1750. // _cmScPerfExec(p, stp, printLvl>0 );
  1751. if( stp->doneFl )
  1752. {
  1753. assert( stp->varId < kScVarCnt );
  1754. lp->begSectPtr->vars[ stp->varId ] = stp->value;
  1755. if( p->cbFunc != NULL )
  1756. {
  1757. cmScMsg_t m;
  1758. m.typeId = kVarMsgScId;
  1759. m.u.meas.varId = stp->varId;
  1760. m.u.meas.value = stp->value;
  1761. p->cbFunc(p->cbArg,&m,sizeof(m));
  1762. }
  1763. }
  1764. }
  1765. }
  1766. }
  1767. }
  1768. void cmScoreSetPerfValue( cmScH_t h, unsigned locIdx, unsigned varId, double value )
  1769. {
  1770. cmSc_t* p = _cmScHandleToPtr(h);
  1771. int li = locIdx;
  1772. for(; li>=0; --li)
  1773. if( p->loc[li].begSectPtr != NULL )
  1774. {
  1775. assert( varId < kScVarCnt );
  1776. p->loc[li].begSectPtr->vars[varId] = value;
  1777. break;
  1778. }
  1779. assert( li>=0);
  1780. }
  1781. void cmScoreSetPerfDynLevel( cmScH_t h, unsigned evtIdx, unsigned dynLvl )
  1782. {
  1783. cmSc_t* p = _cmScHandleToPtr(h);
  1784. assert(evtIdx < p->cnt );
  1785. p->array[ evtIdx ].perfDynLvl = dynLvl;
  1786. }
  1787. cmScRC_t cmScoreDecode( const void* msg, unsigned msgByteCnt, cmScMsg_t* m)
  1788. {
  1789. cmScMsg_t* mp = (cmScMsg_t*)msg;
  1790. *m = *mp;
  1791. return kOkScRC;
  1792. }
  1793. void cmScorePrint( cmScH_t h, cmRpt_t* rpt )
  1794. {
  1795. cmSc_t* p = _cmScHandleToPtr(h);
  1796. unsigned i;
  1797. for(i=0; i<p->cnt; ++i)
  1798. {
  1799. cmScoreEvt_t* r = p->array + i;
  1800. switch(r->type)
  1801. {
  1802. case kBarEvtScId:
  1803. cmRptPrintf(rpt,"%5i %5i %3i bar\n",
  1804. i,
  1805. r->line,
  1806. r->barNumb );
  1807. break;
  1808. case kNonEvtScId:
  1809. cmRptPrintf(rpt,"%5i %5i %3i %3i %s %5s %c%c%c %s\n",
  1810. i,
  1811. r->line,
  1812. r->barNumb,
  1813. r->barNoteIdx,
  1814. cmScEvtTypeIdToLabel(r->type),
  1815. cmMidiToSciPitch(r->pitch,NULL,0),
  1816. cmIsFlag(r->flags,kEvenScFl) ? 'e' : ' ',
  1817. cmIsFlag(r->flags,kTempoScFl) ? 't' : ' ',
  1818. cmIsFlag(r->flags,kDynScFl) ? 'd' : ' ',
  1819. cmIsFlag(r->flags,kDynScFl) ? cmScDynIdToLabel(r->dynVal) : "");
  1820. break;
  1821. default:
  1822. break;
  1823. }
  1824. }
  1825. }
  1826. cmScRC_t cmScoreFileFromMidi( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* scoreFn )
  1827. {
  1828. cmScRC_t rc = kOkScRC;
  1829. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  1830. cmCsvH_t csvH = cmCsvNullHandle;
  1831. cmErr_t err;
  1832. cmChar_t* titles[] = {"id","trk","evt","opcode","dticks","micros","status","meta","ch","d0","d1","arg0","arg1","bar","skip","even","grace","tempo","t frac","dyn","section","remark", NULL };
  1833. cmErrSetup(&err,&ctx->rpt,"MIDI to Score");
  1834. if( cmMidiFileOpen(midiFn, &mfH, ctx ) != kOkMfRC )
  1835. return cmErrMsg(&err,kMidiFileFailScRC,"Unable to open the MIDI file '%s'.",midiFn);
  1836. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  1837. {
  1838. cmErrMsg(&err,kCsvFailScRC,"Unable to initialize the CSV file: '%s'.",scoreFn);
  1839. goto errLabel;
  1840. }
  1841. // Convert the track message 'dtick' field to delta-microseconds.
  1842. cmMidiFileTickToMicros(mfH);
  1843. unsigned msgCnt = cmMidiFileMsgCount(mfH);
  1844. unsigned i;
  1845. const cmMidiTrackMsg_t** tmpp = cmMidiFileMsgArray(mfH);
  1846. double acc_secs = 0;
  1847. unsigned lexTId = 0;
  1848. cmCsvCell_t* cp = NULL;
  1849. if( cmCsvAppendRow(csvH, &cp, cmCsvInsertSymText(csvH,titles[0]), kStrCsvTFl, lexTId ) != kOkCsvRC )
  1850. {
  1851. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'id' column in '%s'.",cmStringNullGuard(scoreFn));
  1852. goto errLabel;
  1853. }
  1854. for(i=1; titles[i]!=NULL; ++i)
  1855. {
  1856. if( cmCsvInsertTextColAfter(csvH, cp, &cp, titles[i], lexTId ) != kOkCsvRC )
  1857. {
  1858. cmErrMsg(&err,kCsvFailScRC,"Error inserting column index '%i' label in '%s'.",i,cmStringNullGuard(scoreFn));
  1859. goto errLabel;
  1860. }
  1861. }
  1862. for(i=0; i<msgCnt; ++i)
  1863. {
  1864. const cmMidiTrackMsg_t* tmp = tmpp[i];
  1865. const cmChar_t* opStr = NULL;
  1866. unsigned midiCh = 0;
  1867. unsigned d0 = 0;
  1868. unsigned d1 = 0;
  1869. unsigned metaId = 0;
  1870. double dsecs = (double)tmp->dtick / 1000000.0;
  1871. acc_secs += dsecs;
  1872. if( tmp->status == kMetaStId )
  1873. {
  1874. opStr = cmMidiMetaStatusToLabel(tmp->metaId);
  1875. metaId = tmp->metaId;
  1876. switch( tmp->metaId )
  1877. {
  1878. case kTempoMdId:
  1879. d0 = tmp->u.iVal;
  1880. }
  1881. }
  1882. else
  1883. {
  1884. opStr = cmMidiStatusToLabel(tmp->status);
  1885. if( cmMidiIsChStatus( tmp->status ) )
  1886. {
  1887. midiCh = tmp->u.chMsgPtr->ch;
  1888. d0 = tmp->u.chMsgPtr->d0;
  1889. d1 = tmp->u.chMsgPtr->d1;
  1890. }
  1891. }
  1892. cp = NULL;
  1893. // skip note-off messages
  1894. if( tmp->status == kNoteOffMdId )
  1895. continue;
  1896. if( cmCsvAppendRow(csvH, &cp, cmCsvInsertSymUInt(csvH,i), kIntCsvTFl, lexTId ) != kOkCsvRC )
  1897. {
  1898. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'id' column in '%s'.",cmStringNullGuard(scoreFn));
  1899. goto errLabel;
  1900. }
  1901. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->trkIdx, lexTId ) != kOkCsvRC )
  1902. {
  1903. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'trk' column in '%s'.",cmStringNullGuard(scoreFn));
  1904. goto errLabel;
  1905. }
  1906. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, 0, lexTId ) != kOkCsvRC )
  1907. {
  1908. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'evt' column in '%s'.",cmStringNullGuard(scoreFn));
  1909. goto errLabel;
  1910. }
  1911. if( cmCsvInsertTextColAfter(csvH, cp, &cp, opStr, lexTId ) != kOkCsvRC )
  1912. {
  1913. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'opcode' column in '%s'.",cmStringNullGuard(scoreFn));
  1914. goto errLabel;
  1915. }
  1916. if( cmCsvInsertDoubleColAfter(csvH, cp, &cp, dsecs, lexTId ) != kOkCsvRC )
  1917. {
  1918. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'dticks' column in '%s'.",cmStringNullGuard(scoreFn));
  1919. goto errLabel;
  1920. }
  1921. if( cmCsvInsertDoubleColAfter(csvH, cp, &cp, acc_secs, lexTId ) != kOkCsvRC )
  1922. {
  1923. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'micros' column in '%s'.",cmStringNullGuard(scoreFn));
  1924. goto errLabel;
  1925. }
  1926. if( cmCsvInsertHexColAfter(csvH, cp, &cp, tmp->status, lexTId ) != kOkCsvRC )
  1927. {
  1928. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'status' column in '%s'.",cmStringNullGuard(scoreFn));
  1929. goto errLabel;
  1930. }
  1931. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, metaId, lexTId ) != kOkCsvRC )
  1932. {
  1933. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'meta' column in '%s'.",cmStringNullGuard(scoreFn));
  1934. goto errLabel;
  1935. }
  1936. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, midiCh, lexTId ) != kOkCsvRC )
  1937. {
  1938. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'ch' column in '%s'.",cmStringNullGuard(scoreFn));
  1939. goto errLabel;
  1940. }
  1941. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, d0, lexTId ) != kOkCsvRC )
  1942. {
  1943. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'd0' column in '%s'.",cmStringNullGuard(scoreFn));
  1944. goto errLabel;
  1945. }
  1946. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, d1, lexTId ) != kOkCsvRC )
  1947. {
  1948. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'd1' column in '%s'.",cmStringNullGuard(scoreFn));
  1949. goto errLabel;
  1950. }
  1951. switch( tmp->status )
  1952. {
  1953. case kNoteOnMdId:
  1954. if( cmCsvInsertTextColAfter(csvH, cp, &cp, cmMidiToSciPitch(tmp->u.chMsgPtr->d0,NULL,0), lexTId ) != kOkCsvRC )
  1955. {
  1956. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'opcode' column in '%s'.",cmStringNullGuard(scoreFn));
  1957. goto errLabel;
  1958. }
  1959. case kMetaStId:
  1960. switch( tmp->metaId )
  1961. {
  1962. case kTimeSigMdId:
  1963. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->u.timeSigPtr->num, lexTId ) != kOkCsvRC )
  1964. {
  1965. cmErrMsg(&err,kCsvFailScRC,"Error inserting time sign. numerator column in '%s'.",cmStringNullGuard(scoreFn));
  1966. goto errLabel;
  1967. }
  1968. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->u.timeSigPtr->den, lexTId ) != kOkCsvRC )
  1969. {
  1970. cmErrMsg(&err,kCsvFailScRC,"Error inserting time sign. denominator column in '%s'.",cmStringNullGuard(scoreFn));
  1971. goto errLabel;
  1972. }
  1973. break;
  1974. case kTempoMdId:
  1975. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, 60000000/tmp->u.iVal, lexTId ) != kOkCsvRC )
  1976. {
  1977. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'tempo' in '%s'.",cmStringNullGuard(scoreFn));
  1978. goto errLabel;
  1979. }
  1980. break;
  1981. }
  1982. }
  1983. }
  1984. if( cmCsvWrite(csvH,scoreFn) != kOkCsvRC )
  1985. {
  1986. cmErrMsg(&err,kCsvFailScRC,"The score output file '%s' could not be written.",cmStringNullGuard(scoreFn));
  1987. goto errLabel;
  1988. }
  1989. errLabel:
  1990. cmMidiFileClose(&mfH);
  1991. cmCsvFinalize(&csvH);
  1992. return rc;
  1993. }
  1994. void cmScoreTest( cmCtx_t* ctx, const cmChar_t* fn )
  1995. {
  1996. cmScH_t h = cmScNullHandle;
  1997. if( cmScoreInitialize(ctx,&h,fn,0,NULL,0,NULL,NULL, cmSymTblNullHandle ) != kOkScRC )
  1998. return;
  1999. cmScorePrint(h,&ctx->rpt);
  2000. cmScoreFinalize(&h);
  2001. }
  2002. // 1. Fix absolute message time which was incorrect on original score file.
  2003. // 2.
  2004. void cmScoreFix( cmCtx_t* ctx )
  2005. {
  2006. const cmChar_t* mfn = "/home/kevin/src/cmgv/src/gv/data/ImaginaryThemes.mid";
  2007. const cmChar_t* crfn = "/home/kevin/src/cmgv/src/gv/data/mod0a.txt";
  2008. const cmChar_t* cwfn = "/home/kevin/src/cmgv/src/gv/data/mod1.csv";
  2009. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  2010. cmCsvH_t csvH = cmCsvNullHandle;
  2011. const cmMidiTrackMsg_t** msg = NULL;
  2012. double secs = 0.0;
  2013. int ci,mi,crn,mn;
  2014. bool errFl = true;
  2015. unsigned handCnt = 0;
  2016. unsigned midiMissCnt = 0;
  2017. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  2018. goto errLabel;
  2019. if( cmCsvLexRegisterMatcher(csvH, cmCsvLexNextAvailId(csvH), _cmScLexSciPitchMatcher ) != kOkCsvRC )
  2020. goto errLabel;
  2021. if( cmCsvParseFile(csvH, crfn, 0 ) != kOkCsvRC )
  2022. goto errLabel;
  2023. if( cmMidiFileOpen(mfn,&mfH,ctx) != kOkMfRC )
  2024. goto errLabel;
  2025. cmMidiFileTickToMicros(mfH);
  2026. cmMidiFileCalcNoteDurations(mfH);
  2027. mn = cmMidiFileMsgCount(mfH);
  2028. msg = cmMidiFileMsgArray(mfH);
  2029. crn = cmCsvRowCount(csvH);
  2030. // for each row in the score file
  2031. for(ci=1,mi=0; ci<crn && cmCsvLastRC(csvH)==kOkCsvRC; ++ci)
  2032. {
  2033. unsigned id;
  2034. // zero the duration column
  2035. if( cmCsvCellPtr(csvH, ci, kDSecsColScIdx ) != NULL )
  2036. cmCsvSetCellUInt( csvH, ci, kDSecsColScIdx, 0 );
  2037. // get the MIDI file event id for this row
  2038. if((id = cmCsvCellUInt(csvH,ci,kMidiFileIdColScIdx)) == UINT_MAX)
  2039. {
  2040. // this is a hand-entered event - so it has no event id
  2041. ++handCnt;
  2042. }
  2043. else
  2044. {
  2045. for(; mi<mn; ++mi)
  2046. {
  2047. const cmMidiTrackMsg_t* m = msg[mi];
  2048. assert( mi+1 <= id );
  2049. secs += m->dtick/1000000.0;
  2050. if( mi+1 != id )
  2051. {
  2052. if( m->status == kNoteOnMdId && m->u.chMsgPtr->d1>0 )
  2053. {
  2054. // this MIDI note-on does not have a corresponding score event
  2055. ++midiMissCnt;
  2056. }
  2057. }
  2058. else
  2059. {
  2060. cmCsvSetCellDouble( csvH, ci, kSecsColScIdx, secs );
  2061. ++mi;
  2062. if( m->status == kNoteOnMdId )
  2063. cmCsvSetCellDouble( csvH, ci, kDSecsColScIdx, m->u.chMsgPtr->durTicks/1000000.0 );
  2064. break;
  2065. }
  2066. }
  2067. if( mi==mn)
  2068. printf("done on row:%i\n",ci);
  2069. }
  2070. }
  2071. if( cmCsvLastRC(csvH) != kOkCsvRC )
  2072. goto errLabel;
  2073. if( cmCsvWrite(csvH,cwfn) != kOkCsvRC )
  2074. goto errLabel;
  2075. errFl = false;
  2076. errLabel:
  2077. if( errFl )
  2078. printf("Score fix failed.\n");
  2079. else
  2080. printf("Score fix done! hand:%i miss:%i\n",handCnt,midiMissCnt);
  2081. cmMidiFileClose(&mfH);
  2082. cmCsvFinalize(&csvH);
  2083. }