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

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