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.

134 lines
3.9KB

  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 "JackExports.h"
  19. #include <errno.h>
  20. #include <string.h>
  21. #ifdef WIN32
  22. #define ENOBUFS 55
  23. #endif
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. EXPORT jack_nframes_t jack_midi_get_event_count(void* port_buffer);
  29. EXPORT int jack_midi_event_get(jack_midi_event_t* event,
  30. void* port_buffer, jack_nframes_t event_index);
  31. EXPORT void jack_midi_clear_buffer(void* port_buffer);
  32. EXPORT size_t jack_midi_max_event_size(void* port_buffer);
  33. EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
  34. jack_nframes_t time, size_t data_size);
  35. EXPORT int jack_midi_event_write(void* port_buffer,
  36. jack_nframes_t time, const jack_midi_data_t* data, size_t data_size);
  37. EXPORT jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer);
  38. #ifdef __cplusplus
  39. }
  40. #endif
  41. using namespace Jack;
  42. EXPORT
  43. jack_nframes_t jack_midi_get_event_count(void* port_buffer)
  44. {
  45. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  46. if (!buf || !buf->IsValid())
  47. return 0;
  48. return buf->event_count;
  49. }
  50. EXPORT
  51. int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, jack_nframes_t event_index)
  52. {
  53. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  54. if (!buf || !buf->IsValid())
  55. return -EINVAL;
  56. if (event_index < 0 || event_index >= buf->event_count)
  57. return -ENOBUFS;
  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. 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. 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. 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 && !buf->IsValid())
  84. return 0;
  85. if (time < 0 || time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time))
  86. return 0;
  87. return buf->ReserveEvent(time, data_size);
  88. }
  89. EXPORT
  90. int jack_midi_event_write(void* port_buffer,
  91. jack_nframes_t time, const jack_midi_data_t* data, size_t data_size)
  92. {
  93. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  94. if (!buf && !buf->IsValid())
  95. return -EINVAL;
  96. if (time < 0 || time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time))
  97. return -EINVAL;
  98. jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
  99. if (!dest)
  100. return -ENOBUFS;
  101. memcpy(dest, data, data_size);
  102. return 0;
  103. }
  104. EXPORT
  105. jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer)
  106. {
  107. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  108. if (buf && buf->IsValid())
  109. return buf->lost_events;
  110. return 0;
  111. }