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

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