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

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