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.

213 lines
7.2KB

  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 <new>
  17. #include "JackCoreMidiOutputPort.h"
  18. #include "JackMidiUtil.h"
  19. using Jack::JackCoreMidiOutputPort;
  20. JackCoreMidiOutputPort::JackCoreMidiOutputPort(double time_ratio,
  21. size_t max_bytes,
  22. size_t max_messages):
  23. JackCoreMidiPort(time_ratio)
  24. {
  25. read_queue = new JackMidiBufferReadQueue();
  26. std::auto_ptr<JackMidiBufferReadQueue> read_ptr(read_queue);
  27. thread_queue = new JackMidiAsyncWaitQueue(max_bytes, max_messages);
  28. std::auto_ptr<JackMidiAsyncWaitQueue> thread_ptr(thread_queue);
  29. thread = new JackThread(this);
  30. thread_ptr.release();
  31. read_ptr.release();
  32. }
  33. JackCoreMidiOutputPort::~JackCoreMidiOutputPort()
  34. {
  35. Stop();
  36. delete thread;
  37. delete read_queue;
  38. delete thread_queue;
  39. }
  40. bool
  41. JackCoreMidiOutputPort::Execute()
  42. {
  43. jack_midi_event_t *event = 0;
  44. MIDIPacketList *packet_list = (MIDIPacketList *) packet_buffer;
  45. for (;;) {
  46. MIDIPacket *packet = MIDIPacketListInit(packet_list);
  47. assert(packet);
  48. assert(thread_queue);
  49. if (! event) {
  50. event = thread_queue->DequeueEvent((long) 0);
  51. }
  52. jack_midi_data_t *data = event->buffer;
  53. // This is the latest time that the packet list can be sent out. We
  54. // may want to consider subtracting some frames to leave room for the
  55. // CoreMIDI driver/client to handle all of the events. There's a
  56. // property called 'kMIDIPropertyAdvanceScheduleTimeMuSec' that might
  57. // be useful in this case.
  58. jack_nframes_t send_time = event->time;
  59. size_t size = event->size;
  60. MIDITimeStamp timestamp = GetTimeStampFromFrames(send_time);
  61. packet = MIDIPacketListAdd(packet_list, PACKET_BUFFER_SIZE, packet,
  62. timestamp, size, data);
  63. if (packet) {
  64. while (GetCurrentFrame() < send_time) {
  65. event = thread_queue->DequeueEvent();
  66. if (! event) {
  67. break;
  68. }
  69. packet = MIDIPacketListAdd(packet_list, sizeof(packet_buffer),
  70. packet,
  71. GetTimeStampFromFrames(event->time),
  72. event->size, event->buffer);
  73. if (! packet) {
  74. break;
  75. }
  76. }
  77. SendPacketList(packet_list);
  78. } else {
  79. // We have a large system exclusive event. We'll have to send it
  80. // out in multiple packets.
  81. size_t bytes_sent = 0;
  82. do {
  83. packet = MIDIPacketListInit(packet_list);
  84. assert(packet);
  85. size_t num_bytes = 0;
  86. for (; bytes_sent < size; bytes_sent += num_bytes) {
  87. size_t num_bytes = size - bytes_sent;
  88. // We use 256 because the MIDIPacket struct defines the
  89. // size of the 'data' member to be 256 bytes. I believe
  90. // this prevents packets from being dynamically allocated
  91. // by 'MIDIPacketListAdd', but I might be wrong.
  92. if (num_bytes > 256) {
  93. num_bytes = 256;
  94. }
  95. packet = MIDIPacketListAdd(packet_list,
  96. sizeof(packet_buffer), packet,
  97. timestamp, num_bytes,
  98. data + bytes_sent);
  99. if (! packet) {
  100. break;
  101. }
  102. }
  103. if (! SendPacketList(packet_list)) {
  104. // An error occurred. The error message has already been
  105. // output. We lick our wounds and move along.
  106. break;
  107. }
  108. } while (bytes_sent < size);
  109. event = 0;
  110. }
  111. }
  112. return false;
  113. }
  114. MIDITimeStamp
  115. JackCoreMidiOutputPort::GetTimeStampFromFrames(jack_nframes_t frames)
  116. {
  117. return GetTimeFromFrames(frames) / time_ratio;
  118. }
  119. bool
  120. JackCoreMidiOutputPort::Init()
  121. {
  122. set_threaded_log_function();
  123. // OSX only...
  124. UInt64 period = 0;
  125. UInt64 computation = 500 * 1000;
  126. UInt64 constraint = 500 * 1000;
  127. thread->SetParams(period, computation, constraint);
  128. // Use the server priority : y
  129. if (thread->AcquireSelfRealTime()) {
  130. jack_error("JackCoreMidiOutputPort::Init - could not acquire realtime "
  131. "scheduling. Continuing anyway.");
  132. }
  133. return true;
  134. }
  135. void
  136. JackCoreMidiOutputPort::Initialize(const char *alias_name,
  137. const char *client_name,
  138. const char *driver_name, int index,
  139. MIDIEndpointRef endpoint)
  140. {
  141. JackCoreMidiPort::Initialize(alias_name, client_name, driver_name, index, endpoint, true);
  142. }
  143. void
  144. JackCoreMidiOutputPort::ProcessJack(JackMidiBuffer *port_buffer,
  145. jack_nframes_t frames)
  146. {
  147. read_queue->ResetMidiBuffer(port_buffer);
  148. for (jack_midi_event_t *event = read_queue->DequeueEvent(); event;
  149. event = read_queue->DequeueEvent()) {
  150. switch (thread_queue->EnqueueEvent(event, frames)) {
  151. case JackMidiWriteQueue::BUFFER_FULL:
  152. jack_error("JackCoreMidiOutputPort::ProcessJack - The thread "
  153. "queue buffer is full. Dropping event.");
  154. continue;
  155. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  156. jack_error("JackCoreMidiOutputPort::ProcessJack - The thread "
  157. "queue couldn't enqueue a %d-byte event. Dropping "
  158. "event.", event->size);
  159. // Fallthrough on purpose
  160. default:
  161. ;
  162. }
  163. }
  164. }
  165. bool
  166. JackCoreMidiOutputPort::Start()
  167. {
  168. bool result = thread->GetStatus() != JackThread::kIdle;
  169. if (! result) {
  170. result = ! thread->StartSync();
  171. if (! result) {
  172. jack_error("JackCoreMidiOutputPort::Start - failed to start MIDI "
  173. "processing thread.");
  174. }
  175. }
  176. return result;
  177. }
  178. bool
  179. JackCoreMidiOutputPort::Stop()
  180. {
  181. bool result = thread->GetStatus() == JackThread::kIdle;
  182. if (! result) {
  183. result = ! thread->Kill();
  184. if (! result) {
  185. jack_error("JackCoreMidiOutputPort::Stop - failed to stop MIDI "
  186. "processing thread.");
  187. }
  188. }
  189. return result;
  190. }