libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmMidiFile.c 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmFile.h"
  7. #include "cmMem.h"
  8. #include "cmMallocDebug.h"
  9. #include "cmLinkedHeap.h"
  10. #include "cmMidi.h"
  11. #include "cmMidiFile.h"
  12. #ifdef cmBIG_ENDIAN
  13. #define mfSwap16(v) (v)
  14. #define mfSwap32(v) (v)
  15. #else
  16. #define mfSwap16(v) cmSwap16(v)
  17. #define mfSwap32(v) cmSwap32(v)
  18. #endif
  19. typedef struct
  20. {
  21. unsigned cnt; // count of track records
  22. cmMidiTrackMsg_t* base; // pointer to first track recd
  23. cmMidiTrackMsg_t* last; // pointer to last track recd
  24. } _cmMidiTrack_t;
  25. typedef struct
  26. {
  27. cmErr_t err; // this objects error object
  28. cmLHeapH_t lhH; // linked heap used for all dynamically alloc'd data space
  29. cmFileH_t fh; // cmFile handle (only used in fmMidiFileOpen())
  30. unsigned short fmtId; // midi file type id: 0,1,2
  31. unsigned short ticksPerQN; // ticks per quarter note or 0 if smpteFmtId is valid
  32. cmMidiByte_t smpteFmtId; // smpte format or 0 if ticksPerQN is valid
  33. cmMidiByte_t smpteTicksPerFrame; // smpte ticks per frame or 0 if ticksPerQN is valid
  34. unsigned short trkN; // track count
  35. _cmMidiTrack_t* trkV; // track vector
  36. char* fn; // file name or NULL if this object did not originate from a file
  37. unsigned msgN; // count of msg's in msgV[]
  38. cmMidiTrackMsg_t** msgV; // sorted msg list
  39. } _cmMidiFile_t;
  40. #define _cmMidiFileError( err, rc ) _cmMidiFileOnError(err, rc, __LINE__,__FILE__,__FUNCTION__ )
  41. cmMidiFileH_t cmMidiFileNullHandle = cmSTATIC_NULL_HANDLE;
  42. cmMfRC_t _cmMidiFileOnError( cmErr_t* err, cmMfRC_t rc, int line, const char* fn, const char* func )
  43. {
  44. return cmErrMsg(err,rc,"rc:%i line:%i %s %s\n",rc,line,func,fn);
  45. }
  46. _cmMidiFile_t* _cmMidiFileHandleToPtr( cmMidiFileH_t h )
  47. {
  48. _cmMidiFile_t* p = (_cmMidiFile_t*)h.h;
  49. assert( p != NULL );
  50. return p;
  51. }
  52. void* _cmMidiFileMalloc( _cmMidiFile_t* mfp, unsigned byteN )
  53. { return cmLHeapAllocZ(mfp->lhH,byteN); }
  54. cmMfRC_t _cmMidiFileRead8( _cmMidiFile_t* mfp, cmMidiByte_t* p )
  55. {
  56. if( cmFileReadUChar(mfp->fh,p,1) != kOkFileRC )
  57. return _cmMidiFileError(&mfp->err,kSysFreadFailMfRC);
  58. return kOkMfRC;
  59. }
  60. cmMfRC_t _cmMidiFileRead16( _cmMidiFile_t* mfp, unsigned short* p )
  61. {
  62. if( cmFileReadUShort(mfp->fh,p,1) != kOkFileRC )
  63. return _cmMidiFileError(&mfp->err,kSysFreadFailMfRC);
  64. *p = mfSwap16(*p);
  65. return kOkMfRC;
  66. }
  67. cmMfRC_t _cmMidiFileRead24( _cmMidiFile_t* mfp, unsigned* p )
  68. {
  69. *p = 0;
  70. int i = 0;
  71. for(; i<3; ++i)
  72. {
  73. unsigned char c;
  74. if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
  75. return _cmMidiFileError(&mfp->err,kSysFreadFailMfRC);
  76. *p = (*p << 8) + c;
  77. }
  78. //*p =mfSwap32(*p);
  79. return kOkMfRC;
  80. }
  81. cmMfRC_t _cmMidiFileRead32( _cmMidiFile_t* mfp, unsigned* p )
  82. {
  83. if( cmFileReadUInt(mfp->fh,p,1) != kOkFileRC )
  84. return _cmMidiFileError(&mfp->err,kSysFreadFailMfRC);
  85. *p = mfSwap32(*p);
  86. return kOkMfRC;
  87. }
  88. cmMfRC_t _cmMidiFileReadText( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
  89. {
  90. if( byteN == 0 )
  91. return kOkMfRC;
  92. char* t = (char*)_cmMidiFileMalloc(mfp,byteN+1);
  93. t[byteN] = 0;
  94. if( cmFileReadChar(mfp->fh,t,byteN) != kOkFileRC )
  95. return _cmMidiFileError(&mfp->err,kSysFreadFailMfRC);
  96. tmp->u.text = t;
  97. tmp->byteCnt = byteN;
  98. return kOkMfRC;
  99. }
  100. cmMfRC_t _cmMidiFileReadRecd( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
  101. {
  102. char* t = (char*)_cmMidiFileMalloc(mfp,byteN);
  103. if( cmFileReadChar(mfp->fh,t,byteN) != kOkFileRC )
  104. return _cmMidiFileError(&mfp->err,kSysFreadFailMfRC);
  105. tmp->byteCnt = byteN;
  106. tmp->u.voidPtr = t;
  107. return kOkMfRC;
  108. }
  109. cmMfRC_t _cmMidiFileReadVarLen( _cmMidiFile_t* mfp, unsigned* p )
  110. {
  111. unsigned char c;
  112. if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
  113. return _cmMidiFileError(&mfp->err,kSysFreadFailMfRC);
  114. if( !(c & 0x80) )
  115. *p = c;
  116. else
  117. {
  118. *p = c & 0x7f;
  119. do
  120. {
  121. if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
  122. return _cmMidiFileError(&mfp->err,kSysFreadFailMfRC);
  123. *p = (*p << 7) + (c & 0x7f);
  124. }while( c & 0x80 );
  125. }
  126. return kOkMfRC;
  127. }
  128. cmMfRC_t _cmMidiFileAppendTrackMsg( _cmMidiFile_t* mfp, unsigned short trkIdx, unsigned dtick, cmMidiByte_t status, cmMidiTrackMsg_t** trkMsgPtrPtr )
  129. {
  130. cmMidiTrackMsg_t* tmp = (cmMidiTrackMsg_t*)_cmMidiFileMalloc(mfp, sizeof(cmMidiTrackMsg_t) );
  131. // link new record onto track record chain
  132. if( mfp->trkV[trkIdx].base == NULL )
  133. mfp->trkV[trkIdx].base = tmp;
  134. else
  135. mfp->trkV[trkIdx].last->link = tmp;
  136. mfp->trkV[trkIdx].last = tmp;
  137. mfp->trkV[trkIdx].cnt++;
  138. // set the generic track record fields
  139. tmp->dtick = dtick;
  140. tmp->status = status;
  141. tmp->metaId = kInvalidMetaMdId;
  142. tmp->trkIdx = trkIdx;
  143. tmp->byteCnt = 0;
  144. *trkMsgPtrPtr = tmp;
  145. return kOkMfRC;
  146. }
  147. cmMfRC_t _cmMidiFileReadSysEx( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
  148. {
  149. cmMfRC_t rc = kOkMfRC;
  150. cmMidiByte_t b = 0;
  151. if( byteN == cmInvalidCnt )
  152. {
  153. long offs;
  154. if( cmFileTell(mfp->fh,&offs) != kOkFileRC )
  155. return _cmMidiFileError(&mfp->err,kSysFtellFailMfRC);
  156. byteN = 0;
  157. // get the length of the sys-ex msg
  158. while( !cmFileEof(mfp->fh) && (b != kSysComEoxMdId) )
  159. {
  160. if((rc = _cmMidiFileRead8(mfp,&b)) != kOkMfRC )
  161. return rc;
  162. ++byteN;
  163. }
  164. // verify that the EOX byte was found
  165. if( b != kSysComEoxMdId )
  166. return _cmMidiFileError(&mfp->err,kMissingEoxMfRC);
  167. // rewind to the beginning of the msg
  168. if( cmFileSeek(mfp->fh,kBeginFileFl,offs) != kOkFileRC )
  169. return _cmMidiFileError(&mfp->err,kSysFseekFailMfRC);
  170. }
  171. // allocate memory to hold the sys-ex msg
  172. cmMidiByte_t* mp = (cmMidiByte_t *)_cmMidiFileMalloc(mfp, byteN );
  173. // read the sys-ex msg from the file into msg memory
  174. if( cmFileReadUChar(mfp->fh,mp,byteN) != kOkFileRC )
  175. return _cmMidiFileError(&mfp->err,kSysFreadFailMfRC);
  176. tmp->byteCnt = byteN;
  177. tmp->u.sysExPtr = mp;
  178. return rc;
  179. }
  180. cmMfRC_t _cmMidiFileReadChannelMsg( _cmMidiFile_t* mfp, cmMidiByte_t* rsPtr, cmMidiByte_t status, cmMidiTrackMsg_t* tmp )
  181. {
  182. cmMfRC_t rc = kOkMfRC;
  183. cmMidiChMsg_t* p = (cmMidiChMsg_t*)_cmMidiFileMalloc(mfp,sizeof(cmMidiChMsg_t));
  184. unsigned useRsFl = status <= 0x7f;
  185. cmMidiByte_t statusCh = useRsFl ? *rsPtr : status;
  186. if( useRsFl )
  187. p->d0 = status;
  188. else
  189. *rsPtr = status;
  190. tmp->byteCnt = sizeof(cmMidiChMsg_t);
  191. tmp->status = statusCh & 0xf0;
  192. p->ch = statusCh & 0x0f;
  193. p->durTicks = 0;
  194. unsigned byteN = cmMidiStatusToByteCount(tmp->status);
  195. if( byteN==kInvalidMidiByte || byteN > 2 )
  196. return cmErrMsg(&mfp->err,kInvalidStatusMfRC,"Invalid status:0x%x %i.",tmp->status,tmp->status);
  197. unsigned i;
  198. for(i=useRsFl; i<byteN; ++i)
  199. {
  200. cmMidiByte_t* b = i==0 ? &p->d0 : &p->d1;
  201. if((rc = _cmMidiFileRead8(mfp,b)) != kOkMfRC )
  202. return rc;
  203. }
  204. // convert note-on velocity=0 to note off
  205. if( tmp->status == kNoteOnMdId && p->d1==0 )
  206. tmp->status = kNoteOffMdId;
  207. tmp->u.chMsgPtr = p;
  208. return rc;
  209. }
  210. cmMfRC_t _cmMidiFileReadMetaMsg( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp )
  211. {
  212. cmMidiByte_t metaId;
  213. cmMfRC_t rc;
  214. unsigned byteN = 0;
  215. if((rc = _cmMidiFileRead8(mfp,&metaId)) != kOkMfRC )
  216. return rc;
  217. if((rc = _cmMidiFileReadVarLen(mfp,&byteN)) != kOkMfRC )
  218. return rc;
  219. //printf("mt: %i 0x%x n:%i\n",metaId,metaId,byteN);
  220. switch( metaId )
  221. {
  222. case kSeqNumbMdId: rc = _cmMidiFileRead16(mfp,&tmp->u.sVal); break;
  223. case kTextMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  224. case kCopyMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  225. case kTrkNameMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  226. case kInstrNameMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  227. case kLyricsMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  228. case kMarkerMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  229. case kCuePointMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  230. case kMidiChMdId: rc = _cmMidiFileRead8(mfp,&tmp->u.bVal); break;
  231. case kEndOfTrkMdId: break;
  232. case kTempoMdId: rc = _cmMidiFileRead24(mfp,&tmp->u.iVal); break;
  233. case kSmpteMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiSmpte_t)); break;
  234. case kTimeSigMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiTimeSig_t)); break;
  235. case kKeySigMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiKeySig_t)); break;
  236. case kSeqSpecMdId: rc = _cmMidiFileReadSysEx(mfp,tmp,byteN); break;
  237. default:
  238. cmFileSeek(mfp->fh,kCurFileFl,byteN);
  239. cmErrMsg(&mfp->err,kUnknownMetaIdMfRC,"Unknown meta status:0x%x %i.",metaId,metaId);
  240. rc = _cmMidiFileError(&mfp->err,kUnknownMetaIdMfRC);
  241. }
  242. tmp->metaId = metaId;
  243. return rc;
  244. }
  245. cmMfRC_t _cmMidiFileReadTrack( _cmMidiFile_t* mfp, unsigned short trkIdx )
  246. {
  247. cmMfRC_t rc = kOkMfRC;
  248. unsigned dticks = 0;
  249. cmMidiByte_t status;
  250. cmMidiByte_t runstatus = 0;
  251. bool contFl = true;
  252. while( contFl && (rc==kOkMfRC))
  253. {
  254. cmMidiTrackMsg_t* tmp = NULL;
  255. // read the tick count
  256. if((rc = _cmMidiFileReadVarLen(mfp,&dticks)) != kOkMfRC )
  257. return rc;
  258. // read the status byte
  259. if((rc = _cmMidiFileRead8(mfp,&status)) != kOkMfRC )
  260. return rc;
  261. //printf("st:%i 0x%x\n",status,status);
  262. // append a track msg
  263. if((rc = _cmMidiFileAppendTrackMsg( mfp, trkIdx, dticks, status, &tmp )) != kOkMfRC )
  264. return rc;
  265. // switch on status
  266. switch( status )
  267. {
  268. // handle sys-ex msg
  269. case kSysExMdId:
  270. rc = _cmMidiFileReadSysEx(mfp,tmp,cmInvalidCnt);
  271. break;
  272. // handle meta msg
  273. case kMetaStId:
  274. rc = _cmMidiFileReadMetaMsg(mfp,tmp);
  275. // ignore unknown meta messages
  276. if( rc == kUnknownMetaIdMfRC )
  277. rc = kOkMfRC;
  278. contFl = tmp->metaId != kEndOfTrkMdId;
  279. break;
  280. default:
  281. // handle channel msg
  282. rc = _cmMidiFileReadChannelMsg(mfp,&runstatus,status,tmp);
  283. }
  284. }
  285. return rc;
  286. }
  287. cmMfRC_t _cmMidiFileReadHdr( _cmMidiFile_t* mfp )
  288. {
  289. cmMfRC_t rc;
  290. unsigned fileId;
  291. unsigned chunkByteN;
  292. // read the file id
  293. if((rc = _cmMidiFileRead32(mfp,&fileId)) != kOkMfRC )
  294. return rc;
  295. // verify the file id
  296. if( fileId != 'MThd' )
  297. return _cmMidiFileError(&mfp->err,kNotAMidiFileMfRC);
  298. // read the file chunk byte count
  299. if((rc = _cmMidiFileRead32(mfp,&chunkByteN)) != kOkMfRC )
  300. return rc;
  301. // read the format id
  302. if((rc = _cmMidiFileRead16(mfp,&mfp->fmtId)) != kOkMfRC )
  303. return rc;
  304. // read the track count
  305. if((rc = _cmMidiFileRead16(mfp,&mfp->trkN)) != kOkMfRC )
  306. return rc;
  307. // read the ticks per quarter note
  308. if((rc = _cmMidiFileRead16(mfp,&mfp->ticksPerQN)) != kOkMfRC )
  309. return rc;
  310. // if the division field was given in smpte
  311. if( mfp->ticksPerQN & 0x8000 )
  312. {
  313. mfp->smpteFmtId = (mfp->ticksPerQN & 0x7f00) >> 8;
  314. mfp->smpteTicksPerFrame = (mfp->ticksPerQN & 0xFF);
  315. mfp->ticksPerQN = 0;
  316. }
  317. // allocate and zero the track array
  318. if( mfp->trkN )
  319. mfp->trkV = _cmMidiFileMalloc( mfp, sizeof(_cmMidiTrack_t)*mfp->trkN);
  320. return rc;
  321. }
  322. int _cmMidiFileSortFunc( const void *p0, const void* p1 )
  323. {
  324. //printf("%i %i\n",(*(cmMidiTrackMsg_t**)p0)->dticks,(*(cmMidiTrackMsg_t**)p1)->dticks);
  325. if( (*(cmMidiTrackMsg_t**)p0)->dtick == (*(cmMidiTrackMsg_t**)p1)->dtick )
  326. return 0;
  327. return (*(cmMidiTrackMsg_t**)p0)->dtick < (*(cmMidiTrackMsg_t**)p1)->dtick ? -1 : 1;
  328. }
  329. cmMfRC_t _cmMidiFileClose( _cmMidiFile_t* mfp )
  330. {
  331. cmMfRC_t rc = kOkMfRC;
  332. if( mfp == NULL )
  333. return rc;
  334. cmMemPtrFree(&mfp->msgV);
  335. if( cmFileIsValid( mfp->fh ) )
  336. if( cmFileClose( &mfp->fh ) != kOkFileRC )
  337. rc = _cmMidiFileError(&mfp->err,kSysFcloseFailMfRC);
  338. if( cmLHeapIsValid( mfp->lhH ) )
  339. cmLHeapDestroy(&mfp->lhH);
  340. cmMemPtrFree(&mfp);
  341. return rc;
  342. }
  343. cmMfRC_t cmMidiFileOpen( const char* fn, cmMidiFileH_t* hPtr, cmCtx_t* ctx )
  344. {
  345. cmMfRC_t rc = kOkMfRC;
  346. _cmMidiFile_t* mfp = NULL;
  347. unsigned short trkIdx = 0;
  348. cmErr_t err;
  349. if( cmMidiFileIsValid(*hPtr) )
  350. if((rc = _cmMidiFileClose(_cmMidiFileHandleToPtr(*hPtr))) != kOkMfRC )
  351. return rc;
  352. cmErrSetup(&err,&ctx->rpt,"MIDI File");
  353. // allocate the midi file object
  354. if(( mfp = cmMemAllocZ( _cmMidiFile_t, 1)) == NULL )
  355. return rc = _cmMidiFileError(&err,kMemAllocFailMfRC);
  356. cmErrClone(&mfp->err,&err);
  357. // allocate the linked heap
  358. if( cmLHeapIsValid( mfp->lhH = cmLHeapCreate( 1024, ctx )) == false )
  359. {
  360. rc = _cmMidiFileError(&err,kMemAllocFailMfRC);
  361. goto errLabel;
  362. }
  363. // open the file
  364. if(cmFileOpen(&mfp->fh,fn,kReadFileFl | kBinaryFileFl,mfp->err.rpt) != kOkFileRC )
  365. {
  366. rc = _cmMidiFileError(&mfp->err,kSysFopenFailMfRC);
  367. goto errLabel;
  368. }
  369. // read header and setup track array
  370. if(( rc = _cmMidiFileReadHdr(mfp)) != kOkMfRC )
  371. goto errLabel;
  372. while( !cmFileEof(mfp->fh) && trkIdx < mfp->trkN )
  373. {
  374. unsigned chkId=0,chkN=0;
  375. // read the chunk id
  376. if((rc = _cmMidiFileRead32(mfp,&chkId)) != kOkMfRC )
  377. goto errLabel;
  378. // read the chunk size
  379. if((rc = _cmMidiFileRead32(mfp,&chkN)) != kOkMfRC )
  380. goto errLabel;
  381. // if this is not a trk chunk then skip it
  382. if( chkId != (unsigned)'MTrk')
  383. {
  384. //if( fseek( mfp->fp, chkN, SEEK_CUR) != 0 )
  385. if( cmFileSeek(mfp->fh,kCurFileFl,chkN) != kOkFileRC )
  386. {
  387. rc = _cmMidiFileError(&mfp->err,kSysFseekFailMfRC);
  388. goto errLabel;
  389. }
  390. }
  391. else
  392. {
  393. if((rc = _cmMidiFileReadTrack(mfp,trkIdx)) != kOkMfRC )
  394. goto errLabel;
  395. ++trkIdx;
  396. }
  397. }
  398. // get the total trk msg count
  399. mfp->msgN = 0;
  400. for(trkIdx=0; trkIdx<mfp->trkN; ++trkIdx)
  401. mfp->msgN += mfp->trkV[ trkIdx ].cnt;
  402. // allocate the trk msg index vector: msgV[]
  403. mfp->msgV = cmMemAllocZ(cmMidiTrackMsg_t*, mfp->msgN);
  404. // store a pointer to every trk msg in msgV[]
  405. // and convert tick to absolute tick
  406. unsigned i = 0;
  407. for(trkIdx=0; trkIdx<mfp->trkN; ++trkIdx)
  408. {
  409. unsigned tick = 0;
  410. cmMidiTrackMsg_t* tmp = mfp->trkV[ trkIdx ].base;
  411. while( tmp != NULL )
  412. {
  413. assert( i < mfp->msgN);
  414. tick += tmp->dtick; // convert delta-ticks to absolute ticks
  415. tmp->dtick = tick;
  416. mfp->msgV[i] = tmp;
  417. tmp = tmp->link;
  418. ++i;
  419. }
  420. }
  421. // sort msgV[] in ascending order on dtick
  422. qsort( mfp->msgV, mfp->msgN, sizeof(cmMidiTrackMsg_t*), _cmMidiFileSortFunc );
  423. //for(i=0; i<25; ++i)
  424. // printf("%i 0x%x 0x%x\n",mfp->msgV[i]->tick,mfp->msgV[i]->status,mfp->msgV[i]->metaId);
  425. mfp->fn = _cmMidiFileMalloc(mfp,strlen(fn)+1);
  426. assert( mfp->fn != NULL );
  427. strcpy(mfp->fn,fn);
  428. //
  429. // calculate the total duration of the MIDI file and convert absolute ticks back to delta ticks
  430. //
  431. unsigned mi;
  432. unsigned prvTick = 0;
  433. for(mi=0; mi<mfp->msgN; ++mi)
  434. {
  435. cmMidiTrackMsg_t* mp = mfp->msgV[mi];
  436. // convert absolute ticks back to delta ticks
  437. unsigned dtick = mp->dtick - prvTick;
  438. prvTick = mp->dtick;
  439. mp->dtick = dtick;
  440. }
  441. hPtr->h = mfp;
  442. errLabel:
  443. if( cmFileClose(&mfp->fh) != kOkFileRC )
  444. rc = _cmMidiFileError(&mfp->err,kCloseFailFileRC);
  445. if( rc != kOkMfRC )
  446. _cmMidiFileClose(mfp);
  447. return rc;
  448. }
  449. cmMfRC_t cmMidiFileClose( cmMidiFileH_t* h )
  450. {
  451. cmMfRC_t rc;
  452. if( cmMidiFileIsNull(*h) )
  453. return kOkMfRC;
  454. if((rc = _cmMidiFileClose(_cmMidiFileHandleToPtr(*h))) == kOkMfRC )
  455. return rc;
  456. h->h = NULL;
  457. return rc;
  458. }
  459. bool cmMidiFileIsValid( cmMidiFileH_t h )
  460. { return !cmMidiFileIsNull(h); }
  461. unsigned cmMidiFileTrackCount( cmMidiFileH_t h )
  462. {
  463. _cmMidiFile_t* mfp;
  464. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  465. return cmInvalidCnt;
  466. return mfp->trkN;
  467. }
  468. unsigned cmMidiFileType( cmMidiFileH_t h )
  469. {
  470. _cmMidiFile_t* mfp;
  471. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  472. return cmInvalidId;
  473. return mfp->fmtId;
  474. }
  475. const char* cmMidiFileName( cmMidiFileH_t h )
  476. {
  477. _cmMidiFile_t* mfp;
  478. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  479. return NULL;
  480. return mfp->fn;
  481. }
  482. unsigned cmMidiFileTicksPerQN( cmMidiFileH_t h )
  483. {
  484. _cmMidiFile_t* mfp;
  485. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  486. return cmInvalidCnt;
  487. return mfp->ticksPerQN;
  488. }
  489. cmMidiByte_t cmMidiFileTicksPerSmpteFrame( cmMidiFileH_t h )
  490. {
  491. _cmMidiFile_t* mfp;
  492. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  493. return kInvalidMidiByte;
  494. if( mfp->ticksPerQN != 0 )
  495. return 0;
  496. return mfp->smpteTicksPerFrame;
  497. }
  498. cmMidiByte_t cmMidiFileSmpteFormatId( cmMidiFileH_t h )
  499. {
  500. _cmMidiFile_t* mfp;
  501. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  502. return kInvalidMidiByte;
  503. if( mfp->ticksPerQN != 0 )
  504. return 0;
  505. return mfp->smpteFmtId;
  506. }
  507. unsigned cmMidiFileTrackMsgCount( cmMidiFileH_t h, unsigned trackIdx )
  508. {
  509. _cmMidiFile_t* mfp;
  510. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  511. return cmInvalidCnt;
  512. return mfp->trkV[trackIdx].cnt;
  513. }
  514. const cmMidiTrackMsg_t* cmMidiFileTrackMsg( cmMidiFileH_t h, unsigned trackIdx )
  515. {
  516. _cmMidiFile_t* mfp;
  517. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  518. return NULL;
  519. return mfp->trkV[trackIdx].base;
  520. }
  521. unsigned cmMidiFileMsgCount( cmMidiFileH_t h )
  522. {
  523. _cmMidiFile_t* mfp;
  524. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  525. return cmInvalidCnt;
  526. return mfp->msgN;
  527. }
  528. const cmMidiTrackMsg_t** cmMidiFileMsgArray( cmMidiFileH_t h )
  529. {
  530. _cmMidiFile_t* mfp;
  531. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  532. return NULL;
  533. // this cast is needed to eliminate an apparently needless 'incompatible type' warning
  534. return (const cmMidiTrackMsg_t**)mfp->msgV;
  535. }
  536. unsigned cmMidiFileSeekUsecs( cmMidiFileH_t h, unsigned offsUSecs, unsigned* msgUsecsPtr, unsigned* microsPerTickPtr )
  537. {
  538. _cmMidiFile_t* p;
  539. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  540. return cmInvalidIdx;
  541. if( p->msgN == 0 )
  542. return cmInvalidIdx;
  543. unsigned mi;
  544. unsigned microsPerQN = 60000000/120;
  545. unsigned microsPerTick = microsPerQN / p->ticksPerQN;
  546. unsigned accUSecs = 0;
  547. for(mi=0; mi<p->msgN; ++mi)
  548. {
  549. const cmMidiTrackMsg_t* mp = p->msgV[mi];
  550. if( mp->status == kMetaStId && mp->metaId == kTempoMdId )
  551. microsPerTick = mp->u.iVal / p->ticksPerQN;
  552. accUSecs += mp->dtick * microsPerTick ;
  553. if( accUSecs >= offsUSecs )
  554. break;
  555. }
  556. if( mi == p->msgN )
  557. return cmInvalidIdx;
  558. if( msgUsecsPtr != NULL )
  559. *msgUsecsPtr = accUSecs - offsUSecs;
  560. if( microsPerTickPtr != NULL )
  561. *microsPerTickPtr = microsPerTick;
  562. return mi;
  563. }
  564. double cmMidiFileDurSecs( cmMidiFileH_t h )
  565. {
  566. _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
  567. unsigned mi;
  568. double durSecs = 0;
  569. unsigned microsPerQN = 60000000/120;
  570. unsigned microsPerTick = microsPerQN / mfp->ticksPerQN;
  571. for(mi=0; mi<mfp->msgN; ++mi)
  572. {
  573. cmMidiTrackMsg_t* mp = mfp->msgV[mi];
  574. if( mp->status == kMetaStId && mp->metaId == kTempoMdId )
  575. microsPerTick = mp->u.iVal / mfp->ticksPerQN;
  576. // update the accumulated seconds
  577. durSecs += (mp->dtick * microsPerTick) / 1000000.0;
  578. }
  579. return durSecs;
  580. }
  581. void cmMidiFileTickToMicros( cmMidiFileH_t h )
  582. {
  583. _cmMidiFile_t* p;
  584. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  585. return;
  586. if( p->msgN == 0 )
  587. return;
  588. unsigned mi;
  589. unsigned microsPerQN = 60000000/120; // default tempo
  590. unsigned microsPerTick = microsPerQN / p->ticksPerQN;
  591. for(mi=0; mi<p->msgN; ++mi)
  592. {
  593. cmMidiTrackMsg_t* mp = p->msgV[mi];
  594. if( mp->status == kMetaStId && mp->metaId == kTempoMdId )
  595. microsPerTick = mp->u.iVal / p->ticksPerQN;
  596. mp->dtick *= microsPerTick;
  597. }
  598. }
  599. void cmMidiFileTickToSamples( cmMidiFileH_t h, double srate, bool absFl )
  600. {
  601. _cmMidiFile_t* p;
  602. unsigned mi;
  603. //bool fl = true;
  604. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  605. return;
  606. cmMidiFileTickToMicros(h);
  607. unsigned absSmp = 0;
  608. for(mi=0; mi<p->msgN; ++mi)
  609. {
  610. cmMidiTrackMsg_t* mp = p->msgV[mi];
  611. unsigned delta = floor((mp->dtick*srate)/1000000.0);
  612. absSmp += delta;
  613. mp->dtick = absFl ? absSmp : delta;
  614. }
  615. }
  616. typedef struct _cmMidiVoice_str
  617. {
  618. const cmMidiTrackMsg_t* mp;
  619. unsigned durTicks;
  620. bool sustainFl;
  621. struct _cmMidiVoice_str* link;
  622. } _cmMidiVoice_t;
  623. void _cmMidFileCalcNoteDurationReleaseNote( _cmMidiVoice_t** listPtrPtr, _cmMidiVoice_t* pp, _cmMidiVoice_t* vp )
  624. {
  625. assert( (pp==NULL && vp==*listPtrPtr) || pp->link==vp);
  626. // store the duration of the note into the track msg
  627. // assoc'd with the note-on msg
  628. cmMidiChMsg_t* cmp = (cmMidiChMsg_t*)vp->mp->u.chMsgPtr; // cast away const
  629. cmp->durTicks = vp->durTicks;
  630. _cmMidiVoice_t* np = vp->link;
  631. // release the voice msg
  632. cmMemFree(vp);
  633. // unlink the active voice msg
  634. if( pp == NULL )
  635. *listPtrPtr = np;
  636. else
  637. pp->link = np;
  638. }
  639. void cmMidiFileCalcNoteDurations( cmMidiFileH_t h )
  640. {
  641. _cmMidiFile_t* p;
  642. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  643. return;
  644. if( p->msgN == 0 )
  645. return;
  646. unsigned mi = cmInvalidId;
  647. _cmMidiVoice_t* list = NULL; // list of active voices
  648. _cmMidiVoice_t* vp = NULL;
  649. bool sustainFlagV[ kMidiChCnt ];
  650. // clear the sustain pedal flag
  651. for(mi=0; mi<kMidiChCnt; ++mi)
  652. sustainFlagV[mi]=false;
  653. for(mi=0; mi<p->msgN; ++mi)
  654. {
  655. cmMidiTrackMsg_t* mp = p->msgV[mi];
  656. // update the duration of the sounding notes
  657. for(vp = list; vp!=NULL; vp=vp->link)
  658. vp->durTicks += mp->dtick;
  659. //
  660. // If this is sustain pedal msg
  661. //
  662. if( mp->status==kCtlMdId && mp->u.chMsgPtr->d0 == kSustainCtlMdId )
  663. {
  664. unsigned chIdx = mp->u.chMsgPtr->ch;
  665. assert( chIdx < kMidiChCnt );
  666. // set the state of the sustain pedal flags
  667. sustainFlagV[chIdx] = mp->u.chMsgPtr->d1 >= 64;
  668. // if the pedal went up ...
  669. if( sustainFlagV[chIdx] == false )
  670. {
  671. // ... then release sustaining notes
  672. _cmMidiVoice_t* pp = NULL;
  673. for(vp=list; vp != NULL; )
  674. {
  675. _cmMidiVoice_t* np = vp->link;
  676. if( vp->sustainFl && (vp->mp->u.chMsgPtr->ch == chIdx) )
  677. _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
  678. else
  679. pp = vp;
  680. vp = np;
  681. }
  682. }
  683. }
  684. //
  685. // if this is a note-on msg
  686. //
  687. if( mp->status==kNoteOnMdId && mp->u.chMsgPtr->d1>0 )
  688. {
  689. vp = cmMemAllocZ(_cmMidiVoice_t,1);
  690. vp->mp = mp;
  691. vp->sustainFl = false;
  692. vp->link = list;
  693. list = vp;
  694. }
  695. else
  696. //
  697. // if this is a note-off msg
  698. //
  699. if( (mp->status==kNoteOnMdId && mp->u.chMsgPtr->d1==0) || (mp->status==kNoteOffMdId) )
  700. {
  701. _cmMidiVoice_t* pp = NULL;
  702. unsigned chIdx = mp->u.chMsgPtr->ch;
  703. assert( chIdx < kMidiChCnt );
  704. // for each active voice
  705. for(vp=list; vp!=NULL; vp=vp->link )
  706. {
  707. // if this active voice ch/pitch matches the note-off msg ch pitch
  708. if( (vp->mp->u.chMsgPtr->d0==mp->u.chMsgPtr->d0) && (vp->mp->u.chMsgPtr->ch==chIdx) )
  709. {
  710. // if the sustain pedal is down for this channel - then defer turning the note off
  711. if( sustainFlagV[chIdx] )
  712. vp->sustainFl = true;
  713. else
  714. _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
  715. break;
  716. }
  717. pp = vp;
  718. } // end for
  719. } // end if
  720. } // end-for
  721. // check for hung notes
  722. _cmMidiVoice_t* np = NULL;
  723. vp = list;
  724. while( vp != NULL )
  725. {
  726. np = vp->link;
  727. cmErrMsg(&p->err,kMissingNoteOffMfRC,"Missing note-off for note-on:%s",cmMidiToSciPitch(vp->mp->u.chMsgPtr->d0,NULL,0));
  728. cmMemFree(vp);
  729. vp = np;
  730. }
  731. }
  732. void cmMidiFileSetDelay( cmMidiFileH_t h, unsigned ticks )
  733. {
  734. _cmMidiFile_t* p;
  735. unsigned mi;
  736. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  737. return;
  738. if( p->msgN == 0 )
  739. return;
  740. for(mi=0; mi<p->msgN; ++mi)
  741. {
  742. cmMidiTrackMsg_t* mp = p->msgV[mi];
  743. // locate the first msg which has a non-zero delta tick
  744. if( mp->dtick > 0 )
  745. {
  746. mp->dtick = ticks;
  747. break;
  748. }
  749. }
  750. }
  751. unsigned cmMidiFilePackTrackMsgBufByteCount( const cmMidiTrackMsg_t* m )
  752. { return sizeof(cmMidiTrackMsg_t) + m->byteCnt; }
  753. cmMidiTrackMsg_t* cmMidiFilePackTrackMsg( const cmMidiTrackMsg_t* m, void* buf, unsigned bufByteCnt )
  754. {
  755. unsigned n = sizeof(cmMidiTrackMsg_t) + m->byteCnt;
  756. if( n < bufByteCnt )
  757. {
  758. assert(0);
  759. return NULL;
  760. }
  761. // copy the cmMidiTrackMsg_t into the buffer
  762. memcpy(buf, m, sizeof(cmMidiTrackMsg_t));
  763. if( m->byteCnt > 0 )
  764. {
  765. // copy any linked data into the buffer
  766. memcpy(buf + sizeof(cmMidiTrackMsg_t), m->u.voidPtr, m->byteCnt );
  767. // fixup the linked data ptr
  768. cmMidiTrackMsg_t* mp = (cmMidiTrackMsg_t*)buf;
  769. mp->u.voidPtr = buf + sizeof(cmMidiTrackMsg_t);
  770. }
  771. return (cmMidiTrackMsg_t*)buf;
  772. }
  773. void cmMidiFilePrint( cmMidiFileH_t h, unsigned trkIdx, cmRpt_t* rpt )
  774. {
  775. const _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
  776. if( mfp->fn != NULL )
  777. cmRptPrintf(rpt,"%s ",mfp->fn);
  778. cmRptPrintf(rpt,"fmt:%i ticksPerQN:%i tracks:%i\n",mfp->fmtId,mfp->ticksPerQN,mfp->trkN);
  779. int i = trkIdx == cmInvalidIdx ? 0 : trkIdx;
  780. int n = trkIdx == cmInvalidIdx ? mfp->trkN : trkIdx+1;
  781. for(; i<n; ++i)
  782. {
  783. cmRptPrintf(rpt,"Track:%i\n",i);
  784. cmMidiTrackMsg_t* tmp = mfp->trkV[i].base;
  785. while( tmp != NULL )
  786. {
  787. cmRptPrintf(rpt,"%5i ", tmp->dtick );
  788. if( tmp->status == kMetaStId )
  789. cmRptPrintf(rpt,"%s ", cmMidiMetaStatusToLabel(tmp->metaId));
  790. else
  791. {
  792. cmRptPrintf(rpt,"%4s %3i %3i %3i", cmMidiStatusToLabel(tmp->status),tmp->u.chMsgPtr->ch,tmp->u.chMsgPtr->d0,tmp->u.chMsgPtr->d1);
  793. }
  794. cmRptPrintf(rpt,"\n");
  795. tmp = tmp->link;
  796. }
  797. }
  798. }
  799. bool cmMidiFileIsNull( cmMidiFileH_t h )
  800. { return (_cmMidiFile_t*)h.h == NULL; }
  801. void cmMidiFileTestPrint( void* printDataPtr, const char* fmt, va_list vl )
  802. { vprintf(fmt,vl); }
  803. void cmMidiFileTest( const char* fn, cmCtx_t* ctx )
  804. {
  805. cmMfRC_t rc;
  806. cmMidiFileH_t h;
  807. if((rc = cmMidiFileOpen(fn,&h,ctx)) != kOkMfRC )
  808. {
  809. printf("Error:%i Unable to open the cmMidi file: %s\n",rc,fn);
  810. return;
  811. }
  812. cmMidiFilePrint(h,cmMidiFileTrackCount(h)-1,&ctx->rpt);
  813. printf("Tracks:%i\n",cmMidiFileTrackCount(h));
  814. cmMidiFileClose(&h);
  815. }