libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmXScore.c 91KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306
  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 "cmFileSys.h"
  14. #include "cmXScore.h"
  15. #include "cmTime.h"
  16. #include "cmMidi.h"
  17. #include "cmMidiFile.h"
  18. #include "cmLex.h"
  19. #include "cmCsv.h"
  20. #include "cmSymTbl.h"
  21. #include "cmScore.h"
  22. #include "cmFile.h"
  23. #include "cmSymTbl.h"
  24. #include "cmAudioFile.h"
  25. #include "cmAudioFile.h"
  26. #include "cmProcObj.h"
  27. #include "cmProcTemplate.h"
  28. #include "cmProc.h"
  29. #include "cmProc2.h"
  30. #include "cmProc5.h"
  31. #include "cmSvgWriter.h"
  32. cmXsH_t cmXsNullHandle = cmSTATIC_NULL_HANDLE;
  33. enum
  34. {
  35. kSectionXsFl = 0x00000001, // rvalue holds section number
  36. kBarXsFl = 0x00000002,
  37. kRestXsFl = 0x00000004,
  38. kGraceXsFl = 0x00000008,
  39. kDotXsFl = 0x00000010,
  40. kChordXsFl = 0x00000020,
  41. kDynXsFl = 0x00000040,
  42. kEvenXsFl = 0x00000080,
  43. kTempoXsFl = 0x00000100,
  44. kHeelXsFl = 0x00000200,
  45. kTieBegXsFl = 0x00000400,
  46. kTieEndXsFl = 0x00000800,
  47. kTieProcXsFl = 0x00001000,
  48. kDampDnXsFl = 0x00002000,
  49. kDampUpXsFl = 0x00004000,
  50. kDampUpDnXsFl = 0x00008000,
  51. kSostDnXsFl = 0x00010000,
  52. kSostUpXsFl = 0x00020000,
  53. kMetronomeXsFl = 0x00040000, // duration holds BPM
  54. kOnsetXsFl = 0x00080000, // this is a sounding note
  55. kBegGroupXsFl = 0x00100000,
  56. kEndGroupXsFl = 0x00200000,
  57. kBegGraceXsFl = 0x00400000, // (b) beg grace note group
  58. kEndGraceXsFl = 0x00800000, // end grace note group
  59. kAddGraceXsFl = 0x01000000, // (a) end grace note group operator flag - add time
  60. kSubGraceXsFl = 0x02000000, // (s) " " " " " " - subtract time
  61. kAFirstGraceXsFl = 0x04000000, // (A) add time after first note
  62. kNFirstGraceXsFl = 0x08000000 // (n) grace notes start as soon as possible after first note and add time
  63. };
  64. struct cmXsMeas_str;
  65. struct cmXsVoice_str;
  66. typedef struct cmXsNote_str
  67. {
  68. unsigned uid; // unique id of this note record
  69. unsigned flags; // See k???XsFl
  70. unsigned pitch; // midi pitch
  71. unsigned velocity; // midi velocity
  72. unsigned dynamics; // dynamic level 1=pppp 9=fff
  73. unsigned vel; // score specified MIDI velocity
  74. cmChar_t step; // A-G
  75. unsigned octave; // sci pitch octave
  76. int alter; // +n=sharps,-n=flats
  77. unsigned staff; // 1=treble 2=bass
  78. unsigned tick; //
  79. unsigned duration; // duration in ticks
  80. double secs; // absolute time in seconds
  81. double dsecs; // delta time in seconds since previous event
  82. unsigned locIdx; // location index (chords share the same location index)
  83. 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, ...)
  84. const cmChar_t* tvalue; // text value
  85. unsigned evenGroupId; // eveness group id
  86. unsigned dynGroupId; // dynamics group id
  87. unsigned tempoGroupId; // tempo group id
  88. unsigned graceGroupId; // grace note group id
  89. struct cmXsVoice_str* voice; // voice this note belongs to
  90. struct cmXsMeas_str* meas; // measure this note belongs to
  91. const cmXmlNode_t* xmlNode; // note xml ptr
  92. struct cmXsNote_str* tied; // subsequent note tied to this note
  93. struct cmXsNote_str* grace; // grace note groups link backward in time from the anchor note
  94. struct cmXsNote_str* mlink; // measure note list
  95. struct cmXsNote_str* slink; // time sorted event list
  96. } cmXsNote_t;
  97. typedef struct cmXsVoice_str
  98. {
  99. unsigned id; // Voice id
  100. cmXsNote_t* noteL; // List of notes in this voice
  101. struct cmXsVoice_str* link; // Link to other voices in this measure
  102. } cmXsVoice_t;
  103. typedef struct cmXsSpan_str
  104. {
  105. unsigned staff;
  106. unsigned number;
  107. struct cmXsMeas_str* meas;
  108. unsigned tick0;
  109. unsigned tick1;
  110. int pitch_offset;
  111. struct cmXsSpan_str* link;
  112. } cmXsSpan_t;
  113. typedef struct cmXsMeas_str
  114. {
  115. unsigned number; // Measure number
  116. unsigned divisions; // ticks-per-quarter-note
  117. unsigned beats; // beats per measure
  118. unsigned beat_type; // whole/half/quarter/eighth ...
  119. cmXsVoice_t* voiceL; // List of voices in this measure
  120. cmXsNote_t* noteL; // List of time sorted notes in this measure
  121. struct cmXsMeas_str* link; // Link to other measures in this part.
  122. } cmXsMeas_t;
  123. typedef struct cmXsPart_str
  124. {
  125. const cmChar_t* idStr; // Id of this part
  126. cmXsMeas_t* measL; // List of measures in this part.
  127. struct cmXsPart_str* link; // Link to other parts in this score
  128. } cmXsPart_t;
  129. typedef struct
  130. {
  131. cmErr_t err;
  132. cmXmlH_t xmlH;
  133. cmLHeapH_t lhH;
  134. cmXsPart_t* partL;
  135. cmCsvH_t csvH;
  136. cmXsSpan_t* spanL;
  137. unsigned nextUid;
  138. } cmXScore_t;
  139. cmXScore_t* _cmXScoreHandleToPtr( cmXsH_t h )
  140. {
  141. cmXScore_t* p = (cmXScore_t*)h.h;
  142. assert( p != NULL );
  143. return p;
  144. }
  145. cmXsRC_t _cmXScoreFinalize( cmXScore_t* p )
  146. {
  147. cmXsRC_t rc = kOkXsRC;
  148. // release the XML file
  149. cmXmlFree( &p->xmlH );
  150. // release the local linked heap memory
  151. cmLHeapDestroy(&p->lhH);
  152. // release the CSV output object
  153. cmCsvFinalize(&p->csvH);
  154. cmMemFree(p);
  155. return rc;
  156. }
  157. cmXsRC_t _cmXScoreMissingNode( cmXScore_t* p, const cmXmlNode_t* parent, const cmChar_t* label )
  158. {
  159. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML node '%s'. Parent line:%i",label,parent->line);
  160. }
  161. cmXsRC_t _cmXScoreMissingAttribute( cmXScore_t* p, const cmXmlNode_t* np, const cmChar_t* attrLabel )
  162. {
  163. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML attribute '%s' from node '%s'.",attrLabel,np->label);
  164. }
  165. cmXsVoice_t* _cmXScoreIdToVoice( cmXsMeas_t* meas, unsigned voiceId )
  166. {
  167. cmXsVoice_t* v = meas->voiceL;
  168. for(; v!=NULL; v=v->link)
  169. if( v->id == voiceId )
  170. return v;
  171. return NULL;
  172. }
  173. cmXsRC_t _cmXScorePushNote( cmXScore_t* p, cmXsMeas_t* meas, unsigned voiceId, cmXsNote_t* note )
  174. {
  175. cmXsVoice_t* v;
  176. // get the voice recd
  177. if((v = _cmXScoreIdToVoice(meas,voiceId)) == NULL)
  178. {
  179. // the voice recd doesn't exist for this voiceId - allocate one
  180. v = cmLhAllocZ(p->lhH,cmXsVoice_t,1);
  181. v->id = voiceId;
  182. // add the voice record to the meas->voiceL
  183. if( meas->voiceL == NULL )
  184. meas->voiceL = v;
  185. else
  186. {
  187. cmXsVoice_t* vp = meas->voiceL;
  188. while( vp->link!=NULL )
  189. vp = vp->link;
  190. vp->link = v;
  191. }
  192. }
  193. // add the note record to the end of meas->voiceL
  194. if( v->noteL == NULL )
  195. v->noteL = note;
  196. else
  197. {
  198. cmXsNote_t* n = v->noteL;
  199. while( n->mlink != NULL )
  200. n = n->mlink;
  201. n->mlink = note;
  202. }
  203. note->voice = v;
  204. note->uid = p->nextUid++;
  205. return kOkXsRC;
  206. }
  207. void _cmXScoreInsertNoteBefore( cmXsNote_t* note, cmXsNote_t* nn )
  208. {
  209. // insert the new note into the voice list before 'note'
  210. cmXsNote_t* n0 = NULL;
  211. cmXsNote_t* n1 = note->voice->noteL;
  212. for(; n1 != NULL; n1=n1->mlink )
  213. {
  214. if( n1->uid == note->uid )
  215. {
  216. if( n0 == NULL )
  217. note->voice->noteL = nn;
  218. else
  219. n0->mlink = nn;
  220. nn->mlink = note;
  221. break;
  222. }
  223. n0 = n1;
  224. }
  225. assert( n1 != NULL );
  226. // insert the new note into the time sorted note list before 'note'
  227. n0 = NULL;
  228. n1 = note->meas->noteL;
  229. for(; n1!=NULL; n1=n1->slink)
  230. {
  231. if( n1->tick >= nn->tick )
  232. {
  233. if( n0 == NULL )
  234. note->meas->noteL = nn;
  235. else
  236. n0->slink = nn;
  237. nn->slink = n1;
  238. break;
  239. }
  240. n0 = n1;
  241. }
  242. assert( n1 != NULL );
  243. }
  244. void _cmXScoreInsertNoteAfter( cmXsNote_t* note, cmXsNote_t* nn )
  245. {
  246. // insert the new note into the voice list after 'note'
  247. cmXsNote_t* n0 = note->voice->noteL;
  248. for(; n0 != NULL; n0=n0->mlink )
  249. if( n0->uid == note->uid )
  250. {
  251. nn->mlink = note->mlink;
  252. note->mlink = nn;
  253. break;
  254. }
  255. assert(n0 != NULL );
  256. // insert the new note into the time sorted note list after 'note'
  257. n0 = note->meas->noteL;
  258. for(; n0!=NULL; n0=n0->slink)
  259. {
  260. if( n0->tick >= nn->tick )
  261. {
  262. nn->slink = n0->slink;
  263. n0->slink = nn;
  264. break;
  265. }
  266. // if n0 is the last ele in the list
  267. if( n0->slink == NULL )
  268. {
  269. n0->slink = nn;
  270. nn->slink = NULL;
  271. break;
  272. }
  273. }
  274. assert(n0 != NULL);
  275. }
  276. cmXsRC_t _cmXScoreParsePartList( cmXScore_t* p )
  277. {
  278. cmXsRC_t rc = kOkXsRC;
  279. cmXsPart_t* lastPartPtr = NULL;
  280. const cmXmlNode_t* xnp;
  281. // find the 'part-list'
  282. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part-list", NULL, 0)) == NULL )
  283. return _cmXScoreMissingNode(p,cmXmlRoot(p->xmlH),"part-list");
  284. const cmXmlNode_t* cnp = xnp->children;
  285. // for each child of the 'part-list'
  286. for(; cnp!=NULL; cnp=cnp->sibling)
  287. if( cmTextCmp( cnp->label, "score-part" ) == 0 )
  288. {
  289. const cmXmlAttr_t* a;
  290. // find the 'score-part' id
  291. if((a = cmXmlFindAttrib(cnp,"id")) == NULL )
  292. return _cmXScoreMissingAttribute(p,cnp,"id");
  293. // allocate a new part record
  294. cmXsPart_t* pp = cmLhAllocZ(p->lhH,cmXsPart_t,1);
  295. pp->idStr = a->value; // set the part id
  296. // link the new part record to the end of the part list
  297. if(lastPartPtr == NULL)
  298. p->partL = pp;
  299. else
  300. lastPartPtr->link = pp;
  301. lastPartPtr = pp;
  302. }
  303. return rc;
  304. }
  305. cmXsRC_t _cmXScoreParsePitch( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* np )
  306. {
  307. cmXsRC_t rc = kOkXsRC;
  308. unsigned octave = 0;
  309. double alter = 0;
  310. const cmChar_t* step = NULL;
  311. if((step = cmXmlNodeValue(nnp,"pitch","step",NULL)) == NULL )
  312. return _cmXScoreMissingNode(p,nnp,"step");
  313. if((rc = cmXmlNodeUInt( nnp,&octave,"pitch","octave",NULL)) != kOkXmlRC )
  314. return _cmXScoreMissingNode(p,nnp,"octave");
  315. cmXmlNodeDouble( nnp,&alter,"pitch","alter",NULL);
  316. int acc = alter;
  317. unsigned midi = cmSciPitchToMidiPitch(*step,acc,octave);
  318. np->pitch = midi;
  319. np->step = *step;
  320. np->octave = octave;
  321. np->alter = alter;
  322. np->flags |= kOnsetXsFl;
  323. return rc;
  324. }
  325. cmXsRC_t _cmXScoreParseNoteRValue( cmXScore_t* p, const cmXmlNode_t* nnp, const cmChar_t* label, double* rvalueRef )
  326. {
  327. typedef struct map_str
  328. {
  329. double rvalue;
  330. const cmChar_t* label;
  331. } map_t;
  332. map_t mapV[] =
  333. {
  334. {-1.0, "measure" }, // whole measure rest
  335. { 0.5, "breve" }, // double whole
  336. { 1.0, "whole" },
  337. { 2.0, "half" },
  338. { 4.0, "quarter" },
  339. { 8.0, "eighth" },
  340. {16.0, "16th" },
  341. {32.0, "32nd" },
  342. { 64.0, "64th" },
  343. {128.0, "128th" },
  344. { 0.0, "" }
  345. };
  346. const cmChar_t* str;
  347. // get the XML rvalue label
  348. if((str = cmXmlNodeValue(nnp,label,NULL)) == NULL)
  349. {
  350. if((nnp = cmXmlSearch(nnp,"rest",NULL,0)) != NULL )
  351. {
  352. const cmXmlAttr_t* a;
  353. if((a = cmXmlFindAttrib(nnp,"measure")) != NULL && cmTextCmp(a->value,"yes")==0)
  354. {
  355. *rvalueRef = -1;
  356. return kOkXsRC;
  357. }
  358. }
  359. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'beat-unit' metronome value is missing on line %i.",nnp->line);
  360. }
  361. unsigned i;
  362. // lookup the rvalue numeric value from the mapV[] table
  363. for(i=0; mapV[i].rvalue!=0; ++i)
  364. if( cmTextCmp(mapV[i].label,str) == 0 )
  365. {
  366. *rvalueRef = mapV[i].rvalue;
  367. return kOkXsRC;
  368. }
  369. // the rvalue label was not found
  370. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unknown rvalue type='%s'.",str);
  371. }
  372. cmXsRC_t _cmXScoreParseColor( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* note )
  373. {
  374. cmXsRC_t rc = kOkXsRC;
  375. const cmXmlAttr_t* a;
  376. typedef struct map_str
  377. {
  378. unsigned value;
  379. const cmChar_t* label;
  380. } map_t;
  381. map_t mapV[] =
  382. {
  383. { kEvenXsFl, "#0000FF" }, // blue (even)
  384. { kTempoXsFl, "#00FF00" }, // green (tempo)
  385. { kDynXsFl, "#FF0000" }, // red (dynamics)
  386. { kTempoXsFl | kEvenXsFl, "#00FFFF" }, // green + blue (turquoise)
  387. { kDynXsFl | kEvenXsFl, "#FF00FF" }, // red + blue
  388. { kDynXsFl | kEvenXsFl, "#FF0CF7" }, // magenta (even+dyn)
  389. { kDynXsFl | kTempoXsFl, "#FF7F00" }, // red + green (brown)
  390. { kTempoXsFl | kEvenXsFl | kDynXsFl, "#996633" }, // (purple)
  391. { kDynXsFl, "#FF6A03" }, // 176 orange (dynamics)
  392. { kEvenXsFl, "#2F00E8" }, // 1001 blue (even)
  393. { kTempoXsFl, "#01CD1F" }, // 1196 green (tempo)
  394. { kEvenXsFl, "#3600E8" }, // 1627 blue (even)
  395. { kDynXsFl | kTempoXsFl, "#9E8F15" }, // 8827 brown (dyn + tempo)
  396. { kEvenXsFl, "#2E00E6" }, // 5393 blue (even)
  397. { kEvenXsFl, "#2C00DD" }, // 5895 blue (even)
  398. { kDynXsFl, "#FF5B03" }, // 6498 orange (dyn)
  399. { kDynXsFl, "#FF6104" }, // 6896 orange
  400. { kEvenXsFl, "#2A00E6" }, // 7781 blue
  401. { kEvenXsFl, "#2300DD" }, // 8300 blue (even)
  402. { kTempoXsFl, "#03CD22" }, // 10820 green (tempo)
  403. { kEvenXsFl, "#3400DB" }, // 11627 blue (dyn)
  404. { -1, "" }
  405. };
  406. /*
  407. orange #FF6A03
  408. magenta #FF0CF7
  409. blue #2F00E8
  410. green #01CD1F
  411. gold #9E8F15
  412. green #03CD22
  413. */
  414. if((a = cmXmlFindAttrib(nnp, "color" )) != NULL )
  415. {
  416. unsigned i;
  417. for(i=0; mapV[i].value != -1; ++i)
  418. if( cmTextCmp(a->value,mapV[i].label) == 0 )
  419. {
  420. note->flags += mapV[i].value;
  421. break;
  422. }
  423. if( mapV[i].value == -1 )
  424. cmErrMsg(&p->err,kSyntaxErrorXsRC,"The note color '%s' was not found on line %i.",a->value,nnp->line);
  425. }
  426. return rc;
  427. }
  428. // On input tick0Ref is set to the tick of the previous event.
  429. // On input tickRef is set to the tick of this event.
  430. // On output tick0Ref is set to the tick of this event.
  431. // On output tickRef is set to the tick of the next event.
  432. cmXsRC_t _cmXScoreParseNote(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* nnp, unsigned* tick0Ref, unsigned* tickRef )
  433. {
  434. cmXsRC_t rc = kOkXsRC;
  435. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  436. unsigned voiceId;
  437. note->meas = meas;
  438. note->xmlNode = nnp;
  439. // get the voice id for this node
  440. if( cmXmlNodeUInt(nnp,&voiceId,"voice",NULL) != kOkXmlRC )
  441. return _cmXScoreMissingNode(p,nnp,"voice");
  442. // if this note has a pitch
  443. if( cmXmlNodeHasChild(nnp,"pitch",NULL) )
  444. if((rc = _cmXScoreParsePitch(p,nnp,note)) != kOkXsRC )
  445. return rc;
  446. cmXmlNodeUInt(nnp,&note->duration,"duration",NULL); // get the note duration
  447. cmXmlNodeUInt(nnp,&note->staff,"staff",NULL); // get th staff number
  448. // is 'rest'
  449. if( cmXmlNodeHasChild(nnp,"rest",NULL) )
  450. note->flags |= kRestXsFl;
  451. // is 'grace'
  452. if( cmXmlNodeHasChild(nnp,"grace",NULL) )
  453. note->flags |= kGraceXsFl;
  454. // is 'dot'
  455. if( cmXmlNodeHasChild(nnp,"dot",NULL) )
  456. note->flags |= kDotXsFl;
  457. // is 'chord'
  458. if( cmXmlNodeHasChild(nnp,"chord",NULL) )
  459. note->flags |= kChordXsFl;
  460. // is this is first note in a tied pair
  461. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","start",NULL) )
  462. note->flags |= kTieBegXsFl;
  463. // is this is second note in a tied pair
  464. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","stop",NULL) )
  465. note->flags |= kTieEndXsFl;
  466. // has 'heel' mark
  467. if( cmXmlNodeHasChild(nnp,"notations","technical","heel",NULL) )
  468. note->flags |= kHeelXsFl;
  469. // set color coded flags
  470. if((rc = _cmXScoreParseColor(p, nnp, note )) != kOkXsRC )
  471. return rc;
  472. // get the note's rythmic value
  473. if((rc = _cmXScoreParseNoteRValue(p,nnp,"type",&note->rvalue)) != kOkXsRC )
  474. return rc;
  475. // if this is a chord note
  476. if( cmIsFlag(note->flags,kChordXsFl) )
  477. {
  478. note->tick = *tick0Ref; // then use the onset time from the previous note and do not advance time
  479. }
  480. else
  481. {
  482. *tick0Ref = *tickRef;
  483. note->tick = *tickRef;
  484. *tickRef += note->duration;
  485. }
  486. return _cmXScorePushNote(p, meas, voiceId, note );
  487. }
  488. 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 )
  489. {
  490. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  491. unsigned voiceId = 0; // non-note's are always assigned to voiceId=0;
  492. note->tick = tick;
  493. note->flags = flags;
  494. note->rvalue = rvalue;
  495. note->tvalue = tvalue;
  496. note->duration = duration;
  497. note->meas = meas;
  498. note->xmlNode = noteXmlNode;
  499. return _cmXScorePushNote(p, meas, voiceId, note );
  500. }
  501. cmXsSpan_t* _cmXScoreFindOpenOctaveShift( cmXScore_t* p, unsigned staff, unsigned number )
  502. {
  503. cmXsSpan_t* s = p->spanL;
  504. for(; s!=NULL; s=s->link)
  505. if( s->tick1 == -1 && s->staff == staff && s->number == number )
  506. return s;
  507. return NULL;
  508. }
  509. cmXsRC_t _cmXScorePushOctaveShift(cmXScore_t* p, cmXsMeas_t* meas, unsigned staff, unsigned span_number, const cmChar_t* type_str, unsigned tick)
  510. {
  511. assert( meas != NULL);
  512. cmXsSpan_t* s;
  513. if( cmTextCmp(type_str,"stop") == 0 )
  514. {
  515. if((s = _cmXScoreFindOpenOctaveShift(p,staff,span_number)) == NULL )
  516. return cmErrWarnMsg(&p->err,kUnterminatedOctaveShiftXsrRC,"An illegal octave shift was encounted in meas %i.\n",meas->number);
  517. s->tick1 = tick;
  518. }
  519. else
  520. {
  521. s = cmLhAllocZ(p->lhH,cmXsSpan_t,1);
  522. s->staff = staff;
  523. s->meas = meas;
  524. s->number = span_number;
  525. s->tick0 = tick;
  526. s->tick1 = -1;
  527. s->pitch_offset = cmTextCmp(type_str,"up")==0 ? -12 : 12;
  528. s->link = p->spanL;
  529. p->spanL = s;
  530. }
  531. return kOkXsRC;
  532. }
  533. cmXsRC_t _cmXScoreParseDirection(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* dnp, unsigned tick)
  534. {
  535. cmXsRC_t rc = kOkXsRC;
  536. const cmXmlNode_t* np = NULL;
  537. const cmXmlAttr_t* a = NULL;
  538. unsigned flags = 0;
  539. int offset = 0;
  540. double rvalue = 0;
  541. const cmChar_t* tvalue = NULL;
  542. unsigned duration = 0;
  543. bool pushFl = true;
  544. unsigned staff = 0;
  545. cmXmlNodeInt( dnp, &offset, "offset", NULL );
  546. cmXmlNodeUInt(dnp, &staff, "staff", NULL );
  547. // if this is a metronome direction
  548. if((np = cmXmlSearch( dnp, "metronome", NULL, 0)) != NULL )
  549. {
  550. if( cmXmlNodeUInt(np,&duration,"per-minute",NULL) != kOkXmlRC )
  551. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'per-minute' metronome value is missing on line %i.",np->line);
  552. if((rc = _cmXScoreParseNoteRValue(p,np,"beat-unit",&rvalue)) != kOkXsRC )
  553. return rc;
  554. flags = kMetronomeXsFl;
  555. }
  556. else
  557. // if this is a pedal direction
  558. if((np = cmXmlSearch( dnp, "pedal",NULL,0)) != NULL )
  559. {
  560. if((a = cmXmlFindAttrib(np,"type")) == NULL )
  561. return _cmXScoreMissingAttribute(p, np, "type" );
  562. if( cmTextCmp(a->value,"start") == 0 )
  563. flags = kDampDnXsFl;
  564. else
  565. if( cmTextCmp(a->value,"change") == 0 )
  566. flags = kDampUpDnXsFl;
  567. else
  568. if( cmTextCmp(a->value,"stop") == 0 )
  569. flags = kDampUpXsFl;
  570. else
  571. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unrecognized pedal type:'%s'.",cmStringNullGuard(a->value));
  572. }
  573. else
  574. // if this is a 'words' direction
  575. if((np = cmXmlSearch( dnp, "words", NULL, 0)) != NULL )
  576. {
  577. if((a = cmXmlFindAttrib(np,"enclosure")) != NULL && cmTextCmp(a->value,"rectangle")==0 )
  578. {
  579. if( cmTextIsEmpty( tvalue = np->dataStr ) )
  580. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Section number is blank or missing on line %i.",np->line);
  581. flags = kSectionXsFl;
  582. }
  583. }
  584. else
  585. // if this is an 'octave-shift' direction
  586. if((np = cmXmlSearch( dnp, "octave-shift", NULL, 0)) != NULL )
  587. {
  588. unsigned span_number = -1;
  589. if( cmXmlAttrUInt(np,"number",&span_number) != kOkXmlRC )
  590. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'number' attribute.");
  591. if((a = cmXmlFindAttrib(np,"type")) == NULL)
  592. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'type' attribute.");
  593. rc = _cmXScorePushOctaveShift(p,meas,staff,span_number,a->value,tick+offset);
  594. pushFl = false;
  595. }
  596. else
  597. {
  598. pushFl = false;
  599. }
  600. if( pushFl )
  601. rc = _cmXScorePushNonNote(p,meas,dnp,tick+offset,duration,rvalue,tvalue,flags);
  602. return rc;
  603. }
  604. // On input tickRef is set to the absolute tick of the bar line and on output it is set
  605. // to the absolute tick of the next bar line.
  606. cmXsRC_t _cmXScoreParseMeasure(cmXScore_t* p, cmXsPart_t* pp, const cmXmlNode_t* mnp, unsigned* tickRef)
  607. {
  608. cmXsRC_t rc = kOkXsRC;
  609. const cmXmlNode_t* np = NULL;
  610. unsigned tick = *tickRef;
  611. unsigned tick0= 0;
  612. cmXsMeas_t* m = NULL;
  613. // allocate the 'measure' record
  614. cmXsMeas_t* meas = cmLhAllocZ(p->lhH,cmXsMeas_t,1);
  615. // get measure number
  616. if( cmXmlAttrUInt(mnp,"number", &meas->number) != kOkXmlRC )
  617. return _cmXScoreMissingAttribute(p,mnp,"number");
  618. if( pp->measL == NULL )
  619. pp->measL = meas;
  620. else
  621. {
  622. m = pp->measL;
  623. while( m->link != NULL )
  624. m = m->link;
  625. m->link = meas;
  626. meas->divisions = m->divisions;
  627. meas->beats = m->beats;
  628. meas->beat_type = m->beat_type;
  629. }
  630. // get measure attributes node
  631. if((np = cmXmlSearch(mnp,"attributes",NULL,0)) != NULL)
  632. {
  633. cmXmlNodeUInt(np,&meas->divisions,"divisions",NULL);
  634. cmXmlNodeUInt(np,&meas->beats, "time","beats",NULL);
  635. cmXmlNodeUInt(np,&meas->beat_type,"time","beat-type",NULL);
  636. }
  637. // store the bar line
  638. if((rc = _cmXScorePushNonNote(p,meas,mnp,tick,0,0,NULL,kBarXsFl)) != kOkXsRC )
  639. return rc;
  640. np = mnp->children;
  641. // for each child of the 'meas' XML node
  642. for(; rc==kOkXsRC && np!=NULL; np=np->sibling)
  643. {
  644. // if this is a 'note' node
  645. if( cmTextCmp(np->label,"note") == 0 )
  646. {
  647. rc = _cmXScoreParseNote(p,meas,np,&tick0,&tick);
  648. }
  649. else
  650. // if this is a 'backup' node
  651. if( cmTextCmp(np->label,"backup") == 0 )
  652. {
  653. unsigned backup;
  654. cmXmlNodeUInt(np,&backup,"duration",NULL);
  655. if( backup > tick )
  656. tick = 0;
  657. else
  658. tick -= backup;
  659. tick0 = tick;
  660. }
  661. else
  662. // if this is a 'direction' node
  663. if( cmTextCmp(np->label,"direction") == 0 )
  664. {
  665. rc = _cmXScoreParseDirection(p,meas,np,tick);
  666. }
  667. }
  668. *tickRef = tick;
  669. return rc;
  670. }
  671. cmXsRC_t _cmXScoreParsePart( cmXScore_t* p, cmXsPart_t* pp )
  672. {
  673. cmXsRC_t rc = kOkXsRC;
  674. const cmXmlNode_t* xnp;
  675. cmXmlAttr_t partAttr = { "id", pp->idStr };
  676. unsigned barTick = 0;
  677. // find the 'part'
  678. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part", &partAttr, 1)) == NULL )
  679. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The part '%s' was not found.",pp->idStr);
  680. // for each child of this part - find each measure
  681. const cmXmlNode_t* cnp = xnp->children;
  682. for(; cnp!=NULL; cnp=cnp->sibling)
  683. if( cmTextCmp(cnp->label,"measure") == 0 )
  684. if((rc = _cmXScoreParseMeasure(p,pp,cnp,&barTick)) != kOkXsRC )
  685. return rc;
  686. return rc;
  687. }
  688. // Insert note 'np' into the sorted note list based at 's0'.
  689. // Return a pointer to the base of the list after the insertion.
  690. cmXsNote_t* _cmXScoreInsertSortedNote( cmXsNote_t* s0, cmXsNote_t* np )
  691. {
  692. assert( np != NULL );
  693. // np->slink is not NULL if the list is being resorted
  694. np->slink = NULL;
  695. // this list is empty so np is the first element on the list
  696. if( s0 == NULL )
  697. return np;
  698. // np is before the first element on the list
  699. if( np->tick < s0->tick )
  700. {
  701. np->slink = s0;
  702. return np;
  703. }
  704. cmXsNote_t* s1 = s0;
  705. cmXsNote_t* s2 = s0->slink;
  706. while( s2 != NULL )
  707. {
  708. if( s2->tick > np->tick )
  709. {
  710. s1->slink = np;
  711. np->slink = s2;
  712. return s0;
  713. }
  714. s1 = s2;
  715. s2 = s2->slink;
  716. }
  717. s1->slink = np;
  718. return s0;
  719. }
  720. void _cmXScoreSort( cmXScore_t* p )
  721. {
  722. // for each part
  723. cmXsPart_t* pp = p->partL;
  724. for(; pp!=NULL; pp=pp->link)
  725. {
  726. // for each measure in this part
  727. cmXsMeas_t* mp = pp->measL;
  728. for(; mp!=NULL; mp=mp->link)
  729. {
  730. // explicitely set noteL to NULL to in case we are re-sorting
  731. mp->noteL = NULL;
  732. // for each voice in this measure
  733. cmXsVoice_t* vp = mp->voiceL;
  734. for(; vp!=NULL; vp=vp->link)
  735. {
  736. // for each note in this measure
  737. cmXsNote_t* np = vp->noteL;
  738. for(; np!=NULL; np=np->mlink)
  739. mp->noteL = _cmXScoreInsertSortedNote(mp->noteL,np);
  740. }
  741. }
  742. }
  743. }
  744. // Set the cmXsNode_t.secs and dsecs.
  745. void _cmXScoreSetAbsoluteTime( cmXScore_t* p )
  746. {
  747. double tpqn = 0; // ticks per quarter note
  748. double tps = 0; // ticks per second
  749. unsigned metro_tick = 0;
  750. double metro_sec = 0;
  751. double sec0 = 0;
  752. cmXsPart_t* pp = p->partL;
  753. for(; pp!=NULL; pp=pp->link)
  754. {
  755. cmXsMeas_t* mp = pp->measL;
  756. for(; mp!=NULL; mp=mp->link)
  757. {
  758. if( mp->divisions != 0 )
  759. tpqn = mp->divisions;
  760. cmXsNote_t* np = mp->noteL;
  761. for(; np!=NULL; np=np->slink)
  762. {
  763. // Seconds are calculated as:
  764. // dticks = np->tick - metro_tick; // where metro_tick is the absolute tick of the last metro event
  765. // secs = (dticks/tps) + metro_secs; // where metro_secs is the absoute time of the last metro event
  766. unsigned dticks = np->tick - metro_tick;
  767. double secs = tps==0 ? 0 : (dticks/tps) + metro_sec;
  768. double dsecs = secs - sec0;
  769. //
  770. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  771. {
  772. double bpm = np->duration; // beats per minute
  773. double bps = bpm / 60.0; // beats per second
  774. tps = bps * tpqn; // ticks per second
  775. metro_tick = np->tick; // store tick of the last metronome marker
  776. metro_sec = secs; // store time of the last metronome marker
  777. }
  778. if( cmIsFlag(np->flags,kBarXsFl|kDampDnXsFl|kDampUpXsFl|kDampUpDnXsFl|kSostDnXsFl|kSostUpXsFl|kOnsetXsFl|kSectionXsFl) )
  779. {
  780. np->secs = secs;
  781. np->dsecs = dsecs;
  782. sec0 = secs;
  783. }
  784. }
  785. }
  786. }
  787. }
  788. // All notes in a[aN] are on the same tick
  789. unsigned _cmXsSpreadGraceNotes( cmXsNote_t** a, unsigned aN )
  790. {
  791. unsigned i;
  792. bool barFl = false;
  793. // set barFl to true if a bar marker is included in the notes
  794. for(i=0; i<aN; ++i)
  795. if( cmIsFlag(a[i]->flags,kBarXsFl) )
  796. barFl = true;
  797. // spread any grace notes by one tick
  798. unsigned nextGraceTick = UINT_MAX;
  799. for(i=0; i<aN; ++i)
  800. if( cmIsFlag(a[i]->flags,kGraceXsFl) )
  801. {
  802. if( nextGraceTick == UINT_MAX )
  803. nextGraceTick = a[i]->tick + 1;
  804. else
  805. {
  806. a[i]->tick = nextGraceTick;
  807. nextGraceTick += 1;
  808. }
  809. }
  810. // if this tick group includes the bar ...
  811. if( barFl && nextGraceTick != UINT_MAX )
  812. {
  813. // ... then move all non-grace note events (except the bar marker) after
  814. // the grace notes
  815. for(i=0; i<aN; ++i)
  816. if( cmIsNotFlag(a[i]->flags,kGraceXsFl) && cmIsNotFlag(a[i]->flags,kBarXsFl) )
  817. a[i]->tick = nextGraceTick;
  818. }
  819. return nextGraceTick==UINT_MAX ? 0 : nextGraceTick;
  820. }
  821. void _cmXScoreSpreadGraceNotes( cmXScore_t* p )
  822. {
  823. cmXsPart_t* pp = p->partL;
  824. for(; pp!=NULL; pp=pp->link)
  825. {
  826. // tick1 is the location of the minimum current tick
  827. // (or 0 if it should be ignored)
  828. unsigned tick1 = 0;
  829. cmXsMeas_t* mp = pp->measL;
  830. for(; mp!=NULL; mp=mp->link)
  831. {
  832. cmXsNote_t* np = mp->noteL;
  833. unsigned aN = 128;
  834. cmXsNote_t* a[ aN ];
  835. unsigned ai = 0;
  836. // The first event in a measure may have been forced ahead
  837. // by spreading at the end of the previous measure
  838. if( tick1 > np->tick )
  839. np->tick = tick1;
  840. else
  841. tick1 = 0;
  842. // tick0 is the tick of the current tick group we are examining
  843. // A tick group is a group of events that share the same tick.
  844. unsigned tick0 = np->tick;
  845. // for each note
  846. for(; np!=NULL; np=np->slink)
  847. {
  848. // if this event is the first of a new tick group (then a[] holds a group completed on the previous note)
  849. if( np->tick != tick0 )
  850. {
  851. // if there is more than one event in the completed tick group ...
  852. if( ai > 1 )
  853. tick1 = _cmXsSpreadGraceNotes(a,ai); // ... then process the group
  854. ai = 0; // empty the tick group array
  855. tick0 = np->tick; // update the current group's common tick
  856. }
  857. // if the min. current tick is ahead of this event then move the event ahead
  858. if( tick1 > np->tick )
  859. np->tick = tick1;
  860. else
  861. tick1 = 0; // otherwise disable tick1
  862. // add this event to the tick group
  863. assert(ai<aN);
  864. a[ai++] = np;
  865. }
  866. // if there are events in the group array then process them
  867. if( ai > 1 )
  868. tick1 = _cmXsSpreadGraceNotes(a,ai);
  869. }
  870. }
  871. }
  872. bool _cmXScoreFindTiedNote( cmXScore_t* p, cmXsMeas_t* mp, cmXsNote_t* n0p, bool rptFl )
  873. {
  874. cmXsNote_t* nnp = n0p->slink; // begin w/ note following np
  875. unsigned measNumb = mp->number;
  876. cmChar_t acc = n0p->alter==-1?'b' : (n0p->alter==1?'#':' ');
  877. if( rptFl )
  878. printf("%i %i %s ",n0p->meas->number,n0p->tick,cmMidiToSciPitch(n0p->pitch,NULL,0));
  879. while(1)
  880. {
  881. // if we are at the end of a measure advance to the next measure
  882. if( nnp == NULL )
  883. {
  884. mp = mp->link;
  885. nnp = mp->noteL;
  886. // if a measure was completed and no end note was found ... then the tie is unterminated
  887. // (a tie must be continued in every measure which it passes through)
  888. if( mp->number > measNumb + 1 )
  889. break;
  890. }
  891. // for each note starting at nnp
  892. for(; nnp!=NULL; nnp=nnp->slink)
  893. {
  894. // if this note is tied to the originating note (np)
  895. if( nnp->voice->id == n0p->voice->id && nnp->step == n0p->step && nnp->octave == n0p->octave )
  896. {
  897. nnp->flags |= kTieProcXsFl;
  898. nnp->flags = cmClrFlag(nnp->flags,kOnsetXsFl);
  899. n0p->tied = nnp;
  900. if( rptFl )
  901. printf("---> %i %i %s ",nnp->meas->number,nnp->tick,cmMidiToSciPitch(nnp->pitch,NULL,0));
  902. // if this note is not tied to a subsequent note
  903. if( cmIsNotFlag(nnp->flags,kTieBegXsFl) )
  904. return true;
  905. n0p = nnp;
  906. // record the measure number of the last note with a tie-start
  907. measNumb = mp->number;
  908. }
  909. }
  910. }
  911. cmErrWarnMsg(&p->err,kUnterminatedTieXsRC,"The tied %c%c%i in measure %i was not terminated.",n0p->step,acc,n0p->octave,measNumb);
  912. return false;
  913. }
  914. void _cmXScoreResolveTiesAndLoc( cmXScore_t* p )
  915. {
  916. unsigned n = 0;
  917. unsigned m = 0;
  918. bool rptFl = false;
  919. cmXsPart_t* pp = p->partL;
  920. // for each part
  921. for(; pp!=NULL; pp=pp->link)
  922. {
  923. unsigned locIdx = 1;
  924. cmXsMeas_t* mp = pp->measL;
  925. // for each measure
  926. for(; mp!=NULL; mp=mp->link)
  927. {
  928. cmXsNote_t* n0 = NULL;
  929. cmXsNote_t* np = mp->noteL;
  930. // for each note in this measure
  931. for(; np!=NULL; np=np->slink)
  932. {
  933. // if this note begins a tie and has not yet been processed
  934. // (A note that continues a tie and therefore has a kTieBegXsFl set
  935. // may have already been processed by an earlier tied note.)
  936. if( cmIsFlag(np->flags,kTieBegXsFl) && cmIsNotFlag(np->flags,kTieProcXsFl))
  937. {
  938. if( _cmXScoreFindTiedNote(p,mp,np,rptFl) )
  939. m += 1;
  940. if( rptFl )
  941. printf("\n");
  942. n += 1;
  943. }
  944. // Validate the tie state of the current note.
  945. if( cmIsFlag(np->flags,kTieEndXsFl) && cmIsFlag(np->flags,kOnsetXsFl) )
  946. {
  947. cmChar_t acc = np->alter==-1?'b' : (np->alter==1?'#':' ');
  948. 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);
  949. }
  950. //
  951. // Set the score location of notes marked for onset and bar lines.
  952. //
  953. if( cmIsFlag(np->flags,kOnsetXsFl|kBarXsFl) )
  954. {
  955. // if this note does not share the same location as the previous 'located' note then increment the 'loc' index
  956. if( cmIsFlag(np->flags,kBarXsFl) || (n0!=NULL && n0->tick!=np->tick))
  957. locIdx += 1;
  958. np->locIdx = locIdx;
  959. n0 = np;
  960. }
  961. }
  962. }
  963. }
  964. printf("Tied notes found:%i Not found:%i\n",m,n-m);
  965. }
  966. cmXsRC_t _cmXScoreResolveOctaveShift( cmXScore_t* p )
  967. {
  968. const cmXsSpan_t* s;
  969. for(s=p->spanL; s!=NULL; s=s->link)
  970. {
  971. if( s->tick1 == -1)
  972. {
  973. cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"An unterminated octave shift span was encountered in measure %i staff=%i.",s->meas->number,s->staff);
  974. }
  975. else
  976. {
  977. cmXsMeas_t* m = p->partL->measL;
  978. for(; m!=NULL; m=m->link)
  979. if( m->number == s->meas->number )
  980. break;
  981. assert( m != NULL );
  982. cmXsNote_t* note = m->noteL;
  983. for(; note!=NULL; note=note->slink)
  984. if( note->staff==s->staff && s->tick0 <= note->tick && note->tick < s->tick1 )
  985. note->pitch += s->pitch_offset;
  986. }
  987. }
  988. return kOkXsRC;
  989. }
  990. // The identical pitch may be notated to play simultaneously on different voices.
  991. // As performed on the piano this will equate to a single sounding note.
  992. // This function clears the onset flag on all except one of the duplicated notes.
  993. void _cmXScoreRemoveDuplicateNotes( cmXScore_t* p )
  994. {
  995. cmXsPart_t* pp = p->partL;
  996. // for each part
  997. for(; pp!=NULL; pp=pp->link)
  998. {
  999. cmXsMeas_t* mp = pp->measL;
  1000. // for each measure
  1001. for(; mp!=NULL; mp=mp->link)
  1002. {
  1003. cmXsNote_t* np = mp->noteL;
  1004. // for each note in this measure
  1005. for(; np!=NULL; np=np->slink)
  1006. if( cmIsFlag(np->flags,kOnsetXsFl) )
  1007. {
  1008. cmXsNote_t* n0p = mp->noteL;
  1009. for(; n0p!=NULL; n0p=n0p->slink)
  1010. if( n0p!=np && cmIsFlag(n0p->flags,kOnsetXsFl) && np->locIdx==n0p->locIdx && np->pitch==n0p->pitch )
  1011. n0p->flags = cmClrFlag(n0p->flags,kOnsetXsFl);
  1012. }
  1013. }
  1014. }
  1015. }
  1016. void _cmXScoreSetMeasGroups( cmXScore_t* p, unsigned flag )
  1017. {
  1018. unsigned sectionId = 0;
  1019. cmXsNote_t* n0 = NULL;
  1020. cmXsPart_t* pp = p->partL;
  1021. // for each part
  1022. for(; pp!=NULL; pp=pp->link)
  1023. {
  1024. cmXsMeas_t* mp = pp->measL;
  1025. // for each measure
  1026. for(; mp!=NULL; mp=mp->link)
  1027. {
  1028. cmXsNote_t* np = mp->noteL;
  1029. // for each note in this measure
  1030. for(; np!=NULL; np=np->slink)
  1031. {
  1032. // if this note has a heel marker and we are looking for evenness events
  1033. if( cmIsFlag(flag,kEvenXsFl) && cmIsFlag(np->flags,kHeelXsFl) )
  1034. {
  1035. np->flags = cmSetFlag(np->flags,kBegGroupXsFl | kEndGroupXsFl );
  1036. np->evenGroupId = sectionId + 1;
  1037. }
  1038. // if this note is of the type we are looking for
  1039. if( cmIsFlag(np->flags,flag) )
  1040. {
  1041. if( n0 == NULL )
  1042. np->flags = cmSetFlag(np->flags,kBegGroupXsFl);
  1043. n0 = np;
  1044. }
  1045. // if this is a section marker
  1046. if( cmIsFlag(np->flags,kSectionXsFl) )
  1047. {
  1048. if( n0 != NULL )
  1049. {
  1050. np->flags = cmSetFlag(np->flags,kEndGroupXsFl);
  1051. switch( flag )
  1052. {
  1053. case kEvenXsFl: n0->evenGroupId = sectionId+1; break;
  1054. case kDynXsFl: n0->dynGroupId = sectionId+1; break;
  1055. case kTempoXsFl: n0->tempoGroupId= sectionId+1; break;
  1056. }
  1057. }
  1058. if( cmIsFlag(np->flags,kSectionXsFl) )
  1059. sectionId = np->tvalue==NULL ? 0 : strtol(np->tvalue,NULL,10);
  1060. n0 = NULL;
  1061. }
  1062. }
  1063. }
  1064. }
  1065. }
  1066. cmXsRC_t _cmXScoreWriteScorePlotFile( cmXScore_t* p, const cmChar_t* fn )
  1067. {
  1068. cmXsRC_t rc = kOkXsRC;
  1069. cmFileH_t fH = cmFileNullHandle;
  1070. double ticks_per_sec = 0;
  1071. double onset_secs = 0;
  1072. if( cmFileOpen(&fH,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  1073. return cmErrMsg(&p->err,kFileFailXsRC,"Unable to create the file '%s'.",cmStringNullGuard(fn));
  1074. cmXsPart_t* pp = p->partL;
  1075. for(; pp!=NULL; pp=pp->link)
  1076. {
  1077. cmXsMeas_t* mp = pp->measL;
  1078. for(; mp!=NULL; mp=mp->link)
  1079. {
  1080. cmFilePrintf(fH,"b %f %i %s B\n",onset_secs,mp->number,"bar");
  1081. cmXsNote_t* np = mp->noteL;
  1082. unsigned tick0 = 0;
  1083. for(; np!=NULL; np=np->slink)
  1084. {
  1085. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  1086. {
  1087. double bps = np->duration / 60.0;
  1088. // t b t
  1089. // - = - -
  1090. // s s b
  1091. ticks_per_sec = bps * mp->divisions;
  1092. }
  1093. else
  1094. {
  1095. if( cmIsFlag(np->flags,kOnsetXsFl) )
  1096. {
  1097. onset_secs += (np->tick - tick0) / ticks_per_sec;
  1098. tick0 = np->tick;
  1099. 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");
  1100. }
  1101. }
  1102. }
  1103. onset_secs += (mp->divisions * mp->beats - tick0) / ticks_per_sec;
  1104. }
  1105. }
  1106. cmFileClose(&fH);
  1107. return rc;
  1108. }
  1109. // Force the bar event to be the first event in the measure.
  1110. void _cmXScoreFixBarLines( cmXScore_t* p )
  1111. {
  1112. cmXsPart_t* pp = p->partL;
  1113. for(; pp!=NULL; pp=pp->link)
  1114. {
  1115. cmXsMeas_t* mp = pp->measL;
  1116. for(; mp!=NULL; mp=mp->link)
  1117. {
  1118. cmXsNote_t* np = mp->noteL;
  1119. cmXsNote_t* ep = NULL;
  1120. for(; np!=NULL; np=np->slink )
  1121. {
  1122. if( cmIsFlag(np->flags,kBarXsFl) )
  1123. {
  1124. if( ep != NULL )
  1125. np->tick = ep->tick;
  1126. break;
  1127. }
  1128. if( ep == NULL )
  1129. ep = np;
  1130. }
  1131. }
  1132. }
  1133. }
  1134. // Assign pedal down durations to pedal down events.
  1135. cmXsRC_t _cmXScoreProcessPedals( cmXScore_t* p )
  1136. {
  1137. cmXsRC_t rc = kOkXsRC;
  1138. cmXsPart_t* pp = p->partL;
  1139. for(; pp!=NULL; pp=pp->link)
  1140. {
  1141. cmXsNote_t* dnp = NULL; // pointer to last damper down event
  1142. cmXsNote_t* snp = NULL; // pointer to last sostenuto down event
  1143. cmXsMeas_t* mp = pp->measL;
  1144. for(; mp!=NULL; mp=mp->link)
  1145. {
  1146. cmXsNote_t* np = mp->noteL;
  1147. for(; np!=NULL; np=np->slink )
  1148. {
  1149. unsigned x = np->flags & (kDampDnXsFl|kDampUpXsFl|kDampUpDnXsFl|kSostUpXsFl|kSostDnXsFl);
  1150. switch( x )
  1151. {
  1152. case 0:
  1153. break;
  1154. case kDampDnXsFl:
  1155. if( dnp != NULL )
  1156. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Damper down not preceded by damper up in measure:%i.",mp->number);
  1157. else
  1158. dnp = np;
  1159. break;
  1160. case kDampUpXsFl:
  1161. if( dnp == NULL )
  1162. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Damper up not preceded by damper down in measure:%i.",mp->number);
  1163. else
  1164. {
  1165. dnp->duration = np->tick - dnp->tick;
  1166. dnp = NULL;
  1167. }
  1168. break;
  1169. case kDampUpDnXsFl:
  1170. if( dnp == NULL )
  1171. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Damper up/down not preceded by damper down in measure:%i.",mp->number);
  1172. else
  1173. {
  1174. dnp->duration = np->tick - dnp->tick;
  1175. dnp = np;
  1176. }
  1177. break;
  1178. case kSostDnXsFl:
  1179. if( snp != NULL )
  1180. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Sostenuto down not preceded by sostenuto up in measure:%i.",mp->number);
  1181. else
  1182. snp = np;
  1183. break;
  1184. case kSostUpXsFl:
  1185. if( snp == NULL )
  1186. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Sostenuto up not preceded by sostenuto down in measure:%i.",mp->number);
  1187. else
  1188. {
  1189. snp->duration = np->tick - snp->tick;
  1190. snp = NULL;
  1191. }
  1192. break;
  1193. default:
  1194. {
  1195. assert(0);
  1196. }
  1197. } // switch
  1198. } // for notes
  1199. } // for measures
  1200. if( dnp != NULL )
  1201. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Damper left down at the end of a part.");
  1202. if( snp != NULL )
  1203. cmErrWarnMsg(&p->err,kPedalStateErrorXsRc,"Sostenuto left down at the end of a part.");
  1204. }
  1205. _cmXScoreSort(p);
  1206. return rc;
  1207. }
  1208. void _cmXScoreInsertTime( cmXScore_t* p, cmXsMeas_t* mp, cmXsNote_t* np, unsigned expand_ticks )
  1209. {
  1210. for(; mp!=NULL; mp=mp->link)
  1211. {
  1212. if( np == NULL )
  1213. np = mp->noteL;
  1214. for(; np!=NULL; np=np->slink)
  1215. np->tick += expand_ticks;
  1216. }
  1217. }
  1218. void _cmXScoreGraceInsertTimeBase( cmXScore_t* p, unsigned graceGroupId, cmXsNote_t* aV[], unsigned aN, unsigned initTick )
  1219. {
  1220. cmXsNote_t* np = NULL;
  1221. unsigned expand_ticks = 0;
  1222. unsigned ticks = initTick;
  1223. unsigned t0 = 0;
  1224. unsigned i;
  1225. for(i=0; i<aN; ++i)
  1226. if( cmIsFlag(aV[i]->flags,kGraceXsFl) && aV[i]->graceGroupId == graceGroupId )
  1227. {
  1228. // if this grace note falls on the same tick as the previous grace note
  1229. if( np != NULL && aV[i]->tick == t0 )
  1230. aV[i]->tick = np->tick;
  1231. else
  1232. {
  1233. t0 = aV[i]->tick; // store the unmodified tick value of this note
  1234. aV[i]->tick = ticks; // set the new tick value
  1235. ticks += aV[i]->duration; // calc the next grace not location
  1236. expand_ticks += aV[i]->duration; // track how much we are expanding time by
  1237. }
  1238. np = aV[i];
  1239. }
  1240. np = np->slink;
  1241. if( np != NULL )
  1242. _cmXScoreInsertTime(p,np->meas,np,expand_ticks);
  1243. }
  1244. // Insert the grace notes in between the first and last note in the group
  1245. // by inserting time between the first and last note.
  1246. // Note that in effect his means that the last note is pushed back
  1247. // in time by the total duration of the grace notes.
  1248. void _cmXScoreGraceInsertTime( cmXScore_t* p, unsigned graceGroupId, cmXsNote_t* aV[], unsigned aN )
  1249. {
  1250. _cmXScoreGraceInsertTimeBase( p, graceGroupId,aV,aN, aV[aN-1]->tick );
  1251. }
  1252. // Insert the grace notes in between the first and last note in the group
  1253. // but do not insert any additional time betwee the first and last note.
  1254. // In effect time is removed from the first note and taken by the grace notes.
  1255. // The time position of the last note is therefore unchanged.
  1256. void _cmXScoreGraceOverlayTime( cmXScore_t* p, unsigned graceGroupId, cmXsNote_t* aV[], unsigned aN )
  1257. {
  1258. assert(aN >= 3 );
  1259. int i = (int)aN-2;
  1260. cmXsNote_t* np = aV[aN-1];
  1261. unsigned t0 = -1;
  1262. for(; i>0; --i)
  1263. if( cmIsFlag(aV[i]->flags,kGraceXsFl) && aV[i]->graceGroupId == graceGroupId )
  1264. {
  1265. if( aV[i]->tick == t0)
  1266. aV[i]->tick = np->tick;
  1267. else
  1268. {
  1269. t0 = aV[i]->tick;
  1270. aV[i]->tick = np->tick - aV[i]->duration;
  1271. }
  1272. np = aV[i];
  1273. }
  1274. }
  1275. // Play the first grace at the time of the first note in the group (which is a non-grace note)
  1276. // and then expand time while inserting the other grace notes.
  1277. void _cmXScoreGraceInsertAfterFirst( cmXScore_t* p, unsigned graceGroupId, cmXsNote_t* aV[], unsigned aN )
  1278. {
  1279. _cmXScoreGraceInsertTimeBase( p, graceGroupId,aV,aN, aV[0]->tick );
  1280. }
  1281. // Play the first grace not shortly (one grace note duration) after the first note
  1282. // in the group (which is a non-grace note) and then expand time while inserting the other
  1283. // grace notes.
  1284. void _cmXScoreGraceInsertSoonAfterFirst( cmXScore_t* p, unsigned graceGroupId, cmXsNote_t* aV[], unsigned aN )
  1285. {
  1286. _cmXScoreGraceInsertTimeBase( p, graceGroupId,aV,aN, aV[0]->tick + aV[1]->duration );
  1287. }
  1288. // Adjust the locations of grace notes. Note that this must be done
  1289. // after reordering so that we can be sure that the order in time of
  1290. // the notes in each group has been set prior to building the
  1291. // grace note groups - which must be in reverse time order.
  1292. cmXsRC_t _cmXScoreProcessGraceNotes( cmXScore_t* p )
  1293. {
  1294. cmXsRC_t rc = kOkXsRC;
  1295. unsigned graceGroupId = 1;
  1296. double graceDurSec = 1.0/15.0; // duration of all grace notes in seconds
  1297. for(; 1; ++graceGroupId)
  1298. {
  1299. cmXsNote_t* gn0p = NULL; // first note in the grace group
  1300. cmXsNote_t* gn1p = NULL; // last note in the grace group
  1301. unsigned gN = 0;
  1302. cmXsPart_t* pp = p->partL;
  1303. double ticksPerSec = 0;
  1304. // Build a note chain, using cmXsNote_t.grace, between gn0p and
  1305. // gn1p containing all the grace notes with
  1306. // cmXsNote_t.graceGroupId == graceGroupId.
  1307. for(; pp!=NULL; pp=pp->link)
  1308. {
  1309. cmXsMeas_t* mp = pp->measL;
  1310. for(; mp!=NULL; mp=mp->link)
  1311. {
  1312. cmXsNote_t* np = mp->noteL;
  1313. for(; np!=NULL; np=np->slink )
  1314. {
  1315. // notice change of tempo
  1316. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  1317. {
  1318. // ticks/sec = ticks/qn * qn/sec
  1319. ticksPerSec = mp->divisions * np->duration / 60.0;
  1320. }
  1321. // if this note is part of the grace note group we are searching for
  1322. if( np->graceGroupId == graceGroupId )
  1323. {
  1324. // track the first note in the grace note list
  1325. if( gn0p == NULL )
  1326. gn0p = np;
  1327. // add the note to the end of the grace note list
  1328. if( gn1p != NULL )
  1329. gn1p->grace = np;
  1330. // track the last note in the grace note list
  1331. gn1p = np;
  1332. // set each grace note to have 1/20 of a second duration
  1333. if( cmIsFlag(np->flags,kGraceXsFl) )
  1334. np->duration = floor(ticksPerSec * graceDurSec);
  1335. gN += 1;
  1336. }
  1337. } // for each note in this meassure
  1338. } // for each measure
  1339. } // for each part
  1340. // no records were found for this grace id - we're done
  1341. if( gn0p == NULL )
  1342. break;
  1343. // grace note groups must have at least 3 members
  1344. if( gN < 3 )
  1345. {
  1346. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"The grace not group ending in meas %i has fewer than 3 (%i) members.", gn1p->meas->number, gN );
  1347. break;
  1348. }
  1349. // gn0p is now set to the first note in th group
  1350. // gn1p is now set to the last note in the group
  1351. // verify that the first note is marked with kBegGraceXsFl
  1352. if( cmIsNotFlag(gn0p->flags,kBegGraceXsFl) )
  1353. {
  1354. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"The first note in a grace note group in meas %i is not marked with a 'b'.", gn0p->meas->number );
  1355. break;
  1356. }
  1357. // verify that the last note is marked with kEndGraceXsFl
  1358. if( cmIsNotFlag(gn1p->flags,kEndGraceXsFl) )
  1359. {
  1360. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"The last note in a grace note group in meas %i is not marked with a valid operator character.", gn1p->meas->number );
  1361. break;
  1362. }
  1363. // Count the total number of events between gn0p and gn1p
  1364. cmXsNote_t* n0p = NULL;
  1365. cmXsNote_t* n1p = gn0p;
  1366. cmXsMeas_t* mp = gn0p->meas;
  1367. unsigned aN = 0;
  1368. for(; n0p != gn1p; n1p=n1p->slink )
  1369. {
  1370. // if we are crossing a measure boundary
  1371. if( n1p == NULL )
  1372. {
  1373. mp = mp->link;
  1374. assert(mp!=NULL);
  1375. n1p = mp->noteL;
  1376. }
  1377. if(1)
  1378. {
  1379. bool fl = n0p != NULL && n0p->tick < n1p->tick;
  1380. unsigned type = n1p->flags & (kBegGraceXsFl|kEndGraceXsFl|kAddGraceXsFl|kSubGraceXsFl|kAFirstGraceXsFl|kNFirstGraceXsFl);
  1381. printf("%3i 0x%08x %i %3i %5i %i\n",n1p->graceGroupId,type,n1p->meas->number,n1p->tick,n1p->duration,fl);
  1382. }
  1383. ++aN;
  1384. n0p = n1p;
  1385. }
  1386. // create a vector of pointers to all events between gn0p and gn1p
  1387. cmXsNote_t* aV[ aN ];
  1388. unsigned i;
  1389. n1p = gn0p;
  1390. n0p = NULL;
  1391. mp = gn0p->meas;
  1392. for(i=0; n0p != gn1p; n1p=n1p->slink )
  1393. {
  1394. // if we are crossing a measure boundary
  1395. if( n1p == NULL )
  1396. {
  1397. mp = mp->link;
  1398. assert(mp!=NULL);
  1399. n1p = mp->noteL;
  1400. }
  1401. assert(i<aN);
  1402. aV[i++] = n1p;
  1403. n0p = n1p;
  1404. }
  1405. switch( gn1p->flags & (kAddGraceXsFl | kSubGraceXsFl | kAFirstGraceXsFl | kNFirstGraceXsFl ) )
  1406. {
  1407. case kAddGraceXsFl:
  1408. _cmXScoreGraceInsertTime(p, graceGroupId, aV, aN );
  1409. break;
  1410. case kSubGraceXsFl:
  1411. _cmXScoreGraceOverlayTime(p, graceGroupId, aV, aN );
  1412. break;
  1413. case kAFirstGraceXsFl:
  1414. _cmXScoreGraceInsertAfterFirst(p,graceGroupId,aV,aN);
  1415. break;
  1416. case kNFirstGraceXsFl:
  1417. _cmXScoreGraceInsertSoonAfterFirst(p,graceGroupId,aV,aN);
  1418. break;
  1419. default:
  1420. { assert(0); }
  1421. }
  1422. }
  1423. return rc;
  1424. }
  1425. cmXsRC_t cmXScoreInitialize( cmCtx_t* ctx, cmXsH_t* hp, const cmChar_t* xmlFn )
  1426. {
  1427. cmXsRC_t rc = kOkXsRC;
  1428. if((rc = cmXScoreFinalize(hp)) != kOkXsRC )
  1429. return rc;
  1430. cmXScore_t* p = cmMemAllocZ(cmXScore_t,1);
  1431. cmErrSetup(&p->err,&ctx->rpt,"XScore");
  1432. // create a local linked heap
  1433. if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8196,ctx)) == false )
  1434. return cmErrMsg(&p->err,kLHeapFailXsRC,"Lheap create failed.");
  1435. // open the music xml file
  1436. if( cmXmlAlloc(ctx, &p->xmlH, xmlFn) != kOkXmlRC )
  1437. {
  1438. rc = cmErrMsg(&p->err,kXmlFailXsRC,"Unable to open the MusicXML file '%s'.",cmStringNullGuard(xmlFn));
  1439. goto errLabel;
  1440. }
  1441. //cmXmlPrint(p->xmlH,&ctx->rpt);
  1442. // parse the part-list
  1443. if((rc = _cmXScoreParsePartList( p )) != kOkXsRC )
  1444. goto errLabel;
  1445. // parse each score 'part'
  1446. cmXsPart_t* pp = p->partL;
  1447. for(; pp!=NULL; pp=pp->link)
  1448. if((rc = _cmXScoreParsePart(p,pp)) != kOkXsRC )
  1449. goto errLabel;
  1450. // fill in the note->slink chain to link the notes in each measure in time order
  1451. _cmXScoreSort(p);
  1452. _cmXScoreSpreadGraceNotes(p);
  1453. _cmXScoreSort(p);
  1454. _cmXScoreSetAbsoluteTime(p);
  1455. _cmXScoreResolveTiesAndLoc(p);
  1456. _cmXScoreRemoveDuplicateNotes(p);
  1457. _cmXScoreSetMeasGroups(p,kEvenXsFl);
  1458. _cmXScoreSetMeasGroups(p,kDynXsFl);
  1459. _cmXScoreSetMeasGroups(p,kTempoXsFl);
  1460. //_cmXScoreResolveOctaveShift(p);
  1461. // CSV output initialize failed.
  1462. if( cmCsvInitialize(&p->csvH,ctx) != kOkCsvRC )
  1463. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output object create failed.");
  1464. errLabel:
  1465. if( rc != kOkXsRC )
  1466. _cmXScoreFinalize(p);
  1467. else
  1468. hp->h = p;
  1469. return rc;
  1470. }
  1471. cmXsRC_t cmXScoreFinalize( cmXsH_t* hp )
  1472. {
  1473. cmXsRC_t rc = kOkXsRC;
  1474. if( hp == NULL || cmXScoreIsValid(*hp)==false )
  1475. return kOkXsRC;
  1476. cmXScore_t* p = _cmXScoreHandleToPtr(*hp);
  1477. if((rc = _cmXScoreFinalize(p)) != kOkXsRC )
  1478. return rc;
  1479. hp->h = NULL;
  1480. return rc;
  1481. }
  1482. bool cmXScoreIsValid( cmXsH_t h )
  1483. { return h.h != NULL; }
  1484. //-------------------------------------------------------------------------------------------
  1485. typedef struct
  1486. {
  1487. unsigned idx; // Fields from the reordering input file which are
  1488. unsigned voice; // used to match the reorder record to
  1489. unsigned locIdx; // the associated a cmXsNode_t record.
  1490. unsigned tick; //
  1491. unsigned durtn; //
  1492. float rval; //
  1493. unsigned midi; //
  1494. cmXsNote_t* note; // The cmXsNote_t* associated with this cmXsReorder_t record
  1495. unsigned dynIdx; // cmInvalidIdx=ignore otherwise index into _cmXScoreDynMarkArray[]
  1496. unsigned newFlags; // 0=ignore | kSostUp/DnXsFl | kDampUp/DnXsFl | kTieEndXsFl
  1497. unsigned newTick; // 0=ignore >0 new tick value
  1498. unsigned graceFlags; // 0=ignore See kXXXGraceXsFl
  1499. unsigned graceGroupId; // 0=ignore >0=grace note group id
  1500. unsigned pitch; // 0=ignore >0 new pitch
  1501. } cmXsReorder_t;
  1502. typedef struct _cmXScoreDynMark_str
  1503. {
  1504. const cmChar_t* mark; //
  1505. unsigned id; // (1-17) maps to velocity
  1506. unsigned dyn; // pppp - fff (1-9) as used by cmScore
  1507. int adjust; // {-1,0,+1}
  1508. unsigned vel; // associated MIDI velocity
  1509. } _cmXScoreDynMark_t;
  1510. _cmXScoreDynMark_t _cmXScoreDynMarkArray[] =
  1511. {
  1512. {"pppp-", 1, 1, -1, 3},
  1513. {"pppp", 2, 1, 0, 10},
  1514. {"pppp+", 3, 1, 1, 22},
  1515. {"ppp-", 3, 2, -1, 22},
  1516. {"ppp", 4, 2, 0, 29},
  1517. {"ppp+", 5, 2, 1, 36},
  1518. {"pp-", 5, 3, -1, 36},
  1519. {"pp", 6, 3, 0, 43},
  1520. {"pp+", 7, 3, 1, 50},
  1521. {"p-", 7, 4, -1, 50},
  1522. {"p", 8, 4, 0, 57},
  1523. {"p+", 9, 4, 1, 64},
  1524. {"mp-", 9, 5, -1, 64},
  1525. {"mp", 10, 5, 0, 71},
  1526. {"mp+", 11, 5, 1, 78},
  1527. {"mf-", 11, 6, -1, 78},
  1528. {"mf", 12, 6, 0, 85},
  1529. {"mf+", 13, 6, 1, 92},
  1530. {"f-", 13, 7, -1, 92},
  1531. {"f", 14, 7, 0, 99},
  1532. {"f+", 15, 7, 1, 106},
  1533. {"ff", 16, 8, 0, 113},
  1534. {"ff+", 17, 8, 1, 120},
  1535. {"fff", 18, 9, 0, 127},
  1536. {NULL,0,0,0,0}
  1537. };
  1538. cmXsNote_t* _cmXsReorderFindNote( cmXScore_t* p, unsigned measNumb, const cmXsReorder_t* r, unsigned iii )
  1539. {
  1540. cmXsPart_t* pp = p->partL;
  1541. for(; pp!=NULL; pp=pp->link)
  1542. {
  1543. cmXsMeas_t* mp = pp->measL;
  1544. for(; mp!=NULL; mp=mp->link)
  1545. if( mp->number == measNumb)
  1546. {
  1547. cmXsNote_t* np = mp->noteL;
  1548. int index = 0;
  1549. for(; np!=NULL; np=np->slink,++index)
  1550. {
  1551. if( np->voice->id == r->voice &&
  1552. np->locIdx == r->locIdx &&
  1553. np->tick == r->tick &&
  1554. np->duration == r->durtn &&
  1555. np->rvalue == r->rval &&
  1556. np->pitch == r->midi &&
  1557. index == r->idx )
  1558. {
  1559. return np;
  1560. }
  1561. }
  1562. }
  1563. }
  1564. cmErrMsg(&p->err,kSyntaxErrorXsRC,"Reorder note not found meas:%i index:%i.",measNumb,iii);
  1565. return NULL;
  1566. }
  1567. void _cmXScoreInsertPedalEvent( cmXScore_t* p, const cmXsReorder_t* r, unsigned flags )
  1568. {
  1569. // Create a new score event record
  1570. cmXsNote_t* nn = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  1571. nn->uid = p->nextUid++;
  1572. nn->voice = r->note->voice;
  1573. nn->meas = r->note->meas;
  1574. nn->flags = flags;
  1575. // Pedal down events occur after the event they are attached to
  1576. if( cmIsFlag(flags,kSostDnXsFl | kDampDnXsFl ) )
  1577. {
  1578. nn->tick = r->note->tick + 1;
  1579. _cmXScoreInsertNoteAfter(r->note,nn);
  1580. }
  1581. else
  1582. {
  1583. // Pedal up events occur before the event they are attached to
  1584. if( cmIsFlag(flags,kSostUpXsFl | kDampUpXsFl ) )
  1585. {
  1586. nn->tick = r->note->tick==0 ? 0 : r->note->tick - 1;
  1587. _cmXScoreInsertNoteBefore(r->note,nn);
  1588. }
  1589. else
  1590. { assert(0); }
  1591. }
  1592. }
  1593. cmXsRC_t _cmXScoreReorderMeas( cmXScore_t* p, unsigned measNumb, cmXsReorder_t* rV, unsigned rN )
  1594. {
  1595. unsigned i;
  1596. if( rN == 0 )
  1597. return kOkXsRC;
  1598. // set the 'note' field on each cmXsReorder_t record
  1599. for(i=0; i<rN; ++i)
  1600. if((rV[i].note = _cmXsReorderFindNote(p,measNumb,rV+i,i)) == NULL )
  1601. return kSyntaxErrorXsRC;
  1602. cmXsMeas_t* mp = rV[0].note->meas;
  1603. cmXsNote_t* n0p = NULL;
  1604. assert( mp->number == measNumb );
  1605. // Reassign the slink of the cmXsNote_t records in this measure
  1606. // according to their order in rV[].
  1607. for(i=0; i<rN; ++i)
  1608. {
  1609. if( n0p == NULL )
  1610. mp->noteL = rV[i].note;
  1611. else
  1612. n0p->slink = rV[i].note;
  1613. // if a new tick was specified
  1614. if( rV[i].newTick != 0 )
  1615. rV[i].note->tick = rV[i].newTick;
  1616. // if a dynamic or velocity mark was included
  1617. if( rV[i].dynIdx != cmInvalidIdx )
  1618. {
  1619. rV[i].note->dynamics = _cmXScoreDynMarkArray[ rV[i].dynIdx ].dyn;
  1620. rV[i].note->vel = _cmXScoreDynMarkArray[ rV[i].dynIdx ].vel;
  1621. }
  1622. // if the tie end flag was set
  1623. if( cmIsFlag(rV[i].newFlags,kTieEndXsFl) )
  1624. {
  1625. rV[i].note->flags |= kTieEndXsFl;
  1626. rV[i].note->flags = cmClrFlag(rV[i].note->flags, kOnsetXsFl);
  1627. rV[i].newFlags = cmClrFlag(rV[i].newFlags,kTieEndXsFl );
  1628. }
  1629. // if a new note value was specified
  1630. if( rV[i].pitch != 0 )
  1631. rV[i].note->pitch = rV[i].pitch;
  1632. rV[i].note->flags |= rV[i].graceFlags;
  1633. rV[i].note->graceGroupId = rV[i].graceGroupId;
  1634. n0p = rV[i].note;
  1635. n0p->slink = NULL;
  1636. }
  1637. // Insert new note records for pedal up/dn events.
  1638. for(i=0; i<rN; ++i)
  1639. {
  1640. if( rV[i].newFlags != 0 )
  1641. {
  1642. if( cmIsFlag(rV[i].newFlags,kDampDnXsFl ) )
  1643. _cmXScoreInsertPedalEvent(p,rV + i,kDampDnXsFl);
  1644. if( cmIsFlag(rV[i].newFlags,kSostDnXsFl ) )
  1645. _cmXScoreInsertPedalEvent(p,rV + i,kSostDnXsFl);
  1646. if( cmIsFlag(rV[i].newFlags,kDampUpXsFl ) )
  1647. _cmXScoreInsertPedalEvent(p,rV + i,kDampUpXsFl);
  1648. if( cmIsFlag(rV[i].newFlags,kSostUpXsFl ) )
  1649. _cmXScoreInsertPedalEvent(p,rV + i,kSostUpXsFl);
  1650. }
  1651. }
  1652. return kOkXsRC;
  1653. }
  1654. cmXsRC_t _cmXScoreReorderParseDyn(cmXScore_t* p, const cmChar_t* b, unsigned lineNumb, unsigned* dynIdxRef )
  1655. {
  1656. cmXsRC_t rc = kOkXsRC;
  1657. const cmChar_t* s;
  1658. *dynIdxRef = cmInvalidIdx;
  1659. // locate the '!' which indicates the start of a dynamic marking
  1660. if( (s = strchr(b,'!')) == NULL )
  1661. return rc;
  1662. ++s; // increment past the '!'
  1663. if( *s == 0 )
  1664. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected end-of-line on dynamics parsing on line:%i.",lineNumb);
  1665. // some dynamic markings are surrounded by parenthesis (to indicate a dynamic level with greater uncertainty)
  1666. if( *s == '(' )
  1667. ++s; // skip the paren.
  1668. if( *s == 0 )
  1669. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected end-of-line on dynamics parsing on line:%i.",lineNumb);
  1670. unsigned i = 0;
  1671. unsigned j = 0;
  1672. unsigned n = 6;
  1673. bool doneFl = false;
  1674. cmChar_t mark[n+1];
  1675. memset(mark,0,n+1);
  1676. for(i=0; j<n && doneFl==false; ++i)
  1677. {
  1678. switch(s[i])
  1679. {
  1680. case 'm':
  1681. case 'p':
  1682. case 'f':
  1683. case '+':
  1684. case '-':
  1685. mark[j++] = s[i];
  1686. break;
  1687. case ')': // ending paren.
  1688. case 0: // end of string
  1689. case ' ': // end of mark
  1690. case '\n': // end of line
  1691. default: // anything else
  1692. doneFl = true;
  1693. break;
  1694. }
  1695. }
  1696. if( !doneFl )
  1697. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal dynamic mark (%s) syntax on line:%i.",mark,lineNumb);
  1698. // look up the dynamic mark in the reference table
  1699. for(j=0; _cmXScoreDynMarkArray[j].mark!=NULL; ++j)
  1700. if( strcmp(mark,_cmXScoreDynMarkArray[j].mark) == 0 )
  1701. break;
  1702. if( _cmXScoreDynMarkArray[j].mark == NULL )
  1703. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The dynamic mark '%s' is not legal on line:%i.",mark,lineNumb);
  1704. *dynIdxRef = j;
  1705. return rc;
  1706. }
  1707. cmXsRC_t _cmXScoreReorderParseFlags(cmXScore_t* p, const cmChar_t* b, unsigned line, unsigned* newFlagsRef )
  1708. {
  1709. cmXsRC_t rc = kOkXsRC;
  1710. const cmChar_t* s;
  1711. bool doneFl = false;
  1712. unsigned i = 0;
  1713. *newFlagsRef = 0;
  1714. // tilde indicates a pedal event
  1715. if((s = strchr(b,'~')) == NULL )
  1716. return rc;
  1717. do
  1718. {
  1719. ++s;
  1720. switch( *s )
  1721. {
  1722. case 'd':
  1723. *newFlagsRef |= kSostDnXsFl; // sostenuto pedal down just after this note onset
  1724. break;
  1725. case 'u':
  1726. *newFlagsRef |= kSostUpXsFl; // sostenuto pedal up just before this event
  1727. break;
  1728. case 'x':
  1729. *newFlagsRef |= (kSostUpXsFl | kSostDnXsFl); // sostenuto pedal up just before this event and sost down just after it.
  1730. break;
  1731. case 'D':
  1732. *newFlagsRef |= kDampDnXsFl; // damper pedal down
  1733. break;
  1734. case 'U':
  1735. *newFlagsRef |= kDampUpXsFl; // damper pedal up
  1736. break;
  1737. case '_':
  1738. *newFlagsRef |= kTieEndXsFl; // set tie end flag
  1739. break;
  1740. default:
  1741. if( i == 0 )
  1742. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected flag marking '%c' on line %i.",*s,line);
  1743. doneFl = true;
  1744. }
  1745. ++i;
  1746. }while(!doneFl);
  1747. return rc;
  1748. }
  1749. cmXsRC_t _cmXScoreReorderParseTick(cmXScore_t* p, const cmChar_t* b, unsigned line, unsigned* tickRef )
  1750. {
  1751. cmXsRC_t rc = kOkXsRC;
  1752. const cmChar_t* s;
  1753. if((s = strchr(b,'@')) == NULL )
  1754. return rc;
  1755. ++s;
  1756. if(!isdigit(*s))
  1757. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unexpected tick reorder value '%c' on line %i.",*s,line);
  1758. if(sscanf(s,"%i",tickRef) != 1 )
  1759. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"tick reorder parse failed on line %i.",line);
  1760. return rc;
  1761. }
  1762. cmXsRC_t _cmXScoreReorderParseGrace(cmXScore_t* p, const cmChar_t* b, unsigned line, cmXsReorder_t* r, unsigned* graceGroupIdRef )
  1763. {
  1764. cmXsRC_t rc = kOkXsRC;
  1765. const cmChar_t* s;
  1766. if((s = strchr(b,'%')) == NULL )
  1767. return rc;
  1768. ++s;
  1769. r->graceGroupId = *graceGroupIdRef;
  1770. while(1)
  1771. {
  1772. switch(*s)
  1773. {
  1774. case 'b': r->graceFlags |= kBegGraceXsFl; break;
  1775. case 'a': r->graceFlags |= kAddGraceXsFl | kEndGraceXsFl; break;
  1776. case 's': r->graceFlags |= kSubGraceXsFl | kEndGraceXsFl; break;
  1777. case 'A': r->graceFlags |= kAFirstGraceXsFl| kEndGraceXsFl; break;
  1778. case 'n': r->graceFlags |= kNFirstGraceXsFl| kEndGraceXsFl; break;
  1779. case 'g': break;
  1780. case '%':
  1781. *graceGroupIdRef += 1;
  1782. ++s;
  1783. continue;
  1784. default:
  1785. { assert(0); }
  1786. }
  1787. break;
  1788. }
  1789. return rc;
  1790. }
  1791. cmXsRC_t _cmXScoreReorderParsePitch(cmXScore_t* p, const cmChar_t* b, unsigned line, unsigned* pitchRef )
  1792. {
  1793. cmXsRC_t rc = kOkXsRC;
  1794. cmChar_t* s;
  1795. cmChar_t buf[4];
  1796. unsigned i,j;
  1797. memset(buf,0,sizeof(buf));
  1798. *pitchRef = 0;
  1799. if((s = strchr(b,'$')) == NULL )
  1800. return rc;
  1801. ++s;
  1802. j=2;
  1803. for(i=0; i<j && s[i]; ++i,++s)
  1804. {
  1805. buf[i] = *s;
  1806. if( i==1 && (*s=='#' || *s=='b') )
  1807. j = 3;
  1808. if( i==0 && strchr("ABCDEFG",*s)==NULL )
  1809. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal pitch letter ('%c')specification line %i.",*s,line);
  1810. if( i==1 && !isdigit(*s) && *s!='#' && *s!='b' )
  1811. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal pitch level ('%c') specification line %i.",*s,line);
  1812. if( i==2 && !isdigit(*s) )
  1813. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Illegal pitch octave ('%c') specification line %i.",*s,line);
  1814. }
  1815. unsigned pitch = cmSciPitchToMidi(buf);
  1816. if( pitch<kInvalidMidiByte)
  1817. *pitchRef = pitch;
  1818. else
  1819. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"Pitch conversion from '%s' failed on line %i.",buf,line);
  1820. return rc;
  1821. }
  1822. cmXsRC_t cmXScoreReorder( cmXsH_t h, const cmChar_t* fn )
  1823. {
  1824. typedef enum { kFindMeasStId, kFindEventStId, kReadEventStId } stateId_t;
  1825. cmXsRC_t rc = kOkXsRC;
  1826. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1827. cmFileH_t fH = cmFileNullHandle;
  1828. cmChar_t* b = NULL;
  1829. unsigned bN = 0;
  1830. unsigned ln = 0;
  1831. stateId_t stateId = kFindMeasStId;
  1832. unsigned rN = 1024;
  1833. unsigned ri = 0;
  1834. unsigned measNumb = 0;
  1835. unsigned graceGroupId = 1;
  1836. cmXsReorder_t rV[ rN ];
  1837. if( cmFileOpen(&fH,fn,kReadFileFl,p->err.rpt) != kOkFileRC )
  1838. {
  1839. rc = cmErrMsg(&p->err,kFileFailXsRC,"The reordering file '%s' could not be opened.",cmStringNullGuard(fn));
  1840. return rc;
  1841. }
  1842. for(; cmFileGetLineAuto(fH,&b,&bN)==kOkFileRC; ++ln)
  1843. {
  1844. switch( stateId )
  1845. {
  1846. case kFindEventStId: // scanning past labels to an event line
  1847. {
  1848. unsigned voice,loc;
  1849. if( sscanf(b,"%i %i",&voice,&loc) != 2 )
  1850. continue;
  1851. stateId = kReadEventStId;
  1852. }
  1853. // fall through
  1854. case kReadEventStId:
  1855. {
  1856. cmXsReorder_t r;
  1857. char pitchStr[4];
  1858. memset(&r,0,sizeof(r));
  1859. // parse an event line
  1860. if( sscanf(b,"%i %i %i %i %i %f",&r.idx,&r.voice,&r.locIdx,&r.tick,&r.durtn,&r.rval) == 6 )
  1861. {
  1862. assert( strlen(b)>=52);
  1863. int PC = 39; // text file column where first pitch char occurs
  1864. if( b[PC] == ' ')
  1865. r.midi = 0;
  1866. else
  1867. {
  1868. pitchStr[0] = b[PC+0];
  1869. pitchStr[1] = b[PC+1];
  1870. pitchStr[2] = b[PC+2];
  1871. pitchStr[3] = 0;
  1872. if( !isdigit(pitchStr[2]) )
  1873. r.midi = 0;
  1874. else
  1875. {
  1876. if( pitchStr[1] == ' ')
  1877. {
  1878. pitchStr[1] = pitchStr[2];
  1879. pitchStr[2] = 0;
  1880. }
  1881. r.midi = cmSciPitchToMidi(pitchStr);
  1882. }
  1883. }
  1884. // parse the dynamic marking following a '!'
  1885. if((rc = _cmXScoreReorderParseDyn(p,b,ln+1,&r.dynIdx)) != kOkXsRC )
  1886. goto errLabel;
  1887. // parse the flag edits
  1888. if((rc = _cmXScoreReorderParseFlags(p,b,ln+1, &r.newFlags)) != kOkXsRC )
  1889. goto errLabel;
  1890. // parse the @newtick marker
  1891. if((rc = _cmXScoreReorderParseTick(p, b, ln+1, &r.newTick)) != kOkXsRC )
  1892. goto errLabel;
  1893. // parse the %grace note marker
  1894. if((rc = _cmXScoreReorderParseGrace(p, b, ln+1, &r, &graceGroupId)) != kOkXsRC )
  1895. goto errLabel;
  1896. // parse the $pitch marker
  1897. if((rc = _cmXScoreReorderParsePitch(p, b, ln+1, &r.pitch )) != kOkXsRC )
  1898. goto errLabel;
  1899. // process grace notes - these need to be processed separate from
  1900. // the _cmXScoreReorderMeas() because grace notes may cross measure boundaries.
  1901. /*
  1902. if( r.graceType != 0 )
  1903. {
  1904. r.graceGroupId = graceGroupId;
  1905. // if this is an end of a grace note group
  1906. if( r.graceType != 'g' && r.graceType != 'b' )
  1907. {
  1908. graceGroupId += 1;
  1909. }
  1910. }
  1911. */
  1912. // store the record
  1913. assert( ri < rN );
  1914. rV[ri++] = r;
  1915. continue;
  1916. }
  1917. // the end of the measure was encountered -
  1918. // reorder the measure based on the cmXsReorder_t in rV[ri]
  1919. if((rc = _cmXScoreReorderMeas(p, measNumb, rV, ri )) != kOkXsRC )
  1920. goto errLabel;
  1921. ri = 0;
  1922. stateId = kFindMeasStId;
  1923. // fall through
  1924. }
  1925. case kFindMeasStId: // scanning for a bar-line
  1926. {
  1927. char colon;
  1928. if( sscanf(b,"%i %c",&measNumb,&colon) == 2 && colon == ':' )
  1929. {
  1930. //printf("meas: %i \n",measNumb);
  1931. stateId = kFindEventStId;
  1932. }
  1933. }
  1934. break;
  1935. }
  1936. }
  1937. // the ticks may have changed so the 'secs' and 'dsecs' must be updated
  1938. _cmXScoreSetAbsoluteTime( p );
  1939. // the bar lines should be the first event in the measure
  1940. _cmXScoreFixBarLines(p);
  1941. // resort to force the links to be correct
  1942. _cmXScoreSort(p);
  1943. // process the grace notes.
  1944. _cmXScoreProcessGraceNotes( p );
  1945. // inserting grace notes may have left the score unsorted
  1946. _cmXScoreSort(p);
  1947. errLabel:
  1948. cmFileClose(&fH);
  1949. cmMemFree(b);
  1950. return rc;
  1951. }
  1952. /*
  1953. kMidiFileIdColScIdx= 0,
  1954. kTypeLabelColScIdx = 3,
  1955. kDSecsColScIdx = 4,
  1956. kSecsColScIdx = 5,
  1957. kD0ColScIdx = 9,
  1958. kD1ColScIdx = 10,
  1959. kPitchColScIdx = 11,
  1960. kBarColScIdx = 13,
  1961. kSkipColScIdx = 14,
  1962. kEvenColScIdx = 15,
  1963. kGraceColScIdx = 16,
  1964. kTempoColScIdx = 17,
  1965. kFracColScIdx = 18,
  1966. kDynColScIdx = 19,
  1967. kSectionColScIdx = 20,
  1968. kRecdPlayColScIdx = 21,
  1969. kRemarkColScIdx = 22
  1970. */
  1971. cmXsRC_t _cmXScoreWriteCsvHdr( cmXScore_t* p )
  1972. {
  1973. const cmChar_t* s[] =
  1974. {
  1975. "id","trk","evt","opcode","dticks","micros","status",
  1976. "meta","ch","d0","d1","arg0","arg1","bar","skip",
  1977. "even","grace","tempo","t frac","dyn","section","play_recd","remark",NULL
  1978. };
  1979. cmCsvCell_t* lcp = NULL;
  1980. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymText(p->csvH,s[0]), 0, 0 ) != kOkCsvRC )
  1981. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  1982. unsigned i;
  1983. for(i=1; s[i]!=NULL; ++i)
  1984. {
  1985. if( cmCsvInsertIdentColAfter(p->csvH, lcp, &lcp, s[i], 0 ) != kOkCsvRC )
  1986. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV error inserting CSV title %i.\n",i);
  1987. }
  1988. return kOkXsRC;
  1989. }
  1990. cmXsRC_t _cmXScoreWriteCsvBlankCols( cmXScore_t* p, unsigned cnt, cmCsvCell_t** leftCellPtrPtr )
  1991. {
  1992. unsigned i;
  1993. for(i=0; i<cnt; ++i)
  1994. if( cmCsvInsertIdentColAfter(p->csvH,*leftCellPtrPtr,leftCellPtrPtr,"",0) != kOkCsvRC )
  1995. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV output failed on blank column.");
  1996. return kOkCsvRC;
  1997. }
  1998. const cmChar_t* _cmXScoreTranslateDynamics( cmXScore_t* p, const cmXsNote_t* np, cmChar_t* buf, unsigned bufN )
  1999. {
  2000. if( cmIsFlag(np->flags,kDynXsFl) && np->dynamics != 0 )
  2001. {
  2002. const cmChar_t* dynStr = NULL;
  2003. switch(np->dynamics)
  2004. {
  2005. case 1: dynStr = "pppp"; break;
  2006. case 2: dynStr = "ppp"; break;
  2007. case 3: dynStr = "pp"; break;
  2008. case 4: dynStr = "p"; break;
  2009. case 5: dynStr = "mp"; break;
  2010. case 6: dynStr = "mf"; break;
  2011. case 7: dynStr = "f"; break;
  2012. case 8: dynStr = "ff"; break;
  2013. case 9: dynStr = "fff"; break;
  2014. default:
  2015. cmErrMsg(&p->err,kSyntaxErrorXsRC,"An invalid dynamic value (%i) was encountered.",np->dynamics);
  2016. goto errLabel;
  2017. }
  2018. if( np->dynGroupId == 0 )
  2019. snprintf(buf,bufN,"%s",dynStr);
  2020. else
  2021. snprintf(buf,bufN,"%s %i",dynStr,np->dynGroupId);
  2022. return buf;
  2023. }
  2024. errLabel:
  2025. return "";
  2026. }
  2027. const cmChar_t* cmXsFormatMeasurementCsvField( unsigned flags, unsigned fl, char abbrev, unsigned sectionId, char* buf, unsigned bufN )
  2028. {
  2029. assert( bufN > 1 );
  2030. buf[0] = ' ';
  2031. buf[1] = 0;
  2032. if( cmIsFlag(flags,fl) )
  2033. {
  2034. if( sectionId != 0 )
  2035. snprintf(buf,bufN-1,"%c %i%c",abbrev,sectionId, cmIsFlag(flags,kHeelXsFl)?'*':' ');
  2036. else
  2037. buf[0] = abbrev;
  2038. }
  2039. return buf;
  2040. }
  2041. cmXsRC_t _cmXScoreWriteCsvRow(
  2042. cmXScore_t* p,
  2043. unsigned rowIdx,
  2044. unsigned uid,
  2045. unsigned bar,
  2046. const cmChar_t* sectionStr,
  2047. const cmChar_t* opCodeStr,
  2048. double dsecs,
  2049. double secs,
  2050. unsigned d0,
  2051. unsigned d1,
  2052. unsigned pitch, // set to -1 if the pitch is not valid
  2053. double frac,
  2054. const cmChar_t* dynStr,
  2055. unsigned flags,
  2056. const cmChar_t* evenStr,
  2057. const cmChar_t* tempoStr)
  2058. {
  2059. cmXsRC_t rc = kOkXsRC;
  2060. cmCsvCell_t* lcp = NULL;
  2061. // append an empty row to the CSV object
  2062. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymUInt(p->csvH, rowIdx ), 0, 0 ) != kOkCsvRC )
  2063. {
  2064. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  2065. goto errLabel;
  2066. }
  2067. /*
  2068. // col 0 : blanks
  2069. if( cmCsvInsertUIntColAfter(p->csvH, lcp, &lcp, rowIdx, 0 ) != kOkCsvRC )
  2070. {
  2071. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output row index failed.");
  2072. goto errLabel;
  2073. }
  2074. */
  2075. // col 1 : track (always 1)
  2076. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,1,0) != kOkCsvRC )
  2077. {
  2078. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  2079. goto errLabel;
  2080. }
  2081. // col 2 : evt (set to event uid, or blank if uid == -1)
  2082. if( uid == -1 )
  2083. {
  2084. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  2085. goto errLabel;
  2086. }
  2087. else
  2088. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,uid,0) != kOkCsvRC )
  2089. {
  2090. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  2091. goto errLabel;
  2092. }
  2093. // col 3 : output the opcode
  2094. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,opCodeStr,0) != kOkCsvRC )
  2095. {
  2096. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on opcode label.");
  2097. goto errLabel;
  2098. }
  2099. // col 4 : dsecs
  2100. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,dsecs,0) != kOkCsvRC )
  2101. {
  2102. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dsecs'.");
  2103. goto errLabel;
  2104. }
  2105. // col 5 : secs
  2106. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,secs,0) != kOkCsvRC )
  2107. {
  2108. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'secs'.");
  2109. goto errLabel;
  2110. }
  2111. // cols 6,7,8 blanks
  2112. if((rc = _cmXScoreWriteCsvBlankCols(p,3,&lcp)) != kOkXsRC )
  2113. goto errLabel;
  2114. // col 9 : d0
  2115. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d0,0) != kOkCsvRC )
  2116. {
  2117. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  2118. goto errLabel;
  2119. }
  2120. // col 10 : d1
  2121. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d1,0) != kOkCsvRC )
  2122. {
  2123. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd1'.");
  2124. goto errLabel;
  2125. }
  2126. // col 11 : pitch
  2127. if( pitch == -1 )
  2128. {
  2129. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  2130. goto errLabel;
  2131. }
  2132. else
  2133. {
  2134. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmMidiToSciPitch(pitch,NULL,0),0) != kOkCsvRC )
  2135. {
  2136. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  2137. goto errLabel;
  2138. }
  2139. }
  2140. // col 12 : blanks
  2141. if((rc = _cmXScoreWriteCsvBlankCols(p,1 + (cmIsFlag(flags,kBarXsFl) ? 0 : 1), &lcp)) != kOkXsRC )
  2142. goto errLabel;
  2143. // col 13 : bar number
  2144. if( cmIsFlag(flags,kBarXsFl) )
  2145. {
  2146. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,bar,0) != kOkCsvRC )
  2147. {
  2148. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  2149. goto errLabel;
  2150. }
  2151. }
  2152. // col 14 : skip (blank for now)
  2153. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  2154. goto errLabel;
  2155. // col 15: even (all grace notes are 'even' notes
  2156. if( cmCsvInsertQTextColAfter(p->csvH,lcp,&lcp, evenStr, 0) != kOkCsvRC )
  2157. {
  2158. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  2159. goto errLabel;
  2160. }
  2161. // col 16: grace
  2162. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kGraceXsFl) ? "g" : "",0) != kOkCsvRC )
  2163. {
  2164. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  2165. goto errLabel;
  2166. }
  2167. // col 17: tempo
  2168. if( cmCsvInsertQTextColAfter(p->csvH,lcp,&lcp,tempoStr,0) != kOkCsvRC )
  2169. {
  2170. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  2171. goto errLabel;
  2172. }
  2173. // col 18: frac
  2174. if( frac == 0 )
  2175. {
  2176. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  2177. goto errLabel;
  2178. }
  2179. else
  2180. {
  2181. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,1.0/frac,0) != kOkCsvRC )
  2182. {
  2183. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 't frac'.");
  2184. goto errLabel;
  2185. }
  2186. }
  2187. // col 19: dynamic marking
  2188. if( cmCsvInsertQTextColAfter(p->csvH, lcp, &lcp, dynStr, 0 ) != kOkCsvRC )
  2189. {
  2190. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dynamics'.");
  2191. goto errLabel;
  2192. }
  2193. //if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  2194. // goto errLabel;
  2195. // col 20: section
  2196. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,sectionStr!=NULL ? sectionStr : "",0) != kOkCsvRC )
  2197. {
  2198. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  2199. goto errLabel;
  2200. }
  2201. // col 21, 22 : recd-play, remark (blank for now)
  2202. if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
  2203. goto errLabel;
  2204. errLabel:
  2205. return rc;
  2206. }
  2207. cmXsRC_t cmXScoreWriteCsv( cmXsH_t h, const cmChar_t* csvFn )
  2208. {
  2209. cmXsRC_t rc = kOkXsRC;
  2210. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  2211. unsigned rowIdx = 1;
  2212. const cmChar_t* sectionIdStr = NULL;
  2213. if( !cmCsvIsValid(p->csvH) )
  2214. return cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output object is not initialized.");
  2215. if((rc = _cmXScoreWriteCsvHdr( p )) != kOkXsRC )
  2216. goto errLabel;
  2217. cmXsPart_t* pp = p->partL;
  2218. for(; pp!=NULL; pp=pp->link)
  2219. {
  2220. cmXsMeas_t* mp = pp->measL;
  2221. for(; mp!=NULL; mp=mp->link)
  2222. {
  2223. cmXsNote_t* np = mp->noteL;
  2224. for(; np!=NULL; np=np->slink)
  2225. {
  2226. // if this is a section event
  2227. if( cmIsFlag(np->flags,kSectionXsFl) )
  2228. sectionIdStr = np->tvalue;
  2229. // if this is a bar event
  2230. if( cmIsFlag(np->flags,kBarXsFl) )
  2231. {
  2232. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,sectionIdStr,"bar",np->dsecs,np->secs,0,0,-1,0,"",np->flags,"","");
  2233. sectionIdStr = NULL;
  2234. }
  2235. else
  2236. {
  2237. // if this is a pedal event
  2238. if( cmIsFlag(np->flags,kDampDnXsFl|kDampUpXsFl|kDampUpDnXsFl|kSostDnXsFl|kSostUpXsFl) )
  2239. {
  2240. unsigned d0 = cmIsFlag(np->flags,kSostDnXsFl |kSostUpXsFl) ? 66 : 64; // pedal MIDI ctl id
  2241. unsigned d1 = cmIsFlag(np->flags,kDampDnXsFl|kSostDnXsFl) ? 64 : 0; // pedal-dn: d1>=64 pedal-up:<64
  2242. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,sectionIdStr,"ctl",np->dsecs,np->secs,d0,d1,-1,0,"",np->flags,"","");
  2243. sectionIdStr = NULL;
  2244. if( cmIsFlag(np->flags,kDampUpDnXsFl) )
  2245. {
  2246. rowIdx += 1;
  2247. double millisecond = 0.0;
  2248. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,sectionIdStr,"ctl",millisecond,np->secs+millisecond,d0,64,-1,0,"",np->flags,"","");
  2249. }
  2250. }
  2251. else
  2252. {
  2253. // if this is a sounding note event
  2254. if( cmIsFlag(np->flags,kOnsetXsFl) )
  2255. {
  2256. unsigned bufN = 128;
  2257. cmChar_t ebuf[ bufN+1]; ebuf[bufN] = 0;
  2258. cmChar_t dbuf[ bufN+1]; dbuf[bufN] = 0;
  2259. cmChar_t tbuf[ bufN+1]; tbuf[bufN] = 0;
  2260. double frac = np->rvalue + (cmIsFlag(np->flags,kDotXsFl) ? (np->rvalue/2) : 0);
  2261. const cmChar_t* dyn = _cmXScoreTranslateDynamics( p, np, dbuf, bufN );
  2262. unsigned vel = np->vel==0 ? 60 : np->vel;
  2263. //
  2264. _cmXScoreWriteCsvRow(p,rowIdx,np->uid,mp->number,sectionIdStr,"non",np->dsecs,np->secs,np->pitch,vel,np->pitch,frac,dyn,np->flags,
  2265. cmXsFormatMeasurementCsvField(np->flags, kEvenXsFl, 'e', np->evenGroupId, ebuf, bufN ),
  2266. cmXsFormatMeasurementCsvField(np->flags, kTempoXsFl,'t', np->tempoGroupId, tbuf, bufN ));
  2267. sectionIdStr = NULL;
  2268. }
  2269. }
  2270. }
  2271. rowIdx += 1;
  2272. }
  2273. }
  2274. }
  2275. // Section labels are output on the next bar/pedal/note
  2276. // but what if there is no bar pedal note-on after the section label
  2277. if( sectionIdStr != NULL )
  2278. cmErrMsg(&p->err,kSyntaxErrorXsRC,"The section label '%s' was ignored because it was not followed by a score event.",sectionIdStr);
  2279. if( cmCsvWrite( p->csvH, csvFn ) != kOkCsvRC )
  2280. rc = cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output write failed on file '%s'.",csvFn);
  2281. errLabel:
  2282. return rc;
  2283. }
  2284. void _cmXScoreReportTitle( cmRpt_t* rpt )
  2285. {
  2286. cmRptPrintf(rpt," idx voc loc tick durtn rval flags\n");
  2287. cmRptPrintf(rpt," --- --- ----- ------- ----- ---- --- ---------------\n");
  2288. }
  2289. void _cmXScoreReportNote( cmRpt_t* rpt, const cmXsNote_t* note,unsigned index )
  2290. {
  2291. const cmChar_t* B = cmIsFlag(note->flags,kBarXsFl) ? "|" : "-";
  2292. const cmChar_t* R = cmIsFlag(note->flags,kRestXsFl) ? "R" : "-";
  2293. const cmChar_t* G = cmIsFlag(note->flags,kGraceXsFl) ? "G" : "-";
  2294. const cmChar_t* D = cmIsFlag(note->flags,kDotXsFl) ? "." : "-";
  2295. const cmChar_t* C = cmIsFlag(note->flags,kChordXsFl) ? "C" : "-";
  2296. const cmChar_t* e = cmIsFlag(note->flags,kEvenXsFl) ? "e" : "-";
  2297. const cmChar_t* d = cmIsFlag(note->flags,kDynXsFl) ? "d" : "-";
  2298. const cmChar_t* t = cmIsFlag(note->flags,kTempoXsFl) ? "t" : "-";
  2299. const cmChar_t* P = cmIsFlag(note->flags,kDampDnXsFl) ? "V" : "-";
  2300. const cmChar_t* s = cmIsFlag(note->flags,kSostDnXsFl) ? "{" : "-";
  2301. const cmChar_t* S = cmIsFlag(note->flags,kSectionXsFl) ? "S" : "-";
  2302. const cmChar_t* H = cmIsFlag(note->flags,kHeelXsFl) ? "H" : "-";
  2303. const cmChar_t* T0 = cmIsFlag(note->flags,kTieBegXsFl) ? "T" : "-";
  2304. const cmChar_t* T1 = cmIsFlag(note->flags,kTieEndXsFl) ? "_" : "-";
  2305. const cmChar_t* O = cmIsFlag(note->flags,kOnsetXsFl) ? "*" : "-";
  2306. P = cmIsFlag(note->flags,kDampUpXsFl) ? "^" : P;
  2307. P = cmIsFlag(note->flags,kDampUpDnXsFl) ? "X" : P;
  2308. s = cmIsFlag(note->flags,kSostUpXsFl) ? "}" : s;
  2309. //const cmChar_t* N = note->pitch==0 ? " " : cmMidiToSciPitch( note->pitch, NULL, 0 );
  2310. cmChar_t N[] = {'\0','\0','\0','\0'};
  2311. cmChar_t acc = note->alter==-1?'b':(note->alter==1?'#':' ');
  2312. snprintf(N,4,"%c%c%1i",note->step,acc,note->octave);
  2313. 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",
  2314. index,
  2315. note->voice->id,
  2316. note->locIdx,
  2317. note->tick,
  2318. note->duration,
  2319. note->rvalue,
  2320. N,B,R,G,D,C,e,d,t,P,s,S,H,T0,T1,O);
  2321. if( cmIsFlag(note->flags,kSectionXsFl) )
  2322. cmRptPrintf(rpt," %s",cmStringNullGuard(note->tvalue));
  2323. if( cmIsFlag(note->flags,kMetronomeXsFl) )
  2324. cmRptPrintf(rpt," %i bpm",note->duration);
  2325. if( note->evenGroupId != 0 )
  2326. cmRptPrintf(rpt," e=%i",note->evenGroupId);
  2327. if( note->dynGroupId != 0 )
  2328. cmRptPrintf(rpt," d=%i",note->dynGroupId);
  2329. if( note->tempoGroupId != 0 )
  2330. cmRptPrintf(rpt," t=%i",note->tempoGroupId);
  2331. if( note->graceGroupId != 0)
  2332. cmRptPrintf(rpt," g=%i",note->graceGroupId);
  2333. }
  2334. void cmXScoreReport( cmXsH_t h, cmRpt_t* rpt, bool sortFl )
  2335. {
  2336. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  2337. cmXsPart_t* pp = p->partL;
  2338. for(; pp!=NULL; pp=pp->link)
  2339. {
  2340. cmRptPrintf(rpt,"Part:%s\n",pp->idStr);
  2341. const cmXsMeas_t* meas = pp->measL;
  2342. for(; meas!=NULL; meas=meas->link)
  2343. {
  2344. unsigned idx = 0;
  2345. 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);
  2346. _cmXScoreReportTitle(rpt);
  2347. if( sortFl )
  2348. {
  2349. const cmXsNote_t* note = meas->noteL;
  2350. unsigned t0 = 0;
  2351. unsigned t1 = 0;
  2352. for(; note!=NULL; note=note->slink,++idx)
  2353. {
  2354. _cmXScoreReportNote(rpt,note,idx);
  2355. t1 = note->slink==NULL ? note->tick : note->slink->tick;
  2356. if( !(t0 <= note->tick && note->tick <= t1) )
  2357. {
  2358. cmRptPrintf(rpt," +");
  2359. }
  2360. t0 = note->tick;
  2361. if( note->slink!=NULL || note->voice->id==0)
  2362. cmRptPrintf(rpt,"\n");
  2363. else
  2364. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  2365. }
  2366. }
  2367. else
  2368. {
  2369. const cmXsVoice_t* v = meas->voiceL;
  2370. for(; v!=NULL; v=v->link)
  2371. {
  2372. const cmXsNote_t* note = v->noteL;
  2373. cmRptPrintf(rpt," voice:%i\n",v->id);
  2374. for(; note!=NULL; note=note->mlink)
  2375. {
  2376. _cmXScoreReportNote(rpt,note,idx);
  2377. if( note->mlink!=NULL || note->voice->id==0)
  2378. cmRptPrintf(rpt,"\n");
  2379. else
  2380. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  2381. }
  2382. }
  2383. }
  2384. }
  2385. }
  2386. }
  2387. cmXsRC_t _cmXsWriteMidiFile( cmCtx_t* ctx, cmXsH_t h, const cmChar_t* dir, const cmChar_t* fn )
  2388. {
  2389. cmXsRC_t rc = kOkXsRC;
  2390. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  2391. if( p->partL==NULL || p->partL->measL == NULL )
  2392. return rc;
  2393. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  2394. unsigned trkN = 2;
  2395. unsigned ticksPerQN = p->partL->measL->divisions;
  2396. const cmChar_t* outFn = cmFsMakeFn(dir,fn,"mid",NULL);
  2397. if( cmMidiFileCreate( ctx, &mfH, trkN, ticksPerQN ) != kOkMfRC )
  2398. return cmErrMsg(&p->err,kMidiFailXsRC,"Unable to create the MIDI file object.");
  2399. cmXsPart_t* pp = p->partL;
  2400. // for each part
  2401. for(; pp!=NULL; pp=pp->link)
  2402. {
  2403. cmXsMeas_t* mp = pp->measL;
  2404. // for each measure
  2405. for(; mp!=NULL; mp=mp->link)
  2406. {
  2407. cmXsNote_t* np = mp->noteL;
  2408. if( mp->divisions != ticksPerQN )
  2409. cmErrWarnMsg(&p->err,kMidiFailXsRC,"The 'tick per quarter note' (divisions) field in measure %i does not match the value in the first measure (%i).",mp->divisions,ticksPerQN);
  2410. // for each note in this measure
  2411. for(; np!=NULL; np=np->slink)
  2412. {
  2413. switch( np->flags & (kOnsetXsFl|kMetronomeXsFl|kDampDnXsFl|kDampUpDnXsFl|kSostDnXsFl) )
  2414. {
  2415. case kOnsetXsFl:
  2416. if( cmMidiFileInsertTrackChMsg(mfH, 1, np->tick, kNoteOnMdId, np->pitch, np->vel ) != kOkMfRC
  2417. ||cmMidiFileInsertTrackChMsg(mfH, 1, np->tick + np->duration, kNoteOffMdId, np->pitch, 0 ) != kOkMfRC )
  2418. {
  2419. rc = kMidiFailXsRC;
  2420. }
  2421. break;
  2422. case kDampDnXsFl:
  2423. case kDampUpDnXsFl:
  2424. case kSostDnXsFl:
  2425. {
  2426. cmMidiByte_t d0 = cmIsFlag(np->flags,kSostDnXsFl) ? kSostenutoCtlMdId : kSustainCtlMdId;
  2427. if( (cmMidiFileInsertTrackChMsg(mfH, 1, np->tick, kCtlMdId, d0, 127 ) != kOkMfRC )
  2428. ||(cmMidiFileInsertTrackChMsg(mfH, 1, np->tick + np->duration, kCtlMdId, d0, 0 ) != kOkMfRC ) )
  2429. {
  2430. rc = kMidiFailXsRC;
  2431. }
  2432. }
  2433. break;
  2434. case kMetronomeXsFl:
  2435. if( cmMidFileInsertTrackTempoMsg(mfH, 0, np->tick, np->duration ) != kOkMfRC )
  2436. rc = kMidiFailXsRC;
  2437. break;
  2438. case 0:
  2439. break;
  2440. default:
  2441. { assert(0); }
  2442. }
  2443. if( rc != kOkXsRC )
  2444. {
  2445. rc = cmErrMsg(&p->err,rc,"MIDI message insert failed on '%s'.",cmStringNullGuard(outFn));
  2446. goto errLabel;
  2447. }
  2448. }
  2449. }
  2450. }
  2451. if( cmMidiFileWrite(mfH,outFn) != kOkMfRC )
  2452. {
  2453. rc = cmErrMsg(&p->err,kMidiFailXsRC,"MIDI file write failed on '%s'.",cmStringNullGuard(outFn));
  2454. goto errLabel;
  2455. }
  2456. errLabel:
  2457. cmFsFreeFn(outFn);
  2458. if( cmMidiFileClose(&mfH) != kOkMfRC )
  2459. {
  2460. rc = cmErrMsg(&p->err,kMidiFailXsRC,"Unable to create the MIDI file object.");
  2461. goto errLabel;
  2462. }
  2463. return rc;
  2464. }
  2465. typedef struct cmXsSvgEvt_str
  2466. {
  2467. unsigned flags; // k???XsFl
  2468. unsigned tick; // start tick
  2469. unsigned durTicks; // dur-ticks
  2470. unsigned voice; // score voice number
  2471. unsigned d0; // MIDI d0 (barNumb)
  2472. unsigned d1; // MIDI d1
  2473. struct cmXsSvgEvt_str* link;
  2474. } cmXsSvgEvt_t;
  2475. typedef struct cmXsMidiFile_str
  2476. {
  2477. cmXsSvgEvt_t* elist;
  2478. cmXsSvgEvt_t* eol;
  2479. unsigned pitch_min;
  2480. unsigned pitch_max;
  2481. } cmXsMidiFile_t;
  2482. cmXsRC_t _cmXsWriteMidiSvg( cmCtx_t* ctx, cmXScore_t* p, cmXsMidiFile_t* mf, const cmChar_t* dir, const cmChar_t* fn )
  2483. {
  2484. cmXsRC_t rc = kOkXsRC;
  2485. cmSvgH_t svgH = cmSvgNullHandle;
  2486. cmXsSvgEvt_t* e = mf->elist;
  2487. unsigned noteHeight = 10;
  2488. const cmChar_t* svgFn = cmFsMakeFn(dir,fn,"html",NULL);
  2489. const cmChar_t* cssFn = cmFsMakeFn(NULL,fn,"css",NULL);
  2490. cmChar_t* t0 = NULL; // temporary dynamic string
  2491. // create the SVG writer
  2492. if( cmSvgWriterAlloc(ctx,&svgH) != kOkSvgRC )
  2493. {
  2494. rc = cmErrMsg(&p->err,kSvgFailXsRC,"Unable to create the MIDI SVG output file '%s'.",cmStringNullGuard(svgFn));
  2495. goto errLabel;
  2496. }
  2497. // for each MIDI file element
  2498. for(; e!=NULL && rc==kOkXsRC; e=e->link)
  2499. {
  2500. switch( e->flags & (kOnsetXsFl|kBarXsFl|kDampDnXsFl|kDampUpDnXsFl|kSostDnXsFl))
  2501. {
  2502. // if this is a note
  2503. case kOnsetXsFl:
  2504. {
  2505. const cmChar_t* classLabel = "note";
  2506. t0 = cmTsPrintfP(t0,"note_%i%s",e->voice, cmIsFlag(e->flags,kGraceXsFl) ? "_g":"");
  2507. if( cmIsFlag(e->flags,kGraceXsFl) )
  2508. classLabel = "grace";
  2509. if( cmSvgWriterRect(svgH, e->tick, e->d0 * noteHeight, e->durTicks, noteHeight-1, t0 ) != kOkSvgRC )
  2510. rc = kSvgFailXsRC;
  2511. else
  2512. if( cmSvgWriterText(svgH, e->tick + e->durTicks/2, e->d0 * noteHeight + noteHeight/2, cmMidiToSciPitch( e->d0, NULL, 0), "pitch") != kOkSvgRC )
  2513. rc = kSvgFailXsRC;
  2514. }
  2515. break;
  2516. // if this is a bar
  2517. case kBarXsFl:
  2518. {
  2519. if( cmSvgWriterLine(svgH, e->tick, 0, e->tick, 127*noteHeight, "bar") != kOkSvgRC )
  2520. rc = kSvgFailXsRC;
  2521. else
  2522. {
  2523. if( cmSvgWriterText(svgH, e->tick, 10, t0 = cmTsPrintfP(t0,"%i",e->d0), "text" ) != kOkSvgRC )
  2524. rc = kSvgFailXsRC;
  2525. }
  2526. }
  2527. break;
  2528. // if this is a pedal event
  2529. case kDampDnXsFl:
  2530. case kDampUpDnXsFl:
  2531. case kSostDnXsFl:
  2532. {
  2533. const cmChar_t* classLabel = cmIsFlag(e->flags,kSostDnXsFl) ? "sost" : "damp";
  2534. unsigned y = (128 + cmIsFlag(e->flags,kSostDnXsFl)?1:0) * noteHeight;
  2535. cmSvgWriterRect(svgH, e->tick, y, e->durTicks, noteHeight-1, classLabel);
  2536. }
  2537. break;
  2538. }
  2539. }
  2540. if( rc != kOkXsRC )
  2541. cmErrMsg(&p->err,kSvgFailXsRC,"SVG element insert failed.");
  2542. if( rc == kOkXsRC )
  2543. if( cmSvgWriterWrite(svgH,cssFn,svgFn) != kOkSvgRC )
  2544. rc = cmErrMsg(&p->err,kSvgFailXsRC,"SVG file write to '%s' failed.",cmStringNullGuard(svgFn));
  2545. errLabel:
  2546. cmSvgWriterFree(&svgH);
  2547. cmFsFreeFn(svgFn);
  2548. cmFsFreeFn(cssFn);
  2549. cmMemFree(t0);
  2550. return rc;
  2551. }
  2552. void _cmXsPushSvgEvent( cmXScore_t* p, cmXsMidiFile_t* mf, unsigned flags, unsigned tick, unsigned durTick, unsigned voice, unsigned d0, unsigned d1 )
  2553. {
  2554. cmXsSvgEvt_t* e = cmLhAllocZ(p->lhH,cmXsSvgEvt_t,1);
  2555. e->flags = flags;
  2556. e->tick = tick;
  2557. e->durTicks = durTick;
  2558. e->voice = voice;
  2559. e->d0 = d0; // note=pitch bar=number pedal=ctl# metronome=BPM
  2560. e->d1 = d1;
  2561. if( mf->eol != NULL )
  2562. mf->eol->link = e;
  2563. else
  2564. mf->elist = e;
  2565. // track the min/max pitch
  2566. if( cmIsFlag(flags,kOnsetXsFl) )
  2567. {
  2568. mf->pitch_min = mf->eol==NULL ? d0 : cmMin(mf->pitch_min,d0);
  2569. mf->pitch_max = mf->eol==NULL ? d0 : cmMin(mf->pitch_max,d0);
  2570. }
  2571. mf->eol = e;
  2572. }
  2573. cmXsRC_t _cmXScoreGenSvg( cmCtx_t* ctx, cmXsH_t h, const cmChar_t* dir, const cmChar_t* fn )
  2574. {
  2575. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  2576. cmXsPart_t* pp = p->partL;
  2577. cmXsMidiFile_t mf;
  2578. memset(&mf,0,sizeof(mf));
  2579. for(; pp!=NULL; pp=pp->link)
  2580. {
  2581. const cmXsMeas_t* meas = pp->measL;
  2582. for(; meas!=NULL; meas=meas->link)
  2583. {
  2584. const cmXsNote_t* note = meas->noteL;
  2585. for(; note!=NULL; note=note->slink)
  2586. {
  2587. // if this is a metronome marker
  2588. if( cmIsFlag(note->flags,kMetronomeXsFl) )
  2589. {
  2590. // set BPM as d0
  2591. _cmXsPushSvgEvent(p,&mf,note->flags,note->tick,0,0,note->duration,0);
  2592. continue;
  2593. }
  2594. // if this is a sounding note
  2595. if( cmIsFlag(note->flags,kOnsetXsFl) )
  2596. {
  2597. unsigned d0 = cmSciPitchToMidiPitch( note->step, note->alter, note->octave );
  2598. unsigned durTick = note->duration;
  2599. if( note->tied != NULL )
  2600. {
  2601. cmXsNote_t* tn = note->tied;
  2602. for(; tn!=NULL; tn=tn->tied)
  2603. durTick += tn->duration;
  2604. }
  2605. _cmXsPushSvgEvent(p,&mf,note->flags,note->tick,durTick,note->voice->id,d0,note->vel);
  2606. continue;
  2607. }
  2608. // if this is a bar event
  2609. if( cmIsFlag(note->flags,kBarXsFl) )
  2610. {
  2611. _cmXsPushSvgEvent(p,&mf,note->flags,note->tick,0,0,note->meas->number,0);
  2612. continue;
  2613. }
  2614. // if this is a pedal event
  2615. if( cmIsFlag(note->flags,kDampDnXsFl|kDampUpDnXsFl|kSostDnXsFl) )
  2616. {
  2617. unsigned d0 = cmIsFlag(note->flags,kSostDnXsFl) ? kSostenutoCtlMdId : kSustainCtlMdId;
  2618. _cmXsPushSvgEvent(p,&mf,note->flags,note->tick,note->duration,0,d0,127);
  2619. continue;
  2620. }
  2621. }
  2622. }
  2623. }
  2624. return _cmXsWriteMidiSvg( ctx, p, &mf, dir, fn );
  2625. }
  2626. cmXsRC_t cmXScoreTest(
  2627. cmCtx_t* ctx,
  2628. const cmChar_t* xmlFn,
  2629. const cmChar_t* reorderFn,
  2630. const cmChar_t* csvOutFn,
  2631. const cmChar_t* midiOutFn)
  2632. {
  2633. cmXsRC_t rc;
  2634. cmXsH_t h = cmXsNullHandle;
  2635. if((rc = cmXScoreInitialize( ctx, &h, xmlFn)) != kOkXsRC )
  2636. return cmErrMsg(&ctx->err,rc,"XScore alloc failed.");
  2637. if( reorderFn != NULL )
  2638. if((rc = cmXScoreReorder(h,reorderFn)) != kOkXsRC )
  2639. {
  2640. cmErrMsg(&ctx->err,rc,"XScore reorder failed.");
  2641. goto errLabel;
  2642. }
  2643. // assign durations to pedal down events
  2644. _cmXScoreProcessPedals(_cmXScoreHandleToPtr(h));
  2645. if( csvOutFn != NULL )
  2646. {
  2647. cmScH_t scH = cmScNullHandle;
  2648. double srate = 44100.0;
  2649. cmXScoreWriteCsv(h,csvOutFn);
  2650. cmSymTblH_t stH = cmSymTblCreate(cmSymTblNullHandle, 0, ctx );
  2651. if( cmScoreInitialize( ctx, &scH, csvOutFn, srate, NULL, 0, NULL, NULL, stH) != kOkScRC )
  2652. cmErrMsg(&ctx->err,kFileFailXsRC,"The generated CSV file could not be parsed.");
  2653. else
  2654. {
  2655. //cmScorePrintSets(scH,&ctx->rpt);
  2656. //cmScorePrint(scH,&ctx->rpt);
  2657. cmScoreFinalize(&scH);
  2658. }
  2659. cmSymTblDestroy(&stH);
  2660. }
  2661. if( midiOutFn != NULL )
  2662. {
  2663. cmFileSysPathPart_t* pp = cmFsPathParts(midiOutFn);
  2664. _cmXScoreGenSvg( ctx, h, pp->dirStr, pp->fnStr );
  2665. _cmXsWriteMidiFile(ctx, h, pp->dirStr, pp->fnStr );
  2666. cmFsFreePathParts(pp);
  2667. }
  2668. cmXScoreReport(h,&ctx->rpt,true);
  2669. errLabel:
  2670. return cmXScoreFinalize(&h);
  2671. }