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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  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. // THIS PRINTF PREVENTS CRASH ON OSX ???????
  658. //printf("PROCSET:%i %c %s\n",p->sets[i].varId,_cmScVarIdToChar(p->sets[i].varId),ep->label);
  659. char cc = _cmScVarIdToChar(p->sets[i].varId);
  660. int nn = snprintf(NULL,0,"%c-%s",cc,ep->label);
  661. char b[nn+2];
  662. snprintf(b,nn,"%c-%s",cc,ep->label);
  663. p->sets[i].symArray[j] = cmSymTblRegisterSymbol(p->stH,b);
  664. snprintf(b,nn+1,"c%c-%s",cc,ep->label);
  665. p->sets[i].costSymArray[j] = cmSymTblRegisterSymbol(p->stH,b);
  666. //p->sets[i].symArray[j] = cmSymTblRegisterFmt(p->stH,"%c-%s", cc,ep->label);
  667. //p->sets[i].costSymArray[j] = cmSymTblRegisterFmt(p->stH,"c%c-%s",cc,ep->label);
  668. }
  669. else
  670. {
  671. p->sets[i].symArray[j] = cmInvalidId;
  672. p->sets[i].costSymArray[j] = cmInvalidId;
  673. }
  674. p->sets[i].sectArray[j] = sp;
  675. sp->setArray = cmMemResizeP(cmScoreSet_t*,sp->setArray,++sp->setCnt);
  676. sp->setArray[sp->setCnt-1] = p->sets + i;
  677. }
  678. }
  679. ++i;
  680. }
  681. assert(i==n);
  682. // assign each set to the location which contains it's last element.
  683. // (this is earliest point in the score location where all the
  684. // performance information contained in the set may be valid)
  685. for(i=0; i<p->setCnt; ++i)
  686. {
  687. assert( p->sets[i].eleCnt >= 1 );
  688. // get a ptr to the last element for the ith set
  689. const cmScoreEvt_t* ep = p->sets[i].eleArray[ p->sets[i].eleCnt-1 ];
  690. unsigned j,k;
  691. // find the location which contains the last element
  692. for(j=0; j<p->locCnt; ++j)
  693. {
  694. for(k=0; k<p->loc[j].evtCnt; ++k)
  695. if( p->loc[j].evtArray[k] == ep )
  696. break;
  697. if(k<p->loc[j].evtCnt)
  698. break;
  699. }
  700. assert( j<p->locCnt );
  701. // assign the ith set to the location which contains it's last element
  702. p->sets[i].llink = p->loc[j].setList;
  703. p->loc[j].setList = p->sets + i;
  704. }
  705. return rc;
  706. }
  707. cmScRC_t _cmScProcSections( cmSc_t* p, cmScSect_t* sectList )
  708. {
  709. cmScRC_t rc = kOkScRC;
  710. unsigned i;
  711. // count the sections
  712. cmScSect_t* sp = sectList;
  713. p->sectCnt = 0;
  714. for(; sp!=NULL; sp=sp->link)
  715. if( sp->label != NULL )
  716. ++p->sectCnt;
  717. // alloc a section array
  718. p->sect = cmMemAllocZ(cmScoreSection_t,p->sectCnt);
  719. // fill the section array
  720. sp = sectList;
  721. for(i=0; sp!=NULL; sp=sp->link)
  722. if( sp->label != NULL )
  723. {
  724. p->sect[i].label = sp->label;
  725. p->sect[i].index = i;
  726. p->sect[i].begEvtIndex = sp->startIdx;
  727. ++i;
  728. }
  729. // assign the begSectPtr to each section
  730. for(i=0; i<p->sectCnt; ++i)
  731. {
  732. assert( p->sect[i].begEvtIndex < p->cnt );
  733. unsigned j,k;
  734. const cmScoreEvt_t* ep = p->array + p->sect[i].begEvtIndex;
  735. for(j=0; j<p->locCnt; ++j)
  736. {
  737. for(k=0; k<p->loc[j].evtCnt; ++k)
  738. if( p->loc[j].evtArray[k] == ep )
  739. {
  740. p->loc[j].begSectPtr = p->sect + i;
  741. p->sect[i].locPtr = p->loc + j;
  742. break;
  743. }
  744. if( k<p->loc[j].evtCnt)
  745. break;
  746. }
  747. }
  748. // release the section linked list
  749. sp = sectList;
  750. cmScSect_t* np = NULL;
  751. while(sp!=NULL)
  752. {
  753. np = sp->link;
  754. cmMemFree(sp);
  755. sp = np;
  756. }
  757. //_cmScPrintSets("Sets",p->setList );
  758. _cmScProcSets(p);
  759. return rc;
  760. }
  761. cmScRC_t _cmScParseFile( cmSc_t* p, cmCtx_t* ctx, const cmChar_t* fn )
  762. {
  763. cmScRC_t rc = kOkScRC;
  764. unsigned barNoteIdx = 0;
  765. int barEvtIdx = cmInvalidIdx;
  766. int barNumb = 0;
  767. double secs;
  768. double cur_secs = 0;
  769. p->sectList = cmMemAllocZ(cmScSect_t,1); // section zero
  770. //_cmScNewSet(p); // preallocate the first set
  771. // initialize the CSV file parser
  772. if( cmCsvInitialize(&p->cH, ctx ) != kOkCsvRC )
  773. {
  774. rc = cmErrMsg(&p->err,kCsvFailScRC,"Score file initialization failed.");
  775. goto errLabel;
  776. }
  777. // register custom lex token matchers for sci-pitch and section id's
  778. if( cmCsvLexRegisterMatcher(p->cH, p->sciPitchLexTId = cmCsvLexNextAvailId(p->cH)+0, _cmScLexSciPitchMatcher ) != kOkCsvRC
  779. ||cmCsvLexRegisterMatcher(p->cH, p->sectionLexTId = cmCsvLexNextAvailId(p->cH)+1, _cmScLexSectionIdMatcher) != kOkCsvRC )
  780. {
  781. rc = cmErrMsg(&p->err,kCsvFailScRC,"CSV token matcher registration failed.");
  782. goto errLabel;
  783. }
  784. // parse the CSV file
  785. if( cmCsvParseFile(p->cH, fn, 0 ) != kOkCsvRC )
  786. {
  787. rc = cmErrMsg(&p->err,kCsvFailScRC,"CSV file parsing failed on the file '%s'.",cmStringNullGuard(fn));
  788. goto errLabel;
  789. }
  790. // allocate the score event array
  791. p->cnt = cmCsvRowCount(p->cH);
  792. p->array = cmMemAllocZ(cmScoreEvt_t,p->cnt);
  793. unsigned i,j;
  794. // skip labels line - start on line 1
  795. for(i=1,j=0; i<p->cnt && rc==kOkScRC; ++i)
  796. {
  797. // get the row 'type' label
  798. const char* typeLabel;
  799. if((typeLabel = cmCsvCellText(p->cH,i,kTypeLabelColScIdx)) == NULL )
  800. {
  801. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"No type label.");
  802. break;
  803. }
  804. // convert the row 'type' label to an id
  805. unsigned tid;
  806. if((tid = _cmScEvtTypeLabelToId(typeLabel)) == kInvalidEvtScId)
  807. {
  808. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"Unknown type '%s'.",cmStringNullGuard(typeLabel));
  809. break;
  810. }
  811. // set the score file line number
  812. p->array[j].line = i + 1;
  813. secs = DBL_MAX;
  814. switch(tid)
  815. {
  816. case kBarEvtScId: // parse bar lines
  817. if((rc = _cmScParseBar(p,i,p->array+j,&barNumb)) == kOkScRC )
  818. {
  819. barNoteIdx = 0;
  820. barEvtIdx = j;
  821. p->array[j].index = j;
  822. ++j;
  823. }
  824. break;
  825. case kNonEvtScId: // parse note-on events
  826. if((rc = _cmScParseNoteOn(p, i, p->array, j, barNumb, barNoteIdx )) == kOkScRC )
  827. {
  828. secs = p->array[j].secs;
  829. if( p->array[j].secs == DBL_MAX )
  830. p->array[j].secs = cur_secs;
  831. if( cmIsFlag(p->array[j].flags,kSkipScFl) == false )
  832. {
  833. p->array[j].index = j;
  834. ++j;
  835. }
  836. ++barNoteIdx;
  837. }
  838. break;
  839. default:
  840. // Returns DBL_MAX on error.
  841. secs = cmCsvCellDouble(p->cH, i, kSecsColScIdx );
  842. break;
  843. }
  844. if( secs != DBL_MAX )
  845. cur_secs = secs;
  846. // form the section list
  847. if( j > 0 )
  848. if((rc = _cmScParseSectionColumn(p,i,j-1,p->sectList)) != kOkScRC )
  849. break;
  850. // the bar lines don't have times so set the time of the bar line to the
  851. // time of the first event in the bar.
  852. if( barEvtIdx != cmInvalidIdx && secs != DBL_MAX )
  853. {
  854. assert( p->array[ barEvtIdx ].type == kBarEvtScId );
  855. p->array[ barEvtIdx ].secs = secs;
  856. // handle the case where the previous bar had no events
  857. // BUG BUG BUG this is a hack which will fail if the first bar does not have events.
  858. if( barEvtIdx>=1 && p->array[ barEvtIdx-1].type == kBarEvtScId )
  859. p->array[ barEvtIdx-1].secs = secs;
  860. barEvtIdx = cmInvalidIdx;
  861. }
  862. }
  863. if( rc == kSyntaxErrScRC )
  864. {
  865. cmErrMsg(&p->err,rc,"Syntax error on line %i in '%s'.",i+1,cmStringNullGuard(fn));
  866. goto errLabel;
  867. }
  868. p->cnt = j;
  869. errLabel:
  870. return rc;
  871. }
  872. cmScRC_t _cmScInitLocArray( cmSc_t* p )
  873. {
  874. cmScRC_t rc = kOkScRC;
  875. double maxDSecs = 0; // max time between events that are considered simultaneous
  876. unsigned barNumb = 0;
  877. int i,j,k;
  878. if( p->cnt==0)
  879. return rc;
  880. p->locCnt = 1;
  881. // count the number of unique time locations in the score
  882. p->locCnt = 1;
  883. for(i=1; i<p->cnt; ++i )
  884. {
  885. if( p->array[i].secs < p->array[i-1].secs )
  886. 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);
  887. if( (p->array[i].secs - p->array[i-1].secs) > maxDSecs )
  888. ++p->locCnt;
  889. }
  890. if( rc != kOkScRC )
  891. return rc;
  892. // allocate the loc. array
  893. p->loc = cmMemAllocZ(cmScoreLoc_t,p->locCnt);
  894. // fill in the location array
  895. for(i=0,k=0; i<p->cnt; ++k)
  896. {
  897. j = i+1;
  898. // get the count of events at this location
  899. while( j<p->cnt && p->array[j].secs - p->array[j-1].secs <= maxDSecs )
  900. ++j;
  901. assert(k<p->locCnt);
  902. p->loc[k].index = k;
  903. p->loc[k].evtCnt = j-i;
  904. p->loc[k].evtArray = cmMemAllocZ(cmScoreEvt_t*,p->loc[k].evtCnt);
  905. // fill in the location record event pointers
  906. for(j=0; j<p->loc[k].evtCnt; ++j)
  907. {
  908. p->loc[k].evtArray[j] = p->array + (i + j);
  909. p->loc[k].evtArray[j]->locIdx = k;
  910. if( p->array[i+j].type == kBarEvtScId )
  911. barNumb = p->array[i+j].barNumb;
  912. }
  913. // fill in the location record
  914. p->loc[k].secs = p->array[i].secs;
  915. p->loc[k].barNumb = barNumb;
  916. i += p->loc[k].evtCnt;
  917. }
  918. assert( p->locCnt == k );
  919. return rc;
  920. }
  921. 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 )
  922. {
  923. cmScRC_t rc = kOkScRC;
  924. if((rc = cmScoreFinalize(hp)) != kOkScRC )
  925. return rc;
  926. cmSc_t* p = cmMemAllocZ(cmSc_t,1);
  927. cmErrSetup(&p->err,&ctx->rpt,"Score");
  928. p->stH = stH;
  929. if((rc = _cmScParseFile(p,ctx,fn)) != kOkScRC )
  930. goto errLabel;
  931. if((rc = _cmScInitLocArray(p)) != kOkScRC )
  932. goto errLabel;
  933. if((rc = _cmScProcSections(p,p->sectList)) != kOkScRC )
  934. goto errLabel;
  935. // load the dynamic reference array
  936. if( dynRefArray != NULL && dynRefCnt > 0)
  937. {
  938. unsigned n = _cmScDynLabelCount();
  939. if( dynRefCnt != n )
  940. {
  941. rc = cmErrMsg(&p->err,kInvalidDynRefCntScRC,"The count of dynamics labels must be %i not %i.",n,dynRefCnt);
  942. goto errLabel;
  943. }
  944. p->dynRefArray = cmMemAllocZ(unsigned,dynRefCnt);
  945. memcpy(p->dynRefArray,dynRefArray,sizeof(unsigned)*dynRefCnt);
  946. p->dynRefCnt = dynRefCnt;
  947. }
  948. p->srate = srate;
  949. p->cbFunc = cbFunc;
  950. p->cbArg = cbArg;
  951. p->fn = cmMemAllocStr(fn);
  952. p->nxtLocIdx = 0;
  953. p->minSetLocIdx = cmInvalidIdx;
  954. p->maxSetLocIdx = cmInvalidIdx;
  955. hp->h = p;
  956. cmScoreClearPerfInfo(*hp);
  957. //cmScorePrintLoc(*hp);
  958. errLabel:
  959. if( rc != kOkScRC )
  960. _cmScFinalize(p);
  961. return rc;
  962. }
  963. cmScRC_t cmScoreFinalize( cmScH_t* hp )
  964. {
  965. cmScRC_t rc = kOkScRC;
  966. if( hp == NULL || cmScoreIsValid(*hp) == false )
  967. return kOkScRC;
  968. cmSc_t* p = _cmScHandleToPtr(*hp);
  969. if((rc = _cmScFinalize(p)) != kOkScRC )
  970. return rc;
  971. hp->h = NULL;
  972. return rc;
  973. }
  974. const cmChar_t* cmScoreFileName( cmScH_t h )
  975. {
  976. cmSc_t* p = _cmScHandleToPtr(h);
  977. return p->fn;
  978. }
  979. double cmScoreSampleRate( cmScH_t h )
  980. {
  981. cmSc_t* p = _cmScHandleToPtr(h);
  982. return p->srate;
  983. }
  984. bool cmScoreIsValid( cmScH_t h )
  985. { return h.h != NULL; }
  986. unsigned cmScoreEvtCount( cmScH_t h )
  987. {
  988. cmSc_t* p = _cmScHandleToPtr(h);
  989. return p->cnt;
  990. }
  991. cmScoreEvt_t* cmScoreEvt( cmScH_t h, unsigned idx )
  992. {
  993. cmSc_t* p = _cmScHandleToPtr(h);
  994. if( idx >= p->cnt )
  995. {
  996. cmErrMsg(&p->err,kInvalidIdxScRC,"%i is an invalid index for %i records.",idx,p->cnt);
  997. return NULL;
  998. }
  999. return p->array + idx;
  1000. }
  1001. cmScoreEvt_t* cmScoreBarEvt( cmScH_t h, unsigned barNumb )
  1002. {
  1003. cmSc_t* p = _cmScHandleToPtr(h);
  1004. unsigned i = 0;
  1005. for(; i<p->cnt; ++i)
  1006. if( p->array[i].type==kBarEvtScId && p->array[i].barNumb==barNumb )
  1007. return p->array + i;
  1008. return NULL;
  1009. }
  1010. unsigned cmScoreSectionCount( cmScH_t h )
  1011. {
  1012. cmSc_t* p = _cmScHandleToPtr(h);
  1013. return p->sectCnt;
  1014. }
  1015. cmScoreSection_t* cmScoreSection( cmScH_t h, unsigned idx )
  1016. {
  1017. cmSc_t* p = _cmScHandleToPtr(h);
  1018. assert( idx < p->sectCnt);
  1019. return p->sect + idx;
  1020. }
  1021. unsigned cmScoreLocCount( cmScH_t h )
  1022. {
  1023. cmSc_t* p = _cmScHandleToPtr(h);
  1024. return p->locCnt;
  1025. }
  1026. cmScoreLoc_t* cmScoreLoc( cmScH_t h, unsigned idx )
  1027. {
  1028. cmSc_t* p = _cmScHandleToPtr(h);
  1029. if( idx >= p->locCnt )
  1030. {
  1031. cmErrMsg(&p->err,kInvalidIdxScRC,"%i is an invalid index for %i location records.",idx,p->locCnt);
  1032. return NULL;
  1033. }
  1034. return p->loc + idx;
  1035. }
  1036. cmScoreLoc_t* cmScoreEvtLoc( cmScH_t h, const cmScoreEvt_t* evt )
  1037. {
  1038. cmSc_t* p = _cmScHandleToPtr(h);
  1039. unsigned i;
  1040. if( evt != NULL )
  1041. for(i=0; i<p->locCnt; ++i)
  1042. {
  1043. unsigned j;
  1044. for(j=0; j<p->loc[i].evtCnt; ++j)
  1045. if( p->loc[i].evtArray[j] == evt )
  1046. return p->loc + i;
  1047. }
  1048. return NULL;
  1049. }
  1050. void cmScorePrintLoc( cmScH_t h )
  1051. {
  1052. unsigned i = 0;
  1053. cmSc_t* p = _cmScHandleToPtr(h);
  1054. unsigned colCnt = 10;
  1055. int bufCharCnt = 4;
  1056. char buf[ bufCharCnt ];
  1057. const char* emptyStr = " ";
  1058. // for each set of 'colCnt' columns
  1059. for(i=0; i<p->locCnt; i+=colCnt )
  1060. {
  1061. // print the location 'index' line
  1062. unsigned c,j,k;
  1063. printf("index: ");
  1064. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1065. printf("%7i ",j);
  1066. printf("\n");
  1067. // print the 'sectn' label line
  1068. printf("sectn: ");
  1069. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1070. if( p->loc[j].begSectPtr==NULL )
  1071. printf("%s",emptyStr);
  1072. else
  1073. printf("%7s ",p->loc[j].begSectPtr->label);
  1074. printf("\n");
  1075. // calculate the max number of simultan. events at any one location
  1076. // for this set of 'colCnt' columns.
  1077. unsigned n=0;
  1078. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1079. if( p->loc[j].evtCnt > n )
  1080. n = p->loc[j].evtCnt;
  1081. // for each 'sco' line
  1082. for(k=0; k<n; ++k)
  1083. {
  1084. printf("sco%2i: ",k);
  1085. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1086. if( k >= p->loc[j].evtCnt )
  1087. printf("%s",emptyStr);
  1088. else
  1089. {
  1090. switch(p->loc[j].evtArray[k]->type)
  1091. {
  1092. case kBarEvtScId:
  1093. printf(" |%3i ",p->loc[j].evtArray[k]->barNumb);
  1094. break;
  1095. case kNonEvtScId:
  1096. {
  1097. int bn=16;
  1098. char b[bn];
  1099. strcpy(b,cmMidiToSciPitch(p->loc[j].evtArray[k]->pitch,NULL,0));
  1100. strcat(b,_cmScFlagsToStr( p->loc[j].evtArray[k]->flags,buf,bufCharCnt));
  1101. assert(strlen(b)<bn);
  1102. printf("%7s ",b);
  1103. break;
  1104. }
  1105. }
  1106. }
  1107. printf("\n");
  1108. }
  1109. // calc the max number of set triggers which will occur on
  1110. // any one location for this set of 'colCnt' columns.
  1111. n=0;
  1112. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1113. if(p->loc[j].begSectPtr != NULL && p->loc[j].begSectPtr->setCnt > n )
  1114. n = p->loc[j].begSectPtr->setCnt;
  1115. for(k=0; k<n; ++k)
  1116. {
  1117. printf("trig%1i: ",k);
  1118. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1119. {
  1120. if( p->loc[j].begSectPtr != NULL && k<p->loc[j].begSectPtr->setCnt )
  1121. {
  1122. const cmScoreSet_t* sp = p->loc[j].begSectPtr->setArray[k];
  1123. printf(" %3s-%c ",p->loc[j].begSectPtr->label,_cmScVarIdToChar(sp->varId) );
  1124. }
  1125. else
  1126. {
  1127. printf("%s",emptyStr);
  1128. }
  1129. }
  1130. printf("\n");
  1131. }
  1132. printf("\n");
  1133. }
  1134. }
  1135. unsigned cmScoreSetCount( cmScH_t h )
  1136. {
  1137. cmSc_t* p = _cmScHandleToPtr(h);
  1138. return p->setCnt;
  1139. }
  1140. cmScRC_t cmScoreSeqNotify( cmScH_t h )
  1141. {
  1142. cmScRC_t rc = kOkScRC;
  1143. cmSc_t* p = _cmScHandleToPtr(h);
  1144. cmScMsg_t m;
  1145. unsigned i;
  1146. if( p->cbFunc != NULL )
  1147. {
  1148. memset(&m.u.evt,0,sizeof(m.u.evt));
  1149. m.typeId = kBeginMsgScId;
  1150. p->cbFunc(p->cbArg,&m,sizeof(m));
  1151. m.typeId = kEventMsgScId;
  1152. for(i=0; i<p->cnt; ++i)
  1153. {
  1154. m.u.evt = p->array[i];
  1155. p->cbFunc(p->cbArg,&m,sizeof(m));
  1156. }
  1157. m.typeId = kSectionMsgScId;
  1158. for(i=0; i<p->sectCnt; ++i)
  1159. {
  1160. m.u.sect = p->sect[i];
  1161. p->cbFunc(p->cbArg,&m,sizeof(m));
  1162. }
  1163. memset(&m.u.evt,0,sizeof(m.u.evt));
  1164. m.typeId = kEndMsgScId;
  1165. p->cbFunc(p->cbArg,&m,sizeof(m));
  1166. }
  1167. return rc;
  1168. }
  1169. void cmScoreClearPerfInfo( cmScH_t h )
  1170. {
  1171. cmSc_t* p = _cmScHandleToPtr(h);
  1172. unsigned i;
  1173. for(i=0; i<p->cnt; ++i)
  1174. {
  1175. p->array[i].perfSmpIdx = cmInvalidIdx;
  1176. p->array[i].perfVel = 0;
  1177. p->array[i].perfDynLvl = 0;
  1178. }
  1179. for(i=0; i<p->locCnt; ++i)
  1180. {
  1181. cmScoreSet_t* sp = p->loc[i].setList;
  1182. for(; sp!=NULL; sp=sp->llink)
  1183. sp->doneFl = false;
  1184. }
  1185. for(i=0; i<p->sectCnt; ++i)
  1186. {
  1187. unsigned j;
  1188. for(j=0; j<kScVarCnt; ++j)
  1189. p->sect[i].vars[j] = DBL_MAX;
  1190. }
  1191. p->nxtLocIdx = 0;
  1192. p->minSetLocIdx = cmInvalidIdx;
  1193. p->maxSetLocIdx = cmInvalidIdx;
  1194. }
  1195. bool _cmScIsSetPerfDone( cmScoreSet_t* sp )
  1196. {
  1197. unsigned i = 0;
  1198. for(i=0; i<sp->eleCnt; ++i)
  1199. if( sp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1200. return false;
  1201. return true;
  1202. }
  1203. void _cmScPerfSortTimes( unsigned *v, unsigned n )
  1204. {
  1205. unsigned i;
  1206. bool fl = true;
  1207. while(fl && n)
  1208. {
  1209. fl = false;
  1210. for(i=1; i<n; ++i)
  1211. {
  1212. if( v[i-1] > v[i] )
  1213. {
  1214. unsigned t = v[i-1];
  1215. v[i-1] = v[i];
  1216. v[i] = t;
  1217. fl = true;
  1218. }
  1219. }
  1220. --n;
  1221. }
  1222. }
  1223. bool _cmScPerfEven(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1224. {
  1225. unsigned i = 0;
  1226. double u = 0;
  1227. double x = 0;
  1228. bool sortFl = false;
  1229. bool printFl = true;
  1230. unsigned v[ stp->eleCnt ];
  1231. unsigned d[ stp->eleCnt - 1];
  1232. assert( stp->eleCnt > 1 );
  1233. // calculate the sum of the time between events
  1234. for(i=0; i<stp->eleCnt; ++i)
  1235. {
  1236. // if this event was not received - then the set is not valid
  1237. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1238. {
  1239. if( printFl && printMissFl)
  1240. printf("EVENESS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1241. return false;
  1242. }
  1243. // load v[i]
  1244. v[i] = stp->eleArray[i]->perfSmpIdx;
  1245. // check for out of order elements
  1246. if( i> 0 )
  1247. if( v[i] < v[i-1] )
  1248. sortFl = true;
  1249. }
  1250. // sort the times in ascending order
  1251. if( sortFl )
  1252. _cmScPerfSortTimes(v,stp->eleCnt);
  1253. // calc the sum of time differences
  1254. for(i=1; i<stp->eleCnt; ++i)
  1255. u += d[i-1] = v[i] - v[i-1];
  1256. // calculate the average time between events
  1257. u /= stp->eleCnt-1;
  1258. // calculate the std-dev of the time between events
  1259. for(i=0; i<stp->eleCnt-1; ++i)
  1260. x += (d[i]-u)*(d[i]-u);
  1261. double sd = sqrt(x/(stp->eleCnt-1));
  1262. // compute the average z-score
  1263. double c = 0;
  1264. for(i=0; i<stp->eleCnt-1; ++i)
  1265. c += fabs(d[i]-u)/sd;
  1266. stp->value = c/(stp->eleCnt-1);
  1267. stp->doneFl = true;
  1268. if(printFl)
  1269. {
  1270. /*
  1271. for(i=0; i<stp->eleCnt; ++i)
  1272. {
  1273. printf("%i %i ",i,v[i]);
  1274. if( i > 0 )
  1275. printf("%i ", d[i-1]);
  1276. printf("\n");
  1277. }
  1278. */
  1279. printf("%s EVENESS:%f\n",sortFl?"SORTED ":"",stp->value);
  1280. }
  1281. return true;
  1282. }
  1283. bool _cmScPerfDyn( cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1284. {
  1285. double a = 0;
  1286. unsigned i = 0;
  1287. bool printFl = true;
  1288. for(i=0; i<stp->eleCnt; ++i)
  1289. {
  1290. unsigned j;
  1291. // if this event was not received - then the set is not valid
  1292. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1293. {
  1294. if( printFl && printMissFl )
  1295. printf("DYNAMICS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1296. return false;
  1297. }
  1298. unsigned m = 0; // lower bound for the first dyn. category
  1299. // determine the dynamic category for the performed velocity of each event
  1300. for(j=0; j<p->dynRefCnt; ++j)
  1301. {
  1302. // if the vel fall's into the jth dyn. category
  1303. if( m <= stp->eleArray[i]->perfVel && stp->eleArray[i]->perfVel < p->dynRefArray[j] )
  1304. break;
  1305. // store the min vel for the next dyn category
  1306. m = p->dynRefArray[j];
  1307. }
  1308. assert( j < p->dynRefCnt );
  1309. stp->eleArray[i]->perfDynLvl = j+1;
  1310. a += abs((j+1) - stp->eleArray[i]->dynVal);
  1311. if( p->cbFunc != NULL )
  1312. {
  1313. cmScMsg_t m;
  1314. m.typeId = kDynMsgScId;
  1315. m.u.dyn.evtIdx = stp->eleArray[i]->index;
  1316. m.u.dyn.dynLvl = stp->eleArray[i]->perfDynLvl;
  1317. p->cbFunc(p->cbArg,&m,sizeof(m));
  1318. }
  1319. }
  1320. stp->value = a / stp->eleCnt;
  1321. stp->doneFl = true;
  1322. if( printFl )
  1323. printf("DYNAMICS:%f\n",stp->value);
  1324. return true;
  1325. }
  1326. bool _cmScPerfTempo1(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1327. {
  1328. bool printFl = true;
  1329. unsigned durSmpCnt = 0;
  1330. double durBeats = 0;
  1331. int i;
  1332. for(i=0; i<stp->eleCnt; ++i)
  1333. {
  1334. // if this event was not received - then the set is not valid
  1335. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1336. {
  1337. if( printFl && printMissFl )
  1338. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1339. return false;
  1340. }
  1341. if( i > 0 )
  1342. durSmpCnt += stp->eleArray[i]->perfSmpIdx - stp->eleArray[i-1]->perfSmpIdx;
  1343. durBeats += stp->eleArray[i]->frac;
  1344. }
  1345. stp->value = durBeats / (durSmpCnt / p->srate*60.0 );
  1346. stp->doneFl = true;
  1347. if( printFl )
  1348. printf("TEMPO:%f\n",stp->value);
  1349. return true;
  1350. }
  1351. bool _cmScPerfTempo(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1352. {
  1353. bool printFl = true;
  1354. unsigned durSmpCnt = 0;
  1355. double durBeats = 0;
  1356. int i;
  1357. unsigned bi = cmInvalidIdx;
  1358. unsigned ei = cmInvalidIdx;
  1359. unsigned missCnt = 0;
  1360. for(i=0; i<stp->eleCnt; ++i)
  1361. {
  1362. // if this event was not received - then the set is not valid
  1363. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1364. {
  1365. ++missCnt;
  1366. if( printFl && printMissFl )
  1367. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1368. }
  1369. else
  1370. {
  1371. if( bi == cmInvalidIdx )
  1372. bi = i;
  1373. ei = i;
  1374. }
  1375. }
  1376. if( ei > bi )
  1377. {
  1378. for(i=bi; i<=ei; ++i)
  1379. durBeats += stp->eleArray[i]->frac;
  1380. durSmpCnt = stp->eleArray[ei]->perfSmpIdx - stp->eleArray[bi]->perfSmpIdx;
  1381. stp->value = durBeats / (durSmpCnt / (p->srate*60.0) );
  1382. stp->doneFl = true;
  1383. }
  1384. if( printFl )
  1385. printf("TEMPO:%f bi:%i ei:%i secs:%f bts:%f\n",stp->value,bi,ei,durSmpCnt/p->srate,durBeats);
  1386. return true;
  1387. }
  1388. void _cmScPerfExec( cmSc_t* p, cmScoreSet_t* sp, bool printMissFl )
  1389. {
  1390. if( sp->doneFl == false )
  1391. {
  1392. switch( sp->varId )
  1393. {
  1394. case kEvenVarScId:
  1395. _cmScPerfEven(p,sp,printMissFl);
  1396. break;
  1397. case kDynVarScId:
  1398. _cmScPerfDyn(p,sp,printMissFl);
  1399. break;
  1400. case kTempoVarScId:
  1401. _cmScPerfTempo(p,sp,printMissFl);
  1402. break;
  1403. default:
  1404. { assert(0); }
  1405. }
  1406. }
  1407. }
  1408. void _cmScPerfExecRange( cmSc_t* p )
  1409. {
  1410. if( p->minSetLocIdx == cmInvalidIdx || p->maxSetLocIdx==cmInvalidIdx )
  1411. return;
  1412. unsigned i = p->minSetLocIdx;
  1413. for(; i<=p->maxSetLocIdx; ++i)
  1414. {
  1415. cmScoreSet_t* sp = p->loc[i].setList;
  1416. for(; sp!=NULL; sp=sp->llink)
  1417. _cmScPerfExec(p,sp,true);
  1418. }
  1419. }
  1420. bool _cmScSetPerfEvent( cmSc_t* p, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1421. {
  1422. assert(locIdx < p->locCnt );
  1423. cmScoreLoc_t* lp = p->loc + locIdx;
  1424. bool doneFl = true;
  1425. unsigned i;
  1426. #ifndef NDEBUG
  1427. bool foundFl = false;
  1428. #endif
  1429. // locate the event at the loc[locIdx]
  1430. for(i=0; i<lp->evtCnt; ++i)
  1431. {
  1432. cmScoreEvt_t* ep = lp->evtArray[i];
  1433. if( ep->type == kNonEvtScId )
  1434. {
  1435. if( ep->pitch == pitch )
  1436. {
  1437. assert( ep->perfSmpIdx == cmInvalidIdx );
  1438. ep->perfSmpIdx = smpIdx;
  1439. ep->perfVel = vel;
  1440. #ifndef NDEBUG
  1441. foundFl = true;
  1442. #endif
  1443. }
  1444. // check if all notes have arrived for this location
  1445. if( ep->perfSmpIdx == cmInvalidIdx )
  1446. doneFl = false;
  1447. }
  1448. }
  1449. // the event must always be found
  1450. assert( foundFl );
  1451. return doneFl;
  1452. }
  1453. bool cmScoreSetPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1454. {
  1455. cmSc_t* p = _cmScHandleToPtr(h);
  1456. return _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1457. }
  1458. void cmScoreExecPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1459. {
  1460. unsigned i;
  1461. cmSc_t* p = _cmScHandleToPtr(h);
  1462. bool doneFl = _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1463. unsigned printLvl = 1;
  1464. cmScoreLoc_t* lp = p->loc + locIdx;
  1465. // all events for a location must be complete to trigger attached events
  1466. if( doneFl == false )
  1467. return;
  1468. if( p->loc[locIdx].setList != NULL )
  1469. {
  1470. // set idx of most recent loc w/ a set end event
  1471. p->maxSetLocIdx = locIdx;
  1472. if( p->minSetLocIdx == cmInvalidIdx )
  1473. p->minSetLocIdx = locIdx;
  1474. }
  1475. // attempt to calculate all sets between loc[p->minSetLocIdx] and loc[p->maxSetLocIdx]
  1476. _cmScPerfExecRange(p);
  1477. // prevent event retriggering or going backwards
  1478. if( printLvl && locIdx < p->nxtLocIdx )
  1479. {
  1480. printf("----- BACK ----- \n");
  1481. return;
  1482. }
  1483. if( printLvl && locIdx > p->nxtLocIdx )
  1484. {
  1485. printf("----- SKIP ----- \n");
  1486. }
  1487. // for each location between the current and previous location
  1488. for(; p->nxtLocIdx<=locIdx; ++p->nxtLocIdx)
  1489. {
  1490. lp = p->loc + p->nxtLocIdx;
  1491. // if this location is the start of a new section - then apply
  1492. // sets that are assigned to this section
  1493. if( lp->begSectPtr != NULL && lp->begSectPtr->setCnt > 0 )
  1494. {
  1495. // notice the location of the oldest section start - once we cross this point
  1496. // it is too late to notice set completions - so incr p->inSetLocIdx
  1497. if( lp->begSectPtr->setCnt )
  1498. p->minSetLocIdx = p->nxtLocIdx+1;
  1499. for(i=0; i<lp->begSectPtr->setCnt; ++i)
  1500. {
  1501. cmScoreSet_t* stp = lp->begSectPtr->setArray[i];
  1502. // temporarily commented out for testing purposes
  1503. // if( stp->doneFl == false )
  1504. // _cmScPerfExec(p, stp, printLvl>0 );
  1505. if( stp->doneFl )
  1506. {
  1507. assert( stp->varId < kScVarCnt );
  1508. lp->begSectPtr->vars[ stp->varId ] = stp->value;
  1509. if( p->cbFunc != NULL )
  1510. {
  1511. cmScMsg_t m;
  1512. m.typeId = kVarMsgScId;
  1513. m.u.meas.varId = stp->varId;
  1514. m.u.meas.value = stp->value;
  1515. p->cbFunc(p->cbArg,&m,sizeof(m));
  1516. }
  1517. }
  1518. }
  1519. }
  1520. }
  1521. }
  1522. void cmScoreSetPerfValue( cmScH_t h, unsigned locIdx, unsigned varId, double value )
  1523. {
  1524. cmSc_t* p = _cmScHandleToPtr(h);
  1525. int li = locIdx;
  1526. for(; li>=0; --li)
  1527. if( p->loc[li].begSectPtr != NULL )
  1528. {
  1529. assert( varId < kScVarCnt );
  1530. p->loc[li].begSectPtr->vars[varId] = value;
  1531. break;
  1532. }
  1533. assert( li>=0);
  1534. }
  1535. void cmScoreSetPerfDynLevel( cmScH_t h, unsigned evtIdx, unsigned dynLvl )
  1536. {
  1537. cmSc_t* p = _cmScHandleToPtr(h);
  1538. assert(evtIdx < p->cnt );
  1539. p->array[ evtIdx ].perfDynLvl = dynLvl;
  1540. }
  1541. cmScRC_t cmScoreDecode( const void* msg, unsigned msgByteCnt, cmScMsg_t* m)
  1542. {
  1543. cmScMsg_t* mp = (cmScMsg_t*)msg;
  1544. *m = *mp;
  1545. return kOkScRC;
  1546. }
  1547. void cmScorePrint( cmScH_t h, cmRpt_t* rpt )
  1548. {
  1549. cmSc_t* p = _cmScHandleToPtr(h);
  1550. unsigned i;
  1551. for(i=0; i<p->cnt; ++i)
  1552. {
  1553. cmScoreEvt_t* r = p->array + i;
  1554. switch(r->type)
  1555. {
  1556. case kBarEvtScId:
  1557. cmRptPrintf(rpt,"%5i %5i %3i bar\n",
  1558. i,
  1559. r->line,
  1560. r->barNumb );
  1561. break;
  1562. case kNonEvtScId:
  1563. cmRptPrintf(rpt,"%5i %5i %3i %3i %s %5s %c%c%c %s\n",
  1564. i,
  1565. r->line,
  1566. r->barNumb,
  1567. r->barNoteIdx,
  1568. cmScEvtTypeIdToLabel(r->type),
  1569. cmMidiToSciPitch(r->pitch,NULL,0),
  1570. cmIsFlag(r->flags,kEvenScFl) ? 'e' : ' ',
  1571. cmIsFlag(r->flags,kTempoScFl) ? 't' : ' ',
  1572. cmIsFlag(r->flags,kDynScFl) ? 'd' : ' ',
  1573. cmIsFlag(r->flags,kDynScFl) ? cmScDynIdToLabel(r->dynVal) : "");
  1574. break;
  1575. default:
  1576. break;
  1577. }
  1578. }
  1579. }
  1580. cmScRC_t cmScoreFileFromMidi( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* scoreFn )
  1581. {
  1582. cmScRC_t rc = kOkScRC;
  1583. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  1584. cmCsvH_t csvH = cmCsvNullHandle;
  1585. cmErr_t err;
  1586. cmErrSetup(&err,&ctx->rpt,"MIDI to Score");
  1587. if( cmMidiFileOpen(midiFn, &mfH, ctx ) != kOkMfRC )
  1588. return cmErrMsg(&err,kMidiFileFailScRC,"Unable to open the MIDI file '%s'.",midiFn);
  1589. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  1590. {
  1591. cmErrMsg(&err,kCsvFailScRC,"Unable to initialize the CSV file: '%s'.",scoreFn);
  1592. goto errLabel;
  1593. }
  1594. // Convert the track message 'dtick' field to delta-microseconds.
  1595. cmMidiFileTickToMicros(mfH);
  1596. unsigned msgCnt = cmMidiFileMsgCount(mfH);
  1597. unsigned i;
  1598. const cmMidiTrackMsg_t** tmpp = cmMidiFileMsgArray(mfH);
  1599. double acc_secs = 0;
  1600. for(i=0; i<msgCnt; ++i)
  1601. {
  1602. const cmMidiTrackMsg_t* tmp = tmpp[i];
  1603. cmCsvCell_t* cp = NULL;
  1604. unsigned lexTId = 0;
  1605. const cmChar_t* opStr = NULL;
  1606. unsigned midiCh = 0;
  1607. unsigned d0 = 0;
  1608. unsigned d1 = 0;
  1609. double dsecs = (double)tmp->dtick / 1000000.0;
  1610. acc_secs += dsecs;
  1611. if( tmp->status == 0xff )
  1612. opStr = cmMidiMetaStatusToLabel(tmp->metaId);
  1613. else
  1614. {
  1615. opStr = cmMidiStatusToLabel(tmp->status);
  1616. if( cmMidiIsChStatus( tmp->status ) )
  1617. {
  1618. midiCh = tmp->u.chMsgPtr->ch;
  1619. d0 = tmp->u.chMsgPtr->d0;
  1620. d1 = tmp->u.chMsgPtr->d1;
  1621. }
  1622. }
  1623. if( cmCsvAppendRow(csvH, &cp, cmCsvInsertSymUInt(csvH,i), kIntCsvTFl, lexTId ) != kOkCsvRC )
  1624. {
  1625. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'id' column in '%s'.",cmStringNullGuard(scoreFn));
  1626. goto errLabel;
  1627. }
  1628. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->trkIdx, lexTId ) != kOkCsvRC )
  1629. {
  1630. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'trk' column in '%s'.",cmStringNullGuard(scoreFn));
  1631. goto errLabel;
  1632. }
  1633. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, 0, lexTId ) != kOkCsvRC )
  1634. {
  1635. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'evt' column in '%s'.",cmStringNullGuard(scoreFn));
  1636. goto errLabel;
  1637. }
  1638. if( cmCsvInsertTextColAfter(csvH, cp, &cp, opStr, lexTId ) != kOkCsvRC )
  1639. {
  1640. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'opcode' column in '%s'.",cmStringNullGuard(scoreFn));
  1641. goto errLabel;
  1642. }
  1643. if( cmCsvInsertDoubleColAfter(csvH, cp, &cp, dsecs, lexTId ) != kOkCsvRC )
  1644. {
  1645. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'dticks' column in '%s'.",cmStringNullGuard(scoreFn));
  1646. goto errLabel;
  1647. }
  1648. if( cmCsvInsertDoubleColAfter(csvH, cp, &cp, acc_secs, lexTId ) != kOkCsvRC )
  1649. {
  1650. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'micros' column in '%s'.",cmStringNullGuard(scoreFn));
  1651. goto errLabel;
  1652. }
  1653. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->status, lexTId ) != kOkCsvRC )
  1654. {
  1655. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'status' column in '%s'.",cmStringNullGuard(scoreFn));
  1656. goto errLabel;
  1657. }
  1658. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->metaId, lexTId ) != kOkCsvRC )
  1659. {
  1660. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'meta' column in '%s'.",cmStringNullGuard(scoreFn));
  1661. goto errLabel;
  1662. }
  1663. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, midiCh, lexTId ) != kOkCsvRC )
  1664. {
  1665. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'ch' column in '%s'.",cmStringNullGuard(scoreFn));
  1666. goto errLabel;
  1667. }
  1668. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, d0, lexTId ) != kOkCsvRC )
  1669. {
  1670. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'd0' column in '%s'.",cmStringNullGuard(scoreFn));
  1671. goto errLabel;
  1672. }
  1673. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, d1, lexTId ) != kOkCsvRC )
  1674. {
  1675. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'd1' column in '%s'.",cmStringNullGuard(scoreFn));
  1676. goto errLabel;
  1677. }
  1678. }
  1679. errLabel:
  1680. cmMidiFileClose(&mfH);
  1681. cmCsvFinalize(&csvH);
  1682. return rc;
  1683. }
  1684. void cmScoreTest( cmCtx_t* ctx, const cmChar_t* fn )
  1685. {
  1686. cmScH_t h = cmScNullHandle;
  1687. if( cmScoreInitialize(ctx,&h,fn,0,NULL,0,NULL,NULL, cmSymTblNullHandle ) != kOkScRC )
  1688. return;
  1689. cmScorePrint(h,&ctx->rpt);
  1690. cmScoreFinalize(&h);
  1691. }
  1692. // 1. Fix absolute message time which was incorrect on original score file.
  1693. // 2.
  1694. void cmScoreFix( cmCtx_t* ctx )
  1695. {
  1696. const cmChar_t* mfn = "/home/kevin/src/cmgv/src/gv/data/ImaginaryThemes.mid";
  1697. const cmChar_t* crfn = "/home/kevin/src/cmgv/src/gv/data/mod0a.txt";
  1698. const cmChar_t* cwfn = "/home/kevin/src/cmgv/src/gv/data/mod1.csv";
  1699. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  1700. cmCsvH_t csvH = cmCsvNullHandle;
  1701. const cmMidiTrackMsg_t** msg = NULL;
  1702. double secs = 0.0;
  1703. int ci,mi,crn,mn;
  1704. bool errFl = true;
  1705. unsigned handCnt = 0;
  1706. unsigned midiMissCnt = 0;
  1707. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  1708. goto errLabel;
  1709. if( cmCsvLexRegisterMatcher(csvH, cmCsvLexNextAvailId(csvH), _cmScLexSciPitchMatcher ) != kOkCsvRC )
  1710. goto errLabel;
  1711. if( cmCsvParseFile(csvH, crfn, 0 ) != kOkCsvRC )
  1712. goto errLabel;
  1713. if( cmMidiFileOpen(mfn,&mfH,ctx) != kOkMfRC )
  1714. goto errLabel;
  1715. cmMidiFileTickToMicros(mfH);
  1716. cmMidiFileCalcNoteDurations(mfH);
  1717. mn = cmMidiFileMsgCount(mfH);
  1718. msg = cmMidiFileMsgArray(mfH);
  1719. crn = cmCsvRowCount(csvH);
  1720. // for each row in the score file
  1721. for(ci=1,mi=0; ci<crn && cmCsvLastRC(csvH)==kOkCsvRC; ++ci)
  1722. {
  1723. unsigned id;
  1724. // zero the duration column
  1725. if( cmCsvCellPtr(csvH, ci, kDSecsColScIdx ) != NULL )
  1726. cmCsvSetCellUInt( csvH, ci, kDSecsColScIdx, 0 );
  1727. // get the MIDI file event id for this row
  1728. if((id = cmCsvCellUInt(csvH,ci,kMidiFileIdColScIdx)) == UINT_MAX)
  1729. {
  1730. // this is a hand-entered event - so it has no event id
  1731. ++handCnt;
  1732. }
  1733. else
  1734. {
  1735. for(; mi<mn; ++mi)
  1736. {
  1737. const cmMidiTrackMsg_t* m = msg[mi];
  1738. assert( mi+1 <= id );
  1739. secs += m->dtick/1000000.0;
  1740. if( mi+1 != id )
  1741. {
  1742. if( m->status == kNoteOnMdId && m->u.chMsgPtr->d1>0 )
  1743. {
  1744. // this MIDI note-on does not have a corresponding score event
  1745. ++midiMissCnt;
  1746. }
  1747. }
  1748. else
  1749. {
  1750. cmCsvSetCellDouble( csvH, ci, kSecsColScIdx, secs );
  1751. ++mi;
  1752. if( m->status == kNoteOnMdId )
  1753. cmCsvSetCellDouble( csvH, ci, kDSecsColScIdx, m->u.chMsgPtr->durTicks/1000000.0 );
  1754. break;
  1755. }
  1756. }
  1757. if( mi==mn)
  1758. printf("done on row:%i\n",ci);
  1759. }
  1760. }
  1761. if( cmCsvLastRC(csvH) != kOkCsvRC )
  1762. goto errLabel;
  1763. if( cmCsvWrite(csvH,cwfn) != kOkCsvRC )
  1764. goto errLabel;
  1765. errFl = false;
  1766. errLabel:
  1767. if( errFl )
  1768. printf("Score fix failed.\n");
  1769. else
  1770. printf("Score fix done! hand:%i miss:%i\n",handCnt,midiMissCnt);
  1771. cmMidiFileClose(&mfH);
  1772. cmCsvFinalize(&csvH);
  1773. }