jack2 codebase
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.

867 lines
28KB

  1. /*
  2. Copyright (C) 2009 Grame
  3. Copyright (C) 2011 Devin Anderson
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "JackCompilerDeps.h"
  17. #include "JackCoreMidiDriver.h"
  18. #include "JackCoreMidiUtil.h"
  19. #include "JackEngineControl.h"
  20. #include "driver_interface.h"
  21. #include <stdexcept>
  22. #include <mach/mach_time.h>
  23. #include <mach/mach_error.h>
  24. using Jack::JackCoreMidiDriver;
  25. static char capture_driver_name[256];
  26. static char playback_driver_name[256];
  27. static int in_channels, out_channels;
  28. static bool capturing, playing, monitor;
  29. static jack_nframes_t capture_latency, playback_latency;
  30. ///////////////////////////////////////////////////////////////////////////////
  31. // Static callbacks
  32. ///////////////////////////////////////////////////////////////////////////////
  33. void
  34. JackCoreMidiDriver::HandleInputEvent(const MIDIPacketList *packet_list,
  35. void *driver, void *port)
  36. {
  37. ((JackCoreMidiPhysicalInputPort *) port)->ProcessCoreMidi(packet_list);
  38. }
  39. void
  40. JackCoreMidiDriver::HandleNotificationEvent(const MIDINotification *message,
  41. void *driver)
  42. {
  43. ((JackCoreMidiDriver *) driver)->HandleNotification(message);
  44. }
  45. ///////////////////////////////////////////////////////////////////////////////
  46. // Class
  47. ///////////////////////////////////////////////////////////////////////////////
  48. JackCoreMidiDriver::JackCoreMidiDriver(const char *name, const char *alias,
  49. JackLockedEngine *engine,
  50. JackSynchro *table):
  51. JackMidiDriver(name, alias, engine, table),fThread(this)
  52. {
  53. mach_timebase_info_data_t info;
  54. kern_return_t result = mach_timebase_info(&info);
  55. if (result != KERN_SUCCESS) {
  56. throw std::runtime_error(mach_error_string(result));
  57. }
  58. client = 0;
  59. fCaptureChannels = 0;
  60. fPlaybackChannels = 0;
  61. num_physical_inputs = 0;
  62. num_physical_outputs = 0;
  63. num_virtual_inputs = 0;
  64. num_virtual_outputs = 0;
  65. physical_input_ports = 0;
  66. physical_output_ports = 0;
  67. time_ratio = (((double) info.numer) / info.denom) / 1000.0;
  68. virtual_input_ports = 0;
  69. virtual_output_ports = 0;
  70. internal_input = 0;
  71. internal_output = 0;
  72. }
  73. JackCoreMidiDriver::~JackCoreMidiDriver()
  74. {}
  75. bool JackCoreMidiDriver::Init()
  76. {
  77. return OpenAux();
  78. }
  79. bool JackCoreMidiDriver::OpenAux()
  80. {
  81. int pi_count = 0;
  82. int po_count = 0;
  83. int vi_count = 0;
  84. int vo_count = 0;
  85. ItemCount potential_po_count;
  86. ItemCount potential_pi_count;
  87. CFStringRef name = CFStringCreateWithCString(0, "JackMidi",
  88. CFStringGetSystemEncoding());
  89. if (! name) {
  90. jack_error("JackCoreMidiDriver::Open - failed to allocate memory for "
  91. "client name string");
  92. return false;
  93. }
  94. OSStatus status = MIDIClientCreate(name, HandleNotificationEvent, this,
  95. &client);
  96. CFRelease(name);
  97. if (status != noErr) {
  98. WriteMacOSError("JackCoreMidiDriver::Open", "MIDIClientCreate",
  99. status);
  100. return false;
  101. }
  102. char *client_name = fClientControl.fName;
  103. // Allocate and connect physical inputs
  104. potential_pi_count = MIDIGetNumberOfSources();
  105. if (potential_pi_count) {
  106. status = MIDIInputPortCreate(client, CFSTR("Physical Input Port"),
  107. HandleInputEvent, this, &internal_input);
  108. if (status != noErr) {
  109. WriteMacOSError("JackCoreMidiDriver::Open", "MIDIInputPortCreate",
  110. status);
  111. goto destroy;
  112. }
  113. try {
  114. physical_input_ports =
  115. new JackCoreMidiPhysicalInputPort*[potential_pi_count];
  116. } catch (std::exception& e) {
  117. jack_error("JackCoreMidiDriver::Open - while creating physical "
  118. "input port array: %s", e.what());
  119. goto destroy;
  120. }
  121. for (ItemCount i = 0; i < potential_pi_count; i++) {
  122. try {
  123. physical_input_ports[pi_count] =
  124. new JackCoreMidiPhysicalInputPort(fAliasName, client_name,
  125. capture_driver_name, i,
  126. client, internal_input,
  127. time_ratio);
  128. } catch (std::exception& e) {
  129. jack_error("JackCoreMidiDriver::Open - while creating "
  130. "physical input port: %s", e.what());
  131. goto destroy;
  132. }
  133. pi_count++;
  134. }
  135. }
  136. // Allocate and connect physical outputs
  137. potential_po_count = MIDIGetNumberOfDestinations();
  138. if (potential_po_count) {
  139. status = MIDIOutputPortCreate(client, CFSTR("Physical Output Port"),
  140. &internal_output);
  141. if (status != noErr) {
  142. WriteMacOSError("JackCoreMidiDriver::Open", "MIDIOutputPortCreate",
  143. status);
  144. goto destroy;
  145. }
  146. try {
  147. physical_output_ports =
  148. new JackCoreMidiPhysicalOutputPort*[potential_po_count];
  149. } catch (std::exception& e) {
  150. jack_error("JackCoreMidiDriver::Open - while creating physical "
  151. "output port array: %s", e.what());
  152. goto destroy;
  153. }
  154. for (ItemCount i = 0; i < potential_po_count; i++) {
  155. try {
  156. physical_output_ports[po_count] =
  157. new JackCoreMidiPhysicalOutputPort(fAliasName, client_name,
  158. playback_driver_name, i,
  159. client, internal_output,
  160. time_ratio);
  161. } catch (std::exception& e) {
  162. jack_error("JackCoreMidiDriver::Open - while creating "
  163. "physical output port: %s", e.what());
  164. goto destroy;
  165. }
  166. po_count++;
  167. }
  168. }
  169. // Allocate and connect virtual inputs
  170. if (in_channels) {
  171. try {
  172. virtual_input_ports =
  173. new JackCoreMidiVirtualInputPort*[in_channels];
  174. } catch (std::exception& e) {
  175. jack_error("JackCoreMidiDriver::Open - while creating virtual "
  176. "input port array: %s", e.what());
  177. goto destroy;
  178. }
  179. for (vi_count = 0; vi_count < in_channels; vi_count++) {
  180. try {
  181. virtual_input_ports[vi_count] =
  182. new JackCoreMidiVirtualInputPort(fAliasName, client_name,
  183. capture_driver_name,
  184. vi_count, vi_count + pi_count, client,
  185. time_ratio);
  186. } catch (std::exception& e) {
  187. jack_error("JackCoreMidiDriver::Open - while creating virtual "
  188. "input port: %s", e.what());
  189. goto destroy;
  190. }
  191. }
  192. }
  193. // Allocate and connect virtual outputs
  194. if (out_channels) {
  195. try {
  196. virtual_output_ports =
  197. new JackCoreMidiVirtualOutputPort*[out_channels];
  198. } catch (std::exception& e) {
  199. jack_error("JackCoreMidiDriver::Open - while creating virtual "
  200. "output port array: %s", e.what());
  201. goto destroy;
  202. }
  203. for (vo_count = 0; vo_count < out_channels; vo_count++) {
  204. try {
  205. virtual_output_ports[vo_count] =
  206. new JackCoreMidiVirtualOutputPort(fAliasName, client_name,
  207. playback_driver_name,
  208. vo_count, vo_count + po_count, client,
  209. time_ratio);
  210. } catch (std::exception& e) {
  211. jack_error("JackCoreMidiDriver::Open - while creating virtual "
  212. "output port: %s", e.what());
  213. goto destroy;
  214. }
  215. }
  216. }
  217. if (! (pi_count || po_count || in_channels || out_channels)) {
  218. jack_error("JackCoreMidiDriver::Open - no CoreMIDI inputs or outputs "
  219. "found, and no virtual ports allocated.");
  220. }
  221. if (! JackMidiDriver::Open(capturing, playing,
  222. in_channels + pi_count,
  223. out_channels + po_count, monitor,
  224. capture_driver_name,
  225. playback_driver_name, capture_latency,
  226. playback_latency)) {
  227. num_physical_inputs = pi_count;
  228. num_physical_outputs = po_count;
  229. num_virtual_inputs = in_channels;
  230. num_virtual_outputs = out_channels;
  231. return true;
  232. }
  233. destroy:
  234. if (physical_input_ports) {
  235. for (int i = 0; i < pi_count; i++) {
  236. delete physical_input_ports[i];
  237. }
  238. delete[] physical_input_ports;
  239. physical_input_ports = 0;
  240. }
  241. if (physical_output_ports) {
  242. for (int i = 0; i < po_count; i++) {
  243. delete physical_output_ports[i];
  244. }
  245. delete[] physical_output_ports;
  246. physical_output_ports = 0;
  247. }
  248. if (virtual_input_ports) {
  249. for (int i = 0; i < vi_count; i++) {
  250. delete virtual_input_ports[i];
  251. }
  252. delete[] virtual_input_ports;
  253. virtual_input_ports = 0;
  254. }
  255. if (virtual_output_ports) {
  256. for (int i = 0; i < vo_count; i++) {
  257. delete virtual_output_ports[i];
  258. }
  259. delete[] virtual_output_ports;
  260. virtual_output_ports = 0;
  261. }
  262. if (internal_output) {
  263. status = MIDIPortDispose(internal_output);
  264. if (status != noErr) {
  265. WriteMacOSError("JackCoreMidiDriver::Open", "MIDIPortDispose", status);
  266. }
  267. }
  268. if (internal_input) {
  269. status = MIDIPortDispose(internal_input);
  270. if (status != noErr) {
  271. WriteMacOSError("JackCoreMidiDriver::Open", "MIDIPortDispose", status);
  272. }
  273. }
  274. if (client) {
  275. status = MIDIClientDispose(client);
  276. if (status != noErr) {
  277. WriteMacOSError("JackCoreMidiDriver::Open", "MIDIClientDispose",
  278. status);
  279. }
  280. }
  281. // Default open
  282. if (! JackMidiDriver::Open(capturing, playing,
  283. in_channels + pi_count,
  284. out_channels + po_count, monitor,
  285. capture_driver_name,
  286. playback_driver_name, capture_latency,
  287. playback_latency)) {
  288. client = 0;
  289. num_physical_inputs = 0;
  290. num_physical_outputs = 0;
  291. num_virtual_inputs = 0;
  292. num_virtual_outputs = 0;
  293. return true;
  294. } else {
  295. return false;
  296. }
  297. }
  298. bool JackCoreMidiDriver::Execute()
  299. {
  300. CFRunLoopRun();
  301. return false;
  302. }
  303. int
  304. JackCoreMidiDriver::Attach()
  305. {
  306. jack_nframes_t buffer_size = fEngineControl->fBufferSize;
  307. jack_port_id_t index;
  308. jack_nframes_t latency = buffer_size;
  309. jack_latency_range_t latency_range;
  310. const char *name;
  311. JackPort *port;
  312. JackCoreMidiPort *port_obj;
  313. latency_range.max = latency;
  314. latency_range.min = latency;
  315. // Physical inputs
  316. for (int i = 0; i < num_physical_inputs; i++) {
  317. port_obj = physical_input_ports[i];
  318. name = port_obj->GetName();
  319. if (fEngine->PortRegister(fClientControl.fRefNum, name,
  320. JACK_DEFAULT_MIDI_TYPE,
  321. CaptureDriverFlags, buffer_size, &index) < 0) {
  322. jack_error("JackCoreMidiDriver::Attach - cannot register physical "
  323. "input port with name '%s'.", name);
  324. // X: Do we need to deallocate ports?
  325. return -1;
  326. }
  327. port = fGraphManager->GetPort(index);
  328. port->SetAlias(port_obj->GetAlias());
  329. port->SetLatencyRange(JackCaptureLatency, &latency_range);
  330. fCapturePortList[i] = index;
  331. }
  332. // Virtual inputs
  333. for (int i = 0; i < num_virtual_inputs; i++) {
  334. port_obj = virtual_input_ports[i];
  335. name = port_obj->GetName();
  336. if (fEngine->PortRegister(fClientControl.fRefNum, name,
  337. JACK_DEFAULT_MIDI_TYPE,
  338. CaptureDriverFlags, buffer_size, &index) < 0) {
  339. jack_error("JackCoreMidiDriver::Attach - cannot register virtual "
  340. "input port with name '%s'.", name);
  341. // X: Do we need to deallocate ports?
  342. return -1;
  343. }
  344. port = fGraphManager->GetPort(index);
  345. port->SetAlias(port_obj->GetAlias());
  346. port->SetLatencyRange(JackCaptureLatency, &latency_range);
  347. fCapturePortList[num_physical_inputs + i] = index;
  348. }
  349. if (! fEngineControl->fSyncMode) {
  350. latency += buffer_size;
  351. latency_range.max = latency;
  352. latency_range.min = latency;
  353. }
  354. // Physical outputs
  355. for (int i = 0; i < num_physical_outputs; i++) {
  356. port_obj = physical_output_ports[i];
  357. name = port_obj->GetName();
  358. fEngine->PortRegister(fClientControl.fRefNum, name,
  359. JACK_DEFAULT_MIDI_TYPE,
  360. PlaybackDriverFlags, buffer_size, &index);
  361. if (index == NO_PORT) {
  362. jack_error("JackCoreMidiDriver::Attach - cannot register physical "
  363. "output port with name '%s'.", name);
  364. // X: Do we need to deallocate ports?
  365. return -1;
  366. }
  367. port = fGraphManager->GetPort(index);
  368. port->SetAlias(port_obj->GetAlias());
  369. port->SetLatencyRange(JackPlaybackLatency, &latency_range);
  370. fPlaybackPortList[i] = index;
  371. }
  372. // Virtual outputs
  373. for (int i = 0; i < num_virtual_outputs; i++) {
  374. port_obj = virtual_output_ports[i];
  375. name = port_obj->GetName();
  376. fEngine->PortRegister(fClientControl.fRefNum, name,
  377. JACK_DEFAULT_MIDI_TYPE,
  378. PlaybackDriverFlags, buffer_size, &index);
  379. if (index == NO_PORT) {
  380. jack_error("JackCoreMidiDriver::Attach - cannot register virtual "
  381. "output port with name '%s'.", name);
  382. // X: Do we need to deallocate ports?
  383. return -1;
  384. }
  385. port = fGraphManager->GetPort(index);
  386. port->SetAlias(port_obj->GetAlias());
  387. port->SetLatencyRange(JackPlaybackLatency, &latency_range);
  388. fPlaybackPortList[num_physical_outputs + i] = index;
  389. }
  390. return 0;
  391. }
  392. int
  393. JackCoreMidiDriver::Close()
  394. {
  395. fThread.Kill();
  396. return CloseAux();
  397. }
  398. int
  399. JackCoreMidiDriver::CloseAux()
  400. {
  401. // Generic MIDI driver close
  402. int result = JackMidiDriver::Close();
  403. OSStatus status;
  404. if (physical_input_ports) {
  405. for (int i = 0; i < num_physical_inputs; i++) {
  406. delete physical_input_ports[i];
  407. }
  408. delete[] physical_input_ports;
  409. num_physical_inputs = 0;
  410. physical_input_ports = 0;
  411. if (internal_input) {
  412. status = MIDIPortDispose(internal_input);
  413. if (status != noErr) {
  414. WriteMacOSError("JackCoreMidiDriver::Close", "MIDIPortDispose",
  415. status);
  416. result = -1;
  417. }
  418. internal_input = 0;
  419. }
  420. }
  421. if (physical_output_ports) {
  422. for (int i = 0; i < num_physical_outputs; i++) {
  423. delete physical_output_ports[i];
  424. }
  425. delete[] physical_output_ports;
  426. num_physical_outputs = 0;
  427. physical_output_ports = 0;
  428. if (internal_output) {
  429. status = MIDIPortDispose(internal_output);
  430. if (status != noErr) {
  431. WriteMacOSError("JackCoreMidiDriver::Close", "MIDIPortDispose",
  432. status);
  433. result = -1;
  434. }
  435. internal_output = 0;
  436. }
  437. }
  438. if (virtual_input_ports) {
  439. for (int i = 0; i < num_virtual_inputs; i++) {
  440. delete virtual_input_ports[i];
  441. }
  442. delete[] virtual_input_ports;
  443. num_virtual_inputs = 0;
  444. virtual_input_ports = 0;
  445. }
  446. if (virtual_output_ports) {
  447. for (int i = 0; i < num_virtual_outputs; i++) {
  448. delete virtual_output_ports[i];
  449. }
  450. delete[] virtual_output_ports;
  451. num_virtual_outputs = 0;
  452. virtual_output_ports = 0;
  453. }
  454. if (client) {
  455. status = MIDIClientDispose(client);
  456. if (status != noErr) {
  457. WriteMacOSError("JackCoreMidiDriver::Close", "MIDIClientDispose",
  458. status);
  459. result = -1;
  460. }
  461. client = 0;
  462. }
  463. return result;
  464. }
  465. void
  466. JackCoreMidiDriver::Restart()
  467. {
  468. // Lock between this thread and RT thread
  469. JackLock lock(this);
  470. // Lock between this thread and request thread
  471. if (fEngine->Lock()) {
  472. // Use first alias
  473. SaveConnections(1);
  474. Stop();
  475. Detach();
  476. CloseAux();
  477. OpenAux();
  478. Attach();
  479. Start();
  480. // Use first alias and partial port naming
  481. LoadConnections(1, false);
  482. fEngine->Unlock();
  483. } else {
  484. jack_error("Cannot acquire engine lock...");
  485. }
  486. }
  487. void
  488. JackCoreMidiDriver::HandleNotification(const MIDINotification *message)
  489. {
  490. switch (message->messageID) {
  491. case kMIDIMsgObjectAdded: {
  492. /*
  493. We don't want to restart when our internal virtual in/out are created.
  494. */
  495. const MIDIObjectAddRemoveNotification* add_message = reinterpret_cast<const MIDIObjectAddRemoveNotification*>(message);
  496. if (!JackCoreMidiPort::IsInternalPort(add_message->child)) {
  497. Restart();
  498. }
  499. break;
  500. }
  501. case kMIDIMsgObjectRemoved: {
  502. /*
  503. We don't want to restart when our internal virtual in/out are created.
  504. */
  505. const MIDIObjectAddRemoveNotification* remove_message = reinterpret_cast<const MIDIObjectAddRemoveNotification*>(message);
  506. if (!JackCoreMidiPort::IsInternalPort(remove_message->child)) {
  507. Restart();
  508. }
  509. break;
  510. }
  511. }
  512. }
  513. int
  514. JackCoreMidiDriver::Open(bool capturing_aux, bool playing_aux, int in_channels_aux,
  515. int out_channels_aux, bool monitor_aux,
  516. const char* capture_driver_name_aux,
  517. const char* playback_driver_name_aux,
  518. jack_nframes_t capture_latency_aux,
  519. jack_nframes_t playback_latency_aux)
  520. {
  521. strcpy(capture_driver_name, capture_driver_name_aux);
  522. strcpy(playback_driver_name, playback_driver_name_aux);
  523. capturing = capturing_aux;
  524. playing = playing_aux;
  525. in_channels = in_channels_aux;
  526. out_channels = out_channels_aux;
  527. monitor = monitor_aux;
  528. capture_latency = capture_latency_aux;
  529. playback_latency = playback_latency_aux;
  530. fThread.StartSync();
  531. int count = 0;
  532. while (fThread.GetStatus() != JackThread::kRunning && ++count < WAIT_COUNTER) {
  533. JackSleep(100000);
  534. jack_log("JackCoreMidiDriver::Open wait count = %d", count);
  535. }
  536. if (count == WAIT_COUNTER) {
  537. jack_info("Cannot open CoreMIDI driver");
  538. fThread.Kill();
  539. return -1;
  540. } else {
  541. JackSleep(10000);
  542. jack_info("CoreMIDI driver is opened...");
  543. }
  544. return 0;
  545. }
  546. int
  547. JackCoreMidiDriver::Start()
  548. {
  549. jack_info("JackCoreMidiDriver::Start - Starting driver.");
  550. JackMidiDriver::Start();
  551. int pi_count = 0;
  552. int po_count = 0;
  553. int vi_count = 0;
  554. int vo_count = 0;
  555. jack_info("JackCoreMidiDriver::Start - Enabling physical input ports.");
  556. for (; pi_count < num_physical_inputs; pi_count++) {
  557. if (physical_input_ports[pi_count]->Start() < 0) {
  558. jack_error("JackCoreMidiDriver::Start - Failed to enable physical "
  559. "input port.");
  560. goto stop_physical_input_ports;
  561. }
  562. }
  563. jack_info("JackCoreMidiDriver::Start - Enabling physical output ports.");
  564. for (; po_count < num_physical_outputs; po_count++) {
  565. if (physical_output_ports[po_count]->Start() < 0) {
  566. jack_error("JackCoreMidiDriver::Start - Failed to enable physical "
  567. "output port.");
  568. goto stop_physical_output_ports;
  569. }
  570. }
  571. jack_info("JackCoreMidiDriver::Start - Enabling virtual input ports.");
  572. for (; vi_count < num_virtual_inputs; vi_count++) {
  573. if (virtual_input_ports[vi_count]->Start() < 0) {
  574. jack_error("JackCoreMidiDriver::Start - Failed to enable virtual "
  575. "input port.");
  576. goto stop_virtual_input_ports;
  577. }
  578. }
  579. jack_info("JackCoreMidiDriver::Start - Enabling virtual output ports.");
  580. for (; vo_count < num_virtual_outputs; vo_count++) {
  581. if (virtual_output_ports[vo_count]->Start() < 0) {
  582. jack_error("JackCoreMidiDriver::Start - Failed to enable virtual "
  583. "output port.");
  584. goto stop_virtual_output_ports;
  585. }
  586. }
  587. jack_info("JackCoreMidiDriver::Start - Driver started.");
  588. return 0;
  589. stop_virtual_output_ports:
  590. for (int i = 0; i < vo_count; i++) {
  591. if (virtual_output_ports[i]->Stop() < 0) {
  592. jack_error("JackCoreMidiDriver::Start - Failed to disable virtual "
  593. "output port.");
  594. }
  595. }
  596. stop_virtual_input_ports:
  597. for (int i = 0; i < vi_count; i++) {
  598. if (virtual_input_ports[i]->Stop() < 0) {
  599. jack_error("JackCoreMidiDriver::Start - Failed to disable virtual "
  600. "input port.");
  601. }
  602. }
  603. stop_physical_output_ports:
  604. for (int i = 0; i < po_count; i++) {
  605. if (physical_output_ports[i]->Stop() < 0) {
  606. jack_error("JackCoreMidiDriver::Start - Failed to disable "
  607. "physical output port.");
  608. }
  609. }
  610. stop_physical_input_ports:
  611. for (int i = 0; i < pi_count; i++) {
  612. if (physical_input_ports[i]->Stop() < 0) {
  613. jack_error("JackCoreMidiDriver::Start - Failed to disable "
  614. "physical input port.");
  615. }
  616. }
  617. return -1;
  618. }
  619. int
  620. JackCoreMidiDriver::Stop()
  621. {
  622. int result = 0;
  623. JackMidiDriver::Stop();
  624. jack_info("JackCoreMidiDriver::Stop - disabling physical input ports.");
  625. for (int i = 0; i < num_physical_inputs; i++) {
  626. if (physical_input_ports[i]->Stop() < 0) {
  627. jack_error("JackCoreMidiDriver::Stop - Failed to disable physical "
  628. "input port.");
  629. result = -1;
  630. }
  631. }
  632. jack_info("JackCoreMidiDriver::Stop - disabling physical output ports.");
  633. for (int i = 0; i < num_physical_outputs; i++) {
  634. if (physical_output_ports[i]->Stop() < 0) {
  635. jack_error("JackCoreMidiDriver::Stop - Failed to disable physical "
  636. "output port.");
  637. result = -1;
  638. }
  639. }
  640. jack_info("JackCoreMidiDriver::Stop - disabling virtual input ports.");
  641. for (int i = 0; i < num_virtual_inputs; i++) {
  642. if (virtual_input_ports[i]->Stop() < 0) {
  643. jack_error("JackCoreMidiDriver::Stop - Failed to disable virtual "
  644. "input port.");
  645. result = -1;
  646. }
  647. }
  648. jack_info("JackCoreMidiDriver::Stop - disabling virtual output ports.");
  649. for (int i = 0; i < num_virtual_outputs; i++) {
  650. if (virtual_output_ports[i]->Stop() < 0) {
  651. jack_error("JackCoreMidiDriver::Stop - Failed to disable virtual "
  652. "output port.");
  653. result = -1;
  654. }
  655. }
  656. return result;
  657. }
  658. int
  659. JackCoreMidiDriver::ProcessRead()
  660. {
  661. int res;
  662. if (Trylock()) {
  663. res = (fEngineControl->fSyncMode) ? ProcessReadSync() : ProcessReadAsync();
  664. Unlock();
  665. } else {
  666. res = -1;
  667. }
  668. return res;
  669. }
  670. int
  671. JackCoreMidiDriver::ProcessWrite()
  672. {
  673. int res;
  674. if (Trylock()) {
  675. res = (fEngineControl->fSyncMode) ? ProcessWriteSync() : ProcessWriteAsync();
  676. Unlock();
  677. } else {
  678. res = -1;
  679. }
  680. return res;
  681. }
  682. int
  683. JackCoreMidiDriver::Read()
  684. {
  685. jack_nframes_t buffer_size = fEngineControl->fBufferSize;
  686. for (int i = 0; i < num_physical_inputs; i++) {
  687. physical_input_ports[i]->ProcessJack(GetInputBuffer(i), buffer_size);
  688. }
  689. for (int i = 0; i < num_virtual_inputs; i++) {
  690. virtual_input_ports[i]->
  691. ProcessJack(GetInputBuffer(num_physical_inputs + i), buffer_size);
  692. }
  693. return 0;
  694. }
  695. int
  696. JackCoreMidiDriver::Write()
  697. {
  698. jack_nframes_t buffer_size = fEngineControl->fBufferSize;
  699. for (int i = 0; i < num_physical_outputs; i++) {
  700. physical_output_ports[i]->ProcessJack(GetOutputBuffer(i), buffer_size);
  701. }
  702. for (int i = 0; i < num_virtual_outputs; i++) {
  703. virtual_output_ports[i]->
  704. ProcessJack(GetOutputBuffer(num_physical_outputs + i), buffer_size);
  705. }
  706. return 0;
  707. }
  708. #ifdef __cplusplus
  709. extern "C" {
  710. #endif
  711. // singleton kind of driver
  712. static Jack::JackCoreMidiDriver* driver = NULL;
  713. SERVER_EXPORT jack_driver_desc_t * driver_get_descriptor()
  714. {
  715. jack_driver_desc_t * desc;
  716. jack_driver_desc_filler_t filler;
  717. jack_driver_param_value_t value;
  718. desc = jack_driver_descriptor_construct("coremidi", JackDriverSlave, "Apple CoreMIDI API based MIDI backend", &filler);
  719. value.ui = 0;
  720. jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamUInt, &value, NULL, "CoreMIDI virtual bus", NULL);
  721. jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamUInt, &value, NULL, "CoreMIDI virtual bus", NULL);
  722. return desc;
  723. }
  724. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  725. {
  726. const JSList * node;
  727. const jack_driver_param_t * param;
  728. int virtual_in = 2;
  729. int virtual_out = 2;
  730. for (node = params; node; node = jack_slist_next (node)) {
  731. param = (const jack_driver_param_t *) node->data;
  732. switch (param->character) {
  733. case 'i':
  734. virtual_in = param->value.ui;
  735. break;
  736. case 'o':
  737. virtual_out = param->value.ui;
  738. break;
  739. }
  740. }
  741. // singleton kind of driver
  742. if (!driver) {
  743. driver = new Jack::JackCoreMidiDriver("system_midi", "coremidi", engine, table);
  744. if (driver->Open(1, 1, virtual_in, virtual_out, false, "in", "out", 0, 0) == 0) {
  745. return driver;
  746. } else {
  747. delete driver;
  748. return NULL;
  749. }
  750. } else {
  751. jack_info("JackCoreMidiDriver already allocated, cannot be loaded twice");
  752. return NULL;
  753. }
  754. }
  755. #ifdef __cplusplus
  756. }
  757. #endif