libcm is a C development framework with an emphasis on audio signal processing applications.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

cmMidiOsx.c 23KB

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