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.

160 lines
4.7KB

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