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.

504 lines
16KB

  1. /*
  2. Copyright (C) 2009 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackWinMMEDriver.h"
  16. #include "JackGraphManager.h"
  17. #include "JackEngineControl.h"
  18. #include "JackDriverLoader.h"
  19. #include <assert.h>
  20. #include <iostream>
  21. #include <sstream>
  22. #include <string>
  23. #include <windows.h>
  24. #include <windowsx.h>
  25. #include <mmsystem.h>
  26. namespace Jack
  27. {
  28. static bool InitHeaders(MidiSlot* slot)
  29. {
  30. slot->fHeader = (LPMIDIHDR)GlobalAllocPtr(GMEM_MOVEABLE|GMEM_SHARE|GMEM_ZEROINIT, sizeof(MIDIHDR) + kBuffSize);
  31. if (!slot->fHeader)
  32. return false;
  33. slot->fHeader->lpData = (LPSTR)((LPBYTE)slot->fHeader + sizeof(MIDIHDR));
  34. slot->fHeader->dwBufferLength = kBuffSize;
  35. slot->fHeader->dwFlags = 0;
  36. slot->fHeader->dwUser = 0;
  37. slot->fHeader->lpNext = 0;
  38. slot->fHeader->dwBytesRecorded = 0;
  39. return true;
  40. }
  41. void CALLBACK JackWinMMEDriver::MidiInProc(HMIDIIN hMidiIn, UINT wMsg, DWORD userData, DWORD dwParam1, DWORD dwParam2)
  42. {
  43. jack_ringbuffer_t* ringbuffer = (jack_ringbuffer_t*)userData;
  44. //jack_info("JackWinMMEDriver::MidiInProc 0\n");
  45. switch (wMsg) {
  46. case MIM_OPEN:
  47. break;
  48. case MIM_ERROR:
  49. case MIM_DATA: {
  50. //jack_info("JackWinMMEDriver::MidiInProc");
  51. // One event
  52. unsigned int num_packet = 1;
  53. jack_ringbuffer_write(ringbuffer, (char*)&num_packet, sizeof(unsigned int));
  54. // Write event actual data
  55. jack_ringbuffer_write(ringbuffer, (char*)&dwParam1, 3);
  56. break;
  57. }
  58. case MIM_LONGERROR:
  59. case MIM_LONGDATA:
  60. /*
  61. Nothing for now
  62. */
  63. break;
  64. }
  65. }
  66. JackWinMMEDriver::JackWinMMEDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table)
  67. : JackMidiDriver(name, alias, engine, table),
  68. fRealCaptureChannels(0),
  69. fRealPlaybackChannels(0),
  70. fMidiSource(NULL),
  71. fMidiDestination(NULL)
  72. {}
  73. JackWinMMEDriver::~JackWinMMEDriver()
  74. {}
  75. int JackWinMMEDriver::Open(jack_nframes_t buffer_size,
  76. jack_nframes_t samplerate,
  77. bool capturing,
  78. bool playing,
  79. int inchannels,
  80. int outchannels,
  81. bool monitor,
  82. const char* capture_driver_name,
  83. const char* playback_driver_name,
  84. jack_nframes_t capture_latency,
  85. jack_nframes_t playback_latency)
  86. {
  87. jack_log("JackWinMMEDriver::Open");
  88. fRealCaptureChannels = midiInGetNumDevs();
  89. fRealPlaybackChannels = midiOutGetNumDevs ();
  90. // Generic JackMidiDriver Open
  91. if (JackMidiDriver::Open(buffer_size, samplerate, capturing, playing, inchannels + fRealCaptureChannels, outchannels + fRealPlaybackChannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency) != 0)
  92. return -1;
  93. fMidiDestination = new MidiSlot[fRealCaptureChannels];
  94. assert(fMidiDestination);
  95. // Real input
  96. for (int i = 0; i < fRealCaptureChannels; i++) {
  97. HMIDIIN handle;
  98. fMidiDestination[i].fIndex = i;
  99. MMRESULT ret = midiInOpen(&handle, fMidiDestination[i].fIndex, (DWORD)MidiInProc, (DWORD)fRingBuffer[i], CALLBACK_FUNCTION);
  100. if (ret == MMSYSERR_NOERROR) {
  101. fMidiDestination[i].fHandle = handle;
  102. if (!InitHeaders(&fMidiDestination[i])) {
  103. jack_error("memory allocation failed");
  104. midiInClose(handle);
  105. goto error;
  106. //continue;
  107. }
  108. ret = midiInPrepareHeader(handle, fMidiDestination[i].fHeader, sizeof(MIDIHDR));
  109. if (ret == MMSYSERR_NOERROR) {
  110. fMidiDestination[i].fHeader->dwUser = 1;
  111. ret = midiInAddBuffer(handle, fMidiDestination[i].fHeader, sizeof(MIDIHDR));
  112. if (ret == MMSYSERR_NOERROR) {
  113. ret = midiInStart(handle);
  114. if (ret != MMSYSERR_NOERROR) {
  115. jack_error("midiInStart error");
  116. CloseInput(&fMidiDestination[i]);
  117. goto error;
  118. }
  119. } else {
  120. jack_error ("midiInAddBuffer error");
  121. CloseInput(&fMidiDestination[i]);
  122. goto error;
  123. }
  124. } else {
  125. jack_error("midiInPrepareHeader error");
  126. midiInClose(handle);
  127. goto error;
  128. }
  129. } else {
  130. jack_error ("midiInOpen error");
  131. goto error;
  132. }
  133. }
  134. fMidiSource = new MidiSlot[fRealPlaybackChannels];
  135. assert(fMidiSource);
  136. // Real output
  137. for (int i = 0; i < fRealPlaybackChannels; i++) {
  138. MMRESULT res;
  139. HMIDIOUT handle;
  140. fMidiSource[i].fIndex = i;
  141. UINT ret = midiOutOpen(&handle, fMidiSource[i].fIndex, 0L, 0L, CALLBACK_NULL);
  142. if (ret == MMSYSERR_NOERROR) {
  143. fMidiSource[i].fHandle = handle;
  144. if (!InitHeaders(&fMidiSource[i])) {
  145. jack_error("memory allocation failed");
  146. midiOutClose(handle);
  147. //continue;
  148. goto error;
  149. }
  150. res = midiOutPrepareHeader(handle, fMidiSource[i].fHeader, sizeof(MIDIHDR));
  151. if (res != MMSYSERR_NOERROR) {
  152. jack_error("midiOutPrepareHeader error %d %d %d", i, handle, res);
  153. //continue;
  154. goto error;
  155. } else {
  156. fMidiSource[i].fHeader->dwUser = 1;
  157. }
  158. } else {
  159. jack_error("midiOutOpen error");
  160. goto error;
  161. }
  162. }
  163. return 0;
  164. error:
  165. Close();
  166. return -1;
  167. }
  168. void JackWinMMEDriver::CloseInput(MidiSlot* slot)
  169. {
  170. MMRESULT res;
  171. int retry = 0;
  172. if (slot->fHandle == 0)
  173. return;
  174. HMIDIIN handle = (HMIDIIN)slot->fHandle;
  175. slot->fHeader->dwUser = 0;
  176. res = midiInStop(handle);
  177. if (res != MMSYSERR_NOERROR) {
  178. jack_error("midiInStop error");
  179. }
  180. res = midiInReset(handle);
  181. if (res != MMSYSERR_NOERROR) {
  182. jack_error("midiInReset error");
  183. }
  184. res = midiInUnprepareHeader(handle, slot->fHeader, sizeof(MIDIHDR));
  185. if (res != MMSYSERR_NOERROR) {
  186. jack_error("midiInUnprepareHeader error");
  187. }
  188. do {
  189. res = midiInClose(handle);
  190. if (res != MMSYSERR_NOERROR) {
  191. jack_error("midiInClose error");
  192. }
  193. if (res == MIDIERR_STILLPLAYING)
  194. midiInReset(handle);
  195. Sleep (10);
  196. retry++;
  197. } while ((res == MIDIERR_STILLPLAYING) && (retry < 10));
  198. if (slot->fHeader) {
  199. GlobalFreePtr(slot->fHeader);
  200. }
  201. }
  202. void JackWinMMEDriver::CloseOutput(MidiSlot* slot)
  203. {
  204. MMRESULT res;
  205. int retry = 0;
  206. if (slot->fHandle == 0)
  207. return;
  208. HMIDIOUT handle = (HMIDIOUT)slot->fHandle;
  209. res = midiOutReset(handle);
  210. if (res != MMSYSERR_NOERROR)
  211. jack_error("midiOutReset error");
  212. midiOutUnprepareHeader(handle, slot->fHeader, sizeof(MIDIHDR));
  213. do {
  214. res = midiOutClose(handle);
  215. if (res != MMSYSERR_NOERROR)
  216. jack_error("midiOutClose error");
  217. Sleep(10);
  218. retry++;
  219. } while ((res == MIDIERR_STILLPLAYING) && (retry < 10));
  220. if (slot->fHeader) {
  221. GlobalFreePtr(slot->fHeader);
  222. }
  223. }
  224. int JackWinMMEDriver::Close()
  225. {
  226. jack_log("JackWinMMEDriver::Close");
  227. // Close input
  228. if (fMidiDestination) {
  229. for (int i = 0; i < fRealCaptureChannels; i++) {
  230. CloseInput(&fMidiDestination[i]);
  231. }
  232. delete[] fMidiDestination;
  233. }
  234. // Close output
  235. if (fMidiSource) {
  236. for (int i = 0; i < fRealPlaybackChannels; i++) {
  237. CloseOutput(&fMidiSource[i]);
  238. }
  239. delete[] fMidiSource;
  240. }
  241. return 0;
  242. }
  243. int JackWinMMEDriver::Attach()
  244. {
  245. JackPort* port;
  246. jack_port_id_t port_index;
  247. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  248. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  249. unsigned long port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  250. MMRESULT res;
  251. int i;
  252. jack_log("JackMidiDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  253. for (i = 0; i < fCaptureChannels; i++) {
  254. MIDIINCAPS caps;
  255. res = midiInGetDevCaps(i, &caps, sizeof(caps));
  256. if (res == MMSYSERR_NOERROR) {
  257. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, caps.szPname, i + 1);
  258. } else {
  259. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
  260. }
  261. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
  262. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  263. jack_error("driver: cannot register port for %s", name);
  264. return -1;
  265. }
  266. port = fGraphManager->GetPort(port_index);
  267. port->SetAlias(alias);
  268. fCapturePortList[i] = port_index;
  269. jack_log("JackMidiDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
  270. }
  271. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  272. for (i = 0; i < fPlaybackChannels; i++) {
  273. MIDIOUTCAPS caps;
  274. res = midiOutGetDevCaps(i, &caps, sizeof(caps));
  275. if (res == MMSYSERR_NOERROR) {
  276. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, caps.szPname, i + 1);
  277. } else {
  278. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", fAliasName, fPlaybackDriverName, i + 1);
  279. }
  280. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
  281. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_MIDI_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  282. jack_error("driver: cannot register port for %s", name);
  283. return -1;
  284. }
  285. port = fGraphManager->GetPort(port_index);
  286. port->SetAlias(alias);
  287. fPlaybackPortList[i] = port_index;
  288. jack_log("JackMidiDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
  289. }
  290. return 0;
  291. }
  292. int JackWinMMEDriver::Read()
  293. {
  294. size_t size;
  295. for (int chan = 0; chan < fCaptureChannels; chan++) {
  296. if (fGraphManager->GetConnectionsNum(fCapturePortList[chan]) > 0) {
  297. JackMidiBuffer* midi_buffer = GetInputBuffer(chan);
  298. if (jack_ringbuffer_read_space (fRingBuffer[chan]) == 0) {
  299. // Reset buffer
  300. midi_buffer->Reset(midi_buffer->nframes);
  301. } else {
  302. while ((size = jack_ringbuffer_read_space (fRingBuffer[chan])) > 0) {
  303. //jack_info("jack_ringbuffer_read_space %d", size);
  304. int ev_count = 0;
  305. jack_ringbuffer_read(fRingBuffer[chan], (char*)&ev_count, sizeof(int));
  306. if (ev_count > 0) {
  307. for (int j = 0; j < ev_count; j++) {
  308. unsigned int event_len = 3;
  309. // Read event actual data
  310. jack_midi_data_t* dest = midi_buffer->ReserveEvent(0, event_len);
  311. jack_ringbuffer_read(fRingBuffer[chan], (char*)dest, event_len);
  312. }
  313. }
  314. }
  315. }
  316. } else {
  317. //jack_info("Consume ring buffer");
  318. jack_ringbuffer_read_advance(fRingBuffer[chan], jack_ringbuffer_read_space(fRingBuffer[chan]));
  319. }
  320. }
  321. return 0;
  322. }
  323. int JackWinMMEDriver::Write()
  324. {
  325. for (int chan = 0; chan < fPlaybackChannels; chan++) {
  326. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[chan]) > 0) {
  327. JackMidiBuffer* midi_buffer = GetOutputBuffer(chan);
  328. // TODO : use timestamp
  329. for (unsigned int j = 0; j < midi_buffer->event_count; j++) {
  330. JackMidiEvent* ev = &midi_buffer->events[j];
  331. if (ev->size <= 3) {
  332. MMRESULT res = midiOutShortMsg((HMIDIOUT)fMidiSource[chan].fHandle, *((DWORD*)ev->GetData(midi_buffer)));
  333. if (res != MMSYSERR_NOERROR)
  334. jack_error ("midiOutShortMsg error res %d", res);
  335. } else {
  336. }
  337. }
  338. }
  339. }
  340. return 0;
  341. }
  342. } // end of namespace
  343. #ifdef __cplusplus
  344. extern "C"
  345. {
  346. #endif
  347. SERVER_EXPORT jack_driver_desc_t * driver_get_descriptor()
  348. {
  349. jack_driver_desc_t * desc;
  350. unsigned int i;
  351. desc = (jack_driver_desc_t*)calloc (1, sizeof (jack_driver_desc_t));
  352. strcpy(desc->name, "dummy"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
  353. strcpy(desc->desc, "Timer based backend"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
  354. desc->nparams = 6;
  355. desc->params = (jack_driver_param_desc_t*)calloc (desc->nparams, sizeof (jack_driver_param_desc_t));
  356. return desc;
  357. }
  358. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  359. {
  360. /*
  361. unsigned int capture_ports = 2;
  362. unsigned int playback_ports = 2;
  363. unsigned long wait_time = 0;
  364. const JSList * node;
  365. const jack_driver_param_t * param;
  366. bool monitor = false;
  367. for (node = params; node; node = jack_slist_next (node)) {
  368. param = (const jack_driver_param_t *) node->data;
  369. switch (param->character) {
  370. case 'C':
  371. capture_ports = param->value.ui;
  372. break;
  373. case 'P':
  374. playback_ports = param->value.ui;
  375. break;
  376. case 'r':
  377. sample_rate = param->value.ui;
  378. break;
  379. case 'p':
  380. period_size = param->value.ui;
  381. break;
  382. case 'w':
  383. wait_time = param->value.ui;
  384. break;
  385. case 'm':
  386. monitor = param->value.i;
  387. break;
  388. }
  389. }
  390. */
  391. Jack::JackDriverClientInterface* driver = new Jack::JackWinMMEDriver("system_midi", "winmme", engine, table);
  392. if (driver->Open(1, 1, 0, 0, false, "in", "out", 0, 0) == 0) {
  393. return driver;
  394. } else {
  395. delete driver;
  396. return NULL;
  397. }
  398. }
  399. #ifdef __cplusplus
  400. }
  401. #endif
  402. /*
  403. jack_connect system:midi_capture_1 system_midi:playback_1
  404. jack_connect system:midi_capture_1 system_midi:playback_2
  405. jack_connect system:midi_capture_1 system_midi:playback_1
  406. jack_connect system:midi_capture_1 system_midi:playback_1
  407. jack_connect system:midi_capture_1 system_midi:playback_1
  408. jack_connect system_midi:capture_1 system:midi_playback_1
  409. jack_connect system_midi:capture_2 system:midi_playback_1
  410. jack_connect system_midi:capture_1 system_midi:playback_1
  411. */