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.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. using Jack::JackCoreMidiOutputPort;
  23. JackCoreMidiOutputPort::JackCoreMidiOutputPort(double time_ratio,
  24. size_t max_bytes,
  25. size_t max_messages):
  26. JackCoreMidiPort(time_ratio)
  27. {
  28. read_queue = new JackMidiBufferReadQueue();
  29. std::auto_ptr<JackMidiBufferReadQueue> read_queue_ptr(read_queue);
  30. thread_queue = new JackMidiAsyncQueue(max_bytes, max_messages);
  31. std::auto_ptr<JackMidiAsyncQueue> thread_queue_ptr(thread_queue);
  32. thread = new JackThread(this);
  33. std::auto_ptr<JackThread> thread_ptr(thread);
  34. sprintf(semaphore_name, "coremidi_%p", this);
  35. thread_queue_semaphore = sem_open(semaphore_name, O_CREAT, 0777, 0);
  36. if (thread_queue_semaphore == (sem_t *) SEM_FAILED) {
  37. throw std::runtime_error(strerror(errno));
  38. }
  39. thread_ptr.release();
  40. thread_queue_ptr.release();
  41. read_queue_ptr.release();
  42. }
  43. JackCoreMidiOutputPort::~JackCoreMidiOutputPort()
  44. {
  45. Stop();
  46. delete thread;
  47. sem_destroy(thread_queue_semaphore);
  48. sem_unlink(semaphore_name);
  49. delete read_queue;
  50. delete thread_queue;
  51. }
  52. bool
  53. JackCoreMidiOutputPort::Execute()
  54. {
  55. jack_midi_event_t *event = 0;
  56. MIDIPacketList *packet_list = (MIDIPacketList *) packet_buffer;
  57. for (;;) {
  58. MIDIPacket *packet = MIDIPacketListInit(packet_list);
  59. assert(packet);
  60. assert(thread_queue);
  61. if (! event) {
  62. event = GetCoreMidiEvent(true);
  63. }
  64. jack_midi_data_t *data = event->buffer;
  65. // This is the latest time that the packet list can be sent out. We
  66. // may want to consider subtracting some frames to leave room for the
  67. // CoreMIDI driver/client to handle all of the events. There's a
  68. // property called 'kMIDIPropertyAdvanceScheduleTimeMuSec' that might
  69. // be useful in this case.
  70. jack_nframes_t send_time = event->time;
  71. size_t size = event->size;
  72. MIDITimeStamp timestamp = GetTimeStampFromFrames(send_time);
  73. packet = MIDIPacketListAdd(packet_list, PACKET_BUFFER_SIZE, packet,
  74. timestamp, size, data);
  75. if (packet) {
  76. while (GetCurrentFrame() < send_time) {
  77. event = GetCoreMidiEvent(false);
  78. if (! event) {
  79. break;
  80. }
  81. packet = MIDIPacketListAdd(packet_list, sizeof(packet_buffer),
  82. packet,
  83. GetTimeStampFromFrames(event->time),
  84. event->size, event->buffer);
  85. if (! packet) {
  86. break;
  87. }
  88. }
  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 != ETIMEDOUT) {
  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...
  158. UInt64 period = 0;
  159. UInt64 computation = 500 * 1000;
  160. UInt64 constraint = 500 * 1000;
  161. thread->SetParams(period, computation, constraint);
  162. // Use the server priority : y
  163. if (thread->AcquireSelfRealTime()) {
  164. jack_error("JackCoreMidiOutputPort::Init - could not acquire realtime "
  165. "scheduling. Continuing anyway.");
  166. }
  167. return true;
  168. }
  169. void
  170. JackCoreMidiOutputPort::Initialize(const char *alias_name,
  171. const char *client_name,
  172. const char *driver_name, int index,
  173. MIDIEndpointRef endpoint)
  174. {
  175. JackCoreMidiPort::Initialize(alias_name, client_name, driver_name, index,
  176. endpoint, true);
  177. }
  178. void
  179. JackCoreMidiOutputPort::ProcessJack(JackMidiBuffer *port_buffer,
  180. jack_nframes_t frames)
  181. {
  182. read_queue->ResetMidiBuffer(port_buffer);
  183. for (jack_midi_event_t *event = read_queue->DequeueEvent(); event;
  184. event = read_queue->DequeueEvent()) {
  185. switch (thread_queue->EnqueueEvent(event, frames)) {
  186. case JackMidiWriteQueue::BUFFER_FULL:
  187. jack_error("JackCoreMidiOutputPort::ProcessJack - The thread "
  188. "queue buffer is full. Dropping event.");
  189. break;
  190. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  191. jack_error("JackCoreMidiOutputPort::ProcessJack - The thread "
  192. "queue couldn't enqueue a %d-byte event. Dropping "
  193. "event.", event->size);
  194. break;
  195. default:
  196. if (sem_post(thread_queue_semaphore)) {
  197. jack_error("JackCoreMidiOutputPort::ProcessJack - unexpected "
  198. "error while posting to thread queue semaphore: %s",
  199. strerror(errno));
  200. }
  201. }
  202. }
  203. }
  204. bool
  205. JackCoreMidiOutputPort::Start()
  206. {
  207. bool result = thread->GetStatus() != JackThread::kIdle;
  208. if (! result) {
  209. result = ! thread->StartSync();
  210. if (! result) {
  211. jack_error("JackCoreMidiOutputPort::Start - failed to start MIDI "
  212. "processing thread.");
  213. }
  214. }
  215. return result;
  216. }
  217. bool
  218. JackCoreMidiOutputPort::Stop()
  219. {
  220. bool result = thread->GetStatus() == JackThread::kIdle;
  221. if (! result) {
  222. result = ! thread->Kill();
  223. if (! result) {
  224. jack_error("JackCoreMidiOutputPort::Stop - failed to stop MIDI "
  225. "processing thread.");
  226. }
  227. }
  228. return result;
  229. }