123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536 |
- #include "cmPrefix.h"
- #include "cmGlobal.h"
- #include "cmRpt.h"
- #include "cmErr.h"
- #include "cmCtx.h"
- #include "cmFile.h"
- #include "cmMem.h"
- #include "cmMallocDebug.h"
- #include "cmLinkedHeap.h"
- #include "cmTime.h"
- #include "cmMidi.h"
- #include "cmMidiFile.h"
-
- #ifdef cmBIG_ENDIAN
- #define mfSwap16(v) (v)
- #define mfSwap32(v) (v)
- #else
- #define mfSwap16(v) cmSwap16(v)
- #define mfSwap32(v) cmSwap32(v)
- #endif
-
-
-
- typedef struct
- {
- unsigned cnt; // count of track records
- cmMidiTrackMsg_t* base; // pointer to first track recd
- cmMidiTrackMsg_t* last; // pointer to last track recd
- } _cmMidiTrack_t;
-
- typedef struct
- {
- cmErr_t err; // this objects error object
- cmLHeapH_t lhH; // linked heap used for all dynamically alloc'd data space
- cmFileH_t fh; // cmFile handle (only used in fmMidiFileOpen() and cmMidiFileWrite())
- unsigned short fmtId; // midi file type id: 0,1,2
- unsigned short ticksPerQN; // ticks per quarter note or 0 if smpteFmtId is valid
- cmMidiByte_t smpteFmtId; // smpte format or 0 if ticksPerQN is valid
- cmMidiByte_t smpteTicksPerFrame; // smpte ticks per frame or 0 if ticksPerQN is valid
- unsigned short trkN; // track count
- _cmMidiTrack_t* trkV; // track vector
- char* fn; // file name or NULL if this object did not originate from a file
- unsigned msgN; // count of msg's in msgV[]
- cmMidiTrackMsg_t** msgV; // sorted msg list
- unsigned nextUid; // next available msg uid
- } _cmMidiFile_t;
-
-
- cmMidiFileH_t cmMidiFileNullHandle = cmSTATIC_NULL_HANDLE;
-
-
- _cmMidiFile_t* _cmMidiFileHandleToPtr( cmMidiFileH_t h )
- {
- _cmMidiFile_t* p = (_cmMidiFile_t*)h.h;
- assert( p != NULL );
- return p;
- }
-
- void* _cmMidiFileMalloc( _cmMidiFile_t* mfp, unsigned byteN )
- { return cmLHeapAllocZ(mfp->lhH,byteN); }
-
-
- cmMfRC_t _cmMidiFileRead8( _cmMidiFile_t* mfp, cmMidiByte_t* p )
- {
- if( cmFileReadUChar(mfp->fh,p,1) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI byte read failed.");
- return kOkMfRC;
- }
-
- cmMfRC_t _cmMidiFileRead16( _cmMidiFile_t* mfp, unsigned short* p )
- {
- if( cmFileReadUShort(mfp->fh,p,1) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI short read failed.");
-
- *p = mfSwap16(*p);
-
- return kOkMfRC;
- }
-
- cmMfRC_t _cmMidiFileRead24( _cmMidiFile_t* mfp, unsigned* p )
- {
- *p = 0;
- int i = 0;
- for(; i<3; ++i)
- {
- unsigned char c;
- if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI 24 bit integer read failed.");
- *p = (*p << 8) + c;
- }
-
- //*p =mfSwap32(*p);
-
- return kOkMfRC;
- }
-
- cmMfRC_t _cmMidiFileRead32( _cmMidiFile_t* mfp, unsigned* p )
- {
- if( cmFileReadUInt(mfp->fh,p,1) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI integer read failed.");
-
- *p = mfSwap32(*p);
-
- return kOkMfRC;
- }
-
- cmMfRC_t _cmMidiFileReadText( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
- {
- if( byteN == 0 )
- return kOkMfRC;
-
- char* t = (char*)_cmMidiFileMalloc(mfp,byteN+1);
- t[byteN] = 0;
-
- if( cmFileReadChar(mfp->fh,t,byteN) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read text failed.");
-
- tmp->u.text = t;
- tmp->byteCnt = byteN;
- return kOkMfRC;
- }
-
- cmMfRC_t _cmMidiFileReadRecd( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
- {
- char* t = (char*)_cmMidiFileMalloc(mfp,byteN);
-
- if( cmFileReadChar(mfp->fh,t,byteN) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read record failed.");
-
- tmp->byteCnt = byteN;
- tmp->u.voidPtr = t;
-
- return kOkMfRC;
- }
-
- cmMfRC_t _cmMidiFileReadVarLen( _cmMidiFile_t* mfp, unsigned* p )
- {
- unsigned char c;
-
- if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read variable length integer failed.");
-
- if( !(c & 0x80) )
- *p = c;
- else
- {
- *p = c & 0x7f;
-
- do
- {
-
- if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read variable length integer failed.");
-
- *p = (*p << 7) + (c & 0x7f);
-
- }while( c & 0x80 );
- }
-
- return kOkMfRC;
- }
-
- cmMfRC_t _cmMidiFileAppendTrackMsg( _cmMidiFile_t* mfp, unsigned short trkIdx, unsigned dtick, cmMidiByte_t status, cmMidiTrackMsg_t** trkMsgPtrPtr )
- {
- cmMidiTrackMsg_t* tmp = (cmMidiTrackMsg_t*)_cmMidiFileMalloc(mfp, sizeof(cmMidiTrackMsg_t) );
-
- // link new record onto track record chain
- if( mfp->trkV[trkIdx].base == NULL )
- mfp->trkV[trkIdx].base = tmp;
- else
- mfp->trkV[trkIdx].last->link = tmp;
-
- mfp->trkV[trkIdx].last = tmp;
- mfp->trkV[trkIdx].cnt++;
-
-
- // set the generic track record fields
- tmp->dtick = dtick;
- tmp->status = status;
- tmp->metaId = kInvalidMetaMdId;
- tmp->trkIdx = trkIdx;
- tmp->byteCnt = 0;
- *trkMsgPtrPtr = tmp;
-
- return kOkMfRC;
- }
-
- cmMfRC_t _cmMidiFileReadSysEx( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
- {
- cmMfRC_t rc = kOkMfRC;
- cmMidiByte_t b = 0;
-
- if( byteN == cmInvalidCnt )
- {
-
- long offs;
- if( cmFileTell(mfp->fh,&offs) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI File 'tell' failed.");
-
- byteN = 0;
-
- // get the length of the sys-ex msg
- while( !cmFileEof(mfp->fh) && (b != kSysComEoxMdId) )
- {
- if((rc = _cmMidiFileRead8(mfp,&b)) != kOkMfRC )
- return rc;
-
- ++byteN;
- }
-
- // verify that the EOX byte was found
- if( b != kSysComEoxMdId )
- return cmErrMsg(&mfp->err,kMissingEoxMfRC,"MIDI file missing 'end-of-sys-ex'.");
-
- // rewind to the beginning of the msg
- if( cmFileSeek(mfp->fh,kBeginFileFl,offs) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file seek failed on sys-ex read.");
-
- }
-
- // allocate memory to hold the sys-ex msg
- cmMidiByte_t* mp = (cmMidiByte_t *)_cmMidiFileMalloc(mfp, byteN );
-
- // read the sys-ex msg from the file into msg memory
- if( cmFileReadUChar(mfp->fh,mp,byteN) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI sys-ex read failed.");
-
- tmp->byteCnt = byteN;
- tmp->u.sysExPtr = mp;
-
- return rc;
- }
-
- cmMfRC_t _cmMidiFileReadChannelMsg( _cmMidiFile_t* mfp, cmMidiByte_t* rsPtr, cmMidiByte_t status, cmMidiTrackMsg_t* tmp )
- {
- cmMfRC_t rc = kOkMfRC;
- cmMidiChMsg_t* p = (cmMidiChMsg_t*)_cmMidiFileMalloc(mfp,sizeof(cmMidiChMsg_t));
- unsigned useRsFl = status <= 0x7f;
- cmMidiByte_t statusCh = useRsFl ? *rsPtr : status;
-
- if( useRsFl )
- p->d0 = status;
- else
- *rsPtr = status;
-
- tmp->byteCnt = sizeof(cmMidiChMsg_t);
- tmp->status = statusCh & 0xf0;
- p->ch = statusCh & 0x0f;
- p->durMicros = 0;
-
- unsigned byteN = cmMidiStatusToByteCount(tmp->status);
-
- if( byteN==kInvalidMidiByte || byteN > 2 )
- return cmErrMsg(&mfp->err,kInvalidStatusMfRC,"Invalid status:0x%x %i.",tmp->status,tmp->status);
-
- unsigned i;
- for(i=useRsFl; i<byteN; ++i)
- {
- cmMidiByte_t* b = i==0 ? &p->d0 : &p->d1;
-
- if((rc = _cmMidiFileRead8(mfp,b)) != kOkMfRC )
- return rc;
- }
-
- // convert note-on velocity=0 to note off
- if( tmp->status == kNoteOnMdId && p->d1==0 )
- tmp->status = kNoteOffMdId;
-
- tmp->u.chMsgPtr = p;
-
- return rc;
- }
-
- cmMfRC_t _cmMidiFileReadMetaMsg( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp )
- {
- cmMidiByte_t metaId;
- cmMfRC_t rc;
- unsigned byteN = 0;
-
- if((rc = _cmMidiFileRead8(mfp,&metaId)) != kOkMfRC )
- return rc;
-
- if((rc = _cmMidiFileReadVarLen(mfp,&byteN)) != kOkMfRC )
- return rc;
-
- //printf("mt: %i 0x%x n:%i\n",metaId,metaId,byteN);
-
- switch( metaId )
- {
- case kSeqNumbMdId: rc = _cmMidiFileRead16(mfp,&tmp->u.sVal); break;
- case kTextMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
- case kCopyMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
- case kTrkNameMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
- case kInstrNameMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
- case kLyricsMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
- case kMarkerMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
- case kCuePointMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
- case kMidiChMdId: rc = _cmMidiFileRead8(mfp,&tmp->u.bVal); break;
- case kEndOfTrkMdId: break;
- case kTempoMdId: rc = _cmMidiFileRead24(mfp,&tmp->u.iVal); break;
- case kSmpteMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiSmpte_t)); break;
- case kTimeSigMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiTimeSig_t)); break;
- case kKeySigMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiKeySig_t)); break;
- case kSeqSpecMdId: rc = _cmMidiFileReadSysEx(mfp,tmp,byteN); break;
-
- default:
- cmFileSeek(mfp->fh,kCurFileFl,byteN);
- rc = cmErrMsg(&mfp->err,kUnknownMetaIdMfRC,"Unknown meta status:0x%x %i.",metaId,metaId);
- }
-
- tmp->metaId = metaId;
-
- return rc;
- }
-
-
- cmMfRC_t _cmMidiFileReadTrack( _cmMidiFile_t* mfp, unsigned short trkIdx )
- {
- cmMfRC_t rc = kOkMfRC;
- unsigned dticks = 0;
- cmMidiByte_t status;
- cmMidiByte_t runstatus = 0;
- bool contFl = true;
-
- while( contFl && (rc==kOkMfRC))
- {
- cmMidiTrackMsg_t* tmp = NULL;
-
- // read the tick count
- if((rc = _cmMidiFileReadVarLen(mfp,&dticks)) != kOkMfRC )
- return rc;
-
- // read the status byte
- if((rc = _cmMidiFileRead8(mfp,&status)) != kOkMfRC )
- return rc;
-
- //printf("st:%i 0x%x\n",status,status);
-
- // append a track msg
- if((rc = _cmMidiFileAppendTrackMsg( mfp, trkIdx, dticks, status, &tmp )) != kOkMfRC )
- return rc;
-
- // switch on status
- switch( status )
- {
- // handle sys-ex msg
- case kSysExMdId:
- rc = _cmMidiFileReadSysEx(mfp,tmp,cmInvalidCnt);
- break;
-
- // handle meta msg
- case kMetaStId:
- rc = _cmMidiFileReadMetaMsg(mfp,tmp);
-
- // ignore unknown meta messages
- if( rc == kUnknownMetaIdMfRC )
- rc = kOkMfRC;
-
- contFl = tmp->metaId != kEndOfTrkMdId;
- break;
-
- default:
- // handle channel msg
- rc = _cmMidiFileReadChannelMsg(mfp,&runstatus,status,tmp);
-
- }
- }
- return rc;
- }
-
-
- cmMfRC_t _cmMidiFileReadHdr( _cmMidiFile_t* mfp )
- {
- cmMfRC_t rc;
- unsigned fileId;
- unsigned chunkByteN;
-
- // read the file id
- if((rc = _cmMidiFileRead32(mfp,&fileId)) != kOkMfRC )
- return rc;
-
- // verify the file id
- if( fileId != 'MThd' )
- return cmErrMsg(&mfp->err,kNotAMidiFileMfRC,"");
-
- // read the file chunk byte count
- if((rc = _cmMidiFileRead32(mfp,&chunkByteN)) != kOkMfRC )
- return rc;
-
- // read the format id
- if((rc = _cmMidiFileRead16(mfp,&mfp->fmtId)) != kOkMfRC )
- return rc;
-
- // read the track count
- if((rc = _cmMidiFileRead16(mfp,&mfp->trkN)) != kOkMfRC )
- return rc;
-
- // read the ticks per quarter note
- if((rc = _cmMidiFileRead16(mfp,&mfp->ticksPerQN)) != kOkMfRC )
- return rc;
-
- // if the division field was given in smpte
- if( mfp->ticksPerQN & 0x8000 )
- {
- mfp->smpteFmtId = (mfp->ticksPerQN & 0x7f00) >> 8;
- mfp->smpteTicksPerFrame = (mfp->ticksPerQN & 0xFF);
- mfp->ticksPerQN = 0;
- }
-
- // allocate and zero the track array
- if( mfp->trkN )
- mfp->trkV = _cmMidiFileMalloc( mfp, sizeof(_cmMidiTrack_t)*mfp->trkN);
-
- return rc;
- }
-
- int _cmMidiFileSortFunc( const void *p0, const void* p1 )
- {
- if( (*(cmMidiTrackMsg_t**)p0)->atick == (*(cmMidiTrackMsg_t**)p1)->atick )
- return 0;
-
- return (*(cmMidiTrackMsg_t**)p0)->atick < (*(cmMidiTrackMsg_t**)p1)->atick ? -1 : 1;
- }
-
- cmMfRC_t _cmMidiFileClose( _cmMidiFile_t* mfp )
- {
- cmMfRC_t rc = kOkMfRC;
-
- if( mfp == NULL )
- return rc;
-
- cmMemPtrFree(&mfp->msgV);
-
- if( cmFileIsValid( mfp->fh ) )
- if( cmFileClose( &mfp->fh ) != kOkFileRC )
- rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file close failed.");
-
- if( cmLHeapIsValid( mfp->lhH ) )
- cmLHeapDestroy(&mfp->lhH);
-
- cmMemPtrFree(&mfp);
-
- return rc;
-
- }
-
-
- cmMfRC_t cmMidiFileOpen( cmCtx_t* ctx, cmMidiFileH_t* hPtr, const char* fn )
- {
- 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
- if(( mfp = cmMemAllocZ( _cmMidiFile_t, 1)) == NULL )
- return rc = cmErrMsg(&err,kMemAllocFailMfRC,"MIDI file memory allocation failed.");
-
- cmErrClone(&mfp->err,&err);
-
- // allocate the linked heap
- if( cmLHeapIsValid( mfp->lhH = cmLHeapCreate( 1024, ctx )) == false )
- {
- rc = cmErrMsg(&err,kMemAllocFailMfRC,"MIDI heap allocation failed.");
- goto errLabel;
- }
-
- // open the file
- if(cmFileOpen(&mfp->fh,fn,kReadFileFl | kBinaryFileFl,mfp->err.rpt) != kOkFileRC )
- {
- rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file open failed.");
- goto errLabel;
- }
-
- // read header and setup track array
- if(( rc = _cmMidiFileReadHdr(mfp)) != kOkMfRC )
- goto errLabel;
-
- while( !cmFileEof(mfp->fh) && trkIdx < mfp->trkN )
- {
- unsigned chkId=0,chkN=0;
-
- // read the chunk id
- if((rc = _cmMidiFileRead32(mfp,&chkId)) != kOkMfRC )
- goto errLabel;
-
- // read the chunk size
- if((rc = _cmMidiFileRead32(mfp,&chkN)) != kOkMfRC )
- goto errLabel;
-
- // if this is not a trk chunk then skip it
- if( chkId != (unsigned)'MTrk')
- {
- //if( fseek( mfp->fp, chkN, SEEK_CUR) != 0 )
- if( cmFileSeek(mfp->fh,kCurFileFl,chkN) != kOkFileRC )
- {
- rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file seek failed.");
- goto errLabel;
- }
- }
- else
- {
- if((rc = _cmMidiFileReadTrack(mfp,trkIdx)) != kOkMfRC )
- goto errLabel;
-
- ++trkIdx;
- }
- }
-
- // get the total trk msg count
- mfp->msgN = 0;
- for(trkIdx=0; trkIdx<mfp->trkN; ++trkIdx)
- mfp->msgN += mfp->trkV[ trkIdx ].cnt;
-
- // allocate the trk msg index vector: msgV[]
- mfp->msgV = cmMemAllocZ(cmMidiTrackMsg_t*, mfp->msgN);
-
- // store a pointer to every trk msg in msgV[]
- // and convert tick to absolute tick
- mfp->nextUid = 0;
-
- double microsPerQN = 60000000/120; // default tempo;
- double microsPerTick;
-
- unsigned i = 0;
- for(trkIdx=0; trkIdx<mfp->trkN; ++trkIdx)
- {
- unsigned tick = 0;
- cmMidiTrackMsg_t* tmp = mfp->trkV[ trkIdx ].base;
-
-
- microsPerTick = microsPerQN / mfp->ticksPerQN;
-
-
- while( tmp != NULL )
- {
- assert( i < mfp->msgN);
-
- tick += tmp->dtick; // convert delta-ticks to absolute ticks
- tmp->atick = tick;
- tmp->uid = mfp->nextUid++; // assign the msg uid
- mfp->msgV[i] = tmp;
-
- // track tempo changes
- if( tmp->status == kMetaStId && tmp->metaId == kTempoMdId )
- microsPerTick = tmp->u.iVal / mfp->ticksPerQN;
-
- // convert dtick to microseconds
- tmp->dmicro = round(tmp->dtick * microsPerTick);
-
- tmp = tmp->link;
- ++i;
- }
- }
-
- // sort msgV[] in ascending order on atick
- qsort( mfp->msgV, mfp->msgN, sizeof(cmMidiTrackMsg_t*), _cmMidiFileSortFunc );
-
- // set the amicro field of each midi message to the
- // absolute time offset in microseconds
- unsigned mi;
- unsigned amicro = 0;
- microsPerTick = microsPerQN / mfp->ticksPerQN;
-
- for(mi=0; mi<mfp->msgN; ++mi)
- {
- cmMidiTrackMsg_t* mp = mfp->msgV[mi];
-
- // track tempo changes
- if( mp->status == kMetaStId && mp->metaId == kTempoMdId )
- microsPerTick = mp->u.iVal / mfp->ticksPerQN;
-
- unsigned dtick = 0;
- if( mi > 0 )
- {
- assert( mp->atick >= mfp->msgV[mi-1]->atick );
- dtick = mp->atick - mfp->msgV[mi-1]->atick;
- }
-
- amicro += round(microsPerTick*dtick);
- mp->amicro = amicro;
- }
-
- //for(i=0; i<25; ++i)
- // printf("%i 0x%x 0x%x\n",mfp->msgV[i]->tick,mfp->msgV[i]->status,mfp->msgV[i]->metaId);
-
- mfp->fn = _cmMidiFileMalloc(mfp,strlen(fn)+1);
- assert( mfp->fn != NULL );
- strcpy(mfp->fn,fn);
-
-
- hPtr->h = mfp;
-
- errLabel:
-
- if( cmFileClose(&mfp->fh) != kOkFileRC )
- rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file close failed.");
-
- if( rc != kOkMfRC )
- _cmMidiFileClose(mfp);
-
- return rc;
- }
-
-
-
- cmMfRC_t cmMidiFileClose( cmMidiFileH_t* h )
- {
- cmMfRC_t rc;
-
- if( cmMidiFileIsNull(*h) )
- return kOkMfRC;
-
- if((rc = _cmMidiFileClose(_cmMidiFileHandleToPtr(*h))) == kOkMfRC )
- return rc;
-
- h->h = NULL;
- return rc;
- }
-
- cmMfRC_t _cmMidiFileWrite8( _cmMidiFile_t* mfp, unsigned char v )
- {
- cmMfRC_t rc = kOkMfRC;
-
- if( cmFileWriteUChar(mfp->fh,&v,1) != kOkFileRC )
- rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file byte write failed.");
-
- return rc;
- }
-
- cmMfRC_t _cmMidiFileWrite16( _cmMidiFile_t* mfp, unsigned short v )
- {
- cmMfRC_t rc = kOkMfRC;
-
- v = mfSwap16(v);
-
- if( cmFileWriteUShort(mfp->fh,&v,1) != kOkFileRC )
- rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file short integer write failed.");
-
- return rc;
- }
-
- cmMfRC_t _cmMidiFileWrite24( _cmMidiFile_t* mfp, unsigned v )
- {
- cmMfRC_t rc = kOkMfRC;
- unsigned mask = 0xff0000;
- int i;
-
- for(i=2; i>=0; --i)
- {
- unsigned char c = (v & mask) >> (i*8);
- mask >>= 8;
-
- if( cmFileWriteUChar(mfp->fh,&c,1) != kOkFileRC )
- {
- rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file 24 bit integer write failed.");
- goto errLabel;
- }
-
- }
-
- errLabel:
- return rc;
- }
-
- cmMfRC_t _cmMidiFileWrite32( _cmMidiFile_t* mfp, unsigned v )
- {
- cmMfRC_t rc = kOkMfRC;
-
- v = mfSwap32(v);
-
- if( cmFileWriteUInt(mfp->fh,&v,1) != kOkFileRC )
- rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file integer write failed.");
-
- return rc;
- }
-
- cmMfRC_t _cmMidiFileWriteRecd( _cmMidiFile_t* mfp, const void* v, unsigned byteCnt )
- {
- cmMfRC_t rc = kOkMfRC;
-
- if( cmFileWriteChar(mfp->fh,v,byteCnt) != kOkFileRC )
- rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file write record failed.");
-
- return rc;
- }
-
- cmMfRC_t _cmMidiFileWriteVarLen( _cmMidiFile_t* mfp, unsigned v )
- {
- cmMfRC_t rc = kOkMfRC;
- unsigned buf = v & 0x7f;
-
- while((v >>= 7) > 0 )
- {
- buf <<= 8;
- buf |= 0x80;
- buf += (v & 0x7f);
- }
-
- while(1)
- {
- unsigned char c = (unsigned char)(buf & 0xff);
- if( cmFileWriteUChar(mfp->fh,&c,1) != kOkFileRC )
- {
- rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file variable length integer write failed.");
- goto errLabel;
- }
-
- if( buf & 0x80 )
- buf >>= 8;
- else
- break;
- }
-
- errLabel:
- return rc;
- }
-
- cmMfRC_t _cmMidiFileWriteHdr( _cmMidiFile_t* mfp )
- {
- cmMfRC_t rc;
- unsigned fileId = 'MThd';
- unsigned chunkByteN = 6;
-
- // write the file id ('MThd')
- if((rc = _cmMidiFileWrite32(mfp,fileId)) != kOkMfRC )
- return rc;
-
- // write the file chunk byte count (always 6)
- if((rc = _cmMidiFileWrite32(mfp,chunkByteN)) != kOkMfRC )
- return rc;
-
- // write the MIDI file format id (0,1,2)
- if((rc = _cmMidiFileWrite16(mfp,mfp->fmtId)) != kOkMfRC )
- return rc;
-
- // write the track count
- if((rc = _cmMidiFileWrite16(mfp,mfp->trkN)) != kOkMfRC )
- return rc;
-
- unsigned short v = 0;
-
- // if the ticks per quarter note field is valid ...
- if( mfp->ticksPerQN )
- v = mfp->ticksPerQN;
- else
- {
- // ... otherwise the division field was given in smpte
- v = mfp->smpteFmtId << 8;
- v += mfp->smpteTicksPerFrame;
- }
-
- if((rc = _cmMidiFileWrite16(mfp,v)) != kOkMfRC )
- return rc;
-
- return rc;
-
- }
-
-
- cmMfRC_t _cmMidiFileWriteSysEx( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp )
- {
- cmMfRC_t rc = kOkMfRC;
-
- if((rc = _cmMidiFileWrite8(mfp,kSysExMdId)) != kOkMfRC )
- goto errLabel;
-
- if( cmFileWriteUChar(mfp->fh,tmp->u.sysExPtr,tmp->byteCnt) != kOkFileRC )
- rc = cmErrMsg(&mfp->err,kFileFailMfRC,"Sys-ex msg write failed.");
-
- errLabel:
- return rc;
- }
-
- cmMfRC_t _cmMidiFileWriteChannelMsg( _cmMidiFile_t* mfp, const cmMidiTrackMsg_t* tmp, cmMidiByte_t* runStatus )
- {
- cmMfRC_t rc = kOkMfRC;
- unsigned byteN = cmMidiStatusToByteCount(tmp->status);
- cmMidiByte_t status = tmp->status + tmp->u.chMsgPtr->ch;
-
- if( status != *runStatus )
- {
- *runStatus = status;
- if((rc = _cmMidiFileWrite8(mfp,status)) != kOkMfRC )
- goto errLabel;
- }
-
- if(byteN>=1)
- if((rc = _cmMidiFileWrite8(mfp,tmp->u.chMsgPtr->d0)) != kOkMfRC )
- goto errLabel;
-
- if(byteN>=2)
- if((rc = _cmMidiFileWrite8(mfp,tmp->u.chMsgPtr->d1)) != kOkMfRC )
- goto errLabel;
-
- errLabel:
- return rc;
- }
-
- cmMfRC_t _cmMidiFileWriteMetaMsg( _cmMidiFile_t* mfp, const cmMidiTrackMsg_t* tmp )
- {
- cmMfRC_t rc;
-
- if((rc = _cmMidiFileWrite8(mfp,kMetaStId)) != kOkMfRC )
- return rc;
-
- if((rc = _cmMidiFileWrite8(mfp,tmp->metaId)) != kOkMfRC )
- return rc;
-
-
- switch( tmp->metaId )
- {
- case kSeqNumbMdId:
- if((rc = _cmMidiFileWrite8(mfp,sizeof(tmp->u.sVal))) == kOkMfRC )
- rc = _cmMidiFileWrite16(mfp,tmp->u.sVal);
- break;
-
- case kTempoMdId:
- if((rc = _cmMidiFileWrite8(mfp,3)) == kOkMfRC )
- rc = _cmMidiFileWrite24(mfp,tmp->u.iVal);
- break;
-
- case kSmpteMdId:
- if((rc = _cmMidiFileWrite8(mfp,sizeof(cmMidiSmpte_t))) == kOkMfRC )
- rc = _cmMidiFileWriteRecd(mfp,tmp->u.smptePtr,sizeof(cmMidiSmpte_t));
- break;
-
- case kTimeSigMdId:
- if((rc = _cmMidiFileWrite8(mfp,sizeof(cmMidiTimeSig_t))) == kOkMfRC )
- rc = _cmMidiFileWriteRecd(mfp,tmp->u.timeSigPtr,sizeof(cmMidiTimeSig_t));
- break;
-
- case kKeySigMdId:
- if((rc = _cmMidiFileWrite8(mfp,sizeof(cmMidiKeySig_t))) == kOkMfRC )
- rc = _cmMidiFileWriteRecd(mfp,tmp->u.keySigPtr,sizeof(cmMidiKeySig_t));
- break;
-
- case kSeqSpecMdId:
- if((rc = _cmMidiFileWriteVarLen(mfp,sizeof(tmp->byteCnt))) == kOkMfRC )
- rc = _cmMidiFileWriteRecd(mfp,tmp->u.sysExPtr,tmp->byteCnt);
- break;
-
- case kMidiChMdId:
- if((rc = _cmMidiFileWrite8(mfp,sizeof(tmp->u.bVal))) == kOkMfRC )
- rc = _cmMidiFileWrite8(mfp,tmp->u.bVal);
- break;
-
- case kEndOfTrkMdId:
- rc = _cmMidiFileWrite8(mfp,0);
- break;
-
- case kTextMdId:
- case kCopyMdId:
- case kTrkNameMdId:
- case kInstrNameMdId:
- case kLyricsMdId:
- case kMarkerMdId:
- case kCuePointMdId:
- {
- unsigned n = tmp->u.text==NULL ? 0 : strlen(tmp->u.text);
- if((rc = _cmMidiFileWriteVarLen(mfp,n)) == kOkMfRC && n>0 )
- rc = _cmMidiFileWriteRecd(mfp,tmp->u.text,n);
- }
- break;
-
- default:
- {
- // ignore unknown meta messages
- }
-
- }
-
- return rc;
- }
-
- cmMfRC_t _cmMidiFileWriteTrack( _cmMidiFile_t* mfp, unsigned trkIdx )
- {
- cmMfRC_t rc = kOkMfRC;
- cmMidiTrackMsg_t* tmp = mfp->trkV[trkIdx].base;
- cmMidiByte_t runStatus = 0;
-
- for(; tmp!=NULL; tmp=tmp->link)
- {
- // write the msg tick count
- if((rc = _cmMidiFileWriteVarLen(mfp,tmp->dtick)) != kOkMfRC )
- return rc;
-
- // switch on status
- switch( tmp->status )
- {
- // handle sys-ex msg
- case kSysExMdId:
- rc = _cmMidiFileWriteSysEx(mfp,tmp);
- break;
-
- // handle meta msg
- case kMetaStId:
- rc = _cmMidiFileWriteMetaMsg(mfp,tmp);
- break;
-
- default:
- // handle channel msg
- rc = _cmMidiFileWriteChannelMsg(mfp,tmp,&runStatus);
-
- }
-
- }
-
- return rc;
- }
-
- cmMfRC_t cmMidiFileWrite( cmMidiFileH_t h, const char* fn )
- {
- cmMfRC_t rc = kOkMfRC;
- _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
- unsigned i;
-
- // create the output file
- if( cmFileOpen(&mfp->fh,fn,kWriteFileFl,mfp->err.rpt) != kOkFileRC )
- return cmErrMsg(&mfp->err,kFileFailMfRC,"The MIDI file '%s' could not be created.",cmStringNullGuard(fn));
-
- // write the file header
- if((rc = _cmMidiFileWriteHdr(mfp)) != kOkMfRC )
- {
- rc = cmErrMsg(&mfp->err,rc,"The file header write failed on the MIDI file '%s'.",cmStringNullGuard(fn));
- goto errLabel;
- }
-
- for(i=0; i < mfp->trkN; ++i )
- {
- unsigned chkId='MTrk';
- long offs0,offs1;
-
- // write the track chunk id ('MTrk')
- if((rc = _cmMidiFileWrite32(mfp,chkId)) != kOkMfRC )
- goto errLabel;
-
- cmFileTell(mfp->fh,&offs0);
-
- // write the track chunk size as zero
- if((rc = _cmMidiFileWrite32(mfp,0)) != kOkMfRC )
- goto errLabel;
-
- if((rc = _cmMidiFileWriteTrack(mfp,i)) != kOkMfRC )
- goto errLabel;
-
- cmFileTell(mfp->fh,&offs1);
-
- cmFileSeek(mfp->fh,kBeginFileFl,offs0);
-
- _cmMidiFileWrite32(mfp,offs1-offs0-4);
-
- cmFileSeek(mfp->fh,kBeginFileFl,offs1);
-
- }
-
- errLabel:
- cmFileClose(&mfp->fh);
- return rc;
- }
-
-
- bool cmMidiFileIsValid( cmMidiFileH_t h )
- { return !cmMidiFileIsNull(h); }
-
- unsigned cmMidiFileTrackCount( cmMidiFileH_t h )
- {
- _cmMidiFile_t* mfp;
-
- if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
- return cmInvalidCnt;
-
- return mfp->trkN;
- }
-
- unsigned cmMidiFileType( cmMidiFileH_t h )
- {
- _cmMidiFile_t* mfp;
-
- if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
- return cmInvalidId;
-
- return mfp->fmtId;
- }
-
- const char* cmMidiFileName( cmMidiFileH_t h )
- {
- _cmMidiFile_t* mfp;
- if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
- return NULL;
- return mfp->fn;
- }
-
- unsigned cmMidiFileTicksPerQN( cmMidiFileH_t h )
- {
- _cmMidiFile_t* mfp;
-
- if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
- return cmInvalidCnt;
-
- return mfp->ticksPerQN;
- }
-
- cmMidiByte_t cmMidiFileTicksPerSmpteFrame( cmMidiFileH_t h )
- {
- _cmMidiFile_t* mfp;
-
- if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
- return kInvalidMidiByte;
-
- if( mfp->ticksPerQN != 0 )
- return 0;
-
- return mfp->smpteTicksPerFrame;
- }
-
- cmMidiByte_t cmMidiFileSmpteFormatId( cmMidiFileH_t h )
- {
- _cmMidiFile_t* mfp;
-
- if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
- return kInvalidMidiByte;
-
- if( mfp->ticksPerQN != 0 )
- return 0;
-
- return mfp->smpteFmtId;
- }
-
- unsigned cmMidiFileTrackMsgCount( cmMidiFileH_t h, unsigned trackIdx )
- {
- _cmMidiFile_t* mfp;
-
- if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
- return cmInvalidCnt;
-
- return mfp->trkV[trackIdx].cnt;
- }
-
-
- const cmMidiTrackMsg_t* cmMidiFileTrackMsg( cmMidiFileH_t h, unsigned trackIdx )
- {
- _cmMidiFile_t* mfp;
-
- if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
- return NULL;
-
- return mfp->trkV[trackIdx].base;
- }
-
- unsigned cmMidiFileMsgCount( cmMidiFileH_t h )
- {
- _cmMidiFile_t* mfp;
-
- if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
- return cmInvalidCnt;
-
- return mfp->msgN;
- }
-
- const cmMidiTrackMsg_t** cmMidiFileMsgArray( cmMidiFileH_t h )
- {
- _cmMidiFile_t* mfp;
-
- if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
- return NULL;
-
- // this cast is needed to eliminate an apparently needless 'incompatible type' warning
- return (const cmMidiTrackMsg_t**)mfp->msgV;
- }
-
- unsigned cmMidiFileSeekUsecs( cmMidiFileH_t h, unsigned offsUSecs, unsigned* msgUsecsPtr, unsigned* microsPerTickPtr )
- {
- _cmMidiFile_t* p;
-
- if((p = _cmMidiFileHandleToPtr(h)) == NULL )
- return cmInvalidIdx;
-
- if( p->msgN == 0 )
- return cmInvalidIdx;
-
- unsigned mi;
- double microsPerQN = 60000000.0/120.0;
- double microsPerTick = microsPerQN / p->ticksPerQN;
- double accUSecs = 0;
-
- for(mi=0; mi<p->msgN; ++mi)
- {
- const cmMidiTrackMsg_t* mp = p->msgV[mi];
- /*
- if( mp->status == kMetaStId && mp->metaId == kTempoMdId )
- microsPerTick = mp->u.iVal / p->ticksPerQN;
-
- unsigned dtick = 0;
- if( mi > 0 )
- {
- assert( mp->atick >= p->msgV[mi-1]->atick )
- dtick = mp->atick - p->msgV[mi-1]->atick;
- }
-
- accUSecs += dtick * microsPerTick ;
-
- if( accUSecs >= offsUSecs )
- break;
- */
-
- if( mp->amicro >= offsUSecs )
- break;
- }
-
- if( mi == p->msgN )
- return cmInvalidIdx;
-
- if( msgUsecsPtr != NULL )
- *msgUsecsPtr = round(accUSecs - offsUSecs);
-
- if( microsPerTickPtr != NULL )
- *microsPerTickPtr = round(microsPerTick);
-
- return mi;
- }
-
- double cmMidiFileDurSecs( cmMidiFileH_t h )
- {
- _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
-
- if( mfp->msgN == 0 )
- return 0;
-
- return mfp->msgV[ mfp->msgN-1 ]->amicro / 1000000.0;
- }
-
- typedef struct _cmMidiVoice_str
- {
- const cmMidiTrackMsg_t* mp;
- unsigned durMicros;
- bool sustainFl;
- struct _cmMidiVoice_str* link;
- } _cmMidiVoice_t;
-
-
- void _cmMidFileCalcNoteDurationReleaseNote( _cmMidiVoice_t** listPtrPtr, _cmMidiVoice_t* pp, _cmMidiVoice_t* vp )
- {
- assert( (pp==NULL && vp==*listPtrPtr) || pp->link==vp);
-
- // store the duration of the note into the track msg
- // assoc'd with the note-on msg
- cmMidiChMsg_t* cmp = (cmMidiChMsg_t*)vp->mp->u.chMsgPtr; // cast away const
- cmp->durMicros = vp->durMicros;
-
- _cmMidiVoice_t* np = vp->link;
-
- // release the voice msg
- cmMemFree(vp);
-
- // unlink the active voice msg
- if( pp == NULL )
- *listPtrPtr = np;
- else
- pp->link = np;
-
- }
-
- void _cmMidiFileCalcNoteDurationsAllocVoice( _cmMidiVoice_t** listPtrPtr, cmMidiTrackMsg_t* mp, bool sustainFl )
- {
- _cmMidiVoice_t* vp = cmMemAllocZ(_cmMidiVoice_t,1);
- vp->mp = mp;
- vp->sustainFl = sustainFl;
- vp->link = *listPtrPtr;
- *listPtrPtr = vp;
- }
-
- void cmMidiFileCalcNoteDurations( cmMidiFileH_t h )
- {
- _cmMidiFile_t* p;
-
- if((p = _cmMidiFileHandleToPtr(h)) == NULL )
- return;
-
- if( p->msgN == 0 )
- return;
-
- unsigned mi = cmInvalidId;
- _cmMidiVoice_t* list = NULL; // list of active voices
- _cmMidiVoice_t* vp = NULL;
- cmMidiTrackMsg_t* sustainPedalDownMsg = NULL;
- bool sustainFlagV[ kMidiChCnt ];
-
- // clear the sustain pedal flag
- for(mi=0; mi<kMidiChCnt; ++mi)
- sustainFlagV[mi]=false;
-
- for(mi=0; mi<p->msgN; ++mi)
- {
- cmMidiTrackMsg_t* mp = p->msgV[mi];
-
- unsigned d_amicro = 0;
- if( mi > 0 )
- {
- assert( mp->amicro >= p->msgV[mi-1]->amicro );
- d_amicro = mp->amicro - p->msgV[mi-1]->amicro;
- }
-
- // update the duration of the sounding notes
- for(vp = list; vp!=NULL; vp=vp->link)
- vp->durMicros += d_amicro;
-
- // update the sustain pedal duration
- if( sustainPedalDownMsg != NULL )
- ((cmMidiChMsg_t*)(sustainPedalDownMsg->u.chMsgPtr))->durMicros += d_amicro; // cast away const
-
- //
- // If this is sustain pedal msg
- //
- if( mp->status==kCtlMdId && mp->u.chMsgPtr->d0 == kSustainCtlMdId )
- {
- unsigned chIdx = mp->u.chMsgPtr->ch;
- assert( chIdx < kMidiChCnt );
-
- // set the state of the sustain pedal flags
- sustainFlagV[chIdx] = mp->u.chMsgPtr->d1 >= 64;
-
- // if the pedal went down ...
- if( sustainFlagV[chIdx] )
- {
-
- if( sustainPedalDownMsg != NULL )
- {
- // TODO: the correct way to handle this is to maintain multiple sustain pedals
- cmErrMsg(&p->err,kSustainPedalMfRC,"Sustain pedal down with no intervening sustain pedal up.");
- }
- else
- {
- sustainPedalDownMsg = mp;
- ((cmMidiChMsg_t*)(sustainPedalDownMsg->u.chMsgPtr))->durMicros = 0; // cast away const
- }
-
- _cmMidiFileCalcNoteDurationsAllocVoice( &list, mp, true );
- }
- else // ... if the pedal went up
- {
-
- sustainPedalDownMsg = NULL;
-
- // ... then release sustaining notes
- _cmMidiVoice_t* pp = NULL;
- for(vp=list; vp != NULL; )
- {
- _cmMidiVoice_t* np = vp->link;
- if( vp->sustainFl && (vp->mp->u.chMsgPtr->ch == chIdx) )
- _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
- else
- pp = vp;
-
- vp = np;
- }
- }
- }
-
- //
- // if this is a note-on msg
- //
- if( mp->status==kNoteOnMdId && mp->u.chMsgPtr->d1>0 )
- {
- _cmMidiFileCalcNoteDurationsAllocVoice( &list, mp, false );
- }
- else
- //
- // if this is a note-off msg
- //
- if( (mp->status==kNoteOnMdId && mp->u.chMsgPtr->d1==0) || (mp->status==kNoteOffMdId) )
- {
- _cmMidiVoice_t* pp = NULL;
- unsigned chIdx = mp->u.chMsgPtr->ch;
- assert( chIdx < kMidiChCnt );
-
-
- // for each active voice
- for(vp=list; vp!=NULL; vp=vp->link )
- {
- // if this active voice ch/pitch matches the note-off msg ch pitch
- if( (vp->mp->u.chMsgPtr->d0==mp->u.chMsgPtr->d0) && (vp->mp->u.chMsgPtr->ch==chIdx) )
- {
- // if the sustain pedal is down for this channel - then defer turning the note off
- if( sustainFlagV[chIdx] )
- vp->sustainFl = true;
- else
- _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
- break;
- }
-
- pp = vp;
- } // end for
- } // end if
-
- } // end-for
-
-
- // check for hung notes
- _cmMidiVoice_t* np = NULL;
- vp = list;
- while( vp != NULL )
- {
- np = vp->link;
-
- if( cmMidiIsNoteOn(vp->mp->status) == false )
- cmErrMsg(&p->err,kMissingNoteOffMfRC,"Missing note-off for note-on:%s",cmMidiToSciPitch(vp->mp->u.chMsgPtr->d0,NULL,0));
-
- cmMemFree(vp);
- vp = np;
- }
- }
-
-
- void cmMidiFileSetDelay( cmMidiFileH_t h, unsigned ticks )
- {
- _cmMidiFile_t* p;
- unsigned mi;
-
- if((p = _cmMidiFileHandleToPtr(h)) == NULL )
- return;
-
- if( p->msgN == 0 )
- return;
-
- for(mi=0; mi<p->msgN; ++mi)
- {
- cmMidiTrackMsg_t* mp = p->msgV[mi];
-
- // locate the first msg which has a non-zero delta tick
- if( mp->dtick > 0 )
- {
- mp->dtick = ticks;
- break;
- }
- }
- }
-
- unsigned cmMidiFilePackTrackMsgBufByteCount( const cmMidiTrackMsg_t* m )
- { return sizeof(cmMidiTrackMsg_t) + m->byteCnt; }
-
- cmMidiTrackMsg_t* cmMidiFilePackTrackMsg( const cmMidiTrackMsg_t* m, void* buf, unsigned bufByteCnt )
- {
- unsigned n = sizeof(cmMidiTrackMsg_t) + m->byteCnt;
-
- if( n < bufByteCnt )
- {
- assert(0);
- return NULL;
- }
-
- // copy the cmMidiTrackMsg_t into the buffer
- memcpy(buf, m, sizeof(cmMidiTrackMsg_t));
-
- if( m->byteCnt > 0 )
- {
- // copy any linked data into the buffer
- memcpy(buf + sizeof(cmMidiTrackMsg_t), m->u.voidPtr, m->byteCnt );
-
- // fixup the linked data ptr
- cmMidiTrackMsg_t* mp = (cmMidiTrackMsg_t*)buf;
- mp->u.voidPtr = buf + sizeof(cmMidiTrackMsg_t);
- }
- return (cmMidiTrackMsg_t*)buf;
- }
-
- void _cmMidiFilePrintHdr( const _cmMidiFile_t* mfp, cmRpt_t* rpt )
- {
- if( mfp->fn != NULL )
- cmRptPrintf(rpt,"%s ",mfp->fn);
-
- cmRptPrintf(rpt,"fmt:%i ticksPerQN:%i tracks:%i\n",mfp->fmtId,mfp->ticksPerQN,mfp->trkN);
- }
-
- void _cmMidiFilePrintMsg( cmRpt_t* rpt, const cmMidiTrackMsg_t* tmp )
- {
- cmRptPrintf(rpt,"%5i %8i %8i %8i %8i : ",
- tmp->uid,
- tmp->dtick,
- tmp->dmicro,
- tmp->atick,
- tmp->amicro );
-
- if( tmp->status == kMetaStId )
- {
- cmRptPrintf(rpt,"%s ", cmMidiMetaStatusToLabel(tmp->metaId));
- }
- else
- {
- cmRptPrintf(rpt,"%4s %3i %3i %3i",
- cmMidiStatusToLabel(tmp->status),
- tmp->u.chMsgPtr->ch,
- tmp->u.chMsgPtr->d0,
- tmp->u.chMsgPtr->d1);
- }
-
- if( cmMidiIsChStatus(tmp->status) && cmMidiIsNoteOn(tmp->status) && (tmp->u.chMsgPtr->d1>0) )
- cmRptPrintf(rpt," %4s ",cmMidiToSciPitch(tmp->u.chMsgPtr->d0,NULL,0));
-
-
- cmRptPrintf(rpt,"\n");
- }
-
- void cmMidiFilePrintMsgs( cmMidiFileH_t h, cmRpt_t* rpt )
- {
- const _cmMidiFile_t* p = _cmMidiFileHandleToPtr(h);
- unsigned mi;
-
- _cmMidiFilePrintHdr(p,rpt);
-
- for(mi=0; mi<p->msgN; ++mi)
- {
- cmMidiTrackMsg_t* mp = p->msgV[mi];
-
- if( mp != NULL )
- _cmMidiFilePrintMsg(rpt,mp);
- }
-
- }
-
- void cmMidiFilePrintTracks( cmMidiFileH_t h, unsigned trkIdx, cmRpt_t* rpt )
- {
- const _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
-
- _cmMidiFilePrintHdr(mfp,rpt);
-
- int i = trkIdx == cmInvalidIdx ? 0 : trkIdx;
- int n = trkIdx == cmInvalidIdx ? mfp->trkN : trkIdx+1;
-
- for(; i<n; ++i)
- {
- cmRptPrintf(rpt,"Track:%i\n",i);
-
- cmMidiTrackMsg_t* tmp = mfp->trkV[i].base;
- while( tmp != NULL )
- {
- _cmMidiFilePrintMsg(rpt,tmp);
- tmp = tmp->link;
- }
- }
- }
-
- bool cmMidiFileIsNull( cmMidiFileH_t h )
- { return (_cmMidiFile_t*)h.h == NULL; }
-
-
- void cmMidiFileTestPrint( void* printDataPtr, const char* fmt, va_list vl )
- { vprintf(fmt,vl); }
-
-
- void cmMidiFilePrintControlNumbers( cmCtx_t* ctx, const char* fn )
- {
- cmMidiFileH_t h = cmMidiFileNullHandle;
- cmMfRC_t rc;
-
- if((rc = cmMidiFileOpen(ctx, &h, fn )) != kOkMfRC )
- {
- cmErrMsg(&ctx->err,rc,"MIDI file open failed on '%s'.",fn);
- goto errLabel;
- }
-
- const cmMidiTrackMsg_t** mm;
- unsigned n = cmMidiFileMsgCount(h);
- if((mm = cmMidiFileMsgArray(h)) != NULL )
- {
- unsigned j;
- for(j=0; j<n; ++j)
- {
- const cmMidiTrackMsg_t* m = mm[j];
-
- if( m->status == kCtlMdId && m->u.chMsgPtr->d0==66 )
- printf("%i %i\n",m->u.chMsgPtr->d0,m->u.chMsgPtr->d1);
- }
- }
-
- errLabel:
- cmMidiFileClose(&h);
-
- }
-
- void cmMidiFileTest( const char* fn, cmCtx_t* ctx )
- {
- cmMfRC_t rc;
- cmMidiFileH_t h = cmMidiFileNullHandle;
-
- if((rc = cmMidiFileOpen(ctx,&h,fn)) != kOkMfRC )
- {
- printf("Error:%i Unable to open the cmMidi file: %s\n",rc,fn);
- return;
- }
-
- if( 1 )
- {
- //cmMidiFileTickToMicros( h );
- //cmMidiFileTickToSamples(h,96000,false);
- cmMidiFilePrintMsgs(h,&ctx->rpt);
- }
-
- if( 0 )
- {
- //cmMidiFilePrint(h,cmMidiFileTrackCount(h)-1,&ctx->rpt);
- //cmMidiFilePrint(h,cmInvalidIdx,&ctx->rpt);
- cmMidiFilePrintControlNumbers(ctx, fn );
-
- }
- if( 0 )
- {
- printf("Tracks:%i\n",cmMidiFileTrackCount(h));
-
- unsigned i = 0;
- for(i=0; i<cmMidiFileMsgCount(h); ++i)
- {
- cmMidiTrackMsg_t* tmp = (cmMidiTrackMsg_t*)cmMidiFileMsgArray(h)[i];
-
- if( tmp->status==kMetaStId && tmp->metaId == kTempoMdId )
- {
- double bpm = 60000000.0/tmp->u.iVal;
- printf("Tempo:%i %f\n",tmp->u.iVal,bpm);
-
- tmp->u.iVal = floor( 60000000.0/69.0 );
-
- break;
- }
- }
-
- cmMidiFileWrite(h,"/home/kevin/temp/test0.mid");
- }
-
- cmMidiFileClose(&h);
- }
|