libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmScore.c 67KB

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