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.

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