libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmMidiOsx.c 22KB

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