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.

211 lines
7.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 "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. running_status_buf[0] = 0;
  35. }
  36. JackCoreMidiInputPort::~JackCoreMidiInputPort()
  37. {
  38. delete thread_queue;
  39. delete write_queue;
  40. delete[] sysex_buffer;
  41. }
  42. jack_nframes_t
  43. JackCoreMidiInputPort::GetFramesFromTimeStamp(MIDITimeStamp timestamp)
  44. {
  45. return GetFramesFromTime((jack_time_t) (timestamp * time_ratio));
  46. }
  47. void
  48. JackCoreMidiInputPort::Initialize(const char *alias_name,
  49. const char *client_name,
  50. const char *driver_name, int index,
  51. MIDIEndpointRef endpoint)
  52. {
  53. JackCoreMidiPort::Initialize(alias_name, client_name, driver_name, index, endpoint, false);
  54. }
  55. void
  56. JackCoreMidiInputPort::ProcessCoreMidi(const MIDIPacketList *packet_list)
  57. {
  58. set_threaded_log_function();
  59. // TODO: maybe parsing should be done by JackMidiRawInputWriteQueue instead
  60. unsigned int packet_count = packet_list->numPackets;
  61. assert(packet_count);
  62. MIDIPacket *packet = (MIDIPacket *) packet_list->packet;
  63. for (unsigned int i = 0; i < packet_count; i++) {
  64. jack_midi_data_t *data = packet->data;
  65. size_t size = packet->length;
  66. assert(size);
  67. jack_midi_event_t event;
  68. // XX: There might be dragons in my spaghetti. This code is begging
  69. // for a rewrite.
  70. if (sysex_bytes_sent) {
  71. if (data[0] & 0x80) {
  72. jack_error("JackCoreMidiInputPort::ProcessCoreMidi - System "
  73. "exclusive message aborted.");
  74. sysex_bytes_sent = 0;
  75. goto parse_event;
  76. }
  77. buffer_sysex_bytes:
  78. if ((sysex_bytes_sent + size) <= sizeof(sysex_buffer)) {
  79. memcpy(sysex_buffer + sysex_bytes_sent, packet,
  80. size * sizeof(jack_midi_data_t));
  81. }
  82. sysex_bytes_sent += size;
  83. if (data[size - 1] == 0xf7) {
  84. if (sysex_bytes_sent > sizeof(sysex_buffer)) {
  85. jack_error("JackCoreMidiInputPort::ProcessCoreMidi - "
  86. "Could not buffer a %d-byte system exclusive "
  87. "message. Discarding message.",
  88. sysex_bytes_sent);
  89. sysex_bytes_sent = 0;
  90. goto get_next_packet;
  91. }
  92. event.buffer = sysex_buffer;
  93. event.size = sysex_bytes_sent;
  94. sysex_bytes_sent = 0;
  95. goto send_event;
  96. }
  97. goto get_next_packet;
  98. }
  99. parse_event:
  100. if (data[0] == 0xf0) {
  101. if (data[size - 1] != 0xf7) {
  102. goto buffer_sysex_bytes;
  103. }
  104. }
  105. // regular status byte ?
  106. if ((data[0] & 0x80) || !running_status_buf[0] ||
  107. (size + 1) > sizeof(running_status_buf))
  108. { // valid status byte (or invalid "running status") ...
  109. event.buffer = data;
  110. event.size = size;
  111. // store status byte for eventual "running status" in next event
  112. if (data[0] & 0x80) {
  113. if (data[0] < 0xf0) {
  114. // "running status" is only allowed for channel messages
  115. running_status_buf[0] = data[0];
  116. } else if (data[0] < 0xf8) {
  117. // "system common" messages (0xf0..0xf7) shall reset any running
  118. // status, however "realtime" messages (0xf8..0xff) shall be
  119. // ignored here
  120. running_status_buf[0] = 0;
  121. }
  122. }
  123. } else { // "running status" mode ...
  124. memcpy(&running_status_buf[1], data, size);
  125. event.buffer = running_status_buf;
  126. event.size = size + 1;
  127. }
  128. send_event:
  129. event.time = GetFramesFromTimeStamp(packet->timeStamp);
  130. switch (thread_queue->EnqueueEvent(&event)) {
  131. case JackMidiWriteQueue::BUFFER_FULL:
  132. jack_error("JackCoreMidiInputPort::ProcessCoreMidi - The thread "
  133. "queue buffer is full. Dropping event.");
  134. break;
  135. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  136. jack_error("JackCoreMidiInputPort::ProcessCoreMidi - The thread "
  137. "queue couldn't enqueue a %d-byte packet. Dropping "
  138. "event.", event.size);
  139. break;
  140. default:
  141. ;
  142. }
  143. get_next_packet:
  144. packet = MIDIPacketNext(packet);
  145. assert(packet);
  146. }
  147. }
  148. void
  149. JackCoreMidiInputPort::ProcessJack(JackMidiBuffer *port_buffer,
  150. jack_nframes_t frames)
  151. {
  152. write_queue->ResetMidiBuffer(port_buffer, frames);
  153. if (! jack_event) {
  154. jack_event = thread_queue->DequeueEvent();
  155. }
  156. for (; jack_event; jack_event = thread_queue->DequeueEvent()) {
  157. // Add 'frames' to MIDI events to align with audio.
  158. switch (write_queue->EnqueueEvent(jack_event, frames)) {
  159. case JackMidiWriteQueue::BUFFER_TOO_SMALL:
  160. jack_error("JackCoreMidiInputPort::ProcessJack - The write queue "
  161. "couldn't enqueue a %d-byte event. Dropping event.",
  162. jack_event->size);
  163. // Fallthrough on purpose
  164. case JackMidiWriteQueue::OK:
  165. continue;
  166. default:
  167. ;
  168. }
  169. break;
  170. }
  171. }
  172. bool
  173. JackCoreMidiInputPort::Start()
  174. {
  175. // Hack: Get rid of any messages that might have come in before starting
  176. // the engine.
  177. while (thread_queue->DequeueEvent());
  178. sysex_bytes_sent = 0;
  179. running_status_buf[0] = 0;
  180. return true;
  181. }
  182. bool
  183. JackCoreMidiInputPort::Stop()
  184. {
  185. return true;
  186. }