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

cmXScore.c 59KB

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