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.

cmScore.c 49KB

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