libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmScore.c 74KB

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