libcm is a C development framework with an emphasis on audio signal processing applications.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cmXScore.c 69KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmComplexTypes.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLinkedHeap.h"
  11. #include "cmXml.h"
  12. #include "cmText.h"
  13. #include "cmXScore.h"
  14. #include "cmTime.h"
  15. #include "cmMidi.h"
  16. #include "cmMidiFile.h"
  17. #include "cmLex.h"
  18. #include "cmCsv.h"
  19. #include "cmSymTbl.h"
  20. #include "cmScore.h"
  21. #include "cmFile.h"
  22. #include "cmSymTbl.h"
  23. #include "cmAudioFile.h"
  24. #include "cmAudioFile.h"
  25. #include "cmProcObj.h"
  26. #include "cmProcTemplate.h"
  27. #include "cmProc.h"
  28. #include "cmProc2.h"
  29. #include "cmProc5.h"
  30. cmXsH_t cmXsNullHandle = cmSTATIC_NULL_HANDLE;
  31. enum
  32. {
  33. kSectionXsFl = 0x000001, // rvalue holds section number
  34. kBarXsFl = 0x000002,
  35. kRestXsFl = 0x000004,
  36. kGraceXsFl = 0x000008,
  37. kDotXsFl = 0x000010,
  38. kChordXsFl = 0x000020,
  39. kDynXsFl = 0x000040,
  40. kEvenXsFl = 0x000080,
  41. kTempoXsFl = 0x000100,
  42. kHeelXsFl = 0x000200,
  43. kTieBegXsFl = 0x000400,
  44. kTieEndXsFl = 0x000800,
  45. kTieProcXsFl = 0x001000,
  46. kPedalDnXsFl = 0x002000,
  47. kPedalUpXsFl = 0x004000,
  48. kPedalUpDnXsFl = 0x008000,
  49. kSostDnXsFl = 0x010000,
  50. kSostUpXsFl = 0x020000,
  51. kMetronomeXsFl = 0x040000, // duration holds BPM
  52. kOnsetXsFl = 0x080000, // this is a sounding note
  53. kBegGroupXsFl = 0x100000,
  54. kEndGroupXsFl = 0x200000
  55. };
  56. struct cmXsMeas_str;
  57. struct cmXsVoice_str;
  58. typedef struct cmXsNote_str
  59. {
  60. unsigned uid; // unique id of this note record
  61. unsigned flags; // See k???XsFl
  62. unsigned pitch; // midi pitch
  63. unsigned velocity; // midi velocity
  64. unsigned dynamics; // dynamic level 1=pppp 9=fff
  65. unsigned vel; // score specified MIDI velocity
  66. cmChar_t step; // A-G
  67. unsigned octave; // sci pitch octave
  68. int alter; // +n=sharps,-n=flats
  69. unsigned staff; // 1=treble 2=bass
  70. unsigned tick; //
  71. unsigned duration; // duration in ticks
  72. double secs; // absolute time in seconds
  73. double dsecs; // delta time in seconds since previous event
  74. unsigned locIdx; // location index (chords share the same location index)
  75. double rvalue; // 1/rvalue = rythmic value (1/0.5 double whole 1/1 whole 1/2 half 1/4=quarter note, 1/8=eighth note, ...)
  76. const cmChar_t* tvalue; // text value
  77. unsigned evenGroupId; // eveness group id
  78. unsigned dynGroupId; // dynamics group id
  79. unsigned tempoGroupId; // tempo group id
  80. struct cmXsVoice_str* voice; // voice this note belongs to
  81. struct cmXsMeas_str* meas; // measure this note belongs to
  82. const cmXmlNode_t* xmlNode; // note xml ptr
  83. struct cmXsNote_str* mlink; // measure note list
  84. struct cmXsNote_str* slink; // time sorted event list
  85. } cmXsNote_t;
  86. typedef struct cmXsVoice_str
  87. {
  88. unsigned id; // Voice id
  89. cmXsNote_t* noteL; // List of notes in this voice
  90. struct cmXsVoice_str* link; // Link to other voices in this measure
  91. } cmXsVoice_t;
  92. typedef struct cmXsSpan_str
  93. {
  94. unsigned staff;
  95. unsigned number;
  96. struct cmXsMeas_str* meas;
  97. unsigned tick0;
  98. unsigned tick1;
  99. int pitch_offset;
  100. struct cmXsSpan_str* link;
  101. } cmXsSpan_t;
  102. typedef struct cmXsMeas_str
  103. {
  104. unsigned number; // Measure number
  105. unsigned divisions; // ticks-per-quarter-note
  106. unsigned beats; // beats per measure
  107. unsigned beat_type; // whole/half/quarter/eighth ...
  108. cmXsVoice_t* voiceL; // List of voices in this measure
  109. cmXsNote_t* noteL; // List of time sorted notes in this measure
  110. struct cmXsMeas_str* link; // Link to other measures in this part.
  111. } cmXsMeas_t;
  112. typedef struct cmXsPart_str
  113. {
  114. const cmChar_t* idStr; // Id of this part
  115. cmXsMeas_t* measL; // List of measures in this part.
  116. struct cmXsPart_str* link; // Link to other parts in this score
  117. } cmXsPart_t;
  118. typedef struct
  119. {
  120. cmErr_t err;
  121. cmXmlH_t xmlH;
  122. cmLHeapH_t lhH;
  123. cmXsPart_t* partL;
  124. cmCsvH_t csvH;
  125. cmXsSpan_t* spanL;
  126. unsigned nextUid;
  127. } cmXScore_t;
  128. cmXScore_t* _cmXScoreHandleToPtr( cmXsH_t h )
  129. {
  130. cmXScore_t* p = (cmXScore_t*)h.h;
  131. assert( p != NULL );
  132. return p;
  133. }
  134. cmXsRC_t _cmXScoreFinalize( cmXScore_t* p )
  135. {
  136. cmXsRC_t rc = kOkXsRC;
  137. // release the XML file
  138. cmXmlFree( &p->xmlH );
  139. // release the local linked heap memory
  140. cmLHeapDestroy(&p->lhH);
  141. // release the CSV output object
  142. cmCsvFinalize(&p->csvH);
  143. cmMemFree(p);
  144. return rc;
  145. }
  146. cmXsRC_t _cmXScoreMissingNode( cmXScore_t* p, const cmXmlNode_t* parent, const cmChar_t* label )
  147. {
  148. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML node '%s'. Parent line:%i",label,parent->line);
  149. }
  150. cmXsRC_t _cmXScoreMissingAttribute( cmXScore_t* p, const cmXmlNode_t* np, const cmChar_t* attrLabel )
  151. {
  152. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML attribute '%s' from node '%s'.",attrLabel,np->label);
  153. }
  154. cmXsVoice_t* _cmXScoreIdToVoice( cmXsMeas_t* meas, unsigned voiceId )
  155. {
  156. cmXsVoice_t* v = meas->voiceL;
  157. for(; v!=NULL; v=v->link)
  158. if( v->id == voiceId )
  159. return v;
  160. return NULL;
  161. }
  162. cmXsRC_t _cmXScorePushNote( cmXScore_t* p, cmXsMeas_t* meas, unsigned voiceId, cmXsNote_t* note )
  163. {
  164. cmXsVoice_t* v;
  165. // get the voice recd
  166. if((v = _cmXScoreIdToVoice(meas,voiceId)) == NULL)
  167. {
  168. // the voice recd doesn't exist for this voiceId - allocate one
  169. v = cmLhAllocZ(p->lhH,cmXsVoice_t,1);
  170. v->id = voiceId;
  171. // add the voice record to the meas->voiceL
  172. if( meas->voiceL == NULL )
  173. meas->voiceL = v;
  174. else
  175. {
  176. cmXsVoice_t* vp = meas->voiceL;
  177. while( vp->link!=NULL )
  178. vp = vp->link;
  179. vp->link = v;
  180. }
  181. }
  182. // add the note record to the end of meas->voiceL
  183. if( v->noteL == NULL )
  184. v->noteL = note;
  185. else
  186. {
  187. cmXsNote_t* n = v->noteL;
  188. while( n->mlink != NULL )
  189. n = n->mlink;
  190. n->mlink = note;
  191. }
  192. note->voice = v;
  193. note->uid = p->nextUid++;
  194. return kOkXsRC;
  195. }
  196. void _cmXScoreInsertNoteBefore( cmXsNote_t* note, cmXsNote_t* nn )
  197. {
  198. // insert the new note into the voice list before 'note'
  199. cmXsNote_t* n0 = NULL;
  200. cmXsNote_t* n1 = note->voice->noteL;
  201. for(; n1 != NULL; n1=n1->mlink )
  202. {
  203. if( n1->uid == note->uid )
  204. {
  205. if( n0 == NULL )
  206. note->voice->noteL = nn;
  207. else
  208. n0->mlink = nn;
  209. nn->mlink = note;
  210. break;
  211. }
  212. n0 = n1;
  213. }
  214. assert( n1 != NULL );
  215. // insert the new note into the time sorted note list before 'note'
  216. n0 = NULL;
  217. n1 = note->meas->noteL;
  218. for(; n1!=NULL; n1=n1->slink)
  219. {
  220. if( n1->tick >= nn->tick )
  221. {
  222. if( n0 == NULL )
  223. note->meas->noteL = nn;
  224. else
  225. n0->slink = nn;
  226. nn->slink = n1;
  227. break;
  228. }
  229. n0 = n1;
  230. }
  231. assert( n1 != NULL );
  232. }
  233. void _cmXScoreInsertNoteAfter( cmXsNote_t* note, cmXsNote_t* nn )
  234. {
  235. // insert the new note into the voice list after 'note'
  236. cmXsNote_t* n0 = note->voice->noteL;
  237. for(; n0 != NULL; n0=n0->mlink )
  238. if( n0->uid == note->uid )
  239. {
  240. nn->mlink = note->mlink;
  241. note->mlink = nn;
  242. break;
  243. }
  244. assert(n0 != NULL );
  245. // insert the new note into the time sorted note list after 'note'
  246. n0 = note->meas->noteL;
  247. for(; n0!=NULL; n0=n0->slink)
  248. {
  249. if( n0->tick >= nn->tick )
  250. {
  251. nn->slink = n0->slink;
  252. n0->slink = nn;
  253. break;
  254. }
  255. // if n0 is the last ele in the list
  256. if( n0->slink == NULL )
  257. {
  258. n0->slink = nn;
  259. nn->slink = NULL;
  260. break;
  261. }
  262. }
  263. assert(n0 != NULL);
  264. }
  265. cmXsRC_t _cmXScoreParsePartList( cmXScore_t* p )
  266. {
  267. cmXsRC_t rc = kOkXsRC;
  268. cmXsPart_t* lastPartPtr = NULL;
  269. const cmXmlNode_t* xnp;
  270. // find the 'part-list'
  271. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part-list", NULL, 0)) == NULL )
  272. return _cmXScoreMissingNode(p,cmXmlRoot(p->xmlH),"part-list");
  273. const cmXmlNode_t* cnp = xnp->children;
  274. // for each child of the 'part-list'
  275. for(; cnp!=NULL; cnp=cnp->sibling)
  276. if( cmTextCmp( cnp->label, "score-part" ) == 0 )
  277. {
  278. const cmXmlAttr_t* a;
  279. // find the 'score-part' id
  280. if((a = cmXmlFindAttrib(cnp,"id")) == NULL )
  281. return _cmXScoreMissingAttribute(p,cnp,"id");
  282. // allocate a new part record
  283. cmXsPart_t* pp = cmLhAllocZ(p->lhH,cmXsPart_t,1);
  284. pp->idStr = a->value; // set the part id
  285. // link the new part record to the end of the part list
  286. if(lastPartPtr == NULL)
  287. p->partL = pp;
  288. else
  289. lastPartPtr->link = pp;
  290. lastPartPtr = pp;
  291. }
  292. return rc;
  293. }
  294. cmXsRC_t _cmXScoreParsePitch( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* np )
  295. {
  296. cmXsRC_t rc = kOkXsRC;
  297. unsigned octave = 0;
  298. double alter = 0;
  299. const cmChar_t* step = NULL;
  300. if((step = cmXmlNodeValue(nnp,"pitch","step",NULL)) == NULL )
  301. return _cmXScoreMissingNode(p,nnp,"step");
  302. if((rc = cmXmlNodeUInt( nnp,&octave,"pitch","octave",NULL)) != kOkXmlRC )
  303. return _cmXScoreMissingNode(p,nnp,"octave");
  304. cmXmlNodeDouble( nnp,&alter,"pitch","alter",NULL);
  305. cmChar_t buf[3] = { *step, '0', '\0'};
  306. unsigned midi = cmSciPitchToMidi(buf);
  307. midi += (12 * octave);
  308. midi += alter;
  309. np->pitch = midi;
  310. np->step = *step;
  311. np->octave = octave;
  312. np->alter = alter;
  313. np->flags |= kOnsetXsFl;
  314. return rc;
  315. }
  316. cmXsRC_t _cmXScoreParseNoteRValue( cmXScore_t* p, const cmXmlNode_t* nnp, const cmChar_t* label, double* rvalueRef )
  317. {
  318. typedef struct map_str
  319. {
  320. double rvalue;
  321. const cmChar_t* label;
  322. } map_t;
  323. map_t mapV[] =
  324. {
  325. {-1.0, "measure" }, // whole measure rest
  326. { 0.5, "breve" }, // double whole
  327. { 1.0, "whole" },
  328. { 2.0, "half" },
  329. { 4.0, "quarter" },
  330. { 8.0, "eighth" },
  331. {16.0, "16th" },
  332. {32.0, "32nd" },
  333. { 64.0, "64th" },
  334. {128.0, "128th" },
  335. { 0.0, "" }
  336. };
  337. const cmChar_t* str;
  338. // get the XML rvalue label
  339. if((str = cmXmlNodeValue(nnp,label,NULL)) == NULL)
  340. {
  341. if((nnp = cmXmlSearch(nnp,"rest",NULL,0)) != NULL )
  342. {
  343. const cmXmlAttr_t* a;
  344. if((a = cmXmlFindAttrib(nnp,"measure")) != NULL && cmTextCmp(a->value,"yes")==0)
  345. {
  346. *rvalueRef = -1;
  347. return kOkXsRC;
  348. }
  349. }
  350. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'beat-unit' metronome value is missing on line %i.",nnp->line);
  351. }
  352. unsigned i;
  353. // lookup the rvalue numeric value from the mapV[] table
  354. for(i=0; mapV[i].rvalue!=0; ++i)
  355. if( cmTextCmp(mapV[i].label,str) == 0 )
  356. {
  357. *rvalueRef = mapV[i].rvalue;
  358. return kOkXsRC;
  359. }
  360. // the rvalue label was not found
  361. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unknown rvalue type='%s'.",str);
  362. }
  363. cmXsRC_t _cmXScoreParseColor( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* note )
  364. {
  365. cmXsRC_t rc = kOkXsRC;
  366. const cmXmlAttr_t* a;
  367. typedef struct map_str
  368. {
  369. unsigned value;
  370. const cmChar_t* label;
  371. } map_t;
  372. map_t mapV[] =
  373. {
  374. { kEvenXsFl, "#0000FF" }, // blue (even)
  375. { kTempoXsFl, "#00FF00" }, // green (tempo)
  376. { kDynXsFl, "#FF0000" }, // red (dynamics)
  377. { kTempoXsFl | kEvenXsFl, "#00FFFF" }, // green + blue (turquoise)
  378. { kDynXsFl | kEvenXsFl, "#FF00FF" }, // red + blue
  379. { kDynXsFl | kEvenXsFl, "#FF0CF7" }, // magenta (even+dyn)
  380. { kDynXsFl | kTempoXsFl, "#FF7F00" }, // red + green (brown)
  381. { kTempoXsFl | kEvenXsFl | kDynXsFl, "#996633" }, // (purple)
  382. { kDynXsFl, "#FF6A03" }, // 176 orange (dynamics)
  383. { kEvenXsFl, "#2F00E8" }, // 1001 blue (even)
  384. { kTempoXsFl, "#01CD1F" }, // 1196 green (tempo)
  385. { kEvenXsFl, "#3600E8" }, // 1627 blue (even)
  386. { kDynXsFl | kTempoXsFl, "#9E8F15" }, // 8827 brown (dyn + tempo)
  387. { kEvenXsFl, "#2E00E6" }, // 5393 blue (even)
  388. { kEvenXsFl, "#2C00DD" }, // 5895 blue (even)
  389. { kDynXsFl, "#FF5B03" }, // 6498 orange (dyn)
  390. { kDynXsFl, "#FF6104" }, // 6896 orange
  391. { kEvenXsFl, "#2A00E6" }, // 7781 blue
  392. { kEvenXsFl, "#2300DD" }, // 8300 blue (even)
  393. { kTempoXsFl, "#03CD22" }, // 10820 green (tempo)
  394. { kEvenXsFl, "#3400DB" }, // 11627 blue (dyn)
  395. { -1, "" }
  396. };
  397. /*
  398. orange #FF6A03
  399. magenta #FF0CF7
  400. blue #2F00E8
  401. green #01CD1F
  402. gold #9E8F15
  403. green #03CD22
  404. */
  405. if((a = cmXmlFindAttrib(nnp, "color" )) != NULL )
  406. {
  407. unsigned i;
  408. for(i=0; mapV[i].value != -1; ++i)
  409. if( cmTextCmp(a->value,mapV[i].label) == 0 )
  410. {
  411. note->flags += mapV[i].value;
  412. break;
  413. }
  414. if( mapV[i].value == -1 )
  415. cmErrMsg(&p->err,kSyntaxErrorXsRC,"The note color '%s' was not found on line %i.",a->value,nnp->line);
  416. }
  417. return rc;
  418. }
  419. // On input tick0Ref is set to the tick of the previous event.
  420. // On input tickRef is set to the tick of this event.
  421. // On output tick0Ref is set to the tick of this event.
  422. // On output tickRef is set to the tick of the next event.
  423. cmXsRC_t _cmXScoreParseNote(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* nnp, unsigned* tick0Ref, unsigned* tickRef )
  424. {
  425. cmXsRC_t rc = kOkXsRC;
  426. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  427. unsigned voiceId;
  428. note->meas = meas;
  429. note->xmlNode = nnp;
  430. // get the voice id for this node
  431. if( cmXmlNodeUInt(nnp,&voiceId,"voice",NULL) != kOkXmlRC )
  432. return _cmXScoreMissingNode(p,nnp,"voice");
  433. // if this note has a pitch
  434. if( cmXmlNodeHasChild(nnp,"pitch",NULL) )
  435. if((rc = _cmXScoreParsePitch(p,nnp,note)) != kOkXsRC )
  436. return rc;
  437. cmXmlNodeUInt(nnp,&note->duration,"duration",NULL); // get the note duration
  438. cmXmlNodeUInt(nnp,&note->staff,"staff",NULL); // get th staff number
  439. // is 'rest'
  440. if( cmXmlNodeHasChild(nnp,"rest",NULL) )
  441. note->flags |= kRestXsFl;
  442. // is 'grace'
  443. if( cmXmlNodeHasChild(nnp,"grace",NULL) )
  444. note->flags |= kGraceXsFl;
  445. // is 'dot'
  446. if( cmXmlNodeHasChild(nnp,"dot",NULL) )
  447. note->flags |= kDotXsFl;
  448. // is 'chord'
  449. if( cmXmlNodeHasChild(nnp,"chord",NULL) )
  450. note->flags |= kChordXsFl;
  451. // is this is first note in a tied pair
  452. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","start",NULL) )
  453. note->flags |= kTieBegXsFl;
  454. // is this is second note in a tied pair
  455. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","stop",NULL) )
  456. note->flags |= kTieEndXsFl;
  457. // has 'heel' mark
  458. if( cmXmlNodeHasChild(nnp,"notations","technical","heel",NULL) )
  459. note->flags |= kHeelXsFl;
  460. // set color coded flags
  461. if((rc = _cmXScoreParseColor(p, nnp, note )) != kOkXsRC )
  462. return rc;
  463. // get the note's rythmic value
  464. if((rc = _cmXScoreParseNoteRValue(p,nnp,"type",&note->rvalue)) != kOkXsRC )
  465. return rc;
  466. // if this is a chord note
  467. if( cmIsFlag(note->flags,kChordXsFl) )
  468. {
  469. note->tick = *tick0Ref; // then use the onset time from the previous note and do not advance time
  470. }
  471. else
  472. {
  473. *tick0Ref = *tickRef;
  474. note->tick = *tickRef;
  475. *tickRef += note->duration;
  476. }
  477. return _cmXScorePushNote(p, meas, voiceId, note );
  478. }
  479. cmXsRC_t _cmXScorePushNonNote( cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* noteXmlNode, unsigned tick, unsigned duration, double rvalue, const cmChar_t* tvalue, unsigned flags )
  480. {
  481. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  482. unsigned voiceId = 0; // non-note's are always assigned to voiceId=0;
  483. note->tick = tick;
  484. note->flags = flags;
  485. note->rvalue = rvalue;
  486. note->tvalue = tvalue;
  487. note->duration = duration;
  488. note->meas = meas;
  489. note->xmlNode = noteXmlNode;
  490. return _cmXScorePushNote(p, meas, voiceId, note );
  491. }
  492. cmXsSpan_t* _cmXScoreFindOpenOctaveShift( cmXScore_t* p, unsigned staff, unsigned number )
  493. {
  494. cmXsSpan_t* s = p->spanL;
  495. for(; s!=NULL; s=s->link)
  496. if( s->tick1 == -1 && s->staff == staff && s->number == number )
  497. return s;
  498. return NULL;
  499. }
  500. cmXsRC_t _cmXScorePushOctaveShift(cmXScore_t* p, cmXsMeas_t* meas, unsigned staff, unsigned span_number, const cmChar_t* type_str, unsigned tick)
  501. {
  502. assert( meas != NULL);
  503. cmXsSpan_t* s;
  504. if( cmTextCmp(type_str,"stop") == 0 )
  505. {
  506. if((s = _cmXScoreFindOpenOctaveShift(p,staff,span_number)) == NULL )
  507. return cmErrWarnMsg(&p->err,kUnterminatedOctaveShiftXsrRC,"An illegal octave shift was encounted in meas %i.\n",meas->number);
  508. s->tick1 = tick;
  509. }
  510. else
  511. {
  512. s = cmLhAllocZ(p->lhH,cmXsSpan_t,1);
  513. s->staff = staff;
  514. s->meas = meas;
  515. s->number = span_number;
  516. s->tick0 = tick;
  517. s->tick1 = -1;
  518. s->pitch_offset = cmTextCmp(type_str,"up")==0 ? -12 : 12;
  519. s->link = p->spanL;
  520. p->spanL = s;
  521. }
  522. return kOkXsRC;
  523. }
  524. cmXsRC_t _cmXScoreParseDirection(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* dnp, unsigned tick)
  525. {
  526. cmXsRC_t rc = kOkXsRC;
  527. const cmXmlNode_t* np = NULL;
  528. const cmXmlAttr_t* a = NULL;
  529. unsigned flags = 0;
  530. int offset = 0;
  531. double rvalue = 0;
  532. const cmChar_t* tvalue = NULL;
  533. unsigned duration = 0;
  534. bool pushFl = true;
  535. unsigned staff = 0;
  536. cmXmlNodeInt( dnp, &offset, "offset", NULL );
  537. cmXmlNodeUInt(dnp, &staff, "staff", NULL );
  538. // if this is a metronome direction
  539. if((np = cmXmlSearch( dnp, "metronome", NULL, 0)) != NULL )
  540. {
  541. if( cmXmlNodeUInt(np,&duration,"per-minute",NULL) != kOkXmlRC )
  542. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'per-minute' metronome value is missing on line %i.",np->line);
  543. if((rc = _cmXScoreParseNoteRValue(p,np,"beat-unit",&rvalue)) != kOkXsRC )
  544. return rc;
  545. flags = kMetronomeXsFl;
  546. }
  547. else
  548. // if this is a pedal direction
  549. if((np = cmXmlSearch( dnp, "pedal",NULL,0)) != NULL )
  550. {
  551. if((a = cmXmlFindAttrib(np,"type")) == NULL )
  552. return _cmXScoreMissingAttribute(p, np, "type" );
  553. if( cmTextCmp(a->value,"start") == 0 )
  554. flags = kPedalDnXsFl;
  555. else
  556. if( cmTextCmp(a->value,"change") == 0 )
  557. flags = kPedalUpDnXsFl;
  558. else
  559. if( cmTextCmp(a->value,"stop") == 0 )
  560. flags = kPedalUpXsFl;
  561. else
  562. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unrecognized pedal type:'%s'.",cmStringNullGuard(a->value));
  563. }
  564. else
  565. // if this is a 'words' direction
  566. if((np = cmXmlSearch( dnp, "words", NULL, 0)) != NULL )
  567. {
  568. if((a = cmXmlFindAttrib(np,"enclosure")) != NULL && cmTextCmp(a->value,"rectangle")==0 )
  569. {
  570. if( cmTextIsEmpty( tvalue = np->dataStr ) )
  571. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Section number is blank or missing on line %i.",np->line);
  572. flags = kSectionXsFl;
  573. }
  574. }
  575. else
  576. // if this is an 'octave-shift' direction
  577. if((np = cmXmlSearch( dnp, "octave-shift", NULL, 0)) != NULL )
  578. {
  579. unsigned span_number = -1;
  580. if( cmXmlAttrUInt(np,"number",&span_number) != kOkXmlRC )
  581. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'number' attribute.");
  582. if((a = cmXmlFindAttrib(np,"type")) == NULL)
  583. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'type' attribute.");
  584. rc = _cmXScorePushOctaveShift(p,meas,staff,span_number,a->value,tick+offset);
  585. pushFl = false;
  586. }
  587. else
  588. {
  589. pushFl = false;
  590. }
  591. if( pushFl )
  592. rc = _cmXScorePushNonNote(p,meas,dnp,tick+offset,duration,rvalue,tvalue,flags);
  593. return rc;
  594. }
  595. // On input tickRef is set to the absolute tick of the bar line and on output it is set
  596. // to the absolute tick of the next bar line.
  597. cmXsRC_t _cmXScoreParseMeasure(cmXScore_t* p, cmXsPart_t* pp, const cmXmlNode_t* mnp, unsigned* tickRef)
  598. {
  599. cmXsRC_t rc = kOkXsRC;
  600. const cmXmlNode_t* np = NULL;
  601. unsigned tick = *tickRef;
  602. unsigned tick0= 0;
  603. cmXsMeas_t* m = NULL;
  604. // allocate the 'measure' record
  605. cmXsMeas_t* meas = cmLhAllocZ(p->lhH,cmXsMeas_t,1);
  606. // get measure number
  607. if( cmXmlAttrUInt(mnp,"number", &meas->number) != kOkXmlRC )
  608. return _cmXScoreMissingAttribute(p,mnp,"number");
  609. if( pp->measL == NULL )
  610. pp->measL = meas;
  611. else
  612. {
  613. m = pp->measL;
  614. while( m->link != NULL )
  615. m = m->link;
  616. m->link = meas;
  617. meas->divisions = m->divisions;
  618. meas->beats = m->beats;
  619. meas->beat_type = m->beat_type;
  620. }
  621. // get measure attributes node
  622. if((np = cmXmlSearch(mnp,"attributes",NULL,0)) != NULL)
  623. {
  624. cmXmlNodeUInt(np,&meas->divisions,"divisions",NULL);
  625. cmXmlNodeUInt(np,&meas->beats, "time","beats",NULL);
  626. cmXmlNodeUInt(np,&meas->beat_type,"time","beat-type",NULL);
  627. }
  628. // store the bar line
  629. if((rc = _cmXScorePushNonNote(p,meas,mnp,tick,0,0,NULL,kBarXsFl)) != kOkXsRC )
  630. return rc;
  631. np = mnp->children;
  632. // for each child of the 'meas' XML node
  633. for(; rc==kOkXsRC && np!=NULL; np=np->sibling)
  634. {
  635. // if this is a 'note' node
  636. if( cmTextCmp(np->label,"note") == 0 )
  637. {
  638. rc = _cmXScoreParseNote(p,meas,np,&tick0,&tick);
  639. }
  640. else
  641. // if this is a 'backup' node
  642. if( cmTextCmp(np->label,"backup") == 0 )
  643. {
  644. unsigned backup;
  645. cmXmlNodeUInt(np,&backup,"duration",NULL);
  646. if( backup > tick )
  647. tick = 0;
  648. else
  649. tick -= backup;
  650. tick0 = tick;
  651. }
  652. else
  653. // if this is a 'direction' node
  654. if( cmTextCmp(np->label,"direction") == 0 )
  655. {
  656. rc = _cmXScoreParseDirection(p,meas,np,tick);
  657. }
  658. }
  659. *tickRef = tick;
  660. return rc;
  661. }
  662. cmXsRC_t _cmXScoreParsePart( cmXScore_t* p, cmXsPart_t* pp )
  663. {
  664. cmXsRC_t rc = kOkXsRC;
  665. const cmXmlNode_t* xnp;
  666. cmXmlAttr_t partAttr = { "id", pp->idStr };
  667. unsigned barTick = 0;
  668. // find the 'part'
  669. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part", &partAttr, 1)) == NULL )
  670. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The part '%s' was not found.",pp->idStr);
  671. // for each child of this part - find each measure
  672. const cmXmlNode_t* cnp = xnp->children;
  673. for(; cnp!=NULL; cnp=cnp->sibling)
  674. if( cmTextCmp(cnp->label,"measure") == 0 )
  675. if((rc = _cmXScoreParseMeasure(p,pp,cnp,&barTick)) != kOkXsRC )
  676. return rc;
  677. return rc;
  678. }
  679. // Insert note 'np' into the sorted note list based at 's0'.
  680. // Return a pointer to the base of the list after the insertion.
  681. cmXsNote_t* _cmXScoreInsertSortedNote( cmXsNote_t* s0, cmXsNote_t* np )
  682. {
  683. assert( np != NULL );
  684. // np->slink is not NULL if the list is being resorted
  685. np->slink = NULL;
  686. // this list is empty so np is the first element on the list
  687. if( s0 == NULL )
  688. return np;
  689. // np is before the first element on the list
  690. if( np->tick < s0->tick )
  691. {
  692. np->slink = s0;
  693. return np;
  694. }
  695. cmXsNote_t* s1 = s0;
  696. cmXsNote_t* s2 = s0->slink;
  697. while( s2 != NULL )
  698. {
  699. if( s2->tick > np->tick )
  700. {
  701. s1->slink = np;
  702. np->slink = s2;
  703. return s0;
  704. }
  705. s1 = s2;
  706. s2 = s2->slink;
  707. }
  708. s1->slink = np;
  709. return s0;
  710. }
  711. void _cmXScoreSort( cmXScore_t* p )
  712. {
  713. // for each part
  714. cmXsPart_t* pp = p->partL;
  715. for(; pp!=NULL; pp=pp->link)
  716. {
  717. // for each measure in this part
  718. cmXsMeas_t* mp = pp->measL;
  719. for(; mp!=NULL; mp=mp->link)
  720. {
  721. // explicitely set noteL to NULL to in case we are re-sorting
  722. mp->noteL = NULL;
  723. // for each voice in this measure
  724. cmXsVoice_t* vp = mp->voiceL;
  725. for(; vp!=NULL; vp=vp->link)
  726. {
  727. // for each note in this measure
  728. cmXsNote_t* np = vp->noteL;
  729. for(; np!=NULL; np=np->mlink)
  730. mp->noteL = _cmXScoreInsertSortedNote(mp->noteL,np);
  731. }
  732. }
  733. }
  734. }
  735. void _cmXScoreSetAbsoluteTime( cmXScore_t* p )
  736. {
  737. double tpqn = 0; // ticks per quarter note
  738. double tps = 0; // ticks per second
  739. unsigned metro_tick = 0;
  740. double metro_sec = 0;
  741. double sec0 = 0;
  742. cmXsPart_t* pp = p->partL;
  743. for(; pp!=NULL; pp=pp->link)
  744. {
  745. cmXsMeas_t* mp = pp->measL;
  746. for(; mp!=NULL; mp=mp->link)
  747. {
  748. if( mp->divisions != 0 )
  749. tpqn = mp->divisions;
  750. cmXsNote_t* np = mp->noteL;
  751. for(; np!=NULL; np=np->slink)
  752. {
  753. // Seconds are calculated as:
  754. // dticks = np->tick - metro_tick; // where metro_tick is the absolute tick of the last metro event
  755. // secs = (dticks/tps) + metro_secs; // where metro_secs is the absoute time of the last metro event
  756. unsigned dticks = np->tick - metro_tick;
  757. double secs = tps==0 ? 0 : (dticks/tps) + metro_sec;
  758. double dsecs = secs - sec0;
  759. //
  760. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  761. {
  762. double bpm = np->duration;
  763. double bps = bpm / 60.0;
  764. tps = bps * tpqn;
  765. metro_tick = np->tick;
  766. metro_sec = secs;
  767. }
  768. if( cmIsFlag(np->flags,kBarXsFl|kPedalDnXsFl|kPedalUpXsFl|kPedalUpDnXsFl|kSostDnXsFl|kSostUpXsFl|kOnsetXsFl|kSectionXsFl) )
  769. {
  770. np->secs = secs;
  771. np->dsecs = dsecs;
  772. sec0 = secs;
  773. }
  774. }
  775. }
  776. }
  777. }
  778. // All notes in a[aN] are on the same tick
  779. unsigned _cmXsSpreadGraceNotes( cmXsNote_t** a, unsigned aN )
  780. {
  781. unsigned i;
  782. bool barFl = false;
  783. // set barFl to true if a bar marker is included in the notes
  784. for(i=0; i<aN; ++i)
  785. if( cmIsFlag(a[i]->flags,kBarXsFl) )
  786. barFl = true;
  787. // spread any grace notes by one tick
  788. unsigned nextGraceTick = UINT_MAX;
  789. for(i=0; i<aN; ++i)
  790. if( cmIsFlag(a[i]->flags,kGraceXsFl) )
  791. {
  792. if( nextGraceTick == UINT_MAX )
  793. nextGraceTick = a[i]->tick + 1;
  794. else
  795. {
  796. a[i]->tick = nextGraceTick;
  797. nextGraceTick += 1;
  798. }
  799. }
  800. // if this tick group includes the bar ...
  801. if( barFl && nextGraceTick != UINT_MAX )
  802. {
  803. // ... then move all non-grace note events (except the bar marker) after
  804. // the grace notes
  805. for(i=0; i<aN; ++i)
  806. if( cmIsNotFlag(a[i]->flags,kGraceXsFl) && cmIsNotFlag(a[i]->flags,kBarXsFl) )
  807. a[i]->tick = nextGraceTick;
  808. }
  809. return nextGraceTick==UINT_MAX ? 0 : nextGraceTick;
  810. }
  811. void _cmXScoreSpreadGraceNotes( cmXScore_t* p )
  812. {
  813. cmXsPart_t* pp = p->partL;
  814. for(; pp!=NULL; pp=pp->link)
  815. {
  816. // tick1 is the location of the minimum current tick
  817. // (or 0 if it should be ignored)
  818. unsigned tick1 = 0;
  819. cmXsMeas_t* mp = pp->measL;
  820. for(; mp!=NULL; mp=mp->link)
  821. {
  822. cmXsNote_t* np = mp->noteL;
  823. unsigned aN = 128;
  824. cmXsNote_t* a[ aN ];
  825. unsigned ai = 0;
  826. // The first event in a measure may have been forced ahead
  827. // by spreading at the end of the previous measure
  828. if( tick1 > np->tick )
  829. np->tick = tick1;
  830. else
  831. tick1 = 0;
  832. // tick0 is the tick of the current tick group we are examining
  833. // A tick group is a group of events that share the same tick.
  834. unsigned tick0 = np->tick;
  835. for(; np!=NULL; np=np->slink)
  836. {
  837. // if this event is the first of a new tick group
  838. if( np->tick != tick0 )
  839. {
  840. // if there is more than one event in the completed tick group ...
  841. if( ai > 1 )
  842. tick1 = _cmXsSpreadGraceNotes(a,ai); // ... then process the group
  843. ai = 0; // empty the tick group array
  844. tick0 = np->tick; // update the current group's common tick
  845. }
  846. // if the min. current tick is ahead of this event then move the event ahead
  847. if( tick1 > np->tick )
  848. np->tick = tick1;
  849. else
  850. tick1 = 0; // otherwise disable tick1
  851. // add this event to the tick group
  852. assert(ai<aN);
  853. a[ai++] = np;
  854. }
  855. // if there are events in the group array then process them
  856. if( ai > 1 )
  857. tick1 = _cmXsSpreadGraceNotes(a,ai);
  858. }
  859. }
  860. }
  861. bool _cmXScoreFindTiedNote( cmXScore_t* p, cmXsMeas_t* mp, cmXsNote_t* n0p, bool rptFl )
  862. {
  863. cmXsNote_t* nnp = n0p->slink; // begin w/ note following np
  864. unsigned measNumb = mp->number;
  865. cmChar_t acc = n0p->alter==-1?'b' : (n0p->alter==1?'#':' ');
  866. if( rptFl )
  867. printf("%i %i %s ",n0p->meas->number,n0p->tick,cmMidiToSciPitch(n0p->pitch,NULL,0));
  868. while(1)
  869. {
  870. // if we are at the end of a measure advance to the next measure
  871. if( nnp == NULL )
  872. {
  873. mp = mp->link;
  874. nnp = mp->noteL;
  875. // if a measure was completed and no end note was found ... then the tie is unterminated
  876. // (a tie must be continued in every measure which it passes through)
  877. if( mp->number > measNumb + 1 )
  878. break;
  879. }
  880. // for each note starting at nnp
  881. for(; nnp!=NULL; nnp=nnp->slink)
  882. {
  883. // if this note is tied to the originating note (np)
  884. if( nnp->voice->id == n0p->voice->id && nnp->step == n0p->step && nnp->octave == n0p->octave )
  885. {
  886. nnp->flags |= kTieProcXsFl;
  887. nnp->flags = cmClrFlag(nnp->flags,kOnsetXsFl);
  888. if( rptFl )
  889. printf("---> %i %i %s ",nnp->meas->number,nnp->tick,cmMidiToSciPitch(nnp->pitch,NULL,0));
  890. // if this note is not tied to a subsequent note
  891. if( cmIsNotFlag(nnp->flags,kTieBegXsFl) )
  892. return true;
  893. // record the measure number of the last note with a tie-start
  894. measNumb = mp->number;
  895. }
  896. }
  897. }
  898. cmErrWarnMsg(&p->err,kUnterminatedTieXsRC,"The tied %c%c%i in measure %i was not terminated.",n0p->step,acc,n0p->octave,measNumb);
  899. return false;
  900. }
  901. void _cmXScoreResolveTiesAndLoc( cmXScore_t* p )
  902. {
  903. unsigned n = 0;
  904. unsigned m = 0;
  905. bool rptFl = false;
  906. cmXsPart_t* pp = p->partL;
  907. // for each part
  908. for(; pp!=NULL; pp=pp->link)
  909. {
  910. unsigned locIdx = 1;
  911. cmXsMeas_t* mp = pp->measL;
  912. // for each measure
  913. for(; mp!=NULL; mp=mp->link)
  914. {
  915. cmXsNote_t* n0 = NULL;
  916. cmXsNote_t* np = mp->noteL;
  917. // for each note in this measure
  918. for(; np!=NULL; np=np->slink)
  919. {
  920. // if this note begins a tie and has not yet been processed
  921. // (A note that continues a tie and therefore has a kTieBegXsFl set
  922. // may have already been processed by an earlier tied note.)
  923. if( cmIsFlag(np->flags,kTieBegXsFl) && cmIsNotFlag(np->flags,kTieProcXsFl))
  924. {
  925. if( _cmXScoreFindTiedNote(p,mp,np,rptFl) )
  926. m += 1;
  927. if( rptFl )
  928. printf("\n");
  929. n += 1;
  930. }
  931. if( cmIsFlag(np->flags,kTieEndXsFl) && cmIsFlag(np->flags,kOnsetXsFl) )
  932. {
  933. cmChar_t acc = np->alter==-1?'b' : (np->alter==1?'#':' ');
  934. cmErrWarnMsg(&p->err,kUnterminatedTieXsRC,"The tied %c%c%i in measure %i marked as a tied note but is also marked to sound.",np->step,acc,np->octave,mp->number);
  935. }
  936. // set the location
  937. if( cmIsFlag(np->flags,kOnsetXsFl|kBarXsFl) )
  938. {
  939. if( cmIsFlag(np->flags,kBarXsFl) || (n0!=NULL && n0->tick!=np->tick))
  940. locIdx += 1;
  941. np->locIdx = locIdx;
  942. n0 = np;
  943. }
  944. }
  945. }
  946. }
  947. printf("Found:%i Not Found:%i\n",m,n-m);
  948. }
  949. cmXsRC_t _cmXScoreResolveOctaveShift( cmXScore_t* p )
  950. {
  951. const cmXsSpan_t* s;
  952. for(s=p->spanL; s!=NULL; s=s->link)
  953. {
  954. if( s->tick1 == -1)
  955. {
  956. cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"An unterminated octave shift span was encountered in measure %i staff=%i.",s->meas->number,s->staff);
  957. }
  958. else
  959. {
  960. cmXsMeas_t* m = p->partL->measL;
  961. for(; m!=NULL; m=m->link)
  962. if( m->number == s->meas->number )
  963. break;
  964. assert( m != NULL );
  965. cmXsNote_t* note = m->noteL;
  966. for(; note!=NULL; note=note->slink)
  967. if( note->staff==s->staff && s->tick0 <= note->tick && note->tick < s->tick1 )
  968. note->pitch += s->pitch_offset;
  969. }
  970. }
  971. return kOkXsRC;
  972. }
  973. // The identical pitch may be notated to play simultaneously on different voices.
  974. // As performed on the piano this will equate to a single sounding note.
  975. // This function clears the onset flag on all except one of the duplicated notes.
  976. void _cmXScoreRemoveDuplicateNotes( cmXScore_t* p )
  977. {
  978. cmXsPart_t* pp = p->partL;
  979. // for each part
  980. for(; pp!=NULL; pp=pp->link)
  981. {
  982. cmXsMeas_t* mp = pp->measL;
  983. // for each measure
  984. for(; mp!=NULL; mp=mp->link)
  985. {
  986. cmXsNote_t* np = mp->noteL;
  987. // for each note in this measure
  988. for(; np!=NULL; np=np->slink)
  989. if( cmIsFlag(np->flags,kOnsetXsFl) )
  990. {
  991. cmXsNote_t* n0p = mp->noteL;
  992. for(; n0p!=NULL; n0p=n0p->slink)
  993. if( n0p!=np && cmIsFlag(n0p->flags,kOnsetXsFl) && np->locIdx==n0p->locIdx && np->pitch==n0p->pitch )
  994. n0p->flags = cmClrFlag(n0p->flags,kOnsetXsFl);
  995. }
  996. }
  997. }
  998. }
  999. void _cmXScoreSetMeasGroups( cmXScore_t* p, unsigned flag )
  1000. {
  1001. unsigned sectionId = 0;
  1002. cmXsNote_t* n0 = NULL;
  1003. cmXsPart_t* pp = p->partL;
  1004. // for each part
  1005. for(; pp!=NULL; pp=pp->link)
  1006. {
  1007. cmXsMeas_t* mp = pp->measL;
  1008. // for each measure
  1009. for(; mp!=NULL; mp=mp->link)
  1010. {
  1011. cmXsNote_t* np = mp->noteL;
  1012. // for each note in this measure
  1013. for(; np!=NULL; np=np->slink)
  1014. {
  1015. // if this note has a heel marker and we are looking for evenness events
  1016. if( cmIsFlag(flag,kEvenXsFl) && cmIsFlag(np->flags,kHeelXsFl) )
  1017. {
  1018. np->flags = cmSetFlag(np->flags,kBegGroupXsFl | kEndGroupXsFl );
  1019. np->evenGroupId = sectionId + 1;
  1020. }
  1021. // if this note is of the type we are looking for
  1022. if( cmIsFlag(np->flags,flag) )
  1023. {
  1024. if( n0 == NULL )
  1025. np->flags = cmSetFlag(np->flags,kBegGroupXsFl);
  1026. n0 = np;
  1027. }
  1028. // if this is a section marker
  1029. if( cmIsFlag(np->flags,kSectionXsFl) )
  1030. {
  1031. if( n0 != NULL )
  1032. {
  1033. np->flags = cmSetFlag(np->flags,kEndGroupXsFl);
  1034. switch( flag )
  1035. {
  1036. case kEvenXsFl: n0->evenGroupId = sectionId+1; break;
  1037. case kDynXsFl: n0->dynGroupId = sectionId+1; break;
  1038. case kTempoXsFl: n0->tempoGroupId= sectionId+1; break;
  1039. }
  1040. }
  1041. if( cmIsFlag(np->flags,kSectionXsFl) )
  1042. sectionId = np->tvalue==NULL ? 0 : strtol(np->tvalue,NULL,10);
  1043. n0 = NULL;
  1044. }
  1045. }
  1046. }
  1047. }
  1048. }
  1049. cmXsRC_t _cmXScoreWriteScorePlotFile( cmXScore_t* p, const cmChar_t* fn )
  1050. {
  1051. cmXsRC_t rc = kOkXsRC;
  1052. cmFileH_t fH = cmFileNullHandle;
  1053. double ticks_per_sec = 0;
  1054. double onset_secs = 0;
  1055. if( cmFileOpen(&fH,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  1056. return cmErrMsg(&p->err,kFileFailXsRC,"Unable to create the file '%s'.",cmStringNullGuard(fn));
  1057. cmXsPart_t* pp = p->partL;
  1058. for(; pp!=NULL; pp=pp->link)
  1059. {
  1060. cmXsMeas_t* mp = pp->measL;
  1061. for(; mp!=NULL; mp=mp->link)
  1062. {
  1063. cmFilePrintf(fH,"b %f %i %s B\n",onset_secs,mp->number,"bar");
  1064. cmXsNote_t* np = mp->noteL;
  1065. unsigned tick0 = 0;
  1066. for(; np!=NULL; np=np->slink)
  1067. {
  1068. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  1069. {
  1070. double bps = np->duration / 60.0;
  1071. // t b t
  1072. // - = - -
  1073. // s s b
  1074. ticks_per_sec = bps * mp->divisions;
  1075. }
  1076. else
  1077. {
  1078. if( cmIsFlag(np->flags,kOnsetXsFl) )
  1079. {
  1080. onset_secs += (np->tick - tick0) / ticks_per_sec;
  1081. tick0 = np->tick;
  1082. cmFilePrintf(fH,"n %f %f %i %s %s\n",onset_secs,np->duration/ticks_per_sec,np->uid,cmMidiToSciPitch(np->pitch,NULL,0),cmIsFlag(np->flags,kGraceXsFl)?"G":"N");
  1083. }
  1084. }
  1085. }
  1086. onset_secs += (mp->divisions * mp->beats - tick0) / ticks_per_sec;
  1087. }
  1088. }
  1089. cmFileClose(&fH);
  1090. return rc;
  1091. }
  1092. // Force the bar event to be the first event in the measure.
  1093. void _cmXScoreFixBarLines( cmXScore_t* p )
  1094. {
  1095. cmXsPart_t* pp = p->partL;
  1096. for(; pp!=NULL; pp=pp->link)
  1097. {
  1098. cmXsMeas_t* mp = pp->measL;
  1099. for(; mp!=NULL; mp=mp->link)
  1100. {
  1101. cmXsNote_t* np = mp->noteL;
  1102. cmXsNote_t* ep = NULL;
  1103. for(; np!=NULL; np=np->slink )
  1104. {
  1105. if( cmIsFlag(np->flags,kBarXsFl) )
  1106. {
  1107. if( ep != NULL )
  1108. np->tick = ep->tick;
  1109. break;
  1110. }
  1111. if( ep == NULL )
  1112. ep = np;
  1113. }
  1114. }
  1115. }
  1116. }
  1117. cmXsRC_t cmXScoreInitialize( cmCtx_t* ctx, cmXsH_t* hp, const cmChar_t* xmlFn, const cmChar_t* midiFn )
  1118. {
  1119. cmXsRC_t rc = kOkXsRC;
  1120. if((rc = cmXScoreFinalize(hp)) != kOkXsRC )
  1121. return rc;
  1122. cmXScore_t* p = cmMemAllocZ(cmXScore_t,1);
  1123. cmErrSetup(&p->err,&ctx->rpt,"XScore");
  1124. // create a local linked heap
  1125. if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8196,ctx)) == false )
  1126. return cmErrMsg(&p->err,kLHeapFailXsRC,"Lheap create failed.");
  1127. // open the music xml file
  1128. if( cmXmlAlloc(ctx, &p->xmlH, xmlFn) != kOkXmlRC )
  1129. {
  1130. rc = cmErrMsg(&p->err,kXmlFailXsRC,"Unable to open the MusicXML file '%s'.",cmStringNullGuard(xmlFn));
  1131. goto errLabel;
  1132. }
  1133. //cmXmlPrint(p->xmlH,&ctx->rpt);
  1134. // parse the part-list
  1135. if((rc = _cmXScoreParsePartList( p )) != kOkXsRC )
  1136. goto errLabel;
  1137. // parse each score 'part'
  1138. cmXsPart_t* pp = p->partL;
  1139. for(; pp!=NULL; pp=pp->link)
  1140. if((rc = _cmXScoreParsePart(p,pp)) != kOkXsRC )
  1141. goto errLabel;
  1142. // fill in the note->slink chain to link the notes in each measure in time order
  1143. _cmXScoreSort(p);
  1144. _cmXScoreSpreadGraceNotes(p);
  1145. _cmXScoreSort(p);
  1146. _cmXScoreSetAbsoluteTime(p);
  1147. _cmXScoreResolveTiesAndLoc(p);
  1148. _cmXScoreRemoveDuplicateNotes(p);
  1149. _cmXScoreSetMeasGroups(p,kEvenXsFl);
  1150. _cmXScoreSetMeasGroups(p,kDynXsFl);
  1151. _cmXScoreSetMeasGroups(p,kTempoXsFl);
  1152. //_cmXScoreResolveOctaveShift(p);
  1153. // CSV output initialize failed.
  1154. if( cmCsvInitialize(&p->csvH,ctx) != kOkCsvRC )
  1155. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output object create failed.");
  1156. errLabel:
  1157. if( rc != kOkXsRC )
  1158. _cmXScoreFinalize(p);
  1159. else
  1160. hp->h = p;
  1161. return rc;
  1162. }
  1163. cmXsRC_t cmXScoreFinalize( cmXsH_t* hp )
  1164. {
  1165. cmXsRC_t rc = kOkXsRC;
  1166. if( hp == NULL || cmXScoreIsValid(*hp)==false )
  1167. return kOkXsRC;
  1168. cmXScore_t* p = _cmXScoreHandleToPtr(*hp);
  1169. if((rc = _cmXScoreFinalize(p)) != kOkXsRC )
  1170. return rc;
  1171. hp->h = NULL;
  1172. return rc;
  1173. }
  1174. bool cmXScoreIsValid( cmXsH_t h )
  1175. { return h.h != NULL; }
  1176. //-------------------------------------------------------------------------------------------
  1177. typedef struct
  1178. {
  1179. unsigned idx; // Fields from the reordering input file which are
  1180. unsigned voice; // used to match the reorder record to
  1181. unsigned locIdx; // the associated a cmXsNode_t record.
  1182. unsigned tick; //
  1183. unsigned durtn; //
  1184. float rval; //
  1185. unsigned midi; //
  1186. cmXsNote_t* note; // The cmXsNode_t* associated with this cmXsReorder_t record
  1187. unsigned dynIdx; // cmInvalidIdx=ignore otherwise index into _cmXScoreDynMarkArray[]
  1188. unsigned newFlags; // 0=ignore | kSostUp/DnXsFl | kPedalUp/DnXsFl | kTieEndXsFl
  1189. unsigned newTick; // 0=ignore >0 new tick value
  1190. unsigned pitch; // 0=ignore >0 new pitch
  1191. } cmXsReorder_t;
  1192. typedef struct _cmXScoreDynMark_str
  1193. {
  1194. const cmChar_t* mark; //
  1195. unsigned id; // (1-17) maps to velocity
  1196. unsigned dyn; // pppp - fff (1-9) as used by cmScore
  1197. int adjust; // {-1,0,+1}
  1198. unsigned vel; // associated MIDI velocity
  1199. } _cmXScoreDynMark_t;
  1200. _cmXScoreDynMark_t _cmXScoreDynMarkArray[] =
  1201. {
  1202. {"pppp-", 1, 1, -1, 3},
  1203. {"pppp", 2, 1, 0, 10},
  1204. {"pppp+", 3, 1, 1, 22},
  1205. {"ppp-", 3, 2, -1, 22},
  1206. {"ppp", 4, 2, 0, 29},
  1207. {"ppp+", 5, 2, 1, 36},
  1208. {"pp-", 5, 3, -1, 36},
  1209. {"pp", 6, 3, 0, 43},
  1210. {"pp+", 7, 3, 1, 50},
  1211. {"p-", 7, 4, -1, 50},
  1212. {"p", 8, 4, 0, 57},
  1213. {"p+", 9, 4, 1, 64},
  1214. {"mp-", 9, 5, -1, 64},
  1215. {"mp", 10, 5, 0, 71},
  1216. {"mp+", 11, 5, 1, 78},
  1217. {"mf-", 11, 6, -1, 78},
  1218. {"mf", 12, 6, 0, 85},
  1219. {"mf+", 13, 6, 1, 92},
  1220. {"f-", 13, 7, -1, 92},
  1221. {"f", 14, 7, 0, 99},
  1222. {"f+", 15, 7, 1, 106},
  1223. {"ff", 16, 8, 0, 113},
  1224. {"ff+", 17, 8, 1, 120},
  1225. {"fff", 18, 9, 0, 127},
  1226. {NULL,0,0,0,0}
  1227. };
  1228. cmXsNote_t* _cmXsReorderFindNote( cmXScore_t* p, unsigned measNumb, const cmXsReorder_t* r, unsigned iii )
  1229. {
  1230. cmXsPart_t* pp = p->partL;
  1231. for(; pp!=NULL; pp=pp->link)
  1232. {
  1233. cmXsMeas_t* mp = pp->measL;
  1234. for(; mp!=NULL; mp=mp->link)
  1235. if( mp->number == measNumb)
  1236. {
  1237. cmXsNote_t* np = mp->noteL;
  1238. int index = 0;
  1239. for(; np!=NULL; np=np->slink,++index)
  1240. {
  1241. if( np->voice->id == r->voice &&
  1242. np->locIdx == r->locIdx &&
  1243. np->tick == r->tick &&
  1244. np->duration == r->durtn &&
  1245. np->rvalue == r->rval &&
  1246. np->pitch == r->midi &&
  1247. index == r->idx )
  1248. {
  1249. return np;
  1250. }
  1251. }
  1252. }
  1253. }
  1254. cmErrMsg(&p->err,kSyntaxErrorXsRC,"Reorder note not found meas:%i index:%i.",measNumb,iii);
  1255. return NULL;
  1256. }
  1257. void _cmXScoreInsertPedalEvent( cmXScore_t* p, const cmXsReorder_t* r, unsigned flags )
  1258. {
  1259. // Create a new score event record
  1260. cmXsNote_t* nn = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  1261. nn->uid = p->nextUid++;
  1262. nn->voice = r->note->voice;
  1263. nn->meas = r->note->meas;
  1264. nn->flags = flags;
  1265. // Pedal down events occur after the event they are attached to
  1266. if( cmIsFlag(flags,kSostDnXsFl | kPedalDnXsFl ) )
  1267. {
  1268. nn->tick = r->note->tick + 1;
  1269. _cmXScoreInsertNoteAfter(r->note,nn);
  1270. }
  1271. else
  1272. {
  1273. // Pedal up events occur before the event they are attached to
  1274. if( cmIsFlag(flags,kSostUpXsFl | kPedalUpXsFl ) )
  1275. {
  1276. nn->tick = r->note->tick==0 ? 0 : r->note->tick - 1;
  1277. _cmXScoreInsertNoteBefore(r->note,nn);
  1278. }
  1279. else
  1280. { assert(0); }
  1281. }
  1282. }
  1283. cmXsRC_t _cmXScoreReorderMeas( cmXScore_t* p, unsigned measNumb, cmXsReorder_t* rV, unsigned rN )
  1284. {
  1285. unsigned i;
  1286. if( rN == 0 )
  1287. return kOkXsRC;
  1288. // set the 'note' field on each cmXsReorder_t record
  1289. for(i=0; i<rN; ++i)
  1290. {
  1291. if((rV[i].note = _cmXsReorderFindNote(p,measNumb,rV+i,i)) == NULL )
  1292. return kSyntaxErrorXsRC;
  1293. }
  1294. cmXsMeas_t* mp = rV[0].note->meas;
  1295. cmXsNote_t* n0p = NULL;
  1296. assert( mp->number == measNumb );
  1297. // Reassign the slink of the cmXsNote_t records in this measure
  1298. // according to their order in rV[].
  1299. for(i=0; i<rN; ++i)
  1300. {
  1301. if( n0p == NULL )
  1302. mp->noteL = rV[i].note;
  1303. else
  1304. n0p->slink = rV[i].note;
  1305. // if a new tick was specified
  1306. if( rV[i].newTick != 0 )
  1307. rV[i].note->tick = rV[i].newTick;
  1308. // if a dynamic or velocity mark was included
  1309. if( rV[i].dynIdx != cmInvalidIdx )
  1310. {
  1311. rV[i].note->dynamics = _cmXScoreDynMarkArray[ rV[i].dynIdx ].dyn;
  1312. rV[i].note->vel = _cmXScoreDynMarkArray[ rV[i].dynIdx ].vel;
  1313. }
  1314. // if the tie end flag was set
  1315. if( cmIsFlag(rV[i].newFlags,kTieEndXsFl) )
  1316. {
  1317. rV[i].note->flags |= kTieEndXsFl;
  1318. rV[i].note->flags = cmClrFlag(rV[i].note->flags, kOnsetXsFl);
  1319. rV[i].newFlags = cmClrFlag(rV[i].newFlags,kTieEndXsFl );
  1320. }
  1321. // if a new note value was specified
  1322. if( rV[i].pitch != 0 )
  1323. rV[i].note->pitch = rV[i].pitch;
  1324. n0p = rV[i].note;
  1325. n0p->slink = NULL;
  1326. }
  1327. // Insert new note records for pedal up/dn events.
  1328. for(i=0; i<rN; ++i)
  1329. {
  1330. if( rV[i].newFlags != 0 )
  1331. {
  1332. if( cmIsFlag(rV[i].newFlags,kPedalDnXsFl ) )
  1333. _cmXScoreInsertPedalEvent(p,rV + i,kPedalDnXsFl);
  1334. if( cmIsFlag(rV[i].newFlags,kSostDnXsFl ) )
  1335. _cmXScoreInsertPedalEvent(p,rV + i,kSostDnXsFl);
  1336. if( cmIsFlag(rV[i].newFlags,kPedalUpXsFl ) )
  1337. _cmXScoreInsertPedalEvent(p,rV + i,kPedalUpXsFl);
  1338. if( cmIsFlag(rV[i].newFlags,kSostUpXsFl ) )
  1339. _cmXScoreInsertPedalEvent(p,rV + i,kSostUpXsFl);
  1340. }
  1341. }
  1342. return kOkXsRC;
  1343. }
  1344. cmXsRC_t _cmXScoreReorderParseDyn(cmXScore_t* p, const cmChar_t* b, unsigned lineNumb, unsigned* dynIdxRef )
  1345. {
  1346. cmXsRC_t rc = kOkXsRC;
  1347. const cmChar_t* s;
  1348. *dynIdxRef = cmInvalidIdx;
  1349. // locate the '!' which indicates the start of a dynamic marking
  1350. if( (s = strchr(b,'!')) == NULL )
  1351. return rc;
  1352. ++s; // increment past the '!'
  1353. if( *s == 0 )
  1354. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected end-of-line on dynamics parsing on line:%i.",lineNumb);
  1355. // some dynamic markings are surrounded by parenthesis (to indicate a dynamic level with greater uncertainty)
  1356. if( *s == '(' )
  1357. ++s; // skip the paren.
  1358. if( *s == 0 )
  1359. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected end-of-line on dynamics parsing on line:%i.",lineNumb);
  1360. unsigned i = 0;
  1361. unsigned j = 0;
  1362. unsigned n = 6;
  1363. bool doneFl = false;
  1364. cmChar_t mark[n+1];
  1365. memset(mark,0,n+1);
  1366. for(i=0; j<n && doneFl==false; ++i)
  1367. {
  1368. switch(s[i])
  1369. {
  1370. case 'm':
  1371. case 'p':
  1372. case 'f':
  1373. case '+':
  1374. case '-':
  1375. mark[j++] = s[i];
  1376. break;
  1377. case ')': // ending paren.
  1378. case 0: // end of string
  1379. case ' ': // end of mark
  1380. case '\n': // end of line
  1381. default: // anything else
  1382. doneFl = true;
  1383. break;
  1384. }
  1385. }
  1386. if( !doneFl )
  1387. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal dynamic mark (%s) syntax on line:%i.",mark,lineNumb);
  1388. // look up the dynamic mark in the reference table
  1389. for(j=0; _cmXScoreDynMarkArray[j].mark!=NULL; ++j)
  1390. if( strcmp(mark,_cmXScoreDynMarkArray[j].mark) == 0 )
  1391. break;
  1392. if( _cmXScoreDynMarkArray[j].mark == NULL )
  1393. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The dynamic mark '%s' is not legal on line:%i.",mark,lineNumb);
  1394. *dynIdxRef = j;
  1395. return rc;
  1396. }
  1397. cmXsRC_t _cmXScoreReorderParseFlags(cmXScore_t* p, const cmChar_t* b, unsigned line, unsigned* newFlagsRef )
  1398. {
  1399. cmXsRC_t rc = kOkXsRC;
  1400. const cmChar_t* s;
  1401. bool doneFl = false;
  1402. unsigned i = 0;
  1403. *newFlagsRef = 0;
  1404. // tilde indicates a pedal event
  1405. if((s = strchr(b,'~')) == NULL )
  1406. return rc;
  1407. do
  1408. {
  1409. ++s;
  1410. switch( *s )
  1411. {
  1412. case 'd':
  1413. *newFlagsRef |= kSostDnXsFl; // sostenuto pedal down just after this note onset
  1414. break;
  1415. case 'u':
  1416. *newFlagsRef |= kSostUpXsFl; // sostenuto pedal up just before this event
  1417. break;
  1418. case 'x':
  1419. *newFlagsRef |= (kSostUpXsFl | kSostDnXsFl); // sostenuto pedal up just before this event and sost down just after it.
  1420. break;
  1421. case 'D':
  1422. *newFlagsRef |= kPedalDnXsFl; // damper pedal down
  1423. break;
  1424. case 'U':
  1425. *newFlagsRef |= kPedalUpXsFl; // damper pedal up
  1426. break;
  1427. case '_':
  1428. *newFlagsRef |= kTieEndXsFl; // set tie end flag
  1429. break;
  1430. default:
  1431. if( i == 0 )
  1432. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected flag marking '%c' on line %i.",*s,line);
  1433. doneFl = true;
  1434. }
  1435. ++i;
  1436. }while(!doneFl);
  1437. return rc;
  1438. }
  1439. cmXsRC_t _cmXScoreReorderParseTick(cmXScore_t* p, const cmChar_t* b, unsigned line, unsigned* tickRef )
  1440. {
  1441. cmXsRC_t rc = kOkXsRC;
  1442. const cmChar_t* s;
  1443. if((s = strchr(b,'@')) == NULL )
  1444. return rc;
  1445. ++s;
  1446. if(!isdigit(*s))
  1447. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected tick reorder value '%c' on line %i.",*s,line);
  1448. if(sscanf(s,"%i",tickRef) != 1 )
  1449. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"tick reorder parse failed on line %i.",line);
  1450. return rc;
  1451. }
  1452. cmXsRC_t _cmXScoreReorderParsePitch(cmXScore_t* p, const cmChar_t* b, unsigned line, unsigned* pitchRef )
  1453. {
  1454. cmXsRC_t rc = kOkXsRC;
  1455. cmChar_t* s;
  1456. cmChar_t buf[4];
  1457. unsigned i,j;
  1458. memset(buf,0,sizeof(buf));
  1459. *pitchRef = 0;
  1460. if((s = strchr(b,'$')) == NULL )
  1461. return rc;
  1462. ++s;
  1463. j=2;
  1464. for(i=0; i<j && s[i]; ++i,++s)
  1465. {
  1466. buf[i] = *s;
  1467. if( i==1 && (*s=='#' || *s=='b') )
  1468. j = 3;
  1469. if( i==0 && strchr("ABCDEFG",*s)==NULL )
  1470. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal pitch letter ('%c')specification line %i.",*s,line);
  1471. if( i==1 && !isdigit(*s) && *s!='#' && *s!='b' )
  1472. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal pitch level ('%c') specification line %i.",*s,line);
  1473. if( i==2 && !isdigit(*s) )
  1474. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal pitch octave ('%c') specification line %i.",*s,line);
  1475. }
  1476. unsigned pitch = cmSciPitchToMidi(buf);
  1477. if( pitch<kInvalidMidiByte)
  1478. *pitchRef = pitch;
  1479. else
  1480. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"Pitch conversion from '%s' failed on line %i.",buf,line);
  1481. return rc;
  1482. }
  1483. cmXsRC_t cmXScoreReorder( cmXsH_t h, const cmChar_t* fn )
  1484. {
  1485. typedef enum { kFindMeasStId, kFindEventStId, kReadEventStId } stateId_t;
  1486. cmXsRC_t rc = kOkXsRC;
  1487. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1488. cmFileH_t fH = cmFileNullHandle;
  1489. cmChar_t* b = NULL;
  1490. unsigned bN = 0;
  1491. unsigned ln = 0;
  1492. stateId_t stateId = kFindMeasStId;
  1493. unsigned rN = 1024;
  1494. unsigned ri = 0;
  1495. unsigned measNumb = 0;
  1496. cmXsReorder_t rV[ rN ];
  1497. if( cmFileOpen(&fH,fn,kReadFileFl,p->err.rpt) != kOkFileRC )
  1498. {
  1499. rc = cmErrMsg(&p->err,kFileFailXsRC,"The reordering file '%s' could not be opened.",cmStringNullGuard(fn));
  1500. return rc;
  1501. }
  1502. for(; cmFileGetLineAuto(fH,&b,&bN)==kOkFileRC; ++ln)
  1503. {
  1504. switch( stateId )
  1505. {
  1506. case kFindEventStId: // scanning past labels to an event line
  1507. {
  1508. unsigned voice,loc;
  1509. if( sscanf(b,"%i %i",&voice,&loc) != 2 )
  1510. continue;
  1511. stateId = kReadEventStId;
  1512. }
  1513. // fall through
  1514. case kReadEventStId:
  1515. {
  1516. cmXsReorder_t r;
  1517. char pitchStr[4];
  1518. memset(&r,0,sizeof(r));
  1519. // parse an event line
  1520. if( sscanf(b,"%i %i %i %i %i %f",&r.idx,&r.voice,&r.locIdx,&r.tick,&r.durtn,&r.rval) == 6 )
  1521. {
  1522. assert( strlen(b)>=52);
  1523. int PC = 39; // text file column where first pitch char occurs
  1524. if( b[PC] == ' ')
  1525. r.midi = 0;
  1526. else
  1527. {
  1528. pitchStr[0] = b[PC+0];
  1529. pitchStr[1] = b[PC+1];
  1530. pitchStr[2] = b[PC+2];
  1531. pitchStr[3] = 0;
  1532. if( !isdigit(pitchStr[2]) )
  1533. r.midi = 0;
  1534. else
  1535. {
  1536. if( pitchStr[1] == ' ')
  1537. {
  1538. pitchStr[1] = pitchStr[2];
  1539. pitchStr[2] = 0;
  1540. }
  1541. r.midi = cmSciPitchToMidi(pitchStr);
  1542. }
  1543. }
  1544. // parse the dynamic marking following a '!'
  1545. if((rc = _cmXScoreReorderParseDyn(p,b,ln+1,&r.dynIdx)) != kOkXsRC )
  1546. goto errLabel;
  1547. // parse the flag edits
  1548. if((rc = _cmXScoreReorderParseFlags(p,b,ln+1, &r.newFlags)) != kOkXsRC )
  1549. goto errLabel;
  1550. // parse the @newtick marker
  1551. if((rc = _cmXScoreReorderParseTick(p, b, ln+1, &r.newTick)) != kOkXsRC )
  1552. goto errLabel;
  1553. // parse the $pitch marker
  1554. if((rc = _cmXScoreReorderParsePitch(p, b, ln+1, &r.pitch )) != kOkXsRC )
  1555. goto errLabel;
  1556. // store the record
  1557. assert( ri < rN );
  1558. rV[ri++] = r;
  1559. continue;
  1560. }
  1561. // the end of the measure was encountered -
  1562. // reorder the measure based on the cmXsReorder_t in rV[ri]
  1563. if((rc = _cmXScoreReorderMeas(p, measNumb, rV, ri )) != kOkXsRC )
  1564. goto errLabel;
  1565. ri = 0;
  1566. stateId = kFindMeasStId;
  1567. // fall through
  1568. }
  1569. case kFindMeasStId: // scanning for a bar-line
  1570. {
  1571. char colon;
  1572. if( sscanf(b,"%i %c",&measNumb,&colon) == 2 && colon == ':' )
  1573. {
  1574. //printf("meas: %i \n",measNumb);
  1575. stateId = kFindEventStId;
  1576. }
  1577. }
  1578. break;
  1579. }
  1580. }
  1581. // the ticks may have changed so the 'secs' and 'dsecs' must be updated
  1582. _cmXScoreSetAbsoluteTime( p );
  1583. // the bar lines should be the first event in the measure
  1584. _cmXScoreFixBarLines(p);
  1585. // resort to force the links to be correct
  1586. _cmXScoreSort(p);
  1587. errLabel:
  1588. cmFileClose(&fH);
  1589. cmMemFree(b);
  1590. return rc;
  1591. }
  1592. /*
  1593. kMidiFileIdColScIdx= 0,
  1594. kTypeLabelColScIdx = 3,
  1595. kDSecsColScIdx = 4,
  1596. kSecsColScIdx = 5,
  1597. kD0ColScIdx = 9,
  1598. kD1ColScIdx = 10,
  1599. kPitchColScIdx = 11,
  1600. kBarColScIdx = 13,
  1601. kSkipColScIdx = 14,
  1602. kEvenColScIdx = 15,
  1603. kGraceColScIdx = 16,
  1604. kTempoColScIdx = 17,
  1605. kFracColScIdx = 18,
  1606. kDynColScIdx = 19,
  1607. kSectionColScIdx = 20,
  1608. kRecdPlayColScIdx = 21,
  1609. kRemarkColScIdx = 22
  1610. */
  1611. cmXsRC_t _cmXScoreWriteCsvHdr( cmXScore_t* p )
  1612. {
  1613. const cmChar_t* s[] =
  1614. {
  1615. "id","trk","evt","opcode","dticks","micros","status",
  1616. "meta","ch","d0","d1","arg0","arg1","bar","skip",
  1617. "even","grace","tempo","t frac","dyn","section","play_recd","remark",NULL
  1618. };
  1619. cmCsvCell_t* lcp = NULL;
  1620. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymText(p->csvH,s[0]), 0, 0 ) != kOkCsvRC )
  1621. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  1622. unsigned i;
  1623. for(i=1; s[i]!=NULL; ++i)
  1624. {
  1625. if( cmCsvInsertIdentColAfter(p->csvH, lcp, &lcp, s[i], 0 ) != kOkCsvRC )
  1626. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV error inserting CSV title %i.\n",i);
  1627. }
  1628. return kOkXsRC;
  1629. }
  1630. cmXsRC_t _cmXScoreWriteCsvBlankCols( cmXScore_t* p, unsigned cnt, cmCsvCell_t** leftCellPtrPtr )
  1631. {
  1632. unsigned i;
  1633. for(i=0; i<cnt; ++i)
  1634. if( cmCsvInsertIdentColAfter(p->csvH,*leftCellPtrPtr,leftCellPtrPtr,"",0) != kOkCsvRC )
  1635. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV output failed on blank column.");
  1636. return kOkCsvRC;
  1637. }
  1638. const cmChar_t* _cmXScoreTranslateDynamics( cmXScore_t* p, const cmXsNote_t* np, cmChar_t* buf, unsigned bufN )
  1639. {
  1640. if( cmIsFlag(np->flags,kDynXsFl) && np->dynamics != 0 )
  1641. {
  1642. const cmChar_t* dynStr = NULL;
  1643. switch(np->dynamics)
  1644. {
  1645. case 1: dynStr = "pppp"; break;
  1646. case 2: dynStr = "ppp"; break;
  1647. case 3: dynStr = "pp"; break;
  1648. case 4: dynStr = "p"; break;
  1649. case 5: dynStr = "mp"; break;
  1650. case 6: dynStr = "mf"; break;
  1651. case 7: dynStr = "f"; break;
  1652. case 8: dynStr = "ff"; break;
  1653. case 9: dynStr = "fff"; break;
  1654. default:
  1655. cmErrMsg(&p->err,kSyntaxErrorXsRC,"An invalid dynamic value (%i) was encountered.",np->dynamics);
  1656. goto errLabel;
  1657. }
  1658. if( np->dynGroupId == 0 )
  1659. snprintf(buf,bufN,"%s",dynStr);
  1660. else
  1661. snprintf(buf,bufN,"%s %i",dynStr,np->dynGroupId);
  1662. return buf;
  1663. }
  1664. errLabel:
  1665. return "";
  1666. }
  1667. const cmChar_t* cmXsFormatMeasurementCsvField( unsigned flags, unsigned fl, char abbrev, unsigned sectionId, char* buf, unsigned bufN )
  1668. {
  1669. assert( bufN > 1 );
  1670. buf[0] = ' ';
  1671. buf[1] = 0;
  1672. if( cmIsFlag(flags,fl) )
  1673. {
  1674. if( sectionId != 0 )
  1675. snprintf(buf,bufN-1,"%c %i%c",abbrev,sectionId, cmIsFlag(flags,kHeelXsFl)?'*':' ');
  1676. else
  1677. buf[0] = abbrev;
  1678. }
  1679. return buf;
  1680. }
  1681. cmXsRC_t _cmXScoreWriteCsvRow(
  1682. cmXScore_t* p,
  1683. unsigned rowIdx,
  1684. unsigned uid,
  1685. unsigned bar,
  1686. const cmChar_t* sectionStr,
  1687. const cmChar_t* opCodeStr,
  1688. double dsecs,
  1689. double secs,
  1690. unsigned d0,
  1691. unsigned d1,
  1692. unsigned pitch, // set to -1 if the pitch is not valid
  1693. double frac,
  1694. const cmChar_t* dynStr,
  1695. unsigned flags,
  1696. const cmChar_t* evenStr,
  1697. const cmChar_t* tempoStr)
  1698. {
  1699. cmXsRC_t rc = kOkXsRC;
  1700. cmCsvCell_t* lcp = NULL;
  1701. // append an empty row to the CSV object
  1702. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymUInt(p->csvH, rowIdx ), 0, 0 ) != kOkCsvRC )
  1703. {
  1704. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  1705. goto errLabel;
  1706. }
  1707. /*
  1708. // col 0 : blanks
  1709. if( cmCsvInsertUIntColAfter(p->csvH, lcp, &lcp, rowIdx, 0 ) != kOkCsvRC )
  1710. {
  1711. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output row index failed.");
  1712. goto errLabel;
  1713. }
  1714. */
  1715. // col 1 : track (always 1)
  1716. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,1,0) != kOkCsvRC )
  1717. {
  1718. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1719. goto errLabel;
  1720. }
  1721. // col 2 : evt (set to event uid, or blank if uid == -1)
  1722. if( uid == -1 )
  1723. {
  1724. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1725. goto errLabel;
  1726. }
  1727. else
  1728. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,uid,0) != kOkCsvRC )
  1729. {
  1730. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1731. goto errLabel;
  1732. }
  1733. // col 3 : output the opcode
  1734. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,opCodeStr,0) != kOkCsvRC )
  1735. {
  1736. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on opcode label.");
  1737. goto errLabel;
  1738. }
  1739. // col 4 : dsecs
  1740. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,dsecs,0) != kOkCsvRC )
  1741. {
  1742. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dsecs'.");
  1743. goto errLabel;
  1744. }
  1745. // col 5 : secs
  1746. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,secs,0) != kOkCsvRC )
  1747. {
  1748. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'secs'.");
  1749. goto errLabel;
  1750. }
  1751. // cols 6,7,8 blanks
  1752. if((rc = _cmXScoreWriteCsvBlankCols(p,3,&lcp)) != kOkXsRC )
  1753. goto errLabel;
  1754. // col 9 : d0
  1755. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d0,0) != kOkCsvRC )
  1756. {
  1757. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1758. goto errLabel;
  1759. }
  1760. // col 10 : d1
  1761. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d1,0) != kOkCsvRC )
  1762. {
  1763. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd1'.");
  1764. goto errLabel;
  1765. }
  1766. // col 11 : pitch
  1767. if( pitch == -1 )
  1768. {
  1769. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1770. goto errLabel;
  1771. }
  1772. else
  1773. {
  1774. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmMidiToSciPitch(pitch,NULL,0),0) != kOkCsvRC )
  1775. {
  1776. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  1777. goto errLabel;
  1778. }
  1779. }
  1780. // col 12 : blanks
  1781. if((rc = _cmXScoreWriteCsvBlankCols(p,1 + (cmIsFlag(flags,kBarXsFl) ? 0 : 1), &lcp)) != kOkXsRC )
  1782. goto errLabel;
  1783. // col 13 : bar number
  1784. if( cmIsFlag(flags,kBarXsFl) )
  1785. {
  1786. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,bar,0) != kOkCsvRC )
  1787. {
  1788. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  1789. goto errLabel;
  1790. }
  1791. }
  1792. // col 14 : skip (blank for now)
  1793. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1794. goto errLabel;
  1795. // col 15: even (all grace notes are 'even' notes
  1796. if( cmCsvInsertQTextColAfter(p->csvH,lcp,&lcp, evenStr, 0) != kOkCsvRC )
  1797. {
  1798. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1799. goto errLabel;
  1800. }
  1801. // col 16: grace
  1802. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kGraceXsFl) ? "g" : "",0) != kOkCsvRC )
  1803. {
  1804. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1805. goto errLabel;
  1806. }
  1807. // col 17: tempo
  1808. if( cmCsvInsertQTextColAfter(p->csvH,lcp,&lcp,tempoStr,0) != kOkCsvRC )
  1809. {
  1810. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1811. goto errLabel;
  1812. }
  1813. // col 18: frac
  1814. if( frac == 0 )
  1815. {
  1816. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1817. goto errLabel;
  1818. }
  1819. else
  1820. {
  1821. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,1.0/frac,0) != kOkCsvRC )
  1822. {
  1823. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 't frac'.");
  1824. goto errLabel;
  1825. }
  1826. }
  1827. // col 19: dynamic marking
  1828. if( cmCsvInsertQTextColAfter(p->csvH, lcp, &lcp, dynStr, 0 ) != kOkCsvRC )
  1829. {
  1830. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dynamics'.");
  1831. goto errLabel;
  1832. }
  1833. //if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1834. // goto errLabel;
  1835. // col 20: section
  1836. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,sectionStr!=NULL ? sectionStr : "",0) != kOkCsvRC )
  1837. {
  1838. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1839. goto errLabel;
  1840. }
  1841. // col 21, 22 : recd-play, remark (blank for now)
  1842. if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
  1843. goto errLabel;
  1844. errLabel:
  1845. return rc;
  1846. }
  1847. cmXsRC_t cmXScoreWriteCsv( cmXsH_t h, const cmChar_t* csvFn )
  1848. {
  1849. cmXsRC_t rc = kOkXsRC;
  1850. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1851. unsigned rowIdx = 1;
  1852. const cmChar_t* sectionIdStr = NULL;
  1853. if( !cmCsvIsValid(p->csvH) )
  1854. return cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output object is not initialized.");
  1855. if((rc = _cmXScoreWriteCsvHdr( p )) != kOkXsRC )
  1856. goto errLabel;
  1857. cmXsPart_t* pp = p->partL;
  1858. for(; pp!=NULL; pp=pp->link)
  1859. {
  1860. cmXsMeas_t* mp = pp->measL;
  1861. for(; mp!=NULL; mp=mp->link)
  1862. {
  1863. cmXsNote_t* np = mp->noteL;
  1864. for(; np!=NULL; np=np->slink)
  1865. {
  1866. // if this is a section event
  1867. if( cmIsFlag(np->flags,kSectionXsFl) )
  1868. sectionIdStr = np->tvalue;
  1869. // if this is a bar event
  1870. if( cmIsFlag(np->flags,kBarXsFl) )
  1871. {
  1872. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,sectionIdStr,"bar",np->dsecs,np->secs,0,0,-1,0,"",np->flags,"","");
  1873. sectionIdStr = NULL;
  1874. }
  1875. else
  1876. {
  1877. // if this is a pedal event
  1878. if( cmIsFlag(np->flags,kPedalDnXsFl|kPedalUpXsFl|kPedalUpDnXsFl|kSostDnXsFl|kSostUpXsFl) )
  1879. {
  1880. unsigned d0 = cmIsFlag(np->flags,kSostDnXsFl |kSostUpXsFl) ? 66 : 64; // pedal MIDI ctl id
  1881. unsigned d1 = cmIsFlag(np->flags,kPedalDnXsFl|kSostDnXsFl) ? 64 : 0; // pedal-dn: d1>=64 pedal-up:<64
  1882. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,sectionIdStr,"ctl",np->dsecs,np->secs,d0,d1,-1,0,"",np->flags,"","");
  1883. sectionIdStr = NULL;
  1884. if( cmIsFlag(np->flags,kPedalUpDnXsFl) )
  1885. {
  1886. rowIdx += 1;
  1887. double millisecond = 0.0;
  1888. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,sectionIdStr,"ctl",millisecond,np->secs+millisecond,d0,64,-1,0,"",np->flags,"","");
  1889. }
  1890. }
  1891. else
  1892. {
  1893. // if this is a sounding note event
  1894. if( cmIsFlag(np->flags,kOnsetXsFl) )
  1895. {
  1896. unsigned bufN = 128;
  1897. cmChar_t ebuf[ bufN+1]; ebuf[bufN] = 0;
  1898. cmChar_t dbuf[ bufN+1]; dbuf[bufN] = 0;
  1899. cmChar_t tbuf[ bufN+1]; tbuf[bufN] = 0;
  1900. double frac = np->rvalue + (cmIsFlag(np->flags,kDotXsFl) ? (np->rvalue/2) : 0);
  1901. const cmChar_t* dyn = _cmXScoreTranslateDynamics( p, np, dbuf, bufN );
  1902. unsigned vel = np->vel==0 ? 60 : np->vel;
  1903. //
  1904. _cmXScoreWriteCsvRow(p,rowIdx,np->uid,mp->number,sectionIdStr,"non",np->dsecs,np->secs,np->pitch,vel,np->pitch,frac,dyn,np->flags,
  1905. cmXsFormatMeasurementCsvField(np->flags, kEvenXsFl, 'e', np->evenGroupId, ebuf, bufN ),
  1906. cmXsFormatMeasurementCsvField(np->flags, kTempoXsFl,'t', np->tempoGroupId, tbuf, bufN ));
  1907. sectionIdStr = NULL;
  1908. }
  1909. }
  1910. }
  1911. rowIdx += 1;
  1912. }
  1913. }
  1914. }
  1915. // Section labels are output on the next bar/pedal/note
  1916. // but what if there is no bar pedal note-on after the section label
  1917. if( sectionIdStr != NULL )
  1918. cmErrMsg(&p->err,kSyntaxErrorXsRC,"The section label '%s' was ignored because it was not followed by a score event.",sectionIdStr);
  1919. if( cmCsvWrite( p->csvH, csvFn ) != kOkCsvRC )
  1920. rc = cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output write failed on file '%s'.",csvFn);
  1921. errLabel:
  1922. return rc;
  1923. }
  1924. void _cmXScoreReportTitle( cmRpt_t* rpt )
  1925. {
  1926. cmRptPrintf(rpt," idx voc loc tick durtn rval flags\n");
  1927. cmRptPrintf(rpt," --- --- ----- ------- ----- ---- --- ---------------\n");
  1928. }
  1929. void _cmXScoreReportNote( cmRpt_t* rpt, const cmXsNote_t* note,unsigned index )
  1930. {
  1931. const cmChar_t* B = cmIsFlag(note->flags,kBarXsFl) ? "|" : "-";
  1932. const cmChar_t* R = cmIsFlag(note->flags,kRestXsFl) ? "R" : "-";
  1933. const cmChar_t* G = cmIsFlag(note->flags,kGraceXsFl) ? "G" : "-";
  1934. const cmChar_t* D = cmIsFlag(note->flags,kDotXsFl) ? "." : "-";
  1935. const cmChar_t* C = cmIsFlag(note->flags,kChordXsFl) ? "C" : "-";
  1936. const cmChar_t* e = cmIsFlag(note->flags,kEvenXsFl) ? "e" : "-";
  1937. const cmChar_t* d = cmIsFlag(note->flags,kDynXsFl) ? "d" : "-";
  1938. const cmChar_t* t = cmIsFlag(note->flags,kTempoXsFl) ? "t" : "-";
  1939. const cmChar_t* P = cmIsFlag(note->flags,kPedalDnXsFl) ? "V" : "-";
  1940. const cmChar_t* s = cmIsFlag(note->flags,kSostDnXsFl) ? "{" : "-";
  1941. const cmChar_t* S = cmIsFlag(note->flags,kSectionXsFl) ? "S" : "-";
  1942. const cmChar_t* H = cmIsFlag(note->flags,kHeelXsFl) ? "H" : "-";
  1943. const cmChar_t* T0 = cmIsFlag(note->flags,kTieBegXsFl) ? "T" : "-";
  1944. const cmChar_t* T1 = cmIsFlag(note->flags,kTieEndXsFl) ? "_" : "-";
  1945. const cmChar_t* O = cmIsFlag(note->flags,kOnsetXsFl) ? "*" : "-";
  1946. P = cmIsFlag(note->flags,kPedalUpXsFl) ? "^" : P;
  1947. P = cmIsFlag(note->flags,kPedalUpDnXsFl) ? "X" : P;
  1948. s = cmIsFlag(note->flags,kSostUpXsFl) ? "}" : s;
  1949. //const cmChar_t* N = note->pitch==0 ? " " : cmMidiToSciPitch( note->pitch, NULL, 0 );
  1950. cmChar_t N[] = {'\0','\0','\0','\0'};
  1951. cmChar_t acc = note->alter==-1?'b':(note->alter==1?'#':' ');
  1952. snprintf(N,4,"%c%c%1i",note->step,acc,note->octave);
  1953. cmRptPrintf(rpt," %3i %3i %5i %7i %5i %4.1f %3s %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  1954. index,
  1955. note->voice->id,
  1956. note->locIdx,
  1957. note->tick,
  1958. note->duration,
  1959. note->rvalue,
  1960. N,B,R,G,D,C,e,d,t,P,s,S,H,T0,T1,O);
  1961. if( cmIsFlag(note->flags,kSectionXsFl) )
  1962. cmRptPrintf(rpt," %s",cmStringNullGuard(note->tvalue));
  1963. if( cmIsFlag(note->flags,kMetronomeXsFl) )
  1964. cmRptPrintf(rpt," %i bpm",note->duration);
  1965. if( note->evenGroupId != 0 )
  1966. cmRptPrintf(rpt," e=%i",note->evenGroupId);
  1967. if( note->dynGroupId != 0 )
  1968. cmRptPrintf(rpt," d=%i",note->dynGroupId);
  1969. if( note->tempoGroupId != 0 )
  1970. cmRptPrintf(rpt," t=%i",note->tempoGroupId);
  1971. }
  1972. void cmXScoreReport( cmXsH_t h, cmRpt_t* rpt, bool sortFl )
  1973. {
  1974. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1975. cmXsPart_t* pp = p->partL;
  1976. for(; pp!=NULL; pp=pp->link)
  1977. {
  1978. cmRptPrintf(rpt,"Part:%s\n",pp->idStr);
  1979. const cmXsMeas_t* meas = pp->measL;
  1980. for(; meas!=NULL; meas=meas->link)
  1981. {
  1982. unsigned idx = 0;
  1983. cmRptPrintf(rpt," %i : div:%i beat:%i beat-type:%i (%i)\n",meas->number,meas->divisions,meas->beats,meas->beat_type,meas->divisions*meas->beats);
  1984. _cmXScoreReportTitle(rpt);
  1985. if( sortFl )
  1986. {
  1987. const cmXsNote_t* note = meas->noteL;
  1988. unsigned t0 = 0;
  1989. unsigned t1 = 0;
  1990. for(; note!=NULL; note=note->slink,++idx)
  1991. {
  1992. _cmXScoreReportNote(rpt,note,idx);
  1993. t1 = note->slink==NULL ? note->tick : note->slink->tick;
  1994. if( !(t0 <= note->tick && note->tick <= t1) )
  1995. {
  1996. cmRptPrintf(rpt," +");
  1997. }
  1998. t0 = note->tick;
  1999. if( note->slink!=NULL || note->voice->id==0)
  2000. cmRptPrintf(rpt,"\n");
  2001. else
  2002. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  2003. }
  2004. }
  2005. else
  2006. {
  2007. const cmXsVoice_t* v = meas->voiceL;
  2008. for(; v!=NULL; v=v->link)
  2009. {
  2010. const cmXsNote_t* note = v->noteL;
  2011. cmRptPrintf(rpt," voice:%i\n",v->id);
  2012. for(; note!=NULL; note=note->mlink)
  2013. {
  2014. _cmXScoreReportNote(rpt,note,idx);
  2015. if( note->mlink!=NULL || note->voice->id==0)
  2016. cmRptPrintf(rpt,"\n");
  2017. else
  2018. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  2019. }
  2020. }
  2021. }
  2022. }
  2023. }
  2024. }
  2025. cmXsRC_t cmXScoreWriteMidi( cmXsH_t h, const cmChar_t* fn )
  2026. {
  2027. assert(0); // function not implemented
  2028. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  2029. cmXsPart_t* pp = p->partL;
  2030. for(; pp!=NULL; pp=pp->link)
  2031. {
  2032. const cmXsMeas_t* meas = pp->measL;
  2033. for(; meas!=NULL; meas=meas->link)
  2034. {
  2035. const cmXsNote_t* note = meas->noteL;
  2036. for(; note!=NULL; note=note->slink)
  2037. {
  2038. }
  2039. }
  2040. }
  2041. }
  2042. cmXsRC_t cmXScoreTest(
  2043. cmCtx_t* ctx,
  2044. const cmChar_t* xmlFn,
  2045. const cmChar_t* midiFn,
  2046. const cmChar_t* outFn,
  2047. const cmChar_t* reorderFn )
  2048. {
  2049. cmXsRC_t rc;
  2050. cmXsH_t h = cmXsNullHandle;
  2051. if((rc = cmXScoreInitialize( ctx, &h, xmlFn, midiFn)) != kOkXsRC )
  2052. return cmErrMsg(&ctx->err,rc,"XScore alloc failed.");
  2053. if( reorderFn != NULL )
  2054. cmXScoreReorder(h,reorderFn);
  2055. if( outFn != NULL )
  2056. {
  2057. cmScH_t scH = cmScNullHandle;
  2058. double srate = 44100.0;
  2059. cmXScoreWriteCsv(h,outFn);
  2060. cmSymTblH_t stH = cmSymTblCreate(cmSymTblNullHandle, 0, ctx );
  2061. if( cmScoreInitialize( ctx, &scH, outFn, srate, NULL, 0, NULL, NULL, stH) != kOkScRC )
  2062. cmErrMsg(&ctx->err,kFileFailXsRC,"The generated CSV file could not be parsed.");
  2063. else
  2064. {
  2065. //cmScorePrintSets(scH,&ctx->rpt);
  2066. //cmScorePrint(scH,&ctx->rpt);
  2067. cmScoreFinalize(&scH);
  2068. }
  2069. cmSymTblDestroy(&stH);
  2070. }
  2071. cmXScoreReport(h,&ctx->rpt,true);
  2072. return cmXScoreFinalize(&h);
  2073. }