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.

147 lines
5.0KB

  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 <memory>
  17. #include "JackALSARawMidiOutputPort.h"
  18. using Jack::JackALSARawMidiOutputPort;
  19. JackALSARawMidiOutputPort::JackALSARawMidiOutputPort(snd_rawmidi_info_t *info,
  20. size_t index,
  21. size_t max_bytes_per_poll,
  22. size_t max_bytes,
  23. size_t max_messages):
  24. JackALSARawMidiPort(info, index, POLLOUT)
  25. {
  26. alsa_event = 0;
  27. read_queue = new JackMidiBufferReadQueue();
  28. std::auto_ptr<JackMidiBufferReadQueue> read_ptr(read_queue);
  29. send_queue = new JackALSARawMidiSendQueue(rawmidi, max_bytes_per_poll);
  30. std::auto_ptr<JackALSARawMidiSendQueue> send_ptr(send_queue);
  31. thread_queue = new JackMidiAsyncQueue(max_bytes, max_messages);
  32. std::auto_ptr<JackMidiAsyncQueue> thread_ptr(thread_queue);
  33. raw_queue = new JackMidiRawOutputWriteQueue(send_queue, max_bytes,
  34. max_messages, max_messages);
  35. thread_ptr.release();
  36. send_ptr.release();
  37. read_ptr.release();
  38. }
  39. JackALSARawMidiOutputPort::~JackALSARawMidiOutputPort()
  40. {
  41. delete raw_queue;
  42. delete read_queue;
  43. delete send_queue;
  44. delete thread_queue;
  45. }
  46. bool
  47. JackALSARawMidiOutputPort::ProcessJack(JackMidiBuffer *port_buffer,
  48. jack_nframes_t frames)
  49. {
  50. read_queue->ResetMidiBuffer(port_buffer);
  51. bool enqueued = false;
  52. for (jack_midi_event_t *event = read_queue->DequeueEvent(); event;
  53. event = read_queue->DequeueEvent()) {
  54. switch (thread_queue->EnqueueEvent(event, frames)) {
  55. case JackMidiWriteQueue::BUFFER_FULL:
  56. jack_error("JackALSARawMidiOutputPort::ProcessJack - The thread "
  57. "queue doesn't have enough room to enqueue a %d-byte "
  58. "event. Dropping event.", event->size);
  59. continue;
  60. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  61. jack_error("JackALSARawMidiOutputPort::ProcessJack - The thread "
  62. "queue is too small to enqueue a %d-byte event. "
  63. "Dropping event.", event->size);
  64. continue;
  65. default:
  66. enqueued = true;
  67. }
  68. }
  69. return enqueued ? TriggerQueueEvent() : true;
  70. }
  71. bool
  72. JackALSARawMidiOutputPort::ProcessPollEvents(bool handle_output, bool timeout,
  73. jack_nframes_t *frame)
  74. {
  75. int io_event;
  76. int queue_event;
  77. send_queue->ResetPollByteCount();
  78. if (! handle_output) {
  79. assert(timeout);
  80. goto process_raw_queue;
  81. }
  82. io_event = GetIOPollEvent();
  83. if (io_event == -1) {
  84. return false;
  85. }
  86. queue_event = GetQueuePollEvent();
  87. if (queue_event == -1) {
  88. return false;
  89. }
  90. if (io_event || timeout) {
  91. process_raw_queue:
  92. // We call the 'Process' event early because there are events waiting
  93. // to be processed that either need to be sent now, or before now.
  94. raw_queue->Process();
  95. } else if (! queue_event) {
  96. return true;
  97. }
  98. if (! alsa_event) {
  99. alsa_event = thread_queue->DequeueEvent();
  100. }
  101. for (; alsa_event; alsa_event = thread_queue->DequeueEvent()) {
  102. switch (raw_queue->EnqueueEvent(alsa_event)) {
  103. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  104. jack_error("JackALSARawMidiOutputPort::ProcessQueues - The raw "
  105. "output queue couldn't enqueue a %d-byte event. "
  106. "Dropping event.", alsa_event->size);
  107. // Fallthrough on purpose.
  108. case JackMidiWriteQueue::OK:
  109. continue;
  110. default:
  111. ;
  112. }
  113. // Try to free up some space by processing events early.
  114. *frame = raw_queue->Process();
  115. switch (raw_queue->EnqueueEvent(alsa_event)) {
  116. case JackMidiWriteQueue::BUFFER_FULL:
  117. goto set_io_events;
  118. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  119. // This shouldn't happen.
  120. assert(false);
  121. default:
  122. ;
  123. }
  124. }
  125. *frame = raw_queue->Process();
  126. set_io_events:
  127. bool blocked = send_queue->IsBlocked();
  128. SetIOEventsEnabled(blocked);
  129. if (blocked) {
  130. *frame = 0;
  131. }
  132. return true;
  133. }