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.

229 lines
7.1KB

  1. /*
  2. Copyright (C) 2010 Devin Anderson
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <memory>
  16. #include <new>
  17. #include "JackError.h"
  18. #include "JackMidiRawOutputWriteQueue.h"
  19. #include "JackMidiUtil.h"
  20. using Jack::JackMidiRawOutputWriteQueue;
  21. #define STILL_TIME(c, b) ((! (b)) || ((c) < (b)))
  22. JackMidiRawOutputWriteQueue::
  23. JackMidiRawOutputWriteQueue(JackMidiSendQueue *send_queue, size_t non_rt_size,
  24. size_t max_non_rt_messages, size_t max_rt_messages)
  25. {
  26. non_rt_queue = new JackMidiAsyncQueue(non_rt_size, max_non_rt_messages);
  27. std::auto_ptr<JackMidiAsyncQueue> non_rt_ptr(non_rt_queue);
  28. rt_queue = new JackMidiAsyncQueue(max_rt_messages, max_rt_messages);
  29. std::auto_ptr<JackMidiAsyncQueue> rt_ptr(rt_queue);
  30. non_rt_event = 0;
  31. rt_event = 0;
  32. running_status = 0;
  33. this->send_queue = send_queue;
  34. rt_ptr.release();
  35. non_rt_ptr.release();
  36. }
  37. JackMidiRawOutputWriteQueue::~JackMidiRawOutputWriteQueue()
  38. {
  39. delete non_rt_queue;
  40. delete rt_queue;
  41. }
  42. bool
  43. JackMidiRawOutputWriteQueue::DequeueNonRealtimeEvent()
  44. {
  45. non_rt_event = non_rt_queue->DequeueEvent();
  46. bool result = non_rt_event != 0;
  47. if (result) {
  48. non_rt_event_time = non_rt_event->time;
  49. running_status = ApplyRunningStatus(non_rt_event, running_status);
  50. }
  51. return result;
  52. }
  53. bool
  54. JackMidiRawOutputWriteQueue::DequeueRealtimeEvent()
  55. {
  56. rt_event = rt_queue->DequeueEvent();
  57. bool result = rt_event != 0;
  58. if (result) {
  59. rt_event_time = rt_event->time;
  60. }
  61. return result;
  62. }
  63. Jack::JackMidiWriteQueue::EnqueueResult
  64. JackMidiRawOutputWriteQueue::EnqueueEvent(jack_nframes_t time, size_t size,
  65. jack_midi_data_t *buffer)
  66. {
  67. JackMidiAsyncQueue *queue = (size == 1) && (*buffer >= 0xf8) ? rt_queue :
  68. non_rt_queue;
  69. EnqueueResult result = queue->EnqueueEvent(time, size, buffer);
  70. if (result == OK) {
  71. last_enqueued_message_time = time;
  72. }
  73. return result;
  74. }
  75. void
  76. JackMidiRawOutputWriteQueue::HandleWriteQueueBug(jack_nframes_t time,
  77. jack_midi_data_t byte)
  78. {
  79. jack_error("JackMidiRawOutputWriteQueue::HandleWriteQueueBug - **BUG** "
  80. "The write queue told us that it couldn't enqueue a 1-byte "
  81. "MIDI event scheduled for frame '%d'. This is probably a bug "
  82. "in the write queue implementation.", time);
  83. }
  84. jack_nframes_t
  85. JackMidiRawOutputWriteQueue::Process(jack_nframes_t boundary_frame)
  86. {
  87. jack_nframes_t current_frame = send_queue->GetNextScheduleFrame();
  88. while (STILL_TIME(current_frame, boundary_frame)) {
  89. if (! non_rt_event) {
  90. DequeueNonRealtimeEvent();
  91. }
  92. if (! rt_event) {
  93. DequeueRealtimeEvent();
  94. }
  95. if (! (non_rt_event || rt_event)) {
  96. return 0;
  97. }
  98. if (! WriteRealtimeEvents(boundary_frame)) {
  99. break;
  100. }
  101. jack_nframes_t non_rt_boundary =
  102. rt_event && STILL_TIME(rt_event_time, boundary_frame) ?
  103. rt_event_time : boundary_frame;
  104. if (! WriteNonRealtimeEvents(non_rt_boundary)) {
  105. break;
  106. }
  107. current_frame = send_queue->GetNextScheduleFrame();
  108. }
  109. // If we get here, that means there is some sort of message available, and
  110. // that either we can't currently write to the write queue or we have
  111. // reached the boundary frame. Return the earliest time that a message is
  112. // scheduled to be sent.
  113. return ! non_rt_event ? rt_event_time :
  114. non_rt_event->size > 1 ? current_frame :
  115. ! rt_event ? non_rt_event_time :
  116. non_rt_event_time < rt_event_time ? non_rt_event_time : rt_event_time;
  117. }
  118. bool
  119. JackMidiRawOutputWriteQueue::SendByte(jack_nframes_t time,
  120. jack_midi_data_t byte)
  121. {
  122. switch (send_queue->EnqueueEvent(time, 1, &byte)) {
  123. case BUFFER_TOO_SMALL:
  124. HandleWriteQueueBug(time, byte);
  125. case OK:
  126. return true;
  127. default:
  128. // This is here to stop compilers from warning us about not handling
  129. // enumeration values.
  130. ;
  131. }
  132. return false;
  133. }
  134. bool
  135. JackMidiRawOutputWriteQueue::
  136. WriteNonRealtimeEvents(jack_nframes_t boundary_frame)
  137. {
  138. if (! non_rt_event) {
  139. if (! DequeueNonRealtimeEvent()) {
  140. return true;
  141. }
  142. }
  143. jack_nframes_t current_frame = send_queue->GetNextScheduleFrame();
  144. do {
  145. // Send out as much of the non-realtime buffer as we can, save for one
  146. // byte which we will send out when the message is supposed to arrive.
  147. for (; non_rt_event->size > 1;
  148. (non_rt_event->size)--, (non_rt_event->buffer)++) {
  149. if (! STILL_TIME(current_frame, boundary_frame)) {
  150. return true;
  151. }
  152. if (! SendByte(current_frame, *(non_rt_event->buffer))) {
  153. return false;
  154. }
  155. current_frame = send_queue->GetNextScheduleFrame();
  156. }
  157. if (! (STILL_TIME(current_frame, boundary_frame) &&
  158. STILL_TIME(non_rt_event_time, boundary_frame))) {
  159. return true;
  160. }
  161. // There's still time. Try to send the byte.
  162. if (! SendByte(non_rt_event_time, *(non_rt_event->buffer))) {
  163. return false;
  164. }
  165. current_frame = send_queue->GetNextScheduleFrame();
  166. if (! DequeueNonRealtimeEvent()) {
  167. break;
  168. }
  169. } while (STILL_TIME(current_frame, boundary_frame));
  170. return true;
  171. }
  172. bool
  173. JackMidiRawOutputWriteQueue::WriteRealtimeEvents(jack_nframes_t boundary_frame)
  174. {
  175. jack_nframes_t current_frame = send_queue->GetNextScheduleFrame();
  176. if (! rt_event) {
  177. if (! DequeueRealtimeEvent()) {
  178. return true;
  179. }
  180. }
  181. for (;;) {
  182. if (! STILL_TIME(current_frame, boundary_frame)) {
  183. return false;
  184. }
  185. // If:
  186. // -there's still time before we need to send the realtime event
  187. // -there's a non-realtime event available for sending
  188. // -non-realtime data can be scheduled before this event
  189. if ((rt_event_time > current_frame) && non_rt_event &&
  190. ((non_rt_event->size > 1) ||
  191. (non_rt_event_time < rt_event_time))) {
  192. return true;
  193. }
  194. if (! SendByte(rt_event_time, *(rt_event->buffer))) {
  195. return false;
  196. }
  197. current_frame = send_queue->GetNextScheduleFrame();
  198. if (! DequeueRealtimeEvent()) {
  199. return true;
  200. }
  201. }
  202. }