diff --git a/cmMidiFile.c b/cmMidiFile.c index 5e46e5a..43b3eef 100644 --- a/cmMidiFile.c +++ b/cmMidiFile.c @@ -365,8 +365,8 @@ cmMfRC_t _cmMidiFileReadTrack( _cmMidiFile_t* mfp, unsigned short trkIdx ) break; default: - // handle channel msg - rc = _cmMidiFileReadChannelMsg(mfp,&runstatus,status,tmp); + // handle channel msg + rc = _cmMidiFileReadChannelMsg(mfp,&runstatus,status,tmp); } } @@ -454,11 +454,15 @@ cmMfRC_t _cmMidiFileClose( _cmMidiFile_t* mfp ) cmMfRC_t cmMidiFileOpen( const char* fn, cmMidiFileH_t* hPtr, cmCtx_t* ctx ) { - cmMfRC_t rc = kOkMfRC; + cmMfRC_t rc = kOkMfRC; _cmMidiFile_t* mfp = NULL; unsigned short trkIdx = 0; cmErr_t err; + if( cmMidiFileIsValid(*hPtr) ) + if((rc = _cmMidiFileClose(_cmMidiFileHandleToPtr(*hPtr))) != kOkMfRC ) + return rc; + cmErrSetup(&err,&ctx->rpt,"MIDI File"); // allocate the midi file object @@ -575,13 +579,13 @@ cmMfRC_t cmMidiFileOpen( const char* fn, cmMidiFileH_t* hPtr, cmCtx_t* ctx ) errLabel: - if( cmFileClose(&mfp->fh) != kOkFileRC ) - rc = _cmMidiFileError(&mfp->err,kCloseFailFileRC); + if( cmFileClose(&mfp->fh) != kOkFileRC ) + rc = _cmMidiFileError(&mfp->err,kCloseFailFileRC); - if( rc != kOkMfRC ) - _cmMidiFileClose(mfp); + if( rc != kOkMfRC ) + _cmMidiFileClose(mfp); - return rc; + return rc; } @@ -600,6 +604,9 @@ cmMfRC_t cmMidiFileClose( cmMidiFileH_t* h ) return rc; } +bool cmMidiFileIsValid( cmMidiFileH_t h ) +{ return !cmMidiFileIsNull(h); } + unsigned cmMidiFileTrackCount( cmMidiFileH_t h ) { _cmMidiFile_t* mfp; @@ -648,7 +655,7 @@ cmMidiByte_t cmMidiFileTicksPerSmpteFrame( cmMidiFileH_t h ) if( mfp->ticksPerQN != 0 ) return 0; - return mfp->smpteTicksPerFrame; + return mfp->smpteTicksPerFrame; } cmMidiByte_t cmMidiFileSmpteFormatId( cmMidiFileH_t h ) @@ -661,7 +668,7 @@ cmMidiByte_t cmMidiFileSmpteFormatId( cmMidiFileH_t h ) if( mfp->ticksPerQN != 0 ) return 0; - return mfp->smpteFmtId; + return mfp->smpteFmtId; } unsigned cmMidiFileTrackMsgCount( cmMidiFileH_t h, unsigned trackIdx ) diff --git a/cmMidiFile.h b/cmMidiFile.h index 2a6e2d8..cdfee1b 100644 --- a/cmMidiFile.h +++ b/cmMidiFile.h @@ -107,6 +107,8 @@ extern "C" { cmMfRC_t cmMidiFileOpen( const char* fn, cmMidiFileH_t* hPtr, cmCtx_t* ctx ); cmMfRC_t cmMidiFileClose( cmMidiFileH_t* hp ); + bool cmMidiFileIsValid( cmMidiFileH_t h ); + // Returns track count or kInvalidCnt if 'h' is invalid. unsigned cmMidiFileTrackCount( cmMidiFileH_t h );