libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmMidiFile.c 26KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. mp->dtick = absFl ? absSmp : delta;
  613. 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. }