libcm is a C development framework with an emphasis on audio signal processing applications.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cmScore.c 70KB

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