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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712
  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. void cmScoreClearPerfInfo( cmScH_t h )
  1520. {
  1521. cmSc_t* p = _cmScHandleToPtr(h);
  1522. unsigned i;
  1523. for(i=0; i<p->cnt; ++i)
  1524. {
  1525. p->array[i].perfSmpIdx = cmInvalidIdx;
  1526. p->array[i].perfVel = 0;
  1527. p->array[i].perfDynLvl = 0;
  1528. }
  1529. for(i=0; i<p->locCnt; ++i)
  1530. {
  1531. cmScoreSet_t* sp = p->loc[i].setList;
  1532. for(; sp!=NULL; sp=sp->llink)
  1533. sp->doneFl = false;
  1534. }
  1535. for(i=0; i<p->sectCnt; ++i)
  1536. {
  1537. unsigned j;
  1538. for(j=0; j<kScVarCnt; ++j)
  1539. p->sect[i].vars[j] = DBL_MAX;
  1540. }
  1541. p->nxtLocIdx = 0;
  1542. p->minSetLocIdx = cmInvalidIdx;
  1543. p->maxSetLocIdx = cmInvalidIdx;
  1544. }
  1545. bool _cmScIsSetPerfDone( cmScoreSet_t* sp )
  1546. {
  1547. unsigned i = 0;
  1548. for(i=0; i<sp->eleCnt; ++i)
  1549. if( sp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1550. return false;
  1551. return true;
  1552. }
  1553. void _cmScPerfSortTimes( unsigned *v, unsigned n )
  1554. {
  1555. unsigned i;
  1556. bool fl = true;
  1557. while(fl && n)
  1558. {
  1559. fl = false;
  1560. for(i=1; i<n; ++i)
  1561. {
  1562. if( v[i-1] > v[i] )
  1563. {
  1564. unsigned t = v[i-1];
  1565. v[i-1] = v[i];
  1566. v[i] = t;
  1567. fl = true;
  1568. }
  1569. }
  1570. --n;
  1571. }
  1572. }
  1573. bool _cmScPerfEven(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1574. {
  1575. unsigned i = 0;
  1576. double u = 0;
  1577. double x = 0;
  1578. bool sortFl = false;
  1579. bool printFl = true;
  1580. unsigned v[ stp->eleCnt ];
  1581. unsigned d[ stp->eleCnt - 1];
  1582. assert( stp->eleCnt > 1 );
  1583. // calculate the sum of the time between events
  1584. for(i=0; i<stp->eleCnt; ++i)
  1585. {
  1586. // if this event was not received - then the set is not valid
  1587. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1588. {
  1589. if( printFl && printMissFl)
  1590. printf("EVENESS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1591. return false;
  1592. }
  1593. // load v[i]
  1594. v[i] = stp->eleArray[i]->perfSmpIdx;
  1595. // check for out of order elements
  1596. if( i> 0 )
  1597. if( v[i] < v[i-1] )
  1598. sortFl = true;
  1599. }
  1600. // sort the times in ascending order
  1601. if( sortFl )
  1602. _cmScPerfSortTimes(v,stp->eleCnt);
  1603. // calc the sum of time differences
  1604. for(i=1; i<stp->eleCnt; ++i)
  1605. u += d[i-1] = v[i] - v[i-1];
  1606. // calculate the average time between events
  1607. u /= stp->eleCnt-1;
  1608. // calculate the std-dev of the time between events
  1609. for(i=0; i<stp->eleCnt-1; ++i)
  1610. x += (d[i]-u)*(d[i]-u);
  1611. double sd = sqrt(x/(stp->eleCnt-1));
  1612. // compute the average z-score
  1613. double c = 0;
  1614. for(i=0; i<stp->eleCnt-1; ++i)
  1615. c += fabs(d[i]-u)/sd;
  1616. stp->value = c/(stp->eleCnt-1);
  1617. stp->doneFl = true;
  1618. if(printFl)
  1619. {
  1620. /*
  1621. for(i=0; i<stp->eleCnt; ++i)
  1622. {
  1623. printf("%i %i ",i,v[i]);
  1624. if( i > 0 )
  1625. printf("%i ", d[i-1]);
  1626. printf("\n");
  1627. }
  1628. */
  1629. printf("%s EVENESS:%f\n",sortFl?"SORTED ":"",stp->value);
  1630. }
  1631. return true;
  1632. }
  1633. bool _cmScPerfDyn( cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1634. {
  1635. double a = 0;
  1636. unsigned i = 0;
  1637. bool printFl = true;
  1638. for(i=0; i<stp->eleCnt; ++i)
  1639. {
  1640. unsigned j;
  1641. // if this event was not received - then the set is not valid
  1642. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1643. {
  1644. if( printFl && printMissFl )
  1645. printf("DYNAMICS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1646. return false;
  1647. }
  1648. unsigned m = 0; // lower bound for the first dyn. category
  1649. // determine the dynamic category for the performed velocity of each event
  1650. for(j=0; j<p->dynRefCnt; ++j)
  1651. {
  1652. // if the vel fall's into the jth dyn. category
  1653. if( m <= stp->eleArray[i]->perfVel && stp->eleArray[i]->perfVel < p->dynRefArray[j] )
  1654. break;
  1655. // store the min vel for the next dyn category
  1656. m = p->dynRefArray[j];
  1657. }
  1658. assert( j < p->dynRefCnt );
  1659. stp->eleArray[i]->perfDynLvl = j+1;
  1660. a += abs((j+1) - stp->eleArray[i]->dynVal);
  1661. if( p->cbFunc != NULL )
  1662. {
  1663. cmScMsg_t m;
  1664. m.typeId = kDynMsgScId;
  1665. m.u.dyn.evtIdx = stp->eleArray[i]->index;
  1666. m.u.dyn.dynLvl = stp->eleArray[i]->perfDynLvl;
  1667. p->cbFunc(p->cbArg,&m,sizeof(m));
  1668. }
  1669. }
  1670. stp->value = a / stp->eleCnt;
  1671. stp->doneFl = true;
  1672. if( printFl )
  1673. printf("DYNAMICS:%f\n",stp->value);
  1674. return true;
  1675. }
  1676. bool _cmScPerfTempo1(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1677. {
  1678. bool printFl = true;
  1679. unsigned durSmpCnt = 0;
  1680. double durBeats = 0;
  1681. int i;
  1682. for(i=0; i<stp->eleCnt; ++i)
  1683. {
  1684. // if this event was not received - then the set is not valid
  1685. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1686. {
  1687. if( printFl && printMissFl )
  1688. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1689. return false;
  1690. }
  1691. if( i > 0 )
  1692. durSmpCnt += stp->eleArray[i]->perfSmpIdx - stp->eleArray[i-1]->perfSmpIdx;
  1693. durBeats += stp->eleArray[i]->frac;
  1694. }
  1695. stp->value = durBeats / (durSmpCnt / p->srate*60.0 );
  1696. stp->doneFl = true;
  1697. if( printFl )
  1698. printf("TEMPO:%f\n",stp->value);
  1699. return true;
  1700. }
  1701. bool _cmScPerfTempo(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1702. {
  1703. bool printFl = true;
  1704. unsigned durSmpCnt = 0;
  1705. double durBeats = 0;
  1706. int i;
  1707. unsigned bi = cmInvalidIdx;
  1708. unsigned ei = cmInvalidIdx;
  1709. unsigned missCnt = 0;
  1710. for(i=0; i<stp->eleCnt; ++i)
  1711. {
  1712. // if this event was not received - then the set is not valid
  1713. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1714. {
  1715. ++missCnt;
  1716. if( printFl && printMissFl )
  1717. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1718. }
  1719. else
  1720. {
  1721. if( bi == cmInvalidIdx )
  1722. bi = i;
  1723. ei = i;
  1724. }
  1725. }
  1726. if( ei > bi )
  1727. {
  1728. for(i=bi; i<=ei; ++i)
  1729. durBeats += stp->eleArray[i]->frac;
  1730. durSmpCnt = stp->eleArray[ei]->perfSmpIdx - stp->eleArray[bi]->perfSmpIdx;
  1731. stp->value = durBeats / (durSmpCnt / (p->srate*60.0) );
  1732. stp->doneFl = true;
  1733. }
  1734. if( printFl )
  1735. printf("TEMPO:%f bi:%i ei:%i secs:%f bts:%f\n",stp->value,bi,ei,durSmpCnt/p->srate,durBeats);
  1736. return true;
  1737. }
  1738. void _cmScPerfExec( cmSc_t* p, cmScoreSet_t* sp, bool printMissFl )
  1739. {
  1740. if( sp->doneFl == false )
  1741. {
  1742. switch( sp->varId )
  1743. {
  1744. case kEvenVarScId:
  1745. _cmScPerfEven(p,sp,printMissFl);
  1746. break;
  1747. case kDynVarScId:
  1748. _cmScPerfDyn(p,sp,printMissFl);
  1749. break;
  1750. case kTempoVarScId:
  1751. _cmScPerfTempo(p,sp,printMissFl);
  1752. break;
  1753. default:
  1754. { assert(0); }
  1755. }
  1756. }
  1757. }
  1758. void _cmScPerfExecRange( cmSc_t* p )
  1759. {
  1760. if( p->minSetLocIdx == cmInvalidIdx || p->maxSetLocIdx==cmInvalidIdx )
  1761. return;
  1762. unsigned i = p->minSetLocIdx;
  1763. for(; i<=p->maxSetLocIdx; ++i)
  1764. {
  1765. cmScoreSet_t* sp = p->loc[i].setList;
  1766. for(; sp!=NULL; sp=sp->llink)
  1767. _cmScPerfExec(p,sp,true);
  1768. }
  1769. }
  1770. bool _cmScSetPerfEvent( cmSc_t* p, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1771. {
  1772. assert(locIdx < p->locCnt );
  1773. cmScoreLoc_t* lp = p->loc + locIdx;
  1774. bool doneFl = true;
  1775. unsigned i;
  1776. #ifndef NDEBUG
  1777. bool foundFl = false;
  1778. #endif
  1779. // locate the event at the loc[locIdx]
  1780. for(i=0; i<lp->evtCnt; ++i)
  1781. {
  1782. cmScoreEvt_t* ep = lp->evtArray[i];
  1783. if( ep->type == kNonEvtScId )
  1784. {
  1785. if( ep->pitch == pitch )
  1786. {
  1787. assert( ep->perfSmpIdx == cmInvalidIdx );
  1788. ep->perfSmpIdx = smpIdx;
  1789. ep->perfVel = vel;
  1790. #ifndef NDEBUG
  1791. foundFl = true;
  1792. #endif
  1793. }
  1794. // check if all notes have arrived for this location
  1795. if( ep->perfSmpIdx == cmInvalidIdx )
  1796. doneFl = false;
  1797. }
  1798. }
  1799. // the event must always be found
  1800. assert( foundFl );
  1801. return doneFl;
  1802. }
  1803. bool cmScoreSetPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1804. {
  1805. cmSc_t* p = _cmScHandleToPtr(h);
  1806. return _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1807. }
  1808. void cmScoreExecPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1809. {
  1810. unsigned i;
  1811. cmSc_t* p = _cmScHandleToPtr(h);
  1812. bool doneFl = _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1813. unsigned printLvl = 1;
  1814. cmScoreLoc_t* lp = p->loc + locIdx;
  1815. // all events for a location must be complete to trigger attached events
  1816. if( doneFl == false )
  1817. return;
  1818. if( p->loc[locIdx].setList != NULL )
  1819. {
  1820. // set idx of most recent loc w/ a set end event
  1821. p->maxSetLocIdx = locIdx;
  1822. if( p->minSetLocIdx == cmInvalidIdx )
  1823. p->minSetLocIdx = locIdx;
  1824. }
  1825. // attempt to calculate all sets between loc[p->minSetLocIdx] and loc[p->maxSetLocIdx]
  1826. _cmScPerfExecRange(p);
  1827. // prevent event retriggering or going backwards
  1828. if( printLvl && locIdx < p->nxtLocIdx )
  1829. {
  1830. printf("----- BACK ----- \n");
  1831. return;
  1832. }
  1833. if( printLvl && locIdx > p->nxtLocIdx )
  1834. {
  1835. printf("----- SKIP ----- \n");
  1836. }
  1837. // for each location between the current and previous location
  1838. for(; p->nxtLocIdx<=locIdx; ++p->nxtLocIdx)
  1839. {
  1840. lp = p->loc + p->nxtLocIdx;
  1841. // if this location is the start of a new section - then apply
  1842. // sets that are assigned to this section
  1843. if( lp->begSectPtr != NULL && lp->begSectPtr->setCnt > 0 )
  1844. {
  1845. // notice the location of the oldest section start - once we cross this point
  1846. // it is too late to notice set completions - so incr p->inSetLocIdx
  1847. if( lp->begSectPtr->setCnt )
  1848. p->minSetLocIdx = p->nxtLocIdx+1;
  1849. for(i=0; i<lp->begSectPtr->setCnt; ++i)
  1850. {
  1851. cmScoreSet_t* stp = lp->begSectPtr->setArray[i];
  1852. // temporarily commented out for testing purposes
  1853. // if( stp->doneFl == false )
  1854. // _cmScPerfExec(p, stp, printLvl>0 );
  1855. if( stp->doneFl )
  1856. {
  1857. assert( stp->varId < kScVarCnt );
  1858. lp->begSectPtr->vars[ stp->varId ] = stp->value;
  1859. if( p->cbFunc != NULL )
  1860. {
  1861. cmScMsg_t m;
  1862. m.typeId = kVarMsgScId;
  1863. m.u.meas.varId = stp->varId;
  1864. m.u.meas.value = stp->value;
  1865. p->cbFunc(p->cbArg,&m,sizeof(m));
  1866. }
  1867. }
  1868. }
  1869. }
  1870. }
  1871. }
  1872. void cmScoreSetPerfValue( cmScH_t h, unsigned locIdx, unsigned varId, double value )
  1873. {
  1874. cmSc_t* p = _cmScHandleToPtr(h);
  1875. int li = locIdx;
  1876. for(; li>=0; --li)
  1877. if( p->loc[li].begSectPtr != NULL )
  1878. {
  1879. assert( varId < kScVarCnt );
  1880. p->loc[li].begSectPtr->vars[varId] = value;
  1881. break;
  1882. }
  1883. assert( li>=0);
  1884. }
  1885. void cmScoreSetPerfDynLevel( cmScH_t h, unsigned evtIdx, unsigned dynLvl )
  1886. {
  1887. cmSc_t* p = _cmScHandleToPtr(h);
  1888. assert(evtIdx < p->cnt );
  1889. p->array[ evtIdx ].perfDynLvl = dynLvl;
  1890. }
  1891. cmScRC_t cmScoreDecode( const void* msg, unsigned msgByteCnt, cmScMsg_t* m)
  1892. {
  1893. cmScMsg_t* mp = (cmScMsg_t*)msg;
  1894. *m = *mp;
  1895. return kOkScRC;
  1896. }
  1897. void cmScorePrint( cmScH_t h, cmRpt_t* rpt )
  1898. {
  1899. cmSc_t* p = _cmScHandleToPtr(h);
  1900. unsigned i;
  1901. for(i=0; i<p->cnt; ++i)
  1902. {
  1903. cmScoreEvt_t* r = p->array + i;
  1904. switch(r->type)
  1905. {
  1906. case kBarEvtScId:
  1907. cmRptPrintf(rpt,"%5i %5i %3i bar\n",
  1908. i,
  1909. r->line,
  1910. r->barNumb );
  1911. break;
  1912. case kNonEvtScId:
  1913. cmRptPrintf(rpt,"%5i %5i %3i %3i %s %5s %c%c%c %s\n",
  1914. i,
  1915. r->line,
  1916. r->barNumb,
  1917. r->barNoteIdx,
  1918. cmScEvtTypeIdToLabel(r->type),
  1919. cmMidiToSciPitch(r->pitch,NULL,0),
  1920. cmIsFlag(r->flags,kEvenScFl) ? 'e' : ' ',
  1921. cmIsFlag(r->flags,kTempoScFl) ? 't' : ' ',
  1922. cmIsFlag(r->flags,kDynScFl) ? 'd' : ' ',
  1923. cmIsFlag(r->flags,kDynScFl) ? cmScDynIdToLabel(r->dynVal) : "");
  1924. break;
  1925. default:
  1926. break;
  1927. }
  1928. }
  1929. }
  1930. cmScRC_t cmScoreFileFromMidi( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* scoreFn )
  1931. {
  1932. cmScRC_t rc = kOkScRC;
  1933. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  1934. cmCsvH_t csvH = cmCsvNullHandle;
  1935. cmErr_t err;
  1936. 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 };
  1937. cmErrSetup(&err,&ctx->rpt,"MIDI to Score");
  1938. if( cmMidiFileOpen(midiFn, &mfH, ctx ) != kOkMfRC )
  1939. return cmErrMsg(&err,kMidiFileFailScRC,"Unable to open the MIDI file '%s'.",midiFn);
  1940. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  1941. {
  1942. cmErrMsg(&err,kCsvFailScRC,"Unable to initialize the CSV file: '%s'.",scoreFn);
  1943. goto errLabel;
  1944. }
  1945. // Convert the track message 'dtick' field to delta-microseconds.
  1946. cmMidiFileTickToMicros(mfH);
  1947. unsigned msgCnt = cmMidiFileMsgCount(mfH);
  1948. unsigned i;
  1949. const cmMidiTrackMsg_t** tmpp = cmMidiFileMsgArray(mfH);
  1950. double acc_secs = 0;
  1951. unsigned lexTId = 0;
  1952. cmCsvCell_t* cp = NULL;
  1953. if( cmCsvAppendRow(csvH, &cp, cmCsvInsertSymText(csvH,titles[0]), kStrCsvTFl, lexTId ) != kOkCsvRC )
  1954. {
  1955. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'id' column in '%s'.",cmStringNullGuard(scoreFn));
  1956. goto errLabel;
  1957. }
  1958. for(i=1; titles[i]!=NULL; ++i)
  1959. {
  1960. if( cmCsvInsertTextColAfter(csvH, cp, &cp, titles[i], lexTId ) != kOkCsvRC )
  1961. {
  1962. cmErrMsg(&err,kCsvFailScRC,"Error inserting column index '%i' label in '%s'.",i,cmStringNullGuard(scoreFn));
  1963. goto errLabel;
  1964. }
  1965. }
  1966. for(i=0; i<msgCnt; ++i)
  1967. {
  1968. const cmMidiTrackMsg_t* tmp = tmpp[i];
  1969. const cmChar_t* opStr = NULL;
  1970. unsigned midiCh = 0;
  1971. unsigned d0 = 0;
  1972. unsigned d1 = 0;
  1973. unsigned metaId = 0;
  1974. double dsecs = (double)tmp->dtick / 1000000.0;
  1975. acc_secs += dsecs;
  1976. if( tmp->status == kMetaStId )
  1977. {
  1978. opStr = cmMidiMetaStatusToLabel(tmp->metaId);
  1979. metaId = tmp->metaId;
  1980. switch( tmp->metaId )
  1981. {
  1982. case kTempoMdId:
  1983. d0 = tmp->u.iVal;
  1984. }
  1985. }
  1986. else
  1987. {
  1988. opStr = cmMidiStatusToLabel(tmp->status);
  1989. if( cmMidiIsChStatus( tmp->status ) )
  1990. {
  1991. midiCh = tmp->u.chMsgPtr->ch;
  1992. d0 = tmp->u.chMsgPtr->d0;
  1993. d1 = tmp->u.chMsgPtr->d1;
  1994. }
  1995. }
  1996. cp = NULL;
  1997. // skip note-off messages
  1998. if( tmp->status == kNoteOffMdId )
  1999. continue;
  2000. if( cmCsvAppendRow(csvH, &cp, cmCsvInsertSymUInt(csvH,i), kIntCsvTFl, lexTId ) != kOkCsvRC )
  2001. {
  2002. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'id' column in '%s'.",cmStringNullGuard(scoreFn));
  2003. goto errLabel;
  2004. }
  2005. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->trkIdx, lexTId ) != kOkCsvRC )
  2006. {
  2007. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'trk' column in '%s'.",cmStringNullGuard(scoreFn));
  2008. goto errLabel;
  2009. }
  2010. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, 0, lexTId ) != kOkCsvRC )
  2011. {
  2012. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'evt' column in '%s'.",cmStringNullGuard(scoreFn));
  2013. goto errLabel;
  2014. }
  2015. if( cmCsvInsertTextColAfter(csvH, cp, &cp, opStr, lexTId ) != kOkCsvRC )
  2016. {
  2017. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'opcode' column in '%s'.",cmStringNullGuard(scoreFn));
  2018. goto errLabel;
  2019. }
  2020. if( cmCsvInsertDoubleColAfter(csvH, cp, &cp, dsecs, lexTId ) != kOkCsvRC )
  2021. {
  2022. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'dticks' column in '%s'.",cmStringNullGuard(scoreFn));
  2023. goto errLabel;
  2024. }
  2025. if( cmCsvInsertDoubleColAfter(csvH, cp, &cp, acc_secs, lexTId ) != kOkCsvRC )
  2026. {
  2027. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'micros' column in '%s'.",cmStringNullGuard(scoreFn));
  2028. goto errLabel;
  2029. }
  2030. if( cmCsvInsertHexColAfter(csvH, cp, &cp, tmp->status, lexTId ) != kOkCsvRC )
  2031. {
  2032. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'status' column in '%s'.",cmStringNullGuard(scoreFn));
  2033. goto errLabel;
  2034. }
  2035. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, metaId, lexTId ) != kOkCsvRC )
  2036. {
  2037. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'meta' column in '%s'.",cmStringNullGuard(scoreFn));
  2038. goto errLabel;
  2039. }
  2040. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, midiCh, lexTId ) != kOkCsvRC )
  2041. {
  2042. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'ch' column in '%s'.",cmStringNullGuard(scoreFn));
  2043. goto errLabel;
  2044. }
  2045. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, d0, lexTId ) != kOkCsvRC )
  2046. {
  2047. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'd0' column in '%s'.",cmStringNullGuard(scoreFn));
  2048. goto errLabel;
  2049. }
  2050. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, d1, lexTId ) != kOkCsvRC )
  2051. {
  2052. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'd1' column in '%s'.",cmStringNullGuard(scoreFn));
  2053. goto errLabel;
  2054. }
  2055. switch( tmp->status )
  2056. {
  2057. case kNoteOnMdId:
  2058. if( cmCsvInsertTextColAfter(csvH, cp, &cp, cmMidiToSciPitch(tmp->u.chMsgPtr->d0,NULL,0), lexTId ) != kOkCsvRC )
  2059. {
  2060. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'opcode' column in '%s'.",cmStringNullGuard(scoreFn));
  2061. goto errLabel;
  2062. }
  2063. case kMetaStId:
  2064. switch( tmp->metaId )
  2065. {
  2066. case kTimeSigMdId:
  2067. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->u.timeSigPtr->num, lexTId ) != kOkCsvRC )
  2068. {
  2069. cmErrMsg(&err,kCsvFailScRC,"Error inserting time sign. numerator column in '%s'.",cmStringNullGuard(scoreFn));
  2070. goto errLabel;
  2071. }
  2072. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->u.timeSigPtr->den, lexTId ) != kOkCsvRC )
  2073. {
  2074. cmErrMsg(&err,kCsvFailScRC,"Error inserting time sign. denominator column in '%s'.",cmStringNullGuard(scoreFn));
  2075. goto errLabel;
  2076. }
  2077. break;
  2078. case kTempoMdId:
  2079. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, 60000000/tmp->u.iVal, lexTId ) != kOkCsvRC )
  2080. {
  2081. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'tempo' in '%s'.",cmStringNullGuard(scoreFn));
  2082. goto errLabel;
  2083. }
  2084. break;
  2085. }
  2086. }
  2087. }
  2088. if( cmCsvWrite(csvH,scoreFn) != kOkCsvRC )
  2089. {
  2090. cmErrMsg(&err,kCsvFailScRC,"The score output file '%s' could not be written.",cmStringNullGuard(scoreFn));
  2091. goto errLabel;
  2092. }
  2093. errLabel:
  2094. cmMidiFileClose(&mfH);
  2095. cmCsvFinalize(&csvH);
  2096. return rc;
  2097. }
  2098. void cmScoreTest( cmCtx_t* ctx, const cmChar_t* fn )
  2099. {
  2100. cmScH_t h = cmScNullHandle;
  2101. if( cmScoreInitialize(ctx,&h,fn,0,NULL,0,NULL,NULL, cmSymTblNullHandle ) != kOkScRC )
  2102. return;
  2103. cmScorePrint(h,&ctx->rpt);
  2104. cmScoreFinalize(&h);
  2105. }
  2106. // 1. Fix absolute message time which was incorrect on original score file.
  2107. // 2.
  2108. void cmScoreFix( cmCtx_t* ctx )
  2109. {
  2110. const cmChar_t* mfn = "/home/kevin/src/cmgv/src/gv/data/ImaginaryThemes.mid";
  2111. const cmChar_t* crfn = "/home/kevin/src/cmgv/src/gv/data/mod0a.txt";
  2112. const cmChar_t* cwfn = "/home/kevin/src/cmgv/src/gv/data/mod1.csv";
  2113. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  2114. cmCsvH_t csvH = cmCsvNullHandle;
  2115. const cmMidiTrackMsg_t** msg = NULL;
  2116. double secs = 0.0;
  2117. int ci,mi,crn,mn;
  2118. bool errFl = true;
  2119. unsigned handCnt = 0;
  2120. unsigned midiMissCnt = 0;
  2121. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  2122. goto errLabel;
  2123. if( cmCsvLexRegisterMatcher(csvH, cmCsvLexNextAvailId(csvH), _cmScLexSciPitchMatcher ) != kOkCsvRC )
  2124. goto errLabel;
  2125. if( cmCsvParseFile(csvH, crfn, 0 ) != kOkCsvRC )
  2126. goto errLabel;
  2127. if( cmMidiFileOpen(mfn,&mfH,ctx) != kOkMfRC )
  2128. goto errLabel;
  2129. cmMidiFileTickToMicros(mfH);
  2130. cmMidiFileCalcNoteDurations(mfH);
  2131. mn = cmMidiFileMsgCount(mfH);
  2132. msg = cmMidiFileMsgArray(mfH);
  2133. crn = cmCsvRowCount(csvH);
  2134. // for each row in the score file
  2135. for(ci=1,mi=0; ci<crn && cmCsvLastRC(csvH)==kOkCsvRC; ++ci)
  2136. {
  2137. unsigned id;
  2138. // zero the duration column
  2139. if( cmCsvCellPtr(csvH, ci, kDSecsColScIdx ) != NULL )
  2140. cmCsvSetCellUInt( csvH, ci, kDSecsColScIdx, 0 );
  2141. // get the MIDI file event id for this row
  2142. if((id = cmCsvCellUInt(csvH,ci,kMidiFileIdColScIdx)) == UINT_MAX)
  2143. {
  2144. // this is a hand-entered event - so it has no event id
  2145. ++handCnt;
  2146. }
  2147. else
  2148. {
  2149. for(; mi<mn; ++mi)
  2150. {
  2151. const cmMidiTrackMsg_t* m = msg[mi];
  2152. assert( mi+1 <= id );
  2153. secs += m->dtick/1000000.0;
  2154. if( mi+1 != id )
  2155. {
  2156. if( m->status == kNoteOnMdId && m->u.chMsgPtr->d1>0 )
  2157. {
  2158. // this MIDI note-on does not have a corresponding score event
  2159. ++midiMissCnt;
  2160. }
  2161. }
  2162. else
  2163. {
  2164. cmCsvSetCellDouble( csvH, ci, kSecsColScIdx, secs );
  2165. ++mi;
  2166. if( m->status == kNoteOnMdId )
  2167. cmCsvSetCellDouble( csvH, ci, kDSecsColScIdx, m->u.chMsgPtr->durTicks/1000000.0 );
  2168. break;
  2169. }
  2170. }
  2171. if( mi==mn)
  2172. printf("done on row:%i\n",ci);
  2173. }
  2174. }
  2175. if( cmCsvLastRC(csvH) != kOkCsvRC )
  2176. goto errLabel;
  2177. if( cmCsvWrite(csvH,cwfn) != kOkCsvRC )
  2178. goto errLabel;
  2179. errFl = false;
  2180. errLabel:
  2181. if( errFl )
  2182. printf("Score fix failed.\n");
  2183. else
  2184. printf("Score fix done! hand:%i miss:%i\n",handCnt,midiMissCnt);
  2185. cmMidiFileClose(&mfH);
  2186. cmCsvFinalize(&csvH);
  2187. }