libcm is a C development framework with an emphasis on audio signal processing applications.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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