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.

133 lines
3.8KB

  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 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. 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 General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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. #endif
  27. EXPORT jack_nframes_t jack_midi_get_event_count(void* port_buffer);
  28. EXPORT int jack_midi_event_get(jack_midi_event_t* event,
  29. void* port_buffer, jack_nframes_t event_index);
  30. EXPORT void jack_midi_clear_buffer(void* port_buffer);
  31. EXPORT size_t jack_midi_max_event_size(void* port_buffer);
  32. EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
  33. jack_nframes_t time, size_t data_size);
  34. EXPORT int jack_midi_event_write(void* port_buffer,
  35. jack_nframes_t time, const jack_midi_data_t* data, size_t data_size);
  36. EXPORT jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer);
  37. #ifdef __cplusplus
  38. }
  39. #endif
  40. using namespace Jack;
  41. EXPORT
  42. jack_nframes_t jack_midi_get_event_count(void* port_buffer)
  43. {
  44. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  45. if (!buf || !buf->IsValid())
  46. return 0;
  47. return buf->event_count;
  48. }
  49. EXPORT
  50. int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, jack_nframes_t event_index)
  51. {
  52. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  53. if (!buf || !buf->IsValid())
  54. return -EINVAL;
  55. if (event_index < 0 || event_index >= buf->event_count)
  56. return -ENOBUFS;
  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. 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. EXPORT
  71. size_t jack_midi_max_event_size(void* port_buffer)
  72. {
  73. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  74. if (buf && buf->IsValid())
  75. return buf->MaxEventSize();
  76. return 0;
  77. }
  78. EXPORT
  79. jack_midi_data_t* jack_midi_event_reserve(void* port_buffer, jack_nframes_t time, size_t data_size)
  80. {
  81. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  82. if (!buf && !buf->IsValid())
  83. return 0;
  84. if (time < 0 || time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time))
  85. return 0;
  86. return buf->ReserveEvent(time, data_size);
  87. }
  88. EXPORT
  89. int jack_midi_event_write(void* port_buffer,
  90. jack_nframes_t time, const jack_midi_data_t* data, size_t data_size)
  91. {
  92. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  93. if (!buf && !buf->IsValid())
  94. return -EINVAL;
  95. if (time < 0 || time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time))
  96. return -EINVAL;
  97. jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
  98. if (!dest)
  99. return -ENOBUFS;
  100. memcpy(dest, data, data_size);
  101. return 0;
  102. }
  103. EXPORT
  104. jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer)
  105. {
  106. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  107. if (buf && buf->IsValid())
  108. return buf->lost_events;
  109. return 0;
  110. }