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.

JackMidiAPI.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright (C) 2007 Dmitry Baikov
  3. Original JACK MIDI implementation Copyright (C) 2004 Ian Esten
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "JackError.h"
  17. #include "JackMidiPort.h"
  18. #include <errno.h>
  19. #include <string.h>
  20. #ifdef __cplusplus
  21. extern "C"
  22. {
  23. #endif
  24. LIB_EXPORT uint32_t jack_midi_get_event_count(void* port_buffer);
  25. LIB_EXPORT int jack_midi_event_get(jack_midi_event_t* event,
  26. void* port_buffer, uint32_t event_index);
  27. LIB_EXPORT void jack_midi_clear_buffer(void* port_buffer);
  28. LIB_EXPORT void jack_midi_reset_buffer(void* port_buffer);
  29. LIB_EXPORT size_t jack_midi_max_event_size(void* port_buffer);
  30. LIB_EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
  31. jack_nframes_t time, size_t data_size);
  32. LIB_EXPORT int jack_midi_event_write(void* port_buffer,
  33. jack_nframes_t time, const jack_midi_data_t* data, size_t data_size);
  34. LIB_EXPORT jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer);
  35. #ifdef __cplusplus
  36. }
  37. #endif
  38. using namespace Jack;
  39. LIB_EXPORT
  40. uint32_t jack_midi_get_event_count(void* port_buffer)
  41. {
  42. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  43. if (!buf || !buf->IsValid()) {
  44. return 0;
  45. }
  46. return buf->event_count;
  47. }
  48. LIB_EXPORT
  49. int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, uint32_t event_index)
  50. {
  51. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  52. if (!buf || !buf->IsValid()) {
  53. return -EINVAL;
  54. }
  55. if (event_index >= buf->event_count) {
  56. return -ENOBUFS;
  57. }
  58. JackMidiEvent* ev = &buf->events[event_index];
  59. event->time = ev->time;
  60. event->size = ev->size;
  61. event->buffer = ev->GetData(buf);
  62. return 0;
  63. }
  64. LIB_EXPORT
  65. void jack_midi_clear_buffer(void* port_buffer)
  66. {
  67. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  68. if (buf && buf->IsValid()) {
  69. buf->Reset(buf->nframes);
  70. }
  71. }
  72. LIB_EXPORT
  73. void jack_midi_reset_buffer(void* port_buffer)
  74. {
  75. MidiBufferInit(port_buffer, BUFFER_SIZE_MAX, BUFFER_SIZE_MAX);
  76. }
  77. LIB_EXPORT
  78. size_t jack_midi_max_event_size(void* port_buffer)
  79. {
  80. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  81. if (buf && buf->IsValid()) {
  82. return buf->MaxEventSize();
  83. }
  84. return 0;
  85. }
  86. LIB_EXPORT
  87. jack_midi_data_t* jack_midi_event_reserve(void* port_buffer, jack_nframes_t time, size_t data_size)
  88. {
  89. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  90. if (! buf) {
  91. jack_error("jack_midi_event_reserve: port buffer is set to NULL");
  92. return 0;
  93. }
  94. if (! buf->IsValid()) {
  95. jack_error("jack_midi_event_reserve: port buffer is invalid");
  96. return 0;
  97. }
  98. if (time >= buf->nframes) {
  99. jack_error("jack_midi_event_reserve: time parameter is out of range "
  100. "(%lu >= %lu)", time, buf->nframes);
  101. return 0;
  102. }
  103. if (buf->event_count && (buf->events[buf->event_count - 1].time > time)) {
  104. jack_error("jack_midi_event_reserve: time parameter is earlier than "
  105. "last reserved event");
  106. return 0;
  107. }
  108. return buf->ReserveEvent(time, data_size);
  109. }
  110. LIB_EXPORT
  111. int jack_midi_event_write(void* port_buffer,
  112. jack_nframes_t time, const jack_midi_data_t* data, size_t data_size)
  113. {
  114. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  115. if (!buf || !buf->IsValid()) {
  116. return -EINVAL;
  117. }
  118. if (time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time)) {
  119. return -EINVAL;
  120. }
  121. jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
  122. if (!dest) {
  123. return -ENOBUFS;
  124. }
  125. memcpy(dest, data, data_size);
  126. return 0;
  127. }
  128. LIB_EXPORT
  129. uint32_t jack_midi_get_lost_event_count(void* port_buffer)
  130. {
  131. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  132. if (buf && buf->IsValid()) {
  133. return buf->lost_events;
  134. }
  135. return 0;
  136. }