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.

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