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

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