jack1 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.

134 lines
3.8KB

  1. /*
  2. * Copyright (c) 2006,2007 Dmitry S. Baikov
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifndef __jack_midi_unpack_h__
  19. #define __jack_midi_unpack_h__
  20. #include <jack/midiport.h>
  21. #include "engine.h"
  22. enum {
  23. MIDI_UNPACK_MAX_MSG = 1024
  24. };
  25. typedef struct {
  26. int pos, need, size;
  27. unsigned char data[MIDI_UNPACK_MAX_MSG];
  28. } midi_unpack_t;
  29. static inline
  30. void midi_unpack_init (midi_unpack_t *u)
  31. {
  32. u->pos = 0;
  33. u->size = sizeof(u->data);
  34. u->need = u->size;
  35. }
  36. static inline
  37. void midi_unpack_reset (midi_unpack_t *u)
  38. {
  39. u->pos = 0;
  40. u->need = u->size;
  41. }
  42. static const unsigned char midi_voice_len[] = {
  43. 3, /*0x80 Note Off*/
  44. 3, /*0x90 Note On*/
  45. 3, /*0xA0 Aftertouch*/
  46. 3, /*0xB0 Control Change*/
  47. 2, /*0xC0 Program Change*/
  48. 2, /*0xD0 Channel Pressure*/
  49. 3, /*0xE0 Pitch Wheel*/
  50. 1 /*0xF0 System*/
  51. };
  52. static const unsigned char midi_system_len[] = {
  53. 0, /*0xF0 System Exclusive Start*/
  54. 2, /*0xF1 MTC Quarter Frame*/
  55. 3, /*0xF2 Song Postion*/
  56. 2, /*0xF3 Song Select*/
  57. 0, /*0xF4 undefined*/
  58. 0, /*0xF5 undefined*/
  59. 1, /*0xF6 Tune Request*/
  60. 1 /*0xF7 System Exlusive End*/
  61. };
  62. static
  63. int midi_unpack_buf (midi_unpack_t *buf, const unsigned char *data, int len, void *jack_port_buf, jack_nframes_t time)
  64. {
  65. int i;
  66. for (i = 0; i < len; ++i) {
  67. const unsigned char byte = data[i];
  68. if (byte >= 0xF8) { // system realtime
  69. jack_midi_event_write (jack_port_buf, time, &data[i], 1);
  70. //jack_error("midi_unpack: written system relatime event\n");
  71. //midi_input_write(in, &data[i], 1);
  72. } else if (byte < 0x80) { // data
  73. assert (buf->pos < buf->size);
  74. buf->data[buf->pos++] = byte;
  75. } else if (byte < 0xF0) { // voice
  76. assert (byte >= 0x80 && byte < 0xF0);
  77. //buf->need = ((byte|0x0F) == 0xCF || (byte|0x0F)==0xDF) ? 2 : 3;
  78. buf->need = midi_voice_len[(byte - 0x80) >> 4];
  79. buf->data[0] = byte;
  80. buf->pos = 1;
  81. } else if (byte == 0xF7) { // sysex end
  82. assert (buf->pos < buf->size);
  83. buf->data[buf->pos++] = byte;
  84. buf->need = buf->pos;
  85. } else {
  86. assert (byte >= 0xF0 && byte < 0xF8);
  87. buf->pos = 1;
  88. buf->data[0] = byte;
  89. buf->need = midi_system_len[byte - 0xF0];
  90. if (!buf->need) {
  91. buf->need = buf->size;
  92. }
  93. }
  94. if (buf->pos == buf->need) {
  95. // TODO: deal with big sysex'es (they are silently dropped for now)
  96. if (buf->data[0] >= 0x80 || (buf->data[0] == 0xF0 && buf->data[buf->pos - 1] == 0xF7)) {
  97. /* convert Note On with velocity 0 to Note Off */
  98. if ((buf->data[0] & 0xF0) == 0x90 && buf->data[2] == 0) {
  99. // we use temp array here to keep running status in sync
  100. jack_midi_data_t temp[3] = { 0x80, 0, 0x40 };
  101. temp[0] |= buf->data[0] & 0x0F;
  102. temp[1] = buf->data[1];
  103. jack_midi_event_write (jack_port_buf, time, temp, 3);
  104. } else {
  105. jack_midi_event_write (jack_port_buf, time, &buf->data[0], buf->pos);
  106. }
  107. //jack_error("midi_unpack: written %d-byte event\n", buf->pos);
  108. //midi_input_write(in, &buf->data[0], buf->pos);
  109. }
  110. /* keep running status */
  111. if (buf->data[0] >= 0x80 && buf->data[0] < 0xF0) {
  112. buf->pos = 1;
  113. } else {
  114. buf->pos = 0;
  115. buf->need = buf->size;
  116. }
  117. }
  118. }
  119. assert (i == len);
  120. return i;
  121. }
  122. #endif /* __jack_midi_unpack_h__ */