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.

353 lines
12KB

  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 <memory>
  16. #include <stdexcept>
  17. #include "JackMidiUtil.h"
  18. #include "JackTime.h"
  19. #include "JackWinMMEOutputPort.h"
  20. using Jack::JackWinMMEOutputPort;
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // Static callbacks
  23. ///////////////////////////////////////////////////////////////////////////////
  24. void CALLBACK
  25. JackWinMMEOutputPort::HandleMessageEvent(HMIDIOUT handle, UINT message,
  26. DWORD_PTR port, DWORD_PTR param1,
  27. DWORD_PTR param2)
  28. {
  29. ((JackWinMMEOutputPort *) port)->HandleMessage(message, param1, param2);
  30. }
  31. ///////////////////////////////////////////////////////////////////////////////
  32. // Class
  33. ///////////////////////////////////////////////////////////////////////////////
  34. JackWinMMEOutputPort::JackWinMMEOutputPort(const char *alias_name,
  35. const char *client_name,
  36. const char *driver_name, UINT index,
  37. size_t max_bytes,
  38. size_t max_messages)
  39. {
  40. read_queue = new JackMidiBufferReadQueue();
  41. std::auto_ptr<JackMidiBufferReadQueue> read_queue_ptr(read_queue);
  42. thread_queue = new JackMidiAsyncQueue(max_bytes, max_messages);
  43. std::auto_ptr<JackMidiAsyncQueue> thread_queue_ptr(thread_queue);
  44. char error_message[MAXERRORLENGTH];
  45. MMRESULT result = midiOutOpen(&handle, index, HandleMessageEvent, this,
  46. CALLBACK_FUNCTION);
  47. if (result != MMSYSERR_NOERROR) {
  48. GetErrorString(result, error_message);
  49. goto raise_exception;
  50. }
  51. thread_queue_semaphore = CreateSemaphore(NULL, 0, max_messages, NULL);
  52. if (thread_queue_semaphore == NULL) {
  53. GetOSErrorString(error_message);
  54. goto close_handle;
  55. }
  56. sysex_semaphore = CreateSemaphore(NULL, 0, 1, NULL);
  57. if (sysex_semaphore == NULL) {
  58. GetOSErrorString(error_message);
  59. goto destroy_thread_queue_semaphore;
  60. }
  61. MIDIINCAPS capabilities;
  62. char *name;
  63. result = midiOutGetDevCaps(index, &capabilities, sizeof(capabilities));
  64. if (result != MMSYSERR_NOERROR) {
  65. WriteMMError("JackWinMMEOutputPort [constructor]", "midiOutGetDevCaps",
  66. result);
  67. name = driver_name;
  68. } else {
  69. name = capabilities.szPname;
  70. }
  71. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", alias_name, driver_name,
  72. index + 1);
  73. snprintf(name, sizeof(name) - 1, "%s:playback_%d", client_name, index + 1);
  74. write_queue_ptr.release();
  75. thread_queue_ptr.release();
  76. return;
  77. destroy_thread_queue_semaphore:
  78. if (! CloseHandle(thread_queue_semaphore)) {
  79. WriteOSError("JackWinMMEOutputPort [constructor]", "CloseHandle");
  80. }
  81. close_handle:
  82. result = midiOutClose(handle);
  83. if (result != MMSYSERR_NOERROR) {
  84. WriteMMError("JackWinMMEOutputPort [constructor]", "midiOutClose",
  85. result);
  86. }
  87. raise_exception:
  88. throw std::runtime_error(error_message);
  89. }
  90. JackWinMMEOutputPort::~JackWinMMEOutputPort()
  91. {
  92. Stop();
  93. MMRESULT result = midiOutReset(handle);
  94. if (result != MMSYSERR_NOERROR) {
  95. WriteMMError("JackWinMMEOutputPort [destructor]", "midiOutReset",
  96. result);
  97. }
  98. result = midiOutClose(handle);
  99. if (result != MMSYSERR_NOERROR) {
  100. WriteMMError("JackWinMMEOutputPort [destructor]", "midiOutClose",
  101. result);
  102. }
  103. if (! CloseHandle(sysex_semaphore)) {
  104. WriteOSError("JackWinMMEOutputPort [destructor]", "CloseHandle");
  105. }
  106. if (! CloseHandle(thread_queue_semaphore)) {
  107. WriteOSError("JackWinMMEOutputPort [destructor]", "CloseHandle");
  108. }
  109. delete read_queue;
  110. delete thread_queue;
  111. }
  112. bool
  113. JackWinMMEOutputPort::Execute()
  114. {
  115. for (;;) {
  116. if (! Wait(thread_queue_semaphore)) {
  117. break;
  118. }
  119. jack_midi_event_t *event = thread_queue->DequeueEvent();
  120. if (! event) {
  121. break;
  122. }
  123. jack_time_t frame_time = GetTimeFromFrames(event->time);
  124. for (jack_time_t current_time = GetMicroSeconds();
  125. frame_time > current_time; current_time = GetMicroSeconds()) {
  126. jack_time_t sleep_time = frame_time - current_time;
  127. // Windows has a millisecond sleep resolution for its Sleep calls.
  128. // This is unfortunate, as MIDI timing often requires a higher
  129. // resolution. For now, we attempt to compensate by letting an
  130. // event be sent if we're less than 500 microseconds from sending
  131. // the event. We assume that it's better to let an event go out
  132. // 499 microseconds early than let an event go out 501 microseconds
  133. // late. Of course, that's assuming optimal sleep times, which is
  134. // a whole different Windows issue ...
  135. if (sleep_time < 500) {
  136. break;
  137. }
  138. if (sleep_time < 1000) {
  139. sleep_time = 1000;
  140. }
  141. JackSleep(sleep_time);
  142. }
  143. jack_midi_data_t *data = event->buffer;
  144. DWORD message = 0;
  145. MMRESULT result;
  146. size_t size = event->size;
  147. switch (size) {
  148. case 3:
  149. message |= (((DWORD) data[2]) << 16);
  150. // Fallthrough on purpose.
  151. case 2:
  152. message |= (((DWORD) data[1]) << 8);
  153. // Fallthrough on purpose.
  154. case 1:
  155. message |= (DWORD) data[0];
  156. result = midiOutShortMsg(handle, message);
  157. if (result != MMSYSERR_NOERROR) {
  158. WriteMMError("JackWinMMEOutputPort::Execute",
  159. "midiOutShortMsg", result);
  160. }
  161. continue;
  162. }
  163. MIDIHDR header;
  164. header.dwFlags = 0;
  165. header.dwLength = size;
  166. header.lpData = data;
  167. result = midiOutPrepareHeader(handle, &header, sizeof(MIDIHDR));
  168. if (result != MMSYSERR_NOERROR) {
  169. WriteMMError("JackWinMMEOutputPort::Execute",
  170. "midiOutPrepareHeader", result);
  171. continue;
  172. }
  173. result = midiOutLongMsg(handle, &header, sizeof(MIDIHDR));
  174. if (result != MMSYSERR_NOERROR) {
  175. WriteMMError("JackWinMMEOutputPort::Execute", "midiOutLongMsg",
  176. result);
  177. continue;
  178. }
  179. // System exclusive messages may be sent synchronously or
  180. // asynchronously. The choice is up to the WinMME driver. So, we wait
  181. // until the message is sent, regardless of the driver's choice.
  182. if (! Wait(sysex_semaphore)) {
  183. break;
  184. }
  185. result = midiOutUnprepareHeader(handle, &header, sizeof(MIDIHDR));
  186. if (result != MMSYSERR_NOERROR) {
  187. WriteMMError("JackWinMMEOutputPort::Execute",
  188. "midiOutUnprepareHeader", result);
  189. break;
  190. }
  191. }
  192. stop_execution:
  193. return false;
  194. }
  195. void
  196. JackWinMMEOutputPort::GetMMErrorString(MMRESULT error, LPTSTR text)
  197. {
  198. MMRESULT result = midiOutGetErrorText(error, text, MAXERRORLENGTH);
  199. if (result != MMSYSERR_NOERROR) {
  200. snprintf(text, MAXERRORLENGTH, "Unknown MM error code '%d'", error);
  201. }
  202. }
  203. void
  204. JackWinMMEOutputPort::GetOSErrorString(LPTSTR text)
  205. {
  206. DWORD error = GetLastError();
  207. if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error,
  208. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &text,
  209. MAXERRORLENGTH, NULL)) {
  210. snprintf(text, MAXERRORLENGTH, "Unknown OS error code '%d'", error);
  211. }
  212. }
  213. void
  214. JackWinMMEOutputPort::HandleMessage(UINT message, DWORD_PTR param1,
  215. DWORD_PTR param2)
  216. {
  217. switch (message) {
  218. case MOM_CLOSE:
  219. jack_info("JackWinMMEOutputPort::HandleMessage - MIDI device closed.");
  220. break;
  221. case MOM_DONE:
  222. if (! ReleaseSemaphore(sysex_semaphore, 1, NULL)) {
  223. WriteOSError("JackWinMMEOutputPort::HandleMessage",
  224. "ReleaseSemaphore");
  225. }
  226. break;
  227. case MOM_OPEN:
  228. jack_info("JackWinMMEOutputPort::HandleMessage - MIDI device opened.");
  229. }
  230. }
  231. bool
  232. JackWinMMEOutputPort::Init()
  233. {
  234. set_threaded_log_function();
  235. // XX: Can more be done? Ideally, this thread should have the JACK server
  236. // thread priority + 1.
  237. if (thread->AcquireSelfRealTime()) {
  238. jack_error("JackWinMMEOutputPort::Init - could not acquire realtime "
  239. "scheduling. Continuing anyway.");
  240. }
  241. return true;
  242. }
  243. void
  244. JackWinMMEOutputPort::ProcessJack(JackMidiBuffer *port_buffer,
  245. jack_nframes_t frames)
  246. {
  247. read_queue->ResetMidiBuffer(port_buffer);
  248. for (jack_midi_event_t *event = read_queue->DequeueEvent(); event;
  249. event = read_queue->DequeueEvent()) {
  250. switch (thread_queue->EnqueueEvent(event, frames)) {
  251. case JackMidiWriteQueue::BUFFER_FULL:
  252. jack_error("JackWinMMEOutputPort::ProcessJack - The thread queue "
  253. "buffer is full. Dropping event.");
  254. break;
  255. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  256. jack_error("JackWinMMEOutputPort::ProcessJack - The thread queue "
  257. "couldn't enqueue a %d-byte event. Dropping event.",
  258. event->size);
  259. break;
  260. default:
  261. if (! ReleaseSemaphore(thread_queue_semaphore, 1, NULL)) {
  262. WriteOSError("JackWinMMEOutputPort::ProcessJack",
  263. "ReleaseSemaphore");
  264. }
  265. }
  266. }
  267. }
  268. bool
  269. JackWinMMEOutputPort::Start()
  270. {
  271. bool result = thread->GetStatus() != JackThread::kIdle;
  272. if (! result) {
  273. result = ! thread->StartSync();
  274. if (! result) {
  275. jack_error("JackWinMMEOutputPort::Start - failed to start MIDI "
  276. "processing thread.");
  277. }
  278. }
  279. return result;
  280. }
  281. bool
  282. JackWinMMEOutputPort::Stop()
  283. {
  284. bool result = thread->GetStatus() == JackThread::kIdle;
  285. if (! result) {
  286. result = ! thread->Kill();
  287. if (! result) {
  288. jack_error("JackWinMMEOutputPort::Stop - failed to stop MIDI "
  289. "processing thread.");
  290. }
  291. }
  292. return result;
  293. }
  294. bool
  295. JackWinMMEOutputPort::Wait(Handle semaphore)
  296. {
  297. DWORD result = WaitForSingleObject(semaphore, INFINITE);
  298. switch (result) {
  299. case WAIT_FAILED:
  300. WriteOSError("JackWinMMEOutputPort::Wait", "WaitForSingleObject");
  301. break;
  302. case WAIT_OBJECT_0:
  303. return true;
  304. default:
  305. jack_error("JackWinMMEOutputPort::Wait - unexpected result from "
  306. "'WaitForSingleObject'.");
  307. }
  308. return false;
  309. }
  310. void
  311. JackWinMMEOutputPort::WriteMMError(const char *jack_func, const char *mm_func,
  312. MMRESULT result)
  313. {
  314. const char error_message[MAXERRORLENGTH];
  315. GetMMErrorString(result, error_message);
  316. jack_error("%s - %s: %s", jack_func, mm_func, error_message);
  317. }
  318. void
  319. JackWinMMEOutputPort::WriteOSError(const char *jack_func, const char *os_func)
  320. {
  321. const char error_message[MAXERRORLENGTH];
  322. GetOSErrorString(result, error_message);
  323. jack_error("%s - %s: %s", jack_func, os_func, error_message);
  324. }