libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmXScore.c 103KB

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