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.

252 lines
8.5KB

  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 <cassert>
  16. #include <cerrno>
  17. #include <cstring>
  18. #include <new>
  19. #include <stdexcept>
  20. #include "JackCoreMidiOutputPort.h"
  21. #include "JackMidiUtil.h"
  22. #include "JackTime.h"
  23. #include "JackError.h"
  24. using Jack::JackCoreMidiOutputPort;
  25. JackCoreMidiOutputPort::JackCoreMidiOutputPort(double time_ratio,
  26. size_t max_bytes,
  27. size_t max_messages):
  28. JackCoreMidiPort(time_ratio)
  29. {
  30. read_queue = new JackMidiBufferReadQueue();
  31. std::unique_ptr<JackMidiBufferReadQueue> read_queue_ptr(read_queue);
  32. thread_queue = new JackMidiAsyncQueue(max_bytes, max_messages);
  33. std::unique_ptr<JackMidiAsyncQueue> thread_queue_ptr(thread_queue);
  34. thread = new JackThread(this);
  35. std::unique_ptr<JackThread> thread_ptr(thread);
  36. snprintf(semaphore_name, sizeof(semaphore_name), "coremidi_%p", this);
  37. thread_queue_semaphore = sem_open(semaphore_name, O_CREAT, 0777, 0);
  38. if (thread_queue_semaphore == (sem_t *) SEM_FAILED) {
  39. throw std::runtime_error(strerror(errno));
  40. }
  41. advance_schedule_time = 0;
  42. thread_ptr.release();
  43. thread_queue_ptr.release();
  44. read_queue_ptr.release();
  45. }
  46. JackCoreMidiOutputPort::~JackCoreMidiOutputPort()
  47. {
  48. delete thread;
  49. sem_close(thread_queue_semaphore);
  50. sem_unlink(semaphore_name);
  51. delete read_queue;
  52. delete thread_queue;
  53. }
  54. bool
  55. JackCoreMidiOutputPort::Execute()
  56. {
  57. jack_midi_event_t *event = 0;
  58. MIDIPacketList *packet_list = (MIDIPacketList *) packet_buffer;
  59. for (;;) {
  60. MIDIPacket *packet = MIDIPacketListInit(packet_list);
  61. assert(packet);
  62. if (! event) {
  63. event = GetCoreMidiEvent(true);
  64. }
  65. jack_midi_data_t *data = event->buffer;
  66. jack_nframes_t send_frame = event->time;
  67. jack_time_t send_time =
  68. GetTimeFromFrames(send_frame) - advance_schedule_time;
  69. size_t size = event->size;
  70. MIDITimeStamp timestamp = GetTimeStampFromFrames(send_frame);
  71. packet = MIDIPacketListAdd(packet_list, PACKET_BUFFER_SIZE, packet,
  72. timestamp, size, data);
  73. if (packet) {
  74. do {
  75. if (GetMicroSeconds() >= send_time) {
  76. event = 0;
  77. break;
  78. }
  79. event = GetCoreMidiEvent(false);
  80. if (! event) {
  81. break;
  82. }
  83. packet = MIDIPacketListAdd(packet_list, sizeof(packet_buffer),
  84. packet,
  85. GetTimeStampFromFrames(event->time),
  86. event->size, event->buffer);
  87. } while (packet);
  88. SendPacketList(packet_list);
  89. } else {
  90. // We have a large system exclusive event. We'll have to send it
  91. // out in multiple packets.
  92. size_t bytes_sent = 0;
  93. do {
  94. packet = MIDIPacketListInit(packet_list);
  95. assert(packet);
  96. size_t num_bytes = 0;
  97. for (; bytes_sent < size; bytes_sent += num_bytes) {
  98. size_t num_bytes = size - bytes_sent;
  99. // We use 256 because the MIDIPacket struct defines the
  100. // size of the 'data' member to be 256 bytes. I believe
  101. // this prevents packets from being dynamically allocated
  102. // by 'MIDIPacketListAdd', but I might be wrong.
  103. if (num_bytes > 256) {
  104. num_bytes = 256;
  105. }
  106. packet = MIDIPacketListAdd(packet_list,
  107. sizeof(packet_buffer), packet,
  108. timestamp, num_bytes,
  109. data + bytes_sent);
  110. if (! packet) {
  111. break;
  112. }
  113. }
  114. if (! SendPacketList(packet_list)) {
  115. // An error occurred. The error message has already been
  116. // output. We lick our wounds and move along.
  117. break;
  118. }
  119. } while (bytes_sent < size);
  120. event = 0;
  121. }
  122. }
  123. return false;
  124. }
  125. jack_midi_event_t *
  126. JackCoreMidiOutputPort::GetCoreMidiEvent(bool block)
  127. {
  128. if (! block) {
  129. if (sem_trywait(thread_queue_semaphore)) {
  130. if (errno != EAGAIN) {
  131. jack_error("JackCoreMidiOutputPort::Execute - sem_trywait: %s",
  132. strerror(errno));
  133. }
  134. return 0;
  135. }
  136. } else {
  137. while (sem_wait(thread_queue_semaphore)) {
  138. if (errno != EINTR) {
  139. jack_error("JackCoreMidiOutputPort::Execute - sem_wait: %s",
  140. strerror(errno));
  141. return 0;
  142. }
  143. }
  144. }
  145. return thread_queue->DequeueEvent();
  146. }
  147. MIDITimeStamp
  148. JackCoreMidiOutputPort::GetTimeStampFromFrames(jack_nframes_t frames)
  149. {
  150. return GetTimeFromFrames(frames) / time_ratio;
  151. }
  152. bool
  153. JackCoreMidiOutputPort::Init()
  154. {
  155. set_threaded_log_function();
  156. // OSX only, values read in RT CoreMIDI thread
  157. UInt64 period = 0;
  158. UInt64 computation = 250 * 1000;
  159. UInt64 constraint = 500 * 1000;
  160. thread->SetParams(period, computation, constraint);
  161. if (thread->AcquireSelfRealTime()) {
  162. jack_error("JackCoreMidiOutputPort::Init - could not acquire realtime "
  163. "scheduling. Continuing anyway.");
  164. }
  165. return true;
  166. }
  167. void
  168. JackCoreMidiOutputPort::Initialize(const char *alias_name,
  169. const char *client_name,
  170. const char *driver_name, int index,
  171. MIDIEndpointRef endpoint,
  172. SInt32 advance_schedule_time)
  173. {
  174. JackCoreMidiPort::Initialize(alias_name, client_name, driver_name, index,
  175. endpoint, true);
  176. assert(advance_schedule_time >= 0);
  177. this->advance_schedule_time = advance_schedule_time;
  178. }
  179. void
  180. JackCoreMidiOutputPort::ProcessJack(JackMidiBuffer *port_buffer,
  181. jack_nframes_t frames)
  182. {
  183. read_queue->ResetMidiBuffer(port_buffer);
  184. for (jack_midi_event_t *event = read_queue->DequeueEvent(); event;
  185. event = read_queue->DequeueEvent()) {
  186. switch (thread_queue->EnqueueEvent(event, frames)) {
  187. case JackMidiWriteQueue::BUFFER_FULL:
  188. jack_error("JackCoreMidiOutputPort::ProcessJack - The thread "
  189. "queue buffer is full. Dropping event.");
  190. break;
  191. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  192. jack_error("JackCoreMidiOutputPort::ProcessJack - The thread "
  193. "queue couldn't enqueue a %d-byte event. Dropping "
  194. "event.", event->size);
  195. break;
  196. default:
  197. if (sem_post(thread_queue_semaphore)) {
  198. jack_error("JackCoreMidiOutputPort::ProcessJack - unexpected "
  199. "error while posting to thread queue semaphore: %s",
  200. strerror(errno));
  201. }
  202. }
  203. }
  204. }
  205. bool
  206. JackCoreMidiOutputPort::Start()
  207. {
  208. bool result = thread->GetStatus() != JackThread::kIdle;
  209. if (! result) {
  210. result = ! thread->StartSync();
  211. if (! result) {
  212. jack_error("JackCoreMidiOutputPort::Start - failed to start MIDI "
  213. "processing thread.");
  214. }
  215. }
  216. return result;
  217. }
  218. bool
  219. JackCoreMidiOutputPort::Stop()
  220. {
  221. bool result = thread->GetStatus() == JackThread::kIdle;
  222. if (! result) {
  223. result = ! thread->Kill();
  224. if (! result) {
  225. jack_error("JackCoreMidiOutputPort::Stop - failed to stop MIDI "
  226. "processing thread.");
  227. }
  228. }
  229. return result;
  230. }