libcm is a C development framework with an emphasis on audio signal processing applications.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cmXScore.c 54KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  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. unsigned dynamics; // dynamic level 1=pppp 9=fff
  59. cmChar_t step; // A-G
  60. unsigned octave; // sci pitch octave
  61. int alter; // +n=sharps,-n=flats
  62. unsigned staff; // 1=treble 2=bass
  63. unsigned tick; //
  64. unsigned duration; // duration in ticks
  65. double secs; // absolute time in seconds
  66. double dsecs; // delta time in seconds since previous event
  67. unsigned locIdx; // location index (chords share the same location index)
  68. 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, ...)
  69. const cmChar_t* tvalue; // text value
  70. unsigned evenGroupId; // eveness group id
  71. unsigned dynGroupId; // dynamics group id
  72. unsigned tempoGroupId; // tempo group id
  73. struct cmXsVoice_str* voice; // voice this note belongs to
  74. struct cmXsMeas_str* meas; // measure this note belongs to
  75. const cmXmlNode_t* xmlNode; // note xml ptr
  76. struct cmXsNote_str* mlink; // measure note list
  77. struct cmXsNote_str* slink; // time sorted event list
  78. } cmXsNote_t;
  79. typedef struct cmXsVoice_str
  80. {
  81. unsigned id; // Voice id
  82. cmXsNote_t* noteL; // List of notes in this voice
  83. struct cmXsVoice_str* link; // Link to other voices in this measure
  84. } cmXsVoice_t;
  85. typedef struct cmXsSpan_str
  86. {
  87. unsigned staff;
  88. unsigned number;
  89. struct cmXsMeas_str* meas;
  90. unsigned tick0;
  91. unsigned tick1;
  92. int pitch_offset;
  93. struct cmXsSpan_str* link;
  94. } cmXsSpan_t;
  95. typedef struct cmXsMeas_str
  96. {
  97. unsigned number; // Measure number
  98. unsigned divisions; // ticks-per-quarter-note
  99. unsigned beats; // beats per measure
  100. unsigned beat_type; // whole/half/quarter/eighth ...
  101. cmXsVoice_t* voiceL; // List of voices in this measure
  102. cmXsNote_t* noteL; // List of time sorted notes in this measure
  103. struct cmXsMeas_str* link; // Link to other measures in this part.
  104. } cmXsMeas_t;
  105. typedef struct cmXsPart_str
  106. {
  107. const cmChar_t* idStr; // Id of this part
  108. cmXsMeas_t* measL; // List of measures in this part.
  109. struct cmXsPart_str* link; // Link to other parts in this score
  110. } cmXsPart_t;
  111. typedef struct
  112. {
  113. cmErr_t err;
  114. cmXmlH_t xmlH;
  115. cmLHeapH_t lhH;
  116. cmXsPart_t* partL;
  117. cmCsvH_t csvH;
  118. cmXsSpan_t* spanL;
  119. unsigned nextUid;
  120. } cmXScore_t;
  121. cmXScore_t* _cmXScoreHandleToPtr( cmXsH_t h )
  122. {
  123. cmXScore_t* p = (cmXScore_t*)h.h;
  124. assert( p != NULL );
  125. return p;
  126. }
  127. cmXsRC_t _cmXScoreFinalize( cmXScore_t* p )
  128. {
  129. cmXsRC_t rc = kOkXsRC;
  130. // release the XML file
  131. cmXmlFree( &p->xmlH );
  132. // release the local linked heap memory
  133. cmLHeapDestroy(&p->lhH);
  134. // release the CSV output object
  135. cmCsvFinalize(&p->csvH);
  136. cmMemFree(p);
  137. return rc;
  138. }
  139. cmXsRC_t _cmXScoreMissingNode( cmXScore_t* p, const cmXmlNode_t* parent, const cmChar_t* label )
  140. {
  141. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML node '%s'. Parent line:%i",label,parent->line);
  142. }
  143. cmXsRC_t _cmXScoreMissingAttribute( cmXScore_t* p, const cmXmlNode_t* np, const cmChar_t* attrLabel )
  144. {
  145. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML attribute '%s' from node '%s'.",attrLabel,np->label);
  146. }
  147. cmXsVoice_t* _cmXScoreIdToVoice( cmXsMeas_t* meas, unsigned voiceId )
  148. {
  149. cmXsVoice_t* v = meas->voiceL;
  150. for(; v!=NULL; v=v->link)
  151. if( v->id == voiceId )
  152. return v;
  153. return NULL;
  154. }
  155. cmXsRC_t _cmXScorePushNote( cmXScore_t* p, cmXsMeas_t* meas, unsigned voiceId, cmXsNote_t* note )
  156. {
  157. cmXsVoice_t* v;
  158. // get the voice recd
  159. if((v = _cmXScoreIdToVoice(meas,voiceId)) == NULL)
  160. {
  161. // the voice recd doesn't exist for this voiceId - allocate one
  162. v = cmLhAllocZ(p->lhH,cmXsVoice_t,1);
  163. v->id = voiceId;
  164. // add the voice record to the meas->voiceL
  165. if( meas->voiceL == NULL )
  166. meas->voiceL = v;
  167. else
  168. {
  169. cmXsVoice_t* vp = meas->voiceL;
  170. while( vp->link!=NULL )
  171. vp = vp->link;
  172. vp->link = v;
  173. }
  174. }
  175. // add the note record to the end of meas->voiceL
  176. if( v->noteL == NULL )
  177. v->noteL = note;
  178. else
  179. {
  180. cmXsNote_t* n = v->noteL;
  181. while( n->mlink != NULL )
  182. n = n->mlink;
  183. n->mlink = note;
  184. }
  185. note->voice = v;
  186. note->uid = p->nextUid++;
  187. return kOkXsRC;
  188. }
  189. cmXsRC_t _cmXScoreParsePartList( cmXScore_t* p )
  190. {
  191. cmXsRC_t rc = kOkXsRC;
  192. cmXsPart_t* lastPartPtr = NULL;
  193. const cmXmlNode_t* xnp;
  194. // find the 'part-list'
  195. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part-list", NULL, 0)) == NULL )
  196. return _cmXScoreMissingNode(p,cmXmlRoot(p->xmlH),"part-list");
  197. const cmXmlNode_t* cnp = xnp->children;
  198. // for each child of the 'part-list'
  199. for(; cnp!=NULL; cnp=cnp->sibling)
  200. if( cmTextCmp( cnp->label, "score-part" ) == 0 )
  201. {
  202. const cmXmlAttr_t* a;
  203. // find the 'score-part' id
  204. if((a = cmXmlFindAttrib(cnp,"id")) == NULL )
  205. return _cmXScoreMissingAttribute(p,cnp,"id");
  206. // allocate a new part record
  207. cmXsPart_t* pp = cmLhAllocZ(p->lhH,cmXsPart_t,1);
  208. pp->idStr = a->value; // set the part id
  209. // link the new part record to the end of the part list
  210. if(lastPartPtr == NULL)
  211. p->partL = pp;
  212. else
  213. lastPartPtr->link = pp;
  214. lastPartPtr = pp;
  215. }
  216. return rc;
  217. }
  218. cmXsRC_t _cmXScoreParsePitch( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* np )
  219. {
  220. cmXsRC_t rc = kOkXsRC;
  221. unsigned octave = 0;
  222. double alter = 0;
  223. const cmChar_t* step = NULL;
  224. if((step = cmXmlNodeValue(nnp,"pitch","step",NULL)) == NULL )
  225. return _cmXScoreMissingNode(p,nnp,"step");
  226. if((rc = cmXmlNodeUInt( nnp,&octave,"pitch","octave",NULL)) != kOkXmlRC )
  227. return _cmXScoreMissingNode(p,nnp,"octave");
  228. cmXmlNodeDouble( nnp,&alter,"pitch","alter",NULL);
  229. cmChar_t buf[3] = { *step, '0', '\0'};
  230. unsigned midi = cmSciPitchToMidi(buf);
  231. midi += (12 * octave);
  232. midi += alter;
  233. np->pitch = midi;
  234. np->step = *step;
  235. np->octave = octave;
  236. np->alter = alter;
  237. np->flags |= kOnsetXsFl;
  238. return rc;
  239. }
  240. cmXsRC_t _cmXScoreParseNoteRValue( cmXScore_t* p, const cmXmlNode_t* nnp, const cmChar_t* label, double* rvalueRef )
  241. {
  242. typedef struct map_str
  243. {
  244. double rvalue;
  245. const cmChar_t* label;
  246. } map_t;
  247. map_t mapV[] =
  248. {
  249. {-1.0, "measure" }, // whole measure rest
  250. { 0.5, "breve" }, // double whole
  251. { 1.0, "whole" },
  252. { 2.0, "half" },
  253. { 4.0, "quarter" },
  254. { 8.0, "eighth" },
  255. {16.0, "16th" },
  256. {32.0, "32nd" },
  257. { 64.0, "64th" },
  258. {128.0, "128th" },
  259. { 0.0, "" }
  260. };
  261. const cmChar_t* str;
  262. // get the XML rvalue label
  263. if((str = cmXmlNodeValue(nnp,label,NULL)) == NULL)
  264. {
  265. if((nnp = cmXmlSearch(nnp,"rest",NULL,0)) != NULL )
  266. {
  267. const cmXmlAttr_t* a;
  268. if((a = cmXmlFindAttrib(nnp,"measure")) != NULL && cmTextCmp(a->value,"yes")==0)
  269. {
  270. *rvalueRef = -1;
  271. return kOkXsRC;
  272. }
  273. }
  274. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'beat-unit' metronome value is missing on line %i.",nnp->line);
  275. }
  276. unsigned i;
  277. // lookup the rvalue numeric value from the mapV[] table
  278. for(i=0; mapV[i].rvalue!=0; ++i)
  279. if( cmTextCmp(mapV[i].label,str) == 0 )
  280. {
  281. *rvalueRef = mapV[i].rvalue;
  282. return kOkXsRC;
  283. }
  284. // the rvalue label was not found
  285. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unknown rvalue type='%s'.",str);
  286. }
  287. cmXsRC_t _cmXScoreParseColor( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* note )
  288. {
  289. cmXsRC_t rc = kOkXsRC;
  290. const cmXmlAttr_t* a;
  291. typedef struct map_str
  292. {
  293. unsigned value;
  294. const cmChar_t* label;
  295. } map_t;
  296. map_t mapV[] =
  297. {
  298. { kEvenXsFl, "#0000FF" }, // blue (even)
  299. { kTempoXsFl, "#00FF00" }, // green (tempo)
  300. { kDynXsFl, "#FF0000" }, // red (dynamics)
  301. { kTempoXsFl | kEvenXsFl, "#00FFFF" }, // green + blue (turquoise)
  302. { kDynXsFl | kEvenXsFl, "#FF00FF" }, // red + blue
  303. { kDynXsFl | kEvenXsFl, "#FF0CF7" }, // magenta (even+dyn)
  304. { kDynXsFl | kTempoXsFl, "#FF7F00" }, // red + green (brown)
  305. { kTempoXsFl | kEvenXsFl | kDynXsFl, "#996633" }, // (purple)
  306. { kDynXsFl, "#FF6A03" }, // 176 orange (dynamics)
  307. { kEvenXsFl, "#2F00E8" }, // 1001 blue (even)
  308. { kTempoXsFl, "#01CD1F" }, // 1196 green (tempo)
  309. { kEvenXsFl, "#3600E8" }, // 1627 blue (even)
  310. { kDynXsFl | kTempoXsFl, "#9E8F15" }, // 8827 brown (dyn + tempo)
  311. { kEvenXsFl, "#2E00E6" }, // 5393 blue (even)
  312. { kEvenXsFl, "#2C00DD" }, // 5895 blue (even)
  313. { kDynXsFl, "#FF5B03" }, // 6498 orange (dyn)
  314. { kDynXsFl, "#FF6104" }, // 6896 orange
  315. { kEvenXsFl, "#2A00E6" }, // 7781 blue
  316. { kEvenXsFl, "#2300DD" }, // 8300 blue (even)
  317. { kTempoXsFl, "#03CD22" }, // 10820 green (tempo)
  318. { kEvenXsFl, "#3400DB" }, // 11627 blue (dyn)
  319. { -1, "" }
  320. };
  321. /*
  322. orange #FF6A03
  323. magenta #FF0CF7
  324. blue #2F00E8
  325. green #01CD1F
  326. gold #9E8F15
  327. green #03CD22
  328. */
  329. if((a = cmXmlFindAttrib(nnp, "color" )) != NULL )
  330. {
  331. unsigned i;
  332. for(i=0; mapV[i].value != -1; ++i)
  333. if( cmTextCmp(a->value,mapV[i].label) == 0 )
  334. {
  335. note->flags += mapV[i].value;
  336. break;
  337. }
  338. if( mapV[i].value == -1 )
  339. cmErrMsg(&p->err,kSyntaxErrorXsRC,"The note color '%s' was not found on line %i.",a->value,nnp->line);
  340. }
  341. return rc;
  342. }
  343. // On input tick0Ref is set to the tick of the previous event.
  344. // On input tickRef is set to the tick of this event.
  345. // On output tick0Ref is set to the tick of this event.
  346. // On output tickRef is set to the tick of the next event.
  347. cmXsRC_t _cmXScoreParseNote(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* nnp, unsigned* tick0Ref, unsigned* tickRef )
  348. {
  349. cmXsRC_t rc = kOkXsRC;
  350. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  351. unsigned voiceId;
  352. note->meas = meas;
  353. note->xmlNode = nnp;
  354. // get the voice id for this node
  355. if( cmXmlNodeUInt(nnp,&voiceId,"voice",NULL) != kOkXmlRC )
  356. return _cmXScoreMissingNode(p,nnp,"voice");
  357. // if this note has a pitch
  358. if( cmXmlNodeHasChild(nnp,"pitch",NULL) )
  359. if((rc = _cmXScoreParsePitch(p,nnp,note)) != kOkXsRC )
  360. return rc;
  361. cmXmlNodeUInt(nnp,&note->duration,"duration",NULL); // get the note duration
  362. cmXmlNodeUInt(nnp,&note->staff,"staff",NULL); // get th staff number
  363. // is 'rest'
  364. if( cmXmlNodeHasChild(nnp,"rest",NULL) )
  365. note->flags |= kRestXsFl;
  366. // is 'grace'
  367. if( cmXmlNodeHasChild(nnp,"grace",NULL) )
  368. note->flags |= kGraceXsFl;
  369. // is 'dot'
  370. if( cmXmlNodeHasChild(nnp,"dot",NULL) )
  371. note->flags |= kDotXsFl;
  372. // is 'chord'
  373. if( cmXmlNodeHasChild(nnp,"chord",NULL) )
  374. note->flags |= kChordXsFl;
  375. // is this is first note in a tied pair
  376. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","start",NULL) )
  377. note->flags |= kTieBegXsFl;
  378. // is this is second note in a tied pair
  379. if( cmXmlNodeHasChildWithAttrAndValue(nnp,"tie","type","stop",NULL) )
  380. note->flags |= kTieEndXsFl;
  381. // has 'heel' mark
  382. if( cmXmlNodeHasChild(nnp,"notations","technical","heel",NULL) )
  383. note->flags |= kHeelXsFl;
  384. // set color coded flags
  385. if((rc = _cmXScoreParseColor(p, nnp, note )) != kOkXsRC )
  386. return rc;
  387. // get the note's rythmic value
  388. if((rc = _cmXScoreParseNoteRValue(p,nnp,"type",&note->rvalue)) != kOkXsRC )
  389. return rc;
  390. // if this is a chord note
  391. if( cmIsFlag(note->flags,kChordXsFl) )
  392. {
  393. note->tick = *tick0Ref; // then use the onset time from the previous note and do not advance time
  394. }
  395. else
  396. {
  397. *tick0Ref = *tickRef;
  398. note->tick = *tickRef;
  399. *tickRef += note->duration;
  400. }
  401. return _cmXScorePushNote(p, meas, voiceId, note );
  402. }
  403. 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 )
  404. {
  405. cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
  406. unsigned voiceId = 0; // non-note's are always assigned to voiceId=0;
  407. note->tick = tick;
  408. note->flags = flags;
  409. note->rvalue = rvalue;
  410. note->tvalue = tvalue;
  411. note->duration = duration;
  412. note->meas = meas;
  413. note->xmlNode = noteXmlNode;
  414. return _cmXScorePushNote(p, meas, voiceId, note );
  415. }
  416. cmXsSpan_t* _cmXScoreFindOpenOctaveShift( cmXScore_t* p, unsigned staff, unsigned number )
  417. {
  418. cmXsSpan_t* s = p->spanL;
  419. for(; s!=NULL; s=s->link)
  420. if( s->tick1 == -1 && s->staff == staff && s->number == number )
  421. return s;
  422. return NULL;
  423. }
  424. cmXsRC_t _cmXScorePushOctaveShift(cmXScore_t* p, cmXsMeas_t* meas, unsigned staff, unsigned span_number, const cmChar_t* type_str, unsigned tick)
  425. {
  426. assert( meas != NULL);
  427. cmXsSpan_t* s;
  428. if( cmTextCmp(type_str,"stop") == 0 )
  429. {
  430. if((s = _cmXScoreFindOpenOctaveShift(p,staff,span_number)) == NULL )
  431. return cmErrWarnMsg(&p->err,kUnterminatedOctaveShiftXsrRC,"An illegal octave shift was encounted in meas %i.\n",meas->number);
  432. s->tick1 = tick;
  433. }
  434. else
  435. {
  436. s = cmLhAllocZ(p->lhH,cmXsSpan_t,1);
  437. s->staff = staff;
  438. s->meas = meas;
  439. s->number = span_number;
  440. s->tick0 = tick;
  441. s->tick1 = -1;
  442. s->pitch_offset = cmTextCmp(type_str,"up")==0 ? -12 : 12;
  443. s->link = p->spanL;
  444. p->spanL = s;
  445. }
  446. return kOkXsRC;
  447. }
  448. cmXsRC_t _cmXScoreParseDirection(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* dnp, unsigned tick)
  449. {
  450. cmXsRC_t rc = kOkXsRC;
  451. const cmXmlNode_t* np = NULL;
  452. const cmXmlAttr_t* a = NULL;
  453. unsigned flags = 0;
  454. int offset = 0;
  455. double rvalue = 0;
  456. const cmChar_t* tvalue = NULL;
  457. unsigned duration = 0;
  458. bool pushFl = true;
  459. unsigned staff = 0;
  460. cmXmlNodeInt( dnp, &offset, "offset", NULL );
  461. cmXmlNodeUInt(dnp, &staff, "staff", NULL );
  462. // if this is a metronome direction
  463. if((np = cmXmlSearch( dnp, "metronome", NULL, 0)) != NULL )
  464. {
  465. if( cmXmlNodeUInt(np,&duration,"per-minute",NULL) != kOkXmlRC )
  466. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'per-minute' metronome value is missing on line %i.",np->line);
  467. if((rc = _cmXScoreParseNoteRValue(p,np,"beat-unit",&rvalue)) != kOkXsRC )
  468. return rc;
  469. flags = kMetronomeXsFl;
  470. }
  471. else
  472. // if this is a pedal direction
  473. if((np = cmXmlSearch( dnp, "pedal",NULL,0)) != NULL )
  474. {
  475. if((a = cmXmlFindAttrib(np,"type")) == NULL )
  476. return _cmXScoreMissingAttribute(p, np, "type" );
  477. if( cmTextCmp(a->value,"start") == 0 )
  478. flags = kPedalDnXsFl;
  479. else
  480. if( cmTextCmp(a->value,"change") == 0 )
  481. flags = kPedalUpDnXsFl;
  482. else
  483. if( cmTextCmp(a->value,"stop") == 0 )
  484. flags = kPedalUpXsFl;
  485. else
  486. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unrecognized pedal type:'%s'.",cmStringNullGuard(a->value));
  487. }
  488. else
  489. // if this is a 'words' direction
  490. if((np = cmXmlSearch( dnp, "words", NULL, 0)) != NULL )
  491. {
  492. if((a = cmXmlFindAttrib(np,"enclosure")) != NULL && cmTextCmp(a->value,"rectangle")==0 )
  493. {
  494. if( cmTextIsEmpty( tvalue = np->dataStr ) )
  495. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Section number is blank or missing on line %i.",np->line);
  496. flags = kSectionXsFl;
  497. }
  498. }
  499. else
  500. // if this is an 'octave-shift' direction
  501. if((np = cmXmlSearch( dnp, "octave-shift", NULL, 0)) != NULL )
  502. {
  503. unsigned span_number = -1;
  504. if( cmXmlAttrUInt(np,"number",&span_number) != kOkXmlRC )
  505. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'number' attribute.");
  506. if((a = cmXmlFindAttrib(np,"type")) == NULL)
  507. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Octave-shift is missing a 'type' attribute.");
  508. rc = _cmXScorePushOctaveShift(p,meas,staff,span_number,a->value,tick+offset);
  509. pushFl = false;
  510. }
  511. else
  512. {
  513. pushFl = false;
  514. }
  515. if( pushFl )
  516. rc = _cmXScorePushNonNote(p,meas,dnp,tick+offset,duration,rvalue,tvalue,flags);
  517. return rc;
  518. }
  519. // On input tickRef is set to the absolute tick of the bar line and on output it is set
  520. // to the absolute tick of the next bar line.
  521. cmXsRC_t _cmXScoreParseMeasure(cmXScore_t* p, cmXsPart_t* pp, const cmXmlNode_t* mnp, unsigned* tickRef)
  522. {
  523. cmXsRC_t rc = kOkXsRC;
  524. const cmXmlNode_t* np = NULL;
  525. unsigned tick = *tickRef;
  526. unsigned tick0= 0;
  527. cmXsMeas_t* m = NULL;
  528. // allocate the 'measure' record
  529. cmXsMeas_t* meas = cmLhAllocZ(p->lhH,cmXsMeas_t,1);
  530. // get measure number
  531. if( cmXmlAttrUInt(mnp,"number", &meas->number) != kOkXmlRC )
  532. return _cmXScoreMissingAttribute(p,mnp,"number");
  533. if( pp->measL == NULL )
  534. pp->measL = meas;
  535. else
  536. {
  537. m = pp->measL;
  538. while( m->link != NULL )
  539. m = m->link;
  540. m->link = meas;
  541. meas->divisions = m->divisions;
  542. meas->beats = m->beats;
  543. meas->beat_type = m->beat_type;
  544. }
  545. // get measure attributes node
  546. if((np = cmXmlSearch(mnp,"attributes",NULL,0)) != NULL)
  547. {
  548. cmXmlNodeUInt(np,&meas->divisions,"divisions",NULL);
  549. cmXmlNodeUInt(np,&meas->beats, "time","beats",NULL);
  550. cmXmlNodeUInt(np,&meas->beat_type,"time","beat-type",NULL);
  551. }
  552. // store the bar line
  553. if((rc = _cmXScorePushNonNote(p,meas,mnp,tick,0,0,NULL,kBarXsFl)) != kOkXsRC )
  554. return rc;
  555. np = mnp->children;
  556. // for each child of the 'meas' XML node
  557. for(; rc==kOkXsRC && np!=NULL; np=np->sibling)
  558. {
  559. // if this is a 'note' node
  560. if( cmTextCmp(np->label,"note") == 0 )
  561. {
  562. rc = _cmXScoreParseNote(p,meas,np,&tick0,&tick);
  563. }
  564. else
  565. // if this is a 'backup' node
  566. if( cmTextCmp(np->label,"backup") == 0 )
  567. {
  568. unsigned backup;
  569. cmXmlNodeUInt(np,&backup,"duration",NULL);
  570. if( backup > tick )
  571. tick = 0;
  572. else
  573. tick -= backup;
  574. tick0 = tick;
  575. }
  576. else
  577. // if this is a 'direction' node
  578. if( cmTextCmp(np->label,"direction") == 0 )
  579. {
  580. rc = _cmXScoreParseDirection(p,meas,np,tick);
  581. }
  582. }
  583. *tickRef = tick;
  584. return rc;
  585. }
  586. cmXsRC_t _cmXScoreParsePart( cmXScore_t* p, cmXsPart_t* pp )
  587. {
  588. cmXsRC_t rc = kOkXsRC;
  589. const cmXmlNode_t* xnp;
  590. cmXmlAttr_t partAttr = { "id", pp->idStr };
  591. unsigned barTick = 0;
  592. // find the 'part'
  593. if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part", &partAttr, 1)) == NULL )
  594. return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The part '%s' was not found.",pp->idStr);
  595. // for each child of this part - find each measure
  596. const cmXmlNode_t* cnp = xnp->children;
  597. for(; cnp!=NULL; cnp=cnp->sibling)
  598. if( cmTextCmp(cnp->label,"measure") == 0 )
  599. if((rc = _cmXScoreParseMeasure(p,pp,cnp,&barTick)) != kOkXsRC )
  600. return rc;
  601. return rc;
  602. }
  603. // Insert note 'np' into the sorted note list based at 's0'.
  604. // Return a pointer to the base of the list after the insertion.
  605. cmXsNote_t* _cmXScoreInsertSortedNote( cmXsNote_t* s0, cmXsNote_t* np )
  606. {
  607. assert( np != NULL );
  608. // np->slink is not NULL if the list is being resorted
  609. np->slink = NULL;
  610. // this list is empty so np is the first element on the list
  611. if( s0 == NULL )
  612. return np;
  613. // np is before the first element on the list
  614. if( np->tick < s0->tick )
  615. {
  616. np->slink = s0;
  617. return np;
  618. }
  619. cmXsNote_t* s1 = s0;
  620. cmXsNote_t* s2 = s0->slink;
  621. while( s2 != NULL )
  622. {
  623. if( s2->tick > np->tick )
  624. {
  625. s1->slink = np;
  626. np->slink = s2;
  627. return s0;
  628. }
  629. s1 = s2;
  630. s2 = s2->slink;
  631. }
  632. s1->slink = np;
  633. return s0;
  634. }
  635. void _cmXScoreSort( cmXScore_t* p )
  636. {
  637. // for each part
  638. cmXsPart_t* pp = p->partL;
  639. for(; pp!=NULL; pp=pp->link)
  640. {
  641. // for each measure in this part
  642. cmXsMeas_t* mp = pp->measL;
  643. for(; mp!=NULL; mp=mp->link)
  644. {
  645. // explicitely set noteL to NULL to in case we are re-sorting
  646. mp->noteL = NULL;
  647. // for each voice in this measure
  648. cmXsVoice_t* vp = mp->voiceL;
  649. for(; vp!=NULL; vp=vp->link)
  650. {
  651. // for each note in this measure
  652. cmXsNote_t* np = vp->noteL;
  653. for(; np!=NULL; np=np->mlink)
  654. mp->noteL = _cmXScoreInsertSortedNote(mp->noteL,np);
  655. }
  656. }
  657. }
  658. }
  659. void _cmXScoreSetAbsoluteTime( cmXScore_t* p )
  660. {
  661. double tpqn = 0; // ticks per quarter note
  662. double tps = 0; // ticks per second
  663. unsigned metro_tick = 0;
  664. double metro_sec = 0;
  665. double sec0 = 0;
  666. cmXsPart_t* pp = p->partL;
  667. for(; pp!=NULL; pp=pp->link)
  668. {
  669. cmXsMeas_t* mp = pp->measL;
  670. for(; mp!=NULL; mp=mp->link)
  671. {
  672. if( mp->divisions != 0 )
  673. tpqn = mp->divisions;
  674. cmXsNote_t* np = mp->noteL;
  675. for(; np!=NULL; np=np->slink)
  676. {
  677. // Seconds are calculated as:
  678. // dticks = np->tick - metro_tick; // where metro_tick is the absolute tick of the last metro event
  679. // secs = (dticks/tps) + metro_secs; // where metro_secs is the absoute time of the last metro event
  680. unsigned dticks = np->tick - metro_tick;
  681. double secs = tps==0 ? 0 : (dticks/tps) + metro_sec;
  682. double dsecs = secs - sec0;
  683. //
  684. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  685. {
  686. double bpm = np->duration;
  687. double bps = bpm / 60.0;
  688. tps = bps * tpqn;
  689. metro_tick = np->tick;
  690. metro_sec = secs;
  691. }
  692. if( cmIsFlag(np->flags,kBarXsFl|kPedalDnXsFl|kPedalUpXsFl|kPedalUpDnXsFl|kOnsetXsFl|kSectionXsFl) )
  693. {
  694. np->secs = secs;
  695. np->dsecs = dsecs;
  696. sec0 = secs;
  697. }
  698. }
  699. }
  700. }
  701. }
  702. // All notes in a[aN] are on the same tick
  703. unsigned _cmXsSpreadGraceNotes( cmXsNote_t** a, unsigned aN )
  704. {
  705. unsigned i;
  706. bool barFl = false;
  707. // set barFl to true if a bar marker is included in the notes
  708. for(i=0; i<aN; ++i)
  709. if( cmIsFlag(a[i]->flags,kBarXsFl) )
  710. barFl = true;
  711. // spread any grace notes by one tick
  712. unsigned nextGraceTick = UINT_MAX;
  713. for(i=0; i<aN; ++i)
  714. if( cmIsFlag(a[i]->flags,kGraceXsFl) )
  715. {
  716. if( nextGraceTick == UINT_MAX )
  717. nextGraceTick = a[i]->tick + 1;
  718. else
  719. {
  720. a[i]->tick = nextGraceTick;
  721. nextGraceTick += 1;
  722. }
  723. }
  724. // if this tick group includes the bar ...
  725. if( barFl && nextGraceTick != UINT_MAX )
  726. {
  727. // ... then move all non-grace note events (except the bar marker) after
  728. // the grace notes
  729. for(i=0; i<aN; ++i)
  730. if( cmIsNotFlag(a[i]->flags,kGraceXsFl) && cmIsNotFlag(a[i]->flags,kBarXsFl) )
  731. a[i]->tick = nextGraceTick;
  732. }
  733. return nextGraceTick==UINT_MAX ? 0 : nextGraceTick;
  734. }
  735. void _cmXScoreSpreadGraceNotes( cmXScore_t* p )
  736. {
  737. cmXsPart_t* pp = p->partL;
  738. for(; pp!=NULL; pp=pp->link)
  739. {
  740. // tick1 is the location of the minimum current tick
  741. // (or 0 if it should be ignored)
  742. unsigned tick1 = 0;
  743. cmXsMeas_t* mp = pp->measL;
  744. for(; mp!=NULL; mp=mp->link)
  745. {
  746. cmXsNote_t* np = mp->noteL;
  747. unsigned aN = 128;
  748. cmXsNote_t* a[ aN ];
  749. unsigned ai = 0;
  750. // The first event in a measure may have been forced ahead
  751. // by spreading at the end of the previous measure
  752. if( tick1 > np->tick )
  753. np->tick = tick1;
  754. else
  755. tick1 = 0;
  756. // tick0 is the tick of the current tick group we are examining
  757. // A tick group is a group of events that share the same tick.
  758. unsigned tick0 = np->tick;
  759. for(; np!=NULL; np=np->slink)
  760. {
  761. // if this event is the first of a new tick group
  762. if( np->tick != tick0 )
  763. {
  764. // if there is more than one event in the completed tick group ...
  765. if( ai > 1 )
  766. tick1 = _cmXsSpreadGraceNotes(a,ai); // ... then process the group
  767. ai = 0; // empty the tick group array
  768. tick0 = np->tick; // update the current group's common tick
  769. }
  770. // if the min. current tick is ahead of this event then move the event ahead
  771. if( tick1 > np->tick )
  772. np->tick = tick1;
  773. else
  774. tick1 = 0; // otherwise disable tick1
  775. // add this event to the tick group
  776. assert(ai<aN);
  777. a[ai++] = np;
  778. }
  779. // if there are events in the group array then process them
  780. if( ai > 1 )
  781. tick1 = _cmXsSpreadGraceNotes(a,ai);
  782. }
  783. }
  784. }
  785. bool _cmXScoreFindTiedNote( cmXScore_t* p, cmXsMeas_t* mp, cmXsNote_t* n0p, bool rptFl )
  786. {
  787. cmXsNote_t* nnp = n0p->slink; // begin w/ note following np
  788. unsigned measNumb = mp->number;
  789. cmChar_t acc = n0p->alter==-1?'b' : (n0p->alter==1?'#':' ');
  790. if( rptFl )
  791. printf("%i %i %s ",n0p->meas->number,n0p->tick,cmMidiToSciPitch(n0p->pitch,NULL,0));
  792. while(1)
  793. {
  794. // if we are at the end of a measure advance to the next measure
  795. if( nnp == NULL )
  796. {
  797. mp = mp->link;
  798. nnp = mp->noteL;
  799. // if a measure was completed and no end note was found ... then the tie is unterminated
  800. // (a tie must be continued in every measure which it passes through)
  801. if( mp->number > measNumb + 1 )
  802. break;
  803. }
  804. // for each note starting at nnp
  805. for(; nnp!=NULL; nnp=nnp->slink)
  806. {
  807. // if this note is tied to the originating note (np)
  808. if( nnp->voice->id == n0p->voice->id && nnp->step == n0p->step && nnp->octave == n0p->octave )
  809. {
  810. nnp->flags |= kTieProcXsFl;
  811. nnp->flags = cmClrFlag(nnp->flags,kOnsetXsFl);
  812. if( rptFl )
  813. printf("---> %i %i %s ",nnp->meas->number,nnp->tick,cmMidiToSciPitch(nnp->pitch,NULL,0));
  814. // if this note is not tied to a subsequent note
  815. if( cmIsNotFlag(nnp->flags,kTieBegXsFl) )
  816. return true;
  817. // record the measure number of the last note with a tie-start
  818. measNumb = mp->number;
  819. }
  820. }
  821. }
  822. cmErrWarnMsg(&p->err,kUnterminatedTieXsRC,"The tied %c%c%i in measure %i was not terminated.",n0p->step,acc,n0p->octave,measNumb);
  823. return false;
  824. }
  825. void _cmXScoreResolveTiesAndLoc( cmXScore_t* p )
  826. {
  827. unsigned n = 0;
  828. unsigned m = 0;
  829. bool rptFl = false;
  830. cmXsPart_t* pp = p->partL;
  831. // for each part
  832. for(; pp!=NULL; pp=pp->link)
  833. {
  834. unsigned locIdx = 1;
  835. cmXsMeas_t* mp = pp->measL;
  836. // for each measure
  837. for(; mp!=NULL; mp=mp->link)
  838. {
  839. cmXsNote_t* n0 = NULL;
  840. cmXsNote_t* np = mp->noteL;
  841. // for each note in this measure
  842. for(; np!=NULL; np=np->slink)
  843. {
  844. // if this note begins a tie and has not yet been processed
  845. // (A note that continues a tie and therefore has a kTieBegXsFl set
  846. // may have already been processed by an earlier tied note.)
  847. if( cmIsFlag(np->flags,kTieBegXsFl) && cmIsNotFlag(np->flags,kTieProcXsFl))
  848. {
  849. if( _cmXScoreFindTiedNote(p,mp,np,rptFl) )
  850. m += 1;
  851. if( rptFl )
  852. printf("\n");
  853. n += 1;
  854. }
  855. if( cmIsFlag(np->flags,kTieEndXsFl) && cmIsFlag(np->flags,kOnsetXsFl) )
  856. {
  857. cmChar_t acc = np->alter==-1?'b' : (np->alter==1?'#':' ');
  858. 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);
  859. }
  860. // set the location
  861. if( cmIsFlag(np->flags,kOnsetXsFl|kBarXsFl) )
  862. {
  863. if( cmIsFlag(np->flags,kBarXsFl) || (n0!=NULL && n0->tick!=np->tick))
  864. locIdx += 1;
  865. np->locIdx = locIdx;
  866. n0 = np;
  867. }
  868. }
  869. }
  870. }
  871. printf("Found:%i Not Found:%i\n",m,n-m);
  872. }
  873. cmXsRC_t _cmXScoreResolveOctaveShift( cmXScore_t* p )
  874. {
  875. const cmXsSpan_t* s;
  876. for(s=p->spanL; s!=NULL; s=s->link)
  877. {
  878. if( s->tick1 == -1)
  879. {
  880. cmErrWarnMsg(&p->err,kSyntaxErrorXsRC,"An unterminated octave shift span was encountered in measure %i staff=%i.",s->meas->number,s->staff);
  881. }
  882. else
  883. {
  884. cmXsMeas_t* m = p->partL->measL;
  885. for(; m!=NULL; m=m->link)
  886. if( m->number == s->meas->number )
  887. break;
  888. assert( m != NULL );
  889. cmXsNote_t* note = m->noteL;
  890. for(; note!=NULL; note=note->slink)
  891. if( note->staff==s->staff && s->tick0 <= note->tick && note->tick < s->tick1 )
  892. note->pitch += s->pitch_offset;
  893. }
  894. }
  895. return kOkXsRC;
  896. }
  897. // The identical pitch may be notated to play simultaneously on different voices.
  898. // As performed on the piano this will equate to a single sounding note.
  899. // This function clears the onset flag on all except one of the duplicated notes.
  900. void _cmXScoreRemoveDuplicateNotes( cmXScore_t* p )
  901. {
  902. cmXsPart_t* pp = p->partL;
  903. // for each part
  904. for(; pp!=NULL; pp=pp->link)
  905. {
  906. cmXsMeas_t* mp = pp->measL;
  907. // for each measure
  908. for(; mp!=NULL; mp=mp->link)
  909. {
  910. cmXsNote_t* np = mp->noteL;
  911. // for each note in this measure
  912. for(; np!=NULL; np=np->slink)
  913. if( cmIsFlag(np->flags,kOnsetXsFl) )
  914. {
  915. cmXsNote_t* n0p = mp->noteL;
  916. for(; n0p!=NULL; n0p=n0p->slink)
  917. if( n0p!=np && cmIsFlag(n0p->flags,kOnsetXsFl) && np->locIdx==n0p->locIdx && np->pitch==n0p->pitch )
  918. n0p->flags = cmClrFlag(n0p->flags,kOnsetXsFl);
  919. }
  920. }
  921. }
  922. }
  923. void _cmXScoreSetMeasGroups( cmXScore_t* p, unsigned flag )
  924. {
  925. unsigned sectionId = 0;
  926. cmXsNote_t* n0 = NULL;
  927. cmXsPart_t* pp = p->partL;
  928. // for each part
  929. for(; pp!=NULL; pp=pp->link)
  930. {
  931. cmXsMeas_t* mp = pp->measL;
  932. // for each measure
  933. for(; mp!=NULL; mp=mp->link)
  934. {
  935. cmXsNote_t* np = mp->noteL;
  936. // for each note in this measure
  937. for(; np!=NULL; np=np->slink)
  938. {
  939. if( cmIsFlag(np->flags,flag) )
  940. n0 = np;
  941. if( cmIsFlag(np->flags,kSectionXsFl) )
  942. {
  943. if( n0 != NULL )
  944. {
  945. switch( flag )
  946. {
  947. case kEvenXsFl: n0->evenGroupId = sectionId; break;
  948. case kDynXsFl: n0->dynGroupId = sectionId; break;
  949. case kTempoXsFl: n0->tempoGroupId= sectionId; break;
  950. }
  951. }
  952. if( cmIsFlag(np->flags,kSectionXsFl) )
  953. sectionId = np->tvalue==NULL ? 0 : strtol(np->tvalue,NULL,10);
  954. n0 = NULL;
  955. }
  956. }
  957. }
  958. }
  959. }
  960. cmXsRC_t _cmXScoreWriteScorePlotFile( cmXScore_t* p, const cmChar_t* fn )
  961. {
  962. cmXsRC_t rc = kOkXsRC;
  963. cmFileH_t fH = cmFileNullHandle;
  964. double ticks_per_sec = 0;
  965. double onset_secs = 0;
  966. if( cmFileOpen(&fH,fn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  967. return cmErrMsg(&p->err,kFileFailXsRC,"Unable to create the file '%s'.",cmStringNullGuard(fn));
  968. cmXsPart_t* pp = p->partL;
  969. for(; pp!=NULL; pp=pp->link)
  970. {
  971. cmXsMeas_t* mp = pp->measL;
  972. for(; mp!=NULL; mp=mp->link)
  973. {
  974. cmFilePrintf(fH,"b %f %i %s B\n",onset_secs,mp->number,"bar");
  975. cmXsNote_t* np = mp->noteL;
  976. unsigned tick0 = 0;
  977. for(; np!=NULL; np=np->slink)
  978. {
  979. if( cmIsFlag(np->flags,kMetronomeXsFl) )
  980. {
  981. double bps = np->duration / 60.0;
  982. // t b t
  983. // - = - -
  984. // s s b
  985. ticks_per_sec = bps * mp->divisions;
  986. }
  987. else
  988. {
  989. if( cmIsFlag(np->flags,kOnsetXsFl) )
  990. {
  991. onset_secs += (np->tick - tick0) / ticks_per_sec;
  992. tick0 = np->tick;
  993. 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");
  994. }
  995. }
  996. }
  997. onset_secs += (mp->divisions * mp->beats - tick0) / ticks_per_sec;
  998. }
  999. }
  1000. cmFileClose(&fH);
  1001. return rc;
  1002. }
  1003. cmXsRC_t cmXScoreInitialize( cmCtx_t* ctx, cmXsH_t* hp, const cmChar_t* xmlFn, const cmChar_t* midiFn )
  1004. {
  1005. cmXsRC_t rc = kOkXsRC;
  1006. if((rc = cmXScoreFinalize(hp)) != kOkXsRC )
  1007. return rc;
  1008. cmXScore_t* p = cmMemAllocZ(cmXScore_t,1);
  1009. cmErrSetup(&p->err,&ctx->rpt,"XScore");
  1010. // create a local linked heap
  1011. if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8196,ctx)) == false )
  1012. return cmErrMsg(&p->err,kLHeapFailXsRC,"Lheap create failed.");
  1013. // open the music xml file
  1014. if( cmXmlAlloc(ctx, &p->xmlH, xmlFn) != kOkXmlRC )
  1015. {
  1016. rc = cmErrMsg(&p->err,kXmlFailXsRC,"Unable to open the MusicXML file '%s'.",cmStringNullGuard(xmlFn));
  1017. goto errLabel;
  1018. }
  1019. //cmXmlPrint(p->xmlH,&ctx->rpt);
  1020. // parse the part-list
  1021. if((rc = _cmXScoreParsePartList( p )) != kOkXsRC )
  1022. goto errLabel;
  1023. // parse each score 'part'
  1024. cmXsPart_t* pp = p->partL;
  1025. for(; pp!=NULL; pp=pp->link)
  1026. if((rc = _cmXScoreParsePart(p,pp)) != kOkXsRC )
  1027. goto errLabel;
  1028. // fill in the note->slink chain to link the notes in each measure in time order
  1029. _cmXScoreSort(p);
  1030. _cmXScoreSpreadGraceNotes(p);
  1031. _cmXScoreSort(p);
  1032. _cmXScoreSetAbsoluteTime(p);
  1033. _cmXScoreResolveTiesAndLoc(p);
  1034. _cmXScoreRemoveDuplicateNotes(p);
  1035. _cmXScoreSetMeasGroups(p,kEvenXsFl);
  1036. _cmXScoreSetMeasGroups(p,kDynXsFl);
  1037. _cmXScoreSetMeasGroups(p,kTempoXsFl);
  1038. //_cmXScoreResolveOctaveShift(p);
  1039. // CSV output initialize failed.
  1040. if( cmCsvInitialize(&p->csvH,ctx) != kOkCsvRC )
  1041. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output object create failed.");
  1042. errLabel:
  1043. if( rc != kOkXsRC )
  1044. _cmXScoreFinalize(p);
  1045. else
  1046. hp->h = p;
  1047. return rc;
  1048. }
  1049. cmXsRC_t cmXScoreFinalize( cmXsH_t* hp )
  1050. {
  1051. cmXsRC_t rc = kOkXsRC;
  1052. if( hp == NULL || cmXScoreIsValid(*hp)==false )
  1053. return kOkXsRC;
  1054. cmXScore_t* p = _cmXScoreHandleToPtr(*hp);
  1055. if((rc = _cmXScoreFinalize(p)) != kOkXsRC )
  1056. return rc;
  1057. hp->h = NULL;
  1058. return rc;
  1059. }
  1060. bool cmXScoreIsValid( cmXsH_t h )
  1061. { return h.h != NULL; }
  1062. /*
  1063. Dynamics File Format:
  1064. <blk>*
  1065. <blk> -> <hdr-line> <note-line> <blank-line>
  1066. <hdr-line> -> <int> "|"
  1067. <note-line> -> <float> <sci-pitch> ":" <int>
  1068. <sci-pitch> -> <A-G><#|b|<space>
  1069. */
  1070. typedef struct cmXsDyn_str
  1071. {
  1072. unsigned bar;
  1073. float rval;
  1074. unsigned pitch;
  1075. char dyn;
  1076. unsigned line;
  1077. } cmXsDyn_t;
  1078. cmXsRC_t _cmXScoreParseDynamicsFile( cmXScore_t* p, const cmChar_t* fn, cmXsDyn_t** aVRef, unsigned* aNRef )
  1079. {
  1080. typedef enum { hdrStateId,noteStateId } state_t;
  1081. cmXsRC_t rc = kOkXsRC;
  1082. cmFileH_t fH = cmFileNullHandle;
  1083. cmChar_t* b = NULL;
  1084. unsigned bN = 0;
  1085. unsigned ln = 1;
  1086. state_t stateId = hdrStateId;
  1087. unsigned bar = 0;
  1088. if( cmFileOpen(&fH,fn,kReadFileFl,p->err.rpt) != kOkFileRC )
  1089. return cmErrMsg(&p->err,kFileFailXsRC,"File open failed on '%s'.",cmStringNullGuard(fn));
  1090. unsigned aN = 0;
  1091. unsigned ai = 0;
  1092. if( cmFileLineCount(fH,&aN) != kOkFileRC )
  1093. {
  1094. rc = cmErrMsg(&p->err,kFileFailXsRC,"File line count acces failed on '%s'.",cmStringNullGuard(fn));
  1095. goto errLabel;
  1096. }
  1097. cmXsDyn_t* aV = cmMemAllocZ(cmXsDyn_t,aN);
  1098. for(; cmFileGetLineAuto(fH,&b,&bN)==kOkFileRC; ++ln)
  1099. {
  1100. char ch;
  1101. if( cmTextIsEmpty(b) )
  1102. {
  1103. stateId = hdrStateId;
  1104. continue;
  1105. }
  1106. switch( stateId )
  1107. {
  1108. case hdrStateId:
  1109. if( sscanf(b,"%i %c",&bar,&ch) != 2 || ch != '|' )
  1110. {
  1111. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"Expected header string read failed on line %i in '%s'.",ln,cmStringNullGuard(fn));
  1112. goto errLabel;
  1113. }
  1114. stateId = noteStateId;
  1115. break;
  1116. case noteStateId:
  1117. {
  1118. float rv;
  1119. char colon;
  1120. char dyn;
  1121. char sps[4];
  1122. sps[3] = 0;
  1123. if(sscanf(b,"%f %c%c%c %c %c", &rv, sps, sps+1, sps+2, &colon, &dyn ) != 6 || colon != ':' )
  1124. {
  1125. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"Expected note string read failed on line %i in '%s'.",ln,cmStringNullGuard(fn));
  1126. goto errLabel;
  1127. }
  1128. //printf("%3i %3.1f %3s %c\n",bar,rv,sps,dyn);
  1129. if( sps[1] == ' ')
  1130. cmTextShrinkS(sps, sps+1, 1 );
  1131. assert(ai<aN);
  1132. aV[ai].bar = bar;
  1133. aV[ai].rval = rv;
  1134. aV[ai].pitch = cmSciPitchToMidi(sps);
  1135. aV[ai].dyn = dyn;
  1136. aV[ai].line = ln;
  1137. ++ai;
  1138. }
  1139. break;
  1140. }
  1141. }
  1142. *aVRef = aV;
  1143. *aNRef = ai;
  1144. errLabel:
  1145. cmMemFree(b);
  1146. cmFileClose(&fH);
  1147. return rc;
  1148. }
  1149. cmXsRC_t cmXScoreInsertDynamics( cmXsH_t h, const cmChar_t* dynFn )
  1150. {
  1151. cmXsRC_t rc = kOkXsRC;
  1152. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1153. cmXsDyn_t* aV = NULL;
  1154. unsigned aN = 0;
  1155. unsigned ai = 0;
  1156. if((rc = _cmXScoreParseDynamicsFile(p, dynFn, &aV, &aN)) != kOkXsRC )
  1157. return rc;
  1158. cmXsPart_t* pp = p->partL;
  1159. for(; pp!=NULL; pp=pp->link)
  1160. {
  1161. cmXsMeas_t* mp = pp->measL;
  1162. for(; mp!=NULL; mp=mp->link)
  1163. {
  1164. cmXsNote_t* np = mp->noteL;
  1165. for(; np!=NULL; np=np->slink)
  1166. if( cmIsFlag(np->flags,kDynXsFl) )
  1167. {
  1168. if( ai >= aN || aV[ai].bar != mp->number || aV[ai].pitch != np->pitch || aV[ai].rval != np->rvalue )
  1169. {
  1170. rc = cmErrMsg(&p->err,kSyntaxErrorXsRC,"Dynamics file mismatch error on dynamics line:%i. expected:%s %f actual:%s %f\n",aV[ai].line,aV[ai].rval,cmMidiToSciPitch(aV[ai].pitch,NULL,0),cmMidiToSciPitch(np->pitch,NULL,0),np->rvalue);
  1171. goto errLabel;
  1172. }
  1173. if( '1' <= aV[ai].dyn && aV[ai].dyn <= '9' )
  1174. np->dynamics = strtol(&aV[ai].dyn,NULL,10);
  1175. ++ai;
  1176. }
  1177. }
  1178. }
  1179. errLabel:
  1180. cmMemFree(aV);
  1181. return rc;
  1182. }
  1183. /*
  1184. kMidiFileIdColScIdx= 0,
  1185. kTypeLabelColScIdx = 3,
  1186. kDSecsColScIdx = 4,
  1187. kSecsColScIdx = 5,
  1188. kD0ColScIdx = 9,
  1189. kD1ColScIdx = 10,
  1190. kPitchColScIdx = 11,
  1191. kBarColScIdx = 13,
  1192. kSkipColScIdx = 14,
  1193. kEvenColScIdx = 15,
  1194. kGraceColScIdx = 16,
  1195. kTempoColScIdx = 17,
  1196. kFracColScIdx = 18,
  1197. kDynColScIdx = 19,
  1198. kSectionColScIdx = 20,
  1199. kRecdPlayColScIdx = 21,
  1200. kRemarkColScIdx = 22
  1201. */
  1202. cmXsRC_t _cmXScoreWriteCsvHdr( cmXScore_t* p )
  1203. {
  1204. const cmChar_t* s[] =
  1205. {
  1206. "id","trk","evt","opcode","dticks","micros","status",
  1207. "meta","ch","d0","d1","arg0","arg1","bar","skip",
  1208. "even","grace","tempo","t frac","dyn","section","play_recd","remark",NULL
  1209. };
  1210. cmCsvCell_t* lcp = NULL;
  1211. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymText(p->csvH,s[0]), 0, 0 ) != kOkCsvRC )
  1212. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  1213. unsigned i;
  1214. for(i=1; s[i]!=NULL; ++i)
  1215. {
  1216. if( cmCsvInsertIdentColAfter(p->csvH, lcp, &lcp, s[i], 0 ) != kOkCsvRC )
  1217. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV error inserting CSV title %i.\n",i);
  1218. }
  1219. return kOkXsRC;
  1220. }
  1221. cmXsRC_t _cmXScoreWriteCsvBlankCols( cmXScore_t* p, unsigned cnt, cmCsvCell_t** leftCellPtrPtr )
  1222. {
  1223. unsigned i;
  1224. for(i=0; i<cnt; ++i)
  1225. if( cmCsvInsertIdentColAfter(p->csvH,*leftCellPtrPtr,leftCellPtrPtr,"",0) != kOkCsvRC )
  1226. return cmErrMsg(&p->err,kCsvFailXsRC,"CSV output failed on blank column.");
  1227. return kOkCsvRC;
  1228. }
  1229. const cmChar_t* _cmXScoreTranslateDynamics( cmXScore_t* p, const cmXsNote_t* np )
  1230. {
  1231. if( cmIsFlag(np->flags,kDynXsFl) && np->dynamics != 0 )
  1232. {
  1233. switch(np->dynamics)
  1234. {
  1235. case 1: return "pppp";
  1236. case 2: return "ppp";
  1237. case 3: return "pp";
  1238. case 4: return "p";
  1239. case 5: return "mp";
  1240. case 6: return "mf";
  1241. case 7: return "f";
  1242. case 8: return "ff";
  1243. case 9: return "fff";
  1244. }
  1245. cmErrMsg(&p->err,kSyntaxErrorXsRC,"An invalid dynamic value (%i) was encountered.",np->dynamics);
  1246. }
  1247. return "";
  1248. }
  1249. cmXsRC_t _cmXScoreWriteCsvRow(
  1250. cmXScore_t* p,
  1251. unsigned rowIdx,
  1252. unsigned uid,
  1253. unsigned bar,
  1254. const cmChar_t* sectionStr,
  1255. const cmChar_t* opCodeStr,
  1256. double dsecs,
  1257. double secs,
  1258. unsigned d0,
  1259. unsigned d1,
  1260. unsigned pitch, // set to -1 if the pitch is not valid
  1261. double frac,
  1262. const cmChar_t* dynStr,
  1263. unsigned flags )
  1264. {
  1265. cmXsRC_t rc = kOkXsRC;
  1266. cmCsvCell_t* lcp = NULL;
  1267. // append an empty row to the CSV object
  1268. if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymUInt(p->csvH, rowIdx ), 0, 0 ) != kOkCsvRC )
  1269. {
  1270. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
  1271. goto errLabel;
  1272. }
  1273. /*
  1274. // col 0 : blanks
  1275. if( cmCsvInsertUIntColAfter(p->csvH, lcp, &lcp, rowIdx, 0 ) != kOkCsvRC )
  1276. {
  1277. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output row index failed.");
  1278. goto errLabel;
  1279. }
  1280. */
  1281. // col 1 : track (always 1)
  1282. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,1,0) != kOkCsvRC )
  1283. {
  1284. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1285. goto errLabel;
  1286. }
  1287. // col 2 : evt (set to event uid, or blank if uid == -1)
  1288. if( uid == -1 )
  1289. {
  1290. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1291. goto errLabel;
  1292. }
  1293. else
  1294. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,uid,0) != kOkCsvRC )
  1295. {
  1296. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1297. goto errLabel;
  1298. }
  1299. // col 3 : output the opcode
  1300. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,opCodeStr,0) != kOkCsvRC )
  1301. {
  1302. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on opcode label.");
  1303. goto errLabel;
  1304. }
  1305. // col 4 : dsecs
  1306. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,dsecs,0) != kOkCsvRC )
  1307. {
  1308. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dsecs'.");
  1309. goto errLabel;
  1310. }
  1311. // col 5 : secs
  1312. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,secs,0) != kOkCsvRC )
  1313. {
  1314. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'secs'.");
  1315. goto errLabel;
  1316. }
  1317. // cols 6,7,8 blanks
  1318. if((rc = _cmXScoreWriteCsvBlankCols(p,3,&lcp)) != kOkXsRC )
  1319. goto errLabel;
  1320. // col 9 : d0
  1321. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d0,0) != kOkCsvRC )
  1322. {
  1323. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
  1324. goto errLabel;
  1325. }
  1326. // col 10 : d1
  1327. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d1,0) != kOkCsvRC )
  1328. {
  1329. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd1'.");
  1330. goto errLabel;
  1331. }
  1332. // col 11 : pitch
  1333. if( pitch == -1 )
  1334. {
  1335. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1336. goto errLabel;
  1337. }
  1338. else
  1339. {
  1340. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmMidiToSciPitch(pitch,NULL,0),0) != kOkCsvRC )
  1341. {
  1342. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  1343. goto errLabel;
  1344. }
  1345. }
  1346. // col 12 : blanks
  1347. if((rc = _cmXScoreWriteCsvBlankCols(p,1 + (cmIsFlag(flags,kBarXsFl) ? 0 : 1), &lcp)) != kOkXsRC )
  1348. goto errLabel;
  1349. // col 13 : bar number
  1350. if( cmIsFlag(flags,kBarXsFl) )
  1351. {
  1352. if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,bar,0) != kOkCsvRC )
  1353. {
  1354. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
  1355. goto errLabel;
  1356. }
  1357. }
  1358. // col 14 : skip (blank for now)
  1359. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1360. goto errLabel;
  1361. // col 15: even (all grace notes are 'even' notes
  1362. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp, cmIsFlag(flags,kGraceXsFl) | cmIsFlag(flags,kEvenXsFl) ? "e" : "",0) != kOkCsvRC )
  1363. {
  1364. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1365. goto errLabel;
  1366. }
  1367. // col 16: grace
  1368. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kGraceXsFl) ? "g" : "",0) != kOkCsvRC )
  1369. {
  1370. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1371. goto errLabel;
  1372. }
  1373. // col 17: tempo
  1374. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kTempoXsFl) ? "t" : "",0) != kOkCsvRC )
  1375. {
  1376. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1377. goto errLabel;
  1378. }
  1379. // col 18: frac
  1380. if( frac == 0 )
  1381. {
  1382. if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1383. goto errLabel;
  1384. }
  1385. else
  1386. {
  1387. if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,1.0/frac,0) != kOkCsvRC )
  1388. {
  1389. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 't frac'.");
  1390. goto errLabel;
  1391. }
  1392. }
  1393. // col 19: dynamic marking
  1394. if( cmCsvInsertIdentColAfter(p->csvH, lcp, &lcp, dynStr, 0 ) != kOkCsvRC )
  1395. {
  1396. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dynamics'.");
  1397. goto errLabel;
  1398. }
  1399. //if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
  1400. // goto errLabel;
  1401. // col 20: section
  1402. if( cmCsvInsertIdentColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kSectionXsFl) ? sectionStr : "",0) != kOkCsvRC )
  1403. {
  1404. rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
  1405. goto errLabel;
  1406. }
  1407. // col 21, 22 : recd-play, remark (blank for now)
  1408. if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
  1409. goto errLabel;
  1410. errLabel:
  1411. return rc;
  1412. }
  1413. cmXsRC_t cmXScoreWriteCsv( cmXsH_t h, const cmChar_t* csvFn )
  1414. {
  1415. cmXsRC_t rc = kOkXsRC;
  1416. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1417. unsigned rowIdx = 1;
  1418. const cmChar_t* sectionIdStr = NULL;
  1419. if( !cmCsvIsValid(p->csvH) )
  1420. return cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output object is not initialized.");
  1421. if((rc = _cmXScoreWriteCsvHdr( p )) != kOkXsRC )
  1422. goto errLabel;
  1423. cmXsPart_t* pp = p->partL;
  1424. for(; pp!=NULL; pp=pp->link)
  1425. {
  1426. cmXsMeas_t* mp = pp->measL;
  1427. for(; mp!=NULL; mp=mp->link)
  1428. {
  1429. cmXsNote_t* np = mp->noteL;
  1430. for(; np!=NULL; np=np->slink)
  1431. {
  1432. // if this is a section event
  1433. if( cmIsFlag(np->flags,kSectionXsFl) )
  1434. sectionIdStr = np->tvalue;
  1435. // if this is a bar event
  1436. if( cmIsFlag(np->flags,kBarXsFl) )
  1437. {
  1438. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,sectionIdStr,"bar",np->dsecs,np->secs,0,0,-1,0,"",np->flags);
  1439. sectionIdStr = NULL;
  1440. }
  1441. else
  1442. {
  1443. // if this is a pedal event
  1444. if( cmIsFlag(np->flags,kPedalDnXsFl|kPedalUpXsFl|kPedalUpDnXsFl) )
  1445. {
  1446. unsigned d0 = 64; // pedal MIDI ctl id
  1447. unsigned d1 = cmIsFlag(np->flags,kPedalDnXsFl) ? 64 : 0; // pedal-dn: d1>=64 pedal-up:<64
  1448. _cmXScoreWriteCsvRow(p,rowIdx,-1,mp->number,sectionIdStr,"ctl",np->dsecs,np->secs,d0,d1,-1,0,"",np->flags);
  1449. sectionIdStr = NULL;
  1450. }
  1451. else
  1452. {
  1453. // if this is a sounding note event
  1454. if( cmIsFlag(np->flags,kOnsetXsFl) )
  1455. {
  1456. double frac = np->rvalue + (cmIsFlag(np->flags,kDotXsFl) ? (np->rvalue/2) : 0);
  1457. const cmChar_t* dyn = _cmXScoreTranslateDynamics( p, np );
  1458. //
  1459. _cmXScoreWriteCsvRow(p,rowIdx,np->uid,mp->number,sectionIdStr,"non",np->dsecs,np->secs,np->pitch,60,np->pitch,frac,dyn,np->flags);
  1460. sectionIdStr = NULL;
  1461. }
  1462. }
  1463. }
  1464. rowIdx += 1;
  1465. }
  1466. }
  1467. }
  1468. // Section labels are output on the next bar/pedal/note
  1469. // but what if there is no bar pedal note-on after the section label
  1470. if( sectionIdStr != NULL )
  1471. cmErrMsg(&p->err,kSyntaxErrorXsRC,"The section label '%s' was ignored because it was not followed by a score event.",sectionIdStr);
  1472. if( cmCsvWrite( p->csvH, csvFn ) != kOkCsvRC )
  1473. rc = cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output write failed on file '%s'.",csvFn);
  1474. errLabel:
  1475. return rc;
  1476. }
  1477. void _cmXScoreReportTitle( cmRpt_t* rpt )
  1478. {
  1479. cmRptPrintf(rpt," voc loc tick durtn rval flags\n");
  1480. cmRptPrintf(rpt," --- ----- ------- ----- ---- --- -------------\n");
  1481. }
  1482. void _cmXScoreReportNote( cmRpt_t* rpt, const cmXsNote_t* note )
  1483. {
  1484. const cmChar_t* B = cmIsFlag(note->flags,kBarXsFl) ? "|" : "-";
  1485. const cmChar_t* R = cmIsFlag(note->flags,kRestXsFl) ? "R" : "-";
  1486. const cmChar_t* G = cmIsFlag(note->flags,kGraceXsFl) ? "G" : "-";
  1487. const cmChar_t* D = cmIsFlag(note->flags,kDotXsFl) ? "." : "-";
  1488. const cmChar_t* C = cmIsFlag(note->flags,kChordXsFl) ? "C" : "-";
  1489. const cmChar_t* e = cmIsFlag(note->flags,kEvenXsFl) ? "e" : "-";
  1490. const cmChar_t* d = cmIsFlag(note->flags,kDynXsFl) ? "d" : "-";
  1491. const cmChar_t* t = cmIsFlag(note->flags,kTempoXsFl) ? "t" : "-";
  1492. const cmChar_t* P = cmIsFlag(note->flags,kPedalDnXsFl) ? "V" : "-";
  1493. const cmChar_t* S = cmIsFlag(note->flags,kSectionXsFl) ? "S" : "-";
  1494. const cmChar_t* H = cmIsFlag(note->flags,kHeelXsFl) ? "H" : "-";
  1495. const cmChar_t* T0 = cmIsFlag(note->flags,kTieBegXsFl) ? "T" : "-";
  1496. const cmChar_t* T1 = cmIsFlag(note->flags,kTieEndXsFl) ? "_" : "-";
  1497. const cmChar_t* O = cmIsFlag(note->flags,kOnsetXsFl) ? "*" : "-";
  1498. P = cmIsFlag(note->flags,kPedalUpXsFl) ? "^" : P;
  1499. P = cmIsFlag(note->flags,kPedalUpDnXsFl) ? "X" : P;
  1500. //const cmChar_t* N = note->pitch==0 ? " " : cmMidiToSciPitch( note->pitch, NULL, 0 );
  1501. cmChar_t N[] = {'\0','\0','\0','\0'};
  1502. cmChar_t acc = note->alter==-1?'b':(note->alter==1?'#':' ');
  1503. snprintf(N,4,"%c%c%1i",note->step,acc,note->octave);
  1504. cmRptPrintf(rpt," %3i %5i %7i %5i %4.1f %3s %s%s%s%s%s%s%s%s%s%s%s%s%s%s",
  1505. note->voice->id,
  1506. note->locIdx,
  1507. note->tick,
  1508. note->duration,
  1509. note->rvalue,
  1510. N,B,R,G,D,C,e,d,t,P,S,H,T0,T1,O);
  1511. if( cmIsFlag(note->flags,kSectionXsFl) )
  1512. cmRptPrintf(rpt," %s",cmStringNullGuard(note->tvalue));
  1513. if( cmIsFlag(note->flags,kMetronomeXsFl) )
  1514. cmRptPrintf(rpt," %i bpm",note->duration);
  1515. if( note->evenGroupId != 0 )
  1516. cmRptPrintf(rpt," e=%i",note->evenGroupId);
  1517. if( note->dynGroupId != 0 )
  1518. cmRptPrintf(rpt," d=%i",note->dynGroupId);
  1519. if( note->tempoGroupId != 0 )
  1520. cmRptPrintf(rpt," t=%i",note->tempoGroupId);
  1521. }
  1522. void cmXScoreReport( cmXsH_t h, cmRpt_t* rpt, bool sortFl )
  1523. {
  1524. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1525. cmXsPart_t* pp = p->partL;
  1526. for(; pp!=NULL; pp=pp->link)
  1527. {
  1528. cmRptPrintf(rpt,"Part:%s\n",pp->idStr);
  1529. const cmXsMeas_t* meas = pp->measL;
  1530. for(; meas!=NULL; meas=meas->link)
  1531. {
  1532. 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);
  1533. _cmXScoreReportTitle(rpt);
  1534. if( sortFl )
  1535. {
  1536. const cmXsNote_t* note = meas->noteL;
  1537. for(; note!=NULL; note=note->slink)
  1538. {
  1539. _cmXScoreReportNote(rpt,note);
  1540. if( note->slink!=NULL || note->voice->id==0)
  1541. cmRptPrintf(rpt,"\n");
  1542. else
  1543. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  1544. }
  1545. }
  1546. else
  1547. {
  1548. const cmXsVoice_t* v = meas->voiceL;
  1549. for(; v!=NULL; v=v->link)
  1550. {
  1551. const cmXsNote_t* note = v->noteL;
  1552. cmRptPrintf(rpt," voice:%i\n",v->id);
  1553. for(; note!=NULL; note=note->mlink)
  1554. {
  1555. _cmXScoreReportNote(rpt,note);
  1556. if( note->mlink!=NULL || note->voice->id==0)
  1557. cmRptPrintf(rpt,"\n");
  1558. else
  1559. cmRptPrintf(rpt," %i\n", note->tick + note->duration);
  1560. }
  1561. }
  1562. }
  1563. }
  1564. }
  1565. }
  1566. cmXsRC_t cmXScoreWriteMidi( cmXsH_t h, const cmChar_t* fn )
  1567. {
  1568. assert(0); // function not implemented
  1569. cmXScore_t* p = _cmXScoreHandleToPtr(h);
  1570. cmXsPart_t* pp = p->partL;
  1571. for(; pp!=NULL; pp=pp->link)
  1572. {
  1573. const cmXsMeas_t* meas = pp->measL;
  1574. for(; meas!=NULL; meas=meas->link)
  1575. {
  1576. const cmXsNote_t* note = meas->noteL;
  1577. for(; note!=NULL; note=note->slink)
  1578. {
  1579. }
  1580. }
  1581. }
  1582. }
  1583. cmXsRC_t cmXScoreTest( cmCtx_t* ctx, const cmChar_t* xmlFn, const cmChar_t* midiFn, const cmChar_t* outFn, const cmChar_t* dynFn )
  1584. {
  1585. cmXsRC_t rc;
  1586. cmXsH_t h = cmXsNullHandle;
  1587. if((rc = cmXScoreInitialize( ctx, &h, xmlFn, midiFn)) != kOkXsRC )
  1588. return cmErrMsg(&ctx->err,rc,"XScore alloc failed.");
  1589. if( dynFn != NULL )
  1590. cmXScoreInsertDynamics(h, dynFn );
  1591. if( outFn != NULL )
  1592. cmXScoreWriteCsv(h,outFn);
  1593. cmXScoreReport(h,&ctx->rpt,true);
  1594. return cmXScoreFinalize(&h);
  1595. }