From 1fc5183394fb633fd1030e66e6108cc45a08ba77 Mon Sep 17 00:00:00 2001 From: Kevin Larke Date: Thu, 4 Feb 2016 11:33:42 -0500 Subject: [PATCH] cmXScore.h/c,Makefile.am : Initial commit. --- Makefile.am | 77 +++++++++++++- app/cmXScore.c | 276 +++++++++++++++++++++++++++++++++++++++++++++++++ app/cmXScore.h | 43 ++++++++ 3 files changed, 394 insertions(+), 2 deletions(-) create mode 100644 app/cmXScore.c create mode 100644 app/cmXScore.h diff --git a/Makefile.am b/Makefile.am index 71ad803..21eb218 100644 --- a/Makefile.am +++ b/Makefile.am @@ -75,7 +75,80 @@ cmSRC += src/libcm/cmProcObj.c src/libcm/cmProc.c src/libcm/cmProc2.c src/libcm/ cmHDR += src/libcm/app/cmOnset.h src/libcm/app/cmTimeLine.h src/libcm/app/cmScore.h src/libcm/app/cmScoreProc.h -cmSRC += src/libcm/app/cmOnset.c src/libcm/app/cmTimeLine.c src/libcm/app/cmScore.c src/libcm/app/cmScoreProc.c + +cmHDR += src/libcm/cmErr.h src/libcm/cmCtx.h src/libcm/cmRpt.h src/libcm/cmGlobal.h src/libcm/cmComplexTypes.h src/libcm/cmFloatTypes.h src/libcm/cmPrefix.h +cmSRC += src/libcm/cmErr.c src/libcm/cmCtx.c src/libcm/cmRpt.c src/libcm/cmGlobal.c src/libcm/cmComplexTypes.c + +cmHDR += src/libcm/cmSerialize.h src/libcm/cmSymTbl.h src/libcm/cmHashTbl.h src/libcm/cmFileSys.h src/libcm/cmFile.h +cmSRC += src/libcm/cmSerialize.c src/libcm/cmSymTbl.c src/libcm/cmHashTbl.c src/libcm/cmFileSys.c src/libcm/cmFile.c + +cmHDR += src/libcm/cmMem.h src/libcm/cmTime.h src/libcm/cmExec.h src/libcm/cmPgmOpts.h +cmSRC += src/libcm/cmMem.c src/libcm/cmTime.c src/libcm/cmExec.c src/libcm/cmPgmOpts.c + +cmHDR += src/libcm/cmData.h src/libcm/cmLib.h src/libcm/cmText.h src/libcm/cmTextTemplate.h +cmSRC += src/libcm/cmData.c src/libcm/cmLib.c src/libcm/cmText.c src/libcm/cmTextTemplate.c + +cmHDR += src/libcm/cmMath.h src/libcm/cmGnuPlot.h src/libcm/cmKeyboard.h src/libcm/cmStrStream.h +cmSRC += src/libcm/cmMath.c src/libcm/cmGnuPlot.c src/libcm/cmKeyboard.c src/libcm/cmStrStream.c + +cmHDR += src/libcm/cmLinkedHeap.h src/libcm/cmMallocDebug.h src/libcm/cmLex.h src/libcm/cmJson.h src/libcm/cmXml.h src/libcm/cmPrefs.h src/libcm/cmStack.h src/libcm/cmArray.h +cmSRC += src/libcm/cmLinkedHeap.c src/libcm/cmMallocDebug.c src/libcm/cmLex.c src/libcm/cmJson.c src/libcm/cmXml.c src/libcm/cmPrefs.c src/libcm/cmStack.c src/libcm/cmArray.c + +cmHDR += src/libcm/cmUdpPort.h src/libcm/cmUdpNet.h src/libcm/cmVirtNet.h +cmSRC += src/libcm/cmUdpPort.c src/libcm/cmUdpNet.c src/libcm/cmVirtNet.c + +cmHDR += src/libcm/cmAudioPort.h src/libcm/cmApBuf.h src/libcm/cmAudioAggDev.h src/libcm/cmAudioNrtDev.h src/libcm/cmThread.h +cmSRC += src/libcm/cmAudioPort.c src/libcm/cmApBuf.c src/libcm/cmAudioAggDev.c src/libcm/cmAudioNrtDev.c src/libcm/cmThread.c + +cmHDR += src/libcm/cmMidiFilePlay.h src/libcm/cmMidiPort.h src/libcm/cmMidiFile.h src/libcm/cmMidi.h +cmSRC += src/libcm/cmMidiFilePlay.c src/libcm/cmMidiPort.c src/libcm/cmMidiFile.c src/libcm/cmMidi.c + +cmHDR += src/libcm/cmAudioFile.h src/libcm/cmAudioFileMgr.h src/libcm/cmMsgProtocol.h src/libcm/cmAudioSys.h src/libcm/cmAudioPortFile.h src/libcm/cmAudioFileDev.h +cmSRC += src/libcm/cmAudioFile.c src/libcm/cmAudioFileMgr.c src/libcm/cmMsgProtocol.c src/libcm/cmAudioSys.c src/libcm/cmAudioPortFile.c src/libcm/cmAudioFileDev.c + +cmHDR += src/libcm/cmRtSys.h src/libcm/cmRtNet.h src/libcm/cmUiRtSysMstr.h src/libcm/cmRtSysMsg.h +cmSRC += src/libcm/cmRtSys.c src/libcm/cmRtNet.c src/libcm/cmUiRtSysMstr.c + +cmHDR += src/libcm/cmDevCfg.h src/libcm/cmUi.h src/libcm/cmUiDrvr.h +cmSRC += src/libcm/cmDevCfg.c src/libcm/cmUi.c src/libcm/cmUiDrvr.c + +cmHDR += src/libcm/cmFrameFile.h src/libcm/cmFeatFile.h src/libcm/cmCsv.h src/libcm/cmAudLabelFile.h src/libcm/cmTagFile.h +cmSRC += src/libcm/cmFrameFile.c src/libcm/cmFeatFile.c src/libcm/cmCsv.c src/libcm/cmAudLabelFile.c src/libcm/cmTagFile.c + +cmSRC += src/libcm/cmGr.c src/libcm/cmGrDevCtx.c src/libcm/cmGrPage.c src/libcm/cmGrPlot.c src/libcm/cmGrPlotAudio.c +cmHDR += src/libcm/cmGr.h src/libcm/cmGrDevCtx.h src/libcm/cmGrPage.h src/libcm/cmGrPlot.h src/libcm/cmGrPlotAudio.h + +cmHDR += src/libcm/dsp/cmDspSys.h src/libcm/dsp/cmDspClass.h src/libcm/dsp/cmDspValue.h src/libcm/dsp/cmDspUi.h src/libcm/dsp/cmDspPreset.h src/libcm/dsp/cmDspNet.h +cmSRC += src/libcm/dsp/cmDspSys.c src/libcm/dsp/cmDspClass.c src/libcm/dsp/cmDspValue.c src/libcm/dsp/cmDspUi.c src/libcm/dsp/cmDspPreset.c src/libcm/dsp/cmDspNet.c + +cmHDR += src/libcm/dsp/cmDspStore.h src/libcm/dsp/cmDspBuiltIn.h src/libcm/dsp/cmDspFx.h +cmSRC += src/libcm/dsp/cmDspStore.c src/libcm/dsp/cmDspBuiltIn.c src/libcm/dsp/cmDspFx.c + +cmHDR += src/libcm/dsp/cmDspPgm.h src/libcm/dsp/cmDspPgmPP.h src/libcm/dsp/cmDspPgmPPMain.h +cmSRC += src/libcm/dsp/cmDspPgm.c src/libcm/dsp/cmDspPgmPP.c src/libcm/dsp/cmDspPgmPPMain.c + +cmHDR += src/libcm/dsp/cmDspKr.h src/libcm/dsp/cmDspPgmKr.h +cmSRC += src/libcm/dsp/cmDspKr.c src/libcm/dsp/cmDspPgmKr.c + +cmHDR += src/libcm/cmAudDsp.h src/libcm/cmAudDspIF.h src/libcm/cmAudDspLocal.h +cmSRC += src/libcm/cmAudDsp.c src/libcm/cmAudDspIF.c src/libcm/cmAudDspLocal.c + +cmHDR += src/libcm/vop/cmVectOpsTemplateUndef.h src/libcm/vop/cmVectOpsTemplateHdr.h src/libcm/vop/cmVectOpsTemplateCode.h src/libcm/vop/cmVectOpsTemplateMain.h +cmHDR += src/libcm/vop/cmVectOpsRIHdr.h src/libcm/vop/cmVectOpsRICode.h +cmHDR += src/libcm/vop/cmProcTemplateUndef.h src/libcm/vop/cmProcTemplateHdr.h src/libcm/vop/cmProcTemplateCode.h src/libcm/vop/cmProcTemplateMain.h +cmHDR += src/libcm/vop/cmVectOps.h src/libcm/vop/cmProcTemplate.h + +cmSRC += src/libcm/vop/cmVectOps.c src/libcm/vop/cmProcTemplate.c + +cmSRC += src/libcm/cmDList.c +cmHDR += src/libcm/cmDList.h src/libcm/cmDListTpl.h + +cmHDR += src/libcm/cmProcObj.h src/libcm/cmProc.h src/libcm/cmProc2.h src/libcm/cmProc3.h src/libcm/cmProc4.h src/libcm/cmProc5.h src/libcm/cmProcTest.h +cmSRC += src/libcm/cmProcObj.c src/libcm/cmProc.c src/libcm/cmProc2.c src/libcm/cmProc3.c src/libcm/cmProc4.c src/libcm/cmProc5.c src/libcm/cmProcTest.c + + +cmHDR += src/libcm/app/cmOnset.h src/libcm/app/cmTimeLine.h src/libcm/app/cmScore.h src/libcm/app/cmScoreProc.h src/libcm/app/cmXScore.h +cmSRC += src/libcm/app/cmOnset.c src/libcm/app/cmTimeLine.c src/libcm/app/cmScore.c src/libcm/app/cmScoreProc.c src/libcm/app/cmXScore.c cmHDR += src/libcm/app/cmSdb.h src/libcm/app/cmTakeSeqBldr.h src/libcm/app/cmDspPgmJsonToDot.h cmSRC += src/libcm/app/cmSdb.c src/libcm/app/cmTakeSeqBldr.c src/libcm/app/cmDspPgmJsonToDot.c @@ -83,7 +156,7 @@ cmSRC += src/libcm/app/cmSdb.c src/libcm/app/cmTakeSeqBldr.c src/libcm/app/cmD cmHDR += src/libcm/app/cmPickup.h src/libcm/cmRbm.h src/libcm/cmTaskMgr.h src/libcm/cmSyncRecd.h cmSRC += src/libcm/app/cmPickup.c src/libcm/cmRbm.c src/libcm/cmTaskMgr.c src/libcm/cmSyncRecd.c -cmHDR += src/libcm/sa/cmSaProc.h +cmHDR += src/libcm/sa/cmSaProc.h cmSRC += src/libcm/sa/cmSaProc.c if INC_SONICART diff --git a/app/cmXScore.c b/app/cmXScore.c new file mode 100644 index 0000000..7a944d1 --- /dev/null +++ b/app/cmXScore.c @@ -0,0 +1,276 @@ +#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" + +cmXsH_t cmXsNullHandle = cmSTATIC_NULL_HANDLE; + +typedef struct cmXsNote_str +{ +} 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; + unsigned beats; + unsigned beat_type; + + cmXsVoice_t* voiceL; // List of voices 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; +} 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 + if( cmXmlIsValid(p->xmlH) ) + cmXmlFree( &p->xmlH ); + + // release the local linked heap memory + if( cmLHeapIsValid(p->lhH) ) + cmLHeapDestroy(&p->lhH); + + cmMemFree(p); + + return rc; +} + +cmXsRC_t _cmXScoreMissingNode( cmXScore_t* p, const cmChar_t* label, const cmXmlAttr_t* attr ) +{ + if( attr == NULL ) + return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML node '%s'.",label); + + return cmErrMsg(&p->err,kSyntaxErrorXsRC,"Missing XML node '%s' - Attribute:%s=%s",label,attr->label,attr->value); +} + +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); +} + +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,"part-list",NULL); + + 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 _cmXScoreParseMeasure(cmXScore_t* p, cmXsPart_t* pp, const cmXmlNode_t* mnp ) +{ + cmXsRC_t rc = kOkXsRC; + + cmXsMeas_t* meas = cmLhAllocZ(p->lhH,cmXsMeas_t,1); + const cmXmlNode_t* np; + + // 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) + return rc; // (this measure does not have any attributes) + + cmXmlNodeUInt(np,&meas->divisions,"divisions",NULL); + cmXmlNodeUInt(np,&meas->beats,"time","beats",NULL); + cmXmlNodeUInt(np,&meas->beat_type,"time","beat-type",NULL); + + 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 _cmXScoreMissingNode(p,"part",&partAttr); + + // 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; +} + +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; + } + + // parse the part-list + if((rc = _cmXScoreParsePartList( p )) != kOkXsRC ) + goto errLabel; + + cmXsPart_t* pp = p->partL; + for(; pp!=NULL; pp=pp->link) + if((rc = _cmXScoreParsePart(p,pp)) != kOkXsRC ) + goto errLabel; + + + 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 cmXScoreReport( cmXsH_t h, cmRpt_t* rpt ) +{ + 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); + } + +} + + +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."); + + cmXScoreReport(h,&ctx->rpt); + + return cmXScoreFinalize(&h); + +} diff --git a/app/cmXScore.h b/app/cmXScore.h new file mode 100644 index 0000000..d114d29 --- /dev/null +++ b/app/cmXScore.h @@ -0,0 +1,43 @@ +#ifndef cmXScore_h +#define cmXScore_h + +#ifdef __cplusplus +extern "C" { +#endif + + enum + { + kOkXsRC = cmOkRC, + kXmlFailXsRC, + kLHeapFailXsRC, + kSyntaxErrorXsRC + }; + + typedef cmRC_t cmXsRC_t; + typedef cmHandle_t cmXsH_t; + + extern cmXsH_t cmXsNullHandle; + + // Prepare the MusicXML file: + // + // 1) Convert XML to UTF-8: + // a. Change: encoding='UTF-16' to encoding='UTF-8' + // b. Emacs C-x f utf-8 + // + // 2) Replace "DoletSibelius Unknown Symbol Index" with "DoletSibelius unknownSymIdx" + + + cmXsRC_t cmXScoreInitialize( cmCtx_t* ctx, cmXsH_t* hp, const cmChar_t* xmlFn ); + cmXsRC_t cmXScoreFinalize( cmXsH_t* hp ); + + bool cmXScoreIsValid( cmXsH_t h ); + + void cmXScoreReport( cmXsH_t h, cmRpt_t* rpt ); + + cmXsRC_t cmXScoreTest( cmCtx_t* ctx, const cmChar_t* fn ); + +#ifdef __cplusplus +} +#endif + +#endif