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 70KB

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