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.

185 lines
5.9KB

  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 "JackCoreMidiInputPort.h"
  18. #include "JackMidiUtil.h"
  19. #include "JackError.h"
  20. using Jack::JackCoreMidiInputPort;
  21. JackCoreMidiInputPort::JackCoreMidiInputPort(double time_ratio,
  22. size_t max_bytes,
  23. size_t max_messages):
  24. JackCoreMidiPort(time_ratio)
  25. {
  26. thread_queue = new JackMidiAsyncQueue(max_bytes, max_messages);
  27. std::auto_ptr<JackMidiAsyncQueue> thread_queue_ptr(thread_queue);
  28. write_queue = new JackMidiBufferWriteQueue();
  29. std::auto_ptr<JackMidiBufferWriteQueue> write_queue_ptr(write_queue);
  30. sysex_buffer = new jack_midi_data_t[max_bytes];
  31. write_queue_ptr.release();
  32. thread_queue_ptr.release();
  33. jack_event = 0;
  34. }
  35. JackCoreMidiInputPort::~JackCoreMidiInputPort()
  36. {
  37. delete thread_queue;
  38. delete write_queue;
  39. delete[] sysex_buffer;
  40. }
  41. jack_nframes_t
  42. JackCoreMidiInputPort::GetFramesFromTimeStamp(MIDITimeStamp timestamp)
  43. {
  44. return GetFramesFromTime((jack_time_t) (timestamp * time_ratio));
  45. }
  46. void
  47. JackCoreMidiInputPort::Initialize(const char *alias_name,
  48. const char *client_name,
  49. const char *driver_name, int index,
  50. MIDIEndpointRef endpoint)
  51. {
  52. JackCoreMidiPort::Initialize(alias_name, client_name, driver_name, index, endpoint, false);
  53. }
  54. void
  55. JackCoreMidiInputPort::ProcessCoreMidi(const MIDIPacketList *packet_list)
  56. {
  57. set_threaded_log_function();
  58. unsigned int packet_count = packet_list->numPackets;
  59. assert(packet_count);
  60. MIDIPacket *packet = (MIDIPacket *) packet_list->packet;
  61. for (unsigned int i = 0; i < packet_count; i++) {
  62. jack_midi_data_t *data = packet->data;
  63. size_t size = packet->length;
  64. assert(size);
  65. jack_midi_event_t event;
  66. // XX: There might be dragons in my spaghetti. This code is begging
  67. // for a rewrite.
  68. if (sysex_bytes_sent) {
  69. if (data[0] & 0x80) {
  70. jack_error("JackCoreMidiInputPort::ProcessCoreMidi - System "
  71. "exclusive message aborted.");
  72. sysex_bytes_sent = 0;
  73. goto parse_event;
  74. }
  75. buffer_sysex_bytes:
  76. if ((sysex_bytes_sent + size) <= sizeof(sysex_buffer)) {
  77. memcpy(sysex_buffer + sysex_bytes_sent, packet,
  78. size * sizeof(jack_midi_data_t));
  79. }
  80. sysex_bytes_sent += size;
  81. if (data[size - 1] == 0xf7) {
  82. if (sysex_bytes_sent > sizeof(sysex_buffer)) {
  83. jack_error("JackCoreMidiInputPort::ProcessCoreMidi - "
  84. "Could not buffer a %d-byte system exclusive "
  85. "message. Discarding message.",
  86. sysex_bytes_sent);
  87. sysex_bytes_sent = 0;
  88. goto get_next_packet;
  89. }
  90. event.buffer = sysex_buffer;
  91. event.size = sysex_bytes_sent;
  92. sysex_bytes_sent = 0;
  93. goto send_event;
  94. }
  95. goto get_next_packet;
  96. }
  97. parse_event:
  98. if (data[0] == 0xf0) {
  99. if (data[size - 1] != 0xf7) {
  100. goto buffer_sysex_bytes;
  101. }
  102. }
  103. event.buffer = data;
  104. event.size = size;
  105. send_event:
  106. event.time = GetFramesFromTimeStamp(packet->timeStamp);
  107. switch (thread_queue->EnqueueEvent(&event)) {
  108. case JackMidiWriteQueue::BUFFER_FULL:
  109. jack_error("JackCoreMidiInputPort::ProcessCoreMidi - The thread "
  110. "queue buffer is full. Dropping event.");
  111. break;
  112. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  113. jack_error("JackCoreMidiInputPort::ProcessCoreMidi - The thread "
  114. "queue couldn't enqueue a %d-byte packet. Dropping "
  115. "event.", event.size);
  116. break;
  117. default:
  118. ;
  119. }
  120. get_next_packet:
  121. packet = MIDIPacketNext(packet);
  122. assert(packet);
  123. }
  124. }
  125. void
  126. JackCoreMidiInputPort::ProcessJack(JackMidiBuffer *port_buffer,
  127. jack_nframes_t frames)
  128. {
  129. write_queue->ResetMidiBuffer(port_buffer, frames);
  130. if (! jack_event) {
  131. jack_event = thread_queue->DequeueEvent();
  132. }
  133. for (; jack_event; jack_event = thread_queue->DequeueEvent()) {
  134. // Add 'frames' to MIDI events to align with audio.
  135. switch (write_queue->EnqueueEvent(jack_event, frames)) {
  136. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  137. jack_error("JackCoreMidiInputPort::ProcessJack - The write queue "
  138. "couldn't enqueue a %d-byte event. Dropping event.",
  139. jack_event->size);
  140. // Fallthrough on purpose
  141. case JackMidiWriteQueue::OK:
  142. continue;
  143. default:
  144. ;
  145. }
  146. break;
  147. }
  148. }
  149. bool
  150. JackCoreMidiInputPort::Start()
  151. {
  152. // Hack: Get rid of any messages that might have come in before starting
  153. // the engine.
  154. while (thread_queue->DequeueEvent());
  155. sysex_bytes_sent = 0;
  156. return true;
  157. }
  158. bool
  159. JackCoreMidiInputPort::Stop()
  160. {
  161. return true;
  162. }