libcm is a C development framework with an emphasis on audio signal processing applications.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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