libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmXScore.c 44KB

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