From 7f84595d08d612b75d48a7ef390769c7d302627b Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 31 Mar 2016 19:06:20 -0400 Subject: [PATCH] cmMidiFile.h/c : Added cmMidiFileGenPlotFile(). --- cmMidiFile.c | 35 +++++++++++++++++++++++++++++++++++ cmMidiFile.h | 4 ++++ 2 files changed, 39 insertions(+) diff --git a/cmMidiFile.c b/cmMidiFile.c index 768f7a7..0a8cf5f 100644 --- a/cmMidiFile.c +++ b/cmMidiFile.c @@ -1453,6 +1453,41 @@ bool cmMidiFileIsNull( cmMidiFileH_t h ) void cmMidiFileTestPrint( void* printDataPtr, const char* fmt, va_list vl ) { vprintf(fmt,vl); } +cmMfRC_t cmMidiFileGenPlotFile( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* outFn ) +{ + cmMfRC_t rc = kOkMfRC; + cmMidiFileH_t mfH = cmMidiFileNullHandle; + cmFileH_t fH = cmFileNullHandle; + unsigned i = 0; + const cmMidiTrackMsg_t** m = NULL; + unsigned mN = 0; + + if((rc = cmMidiFileOpen(ctx, &mfH, midiFn )) != kOkMfRC ) + return cmErrMsg(&ctx->err,rc,"The MIDI file object could not be opened from '%s'.",cmStringNullGuard(midiFn)); + + _cmMidiFile_t* p = _cmMidiFileHandleToPtr(mfH); + + if( (m = cmMidiFileMsgArray(mfH)) == NULL || (mN = cmMidiFileMsgCount(mfH)) == 0 ) + { + rc = cmErrMsg(&p->err,kFileFailMfRC,"The MIDI file object appears to be empty."); + goto errLabel; + } + + cmMidiFileCalcNoteDurations( mfH ); + + if( cmFileOpen(&fH,outFn,kWriteFileFl,p->err.rpt) != kOkFileRC ) + return cmErrMsg(&p->err,kFileFailMfRC,"Unable to create the file '%s'.",cmStringNullGuard(outFn)); + + for(i=0; istatus) && cmMidiIsNoteOn(m[i]->status) && (m[i]->u.chMsgPtr->d1>0) ) + cmFilePrintf(fH,"n %f %f %i %s\n",m[i]->amicro/1000000.0,m[i]->u.chMsgPtr->durMicros/1000000.0,m[i]->uid,cmMidiToSciPitch(m[i]->u.chMsgPtr->d0,NULL,0)); + + errLabel: + + cmMidiFileClose(&mfH); + cmFileClose(&fH); + return rc; +} void cmMidiFilePrintControlNumbers( cmCtx_t* ctx, const char* fn ) { diff --git a/cmMidiFile.h b/cmMidiFile.h index 286efc9..1456b88 100644 --- a/cmMidiFile.h +++ b/cmMidiFile.h @@ -172,6 +172,10 @@ extern "C" { bool cmMidiFileIsNull( cmMidiFileH_t h ); void cmMidiFileTest( const char* fn, cmCtx_t* ctx ); + // Generate a piano-roll plot description file which can be displayed with cmXScore.m + cmMfRC_t cmMidiFileGenPlotFile( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* outFn ); + + //) #ifdef __cplusplus