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.

124 lines
4.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 "JackALSARawMidiInputPort.h"
  18. #include "JackMidiUtil.h"
  19. #include "JackError.h"
  20. using Jack::JackALSARawMidiInputPort;
  21. JackALSARawMidiInputPort::JackALSARawMidiInputPort(snd_rawmidi_info_t *info,
  22. size_t index,
  23. size_t max_bytes,
  24. size_t max_messages):
  25. JackALSARawMidiPort(info, index, POLLIN)
  26. {
  27. alsa_event = 0;
  28. jack_event = 0;
  29. receive_queue = new JackALSARawMidiReceiveQueue(rawmidi, max_bytes);
  30. std::unique_ptr<JackALSARawMidiReceiveQueue> receive_ptr(receive_queue);
  31. thread_queue = new JackMidiAsyncQueue(max_bytes, max_messages);
  32. std::unique_ptr<JackMidiAsyncQueue> thread_ptr(thread_queue);
  33. write_queue = new JackMidiBufferWriteQueue();
  34. std::unique_ptr<JackMidiBufferWriteQueue> write_ptr(write_queue);
  35. raw_queue = new JackMidiRawInputWriteQueue(thread_queue, max_bytes,
  36. max_messages);
  37. write_ptr.release();
  38. thread_ptr.release();
  39. receive_ptr.release();
  40. }
  41. JackALSARawMidiInputPort::~JackALSARawMidiInputPort()
  42. {
  43. delete raw_queue;
  44. delete receive_queue;
  45. delete thread_queue;
  46. delete write_queue;
  47. }
  48. bool
  49. JackALSARawMidiInputPort::ProcessJack(JackMidiBuffer *port_buffer,
  50. jack_nframes_t frames)
  51. {
  52. write_queue->ResetMidiBuffer(port_buffer, frames);
  53. bool dequeued = false;
  54. if (! jack_event) {
  55. goto dequeue_event;
  56. }
  57. for (;;) {
  58. switch (write_queue->EnqueueEvent(jack_event, frames)) {
  59. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  60. jack_error("JackALSARawMidiInputPort::ProcessJack - The write "
  61. "queue couldn't enqueue a %d-byte event. Dropping "
  62. "event.", jack_event->size);
  63. // Fallthrough on purpose.
  64. case JackMidiWriteQueue::OK:
  65. break;
  66. default:
  67. goto trigger_queue_event;
  68. }
  69. dequeue_event:
  70. jack_event = thread_queue->DequeueEvent();
  71. if (! jack_event) {
  72. break;
  73. }
  74. dequeued = true;
  75. }
  76. trigger_queue_event:
  77. return dequeued ? TriggerQueueEvent() : true;
  78. }
  79. bool
  80. JackALSARawMidiInputPort::ProcessPollEvents(jack_nframes_t current_frame)
  81. {
  82. if (GetQueuePollEvent() == -1) {
  83. return false;
  84. }
  85. int io_event = GetIOPollEvent();
  86. switch (io_event) {
  87. case -1:
  88. return false;
  89. case 1:
  90. alsa_event = receive_queue->DequeueEvent();
  91. }
  92. if (alsa_event) {
  93. size_t size = alsa_event->size;
  94. size_t space = raw_queue->GetAvailableSpace();
  95. bool enough_room = space >= size;
  96. if (enough_room) {
  97. assert(raw_queue->EnqueueEvent(current_frame, size,
  98. alsa_event->buffer) ==
  99. JackMidiWriteQueue::OK);
  100. alsa_event = 0;
  101. } else if (space) {
  102. assert(raw_queue->EnqueueEvent(current_frame, space,
  103. alsa_event->buffer) ==
  104. JackMidiWriteQueue::OK);
  105. alsa_event->buffer += space;
  106. alsa_event->size -= space;
  107. }
  108. SetIOEventsEnabled(enough_room);
  109. }
  110. raw_queue->Process();
  111. return true;
  112. }