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.

293 lines
10KB

  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 "JackMidiUtil.h"
  20. #include "JackWinMMEInputPort.h"
  21. #include "JackMidiWriteQueue.h"
  22. using Jack::JackWinMMEInputPort;
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // Static callbacks
  25. ///////////////////////////////////////////////////////////////////////////////
  26. void CALLBACK
  27. JackWinMMEInputPort::HandleMidiInputEvent(HMIDIIN handle, UINT message,
  28. DWORD port, DWORD param1,
  29. DWORD param2)
  30. {
  31. ((JackWinMMEInputPort *) port)->ProcessWinMME(message, param1, param2);
  32. }
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // Class
  35. ///////////////////////////////////////////////////////////////////////////////
  36. JackWinMMEInputPort::JackWinMMEInputPort(const char *alias_name,
  37. const char *client_name,
  38. const char *driver_name, UINT index,
  39. size_t max_bytes, size_t max_messages)
  40. {
  41. thread_queue = new JackMidiAsyncQueue(max_bytes, max_messages);
  42. std::auto_ptr<JackMidiAsyncQueue> thread_queue_ptr(thread_queue);
  43. write_queue = new JackMidiBufferWriteQueue();
  44. std::auto_ptr<JackMidiBufferWriteQueue> write_queue_ptr(write_queue);
  45. sysex_buffer = new jack_midi_data_t[max_bytes];
  46. char error_message[MAXERRORLENGTH];
  47. MMRESULT result = midiInOpen(&handle, index, (DWORD)HandleMidiInputEvent,
  48. (DWORD)this,
  49. CALLBACK_FUNCTION | MIDI_IO_STATUS);
  50. if (result != MMSYSERR_NOERROR) {
  51. GetInErrorString(result, error_message);
  52. goto delete_sysex_buffer;
  53. }
  54. sysex_header.dwBufferLength = max_bytes;
  55. sysex_header.dwBytesRecorded = 0;
  56. sysex_header.dwFlags = 0;
  57. sysex_header.dwUser = 0;
  58. sysex_header.lpData = (LPSTR)(((LPBYTE) &sysex_header) + sizeof(MIDIHDR));
  59. sysex_header.lpNext = 0;
  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 = 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. 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", result);
  98. }
  99. delete_sysex_buffer:
  100. delete[] sysex_buffer;
  101. throw std::runtime_error(error_message);
  102. }
  103. JackWinMMEInputPort::~JackWinMMEInputPort()
  104. {
  105. Stop();
  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]", "midiInUnprepareHeader",
  113. 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(jack_nframes_t time, size_t length,
  125. jack_midi_data_t *data)
  126. {
  127. switch (thread_queue->EnqueueEvent(time, length, data)) {
  128. case JackMidiWriteQueue::BUFFER_FULL:
  129. jack_error("JackWinMMEInputPort::EnqueueMessage - The thread queue "
  130. "cannot currently accept a %d-byte event. Dropping event.",
  131. length);
  132. break;
  133. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  134. jack_error("JackWinMMEInputPort::EnqueueMessage - The thread queue "
  135. "buffer is too small to enqueue a %d-byte event. Dropping "
  136. "event.", length);
  137. break;
  138. default:
  139. ;
  140. }
  141. }
  142. void
  143. JackWinMMEInputPort::ProcessJack(JackMidiBuffer *port_buffer,
  144. jack_nframes_t frames)
  145. {
  146. write_queue->ResetMidiBuffer(port_buffer, frames);
  147. if (! jack_event) {
  148. jack_event = thread_queue->DequeueEvent();
  149. }
  150. for (; jack_event; jack_event = thread_queue->DequeueEvent()) {
  151. switch (write_queue->EnqueueEvent(jack_event)) {
  152. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  153. jack_error("JackWinMMEMidiInputPort::Process - The buffer write "
  154. "queue couldn't enqueue a %d-byte event. Dropping "
  155. "event.", jack_event->size);
  156. // Fallthrough on purpose
  157. case JackMidiWriteQueue::OK:
  158. continue;
  159. }
  160. break;
  161. }
  162. }
  163. void
  164. JackWinMMEInputPort::ProcessWinMME(UINT message, DWORD param1, DWORD param2)
  165. {
  166. set_threaded_log_function();
  167. jack_nframes_t current_frame = GetCurrentFrame();
  168. switch (message) {
  169. case MIM_CLOSE:
  170. jack_info("JackWinMMEInputPort::ProcessWinMME - MIDI device closed.");
  171. break;
  172. case MIM_MOREDATA:
  173. jack_info("JackWinMMEInputPort::ProcessWinMME - The MIDI input device "
  174. "driver thinks that JACK is not processing messages fast "
  175. "enough.");
  176. // Fallthrough on purpose.
  177. case MIM_DATA:
  178. jack_midi_data_t message_buffer[3];
  179. jack_midi_data_t status = param1 & 0xff;
  180. int length = GetMessageLength(status);
  181. switch (length) {
  182. case 3:
  183. message_buffer[2] = param1 & 0xff0000;
  184. // Fallthrough on purpose.
  185. case 2:
  186. message_buffer[1] = param1 & 0xff00;
  187. // Fallthrough on purpose.
  188. case 1:
  189. message_buffer[0] = status;
  190. break;
  191. case 0:
  192. jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
  193. "input driver sent an MIM_DATA message with a sysex "
  194. "status byte.");
  195. return;
  196. case -1:
  197. jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
  198. "input driver sent an MIM_DATA message with an invalid "
  199. "status byte.");
  200. return;
  201. }
  202. EnqueueMessage(current_frame, (size_t) length, message_buffer);
  203. break;
  204. case MIM_LONGDATA:
  205. LPMIDIHDR header = (LPMIDIHDR) param1;
  206. jack_midi_data_t *data = (jack_midi_data_t *) header->lpData;
  207. size_t length1 = header->dwBytesRecorded;
  208. if ((data[0] != 0xf0) || (data[length1 - 1] != 0xf7)) {
  209. jack_error("JackWinMMEInputPort::ProcessWinMME - Discarding "
  210. "%d-byte sysex chunk.", length);
  211. } else {
  212. EnqueueMessage(current_frame, length1, data);
  213. }
  214. // Is this realtime-safe? This function isn't run in the JACK thread,
  215. // but we still want it to perform as quickly as possible. Even if
  216. // this isn't realtime safe, it may not be avoidable.
  217. MMRESULT result = midiInAddBuffer(handle, &sysex_header,
  218. sizeof(MIDIHDR));
  219. if (result != MMSYSERR_NOERROR) {
  220. WriteInError("JackWinMMEInputPort::ProcessWinMME", "midiInAddBuffer",
  221. result);
  222. }
  223. break;
  224. case MIM_LONGERROR:
  225. jack_error("JackWinMMEInputPort::ProcessWinMME - Invalid or "
  226. "incomplete sysex message received.");
  227. break;
  228. case MIM_OPEN:
  229. jack_info("JackWinMMEInputPort::ProcessWinMME - MIDI device opened.");
  230. }
  231. }
  232. bool
  233. JackWinMMEInputPort::Start()
  234. {
  235. if (! started) {
  236. MMRESULT result = midiInStart(handle);
  237. started = result == MMSYSERR_NOERROR;
  238. if (! started) {
  239. WriteInError("JackWinMMEInputPort::Start", "midiInStart", result);
  240. }
  241. }
  242. return started;
  243. }
  244. bool
  245. JackWinMMEInputPort::Stop()
  246. {
  247. if (started) {
  248. MMRESULT result = midiInStop(handle);
  249. started = result != MMSYSERR_NOERROR;
  250. if (started) {
  251. WriteInError("JackWinMMEInputPort::Stop", "midiInStop", result);
  252. }
  253. }
  254. return ! started;
  255. }
  256. void
  257. JackWinMMEInputPort::GetInErrorString(MMRESULT error, LPTSTR text)
  258. {
  259. MMRESULT result = midiInGetErrorText(error, text, MAXERRORLENGTH);
  260. if (result != MMSYSERR_NOERROR) {
  261. snprintf(text, MAXERRORLENGTH, "Unknown error code '%d'", error);
  262. }
  263. }
  264. void
  265. JackWinMMEInputPort::WriteInError(const char *jack_func, const char *mm_func,
  266. MMRESULT result)
  267. {
  268. char error_message[MAXERRORLENGTH];
  269. GetInErrorString(result, error_message);
  270. jack_error("%s - %s: %s", jack_func, mm_func, error_message);
  271. }