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.

253 lines
8.6KB

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