libcm is a C development framework with an emphasis on audio signal processing applications.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cmXScore.c 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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* n0p, bool rptFl )
  639. {
  640. cmXsNote_t* nnp = n0p->slink; // begin w/ note following np
  641. unsigned measNumb = mp->number;
  642. cmChar_t acc = n0p->alter==-1?'b' : (n0p->alter==1?'#':' ');
  643. if( rptFl )
  644. printf("%i %i %s ",n0p->meas->number,n0p->tick,cmMidiToSciPitch(n0p->pitch,NULL,0));
  645. while(1)
  646. {
  647. // if we are at the end of a measure advance to the next measure
  648. if( nnp == NULL )
  649. {
  650. mp = mp->link;
  651. nnp = mp->noteL;
  652. // if a measure was completed and no end note was found ... then the tie is unterminated
  653. // (a tie must be continued in every measure which it passes through)
  654. if( mp->number > measNumb + 1 )
  655. break;
  656. }
  657. // for each note starting at nnp
  658. for(; nnp!=NULL; nnp=nnp->slink)
  659. {
  660. // if this note is tied to the originating note (np)
  661. if( nnp->voice->id == n0p->voice->id && nnp->step == n0p->step && nnp->octave == n0p->octave )
  662. {
  663. nnp->flags |= kTieProcXsFl;
  664. nnp->flags = cmClrFlag(nnp->flags,kOnsetXsFl);
  665. if( rptFl )
  666. printf("---> %i %i %s ",nnp->meas->number,nnp->tick,cmMidiToSciPitch(nnp->pitch,NULL,0));
  667. // if this note is not tied to a subsequent note
  668. if( cmIsNotFlag(nnp->flags,kTieBegXsFl) )
  669. return true;
  670. // record the measure number of the last note with a tie-start
  671. measNumb = mp->number;
  672. }
  673. }
  674. }
  675. cmErrWarnMsg(&p->err,kUnterminatedTieXsRC,"The tied %c%c%i in measure %i was not terminated.",n0p->step,acc,n0p->octave,measNumb);
  676. return false;
  677. }
  678. void _cmXScoreResolveTiesAndLoc( cmXScore_t* p )
  679. {
  680. unsigned n = 0;
  681. unsigned m = 0;
  682. bool rptFl = false;
  683. cmXsPart_t* pp = p->partL;
  684. // for each part
  685. for(; pp!=NULL; pp=pp->link)
  686. {
  687. unsigned locIdx = 1;
  688. cmXsMeas_t* mp = pp->measL;
  689. // for each measure
  690. for(; mp!=NULL; mp=mp->link)
  691. {
  692. cmXsNote_t* n0 = NULL;
  693. cmXsNote_t* np = mp->noteL;
  694. // for each note in this measure
  695. for(; np!=NULL; np=np->slink)
  696. {
  697. // if this note begins a tie and has not yet been processed
  698. // (A note that continues a tie and therefore has a kTieBegXsFl set
  699. // may have already been processed by an earlier tied note.)
  700. if( cmIsFlag(np->flags,kTieBegXsFl) && cmIsNotFlag(np->flags,kTieProcXsFl))
  701. {
  702. if( _cmXScoreFindTiedNote(p,mp,np,rptFl) )
  703. m += 1;
  704. if( rptFl )
  705. printf("\n");
  706. n += 1;
  707. }
  708. if( cmIsFlag(np->flags,kTieEndXsFl) && cmIsFlag(np->flags,kOnsetXsFl) )
  709. {
  710. cmChar_t acc = np->alter==-1?'b' : (np->alter==1?'#':' ');
  711. 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);
  712. }
  713. // set the location
  714. if( cmIsFlag(np->flags,kOnsetXsFl|kBarXsFl) )
  715. {
  716. if( cmIsFlag(np->flags,kBarXsFl) || (n0!=NULL && n0->tick!=np->tick))
  717. locIdx += 1;
  718. np->locIdx = locIdx;
  719. n0 = np;
  720. }
  721. }
  722. }
  723. }
  724. printf("Found:%i Not Found:%i\n",m,n-m);
  725. }
  726. cmXsRC_t _cmXScoreResolveOctaveShift( cmXScore_t* p )
  727. {
  728. const cmXsSpan_t* s;
  729. for(s=p->spanL; s!=NULL; s=s->link)
  730. {
  731. if( s->tick1 == -1)
  732. {
  733. cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"An unterminated octave shift span was encountered in measure %i staff=%i.",s->meas->number,s->staff);
  734. }
  735. else
  736. {
  737. cmXsMeas_t* m = p->partL->measL;
  738. for(; m!=NULL; m=m->link)
  739. if( m->number == s->meas->number )
  740. break;
  741. assert( m != NULL );
  742. cmXsNote_t* note = m->noteL;
  743. for(; note!=NULL; note=note->slink)
  744. if( note->staff==s->staff && s->tick0 <= note->tick && note->tick < s->tick1 )
  745. note->pitch += s->pitch_offset;
  746. }
  747. }
  748. return kOkXsRC;
  749. }
  750. // The identical pitch may be notated to play simultaneously on different voices.
  751. // As performed on the piano this will equate to a single sounding note.
  752. // This function clears the onset flag on all except one of the duplicated notes.
  753. void _cmXScoreRemoveDuplicateNotes( cmXScore_t* p )
  754. {
  755. cmXsPart_t* pp = p->partL;
  756. // for each part
  757. for(; pp!=NULL; pp=pp->link)
  758. {
  759. cmXsMeas_t* mp = pp->measL;
  760. // for each measure
  761. for(; mp!=NULL; mp=mp->link)
  762. {
  763. cmXsNote_t* np = mp->noteL;
  764. // for each note in this measure
  765. for(; np!=NULL; np=np->slink)
  766. if( cmIsFlag(np->flags,kOnsetXsFl) )
  767. {
  768. cmXsNote_t* n0p = mp->noteL;
  769. for(; n0p!=NULL; n0p=n0p->slink)
  770. if( n0p!=np && cmIsFlag(n0p->flags,kOnsetXsFl) && np->locIdx==n0p->locIdx && np->pitch==n0p->pitch )
  771. n0p->flags = cmClrFlag(n0p->flags,kOnsetXsFl);
  772. }
  773. }
  774. }
  775. }
  776. cmXsMeas_t* _cmXScoreNextNonEmptyMeas( cmXsPart_t* pp, cmXsMeas_t* meas )
  777. {
  778. if( meas == NULL )
  779. {
  780. if( pp==NULL || pp->measL==NULL )
  781. return NULL;
  782. meas = pp->measL;
  783. }
  784. else
  785. {
  786. meas = meas->link;
  787. }
  788. while( meas != NULL && meas->noteL == NULL )
  789. meas=meas->link;
  790. return meas;
  791. }
  792. cmXsNote_t* _cmXScoreNextNote( cmXsPart_t* pp, cmXsNote_t* note )
  793. {
  794. // meas should always be valid (unless this is the first note in the score)
  795. cmXsMeas_t* meas = note==NULL ? NULL : note->meas;
  796. do
  797. {
  798. if( note == NULL || note->slink==NULL )
  799. {
  800. if((meas = _cmXScoreNextNonEmptyMeas(pp,meas)) == NULL)
  801. return NULL;
  802. assert( meas->noteL != NULL );
  803. note = meas->noteL;
  804. }
  805. else
  806. {
  807. note = note->slink;
  808. }
  809. assert( note != NULL );
  810. meas = note->meas;
  811. // note is now set to a non-NULL candidate note - advance to a sounding note
  812. while( note!=NULL && cmIsNotFlag(note->flags,kOnsetXsFl) )
  813. note = note->slink;
  814. // if no note was found in this measure
  815. }while( note == NULL );
  816. return note;
  817. }
  818. cmXsRC_t _cmXScoreWriteScorePlotFile( cmXScore_t* p, const cmChar_t* fn )
  819. {
  820. cmXsRC_t rc = kOkXsRC;
  821. cmFileH_t fH = cmFileNullHandle;
  822. double ticks_per_sec = 0;
  823. double onset_secs = 0;
  824. if( cmFileOpen(&fH,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  825. return cmErrMsg(&p->err,kFileFailXsRC,"Unable to create the file '%s'.",cmStringNullGuard(fn));
  826. cmXsPart_t* pp = p->partL;
  827. for(; pp!=NULL; pp=pp->link)
  828. {
  829. cmXsMeas_t* mp = pp->measL;
  830. for(; mp!=NULL; mp=mp->link)
  831. {
  832. cmFilePrintf(fH,"b %f %i %s B\n",onset_secs,mp->number,"bar");
  833. cmXsNote_t* np = mp->noteL;
  834. unsigned tick0 = 0;
  835. for(; np!=NULL; np=np->slink)
  836. {
  837. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  838. {
  839. double bps = np->duration / 60.0;
  840. // t b t
  841. // - = - -
  842. // s s b
  843. ticks_per_sec = bps * mp->divisions;
  844. }
  845. else
  846. {
  847. if( cmIsFlag(np->flags,kOnsetXsFl) )
  848. {
  849. onset_secs += (np->tick - tick0) / ticks_per_sec;
  850. tick0 = np->tick;
  851. 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");
  852. }
  853. }
  854. }
  855. onset_secs += (mp->divisions * mp->beats - tick0) / ticks_per_sec;
  856. }
  857. }
  858. cmFileClose(&fH);
  859. return rc;
  860. }
  861. cmXsRC_t _cmXScoreWriteMidiPlotFile( cmXScore_t* p, cmChar_t* fn, const cmMidiTrackMsg_t** m, unsigned mN )
  862. {
  863. cmXsRC_t rc = kOkXsRC;
  864. cmFileH_t fH = cmFileNullHandle;
  865. unsigned i = 0;
  866. if( cmFileOpen(&fH,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  867. return cmErrMsg(&p->err,kFileFailXsRC,"Unable to create the file '%s'.",cmStringNullGuard(fn));
  868. for(i=0; i<mN; ++i)
  869. if( (m[i]!=NULL) && cmMidiIsChStatus(m[i]->status) && cmMidiIsNoteOn(m[i]->status) && (m[i]->u.chMsgPtr->d1>0) )
  870. 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));
  871. cmFileClose(&fH);
  872. return rc;
  873. }
  874. cmXsRC_t _cmXScoreProcessMidi(cmXScore_t* p, cmCtx_t* ctx, const cmChar_t* midiFn)
  875. {
  876. cmXsRC_t rc = kOkXsRC;
  877. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  878. const cmMidiTrackMsg_t** m = NULL;
  879. unsigned mN = 0;
  880. unsigned i = 0;
  881. unsigned j = 0;
  882. cmXsNote_t* note = NULL;
  883. if( cmMidiFileOpen(ctx, &mfH, midiFn ) != kOkMfRC )
  884. return cmErrMsg(&p->err,kMidiFailXsRC,"The MIDI file object could not be opened from '%s'.",cmStringNullGuard(midiFn));
  885. //cmMidiFilePrintMsgs(mfH, p->err.rpt );
  886. if( (m = cmMidiFileMsgArray(mfH)) == NULL || (mN = cmMidiFileMsgCount(mfH)) == 0 )
  887. {
  888. rc = cmErrMsg(&p->err,kMidiFailXsRC,"The MIDI file object appears to be empty.");
  889. goto errLabel;
  890. }
  891. if((note = _cmXScoreNextNote(p->partL,NULL)) == NULL)
  892. {
  893. rc = cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"No MIDI processing to be done. The score appears to be empty.");
  894. goto errLabel;
  895. }
  896. cmCtx* c = cmCtxAlloc( NULL, p->err.rpt, cmLHeapNullHandle, cmSymTblNullHandle );
  897. cmSeqAlign_t* s = cmSeqAlignAlloc(c,NULL);
  898. unsigned offs = 0;
  899. for(; note!=NULL; note=_cmXScoreNextNote(p->partL,note))
  900. {
  901. if( cmIsFlag(note->flags,kGraceXsFl) )
  902. offs += 1;
  903. cmSeqAlignInsert(s,0,note->locIdx+offs,note->pitch);
  904. }
  905. unsigned locIdx = 1;
  906. for(i=0,j=0; i<mN; ++i)
  907. if( (m[i]!=NULL) && cmMidiIsChStatus(m[i]->status) && cmMidiIsNoteOn(m[i]->status) && (m[i]->u.chMsgPtr->d1>0) )
  908. {
  909. if( m[j]->atick != m[i]->atick )
  910. locIdx += 1;
  911. cmSeqAlignInsert(s,1,locIdx,m[i]->u.chMsgPtr->d0);
  912. //printf("%i : %s\n",locIdx,cmMidiToSciPitch(m[i]->u.chMsgPtr->d0,NULL,0));
  913. j = i;
  914. }
  915. cmMidiFileCalcNoteDurations( mfH );
  916. _cmXScoreWriteScorePlotFile(p, "/Users/kevin/temp/score.txt" );
  917. _cmXScoreWriteMidiPlotFile(p, "/Users/kevin/temp/midi.txt", m, mN );
  918. cmSeqAlignReport(s,p->err.rpt);
  919. cmSeqAlignFree(&s);
  920. cmCtxFree(&c);
  921. goto errLabel;
  922. printf(" i j score midi\n");
  923. printf("---- ---- --- ---- --- ----\n");
  924. for(j=0; note!=NULL; note=_cmXScoreNextNote(p->partL,note),++j)
  925. {
  926. unsigned midiPitch = 0;
  927. for(; i<mN; ++i)
  928. if( m[i]!=NULL && cmMidiIsChStatus(m[i]->status) && cmMidiIsNoteOn(m[i]->status) && m[i]->u.chMsgPtr->d1>0 )
  929. {
  930. midiPitch = m[i]->u.chMsgPtr->d0;
  931. ++i;
  932. break;
  933. }
  934. char buf[6];
  935. printf("%4i %4i %3i %4s %3i %4s\n",j,i,
  936. note->pitch,
  937. cmMidiToSciPitch(note->pitch,NULL,0),
  938. midiPitch,
  939. cmMidiToSciPitch(midiPitch,buf,5));
  940. }
  941. errLabel:
  942. cmMidiFileClose(&mfH);
  943. return rc;
  944. }
  945. cmXsRC_t cmXScoreInitialize( cmCtx_t* ctx, cmXsH_t* hp, const cmChar_t* xmlFn, const cmChar_t* midiFn )
  946. {
  947. cmXsRC_t rc = kOkXsRC;
  948. if((rc = cmXScoreFinalize(hp)) != kOkXsRC )
  949. return rc;
  950. cmXScore_t* p = cmMemAllocZ(cmXScore_t,1);
  951. cmErrSetup(&p->err,&ctx->rpt,"XScore");
  952. // create a local linked heap
  953. if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8196,ctx)) == false )
  954. return cmErrMsg(&p->err,kLHeapFailXsRC,"Lheap create failed.");
  955. // open the music xml file
  956. if( cmXmlAlloc(ctx, &p->xmlH, xmlFn) != kOkXmlRC )
  957. {
  958. rc = cmErrMsg(&p->err,kXmlFailXsRC,"Unable to open the MusicXML file '%s'.",cmStringNullGuard(xmlFn));
  959. goto errLabel;
  960. }
  961. //cmXmlPrint(p->xmlH,&ctx->rpt);
  962. // parse the part-list
  963. if((rc = _cmXScoreParsePartList( p )) != kOkXsRC )
  964. goto errLabel;
  965. // parse each score 'part'
  966. cmXsPart_t* pp = p->partL;
  967. for(; pp!=NULL; pp=pp->link)
  968. if((rc = _cmXScoreParsePart(p,pp)) != kOkXsRC )
  969. goto errLabel;
  970. // fill in the note->slink chain to link the notes in each measure in time order
  971. _cmXScoreSort(p);
  972. _cmXScoreResolveTiesAndLoc(p);
  973. _cmXScoreRemoveDuplicateNotes(p);
  974. //_cmXScoreResolveOctaveShift(p);
  975. //if( midiFn != NULL )
  976. // _cmXScoreProcessMidi(p,ctx,midiFn);
  977. // CSV output initialize failed.
  978. if( cmCsvInitialize(&p->csvH,ctx) != kOkCsvRC )
  979. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output object create failed.");
  980. errLabel:
  981. if( rc != kOkXsRC )
  982. _cmXScoreFinalize(p);
  983. else
  984. hp->h = p;
  985. return rc;
  986. }
  987. cmXsRC_t cmXScoreFinalize( cmXsH_t* hp )
  988. {
  989. cmXsRC_t rc = kOkXsRC;
  990. if( hp == NULL || cmXScoreIsValid(*hp)==false )
  991. return kOkXsRC;
  992. cmXScore_t* p = _cmXScoreHandleToPtr(*hp);
  993. if((rc = _cmXScoreFinalize(p)) != kOkXsRC )
  994. return rc;
  995. hp->h = NULL;
  996. return rc;
  997. }
  998. bool cmXScoreIsValid( cmXsH_t h )
  999. { return h.h != NULL; }
  1000. void _cmXScoreReportTitle( cmRpt_t* rpt )
  1001. {
  1002. cmRptPrintf(rpt," voc loc tick durtn rval flags\n");
  1003. cmRptPrintf(rpt," --- ----- ----- ----- ---- --- -------------\n");
  1004. }
  1005. void _cmXScoreReportNote( cmRpt_t* rpt, const cmXsNote_t* note )
  1006. {
  1007. const cmChar_t* B = cmIsFlag(note->flags,kBarXsFl) ? "|" : "-";
  1008. const cmChar_t* R = cmIsFlag(note->flags,kRestXsFl) ? "R" : "-";
  1009. const cmChar_t* G = cmIsFlag(note->flags,kGraceXsFl) ? "G" : "-";
  1010. const cmChar_t* D = cmIsFlag(note->flags,kDotXsFl) ? "D" : "-";
  1011. const cmChar_t* C = cmIsFlag(note->flags,kChordXsFl) ? "C" : "-";
  1012. const cmChar_t* e = cmIsFlag(note->flags,kEvenXsFl) ? "e" : "-";
  1013. const cmChar_t* d = cmIsFlag(note->flags,kDynXsFl) ? "d" : "-";
  1014. const cmChar_t* t = cmIsFlag(note->flags,kTempoXsFl) ? "t" : "-";
  1015. const cmChar_t* P = cmIsFlag(note->flags,kPedalDnXsFl) ? "V" : "-";
  1016. const cmChar_t* S = cmIsFlag(note->flags,kSectionXsFl) ? "S" : "-";
  1017. const cmChar_t* H = cmIsFlag(note->flags,kHeelXsFl) ? "H" : "-";
  1018. const cmChar_t* T0 = cmIsFlag(note->flags,kTieBegXsFl) ? "T" : "-";
  1019. const cmChar_t* T1 = cmIsFlag(note->flags,kTieEndXsFl) ? "_" : "-";
  1020. P = cmIsFlag(note->flags,kPedalUpXsFl) ? "^" : P;
  1021. P = cmIsFlag(note->flags,kPedalUpDnXsFl) ? "X" : P;
  1022. //const cmChar_t* N = note->pitch==0 ? " " : cmMidiToSciPitch( note->pitch, NULL, 0 );
  1023. cmChar_t N[] = {'\0','\0','\0','\0'};
  1024. cmChar_t acc = note->alter==-1?'b':(note->alter==1?'#':' ');
  1025. snprintf(N,4,"%c%c%1i",note->step,acc,note->octave);
  1026. cmRptPrintf(rpt," %3i %5i %5i %5i %4.1f %3s %s%s%s%s%s%s%s%s%s%s%s%s%s",
  1027. note->voice->id,
  1028. note->locIdx,
  1029. note->tick,
  1030. note->duration,
  1031. note->rvalue,
  1032. N,B,R,G,D,C,e,d,t,P,S,H,T0,T1);
  1033. if( cmIsFlag(note->flags,kSectionXsFl) )
  1034. cmRptPrintf(rpt," %s",cmStringNullGuard(note->tvalue));
  1035. if( cmIsFlag(note->flags,kMetronomeXsFl) )
  1036. cmRptPrintf(rpt," %i bpm",note->duration);
  1037. }
  1038. /*
  1039. kMidiFileIdColScIdx= 0,
  1040. kTypeLabelColScIdx = 3,
  1041. kDSecsColScIdx = 4,
  1042. kSecsColScIdx = 5,
  1043. kD0ColScIdx = 9,
  1044. kD1ColScIdx = 10,
  1045. kPitchColScIdx = 11,
  1046. kBarColScIdx = 13,
  1047. kSkipColScIdx = 14,
  1048. kEvenColScIdx = 15,
  1049. kGraceColScIdx = 16,
  1050. kTempoColScIdx = 17,
  1051. kFracColScIdx = 18,
  1052. kDynColScIdx = 19,
  1053. kSectionColScIdx = 20,
  1054. kRecdPlayColScIdx = 21,
  1055. kRemarkColScIdx = 22
  1056. */
  1057. cmXsRC_t _cmXScoreWriteCsvHdr( cmXScore_t* p )
  1058. {
  1059. const cmChar_t* s[] =
  1060. {
  1061. "id","trk","evt","opcode","dticks","micros","status",
  1062. "meta","ch","d0","d1","arg0","arg1","bar","skip",
  1063. "even","grace","tempo","t frac","dyn","section","play_recd","remark",NULL
  1064. };
  1065. cmCsvCell_t* lcp = NULL;
  1066. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymText(p->csvH,s[0]), 0, 0 ) != kOkCsvRC )
  1067. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  1068. unsigned i;
  1069. for(i=1; s[i]!=NULL; ++i)
  1070. {
  1071. if( cmCsvInsertIdentColAfter(p->csvH, lcp, &lcp, s[i], 0 ) != kOkCsvRC )
  1072. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV error inserting CSV title %i.\n",i);
  1073. }
  1074. return kOkXsRC;
  1075. }
  1076. cmXsRC_t _cmXScoreWriteCsvBlankCols( cmXScore_t* p, unsigned cnt, cmCsvCell_t** leftCellPtrPtr )
  1077. {
  1078. unsigned i;
  1079. for(i=0; i<cnt; ++i)
  1080. if( cmCsvInsertIdentColAfter(p->csvH,*leftCellPtrPtr,leftCellPtrPtr,"",0) != kOkCsvRC )
  1081. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV output failed on blank column.");
  1082. return kOkCsvRC;
  1083. }
  1084. cmXsRC_t _cmXScoreWriteCsvRow(
  1085. cmXScore_t* p,
  1086. unsigned rowIdx,
  1087. unsigned uid,
  1088. unsigned bar,
  1089. const cmChar_t* sectionStr,
  1090. const cmChar_t* opCodeStr,
  1091. double dsecs,
  1092. double secs,
  1093. unsigned d0,
  1094. unsigned d1,
  1095. unsigned pitch, // set to -1 if the pitch is not valid
  1096. double frac,
  1097. unsigned flags )
  1098. {
  1099. cmXsRC_t rc = kOkXsRC;
  1100. cmCsvCell_t* lcp = NULL;
  1101. // append an empty row to the CSV object
  1102. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymUInt(p->csvH, rowIdx ), 0, 0 ) != kOkCsvRC )
  1103. {
  1104. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  1105. goto errLabel;
  1106. }
  1107. /*
  1108. // col 0 : blanks
  1109. if( cmCsvInsertUIntColAfter(p->csvH, lcp, &lcp, rowIdx, 0 ) != kOkCsvRC )
  1110. {
  1111. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output row index failed.");
  1112. goto errLabel;
  1113. }
  1114. */
  1115. // col 1 : track (always 1)
  1116. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,1,0) != kOkCsvRC )
  1117. {
  1118. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1119. goto errLabel;
  1120. }
  1121. // col 2 : evt (set to event uid, or blank if uid == -1)
  1122. if( uid == -1 )
  1123. {
  1124. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1125. goto errLabel;
  1126. }
  1127. else
  1128. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,uid,0) != kOkCsvRC )
  1129. {
  1130. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1131. goto errLabel;
  1132. }
  1133. // col 3 : output the opcode
  1134. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,opCodeStr,0) != kOkCsvRC )
  1135. {
  1136. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on opcode label.");
  1137. goto errLabel;
  1138. }
  1139. // col 4 : dsecs
  1140. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,dsecs,0) != kOkCsvRC )
  1141. {
  1142. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dsecs'.");
  1143. goto errLabel;
  1144. }
  1145. // col 5 : secs
  1146. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,secs,0) != kOkCsvRC )
  1147. {
  1148. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'secs'.");
  1149. goto errLabel;
  1150. }
  1151. // cols 6,7,8 blanks
  1152. if((rc = _cmXScoreWriteCsvBlankCols(p,3,&lcp)) != kOkXsRC )
  1153. goto errLabel;
  1154. // col 9 : d0
  1155. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d0,0) != kOkCsvRC )
  1156. {
  1157. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1158. goto errLabel;
  1159. }
  1160. // col 10 : d1
  1161. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d1,0) != kOkCsvRC )
  1162. {
  1163. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd1'.");
  1164. goto errLabel;
  1165. }
  1166. // col 11 : pitch
  1167. if( pitch == -1 )
  1168. {
  1169. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1170. goto errLabel;
  1171. }
  1172. else
  1173. {
  1174. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmMidiToSciPitch(pitch,NULL,0),0) != kOkCsvRC )
  1175. {
  1176. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  1177. goto errLabel;
  1178. }
  1179. }
  1180. // col 12 : blanks
  1181. if((rc = _cmXScoreWriteCsvBlankCols(p,1 + (cmIsFlag(flags,kBarXsFl) ? 0 : 1), &lcp)) != kOkXsRC )
  1182. goto errLabel;
  1183. // col 13 : bar number
  1184. if( cmIsFlag(flags,kBarXsFl) )
  1185. {
  1186. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,bar,0) != kOkCsvRC )
  1187. {
  1188. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  1189. goto errLabel;
  1190. }
  1191. }
  1192. // col 14 : skip (blank for now)
  1193. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1194. goto errLabel;
  1195. // col 15: even (all grace notes are 'even' notes
  1196. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp, cmIsFlag(flags,kGraceXsFl) | cmIsFlag(flags,kEvenXsFl) ? "e" : "",0) != kOkCsvRC )
  1197. {
  1198. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1199. goto errLabel;
  1200. }
  1201. // col 16: grace
  1202. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kGraceXsFl) ? "g" : "",0) != kOkCsvRC )
  1203. {
  1204. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1205. goto errLabel;
  1206. }
  1207. // col 17: tempo
  1208. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kTempoXsFl) ? "t" : "",0) != kOkCsvRC )
  1209. {
  1210. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1211. goto errLabel;
  1212. }
  1213. // col 18: frac
  1214. if( frac == 0 )
  1215. {
  1216. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1217. goto errLabel;
  1218. }
  1219. else
  1220. {
  1221. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,1.0/frac,0) != kOkCsvRC )
  1222. {
  1223. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 't frac'.");
  1224. goto errLabel;
  1225. }
  1226. }
  1227. // col 19: dynamic marking (blank for now)
  1228. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1229. goto errLabel;
  1230. // col 20: section
  1231. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kSectionXsFl) ? sectionStr : "",0) != kOkCsvRC )
  1232. {
  1233. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1234. goto errLabel;
  1235. }
  1236. // col 21, 22 : recd-play, remark (blank for now)
  1237. if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
  1238. goto errLabel;
  1239. errLabel:
  1240. return rc;
  1241. }
  1242. cmXsRC_t cmXScoreWriteCsv( cmXsH_t h, const cmChar_t* csvFn )
  1243. {
  1244. cmXsRC_t rc = kOkXsRC;
  1245. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1246. unsigned rowIdx = 1;
  1247. double tpqn = 0; // ticks per quarter note
  1248. double tps = 0; // ticks per second
  1249. double sec = 0; // current time in seconds
  1250. const cmChar_t* sectionIdStr = NULL;
  1251. if( !cmCsvIsValid(p->csvH) )
  1252. return cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output object is not initialized.");
  1253. if((rc = _cmXScoreWriteCsvHdr( p )) != kOkXsRC )
  1254. goto errLabel;
  1255. cmXsPart_t* pp = p->partL;
  1256. for(; pp!=NULL; pp=pp->link)
  1257. {
  1258. cmXsMeas_t* mp = pp->measL;
  1259. for(; mp!=NULL; mp=mp->link)
  1260. {
  1261. if( mp->divisions != 0 )
  1262. tpqn = mp->divisions;
  1263. cmXsNote_t* np = mp->noteL;
  1264. double sec0 = sec;
  1265. for(; np!=NULL; np=np->slink)
  1266. {
  1267. //
  1268. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  1269. {
  1270. double bpm = np->duration;
  1271. double bps = bpm / 60.0;
  1272. tps = bps * tpqn;
  1273. }
  1274. double meas_sec = tps == 0 ? 0 : np->tick / tps;
  1275. double sec1 = sec + meas_sec;
  1276. double dsecs = sec1 - sec0;
  1277. sec0 = sec1;
  1278. // if this is a section event
  1279. if( cmIsFlag(np->flags,kSectionXsFl) )
  1280. sectionIdStr = np->tvalue;
  1281. // if this is a bar event
  1282. if( cmIsFlag(np->flags,kBarXsFl) )
  1283. {
  1284. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,NULL,"bar",dsecs,sec1,0,0,-1,0,np->flags);
  1285. }
  1286. else
  1287. // if this is a pedal event
  1288. if( cmIsFlag(np->flags,kPedalDnXsFl|kPedalUpXsFl|kPedalUpDnXsFl) )
  1289. {
  1290. unsigned d0 = 64; // pedal MIDI ctl id
  1291. unsigned d1 = cmIsFlag(np->flags,kPedalDnXsFl) ? 64 : 0; // pedal-dn: d1>=64 pedal-up:<64
  1292. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,NULL,"ctl",dsecs,sec1,d0,d1,-1,0,np->flags);
  1293. }
  1294. else
  1295. // if this is a sounding note event
  1296. if( cmIsFlag(np->flags,kOnsetXsFl) )
  1297. {
  1298. double frac = np->rvalue + (cmIsFlag(np->flags,kDotXsFl) ? (np->rvalue/2) : 0);
  1299. //
  1300. _cmXScoreWriteCsvRow(p,rowIdx,np->uid,mp->number,sectionIdStr,"non",dsecs,sec1,np->pitch,60,np->pitch,frac,np->flags);
  1301. sectionIdStr = NULL;
  1302. }
  1303. rowIdx += 1;
  1304. }
  1305. sec = sec0;
  1306. }
  1307. }
  1308. if( cmCsvWrite( p->csvH, csvFn ) != kOkCsvRC )
  1309. rc = cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output write failed on file '%s'.",csvFn);
  1310. errLabel:
  1311. return rc;
  1312. }
  1313. void cmXScoreReport( cmXsH_t h, cmRpt_t* rpt, bool sortFl )
  1314. {
  1315. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1316. cmXsPart_t* pp = p->partL;
  1317. for(; pp!=NULL; pp=pp->link)
  1318. {
  1319. cmRptPrintf(rpt,"Part:%s\n",pp->idStr);
  1320. const cmXsMeas_t* meas = pp->measL;
  1321. for(; meas!=NULL; meas=meas->link)
  1322. {
  1323. 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);
  1324. if( sortFl )
  1325. {
  1326. _cmXScoreReportTitle(rpt);
  1327. const cmXsNote_t* note = meas->noteL;
  1328. for(; note!=NULL; note=note->slink)
  1329. {
  1330. _cmXScoreReportNote(rpt,note);
  1331. if( note->slink!=NULL || note->voice->id==0)
  1332. cmRptPrintf(rpt,"\n");
  1333. else
  1334. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  1335. }
  1336. }
  1337. else
  1338. {
  1339. _cmXScoreReportTitle(rpt);
  1340. const cmXsVoice_t* v = meas->voiceL;
  1341. for(; v!=NULL; v=v->link)
  1342. {
  1343. const cmXsNote_t* note = v->noteL;
  1344. cmRptPrintf(rpt," voice:%i\n",v->id);
  1345. for(; note!=NULL; note=note->mlink)
  1346. {
  1347. _cmXScoreReportNote(rpt,note);
  1348. if( note->mlink!=NULL || note->voice->id==0)
  1349. cmRptPrintf(rpt,"\n");
  1350. else
  1351. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  1352. }
  1353. }
  1354. }
  1355. }
  1356. }
  1357. }
  1358. cmXsRC_t cmXScoreWriteMidi( cmXsH_t h, const cmChar_t* fn )
  1359. {
  1360. assert(0); // function not implemented
  1361. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1362. cmXsPart_t* pp = p->partL;
  1363. for(; pp!=NULL; pp=pp->link)
  1364. {
  1365. const cmXsMeas_t* meas = pp->measL;
  1366. for(; meas!=NULL; meas=meas->link)
  1367. {
  1368. const cmXsNote_t* note = meas->noteL;
  1369. for(; note!=NULL; note=note->slink)
  1370. {
  1371. }
  1372. }
  1373. }
  1374. }
  1375. cmXsRC_t cmXScoreTest( cmCtx_t* ctx, const cmChar_t* xmlFn, const cmChar_t* midiFn, const cmChar_t* outFn )
  1376. {
  1377. cmXsRC_t rc;
  1378. cmXsH_t h = cmXsNullHandle;
  1379. if((rc = cmXScoreInitialize( ctx, &h, xmlFn, midiFn)) != kOkXsRC )
  1380. return cmErrMsg(&ctx->err,rc,"XScore alloc failed.");
  1381. if( outFn != NULL )
  1382. cmXScoreWriteCsv(h,outFn);
  1383. cmXScoreReport(h,&ctx->rpt,true);
  1384. return cmXScoreFinalize(&h);
  1385. }