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.

151 lines
4.3KB

  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 size_t jack_midi_max_event_size(void* port_buffer);
  29. LIB_EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
  30. jack_nframes_t time, size_t data_size);
  31. LIB_EXPORT int jack_midi_event_write(void* port_buffer,
  32. jack_nframes_t time, const jack_midi_data_t* data, size_t data_size);
  33. LIB_EXPORT jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer);
  34. #ifdef __cplusplus
  35. }
  36. #endif
  37. using namespace Jack;
  38. LIB_EXPORT
  39. uint32_t jack_midi_get_event_count(void* port_buffer)
  40. {
  41. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  42. if (!buf || !buf->IsValid()) {
  43. return 0;
  44. }
  45. return buf->event_count;
  46. }
  47. LIB_EXPORT
  48. int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, uint32_t event_index)
  49. {
  50. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  51. if (!buf || !buf->IsValid()) {
  52. return -EINVAL;
  53. }
  54. if (event_index >= buf->event_count) {
  55. return -ENOBUFS;
  56. }
  57. JackMidiEvent* ev = &buf->events[event_index];
  58. event->time = ev->time;
  59. event->size = ev->size;
  60. event->buffer = ev->GetData(buf);
  61. return 0;
  62. }
  63. LIB_EXPORT
  64. void jack_midi_clear_buffer(void* port_buffer)
  65. {
  66. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  67. if (buf && buf->IsValid()) {
  68. buf->Reset(buf->nframes);
  69. }
  70. }
  71. LIB_EXPORT
  72. size_t jack_midi_max_event_size(void* port_buffer)
  73. {
  74. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  75. if (buf && buf->IsValid())
  76. return buf->MaxEventSize();
  77. return 0;
  78. }
  79. LIB_EXPORT
  80. jack_midi_data_t* jack_midi_event_reserve(void* port_buffer, jack_nframes_t time, size_t data_size)
  81. {
  82. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  83. if (! buf) {
  84. jack_error("jack_midi_event_reserve: port buffer is set to NULL");
  85. return 0;
  86. }
  87. if (! buf->IsValid()) {
  88. jack_error("jack_midi_event_reserve: port buffer is invalid");
  89. return 0;
  90. }
  91. if (time >= buf->nframes) {
  92. jack_error("jack_midi_event_reserve: time parameter is out of range "
  93. "(%lu >= %lu)", time, buf->nframes);
  94. return 0;
  95. }
  96. if (buf->event_count && (buf->events[buf->event_count - 1].time > time)) {
  97. jack_error("jack_midi_event_reserve: time parameter is earlier than "
  98. "last reserved event");
  99. return 0;
  100. }
  101. return buf->ReserveEvent(time, data_size);
  102. }
  103. LIB_EXPORT
  104. int jack_midi_event_write(void* port_buffer,
  105. jack_nframes_t time, const jack_midi_data_t* data, size_t data_size)
  106. {
  107. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  108. if (!buf && !buf->IsValid()) {
  109. return -EINVAL;
  110. }
  111. if (time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time)) {
  112. return -EINVAL;
  113. }
  114. jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
  115. if (!dest) {
  116. return -ENOBUFS;
  117. }
  118. memcpy(dest, data, data_size);
  119. return 0;
  120. }
  121. LIB_EXPORT
  122. uint32_t jack_midi_get_lost_event_count(void* port_buffer)
  123. {
  124. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  125. if (buf && buf->IsValid())
  126. return buf->lost_events;
  127. return 0;
  128. }