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.

288 lines
8.5KB

  1. /*
  2. Copyright (C) 2009 Devin Anderson
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <cassert>
  16. #include <cstring>
  17. #include <new>
  18. #include "JackError.h"
  19. #include "JackPhysicalMidiInput.h"
  20. namespace Jack {
  21. JackPhysicalMidiInput::JackPhysicalMidiInput(size_t buffer_size)
  22. {
  23. size_t datum_size = sizeof(jack_midi_data_t);
  24. assert(buffer_size > 0);
  25. input_ring = jack_ringbuffer_create((buffer_size + 1) * datum_size);
  26. if (! input_ring) {
  27. throw std::bad_alloc();
  28. }
  29. jack_ringbuffer_mlock(input_ring);
  30. Clear();
  31. expected_data_bytes = 0;
  32. status_byte = 0;
  33. }
  34. JackPhysicalMidiInput::~JackPhysicalMidiInput()
  35. {
  36. jack_ringbuffer_free(input_ring);
  37. }
  38. void
  39. JackPhysicalMidiInput::Clear()
  40. {
  41. jack_ringbuffer_reset(input_ring);
  42. buffered_bytes = 0;
  43. unbuffered_bytes = 0;
  44. }
  45. void
  46. JackPhysicalMidiInput::HandleBufferFailure(size_t unbuffered_bytes,
  47. size_t total_bytes)
  48. {
  49. jack_error("%d MIDI byte(s) of a %d byte message could not be buffered - "
  50. "message dropped", unbuffered_bytes, total_bytes);
  51. }
  52. void
  53. JackPhysicalMidiInput::HandleIncompleteMessage(size_t bytes)
  54. {
  55. jack_error("Discarding %d MIDI byte(s) - incomplete message (cable "
  56. "unplugged?)", bytes);
  57. }
  58. void
  59. JackPhysicalMidiInput::HandleInvalidStatusByte(jack_midi_data_t status)
  60. {
  61. jack_error("Dropping invalid MIDI status byte '%x'",
  62. (unsigned int) status);
  63. }
  64. void
  65. JackPhysicalMidiInput::HandleUnexpectedSysexEnd(size_t bytes)
  66. {
  67. jack_error("Discarding %d MIDI byte(s) - received sysex end without sysex "
  68. "start (cable unplugged?)", bytes);
  69. }
  70. void
  71. JackPhysicalMidiInput::HandleWriteFailure(size_t bytes)
  72. {
  73. jack_error("Failed to write a %d byte MIDI message to the port buffer",
  74. bytes);
  75. }
  76. void
  77. JackPhysicalMidiInput::Process(jack_nframes_t frames)
  78. {
  79. assert(port_buffer);
  80. port_buffer->Reset(frames);
  81. jack_nframes_t current_frame = 0;
  82. size_t datum_size = sizeof(jack_midi_data_t);
  83. for (;;) {
  84. jack_midi_data_t datum;
  85. current_frame = Receive(&datum, current_frame, frames);
  86. if (current_frame >= frames) {
  87. break;
  88. }
  89. jack_log("JackPhysicalMidiInput::Process (%d) - Received '%x' byte",
  90. current_frame, (unsigned int) datum);
  91. if (datum >= 0xf8) {
  92. // Realtime
  93. if (datum == 0xfd) {
  94. HandleInvalidStatusByte(datum);
  95. } else {
  96. jack_log("JackPhysicalMidiInput::Process - Writing realtime "
  97. "event.");
  98. WriteByteEvent(current_frame, datum);
  99. }
  100. continue;
  101. }
  102. if (datum == 0xf7) {
  103. // Sysex end
  104. if (status_byte != 0xf0) {
  105. HandleUnexpectedSysexEnd(buffered_bytes + unbuffered_bytes);
  106. Clear();
  107. expected_data_bytes = 0;
  108. status_byte = 0;
  109. } else {
  110. jack_log("JackPhysicalMidiInput::Process - Writing sysex "
  111. "event.");
  112. WriteBufferedSysexEvent(current_frame);
  113. }
  114. continue;
  115. }
  116. if (datum >= 0x80) {
  117. // We're handling a non-realtime status byte
  118. jack_log("JackPhysicalMidiInput::Process - Handling non-realtime "
  119. "status byte.");
  120. if (buffered_bytes || unbuffered_bytes) {
  121. HandleIncompleteMessage(buffered_bytes + unbuffered_bytes + 1);
  122. Clear();
  123. }
  124. status_byte = datum;
  125. switch (datum & 0xf0) {
  126. case 0x80:
  127. case 0x90:
  128. case 0xa0:
  129. case 0xb0:
  130. case 0xe0:
  131. // Note On, Note Off, Aftertouch, Control Change, Pitch Wheel
  132. expected_data_bytes = 2;
  133. break;
  134. case 0xc0:
  135. case 0xd0:
  136. // Program Change, Channel Pressure
  137. expected_data_bytes = 1;
  138. break;
  139. case 0xf0:
  140. switch (datum) {
  141. case 0xf0:
  142. // Sysex message
  143. expected_data_bytes = 0;
  144. break;
  145. case 0xf1:
  146. case 0xf3:
  147. // MTC Quarter frame, Song Select
  148. expected_data_bytes = 1;
  149. break;
  150. case 0xf2:
  151. // Song Position
  152. expected_data_bytes = 2;
  153. break;
  154. case 0xf4:
  155. case 0xf5:
  156. // Undefined
  157. HandleInvalidStatusByte(datum);
  158. expected_data_bytes = 0;
  159. status_byte = 0;
  160. break;
  161. case 0xf6:
  162. // Tune Request
  163. WriteByteEvent(current_frame, datum);
  164. expected_data_bytes = 0;
  165. status_byte = 0;
  166. }
  167. break;
  168. }
  169. continue;
  170. }
  171. // We're handling a data byte
  172. jack_log("JackPhysicalMidiInput::Process - Buffering data byte.");
  173. if (jack_ringbuffer_write(input_ring, (const char *) &datum,
  174. datum_size) == datum_size) {
  175. buffered_bytes++;
  176. } else {
  177. unbuffered_bytes++;
  178. }
  179. unsigned long total_bytes = buffered_bytes + unbuffered_bytes;
  180. assert((! expected_data_bytes) ||
  181. (total_bytes <= expected_data_bytes));
  182. if (total_bytes == expected_data_bytes) {
  183. if (! unbuffered_bytes) {
  184. jack_log("JackPhysicalMidiInput::Process - Writing buffered "
  185. "event.");
  186. WriteBufferedEvent(current_frame);
  187. } else {
  188. HandleBufferFailure(unbuffered_bytes, total_bytes);
  189. Clear();
  190. }
  191. if (status_byte >= 0xf0) {
  192. expected_data_bytes = 0;
  193. status_byte = 0;
  194. }
  195. }
  196. }
  197. }
  198. void
  199. JackPhysicalMidiInput::WriteBufferedEvent(jack_nframes_t frame)
  200. {
  201. assert(port_buffer && port_buffer->IsValid());
  202. size_t space = jack_ringbuffer_read_space(input_ring);
  203. jack_midi_data_t *event = port_buffer->ReserveEvent(frame, space + 1);
  204. if (event) {
  205. jack_ringbuffer_data_t vector[2];
  206. jack_ringbuffer_get_read_vector(input_ring, vector);
  207. event[0] = status_byte;
  208. size_t data_length_1 = vector[0].len;
  209. memcpy(event + 1, vector[0].buf, data_length_1);
  210. size_t data_length_2 = vector[1].len;
  211. if (data_length_2) {
  212. memcpy(event + data_length_1 + 1, vector[1].buf, data_length_2);
  213. }
  214. } else {
  215. HandleWriteFailure(space + 1);
  216. }
  217. Clear();
  218. }
  219. void
  220. JackPhysicalMidiInput::WriteBufferedSysexEvent(jack_nframes_t frame)
  221. {
  222. assert(port_buffer && port_buffer->IsValid());
  223. size_t space = jack_ringbuffer_read_space(input_ring);
  224. jack_midi_data_t *event = port_buffer->ReserveEvent(frame, space + 2);
  225. if (event) {
  226. jack_ringbuffer_data_t vector[2];
  227. jack_ringbuffer_get_read_vector(input_ring, vector);
  228. event[0] = status_byte;
  229. size_t data_length_1 = vector[0].len;
  230. memcpy(event + 1, vector[0].buf, data_length_1);
  231. size_t data_length_2 = vector[1].len;
  232. if (data_length_2) {
  233. memcpy(event + data_length_1 + 1, vector[1].buf, data_length_2);
  234. }
  235. event[data_length_1 + data_length_2 + 1] = 0xf7;
  236. } else {
  237. HandleWriteFailure(space + 2);
  238. }
  239. Clear();
  240. }
  241. void
  242. JackPhysicalMidiInput::WriteByteEvent(jack_nframes_t frame,
  243. jack_midi_data_t datum)
  244. {
  245. assert(port_buffer && port_buffer->IsValid());
  246. jack_midi_data_t *event = port_buffer->ReserveEvent(frame, 1);
  247. if (event) {
  248. event[0] = datum;
  249. } else {
  250. HandleWriteFailure(1);
  251. }
  252. }
  253. }