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

cmXScore.c 80KB

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