libcm is a C development framework with an emphasis on audio signal processing applications.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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