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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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. // On input tick0Ref is set to the tick of the previous event.
  338. // On input tickRef is set to the tick of this event.
  339. // On output tick0Ref is set to the tick of this event.
  340. // On output tickRef is set to the tick of the next event.
  341. cmXsRC_t _cmXScoreParseNote(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* nnp, unsigned* tick0Ref, unsigned* tickRef )
  342. {
  343. cmXsRC_t rc = kOkXsRC;
  344. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  345. unsigned voiceId;
  346. note->meas = meas;
  347. note->xmlNode = nnp;
  348. // get the voice id for this node
  349. if( cmXmlNodeUInt(nnp,&voiceId,"voice",NULL) != kOkXmlRC )
  350. return _cmXScoreMissingNode(p,nnp,"voice");
  351. // if this note has a pitch
  352. if( cmXmlNodeHasChild(nnp,"pitch",NULL) )
  353. if((rc = _cmXScoreParsePitch(p,nnp,note)) != kOkXsRC )
  354. return rc;
  355. cmXmlNodeUInt(nnp,&note->duration,"duration",NULL); // get the note duration
  356. cmXmlNodeUInt(nnp,&note->staff,"staff",NULL); // get th staff number
  357. // is 'rest'
  358. if( cmXmlNodeHasChild(nnp,"rest",NULL) )
  359. note->flags |= kRestXsFl;
  360. // is 'grace'
  361. if( cmXmlNodeHasChild(nnp,"grace",NULL) )
  362. note->flags |= kGraceXsFl;
  363. // is 'dot'
  364. if( cmXmlNodeHasChild(nnp,"dot",NULL) )
  365. note->flags |= kDotXsFl;
  366. // is 'chord'
  367. if( cmXmlNodeHasChild(nnp,"chord",NULL) )
  368. note->flags |= kChordXsFl;
  369. // is this is first note in a tied pair
  370. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","start",NULL) )
  371. note->flags |= kTieBegXsFl;
  372. // is this is second note in a tied pair
  373. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","stop",NULL) )
  374. note->flags |= kTieEndXsFl;
  375. // has 'heel' mark
  376. if( cmXmlNodeHasChild(nnp,"notations","technical","heel",NULL) )
  377. note->flags |= kHeelXsFl;
  378. // set color coded flags
  379. if((rc = _cmXScoreParseColor(p, nnp, note )) != kOkXsRC )
  380. return rc;
  381. // get the note's rythmic value
  382. if((rc = _cmXScoreParseNoteRValue(p,nnp,"type",&note->rvalue)) != kOkXsRC )
  383. return rc;
  384. // if this is a chord note
  385. if( cmIsFlag(note->flags,kChordXsFl) )
  386. {
  387. note->tick = *tick0Ref; // then use the onset time from the previous note and do not advance time
  388. }
  389. else
  390. {
  391. *tick0Ref = *tickRef;
  392. note->tick = *tickRef;
  393. *tickRef += note->duration;
  394. }
  395. return _cmXScorePushNote(p, meas, voiceId, note );
  396. }
  397. 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 )
  398. {
  399. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  400. unsigned voiceId = 0; // non-note's are always assigned to voiceId=0;
  401. note->tick = tick;
  402. note->flags = flags;
  403. note->rvalue = rvalue;
  404. note->tvalue = tvalue;
  405. note->duration = duration;
  406. note->meas = meas;
  407. note->xmlNode = noteXmlNode;
  408. return _cmXScorePushNote(p, meas, voiceId, note );
  409. }
  410. cmXsSpan_t* _cmXScoreFindOpenOctaveShift( cmXScore_t* p, unsigned staff, unsigned number )
  411. {
  412. cmXsSpan_t* s = p->spanL;
  413. for(; s!=NULL; s=s->link)
  414. if( s->tick1 == -1 && s->staff == staff && s->number == number )
  415. return s;
  416. return NULL;
  417. }
  418. cmXsRC_t _cmXScorePushOctaveShift(cmXScore_t* p, cmXsMeas_t* meas, unsigned staff, unsigned span_number, const cmChar_t* type_str, unsigned tick)
  419. {
  420. assert( meas != NULL);
  421. cmXsSpan_t* s;
  422. if( cmTextCmp(type_str,"stop") == 0 )
  423. {
  424. if((s = _cmXScoreFindOpenOctaveShift(p,staff,span_number)) == NULL )
  425. return cmErrWarnMsg(&p->err,kUnterminatedOctaveShiftXsrRC,"An illegal octave shift was encounted in meas %i.\n",meas->number);
  426. s->tick1 = tick;
  427. }
  428. else
  429. {
  430. s = cmLhAllocZ(p->lhH,cmXsSpan_t,1);
  431. s->staff = staff;
  432. s->meas = meas;
  433. s->number = span_number;
  434. s->tick0 = tick;
  435. s->tick1 = -1;
  436. s->pitch_offset = cmTextCmp(type_str,"up")==0 ? -12 : 12;
  437. s->link = p->spanL;
  438. p->spanL = s;
  439. }
  440. return kOkXsRC;
  441. }
  442. cmXsRC_t _cmXScoreParseDirection(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* dnp, unsigned tick)
  443. {
  444. cmXsRC_t rc = kOkXsRC;
  445. const cmXmlNode_t* np = NULL;
  446. const cmXmlAttr_t* a = NULL;
  447. unsigned flags = 0;
  448. int offset = 0;
  449. double rvalue = 0;
  450. const cmChar_t* tvalue = NULL;
  451. unsigned duration = 0;
  452. bool pushFl = true;
  453. unsigned staff = 0;
  454. cmXmlNodeInt( dnp, &offset, "offset", NULL );
  455. cmXmlNodeUInt(dnp, &staff, "staff", NULL );
  456. // if this is a metronome direction
  457. if((np = cmXmlSearch( dnp, "metronome", NULL, 0)) != NULL )
  458. {
  459. if( cmXmlNodeUInt(np,&duration,"per-minute",NULL) != kOkXmlRC )
  460. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'per-minute' metronome value is missing on line %i.",np->line);
  461. if((rc = _cmXScoreParseNoteRValue(p,np,"beat-unit",&rvalue)) != kOkXsRC )
  462. return rc;
  463. flags = kMetronomeXsFl;
  464. }
  465. else
  466. // if this is a pedal direction
  467. if((np = cmXmlSearch( dnp, "pedal",NULL,0)) != NULL )
  468. {
  469. if((a = cmXmlFindAttrib(np,"type")) == NULL )
  470. return _cmXScoreMissingAttribute(p, np, "type" );
  471. if( cmTextCmp(a->value,"start") == 0 )
  472. flags = kPedalDnXsFl;
  473. else
  474. if( cmTextCmp(a->value,"change") == 0 )
  475. flags = kPedalUpDnXsFl;
  476. else
  477. if( cmTextCmp(a->value,"stop") == 0 )
  478. flags = kPedalUpXsFl;
  479. else
  480. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unrecognized pedal type:'%s'.",cmStringNullGuard(a->value));
  481. }
  482. else
  483. // if this is a 'words' direction
  484. if((np = cmXmlSearch( dnp, "words", NULL, 0)) != NULL )
  485. {
  486. if((a = cmXmlFindAttrib(np,"enclosure")) != NULL && cmTextCmp(a->value,"rectangle")==0 )
  487. {
  488. if( cmTextIsEmpty( tvalue = np->dataStr ) )
  489. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Section number is blank or missing on line %i.",np->line);
  490. flags = kSectionXsFl;
  491. }
  492. }
  493. else
  494. // if this is an 'octave-shift' direction
  495. if((np = cmXmlSearch( dnp, "octave-shift", NULL, 0)) != NULL )
  496. {
  497. unsigned span_number = -1;
  498. if( cmXmlAttrUInt(np,"number",&span_number) != kOkXmlRC )
  499. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'number' attribute.");
  500. if((a = cmXmlFindAttrib(np,"type")) == NULL)
  501. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'type' attribute.");
  502. rc = _cmXScorePushOctaveShift(p,meas,staff,span_number,a->value,tick+offset);
  503. pushFl = false;
  504. }
  505. else
  506. {
  507. pushFl = false;
  508. }
  509. if( pushFl )
  510. rc = _cmXScorePushNonNote(p,meas,dnp,tick+offset,duration,rvalue,tvalue,flags);
  511. return rc;
  512. }
  513. // On input tickRef is set to the absolute tick of the bar line and on output it is set
  514. // to the absolute tick of the next bar line.
  515. cmXsRC_t _cmXScoreParseMeasure(cmXScore_t* p, cmXsPart_t* pp, const cmXmlNode_t* mnp, unsigned* tickRef)
  516. {
  517. cmXsRC_t rc = kOkXsRC;
  518. const cmXmlNode_t* np = NULL;
  519. unsigned tick = *tickRef;
  520. unsigned tick0= 0;
  521. cmXsMeas_t* m = NULL;
  522. // allocate the 'measure' record
  523. cmXsMeas_t* meas = cmLhAllocZ(p->lhH,cmXsMeas_t,1);
  524. // get measure number
  525. if( cmXmlAttrUInt(mnp,"number", &meas->number) != kOkXmlRC )
  526. return _cmXScoreMissingAttribute(p,mnp,"number");
  527. if( pp->measL == NULL )
  528. pp->measL = meas;
  529. else
  530. {
  531. m = pp->measL;
  532. while( m->link != NULL )
  533. m = m->link;
  534. m->link = meas;
  535. meas->divisions = m->divisions;
  536. meas->beats = m->beats;
  537. meas->beat_type = m->beat_type;
  538. }
  539. // get measure attributes node
  540. if((np = cmXmlSearch(mnp,"attributes",NULL,0)) != NULL)
  541. {
  542. cmXmlNodeUInt(np,&meas->divisions,"divisions",NULL);
  543. cmXmlNodeUInt(np,&meas->beats, "time","beats",NULL);
  544. cmXmlNodeUInt(np,&meas->beat_type,"time","beat-type",NULL);
  545. }
  546. // store the bar line
  547. if((rc = _cmXScorePushNonNote(p,meas,mnp,tick,0,0,NULL,kBarXsFl)) != kOkXsRC )
  548. return rc;
  549. np = mnp->children;
  550. // for each child of the 'meas' XML node
  551. for(; rc==kOkXsRC && np!=NULL; np=np->sibling)
  552. {
  553. // if this is a 'note' node
  554. if( cmTextCmp(np->label,"note") == 0 )
  555. {
  556. rc = _cmXScoreParseNote(p,meas,np,&tick0,&tick);
  557. }
  558. else
  559. // if this is a 'backup' node
  560. if( cmTextCmp(np->label,"backup") == 0 )
  561. {
  562. unsigned backup;
  563. cmXmlNodeUInt(np,&backup,"duration",NULL);
  564. if( backup > tick )
  565. tick = 0;
  566. else
  567. tick -= backup;
  568. tick0 = tick;
  569. }
  570. else
  571. // if this is a 'direction' node
  572. if( cmTextCmp(np->label,"direction") == 0 )
  573. {
  574. rc = _cmXScoreParseDirection(p,meas,np,tick);
  575. }
  576. }
  577. *tickRef = tick;
  578. return rc;
  579. }
  580. cmXsRC_t _cmXScoreParsePart( cmXScore_t* p, cmXsPart_t* pp )
  581. {
  582. cmXsRC_t rc = kOkXsRC;
  583. const cmXmlNode_t* xnp;
  584. cmXmlAttr_t partAttr = { "id", pp->idStr };
  585. unsigned barTick = 0;
  586. // find the 'part'
  587. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part", &partAttr, 1)) == NULL )
  588. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The part '%s' was not found.",pp->idStr);
  589. // for each child of this part - find each measure
  590. const cmXmlNode_t* cnp = xnp->children;
  591. for(; cnp!=NULL; cnp=cnp->sibling)
  592. if( cmTextCmp(cnp->label,"measure") == 0 )
  593. if((rc = _cmXScoreParseMeasure(p,pp,cnp,&barTick)) != kOkXsRC )
  594. return rc;
  595. return rc;
  596. }
  597. // Insert note 'np' into the sorted note list based at 's0'.
  598. // Return a pointer to the base of the list after the insertion.
  599. cmXsNote_t* _cmXScoreInsertSortedNote( cmXsNote_t* s0, cmXsNote_t* np )
  600. {
  601. if( s0 == NULL )
  602. return np;
  603. if( np->tick < s0->tick )
  604. {
  605. np->slink = s0;
  606. return np;
  607. }
  608. cmXsNote_t* s1 = s0;
  609. cmXsNote_t* s2 = s0->slink;
  610. while( s2 != NULL )
  611. {
  612. if( s2->tick > np->tick )
  613. {
  614. s1->slink = np;
  615. np->slink = s2;
  616. return s0;
  617. }
  618. s1 = s2;
  619. s2 = s2->slink;
  620. }
  621. s1->slink = np;
  622. return s0;
  623. }
  624. void _cmXScoreSort( cmXScore_t* p )
  625. {
  626. // for each part
  627. cmXsPart_t* pp = p->partL;
  628. for(; pp!=NULL; pp=pp->link)
  629. {
  630. // for each measure in this part
  631. cmXsMeas_t* mp = pp->measL;
  632. for(; mp!=NULL; mp=mp->link)
  633. {
  634. // for each voice in this measure
  635. cmXsVoice_t* vp = mp->voiceL;
  636. for(; vp!=NULL; vp=vp->link)
  637. {
  638. // for each note in this measure
  639. cmXsNote_t* np = vp->noteL;
  640. for(; np!=NULL; np=np->mlink)
  641. mp->noteL = _cmXScoreInsertSortedNote(mp->noteL,np);
  642. }
  643. }
  644. }
  645. }
  646. bool _cmXScoreFindTiedNote( cmXScore_t* p, cmXsMeas_t* mp, cmXsNote_t* n0p, bool rptFl )
  647. {
  648. cmXsNote_t* nnp = n0p->slink; // begin w/ note following np
  649. unsigned measNumb = mp->number;
  650. cmChar_t acc = n0p->alter==-1?'b' : (n0p->alter==1?'#':' ');
  651. if( rptFl )
  652. printf("%i %i %s ",n0p->meas->number,n0p->tick,cmMidiToSciPitch(n0p->pitch,NULL,0));
  653. while(1)
  654. {
  655. // if we are at the end of a measure advance to the next measure
  656. if( nnp == NULL )
  657. {
  658. mp = mp->link;
  659. nnp = mp->noteL;
  660. // if a measure was completed and no end note was found ... then the tie is unterminated
  661. // (a tie must be continued in every measure which it passes through)
  662. if( mp->number > measNumb + 1 )
  663. break;
  664. }
  665. // for each note starting at nnp
  666. for(; nnp!=NULL; nnp=nnp->slink)
  667. {
  668. // if this note is tied to the originating note (np)
  669. if( nnp->voice->id == n0p->voice->id && nnp->step == n0p->step && nnp->octave == n0p->octave )
  670. {
  671. nnp->flags |= kTieProcXsFl;
  672. nnp->flags = cmClrFlag(nnp->flags,kOnsetXsFl);
  673. if( rptFl )
  674. printf("---> %i %i %s ",nnp->meas->number,nnp->tick,cmMidiToSciPitch(nnp->pitch,NULL,0));
  675. // if this note is not tied to a subsequent note
  676. if( cmIsNotFlag(nnp->flags,kTieBegXsFl) )
  677. return true;
  678. // record the measure number of the last note with a tie-start
  679. measNumb = mp->number;
  680. }
  681. }
  682. }
  683. cmErrWarnMsg(&p->err,kUnterminatedTieXsRC,"The tied %c%c%i in measure %i was not terminated.",n0p->step,acc,n0p->octave,measNumb);
  684. return false;
  685. }
  686. void _cmXScoreResolveTiesAndLoc( cmXScore_t* p )
  687. {
  688. unsigned n = 0;
  689. unsigned m = 0;
  690. bool rptFl = false;
  691. cmXsPart_t* pp = p->partL;
  692. // for each part
  693. for(; pp!=NULL; pp=pp->link)
  694. {
  695. unsigned locIdx = 1;
  696. cmXsMeas_t* mp = pp->measL;
  697. // for each measure
  698. for(; mp!=NULL; mp=mp->link)
  699. {
  700. cmXsNote_t* n0 = NULL;
  701. cmXsNote_t* np = mp->noteL;
  702. // for each note in this measure
  703. for(; np!=NULL; np=np->slink)
  704. {
  705. // if this note begins a tie and has not yet been processed
  706. // (A note that continues a tie and therefore has a kTieBegXsFl set
  707. // may have already been processed by an earlier tied note.)
  708. if( cmIsFlag(np->flags,kTieBegXsFl) && cmIsNotFlag(np->flags,kTieProcXsFl))
  709. {
  710. if( _cmXScoreFindTiedNote(p,mp,np,rptFl) )
  711. m += 1;
  712. if( rptFl )
  713. printf("\n");
  714. n += 1;
  715. }
  716. if( cmIsFlag(np->flags,kTieEndXsFl) && cmIsFlag(np->flags,kOnsetXsFl) )
  717. {
  718. cmChar_t acc = np->alter==-1?'b' : (np->alter==1?'#':' ');
  719. cmErrWarnMsg(&p->err,kUnterminatedTieXsRC,"The tied %c%c%i in measure %i marked as a tied note but is also marked to sound.",np->step,acc,np->octave,mp->number);
  720. }
  721. // set the location
  722. if( cmIsFlag(np->flags,kOnsetXsFl|kBarXsFl) )
  723. {
  724. if( cmIsFlag(np->flags,kBarXsFl) || (n0!=NULL && n0->tick!=np->tick))
  725. locIdx += 1;
  726. np->locIdx = locIdx;
  727. n0 = np;
  728. }
  729. }
  730. }
  731. }
  732. printf("Found:%i Not Found:%i\n",m,n-m);
  733. }
  734. cmXsRC_t _cmXScoreResolveOctaveShift( cmXScore_t* p )
  735. {
  736. const cmXsSpan_t* s;
  737. for(s=p->spanL; s!=NULL; s=s->link)
  738. {
  739. if( s->tick1 == -1)
  740. {
  741. cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"An unterminated octave shift span was encountered in measure %i staff=%i.",s->meas->number,s->staff);
  742. }
  743. else
  744. {
  745. cmXsMeas_t* m = p->partL->measL;
  746. for(; m!=NULL; m=m->link)
  747. if( m->number == s->meas->number )
  748. break;
  749. assert( m != NULL );
  750. cmXsNote_t* note = m->noteL;
  751. for(; note!=NULL; note=note->slink)
  752. if( note->staff==s->staff && s->tick0 <= note->tick && note->tick < s->tick1 )
  753. note->pitch += s->pitch_offset;
  754. }
  755. }
  756. return kOkXsRC;
  757. }
  758. // The identical pitch may be notated to play simultaneously on different voices.
  759. // As performed on the piano this will equate to a single sounding note.
  760. // This function clears the onset flag on all except one of the duplicated notes.
  761. void _cmXScoreRemoveDuplicateNotes( cmXScore_t* p )
  762. {
  763. cmXsPart_t* pp = p->partL;
  764. // for each part
  765. for(; pp!=NULL; pp=pp->link)
  766. {
  767. cmXsMeas_t* mp = pp->measL;
  768. // for each measure
  769. for(; mp!=NULL; mp=mp->link)
  770. {
  771. cmXsNote_t* np = mp->noteL;
  772. // for each note in this measure
  773. for(; np!=NULL; np=np->slink)
  774. if( cmIsFlag(np->flags,kOnsetXsFl) )
  775. {
  776. cmXsNote_t* n0p = mp->noteL;
  777. for(; n0p!=NULL; n0p=n0p->slink)
  778. if( n0p!=np && cmIsFlag(n0p->flags,kOnsetXsFl) && np->locIdx==n0p->locIdx && np->pitch==n0p->pitch )
  779. n0p->flags = cmClrFlag(n0p->flags,kOnsetXsFl);
  780. }
  781. }
  782. }
  783. }
  784. cmXsMeas_t* _cmXScoreNextNonEmptyMeas( cmXsPart_t* pp, cmXsMeas_t* meas )
  785. {
  786. if( meas == NULL )
  787. {
  788. if( pp==NULL || pp->measL==NULL )
  789. return NULL;
  790. meas = pp->measL;
  791. }
  792. else
  793. {
  794. meas = meas->link;
  795. }
  796. while( meas != NULL && meas->noteL == NULL )
  797. meas=meas->link;
  798. return meas;
  799. }
  800. cmXsNote_t* _cmXScoreNextNote( cmXsPart_t* pp, cmXsNote_t* note )
  801. {
  802. // meas should always be valid (unless this is the first note in the score)
  803. cmXsMeas_t* meas = note==NULL ? NULL : note->meas;
  804. do
  805. {
  806. if( note == NULL || note->slink==NULL )
  807. {
  808. if((meas = _cmXScoreNextNonEmptyMeas(pp,meas)) == NULL)
  809. return NULL;
  810. assert( meas->noteL != NULL );
  811. note = meas->noteL;
  812. }
  813. else
  814. {
  815. note = note->slink;
  816. }
  817. assert( note != NULL );
  818. meas = note->meas;
  819. // note is now set to a non-NULL candidate note - advance to a sounding note
  820. while( note!=NULL && cmIsNotFlag(note->flags,kOnsetXsFl) )
  821. note = note->slink;
  822. // if no note was found in this measure
  823. }while( note == NULL );
  824. return note;
  825. }
  826. cmXsRC_t _cmXScoreWriteScorePlotFile( cmXScore_t* p, const cmChar_t* fn )
  827. {
  828. cmXsRC_t rc = kOkXsRC;
  829. cmFileH_t fH = cmFileNullHandle;
  830. double ticks_per_sec = 0;
  831. double onset_secs = 0;
  832. if( cmFileOpen(&fH,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  833. return cmErrMsg(&p->err,kFileFailXsRC,"Unable to create the file '%s'.",cmStringNullGuard(fn));
  834. cmXsPart_t* pp = p->partL;
  835. for(; pp!=NULL; pp=pp->link)
  836. {
  837. cmXsMeas_t* mp = pp->measL;
  838. for(; mp!=NULL; mp=mp->link)
  839. {
  840. cmFilePrintf(fH,"b %f %i %s B\n",onset_secs,mp->number,"bar");
  841. cmXsNote_t* np = mp->noteL;
  842. unsigned tick0 = 0;
  843. for(; np!=NULL; np=np->slink)
  844. {
  845. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  846. {
  847. double bps = np->duration / 60.0;
  848. // t b t
  849. // - = - -
  850. // s s b
  851. ticks_per_sec = bps * mp->divisions;
  852. }
  853. else
  854. {
  855. if( cmIsFlag(np->flags,kOnsetXsFl) )
  856. {
  857. onset_secs += (np->tick - tick0) / ticks_per_sec;
  858. tick0 = np->tick;
  859. 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");
  860. }
  861. }
  862. }
  863. onset_secs += (mp->divisions * mp->beats - tick0) / ticks_per_sec;
  864. }
  865. }
  866. cmFileClose(&fH);
  867. return rc;
  868. }
  869. cmXsRC_t _cmXScoreWriteMidiPlotFile( cmXScore_t* p, cmChar_t* fn, const cmMidiTrackMsg_t** m, unsigned mN )
  870. {
  871. cmXsRC_t rc = kOkXsRC;
  872. cmFileH_t fH = cmFileNullHandle;
  873. unsigned i = 0;
  874. if( cmFileOpen(&fH,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  875. return cmErrMsg(&p->err,kFileFailXsRC,"Unable to create the file '%s'.",cmStringNullGuard(fn));
  876. for(i=0; i<mN; ++i)
  877. if( (m[i]!=NULL) && cmMidiIsChStatus(m[i]->status) && cmMidiIsNoteOn(m[i]->status) && (m[i]->u.chMsgPtr->d1>0) )
  878. 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));
  879. cmFileClose(&fH);
  880. return rc;
  881. }
  882. cmXsRC_t _cmXScoreProcessMidi(cmXScore_t* p, cmCtx_t* ctx, const cmChar_t* midiFn)
  883. {
  884. cmXsRC_t rc = kOkXsRC;
  885. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  886. const cmMidiTrackMsg_t** m = NULL;
  887. unsigned mN = 0;
  888. unsigned i = 0;
  889. unsigned j = 0;
  890. cmXsNote_t* note = NULL;
  891. if( cmMidiFileOpen(ctx, &mfH, midiFn ) != kOkMfRC )
  892. return cmErrMsg(&p->err,kMidiFailXsRC,"The MIDI file object could not be opened from '%s'.",cmStringNullGuard(midiFn));
  893. //cmMidiFilePrintMsgs(mfH, p->err.rpt );
  894. if( (m = cmMidiFileMsgArray(mfH)) == NULL || (mN = cmMidiFileMsgCount(mfH)) == 0 )
  895. {
  896. rc = cmErrMsg(&p->err,kMidiFailXsRC,"The MIDI file object appears to be empty.");
  897. goto errLabel;
  898. }
  899. if((note = _cmXScoreNextNote(p->partL,NULL)) == NULL)
  900. {
  901. rc = cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"No MIDI processing to be done. The score appears to be empty.");
  902. goto errLabel;
  903. }
  904. cmCtx* c = cmCtxAlloc( NULL, p->err.rpt, cmLHeapNullHandle, cmSymTblNullHandle );
  905. cmSeqAlign_t* s = cmSeqAlignAlloc(c,NULL);
  906. unsigned offs = 0;
  907. for(; note!=NULL; note=_cmXScoreNextNote(p->partL,note))
  908. {
  909. if( cmIsFlag(note->flags,kGraceXsFl) )
  910. offs += 1;
  911. cmSeqAlignInsert(s,0,note->locIdx+offs,note->pitch);
  912. }
  913. unsigned locIdx = 1;
  914. for(i=0,j=0; i<mN; ++i)
  915. if( (m[i]!=NULL) && cmMidiIsChStatus(m[i]->status) && cmMidiIsNoteOn(m[i]->status) && (m[i]->u.chMsgPtr->d1>0) )
  916. {
  917. if( m[j]->atick != m[i]->atick )
  918. locIdx += 1;
  919. cmSeqAlignInsert(s,1,locIdx,m[i]->u.chMsgPtr->d0);
  920. //printf("%i : %s\n",locIdx,cmMidiToSciPitch(m[i]->u.chMsgPtr->d0,NULL,0));
  921. j = i;
  922. }
  923. cmMidiFileCalcNoteDurations( mfH );
  924. _cmXScoreWriteScorePlotFile(p, "/Users/kevin/temp/score.txt" );
  925. _cmXScoreWriteMidiPlotFile(p, "/Users/kevin/temp/midi.txt", m, mN );
  926. cmSeqAlignReport(s,p->err.rpt);
  927. cmSeqAlignFree(&s);
  928. cmCtxFree(&c);
  929. goto errLabel;
  930. printf(" i j score midi\n");
  931. printf("---- ---- --- ---- --- ----\n");
  932. for(j=0; note!=NULL; note=_cmXScoreNextNote(p->partL,note),++j)
  933. {
  934. unsigned midiPitch = 0;
  935. for(; i<mN; ++i)
  936. if( m[i]!=NULL && cmMidiIsChStatus(m[i]->status) && cmMidiIsNoteOn(m[i]->status) && m[i]->u.chMsgPtr->d1>0 )
  937. {
  938. midiPitch = m[i]->u.chMsgPtr->d0;
  939. ++i;
  940. break;
  941. }
  942. char buf[6];
  943. printf("%4i %4i %3i %4s %3i %4s\n",j,i,
  944. note->pitch,
  945. cmMidiToSciPitch(note->pitch,NULL,0),
  946. midiPitch,
  947. cmMidiToSciPitch(midiPitch,buf,5));
  948. }
  949. errLabel:
  950. cmMidiFileClose(&mfH);
  951. return rc;
  952. }
  953. cmXsRC_t cmXScoreInitialize( cmCtx_t* ctx, cmXsH_t* hp, const cmChar_t* xmlFn, const cmChar_t* midiFn )
  954. {
  955. cmXsRC_t rc = kOkXsRC;
  956. if((rc = cmXScoreFinalize(hp)) != kOkXsRC )
  957. return rc;
  958. cmXScore_t* p = cmMemAllocZ(cmXScore_t,1);
  959. cmErrSetup(&p->err,&ctx->rpt,"XScore");
  960. // create a local linked heap
  961. if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8196,ctx)) == false )
  962. return cmErrMsg(&p->err,kLHeapFailXsRC,"Lheap create failed.");
  963. // open the music xml file
  964. if( cmXmlAlloc(ctx, &p->xmlH, xmlFn) != kOkXmlRC )
  965. {
  966. rc = cmErrMsg(&p->err,kXmlFailXsRC,"Unable to open the MusicXML file '%s'.",cmStringNullGuard(xmlFn));
  967. goto errLabel;
  968. }
  969. //cmXmlPrint(p->xmlH,&ctx->rpt);
  970. // parse the part-list
  971. if((rc = _cmXScoreParsePartList( p )) != kOkXsRC )
  972. goto errLabel;
  973. // parse each score 'part'
  974. cmXsPart_t* pp = p->partL;
  975. for(; pp!=NULL; pp=pp->link)
  976. if((rc = _cmXScoreParsePart(p,pp)) != kOkXsRC )
  977. goto errLabel;
  978. // fill in the note->slink chain to link the notes in each measure in time order
  979. _cmXScoreSort(p);
  980. _cmXScoreResolveTiesAndLoc(p);
  981. _cmXScoreRemoveDuplicateNotes(p);
  982. //_cmXScoreResolveOctaveShift(p);
  983. //if( midiFn != NULL )
  984. // _cmXScoreProcessMidi(p,ctx,midiFn);
  985. // CSV output initialize failed.
  986. if( cmCsvInitialize(&p->csvH,ctx) != kOkCsvRC )
  987. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output object create failed.");
  988. errLabel:
  989. if( rc != kOkXsRC )
  990. _cmXScoreFinalize(p);
  991. else
  992. hp->h = p;
  993. return rc;
  994. }
  995. cmXsRC_t cmXScoreFinalize( cmXsH_t* hp )
  996. {
  997. cmXsRC_t rc = kOkXsRC;
  998. if( hp == NULL || cmXScoreIsValid(*hp)==false )
  999. return kOkXsRC;
  1000. cmXScore_t* p = _cmXScoreHandleToPtr(*hp);
  1001. if((rc = _cmXScoreFinalize(p)) != kOkXsRC )
  1002. return rc;
  1003. hp->h = NULL;
  1004. return rc;
  1005. }
  1006. bool cmXScoreIsValid( cmXsH_t h )
  1007. { return h.h != NULL; }
  1008. void _cmXScoreReportTitle( cmRpt_t* rpt )
  1009. {
  1010. cmRptPrintf(rpt," voc loc tick durtn rval flags\n");
  1011. cmRptPrintf(rpt," --- ----- ------- ----- ---- --- -------------\n");
  1012. }
  1013. void _cmXScoreReportNote( cmRpt_t* rpt, const cmXsNote_t* note )
  1014. {
  1015. const cmChar_t* B = cmIsFlag(note->flags,kBarXsFl) ? "|" : "-";
  1016. const cmChar_t* R = cmIsFlag(note->flags,kRestXsFl) ? "R" : "-";
  1017. const cmChar_t* G = cmIsFlag(note->flags,kGraceXsFl) ? "G" : "-";
  1018. const cmChar_t* D = cmIsFlag(note->flags,kDotXsFl) ? "D" : "-";
  1019. const cmChar_t* C = cmIsFlag(note->flags,kChordXsFl) ? "C" : "-";
  1020. const cmChar_t* e = cmIsFlag(note->flags,kEvenXsFl) ? "e" : "-";
  1021. const cmChar_t* d = cmIsFlag(note->flags,kDynXsFl) ? "d" : "-";
  1022. const cmChar_t* t = cmIsFlag(note->flags,kTempoXsFl) ? "t" : "-";
  1023. const cmChar_t* P = cmIsFlag(note->flags,kPedalDnXsFl) ? "V" : "-";
  1024. const cmChar_t* S = cmIsFlag(note->flags,kSectionXsFl) ? "S" : "-";
  1025. const cmChar_t* H = cmIsFlag(note->flags,kHeelXsFl) ? "H" : "-";
  1026. const cmChar_t* T0 = cmIsFlag(note->flags,kTieBegXsFl) ? "T" : "-";
  1027. const cmChar_t* T1 = cmIsFlag(note->flags,kTieEndXsFl) ? "_" : "-";
  1028. P = cmIsFlag(note->flags,kPedalUpXsFl) ? "^" : P;
  1029. P = cmIsFlag(note->flags,kPedalUpDnXsFl) ? "X" : P;
  1030. //const cmChar_t* N = note->pitch==0 ? " " : cmMidiToSciPitch( note->pitch, NULL, 0 );
  1031. cmChar_t N[] = {'\0','\0','\0','\0'};
  1032. cmChar_t acc = note->alter==-1?'b':(note->alter==1?'#':' ');
  1033. snprintf(N,4,"%c%c%1i",note->step,acc,note->octave);
  1034. cmRptPrintf(rpt," %3i %5i %7i %5i %4.1f %3s %s%s%s%s%s%s%s%s%s%s%s%s%s",
  1035. note->voice->id,
  1036. note->locIdx,
  1037. note->tick,
  1038. note->duration,
  1039. note->rvalue,
  1040. N,B,R,G,D,C,e,d,t,P,S,H,T0,T1);
  1041. if( cmIsFlag(note->flags,kSectionXsFl) )
  1042. cmRptPrintf(rpt," %s",cmStringNullGuard(note->tvalue));
  1043. if( cmIsFlag(note->flags,kMetronomeXsFl) )
  1044. cmRptPrintf(rpt," %i bpm",note->duration);
  1045. }
  1046. /*
  1047. kMidiFileIdColScIdx= 0,
  1048. kTypeLabelColScIdx = 3,
  1049. kDSecsColScIdx = 4,
  1050. kSecsColScIdx = 5,
  1051. kD0ColScIdx = 9,
  1052. kD1ColScIdx = 10,
  1053. kPitchColScIdx = 11,
  1054. kBarColScIdx = 13,
  1055. kSkipColScIdx = 14,
  1056. kEvenColScIdx = 15,
  1057. kGraceColScIdx = 16,
  1058. kTempoColScIdx = 17,
  1059. kFracColScIdx = 18,
  1060. kDynColScIdx = 19,
  1061. kSectionColScIdx = 20,
  1062. kRecdPlayColScIdx = 21,
  1063. kRemarkColScIdx = 22
  1064. */
  1065. cmXsRC_t _cmXScoreWriteCsvHdr( cmXScore_t* p )
  1066. {
  1067. const cmChar_t* s[] =
  1068. {
  1069. "id","trk","evt","opcode","dticks","micros","status",
  1070. "meta","ch","d0","d1","arg0","arg1","bar","skip",
  1071. "even","grace","tempo","t frac","dyn","section","play_recd","remark",NULL
  1072. };
  1073. cmCsvCell_t* lcp = NULL;
  1074. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymText(p->csvH,s[0]), 0, 0 ) != kOkCsvRC )
  1075. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  1076. unsigned i;
  1077. for(i=1; s[i]!=NULL; ++i)
  1078. {
  1079. if( cmCsvInsertIdentColAfter(p->csvH, lcp, &lcp, s[i], 0 ) != kOkCsvRC )
  1080. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV error inserting CSV title %i.\n",i);
  1081. }
  1082. return kOkXsRC;
  1083. }
  1084. cmXsRC_t _cmXScoreWriteCsvBlankCols( cmXScore_t* p, unsigned cnt, cmCsvCell_t** leftCellPtrPtr )
  1085. {
  1086. unsigned i;
  1087. for(i=0; i<cnt; ++i)
  1088. if( cmCsvInsertIdentColAfter(p->csvH,*leftCellPtrPtr,leftCellPtrPtr,"",0) != kOkCsvRC )
  1089. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV output failed on blank column.");
  1090. return kOkCsvRC;
  1091. }
  1092. cmXsRC_t _cmXScoreWriteCsvRow(
  1093. cmXScore_t* p,
  1094. unsigned rowIdx,
  1095. unsigned uid,
  1096. unsigned bar,
  1097. const cmChar_t* sectionStr,
  1098. const cmChar_t* opCodeStr,
  1099. double dsecs,
  1100. double secs,
  1101. unsigned d0,
  1102. unsigned d1,
  1103. unsigned pitch, // set to -1 if the pitch is not valid
  1104. double frac,
  1105. unsigned flags )
  1106. {
  1107. cmXsRC_t rc = kOkXsRC;
  1108. cmCsvCell_t* lcp = NULL;
  1109. // append an empty row to the CSV object
  1110. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymUInt(p->csvH, rowIdx ), 0, 0 ) != kOkCsvRC )
  1111. {
  1112. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  1113. goto errLabel;
  1114. }
  1115. /*
  1116. // col 0 : blanks
  1117. if( cmCsvInsertUIntColAfter(p->csvH, lcp, &lcp, rowIdx, 0 ) != kOkCsvRC )
  1118. {
  1119. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output row index failed.");
  1120. goto errLabel;
  1121. }
  1122. */
  1123. // col 1 : track (always 1)
  1124. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,1,0) != kOkCsvRC )
  1125. {
  1126. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1127. goto errLabel;
  1128. }
  1129. // col 2 : evt (set to event uid, or blank if uid == -1)
  1130. if( uid == -1 )
  1131. {
  1132. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1133. goto errLabel;
  1134. }
  1135. else
  1136. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,uid,0) != kOkCsvRC )
  1137. {
  1138. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1139. goto errLabel;
  1140. }
  1141. // col 3 : output the opcode
  1142. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,opCodeStr,0) != kOkCsvRC )
  1143. {
  1144. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on opcode label.");
  1145. goto errLabel;
  1146. }
  1147. // col 4 : dsecs
  1148. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,dsecs,0) != kOkCsvRC )
  1149. {
  1150. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dsecs'.");
  1151. goto errLabel;
  1152. }
  1153. // col 5 : secs
  1154. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,secs,0) != kOkCsvRC )
  1155. {
  1156. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'secs'.");
  1157. goto errLabel;
  1158. }
  1159. // cols 6,7,8 blanks
  1160. if((rc = _cmXScoreWriteCsvBlankCols(p,3,&lcp)) != kOkXsRC )
  1161. goto errLabel;
  1162. // col 9 : d0
  1163. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d0,0) != kOkCsvRC )
  1164. {
  1165. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1166. goto errLabel;
  1167. }
  1168. // col 10 : d1
  1169. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d1,0) != kOkCsvRC )
  1170. {
  1171. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd1'.");
  1172. goto errLabel;
  1173. }
  1174. // col 11 : pitch
  1175. if( pitch == -1 )
  1176. {
  1177. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1178. goto errLabel;
  1179. }
  1180. else
  1181. {
  1182. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmMidiToSciPitch(pitch,NULL,0),0) != kOkCsvRC )
  1183. {
  1184. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  1185. goto errLabel;
  1186. }
  1187. }
  1188. // col 12 : blanks
  1189. if((rc = _cmXScoreWriteCsvBlankCols(p,1 + (cmIsFlag(flags,kBarXsFl) ? 0 : 1), &lcp)) != kOkXsRC )
  1190. goto errLabel;
  1191. // col 13 : bar number
  1192. if( cmIsFlag(flags,kBarXsFl) )
  1193. {
  1194. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,bar,0) != kOkCsvRC )
  1195. {
  1196. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  1197. goto errLabel;
  1198. }
  1199. }
  1200. // col 14 : skip (blank for now)
  1201. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1202. goto errLabel;
  1203. // col 15: even (all grace notes are 'even' notes
  1204. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp, cmIsFlag(flags,kGraceXsFl) | cmIsFlag(flags,kEvenXsFl) ? "e" : "",0) != kOkCsvRC )
  1205. {
  1206. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1207. goto errLabel;
  1208. }
  1209. // col 16: grace
  1210. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kGraceXsFl) ? "g" : "",0) != kOkCsvRC )
  1211. {
  1212. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1213. goto errLabel;
  1214. }
  1215. // col 17: tempo
  1216. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kTempoXsFl) ? "t" : "",0) != kOkCsvRC )
  1217. {
  1218. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1219. goto errLabel;
  1220. }
  1221. // col 18: frac
  1222. if( frac == 0 )
  1223. {
  1224. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1225. goto errLabel;
  1226. }
  1227. else
  1228. {
  1229. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,1.0/frac,0) != kOkCsvRC )
  1230. {
  1231. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 't frac'.");
  1232. goto errLabel;
  1233. }
  1234. }
  1235. // col 19: dynamic marking (blank for now)
  1236. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1237. goto errLabel;
  1238. // col 20: section
  1239. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kSectionXsFl) ? sectionStr : "",0) != kOkCsvRC )
  1240. {
  1241. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1242. goto errLabel;
  1243. }
  1244. // col 21, 22 : recd-play, remark (blank for now)
  1245. if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
  1246. goto errLabel;
  1247. errLabel:
  1248. return rc;
  1249. }
  1250. cmXsRC_t cmXScoreWriteCsv( cmXsH_t h, const cmChar_t* csvFn )
  1251. {
  1252. cmXsRC_t rc = kOkXsRC;
  1253. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1254. unsigned rowIdx = 1;
  1255. double tpqn = 0; // ticks per quarter note
  1256. double tps = 0; // ticks per second
  1257. const cmChar_t* sectionIdStr = NULL;
  1258. unsigned metro_tick = 0;
  1259. double metro_sec = 0;
  1260. double sec0 = 0;
  1261. if( !cmCsvIsValid(p->csvH) )
  1262. return cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output object is not initialized.");
  1263. if((rc = _cmXScoreWriteCsvHdr( p )) != kOkXsRC )
  1264. goto errLabel;
  1265. cmXsPart_t* pp = p->partL;
  1266. for(; pp!=NULL; pp=pp->link)
  1267. {
  1268. cmXsMeas_t* mp = pp->measL;
  1269. for(; mp!=NULL; mp=mp->link)
  1270. {
  1271. if( mp->divisions != 0 )
  1272. tpqn = mp->divisions;
  1273. cmXsNote_t* np = mp->noteL;
  1274. for(; np!=NULL; np=np->slink)
  1275. {
  1276. // Seconds are calculated as:
  1277. // dticks = np->tick - metro_tick; // where metro_tick is the absolute tick of the last metro event
  1278. // secs = (dticks/tps) + metro_secs; // where metro_secs is the absoute time of the last metro event
  1279. unsigned dticks = np->tick - metro_tick;
  1280. double secs = tps==0 ? 0 : (dticks/tps) + metro_sec;
  1281. double dsecs = secs - sec0;
  1282. //
  1283. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  1284. {
  1285. double bpm = np->duration;
  1286. double bps = bpm / 60.0;
  1287. tps = bps * tpqn;
  1288. metro_tick = np->tick;
  1289. metro_sec = secs;
  1290. }
  1291. // if this is a section event
  1292. if( cmIsFlag(np->flags,kSectionXsFl) )
  1293. sectionIdStr = np->tvalue;
  1294. // if this is a bar event
  1295. if( cmIsFlag(np->flags,kBarXsFl) )
  1296. {
  1297. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,NULL,"bar",dsecs,secs,0,0,-1,0,np->flags);
  1298. sec0 = secs;
  1299. }
  1300. else
  1301. // if this is a pedal event
  1302. if( cmIsFlag(np->flags,kPedalDnXsFl|kPedalUpXsFl|kPedalUpDnXsFl) )
  1303. {
  1304. unsigned d0 = 64; // pedal MIDI ctl id
  1305. unsigned d1 = cmIsFlag(np->flags,kPedalDnXsFl) ? 64 : 0; // pedal-dn: d1>=64 pedal-up:<64
  1306. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,NULL,"ctl",dsecs,secs,d0,d1,-1,0,np->flags);
  1307. sec0 = secs;
  1308. }
  1309. else
  1310. // if this is a sounding note event
  1311. if( cmIsFlag(np->flags,kOnsetXsFl) )
  1312. {
  1313. double frac = np->rvalue + (cmIsFlag(np->flags,kDotXsFl) ? (np->rvalue/2) : 0);
  1314. //
  1315. _cmXScoreWriteCsvRow(p,rowIdx,np->uid,mp->number,sectionIdStr,"non",dsecs,secs,np->pitch,60,np->pitch,frac,np->flags);
  1316. sec0 = secs;
  1317. sectionIdStr = NULL;
  1318. }
  1319. rowIdx += 1;
  1320. }
  1321. }
  1322. }
  1323. if( cmCsvWrite( p->csvH, csvFn ) != kOkCsvRC )
  1324. rc = cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output write failed on file '%s'.",csvFn);
  1325. errLabel:
  1326. return rc;
  1327. }
  1328. void cmXScoreReport( cmXsH_t h, cmRpt_t* rpt, bool sortFl )
  1329. {
  1330. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1331. cmXsPart_t* pp = p->partL;
  1332. for(; pp!=NULL; pp=pp->link)
  1333. {
  1334. cmRptPrintf(rpt,"Part:%s\n",pp->idStr);
  1335. const cmXsMeas_t* meas = pp->measL;
  1336. for(; meas!=NULL; meas=meas->link)
  1337. {
  1338. 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);
  1339. if( sortFl )
  1340. {
  1341. _cmXScoreReportTitle(rpt);
  1342. const cmXsNote_t* note = meas->noteL;
  1343. for(; note!=NULL; note=note->slink)
  1344. {
  1345. _cmXScoreReportNote(rpt,note);
  1346. if( note->slink!=NULL || note->voice->id==0)
  1347. cmRptPrintf(rpt,"\n");
  1348. else
  1349. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  1350. }
  1351. }
  1352. else
  1353. {
  1354. _cmXScoreReportTitle(rpt);
  1355. const cmXsVoice_t* v = meas->voiceL;
  1356. for(; v!=NULL; v=v->link)
  1357. {
  1358. const cmXsNote_t* note = v->noteL;
  1359. cmRptPrintf(rpt," voice:%i\n",v->id);
  1360. for(; note!=NULL; note=note->mlink)
  1361. {
  1362. _cmXScoreReportNote(rpt,note);
  1363. if( note->mlink!=NULL || note->voice->id==0)
  1364. cmRptPrintf(rpt,"\n");
  1365. else
  1366. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  1367. }
  1368. }
  1369. }
  1370. }
  1371. }
  1372. }
  1373. cmXsRC_t cmXScoreWriteMidi( cmXsH_t h, const cmChar_t* fn )
  1374. {
  1375. assert(0); // function not implemented
  1376. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1377. cmXsPart_t* pp = p->partL;
  1378. for(; pp!=NULL; pp=pp->link)
  1379. {
  1380. const cmXsMeas_t* meas = pp->measL;
  1381. for(; meas!=NULL; meas=meas->link)
  1382. {
  1383. const cmXsNote_t* note = meas->noteL;
  1384. for(; note!=NULL; note=note->slink)
  1385. {
  1386. }
  1387. }
  1388. }
  1389. }
  1390. cmXsRC_t cmXScoreTest( cmCtx_t* ctx, const cmChar_t* xmlFn, const cmChar_t* midiFn, const cmChar_t* outFn )
  1391. {
  1392. cmXsRC_t rc;
  1393. cmXsH_t h = cmXsNullHandle;
  1394. if((rc = cmXScoreInitialize( ctx, &h, xmlFn, midiFn)) != kOkXsRC )
  1395. return cmErrMsg(&ctx->err,rc,"XScore alloc failed.");
  1396. if( outFn != NULL )
  1397. cmXScoreWriteCsv(h,outFn);
  1398. cmXScoreReport(h,&ctx->rpt,true);
  1399. return cmXScoreFinalize(&h);
  1400. }