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.

303 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::auto_ptr<JackMidiAsyncQueue> thread_queue_ptr(thread_queue);
  44. write_queue = new JackMidiBufferWriteQueue();
  45. std::auto_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 = GetFramesFromTime(start_time + (timestamp * 1000));
  127. switch (thread_queue->EnqueueEvent(frame, 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::GetInErrorString(MMRESULT error, LPTSTR text)
  144. {
  145. MMRESULT result = midiInGetErrorText(error, text, MAXERRORLENGTH);
  146. if (result != MMSYSERR_NOERROR) {
  147. snprintf(text, MAXERRORLENGTH, "Unknown error code '%d'", error);
  148. }
  149. }
  150. void
  151. JackWinMMEInputPort::ProcessJack(JackMidiBuffer *port_buffer,
  152. jack_nframes_t frames)
  153. {
  154. write_queue->ResetMidiBuffer(port_buffer, frames);
  155. if (! jack_event) {
  156. jack_event = thread_queue->DequeueEvent();
  157. }
  158. for (; jack_event; jack_event = thread_queue->DequeueEvent()) {
  159. switch (write_queue->EnqueueEvent(jack_event, frames)) {
  160. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  161. jack_error("JackWinMMEMidiInputPort::Process - The buffer write "
  162. "queue couldn't enqueue a %d-byte event. Dropping "
  163. "event.", jack_event->size);
  164. // Fallthrough on purpose
  165. case JackMidiWriteQueue::OK:
  166. continue;
  167. }
  168. break;
  169. }
  170. }
  171. void
  172. JackWinMMEInputPort::ProcessWinMME(UINT message, DWORD param1, DWORD param2)
  173. {
  174. set_threaded_log_function();
  175. switch (message) {
  176. case MIM_CLOSE:
  177. jack_info("JackWinMMEInputPort::ProcessWinMME - MIDI device closed.");
  178. break;
  179. case MIM_MOREDATA:
  180. jack_info("JackWinMMEInputPort::ProcessWinMME - The MIDI input device "
  181. "driver thinks that JACK is not processing messages fast "
  182. "enough.");
  183. // Fallthrough on purpose.
  184. case MIM_DATA: {
  185. jack_midi_data_t message_buffer[3];
  186. jack_midi_data_t status = param1 & 0xff;
  187. int length = GetMessageLength(status);
  188. switch (length) {
  189. case 3:
  190. message_buffer[2] = (param1 >> 16) & 0xff;
  191. // Fallthrough on purpose.
  192. case 2:
  193. message_buffer[1] = (param1 >> 8) & 0xff;
  194. // Fallthrough on purpose.
  195. case 1:
  196. message_buffer[0] = status;
  197. break;
  198. case 0:
  199. jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
  200. "input driver sent an MIM_DATA message with a sysex "
  201. "status byte.");
  202. return;
  203. case -1:
  204. jack_error("JackWinMMEInputPort::ProcessWinMME - **BUG** MIDI "
  205. "input driver sent an MIM_DATA message with an invalid "
  206. "status byte.");
  207. return;
  208. }
  209. EnqueueMessage(param2, (size_t) length, message_buffer);
  210. break;
  211. }
  212. case MIM_LONGDATA: {
  213. LPMIDIHDR header = (LPMIDIHDR) param1;
  214. size_t byte_count = header->dwBytesRecorded;
  215. if (! byte_count) {
  216. jack_info("JackWinMMEInputPort::ProcessWinMME - WinMME driver has "
  217. "returned sysex header to us with no bytes. The JACK "
  218. "driver is probably being stopped.");
  219. break;
  220. }
  221. jack_midi_data_t *data = (jack_midi_data_t *) header->lpData;
  222. if ((data[0] != 0xf0) || (data[byte_count - 1] != 0xf7)) {
  223. jack_error("JackWinMMEInputPort::ProcessWinMME - Discarding "
  224. "%d-byte sysex chunk.", byte_count);
  225. } else {
  226. EnqueueMessage(param2, byte_count, data);
  227. }
  228. // Is this realtime-safe? This function isn't run in the JACK thread,
  229. // but we still want it to perform as quickly as possible. Even if
  230. // this isn't realtime safe, it may not be avoidable.
  231. MMRESULT result = midiInAddBuffer(handle, &sysex_header,
  232. sizeof(MIDIHDR));
  233. if (result != MMSYSERR_NOERROR) {
  234. WriteInError("JackWinMMEInputPort::ProcessWinMME",
  235. "midiInAddBuffer", result);
  236. }
  237. break;
  238. }
  239. case MIM_LONGERROR:
  240. jack_error("JackWinMMEInputPort::ProcessWinMME - Invalid or "
  241. "incomplete sysex message received.");
  242. break;
  243. case MIM_OPEN:
  244. jack_info("JackWinMMEInputPort::ProcessWinMME - MIDI device opened.");
  245. }
  246. }
  247. bool
  248. JackWinMMEInputPort::Start()
  249. {
  250. if (! started) {
  251. start_time = GetMicroSeconds();
  252. MMRESULT result = midiInStart(handle);
  253. started = result == MMSYSERR_NOERROR;
  254. if (! started) {
  255. WriteInError("JackWinMMEInputPort::Start", "midiInStart", result);
  256. }
  257. }
  258. return started;
  259. }
  260. bool
  261. JackWinMMEInputPort::Stop()
  262. {
  263. if (started) {
  264. MMRESULT result = midiInStop(handle);
  265. started = result != MMSYSERR_NOERROR;
  266. if (started) {
  267. WriteInError("JackWinMMEInputPort::Stop", "midiInStop", result);
  268. }
  269. }
  270. return ! started;
  271. }
  272. void
  273. JackWinMMEInputPort::WriteInError(const char *jack_func, const char *mm_func,
  274. MMRESULT result)
  275. {
  276. char error_message[MAXERRORLENGTH];
  277. GetInErrorString(result, error_message);
  278. jack_error("%s - %s: %s", jack_func, mm_func, error_message);
  279. }