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

cmXScore.c 68KB

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