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

cmScore.c 65KB

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