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.

cmMidiOsx.c 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. #include <Carbon/Carbon.h>
  2. #include <CoreAudio/CoreAudio.h>
  3. #include <CoreMIDI/CoreMIDI.h>
  4. #include <mach/mach_time.h>
  5. #include "cmPrefix.h"
  6. #include "cmGlobal.h"
  7. #include "cmRpt.h"
  8. #include "cmErr.h"
  9. #include "cmCtx.h"
  10. #include "cmMem.h"
  11. #include "cmMallocDebug.h"
  12. #include "cmMidi.h"
  13. #include "cmMidiPort.h"
  14. typedef struct
  15. {
  16. bool inputFl;
  17. char* nameStr;
  18. SInt32 uid;
  19. MIDIEndpointRef epr;
  20. cmMpParserH_t parserH;
  21. double prevMicroSecs;
  22. } cmMpPort;
  23. typedef struct
  24. {
  25. char* nameStr;
  26. unsigned iPortCnt;
  27. cmMpPort* iPortArray;
  28. unsigned oPortCnt;
  29. cmMpPort* oPortArray;
  30. } cmMpDev;
  31. typedef struct
  32. {
  33. cmErr_t err;
  34. unsigned devCnt;
  35. cmMpDev* devArray;
  36. cmMpCallback_t cbFunc;
  37. void* cbDataPtr;
  38. MIDIClientRef clientRef;
  39. MIDIPortRef inPortRef;
  40. MIDIPortRef outPortRef;
  41. mach_timebase_info_data_t timeBaseInfo;
  42. } cmMpRoot;
  43. cmMpRoot _cmMpRoot;
  44. cmMpRoot* _cmMpRtPtr = NULL;
  45. cmMpRC_t _cmMpError( cmErr_t* errPtr, cmMpRC_t rc, OSStatus err, const char* fmt, ... )
  46. {
  47. va_list vl;
  48. unsigned n0 = vsnprintf(NULL,0,fmt,vl);
  49. unsigned n1 = 0;
  50. unsigned n2 = 0;
  51. const char* fmt2 = "OS Status:%i";
  52. va_start(vl,fmt);
  53. if( err != noErr )
  54. n1 = snprintf(NULL,0,fmt2,err);
  55. unsigned bufCharCnt = n0 + n1;
  56. char buf[ bufCharCnt + 1];
  57. va_end(vl);
  58. va_start(vl,fmt);
  59. vsnprintf(buf,n0,fmt,vl);
  60. n2 = strlen(buf);
  61. snprintf(buf+n2,bufCharCnt-n2,fmt2,err);
  62. buf[bufCharCnt]=0;
  63. cmErrMsg(errPtr,rc,buf);
  64. va_end(vl);
  65. return rc;
  66. }
  67. char* _cmMpAllocStringProperty( cmErr_t* errPtr, MIDIObjectRef obj, CFStringRef propId )
  68. {
  69. OSStatus err;
  70. CFStringRef cfStrRef = NULL;
  71. char* strPtr = NULL;
  72. if(( err = MIDIObjectGetStringProperty(obj,propId,&cfStrRef)) != noErr )
  73. {
  74. _cmMpError(errPtr,kCfStringErrMpRC,err,"Get property failed.");
  75. return NULL;
  76. }
  77. unsigned charCnt = CFStringGetLength( cfStrRef ) + 1;
  78. if( charCnt > 0 )
  79. {
  80. strPtr = cmMemAllocZ( char, charCnt );
  81. if( !CFStringGetCString( cfStrRef, strPtr, charCnt, kCFStringEncodingASCII))
  82. {
  83. _cmMpError(errPtr,kCfStringErrMpRC,noErr,"CFstring to cstring failed.");
  84. cmMemFree(strPtr);
  85. strPtr = NULL;
  86. }
  87. }
  88. CFRelease(cfStrRef);
  89. return strPtr;
  90. }
  91. void _cmMpDeviceInit( cmMpDev* drp )
  92. {
  93. drp->nameStr = NULL;
  94. drp->iPortCnt = 0;
  95. drp->oPortCnt = 0;
  96. drp->iPortArray = NULL;
  97. drp->oPortArray = NULL;
  98. }
  99. void _cmMpDeviceFree( cmMpDev* drp )
  100. {
  101. unsigned i;
  102. for(i=0; i<drp->iPortCnt; ++i)
  103. {
  104. cmMemPtrFree(&drp->iPortArray[i].nameStr);
  105. cmMpParserDestroy( &drp->iPortArray[i].parserH );
  106. }
  107. for(i=0; i<drp->oPortCnt; ++i)
  108. cmMemPtrFree(&drp->oPortArray[i].nameStr);
  109. cmMemPtrFree(&drp->iPortArray);
  110. cmMemPtrFree(&drp->oPortArray);
  111. cmMemPtrFree(&drp->nameStr);
  112. _cmMpDeviceInit(drp);
  113. }
  114. void _cmMpDevicePrint( cmMpDev* drp, unsigned devIdx, cmRpt_t* rpt )
  115. {
  116. unsigned i,j;
  117. cmRptPrintf(rpt,"%i %s\n",devIdx,drp->nameStr);
  118. for(j=0; j<2; ++j)
  119. {
  120. unsigned portCnt = j==0 ? drp->iPortCnt : drp->oPortCnt;
  121. for(i=0; i<portCnt; ++i)
  122. {
  123. const cmMpPort* p = j==0 ? drp->iPortArray + i : drp->oPortArray + i ;
  124. cmRptPrintf(rpt,"%i %s id:0x:%x %s\n", i, p->inputFl ? "in " : "out", p->uid, p->nameStr );
  125. }
  126. }
  127. }
  128. char* _cmMpFormLabel( cmErr_t* errPtr, MIDIObjectRef obj )
  129. {
  130. char* modelStr = _cmMpAllocStringProperty( errPtr, obj, kMIDIPropertyManufacturer );
  131. char* nameStr = _cmMpAllocStringProperty( errPtr, obj, kMIDIPropertyModel );
  132. char* labelStr = cmMemAllocZ( char, strlen(modelStr) + strlen(nameStr) + 4 );
  133. strcpy(labelStr,modelStr);
  134. strcat(labelStr," - ");
  135. strcat(labelStr,nameStr);
  136. cmMemFree(modelStr);
  137. cmMemFree(nameStr);
  138. return labelStr;
  139. }
  140. char* _cmMpFormPortLabel( cmErr_t* errPtr, MIDIEntityRef mer, ItemCount pi, bool inputFl )
  141. {
  142. MIDIEndpointRef epr;
  143. if( inputFl )
  144. epr = MIDIEntityGetSource( mer, pi);
  145. else
  146. epr = MIDIEntityGetDestination( mer, pi);
  147. if(epr == 0 )
  148. {
  149. _cmMpError(errPtr,kSysErrMpRC,noErr,"Get %s endpoiint ref %i failed.", inputFl ? "source" : "destination",pi);
  150. return NULL;
  151. }
  152. return _cmMpAllocStringProperty( errPtr, (MIDIObjectRef)epr, kMIDIPropertyName );
  153. }
  154. // Fill id array with the unique id of each endpoint or 0 if the endpoint is offline.
  155. cmMpRC_t _cmMpGetEntityUniqueIdArray( MIDIEntityRef mer, SInt32* idArray, unsigned idCnt, bool inputFl, ItemCount* activeCntPtr, cmErr_t* errPtr )
  156. {
  157. cmMpRC_t rc = kOkMpRC;
  158. MIDIEndpointRef epr;
  159. unsigned pi;
  160. char* dirLabel = inputFl ? "source" : "dest";
  161. SInt32 offline;
  162. OSStatus err;
  163. *activeCntPtr = 0;
  164. for(pi=0; pi<idCnt; ++pi)
  165. {
  166. epr = inputFl ? MIDIEntityGetSource( mer, pi) : MIDIEntityGetDestination( mer, pi);
  167. if(epr == 0 )
  168. {
  169. rc = _cmMpError(errPtr,kSysErrMpRC,noErr,"Get %s endpoiint ref %i failed.",dirLabel,pi);
  170. goto errLabel;
  171. }
  172. offline = 1;
  173. if((err = MIDIObjectGetIntegerProperty( epr, kMIDIPropertyOffline, &offline)) != noErr )
  174. {
  175. rc= _cmMpError(errPtr,kSysErrMpRC,err,"Get online status on %s endpoint %i failed.",dirLabel,pi);
  176. goto errLabel;
  177. }
  178. if( offline )
  179. idArray[pi] = 0;
  180. else
  181. {
  182. ++*activeCntPtr;
  183. if(( err = MIDIObjectGetIntegerProperty( epr, kMIDIPropertyUniqueID, idArray + pi)) != noErr )
  184. {
  185. rc = _cmMpError(errPtr,kSysErrMpRC,err,"Get unique id on %s endpoint %i failed.",dirLabel,pi);
  186. goto errLabel;
  187. }
  188. }
  189. }
  190. errLabel:
  191. return rc;
  192. }
  193. cmMpRC_t _cmMpInitPortArray( unsigned devIdx, MIDIPortRef mpr, MIDIEntityRef mer, cmMpPort* portArray, SInt32* idArray, unsigned portCnt, bool inputFl, cmMpCallback_t cbFunc, void* cbDataPtr, unsigned parserBufByteCnt, cmErr_t* errPtr )
  194. {
  195. cmMpRC_t rc = kOkMpRC;
  196. unsigned pi;
  197. for(pi=0; pi<portCnt; ++pi)
  198. {
  199. MIDIEndpointRef epr = inputFl ? MIDIEntityGetSource(mer,pi) : MIDIEntityGetDestination(mer,pi);
  200. if( epr == 0 )
  201. return _cmMpError(errPtr,kSysErrMpRC,noErr,"Get %s endpoiint ref %i failed.",inputFl ? "source" : "destination", pi);
  202. // if this is an active input port then connect it to the source port
  203. if( inputFl && (idArray[pi] != 0) )
  204. {
  205. OSStatus err = noErr;
  206. if((err = MIDIPortConnectSource( mpr, epr, portArray + pi )) != noErr )
  207. return _cmMpError(errPtr,kSysErrMpRC,err,"Source connect failed on port:%i",pi);
  208. portArray[pi].parserH = cmMpParserCreate( devIdx, pi, cbFunc, cbDataPtr, parserBufByteCnt, errPtr->rpt );
  209. }
  210. portArray[pi].uid = idArray[pi];
  211. portArray[pi].inputFl = inputFl;
  212. portArray[pi].nameStr = _cmMpFormPortLabel(errPtr, mer, pi, inputFl );
  213. portArray[pi].epr = epr;
  214. portArray[pi].prevMicroSecs = 0;
  215. }
  216. return rc;
  217. }
  218. cmMpRC_t _cmMpGetActiveEntityPortCount( MIDIEntityRef mer, bool inputFl, ItemCount* portCntPtr, ItemCount* activePortCntPtr, cmErr_t* errPtr )
  219. {
  220. *portCntPtr = inputFl ? MIDIEntityGetNumberOfSources( mer ) : MIDIEntityGetNumberOfDestinations( mer );
  221. SInt32 idArray[ *portCntPtr ];
  222. return _cmMpGetEntityUniqueIdArray( mer, idArray, *portCntPtr, inputFl, activePortCntPtr, errPtr );
  223. }
  224. cmMpRC_t _cmMpIsDeviceActive( unsigned devIdx, MIDIDeviceRef mdr, ItemCount* srcCntPtr, ItemCount* dstCntPtr, bool* activeFlPtr, cmErr_t* errPtr )
  225. {
  226. cmMpRC_t rc = kOkMpRC;
  227. ItemCount entityCnt = MIDIDeviceGetNumberOfEntities(mdr);
  228. unsigned ei;
  229. *srcCntPtr = 0;
  230. *dstCntPtr = 0;
  231. *activeFlPtr = false;
  232. for(ei=0; ei<entityCnt; ++ei)
  233. {
  234. ItemCount srcCnt = 0;
  235. ItemCount dstCnt = 0;
  236. ItemCount activeCnt = 0;
  237. MIDIEntityRef mer;
  238. if((mer = MIDIDeviceGetEntity( mdr, ei)) == 0 )
  239. return _cmMpError( errPtr,kSysErrMpRC,noErr,"Get midi device %i entity %i failed.",devIdx,ei);
  240. if((rc= _cmMpGetActiveEntityPortCount(mer,true,&srcCnt,&activeCnt,errPtr)) != kOkMpRC )
  241. return _cmMpError( errPtr, rc, noErr, "Error retrieving source port information for device:%i entity:%i",devIdx,ei);
  242. *srcCntPtr += srcCnt;
  243. if( activeCnt )
  244. *activeFlPtr = true;
  245. activeCnt = 0;
  246. if((rc= _cmMpGetActiveEntityPortCount(mer,false,&dstCnt,&activeCnt,errPtr)) != kOkMpRC )
  247. return _cmMpError( errPtr, rc, noErr, "Error retrieving destination port information for device:%i entity:%i",devIdx,ei);
  248. *dstCntPtr += dstCnt;
  249. if( activeCnt )
  250. *activeFlPtr = true;
  251. }
  252. return rc;
  253. }
  254. cmMpRC_t _cmMpCreateDevice( unsigned devIdx, cmMpDev* drp, MIDIPortRef inPortRef, cmMpCallback_t cbFunc, void* cbDataPtr, unsigned parserBufByteCnt, cmErr_t* errPtr )
  255. {
  256. cmMpRC_t rc = kOkMpRC;
  257. MIDIDeviceRef mdr;
  258. ItemCount devSrcCnt = 0;
  259. ItemCount devDstCnt = 0;
  260. bool activeFl = false;
  261. unsigned ei;
  262. // zero the device record
  263. _cmMpDeviceInit(drp);
  264. // get the device ref
  265. if((mdr = MIDIGetDevice(devIdx)) == 0 )
  266. return _cmMpError(errPtr,kSysErrMpRC,noErr,"Get midi device %i failed.",devIdx);
  267. // determine if the device port count and whether it is active
  268. if((rc = _cmMpIsDeviceActive(devIdx, mdr, &devSrcCnt, &devDstCnt, &activeFl, errPtr )) != kOkMpRC )
  269. return rc;
  270. // if the device is not active return with the device record empty
  271. if( !activeFl)
  272. return kOkMpRC;
  273. // allocate the input port array
  274. drp->iPortCnt = devSrcCnt;
  275. drp->iPortArray = cmMemAllocZ( cmMpPort, devSrcCnt );
  276. // allocate the output port array
  277. drp->oPortCnt = devDstCnt;
  278. drp->oPortArray = cmMemAllocZ( cmMpPort, devDstCnt );
  279. // form the device name string
  280. drp->nameStr = _cmMpFormLabel( errPtr, (MIDIObjectRef)mdr);
  281. unsigned entityCnt = MIDIDeviceGetNumberOfEntities(mdr);
  282. unsigned iPortCnt = 0;
  283. unsigned oPortCnt = 0;
  284. // for each entity on this device
  285. for(ei=0; ei<entityCnt; ++ei)
  286. {
  287. MIDIEntityRef mer;
  288. unsigned i;
  289. // get the entity reference
  290. if((mer = MIDIDeviceGetEntity( mdr, ei)) == 0 )
  291. {
  292. rc = _cmMpError(errPtr,kSysErrMpRC,noErr,"Get midi device %i entity %i failed.",devIdx,ei);
  293. goto errLabel;
  294. }
  295. // for each input and output on this entity
  296. for( i=0; i<2; ++i)
  297. {
  298. bool inputFl = i==0;
  299. ItemCount activeCnt = 0;
  300. unsigned portCnt = inputFl ? iPortCnt : oPortCnt;
  301. cmMpPort* portArray = inputFl ? drp->iPortArray : drp->oPortArray;
  302. ItemCount entityPortCnt = inputFl ? MIDIEntityGetNumberOfSources( mer ) : MIDIEntityGetNumberOfDestinations( mer );
  303. SInt32 idArray[ entityPortCnt ];
  304. // fill idArray with the unique id's of the active ports on this entity
  305. if((rc = _cmMpGetEntityUniqueIdArray(mer,idArray,entityPortCnt,inputFl,&activeCnt,errPtr)) != kOkMpRC )
  306. {
  307. rc = _cmMpError(errPtr,rc,noErr,"Unable to locate unique source ids on device:%i entity:%i",devIdx,ei);
  308. goto errLabel;
  309. }
  310. // assign the unique id's to each cmMpPort assoc'd with this entity
  311. _cmMpInitPortArray(devIdx, inPortRef, mer, portArray + portCnt, idArray, entityPortCnt, inputFl, cbFunc, cbDataPtr, parserBufByteCnt, errPtr );
  312. if( inputFl )
  313. iPortCnt += entityPortCnt;
  314. else
  315. oPortCnt += entityPortCnt;
  316. }
  317. }
  318. assert( iPortCnt == devSrcCnt );
  319. assert( oPortCnt == devDstCnt );
  320. errLabel:
  321. if( rc != kOkMpRC )
  322. _cmMpDeviceFree(drp);
  323. return rc;
  324. }
  325. void _cmMpDestroyDeviceArray( cmMpRoot* rp )
  326. {
  327. unsigned i;
  328. for(i=0; i<rp->devCnt; ++i)
  329. _cmMpDeviceFree( rp->devArray + i );
  330. cmMemPtrFree(&rp->devArray);
  331. rp->devCnt = 0;
  332. }
  333. cmMpRC_t _cmMpCreateDeviceArray( cmMpRoot* rp, unsigned parserBufByteCnt )
  334. {
  335. cmMpRC_t rc = kOkMpRC;
  336. ItemCount devCnt = MIDIGetNumberOfDevices();
  337. ItemCount di = 0;
  338. rp->devCnt = 0;
  339. rp->devArray = cmMemAllocZ( cmMpDev, devCnt );
  340. for(di=0; di<devCnt; ++di)
  341. {
  342. cmMpDev* drp = rp->devArray + rp->devCnt;
  343. // attempt to create a device record for device di
  344. if((rc = _cmMpCreateDevice( di, drp, rp->inPortRef, rp->cbFunc, rp->cbDataPtr, parserBufByteCnt, &rp->err )) != kOkMpRC )
  345. goto errLabel;
  346. // if the device di is active
  347. if( drp->iPortCnt || drp->oPortCnt )
  348. ++rp->devCnt;
  349. }
  350. errLabel:
  351. if( rc != kOkMpRC )
  352. _cmMpDestroyDeviceArray(rp);
  353. return rc;
  354. }
  355. void _cmMpMidiSystemNotifyProc( const MIDINotification* notifyMsgPtr, void *refCon )
  356. {
  357. // cmMpRoot* p = (mdRoot*)refCon;
  358. // if(p != NULL )
  359. // ((fw::osxMidiPorts*)refCon)->onNotify(notifyMsgPtr);
  360. }
  361. void _cmMpMIDISystemReadProc( const MIDIPacketList *pktListPtr, void* readProcRefCon, void* srcConnRefCon )
  362. {
  363. cmMpRoot* rp = (cmMpRoot*)readProcRefCon;
  364. cmMpPort* pp = (cmMpPort*)srcConnRefCon;
  365. unsigned i;
  366. if( pktListPtr == NULL || rp == NULL || pp == NULL || rp->cbFunc == NULL )
  367. return;
  368. const MIDIPacket* packetPtr = &pktListPtr->packet[0];
  369. for(i=0; i < pktListPtr->numPackets; ++i)
  370. {
  371. /*
  372. unsigned j;
  373. for(j=0; j<packetPtr->length; ++j)
  374. printf("0x%x ", packetPtr->data[j]);
  375. printf("\n");
  376. */
  377. // About converting mach_absolute_time() to seconds: http://lists.apple.com/archives/carbon-dev/2007/Jan//msg00282.html
  378. // About MIDIPacket.timeStamp: http://developer.apple.com/library/ios/#documentation/CoreMidi/Reference/MIDIServices_Reference/Reference/reference.html
  379. double nano = 1e-9 * ( (double) _cmMpRoot.timeBaseInfo.numer) / ((double) _cmMpRoot.timeBaseInfo.denom);
  380. // so here's the delta in nanoseconds:
  381. double nanoSeconds = ((double) packetPtr->timeStamp) * nano;
  382. // 1000 times that for microSeconds:
  383. double microSecs = 1000.0f * nanoSeconds;
  384. double deltaMicroSecs = microSecs - pp->prevMicroSecs;
  385. pp->prevMicroSecs = microSecs;
  386. assert( pp->inputFl == true );
  387. cmMpParseMidiData( pp->parserH, (unsigned)deltaMicroSecs, packetPtr->data, packetPtr->length );
  388. packetPtr = MIDIPacketNext(packetPtr);
  389. }
  390. }
  391. cmMpPort* _cmMpGetPort( cmMpDev* drp, unsigned portIdx, unsigned flags )
  392. {
  393. assert( flags != 0 );
  394. bool inputFl = cmIsFlag(flags, kInMpFl);
  395. assert( portIdx < (inputFl ? drp->iPortCnt : drp->oPortCnt));
  396. return inputFl ? drp->iPortArray + portIdx : drp->oPortArray + portIdx;
  397. }
  398. cmMpRC_t cmMpInitialize( cmCtx_t* c, cmMpCallback_t cbFunc, void* cbDataPtr, unsigned parserBufByteCnt, const char* appNameStr )
  399. {
  400. cmMpRC_t rc = kOkMpRC;
  401. OSStatus err = noErr;
  402. CFStringRef clientNameStrRef = NULL;
  403. CFStringRef inPortNameStrRef = NULL;
  404. CFStringRef outPortNameStrRef = NULL;
  405. cmRpt_t* rpt = &c->rpt;
  406. if( _cmMpRtPtr == NULL )
  407. {
  408. memset(&_cmMpRoot,0,sizeof(_cmMpRoot));
  409. _cmMpRtPtr = &_cmMpRoot;
  410. }
  411. if((rc = cmMpFinalize()) != kOkMpRC )
  412. return rc;
  413. cmErrSetup(&_cmMpRoot.err,rpt,"MIDI Port");
  414. _cmMpRoot.cbFunc = cbFunc;
  415. _cmMpRoot.cbDataPtr = cbDataPtr;
  416. if(( clientNameStrRef = CFStringCreateWithCString(NULL,"MIDI",kCFStringEncodingASCII)) == NULL )
  417. {
  418. rc = _cmMpError(&_cmMpRoot.err,kCfStringErrMpRC,noErr,"Client name create failed.");
  419. goto errLabel;
  420. }
  421. if(( err = MIDIClientCreate( clientNameStrRef, _cmMpMidiSystemNotifyProc, &_cmMpRoot, &_cmMpRoot.clientRef )) != noErr )
  422. {
  423. rc = _cmMpError(&_cmMpRoot.err,kSysErrMpRC,err,"Client create failed.");
  424. goto errLabel;
  425. }
  426. if((inPortNameStrRef = CFStringCreateWithCString(NULL,"in",kCFStringEncodingASCII)) == NULL )
  427. {
  428. rc = _cmMpError(&_cmMpRoot.err,kCfStringErrMpRC,noErr,"In port name string create failed.");
  429. goto errLabel;
  430. }
  431. if((err = MIDIInputPortCreate( _cmMpRoot.clientRef, inPortNameStrRef, _cmMpMIDISystemReadProc, &_cmMpRoot, &_cmMpRoot.inPortRef)) != noErr )
  432. {
  433. rc = _cmMpError(&_cmMpRoot.err,kSysErrMpRC,err,"In port create failed.");
  434. goto errLabel;
  435. }
  436. if((outPortNameStrRef = CFStringCreateWithCString(NULL,"out",kCFStringEncodingASCII)) == NULL )
  437. {
  438. rc = _cmMpError(&_cmMpRoot.err,kCfStringErrMpRC,noErr,"Out port name string create failed.");
  439. goto errLabel;
  440. }
  441. if((err = MIDIOutputPortCreate( _cmMpRoot.clientRef, outPortNameStrRef, &_cmMpRoot.outPortRef)) != noErr )
  442. {
  443. rc = _cmMpError(&_cmMpRoot.err,kSysErrMpRC,err,"Out port create failed.");
  444. goto errLabel;
  445. }
  446. if((rc = _cmMpCreateDeviceArray(&_cmMpRoot,parserBufByteCnt)) != kOkMpRC )
  447. goto errLabel;
  448. mach_timebase_info(&_cmMpRoot.timeBaseInfo);
  449. errLabel:
  450. if( clientNameStrRef != NULL )
  451. CFRelease(clientNameStrRef);
  452. if( inPortNameStrRef != NULL )
  453. CFRelease(inPortNameStrRef);
  454. if( outPortNameStrRef != NULL )
  455. CFRelease(outPortNameStrRef);
  456. if( rc != kOkMpRC )
  457. cmMpFinalize();
  458. return rc;
  459. }
  460. cmMpRC_t cmMpFinalize()
  461. {
  462. OSStatus err = noErr;
  463. cmMpRC_t rc = kOkMpRC;
  464. if( _cmMpRoot.inPortRef != 0 )
  465. {
  466. if((err = MIDIPortDispose(_cmMpRoot.inPortRef)) != noErr )
  467. rc = _cmMpError(&_cmMpRoot.err,kSysErrMpRC,err,"MIDIPortDispose() failed on the input port.");
  468. else
  469. _cmMpRoot.inPortRef = 0;
  470. }
  471. if( _cmMpRoot.outPortRef != 0 )
  472. {
  473. if((err = MIDIPortDispose(_cmMpRoot.outPortRef)) != noErr )
  474. rc = _cmMpError(&_cmMpRoot.err,kSysErrMpRC,err,"MIDIPortDispose() failed on the output port.");
  475. else
  476. _cmMpRoot.outPortRef = 0;
  477. }
  478. if( _cmMpRoot.clientRef != 0 )
  479. {
  480. if((err = MIDIClientDispose(_cmMpRoot.clientRef)) != noErr )
  481. rc = _cmMpError(&_cmMpRoot.err,kSysErrMpRC,err,"MIDIClientDispose() failed.");
  482. else
  483. _cmMpRoot.clientRef = 0;
  484. }
  485. _cmMpDestroyDeviceArray(&_cmMpRoot);
  486. _cmMpRoot.devCnt = 0;
  487. _cmMpRoot.devArray = NULL;
  488. _cmMpRoot.cbFunc = NULL;
  489. _cmMpRoot.cbDataPtr = NULL;
  490. _cmMpRoot.clientRef = 0;
  491. _cmMpRoot.inPortRef = 0;
  492. _cmMpRoot.outPortRef = 0;
  493. return rc;
  494. }
  495. bool cmMpIsInitialized()
  496. { return _cmMpRoot.clientRef != 0; }
  497. unsigned cmMpDeviceCount()
  498. {
  499. return _cmMpRoot.devCnt;
  500. }
  501. const char* cmMpDeviceName( unsigned devIdx )
  502. {
  503. assert( devIdx < _cmMpRoot.devCnt );
  504. return _cmMpRoot.devArray[devIdx].nameStr;
  505. }
  506. unsigned cmMpDevicePortCount( unsigned devIdx, unsigned flags )
  507. {
  508. assert( devIdx < _cmMpRoot.devCnt );
  509. return cmIsFlag(flags,kInMpFl) ? _cmMpRoot.devArray[devIdx].iPortCnt : _cmMpRoot.devArray[devIdx].oPortCnt;
  510. }
  511. const char* cmMpDevicePortName( unsigned devIdx, unsigned flags, unsigned portIdx )
  512. {
  513. assert( devIdx < _cmMpRoot.devCnt );
  514. cmMpDev* drp = _cmMpRoot.devArray + devIdx;
  515. assert( portIdx < (cmIsFlag(flags,kInMpFl) ? drp->iPortCnt : drp->oPortCnt));
  516. return _cmMpGetPort( drp, portIdx, flags )->nameStr;
  517. }
  518. cmMpRC_t cmMpDeviceSend( unsigned devIdx, unsigned portIdx, cmMidiByte_t status, cmMidiByte_t d0, cmMidiByte_t d1 )
  519. {
  520. assert( devIdx < _cmMpRoot.devCnt );
  521. cmMpDev* drp = _cmMpRoot.devArray + devIdx;
  522. cmMpPort* pp = _cmMpGetPort( drp, portIdx, kOutMpFl);
  523. OSStatus err = noErr;
  524. MIDIPacketList mpl;
  525. unsigned byteCnt = cmMidiStatusToByteCount(status & 0xf0 );
  526. if( byteCnt == 0 )
  527. return _cmMpError(&_cmMpRoot.err,kInvalidArgMpRC,noErr,"Invalid status byte (0x%x) on send to device:%i port:%i",status,devIdx,portIdx);
  528. mpl.numPackets = 1;
  529. mpl.packet[0].timeStamp = 0;
  530. mpl.packet[0].length = byteCnt;
  531. mpl.packet[0].data[0] = status;
  532. mpl.packet[0].data[1] = d0;
  533. mpl.packet[0].data[2] = d1;
  534. if(( err = MIDISend( _cmMpRoot.outPortRef, pp->epr, &mpl)) != noErr )
  535. return _cmMpError(&_cmMpRoot.err,kSysErrMpRC,err,"Send on device:%i port:%i failed.",devIdx,portIdx);
  536. return kOkMpRC;
  537. }
  538. cmMpRC_t cmMpDeviceSendData( unsigned devIdx, unsigned portIdx, const cmMidiByte_t* dataPtr, unsigned byteCnt )
  539. {
  540. assert( devIdx < _cmMpRoot.devCnt );
  541. cmMpDev* drp = _cmMpRoot.devArray + devIdx;
  542. cmMpPort* pp = _cmMpGetPort( drp, portIdx, kOutMpFl);
  543. OSStatus err = noErr;
  544. MIDIPacketList mpl;
  545. unsigned i;
  546. if( byteCnt > 256 )
  547. return _cmMpError(&_cmMpRoot.err,kInvalidArgMpRC,noErr,"mdDeviceSendData() only support data buffers up to 256 bytes.");
  548. mpl.numPackets = 1;
  549. mpl.packet[0].timeStamp = 0;
  550. mpl.packet[0].length = byteCnt;
  551. for(i=0; i<byteCnt; i++)
  552. mpl.packet[0].data[i] = dataPtr[i];
  553. if(( err = MIDISend( _cmMpRoot.outPortRef, pp->epr, &mpl)) != noErr )
  554. return _cmMpError(&_cmMpRoot.err,kSysErrMpRC,err,"Data Send on device:%i port:%i failed.",devIdx,portIdx);
  555. return kOkMpRC;
  556. }
  557. cmMpRC_t cmMpInstallCallback( unsigned devIdx, unsigned portIdx, cmMpCallback_t cbFunc, void* cbDataPtr )
  558. {
  559. cmMpRC_t rc = kOkMpRC;
  560. unsigned di;
  561. unsigned dn = cmMpDeviceCount();
  562. for(di=0; di<dn; ++di)
  563. if( di==devIdx || devIdx == -1 )
  564. {
  565. unsigned pi;
  566. unsigned pn = cmMpDevicePortCount(di,kInMpFl);
  567. for(pi=0; pi<pn; ++pi)
  568. if( pi==portIdx || portIdx == -1 )
  569. if( cmMpParserInstallCallback( _cmMpRoot.devArray[di].iPortArray[pi].parserH, cbFunc, cbDataPtr ) != kOkMpRC )
  570. goto errLabel;
  571. }
  572. errLabel:
  573. return rc;
  574. }
  575. cmMpRC_t cmMpRemoveCallback( unsigned devIdx, unsigned portIdx, cmMpCallback_t cbFunc, void* cbDataPtr )
  576. {
  577. cmMpRC_t rc = kOkMpRC;
  578. unsigned di;
  579. unsigned dn = cmMpDeviceCount();
  580. unsigned remCnt = 0;
  581. for(di=0; di<dn; ++di)
  582. if( di==devIdx || devIdx == -1 )
  583. {
  584. unsigned pi;
  585. unsigned pn = cmMpDevicePortCount(di,kInMpFl);
  586. for(pi=0; pi<pn; ++pi)
  587. if( pi==portIdx || portIdx == -1 )
  588. if( cmMpParserHasCallback( _cmMpRoot.devArray[di].iPortArray[pi].parserH, cbFunc, cbDataPtr ) )
  589. {
  590. if( cmMpParserRemoveCallback( _cmMpRoot.devArray[di].iPortArray[pi].parserH, cbFunc, cbDataPtr ) != kOkMpRC )
  591. goto errLabel;
  592. else
  593. ++remCnt;
  594. }
  595. }
  596. if( remCnt == 0 && dn > 0 )
  597. rc = _cmMpError(&_cmMpRoot.err,kCbNotFoundMpRC,0,"The callback was not found on any of the specified devices or ports.");
  598. errLabel:
  599. return rc;
  600. }
  601. bool cmMpUsesCallback( unsigned devIdx, unsigned portIdx, cmMpCallback_t cbFunc, void* cbDataPtr )
  602. {
  603. unsigned di;
  604. unsigned dn = cmMpDeviceCount();
  605. for(di=0; di<dn; ++di)
  606. if( di==devIdx || devIdx == -1 )
  607. {
  608. unsigned pi;
  609. unsigned pn = cmMpDevicePortCount(di,kInMpFl);
  610. for(pi=0; pi<pn; ++pi)
  611. if( pi==portIdx || portIdx == -1 )
  612. if( cmMpParserHasCallback( _cmMpRoot.devArray[di].iPortArray[pi].parserH, cbFunc, cbDataPtr ) )
  613. return true;
  614. }
  615. return false;
  616. }
  617. void cmMpReport( cmRpt_t* rpt )
  618. {
  619. unsigned i;
  620. for(i=0; i<_cmMpRoot.devCnt; ++i)
  621. _cmMpDevicePrint(_cmMpRoot.devArray + i, i, rpt );
  622. }