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.

398 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, (DWORD_PTR)HandleMessageEvent,
  48. (DWORD_PTR)this, CALLBACK_FUNCTION);
  49. if (result != MMSYSERR_NOERROR) {
  50. GetOutErrorString(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. MIDIOUTCAPS capabilities;
  64. char *name_tmp;
  65. result = midiOutGetDevCaps(index, &capabilities, sizeof(capabilities));
  66. if (result != MMSYSERR_NOERROR) {
  67. WriteOutError("JackWinMMEOutputPort [constructor]", "midiOutGetDevCaps",
  68. result);
  69. name_tmp = (char*)driver_name;
  70. } else {
  71. name_tmp = capabilities.szPname;
  72. }
  73. snprintf(alias, sizeof(alias) - 1, "%s:%s:out%d", alias_name, name_tmp,
  74. index + 1);
  75. snprintf(name, sizeof(name) - 1, "%s:playback_%d", client_name, index + 1);
  76. read_queue_ptr.release();
  77. thread_queue_ptr.release();
  78. thread_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. WriteOutError("JackWinMMEOutputPort [constructor]", "midiOutClose",
  88. result);
  89. }
  90. raise_exception:
  91. throw std::runtime_error(error_message);
  92. }
  93. JackWinMMEOutputPort::~JackWinMMEOutputPort()
  94. {
  95. MMRESULT result = midiOutReset(handle);
  96. if (result != MMSYSERR_NOERROR) {
  97. WriteOutError("JackWinMMEOutputPort [destructor]", "midiOutReset",
  98. result);
  99. }
  100. result = midiOutClose(handle);
  101. if (result != MMSYSERR_NOERROR) {
  102. WriteOutError("JackWinMMEOutputPort [destructor]", "midiOutClose",
  103. result);
  104. }
  105. if (! CloseHandle(sysex_semaphore)) {
  106. WriteOSError("JackWinMMEOutputPort [destructor]", "CloseHandle");
  107. }
  108. if (! CloseHandle(thread_queue_semaphore)) {
  109. WriteOSError("JackWinMMEOutputPort [destructor]", "CloseHandle");
  110. }
  111. delete read_queue;
  112. delete thread_queue;
  113. delete thread;
  114. }
  115. bool
  116. JackWinMMEOutputPort::Execute()
  117. {
  118. for (;;) {
  119. if (! Wait(thread_queue_semaphore)) {
  120. jack_log("JackWinMMEOutputPort::Execute BREAK");
  121. break;
  122. }
  123. jack_midi_event_t *event = thread_queue->DequeueEvent();
  124. if (! event) {
  125. break;
  126. }
  127. jack_time_t frame_time = GetTimeFromFrames(event->time);
  128. jack_time_t current_time = GetMicroSeconds();
  129. if (frame_time > current_time) {
  130. LARGE_INTEGER due_time;
  131. // 100 ns resolution
  132. due_time.QuadPart =
  133. -((LONGLONG) ((frame_time - current_time) * 10));
  134. if (! SetWaitableTimer(timer, &due_time, 0, NULL, NULL, 0)) {
  135. WriteOSError("JackWinMMEOutputPort::Execute",
  136. "SetWaitableTimer");
  137. break;
  138. }
  139. // Debugging code
  140. jack_log("JackWinMMEOutputPort::Execute - waiting at %f for %f "
  141. "milliseconds before sending message (current frame: %d, "
  142. "send frame: %d)",
  143. ((double) current_time) / 1000.0,
  144. ((double) (frame_time - current_time)) / 1000.0,
  145. GetFramesFromTime(current_time), event->time);
  146. // End debugging code
  147. if (! Wait(timer)) {
  148. break;
  149. }
  150. // Debugging code
  151. jack_time_t wakeup_time = GetMicroSeconds();
  152. jack_log("JackWinMMEOutputPort::Execute - woke up at %f.",
  153. ((double) wakeup_time) / 1000.0);
  154. if (wakeup_time > frame_time) {
  155. jack_log("JackWinMMEOutputPort::Execute - overslept by %f "
  156. "milliseconds.",
  157. ((double) (wakeup_time - frame_time)) / 1000.0);
  158. } else if (wakeup_time < frame_time) {
  159. jack_log("JackWinMMEOutputPort::Execute - woke up %f "
  160. "milliseconds too early.",
  161. ((double) (frame_time - wakeup_time)) / 1000.0);
  162. }
  163. // End debugging code
  164. }
  165. jack_midi_data_t *data = event->buffer;
  166. DWORD message = 0;
  167. MMRESULT result;
  168. size_t size = event->size;
  169. switch (size) {
  170. case 3:
  171. message |= (((DWORD) data[2]) << 16);
  172. // Fallthrough on purpose.
  173. case 2:
  174. message |= (((DWORD) data[1]) << 8);
  175. // Fallthrough on purpose.
  176. case 1:
  177. message |= (DWORD) data[0];
  178. result = midiOutShortMsg(handle, message);
  179. if (result != MMSYSERR_NOERROR) {
  180. WriteOutError("JackWinMMEOutputPort::Execute",
  181. "midiOutShortMsg", result);
  182. }
  183. continue;
  184. }
  185. MIDIHDR header;
  186. header.dwBufferLength = size;
  187. header.dwFlags = 0;
  188. header.lpData = (LPSTR) data;
  189. result = midiOutPrepareHeader(handle, &header, sizeof(MIDIHDR));
  190. if (result != MMSYSERR_NOERROR) {
  191. WriteOutError("JackWinMMEOutputPort::Execute",
  192. "midiOutPrepareHeader", result);
  193. continue;
  194. }
  195. result = midiOutLongMsg(handle, &header, sizeof(MIDIHDR));
  196. if (result != MMSYSERR_NOERROR) {
  197. WriteOutError("JackWinMMEOutputPort::Execute", "midiOutLongMsg",
  198. result);
  199. continue;
  200. }
  201. // System exclusive messages may be sent synchronously or
  202. // asynchronously. The choice is up to the WinMME driver. So, we wait
  203. // until the message is sent, regardless of the driver's choice.
  204. if (! Wait(sysex_semaphore)) {
  205. break;
  206. }
  207. result = midiOutUnprepareHeader(handle, &header, sizeof(MIDIHDR));
  208. if (result != MMSYSERR_NOERROR) {
  209. WriteOutError("JackWinMMEOutputPort::Execute",
  210. "midiOutUnprepareHeader", result);
  211. break;
  212. }
  213. }
  214. return false;
  215. }
  216. void
  217. JackWinMMEOutputPort::GetOutErrorString(MMRESULT error, LPTSTR text)
  218. {
  219. MMRESULT result = midiOutGetErrorText(error, text, MAXERRORLENGTH);
  220. if (result != MMSYSERR_NOERROR) {
  221. snprintf(text, MAXERRORLENGTH, "Unknown MM error code '%d'", error);
  222. }
  223. }
  224. void
  225. JackWinMMEOutputPort::HandleMessage(UINT message, DWORD_PTR param1,
  226. DWORD_PTR param2)
  227. {
  228. set_threaded_log_function();
  229. switch (message) {
  230. case MOM_CLOSE:
  231. jack_info("JackWinMMEOutputPort::HandleMessage - MIDI device closed.");
  232. break;
  233. case MOM_DONE:
  234. Signal(sysex_semaphore);
  235. break;
  236. case MOM_OPEN:
  237. jack_info("JackWinMMEOutputPort::HandleMessage - MIDI device opened.");
  238. break;
  239. case MOM_POSITIONCB:
  240. LPMIDIHDR header = (LPMIDIHDR) param1;
  241. jack_info("JackWinMMEOutputPort::HandleMessage - %d bytes out of %d "
  242. "bytes of the current sysex message have been sent.",
  243. header->dwOffset, header->dwBytesRecorded);
  244. }
  245. }
  246. bool
  247. JackWinMMEOutputPort::Init()
  248. {
  249. set_threaded_log_function();
  250. // XX: Can more be done? Ideally, this thread should have the JACK server
  251. // thread priority + 1.
  252. if (thread->AcquireSelfRealTime()) {
  253. jack_error("JackWinMMEOutputPort::Init - could not acquire realtime "
  254. "scheduling. Continuing anyway.");
  255. }
  256. return true;
  257. }
  258. void
  259. JackWinMMEOutputPort::ProcessJack(JackMidiBuffer *port_buffer,
  260. jack_nframes_t frames)
  261. {
  262. read_queue->ResetMidiBuffer(port_buffer);
  263. for (jack_midi_event_t *event = read_queue->DequeueEvent(); event;
  264. event = read_queue->DequeueEvent()) {
  265. switch (thread_queue->EnqueueEvent(event, frames)) {
  266. case JackMidiWriteQueue::BUFFER_FULL:
  267. jack_error("JackWinMMEOutputPort::ProcessJack - The thread queue "
  268. "buffer is full. Dropping event.");
  269. break;
  270. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  271. jack_error("JackWinMMEOutputPort::ProcessJack - The thread queue "
  272. "couldn't enqueue a %d-byte event. Dropping event.",
  273. event->size);
  274. break;
  275. default:
  276. Signal(thread_queue_semaphore);
  277. }
  278. }
  279. }
  280. bool
  281. JackWinMMEOutputPort::Signal(HANDLE semaphore)
  282. {
  283. bool result = (bool) ReleaseSemaphore(semaphore, 1, NULL);
  284. if (! result) {
  285. WriteOSError("JackWinMMEOutputPort::Signal", "ReleaseSemaphore");
  286. }
  287. return result;
  288. }
  289. bool
  290. JackWinMMEOutputPort::Start()
  291. {
  292. if (thread->GetStatus() != JackThread::kIdle) {
  293. return true;
  294. }
  295. timer = CreateWaitableTimer(NULL, FALSE, NULL);
  296. if (! timer) {
  297. WriteOSError("JackWinMMEOutputPort::Start", "CreateWaitableTimer");
  298. return false;
  299. }
  300. if (! thread->StartSync()) {
  301. return true;
  302. }
  303. jack_error("JackWinMMEOutputPort::Start - failed to start MIDI processing "
  304. "thread.");
  305. if (! CloseHandle(timer)) {
  306. WriteOSError("JackWinMMEOutputPort::Start", "CloseHandle");
  307. }
  308. return false;
  309. }
  310. bool
  311. JackWinMMEOutputPort::Stop()
  312. {
  313. jack_info("JackWinMMEOutputPort::Stop - stopping MIDI output port "
  314. "processing thread.");
  315. int result;
  316. const char *verb;
  317. switch (thread->GetStatus()) {
  318. case JackThread::kIniting:
  319. case JackThread::kStarting:
  320. result = thread->Kill();
  321. verb = "kill";
  322. break;
  323. case JackThread::kRunning:
  324. Signal(thread_queue_semaphore);
  325. result = thread->Stop();
  326. verb = "stop";
  327. break;
  328. default:
  329. return true;
  330. }
  331. if (result) {
  332. jack_error("JackWinMMEOutputPort::Stop - could not %s MIDI processing "
  333. "thread.", verb);
  334. }
  335. if (! CloseHandle(timer)) {
  336. WriteOSError("JackWinMMEOutputPort::Stop", "CloseHandle");
  337. result = -1;
  338. }
  339. return ! result;
  340. }
  341. bool
  342. JackWinMMEOutputPort::Wait(HANDLE semaphore)
  343. {
  344. DWORD result = WaitForSingleObject(semaphore, INFINITE);
  345. switch (result) {
  346. case WAIT_FAILED:
  347. WriteOSError("JackWinMMEOutputPort::Wait", "WaitForSingleObject");
  348. break;
  349. case WAIT_OBJECT_0:
  350. return true;
  351. default:
  352. jack_error("JackWinMMEOutputPort::Wait - unexpected result from "
  353. "'WaitForSingleObject'.");
  354. }
  355. return false;
  356. }
  357. void
  358. JackWinMMEOutputPort::WriteOutError(const char *jack_func, const char *mm_func,
  359. MMRESULT result)
  360. {
  361. char error_message[MAXERRORLENGTH];
  362. GetOutErrorString(result, error_message);
  363. jack_error("%s - %s: %s", jack_func, mm_func, error_message);
  364. }