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.

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