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

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