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

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