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

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