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.

299 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 = (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. 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. 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]", "midiInUnprepareHeader",
  112. 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(jack_nframes_t time, size_t length,
  124. jack_midi_data_t *data)
  125. {
  126. switch (thread_queue->EnqueueEvent(time, length, data)) {
  127. case JackMidiWriteQueue::BUFFER_FULL:
  128. jack_error("JackWinMMEInputPort::EnqueueMessage - The thread queue "
  129. "cannot currently accept a %d-byte event. Dropping event.",
  130. length);
  131. break;
  132. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  133. jack_error("JackWinMMEInputPort::EnqueueMessage - The thread queue "
  134. "buffer is too small to enqueue a %d-byte event. Dropping "
  135. "event.", length);
  136. break;
  137. default:
  138. ;
  139. }
  140. }
  141. void
  142. JackWinMMEInputPort::ProcessJack(JackMidiBuffer *port_buffer,
  143. jack_nframes_t frames)
  144. {
  145. write_queue->ResetMidiBuffer(port_buffer, frames);
  146. if (! jack_event) {
  147. jack_event = thread_queue->DequeueEvent();
  148. }
  149. for (; jack_event; jack_event = thread_queue->DequeueEvent()) {
  150. switch (write_queue->EnqueueEvent(jack_event)) {
  151. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  152. jack_error("JackWinMMEMidiInputPort::Process - The buffer write "
  153. "queue couldn't enqueue a %d-byte event. Dropping "
  154. "event.", jack_event->size);
  155. // Fallthrough on purpose
  156. case JackMidiWriteQueue::OK:
  157. continue;
  158. }
  159. break;
  160. }
  161. }
  162. void
  163. JackWinMMEInputPort::ProcessWinMME(UINT message, DWORD param1, DWORD param2)
  164. {
  165. set_threaded_log_function();
  166. jack_nframes_t current_frame = GetCurrentFrame();
  167. switch (message) {
  168. case MIM_CLOSE:
  169. jack_info("JackWinMMEInputPort::ProcessWinMME - MIDI device closed.");
  170. break;
  171. case MIM_MOREDATA:
  172. jack_info("JackWinMMEInputPort::ProcessWinMME - The MIDI input device "
  173. "driver thinks that JACK is not processing messages fast "
  174. "enough.");
  175. // Fallthrough on purpose.
  176. case MIM_DATA: {
  177. jack_midi_data_t message_buffer[3];
  178. jack_midi_data_t status = param1 & 0xff;
  179. int length = GetMessageLength(status);
  180. switch (length) {
  181. case 3:
  182. message_buffer[2] = (param1 >> 16) & 0xff;
  183. // Fallthrough on purpose.
  184. case 2:
  185. message_buffer[1] = (param1 >> 8) & 0xff;
  186. // Fallthrough on purpose.
  187. case 1:
  188. message_buffer[0] = status;
  189. break;
  190. case 0:
  191. jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
  192. "input driver sent an MIM_DATA message with a sysex "
  193. "status byte.");
  194. return;
  195. case -1:
  196. jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
  197. "input driver sent an MIM_DATA message with an invalid "
  198. "status byte.");
  199. return;
  200. }
  201. EnqueueMessage(current_frame, (size_t) length, message_buffer);
  202. break;
  203. }
  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.", length1);
  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. }
  225. case MIM_LONGERROR:
  226. jack_error("JackWinMMEInputPort::ProcessWinMME - Invalid or "
  227. "incomplete sysex message received.");
  228. break;
  229. case MIM_OPEN:
  230. jack_info("JackWinMMEInputPort::ProcessWinMME - MIDI device opened.");
  231. }
  232. }
  233. bool
  234. JackWinMMEInputPort::Start()
  235. {
  236. if (! started) {
  237. MMRESULT result = midiInStart(handle);
  238. started = result == MMSYSERR_NOERROR;
  239. if (! started) {
  240. WriteInError("JackWinMMEInputPort::Start", "midiInStart", result);
  241. }
  242. }
  243. return started;
  244. }
  245. bool
  246. JackWinMMEInputPort::Stop()
  247. {
  248. if (started) {
  249. MMRESULT result = midiInStop(handle);
  250. started = result != MMSYSERR_NOERROR;
  251. if (started) {
  252. WriteInError("JackWinMMEInputPort::Stop", "midiInStop", result);
  253. }
  254. }
  255. return ! started;
  256. }
  257. void
  258. JackWinMMEInputPort::GetInErrorString(MMRESULT error, LPTSTR text)
  259. {
  260. MMRESULT result = midiInGetErrorText(error, text, MAXERRORLENGTH);
  261. if (result != MMSYSERR_NOERROR) {
  262. snprintf(text, MAXERRORLENGTH, "Unknown error code '%d'", error);
  263. }
  264. }
  265. void
  266. JackWinMMEInputPort::WriteInError(const char *jack_func, const char *mm_func,
  267. MMRESULT result)
  268. {
  269. char error_message[MAXERRORLENGTH];
  270. GetInErrorString(result, error_message);
  271. jack_error("%s - %s: %s", jack_func, mm_func, error_message);
  272. }