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.

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