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.

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