libcm is a C development framework with an emphasis on audio signal processing applications.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cmScore.c 72KB

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