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.

428 lines
13KB

  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 <cmath>
  17. #include "JackEngineControl.h"
  18. #include "JackWinMMEDriver.h"
  19. #include "driver_interface.h"
  20. using Jack::JackWinMMEDriver;
  21. JackWinMMEDriver::JackWinMMEDriver(const char *name, const char *alias,
  22. JackLockedEngine *engine,
  23. JackSynchro *table):
  24. JackMidiDriver(name, alias, engine, table)
  25. {
  26. input_ports = 0;
  27. output_ports = 0;
  28. period = 0;
  29. }
  30. JackWinMMEDriver::~JackWinMMEDriver()
  31. {}
  32. int
  33. JackWinMMEDriver::Attach()
  34. {
  35. jack_nframes_t buffer_size = fEngineControl->fBufferSize;
  36. jack_port_id_t index;
  37. jack_nframes_t latency = buffer_size;
  38. jack_latency_range_t latency_range;
  39. const char *name;
  40. JackPort *port;
  41. latency_range.max = latency +
  42. ((jack_nframes_t) std::ceil((period / 1000.0) *
  43. fEngineControl->fSampleRate));
  44. latency_range.min = latency;
  45. jack_log("JackWinMMEDriver::Attach - fCaptureChannels %d", fCaptureChannels);
  46. jack_log("JackWinMMEDriver::Attach - fPlaybackChannels %d", fPlaybackChannels);
  47. // Inputs
  48. for (int i = 0; i < fCaptureChannels; i++) {
  49. JackWinMMEInputPort *input_port = input_ports[i];
  50. name = input_port->GetName();
  51. if (fEngine->PortRegister(fClientControl.fRefNum, name,
  52. JACK_DEFAULT_MIDI_TYPE,
  53. CaptureDriverFlags, buffer_size, &index) < 0) {
  54. jack_error("JackWinMMEDriver::Attach - cannot register input port "
  55. "with name '%s'.", name);
  56. // X: Do we need to deallocate ports?
  57. return -1;
  58. }
  59. port = fGraphManager->GetPort(index);
  60. port->SetAlias(input_port->GetAlias());
  61. port->SetLatencyRange(JackCaptureLatency, &latency_range);
  62. fEngine->PortSetDefaultMetadata(fClientControl.fRefNum, index,
  63. input_port->GetDeviceName());
  64. fCapturePortList[i] = index;
  65. }
  66. if (! fEngineControl->fSyncMode) {
  67. latency += buffer_size;
  68. latency_range.max = latency;
  69. latency_range.min = latency;
  70. }
  71. // Outputs
  72. for (int i = 0; i < fPlaybackChannels; i++) {
  73. JackWinMMEOutputPort *output_port = output_ports[i];
  74. name = output_port->GetName();
  75. if (fEngine->PortRegister(fClientControl.fRefNum, name,
  76. JACK_DEFAULT_MIDI_TYPE,
  77. PlaybackDriverFlags, buffer_size, &index) < 0) {
  78. jack_error("JackWinMMEDriver::Attach - cannot register output "
  79. "port with name '%s'.", name);
  80. // X: Do we need to deallocate ports?
  81. return -1;
  82. }
  83. port = fGraphManager->GetPort(index);
  84. port->SetAlias(output_port->GetAlias());
  85. port->SetLatencyRange(JackPlaybackLatency, &latency_range);
  86. fEngine->PortSetDefaultMetadata(fClientControl.fRefNum, index,
  87. output_port->GetDeviceName());
  88. fPlaybackPortList[i] = index;
  89. }
  90. return 0;
  91. }
  92. int
  93. JackWinMMEDriver::Close()
  94. {
  95. // Generic MIDI driver close
  96. int result = JackMidiDriver::Close();
  97. if (input_ports) {
  98. for (int i = 0; i < fCaptureChannels; i++) {
  99. delete input_ports[i];
  100. }
  101. delete[] input_ports;
  102. input_ports = 0;
  103. }
  104. if (output_ports) {
  105. for (int i = 0; i < fPlaybackChannels; i++) {
  106. delete output_ports[i];
  107. }
  108. delete[] output_ports;
  109. output_ports = 0;
  110. }
  111. if (period) {
  112. if (timeEndPeriod(period) != TIMERR_NOERROR) {
  113. jack_error("JackWinMMEDriver::Close - failed to unset timer "
  114. "resolution.");
  115. result = -1;
  116. }
  117. }
  118. return result;
  119. }
  120. int
  121. JackWinMMEDriver::Open(bool capturing, bool playing, int in_channels,
  122. int out_channels, bool monitor,
  123. const char* capture_driver_name,
  124. const char* playback_driver_name,
  125. jack_nframes_t capture_latency,
  126. jack_nframes_t playback_latency)
  127. {
  128. const char *client_name = fClientControl.fName;
  129. int input_count = 0;
  130. int output_count = 0;
  131. int num_potential_inputs = midiInGetNumDevs();
  132. int num_potential_outputs = midiOutGetNumDevs();
  133. jack_log("JackWinMMEDriver::Open - num_potential_inputs %d", num_potential_inputs);
  134. jack_log("JackWinMMEDriver::Open - num_potential_outputs %d", num_potential_outputs);
  135. period = 0;
  136. TIMECAPS caps;
  137. if (timeGetDevCaps(&caps, sizeof(TIMECAPS)) != TIMERR_NOERROR) {
  138. jack_error("JackWinMMEDriver::Open - could not get timer device "
  139. "capabilities. Continuing anyway ...");
  140. } else {
  141. period = caps.wPeriodMin;
  142. if (timeBeginPeriod(period) != TIMERR_NOERROR) {
  143. jack_error("JackWinMMEDriver::Open - could not set minimum timer "
  144. "resolution. Continuing anyway ...");
  145. period = 0;
  146. } else {
  147. jack_log("JackWinMMEDriver::Open - multimedia timer resolution "
  148. "set to %d milliseconds.", period);
  149. }
  150. }
  151. if (num_potential_inputs) {
  152. try {
  153. input_ports = new JackWinMMEInputPort *[num_potential_inputs];
  154. } catch (std::exception& e) {
  155. jack_error("JackWinMMEDriver::Open - while creating input port "
  156. "array: %s", e.what());
  157. goto unset_timer_resolution;
  158. }
  159. for (int i = 0; i < num_potential_inputs; i++) {
  160. try {
  161. input_ports[input_count] =
  162. new JackWinMMEInputPort(fAliasName, client_name,
  163. capture_driver_name, i);
  164. } catch (std::exception& e) {
  165. jack_error("JackWinMMEDriver::Open - while creating input "
  166. "port: %s", e.what());
  167. continue;
  168. }
  169. input_count++;
  170. }
  171. }
  172. if (num_potential_outputs) {
  173. try {
  174. output_ports = new JackWinMMEOutputPort *[num_potential_outputs];
  175. } catch (std::exception& e) {
  176. jack_error("JackWinMMEDriver::Open - while creating output port "
  177. "array: %s", e.what());
  178. goto destroy_input_ports;
  179. }
  180. for (int i = 0; i < num_potential_outputs; i++) {
  181. try {
  182. output_ports[output_count] =
  183. new JackWinMMEOutputPort(fAliasName, client_name,
  184. playback_driver_name, i);
  185. } catch (std::exception& e) {
  186. jack_error("JackWinMMEDriver::Open - while creating output port: %s | %s, %s, %s",
  187. e.what(), fAliasName, client_name, playback_driver_name);
  188. continue;
  189. }
  190. output_count++;
  191. }
  192. }
  193. jack_log("JackWinMMEDriver::Open - input_count %d", input_count);
  194. jack_log("JackWinMMEDriver::Open - output_count %d", output_count);
  195. if (! JackMidiDriver::Open(capturing, playing, input_count,
  196. output_count, monitor,
  197. capture_driver_name,
  198. playback_driver_name, capture_latency,
  199. playback_latency)) {
  200. return 0;
  201. }
  202. if (output_ports) {
  203. for (int i = 0; i < output_count; i++) {
  204. delete output_ports[i];
  205. }
  206. delete[] output_ports;
  207. output_ports = 0;
  208. }
  209. destroy_input_ports:
  210. if (input_ports) {
  211. for (int i = 0; i < input_count; i++) {
  212. delete input_ports[i];
  213. }
  214. delete[] input_ports;
  215. input_ports = 0;
  216. }
  217. unset_timer_resolution:
  218. if (period) {
  219. if (timeEndPeriod(period) != TIMERR_NOERROR) {
  220. jack_error("JackWinMMEDriver::Open - failed to unset timer "
  221. "resolution.");
  222. }
  223. }
  224. return -1;
  225. }
  226. int
  227. JackWinMMEDriver::Read()
  228. {
  229. jack_nframes_t buffer_size = fEngineControl->fBufferSize;
  230. for (int i = 0; i < fCaptureChannels; i++) {
  231. input_ports[i]->ProcessJack(GetInputBuffer(i), buffer_size);
  232. }
  233. return 0;
  234. }
  235. int
  236. JackWinMMEDriver::Write()
  237. {
  238. jack_nframes_t buffer_size = fEngineControl->fBufferSize;
  239. for (int i = 0; i < fPlaybackChannels; i++) {
  240. output_ports[i]->ProcessJack(GetOutputBuffer(i), buffer_size);
  241. }
  242. return 0;
  243. }
  244. int
  245. JackWinMMEDriver::Start()
  246. {
  247. jack_log("JackWinMMEDriver::Start - Starting driver.");
  248. JackMidiDriver::Start();
  249. int input_count = 0;
  250. int output_count = 0;
  251. jack_log("JackWinMMEDriver::Start - Enabling input ports.");
  252. for (; input_count < fCaptureChannels; input_count++) {
  253. if (! input_ports[input_count]->Start()) {
  254. jack_error("JackWinMMEDriver::Start - Failed to enable input "
  255. "port.");
  256. goto stop_input_ports;
  257. }
  258. }
  259. jack_log("JackWinMMEDriver::Start - Enabling output ports.");
  260. for (; output_count < fPlaybackChannels; output_count++) {
  261. if (! output_ports[output_count]->Start()) {
  262. jack_error("JackWinMMEDriver::Start - Failed to enable output "
  263. "port.");
  264. goto stop_output_ports;
  265. }
  266. }
  267. jack_log("JackWinMMEDriver::Start - Driver started.");
  268. return 0;
  269. stop_output_ports:
  270. for (int i = 0; i < output_count; i++) {
  271. if (! output_ports[i]->Stop()) {
  272. jack_error("JackWinMMEDriver::Start - Failed to disable output "
  273. "port.");
  274. }
  275. }
  276. stop_input_ports:
  277. for (int i = 0; i < input_count; i++) {
  278. if (! input_ports[i]->Stop()) {
  279. jack_error("JackWinMMEDriver::Start - Failed to disable input "
  280. "port.");
  281. }
  282. }
  283. return -1;
  284. }
  285. int
  286. JackWinMMEDriver::Stop()
  287. {
  288. int result = 0;
  289. JackMidiDriver::Stop();
  290. jack_log("JackWinMMEDriver::Stop - disabling input ports.");
  291. for (int i = 0; i < fCaptureChannels; i++) {
  292. if (! input_ports[i]->Stop()) {
  293. jack_error("JackWinMMEDriver::Stop - Failed to disable input "
  294. "port.");
  295. result = -1;
  296. }
  297. }
  298. jack_log("JackWinMMEDriver::Stop - disabling output ports.");
  299. for (int i = 0; i < fPlaybackChannels; i++) {
  300. if (! output_ports[i]->Stop()) {
  301. jack_error("JackWinMMEDriver::Stop - Failed to disable output "
  302. "port.");
  303. result = -1;
  304. }
  305. }
  306. return result;
  307. }
  308. #ifdef __cplusplus
  309. extern "C"
  310. {
  311. #endif
  312. // singleton kind of driver
  313. static Jack::JackWinMMEDriver* driver = NULL;
  314. SERVER_EXPORT jack_driver_desc_t * driver_get_descriptor()
  315. {
  316. return jack_driver_descriptor_construct("winmme", JackDriverSlave, "WinMME API based MIDI backend", NULL);
  317. }
  318. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  319. {
  320. /*
  321. unsigned int capture_ports = 2;
  322. unsigned int playback_ports = 2;
  323. unsigned long wait_time = 0;
  324. const JSList * node;
  325. const jack_driver_param_t * param;
  326. bool monitor = false;
  327. for (node = params; node; node = jack_slist_next (node)) {
  328. param = (const jack_driver_param_t *) node->data;
  329. switch (param->character) {
  330. case 'C':
  331. capture_ports = param->value.ui;
  332. break;
  333. case 'P':
  334. playback_ports = param->value.ui;
  335. break;
  336. case 'r':
  337. sample_rate = param->value.ui;
  338. break;
  339. case 'p':
  340. period_size = param->value.ui;
  341. break;
  342. case 'w':
  343. wait_time = param->value.ui;
  344. break;
  345. case 'm':
  346. monitor = param->value.i;
  347. break;
  348. }
  349. }
  350. */
  351. // singleton kind of driver
  352. if (!driver) {
  353. driver = new Jack::JackWinMMEDriver("system_midi", "winmme", engine, table);
  354. if (driver->Open(1, 1, 0, 0, false, "in", "out", 0, 0) == 0) {
  355. return driver;
  356. } else {
  357. delete driver;
  358. return NULL;
  359. }
  360. } else {
  361. jack_info("JackWinMMEDriver already allocated, cannot be loaded twice");
  362. return NULL;
  363. }
  364. }
  365. #ifdef __cplusplus
  366. }
  367. #endif