libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmScore.c 65KB

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