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.

317 lines
11KB

  1. /*
  2. Copyright (C) 2011 Devin Anderson
  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 <cassert>
  16. #include <memory>
  17. #include <stdexcept>
  18. #include "JackError.h"
  19. #include "JackTime.h"
  20. #include "JackMidiUtil.h"
  21. #include "JackWinMMEInputPort.h"
  22. #include "JackMidiWriteQueue.h"
  23. using Jack::JackWinMMEInputPort;
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // Static callbacks
  26. ///////////////////////////////////////////////////////////////////////////////
  27. void CALLBACK
  28. JackWinMMEInputPort::HandleMidiInputEvent(HMIDIIN handle, UINT message,
  29. DWORD_PTR port,
  30. DWORD_PTR param1,
  31. DWORD_PTR param2)
  32. {
  33. ((JackWinMMEInputPort *) port)->ProcessWinMME(message, param1, param2);
  34. }
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // Class
  37. ///////////////////////////////////////////////////////////////////////////////
  38. JackWinMMEInputPort::JackWinMMEInputPort(const char *alias_name,
  39. const char *client_name,
  40. const char *driver_name, UINT index,
  41. size_t max_bytes, size_t max_messages)
  42. {
  43. thread_queue = new JackMidiAsyncQueue(max_bytes, max_messages);
  44. std::unique_ptr<JackMidiAsyncQueue> thread_queue_ptr(thread_queue);
  45. write_queue = new JackMidiBufferWriteQueue();
  46. std::unique_ptr<JackMidiBufferWriteQueue> write_queue_ptr(write_queue);
  47. sysex_buffer = new jack_midi_data_t[max_bytes];
  48. char error_message[MAXERRORLENGTH];
  49. MMRESULT result = midiInOpen(&handle, index,
  50. (DWORD_PTR) HandleMidiInputEvent,
  51. (DWORD_PTR)this,
  52. CALLBACK_FUNCTION | MIDI_IO_STATUS);
  53. if (result != MMSYSERR_NOERROR) {
  54. GetInErrorString(result, error_message);
  55. goto delete_sysex_buffer;
  56. }
  57. sysex_header.dwBufferLength = max_bytes;
  58. sysex_header.dwFlags = 0;
  59. sysex_header.lpData = (LPSTR)sysex_buffer;
  60. result = midiInPrepareHeader(handle, &sysex_header, sizeof(MIDIHDR));
  61. if (result != MMSYSERR_NOERROR) {
  62. GetInErrorString(result, error_message);
  63. goto close_handle;
  64. }
  65. result = midiInAddBuffer(handle, &sysex_header, sizeof(MIDIHDR));
  66. if (result != MMSYSERR_NOERROR) {
  67. GetInErrorString(result, error_message);
  68. goto unprepare_header;
  69. }
  70. MIDIINCAPS capabilities;
  71. char *name_tmp;
  72. result = midiInGetDevCaps(index, &capabilities, sizeof(capabilities));
  73. if (result != MMSYSERR_NOERROR) {
  74. WriteInError("JackWinMMEInputPort [constructor]", "midiInGetDevCaps",
  75. result);
  76. name_tmp = (char*) driver_name;
  77. } else {
  78. name_tmp = capabilities.szPname;
  79. }
  80. snprintf(alias, sizeof(alias) - 1, "%s:%s:in%d", alias_name, name_tmp,
  81. index + 1);
  82. snprintf(name, sizeof(name) - 1, "%s:capture_%d", client_name, index + 1);
  83. strncpy(device_name, name_tmp, sizeof(device_name) - 1);
  84. jack_event = 0;
  85. started = false;
  86. write_queue_ptr.release();
  87. thread_queue_ptr.release();
  88. return;
  89. unprepare_header:
  90. result = midiInUnprepareHeader(handle, &sysex_header, sizeof(MIDIHDR));
  91. if (result != MMSYSERR_NOERROR) {
  92. WriteInError("JackWinMMEInputPort [constructor]",
  93. "midiInUnprepareHeader", result);
  94. }
  95. close_handle:
  96. result = midiInClose(handle);
  97. if (result != MMSYSERR_NOERROR) {
  98. WriteInError("JackWinMMEInputPort [constructor]", "midiInClose",
  99. result);
  100. }
  101. delete_sysex_buffer:
  102. delete[] sysex_buffer;
  103. throw std::runtime_error(error_message);
  104. }
  105. JackWinMMEInputPort::~JackWinMMEInputPort()
  106. {
  107. MMRESULT result = midiInReset(handle);
  108. if (result != MMSYSERR_NOERROR) {
  109. WriteInError("JackWinMMEInputPort [destructor]", "midiInReset", result);
  110. }
  111. result = midiInUnprepareHeader(handle, &sysex_header, sizeof(MIDIHDR));
  112. if (result != MMSYSERR_NOERROR) {
  113. WriteInError("JackWinMMEInputPort [destructor]",
  114. "midiInUnprepareHeader", result);
  115. }
  116. result = midiInClose(handle);
  117. if (result != MMSYSERR_NOERROR) {
  118. WriteInError("JackWinMMEInputPort [destructor]", "midiInClose", result);
  119. }
  120. delete[] sysex_buffer;
  121. delete thread_queue;
  122. delete write_queue;
  123. }
  124. void
  125. JackWinMMEInputPort::EnqueueMessage(DWORD timestamp, size_t length,
  126. jack_midi_data_t *data)
  127. {
  128. jack_nframes_t frame =
  129. GetFramesFromTime(start_time + (((jack_time_t) timestamp) * 1000));
  130. // Debugging code
  131. jack_time_t current_time = GetMicroSeconds();
  132. jack_log("JackWinMMEInputPort::EnqueueMessage - enqueueing event at %f "
  133. "(frame: %d) with start offset '%d' scheduled for frame '%d'",
  134. ((double) current_time) / 1000.0, GetFramesFromTime(current_time),
  135. timestamp, frame);
  136. // End debugging code
  137. switch (thread_queue->EnqueueEvent(frame, length, data)) {
  138. case JackMidiWriteQueue::BUFFER_FULL:
  139. jack_error("JackWinMMEInputPort::EnqueueMessage - The thread queue "
  140. "cannot currently accept a %d-byte event. Dropping event.",
  141. length);
  142. break;
  143. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  144. jack_error("JackWinMMEInputPort::EnqueueMessage - The thread queue "
  145. "buffer is too small to enqueue a %d-byte event. Dropping "
  146. "event.", length);
  147. break;
  148. default:
  149. ;
  150. }
  151. }
  152. void
  153. JackWinMMEInputPort::GetInErrorString(MMRESULT error, LPTSTR text)
  154. {
  155. MMRESULT result = midiInGetErrorText(error, text, MAXERRORLENGTH);
  156. if (result != MMSYSERR_NOERROR) {
  157. snprintf(text, MAXERRORLENGTH, "Unknown error code '%d'", error);
  158. }
  159. }
  160. void
  161. JackWinMMEInputPort::ProcessJack(JackMidiBuffer *port_buffer,
  162. jack_nframes_t frames)
  163. {
  164. write_queue->ResetMidiBuffer(port_buffer, frames);
  165. if (! jack_event) {
  166. jack_event = thread_queue->DequeueEvent();
  167. }
  168. for (; jack_event; jack_event = thread_queue->DequeueEvent()) {
  169. switch (write_queue->EnqueueEvent(jack_event, frames)) {
  170. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  171. jack_error("JackWinMMEMidiInputPort::Process - The buffer write "
  172. "queue couldn't enqueue a %d-byte event. Dropping "
  173. "event.", jack_event->size);
  174. // Fallthrough on purpose
  175. case JackMidiWriteQueue::OK:
  176. continue;
  177. default:
  178. break;
  179. }
  180. break;
  181. }
  182. }
  183. void
  184. JackWinMMEInputPort::ProcessWinMME(UINT message, DWORD_PTR param1, DWORD_PTR param2)
  185. {
  186. set_threaded_log_function();
  187. switch (message) {
  188. case MIM_CLOSE:
  189. jack_log("JackWinMMEInputPort::ProcessWinMME - MIDI device closed.");
  190. break;
  191. case MIM_MOREDATA:
  192. jack_log("JackWinMMEInputPort::ProcessWinMME - The MIDI input device "
  193. "driver thinks that JACK is not processing messages fast "
  194. "enough.");
  195. // Fallthrough on purpose.
  196. case MIM_DATA: {
  197. jack_midi_data_t message_buffer[3];
  198. jack_midi_data_t status = param1 & 0xff;
  199. int length = GetMessageLength(status);
  200. switch (length) {
  201. case 3:
  202. message_buffer[2] = (param1 >> 16) & 0xff;
  203. // Fallthrough on purpose.
  204. case 2:
  205. message_buffer[1] = (param1 >> 8) & 0xff;
  206. // Fallthrough on purpose.
  207. case 1:
  208. message_buffer[0] = status;
  209. break;
  210. case 0:
  211. jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
  212. "input driver sent an MIM_DATA message with a sysex "
  213. "status byte.");
  214. return;
  215. case -1:
  216. jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
  217. "input driver sent an MIM_DATA message with an invalid "
  218. "status byte.");
  219. return;
  220. }
  221. EnqueueMessage(param2, (size_t) length, message_buffer);
  222. break;
  223. }
  224. case MIM_LONGDATA: {
  225. LPMIDIHDR header = (LPMIDIHDR) param1;
  226. size_t byte_count = header->dwBytesRecorded;
  227. if (! byte_count) {
  228. jack_log("JackWinMMEInputPort::ProcessWinMME - WinMME driver has "
  229. "returned sysex header to us with no bytes. The JACK "
  230. "driver is probably being stopped.");
  231. break;
  232. }
  233. jack_midi_data_t *data = (jack_midi_data_t *) header->lpData;
  234. if ((data[0] != 0xf0) || (data[byte_count - 1] != 0xf7)) {
  235. jack_error("JackWinMMEInputPort::ProcessWinMME - Discarding "
  236. "%d-byte sysex chunk.", byte_count);
  237. } else {
  238. EnqueueMessage(param2, byte_count, data);
  239. }
  240. // Is this realtime-safe? This function isn't run in the JACK thread,
  241. // but we still want it to perform as quickly as possible. Even if
  242. // this isn't realtime safe, it may not be avoidable.
  243. MMRESULT result = midiInAddBuffer(handle, &sysex_header,
  244. sizeof(MIDIHDR));
  245. if (result != MMSYSERR_NOERROR) {
  246. WriteInError("JackWinMMEInputPort::ProcessWinMME",
  247. "midiInAddBuffer", result);
  248. }
  249. break;
  250. }
  251. case MIM_LONGERROR:
  252. jack_error("JackWinMMEInputPort::ProcessWinMME - Invalid or "
  253. "incomplete sysex message received.");
  254. break;
  255. case MIM_OPEN:
  256. jack_log("JackWinMMEInputPort::ProcessWinMME - MIDI device opened.");
  257. }
  258. }
  259. bool
  260. JackWinMMEInputPort::Start()
  261. {
  262. if (! started) {
  263. start_time = GetMicroSeconds();
  264. MMRESULT result = midiInStart(handle);
  265. started = result == MMSYSERR_NOERROR;
  266. if (! started) {
  267. WriteInError("JackWinMMEInputPort::Start", "midiInStart", result);
  268. }
  269. }
  270. return started;
  271. }
  272. bool
  273. JackWinMMEInputPort::Stop()
  274. {
  275. if (started) {
  276. MMRESULT result = midiInStop(handle);
  277. started = result != MMSYSERR_NOERROR;
  278. if (started) {
  279. WriteInError("JackWinMMEInputPort::Stop", "midiInStop", result);
  280. }
  281. }
  282. return ! started;
  283. }
  284. void
  285. JackWinMMEInputPort::WriteInError(const char *jack_func, const char *mm_func,
  286. MMRESULT result)
  287. {
  288. char error_message[MAXERRORLENGTH];
  289. GetInErrorString(result, error_message);
  290. jack_error("%s - %s: %s", jack_func, mm_func, error_message);
  291. }