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

cmScore.c 70KB

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