libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmXScore.c 39KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmRpt.h"
  5. #include "cmErr.h"
  6. #include "cmCtx.h"
  7. #include "cmMem.h"
  8. #include "cmMallocDebug.h"
  9. #include "cmLinkedHeap.h"
  10. #include "cmXml.h"
  11. #include "cmText.h"
  12. #include "cmXScore.h"
  13. #include "cmTime.h"
  14. #include "cmMidi.h"
  15. #include "cmMidiFile.h"
  16. #include "cmLex.h"
  17. #include "cmCsv.h"
  18. cmXsH_t cmXsNullHandle = cmSTATIC_NULL_HANDLE;
  19. enum
  20. {
  21. kSectionXsFl = 0x00001, // rvalue holds section number
  22. kBarXsFl = 0x00002,
  23. kRestXsFl = 0x00004,
  24. kGraceXsFl = 0x00008,
  25. kDotXsFl = 0x00010,
  26. kChordXsFl = 0x00020,
  27. kDynXsFl = 0x00040,
  28. kEvenXsFl = 0x00080,
  29. kTempoXsFl = 0x00100,
  30. kHeelXsFl = 0x00200,
  31. kTieBegXsFl = 0x00400,
  32. kTieEndXsFl = 0x00800,
  33. kTieProcXsFl = 0x01000,
  34. kPedalDnXsFl = 0x02000,
  35. kPedalUpXsFl = 0x04000,
  36. kPedalUpDnXsFl = 0x08000,
  37. kMetronomeXsFl = 0x10000, // duration holds BPM
  38. kOnsetXsFl = 0x20000 // this is a sounding note
  39. };
  40. struct cmXsMeas_str;
  41. struct cmXsVoice_str;
  42. typedef struct cmXsNote_str
  43. {
  44. unsigned flags; // See k???XsFl
  45. unsigned pitch; // midi pitch
  46. unsigned velocity; // midi velocity
  47. cmChar_t step; // A-G
  48. unsigned octave; // sci pitch octave
  49. int alter; // +n=sharps,-n=flats
  50. unsigned staff;
  51. unsigned tick; //
  52. unsigned duration; // duration in ticks
  53. 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, ...)
  54. const cmChar_t* tvalue; // text value
  55. struct cmXsVoice_str* voice; // voice this note belongs to
  56. struct cmXsMeas_str* meas; // measure this note belongs to
  57. const cmXmlNode_t* xmlNode; // note xml ptr
  58. struct cmXsNote_str* mlink; // measure note list
  59. struct cmXsNote_str* slink; // time sorted event list
  60. } cmXsNote_t;
  61. typedef struct cmXsVoice_str
  62. {
  63. unsigned id; // Voice id
  64. cmXsNote_t* noteL; // List of notes in this voice
  65. struct cmXsVoice_str* link; // Link to other voices in this measure
  66. } cmXsVoice_t;
  67. typedef struct cmXsSpan_str
  68. {
  69. unsigned staff;
  70. unsigned number;
  71. struct cmXsMeas_str* meas;
  72. unsigned tick0;
  73. unsigned tick1;
  74. int pitch_offset;
  75. struct cmXsSpan_str* link;
  76. } cmXsSpan_t;
  77. typedef struct cmXsMeas_str
  78. {
  79. unsigned number; // Measure number
  80. unsigned divisions; // ticks-per-quarter-note
  81. unsigned beats; // beats per measure
  82. unsigned beat_type; // whole/half/quarter/eighth ...
  83. cmXsVoice_t* voiceL; // List of voices in this measure
  84. cmXsNote_t* noteL; // List of time sorted notes in this measure
  85. struct cmXsMeas_str* link; // Link to other measures in this part.
  86. } cmXsMeas_t;
  87. typedef struct cmXsPart_str
  88. {
  89. const cmChar_t* idStr; // Id of this part
  90. cmXsMeas_t* measL; // List of measures in this part.
  91. struct cmXsPart_str* link; // Link to other parts in this score
  92. } cmXsPart_t;
  93. typedef struct
  94. {
  95. cmErr_t err;
  96. cmXmlH_t xmlH;
  97. cmLHeapH_t lhH;
  98. cmXsPart_t* partL;
  99. cmCsvH_t csvH;
  100. cmXsSpan_t* spanL;
  101. } cmXScore_t;
  102. cmXScore_t* _cmXScoreHandleToPtr( cmXsH_t h )
  103. {
  104. cmXScore_t* p = (cmXScore_t*)h.h;
  105. assert( p != NULL );
  106. return p;
  107. }
  108. cmXsRC_t _cmXScoreFinalize( cmXScore_t* p )
  109. {
  110. cmXsRC_t rc = kOkXsRC;
  111. // release the XML file
  112. cmXmlFree( &p->xmlH );
  113. // release the local linked heap memory
  114. cmLHeapDestroy(&p->lhH);
  115. // release the CSV output object
  116. cmCsvFinalize(&p->csvH);
  117. cmMemFree(p);
  118. return rc;
  119. }
  120. cmXsRC_t _cmXScoreMissingNode( cmXScore_t* p, const cmXmlNode_t* parent, const cmChar_t* label )
  121. {
  122. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML node '%s'. Parent line:%i",label,parent->line);
  123. }
  124. cmXsRC_t _cmXScoreMissingAttribute( cmXScore_t* p, const cmXmlNode_t* np, const cmChar_t* attrLabel )
  125. {
  126. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML attribute '%s' from node '%s'.",attrLabel,np->label);
  127. }
  128. cmXsVoice_t* _cmXScoreIdToVoice( cmXsMeas_t* meas, unsigned voiceId )
  129. {
  130. cmXsVoice_t* v = meas->voiceL;
  131. for(; v!=NULL; v=v->link)
  132. if( v->id == voiceId )
  133. return v;
  134. return NULL;
  135. }
  136. cmXsRC_t _cmXScorePushNote( cmXScore_t* p, cmXsMeas_t* meas, unsigned voiceId, cmXsNote_t* note )
  137. {
  138. cmXsVoice_t* v;
  139. // get the voice recd
  140. if((v = _cmXScoreIdToVoice(meas,voiceId)) == NULL)
  141. {
  142. // the voice recd doesn't exist for this voiceId - allocate one
  143. v = cmLhAllocZ(p->lhH,cmXsVoice_t,1);
  144. v->id = voiceId;
  145. // add the voice record to the meas->voiceL
  146. if( meas->voiceL == NULL )
  147. meas->voiceL = v;
  148. else
  149. {
  150. cmXsVoice_t* vp = meas->voiceL;
  151. while( vp->link!=NULL )
  152. vp = vp->link;
  153. vp->link = v;
  154. }
  155. }
  156. // add the note record to the end of meas->voiceL
  157. if( v->noteL == NULL )
  158. v->noteL = note;
  159. else
  160. {
  161. cmXsNote_t* n = v->noteL;
  162. while( n->mlink != NULL )
  163. n = n->mlink;
  164. n->mlink = note;
  165. }
  166. note->voice = v;
  167. return kOkXsRC;
  168. }
  169. cmXsRC_t _cmXScoreParsePartList( cmXScore_t* p )
  170. {
  171. cmXsRC_t rc = kOkXsRC;
  172. cmXsPart_t* lastPartPtr = NULL;
  173. const cmXmlNode_t* xnp;
  174. // find the 'part-list'
  175. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part-list", NULL, 0)) == NULL )
  176. return _cmXScoreMissingNode(p,cmXmlRoot(p->xmlH),"part-list");
  177. const cmXmlNode_t* cnp = xnp->children;
  178. // for each child of the 'part-list'
  179. for(; cnp!=NULL; cnp=cnp->sibling)
  180. if( cmTextCmp( cnp->label, "score-part" ) == 0 )
  181. {
  182. const cmXmlAttr_t* a;
  183. // find the 'score-part' id
  184. if((a = cmXmlFindAttrib(cnp,"id")) == NULL )
  185. return _cmXScoreMissingAttribute(p,cnp,"id");
  186. // allocate a new part record
  187. cmXsPart_t* pp = cmLhAllocZ(p->lhH,cmXsPart_t,1);
  188. pp->idStr = a->value; // set the part id
  189. // link the new part record to the end of the part list
  190. if(lastPartPtr == NULL)
  191. p->partL = pp;
  192. else
  193. lastPartPtr->link = pp;
  194. lastPartPtr = pp;
  195. }
  196. return rc;
  197. }
  198. cmXsRC_t _cmXScoreParsePitch( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* np )
  199. {
  200. cmXsRC_t rc = kOkXsRC;
  201. unsigned octave = 0;
  202. double alter = 0;
  203. const cmChar_t* step = NULL;
  204. if((step = cmXmlNodeValue(nnp,"pitch","step",NULL)) == NULL )
  205. return _cmXScoreMissingNode(p,nnp,"step");
  206. if((rc = cmXmlNodeUInt( nnp,&octave,"pitch","octave",NULL)) != kOkXmlRC )
  207. return _cmXScoreMissingNode(p,nnp,"octave");
  208. cmXmlNodeDouble( nnp,&alter,"pitch","alter",NULL);
  209. cmChar_t buf[3] = { *step, '0', '\0'};
  210. unsigned midi = cmSciPitchToMidi(buf);
  211. midi += (12 * octave);
  212. midi += alter;
  213. np->pitch = midi;
  214. np->step = *step;
  215. np->octave = octave;
  216. np->alter = alter;
  217. np->flags |= kOnsetXsFl;
  218. return rc;
  219. }
  220. cmXsRC_t _cmXScoreParseNoteRValue( cmXScore_t* p, const cmXmlNode_t* nnp, const cmChar_t* label, double* rvalueRef )
  221. {
  222. typedef struct map_str
  223. {
  224. double rvalue;
  225. const cmChar_t* label;
  226. } map_t;
  227. map_t mapV[] =
  228. {
  229. {-1.0, "measure" }, // whole measure rest
  230. { 0.5, "breve" }, // double whole
  231. { 1.0, "whole" },
  232. { 2.0, "half" },
  233. { 4.0, "quarter" },
  234. { 8.0, "eighth" },
  235. {16.0, "16th" },
  236. {32.0, "32nd" },
  237. { 64.0, "64th" },
  238. {128.0, "128th" },
  239. { 0.0, "" }
  240. };
  241. const cmChar_t* str;
  242. // get the XML rvalue label
  243. if((str = cmXmlNodeValue(nnp,label,NULL)) == NULL)
  244. {
  245. if((nnp = cmXmlSearch(nnp,"rest",NULL,0)) != NULL )
  246. {
  247. const cmXmlAttr_t* a;
  248. if((a = cmXmlFindAttrib(nnp,"measure")) != NULL && cmTextCmp(a->value,"yes")==0)
  249. {
  250. *rvalueRef = -1;
  251. return kOkXsRC;
  252. }
  253. }
  254. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'beat-unit' metronome value is missing on line %i.",nnp->line);
  255. }
  256. unsigned i;
  257. // lookup the rvalue numeric value from the mapV[] table
  258. for(i=0; mapV[i].rvalue!=0; ++i)
  259. if( cmTextCmp(mapV[i].label,str) == 0 )
  260. {
  261. *rvalueRef = mapV[i].rvalue;
  262. return kOkXsRC;
  263. }
  264. // the rvalue label was not found
  265. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unknown rvalue type='%s'.",str);
  266. }
  267. cmXsRC_t _cmXScoreParseColor( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* note )
  268. {
  269. cmXsRC_t rc = kOkXsRC;
  270. const cmXmlAttr_t* a;
  271. typedef struct map_str
  272. {
  273. unsigned value;
  274. const cmChar_t* label;
  275. } map_t;
  276. map_t mapV[] =
  277. {
  278. { kEvenXsFl, "#0000FF" }, // blue (even)
  279. { kTempoXsFl, "#00FF00" }, // green (tempo)
  280. { kDynXsFl, "#FF0000" }, // red (dynamics)
  281. { kTempoXsFl | kEvenXsFl, "#00FFFF" }, // green + blue (turquoise)
  282. { kDynXsFl | kEvenXsFl, "#FF00FF" }, // red + blue
  283. { kDynXsFl | kEvenXsFl, "#FF0CF7" }, // magenta (even+dyn)
  284. { kDynXsFl | kTempoXsFl, "#FF7F00" }, // red + green (brown)
  285. { kTempoXsFl | kEvenXsFl | kDynXsFl, "#996633" }, // (purple)
  286. { kDynXsFl, "#FF6A03" }, // 176 orange (dynamics)
  287. { kEvenXsFl, "#2F00E8" }, // 1001 blue (even)
  288. { kTempoXsFl, "#01CD1F" }, // 1196 green (tempo)
  289. { kEvenXsFl, "#3600E8" }, // 1627 blue (even)
  290. { kDynXsFl | kTempoXsFl, "#9E8F15" }, // 8827 brown (dyn + tempo)
  291. { kEvenXsFl, "#2E00E6" }, // 5393 blue (even)
  292. { kEvenXsFl, "#2C00DD" }, // 5895 blue (even)
  293. { kDynXsFl, "#FF5B03" }, // 6498 orange (dyn)
  294. { kDynXsFl, "#FF6104" }, // 6896 orange
  295. { kEvenXsFl, "#2A00E6" }, // 7781 blue
  296. { kEvenXsFl, "#2300DD" }, // 8300 blue (even)
  297. { kTempoXsFl, "#03CD22" }, // 10820 green (tempo)
  298. { kEvenXsFl, "#3400DB" }, // 11627 blue (dyn)
  299. { -1, "" }
  300. };
  301. /*
  302. orange #FF6A03
  303. magenta #FF0CF7
  304. blue #2F00E8
  305. green #01CD1F
  306. gold #9E8F15
  307. green #03CD22
  308. */
  309. if((a = cmXmlFindAttrib(nnp, "color" )) != NULL )
  310. {
  311. unsigned i;
  312. for(i=0; mapV[i].value != -1; ++i)
  313. if( cmTextCmp(a->value,mapV[i].label) == 0 )
  314. {
  315. note->flags += mapV[i].value;
  316. break;
  317. }
  318. if( mapV[i].value == -1 )
  319. cmErrMsg(&p->err,kSyntaxErrorXsRC,"The note color '%s' was not found on line %i.",a->value,nnp->line);
  320. }
  321. return rc;
  322. }
  323. cmXsRC_t _cmXScoreParseNote(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* nnp, unsigned* tick0Ref, unsigned* tickRef )
  324. {
  325. cmXsRC_t rc = kOkXsRC;
  326. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  327. unsigned voiceId;
  328. note->meas = meas;
  329. note->xmlNode = nnp;
  330. // get the voice id for this node
  331. if( cmXmlNodeUInt(nnp,&voiceId,"voice",NULL) != kOkXmlRC )
  332. return _cmXScoreMissingNode(p,nnp,"voice");
  333. // if this note has a pitch
  334. if( cmXmlNodeHasChild(nnp,"pitch",NULL) )
  335. if((rc = _cmXScoreParsePitch(p,nnp,note)) != kOkXsRC )
  336. return rc;
  337. cmXmlNodeUInt(nnp,&note->duration,"duration",NULL); // get the note duration
  338. cmXmlNodeUInt(nnp,&note->staff,"staff",NULL); // get th staff number
  339. // is 'rest'
  340. if( cmXmlNodeHasChild(nnp,"rest",NULL) )
  341. note->flags |= kRestXsFl;
  342. // is 'grace'
  343. if( cmXmlNodeHasChild(nnp,"grace",NULL) )
  344. note->flags |= kGraceXsFl;
  345. // is 'dot'
  346. if( cmXmlNodeHasChild(nnp,"dot",NULL) )
  347. note->flags |= kDotXsFl;
  348. // is 'chord'
  349. if( cmXmlNodeHasChild(nnp,"chord",NULL) )
  350. note->flags |= kChordXsFl;
  351. // is this is first note in a tied pair
  352. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","start",NULL) )
  353. note->flags |= kTieBegXsFl;
  354. // is this is second note in a tied pair
  355. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","stop",NULL) )
  356. note->flags |= kTieEndXsFl;
  357. // has 'heel' mark
  358. if( cmXmlNodeHasChild(nnp,"notations","technical","heel",NULL) )
  359. note->flags |= kHeelXsFl;
  360. // set color coded flags
  361. if((rc = _cmXScoreParseColor(p, nnp, note )) != kOkXsRC )
  362. return rc;
  363. // get the note's rythmic value
  364. if((rc = _cmXScoreParseNoteRValue(p,nnp,"type",&note->rvalue)) != kOkXsRC )
  365. return rc;
  366. // if this is a chord note
  367. if( cmIsFlag(note->flags,kChordXsFl) )
  368. {
  369. note->tick = *tick0Ref; // then use the onset time from the previous note and do not advance time
  370. }
  371. else
  372. {
  373. *tick0Ref = *tickRef;
  374. note->tick = *tickRef;
  375. *tickRef += note->duration;
  376. }
  377. return _cmXScorePushNote(p, meas, voiceId, note );
  378. }
  379. 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 )
  380. {
  381. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  382. unsigned voiceId = 0; // non-note's are always assigned to voiceId=0;
  383. note->tick = tick;
  384. note->flags = flags;
  385. note->rvalue = rvalue;
  386. note->tvalue = tvalue;
  387. note->duration = duration;
  388. note->meas = meas;
  389. note->xmlNode = noteXmlNode;
  390. return _cmXScorePushNote(p, meas, voiceId, note );
  391. }
  392. cmXsSpan_t* _cmXScoreFindOpenOctaveShift( cmXScore_t* p, unsigned staff, unsigned number )
  393. {
  394. cmXsSpan_t* s = p->spanL;
  395. for(; s!=NULL; s=s->link)
  396. if( s->tick1 == -1 && s->staff == staff && s->number == number )
  397. return s;
  398. return NULL;
  399. }
  400. cmXsRC_t _cmXScorePushOctaveShift(cmXScore_t* p, cmXsMeas_t* meas, unsigned staff, unsigned span_number, const cmChar_t* type_str, unsigned tick)
  401. {
  402. assert( meas != NULL);
  403. cmXsSpan_t* s;
  404. if( cmTextCmp(type_str,"stop") == 0 )
  405. {
  406. if((s = _cmXScoreFindOpenOctaveShift(p,staff,span_number)) == NULL )
  407. return cmErrWarnMsg(&p->err,kUnterminatedOctaveShiftXsrRC,"An illegal octave shift was encounted in meas %i.\n",meas->number);
  408. s->tick1 = tick;
  409. }
  410. else
  411. {
  412. s = cmLhAllocZ(p->lhH,cmXsSpan_t,1);
  413. s->staff = staff;
  414. s->meas = meas;
  415. s->number = span_number;
  416. s->tick0 = tick;
  417. s->tick1 = -1;
  418. s->pitch_offset = cmTextCmp(type_str,"up")==0 ? -12 : 12;
  419. s->link = p->spanL;
  420. p->spanL = s;
  421. }
  422. return kOkXsRC;
  423. }
  424. cmXsRC_t _cmXScoreParseDirection(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* dnp, unsigned tick)
  425. {
  426. cmXsRC_t rc = kOkXsRC;
  427. const cmXmlNode_t* np = NULL;
  428. const cmXmlAttr_t* a = NULL;
  429. unsigned flags = 0;
  430. int offset = 0;
  431. double rvalue = 0;
  432. const cmChar_t* tvalue = NULL;
  433. unsigned duration = 0;
  434. bool pushFl = true;
  435. unsigned staff = 0;
  436. cmXmlNodeInt( dnp, &offset, "offset", NULL );
  437. cmXmlNodeUInt(dnp, &staff, "staff", NULL );
  438. // if this is a metronome direction
  439. if((np = cmXmlSearch( dnp, "metronome", NULL, 0)) != NULL )
  440. {
  441. if( cmXmlNodeUInt(np,&duration,"per-minute",NULL) != kOkXmlRC )
  442. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'per-minute' metronome value is missing on line %i.",np->line);
  443. if((rc = _cmXScoreParseNoteRValue(p,np,"beat-unit",&rvalue)) != kOkXsRC )
  444. return rc;
  445. flags = kMetronomeXsFl;
  446. }
  447. else
  448. // if this is a pedal direction
  449. if((np = cmXmlSearch( dnp, "pedal",NULL,0)) != NULL )
  450. {
  451. if((a = cmXmlFindAttrib(np,"type")) == NULL )
  452. return _cmXScoreMissingAttribute(p, np, "type" );
  453. if( cmTextCmp(a->value,"start") == 0 )
  454. flags = kPedalDnXsFl;
  455. else
  456. if( cmTextCmp(a->value,"change") == 0 )
  457. flags = kPedalUpDnXsFl;
  458. else
  459. if( cmTextCmp(a->value,"stop") == 0 )
  460. flags = kPedalUpXsFl;
  461. else
  462. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unrecognized pedal type:'%s'.",cmStringNullGuard(a->value));
  463. }
  464. else
  465. // if this is a 'words' direction
  466. if((np = cmXmlSearch( dnp, "words", NULL, 0)) != NULL )
  467. {
  468. if((a = cmXmlFindAttrib(np,"enclosure")) != NULL && cmTextCmp(a->value,"rectangle")==0 )
  469. {
  470. if( cmTextIsEmpty( tvalue = np->dataStr ) )
  471. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Section number is blank or missing on line %i.",np->line);
  472. flags = kSectionXsFl;
  473. }
  474. }
  475. else
  476. // if this is an 'octave-shift' direction
  477. if((np = cmXmlSearch( dnp, "octave-shift", NULL, 0)) != NULL )
  478. {
  479. unsigned span_number = -1;
  480. if( cmXmlAttrUInt(np,"number",&span_number) != kOkXmlRC )
  481. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'number' attribute.");
  482. if((a = cmXmlFindAttrib(np,"type")) == NULL)
  483. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'type' attribute.");
  484. rc = _cmXScorePushOctaveShift(p,meas,staff,span_number,a->value,tick+offset);
  485. pushFl = false;
  486. }
  487. else
  488. {
  489. pushFl = false;
  490. }
  491. if( pushFl )
  492. rc = _cmXScorePushNonNote(p,meas,dnp,tick+offset,duration,rvalue,tvalue,flags);
  493. return rc;
  494. }
  495. cmXsRC_t _cmXScoreParseMeasure(cmXScore_t* p, cmXsPart_t* pp, const cmXmlNode_t* mnp)
  496. {
  497. cmXsRC_t rc = kOkXsRC;
  498. const cmXmlNode_t* np = NULL;
  499. unsigned tick = 0;
  500. unsigned tick0= 0;
  501. cmXsMeas_t* m = NULL;
  502. // allocate the 'measure' record
  503. cmXsMeas_t* meas = cmLhAllocZ(p->lhH,cmXsMeas_t,1);
  504. // get measure number
  505. if( cmXmlAttrUInt(mnp,"number", &meas->number) != kOkXmlRC )
  506. return _cmXScoreMissingAttribute(p,mnp,"number");
  507. if( pp->measL == NULL )
  508. pp->measL = meas;
  509. else
  510. {
  511. m = pp->measL;
  512. while( m->link != NULL )
  513. m = m->link;
  514. m->link = meas;
  515. meas->divisions = m->divisions;
  516. meas->beats = m->beats;
  517. meas->beat_type = m->beat_type;
  518. }
  519. // get measure attributes node
  520. if((np = cmXmlSearch(mnp,"attributes",NULL,0)) != NULL)
  521. {
  522. cmXmlNodeUInt(np,&meas->divisions,"divisions",NULL);
  523. cmXmlNodeUInt(np,&meas->beats, "time","beats",NULL);
  524. cmXmlNodeUInt(np,&meas->beat_type,"time","beat-type",NULL);
  525. }
  526. // store the bar line
  527. if((rc = _cmXScorePushNonNote(p,meas,mnp,tick,0,0,NULL,kBarXsFl)) != kOkXsRC )
  528. return rc;
  529. np = mnp->children;
  530. // for each child of the 'meas' XML node
  531. for(; rc==kOkXsRC && np!=NULL; np=np->sibling)
  532. {
  533. // if this is a 'note' node
  534. if( cmTextCmp(np->label,"note") == 0 )
  535. {
  536. rc = _cmXScoreParseNote(p,meas,np,&tick0,&tick);
  537. }
  538. else
  539. // if this is a 'backup' node
  540. if( cmTextCmp(np->label,"backup") == 0 )
  541. {
  542. unsigned backup;
  543. cmXmlNodeUInt(np,&backup,"duration",NULL);
  544. if( backup > tick )
  545. tick = 0;
  546. else
  547. tick -= backup;
  548. tick0 = tick;
  549. }
  550. else
  551. // if this is a 'direction' node
  552. if( cmTextCmp(np->label,"direction") == 0 )
  553. {
  554. rc = _cmXScoreParseDirection(p,meas,np,tick);
  555. }
  556. }
  557. return rc;
  558. }
  559. cmXsRC_t _cmXScoreParsePart( cmXScore_t* p, cmXsPart_t* pp )
  560. {
  561. cmXsRC_t rc = kOkXsRC;
  562. const cmXmlNode_t* xnp;
  563. cmXmlAttr_t partAttr = { "id", pp->idStr };
  564. // find the 'part'
  565. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part", &partAttr, 1)) == NULL )
  566. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The part '%s' was not found.",pp->idStr);
  567. // for each child of this part - find each measure
  568. const cmXmlNode_t* cnp = xnp->children;
  569. for(; cnp!=NULL; cnp=cnp->sibling)
  570. if( cmTextCmp(cnp->label,"measure") == 0 )
  571. if((rc = _cmXScoreParseMeasure(p,pp,cnp)) != kOkXsRC )
  572. return rc;
  573. return rc;
  574. }
  575. cmXsNote_t* _cmXScoreInsertSortedNote( cmXsNote_t* s0, cmXsNote_t* np )
  576. {
  577. if( s0 == NULL )
  578. return np;
  579. if( np->tick < s0->tick )
  580. {
  581. np->slink = s0;
  582. return np;
  583. }
  584. cmXsNote_t* s1 = s0;
  585. cmXsNote_t* s2 = s0->slink;
  586. while( s2 != NULL )
  587. {
  588. if( s2->tick > np->tick )
  589. {
  590. s1->slink = np;
  591. np->slink = s2;
  592. return s0;
  593. }
  594. s1 = s2;
  595. s2 = s2->slink;
  596. }
  597. s1->slink = np;
  598. return s0;
  599. }
  600. void _cmXScoreSort( cmXScore_t* p )
  601. {
  602. cmXsPart_t* pp = p->partL;
  603. for(; pp!=NULL; pp=pp->link)
  604. {
  605. cmXsMeas_t* mp = pp->measL;
  606. for(; mp!=NULL; mp=mp->link)
  607. {
  608. // for each voice in this measure
  609. cmXsVoice_t* vp = mp->voiceL;
  610. for(; vp!=NULL; vp=vp->link)
  611. {
  612. cmXsNote_t* np = vp->noteL;
  613. for(; np!=NULL; np=np->mlink)
  614. mp->noteL = _cmXScoreInsertSortedNote(mp->noteL,np);
  615. }
  616. }
  617. }
  618. }
  619. bool _cmXScoreFindTiedNote( cmXScore_t* p, cmXsMeas_t* mp, cmXsNote_t* np )
  620. {
  621. cmXsNote_t* nnp = np->slink;
  622. unsigned measNumb = mp->number;
  623. unsigned measNumb0= measNumb;
  624. cmChar_t acc = np->alter==-1?'b' : (np->alter==1?'#':' ');
  625. for(; mp!=NULL; mp=mp->link)
  626. {
  627. if( nnp == NULL )
  628. nnp = mp->noteL;
  629. for(; nnp!=NULL; nnp=nnp->slink)
  630. {
  631. if( /*nnp->voice->id == np->voice->id &&*/ nnp->step == np->step && nnp->octave == np->octave )
  632. {
  633. nnp->flags |= kTieProcXsFl;
  634. nnp->flags = cmClrFlag(nnp->flags,kOnsetXsFl);
  635. if( cmIsNotFlag(nnp->flags,kTieBegXsFl) )
  636. {
  637. return true;
  638. }
  639. measNumb0 = mp->number;
  640. }
  641. }
  642. // if a measure was completed and no end note was found ... then the tie is unterminated
  643. // (a tie must be continued in every measure which it passes through)
  644. if( measNumb0 < mp->number )
  645. break;
  646. }
  647. cmErrWarnMsg(&p->err,kUnterminatedTieXsRC,"The tied %c%c%i in measure %i was not terminated.",np->step,acc,np->octave,measNumb);
  648. return false;
  649. }
  650. void _cmXScoreProcessTies( cmXScore_t* p )
  651. {
  652. unsigned n = 0;
  653. unsigned m = 0;
  654. cmXsPart_t* pp = p->partL;
  655. for(; pp!=NULL; pp=pp->link)
  656. {
  657. cmXsMeas_t* mp = pp->measL;
  658. for(; mp!=NULL; mp=mp->link)
  659. {
  660. cmXsNote_t* np = mp->noteL;
  661. for(; np!=NULL; np=np->slink)
  662. if( cmIsFlag(np->flags,kTieBegXsFl) && cmIsNotFlag(np->flags,kTieProcXsFl))
  663. {
  664. if( _cmXScoreFindTiedNote(p,mp,np) )
  665. m += 1;
  666. n += 1;
  667. }
  668. }
  669. }
  670. printf("Found:%i Not Found:%i\n",m,n-m);
  671. }
  672. cmXsRC_t _cmXScoreResolveOctaveShift( cmXScore_t* p )
  673. {
  674. const cmXsSpan_t* s;
  675. for(s=p->spanL; s!=NULL; s=s->link)
  676. {
  677. if( s->tick1 == -1)
  678. {
  679. cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"An unterminated octave shift span was encountered in measure %i staff=%i.",s->meas->number,s->staff);
  680. }
  681. else
  682. {
  683. cmXsMeas_t* m = p->partL->measL;
  684. for(; m!=NULL; m=m->link)
  685. if( m->number == s->meas->number )
  686. break;
  687. assert( m != NULL );
  688. cmXsNote_t* note = m->noteL;
  689. for(; note!=NULL; note=note->slink)
  690. if( note->staff==s->staff && s->tick0 <= note->tick && note->tick < s->tick1 )
  691. note->pitch += s->pitch_offset;
  692. }
  693. }
  694. return kOkXsRC;
  695. }
  696. cmXsMeas_t* _cmXScoreNextNonEmptyMeas( cmXsPart_t* pp, cmXsMeas_t* meas )
  697. {
  698. if( meas == NULL )
  699. {
  700. if( pp==NULL || pp->measL==NULL )
  701. return NULL;
  702. meas = pp->measL;
  703. }
  704. else
  705. {
  706. meas = meas->link;
  707. }
  708. while( meas != NULL && meas->noteL == NULL )
  709. meas=meas->link;
  710. return meas;
  711. }
  712. cmXsNote_t* _cmXScoreNextNote( cmXsPart_t* pp, cmXsNote_t* note )
  713. {
  714. // meas should always be valid (unless this is the first note in the score)
  715. cmXsMeas_t* meas = note==NULL ? NULL : note->meas;
  716. do
  717. {
  718. if( note == NULL || note->slink==NULL )
  719. {
  720. if((meas = _cmXScoreNextNonEmptyMeas(pp,meas)) == NULL)
  721. return NULL;
  722. assert( meas->noteL != NULL );
  723. note = meas->noteL;
  724. }
  725. else
  726. {
  727. note = note->slink;
  728. }
  729. assert( note != NULL );
  730. meas = note->meas;
  731. // note is now set to a non-NULL candidate note - advance to a sounding note
  732. while( note!=NULL && cmIsNotFlag(note->flags,kOnsetXsFl) )
  733. note = note->slink;
  734. // if no note was found in this measure
  735. }while( note == NULL );
  736. return note;
  737. }
  738. cmXsRC_t _cmXScoreProcessMidi(cmXScore_t* p, cmCtx_t* ctx, const cmChar_t* midiFn)
  739. {
  740. cmXsRC_t rc = kOkXsRC;
  741. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  742. const cmMidiTrackMsg_t** m = NULL;
  743. unsigned mN = 0;
  744. unsigned i = 0;
  745. unsigned j = 0;
  746. cmXsNote_t* note = NULL;
  747. if( cmMidiFileOpen(ctx, &mfH, midiFn ) != kOkMfRC )
  748. return cmErrMsg(&p->err,kMidiFailXsRC,"The MIDI file object could not be opened from '%s'.",cmStringNullGuard(midiFn));
  749. if( (m = cmMidiFileMsgArray(mfH)) == NULL || (mN = cmMidiFileMsgCount(mfH)) == 0 )
  750. {
  751. rc = cmErrMsg(&p->err,kMidiFailXsRC,"The MIDI file object appears to be empty.");
  752. goto errLabel;
  753. }
  754. if((note = _cmXScoreNextNote(p->partL,NULL)) == NULL)
  755. {
  756. rc = cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"No MIDI processing to be done. The score appears to be empty.");
  757. goto errLabel;
  758. }
  759. printf(" i j score midi\n");
  760. printf("---- ---- --- ---- --- ----\n");
  761. for(j=0; note!=NULL; note=_cmXScoreNextNote(p->partL,note),++j)
  762. {
  763. unsigned midiPitch = 0;
  764. for(; i<mN; ++i)
  765. if( m[i]!=NULL && cmMidiIsChStatus(m[i]->status) && cmMidiIsNoteOn(m[i]->status) && m[i]->u.chMsgPtr->d1>0 )
  766. {
  767. midiPitch = m[i]->u.chMsgPtr->d0;
  768. ++i;
  769. break;
  770. }
  771. char buf[6];
  772. printf("%4i %4i %3i %4s %3i %4s\n",j,i,
  773. note->pitch,
  774. cmMidiToSciPitch(note->pitch,NULL,0),
  775. midiPitch,
  776. cmMidiToSciPitch(midiPitch,buf,5));
  777. }
  778. errLabel:
  779. cmMidiFileClose(&mfH);
  780. return rc;
  781. }
  782. cmXsRC_t cmXScoreInitialize( cmCtx_t* ctx, cmXsH_t* hp, const cmChar_t* xmlFn, const cmChar_t* midiFn )
  783. {
  784. cmXsRC_t rc = kOkXsRC;
  785. if((rc = cmXScoreFinalize(hp)) != kOkXsRC )
  786. return rc;
  787. cmXScore_t* p = cmMemAllocZ(cmXScore_t,1);
  788. cmErrSetup(&p->err,&ctx->rpt,"XScore");
  789. // create a local linked heap
  790. if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8196,ctx)) == false )
  791. return cmErrMsg(&p->err,kLHeapFailXsRC,"Lheap create failed.");
  792. // open the music xml file
  793. if( cmXmlAlloc(ctx, &p->xmlH, xmlFn) != kOkXmlRC )
  794. {
  795. rc = cmErrMsg(&p->err,kXmlFailXsRC,"Unable to open the MusicXML file '%s'.",cmStringNullGuard(xmlFn));
  796. goto errLabel;
  797. }
  798. //cmXmlPrint(p->xmlH,&ctx->rpt);
  799. // parse the part-list
  800. if((rc = _cmXScoreParsePartList( p )) != kOkXsRC )
  801. goto errLabel;
  802. // parse each score 'part'
  803. cmXsPart_t* pp = p->partL;
  804. for(; pp!=NULL; pp=pp->link)
  805. if((rc = _cmXScoreParsePart(p,pp)) != kOkXsRC )
  806. goto errLabel;
  807. // fill in the note->slink chain to link the notes in each measure in time order
  808. _cmXScoreSort(p);
  809. _cmXScoreProcessTies(p);
  810. //_cmXScoreResolveOctaveShift(p);
  811. if( midiFn != NULL )
  812. _cmXScoreProcessMidi(p,ctx,midiFn);
  813. // CSV output initialize failed.
  814. if( cmCsvInitialize(&p->csvH,ctx) != kOkCsvRC )
  815. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output object create failed.");
  816. errLabel:
  817. if( rc != kOkXsRC )
  818. _cmXScoreFinalize(p);
  819. else
  820. hp->h = p;
  821. return rc;
  822. }
  823. cmXsRC_t cmXScoreFinalize( cmXsH_t* hp )
  824. {
  825. cmXsRC_t rc = kOkXsRC;
  826. if( hp == NULL || cmXScoreIsValid(*hp)==false )
  827. return kOkXsRC;
  828. cmXScore_t* p = _cmXScoreHandleToPtr(*hp);
  829. if((rc = _cmXScoreFinalize(p)) != kOkXsRC )
  830. return rc;
  831. hp->h = NULL;
  832. return rc;
  833. }
  834. bool cmXScoreIsValid( cmXsH_t h )
  835. { return h.h != NULL; }
  836. void _cmXScoreReportNote( cmRpt_t* rpt, const cmXsNote_t* note )
  837. {
  838. const cmChar_t* B = cmIsFlag(note->flags,kBarXsFl) ? "|" : "-";
  839. const cmChar_t* R = cmIsFlag(note->flags,kRestXsFl) ? "R" : "-";
  840. const cmChar_t* G = cmIsFlag(note->flags,kGraceXsFl) ? "G" : "-";
  841. const cmChar_t* D = cmIsFlag(note->flags,kDotXsFl) ? "D" : "-";
  842. const cmChar_t* C = cmIsFlag(note->flags,kChordXsFl) ? "C" : "-";
  843. const cmChar_t* e = cmIsFlag(note->flags,kEvenXsFl) ? "e" : "-";
  844. const cmChar_t* d = cmIsFlag(note->flags,kDynXsFl) ? "d" : "-";
  845. const cmChar_t* t = cmIsFlag(note->flags,kTempoXsFl) ? "t" : "-";
  846. const cmChar_t* P = cmIsFlag(note->flags,kPedalDnXsFl) ? "V" : "-";
  847. const cmChar_t* S = cmIsFlag(note->flags,kSectionXsFl) ? "S" : "-";
  848. const cmChar_t* H = cmIsFlag(note->flags,kHeelXsFl) ? "H" : "-";
  849. const cmChar_t* T0 = cmIsFlag(note->flags,kTieBegXsFl) ? "T" : "-";
  850. const cmChar_t* T1 = cmIsFlag(note->flags,kTieEndXsFl) ? "_" : "-";
  851. P = cmIsFlag(note->flags,kPedalUpXsFl) ? "^" : P;
  852. P = cmIsFlag(note->flags,kPedalUpDnXsFl) ? "X" : P;
  853. //const cmChar_t* N = note->pitch==0 ? " " : cmMidiToSciPitch( note->pitch, NULL, 0 );
  854. cmChar_t N[] = {'\0','\0','\0','\0'};
  855. cmChar_t acc = note->alter==-1?'b':(note->alter==1?'#':' ');
  856. snprintf(N,4,"%c%c%1i",note->step,acc,note->octave);
  857. cmRptPrintf(rpt," %3i %5i %5i %4.1f %3s %s%s%s%s%s%s%s%s%s%s%s%s%s",note->voice->id,note->tick,note->duration,note->rvalue,N,B,R,G,D,C,e,d,t,P,S,H,T0,T1);
  858. if( cmIsFlag(note->flags,kSectionXsFl) )
  859. cmRptPrintf(rpt," %s",cmStringNullGuard(note->tvalue));
  860. if( cmIsFlag(note->flags,kMetronomeXsFl) )
  861. cmRptPrintf(rpt," %i bpm",note->duration);
  862. }
  863. /*
  864. kMidiFileIdColScIdx= 0,
  865. kTypeLabelColScIdx = 3,
  866. kDSecsColScIdx = 4,
  867. kSecsColScIdx = 5,
  868. kD0ColScIdx = 9,
  869. kD1ColScIdx = 10,
  870. kPitchColScIdx = 11,
  871. kBarColScIdx = 13,
  872. kSkipColScIdx = 14,
  873. kEvenColScIdx = 15,
  874. kGraceColScIdx = 16,
  875. kTempoColScIdx = 17,
  876. kFracColScIdx = 18,
  877. kDynColScIdx = 19,
  878. kSectionColScIdx = 20,
  879. kRecdPlayColScIdx = 21,
  880. kRemarkColScIdx = 22
  881. */
  882. cmXsRC_t _cmXScoreWriteCsvHdr( cmXScore_t* p )
  883. {
  884. const cmChar_t* s[] =
  885. {
  886. "id","trk","evt","opcode","dticks","micros","status",
  887. "meta","ch","d0","d1","arg0","arg1","bar","skip",
  888. "even","grace","tempo","t frac","dyn","section","play_recd","remark",NULL
  889. };
  890. cmCsvCell_t* lcp = NULL;
  891. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymText(p->csvH,s[0]), 0, 0 ) != kOkCsvRC )
  892. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  893. unsigned i;
  894. for(i=1; s[i]!=NULL; ++i)
  895. {
  896. if( cmCsvInsertTextColAfter(p->csvH, lcp, &lcp, s[i], 0 ) != kOkCsvRC )
  897. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV error inserting CSV title %i.\n",i);
  898. }
  899. return kOkXsRC;
  900. }
  901. cmXsRC_t _cmXScoreWriteCsvBlankCols( cmXScore_t* p, unsigned cnt, cmCsvCell_t** leftCellPtrPtr )
  902. {
  903. unsigned i;
  904. for(i=0; i<cnt; ++i)
  905. if( cmCsvInsertTextColAfter(p->csvH,*leftCellPtrPtr,leftCellPtrPtr,0,0) != kOkCsvRC )
  906. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV output failed on blank column.");
  907. return kOkCsvRC;
  908. }
  909. cmXsRC_t _cmXScoreWriteCsvRow(
  910. cmXScore_t* p,
  911. unsigned rowIdx,
  912. unsigned bar,
  913. const cmChar_t* sectionStr,
  914. const cmChar_t* opCodeStr,
  915. double dsecs,
  916. double secs,
  917. unsigned d0,
  918. unsigned d1,
  919. unsigned pitch,
  920. double frac,
  921. unsigned flags )
  922. {
  923. cmXsRC_t rc = kOkXsRC;
  924. cmCsvCell_t* lcp = NULL;
  925. // append an empty row to the CSV object
  926. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymUInt(p->csvH, rowIdx ), 0, 0 ) != kOkCsvRC )
  927. {
  928. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  929. goto errLabel;
  930. }
  931. /*
  932. // col 0 : blanks
  933. if( cmCsvInsertUIntColAfter(p->csvH, lcp, &lcp, rowIdx, 0 ) != kOkCsvRC )
  934. {
  935. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output row index failed.");
  936. goto errLabel;
  937. }
  938. */
  939. // cols 1,2
  940. if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
  941. goto errLabel;
  942. // col 3 : output the opcode
  943. if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,opCodeStr,0) != kOkCsvRC )
  944. {
  945. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on opcode label.");
  946. goto errLabel;
  947. }
  948. // col 4 : dsecs
  949. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,dsecs,0) != kOkCsvRC )
  950. {
  951. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dsecs'.");
  952. goto errLabel;
  953. }
  954. // col 5 : secs
  955. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,secs,0) != kOkCsvRC )
  956. {
  957. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'secs'.");
  958. goto errLabel;
  959. }
  960. // cols 6,7,8 blanks
  961. if((rc = _cmXScoreWriteCsvBlankCols(p,3,&lcp)) != kOkXsRC )
  962. goto errLabel;
  963. // col 9 : d0
  964. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d0,0) != kOkCsvRC )
  965. {
  966. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  967. goto errLabel;
  968. }
  969. // col 10 : d1
  970. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d1,0) != kOkCsvRC )
  971. {
  972. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd1'.");
  973. goto errLabel;
  974. }
  975. // col 11 : pitch
  976. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,pitch,0) != kOkCsvRC )
  977. {
  978. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  979. goto errLabel;
  980. }
  981. // col 12 : blanks
  982. if((rc = _cmXScoreWriteCsvBlankCols(p,1 + (cmIsFlag(flags,kBarXsFl) ? 0 : 1), &lcp)) != kOkXsRC )
  983. goto errLabel;
  984. // col 13 : bar number
  985. if( cmIsFlag(flags,kBarXsFl) )
  986. {
  987. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,bar,0) != kOkCsvRC )
  988. {
  989. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  990. goto errLabel;
  991. }
  992. }
  993. // col 14 : skip (blank for now)
  994. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  995. goto errLabel;
  996. // col 15: even
  997. if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kEvenXsFl) ? "e" : "",0) != kOkCsvRC )
  998. {
  999. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1000. goto errLabel;
  1001. }
  1002. // col 16: grace
  1003. if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kGraceXsFl) ? "g" : "",0) != kOkCsvRC )
  1004. {
  1005. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1006. goto errLabel;
  1007. }
  1008. // col 17: tempo
  1009. if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kTempoXsFl) ? "t" : "",0) != kOkCsvRC )
  1010. {
  1011. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1012. goto errLabel;
  1013. }
  1014. // col 18: frac
  1015. if( frac == 0 )
  1016. {
  1017. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1018. goto errLabel;
  1019. }
  1020. else
  1021. {
  1022. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,1.0/frac,0) != kOkCsvRC )
  1023. {
  1024. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 't frac'.");
  1025. goto errLabel;
  1026. }
  1027. }
  1028. // col 19: dynamic marking (blank for now)
  1029. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1030. goto errLabel;
  1031. // col 20: section
  1032. if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kSectionXsFl) ? sectionStr : "",0) != kOkCsvRC )
  1033. {
  1034. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1035. goto errLabel;
  1036. }
  1037. // col 21, 22 : recd-play, remark (blank for now)
  1038. if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
  1039. goto errLabel;
  1040. errLabel:
  1041. return rc;
  1042. }
  1043. cmXsRC_t cmXScoreWriteCsv( cmXsH_t h, const cmChar_t* csvFn )
  1044. {
  1045. cmXsRC_t rc = kOkXsRC;
  1046. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1047. unsigned rowIdx = 1;
  1048. double tpqn = 0; // ticks per quarter note
  1049. double tps = 0; // ticks per second
  1050. double sec = 0; // current time in seconds
  1051. const cmChar_t* sectionIdStr = NULL;
  1052. if( !cmCsvIsValid(p->csvH) )
  1053. return cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output object is not initialized.");
  1054. if((rc = _cmXScoreWriteCsvHdr( p )) != kOkXsRC )
  1055. goto errLabel;
  1056. cmXsPart_t* pp = p->partL;
  1057. for(; pp!=NULL; pp=pp->link)
  1058. {
  1059. cmXsMeas_t* mp = pp->measL;
  1060. for(; mp!=NULL; mp=mp->link)
  1061. {
  1062. if( mp->divisions != 0 )
  1063. tpqn = mp->divisions;
  1064. cmXsNote_t* np = mp->noteL;
  1065. double sec0 = sec;
  1066. for(; np!=NULL; np=np->slink)
  1067. {
  1068. //
  1069. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  1070. {
  1071. double bpm = np->duration;
  1072. double bps = bpm / 60.0;
  1073. tps = bps * tpqn;
  1074. }
  1075. double meas_sec = tps == 0 ? 0 : np->tick / tps;
  1076. double sec1 = sec + meas_sec;
  1077. double dsecs = sec1 - sec0;
  1078. sec0 = sec1;
  1079. // if this is a section event
  1080. if( cmIsFlag(np->flags,kSectionXsFl) )
  1081. sectionIdStr = np->tvalue;
  1082. // if this is a bar event
  1083. if( cmIsFlag(np->flags,kBarXsFl) )
  1084. {
  1085. _cmXScoreWriteCsvRow(p,rowIdx,mp->number,NULL,"bar",dsecs,sec1,0,0,0,0,np->flags);
  1086. }
  1087. else
  1088. // if this is a pedal event
  1089. if( cmIsFlag(np->flags,kPedalDnXsFl|kPedalUpXsFl|kPedalUpDnXsFl) )
  1090. {
  1091. unsigned d0 = 64; // pedal MIDI ctl id
  1092. unsigned d1 = cmIsFlag(np->flags,kPedalDnXsFl) ? 64 : 0; // pedal-dn: d1>=64 pedal-up:<64
  1093. _cmXScoreWriteCsvRow(p,rowIdx,mp->number,NULL,"ctl",dsecs,sec1,d0,d1,0,0,np->flags);
  1094. }
  1095. else
  1096. // if this is a sounding note event
  1097. if( cmIsNotFlag(np->flags,kRestXsFl) )
  1098. {
  1099. double frac = np->rvalue + (cmIsFlag(np->flags,kDotXsFl) ? (np->rvalue/2) : 0);
  1100. //
  1101. _cmXScoreWriteCsvRow(p,rowIdx,mp->number,sectionIdStr,"non",dsecs,sec1,0,0,np->pitch,frac,np->flags);
  1102. sectionIdStr = NULL;
  1103. }
  1104. rowIdx += 1;
  1105. }
  1106. sec = sec0;
  1107. }
  1108. }
  1109. if( cmCsvWrite( p->csvH, csvFn ) != kOkCsvRC )
  1110. rc = cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output write failed on file '%s'.",csvFn);
  1111. errLabel:
  1112. return rc;
  1113. }
  1114. void cmXScoreReport( cmXsH_t h, cmRpt_t* rpt, bool sortFl )
  1115. {
  1116. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1117. cmXsPart_t* pp = p->partL;
  1118. for(; pp!=NULL; pp=pp->link)
  1119. {
  1120. cmRptPrintf(rpt,"Part:%s\n",pp->idStr);
  1121. const cmXsMeas_t* meas = pp->measL;
  1122. for(; meas!=NULL; meas=meas->link)
  1123. {
  1124. 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);
  1125. if( sortFl )
  1126. {
  1127. const cmXsNote_t* note = meas->noteL;
  1128. for(; note!=NULL; note=note->slink)
  1129. {
  1130. _cmXScoreReportNote(rpt,note);
  1131. if( note->slink!=NULL || note->voice->id==0)
  1132. cmRptPrintf(rpt,"\n");
  1133. else
  1134. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  1135. }
  1136. }
  1137. else
  1138. {
  1139. const cmXsVoice_t* v = meas->voiceL;
  1140. for(; v!=NULL; v=v->link)
  1141. {
  1142. const cmXsNote_t* note = v->noteL;
  1143. cmRptPrintf(rpt," voice:%i\n",v->id);
  1144. for(; note!=NULL; note=note->mlink)
  1145. {
  1146. _cmXScoreReportNote(rpt,note);
  1147. if( note->mlink!=NULL || note->voice->id==0)
  1148. cmRptPrintf(rpt,"\n");
  1149. else
  1150. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  1151. }
  1152. }
  1153. }
  1154. }
  1155. }
  1156. }
  1157. cmXsRC_t cmXScoreWriteMidi( cmXsH_t h, const cmChar_t* fn )
  1158. {
  1159. assert(0); // function not implemented
  1160. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1161. cmXsPart_t* pp = p->partL;
  1162. for(; pp!=NULL; pp=pp->link)
  1163. {
  1164. const cmXsMeas_t* meas = pp->measL;
  1165. for(; meas!=NULL; meas=meas->link)
  1166. {
  1167. const cmXsNote_t* note = meas->noteL;
  1168. for(; note!=NULL; note=note->slink)
  1169. {
  1170. }
  1171. }
  1172. }
  1173. }
  1174. cmXsRC_t cmXScoreTest( cmCtx_t* ctx, const cmChar_t* xmlFn, const cmChar_t* midiFn )
  1175. {
  1176. cmXsRC_t rc;
  1177. cmXsH_t h = cmXsNullHandle;
  1178. if((rc = cmXScoreInitialize( ctx, &h, xmlFn, midiFn)) != kOkXsRC )
  1179. return cmErrMsg(&ctx->err,rc,"XScore alloc failed.");
  1180. cmXScoreWriteCsv(h,"/Users/kevin/temp/a0.csv");
  1181. cmXScoreReport(h,&ctx->rpt,false);
  1182. return cmXScoreFinalize(&h);
  1183. }