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.

386 lines
13KB

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