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

cmXScore.c 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmRpt.h"
  5. #include "cmErr.h"
  6. #include "cmCtx.h"
  7. #include "cmMem.h"
  8. #include "cmMallocDebug.h"
  9. #include "cmLinkedHeap.h"
  10. #include "cmXml.h"
  11. #include "cmText.h"
  12. #include "cmXScore.h"
  13. #include "cmTime.h"
  14. #include "cmMidi.h"
  15. #include "cmLex.h"
  16. #include "cmCsv.h"
  17. cmXsH_t cmXsNullHandle = cmSTATIC_NULL_HANDLE;
  18. enum
  19. {
  20. kSectionXsFl = 0x0001, // rvalue holds section number
  21. kBarXsFl = 0x0002,
  22. kRestXsFl = 0x0004,
  23. kGraceXsFl = 0x0008,
  24. kDotXsFl = 0x0010,
  25. kChordXsFl = 0x0020,
  26. kDynXsFl = 0x0040,
  27. kEvenXsFl = 0x0080,
  28. kTempoXsFl = 0x0100,
  29. kPedalDnXsFl = 0x0200,
  30. kPedalUpXsFl = 0x0400,
  31. kPedalUpDnXsFl = 0x0800,
  32. kMetronomeXsFl = 0x1000 // duration holds BPM
  33. };
  34. typedef struct cmXsNote_str
  35. {
  36. unsigned flags; // See k???XsFl
  37. unsigned pitch; // midi pitch
  38. unsigned tick; //
  39. unsigned duration; // duration in ticks
  40. unsigned rvalue; // 1/type = rythmic value (1/4=quarter note, 1/8=eighth note, ...)
  41. const cmChar_t* tvalue; // text value
  42. struct cmXsNote_str* mlink; // measure note list
  43. struct cmXsNote_str* slink; // time sorted event list
  44. } cmXsNote_t;
  45. typedef struct cmXsVoice_str
  46. {
  47. unsigned id; // Voice id
  48. cmXsNote_t* noteL; // List of notes in this voice
  49. struct cmXsVoice_str* link; // Link to other voices in this measure
  50. } cmXsVoice_t;
  51. typedef struct cmXsMeas_str
  52. {
  53. unsigned number; // Measure number
  54. unsigned divisions; // ticks-per-quarter-note
  55. unsigned beats; // beats per measure
  56. unsigned beat_type; // whole/half/quarter/eighth ...
  57. cmXsVoice_t* voiceL; // List of voices in this measure
  58. cmXsNote_t* noteL; // List of time sorted notes in this measure
  59. struct cmXsMeas_str* link; // Link to other measures in this part.
  60. } cmXsMeas_t;
  61. typedef struct cmXsPart_str
  62. {
  63. const cmChar_t* idStr; // Id of this part
  64. cmXsMeas_t* measL; // List of measures in this part.
  65. struct cmXsPart_str* link; // Link to other parts in this score
  66. } cmXsPart_t;
  67. typedef struct
  68. {
  69. cmErr_t err;
  70. cmXmlH_t xmlH;
  71. cmLHeapH_t lhH;
  72. cmXsPart_t* partL;
  73. cmCsvH_t csvH;
  74. } cmXScore_t;
  75. cmXScore_t* _cmXScoreHandleToPtr( cmXsH_t h )
  76. {
  77. cmXScore_t* p = (cmXScore_t*)h.h;
  78. assert( p != NULL );
  79. return p;
  80. }
  81. cmXsRC_t _cmXScoreFinalize( cmXScore_t* p )
  82. {
  83. cmXsRC_t rc = kOkXsRC;
  84. // release the XML file
  85. cmXmlFree( &p->xmlH );
  86. // release the local linked heap memory
  87. cmLHeapDestroy(&p->lhH);
  88. // release the CSV output object
  89. cmCsvFinalize(&p->csvH);
  90. cmMemFree(p);
  91. return rc;
  92. }
  93. cmXsRC_t _cmXScoreMissingNode( cmXScore_t* p, const cmXmlNode_t* parent, const cmChar_t* label )
  94. {
  95. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML node '%s'. Parent line:%i",label,parent->line);
  96. }
  97. cmXsRC_t _cmXScoreMissingAttribute( cmXScore_t* p, const cmXmlNode_t* np, const cmChar_t* attrLabel )
  98. {
  99. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML attribute '%s' from node '%s'.",attrLabel,np->label);
  100. }
  101. cmXsVoice_t* _cmXScoreIdToVoice( cmXsMeas_t* meas, unsigned voiceId )
  102. {
  103. cmXsVoice_t* v = meas->voiceL;
  104. for(; v!=NULL; v=v->link)
  105. if( v->id == voiceId )
  106. return v;
  107. return NULL;
  108. }
  109. cmXsRC_t _cmXScorePushNote( cmXScore_t* p, cmXsMeas_t* meas, unsigned voiceId, cmXsNote_t* note )
  110. {
  111. cmXsVoice_t* v;
  112. // get the voice recd
  113. if((v = _cmXScoreIdToVoice(meas,voiceId)) == NULL)
  114. {
  115. // the voice recd doesn't exist for this voiceId - allocate one
  116. v = cmLhAllocZ(p->lhH,cmXsVoice_t,1);
  117. v->id = voiceId;
  118. // add the voice record to the meas->voiceL
  119. if( meas->voiceL == NULL )
  120. meas->voiceL = v;
  121. else
  122. {
  123. cmXsVoice_t* vp = meas->voiceL;
  124. while( vp->link!=NULL )
  125. vp = vp->link;
  126. vp->link = v;
  127. }
  128. }
  129. // add the note record to the end of meas->voiceL
  130. if( v->noteL == NULL )
  131. v->noteL = note;
  132. else
  133. {
  134. cmXsNote_t* n = v->noteL;
  135. while( n->mlink != NULL )
  136. n = n->mlink;
  137. n->mlink = note;
  138. }
  139. return kOkXsRC;
  140. }
  141. cmXsRC_t _cmXScoreParsePartList( cmXScore_t* p )
  142. {
  143. cmXsRC_t rc = kOkXsRC;
  144. cmXsPart_t* lastPartPtr = NULL;
  145. const cmXmlNode_t* xnp;
  146. // find the 'part-list'
  147. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part-list", NULL, 0)) == NULL )
  148. return _cmXScoreMissingNode(p,cmXmlRoot(p->xmlH),"part-list");
  149. const cmXmlNode_t* cnp = xnp->children;
  150. // for each child of the 'part-list'
  151. for(; cnp!=NULL; cnp=cnp->sibling)
  152. if( cmTextCmp( cnp->label, "score-part" ) == 0 )
  153. {
  154. const cmXmlAttr_t* a;
  155. // find the 'score-part' id
  156. if((a = cmXmlFindAttrib(cnp,"id")) == NULL )
  157. return _cmXScoreMissingAttribute(p,cnp,"id");
  158. // allocate a new part record
  159. cmXsPart_t* pp = cmLhAllocZ(p->lhH,cmXsPart_t,1);
  160. pp->idStr = a->value; // set the part id
  161. // link the new part record to the end of the part list
  162. if(lastPartPtr == NULL)
  163. p->partL = pp;
  164. else
  165. lastPartPtr->link = pp;
  166. lastPartPtr = pp;
  167. }
  168. return rc;
  169. }
  170. cmXsRC_t _cmXScoreParsePitch( cmXScore_t* p, const cmXmlNode_t* nnp, unsigned* midiPitchRef )
  171. {
  172. cmXsRC_t rc = kOkXsRC;
  173. unsigned octave = 0;
  174. double alter = 0;
  175. const cmChar_t* step = NULL;
  176. if((step = cmXmlNodeValue(nnp,"pitch","step",NULL)) == NULL )
  177. return _cmXScoreMissingNode(p,nnp,"step");
  178. if((rc = cmXmlNodeUInt( nnp,&octave,"pitch","octave",NULL)) != kOkXmlRC )
  179. return _cmXScoreMissingNode(p,nnp,"octave");
  180. cmXmlNodeDouble( nnp,&alter,"pitch","alter",NULL);
  181. cmChar_t buf[3] = { *step, '0', '\0'};
  182. unsigned midi = cmSciPitchToMidi(buf);
  183. midi += (12 * octave);
  184. midi += alter;
  185. *midiPitchRef = midi;
  186. return rc;
  187. }
  188. unsigned _cmXScoreParseNoteRValue( cmXScore_t* p, const cmXmlNode_t* nnp, const cmChar_t* label )
  189. {
  190. typedef struct map_str
  191. {
  192. unsigned rvalue;
  193. const cmChar_t* label;
  194. } map_t;
  195. map_t mapV[] =
  196. {
  197. { -1, "measure" }, // whole measure rest
  198. { 1, "whole" },
  199. { 2, "half" },
  200. { 4, "quarter" },
  201. { 8, "eighth" },
  202. { 16, "16th" },
  203. { 32, "32nd" },
  204. { 64, "64th" },
  205. { 128, "128th" },
  206. { 0, "" }
  207. };
  208. const cmChar_t* str;
  209. if((str = cmXmlNodeValue(nnp,label,NULL)) == NULL)
  210. {
  211. if((nnp = cmXmlSearch(nnp,"rest",NULL,0)) != NULL )
  212. {
  213. const cmXmlAttr_t* a;
  214. if((a = cmXmlFindAttrib(nnp,"measure")) != NULL && cmTextCmp(a->value,"yes")==0)
  215. return -1;
  216. }
  217. return 0;
  218. }
  219. unsigned i;
  220. for(i=0; mapV[i].rvalue!=0; ++i)
  221. if( cmTextCmp(mapV[i].label,str) == 0 )
  222. return mapV[i].rvalue;
  223. return 0;
  224. }
  225. cmXsRC_t _cmXScoreParseColor( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* note )
  226. {
  227. cmXsRC_t rc = kOkXsRC;
  228. const cmXmlAttr_t* a;
  229. typedef struct map_str
  230. {
  231. unsigned value;
  232. const cmChar_t* label;
  233. } map_t;
  234. map_t mapV[] =
  235. {
  236. { kEvenXsFl, "#0000FF" },
  237. { kTempoXsFl, "#00FF00" },
  238. { kDynXsFl, "#FF0000" },
  239. { kTempoXsFl | kEvenXsFl, "#00FFFF" },
  240. { kDynXsFl | kEvenXsFl, "#FF00FF" },
  241. { kDynXsFl | kTempoXsFl, "#FF7F00" },
  242. { kTempoXsFl | kEvenXsFl | kDynXsFl, "#996633" },
  243. { 0, "" }
  244. };
  245. if((a = cmXmlFindAttrib(nnp, "color" )) != NULL )
  246. {
  247. unsigned i;
  248. for(i=0; mapV[i].value != 0; ++i)
  249. if( cmTextCmp(a->value,mapV[i].label) == 0 )
  250. {
  251. note->flags += mapV[i].value;
  252. break;
  253. }
  254. if( mapV[i].value == 0 )
  255. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"The note color '%s' was not found.",a->value);
  256. }
  257. return rc;
  258. }
  259. cmXsRC_t _cmXScoreParseNote(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* nnp, unsigned* tickRef )
  260. {
  261. cmXsRC_t rc = kOkXsRC;
  262. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  263. unsigned voiceId;
  264. // get the voice id for this node
  265. if( cmXmlNodeUInt(nnp,&voiceId,"voice",NULL) != kOkXmlRC )
  266. return _cmXScoreMissingNode(p,nnp,"voice");
  267. // if this note has a pitch
  268. if( cmXmlNodeHasChild(nnp,"pitch",NULL) )
  269. if((rc = _cmXScoreParsePitch(p,nnp,&note->pitch)) != kOkXsRC )
  270. return rc;
  271. // get the note duration
  272. cmXmlNodeUInt(nnp,&note->duration,"duration",NULL);
  273. // is 'rest'
  274. if( cmXmlNodeHasChild(nnp,"rest",NULL) )
  275. note->flags |= kRestXsFl;
  276. // is 'grace'
  277. if( cmXmlNodeHasChild(nnp,"grace",NULL) )
  278. note->flags |= kGraceXsFl;
  279. // is 'dot'
  280. if( cmXmlNodeHasChild(nnp,"dot",NULL) )
  281. note->flags |= kDotXsFl;
  282. // is 'chord'
  283. if( cmXmlNodeHasChild(nnp,"chord",NULL) )
  284. note->flags |= kChordXsFl;
  285. // set color coded flags
  286. if((rc = _cmXScoreParseColor(p, nnp, note )) != kOkXsRC )
  287. return rc;
  288. // get the note's rythmic value
  289. if((note->rvalue = _cmXScoreParseNoteRValue(p,nnp,"type")) == 0 )
  290. return _cmXScoreMissingNode(p,nnp,"type");
  291. note->tick = *tickRef;
  292. if( cmIsNotFlag(note->flags,kChordXsFl) )
  293. *tickRef += note->duration;
  294. return _cmXScorePushNote(p, meas, voiceId, note );
  295. }
  296. cmXsRC_t _cmXScorePushNonNote( cmXScore_t* p, cmXsMeas_t* meas, unsigned tick, unsigned duration, unsigned rvalue, const cmChar_t* tvalue, unsigned flags )
  297. {
  298. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  299. unsigned voiceId = 0; // non-note's are always assigned to voiceId=0;
  300. note->tick = tick;
  301. note->flags = flags;
  302. note->rvalue = rvalue;
  303. note->tvalue = tvalue;
  304. note->duration = duration;
  305. return _cmXScorePushNote(p, meas, voiceId, note );
  306. }
  307. cmXsRC_t _cmXScoreParseDirection(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* dnp, unsigned tick)
  308. {
  309. const cmXmlNode_t* np = NULL;
  310. const cmXmlAttr_t* a = NULL;
  311. unsigned flags = 0;
  312. int offset = 0;
  313. unsigned rvalue = 0;
  314. const cmChar_t* tvalue = NULL;
  315. unsigned duration = 0;
  316. cmXmlNodeInt(dnp, &offset, "offset", NULL );
  317. // if this is a metronome direction
  318. if((np = cmXmlSearch( dnp, "metronome", NULL, 0)) != NULL )
  319. {
  320. if( cmXmlNodeUInt(np,&duration,"per-minute",NULL) != kOkXmlRC )
  321. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'per-minute' metronome value is missing on line %i.",np->line);
  322. if((rvalue = _cmXScoreParseNoteRValue(p,np,"beat-unit")) == 0 )
  323. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'beat-unit' metronome value is missing on line %i.",np->line);
  324. flags = kMetronomeXsFl;
  325. }
  326. else
  327. // if this is a pedal direction
  328. if((np = cmXmlSearch( dnp, "pedal",NULL,0)) != NULL )
  329. {
  330. if((a = cmXmlFindAttrib(np,"type")) == NULL )
  331. return _cmXScoreMissingAttribute(p, np, "type" );
  332. if( cmTextCmp(a->value,"start") == 0 )
  333. flags = kPedalDnXsFl;
  334. else
  335. if( cmTextCmp(a->value,"change") == 0 )
  336. flags = kPedalUpDnXsFl;
  337. else
  338. if( cmTextCmp(a->value,"stop") == 0 )
  339. flags = kPedalUpXsFl;
  340. else
  341. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unrecognized pedal type:'%s'.",cmStringNullGuard(a->value));
  342. }
  343. else
  344. // if this is a 'words' direction
  345. if((np = cmXmlSearch( dnp, "words", NULL, 0)) != NULL )
  346. {
  347. if((a = cmXmlFindAttrib(np,"enclosure")) != NULL && cmTextCmp(a->value,"rectangle")==0 )
  348. {
  349. if( cmTextIsEmpty( tvalue = np->dataStr ) )
  350. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Section number is blank or missing on line %i.",np->line);
  351. flags = kSectionXsFl;
  352. }
  353. }
  354. return _cmXScorePushNonNote(p,meas,tick+offset,duration,rvalue,tvalue,flags);
  355. }
  356. cmXsRC_t _cmXScoreParseMeasure(cmXScore_t* p, cmXsPart_t* pp, const cmXmlNode_t* mnp)
  357. {
  358. cmXsRC_t rc = kOkXsRC;
  359. const cmXmlNode_t* np = NULL;
  360. unsigned tick = 0;
  361. // allocate the 'measure' record
  362. cmXsMeas_t* meas = cmLhAllocZ(p->lhH,cmXsMeas_t,1);
  363. // get measure number
  364. if( cmXmlAttrUInt(mnp,"number", &meas->number) != kOkXmlRC )
  365. return _cmXScoreMissingAttribute(p,mnp,"number");
  366. if( pp->measL == NULL )
  367. pp->measL = meas;
  368. else
  369. {
  370. cmXsMeas_t* m = pp->measL;
  371. while( m->link != NULL )
  372. m = m->link;
  373. m->link = meas;
  374. }
  375. // get measure attributes node
  376. if((np = cmXmlSearch(mnp,"attributes",NULL,0)) != NULL)
  377. {
  378. cmXmlNodeUInt(np,&meas->divisions,"divisions",NULL);
  379. cmXmlNodeUInt(np,&meas->beats, "time","beats",NULL);
  380. cmXmlNodeUInt(np,&meas->beat_type,"time","beat-type",NULL);
  381. }
  382. // store the bar line
  383. if((rc = _cmXScorePushNonNote(p,meas,tick,0,0,NULL,kBarXsFl)) != kOkXsRC )
  384. return rc;
  385. np = mnp->children;
  386. // for each child of the 'meas' XML node
  387. for(; rc==kOkXsRC && np!=NULL; np=np->sibling)
  388. {
  389. // if this is a 'note' node
  390. if( cmTextCmp(np->label,"note") == 0 )
  391. {
  392. rc = _cmXScoreParseNote(p,meas,np,&tick);
  393. }
  394. else
  395. // if this is a 'backup' node
  396. if( cmTextCmp(np->label,"backup") == 0 )
  397. {
  398. unsigned backup;
  399. cmXmlNodeUInt(np,&backup,"duration",NULL);
  400. tick -= backup;
  401. }
  402. else
  403. // if this is a 'direction' node
  404. if( cmTextCmp(np->label,"direction") == 0 )
  405. {
  406. rc = _cmXScoreParseDirection(p,meas,np,tick);
  407. }
  408. }
  409. return rc;
  410. }
  411. cmXsRC_t _cmXScoreParsePart( cmXScore_t* p, cmXsPart_t* pp )
  412. {
  413. cmXsRC_t rc = kOkXsRC;
  414. const cmXmlNode_t* xnp;
  415. cmXmlAttr_t partAttr = { "id", pp->idStr };
  416. // find the 'part'
  417. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part", &partAttr, 1)) == NULL )
  418. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The part '%s' was not found.",pp->idStr);
  419. // for each child of this part - find each measure
  420. const cmXmlNode_t* cnp = xnp->children;
  421. for(; cnp!=NULL; cnp=cnp->sibling)
  422. if( cmTextCmp(cnp->label,"measure") == 0 )
  423. if((rc = _cmXScoreParseMeasure(p,pp,cnp)) != kOkXsRC )
  424. return rc;
  425. return rc;
  426. }
  427. cmXsNote_t* _cmXScoreInsertSortedNote( cmXsNote_t* s0, cmXsNote_t* np )
  428. {
  429. if( s0 == NULL )
  430. return np;
  431. if( np->tick < s0->tick )
  432. {
  433. np->slink = s0;
  434. return np;
  435. }
  436. cmXsNote_t* s1 = s0;
  437. cmXsNote_t* s2 = s0->slink;
  438. while( s2 != NULL )
  439. {
  440. if( s2->tick > np->tick )
  441. {
  442. s1->slink = np;
  443. np->slink = s2;
  444. return s0;
  445. }
  446. s1 = s2;
  447. s2 = s2->slink;
  448. }
  449. s1->slink = np;
  450. return s0;
  451. }
  452. void _cmXScoreSort( cmXScore_t* p )
  453. {
  454. cmXsPart_t* pp = p->partL;
  455. for(; pp!=NULL; pp=pp->link)
  456. {
  457. cmXsMeas_t* mp = pp->measL;
  458. for(; mp!=NULL; mp=mp->link)
  459. {
  460. // for each voice in this measure
  461. cmXsVoice_t* vp = mp->voiceL;
  462. for(; vp!=NULL; vp=vp->link)
  463. {
  464. cmXsNote_t* np = vp->noteL;
  465. for(; np!=NULL; np=np->mlink)
  466. mp->noteL = _cmXScoreInsertSortedNote(mp->noteL,np);
  467. }
  468. }
  469. }
  470. }
  471. cmXsRC_t cmXScoreInitialize( cmCtx_t* ctx, cmXsH_t* hp, const cmChar_t* xmlFn )
  472. {
  473. cmXsRC_t rc = kOkXsRC;
  474. if((rc = cmXScoreFinalize(hp)) != kOkXsRC )
  475. return rc;
  476. cmXScore_t* p = cmMemAllocZ(cmXScore_t,1);
  477. cmErrSetup(&p->err,&ctx->rpt,"XScore");
  478. // create a local linked heap
  479. if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8196,ctx)) == false )
  480. return cmErrMsg(&p->err,kLHeapFailXsRC,"Lheap create failed.");
  481. // open the music xml file
  482. if( cmXmlAlloc(ctx, &p->xmlH, xmlFn) != kOkXmlRC )
  483. {
  484. rc = cmErrMsg(&p->err,kXmlFailXsRC,"Unable to open the MusicXML file '%s'.",cmStringNullGuard(xmlFn));
  485. goto errLabel;
  486. }
  487. //cmXmlPrint(p->xmlH,&ctx->rpt);
  488. // parse the part-list
  489. if((rc = _cmXScoreParsePartList( p )) != kOkXsRC )
  490. goto errLabel;
  491. // parse each score 'part'
  492. cmXsPart_t* pp = p->partL;
  493. for(; pp!=NULL; pp=pp->link)
  494. if((rc = _cmXScoreParsePart(p,pp)) != kOkXsRC )
  495. goto errLabel;
  496. // fill in the note->slink chain to link the notes in each measure in time order
  497. _cmXScoreSort(p);
  498. // CSV output initialize failed.
  499. if( cmCsvInitialize(&p->csvH,ctx) != kOkCsvRC )
  500. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output object create failed.");
  501. errLabel:
  502. if( rc != kOkXsRC )
  503. _cmXScoreFinalize(p);
  504. else
  505. hp->h = p;
  506. return rc;
  507. }
  508. cmXsRC_t cmXScoreFinalize( cmXsH_t* hp )
  509. {
  510. cmXsRC_t rc = kOkXsRC;
  511. if( hp == NULL || cmXScoreIsValid(*hp)==false )
  512. return kOkXsRC;
  513. cmXScore_t* p = _cmXScoreHandleToPtr(*hp);
  514. if((rc = _cmXScoreFinalize(p)) != kOkXsRC )
  515. return rc;
  516. hp->h = NULL;
  517. return rc;
  518. }
  519. bool cmXScoreIsValid( cmXsH_t h )
  520. { return h.h != NULL; }
  521. void _cmXScoreReportNote( cmRpt_t* rpt, const cmXsNote_t* note )
  522. {
  523. const cmChar_t* B = cmIsFlag(note->flags,kBarXsFl) ? "|" : "-";
  524. const cmChar_t* R = cmIsFlag(note->flags,kRestXsFl) ? "R" : "-";
  525. const cmChar_t* G = cmIsFlag(note->flags,kGraceXsFl) ? "G" : "-";
  526. const cmChar_t* D = cmIsFlag(note->flags,kDotXsFl) ? "D" : "-";
  527. const cmChar_t* C = cmIsFlag(note->flags,kChordXsFl) ? "C" : "-";
  528. const cmChar_t* e = cmIsFlag(note->flags,kEvenXsFl) ? "e" : "-";
  529. const cmChar_t* d = cmIsFlag(note->flags,kDynXsFl) ? "d" : "-";
  530. const cmChar_t* t = cmIsFlag(note->flags,kTempoXsFl) ? "t" : "-";
  531. const cmChar_t* P = cmIsFlag(note->flags,kPedalDnXsFl) ? "V" : "-";
  532. P = cmIsFlag(note->flags,kPedalUpXsFl) ? "^" : P;
  533. P = cmIsFlag(note->flags,kPedalUpDnXsFl) ? "X" : P;
  534. const cmChar_t* N = note->pitch==0 ? " " : cmMidiToSciPitch( note->pitch, NULL, 0 );
  535. cmRptPrintf(rpt," %5i %5i %2i %3s %s%s%s%s%s%s%s%s%s",note->tick,note->duration,note->rvalue,N,B,R,G,D,C,e,d,t,P);
  536. if( cmIsFlag(note->flags,kSectionXsFl) )
  537. cmRptPrintf(rpt," %i",cmStringNullGuard(note->tvalue));
  538. printf("\n");
  539. }
  540. /*
  541. kMidiFileIdColScIdx= 0,
  542. kTypeLabelColScIdx = 3,
  543. kDSecsColScIdx = 4,
  544. kSecsColScIdx = 5,
  545. kD0ColScIdx = 9,
  546. kD1ColScIdx = 10,
  547. kPitchColScIdx = 11,
  548. kBarColScIdx = 13,
  549. kSkipColScIdx = 14,
  550. kEvenColScIdx = 15,
  551. kGraceColScIdx = 16,
  552. kTempoColScIdx = 17,
  553. kFracColScIdx = 18,
  554. kDynColScIdx = 19,
  555. kSectionColScIdx = 20,
  556. kRecdPlayColScIdx = 21,
  557. kRemarkColScIdx = 22
  558. */
  559. cmXsRC_t _cmXScoreWriteCsvHdr( cmXScore_t* p )
  560. {
  561. const cmChar_t* s[] =
  562. {
  563. "id","trk","evt","opcode","dticks","micros","status",
  564. "meta","ch","d0","d1","arg0","arg1","bar","skip",
  565. "even","grace","tempo","t frac","dyn","section","play_recd","remark",NULL
  566. };
  567. cmCsvCell_t* lcp = NULL;
  568. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymText(p->csvH,s[0]), 0, 0 ) != kOkCsvRC )
  569. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  570. unsigned i;
  571. for(i=1; s[i]!=NULL; ++i)
  572. {
  573. if( cmCsvInsertTextColAfter(p->csvH, lcp, &lcp, s[i], 0 ) != kOkCsvRC )
  574. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV error inserting CSV title %i.\n",i);
  575. }
  576. return kOkXsRC;
  577. }
  578. cmXsRC_t _cmXScoreWriteCsvBlankCols( cmXScore_t* p, unsigned cnt, cmCsvCell_t** leftCellPtrPtr )
  579. {
  580. unsigned i;
  581. for(i=0; i<cnt; ++i)
  582. if( cmCsvInsertTextColAfter(p->csvH,*leftCellPtrPtr,leftCellPtrPtr,0,0) != kOkCsvRC )
  583. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV output failed on blank column.");
  584. return kOkCsvRC;
  585. }
  586. cmXsRC_t _cmXScoreWriteCsvRow(
  587. cmXScore_t* p,
  588. unsigned rowIdx,
  589. unsigned bar,
  590. const cmChar_t* sectionStr,
  591. const cmChar_t* opCodeStr,
  592. double dsecs,
  593. double secs,
  594. unsigned d0,
  595. unsigned d1,
  596. unsigned pitch,
  597. double frac,
  598. unsigned flags )
  599. {
  600. cmXsRC_t rc = kOkXsRC;
  601. cmCsvCell_t* lcp = NULL;
  602. // append an empty row to the CSV object
  603. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymUInt(p->csvH, rowIdx ), 0, 0 ) != kOkCsvRC )
  604. {
  605. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  606. goto errLabel;
  607. }
  608. /*
  609. // col 0 : blanks
  610. if( cmCsvInsertUIntColAfter(p->csvH, lcp, &lcp, rowIdx, 0 ) != kOkCsvRC )
  611. {
  612. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output row index failed.");
  613. goto errLabel;
  614. }
  615. */
  616. // cols 1,2
  617. if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
  618. goto errLabel;
  619. // col 3 : output the opcode
  620. if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,opCodeStr,0) != kOkCsvRC )
  621. {
  622. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on opcode label.");
  623. goto errLabel;
  624. }
  625. // col 4 : dsecs
  626. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,dsecs,0) != kOkCsvRC )
  627. {
  628. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dsecs'.");
  629. goto errLabel;
  630. }
  631. // col 5 : secs
  632. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,secs,0) != kOkCsvRC )
  633. {
  634. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'secs'.");
  635. goto errLabel;
  636. }
  637. // cols 6,7,8 blanks
  638. if((rc = _cmXScoreWriteCsvBlankCols(p,3,&lcp)) != kOkXsRC )
  639. goto errLabel;
  640. // col 9 : d0
  641. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d0,0) != kOkCsvRC )
  642. {
  643. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  644. goto errLabel;
  645. }
  646. // col 10 : d1
  647. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d1,0) != kOkCsvRC )
  648. {
  649. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd1'.");
  650. goto errLabel;
  651. }
  652. // col 11 : pitch
  653. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,pitch,0) != kOkCsvRC )
  654. {
  655. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  656. goto errLabel;
  657. }
  658. // col 12 : blanks
  659. if((rc = _cmXScoreWriteCsvBlankCols(p,1 + (cmIsFlag(flags,kBarXsFl) ? 0 : 1), &lcp)) != kOkXsRC )
  660. goto errLabel;
  661. // col 13 : bar number
  662. if( cmIsFlag(flags,kBarXsFl) )
  663. {
  664. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,bar,0) != kOkCsvRC )
  665. {
  666. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  667. goto errLabel;
  668. }
  669. }
  670. // col 14 : skip (blank for now)
  671. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  672. goto errLabel;
  673. // col 15: even
  674. if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kEvenXsFl) ? "e" : "",0) != kOkCsvRC )
  675. {
  676. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  677. goto errLabel;
  678. }
  679. // col 16: grace
  680. if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kGraceXsFl) ? "g" : "",0) != kOkCsvRC )
  681. {
  682. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  683. goto errLabel;
  684. }
  685. // col 17: tempo
  686. if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kTempoXsFl) ? "t" : "",0) != kOkCsvRC )
  687. {
  688. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  689. goto errLabel;
  690. }
  691. // col 18: frac
  692. if( frac == 0 )
  693. {
  694. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  695. goto errLabel;
  696. }
  697. else
  698. {
  699. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,1.0/frac,0) != kOkCsvRC )
  700. {
  701. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 't frac'.");
  702. goto errLabel;
  703. }
  704. }
  705. // col 19: dynamic marking (blank for now)
  706. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  707. goto errLabel;
  708. // col 20: section
  709. if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kSectionXsFl) ? sectionStr : "",0) != kOkCsvRC )
  710. {
  711. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  712. goto errLabel;
  713. }
  714. // col 21, 22 : recd-play, remark (blank for now)
  715. if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
  716. goto errLabel;
  717. errLabel:
  718. return rc;
  719. }
  720. cmXsRC_t cmXScoreWriteCsv( cmXsH_t h, const cmChar_t* csvFn )
  721. {
  722. cmXsRC_t rc = kOkXsRC;
  723. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  724. unsigned rowIdx = 1;
  725. double tpqn = 0; // ticks per quarter note
  726. double tps = 0; // ticks per second
  727. double sec = 0; // current time in seconds
  728. const cmChar_t* sectionIdStr = NULL;
  729. if( !cmCsvIsValid(p->csvH) )
  730. return cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output object is not initialized.");
  731. if((rc = _cmXScoreWriteCsvHdr( p )) != kOkXsRC )
  732. goto errLabel;
  733. cmXsPart_t* pp = p->partL;
  734. for(; pp!=NULL; pp=pp->link)
  735. {
  736. cmXsMeas_t* mp = pp->measL;
  737. for(; mp!=NULL; mp=mp->link)
  738. {
  739. if( mp->divisions != 0 )
  740. tpqn = mp->divisions;
  741. cmXsNote_t* np = mp->noteL;
  742. double sec0 = sec;
  743. for(; np!=NULL; np=np->slink)
  744. {
  745. //
  746. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  747. {
  748. double bpm = np->duration;
  749. double bps = bpm / 60.0;
  750. tps = bps * tpqn;
  751. }
  752. double meas_sec = tps == 0 ? 0 : np->tick / tps;
  753. double sec1 = sec + meas_sec;
  754. double dsecs = sec1 - sec0;
  755. sec0 = sec1;
  756. // if this is a section event
  757. if( cmIsFlag(np->flags,kSectionXsFl) )
  758. sectionIdStr = np->tvalue;
  759. // if this is a bar event
  760. if( cmIsFlag(np->flags,kBarXsFl) )
  761. {
  762. _cmXScoreWriteCsvRow(p,rowIdx,mp->number,NULL,"bar",dsecs,sec1,0,0,0,0,np->flags);
  763. }
  764. else
  765. // if this is a pedal event
  766. if( cmIsFlag(np->flags,kPedalDnXsFl|kPedalUpXsFl|kPedalUpDnXsFl) )
  767. {
  768. unsigned d0 = 64; // pedal MIDI ctl id
  769. unsigned d1 = cmIsFlag(np->flags,kPedalDnXsFl) ? 64 : 0; // pedal-dn: d1>=64 pedal-up:<64
  770. _cmXScoreWriteCsvRow(p,rowIdx,mp->number,NULL,"ctl",dsecs,sec1,d0,d1,0,0,np->flags);
  771. }
  772. else
  773. // if this is a sounding note event
  774. if( cmIsNotFlag(np->flags,kRestXsFl) )
  775. {
  776. double frac = np->rvalue + (cmIsFlag(np->flags,kDotXsFl) ? (np->rvalue/2) : 0);
  777. //
  778. _cmXScoreWriteCsvRow(p,rowIdx,mp->number,sectionIdStr,"non",dsecs,sec1,0,0,np->pitch,frac,np->flags);
  779. sectionIdStr = NULL;
  780. }
  781. rowIdx += 1;
  782. }
  783. sec = sec0;
  784. }
  785. }
  786. if( cmCsvWrite( p->csvH, csvFn ) != kOkCsvRC )
  787. rc = cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output write failed on file '%s'.",csvFn);
  788. errLabel:
  789. return rc;
  790. }
  791. void cmXScoreReport( cmXsH_t h, cmRpt_t* rpt, bool sortFl )
  792. {
  793. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  794. cmXsPart_t* pp = p->partL;
  795. for(; pp!=NULL; pp=pp->link)
  796. {
  797. cmRptPrintf(rpt,"Part:%s\n",pp->idStr);
  798. const cmXsMeas_t* meas = pp->measL;
  799. for(; meas!=NULL; meas=meas->link)
  800. {
  801. cmRptPrintf(rpt," %i : div:%i beat:%i beat-type:%i\n",meas->number,meas->divisions,meas->beats,meas->beat_type);
  802. if( sortFl )
  803. {
  804. const cmXsNote_t* note = meas->noteL;
  805. for(; note!=NULL; note=note->slink)
  806. _cmXScoreReportNote(rpt,note);
  807. }
  808. else
  809. {
  810. const cmXsVoice_t* v = meas->voiceL;
  811. for(; v!=NULL; v=v->link)
  812. {
  813. const cmXsNote_t* note = v->noteL;
  814. cmRptPrintf(rpt," voice:%i\n",v->id);
  815. for(; note!=NULL; note=note->mlink)
  816. _cmXScoreReportNote(rpt,note);
  817. }
  818. }
  819. }
  820. }
  821. }
  822. cmXsRC_t cmXScoreTest( cmCtx_t* ctx, const cmChar_t* fn )
  823. {
  824. cmXsRC_t rc;
  825. cmXsH_t h = cmXsNullHandle;
  826. if((rc = cmXScoreInitialize( ctx, &h, fn)) != kOkXsRC )
  827. return cmErrMsg(&ctx->err,rc,"XScore alloc failed.");
  828. cmXScoreWriteCsv(h,"/home/kevin/temp/a0.csv");
  829. cmXScoreReport(h,&ctx->rpt,true);
  830. return cmXScoreFinalize(&h);
  831. }