libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmMidiFile.c 46KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  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 "cmTime.h"
  11. #include "cmMidi.h"
  12. #include "cmMidiFile.h"
  13. #ifdef cmBIG_ENDIAN
  14. #define mfSwap16(v) (v)
  15. #define mfSwap32(v) (v)
  16. #else
  17. #define mfSwap16(v) cmSwap16(v)
  18. #define mfSwap32(v) cmSwap32(v)
  19. #endif
  20. typedef struct
  21. {
  22. unsigned cnt; // count of track records
  23. cmMidiTrackMsg_t* base; // pointer to first track recd
  24. cmMidiTrackMsg_t* last; // pointer to last track recd
  25. } _cmMidiTrack_t;
  26. typedef struct
  27. {
  28. cmErr_t err; // this objects error object
  29. cmLHeapH_t lhH; // linked heap used for all dynamically alloc'd data space
  30. cmFileH_t fh; // cmFile handle (only used in fmMidiFileOpen() and cmMidiFileWrite())
  31. unsigned short fmtId; // midi file type id: 0,1,2
  32. unsigned short ticksPerQN; // ticks per quarter note or 0 if smpteFmtId is valid
  33. cmMidiByte_t smpteFmtId; // smpte format or 0 if ticksPerQN is valid
  34. cmMidiByte_t smpteTicksPerFrame; // smpte ticks per frame or 0 if ticksPerQN is valid
  35. unsigned short trkN; // track count
  36. _cmMidiTrack_t* trkV; // track vector
  37. char* fn; // file name or NULL if this object did not originate from a file
  38. unsigned msgN; // count of msg's in msgV[]
  39. cmMidiTrackMsg_t** msgV; // sorted msg list
  40. unsigned nextUid; // next available msg uid
  41. } _cmMidiFile_t;
  42. cmMidiFileH_t cmMidiFileNullHandle = cmSTATIC_NULL_HANDLE;
  43. _cmMidiFile_t* _cmMidiFileHandleToPtr( cmMidiFileH_t h )
  44. {
  45. _cmMidiFile_t* p = (_cmMidiFile_t*)h.h;
  46. assert( p != NULL );
  47. return p;
  48. }
  49. void* _cmMidiFileMalloc( _cmMidiFile_t* mfp, unsigned byteN )
  50. { return cmLHeapAllocZ(mfp->lhH,byteN); }
  51. cmMfRC_t _cmMidiFileRead8( _cmMidiFile_t* mfp, cmMidiByte_t* p )
  52. {
  53. if( cmFileReadUChar(mfp->fh,p,1) != kOkFileRC )
  54. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI byte read failed.");
  55. return kOkMfRC;
  56. }
  57. cmMfRC_t _cmMidiFileRead16( _cmMidiFile_t* mfp, unsigned short* p )
  58. {
  59. if( cmFileReadUShort(mfp->fh,p,1) != kOkFileRC )
  60. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI short read failed.");
  61. *p = mfSwap16(*p);
  62. return kOkMfRC;
  63. }
  64. cmMfRC_t _cmMidiFileRead24( _cmMidiFile_t* mfp, unsigned* p )
  65. {
  66. *p = 0;
  67. int i = 0;
  68. for(; i<3; ++i)
  69. {
  70. unsigned char c;
  71. if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
  72. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI 24 bit integer read failed.");
  73. *p = (*p << 8) + c;
  74. }
  75. //*p =mfSwap32(*p);
  76. return kOkMfRC;
  77. }
  78. cmMfRC_t _cmMidiFileRead32( _cmMidiFile_t* mfp, unsigned* p )
  79. {
  80. if( cmFileReadUInt(mfp->fh,p,1) != kOkFileRC )
  81. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI integer read failed.");
  82. *p = mfSwap32(*p);
  83. return kOkMfRC;
  84. }
  85. cmMfRC_t _cmMidiFileReadText( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
  86. {
  87. if( byteN == 0 )
  88. return kOkMfRC;
  89. char* t = (char*)_cmMidiFileMalloc(mfp,byteN+1);
  90. t[byteN] = 0;
  91. if( cmFileReadChar(mfp->fh,t,byteN) != kOkFileRC )
  92. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read text failed.");
  93. tmp->u.text = t;
  94. tmp->byteCnt = byteN;
  95. return kOkMfRC;
  96. }
  97. cmMfRC_t _cmMidiFileReadRecd( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
  98. {
  99. char* t = (char*)_cmMidiFileMalloc(mfp,byteN);
  100. if( cmFileReadChar(mfp->fh,t,byteN) != kOkFileRC )
  101. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read record failed.");
  102. tmp->byteCnt = byteN;
  103. tmp->u.voidPtr = t;
  104. return kOkMfRC;
  105. }
  106. cmMfRC_t _cmMidiFileReadVarLen( _cmMidiFile_t* mfp, unsigned* p )
  107. {
  108. unsigned char c;
  109. if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
  110. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read variable length integer failed.");
  111. if( !(c & 0x80) )
  112. *p = c;
  113. else
  114. {
  115. *p = c & 0x7f;
  116. do
  117. {
  118. if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
  119. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read variable length integer failed.");
  120. *p = (*p << 7) + (c & 0x7f);
  121. }while( c & 0x80 );
  122. }
  123. return kOkMfRC;
  124. }
  125. cmMfRC_t _cmMidiFileAppendTrackMsg( _cmMidiFile_t* mfp, unsigned short trkIdx, unsigned dtick, cmMidiByte_t status, cmMidiTrackMsg_t** trkMsgPtrPtr )
  126. {
  127. cmMidiTrackMsg_t* tmp = (cmMidiTrackMsg_t*)_cmMidiFileMalloc(mfp, sizeof(cmMidiTrackMsg_t) );
  128. // link new record onto track record chain
  129. if( mfp->trkV[trkIdx].base == NULL )
  130. mfp->trkV[trkIdx].base = tmp;
  131. else
  132. mfp->trkV[trkIdx].last->link = tmp;
  133. mfp->trkV[trkIdx].last = tmp;
  134. mfp->trkV[trkIdx].cnt++;
  135. // set the generic track record fields
  136. tmp->dtick = dtick;
  137. tmp->status = status;
  138. tmp->metaId = kInvalidMetaMdId;
  139. tmp->trkIdx = trkIdx;
  140. tmp->byteCnt = 0;
  141. *trkMsgPtrPtr = tmp;
  142. return kOkMfRC;
  143. }
  144. cmMfRC_t _cmMidiFileReadSysEx( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
  145. {
  146. cmMfRC_t rc = kOkMfRC;
  147. cmMidiByte_t b = 0;
  148. if( byteN == cmInvalidCnt )
  149. {
  150. long offs;
  151. if( cmFileTell(mfp->fh,&offs) != kOkFileRC )
  152. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI File 'tell' failed.");
  153. byteN = 0;
  154. // get the length of the sys-ex msg
  155. while( !cmFileEof(mfp->fh) && (b != kSysComEoxMdId) )
  156. {
  157. if((rc = _cmMidiFileRead8(mfp,&b)) != kOkMfRC )
  158. return rc;
  159. ++byteN;
  160. }
  161. // verify that the EOX byte was found
  162. if( b != kSysComEoxMdId )
  163. return cmErrMsg(&mfp->err,kMissingEoxMfRC,"MIDI file missing 'end-of-sys-ex'.");
  164. // rewind to the beginning of the msg
  165. if( cmFileSeek(mfp->fh,kBeginFileFl,offs) != kOkFileRC )
  166. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file seek failed on sys-ex read.");
  167. }
  168. // allocate memory to hold the sys-ex msg
  169. cmMidiByte_t* mp = (cmMidiByte_t *)_cmMidiFileMalloc(mfp, byteN );
  170. // read the sys-ex msg from the file into msg memory
  171. if( cmFileReadUChar(mfp->fh,mp,byteN) != kOkFileRC )
  172. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI sys-ex read failed.");
  173. tmp->byteCnt = byteN;
  174. tmp->u.sysExPtr = mp;
  175. return rc;
  176. }
  177. cmMfRC_t _cmMidiFileReadChannelMsg( _cmMidiFile_t* mfp, cmMidiByte_t* rsPtr, cmMidiByte_t status, cmMidiTrackMsg_t* tmp )
  178. {
  179. cmMfRC_t rc = kOkMfRC;
  180. cmMidiChMsg_t* p = (cmMidiChMsg_t*)_cmMidiFileMalloc(mfp,sizeof(cmMidiChMsg_t));
  181. unsigned useRsFl = status <= 0x7f;
  182. cmMidiByte_t statusCh = useRsFl ? *rsPtr : status;
  183. if( useRsFl )
  184. p->d0 = status;
  185. else
  186. *rsPtr = status;
  187. tmp->byteCnt = sizeof(cmMidiChMsg_t);
  188. tmp->status = statusCh & 0xf0;
  189. p->ch = statusCh & 0x0f;
  190. p->durMicros = 0;
  191. unsigned byteN = cmMidiStatusToByteCount(tmp->status);
  192. if( byteN==kInvalidMidiByte || byteN > 2 )
  193. return cmErrMsg(&mfp->err,kInvalidStatusMfRC,"Invalid status:0x%x %i.",tmp->status,tmp->status);
  194. unsigned i;
  195. for(i=useRsFl; i<byteN; ++i)
  196. {
  197. cmMidiByte_t* b = i==0 ? &p->d0 : &p->d1;
  198. if((rc = _cmMidiFileRead8(mfp,b)) != kOkMfRC )
  199. return rc;
  200. }
  201. // convert note-on velocity=0 to note off
  202. if( tmp->status == kNoteOnMdId && p->d1==0 )
  203. tmp->status = kNoteOffMdId;
  204. tmp->u.chMsgPtr = p;
  205. return rc;
  206. }
  207. cmMfRC_t _cmMidiFileReadMetaMsg( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp )
  208. {
  209. cmMidiByte_t metaId;
  210. cmMfRC_t rc;
  211. unsigned byteN = 0;
  212. if((rc = _cmMidiFileRead8(mfp,&metaId)) != kOkMfRC )
  213. return rc;
  214. if((rc = _cmMidiFileReadVarLen(mfp,&byteN)) != kOkMfRC )
  215. return rc;
  216. //printf("mt: %i 0x%x n:%i\n",metaId,metaId,byteN);
  217. switch( metaId )
  218. {
  219. case kSeqNumbMdId: rc = _cmMidiFileRead16(mfp,&tmp->u.sVal); break;
  220. case kTextMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  221. case kCopyMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  222. case kTrkNameMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  223. case kInstrNameMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  224. case kLyricsMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  225. case kMarkerMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  226. case kCuePointMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  227. case kMidiChMdId: rc = _cmMidiFileRead8(mfp,&tmp->u.bVal); break;
  228. case kEndOfTrkMdId: break;
  229. case kTempoMdId: rc = _cmMidiFileRead24(mfp,&tmp->u.iVal); break;
  230. case kSmpteMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiSmpte_t)); break;
  231. case kTimeSigMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiTimeSig_t)); break;
  232. case kKeySigMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiKeySig_t)); break;
  233. case kSeqSpecMdId: rc = _cmMidiFileReadSysEx(mfp,tmp,byteN); break;
  234. default:
  235. cmFileSeek(mfp->fh,kCurFileFl,byteN);
  236. rc = cmErrMsg(&mfp->err,kUnknownMetaIdMfRC,"Unknown meta status:0x%x %i.",metaId,metaId);
  237. }
  238. tmp->metaId = metaId;
  239. return rc;
  240. }
  241. cmMfRC_t _cmMidiFileReadTrack( _cmMidiFile_t* mfp, unsigned short trkIdx )
  242. {
  243. cmMfRC_t rc = kOkMfRC;
  244. unsigned dticks = 0;
  245. cmMidiByte_t status;
  246. cmMidiByte_t runstatus = 0;
  247. bool contFl = true;
  248. while( contFl && (rc==kOkMfRC))
  249. {
  250. cmMidiTrackMsg_t* tmp = NULL;
  251. // read the tick count
  252. if((rc = _cmMidiFileReadVarLen(mfp,&dticks)) != kOkMfRC )
  253. return rc;
  254. // read the status byte
  255. if((rc = _cmMidiFileRead8(mfp,&status)) != kOkMfRC )
  256. return rc;
  257. //printf("st:%i 0x%x\n",status,status);
  258. // append a track msg
  259. if((rc = _cmMidiFileAppendTrackMsg( mfp, trkIdx, dticks, status, &tmp )) != kOkMfRC )
  260. return rc;
  261. // switch on status
  262. switch( status )
  263. {
  264. // handle sys-ex msg
  265. case kSysExMdId:
  266. rc = _cmMidiFileReadSysEx(mfp,tmp,cmInvalidCnt);
  267. break;
  268. // handle meta msg
  269. case kMetaStId:
  270. rc = _cmMidiFileReadMetaMsg(mfp,tmp);
  271. // ignore unknown meta messages
  272. if( rc == kUnknownMetaIdMfRC )
  273. rc = kOkMfRC;
  274. contFl = tmp->metaId != kEndOfTrkMdId;
  275. break;
  276. default:
  277. // handle channel msg
  278. rc = _cmMidiFileReadChannelMsg(mfp,&runstatus,status,tmp);
  279. }
  280. }
  281. return rc;
  282. }
  283. cmMfRC_t _cmMidiFileReadHdr( _cmMidiFile_t* mfp )
  284. {
  285. cmMfRC_t rc;
  286. unsigned fileId;
  287. unsigned chunkByteN;
  288. // read the file id
  289. if((rc = _cmMidiFileRead32(mfp,&fileId)) != kOkMfRC )
  290. return rc;
  291. // verify the file id
  292. if( fileId != 'MThd' )
  293. return cmErrMsg(&mfp->err,kNotAMidiFileMfRC,"");
  294. // read the file chunk byte count
  295. if((rc = _cmMidiFileRead32(mfp,&chunkByteN)) != kOkMfRC )
  296. return rc;
  297. // read the format id
  298. if((rc = _cmMidiFileRead16(mfp,&mfp->fmtId)) != kOkMfRC )
  299. return rc;
  300. // read the track count
  301. if((rc = _cmMidiFileRead16(mfp,&mfp->trkN)) != kOkMfRC )
  302. return rc;
  303. // read the ticks per quarter note
  304. if((rc = _cmMidiFileRead16(mfp,&mfp->ticksPerQN)) != kOkMfRC )
  305. return rc;
  306. // if the division field was given in smpte
  307. if( mfp->ticksPerQN & 0x8000 )
  308. {
  309. mfp->smpteFmtId = (mfp->ticksPerQN & 0x7f00) >> 8;
  310. mfp->smpteTicksPerFrame = (mfp->ticksPerQN & 0xFF);
  311. mfp->ticksPerQN = 0;
  312. }
  313. // allocate and zero the track array
  314. if( mfp->trkN )
  315. mfp->trkV = _cmMidiFileMalloc( mfp, sizeof(_cmMidiTrack_t)*mfp->trkN);
  316. return rc;
  317. }
  318. int _cmMidiFileSortFunc( const void *p0, const void* p1 )
  319. {
  320. if( (*(cmMidiTrackMsg_t**)p0)->atick == (*(cmMidiTrackMsg_t**)p1)->atick )
  321. return 0;
  322. return (*(cmMidiTrackMsg_t**)p0)->atick < (*(cmMidiTrackMsg_t**)p1)->atick ? -1 : 1;
  323. }
  324. // Set the absolute accumulated ticks (atick) value of each track message.
  325. // The absolute accumulated ticks gives a global time ordering for all
  326. // messages in the file.
  327. void _cmMidiFileSetAccumulateTicks( _cmMidiFile_t* p )
  328. {
  329. cmMidiTrackMsg_t* nextTrkMsg[ p->trkN ]; // next msg in each track
  330. unsigned long long atick = 0;
  331. unsigned i;
  332. bool fl = true;
  333. // iniitalize nextTrkTick[] and nextTrkMsg[].
  334. for(i=0; i<p->trkN; ++i)
  335. if((nextTrkMsg[i] = p->trkV[i].base) != NULL )
  336. nextTrkMsg[i]->atick = nextTrkMsg[i]->dtick;
  337. while(1)
  338. {
  339. unsigned k = cmInvalidIdx;
  340. // find the trk which has the next msg (min atick time)
  341. for(i=0; i<p->trkN; ++i)
  342. if( nextTrkMsg[i]!=NULL && (k==cmInvalidIdx || nextTrkMsg[i]->atick < nextTrkMsg[k]->atick) )
  343. k = i;
  344. // no next msg was found - we're done
  345. if( k == cmInvalidIdx )
  346. break;
  347. if( fl && nextTrkMsg[k]->dtick > 0 )
  348. {
  349. fl = false;
  350. nextTrkMsg[k]->dtick = 1;
  351. nextTrkMsg[k]->atick = 1;
  352. }
  353. // store the current atick
  354. atick = nextTrkMsg[k]->atick;
  355. // advance the selected track to it's next message
  356. nextTrkMsg[k] = nextTrkMsg[k]->link;
  357. // set the selected tracks next atick time
  358. if( nextTrkMsg[k] != NULL )
  359. nextTrkMsg[k]->atick = atick + nextTrkMsg[k]->dtick;
  360. }
  361. }
  362. void _cmMidiFileSetAbsoluteTime( _cmMidiFile_t* mfp )
  363. {
  364. double microsPerQN = 60000000/120; // default tempo;
  365. double microsPerTick = microsPerQN / mfp->ticksPerQN;
  366. unsigned long long amicro = 0;
  367. unsigned i;
  368. for(i=0; i<mfp->msgN; ++i)
  369. {
  370. cmMidiTrackMsg_t* mp = mfp->msgV[i];
  371. unsigned dtick = 0;
  372. if( i > 0 )
  373. {
  374. // atick must have already been set and sorted
  375. assert( mp->atick >= mfp->msgV[i-1]->atick );
  376. dtick = mp->atick - mfp->msgV[i-1]->atick;
  377. }
  378. amicro += microsPerTick * dtick;
  379. mp->amicro = amicro;
  380. // track tempo changes
  381. if( mp->status == kMetaStId && mp->metaId == kTempoMdId )
  382. microsPerTick = mp->u.iVal / mfp->ticksPerQN;
  383. }
  384. }
  385. cmMfRC_t _cmMidiFileClose( _cmMidiFile_t* mfp )
  386. {
  387. cmMfRC_t rc = kOkMfRC;
  388. if( mfp == NULL )
  389. return rc;
  390. cmMemPtrFree(&mfp->msgV);
  391. if( cmFileIsValid( mfp->fh ) )
  392. if( cmFileClose( &mfp->fh ) != kOkFileRC )
  393. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file close failed.");
  394. if( cmLHeapIsValid( mfp->lhH ) )
  395. cmLHeapDestroy(&mfp->lhH);
  396. cmMemPtrFree(&mfp);
  397. return rc;
  398. }
  399. cmMfRC_t cmMidiFileOpen( cmCtx_t* ctx, cmMidiFileH_t* hPtr, const char* fn )
  400. {
  401. cmMfRC_t rc = kOkMfRC;
  402. _cmMidiFile_t* mfp = NULL;
  403. unsigned short trkIdx = 0;
  404. cmErr_t err;
  405. unsigned i,j;
  406. if( cmMidiFileIsValid(*hPtr) )
  407. if((rc = _cmMidiFileClose(_cmMidiFileHandleToPtr(*hPtr))) != kOkMfRC )
  408. return rc;
  409. cmErrSetup(&err,&ctx->rpt,"MIDI File");
  410. // allocate the midi file object
  411. if(( mfp = cmMemAllocZ( _cmMidiFile_t, 1)) == NULL )
  412. return rc = cmErrMsg(&err,kMemAllocFailMfRC,"MIDI file memory allocation failed.");
  413. cmErrClone(&mfp->err,&err);
  414. // allocate the linked heap
  415. if( cmLHeapIsValid( mfp->lhH = cmLHeapCreate( 1024, ctx )) == false )
  416. {
  417. rc = cmErrMsg(&err,kMemAllocFailMfRC,"MIDI heap allocation failed.");
  418. goto errLabel;
  419. }
  420. // open the file
  421. if(cmFileOpen(&mfp->fh,fn,kReadFileFl | kBinaryFileFl,mfp->err.rpt) != kOkFileRC )
  422. {
  423. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file open failed.");
  424. goto errLabel;
  425. }
  426. // read header and setup track array
  427. if(( rc = _cmMidiFileReadHdr(mfp)) != kOkMfRC )
  428. goto errLabel;
  429. while( !cmFileEof(mfp->fh) && trkIdx < mfp->trkN )
  430. {
  431. unsigned chkId=0,chkN=0;
  432. // read the chunk id
  433. if((rc = _cmMidiFileRead32(mfp,&chkId)) != kOkMfRC )
  434. goto errLabel;
  435. // read the chunk size
  436. if((rc = _cmMidiFileRead32(mfp,&chkN)) != kOkMfRC )
  437. goto errLabel;
  438. // if this is not a trk chunk then skip it
  439. if( chkId != (unsigned)'MTrk')
  440. {
  441. //if( fseek( mfp->fp, chkN, SEEK_CUR) != 0 )
  442. if( cmFileSeek(mfp->fh,kCurFileFl,chkN) != kOkFileRC )
  443. {
  444. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file seek failed.");
  445. goto errLabel;
  446. }
  447. }
  448. else
  449. {
  450. if((rc = _cmMidiFileReadTrack(mfp,trkIdx)) != kOkMfRC )
  451. goto errLabel;
  452. ++trkIdx;
  453. }
  454. }
  455. // get the total trk msg count
  456. mfp->msgN = 0;
  457. for(trkIdx=0; trkIdx<mfp->trkN; ++trkIdx)
  458. mfp->msgN += mfp->trkV[ trkIdx ].cnt;
  459. // allocate the trk msg index vector: msgV[]
  460. mfp->msgV = cmMemAllocZ(cmMidiTrackMsg_t*, mfp->msgN);
  461. mfp->nextUid = 0;
  462. // store a pointer to every trk msg in msgV[] and set 'uid'
  463. for(i=0,j=0; i<mfp->trkN; ++i)
  464. {
  465. cmMidiTrackMsg_t* m = mfp->trkV[i].base;
  466. for(; m!=NULL; m=m->link)
  467. {
  468. assert( j < mfp->msgN );
  469. mfp->msgV[j++] = m;
  470. m->uid = mfp->nextUid++;
  471. }
  472. }
  473. mfp->fn = _cmMidiFileMalloc(mfp,strlen(fn)+1);
  474. assert( mfp->fn != NULL );
  475. strcpy(mfp->fn,fn);
  476. // set the atick value in each msg
  477. _cmMidiFileSetAccumulateTicks(mfp);
  478. // sort msgV[] in ascending order on atick
  479. qsort( mfp->msgV, mfp->msgN, sizeof(cmMidiTrackMsg_t*), _cmMidiFileSortFunc );
  480. // set the amicro value in each msg
  481. _cmMidiFileSetAbsoluteTime(mfp);
  482. hPtr->h = mfp;
  483. errLabel:
  484. if( cmFileClose(&mfp->fh) != kOkFileRC )
  485. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file close failed.");
  486. if( rc != kOkMfRC )
  487. _cmMidiFileClose(mfp);
  488. return rc;
  489. }
  490. cmMfRC_t cmMidiFileClose( cmMidiFileH_t* h )
  491. {
  492. cmMfRC_t rc;
  493. if( cmMidiFileIsNull(*h) )
  494. return kOkMfRC;
  495. if((rc = _cmMidiFileClose(_cmMidiFileHandleToPtr(*h))) == kOkMfRC )
  496. return rc;
  497. h->h = NULL;
  498. return rc;
  499. }
  500. cmMfRC_t _cmMidiFileWrite8( _cmMidiFile_t* mfp, unsigned char v )
  501. {
  502. cmMfRC_t rc = kOkMfRC;
  503. if( cmFileWriteUChar(mfp->fh,&v,1) != kOkFileRC )
  504. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file byte write failed.");
  505. return rc;
  506. }
  507. cmMfRC_t _cmMidiFileWrite16( _cmMidiFile_t* mfp, unsigned short v )
  508. {
  509. cmMfRC_t rc = kOkMfRC;
  510. v = mfSwap16(v);
  511. if( cmFileWriteUShort(mfp->fh,&v,1) != kOkFileRC )
  512. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file short integer write failed.");
  513. return rc;
  514. }
  515. cmMfRC_t _cmMidiFileWrite24( _cmMidiFile_t* mfp, unsigned v )
  516. {
  517. cmMfRC_t rc = kOkMfRC;
  518. unsigned mask = 0xff0000;
  519. int i;
  520. for(i=2; i>=0; --i)
  521. {
  522. unsigned char c = (v & mask) >> (i*8);
  523. mask >>= 8;
  524. if( cmFileWriteUChar(mfp->fh,&c,1) != kOkFileRC )
  525. {
  526. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file 24 bit integer write failed.");
  527. goto errLabel;
  528. }
  529. }
  530. errLabel:
  531. return rc;
  532. }
  533. cmMfRC_t _cmMidiFileWrite32( _cmMidiFile_t* mfp, unsigned v )
  534. {
  535. cmMfRC_t rc = kOkMfRC;
  536. v = mfSwap32(v);
  537. if( cmFileWriteUInt(mfp->fh,&v,1) != kOkFileRC )
  538. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file integer write failed.");
  539. return rc;
  540. }
  541. cmMfRC_t _cmMidiFileWriteRecd( _cmMidiFile_t* mfp, const void* v, unsigned byteCnt )
  542. {
  543. cmMfRC_t rc = kOkMfRC;
  544. if( cmFileWriteChar(mfp->fh,v,byteCnt) != kOkFileRC )
  545. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file write record failed.");
  546. return rc;
  547. }
  548. cmMfRC_t _cmMidiFileWriteVarLen( _cmMidiFile_t* mfp, unsigned v )
  549. {
  550. cmMfRC_t rc = kOkMfRC;
  551. unsigned buf = v & 0x7f;
  552. while((v >>= 7) > 0 )
  553. {
  554. buf <<= 8;
  555. buf |= 0x80;
  556. buf += (v & 0x7f);
  557. }
  558. while(1)
  559. {
  560. unsigned char c = (unsigned char)(buf & 0xff);
  561. if( cmFileWriteUChar(mfp->fh,&c,1) != kOkFileRC )
  562. {
  563. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file variable length integer write failed.");
  564. goto errLabel;
  565. }
  566. if( buf & 0x80 )
  567. buf >>= 8;
  568. else
  569. break;
  570. }
  571. errLabel:
  572. return rc;
  573. }
  574. cmMfRC_t _cmMidiFileWriteHdr( _cmMidiFile_t* mfp )
  575. {
  576. cmMfRC_t rc;
  577. unsigned fileId = 'MThd';
  578. unsigned chunkByteN = 6;
  579. // write the file id ('MThd')
  580. if((rc = _cmMidiFileWrite32(mfp,fileId)) != kOkMfRC )
  581. return rc;
  582. // write the file chunk byte count (always 6)
  583. if((rc = _cmMidiFileWrite32(mfp,chunkByteN)) != kOkMfRC )
  584. return rc;
  585. // write the MIDI file format id (0,1,2)
  586. if((rc = _cmMidiFileWrite16(mfp,mfp->fmtId)) != kOkMfRC )
  587. return rc;
  588. // write the track count
  589. if((rc = _cmMidiFileWrite16(mfp,mfp->trkN)) != kOkMfRC )
  590. return rc;
  591. unsigned short v = 0;
  592. // if the ticks per quarter note field is valid ...
  593. if( mfp->ticksPerQN )
  594. v = mfp->ticksPerQN;
  595. else
  596. {
  597. // ... otherwise the division field was given in smpte
  598. v = mfp->smpteFmtId << 8;
  599. v += mfp->smpteTicksPerFrame;
  600. }
  601. if((rc = _cmMidiFileWrite16(mfp,v)) != kOkMfRC )
  602. return rc;
  603. return rc;
  604. }
  605. cmMfRC_t _cmMidiFileWriteSysEx( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp )
  606. {
  607. cmMfRC_t rc = kOkMfRC;
  608. if((rc = _cmMidiFileWrite8(mfp,kSysExMdId)) != kOkMfRC )
  609. goto errLabel;
  610. if( cmFileWriteUChar(mfp->fh,tmp->u.sysExPtr,tmp->byteCnt) != kOkFileRC )
  611. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"Sys-ex msg write failed.");
  612. errLabel:
  613. return rc;
  614. }
  615. cmMfRC_t _cmMidiFileWriteChannelMsg( _cmMidiFile_t* mfp, const cmMidiTrackMsg_t* tmp, cmMidiByte_t* runStatus )
  616. {
  617. cmMfRC_t rc = kOkMfRC;
  618. unsigned byteN = cmMidiStatusToByteCount(tmp->status);
  619. cmMidiByte_t status = tmp->status + tmp->u.chMsgPtr->ch;
  620. if( status != *runStatus )
  621. {
  622. *runStatus = status;
  623. if((rc = _cmMidiFileWrite8(mfp,status)) != kOkMfRC )
  624. goto errLabel;
  625. }
  626. if(byteN>=1)
  627. if((rc = _cmMidiFileWrite8(mfp,tmp->u.chMsgPtr->d0)) != kOkMfRC )
  628. goto errLabel;
  629. if(byteN>=2)
  630. if((rc = _cmMidiFileWrite8(mfp,tmp->u.chMsgPtr->d1)) != kOkMfRC )
  631. goto errLabel;
  632. errLabel:
  633. return rc;
  634. }
  635. cmMfRC_t _cmMidiFileWriteMetaMsg( _cmMidiFile_t* mfp, const cmMidiTrackMsg_t* tmp )
  636. {
  637. cmMfRC_t rc;
  638. if((rc = _cmMidiFileWrite8(mfp,kMetaStId)) != kOkMfRC )
  639. return rc;
  640. if((rc = _cmMidiFileWrite8(mfp,tmp->metaId)) != kOkMfRC )
  641. return rc;
  642. switch( tmp->metaId )
  643. {
  644. case kSeqNumbMdId:
  645. if((rc = _cmMidiFileWrite8(mfp,sizeof(tmp->u.sVal))) == kOkMfRC )
  646. rc = _cmMidiFileWrite16(mfp,tmp->u.sVal);
  647. break;
  648. case kTempoMdId:
  649. if((rc = _cmMidiFileWrite8(mfp,3)) == kOkMfRC )
  650. rc = _cmMidiFileWrite24(mfp,tmp->u.iVal);
  651. break;
  652. case kSmpteMdId:
  653. if((rc = _cmMidiFileWrite8(mfp,sizeof(cmMidiSmpte_t))) == kOkMfRC )
  654. rc = _cmMidiFileWriteRecd(mfp,tmp->u.smptePtr,sizeof(cmMidiSmpte_t));
  655. break;
  656. case kTimeSigMdId:
  657. if((rc = _cmMidiFileWrite8(mfp,sizeof(cmMidiTimeSig_t))) == kOkMfRC )
  658. rc = _cmMidiFileWriteRecd(mfp,tmp->u.timeSigPtr,sizeof(cmMidiTimeSig_t));
  659. break;
  660. case kKeySigMdId:
  661. if((rc = _cmMidiFileWrite8(mfp,sizeof(cmMidiKeySig_t))) == kOkMfRC )
  662. rc = _cmMidiFileWriteRecd(mfp,tmp->u.keySigPtr,sizeof(cmMidiKeySig_t));
  663. break;
  664. case kSeqSpecMdId:
  665. if((rc = _cmMidiFileWriteVarLen(mfp,sizeof(tmp->byteCnt))) == kOkMfRC )
  666. rc = _cmMidiFileWriteRecd(mfp,tmp->u.sysExPtr,tmp->byteCnt);
  667. break;
  668. case kMidiChMdId:
  669. if((rc = _cmMidiFileWrite8(mfp,sizeof(tmp->u.bVal))) == kOkMfRC )
  670. rc = _cmMidiFileWrite8(mfp,tmp->u.bVal);
  671. break;
  672. case kEndOfTrkMdId:
  673. rc = _cmMidiFileWrite8(mfp,0);
  674. break;
  675. case kTextMdId:
  676. case kCopyMdId:
  677. case kTrkNameMdId:
  678. case kInstrNameMdId:
  679. case kLyricsMdId:
  680. case kMarkerMdId:
  681. case kCuePointMdId:
  682. {
  683. unsigned n = tmp->u.text==NULL ? 0 : strlen(tmp->u.text);
  684. if((rc = _cmMidiFileWriteVarLen(mfp,n)) == kOkMfRC && n>0 )
  685. rc = _cmMidiFileWriteRecd(mfp,tmp->u.text,n);
  686. }
  687. break;
  688. default:
  689. {
  690. // ignore unknown meta messages
  691. }
  692. }
  693. return rc;
  694. }
  695. cmMfRC_t _cmMidiFileWriteTrack( _cmMidiFile_t* mfp, unsigned trkIdx )
  696. {
  697. cmMfRC_t rc = kOkMfRC;
  698. cmMidiTrackMsg_t* tmp = mfp->trkV[trkIdx].base;
  699. cmMidiByte_t runStatus = 0;
  700. for(; tmp!=NULL; tmp=tmp->link)
  701. {
  702. // write the msg tick count
  703. if((rc = _cmMidiFileWriteVarLen(mfp,tmp->dtick)) != kOkMfRC )
  704. return rc;
  705. // switch on status
  706. switch( tmp->status )
  707. {
  708. // handle sys-ex msg
  709. case kSysExMdId:
  710. rc = _cmMidiFileWriteSysEx(mfp,tmp);
  711. break;
  712. // handle meta msg
  713. case kMetaStId:
  714. rc = _cmMidiFileWriteMetaMsg(mfp,tmp);
  715. break;
  716. default:
  717. // handle channel msg
  718. rc = _cmMidiFileWriteChannelMsg(mfp,tmp,&runStatus);
  719. }
  720. }
  721. return rc;
  722. }
  723. cmMfRC_t cmMidiFileWrite( cmMidiFileH_t h, const char* fn )
  724. {
  725. cmMfRC_t rc = kOkMfRC;
  726. _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
  727. unsigned i;
  728. // create the output file
  729. if( cmFileOpen(&mfp->fh,fn,kWriteFileFl,mfp->err.rpt) != kOkFileRC )
  730. return cmErrMsg(&mfp->err,kFileFailMfRC,"The MIDI file '%s' could not be created.",cmStringNullGuard(fn));
  731. // write the file header
  732. if((rc = _cmMidiFileWriteHdr(mfp)) != kOkMfRC )
  733. {
  734. rc = cmErrMsg(&mfp->err,rc,"The file header write failed on the MIDI file '%s'.",cmStringNullGuard(fn));
  735. goto errLabel;
  736. }
  737. for(i=0; i < mfp->trkN; ++i )
  738. {
  739. unsigned chkId='MTrk';
  740. long offs0,offs1;
  741. // write the track chunk id ('MTrk')
  742. if((rc = _cmMidiFileWrite32(mfp,chkId)) != kOkMfRC )
  743. goto errLabel;
  744. cmFileTell(mfp->fh,&offs0);
  745. // write the track chunk size as zero
  746. if((rc = _cmMidiFileWrite32(mfp,0)) != kOkMfRC )
  747. goto errLabel;
  748. if((rc = _cmMidiFileWriteTrack(mfp,i)) != kOkMfRC )
  749. goto errLabel;
  750. cmFileTell(mfp->fh,&offs1);
  751. cmFileSeek(mfp->fh,kBeginFileFl,offs0);
  752. _cmMidiFileWrite32(mfp,offs1-offs0-4);
  753. cmFileSeek(mfp->fh,kBeginFileFl,offs1);
  754. }
  755. errLabel:
  756. cmFileClose(&mfp->fh);
  757. return rc;
  758. }
  759. bool cmMidiFileIsValid( cmMidiFileH_t h )
  760. { return !cmMidiFileIsNull(h); }
  761. unsigned cmMidiFileTrackCount( cmMidiFileH_t h )
  762. {
  763. _cmMidiFile_t* mfp;
  764. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  765. return cmInvalidCnt;
  766. return mfp->trkN;
  767. }
  768. unsigned cmMidiFileType( cmMidiFileH_t h )
  769. {
  770. _cmMidiFile_t* mfp;
  771. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  772. return cmInvalidId;
  773. return mfp->fmtId;
  774. }
  775. const char* cmMidiFileName( cmMidiFileH_t h )
  776. {
  777. _cmMidiFile_t* mfp;
  778. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  779. return NULL;
  780. return mfp->fn;
  781. }
  782. unsigned cmMidiFileTicksPerQN( cmMidiFileH_t h )
  783. {
  784. _cmMidiFile_t* mfp;
  785. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  786. return cmInvalidCnt;
  787. return mfp->ticksPerQN;
  788. }
  789. cmMidiByte_t cmMidiFileTicksPerSmpteFrame( cmMidiFileH_t h )
  790. {
  791. _cmMidiFile_t* mfp;
  792. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  793. return kInvalidMidiByte;
  794. if( mfp->ticksPerQN != 0 )
  795. return 0;
  796. return mfp->smpteTicksPerFrame;
  797. }
  798. cmMidiByte_t cmMidiFileSmpteFormatId( cmMidiFileH_t h )
  799. {
  800. _cmMidiFile_t* mfp;
  801. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  802. return kInvalidMidiByte;
  803. if( mfp->ticksPerQN != 0 )
  804. return 0;
  805. return mfp->smpteFmtId;
  806. }
  807. unsigned cmMidiFileTrackMsgCount( cmMidiFileH_t h, unsigned trackIdx )
  808. {
  809. _cmMidiFile_t* mfp;
  810. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  811. return cmInvalidCnt;
  812. return mfp->trkV[trackIdx].cnt;
  813. }
  814. const cmMidiTrackMsg_t* cmMidiFileTrackMsg( cmMidiFileH_t h, unsigned trackIdx )
  815. {
  816. _cmMidiFile_t* mfp;
  817. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  818. return NULL;
  819. return mfp->trkV[trackIdx].base;
  820. }
  821. unsigned cmMidiFileMsgCount( cmMidiFileH_t h )
  822. {
  823. _cmMidiFile_t* mfp;
  824. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  825. return cmInvalidCnt;
  826. return mfp->msgN;
  827. }
  828. const cmMidiTrackMsg_t** cmMidiFileMsgArray( cmMidiFileH_t h )
  829. {
  830. _cmMidiFile_t* mfp;
  831. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  832. return NULL;
  833. // this cast is needed to eliminate an apparently needless 'incompatible type' warning
  834. return (const cmMidiTrackMsg_t**)mfp->msgV;
  835. }
  836. unsigned cmMidiFileSeekUsecs( cmMidiFileH_t h, unsigned long long offsUSecs, unsigned* msgUsecsPtr, unsigned* microsPerTickPtr )
  837. {
  838. _cmMidiFile_t* p;
  839. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  840. return cmInvalidIdx;
  841. if( p->msgN == 0 )
  842. return cmInvalidIdx;
  843. unsigned mi;
  844. double microsPerQN = 60000000.0/120.0;
  845. double microsPerTick = microsPerQN / p->ticksPerQN;
  846. double accUSecs = 0;
  847. for(mi=0; mi<p->msgN; ++mi)
  848. {
  849. const cmMidiTrackMsg_t* mp = p->msgV[mi];
  850. if( mp->amicro >= offsUSecs )
  851. break;
  852. }
  853. if( mi == p->msgN )
  854. return cmInvalidIdx;
  855. if( msgUsecsPtr != NULL )
  856. *msgUsecsPtr = round(accUSecs - offsUSecs);
  857. if( microsPerTickPtr != NULL )
  858. *microsPerTickPtr = round(microsPerTick);
  859. return mi;
  860. }
  861. double cmMidiFileDurSecs( cmMidiFileH_t h )
  862. {
  863. _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
  864. if( mfp->msgN == 0 )
  865. return 0;
  866. return mfp->msgV[ mfp->msgN-1 ]->amicro / 1000000.0;
  867. }
  868. typedef struct _cmMidiVoice_str
  869. {
  870. const cmMidiTrackMsg_t* mp;
  871. unsigned durMicros;
  872. bool sustainFl;
  873. struct _cmMidiVoice_str* link;
  874. } _cmMidiVoice_t;
  875. void _cmMidFileCalcNoteDurationReleaseNote( _cmMidiVoice_t** listPtrPtr, _cmMidiVoice_t* pp, _cmMidiVoice_t* vp )
  876. {
  877. assert( (pp==NULL && vp==*listPtrPtr) || pp->link==vp);
  878. // store the duration of the note into the track msg
  879. // assoc'd with the note-on msg
  880. cmMidiChMsg_t* cmp = (cmMidiChMsg_t*)vp->mp->u.chMsgPtr; // cast away const
  881. cmp->durMicros = vp->durMicros;
  882. _cmMidiVoice_t* np = vp->link;
  883. // release the voice msg
  884. cmMemFree(vp);
  885. // unlink the active voice msg
  886. if( pp == NULL )
  887. *listPtrPtr = np;
  888. else
  889. pp->link = np;
  890. }
  891. void _cmMidiFileCalcNoteDurationsAllocVoice( _cmMidiVoice_t** listPtrPtr, cmMidiTrackMsg_t* mp, bool sustainFl )
  892. {
  893. _cmMidiVoice_t* vp = cmMemAllocZ(_cmMidiVoice_t,1);
  894. vp->mp = mp;
  895. vp->sustainFl = sustainFl;
  896. vp->link = *listPtrPtr;
  897. *listPtrPtr = vp;
  898. }
  899. void cmMidiFileCalcNoteDurations2( cmMidiFileH_t h )
  900. {
  901. _cmMidiFile_t* p;
  902. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  903. return;
  904. if( p->msgN == 0 )
  905. return;
  906. unsigned mi = cmInvalidId;
  907. _cmMidiVoice_t* list = NULL; // list of active voices
  908. _cmMidiVoice_t* vp = NULL;
  909. cmMidiTrackMsg_t* sustainPedalDownMsg = NULL;
  910. bool sustainFlagV[ kMidiChCnt ];
  911. // clear the sustain pedal flag
  912. for(mi=0; mi<kMidiChCnt; ++mi)
  913. sustainFlagV[mi]=false;
  914. for(mi=0; mi<p->msgN; ++mi)
  915. {
  916. cmMidiTrackMsg_t* mp = p->msgV[mi];
  917. unsigned d_amicro = 0;
  918. if( mi > 0 )
  919. {
  920. assert( mp->amicro >= p->msgV[mi-1]->amicro );
  921. d_amicro = mp->amicro - p->msgV[mi-1]->amicro;
  922. }
  923. // update the duration of the sounding notes
  924. for(vp = list; vp!=NULL; vp=vp->link)
  925. vp->durMicros += d_amicro;
  926. // update the sustain pedal duration
  927. if( sustainPedalDownMsg != NULL )
  928. ((cmMidiChMsg_t*)(sustainPedalDownMsg->u.chMsgPtr))->durMicros += d_amicro; // cast away const
  929. //
  930. // If this is sustain pedal msg
  931. //
  932. if( mp->status==kCtlMdId && mp->u.chMsgPtr->d0 == kSustainCtlMdId )
  933. {
  934. unsigned chIdx = mp->u.chMsgPtr->ch;
  935. assert( chIdx < kMidiChCnt );
  936. // set the state of the sustain pedal flags
  937. sustainFlagV[chIdx] = mp->u.chMsgPtr->d1 >= 64;
  938. // if the pedal went down ...
  939. if( sustainFlagV[chIdx] )
  940. {
  941. if( sustainPedalDownMsg != NULL )
  942. {
  943. // TODO: the correct way to handle this is to maintain multiple sustain pedals
  944. //cmErrMsg(&p->err,kSustainPedalMfRC,"Sustain pedal down with no intervening sustain pedal up.");
  945. }
  946. else
  947. {
  948. sustainPedalDownMsg = mp;
  949. ((cmMidiChMsg_t*)(sustainPedalDownMsg->u.chMsgPtr))->durMicros = 0; // cast away const
  950. }
  951. _cmMidiFileCalcNoteDurationsAllocVoice( &list, mp, true );
  952. }
  953. else // ... if the pedal went up
  954. {
  955. sustainPedalDownMsg = NULL;
  956. // ... then release sustaining notes
  957. _cmMidiVoice_t* pp = NULL;
  958. for(vp=list; vp != NULL; )
  959. {
  960. _cmMidiVoice_t* np = vp->link;
  961. if( vp->sustainFl && (vp->mp->u.chMsgPtr->ch == chIdx) )
  962. _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
  963. else
  964. pp = vp;
  965. vp = np;
  966. }
  967. }
  968. }
  969. //
  970. // if this is a note-on msg
  971. //
  972. if( mp->status==kNoteOnMdId && mp->u.chMsgPtr->d1>0 )
  973. {
  974. _cmMidiFileCalcNoteDurationsAllocVoice( &list, mp, false );
  975. }
  976. else
  977. //
  978. // if this is a note-off msg
  979. //
  980. if( (mp->status==kNoteOnMdId && mp->u.chMsgPtr->d1==0) || (mp->status==kNoteOffMdId) )
  981. {
  982. _cmMidiVoice_t* pp = NULL;
  983. unsigned chIdx = mp->u.chMsgPtr->ch;
  984. assert( chIdx < kMidiChCnt );
  985. // for each active voice
  986. for(vp=list; vp!=NULL; vp=vp->link )
  987. {
  988. // if this active voice ch/pitch matches the note-off msg ch pitch
  989. if( (vp->mp->u.chMsgPtr->d0==mp->u.chMsgPtr->d0) && (vp->mp->u.chMsgPtr->ch==chIdx) )
  990. {
  991. // if the sustain pedal is down for this channel - then defer turning the note off
  992. if( sustainFlagV[chIdx] )
  993. vp->sustainFl = true;
  994. else
  995. _cmMidFileCalcNoteDurationReleaseNote(&list,pp,vp);
  996. break;
  997. }
  998. pp = vp;
  999. } // end for
  1000. } // end if
  1001. } // end-for
  1002. // check for hung notes
  1003. _cmMidiVoice_t* np = NULL;
  1004. vp = list;
  1005. while( vp != NULL )
  1006. {
  1007. np = vp->link;
  1008. if( cmMidiIsNoteOn(vp->mp->status) == false )
  1009. cmErrMsg(&p->err,kMissingNoteOffMfRC,"Missing note-off for note-on:%s",cmMidiToSciPitch(vp->mp->u.chMsgPtr->d0,NULL,0));
  1010. cmMemFree(vp);
  1011. vp = np;
  1012. }
  1013. }
  1014. void _cmMidiFileSetDur( cmMidiTrackMsg_t* m0, cmMidiTrackMsg_t* m1 )
  1015. {
  1016. // calculate the duration of the sounding note
  1017. ((cmMidiChMsg_t*)m0->u.chMsgPtr)->durMicros = m1->amicro - m0->amicro;
  1018. }
  1019. bool _cmMidiFileCalcNoteDur( cmMidiTrackMsg_t* m0, cmMidiTrackMsg_t* m1, int noteGateFl, int sustainGateFl, bool sostGateFl )
  1020. {
  1021. // if the note is being kept sounding because the key is still depressed,
  1022. // the sustain pedal is down or it is being held by the sostenuto pedal ....
  1023. if( noteGateFl>0 || sustainGateFl>0 || sostGateFl )
  1024. return false; // ... do nothing
  1025. _cmMidiFileSetDur(m0,m1);
  1026. return true;
  1027. }
  1028. void cmMidiFileCalcNoteDurations( cmMidiFileH_t h )
  1029. {
  1030. _cmMidiFile_t* p;
  1031. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  1032. return;
  1033. if( p->msgN == 0 )
  1034. return;
  1035. unsigned mi = cmInvalidId;
  1036. cmMidiTrackMsg_t* noteM[ kMidiNoteCnt * kMidiChCnt ]; // ptr to note-on or NULL if the note is not sounding
  1037. cmMidiTrackMsg_t* sustV[ kMidiChCnt ];
  1038. cmMidiTrackMsg_t* sostV[ kMidiChCnt ];
  1039. int noteGateM[ kMidiNoteCnt * kMidiChCnt ]; // true if the associated note key is depressed
  1040. bool sostGateM[ kMidiNoteCnt * kMidiChCnt ]; // true if the associated note was active when the sost. pedal went down
  1041. int sustGateV[ kMidiChCnt]; // true if the associated sustain pedal is down
  1042. int sostGateV[ kMidiChCnt]; // true if the associated sostenuto pedal is down
  1043. unsigned i,j;
  1044. // initialize the state tracking variables
  1045. for(i=0; i<kMidiChCnt; ++i)
  1046. {
  1047. sustV[i] = NULL;
  1048. sustGateV[i] = 0;
  1049. sostV[i] = NULL;
  1050. sostGateV[i] = 0;
  1051. for(j=0; j<kMidiNoteCnt; ++j)
  1052. {
  1053. noteM[ i*kMidiNoteCnt + j ] = NULL;
  1054. noteGateM[ i*kMidiNoteCnt + j ] = 0;
  1055. sostGateM[ i*kMidiNoteCnt + j ] = false;
  1056. }
  1057. }
  1058. // for each midi event
  1059. for(mi=0; mi<p->msgN; ++mi)
  1060. {
  1061. cmMidiTrackMsg_t* m = p->msgV[mi];
  1062. // verify that time is also incrementing
  1063. assert( mi==0 || (mi>0 && m->amicro >= p->msgV[mi-1]->amicro) );
  1064. // ignore all non-channel messages
  1065. if( !cmMidiIsChStatus( m->status ) )
  1066. continue;
  1067. cmMidiByte_t ch = m->u.chMsgPtr->ch; // get the midi msg channel
  1068. cmMidiByte_t d0 = m->u.chMsgPtr->d0; // get the midi msg data value
  1069. // if this is a note-on msg
  1070. if( cmMidiFileIsNoteOn(m) )
  1071. {
  1072. unsigned k = ch*kMidiNoteCnt + d0;
  1073. // there should be no existing sounding note instance for this pitch
  1074. //if( noteGateM[k] == 0 && noteM[k] != NULL )
  1075. // cmErrWarnMsg(&p->err,kMissingNoteOffMfRC,"%i : Missing note-off instance for note on:%s",m->uid,cmMidiToSciPitch(d0,NULL,0));
  1076. if( noteM[k] != NULL )
  1077. noteGateM[k] += 1;
  1078. else
  1079. {
  1080. noteM[k] = m;
  1081. noteGateM[k] = 1;
  1082. }
  1083. }
  1084. else
  1085. // if this is a note-off msg
  1086. if( cmMidiFileIsNoteOff(m) )
  1087. {
  1088. unsigned k = ch*kMidiNoteCnt + d0;
  1089. cmMidiTrackMsg_t* m0 = noteM[k];
  1090. if( m0 == NULL )
  1091. cmErrWarnMsg(&p->err,kMissingNoteOffMfRC,"%i : Missing note-on instance for note-off:%s",m->uid,cmMidiToSciPitch(d0,NULL,0));
  1092. else
  1093. {
  1094. // a key was released - so it should not already be up
  1095. if( noteGateM[k]==0 )
  1096. cmErrWarnMsg(&p->err,kMissingNoteOffMfRC,"%i : Missing note-on for note-off:%s",m->uid,cmMidiToSciPitch(d0,NULL,0));
  1097. else
  1098. {
  1099. noteGateM[k] -= 1; // update the note gate state
  1100. // update the sounding note status
  1101. if( _cmMidiFileCalcNoteDur(m0, m, noteGateM[k], sustGateV[ch], sostGateM[k]) )
  1102. noteM[k] = NULL;
  1103. }
  1104. }
  1105. }
  1106. else
  1107. // This is a sustain-pedal down msg
  1108. if( cmMidiFileIsSustainPedalDown(m) )
  1109. {
  1110. // if the sustain channel is already down
  1111. //if( sustGateV[ch] )
  1112. // cmErrWarnMsg(&p->err,kSustainPedalMfRC,"%i : The sustain pedal went down twice with no intervening release.",m->uid);
  1113. sustGateV[ch] += 1;
  1114. if( sustV[ch] == NULL )
  1115. sustV[ch] = m;
  1116. }
  1117. else
  1118. // This is a sustain-pedal up msg
  1119. if( cmMidiFileIsSustainPedalUp(m) )
  1120. {
  1121. // if the sustain channel is already up
  1122. if( sustGateV[ch]==0 )
  1123. cmErrWarnMsg(&p->err,kSustainPedalMfRC,"%i : The sustain pedal release message was received with no previous pedal down.",m->uid);
  1124. if( sustGateV[ch] >= 1 )
  1125. {
  1126. sustGateV[ch] -= 1;
  1127. if( sustGateV[ch] == 0 )
  1128. {
  1129. unsigned k = ch*kMidiNoteCnt;
  1130. // for each sounding note on this channel
  1131. for(; k<ch*kMidiNoteCnt+kMidiNoteCnt; ++k)
  1132. if( noteM[k]!=NULL && _cmMidiFileCalcNoteDur(noteM[k], m, noteGateM[k], sustGateV[ch], sostGateM[k]) )
  1133. noteM[k] = NULL;
  1134. if( sustV[ch] != NULL )
  1135. {
  1136. _cmMidiFileSetDur(sustV[ch],m);
  1137. sustV[ch] = NULL;
  1138. }
  1139. }
  1140. }
  1141. }
  1142. else
  1143. // This is a sostenuto-pedal down msg
  1144. if( cmMidiFileIsSostenutoPedalDown(m) )
  1145. {
  1146. // if the sustain channel is already down
  1147. //if( sostGateV[ch] )
  1148. // cmErrWarnMsg(&p->err,kSostenutoPedalMfRC,"%i : The sostenuto pedal went down twice with no intervening release.",m->uid);
  1149. // record the notes that are active when the sostenuto pedal went down
  1150. unsigned k = ch * kMidiNoteCnt;
  1151. for(i=0; i<kMidiNoteCnt; ++i)
  1152. sostGateM[k+i] = noteGateM[k+i] > 0;
  1153. sostGateV[ch] += 1;
  1154. }
  1155. else
  1156. // This is a sostenuto-pedal up msg
  1157. if( cmMidiFileIsSostenutoPedalUp(m) )
  1158. {
  1159. // if the sustain channel is already up
  1160. if( sostGateV[ch]==0 )
  1161. cmErrWarnMsg(&p->err,kSostenutoPedalMfRC,"%i : The sostenuto pedal release message was received with no previous pedal down.",m->uid);
  1162. if( sostGateV[ch] >= 1 )
  1163. {
  1164. sostGateV[ch] -= 1;
  1165. if( sostGateV[ch] == 0 )
  1166. {
  1167. unsigned k = ch*kMidiNoteCnt;
  1168. // for each note on this channel
  1169. for(; k<ch*kMidiNoteCnt+kMidiNoteCnt; ++k)
  1170. {
  1171. sostGateM[k] = false;
  1172. if( noteM[k]!=NULL && _cmMidiFileCalcNoteDur(noteM[k], m, noteGateM[k], sustGateV[ch], sostGateM[k]) )
  1173. noteM[k] = NULL;
  1174. }
  1175. if( sostV[ch] != NULL )
  1176. {
  1177. _cmMidiFileSetDur(sostV[ch],m);
  1178. sostV[ch] = NULL;
  1179. }
  1180. }
  1181. }
  1182. }
  1183. } // for each midi file event
  1184. }
  1185. void cmMidiFileSetDelay( cmMidiFileH_t h, unsigned ticks )
  1186. {
  1187. _cmMidiFile_t* p;
  1188. unsigned mi;
  1189. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  1190. return;
  1191. if( p->msgN == 0 )
  1192. return;
  1193. for(mi=0; mi<p->msgN; ++mi)
  1194. {
  1195. cmMidiTrackMsg_t* mp = p->msgV[mi];
  1196. // locate the first msg which has a non-zero delta tick
  1197. if( mp->dtick > 0 )
  1198. {
  1199. mp->dtick = ticks;
  1200. break;
  1201. }
  1202. }
  1203. }
  1204. unsigned cmMidiFilePackTrackMsgBufByteCount( const cmMidiTrackMsg_t* m )
  1205. { return sizeof(cmMidiTrackMsg_t) + m->byteCnt; }
  1206. cmMidiTrackMsg_t* cmMidiFilePackTrackMsg( const cmMidiTrackMsg_t* m, void* buf, unsigned bufByteCnt )
  1207. {
  1208. unsigned n = sizeof(cmMidiTrackMsg_t) + m->byteCnt;
  1209. if( n < bufByteCnt )
  1210. {
  1211. assert(0);
  1212. return NULL;
  1213. }
  1214. // copy the cmMidiTrackMsg_t into the buffer
  1215. memcpy(buf, m, sizeof(cmMidiTrackMsg_t));
  1216. if( m->byteCnt > 0 )
  1217. {
  1218. // copy any linked data into the buffer
  1219. memcpy(buf + sizeof(cmMidiTrackMsg_t), m->u.voidPtr, m->byteCnt );
  1220. // fixup the linked data ptr
  1221. cmMidiTrackMsg_t* mp = (cmMidiTrackMsg_t*)buf;
  1222. mp->u.voidPtr = buf + sizeof(cmMidiTrackMsg_t);
  1223. }
  1224. return (cmMidiTrackMsg_t*)buf;
  1225. }
  1226. void _cmMidiFilePrintHdr( const _cmMidiFile_t* mfp, cmRpt_t* rpt )
  1227. {
  1228. if( mfp->fn != NULL )
  1229. cmRptPrintf(rpt,"%s ",mfp->fn);
  1230. cmRptPrintf(rpt,"fmt:%i ticksPerQN:%i tracks:%i\n",mfp->fmtId,mfp->ticksPerQN,mfp->trkN);
  1231. cmRptPrintf(rpt," UID dtick atick amicro type ch D0 D1\n");
  1232. cmRptPrintf(rpt,"----- ---------- ---------- ---------- : ---- --- --- ---\n");
  1233. }
  1234. void _cmMidiFilePrintMsg( cmRpt_t* rpt, const cmMidiTrackMsg_t* tmp )
  1235. {
  1236. cmRptPrintf(rpt,"%5i %10u %10llu %10llu : ",
  1237. tmp->uid,
  1238. tmp->dtick,
  1239. tmp->atick,
  1240. tmp->amicro );
  1241. if( tmp->status == kMetaStId )
  1242. {
  1243. cmRptPrintf(rpt,"%s ", cmMidiMetaStatusToLabel(tmp->metaId));
  1244. }
  1245. else
  1246. {
  1247. cmRptPrintf(rpt,"%4s %3i %3i %3i",
  1248. cmMidiStatusToLabel(tmp->status),
  1249. tmp->u.chMsgPtr->ch,
  1250. tmp->u.chMsgPtr->d0,
  1251. tmp->u.chMsgPtr->d1);
  1252. }
  1253. if( cmMidiIsChStatus(tmp->status) && cmMidiIsNoteOn(tmp->status) && (tmp->u.chMsgPtr->d1>0) )
  1254. cmRptPrintf(rpt," %4s ",cmMidiToSciPitch(tmp->u.chMsgPtr->d0,NULL,0));
  1255. cmRptPrintf(rpt,"\n");
  1256. }
  1257. void cmMidiFilePrintMsgs( cmMidiFileH_t h, cmRpt_t* rpt )
  1258. {
  1259. const _cmMidiFile_t* p = _cmMidiFileHandleToPtr(h);
  1260. unsigned mi;
  1261. _cmMidiFilePrintHdr(p,rpt);
  1262. for(mi=0; mi<p->msgN; ++mi)
  1263. {
  1264. cmMidiTrackMsg_t* mp = p->msgV[mi];
  1265. if( mp != NULL )
  1266. _cmMidiFilePrintMsg(rpt,mp);
  1267. }
  1268. }
  1269. void cmMidiFilePrintTracks( cmMidiFileH_t h, unsigned trkIdx, cmRpt_t* rpt )
  1270. {
  1271. const _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
  1272. _cmMidiFilePrintHdr(mfp,rpt);
  1273. int i = trkIdx == cmInvalidIdx ? 0 : trkIdx;
  1274. int n = trkIdx == cmInvalidIdx ? mfp->trkN : trkIdx+1;
  1275. for(; i<n; ++i)
  1276. {
  1277. cmRptPrintf(rpt,"Track:%i\n",i);
  1278. cmMidiTrackMsg_t* tmp = mfp->trkV[i].base;
  1279. while( tmp != NULL )
  1280. {
  1281. _cmMidiFilePrintMsg(rpt,tmp);
  1282. tmp = tmp->link;
  1283. }
  1284. }
  1285. }
  1286. bool cmMidiFileIsNull( cmMidiFileH_t h )
  1287. { return (_cmMidiFile_t*)h.h == NULL; }
  1288. void cmMidiFileTestPrint( void* printDataPtr, const char* fmt, va_list vl )
  1289. { vprintf(fmt,vl); }
  1290. cmMfRC_t cmMidiFileGenPlotFile( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* outFn )
  1291. {
  1292. cmMfRC_t rc = kOkMfRC;
  1293. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  1294. cmFileH_t fH = cmFileNullHandle;
  1295. unsigned i = 0;
  1296. const cmMidiTrackMsg_t** m = NULL;
  1297. unsigned mN = 0;
  1298. if((rc = cmMidiFileOpen(ctx, &mfH, midiFn )) != kOkMfRC )
  1299. return cmErrMsg(&ctx->err,rc,"The MIDI file object could not be opened from '%s'.",cmStringNullGuard(midiFn));
  1300. _cmMidiFile_t* p = _cmMidiFileHandleToPtr(mfH);
  1301. if( (m = cmMidiFileMsgArray(mfH)) == NULL || (mN = cmMidiFileMsgCount(mfH)) == 0 )
  1302. {
  1303. rc = cmErrMsg(&p->err,kFileFailMfRC,"The MIDI file object appears to be empty.");
  1304. goto errLabel;
  1305. }
  1306. cmMidiFileCalcNoteDurations( mfH );
  1307. if( cmFileOpen(&fH,outFn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  1308. return cmErrMsg(&p->err,kFileFailMfRC,"Unable to create the file '%s'.",cmStringNullGuard(outFn));
  1309. for(i=0; i<mN; ++i)
  1310. if( (m[i]!=NULL) && cmMidiIsChStatus(m[i]->status) && cmMidiIsNoteOn(m[i]->status) && (m[i]->u.chMsgPtr->d1>0) )
  1311. cmFilePrintf(fH,"n %f %f %i %s\n",m[i]->amicro/1000000.0,m[i]->u.chMsgPtr->durMicros/1000000.0,m[i]->uid,cmMidiToSciPitch(m[i]->u.chMsgPtr->d0,NULL,0));
  1312. errLabel:
  1313. cmMidiFileClose(&mfH);
  1314. cmFileClose(&fH);
  1315. return rc;
  1316. }
  1317. void cmMidiFilePrintControlNumbers( cmCtx_t* ctx, const char* fn )
  1318. {
  1319. cmMidiFileH_t h = cmMidiFileNullHandle;
  1320. cmMfRC_t rc;
  1321. if((rc = cmMidiFileOpen(ctx, &h, fn )) != kOkMfRC )
  1322. {
  1323. cmErrMsg(&ctx->err,rc,"MIDI file open failed on '%s'.",fn);
  1324. goto errLabel;
  1325. }
  1326. const cmMidiTrackMsg_t** mm;
  1327. unsigned n = cmMidiFileMsgCount(h);
  1328. if((mm = cmMidiFileMsgArray(h)) != NULL )
  1329. {
  1330. unsigned j;
  1331. for(j=0; j<n; ++j)
  1332. {
  1333. const cmMidiTrackMsg_t* m = mm[j];
  1334. if( m->status == kCtlMdId && m->u.chMsgPtr->d0==66 )
  1335. printf("%i %i\n",m->u.chMsgPtr->d0,m->u.chMsgPtr->d1);
  1336. }
  1337. }
  1338. errLabel:
  1339. cmMidiFileClose(&h);
  1340. }
  1341. void cmMidiFileTest( const char* fn, cmCtx_t* ctx )
  1342. {
  1343. cmMfRC_t rc;
  1344. cmMidiFileH_t h = cmMidiFileNullHandle;
  1345. if((rc = cmMidiFileOpen(ctx,&h,fn)) != kOkMfRC )
  1346. {
  1347. printf("Error:%i Unable to open the cmMidi file: %s\n",rc,fn);
  1348. return;
  1349. }
  1350. cmMidiFileCalcNoteDurations( h );
  1351. if( 1 )
  1352. {
  1353. //cmMidiFileTickToMicros( h );
  1354. //cmMidiFileTickToSamples(h,96000,false);
  1355. cmMidiFilePrintMsgs(h,&ctx->rpt);
  1356. }
  1357. if( 0 )
  1358. {
  1359. //cmMidiFilePrint(h,cmMidiFileTrackCount(h)-1,&ctx->rpt);
  1360. //cmMidiFilePrint(h,cmInvalidIdx,&ctx->rpt);
  1361. cmMidiFilePrintControlNumbers(ctx, fn );
  1362. }
  1363. if( 0 )
  1364. {
  1365. printf("Tracks:%i\n",cmMidiFileTrackCount(h));
  1366. unsigned i = 0;
  1367. for(i=0; i<cmMidiFileMsgCount(h); ++i)
  1368. {
  1369. cmMidiTrackMsg_t* tmp = (cmMidiTrackMsg_t*)cmMidiFileMsgArray(h)[i];
  1370. if( tmp->status==kMetaStId && tmp->metaId == kTempoMdId )
  1371. {
  1372. double bpm = 60000000.0/tmp->u.iVal;
  1373. printf("Tempo:%i %f\n",tmp->u.iVal,bpm);
  1374. tmp->u.iVal = floor( 60000000.0/69.0 );
  1375. break;
  1376. }
  1377. }
  1378. cmMidiFileWrite(h,"/home/kevin/temp/test0.mid");
  1379. }
  1380. cmMidiFileClose(&h);
  1381. }