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 59KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  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 "cmRptFile.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLinkedHeap.h"
  11. #include "cmTime.h"
  12. #include "cmText.h"
  13. #include "cmMidi.h"
  14. #include "cmMidiFile.h"
  15. #include "cmSvgWriter.h"
  16. #ifdef cmBIG_ENDIAN
  17. #define mfSwap16(v) (v)
  18. #define mfSwap32(v) (v)
  19. #else
  20. #define mfSwap16(v) cmSwap16(v)
  21. #define mfSwap32(v) cmSwap32(v)
  22. #endif
  23. typedef struct
  24. {
  25. unsigned cnt; // count of track records
  26. cmMidiTrackMsg_t* base; // pointer to first track recd
  27. cmMidiTrackMsg_t* last; // pointer to last track recd
  28. } _cmMidiTrack_t;
  29. typedef struct
  30. {
  31. cmErr_t err; // this objects error object
  32. cmLHeapH_t lhH; // linked heap used for all dynamically alloc'd data space
  33. cmFileH_t fh; // cmFile handle (only used in fmMidiFileOpen() and cmMidiFileWrite())
  34. unsigned short fmtId; // midi file type id: 0,1,2
  35. unsigned short ticksPerQN; // ticks per quarter note or 0 if smpteFmtId is valid
  36. cmMidiByte_t smpteFmtId; // smpte format or 0 if ticksPerQN is valid
  37. cmMidiByte_t smpteTicksPerFrame; // smpte ticks per frame or 0 if ticksPerQN is valid
  38. unsigned short trkN; // track count
  39. _cmMidiTrack_t* trkV; // track vector
  40. char* fn; // file name or NULL if this object did not originate from a file
  41. unsigned msgN; // count of msg's in msgV[]
  42. cmMidiTrackMsg_t** msgV; // sorted msg list
  43. bool msgVDirtyFl; // msgV[] needs to be refreshed from trkV[] because new msg's were inserted.
  44. unsigned nextUid; // next available msg uid
  45. } _cmMidiFile_t;
  46. cmMidiFileH_t cmMidiFileNullHandle = cmSTATIC_NULL_HANDLE;
  47. const cmMidiTrackMsg_t** _cmMidiFileMsgArray( _cmMidiFile_t* p );
  48. _cmMidiFile_t* _cmMidiFileHandleToPtr( cmMidiFileH_t h )
  49. {
  50. _cmMidiFile_t* p = (_cmMidiFile_t*)h.h;
  51. assert( p != NULL );
  52. return p;
  53. }
  54. cmMfRC_t _cmMidiFileRead8( _cmMidiFile_t* mfp, cmMidiByte_t* p )
  55. {
  56. if( cmFileReadUChar(mfp->fh,p,1) != kOkFileRC )
  57. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI byte read failed.");
  58. return kOkMfRC;
  59. }
  60. cmMfRC_t _cmMidiFileRead16( _cmMidiFile_t* mfp, unsigned short* p )
  61. {
  62. if( cmFileReadUShort(mfp->fh,p,1) != kOkFileRC )
  63. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI short read failed.");
  64. *p = mfSwap16(*p);
  65. return kOkMfRC;
  66. }
  67. cmMfRC_t _cmMidiFileRead24( _cmMidiFile_t* mfp, unsigned* p )
  68. {
  69. *p = 0;
  70. int i = 0;
  71. for(; i<3; ++i)
  72. {
  73. unsigned char c;
  74. if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
  75. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI 24 bit integer read failed.");
  76. *p = (*p << 8) + c;
  77. }
  78. //*p =mfSwap32(*p);
  79. return kOkMfRC;
  80. }
  81. cmMfRC_t _cmMidiFileRead32( _cmMidiFile_t* mfp, unsigned* p )
  82. {
  83. if( cmFileReadUInt(mfp->fh,p,1) != kOkFileRC )
  84. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI integer read failed.");
  85. *p = mfSwap32(*p);
  86. return kOkMfRC;
  87. }
  88. cmMfRC_t _cmMidiFileReadText( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
  89. {
  90. if( byteN == 0 )
  91. return kOkMfRC;
  92. char* t = cmLhAllocZ(mfp->lhH,char,byteN+1);
  93. t[byteN] = 0;
  94. if( cmFileReadChar(mfp->fh,t,byteN) != kOkFileRC )
  95. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read text failed.");
  96. tmp->u.text = t;
  97. tmp->byteCnt = byteN;
  98. return kOkMfRC;
  99. }
  100. cmMfRC_t _cmMidiFileReadRecd( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
  101. {
  102. char* t = cmLhAllocZ(mfp->lhH,char,byteN);
  103. if( cmFileReadChar(mfp->fh,t,byteN) != kOkFileRC )
  104. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read record failed.");
  105. tmp->byteCnt = byteN;
  106. tmp->u.voidPtr = t;
  107. return kOkMfRC;
  108. }
  109. cmMfRC_t _cmMidiFileReadVarLen( _cmMidiFile_t* mfp, unsigned* p )
  110. {
  111. unsigned char c;
  112. if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
  113. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read variable length integer failed.");
  114. if( !(c & 0x80) )
  115. *p = c;
  116. else
  117. {
  118. *p = c & 0x7f;
  119. do
  120. {
  121. if( cmFileReadUChar(mfp->fh,&c,1) != kOkFileRC )
  122. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI read variable length integer failed.");
  123. *p = (*p << 7) + (c & 0x7f);
  124. }while( c & 0x80 );
  125. }
  126. return kOkMfRC;
  127. }
  128. cmMidiTrackMsg_t* _cmMidiFileAllocMsg( _cmMidiFile_t* mfp, unsigned short trkIdx, unsigned dtick, cmMidiByte_t status )
  129. {
  130. cmMidiTrackMsg_t* tmp = cmLhAllocZ(mfp->lhH,cmMidiTrackMsg_t, 1 );
  131. // set the generic track record fields
  132. tmp->dtick = dtick;
  133. tmp->status = status;
  134. tmp->metaId = kInvalidMetaMdId;
  135. tmp->trkIdx = trkIdx;
  136. tmp->byteCnt = 0;
  137. tmp->uid = mfp->nextUid++;
  138. return tmp;
  139. }
  140. cmMfRC_t _cmMidiFileAppendTrackMsg( _cmMidiFile_t* mfp, unsigned short trkIdx, unsigned dtick, cmMidiByte_t status, cmMidiTrackMsg_t** trkMsgPtrPtr )
  141. {
  142. cmMidiTrackMsg_t* tmp = _cmMidiFileAllocMsg( mfp, trkIdx, dtick, status );
  143. // link new record onto track record chain
  144. if( mfp->trkV[trkIdx].base == NULL )
  145. mfp->trkV[trkIdx].base = tmp;
  146. else
  147. mfp->trkV[trkIdx].last->link = tmp;
  148. mfp->trkV[trkIdx].last = tmp;
  149. mfp->trkV[trkIdx].cnt++;
  150. *trkMsgPtrPtr = tmp;
  151. return kOkMfRC;
  152. }
  153. cmMfRC_t _cmMidiFileReadSysEx( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp, unsigned byteN )
  154. {
  155. cmMfRC_t rc = kOkMfRC;
  156. cmMidiByte_t b = 0;
  157. if( byteN == cmInvalidCnt )
  158. {
  159. long offs;
  160. if( cmFileTell(mfp->fh,&offs) != kOkFileRC )
  161. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI File 'tell' failed.");
  162. byteN = 0;
  163. // get the length of the sys-ex msg
  164. while( !cmFileEof(mfp->fh) && (b != kSysComEoxMdId) )
  165. {
  166. if((rc = _cmMidiFileRead8(mfp,&b)) != kOkMfRC )
  167. return rc;
  168. ++byteN;
  169. }
  170. // verify that the EOX byte was found
  171. if( b != kSysComEoxMdId )
  172. return cmErrMsg(&mfp->err,kMissingEoxMfRC,"MIDI file missing 'end-of-sys-ex'.");
  173. // rewind to the beginning of the msg
  174. if( cmFileSeek(mfp->fh,kBeginFileFl,offs) != kOkFileRC )
  175. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file seek failed on sys-ex read.");
  176. }
  177. // allocate memory to hold the sys-ex msg
  178. cmMidiByte_t* mp = cmLhAllocZ(mfp->lhH,cmMidiByte_t, byteN );
  179. // read the sys-ex msg from the file into msg memory
  180. if( cmFileReadUChar(mfp->fh,mp,byteN) != kOkFileRC )
  181. return cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI sys-ex read failed.");
  182. tmp->byteCnt = byteN;
  183. tmp->u.sysExPtr = mp;
  184. return rc;
  185. }
  186. cmMfRC_t _cmMidiFileReadChannelMsg( _cmMidiFile_t* mfp, cmMidiByte_t* rsPtr, cmMidiByte_t status, cmMidiTrackMsg_t* tmp )
  187. {
  188. cmMfRC_t rc = kOkMfRC;
  189. cmMidiChMsg_t* p = cmLhAllocZ(mfp->lhH,cmMidiChMsg_t,1);
  190. unsigned useRsFl = status <= 0x7f;
  191. cmMidiByte_t statusCh = useRsFl ? *rsPtr : status;
  192. if( useRsFl )
  193. p->d0 = status;
  194. else
  195. *rsPtr = status;
  196. tmp->byteCnt = sizeof(cmMidiChMsg_t);
  197. tmp->status = statusCh & 0xf0;
  198. p->ch = statusCh & 0x0f;
  199. p->durMicros = 0;
  200. unsigned byteN = cmMidiStatusToByteCount(tmp->status);
  201. if( byteN==kInvalidMidiByte || byteN > 2 )
  202. return cmErrMsg(&mfp->err,kInvalidStatusMfRC,"Invalid status:0x%x %i byte cnt:%i.",tmp->status,tmp->status,byteN);
  203. unsigned i;
  204. for(i=useRsFl; i<byteN; ++i)
  205. {
  206. cmMidiByte_t* b = i==0 ? &p->d0 : &p->d1;
  207. if((rc = _cmMidiFileRead8(mfp,b)) != kOkMfRC )
  208. return rc;
  209. }
  210. // convert note-on velocity=0 to note off
  211. if( tmp->status == kNoteOnMdId && p->d1==0 )
  212. tmp->status = kNoteOffMdId;
  213. tmp->u.chMsgPtr = p;
  214. return rc;
  215. }
  216. cmMfRC_t _cmMidiFileReadMetaMsg( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp )
  217. {
  218. cmMidiByte_t metaId;
  219. cmMfRC_t rc;
  220. unsigned byteN = 0;
  221. if((rc = _cmMidiFileRead8(mfp,&metaId)) != kOkMfRC )
  222. return rc;
  223. if((rc = _cmMidiFileReadVarLen(mfp,&byteN)) != kOkMfRC )
  224. return rc;
  225. //printf("mt: %i 0x%x n:%i\n",metaId,metaId,byteN);
  226. switch( metaId )
  227. {
  228. case kSeqNumbMdId: rc = _cmMidiFileRead16(mfp,&tmp->u.sVal); break;
  229. case kTextMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  230. case kCopyMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  231. case kTrkNameMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  232. case kInstrNameMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  233. case kLyricsMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  234. case kMarkerMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  235. case kCuePointMdId: rc = _cmMidiFileReadText(mfp,tmp,byteN); break;
  236. case kMidiChMdId: rc = _cmMidiFileRead8(mfp,&tmp->u.bVal); break;
  237. case kEndOfTrkMdId: break;
  238. case kTempoMdId: rc = _cmMidiFileRead24(mfp,&tmp->u.iVal); break;
  239. case kSmpteMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiSmpte_t)); break;
  240. case kTimeSigMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiTimeSig_t)); break;
  241. case kKeySigMdId: rc = _cmMidiFileReadRecd(mfp,tmp,sizeof(cmMidiKeySig_t)); break;
  242. case kSeqSpecMdId: rc = _cmMidiFileReadSysEx(mfp,tmp,byteN); break;
  243. default:
  244. cmFileSeek(mfp->fh,kCurFileFl,byteN);
  245. rc = cmErrMsg(&mfp->err,kUnknownMetaIdMfRC,"Unknown meta status:0x%x %i.",metaId,metaId);
  246. }
  247. tmp->metaId = metaId;
  248. return rc;
  249. }
  250. cmMfRC_t _cmMidiFileReadTrack( _cmMidiFile_t* mfp, unsigned short trkIdx )
  251. {
  252. cmMfRC_t rc = kOkMfRC;
  253. unsigned dticks = 0;
  254. cmMidiByte_t status;
  255. cmMidiByte_t runstatus = 0;
  256. bool contFl = true;
  257. while( contFl && (rc==kOkMfRC))
  258. {
  259. cmMidiTrackMsg_t* tmp = NULL;
  260. // read the tick count
  261. if((rc = _cmMidiFileReadVarLen(mfp,&dticks)) != kOkMfRC )
  262. return rc;
  263. // read the status byte
  264. if((rc = _cmMidiFileRead8(mfp,&status)) != kOkMfRC )
  265. return rc;
  266. //printf("%5i st:%i 0x%x\n",dticks,status,status);
  267. // append a track msg
  268. if((rc = _cmMidiFileAppendTrackMsg( mfp, trkIdx, dticks, status, &tmp )) != kOkMfRC )
  269. return rc;
  270. // switch on status
  271. switch( status )
  272. {
  273. // handle sys-ex msg
  274. case kSysExMdId:
  275. rc = _cmMidiFileReadSysEx(mfp,tmp,cmInvalidCnt);
  276. break;
  277. // handle meta msg
  278. case kMetaStId:
  279. rc = _cmMidiFileReadMetaMsg(mfp,tmp);
  280. // ignore unknown meta messages
  281. if( rc == kUnknownMetaIdMfRC )
  282. rc = kOkMfRC;
  283. contFl = tmp->metaId != kEndOfTrkMdId;
  284. break;
  285. default:
  286. // handle channel msg
  287. rc = _cmMidiFileReadChannelMsg(mfp,&runstatus,status,tmp);
  288. }
  289. }
  290. return rc;
  291. }
  292. cmMfRC_t _cmMidiFileReadHdr( _cmMidiFile_t* mfp )
  293. {
  294. cmMfRC_t rc;
  295. unsigned fileId;
  296. unsigned chunkByteN;
  297. // read the file id
  298. if((rc = _cmMidiFileRead32(mfp,&fileId)) != kOkMfRC )
  299. return rc;
  300. // verify the file id
  301. if( fileId != 'MThd' )
  302. return cmErrMsg(&mfp->err,kNotAMidiFileMfRC,"");
  303. // read the file chunk byte count
  304. if((rc = _cmMidiFileRead32(mfp,&chunkByteN)) != kOkMfRC )
  305. return rc;
  306. // read the format id
  307. if((rc = _cmMidiFileRead16(mfp,&mfp->fmtId)) != kOkMfRC )
  308. return rc;
  309. // read the track count
  310. if((rc = _cmMidiFileRead16(mfp,&mfp->trkN)) != kOkMfRC )
  311. return rc;
  312. // read the ticks per quarter note
  313. if((rc = _cmMidiFileRead16(mfp,&mfp->ticksPerQN)) != kOkMfRC )
  314. return rc;
  315. // if the division field was given in smpte
  316. if( mfp->ticksPerQN & 0x8000 )
  317. {
  318. mfp->smpteFmtId = (mfp->ticksPerQN & 0x7f00) >> 8;
  319. mfp->smpteTicksPerFrame = (mfp->ticksPerQN & 0xFF);
  320. mfp->ticksPerQN = 0;
  321. }
  322. // allocate and zero the track array
  323. if( mfp->trkN )
  324. mfp->trkV = cmLhAllocZ(mfp->lhH, _cmMidiTrack_t, mfp->trkN);
  325. return rc;
  326. }
  327. void _cmMidiFileDrop( _cmMidiFile_t* p )
  328. {
  329. unsigned i;
  330. unsigned n = 0;
  331. for(i=0; i<p->trkN; ++i)
  332. {
  333. _cmMidiTrack_t* trk = p->trkV + i;
  334. cmMidiTrackMsg_t* m0 = NULL;
  335. cmMidiTrackMsg_t* m = trk->base;
  336. for(; m!=NULL; m=m->link)
  337. {
  338. if( cmIsFlag(m->flags,kDropTrkMsgFl) )
  339. {
  340. ++n;
  341. if( m0 == NULL )
  342. trk->base = m->link;
  343. else
  344. m0->link = m->link;
  345. }
  346. else
  347. {
  348. m0 = m;
  349. }
  350. }
  351. }
  352. }
  353. int _cmMidiFileSortFunc( const void *p0, const void* p1 )
  354. {
  355. if( (*(cmMidiTrackMsg_t**)p0)->atick == (*(cmMidiTrackMsg_t**)p1)->atick )
  356. return 0;
  357. return (*(cmMidiTrackMsg_t**)p0)->atick < (*(cmMidiTrackMsg_t**)p1)->atick ? -1 : 1;
  358. }
  359. // Set the absolute accumulated ticks (atick) value of each track message.
  360. // The absolute accumulated ticks gives a global time ordering for all
  361. // messages in the file.
  362. void _cmMidiFileSetAccumulateTicks( _cmMidiFile_t* p )
  363. {
  364. cmMidiTrackMsg_t* nextTrkMsg[ p->trkN ]; // next msg in each track
  365. unsigned long long atick = 0;
  366. unsigned i;
  367. bool fl = true;
  368. // iniitalize nextTrkTick[] and nextTrkMsg[] to the first msg in each track
  369. for(i=0; i<p->trkN; ++i)
  370. if((nextTrkMsg[i] = p->trkV[i].base) != NULL )
  371. nextTrkMsg[i]->atick = nextTrkMsg[i]->dtick;
  372. while(1)
  373. {
  374. unsigned k = cmInvalidIdx;
  375. // find the trk which has the next msg (min atick time)
  376. for(i=0; i<p->trkN; ++i)
  377. if( nextTrkMsg[i]!=NULL && (k==cmInvalidIdx || nextTrkMsg[i]->atick < nextTrkMsg[k]->atick) )
  378. k = i;
  379. // no next msg was found - we're done
  380. if( k == cmInvalidIdx )
  381. break;
  382. if( fl && nextTrkMsg[k]->dtick > 0 )
  383. {
  384. fl = false;
  385. nextTrkMsg[k]->dtick = 1;
  386. nextTrkMsg[k]->atick = 1;
  387. }
  388. // store the current atick
  389. atick = nextTrkMsg[k]->atick;
  390. // advance the selected track to it's next message
  391. nextTrkMsg[k] = nextTrkMsg[k]->link;
  392. // set the selected tracks next atick time
  393. if( nextTrkMsg[k] != NULL )
  394. nextTrkMsg[k]->atick = atick + nextTrkMsg[k]->dtick;
  395. }
  396. }
  397. void _cmMidiFileSetAbsoluteTime( _cmMidiFile_t* mfp )
  398. {
  399. const cmMidiTrackMsg_t** msgV = _cmMidiFileMsgArray(mfp);
  400. double microsPerQN = 60000000/120; // default tempo;
  401. double microsPerTick = microsPerQN / mfp->ticksPerQN;
  402. unsigned long long amicro = 0;
  403. unsigned i;
  404. for(i=0; i<mfp->msgN; ++i)
  405. {
  406. cmMidiTrackMsg_t* mp = (cmMidiTrackMsg_t*)msgV[i]; // cast away const
  407. unsigned dtick = 0;
  408. if( i > 0 )
  409. {
  410. // atick must have already been set and sorted
  411. assert( mp->atick >= msgV[i-1]->atick );
  412. dtick = mp->atick - msgV[i-1]->atick;
  413. }
  414. amicro += microsPerTick * dtick;
  415. mp->amicro = amicro;
  416. // track tempo changes
  417. if( mp->status == kMetaStId && mp->metaId == kTempoMdId )
  418. microsPerTick = mp->u.iVal / mfp->ticksPerQN;
  419. }
  420. }
  421. cmMfRC_t _cmMidiFileClose( _cmMidiFile_t* mfp )
  422. {
  423. cmMfRC_t rc = kOkMfRC;
  424. if( mfp == NULL )
  425. return rc;
  426. cmMemPtrFree(&mfp->msgV);
  427. if( cmFileIsValid( mfp->fh ) )
  428. if( cmFileClose( &mfp->fh ) != kOkFileRC )
  429. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file close failed.");
  430. if( cmLHeapIsValid( mfp->lhH ) )
  431. cmLHeapDestroy(&mfp->lhH);
  432. cmMemPtrFree(&mfp);
  433. return rc;
  434. }
  435. void _cmMidiFileLinearize( _cmMidiFile_t* mfp )
  436. {
  437. unsigned trkIdx,i,j;
  438. if( mfp->msgVDirtyFl == false )
  439. return;
  440. mfp->msgVDirtyFl = false;
  441. // get the total trk msg count
  442. mfp->msgN = 0;
  443. for(trkIdx=0; trkIdx<mfp->trkN; ++trkIdx)
  444. mfp->msgN += mfp->trkV[ trkIdx ].cnt;
  445. // allocate the trk msg index vector: msgV[]
  446. mfp->msgV = cmMemResizeZ(cmMidiTrackMsg_t*, mfp->msgV, mfp->msgN);
  447. // store a pointer to every trk msg in msgV[]
  448. for(i=0,j=0; i<mfp->trkN; ++i)
  449. {
  450. cmMidiTrackMsg_t* m = mfp->trkV[i].base;
  451. for(; m!=NULL; m=m->link)
  452. {
  453. assert( j < mfp->msgN );
  454. mfp->msgV[j++] = m;
  455. }
  456. }
  457. // set the atick value in each msg
  458. _cmMidiFileSetAccumulateTicks(mfp);
  459. // sort msgV[] in ascending order on atick
  460. qsort( mfp->msgV, mfp->msgN, sizeof(cmMidiTrackMsg_t*), _cmMidiFileSortFunc );
  461. // set the amicro value in each msg
  462. _cmMidiFileSetAbsoluteTime(mfp);
  463. }
  464. // Note that p->msgV[] should always be accessed through this function
  465. // to guarantee that the p->msgVDirtyFl is checked and msgV[] is updated
  466. // in case msgV[] is out of sync (due to inserted msgs (see cmMidiFileInsertTrackMsg())
  467. // with trkV[].
  468. const cmMidiTrackMsg_t** _cmMidiFileMsgArray( _cmMidiFile_t* p )
  469. {
  470. _cmMidiFileLinearize(p);
  471. // this cast is needed to eliminate an apparently needless 'incompatible type' warning
  472. return (const cmMidiTrackMsg_t**)p->msgV;
  473. }
  474. cmMfRC_t _cmMidiFileCreate( cmCtx_t* ctx, cmMidiFileH_t* hp )
  475. {
  476. cmMfRC_t rc = kOkMfRC;
  477. _cmMidiFile_t* p = NULL;
  478. if((rc = cmMidiFileClose(hp)) != kOkMfRC )
  479. return rc;
  480. // allocate the midi file object
  481. if(( p = cmMemAllocZ( _cmMidiFile_t, 1)) == NULL )
  482. return rc = cmErrMsg(&ctx->err,kMemAllocFailMfRC,"MIDI file memory allocation failed.");
  483. cmErrSetup(&p->err,&ctx->rpt,"MIDI File");
  484. // allocate the linked heap
  485. if( cmLHeapIsValid( p->lhH = cmLHeapCreate( 1024, ctx )) == false )
  486. rc = cmErrMsg(&p->err,kMemAllocFailMfRC,"MIDI heap allocation failed.");
  487. if( rc != kOkMfRC )
  488. _cmMidiFileClose(p);
  489. else
  490. hp->h = p;
  491. return rc;
  492. }
  493. cmMfRC_t cmMidiFileOpen( cmCtx_t* ctx, cmMidiFileH_t* hp, const char* fn )
  494. {
  495. cmMfRC_t rc = kOkMfRC;
  496. unsigned short trkIdx = 0;
  497. if((rc = _cmMidiFileCreate(ctx,hp)) != kOkMfRC )
  498. return rc;
  499. _cmMidiFile_t* p = _cmMidiFileHandleToPtr(*hp);
  500. // open the file
  501. if(cmFileOpen(&p->fh,fn,kReadFileFl | kBinaryFileFl,p->err.rpt) != kOkFileRC )
  502. {
  503. rc = cmErrMsg(&p->err,kFileFailMfRC,"MIDI file open failed.");
  504. goto errLabel;
  505. }
  506. // read header and setup track array
  507. if(( rc = _cmMidiFileReadHdr(p)) != kOkMfRC )
  508. goto errLabel;
  509. while( !cmFileEof(p->fh) && trkIdx < p->trkN )
  510. {
  511. unsigned chkId = 0,chkN=0;
  512. // read the chunk id
  513. if((rc = _cmMidiFileRead32(p,&chkId)) != kOkMfRC )
  514. goto errLabel;
  515. // read the chunk size
  516. if((rc = _cmMidiFileRead32(p,&chkN)) != kOkMfRC )
  517. goto errLabel;
  518. // if this is not a trk chunk then skip it
  519. if( chkId != (unsigned)'MTrk')
  520. {
  521. //if( fseek( p->fp, chkN, SEEK_CUR) != 0 )
  522. if( cmFileSeek(p->fh,kCurFileFl,chkN) != kOkFileRC )
  523. {
  524. rc = cmErrMsg(&p->err,kFileFailMfRC,"MIDI file seek failed.");
  525. goto errLabel;
  526. }
  527. }
  528. else
  529. {
  530. if((rc = _cmMidiFileReadTrack(p,trkIdx)) != kOkMfRC )
  531. goto errLabel;
  532. ++trkIdx;
  533. }
  534. }
  535. // store the file name
  536. p->fn = cmLhAllocZ(p->lhH,char,strlen(fn)+1);
  537. assert( p->fn != NULL );
  538. strcpy(p->fn,fn);
  539. p->msgVDirtyFl = true;
  540. _cmMidiFileLinearize(p);
  541. errLabel:
  542. if( cmFileClose(&p->fh) != kOkFileRC )
  543. rc = cmErrMsg(&p->err,kFileFailMfRC,"MIDI file close failed.");
  544. if( rc != kOkMfRC )
  545. {
  546. _cmMidiFileClose(p);
  547. hp->h = NULL;
  548. }
  549. return rc;
  550. }
  551. cmMfRC_t cmMidiFileCreate( cmCtx_t* ctx, cmMidiFileH_t* hp, unsigned trkN, unsigned ticksPerQN )
  552. {
  553. cmMfRC_t rc = kOkMfRC;
  554. if((rc = _cmMidiFileCreate(ctx,hp)) != kOkMfRC )
  555. return rc;
  556. _cmMidiFile_t* p = _cmMidiFileHandleToPtr(*hp);
  557. p->ticksPerQN = ticksPerQN;
  558. p->fmtId = 1;
  559. p->trkN = trkN;
  560. p->trkV = cmLhAllocZ(p->lhH, _cmMidiTrack_t, p->trkN);
  561. return rc;
  562. }
  563. cmMfRC_t cmMidiFileClose( cmMidiFileH_t* hp )
  564. {
  565. cmMfRC_t rc = kOkMfRC;
  566. if( hp==NULL || cmMidiFileIsValid(*hp)==false )
  567. return kOkMfRC;
  568. _cmMidiFile_t* p = _cmMidiFileHandleToPtr(*hp);
  569. if((rc = _cmMidiFileClose(p)) != kOkMfRC )
  570. return rc;
  571. hp->h = NULL;
  572. return rc;
  573. }
  574. cmMfRC_t _cmMidiFileWrite8( _cmMidiFile_t* mfp, unsigned char v )
  575. {
  576. cmMfRC_t rc = kOkMfRC;
  577. if( cmFileWriteUChar(mfp->fh,&v,1) != kOkFileRC )
  578. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file byte write failed.");
  579. return rc;
  580. }
  581. cmMfRC_t _cmMidiFileWrite16( _cmMidiFile_t* mfp, unsigned short v )
  582. {
  583. cmMfRC_t rc = kOkMfRC;
  584. v = mfSwap16(v);
  585. if( cmFileWriteUShort(mfp->fh,&v,1) != kOkFileRC )
  586. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file short integer write failed.");
  587. return rc;
  588. }
  589. cmMfRC_t _cmMidiFileWrite24( _cmMidiFile_t* mfp, unsigned v )
  590. {
  591. cmMfRC_t rc = kOkMfRC;
  592. unsigned mask = 0xff0000;
  593. int i;
  594. for(i = 2; i>=0; --i)
  595. {
  596. unsigned char c = (v & mask) >> (i*8);
  597. mask >>= 8;
  598. if( cmFileWriteUChar(mfp->fh,&c,1) != kOkFileRC )
  599. {
  600. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file 24 bit integer write failed.");
  601. goto errLabel;
  602. }
  603. }
  604. errLabel:
  605. return rc;
  606. }
  607. cmMfRC_t _cmMidiFileWrite32( _cmMidiFile_t* mfp, unsigned v )
  608. {
  609. cmMfRC_t rc = kOkMfRC;
  610. v = mfSwap32(v);
  611. if( cmFileWriteUInt(mfp->fh,&v,1) != kOkFileRC )
  612. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file integer write failed.");
  613. return rc;
  614. }
  615. cmMfRC_t _cmMidiFileWriteRecd( _cmMidiFile_t* mfp, const void* v, unsigned byteCnt )
  616. {
  617. cmMfRC_t rc = kOkMfRC;
  618. if( cmFileWriteChar(mfp->fh,v,byteCnt) != kOkFileRC )
  619. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file write record failed.");
  620. return rc;
  621. }
  622. cmMfRC_t _cmMidiFileWriteVarLen( _cmMidiFile_t* mfp, unsigned v )
  623. {
  624. cmMfRC_t rc = kOkMfRC;
  625. unsigned buf = v & 0x7f;
  626. while((v >>= 7) > 0 )
  627. {
  628. buf <<= 8;
  629. buf |= 0x80;
  630. buf += (v & 0x7f);
  631. }
  632. while(1)
  633. {
  634. unsigned char c = (unsigned char)(buf & 0xff);
  635. if( cmFileWriteUChar(mfp->fh,&c,1) != kOkFileRC )
  636. {
  637. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"MIDI file variable length integer write failed.");
  638. goto errLabel;
  639. }
  640. if( buf & 0x80 )
  641. buf >>= 8;
  642. else
  643. break;
  644. }
  645. errLabel:
  646. return rc;
  647. }
  648. cmMfRC_t _cmMidiFileWriteHdr( _cmMidiFile_t* mfp )
  649. {
  650. cmMfRC_t rc;
  651. unsigned fileId = 'MThd';
  652. unsigned chunkByteN = 6;
  653. // write the file id ('MThd')
  654. if((rc = _cmMidiFileWrite32(mfp,fileId)) != kOkMfRC )
  655. return rc;
  656. // write the file chunk byte count (always 6)
  657. if((rc = _cmMidiFileWrite32(mfp,chunkByteN)) != kOkMfRC )
  658. return rc;
  659. // write the MIDI file format id (0,1,2)
  660. if((rc = _cmMidiFileWrite16(mfp,mfp->fmtId)) != kOkMfRC )
  661. return rc;
  662. // write the track count
  663. if((rc = _cmMidiFileWrite16(mfp,mfp->trkN)) != kOkMfRC )
  664. return rc;
  665. unsigned short v = 0;
  666. // if the ticks per quarter note field is valid ...
  667. if( mfp->ticksPerQN )
  668. v = mfp->ticksPerQN;
  669. else
  670. {
  671. // ... otherwise the division field was given in smpte
  672. v = mfp->smpteFmtId << 8;
  673. v += mfp->smpteTicksPerFrame;
  674. }
  675. if((rc = _cmMidiFileWrite16(mfp,v)) != kOkMfRC )
  676. return rc;
  677. return rc;
  678. }
  679. cmMfRC_t _cmMidiFileWriteSysEx( _cmMidiFile_t* mfp, cmMidiTrackMsg_t* tmp )
  680. {
  681. cmMfRC_t rc = kOkMfRC;
  682. if((rc = _cmMidiFileWrite8(mfp,kSysExMdId)) != kOkMfRC )
  683. goto errLabel;
  684. if( cmFileWriteUChar(mfp->fh,tmp->u.sysExPtr,tmp->byteCnt) != kOkFileRC )
  685. rc = cmErrMsg(&mfp->err,kFileFailMfRC,"Sys-ex msg write failed.");
  686. errLabel:
  687. return rc;
  688. }
  689. cmMfRC_t _cmMidiFileWriteChannelMsg( _cmMidiFile_t* mfp, const cmMidiTrackMsg_t* tmp, cmMidiByte_t* runStatus )
  690. {
  691. cmMfRC_t rc = kOkMfRC;
  692. unsigned byteN = cmMidiStatusToByteCount(tmp->status);
  693. cmMidiByte_t status = tmp->status + tmp->u.chMsgPtr->ch;
  694. if( status != *runStatus )
  695. {
  696. *runStatus = status;
  697. if((rc = _cmMidiFileWrite8(mfp,status)) != kOkMfRC )
  698. goto errLabel;
  699. }
  700. if(byteN>=1)
  701. if((rc = _cmMidiFileWrite8(mfp,tmp->u.chMsgPtr->d0)) != kOkMfRC )
  702. goto errLabel;
  703. if(byteN>=2)
  704. if((rc = _cmMidiFileWrite8(mfp,tmp->u.chMsgPtr->d1)) != kOkMfRC )
  705. goto errLabel;
  706. errLabel:
  707. return rc;
  708. }
  709. cmMfRC_t _cmMidiFileWriteMetaMsg( _cmMidiFile_t* mfp, const cmMidiTrackMsg_t* tmp )
  710. {
  711. cmMfRC_t rc;
  712. if((rc = _cmMidiFileWrite8(mfp,kMetaStId)) != kOkMfRC )
  713. return rc;
  714. if((rc = _cmMidiFileWrite8(mfp,tmp->metaId)) != kOkMfRC )
  715. return rc;
  716. switch( tmp->metaId )
  717. {
  718. case kSeqNumbMdId:
  719. if((rc = _cmMidiFileWrite8(mfp,sizeof(tmp->u.sVal))) == kOkMfRC )
  720. rc = _cmMidiFileWrite16(mfp,tmp->u.sVal);
  721. break;
  722. case kTempoMdId:
  723. if((rc = _cmMidiFileWrite8(mfp,3)) == kOkMfRC )
  724. rc = _cmMidiFileWrite24(mfp,tmp->u.iVal);
  725. break;
  726. case kSmpteMdId:
  727. if((rc = _cmMidiFileWrite8(mfp,sizeof(cmMidiSmpte_t))) == kOkMfRC )
  728. rc = _cmMidiFileWriteRecd(mfp,tmp->u.smptePtr,sizeof(cmMidiSmpte_t));
  729. break;
  730. case kTimeSigMdId:
  731. if((rc = _cmMidiFileWrite8(mfp,sizeof(cmMidiTimeSig_t))) == kOkMfRC )
  732. rc = _cmMidiFileWriteRecd(mfp,tmp->u.timeSigPtr,sizeof(cmMidiTimeSig_t));
  733. break;
  734. case kKeySigMdId:
  735. if((rc = _cmMidiFileWrite8(mfp,sizeof(cmMidiKeySig_t))) == kOkMfRC )
  736. rc = _cmMidiFileWriteRecd(mfp,tmp->u.keySigPtr,sizeof(cmMidiKeySig_t));
  737. break;
  738. case kSeqSpecMdId:
  739. if((rc = _cmMidiFileWriteVarLen(mfp,sizeof(tmp->byteCnt))) == kOkMfRC )
  740. rc = _cmMidiFileWriteRecd(mfp,tmp->u.sysExPtr,tmp->byteCnt);
  741. break;
  742. case kMidiChMdId:
  743. if((rc = _cmMidiFileWrite8(mfp,sizeof(tmp->u.bVal))) == kOkMfRC )
  744. rc = _cmMidiFileWrite8(mfp,tmp->u.bVal);
  745. break;
  746. case kEndOfTrkMdId:
  747. rc = _cmMidiFileWrite8(mfp,0);
  748. break;
  749. case kTextMdId:
  750. case kCopyMdId:
  751. case kTrkNameMdId:
  752. case kInstrNameMdId:
  753. case kLyricsMdId:
  754. case kMarkerMdId:
  755. case kCuePointMdId:
  756. {
  757. unsigned n = tmp->u.text==NULL ? 0 : strlen(tmp->u.text);
  758. if((rc = _cmMidiFileWriteVarLen(mfp,n)) == kOkMfRC && n>0 )
  759. rc = _cmMidiFileWriteRecd(mfp,tmp->u.text,n);
  760. }
  761. break;
  762. default:
  763. {
  764. // ignore unknown meta messages
  765. }
  766. }
  767. return rc;
  768. }
  769. cmMfRC_t _cmMidiFileInsertEotMsg( _cmMidiFile_t* p, unsigned trkIdx )
  770. {
  771. _cmMidiTrack_t* trk = p->trkV + trkIdx;
  772. cmMidiTrackMsg_t* m0 = NULL;
  773. cmMidiTrackMsg_t* m = trk->base;
  774. // locate the current EOT msg on this track
  775. for(; m!=NULL; m=m->link)
  776. {
  777. if( m->status == kMetaStId && m->metaId == kEndOfTrkMdId )
  778. {
  779. // If this EOT msg is the last msg in the track ...
  780. if( m->link == NULL )
  781. {
  782. assert( m == trk->last );
  783. return kOkMfRC; // ... then there is nothing else to do
  784. }
  785. // If this EOT msg is not the last in the track ...
  786. if( m0 != NULL )
  787. m0->link = m->link; // ... then unlink it
  788. break;
  789. }
  790. m0 = m;
  791. }
  792. // if we get here then the last msg in the track was not an EOT msg
  793. // if there was no previously allocated EOT msg
  794. if( m == NULL )
  795. {
  796. m = _cmMidiFileAllocMsg(p, trkIdx, 1, kMetaStId );
  797. m->metaId = kEndOfTrkMdId;
  798. trk->cnt += 1;
  799. }
  800. // link an EOT msg as the last msg on the track
  801. // if the track is currently empty
  802. if( m0 == NULL )
  803. {
  804. trk->base = m;
  805. trk->last = m;
  806. }
  807. else // link the msg as the last on on the track
  808. {
  809. assert( m0 == trk->last);
  810. m0->link = m;
  811. m->link = NULL;
  812. trk->last = m;
  813. }
  814. return kOkMfRC;
  815. }
  816. cmMfRC_t _cmMidiFileWriteTrack( _cmMidiFile_t* mfp, unsigned trkIdx )
  817. {
  818. cmMfRC_t rc = kOkMfRC;
  819. cmMidiTrackMsg_t* tmp = mfp->trkV[trkIdx].base;
  820. cmMidiByte_t runStatus = 0;
  821. // be sure there is a EOT msg at the end of this track
  822. if((rc = _cmMidiFileInsertEotMsg(mfp, trkIdx )) != kOkMfRC )
  823. return rc;
  824. for(; tmp != NULL; tmp=tmp->link)
  825. {
  826. // write the msg tick count
  827. if((rc = _cmMidiFileWriteVarLen(mfp,tmp->dtick)) != kOkMfRC )
  828. return rc;
  829. // switch on status
  830. switch( tmp->status )
  831. {
  832. // handle sys-ex msg
  833. case kSysExMdId:
  834. rc = _cmMidiFileWriteSysEx(mfp,tmp);
  835. break;
  836. // handle meta msg
  837. case kMetaStId:
  838. rc = _cmMidiFileWriteMetaMsg(mfp,tmp);
  839. break;
  840. default:
  841. // handle channel msg
  842. rc = _cmMidiFileWriteChannelMsg(mfp,tmp,&runStatus);
  843. }
  844. }
  845. return rc;
  846. }
  847. cmMfRC_t cmMidiFileWrite( cmMidiFileH_t h, const char* fn )
  848. {
  849. cmMfRC_t rc = kOkMfRC;
  850. _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
  851. unsigned i;
  852. // create the output file
  853. if( cmFileOpen(&mfp->fh,fn,kWriteFileFl,mfp->err.rpt) != kOkFileRC )
  854. return cmErrMsg(&mfp->err,kFileFailMfRC,"The MIDI file '%s' could not be created.",cmStringNullGuard(fn));
  855. // write the file header
  856. if((rc = _cmMidiFileWriteHdr(mfp)) != kOkMfRC )
  857. {
  858. rc = cmErrMsg(&mfp->err,rc,"The file header write failed on the MIDI file '%s'.",cmStringNullGuard(fn));
  859. goto errLabel;
  860. }
  861. for(i=0; i < mfp->trkN; ++i )
  862. {
  863. unsigned chkId = 'MTrk';
  864. long offs0,offs1;
  865. // write the track chunk id ('MTrk')
  866. if((rc = _cmMidiFileWrite32(mfp,chkId)) != kOkMfRC )
  867. goto errLabel;
  868. cmFileTell(mfp->fh,&offs0);
  869. // write the track chunk size as zero
  870. if((rc = _cmMidiFileWrite32(mfp,0)) != kOkMfRC )
  871. goto errLabel;
  872. if((rc = _cmMidiFileWriteTrack(mfp,i)) != kOkMfRC )
  873. goto errLabel;
  874. cmFileTell(mfp->fh,&offs1);
  875. cmFileSeek(mfp->fh,kBeginFileFl,offs0);
  876. _cmMidiFileWrite32(mfp,offs1-offs0-4);
  877. cmFileSeek(mfp->fh,kBeginFileFl,offs1);
  878. }
  879. errLabel:
  880. cmFileClose(&mfp->fh);
  881. return rc;
  882. }
  883. bool cmMidiFileIsValid( cmMidiFileH_t h )
  884. { return h.h != NULL; }
  885. unsigned cmMidiFileTrackCount( cmMidiFileH_t h )
  886. {
  887. _cmMidiFile_t* mfp;
  888. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  889. return cmInvalidCnt;
  890. return mfp->trkN;
  891. }
  892. unsigned cmMidiFileType( cmMidiFileH_t h )
  893. {
  894. _cmMidiFile_t* mfp;
  895. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  896. return cmInvalidId;
  897. return mfp->fmtId;
  898. }
  899. const char* cmMidiFileName( cmMidiFileH_t h )
  900. {
  901. _cmMidiFile_t* mfp;
  902. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  903. return NULL;
  904. return mfp->fn;
  905. }
  906. unsigned cmMidiFileTicksPerQN( cmMidiFileH_t h )
  907. {
  908. _cmMidiFile_t* mfp;
  909. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  910. return cmInvalidCnt;
  911. return mfp->ticksPerQN;
  912. }
  913. cmMidiByte_t cmMidiFileTicksPerSmpteFrame( cmMidiFileH_t h )
  914. {
  915. _cmMidiFile_t* mfp;
  916. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  917. return kInvalidMidiByte;
  918. if( mfp->ticksPerQN != 0 )
  919. return 0;
  920. return mfp->smpteTicksPerFrame;
  921. }
  922. cmMidiByte_t cmMidiFileSmpteFormatId( cmMidiFileH_t h )
  923. {
  924. _cmMidiFile_t* mfp;
  925. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  926. return kInvalidMidiByte;
  927. if( mfp->ticksPerQN != 0 )
  928. return 0;
  929. return mfp->smpteFmtId;
  930. }
  931. unsigned cmMidiFileTrackMsgCount( cmMidiFileH_t h, unsigned trackIdx )
  932. {
  933. _cmMidiFile_t* mfp;
  934. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  935. return cmInvalidCnt;
  936. return mfp->trkV[trackIdx].cnt;
  937. }
  938. const cmMidiTrackMsg_t* cmMidiFileTrackMsg( cmMidiFileH_t h, unsigned trackIdx )
  939. {
  940. _cmMidiFile_t* mfp;
  941. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  942. return NULL;
  943. return mfp->trkV[trackIdx].base;
  944. }
  945. unsigned cmMidiFileMsgCount( cmMidiFileH_t h )
  946. {
  947. _cmMidiFile_t* mfp;
  948. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  949. return cmInvalidCnt;
  950. return mfp->msgN;
  951. }
  952. const cmMidiTrackMsg_t** cmMidiFileMsgArray( cmMidiFileH_t h )
  953. {
  954. _cmMidiFile_t* mfp;
  955. if((mfp = _cmMidiFileHandleToPtr(h)) == NULL )
  956. return NULL;
  957. return _cmMidiFileMsgArray(mfp);
  958. }
  959. cmMidiTrackMsg_t* _cmMidiFileUidToMsg( _cmMidiFile_t* mfp, unsigned uid )
  960. {
  961. unsigned i;
  962. const cmMidiTrackMsg_t** msgV = _cmMidiFileMsgArray(mfp);
  963. for(i=0; i<mfp->msgN; ++i)
  964. if( msgV[i]->uid == uid )
  965. return (cmMidiTrackMsg_t*)msgV[i];
  966. return NULL;
  967. }
  968. cmMfRC_t cmMidiFileSetVelocity( cmMidiFileH_t h, unsigned uid, cmMidiByte_t vel )
  969. {
  970. cmMidiTrackMsg_t* r;
  971. _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
  972. assert( mfp != NULL );
  973. if((r = _cmMidiFileUidToMsg(mfp,uid)) == NULL )
  974. return cmErrMsg(&mfp->err,kUidNotFoundMfRC,"The MIDI file uid %i could not be found.",uid);
  975. if( cmMidiIsNoteOn(r->status) == false && cmMidiIsNoteOff(r->status,0)==false )
  976. return cmErrMsg(&mfp->err,kUidNotANoteMsgMfRC,"Cannot set velocity on a non-Note-On/Off msg.");
  977. cmMidiChMsg_t* chm = (cmMidiChMsg_t*)r->u.chMsgPtr;
  978. chm->d1 = vel;
  979. return kOkMfRC;
  980. }
  981. // Returns NULL if uid is not found or if it the first msg on the track.
  982. cmMidiTrackMsg_t* _cmMidiFileMsgBeforeUid( _cmMidiFile_t* p, unsigned uid )
  983. {
  984. cmMidiTrackMsg_t* m;
  985. if((m = _cmMidiFileUidToMsg(p,uid)) == NULL )
  986. return NULL;
  987. assert( m->trkIdx < p->trkN );
  988. cmMidiTrackMsg_t* m0 = NULL;
  989. cmMidiTrackMsg_t* m1 = p->trkV[ m->trkIdx ].base;
  990. for(; m1!=NULL; m1 = m1->link)
  991. {
  992. if( m1->uid == uid )
  993. break;
  994. m0 = m1;
  995. }
  996. return m0;
  997. }
  998. unsigned _cmMidiFileIsMsgFirstOnTrack( _cmMidiFile_t* p, unsigned uid )
  999. {
  1000. unsigned i;
  1001. for(i=0; i<p->trkN; ++i)
  1002. if( p->trkV[i].base!=NULL && p->trkV[i].base->uid == uid )
  1003. return i;
  1004. return cmInvalidIdx;
  1005. }
  1006. cmMfRC_t cmMidiFileInsertMsg( cmMidiFileH_t h, unsigned uid, int dtick, cmMidiByte_t ch, cmMidiByte_t status, cmMidiByte_t d0, cmMidiByte_t d1 )
  1007. {
  1008. _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
  1009. assert( mfp != NULL );
  1010. cmMidiTrackMsg_t* ref = NULL;
  1011. unsigned trkIdx = cmInvalidIdx;
  1012. // if dtick is positive ...
  1013. if( dtick >= 0 )
  1014. {
  1015. ref = _cmMidiFileUidToMsg(mfp,uid); // ... then get the ref. msg.
  1016. trkIdx = ref->trkIdx;
  1017. }
  1018. else // if dtick is negative ...
  1019. {
  1020. // ... get get the msg before the ref. msg.
  1021. if((ref = _cmMidiFileMsgBeforeUid(mfp,uid)) != NULL )
  1022. trkIdx = ref->trkIdx;
  1023. else
  1024. {
  1025. // ... the ref. msg was first in the track so there is no msg before it
  1026. trkIdx = _cmMidiFileIsMsgFirstOnTrack(mfp,uid);
  1027. }
  1028. }
  1029. // verify that the reference msg was found
  1030. if( trkIdx == cmInvalidIdx )
  1031. return cmErrMsg(&mfp->err,kUidNotFoundMfRC,"The UID (%i) reference note could not be located.",uid);
  1032. assert( trkIdx < mfp->trkN );
  1033. // complete the msg setup
  1034. _cmMidiTrack_t* trk = mfp->trkV + trkIdx;
  1035. cmMidiTrackMsg_t* m = _cmMidiFileAllocMsg(mfp, trkIdx, abs(dtick), status );
  1036. cmMidiChMsg_t* c = cmLhAllocZ(mfp->lhH,cmMidiChMsg_t,1);
  1037. m->u.chMsgPtr = c;
  1038. c->ch = ch;
  1039. c->d0 = d0;
  1040. c->d1 = d1;
  1041. // if 'm' is prior to the first msg in the track
  1042. if( ref == NULL )
  1043. {
  1044. // ... then make 'm' the first msg in the first msg
  1045. m->link = trk->base;
  1046. trk->base = m;
  1047. // 'm' is before ref and the track cannot be empty (because ref is in it) 'm'
  1048. // can never be the last msg in the list
  1049. }
  1050. else // ref is the msg before 'm'
  1051. {
  1052. m->link = ref->link;
  1053. ref->link = m;
  1054. // if ref was the last msg in the trk ...
  1055. if( trk->last == ref )
  1056. trk->last = m; //... then 'm' is now the last msg in the trk
  1057. }
  1058. trk->cnt += 1;
  1059. mfp->msgVDirtyFl = true;
  1060. return kOkMfRC;
  1061. }
  1062. cmMfRC_t cmMidiFileInsertTrackMsg( cmMidiFileH_t h, unsigned trkIdx, const cmMidiTrackMsg_t* msg )
  1063. {
  1064. _cmMidiFile_t* p = _cmMidiFileHandleToPtr(h);
  1065. // validate the track index
  1066. if( trkIdx >= p->trkN )
  1067. return cmErrMsg(&p->err,kInvalidTrkIndexMfRC,"The track index (%i) is invalid.",trkIdx);
  1068. // allocate a new track record
  1069. cmMidiTrackMsg_t* m = (cmMidiTrackMsg_t*)cmLhAllocZ(p->lhH,char,sizeof(cmMidiTrackMsg_t)+msg->byteCnt);
  1070. // fill the track record
  1071. m->uid = p->nextUid++;
  1072. m->atick = msg->atick;
  1073. m->status = msg->status;
  1074. m->metaId = msg->metaId;
  1075. m->trkIdx = trkIdx;
  1076. m->byteCnt = msg->byteCnt;
  1077. memcpy(&m->u,&msg->u,sizeof(msg->u));
  1078. // copy the exernal data
  1079. if( msg->byteCnt > 0 )
  1080. {
  1081. m->u.voidPtr = (m+1);
  1082. memcpy((void*)m->u.voidPtr,msg->u.voidPtr,msg->byteCnt);
  1083. }
  1084. cmMidiTrackMsg_t* m0 = NULL; // msg before insertion
  1085. cmMidiTrackMsg_t* m1 = p->trkV[trkIdx].base; // msg after insertion
  1086. // locate the track record before and after the new msg based on 'atick' value
  1087. for(; m1!=NULL; m1=m1->link)
  1088. {
  1089. if( m1->atick > m->atick )
  1090. {
  1091. if( m0 == NULL )
  1092. p->trkV[trkIdx].base = m;
  1093. else
  1094. m0->link = m;
  1095. m->link = m1;
  1096. break;
  1097. }
  1098. m0 = m1;
  1099. }
  1100. // if the new track record was not inserted then it is the last msg
  1101. if( m1 == NULL )
  1102. {
  1103. assert(m0 == p->trkV[trkIdx].last);
  1104. // link in the new msg
  1105. if( m0 != NULL )
  1106. m0->link = m;
  1107. // the new msg always becomes the last msg
  1108. p->trkV[trkIdx].last = m;
  1109. // if the new msg is the first msg inserted in this track
  1110. if( p->trkV[trkIdx].base == NULL )
  1111. p->trkV[trkIdx].base = m;
  1112. }
  1113. // set the dtick field of the new msg
  1114. if( m0 != NULL )
  1115. {
  1116. assert( m->atick >= m0->atick );
  1117. m->dtick = m->atick - m0->atick;
  1118. }
  1119. // update the dtick field of the msg following the new msg
  1120. if( m1 != NULL )
  1121. {
  1122. assert( m1->atick >= m->atick );
  1123. m1->dtick = m1->atick - m->atick;
  1124. }
  1125. p->trkV[trkIdx].cnt += 1;
  1126. p->msgVDirtyFl = true;
  1127. return kOkMfRC;
  1128. }
  1129. cmMfRC_t cmMidiFileInsertTrackChMsg( cmMidiFileH_t h, unsigned trkIdx, unsigned atick, cmMidiByte_t status, cmMidiByte_t d0, cmMidiByte_t d1 )
  1130. {
  1131. cmMidiTrackMsg_t m;
  1132. cmMidiChMsg_t cm;
  1133. memset(&m,0,sizeof(m));
  1134. memset(&cm,0,sizeof(cm));
  1135. cm.ch = status & 0x0f;
  1136. cm.d0 = d0;
  1137. cm.d1 = d1;
  1138. m.atick = atick;
  1139. m.status = status & 0xf0;
  1140. m.byteCnt = sizeof(cm);
  1141. m.u.chMsgPtr = &cm;
  1142. assert( m.status >= kNoteOffMdId && m.status <= kPbendMdId );
  1143. return cmMidiFileInsertTrackMsg(h,trkIdx,&m);
  1144. }
  1145. cmMfRC_t cmMidFileInsertTrackTempoMsg( cmMidiFileH_t h, unsigned trkIdx, unsigned atick, unsigned bpm )
  1146. {
  1147. cmMidiTrackMsg_t m;
  1148. memset(&m,0,sizeof(m));
  1149. m.atick = atick;
  1150. m.status = kMetaStId;
  1151. m.metaId = kTempoMdId;
  1152. m.u.iVal = 60000000/bpm; // convert BPM to microsPerQN
  1153. return cmMidiFileInsertTrackMsg(h,trkIdx,&m);
  1154. }
  1155. unsigned cmMidiFileSeekUsecs( cmMidiFileH_t h, unsigned long long offsUSecs, unsigned* msgUsecsPtr, unsigned* microsPerTickPtr )
  1156. {
  1157. _cmMidiFile_t* p;
  1158. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  1159. return cmInvalidIdx;
  1160. if( p->msgN == 0 )
  1161. return cmInvalidIdx;
  1162. unsigned mi;
  1163. double microsPerQN = 60000000.0/120.0;
  1164. double microsPerTick = microsPerQN / p->ticksPerQN;
  1165. double accUSecs = 0;
  1166. const cmMidiTrackMsg_t** msgV = _cmMidiFileMsgArray(p);
  1167. for(mi=0; mi<p->msgN; ++mi)
  1168. {
  1169. const cmMidiTrackMsg_t* mp = msgV[mi];
  1170. if( mp->amicro >= offsUSecs )
  1171. break;
  1172. }
  1173. if( mi == p->msgN )
  1174. return cmInvalidIdx;
  1175. if( msgUsecsPtr != NULL )
  1176. *msgUsecsPtr = round(accUSecs - offsUSecs);
  1177. if( microsPerTickPtr != NULL )
  1178. *microsPerTickPtr = round(microsPerTick);
  1179. return mi;
  1180. }
  1181. /*
  1182. 1.Move closest previous tempo msg to begin.
  1183. 2.The first msg in each track must be the first msg >= begin.time
  1184. 3.Remove all msgs > end.time - except the 'endMsg' for each note/pedal that is active at end time.
  1185. */
  1186. unsigned _cmMidiFileIsEndMsg( cmMidiTrackMsg_t* m, cmMidiTrackMsg_t** endMsgArray, unsigned n )
  1187. {
  1188. unsigned i = 0;
  1189. for(; i<n; ++i)
  1190. if( endMsgArray[i] == m )
  1191. return i;
  1192. return cmInvalidIdx;
  1193. }
  1194. bool _cmMidiFileAllEndMsgFound( cmMidiTrackMsg_t** noteMsgArray, unsigned n0, cmMidiTrackMsg_t** pedalMsgArray, unsigned n1 )
  1195. {
  1196. unsigned i=0;
  1197. for(; i<n0; ++i)
  1198. if( noteMsgArray[i] != NULL )
  1199. return false;
  1200. for(i=0; i<n1; ++i)
  1201. if( pedalMsgArray[i] != NULL )
  1202. return false;
  1203. return true;
  1204. }
  1205. double cmMidiFileDurSecs( cmMidiFileH_t h )
  1206. {
  1207. _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
  1208. if( mfp->msgN == 0 )
  1209. return 0;
  1210. const cmMidiTrackMsg_t** msgV = _cmMidiFileMsgArray(mfp);
  1211. return msgV[ mfp->msgN-1 ]->amicro / 1000000.0;
  1212. }
  1213. void _cmMidiFileSetDur( cmMidiTrackMsg_t* m0, cmMidiTrackMsg_t* m1 )
  1214. {
  1215. // calculate the duration of the sounding note
  1216. ((cmMidiChMsg_t*)m0->u.chMsgPtr)->durMicros = m1->amicro - m0->amicro;
  1217. // set the note-off msg pointer
  1218. ((cmMidiChMsg_t*)m0->u.chMsgPtr)->end = m1;
  1219. }
  1220. bool _cmMidiFileCalcNoteDur( cmMidiTrackMsg_t* m0, cmMidiTrackMsg_t* m1, int noteGateFl, int sustainGateFl, bool sostGateFl )
  1221. {
  1222. // if the note is being kept sounding because the key is still depressed,
  1223. // the sustain pedal is down or it is being held by the sostenuto pedal ....
  1224. if( noteGateFl>0 || sustainGateFl>0 || sostGateFl )
  1225. return false; // ... do nothing
  1226. _cmMidiFileSetDur(m0,m1);
  1227. return true;
  1228. }
  1229. void cmMidiFileCalcNoteDurations( cmMidiFileH_t h, unsigned flags )
  1230. {
  1231. _cmMidiFile_t* p;
  1232. bool warningFl = cmIsFlag(flags,kWarningsMfFl);
  1233. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  1234. return;
  1235. if( p->msgN == 0 )
  1236. return;
  1237. unsigned mi = cmInvalidId;
  1238. cmMidiTrackMsg_t* noteM[ kMidiNoteCnt * kMidiChCnt ]; // ptr to note-on or NULL if the note is not sounding
  1239. cmMidiTrackMsg_t* sustV[ kMidiChCnt ]; // ptr to last sustain pedal down msg or NULL if susteain pedal is not down
  1240. cmMidiTrackMsg_t* sostV[ kMidiChCnt ]; // ptr to last sost. pedal down msg or NULL if sost. pedal is not down
  1241. int noteGateM[ kMidiNoteCnt * kMidiChCnt ]; // true if the associated note key is depressed
  1242. bool sostGateM[ kMidiNoteCnt * kMidiChCnt ]; // true if the associated note was active when the sost. pedal went down
  1243. int sustGateV[ kMidiChCnt]; // true if the associated sustain pedal is down
  1244. int sostGateV[ kMidiChCnt]; // true if the associated sostenuto pedal is down
  1245. unsigned i,j;
  1246. unsigned n = 0;
  1247. const cmMidiTrackMsg_t** msgV = _cmMidiFileMsgArray(p);
  1248. // initialize the state tracking variables
  1249. for(i=0; i<kMidiChCnt; ++i)
  1250. {
  1251. sustV[i] = NULL;
  1252. sustGateV[i] = 0;
  1253. sostV[i] = NULL;
  1254. sostGateV[i] = 0;
  1255. for(j=0; j<kMidiNoteCnt; ++j)
  1256. {
  1257. noteM[ i*kMidiNoteCnt + j ] = NULL;
  1258. noteGateM[ i*kMidiNoteCnt + j ] = 0;
  1259. sostGateM[ i*kMidiNoteCnt + j ] = false;
  1260. }
  1261. }
  1262. // for each midi event
  1263. for(mi=0; mi<p->msgN; ++mi)
  1264. {
  1265. cmMidiTrackMsg_t* m = (cmMidiTrackMsg_t*)msgV[mi]; // cast away const
  1266. // verify that time is also incrementing
  1267. assert( mi==0 || (mi>0 && m->amicro >= msgV[mi-1]->amicro) );
  1268. // ignore all non-channel messages
  1269. if( !cmMidiIsChStatus( m->status ) )
  1270. continue;
  1271. cmMidiByte_t ch = m->u.chMsgPtr->ch; // get the midi msg channel
  1272. cmMidiByte_t d0 = m->u.chMsgPtr->d0; // get the midi msg data value
  1273. // if this is a note-on msg
  1274. if( cmMidiFileIsNoteOn(m) )
  1275. {
  1276. unsigned k = ch*kMidiNoteCnt + d0;
  1277. // there should be no existing sounding note instance for this pitch
  1278. if( noteGateM[k] == 0 && noteM[k] != NULL )
  1279. {
  1280. if( warningFl )
  1281. cmErrWarnMsg(&p->err,kMissingNoteOffMfRC,"%i : Missing note-off instance for note on:%s",m->uid,cmMidiToSciPitch(d0,NULL,0));
  1282. if( cmIsFlag(flags,kDropReattacksMfFl) )
  1283. {
  1284. m->flags |= kDropTrkMsgFl;
  1285. n += 1;
  1286. }
  1287. }
  1288. // if this is a re-attack
  1289. if( noteM[k] != NULL )
  1290. noteGateM[k] += 1;
  1291. else // this is a new attack
  1292. {
  1293. noteM[k] = m;
  1294. noteGateM[k] = 1;
  1295. }
  1296. }
  1297. else
  1298. // if this is a note-off msg
  1299. if( cmMidiFileIsNoteOff(m) )
  1300. {
  1301. unsigned k = ch*kMidiNoteCnt + d0;
  1302. cmMidiTrackMsg_t* m0 = noteM[k];
  1303. if( m0 == NULL )
  1304. cmErrWarnMsg(&p->err,kMissingNoteOffMfRC,"%i : Missing note-on instance for note-off:%s",m->uid,cmMidiToSciPitch(d0,NULL,0));
  1305. else
  1306. {
  1307. // a key was released - so it should not already be up
  1308. if( noteGateM[k]==0 )
  1309. cmErrWarnMsg(&p->err,kMissingNoteOffMfRC,"%i : Missing note-on for note-off:%s",m->uid,cmMidiToSciPitch(d0,NULL,0));
  1310. else
  1311. {
  1312. noteGateM[k] -= 1; // update the note gate state
  1313. // update the sounding note status
  1314. if( _cmMidiFileCalcNoteDur(m0, m, noteGateM[k], sustGateV[ch], sostGateM[k]) )
  1315. noteM[k] = NULL;
  1316. }
  1317. }
  1318. }
  1319. else
  1320. // This is a sustain-pedal down msg
  1321. if( cmMidiFileIsSustainPedalDown(m) )
  1322. {
  1323. // if the sustain channel is already down
  1324. if( warningFl && sustGateV[ch] )
  1325. cmErrWarnMsg(&p->err,kSustainPedalMfRC,"%i : The sustain pedal went down twice with no intervening release.",m->uid);
  1326. sustGateV[ch] += 1;
  1327. if( sustV[ch] == NULL )
  1328. sustV[ch] = m;
  1329. }
  1330. else
  1331. // This is a sustain-pedal up msg
  1332. if( cmMidiFileIsSustainPedalUp(m) )
  1333. {
  1334. // if the sustain channel is already up
  1335. if( sustGateV[ch]==0 )
  1336. cmErrWarnMsg(&p->err,kSustainPedalMfRC,"%i : The sustain pedal release message was received with no previous pedal down.",m->uid);
  1337. if( sustGateV[ch] >= 1 )
  1338. {
  1339. sustGateV[ch] -= 1;
  1340. if( sustGateV[ch] == 0 )
  1341. {
  1342. unsigned k = ch*kMidiNoteCnt;
  1343. // for each sounding note on this channel
  1344. for(; k<ch*kMidiNoteCnt+kMidiNoteCnt; ++k)
  1345. if( noteM[k]!=NULL && _cmMidiFileCalcNoteDur(noteM[k], m, noteGateM[k], sustGateV[ch], sostGateM[k]) )
  1346. noteM[k] = NULL;
  1347. if( sustV[ch] != NULL )
  1348. {
  1349. _cmMidiFileSetDur(sustV[ch],m);
  1350. ((cmMidiChMsg_t*)sustV[ch]->u.chMsgPtr)->end = m; // set the pedal-up msg ptr. in the pedal-down msg.
  1351. sustV[ch] = NULL;
  1352. }
  1353. }
  1354. }
  1355. }
  1356. else
  1357. // This is a sostenuto pedal-down msg
  1358. if( cmMidiFileIsSostenutoPedalDown(m) )
  1359. {
  1360. // if the sustain channel is already down
  1361. if( warningFl && sostGateV[ch] )
  1362. cmErrWarnMsg(&p->err,kSostenutoPedalMfRC,"%i : The sostenuto pedal went down twice with no intervening release.",m->uid);
  1363. // record the notes that are active when the sostenuto pedal went down
  1364. unsigned k = ch * kMidiNoteCnt;
  1365. for(i=0; i<kMidiNoteCnt; ++i)
  1366. sostGateM[k+i] = noteGateM[k+i] > 0;
  1367. sostGateV[ch] += 1;
  1368. }
  1369. else
  1370. // This is a sostenuto pedal-up msg
  1371. if( cmMidiFileIsSostenutoPedalUp(m) )
  1372. {
  1373. // if the sustain channel is already up
  1374. if( sostGateV[ch]==0 )
  1375. cmErrWarnMsg(&p->err,kSostenutoPedalMfRC,"%i : The sostenuto pedal release message was received with no previous pedal down.",m->uid);
  1376. if( sostGateV[ch] >= 1 )
  1377. {
  1378. sostGateV[ch] -= 1;
  1379. if( sostGateV[ch] == 0 )
  1380. {
  1381. unsigned k = ch*kMidiNoteCnt;
  1382. // for each note on this channel
  1383. for(; k<ch*kMidiNoteCnt+kMidiNoteCnt; ++k)
  1384. {
  1385. sostGateM[k] = false;
  1386. if( noteM[k]!=NULL && _cmMidiFileCalcNoteDur(noteM[k], m, noteGateM[k], sustGateV[ch], sostGateM[k]) )
  1387. noteM[k] = NULL;
  1388. }
  1389. if( sostV[ch] != NULL )
  1390. {
  1391. _cmMidiFileSetDur(sostV[ch],m);
  1392. ((cmMidiChMsg_t*)sostV[ch]->u.chMsgPtr)->end = m; // set the pedal-up msg ptr. in the pedal-down msg.
  1393. sostV[ch] = NULL;
  1394. }
  1395. }
  1396. }
  1397. }
  1398. } // for each midi file event
  1399. if( warningFl )
  1400. {
  1401. unsigned sustChN = 0; // count of channels where the sustain pedal was left on at the end of the file
  1402. unsigned sostChN = 0; // sostenuto
  1403. unsigned sustInstN = 0; // count of sustain on with no previous sustain off
  1404. unsigned sostInstN = 0; // sostenuto on
  1405. unsigned noteN = 0; // count of notes left on at the end of the file
  1406. unsigned noteInstN = 0; // count of reattacks
  1407. // initialize the state tracking variables
  1408. for(i=0; i<kMidiChCnt; ++i)
  1409. {
  1410. if( sustV[i]!=NULL )
  1411. sustChN += 1;
  1412. sustInstN += sustGateV[i];
  1413. if( sostV[i] != NULL )
  1414. sostChN += 1;
  1415. sostInstN += sostGateV[i] = 0;
  1416. for(j=0; j<kMidiNoteCnt; ++j)
  1417. {
  1418. noteN += noteM[ i*kMidiNoteCnt + j ] != NULL;
  1419. noteInstN += noteGateM[ i*kMidiNoteCnt + j ];
  1420. }
  1421. }
  1422. cmErrWarnMsg(&p->err,kEventTerminationMfRC,"note:%i inst:%i sustain: %i inst: %i sost: %i inst: %i",noteN,noteInstN,sustChN,sustInstN,sostChN,sostInstN);
  1423. }
  1424. // drop
  1425. if( cmIsFlag(flags,kDropReattacksMfFl) )
  1426. _cmMidiFileDrop(p);
  1427. }
  1428. void cmMidiFileSetDelay( cmMidiFileH_t h, unsigned ticks )
  1429. {
  1430. _cmMidiFile_t* p;
  1431. unsigned mi;
  1432. if((p = _cmMidiFileHandleToPtr(h)) == NULL )
  1433. return;
  1434. const cmMidiTrackMsg_t** msgV = _cmMidiFileMsgArray(p);
  1435. if( p->msgN == 0 )
  1436. return;
  1437. for(mi=0; mi<p->msgN; ++mi)
  1438. {
  1439. cmMidiTrackMsg_t* mp = (cmMidiTrackMsg_t*)msgV[mi]; // cast away const
  1440. // locate the first msg which has a non-zero delta tick
  1441. if( mp->dtick > 0 )
  1442. {
  1443. mp->dtick = ticks;
  1444. break;
  1445. }
  1446. }
  1447. }
  1448. unsigned cmMidiFilePackTrackMsgBufByteCount( const cmMidiTrackMsg_t* m )
  1449. { return sizeof(cmMidiTrackMsg_t) + m->byteCnt; }
  1450. cmMidiTrackMsg_t* cmMidiFilePackTrackMsg( const cmMidiTrackMsg_t* m, void* buf, unsigned bufByteCnt )
  1451. {
  1452. unsigned n = sizeof(cmMidiTrackMsg_t) + m->byteCnt;
  1453. if( n < bufByteCnt )
  1454. {
  1455. assert(0);
  1456. return NULL;
  1457. }
  1458. // copy the cmMidiTrackMsg_t into the buffer
  1459. memcpy(buf, m, sizeof(cmMidiTrackMsg_t));
  1460. if( m->byteCnt > 0 )
  1461. {
  1462. // copy any linked data into the buffer
  1463. memcpy(buf + sizeof(cmMidiTrackMsg_t), m->u.voidPtr, m->byteCnt );
  1464. // fixup the linked data ptr
  1465. cmMidiTrackMsg_t* mp = (cmMidiTrackMsg_t*)buf;
  1466. mp->u.voidPtr = buf + sizeof(cmMidiTrackMsg_t);
  1467. }
  1468. return (cmMidiTrackMsg_t*)buf;
  1469. }
  1470. void _cmMidiFilePrintHdr( const _cmMidiFile_t* mfp, cmRpt_t* rpt )
  1471. {
  1472. if( mfp->fn != NULL )
  1473. cmRptPrintf(rpt,"%s ",mfp->fn);
  1474. cmRptPrintf(rpt,"fmt:%i ticksPerQN:%i tracks:%i\n",mfp->fmtId,mfp->ticksPerQN,mfp->trkN);
  1475. cmRptPrintf(rpt," UID trk dtick atick amicro type ch D0 D1\n");
  1476. cmRptPrintf(rpt,"----- --- ---------- ---------- ---------- : ---- --- --- ---\n");
  1477. }
  1478. void _cmMidiFilePrintMsg( cmRpt_t* rpt, const cmMidiTrackMsg_t* tmp )
  1479. {
  1480. cmRptPrintf(rpt,"%5i %3i %10u %10llu %10llu : ",
  1481. tmp->uid,
  1482. tmp->trkIdx,
  1483. tmp->dtick,
  1484. tmp->atick,
  1485. tmp->amicro );
  1486. if( tmp->status == kMetaStId )
  1487. {
  1488. switch( tmp->metaId )
  1489. {
  1490. case kTempoMdId:
  1491. cmRptPrintf(rpt,"%s bpm %i", cmMidiMetaStatusToLabel(tmp->metaId),60000000 / tmp->u.iVal);
  1492. break;
  1493. case kTimeSigMdId:
  1494. cmRptPrintf(rpt,"%s %i %i", cmMidiMetaStatusToLabel(tmp->metaId), tmp->u.timeSigPtr->num,tmp->u.timeSigPtr->den);
  1495. break;
  1496. default:
  1497. cmRptPrintf(rpt,"%s ", cmMidiMetaStatusToLabel(tmp->metaId));
  1498. }
  1499. }
  1500. else
  1501. {
  1502. cmRptPrintf(rpt,"%4s %3i %3i %3i",
  1503. cmMidiStatusToLabel(tmp->status),
  1504. tmp->u.chMsgPtr->ch,
  1505. tmp->u.chMsgPtr->d0,
  1506. tmp->u.chMsgPtr->d1);
  1507. }
  1508. if( cmMidiIsChStatus(tmp->status) && cmMidiIsNoteOn(tmp->status) && (tmp->u.chMsgPtr->d1>0) )
  1509. cmRptPrintf(rpt," %4s ",cmMidiToSciPitch(tmp->u.chMsgPtr->d0,NULL,0));
  1510. cmRptPrintf(rpt,"\n");
  1511. }
  1512. void cmMidiFilePrintMsgs( cmMidiFileH_t h, cmRpt_t* rpt )
  1513. {
  1514. _cmMidiFile_t* p = _cmMidiFileHandleToPtr(h);
  1515. unsigned mi;
  1516. _cmMidiFilePrintHdr(p,rpt);
  1517. const cmMidiTrackMsg_t** msgV = _cmMidiFileMsgArray(p);
  1518. for(mi=0; mi<p->msgN; ++mi)
  1519. {
  1520. const cmMidiTrackMsg_t* mp = msgV[mi];
  1521. if( mp != NULL )
  1522. _cmMidiFilePrintMsg(rpt,mp);
  1523. }
  1524. }
  1525. void cmMidiFilePrintTracks( cmMidiFileH_t h, unsigned trkIdx, cmRpt_t* rpt )
  1526. {
  1527. const _cmMidiFile_t* mfp = _cmMidiFileHandleToPtr(h);
  1528. _cmMidiFilePrintHdr(mfp,rpt);
  1529. int i = trkIdx == cmInvalidIdx ? 0 : trkIdx;
  1530. int n = trkIdx == cmInvalidIdx ? mfp->trkN : trkIdx+1;
  1531. for(; i<n; ++i)
  1532. {
  1533. cmRptPrintf(rpt,"Track:%i\n",i);
  1534. cmMidiTrackMsg_t* tmp = mfp->trkV[i].base;
  1535. while( tmp != NULL )
  1536. {
  1537. _cmMidiFilePrintMsg(rpt,tmp);
  1538. tmp = tmp->link;
  1539. }
  1540. }
  1541. }
  1542. void cmMidiFileTestPrint( void* printDataPtr, const char* fmt, va_list vl )
  1543. { vprintf(fmt,vl); }
  1544. cmMidiFileDensity_t* cmMidiFileNoteDensity( cmMidiFileH_t h, unsigned* cntRef )
  1545. {
  1546. int msgN = cmMidiFileMsgCount(h);
  1547. const cmMidiTrackMsg_t** msgs = cmMidiFileMsgArray(h);
  1548. cmMidiFileDensity_t* dV = cmMemAllocZ(cmMidiFileDensity_t,msgN);
  1549. int i,j,k;
  1550. for(i=0,k=0; i<msgN && k<msgN; ++i)
  1551. if( msgs[i]->status == kNoteOnMdId && msgs[i]->u.chMsgPtr->d1 > 0 )
  1552. {
  1553. dV[k].uid = msgs[i]->uid;
  1554. dV[k].amicro = msgs[i]->amicro;
  1555. // count the number of notes occuring in the time window
  1556. // between this note and one second prior to this note.
  1557. for(j=i; j>=0; --j)
  1558. {
  1559. if( msgs[i]->amicro - msgs[j]->amicro > 1000000 )
  1560. break;
  1561. dV[k].density += 1;
  1562. }
  1563. k += 1;
  1564. }
  1565. if( cntRef != NULL )
  1566. *cntRef = k;
  1567. return dV;
  1568. }
  1569. cmMfRC_t cmMidiFileGenPlotFile( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* outFn )
  1570. {
  1571. cmMfRC_t rc = kOkMfRC;
  1572. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  1573. cmFileH_t fH = cmFileNullHandle;
  1574. unsigned i = 0;
  1575. const cmMidiTrackMsg_t** m = NULL;
  1576. unsigned mN = 0;
  1577. if((rc = cmMidiFileOpen(ctx, &mfH, midiFn )) != kOkMfRC )
  1578. return cmErrMsg(&ctx->err,rc,"The MIDI file object could not be opened from '%s'.",cmStringNullGuard(midiFn));
  1579. _cmMidiFile_t* p = _cmMidiFileHandleToPtr(mfH);
  1580. if( (m = cmMidiFileMsgArray(mfH)) == NULL || (mN = cmMidiFileMsgCount(mfH)) == 0 )
  1581. {
  1582. rc = cmErrMsg(&p->err,kFileFailMfRC,"The MIDI file object appears to be empty.");
  1583. goto errLabel;
  1584. }
  1585. cmMidiFileCalcNoteDurations( mfH, 0 );
  1586. if( cmFileOpen(&fH,outFn,kWriteFileFl,p->err.rpt) != kOkFileRC )
  1587. return cmErrMsg(&p->err,kFileFailMfRC,"Unable to create the file '%s'.",cmStringNullGuard(outFn));
  1588. for(i=0; i<mN; ++i)
  1589. if( (m[i]!=NULL) && cmMidiIsChStatus(m[i]->status) && cmMidiIsNoteOn(m[i]->status) && (m[i]->u.chMsgPtr->d1>0) )
  1590. 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));
  1591. errLabel:
  1592. cmMidiFileClose(&mfH);
  1593. cmFileClose(&fH);
  1594. return rc;
  1595. }
  1596. cmMfRC_t cmMidiFileGenSvgFile( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* outSvgFn, const cmChar_t* cssFn, bool standAloneFl, bool panZoomFl )
  1597. {
  1598. cmMfRC_t rc = kOkMfRC;
  1599. cmSvgH_t svgH = cmSvgNullHandle;
  1600. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  1601. unsigned msgN = 0;
  1602. const cmMidiTrackMsg_t** msgs = NULL;
  1603. unsigned noteHeight = 10;
  1604. double micros_per_sec = 1000.0;
  1605. unsigned i;
  1606. if((rc = cmMidiFileOpen(ctx,&mfH,midiFn)) != kOkMfRC )
  1607. {
  1608. rc = cmErrMsg(&ctx->err,rc,"Unable to open the MIDI file '%s'.",cmStringNullGuard(midiFn));
  1609. goto errLabel;
  1610. }
  1611. cmMidiFileCalcNoteDurations( mfH, 0 );
  1612. msgN = cmMidiFileMsgCount(mfH);
  1613. msgs = cmMidiFileMsgArray(mfH);
  1614. if( cmSvgWriterAlloc(ctx,&svgH) != kOkSvgRC )
  1615. {
  1616. rc = cmErrMsg(&ctx->err,kSvgFailMfRC,"Unable to create the MIDI SVG output file '%s'.",cmStringNullGuard(outSvgFn));
  1617. goto errLabel;
  1618. }
  1619. for(i=0; i<msgN && rc==kOkMfRC; ++i)
  1620. if( msgs[i]->status == kNoteOnMdId && msgs[i]->u.chMsgPtr->d1 > 0 )
  1621. {
  1622. const cmMidiTrackMsg_t* m = msgs[i];
  1623. if( cmSvgWriterRect(svgH, m->amicro/micros_per_sec, m->u.chMsgPtr->d0 * noteHeight, m->u.chMsgPtr->durMicros/micros_per_sec, noteHeight-1, "note" ) != kOkSvgRC )
  1624. rc = kSvgFailMfRC;
  1625. const cmChar_t* t0 = cmMidiToSciPitch(m->u.chMsgPtr->d0,NULL,0);
  1626. if( cmSvgWriterText(svgH, (m->amicro + (m->u.chMsgPtr->durMicros/2)) / micros_per_sec, m->u.chMsgPtr->d0 * noteHeight, t0, "text" ) != kOkSvgRC )
  1627. rc = kSvgFailMfRC;
  1628. }
  1629. if( rc != kOkMfRC )
  1630. {
  1631. cmErrMsg(&ctx->err,rc,"SVG Shape insertion failed.");
  1632. goto errLabel;
  1633. }
  1634. unsigned dN = 0;
  1635. cmMidiFileDensity_t* dV = cmMidiFileNoteDensity( mfH, &dN );
  1636. double t0 = 0;
  1637. double y0 = 64.0;
  1638. cmChar_t* tx = NULL;
  1639. for(i=0; i<dN; ++i)
  1640. {
  1641. const cmMidiTrackMsg_t* m;
  1642. if((m = _cmMidiFileUidToMsg( _cmMidiFileHandleToPtr(mfH), dV[i].uid )) == NULL )
  1643. rc = cmErrMsg(&ctx->err,kUidNotFoundMfRC,"The MIDI msg form UID:%i was not found.",dV[i].uid);
  1644. else
  1645. {
  1646. double t1 = m->amicro / micros_per_sec;
  1647. double y1 = dV[i].density * noteHeight;
  1648. cmSvgWriterLine(svgH, t0, y0, t1, y1, "density" );
  1649. cmSvgWriterText(svgH, t1, y1, tx = cmTsPrintfP(tx,"%i",dV[i].density),"dtext");
  1650. t0 = t1;
  1651. y0 = y1;
  1652. }
  1653. }
  1654. cmMemFree(dV);
  1655. cmMemFree(tx);
  1656. if( rc == kOkMfRC )
  1657. if( cmSvgWriterWrite(svgH,cssFn,outSvgFn, standAloneFl, panZoomFl) != kOkSvgRC )
  1658. rc = cmErrMsg(&ctx->err,kSvgFailMfRC,"SVG file write to '%s' failed.",cmStringNullGuard(outSvgFn));
  1659. errLabel:
  1660. cmMidiFileClose(&mfH);
  1661. cmSvgWriterFree(&svgH);
  1662. return rc;
  1663. }
  1664. cmMfRC_t cmMidiFileReport( cmCtx_t* ctx, const cmChar_t* midiFn, const cmChar_t* outTextFn )
  1665. {
  1666. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  1667. cmRptFileH_t rptH = cmRptFileNullHandle;
  1668. cmMfRC_t rc;
  1669. if((rc = cmMidiFileOpen(ctx,&mfH,midiFn)) != kOkMfRC )
  1670. {
  1671. rc = cmErrMsg(&ctx->err,rc,"Unable to open the MIDI file: %s\n",cmStringNullGuard(midiFn));
  1672. goto errLabel;
  1673. }
  1674. if(( rc = cmRptFileCreate(ctx,&rptH, outTextFn, NULL )) != kOkRfRC )
  1675. {
  1676. rc = cmErrMsg(&ctx->err,rc,"Unable to open the report file: %s\n",cmStringNullGuard(outTextFn));
  1677. goto errLabel;
  1678. }
  1679. cmMidiFilePrintMsgs(mfH, cmRptFileRpt(rptH) );
  1680. errLabel:
  1681. cmRptFileClose(&rptH);
  1682. cmMidiFileClose(&mfH);
  1683. return rc;
  1684. }
  1685. void cmMidiFilePrintControlNumbers( cmCtx_t* ctx, const char* fn )
  1686. {
  1687. cmMidiFileH_t h = cmMidiFileNullHandle;
  1688. cmMfRC_t rc;
  1689. if((rc = cmMidiFileOpen(ctx, &h, fn )) != kOkMfRC )
  1690. {
  1691. cmErrMsg(&ctx->err,rc,"MIDI file open failed on '%s'.",fn);
  1692. goto errLabel;
  1693. }
  1694. const cmMidiTrackMsg_t** mm;
  1695. unsigned n = cmMidiFileMsgCount(h);
  1696. if((mm = cmMidiFileMsgArray(h)) != NULL )
  1697. {
  1698. unsigned j;
  1699. for(j=0; j<n; ++j)
  1700. {
  1701. const cmMidiTrackMsg_t* m = mm[j];
  1702. if( m->status == kCtlMdId && m->u.chMsgPtr->d0==66 )
  1703. printf("%i %i\n",m->u.chMsgPtr->d0,m->u.chMsgPtr->d1);
  1704. }
  1705. }
  1706. errLabel:
  1707. cmMidiFileClose(&h);
  1708. }
  1709. void cmMidiFileTest( const char* fn, cmCtx_t* ctx )
  1710. {
  1711. cmMfRC_t rc;
  1712. cmMidiFileH_t h = cmMidiFileNullHandle;
  1713. if((rc = cmMidiFileOpen(ctx,&h,fn)) != kOkMfRC )
  1714. {
  1715. printf("Error:%i Unable to open the cmMidi file: %s\n",rc,fn);
  1716. return;
  1717. }
  1718. cmMidiFileCalcNoteDurations( h, 0 );
  1719. if( 1 )
  1720. {
  1721. //cmMidiFileTickToMicros( h );
  1722. //cmMidiFileTickToSamples(h,96000,false);
  1723. cmMidiFilePrintMsgs(h,&ctx->rpt);
  1724. }
  1725. if( 0 )
  1726. {
  1727. //cmMidiFilePrint(h,cmMidiFileTrackCount(h)-1,&ctx->rpt);
  1728. //cmMidiFilePrint(h,cmInvalidIdx,&ctx->rpt);
  1729. cmMidiFilePrintControlNumbers(ctx, fn );
  1730. }
  1731. if( 0 )
  1732. {
  1733. printf("Tracks:%i\n",cmMidiFileTrackCount(h));
  1734. unsigned i = 0;
  1735. for(i=0; i<cmMidiFileMsgCount(h); ++i)
  1736. {
  1737. cmMidiTrackMsg_t* tmp = (cmMidiTrackMsg_t*)cmMidiFileMsgArray(h)[i];
  1738. if( tmp->status==kMetaStId && tmp->metaId == kTempoMdId )
  1739. {
  1740. double bpm = 60000000.0/tmp->u.iVal;
  1741. printf("Tempo:%i %f\n",tmp->u.iVal,bpm);
  1742. tmp->u.iVal = floor( 60000000.0/69.0 );
  1743. break;
  1744. }
  1745. }
  1746. cmMidiFileWrite(h,"/home/kevin/temp/test0.mid");
  1747. }
  1748. cmMidiFileClose(&h);
  1749. }