libcm is a C development framework with an emphasis on audio signal processing applications.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cmMidiFilePlay.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #include <sys/time.h> // gettimeofday()
  2. #include "cmPrefix.h"
  3. #include "cmGlobal.h"
  4. #include "cmRpt.h"
  5. #include "cmErr.h"
  6. #include "cmCtx.h"
  7. #include "cmMem.h"
  8. #include "cmMallocDebug.h"
  9. #include "cmFile.h"
  10. #include "cmTime.h"
  11. #include "cmMidi.h"
  12. #include "cmMidiPort.h"
  13. #include "cmMidiFile.h"
  14. #include "cmMidiFilePlay.h"
  15. #include "cmThread.h" // cmSleepUs()
  16. #include "cmTime.h"
  17. typedef struct
  18. {
  19. cmErr_t err;
  20. cmCtx_t ctx;
  21. cmMfpCallback_t cbFunc;
  22. void* userCbPtr;
  23. void* printDataPtr;
  24. unsigned memBlockByteCnt;
  25. cmMidiFileH_t mfH; // midi file handle
  26. bool closeFileFl; // true mfH should be closed when this midi file player is closed
  27. unsigned ticksPerQN; // global for file
  28. unsigned microsPerTick; // set via tempo
  29. unsigned etime; // usecs elapsed since transmitting prev msg
  30. unsigned mtime; // usecs to wait before transmitting next msg
  31. unsigned msgN; // count of pointers in msgV[]
  32. unsigned msgIdx; // index into msgV[] of next msg to transmit
  33. const cmMidiTrackMsg_t** msgV; // array of msg pointers
  34. } cmMfp_t;
  35. cmMfpH_t cmMfpNullHandle = cmSTATIC_NULL_HANDLE;
  36. #define _cmMfpError( mfp, rc ) _cmMfpOnError(mfp, rc, __LINE__,__FILE__,__FUNCTION__ )
  37. // note: mfp may be NULL
  38. cmMfpRC_t _cmMfpOnError( cmMfp_t* mfp, cmMfpRC_t rc, int line, const char* fn, const char* func )
  39. {
  40. return cmErrMsg(&mfp->err,rc,"rc:%i %i %s %s\n",rc,line,func,fn);
  41. }
  42. cmMfp_t* _cmMfpHandleToPtr( cmMfpH_t h )
  43. {
  44. cmMfp_t* p = (cmMfp_t*)h.h;
  45. assert(p != NULL);
  46. return p;
  47. }
  48. void _cmMfpUpdateMicrosPerTick( cmMfp_t* mfp, unsigned microsPerQN )
  49. {
  50. mfp->microsPerTick = microsPerQN / mfp->ticksPerQN;
  51. printf("microsPerTick: %i bpm:%i ticksPerQN:%i\n", mfp->microsPerTick,microsPerQN,mfp->ticksPerQN);
  52. }
  53. cmMfpRC_t cmMfpCreate( cmMfpH_t* hp, cmMfpCallback_t cbFunc, void* userCbPtr, cmCtx_t* ctx )
  54. {
  55. cmMfp_t* p = cmMemAllocZ( cmMfp_t, 1 );
  56. cmErrSetup(&p->err,&ctx->rpt,"MIDI File Player");
  57. p->ctx = *ctx;
  58. p->cbFunc = cbFunc;
  59. p->userCbPtr = userCbPtr;
  60. p->mfH.h = NULL;
  61. p->closeFileFl = false;
  62. p->ticksPerQN = 0;
  63. p->microsPerTick = 0;
  64. p->etime = 0;
  65. p->msgN = 0;
  66. p->msgV = NULL;
  67. p->msgIdx = 0;
  68. hp->h = p;
  69. return kOkMfpRC;
  70. }
  71. cmMfpRC_t cmMfpDestroy( cmMfpH_t* hp )
  72. {
  73. if( hp == NULL )
  74. return kOkMfpRC;
  75. if( cmMfpIsValid(*hp) )
  76. {
  77. cmMfp_t* p = _cmMfpHandleToPtr(*hp);
  78. if( cmMidiFileIsNull(p->mfH)==false && p->closeFileFl==true )
  79. cmMidiFileClose(&p->mfH);
  80. cmMemFree(p);
  81. hp->h = NULL;
  82. }
  83. return kOkMfpRC;
  84. }
  85. bool cmMfpIsValid( cmMfpH_t h )
  86. { return h.h != NULL; }
  87. cmMfpRC_t cmMfpLoadFile( cmMfpH_t h, const char* fn )
  88. {
  89. cmMfpRC_t rc = kOkMfpRC;
  90. cmMfp_t* p = _cmMfpHandleToPtr(h);
  91. cmMidiFileH_t mfH = cmMidiFileNullHandle;
  92. if((rc = cmMidiFileOpen( fn, &mfH, &p->ctx )) != kOkMfRC )
  93. return _cmMfpError(p,kFileOpenFailMfpRC);
  94. if((rc= cmMfpLoadHandle( h, mfH )) == kOkMfpRC )
  95. p->closeFileFl = true;
  96. return rc;
  97. }
  98. cmMfpRC_t cmMfpLoadHandle( cmMfpH_t h, cmMidiFileH_t mfH )
  99. {
  100. cmMfp_t* p = _cmMfpHandleToPtr(h);
  101. // if a file has already been assigned to this player
  102. if( (cmMidiFileIsNull(p->mfH) == false) && p->closeFileFl)
  103. {
  104. // close the existing file
  105. cmMidiFileClose(&p->mfH);
  106. }
  107. // get the count of msg's in the new midi file
  108. if((p->msgN = cmMidiFileMsgCount(mfH)) == cmInvalidCnt )
  109. return _cmMfpError(p,kInvalidFileMfpRC);
  110. // get a pointer to the first mesage
  111. if((p->msgV = cmMidiFileMsgArray(mfH)) == NULL )
  112. return _cmMfpError(p,kInvalidFileMfpRC);
  113. // get the count of ticks per qn
  114. if((p->ticksPerQN = cmMidiFileTicksPerQN( mfH )) == 0 )
  115. return _cmMfpError(p,kSmpteTickNotImplMfpRC);
  116. // set the initial tempo to 120
  117. _cmMfpUpdateMicrosPerTick(p,60000000/120);
  118. p->msgIdx = 0;
  119. p->mfH = mfH;
  120. p->etime = 0;
  121. p->mtime = 0;
  122. p->closeFileFl= false;
  123. //if( p->msgIdx > 0 )
  124. // p->mtime = p->msgV[0]->tick * p->microsPerTick;
  125. return kOkMfpRC;
  126. }
  127. cmMfpRC_t cmMfpSeek( cmMfpH_t h, unsigned offsUsecs )
  128. {
  129. cmMfp_t* p = _cmMfpHandleToPtr(h);
  130. unsigned msgOffsUsecs = 0;
  131. unsigned msgIdx;
  132. unsigned newMicrosPerTick;
  133. // if the requested offset is past the end of the file then return EOF
  134. if((msgIdx = cmMidiFileSeekUsecs( p->mfH, offsUsecs, &msgOffsUsecs, &newMicrosPerTick )) == cmInvalidIdx )
  135. {
  136. p->msgIdx = p->msgN;
  137. return _cmMfpError(p,kEndOfFileMfpRC);
  138. }
  139. if( msgIdx < p->msgIdx )
  140. p->msgIdx = 0;
  141. p->mtime = msgOffsUsecs;
  142. p->etime = 0;
  143. p->microsPerTick = newMicrosPerTick;
  144. p->msgIdx = msgIdx;
  145. assert(p->mtime >= 0);
  146. return kOkMfpRC;
  147. }
  148. // p 0 1 n 2
  149. // v v v v v
  150. // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  151. // 012345678901234567890123456780
  152. // 0 1 2
  153. //
  154. // p = 3 = prev msg sent
  155. // n = 19 = next msg to send
  156. // 0 = 6 = call to cmMfpClock()
  157. // 1 = 12 = call to cmMfpClock()
  158. // 2 = 22 = call to cmMfpClock()
  159. //
  160. // dusecs etime mtime
  161. // 0 n/a 3 13
  162. // 1 6 9 7
  163. // 2 10 19 -3
  164. //
  165. cmMfpRC_t cmMfpClock( cmMfpH_t h, unsigned dusecs )
  166. {
  167. cmMfp_t* p = _cmMfpHandleToPtr(h);
  168. if( p->msgIdx >= p->msgN )
  169. return kEndOfFileMfpRC;
  170. // get a pointer to the next msg to send
  171. const cmMidiTrackMsg_t* mp = p->msgV[p->msgIdx];
  172. // p->etime is the interval of time between when the last msg was
  173. // sent and the end of the time window for this mfpClock() cycle
  174. p->etime += dusecs;
  175. //printf("init e:%i d:%i\n",p->etime, p->mtime);
  176. // if the elapsed time (etime) since the last msg is greater or equal
  177. // to the delta time to the next msg (mtime)
  178. while( p->etime >= p->mtime )
  179. {
  180. //printf("e:%i d:%i\n",p->etime, p->mtime);
  181. if( mp->status == kMetaStId && mp->metaId == kTempoMdId )
  182. _cmMfpUpdateMicrosPerTick(p,mp->u.iVal );
  183. p->cbFunc( p->userCbPtr, p->mtime, mp );
  184. unsigned atick0 = mp->atick;
  185. ++(p->msgIdx);
  186. if( p->msgIdx >= p->msgN )
  187. break;
  188. // get the next msg to send
  189. mp = p->msgV[p->msgIdx];
  190. assert( mp->atick >= atick0 );
  191. unsigned dtick = mp->atick - atick0;
  192. // we probably went past the actual mtime - so update etime
  193. // with the delta usecs from the msg just sent and the current time
  194. p->etime -= p->mtime;
  195. // calc the delta usecs from the message just sent to the next msg to send
  196. //p->mtime = (mp->tick - p->msgV[p->msgIdx-1]->tick) * p->microsPerTick;
  197. p->mtime = dtick * p->microsPerTick;
  198. }
  199. return p->msgIdx >= p->msgN ? kEndOfFileMfpRC : kOkMfpRC;
  200. }
  201. void mfpPrint( void* userDataPtr, const char* fmt, va_list vl )
  202. {
  203. vprintf(fmt,vl);
  204. }
  205. // this assumes that the seconds have been normalized to a recent start time
  206. // so as to avoid overflow
  207. unsigned _cmMfpElapsedMicroSecs( const struct timespec* t0, const struct timespec* t1 )
  208. {
  209. // convert seconds to usecs
  210. long u0 = t0->tv_sec * 1000000;
  211. long u1 = t1->tv_sec * 1000000;
  212. // convert nanoseconds to usec
  213. u0 += t0->tv_nsec / 1000;
  214. u1 += t1->tv_nsec / 1000;
  215. // take diff between t1 and t0
  216. return u1 - u0;
  217. }
  218. void _cmMfpTestTimer()
  219. {
  220. useconds_t suspendUsecs = 15 * 1000;
  221. struct timespec t0,t1,t2;
  222. unsigned accum = 0;
  223. unsigned i;
  224. unsigned n = 4000;
  225. // t0 will be the base time which all other times will be
  226. // set relative to.
  227. cmTimeGet(&t0);
  228. t2 = t0;
  229. t2.tv_sec = 0;
  230. for(i=0; i<n; ++i)
  231. {
  232. cmSleepUs(suspendUsecs);
  233. cmTimeGet(&t1);
  234. t1.tv_sec -= t0.tv_sec;
  235. unsigned d0usec = _cmMfpElapsedMicroSecs(&t0,&t1);
  236. unsigned d1usec = _cmMfpElapsedMicroSecs(&t2,&t1);
  237. accum += d1usec;
  238. if( i == n-1 )
  239. printf("%i %i %i\n",d0usec,d1usec,accum);
  240. t2 = t1;
  241. }
  242. }
  243. // midi file player callback test function
  244. void _cmMfpCallbackTest( void* userCbPtr, unsigned dmicros, const cmMidiTrackMsg_t* msgPtr )
  245. {
  246. if( kNoteOffMdId <= msgPtr->status && msgPtr->status <= kPbendMdId )
  247. cmMpDeviceSend( 0, 0, msgPtr->status+msgPtr->u.chMsgPtr->ch, msgPtr->u.chMsgPtr->d0,msgPtr->u.chMsgPtr->d1);
  248. //printf("%i 0x%x 0x%x %i\n",msgPtr->tick,msgPtr->status,msgPtr->metaId,msgPtr->trkIdx);
  249. }
  250. // midi port callback test function
  251. void _cmMpCallbackTest( const cmMidiPacket_t* pktArray, unsigned pktCnt )
  252. {}
  253. cmMfpRC_t cmMfpTest( const char* fn, cmCtx_t* ctx )
  254. {
  255. cmMfpH_t mfpH = cmMfpNullHandle;
  256. cmMfpRC_t rc;
  257. useconds_t suspendUsecs = 15 * 1000;
  258. struct timespec t0,t1,base;
  259. //unsigned i;
  260. //unsigned n = 4000;
  261. unsigned mdParserBufByteCnt = 1024;
  262. printf("Initializing MIDI Devices...\n");
  263. cmMpInitialize( ctx, _cmMpCallbackTest, NULL, mdParserBufByteCnt,"app" );
  264. //mdReport();
  265. printf("Creating Player...\n");
  266. if((rc = cmMfpCreate( &mfpH, _cmMfpCallbackTest, NULL, ctx )) != kOkMfpRC )
  267. return rc;
  268. printf("Loading MIDI file...\n");
  269. if((rc = cmMfpLoadFile( mfpH, fn )) != kOkMfpRC )
  270. goto errLabel;
  271. if((rc = cmMfpSeek( mfpH, 60 * 1000000 )) != kOkMfpRC )
  272. goto errLabel;
  273. cmTimeGet(&base);
  274. t0 = base;
  275. t0.tv_sec = 0;
  276. //for(i=0; i<n; ++i)
  277. while(rc != kEndOfFileMfpRC)
  278. {
  279. cmSleepUs(suspendUsecs);
  280. cmTimeGet(&t1);
  281. t1.tv_sec -= base.tv_sec;
  282. unsigned dusecs = _cmMfpElapsedMicroSecs(&t0,&t1);
  283. rc = cmMfpClock( mfpH, dusecs );
  284. //printf("%i %i\n",dusecs,rc);
  285. t0 = t1;
  286. }
  287. errLabel:
  288. cmMfpDestroy(&mfpH);
  289. cmMpFinalize();
  290. return rc;
  291. }
  292. //------------------------------------------------------------------------------------------------------------
  293. #include "cmFloatTypes.h"
  294. #include "cmComplexTypes.h"
  295. #include "cmLinkedHeap.h"
  296. #include "cmSymTbl.h"
  297. #include "cmAudioFile.h"
  298. #include "cmProcObj.h"
  299. #include "cmProcTemplateMain.h"
  300. #include "cmVectOps.h"
  301. #include "cmProc.h"
  302. #include "cmProc2.h"
  303. enum
  304. {
  305. kOkMfptRC = cmOkRC,
  306. kMfpFailMfptRC,
  307. kAudioFileFailMfptRC,
  308. kProcObjFailMfptRC
  309. };
  310. typedef struct
  311. {
  312. cmErr_t* err;
  313. cmMidiSynth* msp;
  314. } _cmMfpTest2CbData_t;
  315. // Called by the MIDI file player to send a msg to the MIDI synth.
  316. void _cmMfpCb( void* userCbPtr, unsigned dmicros, const cmMidiTrackMsg_t* msgPtr )
  317. {
  318. if( kNoteOffMdId <= msgPtr->status && msgPtr->status <= kPbendMdId )
  319. {
  320. cmMidiPacket_t pkt;
  321. cmMidiMsg msg;
  322. _cmMfpTest2CbData_t* d = (_cmMfpTest2CbData_t*)userCbPtr;
  323. msg.timeStamp.tv_sec = 0;
  324. msg.timeStamp.tv_nsec = 0;
  325. msg.status = msgPtr->status + msgPtr->u.chMsgPtr->ch;
  326. msg.d0 = msgPtr->u.chMsgPtr->d0;
  327. msg.d1 = msgPtr->u.chMsgPtr->d1;
  328. pkt.cbDataPtr = NULL;
  329. pkt.devIdx = cmInvalidIdx;
  330. pkt.portIdx = cmInvalidIdx;
  331. pkt.msgArray = &msg;
  332. pkt.sysExMsg = NULL;
  333. pkt.msgCnt = 1;
  334. if( cmMidiSynthOnMidi( d->msp, &pkt, 1 ) != cmOkRC )
  335. cmErrMsg(d->err,kProcObjFailMfptRC,"Synth. MIDI receive failed.");
  336. }
  337. }
  338. // Called by the MIDI synth to send a msg to the voice bank.
  339. int _cmMidiSynthCb( struct cmMidiVoice_str* voicePtr, unsigned sel, cmSample_t* outChArray[], unsigned outChCnt )
  340. {
  341. return cmWtVoiceBankExec( ((cmWtVoiceBank*)voicePtr->pgm.cbDataPtr), voicePtr, sel, outChArray, outChCnt );
  342. }
  343. // BUG BUG BUG: THIS FUNCTION IS NOT TESTED!!!!!
  344. cmRC_t cmMfpTest2( const char* midiFn, const char* audioFn, cmCtx_t* ctx )
  345. {
  346. cmRC_t rc = kOkMfptRC;
  347. cmMfpH_t mfpH = cmMfpNullHandle;
  348. _cmMfpTest2CbData_t cbData;
  349. cmErr_t err;
  350. cmAudioFileH_t afH = cmNullAudioFileH;
  351. cmRC_t afRC = kOkAfRC;
  352. double afSrate = 44100;
  353. unsigned afBits = 16;
  354. unsigned afChCnt = 1;
  355. cmCtx* cctx;
  356. cmMidiSynth* msp;
  357. cmWtVoiceBank* vbp;
  358. unsigned msPgmCnt = 127;
  359. cmMidiSynthPgm msPgmArray[ msPgmCnt ];
  360. unsigned msVoiceCnt = 36;
  361. unsigned procSmpCnt = 64;
  362. unsigned i;
  363. cmErrSetup(&err,&ctx->rpt,"MFP Test 2");
  364. // create the MIDI file player
  365. if( cmMfpCreate(&mfpH, _cmMfpCb, &cbData, ctx ) != kOkMfpRC )
  366. return cmErrMsg(&err,kMfpFailMfptRC,"MIDI file player create failed.");
  367. // create an output audio file
  368. if( cmAudioFileIsValid( afH = cmAudioFileNewCreate(audioFn, afSrate, afBits, afChCnt, &afRC, &ctx->rpt))==false)
  369. {
  370. rc = cmErrMsg(&err,kAudioFileFailMfptRC,"The audio file create failed.");
  371. goto errLabel;
  372. }
  373. // load the midi file into the player
  374. if( cmMfpLoadFile( mfpH, midiFn ) != kOkMfpRC )
  375. {
  376. rc = cmErrMsg(&err,kMfpFailMfptRC,"MIDI file load failed.");
  377. goto errLabel;
  378. }
  379. // create the proc obj context
  380. if((cctx = cmCtxAlloc(NULL, &ctx->rpt, cmLHeapNullHandle, cmSymTblNullHandle )) == NULL)
  381. {
  382. rc = cmErrMsg(&err,kProcObjFailMfptRC,"cmCtx allocate failed.");
  383. goto errLabel;
  384. }
  385. // create the voice bank
  386. if((vbp = cmWtVoiceBankAlloc(cctx, NULL, afSrate, procSmpCnt, msVoiceCnt, afChCnt )) == NULL)
  387. {
  388. rc = cmErrMsg(&err,kProcObjFailMfptRC,"WT voice bank allocate failed.");
  389. goto errLabel;
  390. }
  391. // a MIDI synth
  392. if((msp = cmMidiSynthAlloc(cctx, NULL, msPgmArray, msPgmCnt, msVoiceCnt, procSmpCnt, afChCnt, afSrate )) == NULL )
  393. {
  394. rc = cmErrMsg(&err,kProcObjFailMfptRC,"MIDI synth allocate failed.");
  395. goto errLabel;
  396. }
  397. cbData.msp = msp;
  398. cbData.err = &err;
  399. // load all of the the MIDI pgm recds with the same settings
  400. for(i=0; i<msPgmCnt; ++i)
  401. {
  402. msPgmArray[i].pgm = i;
  403. msPgmArray[i].cbPtr = _cmMidiSynthCb; // Call this function to update voices using this pgm
  404. msPgmArray[i].cbDataPtr = vbp; // Voice bank containing the voice states.
  405. }
  406. unsigned dusecs = floor((double)procSmpCnt * 1000000. / afSrate);
  407. while(rc != kEndOfFileMfpRC)
  408. {
  409. // update the MFP's current time and call _cmMfpCb() for MIDI msgs whose time has elapsed
  410. rc = cmMfpClock( mfpH, dusecs );
  411. // check for MFP errors
  412. if(rc!=kOkMfpRC && rc!=kEndOfFileMfpRC)
  413. {
  414. cmErrMsg(&err,kMfpFailMfptRC,"MIDI file player exec failed.");
  415. goto errLabel;
  416. }
  417. // generate audio based on the current state of the synth voices
  418. if( cmMidiSynthExec(msp, NULL, 0 ) != cmOkRC )
  419. {
  420. cmErrMsg(&err,kProcObjFailMfptRC,"MIDI synth exec. failed.");
  421. goto errLabel;
  422. }
  423. // write the last frame of synth. generated audio to the output file
  424. if( cmAudioFileWriteSample(afH, procSmpCnt, msp->outChCnt, msp->outChArray ) != kOkAfRC )
  425. {
  426. cmErrMsg(&err,kProcObjFailMfptRC,"Audio file write failed.");
  427. goto errLabel;
  428. }
  429. }
  430. errLabel:
  431. if( cmMidiSynthFree(&msp) != cmOkRC )
  432. cmErrMsg(&err,kProcObjFailMfptRC,"MIDI synth. free failed.");
  433. if( cmWtVoiceBankFree(&vbp) != cmOkRC )
  434. cmErrMsg(&err,kProcObjFailMfptRC,"WT voice free failed.");
  435. if( cmCtxFree(&cctx) != cmOkRC )
  436. cmErrMsg(&err,kProcObjFailMfptRC,"cmCtx free failed.");
  437. if( cmAudioFileDelete(&afH) )
  438. cmErrMsg(&err,kAudioFileFailMfptRC,"The audio file close failed.");
  439. if( cmMfpDestroy(&mfpH) != kOkMfpRC )
  440. cmErrMsg(&err,kMfpFailMfptRC,"MIDI file player destroy failed.");
  441. return rc;
  442. }