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 58KB

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