libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmXScore.c 67KB

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