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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591
  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. for(;(cp = cmTextNextNonWhiteC(cp)) != NULL; cp=ep )
  512. {
  513. // go to command/id space
  514. if((ip = cmTextNextWhiteOrEosC(cp)) == NULL )
  515. goto errLabel;
  516. // goto label
  517. if((ip = cmTextNextNonWhiteC(ip)) == NULL )
  518. goto errLabel;
  519. // goto end of label
  520. if((ep = cmTextNextWhiteOrEosC(ip)) == NULL )
  521. goto errLabel;
  522. else
  523. {
  524. unsigned n = (ep-ip)+1;
  525. cmChar_t markTextStr[n+1];
  526. strncpy(markTextStr,ip,n);
  527. markTextStr[n] = 0;
  528. // remove any trailing white space
  529. cmTextTrimEnd(markTextStr);
  530. cmMarkScMId_t cmdId = kInvalidScMId;
  531. switch( *cp )
  532. {
  533. case 'c': cmdId = kRecdBegScMId; break;
  534. case 'e': cmdId = kRecdEndScMId; break;
  535. case 'p': cmdId = kPlayBegScMId; break;
  536. case 'd': cmdId = kPlayEndScMId; break;
  537. case 'f': cmdId = kFadeScMId; break;
  538. default:
  539. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unrecognized marker command character '%c' at row index %i.",*cp,rowIdx);
  540. }
  541. cmScMark_t* mp = cmMemAllocZ(cmScMark_t,1);
  542. mp->cmdId = cmdId;
  543. mp->labelSymId = cmSymTblRegisterSymbol(p->stH,markTextStr);
  544. mp->scoreIdx = scoreIdx;
  545. mp->csvRowIdx = rowIdx;
  546. //printf("%i %c '%s'\n",rowIdx,*cp,markTextStr);
  547. // insert the new mark at the end of the list
  548. if( p->markList == NULL )
  549. p->markList = mp;
  550. else
  551. {
  552. cmScMark_t* ep = p->markList;
  553. while( ep->link != NULL )
  554. ep = ep->link;
  555. ep->link = mp;
  556. }
  557. }
  558. }
  559. return kOkScRC;
  560. errLabel:
  561. return cmErrMsg(&p->err,kSyntaxErrScRC,"Invalid record/playback field ('%s') on row index:%i.",cmStringNullGuard(text),rowIdx);
  562. }
  563. void _cmScPrintSets( const cmChar_t* label, cmScSet_t* setList )
  564. {
  565. printf("%s\n",label);
  566. const cmScSet_t* sp = setList;
  567. for(; sp != NULL; sp=sp->link )
  568. {
  569. const cmScSetEle_t* ep = sp->eles;
  570. for(; ep!=NULL; ep=ep->link)
  571. printf("%i ",ep->eleIdx);
  572. printf(" : ");
  573. for(ep=sp->sects; ep!=NULL; ep=ep->link)
  574. printf("%s ",cmStringNullGuard(ep->label));
  575. printf("\n");
  576. }
  577. }
  578. cmScRC_t _cmScParseNoteOn( cmSc_t* p, unsigned rowIdx, cmScoreEvt_t* s, unsigned scoreIdx, int barNumb, unsigned barNoteIdx )
  579. {
  580. cmScRC_t rc = kOkScRC;
  581. unsigned flags = 0;
  582. unsigned dynVal = kInvalidDynScId;
  583. const cmChar_t* sciPitch;
  584. cmMidiByte_t midiPitch;
  585. const cmChar_t* attr;
  586. double secs;
  587. double durSecs;
  588. const cmCsvCell_t* cell;
  589. s += scoreIdx;
  590. // verify the scientific pitch cell was formatted correcly
  591. if((cell = cmCsvCellPtr(p->cH,rowIdx,kPitchColScIdx)) == NULL || cell->lexTId != p->sciPitchLexTId )
  592. return cmErrMsg(&p->err,kSyntaxErrScRC,"Pitch column format error.");
  593. if((sciPitch = cmCsvCellText(p->cH,rowIdx,kPitchColScIdx)) == NULL )
  594. return cmErrMsg(&p->err,kSyntaxErrScRC,"Expected a scientific pitch value");
  595. if((midiPitch = cmSciPitchToMidi(sciPitch)) == kInvalidMidiPitch)
  596. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unable to convert the scientific pitch '%s' to a MIDI value. ");
  597. // it is possible that note delta-secs field is empty - so default to 0
  598. if((secs = cmCsvCellDouble(p->cH, rowIdx, kSecsColScIdx )) == DBL_MAX) // Returns DBL_MAX on error.
  599. flags += kInvalidScFl;
  600. // skip attribute
  601. if((attr = cmCsvCellText(p->cH,rowIdx,kSkipColScIdx)) != NULL && *attr == 's' )
  602. flags += kSkipScFl;
  603. // evenness attribute
  604. if((attr = cmCsvCellText(p->cH,rowIdx,kEvenColScIdx)) != NULL && *attr == 'e' )
  605. {
  606. flags += kEvenScFl;
  607. _cmScParseAttr(p,scoreIdx,attr,kEvenScFl);
  608. }
  609. // grace attribute
  610. if((attr = cmCsvCellText(p->cH,rowIdx,kGraceColScIdx)) != NULL && *attr == 'g' )
  611. {
  612. flags += kGraceScFl;
  613. if( cmIsNotFlag(flags,kEvenScFl) )
  614. return cmErrMsg(&p->err,kSyntaxErrScRC,"All 'grace' notes should also be 'even' notes.");
  615. }
  616. // tempo attribute
  617. if((attr = cmCsvCellText(p->cH,rowIdx,kTempoColScIdx)) != NULL && *attr == 't' )
  618. {
  619. flags += kTempoScFl;
  620. _cmScParseAttr(p,scoreIdx,attr,kTempoScFl);
  621. }
  622. // dynamics attribute
  623. if((attr = cmCsvCellText(p->cH,rowIdx,kDynColScIdx)) != NULL )
  624. {
  625. if((dynVal = _cmScDynLabelToId(attr)) == kInvalidDynScId )
  626. return cmErrMsg(&p->err,kSyntaxErrScRC,"Unknown dynamic label '%s'.",cmStringNullGuard(attr));
  627. flags += kDynScFl;
  628. _cmScParseAttr(p,scoreIdx,attr,kDynScFl);
  629. }
  630. // tempo/non-grace rythmic value
  631. if( cmIsFlag(flags,kTempoScFl) || (cmIsFlag(flags,kEvenScFl) && cmIsNotFlag(flags,kGraceScFl)) )
  632. {
  633. double frac = cmCsvCellDouble(p->cH,rowIdx,kFracColScIdx);
  634. // no 'frac' value is given for the last note of the set we must accept error
  635. // values here and validate the actual values later
  636. if( frac>0 && frac!=DBL_MAX )
  637. s->frac = frac;
  638. }
  639. // Returns DBL_MAX on error.
  640. if((durSecs = cmCsvCellDouble(p->cH, rowIdx, kDSecsColScIdx )) == DBL_MAX)
  641. durSecs = 0.25;
  642. // parse the recd/play markers
  643. if((attr = cmCsvCellText(p->cH,rowIdx,kRecdPlayColScIdx)) != NULL )
  644. {
  645. if((rc = _cmScParseMarkers(p,scoreIdx,attr,rowIdx)) != kOkScRC )
  646. return rc;
  647. }
  648. s->type = kNonEvtScId;
  649. s->secs = secs;
  650. s->pitch = midiPitch;
  651. s->flags = flags;
  652. s->dynVal = dynVal;
  653. s->barNumb = barNumb;
  654. s->barNoteIdx = barNoteIdx;
  655. s->durSecs = durSecs;
  656. s->csvRowNumb = rowIdx+1;
  657. return rc;
  658. }
  659. cmScRC_t _cmScParseSectionColumn( cmSc_t* p, unsigned rowIdx, unsigned evtIdx, cmScSect_t* sectList )
  660. {
  661. const cmCsvCell_t* cell;
  662. cmScSect_t* sect;
  663. const cmChar_t* label;
  664. // most rows don't have section labels
  665. if( (cell = cmCsvCellPtr( p->cH,rowIdx,kSectionColScIdx)) == NULL
  666. || (label = cmCsvCellText(p->cH,rowIdx,kSectionColScIdx)) == NULL)
  667. return kOkScRC;
  668. // verify the section id type
  669. if( cell->lexTId != p->sectionLexTId && cell->lexTId != kIntLexTId )
  670. return cmErrMsg(&p->err,kSyntaxErrScRC,"'section' column format error.");
  671. sect = cmMemAllocZ(cmScSect_t,1);
  672. sect->label = label;
  673. sect->startIdx = evtIdx;
  674. //printf("section:%s\n",label);
  675. cmScSect_t* sp = sectList;
  676. assert( sp != NULL );
  677. while( sp->link != NULL )
  678. sp = sp->link;
  679. sp->link = sect;
  680. return kOkScRC;
  681. }
  682. cmScoreSection_t* _cmScLabelToSection( cmSc_t* p, const cmChar_t* label )
  683. {
  684. int i;
  685. for(i=0; i<p->sectCnt; ++i)
  686. if( strcmp(p->sect[i].label,label) == 0 )
  687. return p->sect + i;
  688. return NULL;
  689. }
  690. // Calculate the total number of all types of sets and
  691. // then convert each of the cmScSet_t linked list's to
  692. // a single linear cmScoreSet_t list (p->sets[]).
  693. cmScRC_t _cmScProcSets( cmSc_t* p )
  694. {
  695. cmScRC_t rc = kOkScRC;
  696. // calculate the count of all sets
  697. unsigned i;
  698. unsigned n = 0;
  699. cmScSet_t* sp = p->setList;
  700. for(n=0; sp!=NULL; sp=sp->link)
  701. if( sp->eles != NULL )
  702. ++n;
  703. // allocate the linear set array
  704. p->sets = cmMemAllocZ(cmScoreSet_t,n);
  705. p->setCnt = n;
  706. // fill in the linear set array
  707. sp = p->setList;
  708. for(i=0; sp!=NULL; sp=sp->link)
  709. if( sp->eles != NULL )
  710. {
  711. unsigned j;
  712. unsigned en;
  713. unsigned rowNumb = 0;
  714. assert( i<n );
  715. // get the count of elements assoc'd with this set
  716. cmScSetEle_t* ep = sp->eles;
  717. for(en=0; ep!=NULL; ep=ep->link)
  718. ++en;
  719. assert( en > 0 );
  720. // allocate the element array
  721. p->sets[i].eleCnt = en;
  722. p->sets[i].eleArray = cmMemAllocZ(cmScoreEvt_t*,en);
  723. // fill in the element array
  724. ep = sp->eles;
  725. unsigned graceCnt = 0;
  726. for(j=0; ep!=NULL; ep=ep->link,++j)
  727. {
  728. assert(ep->eleIdx != cmInvalidIdx && ep->eleIdx<p->cnt);
  729. p->sets[i].eleArray[j] = p->array + ep->eleIdx;
  730. assert( cmIsFlag( p->sets[i].eleArray[j]->flags, sp->typeFl) );
  731. rowNumb = p->array[ep->eleIdx].csvRowNumb;
  732. unsigned flags = p->array[ep->eleIdx].flags;
  733. // count grace notes
  734. if( cmIsFlag(flags,kGraceScFl) )
  735. ++graceCnt;
  736. // validate the 'frac' field - all but the last note in
  737. // tempo and non-grace evenness sets must have a non-zero 'frac' value.
  738. if( en>0 && j<en-1 && (sp->typeFl==kTempoScFl || (sp->typeFl==kEvenScFl && graceCnt==0)) && p->array[ep->eleIdx].frac==0)
  739. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The note on row number %i must have a non-zero 'frac' value.",p->array[ep->eleIdx].csvRowNumb);
  740. }
  741. // get the count of sections assoc'd with this set
  742. ep = sp->sects;
  743. for(en=0; ep!=NULL; ep=ep->link)
  744. ++en;
  745. // allocate the section array
  746. p->sets[i].varId = _cmScVarFlagToId(sp->typeFl);
  747. p->sets[i].sectCnt = en;
  748. p->sets[i].sectArray = cmMemAllocZ(cmScoreSection_t*,en);
  749. p->sets[i].symArray = cmMemAllocZ(unsigned,en);
  750. p->sets[i].costSymArray = cmMemAllocZ(unsigned,en);
  751. // fill in the section array with sections which this set will be applied to
  752. ep = sp->sects;
  753. for(j=0; ep!=NULL; ep=ep->link,++j)
  754. {
  755. cmScoreSection_t* sp;
  756. assert(ep->label != NULL);
  757. if((sp = _cmScLabelToSection(p, ep->label )) == NULL )
  758. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The section labelled '%s' could not be found for the set which includes row number %i.",ep->label,rowNumb);
  759. else
  760. {
  761. if( cmSymTblIsValid(p->stH) )
  762. {
  763. // THIS PRINTF PREVENTS CRASH ON OSX ???????
  764. //printf("PROCSET:%i %c %s\n",p->sets[i].varId,_cmScVarIdToChar(p->sets[i].varId),ep->label);
  765. char cc = _cmScVarIdToChar(p->sets[i].varId);
  766. int nn = snprintf(NULL,0,"%c-%s",cc,ep->label);
  767. char b[nn+2];
  768. snprintf(b,nn,"%c-%s",cc,ep->label);
  769. p->sets[i].symArray[j] = cmSymTblRegisterSymbol(p->stH,b);
  770. snprintf(b,nn+1,"c%c-%s",cc,ep->label);
  771. p->sets[i].costSymArray[j] = cmSymTblRegisterSymbol(p->stH,b);
  772. //p->sets[i].symArray[j] = cmSymTblRegisterFmt(p->stH,"%c-%s", cc,ep->label);
  773. //p->sets[i].costSymArray[j] = cmSymTblRegisterFmt(p->stH,"c%c-%s",cc,ep->label);
  774. }
  775. else
  776. {
  777. p->sets[i].symArray[j] = cmInvalidId;
  778. p->sets[i].costSymArray[j] = cmInvalidId;
  779. }
  780. p->sets[i].sectArray[j] = sp;
  781. sp->setArray = cmMemResizeP(cmScoreSet_t*,sp->setArray,++sp->setCnt);
  782. sp->setArray[sp->setCnt-1] = p->sets + i;
  783. }
  784. }
  785. ++i;
  786. }
  787. assert(i==n);
  788. // assign each set to the location which contains it's last element.
  789. // (this is earliest point in the score location where all the
  790. // performance information contained in the set may be valid)
  791. for(i=0; i<p->setCnt; ++i)
  792. {
  793. assert( p->sets[i].eleCnt >= 1 );
  794. // get a ptr to the last element for the ith set
  795. const cmScoreEvt_t* ep = p->sets[i].eleArray[ p->sets[i].eleCnt-1 ];
  796. unsigned j,k;
  797. // find the location which contains the last element
  798. for(j=0; j<p->locCnt; ++j)
  799. {
  800. for(k=0; k<p->loc[j].evtCnt; ++k)
  801. if( p->loc[j].evtArray[k] == ep )
  802. break;
  803. if(k<p->loc[j].evtCnt)
  804. break;
  805. }
  806. assert( j<p->locCnt );
  807. // assign the ith set to the location which contains it's last element
  808. p->sets[i].llink = p->loc[j].setList;
  809. p->loc[j].setList = p->sets + i;
  810. }
  811. return rc;
  812. }
  813. cmScRC_t _cmScProcSections( cmSc_t* p, cmScSect_t* sectList )
  814. {
  815. cmScRC_t rc = kOkScRC;
  816. unsigned i;
  817. // count the sections
  818. cmScSect_t* sp = sectList;
  819. p->sectCnt = 0;
  820. for(; sp!=NULL; sp=sp->link)
  821. if( sp->label != NULL )
  822. ++p->sectCnt;
  823. // alloc a section array
  824. p->sect = cmMemAllocZ(cmScoreSection_t,p->sectCnt);
  825. // fill the section array
  826. sp = sectList;
  827. for(i=0; sp!=NULL; sp=sp->link)
  828. if( sp->label != NULL )
  829. {
  830. p->sect[i].label = sp->label;
  831. p->sect[i].index = i;
  832. p->sect[i].begEvtIndex = sp->startIdx;
  833. ++i;
  834. }
  835. // assign the begSectPtr to each section
  836. for(i=0; i<p->sectCnt; ++i)
  837. {
  838. assert( p->sect[i].begEvtIndex < p->cnt );
  839. unsigned j,k;
  840. const cmScoreEvt_t* ep = p->array + p->sect[i].begEvtIndex;
  841. for(j=0; j<p->locCnt; ++j)
  842. {
  843. for(k=0; k<p->loc[j].evtCnt; ++k)
  844. if( p->loc[j].evtArray[k] == ep )
  845. {
  846. p->loc[j].begSectPtr = p->sect + i;
  847. p->sect[i].locPtr = p->loc + j;
  848. break;
  849. }
  850. if( k<p->loc[j].evtCnt)
  851. break;
  852. }
  853. }
  854. // release the section linked list
  855. sp = sectList;
  856. cmScSect_t* np = NULL;
  857. while(sp!=NULL)
  858. {
  859. np = sp->link;
  860. cmMemFree(sp);
  861. sp = np;
  862. }
  863. //_cmScPrintSets("Sets",p->setList );
  864. _cmScProcSets(p);
  865. return rc;
  866. }
  867. const cmScoreLoc_t* _cmScFindMarkLoc( cmSc_t* p, cmMarkScMId_t cmdId, unsigned labelSymId, const cmScoreMarker_t** markRef )
  868. {
  869. unsigned i;
  870. for(i=0; i<p->locCnt; ++i)
  871. {
  872. cmScoreMarker_t* smp = p->loc[i].markList;
  873. for(; smp!=NULL; smp=smp->link)
  874. {
  875. if( smp->markTypeId==cmdId && smp->labelSymId==labelSymId )
  876. return p->loc + i;
  877. if( markRef != NULL )
  878. *markRef = smp;
  879. }
  880. }
  881. return NULL;
  882. }
  883. unsigned _cmScMarkerLabelIndex( cmSc_t* p, unsigned labelSymId )
  884. {
  885. unsigned i;
  886. for(i=0; i<p->markLabelCnt; ++i)
  887. if( p->markLabelArray[i] == labelSymId )
  888. return i;
  889. return cmInvalidIdx;
  890. }
  891. // Verify that the record/play begin/end and fade markers fall in the correct time order.
  892. // (e.g. 'begin' must be before 'end' and 'fade' must be between and 'begin' and 'end').
  893. cmScRC_t _cmScValidateMarkers( cmSc_t* p )
  894. {
  895. cmScRC_t rc = kOkScRC;
  896. unsigned i;
  897. for(i=0; i<p->locCnt; ++i)
  898. {
  899. cmScoreMarker_t* sm0p = p->loc[i].markList;
  900. for(; sm0p!=NULL; sm0p=sm0p->link)
  901. {
  902. const cmScoreLoc_t* sl0p;
  903. const cmScoreLoc_t* sl1p;
  904. switch( sm0p->markTypeId )
  905. {
  906. case kRecdBegScMId:
  907. if((sl0p = _cmScFindMarkLoc(p,kRecdEndScMId, sm0p->labelSymId, NULL )) == NULL )
  908. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'record begin' marker at CSV row index %i does not have an associated 'record end' marker.",sm0p->csvRowIdx);
  909. else
  910. if( sl0p->index <= p->loc[i].index )
  911. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'record end' marker comes before associated with the 'record begin' marker at CSV row index %i.",sm0p->csvRowIdx);
  912. break;
  913. case kRecdEndScMId:
  914. if((sl0p = _cmScFindMarkLoc(p,kRecdBegScMId, sm0p->labelSymId, NULL )) == NULL )
  915. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'record end' marker at CSV row index %i does not have an associated 'record begin' marker.",sm0p->csvRowIdx);
  916. else
  917. if( sl0p->index > p->loc[i].index )
  918. 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);
  919. break;
  920. case kFadeScMId:
  921. if((sl0p = _cmScFindMarkLoc(p,kPlayBegScMId, sm0p->labelSymId, NULL )) == NULL )
  922. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'fade' marker at CSV row index %i does not have an associated 'play begin' marker.",sm0p->csvRowIdx);
  923. else
  924. if((sl1p = _cmScFindMarkLoc(p,kPlayEndScMId, sm0p->labelSymId, NULL )) == NULL )
  925. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'fade' marker at CSV row index %i does not have an associated 'play end' marker.",sm0p->csvRowIdx);
  926. else
  927. if( sl0p->index > p->loc[i].index || sl1p->index < p->loc[i].index )
  928. 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);
  929. break;
  930. case kPlayBegScMId:
  931. if((sl0p = _cmScFindMarkLoc(p,kPlayEndScMId, sm0p->labelSymId, NULL )) == NULL )
  932. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'play begin' marker at CSV row index %i does not have an associated 'play end' marker.",sm0p->csvRowIdx);
  933. else
  934. if( sl0p->index <= p->loc[i].index )
  935. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'play end' marker comes before associated with the 'play begin' marker at CSV row index %i.",sm0p->csvRowIdx);
  936. break;
  937. case kPlayEndScMId:
  938. if((sl0p = _cmScFindMarkLoc(p,kPlayBegScMId, sm0p->labelSymId, NULL )) == NULL )
  939. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"The 'play end' marker at CSV row index %i does not have an associated 'play begin' marker.",sm0p->csvRowIdx);
  940. else
  941. if( sl0p->index > p->loc[i].index )
  942. 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);
  943. break;
  944. default:
  945. break;
  946. }
  947. }
  948. }
  949. return rc;
  950. }
  951. cmScRC_t _cmScProcMarkers( cmSc_t* p )
  952. {
  953. // for each marker in the p->markList
  954. // (p->markList is created by _cmScParseMarkers() during CSV file parsing.)
  955. cmScMark_t* mp = p->markList;
  956. for(; mp!=NULL; mp=mp->link)
  957. {
  958. assert( mp->scoreIdx < p->cnt );
  959. // get the score location assoc'd with this marker
  960. unsigned locIdx = p->array[ mp->scoreIdx ].locIdx;
  961. assert( locIdx < p->locCnt );
  962. cmScoreLoc_t* slp = p->loc + locIdx;
  963. // create a cmScoreMarker record.
  964. cmScoreMarker_t* smp = cmMemAllocZ(cmScoreMarker_t,1);
  965. smp->markTypeId = mp->cmdId;
  966. smp->labelSymId = mp->labelSymId;
  967. smp->csvRowIdx = mp->csvRowIdx;
  968. smp->scoreLocPtr = slp;
  969. // attach the new scoreMarker record to the assoc'd score loc. recd
  970. if( slp->markList == NULL )
  971. slp->markList = smp;
  972. else
  973. {
  974. cmScoreMarker_t* sm0p = slp->markList;
  975. while( sm0p->link != NULL )
  976. sm0p = sm0p->link;
  977. sm0p->link = smp;
  978. }
  979. // if the id represented by this marker
  980. if( _cmScMarkerLabelIndex(p,smp->labelSymId) == cmInvalidIdx )
  981. {
  982. p->markLabelArray = cmMemResizeP(unsigned,p->markLabelArray,p->markLabelCnt+1);
  983. p->markLabelArray[p->markLabelCnt] = smp->labelSymId;
  984. p->markLabelCnt += 1;
  985. }
  986. }
  987. // validate the markers
  988. return _cmScValidateMarkers(p);
  989. }
  990. cmScRC_t _cmScParseFile( cmSc_t* p, cmCtx_t* ctx, const cmChar_t* fn )
  991. {
  992. cmScRC_t rc = kOkScRC;
  993. unsigned barNoteIdx = 0;
  994. int barEvtIdx = cmInvalidIdx;
  995. int barNumb = 0;
  996. double secs;
  997. double cur_secs = 0;
  998. p->sectList = cmMemAllocZ(cmScSect_t,1); // section zero
  999. //_cmScNewSet(p); // preallocate the first set
  1000. // initialize the CSV file parser
  1001. if( cmCsvInitialize(&p->cH, ctx ) != kOkCsvRC )
  1002. {
  1003. rc = cmErrMsg(&p->err,kCsvFailScRC,"Score file initialization failed.");
  1004. goto errLabel;
  1005. }
  1006. // register custom lex token matchers for sci-pitch and section id's
  1007. if( cmCsvLexRegisterMatcher(p->cH, p->sciPitchLexTId = cmCsvLexNextAvailId(p->cH)+0, _cmScLexSciPitchMatcher ) != kOkCsvRC
  1008. ||cmCsvLexRegisterMatcher(p->cH, p->sectionLexTId = cmCsvLexNextAvailId(p->cH)+1, _cmScLexSectionIdMatcher) != kOkCsvRC )
  1009. {
  1010. rc = cmErrMsg(&p->err,kCsvFailScRC,"CSV token matcher registration failed.");
  1011. goto errLabel;
  1012. }
  1013. // parse the CSV file
  1014. if( cmCsvParseFile(p->cH, fn, 0 ) != kOkCsvRC )
  1015. {
  1016. rc = cmErrMsg(&p->err,kCsvFailScRC,"CSV file parsing failed on the file '%s'.",cmStringNullGuard(fn));
  1017. goto errLabel;
  1018. }
  1019. // allocate the score event array
  1020. p->cnt = cmCsvRowCount(p->cH);
  1021. p->array = cmMemAllocZ(cmScoreEvt_t,p->cnt);
  1022. unsigned i,j;
  1023. // skip labels line - start on line 1
  1024. for(i=1,j=0; i<p->cnt && rc==kOkScRC; ++i)
  1025. {
  1026. // get the row 'type' label
  1027. const char* typeLabel;
  1028. if((typeLabel = cmCsvCellText(p->cH,i,kTypeLabelColScIdx)) == NULL )
  1029. {
  1030. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"No type label.");
  1031. break;
  1032. }
  1033. // convert the row 'type' label to an id
  1034. unsigned tid;
  1035. if((tid = _cmScEvtTypeLabelToId(typeLabel)) == kInvalidEvtScId)
  1036. {
  1037. rc = cmErrMsg(&p->err,kSyntaxErrScRC,"Unknown type '%s'.",cmStringNullGuard(typeLabel));
  1038. break;
  1039. }
  1040. // set the score file line number
  1041. p->array[j].line = i + 1;
  1042. secs = DBL_MAX;
  1043. switch(tid)
  1044. {
  1045. case kBarEvtScId: // parse bar lines
  1046. if((rc = _cmScParseBar(p,i,p->array+j,&barNumb)) == kOkScRC )
  1047. {
  1048. barNoteIdx = 0;
  1049. barEvtIdx = j;
  1050. p->array[j].index = j;
  1051. ++j;
  1052. }
  1053. break;
  1054. case kNonEvtScId: // parse note-on events
  1055. if((rc = _cmScParseNoteOn(p, i, p->array, j, barNumb, barNoteIdx )) == kOkScRC )
  1056. {
  1057. secs = p->array[j].secs;
  1058. if( p->array[j].secs == DBL_MAX )
  1059. p->array[j].secs = cur_secs;
  1060. if( cmIsFlag(p->array[j].flags,kSkipScFl) == false )
  1061. {
  1062. p->array[j].index = j;
  1063. ++j;
  1064. }
  1065. ++barNoteIdx;
  1066. }
  1067. break;
  1068. default:
  1069. // Returns DBL_MAX on error.
  1070. secs = cmCsvCellDouble(p->cH, i, kSecsColScIdx );
  1071. break;
  1072. }
  1073. if( rc == kOkScRC )
  1074. {
  1075. if( secs != DBL_MAX )
  1076. cur_secs = secs;
  1077. // form the section list
  1078. if( j > 0 )
  1079. if((rc = _cmScParseSectionColumn(p,i,j-1,p->sectList)) != kOkScRC )
  1080. break;
  1081. // the bar lines don't have times so set the time of the bar line to the
  1082. // time of the first event in the bar.
  1083. if( barEvtIdx != cmInvalidIdx && secs != DBL_MAX )
  1084. {
  1085. assert( p->array[ barEvtIdx ].type == kBarEvtScId );
  1086. p->array[ barEvtIdx ].secs = secs;
  1087. // handle the case where the previous bar had no events
  1088. // BUG BUG BUG this is a hack which will fail if the first bar does not have events.
  1089. if( barEvtIdx>=1 && p->array[ barEvtIdx-1].type == kBarEvtScId )
  1090. p->array[ barEvtIdx-1].secs = secs;
  1091. barEvtIdx = cmInvalidIdx;
  1092. }
  1093. }
  1094. }
  1095. if( rc == kSyntaxErrScRC )
  1096. {
  1097. cmErrMsg(&p->err,rc,"Syntax error on line %i in '%s'.",i+1,cmStringNullGuard(fn));
  1098. goto errLabel;
  1099. }
  1100. p->cnt = j;
  1101. errLabel:
  1102. return rc;
  1103. }
  1104. cmScRC_t _cmScInitLocArray( cmSc_t* p )
  1105. {
  1106. cmScRC_t rc = kOkScRC;
  1107. double maxDSecs = 0; // max time between events that are considered simultaneous
  1108. unsigned barNumb = 0;
  1109. int i,j,k;
  1110. if( p->cnt==0)
  1111. return rc;
  1112. p->locCnt = 1;
  1113. // count the number of unique time locations in the score
  1114. p->locCnt = 1;
  1115. for(i=1; i<p->cnt; ++i )
  1116. {
  1117. if( p->array[i].secs < p->array[i-1].secs )
  1118. 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);
  1119. if( (p->array[i].secs - p->array[i-1].secs) > maxDSecs )
  1120. ++p->locCnt;
  1121. }
  1122. if( rc != kOkScRC )
  1123. return rc;
  1124. // allocate the loc. array
  1125. p->loc = cmMemAllocZ(cmScoreLoc_t,p->locCnt);
  1126. // fill in the location array
  1127. for(i=0,k=0; i<p->cnt; ++k)
  1128. {
  1129. j = i+1;
  1130. // get the count of events at this location
  1131. while( j<p->cnt && p->array[j].secs - p->array[j-1].secs <= maxDSecs )
  1132. ++j;
  1133. assert(k<p->locCnt);
  1134. p->loc[k].index = k;
  1135. p->loc[k].evtCnt = j-i;
  1136. p->loc[k].evtArray = cmMemAllocZ(cmScoreEvt_t*,p->loc[k].evtCnt);
  1137. // fill in the location record event pointers
  1138. for(j=0; j<p->loc[k].evtCnt; ++j)
  1139. {
  1140. p->loc[k].evtArray[j] = p->array + (i + j);
  1141. p->loc[k].evtArray[j]->locIdx = k;
  1142. if( p->array[i+j].type == kBarEvtScId )
  1143. barNumb = p->array[i+j].barNumb;
  1144. }
  1145. // fill in the location record
  1146. p->loc[k].secs = p->array[i].secs;
  1147. p->loc[k].barNumb = barNumb;
  1148. i += p->loc[k].evtCnt;
  1149. }
  1150. assert( p->locCnt == k );
  1151. return rc;
  1152. }
  1153. 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 )
  1154. {
  1155. cmScRC_t rc = kOkScRC;
  1156. if((rc = cmScoreFinalize(hp)) != kOkScRC )
  1157. return rc;
  1158. cmSc_t* p = cmMemAllocZ(cmSc_t,1);
  1159. cmErrSetup(&p->err,&ctx->rpt,"Score");
  1160. p->stH = stH;
  1161. if((rc = _cmScParseFile(p,ctx,fn)) != kOkScRC )
  1162. goto errLabel;
  1163. if((rc = _cmScInitLocArray(p)) != kOkScRC )
  1164. goto errLabel;
  1165. if((rc = _cmScProcSections(p,p->sectList)) != kOkScRC )
  1166. goto errLabel;
  1167. if((rc = _cmScProcMarkers(p)) != kOkScRC )
  1168. goto errLabel;
  1169. // load the dynamic reference array
  1170. if( dynRefArray != NULL && dynRefCnt > 0)
  1171. {
  1172. unsigned n = _cmScDynLabelCount();
  1173. if( dynRefCnt != n )
  1174. {
  1175. rc = cmErrMsg(&p->err,kInvalidDynRefCntScRC,"The count of dynamics labels must be %i not %i.",n,dynRefCnt);
  1176. goto errLabel;
  1177. }
  1178. p->dynRefArray = cmMemAllocZ(unsigned,dynRefCnt);
  1179. memcpy(p->dynRefArray,dynRefArray,sizeof(unsigned)*dynRefCnt);
  1180. p->dynRefCnt = dynRefCnt;
  1181. }
  1182. p->srate = srate;
  1183. p->cbFunc = cbFunc;
  1184. p->cbArg = cbArg;
  1185. p->fn = cmMemAllocStr(fn);
  1186. p->nxtLocIdx = 0;
  1187. p->minSetLocIdx = cmInvalidIdx;
  1188. p->maxSetLocIdx = cmInvalidIdx;
  1189. hp->h = p;
  1190. cmScoreClearPerfInfo(*hp);
  1191. //cmScorePrintLoc(*hp);
  1192. errLabel:
  1193. if( rc != kOkScRC )
  1194. _cmScFinalize(p);
  1195. return rc;
  1196. }
  1197. cmScRC_t cmScoreFinalize( cmScH_t* hp )
  1198. {
  1199. cmScRC_t rc = kOkScRC;
  1200. if( hp == NULL || cmScoreIsValid(*hp) == false )
  1201. return kOkScRC;
  1202. cmSc_t* p = _cmScHandleToPtr(*hp);
  1203. if((rc = _cmScFinalize(p)) != kOkScRC )
  1204. return rc;
  1205. hp->h = NULL;
  1206. return rc;
  1207. }
  1208. const cmChar_t* cmScoreFileName( cmScH_t h )
  1209. {
  1210. cmSc_t* p = _cmScHandleToPtr(h);
  1211. return p->fn;
  1212. }
  1213. double cmScoreSampleRate( cmScH_t h )
  1214. {
  1215. cmSc_t* p = _cmScHandleToPtr(h);
  1216. return p->srate;
  1217. }
  1218. bool cmScoreIsValid( cmScH_t h )
  1219. { return h.h != NULL; }
  1220. unsigned cmScoreEvtCount( cmScH_t h )
  1221. {
  1222. cmSc_t* p = _cmScHandleToPtr(h);
  1223. return p->cnt;
  1224. }
  1225. cmScoreEvt_t* cmScoreEvt( cmScH_t h, unsigned idx )
  1226. {
  1227. cmSc_t* p = _cmScHandleToPtr(h);
  1228. if( idx >= p->cnt )
  1229. {
  1230. cmErrMsg(&p->err,kInvalidIdxScRC,"%i is an invalid index for %i records.",idx,p->cnt);
  1231. return NULL;
  1232. }
  1233. return p->array + idx;
  1234. }
  1235. cmScoreEvt_t* cmScoreBarEvt( cmScH_t h, unsigned barNumb )
  1236. {
  1237. cmSc_t* p = _cmScHandleToPtr(h);
  1238. unsigned i = 0;
  1239. for(; i<p->cnt; ++i)
  1240. if( p->array[i].type==kBarEvtScId && p->array[i].barNumb==barNumb )
  1241. return p->array + i;
  1242. return NULL;
  1243. }
  1244. unsigned cmScoreSectionCount( cmScH_t h )
  1245. {
  1246. cmSc_t* p = _cmScHandleToPtr(h);
  1247. return p->sectCnt;
  1248. }
  1249. cmScoreSection_t* cmScoreSection( cmScH_t h, unsigned idx )
  1250. {
  1251. cmSc_t* p = _cmScHandleToPtr(h);
  1252. assert( idx < p->sectCnt);
  1253. return p->sect + idx;
  1254. }
  1255. unsigned cmScoreLocCount( cmScH_t h )
  1256. {
  1257. cmSc_t* p = _cmScHandleToPtr(h);
  1258. return p->locCnt;
  1259. }
  1260. cmScoreLoc_t* cmScoreLoc( cmScH_t h, unsigned idx )
  1261. {
  1262. cmSc_t* p = _cmScHandleToPtr(h);
  1263. if( idx >= p->locCnt )
  1264. {
  1265. cmErrMsg(&p->err,kInvalidIdxScRC,"%i is an invalid index for %i location records.",idx,p->locCnt);
  1266. return NULL;
  1267. }
  1268. return p->loc + idx;
  1269. }
  1270. cmScoreLoc_t* cmScoreEvtLoc( cmScH_t h, const cmScoreEvt_t* evt )
  1271. {
  1272. cmSc_t* p = _cmScHandleToPtr(h);
  1273. unsigned i;
  1274. if( evt != NULL )
  1275. for(i=0; i<p->locCnt; ++i)
  1276. {
  1277. unsigned j;
  1278. for(j=0; j<p->loc[i].evtCnt; ++j)
  1279. if( p->loc[i].evtArray[j] == evt )
  1280. return p->loc + i;
  1281. }
  1282. return NULL;
  1283. }
  1284. void cmScorePrintLoc( cmScH_t h )
  1285. {
  1286. unsigned i = 0;
  1287. cmSc_t* p = _cmScHandleToPtr(h);
  1288. unsigned colCnt = 10;
  1289. int bufCharCnt = 4;
  1290. char buf[ bufCharCnt ];
  1291. const char* emptyStr = " ";
  1292. // for each set of 'colCnt' columns
  1293. for(i=0; i<p->locCnt; i+=colCnt )
  1294. {
  1295. // print the location 'index' line
  1296. unsigned c,j,k;
  1297. printf("index: ");
  1298. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1299. printf("%7i ",j);
  1300. printf("\n");
  1301. // print the 'sectn' label line
  1302. printf("sectn: ");
  1303. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1304. if( p->loc[j].begSectPtr==NULL )
  1305. printf("%s",emptyStr);
  1306. else
  1307. printf("%7s ",p->loc[j].begSectPtr->label);
  1308. printf("\n");
  1309. // calculate the max number of simultan. events at any one location
  1310. // for this set of 'colCnt' columns.
  1311. unsigned n=0;
  1312. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1313. if( p->loc[j].evtCnt > n )
  1314. n = p->loc[j].evtCnt;
  1315. // for each 'sco' line
  1316. for(k=0; k<n; ++k)
  1317. {
  1318. printf("sco%2i: ",k);
  1319. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1320. if( k >= p->loc[j].evtCnt )
  1321. printf("%s",emptyStr);
  1322. else
  1323. {
  1324. switch(p->loc[j].evtArray[k]->type)
  1325. {
  1326. case kBarEvtScId:
  1327. printf(" |%3i ",p->loc[j].evtArray[k]->barNumb);
  1328. break;
  1329. case kNonEvtScId:
  1330. {
  1331. int bn=16;
  1332. char b[bn];
  1333. strcpy(b,cmMidiToSciPitch(p->loc[j].evtArray[k]->pitch,NULL,0));
  1334. strcat(b,_cmScFlagsToStr( p->loc[j].evtArray[k]->flags,buf,bufCharCnt));
  1335. assert(strlen(b)<bn);
  1336. printf("%7s ",b);
  1337. break;
  1338. }
  1339. }
  1340. }
  1341. printf("\n");
  1342. }
  1343. // calc the max number of set triggers which will occur on
  1344. // any one location for this set of 'colCnt' columns.
  1345. n=0;
  1346. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1347. if(p->loc[j].begSectPtr != NULL && p->loc[j].begSectPtr->setCnt > n )
  1348. n = p->loc[j].begSectPtr->setCnt;
  1349. for(k=0; k<n; ++k)
  1350. {
  1351. printf("trig%1i: ",k);
  1352. for(c=0,j=i; j<p->locCnt && c<colCnt; ++c,++j)
  1353. {
  1354. if( p->loc[j].begSectPtr != NULL && k<p->loc[j].begSectPtr->setCnt )
  1355. {
  1356. const cmScoreSet_t* sp = p->loc[j].begSectPtr->setArray[k];
  1357. printf(" %3s-%c ",p->loc[j].begSectPtr->label,_cmScVarIdToChar(sp->varId) );
  1358. }
  1359. else
  1360. {
  1361. printf("%s",emptyStr);
  1362. }
  1363. }
  1364. printf("\n");
  1365. }
  1366. printf("\n");
  1367. }
  1368. }
  1369. unsigned cmScoreSetCount( cmScH_t h )
  1370. {
  1371. cmSc_t* p = _cmScHandleToPtr(h);
  1372. return p->setCnt;
  1373. }
  1374. unsigned cmScoreMarkerLabelCount( cmScH_t h )
  1375. {
  1376. cmSc_t* p = _cmScHandleToPtr(h);
  1377. return p->markLabelCnt;
  1378. }
  1379. unsigned cmScoreMarkerLabelSymbolId( cmScH_t h, unsigned idx )
  1380. {
  1381. cmSc_t* p = _cmScHandleToPtr(h);
  1382. assert( idx < p->markLabelCnt );
  1383. return p->markLabelArray[idx];
  1384. }
  1385. const cmScoreMarker_t* cmScoreMarker( cmScH_t h, cmMarkScMId_t markMId, unsigned labelSymId )
  1386. {
  1387. cmSc_t* p = _cmScHandleToPtr(h);
  1388. const cmScoreMarker_t* smp = NULL;
  1389. if( _cmScFindMarkLoc(p, markMId, labelSymId, &smp ) == NULL )
  1390. return NULL;
  1391. return smp;
  1392. }
  1393. cmScRC_t cmScoreSeqNotify( cmScH_t h )
  1394. {
  1395. cmScRC_t rc = kOkScRC;
  1396. cmSc_t* p = _cmScHandleToPtr(h);
  1397. cmScMsg_t m;
  1398. unsigned i;
  1399. if( p->cbFunc != NULL )
  1400. {
  1401. memset(&m.u.evt,0,sizeof(m.u.evt));
  1402. m.typeId = kBeginMsgScId;
  1403. p->cbFunc(p->cbArg,&m,sizeof(m));
  1404. m.typeId = kEventMsgScId;
  1405. for(i=0; i<p->cnt; ++i)
  1406. {
  1407. m.u.evt = p->array[i];
  1408. p->cbFunc(p->cbArg,&m,sizeof(m));
  1409. }
  1410. m.typeId = kSectionMsgScId;
  1411. for(i=0; i<p->sectCnt; ++i)
  1412. {
  1413. m.u.sect = p->sect[i];
  1414. p->cbFunc(p->cbArg,&m,sizeof(m));
  1415. }
  1416. memset(&m.u.evt,0,sizeof(m.u.evt));
  1417. m.typeId = kEndMsgScId;
  1418. p->cbFunc(p->cbArg,&m,sizeof(m));
  1419. }
  1420. return rc;
  1421. }
  1422. void cmScoreClearPerfInfo( cmScH_t h )
  1423. {
  1424. cmSc_t* p = _cmScHandleToPtr(h);
  1425. unsigned i;
  1426. for(i=0; i<p->cnt; ++i)
  1427. {
  1428. p->array[i].perfSmpIdx = cmInvalidIdx;
  1429. p->array[i].perfVel = 0;
  1430. p->array[i].perfDynLvl = 0;
  1431. }
  1432. for(i=0; i<p->locCnt; ++i)
  1433. {
  1434. cmScoreSet_t* sp = p->loc[i].setList;
  1435. for(; sp!=NULL; sp=sp->llink)
  1436. sp->doneFl = false;
  1437. }
  1438. for(i=0; i<p->sectCnt; ++i)
  1439. {
  1440. unsigned j;
  1441. for(j=0; j<kScVarCnt; ++j)
  1442. p->sect[i].vars[j] = DBL_MAX;
  1443. }
  1444. p->nxtLocIdx = 0;
  1445. p->minSetLocIdx = cmInvalidIdx;
  1446. p->maxSetLocIdx = cmInvalidIdx;
  1447. }
  1448. bool _cmScIsSetPerfDone( cmScoreSet_t* sp )
  1449. {
  1450. unsigned i = 0;
  1451. for(i=0; i<sp->eleCnt; ++i)
  1452. if( sp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1453. return false;
  1454. return true;
  1455. }
  1456. void _cmScPerfSortTimes( unsigned *v, unsigned n )
  1457. {
  1458. unsigned i;
  1459. bool fl = true;
  1460. while(fl && n)
  1461. {
  1462. fl = false;
  1463. for(i=1; i<n; ++i)
  1464. {
  1465. if( v[i-1] > v[i] )
  1466. {
  1467. unsigned t = v[i-1];
  1468. v[i-1] = v[i];
  1469. v[i] = t;
  1470. fl = true;
  1471. }
  1472. }
  1473. --n;
  1474. }
  1475. }
  1476. bool _cmScPerfEven(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1477. {
  1478. unsigned i = 0;
  1479. double u = 0;
  1480. double x = 0;
  1481. bool sortFl = false;
  1482. bool printFl = true;
  1483. unsigned v[ stp->eleCnt ];
  1484. unsigned d[ stp->eleCnt - 1];
  1485. assert( stp->eleCnt > 1 );
  1486. // calculate the sum of the time between events
  1487. for(i=0; i<stp->eleCnt; ++i)
  1488. {
  1489. // if this event was not received - then the set is not valid
  1490. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1491. {
  1492. if( printFl && printMissFl)
  1493. printf("EVENESS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1494. return false;
  1495. }
  1496. // load v[i]
  1497. v[i] = stp->eleArray[i]->perfSmpIdx;
  1498. // check for out of order elements
  1499. if( i> 0 )
  1500. if( v[i] < v[i-1] )
  1501. sortFl = true;
  1502. }
  1503. // sort the times in ascending order
  1504. if( sortFl )
  1505. _cmScPerfSortTimes(v,stp->eleCnt);
  1506. // calc the sum of time differences
  1507. for(i=1; i<stp->eleCnt; ++i)
  1508. u += d[i-1] = v[i] - v[i-1];
  1509. // calculate the average time between events
  1510. u /= stp->eleCnt-1;
  1511. // calculate the std-dev of the time between events
  1512. for(i=0; i<stp->eleCnt-1; ++i)
  1513. x += (d[i]-u)*(d[i]-u);
  1514. double sd = sqrt(x/(stp->eleCnt-1));
  1515. // compute the average z-score
  1516. double c = 0;
  1517. for(i=0; i<stp->eleCnt-1; ++i)
  1518. c += fabs(d[i]-u)/sd;
  1519. stp->value = c/(stp->eleCnt-1);
  1520. stp->doneFl = true;
  1521. if(printFl)
  1522. {
  1523. /*
  1524. for(i=0; i<stp->eleCnt; ++i)
  1525. {
  1526. printf("%i %i ",i,v[i]);
  1527. if( i > 0 )
  1528. printf("%i ", d[i-1]);
  1529. printf("\n");
  1530. }
  1531. */
  1532. printf("%s EVENESS:%f\n",sortFl?"SORTED ":"",stp->value);
  1533. }
  1534. return true;
  1535. }
  1536. bool _cmScPerfDyn( cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1537. {
  1538. double a = 0;
  1539. unsigned i = 0;
  1540. bool printFl = true;
  1541. for(i=0; i<stp->eleCnt; ++i)
  1542. {
  1543. unsigned j;
  1544. // if this event was not received - then the set is not valid
  1545. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1546. {
  1547. if( printFl && printMissFl )
  1548. printf("DYNAMICS: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1549. return false;
  1550. }
  1551. unsigned m = 0; // lower bound for the first dyn. category
  1552. // determine the dynamic category for the performed velocity of each event
  1553. for(j=0; j<p->dynRefCnt; ++j)
  1554. {
  1555. // if the vel fall's into the jth dyn. category
  1556. if( m <= stp->eleArray[i]->perfVel && stp->eleArray[i]->perfVel < p->dynRefArray[j] )
  1557. break;
  1558. // store the min vel for the next dyn category
  1559. m = p->dynRefArray[j];
  1560. }
  1561. assert( j < p->dynRefCnt );
  1562. stp->eleArray[i]->perfDynLvl = j+1;
  1563. a += abs((j+1) - stp->eleArray[i]->dynVal);
  1564. if( p->cbFunc != NULL )
  1565. {
  1566. cmScMsg_t m;
  1567. m.typeId = kDynMsgScId;
  1568. m.u.dyn.evtIdx = stp->eleArray[i]->index;
  1569. m.u.dyn.dynLvl = stp->eleArray[i]->perfDynLvl;
  1570. p->cbFunc(p->cbArg,&m,sizeof(m));
  1571. }
  1572. }
  1573. stp->value = a / stp->eleCnt;
  1574. stp->doneFl = true;
  1575. if( printFl )
  1576. printf("DYNAMICS:%f\n",stp->value);
  1577. return true;
  1578. }
  1579. bool _cmScPerfTempo1(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1580. {
  1581. bool printFl = true;
  1582. unsigned durSmpCnt = 0;
  1583. double durBeats = 0;
  1584. int i;
  1585. for(i=0; i<stp->eleCnt; ++i)
  1586. {
  1587. // if this event was not received - then the set is not valid
  1588. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1589. {
  1590. if( printFl && printMissFl )
  1591. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1592. return false;
  1593. }
  1594. if( i > 0 )
  1595. durSmpCnt += stp->eleArray[i]->perfSmpIdx - stp->eleArray[i-1]->perfSmpIdx;
  1596. durBeats += stp->eleArray[i]->frac;
  1597. }
  1598. stp->value = durBeats / (durSmpCnt / p->srate*60.0 );
  1599. stp->doneFl = true;
  1600. if( printFl )
  1601. printf("TEMPO:%f\n",stp->value);
  1602. return true;
  1603. }
  1604. bool _cmScPerfTempo(cmSc_t* p, cmScoreSet_t* stp, bool printMissFl)
  1605. {
  1606. bool printFl = true;
  1607. unsigned durSmpCnt = 0;
  1608. double durBeats = 0;
  1609. int i;
  1610. unsigned bi = cmInvalidIdx;
  1611. unsigned ei = cmInvalidIdx;
  1612. unsigned missCnt = 0;
  1613. for(i=0; i<stp->eleCnt; ++i)
  1614. {
  1615. // if this event was not received - then the set is not valid
  1616. if( stp->eleArray[i]->perfSmpIdx == cmInvalidIdx )
  1617. {
  1618. ++missCnt;
  1619. if( printFl && printMissFl )
  1620. printf("TEMPO: missing loc:%i %s\n",stp->eleArray[i]->locIdx,cmMidiToSciPitch(stp->eleArray[i]->pitch,NULL,0));
  1621. }
  1622. else
  1623. {
  1624. if( bi == cmInvalidIdx )
  1625. bi = i;
  1626. ei = i;
  1627. }
  1628. }
  1629. if( ei > bi )
  1630. {
  1631. for(i=bi; i<=ei; ++i)
  1632. durBeats += stp->eleArray[i]->frac;
  1633. durSmpCnt = stp->eleArray[ei]->perfSmpIdx - stp->eleArray[bi]->perfSmpIdx;
  1634. stp->value = durBeats / (durSmpCnt / (p->srate*60.0) );
  1635. stp->doneFl = true;
  1636. }
  1637. if( printFl )
  1638. printf("TEMPO:%f bi:%i ei:%i secs:%f bts:%f\n",stp->value,bi,ei,durSmpCnt/p->srate,durBeats);
  1639. return true;
  1640. }
  1641. void _cmScPerfExec( cmSc_t* p, cmScoreSet_t* sp, bool printMissFl )
  1642. {
  1643. if( sp->doneFl == false )
  1644. {
  1645. switch( sp->varId )
  1646. {
  1647. case kEvenVarScId:
  1648. _cmScPerfEven(p,sp,printMissFl);
  1649. break;
  1650. case kDynVarScId:
  1651. _cmScPerfDyn(p,sp,printMissFl);
  1652. break;
  1653. case kTempoVarScId:
  1654. _cmScPerfTempo(p,sp,printMissFl);
  1655. break;
  1656. default:
  1657. { assert(0); }
  1658. }
  1659. }
  1660. }
  1661. void _cmScPerfExecRange( cmSc_t* p )
  1662. {
  1663. if( p->minSetLocIdx == cmInvalidIdx || p->maxSetLocIdx==cmInvalidIdx )
  1664. return;
  1665. unsigned i = p->minSetLocIdx;
  1666. for(; i<=p->maxSetLocIdx; ++i)
  1667. {
  1668. cmScoreSet_t* sp = p->loc[i].setList;
  1669. for(; sp!=NULL; sp=sp->llink)
  1670. _cmScPerfExec(p,sp,true);
  1671. }
  1672. }
  1673. bool _cmScSetPerfEvent( cmSc_t* p, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1674. {
  1675. assert(locIdx < p->locCnt );
  1676. cmScoreLoc_t* lp = p->loc + locIdx;
  1677. bool doneFl = true;
  1678. unsigned i;
  1679. #ifndef NDEBUG
  1680. bool foundFl = false;
  1681. #endif
  1682. // locate the event at the loc[locIdx]
  1683. for(i=0; i<lp->evtCnt; ++i)
  1684. {
  1685. cmScoreEvt_t* ep = lp->evtArray[i];
  1686. if( ep->type == kNonEvtScId )
  1687. {
  1688. if( ep->pitch == pitch )
  1689. {
  1690. assert( ep->perfSmpIdx == cmInvalidIdx );
  1691. ep->perfSmpIdx = smpIdx;
  1692. ep->perfVel = vel;
  1693. #ifndef NDEBUG
  1694. foundFl = true;
  1695. #endif
  1696. }
  1697. // check if all notes have arrived for this location
  1698. if( ep->perfSmpIdx == cmInvalidIdx )
  1699. doneFl = false;
  1700. }
  1701. }
  1702. // the event must always be found
  1703. assert( foundFl );
  1704. return doneFl;
  1705. }
  1706. bool cmScoreSetPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1707. {
  1708. cmSc_t* p = _cmScHandleToPtr(h);
  1709. return _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1710. }
  1711. void cmScoreExecPerfEvent( cmScH_t h, unsigned locIdx, unsigned smpIdx, unsigned pitch, unsigned vel )
  1712. {
  1713. unsigned i;
  1714. cmSc_t* p = _cmScHandleToPtr(h);
  1715. bool doneFl = _cmScSetPerfEvent(p,locIdx,smpIdx,pitch,vel);
  1716. unsigned printLvl = 1;
  1717. cmScoreLoc_t* lp = p->loc + locIdx;
  1718. // all events for a location must be complete to trigger attached events
  1719. if( doneFl == false )
  1720. return;
  1721. if( p->loc[locIdx].setList != NULL )
  1722. {
  1723. // set idx of most recent loc w/ a set end event
  1724. p->maxSetLocIdx = locIdx;
  1725. if( p->minSetLocIdx == cmInvalidIdx )
  1726. p->minSetLocIdx = locIdx;
  1727. }
  1728. // attempt to calculate all sets between loc[p->minSetLocIdx] and loc[p->maxSetLocIdx]
  1729. _cmScPerfExecRange(p);
  1730. // prevent event retriggering or going backwards
  1731. if( printLvl && locIdx < p->nxtLocIdx )
  1732. {
  1733. printf("----- BACK ----- \n");
  1734. return;
  1735. }
  1736. if( printLvl && locIdx > p->nxtLocIdx )
  1737. {
  1738. printf("----- SKIP ----- \n");
  1739. }
  1740. // for each location between the current and previous location
  1741. for(; p->nxtLocIdx<=locIdx; ++p->nxtLocIdx)
  1742. {
  1743. lp = p->loc + p->nxtLocIdx;
  1744. // if this location is the start of a new section - then apply
  1745. // sets that are assigned to this section
  1746. if( lp->begSectPtr != NULL && lp->begSectPtr->setCnt > 0 )
  1747. {
  1748. // notice the location of the oldest section start - once we cross this point
  1749. // it is too late to notice set completions - so incr p->inSetLocIdx
  1750. if( lp->begSectPtr->setCnt )
  1751. p->minSetLocIdx = p->nxtLocIdx+1;
  1752. for(i=0; i<lp->begSectPtr->setCnt; ++i)
  1753. {
  1754. cmScoreSet_t* stp = lp->begSectPtr->setArray[i];
  1755. // temporarily commented out for testing purposes
  1756. // if( stp->doneFl == false )
  1757. // _cmScPerfExec(p, stp, printLvl>0 );
  1758. if( stp->doneFl )
  1759. {
  1760. assert( stp->varId < kScVarCnt );
  1761. lp->begSectPtr->vars[ stp->varId ] = stp->value;
  1762. if( p->cbFunc != NULL )
  1763. {
  1764. cmScMsg_t m;
  1765. m.typeId = kVarMsgScId;
  1766. m.u.meas.varId = stp->varId;
  1767. m.u.meas.value = stp->value;
  1768. p->cbFunc(p->cbArg,&m,sizeof(m));
  1769. }
  1770. }
  1771. }
  1772. }
  1773. }
  1774. }
  1775. void cmScoreSetPerfValue( cmScH_t h, unsigned locIdx, unsigned varId, double value )
  1776. {
  1777. cmSc_t* p = _cmScHandleToPtr(h);
  1778. int li = locIdx;
  1779. for(; li>=0; --li)
  1780. if( p->loc[li].begSectPtr != NULL )
  1781. {
  1782. assert( varId < kScVarCnt );
  1783. p->loc[li].begSectPtr->vars[varId] = value;
  1784. break;
  1785. }
  1786. assert( li>=0);
  1787. }
  1788. void cmScoreSetPerfDynLevel( cmScH_t h, unsigned evtIdx, unsigned dynLvl )
  1789. {
  1790. cmSc_t* p = _cmScHandleToPtr(h);
  1791. assert(evtIdx < p->cnt );
  1792. p->array[ evtIdx ].perfDynLvl = dynLvl;
  1793. }
  1794. cmScRC_t cmScoreDecode( const void* msg, unsigned msgByteCnt, cmScMsg_t* m)
  1795. {
  1796. cmScMsg_t* mp = (cmScMsg_t*)msg;
  1797. *m = *mp;
  1798. return kOkScRC;
  1799. }
  1800. void cmScorePrint( cmScH_t h, cmRpt_t* rpt )
  1801. {
  1802. cmSc_t* p = _cmScHandleToPtr(h);
  1803. unsigned i;
  1804. for(i=0; i<p->cnt; ++i)
  1805. {
  1806. cmScoreEvt_t* r = p->array + i;
  1807. switch(r->type)
  1808. {
  1809. case kBarEvtScId:
  1810. cmRptPrintf(rpt,"%5i %5i %3i bar\n",
  1811. i,
  1812. r->line,
  1813. r->barNumb );
  1814. break;
  1815. case kNonEvtScId:
  1816. cmRptPrintf(rpt,"%5i %5i %3i %3i %s %5s %c%c%c %s\n",
  1817. i,
  1818. r->line,
  1819. r->barNumb,
  1820. r->barNoteIdx,
  1821. cmScEvtTypeIdToLabel(r->type),
  1822. cmMidiToSciPitch(r->pitch,NULL,0),
  1823. cmIsFlag(r->flags,kEvenScFl) ? 'e' : ' ',
  1824. cmIsFlag(r->flags,kTempoScFl) ? 't' : ' ',
  1825. cmIsFlag(r->flags,kDynScFl) ? 'd' : ' ',
  1826. cmIsFlag(r->flags,kDynScFl) ? cmScDynIdToLabel(r->dynVal) : "");
  1827. break;
  1828. default:
  1829. break;
  1830. }
  1831. }
  1832. }
  1833. cmScRC_t cmScoreFileFromMidi( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* scoreFn )
  1834. {
  1835. cmScRC_t rc = kOkScRC;
  1836. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  1837. cmCsvH_t csvH = cmCsvNullHandle;
  1838. cmErr_t err;
  1839. 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 };
  1840. cmErrSetup(&err,&ctx->rpt,"MIDI to Score");
  1841. if( cmMidiFileOpen(midiFn, &mfH, ctx ) != kOkMfRC )
  1842. return cmErrMsg(&err,kMidiFileFailScRC,"Unable to open the MIDI file '%s'.",midiFn);
  1843. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  1844. {
  1845. cmErrMsg(&err,kCsvFailScRC,"Unable to initialize the CSV file: '%s'.",scoreFn);
  1846. goto errLabel;
  1847. }
  1848. // Convert the track message 'dtick' field to delta-microseconds.
  1849. cmMidiFileTickToMicros(mfH);
  1850. unsigned msgCnt = cmMidiFileMsgCount(mfH);
  1851. unsigned i;
  1852. const cmMidiTrackMsg_t** tmpp = cmMidiFileMsgArray(mfH);
  1853. double acc_secs = 0;
  1854. unsigned lexTId = 0;
  1855. cmCsvCell_t* cp = NULL;
  1856. if( cmCsvAppendRow(csvH, &cp, cmCsvInsertSymText(csvH,titles[0]), kStrCsvTFl, lexTId ) != kOkCsvRC )
  1857. {
  1858. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'id' column in '%s'.",cmStringNullGuard(scoreFn));
  1859. goto errLabel;
  1860. }
  1861. for(i=1; titles[i]!=NULL; ++i)
  1862. {
  1863. if( cmCsvInsertTextColAfter(csvH, cp, &cp, titles[i], lexTId ) != kOkCsvRC )
  1864. {
  1865. cmErrMsg(&err,kCsvFailScRC,"Error inserting column index '%i' label in '%s'.",i,cmStringNullGuard(scoreFn));
  1866. goto errLabel;
  1867. }
  1868. }
  1869. for(i=0; i<msgCnt; ++i)
  1870. {
  1871. const cmMidiTrackMsg_t* tmp = tmpp[i];
  1872. const cmChar_t* opStr = NULL;
  1873. unsigned midiCh = 0;
  1874. unsigned d0 = 0;
  1875. unsigned d1 = 0;
  1876. unsigned metaId = 0;
  1877. double dsecs = (double)tmp->dtick / 1000000.0;
  1878. acc_secs += dsecs;
  1879. if( tmp->status == kMetaStId )
  1880. {
  1881. opStr = cmMidiMetaStatusToLabel(tmp->metaId);
  1882. metaId = tmp->metaId;
  1883. switch( tmp->metaId )
  1884. {
  1885. case kTempoMdId:
  1886. d0 = tmp->u.iVal;
  1887. }
  1888. }
  1889. else
  1890. {
  1891. opStr = cmMidiStatusToLabel(tmp->status);
  1892. if( cmMidiIsChStatus( tmp->status ) )
  1893. {
  1894. midiCh = tmp->u.chMsgPtr->ch;
  1895. d0 = tmp->u.chMsgPtr->d0;
  1896. d1 = tmp->u.chMsgPtr->d1;
  1897. }
  1898. }
  1899. cp = NULL;
  1900. // skip note-off messages
  1901. if( tmp->status == kNoteOffMdId )
  1902. continue;
  1903. if( cmCsvAppendRow(csvH, &cp, cmCsvInsertSymUInt(csvH,i), kIntCsvTFl, lexTId ) != kOkCsvRC )
  1904. {
  1905. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'id' column in '%s'.",cmStringNullGuard(scoreFn));
  1906. goto errLabel;
  1907. }
  1908. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->trkIdx, lexTId ) != kOkCsvRC )
  1909. {
  1910. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'trk' column in '%s'.",cmStringNullGuard(scoreFn));
  1911. goto errLabel;
  1912. }
  1913. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, 0, lexTId ) != kOkCsvRC )
  1914. {
  1915. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'evt' column in '%s'.",cmStringNullGuard(scoreFn));
  1916. goto errLabel;
  1917. }
  1918. if( cmCsvInsertTextColAfter(csvH, cp, &cp, opStr, lexTId ) != kOkCsvRC )
  1919. {
  1920. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'opcode' column in '%s'.",cmStringNullGuard(scoreFn));
  1921. goto errLabel;
  1922. }
  1923. if( cmCsvInsertDoubleColAfter(csvH, cp, &cp, dsecs, lexTId ) != kOkCsvRC )
  1924. {
  1925. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'dticks' column in '%s'.",cmStringNullGuard(scoreFn));
  1926. goto errLabel;
  1927. }
  1928. if( cmCsvInsertDoubleColAfter(csvH, cp, &cp, acc_secs, lexTId ) != kOkCsvRC )
  1929. {
  1930. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'micros' column in '%s'.",cmStringNullGuard(scoreFn));
  1931. goto errLabel;
  1932. }
  1933. if( cmCsvInsertHexColAfter(csvH, cp, &cp, tmp->status, lexTId ) != kOkCsvRC )
  1934. {
  1935. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'status' column in '%s'.",cmStringNullGuard(scoreFn));
  1936. goto errLabel;
  1937. }
  1938. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, metaId, lexTId ) != kOkCsvRC )
  1939. {
  1940. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'meta' column in '%s'.",cmStringNullGuard(scoreFn));
  1941. goto errLabel;
  1942. }
  1943. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, midiCh, lexTId ) != kOkCsvRC )
  1944. {
  1945. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'ch' column in '%s'.",cmStringNullGuard(scoreFn));
  1946. goto errLabel;
  1947. }
  1948. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, d0, lexTId ) != kOkCsvRC )
  1949. {
  1950. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'd0' column in '%s'.",cmStringNullGuard(scoreFn));
  1951. goto errLabel;
  1952. }
  1953. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, d1, lexTId ) != kOkCsvRC )
  1954. {
  1955. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'd1' column in '%s'.",cmStringNullGuard(scoreFn));
  1956. goto errLabel;
  1957. }
  1958. switch( tmp->status )
  1959. {
  1960. case kNoteOnMdId:
  1961. if( cmCsvInsertTextColAfter(csvH, cp, &cp, cmMidiToSciPitch(tmp->u.chMsgPtr->d0,NULL,0), lexTId ) != kOkCsvRC )
  1962. {
  1963. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'opcode' column in '%s'.",cmStringNullGuard(scoreFn));
  1964. goto errLabel;
  1965. }
  1966. case kMetaStId:
  1967. switch( tmp->metaId )
  1968. {
  1969. case kTimeSigMdId:
  1970. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->u.timeSigPtr->num, lexTId ) != kOkCsvRC )
  1971. {
  1972. cmErrMsg(&err,kCsvFailScRC,"Error inserting time sign. numerator column in '%s'.",cmStringNullGuard(scoreFn));
  1973. goto errLabel;
  1974. }
  1975. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, tmp->u.timeSigPtr->den, lexTId ) != kOkCsvRC )
  1976. {
  1977. cmErrMsg(&err,kCsvFailScRC,"Error inserting time sign. denominator column in '%s'.",cmStringNullGuard(scoreFn));
  1978. goto errLabel;
  1979. }
  1980. break;
  1981. case kTempoMdId:
  1982. if( cmCsvInsertUIntColAfter(csvH, cp, &cp, 60000000/tmp->u.iVal, lexTId ) != kOkCsvRC )
  1983. {
  1984. cmErrMsg(&err,kCsvFailScRC,"Error inserting 'tempo' in '%s'.",cmStringNullGuard(scoreFn));
  1985. goto errLabel;
  1986. }
  1987. break;
  1988. }
  1989. }
  1990. }
  1991. if( cmCsvWrite(csvH,scoreFn) != kOkCsvRC )
  1992. {
  1993. cmErrMsg(&err,kCsvFailScRC,"The score output file '%s' could not be written.",cmStringNullGuard(scoreFn));
  1994. goto errLabel;
  1995. }
  1996. errLabel:
  1997. cmMidiFileClose(&mfH);
  1998. cmCsvFinalize(&csvH);
  1999. return rc;
  2000. }
  2001. void cmScoreTest( cmCtx_t* ctx, const cmChar_t* fn )
  2002. {
  2003. cmScH_t h = cmScNullHandle;
  2004. if( cmScoreInitialize(ctx,&h,fn,0,NULL,0,NULL,NULL, cmSymTblNullHandle ) != kOkScRC )
  2005. return;
  2006. cmScorePrint(h,&ctx->rpt);
  2007. cmScoreFinalize(&h);
  2008. }
  2009. // 1. Fix absolute message time which was incorrect on original score file.
  2010. // 2.
  2011. void cmScoreFix( cmCtx_t* ctx )
  2012. {
  2013. const cmChar_t* mfn = "/home/kevin/src/cmgv/src/gv/data/ImaginaryThemes.mid";
  2014. const cmChar_t* crfn = "/home/kevin/src/cmgv/src/gv/data/mod0a.txt";
  2015. const cmChar_t* cwfn = "/home/kevin/src/cmgv/src/gv/data/mod1.csv";
  2016. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  2017. cmCsvH_t csvH = cmCsvNullHandle;
  2018. const cmMidiTrackMsg_t** msg = NULL;
  2019. double secs = 0.0;
  2020. int ci,mi,crn,mn;
  2021. bool errFl = true;
  2022. unsigned handCnt = 0;
  2023. unsigned midiMissCnt = 0;
  2024. if( cmCsvInitialize(&csvH,ctx) != kOkCsvRC )
  2025. goto errLabel;
  2026. if( cmCsvLexRegisterMatcher(csvH, cmCsvLexNextAvailId(csvH), _cmScLexSciPitchMatcher ) != kOkCsvRC )
  2027. goto errLabel;
  2028. if( cmCsvParseFile(csvH, crfn, 0 ) != kOkCsvRC )
  2029. goto errLabel;
  2030. if( cmMidiFileOpen(mfn,&mfH,ctx) != kOkMfRC )
  2031. goto errLabel;
  2032. cmMidiFileTickToMicros(mfH);
  2033. cmMidiFileCalcNoteDurations(mfH);
  2034. mn = cmMidiFileMsgCount(mfH);
  2035. msg = cmMidiFileMsgArray(mfH);
  2036. crn = cmCsvRowCount(csvH);
  2037. // for each row in the score file
  2038. for(ci=1,mi=0; ci<crn && cmCsvLastRC(csvH)==kOkCsvRC; ++ci)
  2039. {
  2040. unsigned id;
  2041. // zero the duration column
  2042. if( cmCsvCellPtr(csvH, ci, kDSecsColScIdx ) != NULL )
  2043. cmCsvSetCellUInt( csvH, ci, kDSecsColScIdx, 0 );
  2044. // get the MIDI file event id for this row
  2045. if((id = cmCsvCellUInt(csvH,ci,kMidiFileIdColScIdx)) == UINT_MAX)
  2046. {
  2047. // this is a hand-entered event - so it has no event id
  2048. ++handCnt;
  2049. }
  2050. else
  2051. {
  2052. for(; mi<mn; ++mi)
  2053. {
  2054. const cmMidiTrackMsg_t* m = msg[mi];
  2055. assert( mi+1 <= id );
  2056. secs += m->dtick/1000000.0;
  2057. if( mi+1 != id )
  2058. {
  2059. if( m->status == kNoteOnMdId && m->u.chMsgPtr->d1>0 )
  2060. {
  2061. // this MIDI note-on does not have a corresponding score event
  2062. ++midiMissCnt;
  2063. }
  2064. }
  2065. else
  2066. {
  2067. cmCsvSetCellDouble( csvH, ci, kSecsColScIdx, secs );
  2068. ++mi;
  2069. if( m->status == kNoteOnMdId )
  2070. cmCsvSetCellDouble( csvH, ci, kDSecsColScIdx, m->u.chMsgPtr->durTicks/1000000.0 );
  2071. break;
  2072. }
  2073. }
  2074. if( mi==mn)
  2075. printf("done on row:%i\n",ci);
  2076. }
  2077. }
  2078. if( cmCsvLastRC(csvH) != kOkCsvRC )
  2079. goto errLabel;
  2080. if( cmCsvWrite(csvH,cwfn) != kOkCsvRC )
  2081. goto errLabel;
  2082. errFl = false;
  2083. errLabel:
  2084. if( errFl )
  2085. printf("Score fix failed.\n");
  2086. else
  2087. printf("Score fix done! hand:%i miss:%i\n",handCnt,midiMissCnt);
  2088. cmMidiFileClose(&mfH);
  2089. cmCsvFinalize(&csvH);
  2090. }