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.

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