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

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