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.

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