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.

373 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. 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. "ChangeTimerQueueTimer");
  137. break;
  138. }
  139. if (! Wait(timer)) {
  140. break;
  141. }
  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. WriteOutError("JackWinMMEOutputPort::Execute",
  159. "midiOutShortMsg", result);
  160. }
  161. continue;
  162. }
  163. MIDIHDR header;
  164. header.dwBufferLength = size;
  165. header.dwFlags = 0;
  166. header.lpData = (LPSTR) data;
  167. result = midiOutPrepareHeader(handle, &header, sizeof(MIDIHDR));
  168. if (result != MMSYSERR_NOERROR) {
  169. WriteOutError("JackWinMMEOutputPort::Execute",
  170. "midiOutPrepareHeader", result);
  171. continue;
  172. }
  173. result = midiOutLongMsg(handle, &header, sizeof(MIDIHDR));
  174. if (result != MMSYSERR_NOERROR) {
  175. WriteOutError("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. WriteOutError("JackWinMMEOutputPort::Execute",
  188. "midiOutUnprepareHeader", result);
  189. break;
  190. }
  191. }
  192. return false;
  193. }
  194. void
  195. JackWinMMEOutputPort::GetOutErrorString(MMRESULT error, LPTSTR text)
  196. {
  197. MMRESULT result = midiOutGetErrorText(error, text, MAXERRORLENGTH);
  198. if (result != MMSYSERR_NOERROR) {
  199. snprintf(text, MAXERRORLENGTH, "Unknown MM error code '%d'", error);
  200. }
  201. }
  202. void
  203. JackWinMMEOutputPort::HandleMessage(UINT message, DWORD_PTR param1,
  204. DWORD_PTR param2)
  205. {
  206. set_threaded_log_function();
  207. switch (message) {
  208. case MOM_CLOSE:
  209. jack_info("JackWinMMEOutputPort::HandleMessage - MIDI device closed.");
  210. break;
  211. case MOM_DONE:
  212. Signal(sysex_semaphore);
  213. break;
  214. case MOM_OPEN:
  215. jack_info("JackWinMMEOutputPort::HandleMessage - MIDI device opened.");
  216. break;
  217. case MOM_POSITIONCB:
  218. LPMIDIHDR header = (LPMIDIHDR) param1;
  219. jack_info("JackWinMMEOutputPort::HandleMessage - %d bytes out of %d "
  220. "bytes of the current sysex message have been sent.",
  221. header->dwOffset, header->dwBytesRecorded);
  222. }
  223. }
  224. bool
  225. JackWinMMEOutputPort::Init()
  226. {
  227. set_threaded_log_function();
  228. // XX: Can more be done? Ideally, this thread should have the JACK server
  229. // thread priority + 1.
  230. if (thread->AcquireSelfRealTime()) {
  231. jack_error("JackWinMMEOutputPort::Init - could not acquire realtime "
  232. "scheduling. Continuing anyway.");
  233. }
  234. return true;
  235. }
  236. void
  237. JackWinMMEOutputPort::ProcessJack(JackMidiBuffer *port_buffer,
  238. jack_nframes_t frames)
  239. {
  240. read_queue->ResetMidiBuffer(port_buffer);
  241. for (jack_midi_event_t *event = read_queue->DequeueEvent(); event;
  242. event = read_queue->DequeueEvent()) {
  243. switch (thread_queue->EnqueueEvent(event, frames)) {
  244. case JackMidiWriteQueue::BUFFER_FULL:
  245. jack_error("JackWinMMEOutputPort::ProcessJack - The thread queue "
  246. "buffer is full. Dropping event.");
  247. break;
  248. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  249. jack_error("JackWinMMEOutputPort::ProcessJack - The thread queue "
  250. "couldn't enqueue a %d-byte event. Dropping event.",
  251. event->size);
  252. break;
  253. default:
  254. Signal(thread_queue_semaphore);
  255. }
  256. }
  257. }
  258. bool
  259. JackWinMMEOutputPort::Signal(HANDLE semaphore)
  260. {
  261. bool result = (bool) ReleaseSemaphore(semaphore, 1, NULL);
  262. if (! result) {
  263. WriteOSError("JackWinMMEOutputPort::Signal", "ReleaseSemaphore");
  264. }
  265. return result;
  266. }
  267. bool
  268. JackWinMMEOutputPort::Start()
  269. {
  270. if (thread->GetStatus() != JackThread::kIdle) {
  271. return true;
  272. }
  273. timer = CreateWaitableTimer(NULL, FALSE, NULL);
  274. if (! timer) {
  275. WriteOSError("JackWinMMEOutputPort::Start", "CreateWaitableTimer");
  276. return false;
  277. }
  278. if (! thread->StartSync()) {
  279. return true;
  280. }
  281. jack_error("JackWinMMEOutputPort::Start - failed to start MIDI processing "
  282. "thread.");
  283. if (! CloseHandle(timer)) {
  284. WriteOSError("JackWinMMEOutputPort::Start", "CloseHandle");
  285. }
  286. return false;
  287. }
  288. bool
  289. JackWinMMEOutputPort::Stop()
  290. {
  291. jack_info("JackWinMMEOutputPort::Stop - stopping MIDI output port "
  292. "processing thread.");
  293. int result;
  294. const char *verb;
  295. switch (thread->GetStatus()) {
  296. case JackThread::kIniting:
  297. case JackThread::kStarting:
  298. result = thread->Kill();
  299. verb = "kill";
  300. break;
  301. case JackThread::kRunning:
  302. Signal(thread_queue_semaphore);
  303. result = thread->Stop();
  304. verb = "stop";
  305. break;
  306. default:
  307. return true;
  308. }
  309. if (result) {
  310. jack_error("JackWinMMEOutputPort::Stop - could not %s MIDI processing "
  311. "thread.", verb);
  312. }
  313. if (! CloseHandle(timer)) {
  314. WriteOSError("JackWinMMEOutputPort::Stop", "CloseHandle");
  315. result = -1;
  316. }
  317. return ! result;
  318. }
  319. bool
  320. JackWinMMEOutputPort::Wait(HANDLE semaphore)
  321. {
  322. DWORD result = WaitForSingleObject(semaphore, INFINITE);
  323. switch (result) {
  324. case WAIT_FAILED:
  325. WriteOSError("JackWinMMEOutputPort::Wait", "WaitForSingleObject");
  326. break;
  327. case WAIT_OBJECT_0:
  328. return true;
  329. default:
  330. jack_error("JackWinMMEOutputPort::Wait - unexpected result from "
  331. "'WaitForSingleObject'.");
  332. }
  333. return false;
  334. }
  335. void
  336. JackWinMMEOutputPort::WriteOutError(const char *jack_func, const char *mm_func,
  337. MMRESULT result)
  338. {
  339. char error_message[MAXERRORLENGTH];
  340. GetOutErrorString(result, error_message);
  341. jack_error("%s - %s: %s", jack_func, mm_func, error_message);
  342. }