12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079 |
- #include "cmPrefix.h"
- #include "cmGlobal.h"
- #include "cmFloatTypes.h"
- #include "cmRpt.h"
- #include "cmErr.h"
- #include "cmCtx.h"
- #include "cmMem.h"
- #include "cmMallocDebug.h"
- #include "cmLinkedHeap.h"
- #include "cmXml.h"
- #include "cmText.h"
- #include "cmXScore.h"
- #include "cmTime.h"
- #include "cmMidi.h"
- #include "cmLex.h"
- #include "cmCsv.h"
-
- cmXsH_t cmXsNullHandle = cmSTATIC_NULL_HANDLE;
-
- enum
- {
- kSectionXsFl = 0x0001, // rvalue holds section number
- kBarXsFl = 0x0002,
- kRestXsFl = 0x0004,
- kGraceXsFl = 0x0008,
- kDotXsFl = 0x0010,
- kChordXsFl = 0x0020,
- kDynXsFl = 0x0040,
- kEvenXsFl = 0x0080,
- kTempoXsFl = 0x0100,
- kHeelXsFl = 0x0200,
- kPedalDnXsFl = 0x0400,
- kPedalUpXsFl = 0x0800,
- kPedalUpDnXsFl = 0x1000,
- kMetronomeXsFl = 0x2000 // duration holds BPM
- };
-
-
- typedef struct cmXsNote_str
- {
- unsigned flags; // See k???XsFl
- unsigned pitch; // midi pitch
- unsigned tick; //
- unsigned duration; // duration in ticks
- 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, ...)
- const cmChar_t* tvalue; // text value
- struct cmXsNote_str* mlink; // measure note list
- struct cmXsNote_str* slink; // time sorted event list
- } cmXsNote_t;
-
- typedef struct cmXsVoice_str
- {
- unsigned id; // Voice id
- cmXsNote_t* noteL; // List of notes in this voice
- struct cmXsVoice_str* link; // Link to other voices in this measure
- } cmXsVoice_t;
-
- typedef struct cmXsMeas_str
- {
- unsigned number; // Measure number
- unsigned divisions; // ticks-per-quarter-note
- unsigned beats; // beats per measure
- unsigned beat_type; // whole/half/quarter/eighth ...
-
- cmXsVoice_t* voiceL; // List of voices in this measure
- cmXsNote_t* noteL; // List of time sorted notes in this measure
-
- struct cmXsMeas_str* link; // Link to other measures in this part.
- } cmXsMeas_t;
-
- typedef struct cmXsPart_str
- {
- const cmChar_t* idStr; // Id of this part
- cmXsMeas_t* measL; // List of measures in this part.
- struct cmXsPart_str* link; // Link to other parts in this score
- } cmXsPart_t;
-
- typedef struct
- {
- cmErr_t err;
- cmXmlH_t xmlH;
- cmLHeapH_t lhH;
- cmXsPart_t* partL;
- cmCsvH_t csvH;
- } cmXScore_t;
-
- cmXScore_t* _cmXScoreHandleToPtr( cmXsH_t h )
- {
- cmXScore_t* p = (cmXScore_t*)h.h;
- assert( p != NULL );
- return p;
- }
-
- cmXsRC_t _cmXScoreFinalize( cmXScore_t* p )
- {
- cmXsRC_t rc = kOkXsRC;
-
- // release the XML file
- cmXmlFree( &p->xmlH );
-
- // release the local linked heap memory
- cmLHeapDestroy(&p->lhH);
-
- // release the CSV output object
- cmCsvFinalize(&p->csvH);
-
- cmMemFree(p);
-
- return rc;
- }
-
- cmXsRC_t _cmXScoreMissingNode( cmXScore_t* p, const cmXmlNode_t* parent, const cmChar_t* label )
- {
- return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML node '%s'. Parent line:%i",label,parent->line);
- }
-
- cmXsRC_t _cmXScoreMissingAttribute( cmXScore_t* p, const cmXmlNode_t* np, const cmChar_t* attrLabel )
- {
- return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML attribute '%s' from node '%s'.",attrLabel,np->label);
- }
-
- cmXsVoice_t* _cmXScoreIdToVoice( cmXsMeas_t* meas, unsigned voiceId )
- {
- cmXsVoice_t* v = meas->voiceL;
- for(; v!=NULL; v=v->link)
- if( v->id == voiceId )
- return v;
-
- return NULL;
- }
-
- cmXsRC_t _cmXScorePushNote( cmXScore_t* p, cmXsMeas_t* meas, unsigned voiceId, cmXsNote_t* note )
- {
- cmXsVoice_t* v;
-
- // get the voice recd
- if((v = _cmXScoreIdToVoice(meas,voiceId)) == NULL)
- {
- // the voice recd doesn't exist for this voiceId - allocate one
- v = cmLhAllocZ(p->lhH,cmXsVoice_t,1);
- v->id = voiceId;
-
- // add the voice record to the meas->voiceL
- if( meas->voiceL == NULL )
- meas->voiceL = v;
- else
- {
- cmXsVoice_t* vp = meas->voiceL;
- while( vp->link!=NULL )
- vp = vp->link;
-
- vp->link = v;
- }
- }
-
- // add the note record to the end of meas->voiceL
- if( v->noteL == NULL )
- v->noteL = note;
- else
- {
- cmXsNote_t* n = v->noteL;
- while( n->mlink != NULL )
- n = n->mlink;
-
- n->mlink = note;
- }
-
- return kOkXsRC;
- }
-
- cmXsRC_t _cmXScoreParsePartList( cmXScore_t* p )
- {
- cmXsRC_t rc = kOkXsRC;
- cmXsPart_t* lastPartPtr = NULL;
- const cmXmlNode_t* xnp;
-
- // find the 'part-list'
- if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part-list", NULL, 0)) == NULL )
- return _cmXScoreMissingNode(p,cmXmlRoot(p->xmlH),"part-list");
-
- const cmXmlNode_t* cnp = xnp->children;
-
- // for each child of the 'part-list'
- for(; cnp!=NULL; cnp=cnp->sibling)
- if( cmTextCmp( cnp->label, "score-part" ) == 0 )
- {
- const cmXmlAttr_t* a;
-
- // find the 'score-part' id
- if((a = cmXmlFindAttrib(cnp,"id")) == NULL )
- return _cmXScoreMissingAttribute(p,cnp,"id");
-
- // allocate a new part record
- cmXsPart_t* pp = cmLhAllocZ(p->lhH,cmXsPart_t,1);
-
- pp->idStr = a->value; // set the part id
-
- // link the new part record to the end of the part list
- if(lastPartPtr == NULL)
- p->partL = pp;
- else
- lastPartPtr->link = pp;
-
- lastPartPtr = pp;
- }
-
- return rc;
- }
-
- cmXsRC_t _cmXScoreParsePitch( cmXScore_t* p, const cmXmlNode_t* nnp, unsigned* midiPitchRef )
- {
- cmXsRC_t rc = kOkXsRC;
- unsigned octave = 0;
- double alter = 0;
- const cmChar_t* step = NULL;
-
- if((step = cmXmlNodeValue(nnp,"pitch","step",NULL)) == NULL )
- return _cmXScoreMissingNode(p,nnp,"step");
-
- if((rc = cmXmlNodeUInt( nnp,&octave,"pitch","octave",NULL)) != kOkXmlRC )
- return _cmXScoreMissingNode(p,nnp,"octave");
-
- cmXmlNodeDouble( nnp,&alter,"pitch","alter",NULL);
-
- cmChar_t buf[3] = { *step, '0', '\0'};
- unsigned midi = cmSciPitchToMidi(buf);
-
- midi += (12 * octave);
-
- midi += alter;
-
- *midiPitchRef = midi;
-
- return rc;
- }
-
- cmXsRC_t _cmXScoreParseNoteRValue( cmXScore_t* p, const cmXmlNode_t* nnp, const cmChar_t* label, double* rvalueRef )
- {
- typedef struct map_str
- {
- double rvalue;
- const cmChar_t* label;
- } map_t;
-
- map_t mapV[] =
- {
- {-1.0, "measure" }, // whole measure rest
- { 0.5, "breve" }, // double whole
- { 1.0, "whole" },
- { 2.0, "half" },
- { 4.0, "quarter" },
- { 8.0, "eighth" },
- {16.0, "16th" },
- {32.0, "32nd" },
- { 64.0, "64th" },
- {128.0, "128th" },
- { 0.0, "" }
- };
-
- const cmChar_t* str;
- // get the XML rvalue label
- if((str = cmXmlNodeValue(nnp,label,NULL)) == NULL)
- {
- if((nnp = cmXmlSearch(nnp,"rest",NULL,0)) != NULL )
- {
- const cmXmlAttr_t* a;
- if((a = cmXmlFindAttrib(nnp,"measure")) != NULL && cmTextCmp(a->value,"yes")==0)
- {
- *rvalueRef = -1;
- return kOkXsRC;
- }
- }
-
- return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'beat-unit' metronome value is missing on line %i.",nnp->line);
- }
-
- unsigned i;
- // lookup the rvalue numeric value from the mapV[] table
- for(i=0; mapV[i].rvalue!=0; ++i)
- if( cmTextCmp(mapV[i].label,str) == 0 )
- {
- *rvalueRef = mapV[i].rvalue;
- return kOkXsRC;
- }
-
- // the rvalue label was not found
- return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unknown rvalue type='%s'.",str);
- }
-
- cmXsRC_t _cmXScoreParseColor( cmXScore_t* p, const cmXmlNode_t* nnp, cmXsNote_t* note )
- {
- cmXsRC_t rc = kOkXsRC;
- const cmXmlAttr_t* a;
-
- typedef struct map_str
- {
- unsigned value;
- const cmChar_t* label;
- } map_t;
-
- map_t mapV[] =
- {
- { kEvenXsFl, "#0000FF" }, // blue (even)
- { kTempoXsFl, "#00FF00" }, // green (tempo)
- { kDynXsFl, "#FF0000" }, // red (dynamics)
- { kTempoXsFl | kEvenXsFl, "#00FFFF" }, // green + blue (turquoise)
- { kDynXsFl | kEvenXsFl, "#FF00FF" }, // red + blue
- { kDynXsFl | kEvenXsFl, "#FF0CF7" }, // magenta (even+dyn)
- { kDynXsFl | kTempoXsFl, "#FF7F00" }, // red + green (brown)
- { kTempoXsFl | kEvenXsFl | kDynXsFl, "#996633" }, // (purple)
- { kDynXsFl, "#FF6A03" }, // 176 orange (dynamics)
- { kEvenXsFl, "#2F00E8" }, // 1001 blue (even)
- { kTempoXsFl, "#01CD1F" }, // 1196 green (tempo)
- { kEvenXsFl, "#3600E8" }, // 1627 blue (even)
- { kDynXsFl | kTempoXsFl, "#9E8F15" }, // 8827 brown (dyn + tempo)
- { kEvenXsFl, "#2E00E6" }, // 5393 blue (even)
- { kEvenXsFl, "#2C00DD" }, // 5895 blue (even)
- { kDynXsFl, "#FF5B03" }, // 6498 orange (dyn)
- { kDynXsFl, "#FF6104" }, // 6896 orange
- { kEvenXsFl, "#2A00E6" }, // 7781 blue
- { kEvenXsFl, "#2300DD" }, // 8300 blue (even)
- { kTempoXsFl, "#03CD22" }, // 10820 green (tempo)
- { kEvenXsFl, "#3400DB" }, // 11627 blue (dyn)
- { -1, "" }
- };
-
- /*
- orange #FF6A03
- magenta #FF0CF7
- blue #2F00E8
- green #01CD1F
- gold #9E8F15
- green #03CD22
- */
-
- if((a = cmXmlFindAttrib(nnp, "color" )) != NULL )
- {
- unsigned i;
- for(i=0; mapV[i].value != -1; ++i)
- if( cmTextCmp(a->value,mapV[i].label) == 0 )
- {
- note->flags += mapV[i].value;
- break;
- }
-
- if( mapV[i].value == -1 )
- cmErrMsg(&p->err,kSyntaxErrorXsRC,"The note color '%s' was not found on line %i.",a->value,nnp->line);
- }
-
- return rc;
- }
-
- cmXsRC_t _cmXScoreParseNote(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* nnp, unsigned* tickRef )
- {
- cmXsRC_t rc = kOkXsRC;
- cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
- unsigned voiceId;
-
- // get the voice id for this node
- if( cmXmlNodeUInt(nnp,&voiceId,"voice",NULL) != kOkXmlRC )
- return _cmXScoreMissingNode(p,nnp,"voice");
-
- // if this note has a pitch
- if( cmXmlNodeHasChild(nnp,"pitch",NULL) )
- if((rc = _cmXScoreParsePitch(p,nnp,¬e->pitch)) != kOkXsRC )
- return rc;
-
- // get the note duration
- cmXmlNodeUInt(nnp,¬e->duration,"duration",NULL);
-
- // is 'rest'
- if( cmXmlNodeHasChild(nnp,"rest",NULL) )
- note->flags |= kRestXsFl;
-
- // is 'grace'
- if( cmXmlNodeHasChild(nnp,"grace",NULL) )
- note->flags |= kGraceXsFl;
-
- // is 'dot'
- if( cmXmlNodeHasChild(nnp,"dot",NULL) )
- note->flags |= kDotXsFl;
-
- // is 'chord'
- if( cmXmlNodeHasChild(nnp,"chord",NULL) )
- note->flags |= kChordXsFl;
-
- // has 'heel' mark
- if( cmXmlNodeHasChild(nnp,"notations","technical","heel",NULL) )
- note->flags |= kHeelXsFl;
-
- // set color coded flags
- if((rc = _cmXScoreParseColor(p, nnp, note )) != kOkXsRC )
- return rc;
-
- // get the note's rythmic value
- if((rc = _cmXScoreParseNoteRValue(p,nnp,"type",¬e->rvalue)) != kOkXsRC )
- return rc;
-
- note->tick = *tickRef;
-
- if( cmIsNotFlag(note->flags,kChordXsFl) )
- *tickRef += note->duration;
-
- return _cmXScorePushNote(p, meas, voiceId, note );
- }
-
- cmXsRC_t _cmXScorePushNonNote( cmXScore_t* p, cmXsMeas_t* meas, unsigned tick, unsigned duration, double rvalue, const cmChar_t* tvalue, unsigned flags )
- {
- cmXsNote_t* note = cmLhAllocZ(p->lhH,cmXsNote_t,1);
- unsigned voiceId = 0; // non-note's are always assigned to voiceId=0;
-
- note->tick = tick;
- note->flags = flags;
- note->rvalue = rvalue;
- note->tvalue = tvalue;
- note->duration = duration;
-
- return _cmXScorePushNote(p, meas, voiceId, note );
- }
-
- cmXsRC_t _cmXScoreParseDirection(cmXScore_t* p, cmXsMeas_t* meas, const cmXmlNode_t* dnp, unsigned tick)
- {
- cmXsRC_t rc = kOkXsRC;
- const cmXmlNode_t* np = NULL;
- const cmXmlAttr_t* a = NULL;
- unsigned flags = 0;
- int offset = 0;
- double rvalue = 0;
- const cmChar_t* tvalue = NULL;
- unsigned duration = 0;
-
- cmXmlNodeInt(dnp, &offset, "offset", NULL );
-
- // if this is a metronome direction
- if((np = cmXmlSearch( dnp, "metronome", NULL, 0)) != NULL )
- {
-
- if( cmXmlNodeUInt(np,&duration,"per-minute",NULL) != kOkXmlRC )
- return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The 'per-minute' metronome value is missing on line %i.",np->line);
-
- if((rc = _cmXScoreParseNoteRValue(p,np,"beat-unit",&rvalue)) != kOkXsRC )
- return rc;
-
- flags = kMetronomeXsFl;
- }
- else
-
- // if this is a pedal direction
- if((np = cmXmlSearch( dnp, "pedal",NULL,0)) != NULL )
- {
-
- if((a = cmXmlFindAttrib(np,"type")) == NULL )
- return _cmXScoreMissingAttribute(p, np, "type" );
-
- if( cmTextCmp(a->value,"start") == 0 )
- flags = kPedalDnXsFl;
- else
- if( cmTextCmp(a->value,"change") == 0 )
- flags = kPedalUpDnXsFl;
- else
- if( cmTextCmp(a->value,"stop") == 0 )
- flags = kPedalUpXsFl;
- else
- return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Unrecognized pedal type:'%s'.",cmStringNullGuard(a->value));
- }
- else
-
- // if this is a 'words' direction
- if((np = cmXmlSearch( dnp, "words", NULL, 0)) != NULL )
- {
- if((a = cmXmlFindAttrib(np,"enclosure")) != NULL && cmTextCmp(a->value,"rectangle")==0 )
- {
- if( cmTextIsEmpty( tvalue = np->dataStr ) )
- return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Section number is blank or missing on line %i.",np->line);
-
- flags = kSectionXsFl;
- }
- }
-
- return _cmXScorePushNonNote(p,meas,tick+offset,duration,rvalue,tvalue,flags);
-
- }
-
-
- cmXsRC_t _cmXScoreParseMeasure(cmXScore_t* p, cmXsPart_t* pp, const cmXmlNode_t* mnp)
- {
- cmXsRC_t rc = kOkXsRC;
- const cmXmlNode_t* np = NULL;
- unsigned tick = 0;
-
- // allocate the 'measure' record
- cmXsMeas_t* meas = cmLhAllocZ(p->lhH,cmXsMeas_t,1);
-
- // get measure number
- if( cmXmlAttrUInt(mnp,"number", &meas->number) != kOkXmlRC )
- return _cmXScoreMissingAttribute(p,mnp,"number");
-
- if( pp->measL == NULL )
- pp->measL = meas;
- else
- {
- cmXsMeas_t* m = pp->measL;
- while( m->link != NULL )
- m = m->link;
-
- m->link = meas;
- }
-
- // get measure attributes node
- if((np = cmXmlSearch(mnp,"attributes",NULL,0)) != NULL)
- {
- cmXmlNodeUInt(np,&meas->divisions,"divisions",NULL);
- cmXmlNodeUInt(np,&meas->beats, "time","beats",NULL);
- cmXmlNodeUInt(np,&meas->beat_type,"time","beat-type",NULL);
- }
-
- // store the bar line
- if((rc = _cmXScorePushNonNote(p,meas,tick,0,0,NULL,kBarXsFl)) != kOkXsRC )
- return rc;
-
-
- np = mnp->children;
-
- // for each child of the 'meas' XML node
- for(; rc==kOkXsRC && np!=NULL; np=np->sibling)
- {
- // if this is a 'note' node
- if( cmTextCmp(np->label,"note") == 0 )
- {
- rc = _cmXScoreParseNote(p,meas,np,&tick);
- }
- else
- // if this is a 'backup' node
- if( cmTextCmp(np->label,"backup") == 0 )
- {
- unsigned backup;
- cmXmlNodeUInt(np,&backup,"duration",NULL);
- tick -= backup;
- }
- else
- // if this is a 'direction' node
- if( cmTextCmp(np->label,"direction") == 0 )
- {
- rc = _cmXScoreParseDirection(p,meas,np,tick);
- }
-
- }
-
- return rc;
- }
-
- cmXsRC_t _cmXScoreParsePart( cmXScore_t* p, cmXsPart_t* pp )
- {
- cmXsRC_t rc = kOkXsRC;
- const cmXmlNode_t* xnp;
- cmXmlAttr_t partAttr = { "id", pp->idStr };
-
- // find the 'part'
- if((xnp = cmXmlSearch( cmXmlRoot(p->xmlH), "part", &partAttr, 1)) == NULL )
- return cmErrMsg(&p->err,kSyntaxErrorXsRC,"The part '%s' was not found.",pp->idStr);
-
- // for each child of this part - find each measure
- const cmXmlNode_t* cnp = xnp->children;
- for(; cnp!=NULL; cnp=cnp->sibling)
- if( cmTextCmp(cnp->label,"measure") == 0 )
- if((rc = _cmXScoreParseMeasure(p,pp,cnp)) != kOkXsRC )
- return rc;
-
- return rc;
- }
-
- cmXsNote_t* _cmXScoreInsertSortedNote( cmXsNote_t* s0, cmXsNote_t* np )
- {
- if( s0 == NULL )
- return np;
-
- if( np->tick < s0->tick )
- {
- np->slink = s0;
- return np;
- }
-
- cmXsNote_t* s1 = s0;
- cmXsNote_t* s2 = s0->slink;
-
- while( s2 != NULL )
- {
- if( s2->tick > np->tick )
- {
- s1->slink = np;
- np->slink = s2;
- return s0;
- }
-
- s1 = s2;
- s2 = s2->slink;
- }
-
- s1->slink = np;
-
- return s0;
- }
-
- void _cmXScoreSort( cmXScore_t* p )
- {
- cmXsPart_t* pp = p->partL;
- for(; pp!=NULL; pp=pp->link)
- {
- cmXsMeas_t* mp = pp->measL;
- for(; mp!=NULL; mp=mp->link)
- {
- // for each voice in this measure
- cmXsVoice_t* vp = mp->voiceL;
- for(; vp!=NULL; vp=vp->link)
- {
- cmXsNote_t* np = vp->noteL;
- for(; np!=NULL; np=np->mlink)
- mp->noteL = _cmXScoreInsertSortedNote(mp->noteL,np);
- }
- }
- }
- }
-
- cmXsRC_t cmXScoreInitialize( cmCtx_t* ctx, cmXsH_t* hp, const cmChar_t* xmlFn )
- {
- cmXsRC_t rc = kOkXsRC;
-
- if((rc = cmXScoreFinalize(hp)) != kOkXsRC )
- return rc;
-
- cmXScore_t* p = cmMemAllocZ(cmXScore_t,1);
-
- cmErrSetup(&p->err,&ctx->rpt,"XScore");
-
- // create a local linked heap
- if( cmLHeapIsValid( p->lhH = cmLHeapCreate(8196,ctx)) == false )
- return cmErrMsg(&p->err,kLHeapFailXsRC,"Lheap create failed.");
-
- // open the music xml file
- if( cmXmlAlloc(ctx, &p->xmlH, xmlFn) != kOkXmlRC )
- {
- rc = cmErrMsg(&p->err,kXmlFailXsRC,"Unable to open the MusicXML file '%s'.",cmStringNullGuard(xmlFn));
- goto errLabel;
- }
-
- //cmXmlPrint(p->xmlH,&ctx->rpt);
-
- // parse the part-list
- if((rc = _cmXScoreParsePartList( p )) != kOkXsRC )
- goto errLabel;
-
- // parse each score 'part'
- cmXsPart_t* pp = p->partL;
- for(; pp!=NULL; pp=pp->link)
- if((rc = _cmXScoreParsePart(p,pp)) != kOkXsRC )
- goto errLabel;
-
- // fill in the note->slink chain to link the notes in each measure in time order
- _cmXScoreSort(p);
-
- // CSV output initialize failed.
- if( cmCsvInitialize(&p->csvH,ctx) != kOkCsvRC )
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output object create failed.");
-
- errLabel:
- if( rc != kOkXsRC )
- _cmXScoreFinalize(p);
- else
- hp->h = p;
-
- return rc;
- }
-
- cmXsRC_t cmXScoreFinalize( cmXsH_t* hp )
- {
- cmXsRC_t rc = kOkXsRC;
-
- if( hp == NULL || cmXScoreIsValid(*hp)==false )
- return kOkXsRC;
-
- cmXScore_t* p = _cmXScoreHandleToPtr(*hp);
-
- if((rc = _cmXScoreFinalize(p)) != kOkXsRC )
- return rc;
-
- hp->h = NULL;
-
- return rc;
- }
-
-
- bool cmXScoreIsValid( cmXsH_t h )
- { return h.h != NULL; }
-
- void _cmXScoreReportNote( cmRpt_t* rpt, const cmXsNote_t* note )
- {
- const cmChar_t* B = cmIsFlag(note->flags,kBarXsFl) ? "|" : "-";
- const cmChar_t* R = cmIsFlag(note->flags,kRestXsFl) ? "R" : "-";
- const cmChar_t* G = cmIsFlag(note->flags,kGraceXsFl) ? "G" : "-";
- const cmChar_t* D = cmIsFlag(note->flags,kDotXsFl) ? "D" : "-";
- const cmChar_t* C = cmIsFlag(note->flags,kChordXsFl) ? "C" : "-";
- const cmChar_t* e = cmIsFlag(note->flags,kEvenXsFl) ? "e" : "-";
- const cmChar_t* d = cmIsFlag(note->flags,kDynXsFl) ? "d" : "-";
- const cmChar_t* t = cmIsFlag(note->flags,kTempoXsFl) ? "t" : "-";
- const cmChar_t* P = cmIsFlag(note->flags,kPedalDnXsFl) ? "V" : "-";
- P = cmIsFlag(note->flags,kPedalUpXsFl) ? "^" : P;
- P = cmIsFlag(note->flags,kPedalUpDnXsFl) ? "X" : P;
- const cmChar_t* N = note->pitch==0 ? " " : cmMidiToSciPitch( note->pitch, NULL, 0 );
- cmRptPrintf(rpt," %5i %5i %4.1f %3s %s%s%s%s%s%s%s%s%s",note->tick,note->duration,note->rvalue,N,B,R,G,D,C,e,d,t,P);
-
- if( cmIsFlag(note->flags,kSectionXsFl) )
- cmRptPrintf(rpt," %i",cmStringNullGuard(note->tvalue));
-
- printf("\n");
-
- }
-
- /*
- kMidiFileIdColScIdx= 0,
- kTypeLabelColScIdx = 3,
- kDSecsColScIdx = 4,
- kSecsColScIdx = 5,
- kD0ColScIdx = 9,
- kD1ColScIdx = 10,
- kPitchColScIdx = 11,
- kBarColScIdx = 13,
- kSkipColScIdx = 14,
- kEvenColScIdx = 15,
- kGraceColScIdx = 16,
- kTempoColScIdx = 17,
- kFracColScIdx = 18,
- kDynColScIdx = 19,
- kSectionColScIdx = 20,
- kRecdPlayColScIdx = 21,
- kRemarkColScIdx = 22
- */
-
-
- cmXsRC_t _cmXScoreWriteCsvHdr( cmXScore_t* p )
- {
- const cmChar_t* s[] =
- {
- "id","trk","evt","opcode","dticks","micros","status",
- "meta","ch","d0","d1","arg0","arg1","bar","skip",
- "even","grace","tempo","t frac","dyn","section","play_recd","remark",NULL
- };
-
- cmCsvCell_t* lcp = NULL;
-
- if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymText(p->csvH,s[0]), 0, 0 ) != kOkCsvRC )
- return cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
-
- unsigned i;
- for(i=1; s[i]!=NULL; ++i)
- {
- if( cmCsvInsertTextColAfter(p->csvH, lcp, &lcp, s[i], 0 ) != kOkCsvRC )
- return cmErrMsg(&p->err,kCsvFailXsRC,"CSV error inserting CSV title %i.\n",i);
-
- }
-
- return kOkXsRC;
- }
-
- cmXsRC_t _cmXScoreWriteCsvBlankCols( cmXScore_t* p, unsigned cnt, cmCsvCell_t** leftCellPtrPtr )
- {
- unsigned i;
- for(i=0; i<cnt; ++i)
- if( cmCsvInsertTextColAfter(p->csvH,*leftCellPtrPtr,leftCellPtrPtr,0,0) != kOkCsvRC )
- return cmErrMsg(&p->err,kCsvFailXsRC,"CSV output failed on blank column.");
-
- return kOkCsvRC;
- }
-
- cmXsRC_t _cmXScoreWriteCsvRow(
- cmXScore_t* p,
- unsigned rowIdx,
- unsigned bar,
- const cmChar_t* sectionStr,
- const cmChar_t* opCodeStr,
- double dsecs,
- double secs,
- unsigned d0,
- unsigned d1,
- unsigned pitch,
- double frac,
- unsigned flags )
- {
- cmXsRC_t rc = kOkXsRC;
- cmCsvCell_t* lcp = NULL;
-
- // append an empty row to the CSV object
- if( cmCsvAppendRow( p->csvH, &lcp, cmCsvInsertSymUInt(p->csvH, rowIdx ), 0, 0 ) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV append row failed.");
- goto errLabel;
- }
-
- /*
- // col 0 : blanks
- if( cmCsvInsertUIntColAfter(p->csvH, lcp, &lcp, rowIdx, 0 ) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV output row index failed.");
- goto errLabel;
- }
- */
-
- // cols 1,2
- if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
- goto errLabel;
-
- // col 3 : output the opcode
- if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,opCodeStr,0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on opcode label.");
- goto errLabel;
- }
-
- // col 4 : dsecs
- if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,dsecs,0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'dsecs'.");
- goto errLabel;
- }
-
- // col 5 : secs
- if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,secs,0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'secs'.");
- goto errLabel;
- }
-
- // cols 6,7,8 blanks
- if((rc = _cmXScoreWriteCsvBlankCols(p,3,&lcp)) != kOkXsRC )
- goto errLabel;
-
- // col 9 : d0
- if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d0,0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd0'.");
- goto errLabel;
- }
-
- // col 10 : d1
- if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,d1,0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'd1'.");
- goto errLabel;
- }
-
- // col 11 : pitch
- if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,pitch,0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
- goto errLabel;
- }
-
- // col 12 : blanks
- if((rc = _cmXScoreWriteCsvBlankCols(p,1 + (cmIsFlag(flags,kBarXsFl) ? 0 : 1), &lcp)) != kOkXsRC )
- goto errLabel;
-
- // col 13 : bar number
- if( cmIsFlag(flags,kBarXsFl) )
- {
- if( cmCsvInsertUIntColAfter(p->csvH,lcp,&lcp,bar,0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 'pitch'.");
- goto errLabel;
- }
- }
-
- // col 14 : skip (blank for now)
- if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
- goto errLabel;
-
- // col 15: even
- if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kEvenXsFl) ? "e" : "",0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
- goto errLabel;
- }
-
- // col 16: grace
- if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kGraceXsFl) ? "g" : "",0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
- goto errLabel;
- }
-
- // col 17: tempo
- if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kTempoXsFl) ? "t" : "",0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
- goto errLabel;
- }
-
- // col 18: frac
- if( frac == 0 )
- {
- if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
- goto errLabel;
- }
- else
- {
- if( cmCsvInsertDoubleColAfter(p->csvH,lcp,&lcp,1.0/frac,0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on 't frac'.");
- goto errLabel;
- }
- }
-
- // col 19: dynamic marking (blank for now)
- if((rc = _cmXScoreWriteCsvBlankCols(p,1,&lcp)) != kOkXsRC )
- goto errLabel;
-
- // col 20: section
- if( cmCsvInsertTextColAfter(p->csvH,lcp,&lcp,cmIsFlag(flags,kSectionXsFl) ? sectionStr : "",0) != kOkCsvRC )
- {
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"CSV insert failed on eveness flag label.");
- goto errLabel;
- }
-
-
- // col 21, 22 : recd-play, remark (blank for now)
- if((rc = _cmXScoreWriteCsvBlankCols(p,2,&lcp)) != kOkXsRC )
- goto errLabel;
-
- errLabel:
- return rc;
-
- }
-
- cmXsRC_t cmXScoreWriteCsv( cmXsH_t h, const cmChar_t* csvFn )
- {
- cmXsRC_t rc = kOkXsRC;
- cmXScore_t* p = _cmXScoreHandleToPtr(h);
- unsigned rowIdx = 1;
- double tpqn = 0; // ticks per quarter note
- double tps = 0; // ticks per second
- double sec = 0; // current time in seconds
- const cmChar_t* sectionIdStr = NULL;
-
-
- if( !cmCsvIsValid(p->csvH) )
- return cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output object is not initialized.");
-
- if((rc = _cmXScoreWriteCsvHdr( p )) != kOkXsRC )
- goto errLabel;
-
- cmXsPart_t* pp = p->partL;
- for(; pp!=NULL; pp=pp->link)
- {
- cmXsMeas_t* mp = pp->measL;
- for(; mp!=NULL; mp=mp->link)
- {
- if( mp->divisions != 0 )
- tpqn = mp->divisions;
-
- cmXsNote_t* np = mp->noteL;
-
- double sec0 = sec;
-
- for(; np!=NULL; np=np->slink)
- {
-
- //
- if( cmIsFlag(np->flags,kMetronomeXsFl) )
- {
- double bpm = np->duration;
- double bps = bpm / 60.0;
- tps = bps * tpqn;
- }
-
- double meas_sec = tps == 0 ? 0 : np->tick / tps;
- double sec1 = sec + meas_sec;
- double dsecs = sec1 - sec0;
- sec0 = sec1;
-
- // if this is a section event
- if( cmIsFlag(np->flags,kSectionXsFl) )
- sectionIdStr = np->tvalue;
-
- // if this is a bar event
- if( cmIsFlag(np->flags,kBarXsFl) )
- {
- _cmXScoreWriteCsvRow(p,rowIdx,mp->number,NULL,"bar",dsecs,sec1,0,0,0,0,np->flags);
- }
- else
-
- // if this is a pedal event
- if( cmIsFlag(np->flags,kPedalDnXsFl|kPedalUpXsFl|kPedalUpDnXsFl) )
- {
- unsigned d0 = 64; // pedal MIDI ctl id
- unsigned d1 = cmIsFlag(np->flags,kPedalDnXsFl) ? 64 : 0; // pedal-dn: d1>=64 pedal-up:<64
- _cmXScoreWriteCsvRow(p,rowIdx,mp->number,NULL,"ctl",dsecs,sec1,d0,d1,0,0,np->flags);
- }
- else
-
-
- // if this is a sounding note event
- if( cmIsNotFlag(np->flags,kRestXsFl) )
- {
- double frac = np->rvalue + (cmIsFlag(np->flags,kDotXsFl) ? (np->rvalue/2) : 0);
-
- //
- _cmXScoreWriteCsvRow(p,rowIdx,mp->number,sectionIdStr,"non",dsecs,sec1,0,0,np->pitch,frac,np->flags);
- sectionIdStr = NULL;
- }
-
- rowIdx += 1;
-
-
- }
-
- sec = sec0;
- }
-
- }
-
- if( cmCsvWrite( p->csvH, csvFn ) != kOkCsvRC )
- rc = cmErrMsg(&p->err,kCsvFailXsRC,"The CSV output write failed on file '%s'.",csvFn);
-
- errLabel:
- return rc;
- }
-
-
- void cmXScoreReport( cmXsH_t h, cmRpt_t* rpt, bool sortFl )
- {
- cmXScore_t* p = _cmXScoreHandleToPtr(h);
- cmXsPart_t* pp = p->partL;
-
- for(; pp!=NULL; pp=pp->link)
- {
- cmRptPrintf(rpt,"Part:%s\n",pp->idStr);
-
- const cmXsMeas_t* meas = pp->measL;
- for(; meas!=NULL; meas=meas->link)
- {
- cmRptPrintf(rpt," %i : div:%i beat:%i beat-type:%i\n",meas->number,meas->divisions,meas->beats,meas->beat_type);
-
- if( sortFl )
- {
- const cmXsNote_t* note = meas->noteL;
- for(; note!=NULL; note=note->slink)
- _cmXScoreReportNote(rpt,note);
- }
- else
- {
-
- const cmXsVoice_t* v = meas->voiceL;
- for(; v!=NULL; v=v->link)
- {
- const cmXsNote_t* note = v->noteL;
-
- cmRptPrintf(rpt," voice:%i\n",v->id);
-
- for(; note!=NULL; note=note->mlink)
- _cmXScoreReportNote(rpt,note);
- }
- }
- }
- }
- }
-
-
- cmXsRC_t cmXScoreTest( cmCtx_t* ctx, const cmChar_t* fn )
- {
- cmXsRC_t rc;
- cmXsH_t h = cmXsNullHandle;
-
- if((rc = cmXScoreInitialize( ctx, &h, fn)) != kOkXsRC )
- return cmErrMsg(&ctx->err,rc,"XScore alloc failed.");
-
- cmXScoreWriteCsv(h,"/Users/kevin/temp/a0.csv");
- cmXScoreReport(h,&ctx->rpt,true);
-
- return cmXScoreFinalize(&h);
-
- }
|