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.

159 lines
5.0KB

  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::unique_ptr<JackMidiAsyncQueue> non_rt_ptr(non_rt_queue);
  28. rt_queue = new JackMidiAsyncQueue(max_rt_messages, max_rt_messages);
  29. std::unique_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. void
  43. JackMidiRawOutputWriteQueue::DequeueNonRealtimeEvent()
  44. {
  45. non_rt_event = non_rt_queue->DequeueEvent();
  46. if (non_rt_event) {
  47. non_rt_event_time = non_rt_event->time;
  48. running_status = ApplyRunningStatus(non_rt_event, running_status);
  49. }
  50. }
  51. void
  52. JackMidiRawOutputWriteQueue::DequeueRealtimeEvent()
  53. {
  54. rt_event = rt_queue->DequeueEvent();
  55. if (rt_event) {
  56. rt_event_time = rt_event->time;
  57. }
  58. }
  59. Jack::JackMidiWriteQueue::EnqueueResult
  60. JackMidiRawOutputWriteQueue::EnqueueEvent(jack_nframes_t time, size_t size,
  61. jack_midi_data_t *buffer)
  62. {
  63. JackMidiAsyncQueue *queue = (size == 1) && (*buffer >= 0xf8) ? rt_queue :
  64. non_rt_queue;
  65. return queue->EnqueueEvent(time, size, buffer);
  66. }
  67. void
  68. JackMidiRawOutputWriteQueue::HandleWriteQueueBug(jack_nframes_t time,
  69. jack_midi_data_t byte)
  70. {
  71. jack_error("JackMidiRawOutputWriteQueue::HandleWriteQueueBug - **BUG** "
  72. "The write queue told us that it couldn't enqueue a 1-byte "
  73. "MIDI event scheduled for frame '%d'. This is probably a bug "
  74. "in the write queue implementation.", time);
  75. }
  76. jack_nframes_t
  77. JackMidiRawOutputWriteQueue::Process(jack_nframes_t boundary_frame)
  78. {
  79. if (! non_rt_event) {
  80. DequeueNonRealtimeEvent();
  81. }
  82. if (! rt_event) {
  83. DequeueRealtimeEvent();
  84. }
  85. while (rt_event) {
  86. jack_nframes_t current_frame = send_queue->GetNextScheduleFrame();
  87. if ((rt_event_time > current_frame) && non_rt_event &&
  88. (non_rt_event_time < rt_event_time)) {
  89. if (! SendNonRTBytes(rt_event_time < boundary_frame ?
  90. rt_event_time : boundary_frame)) {
  91. return non_rt_event_time;
  92. }
  93. current_frame = send_queue->GetNextScheduleFrame();
  94. }
  95. if (! STILL_TIME(current_frame, boundary_frame)) {
  96. return (! non_rt_event) ? rt_event_time :
  97. non_rt_event_time < rt_event_time ? non_rt_event_time :
  98. rt_event_time;
  99. }
  100. if (! SendByte(rt_event_time, *(rt_event->buffer))) {
  101. return rt_event_time;
  102. }
  103. DequeueRealtimeEvent();
  104. }
  105. SendNonRTBytes(boundary_frame);
  106. return non_rt_event ? non_rt_event_time : 0;
  107. }
  108. bool
  109. JackMidiRawOutputWriteQueue::SendByte(jack_nframes_t time,
  110. jack_midi_data_t byte)
  111. {
  112. switch (send_queue->EnqueueEvent(time, 1, &byte)) {
  113. case BUFFER_TOO_SMALL:
  114. HandleWriteQueueBug(time, byte);
  115. case OK:
  116. return true;
  117. default:
  118. // This is here to stop compilers from warning us about not handling
  119. // enumeration values.
  120. ;
  121. }
  122. return false;
  123. }
  124. bool
  125. JackMidiRawOutputWriteQueue::SendNonRTBytes(jack_nframes_t boundary_frame)
  126. {
  127. while (non_rt_event) {
  128. for (; non_rt_event->size;
  129. (non_rt_event->size)--, (non_rt_event->buffer)++) {
  130. jack_nframes_t current_frame = send_queue->GetNextScheduleFrame();
  131. if (! STILL_TIME(current_frame, boundary_frame)) {
  132. return true;
  133. }
  134. if (! SendByte(non_rt_event_time, *(non_rt_event->buffer))) {
  135. return false;
  136. }
  137. }
  138. DequeueNonRealtimeEvent();
  139. }
  140. return true;
  141. }