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.

848 lines
26KB

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