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.

139 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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include "JackError.h"
  20. #include "JackMidiPort.h"
  21. #include "JackExports.h"
  22. #include <errno.h>
  23. #include <string.h>
  24. #ifdef WIN32
  25. #define ENOBUFS 55
  26. #endif
  27. #ifdef __cplusplus
  28. extern "C"
  29. {
  30. #endif
  31. EXPORT jack_nframes_t jack_midi_get_event_count(void* port_buffer);
  32. EXPORT int jack_midi_event_get(jack_midi_event_t* event,
  33. void* port_buffer, jack_nframes_t event_index);
  34. EXPORT void jack_midi_clear_buffer(void* port_buffer);
  35. EXPORT size_t jack_midi_max_event_size(void* port_buffer);
  36. EXPORT jack_midi_data_t* jack_midi_event_reserve(void* port_buffer,
  37. jack_nframes_t time, size_t data_size);
  38. EXPORT int jack_midi_event_write(void* port_buffer,
  39. jack_nframes_t time, const jack_midi_data_t* data, size_t data_size);
  40. EXPORT jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer);
  41. #ifdef __cplusplus
  42. }
  43. #endif
  44. using namespace Jack;
  45. EXPORT
  46. jack_nframes_t jack_midi_get_event_count(void* port_buffer)
  47. {
  48. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  49. if (!buf || !buf->IsValid())
  50. return 0;
  51. return buf->event_count;
  52. }
  53. EXPORT
  54. int jack_midi_event_get(jack_midi_event_t *event, void* port_buffer, jack_nframes_t event_index)
  55. {
  56. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  57. if (!buf || !buf->IsValid())
  58. return -EINVAL;
  59. if (event_index < 0 || event_index >= buf->event_count)
  60. return -ENOBUFS;
  61. JackMidiEvent* ev = &buf->events[event_index];
  62. event->time = ev->time;
  63. event->size = ev->size;
  64. event->buffer = ev->GetData(buf);
  65. return 0;
  66. }
  67. EXPORT
  68. void jack_midi_clear_buffer(void* port_buffer)
  69. {
  70. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  71. if (buf && buf->IsValid())
  72. buf->Reset(buf->nframes);
  73. }
  74. EXPORT
  75. size_t jack_midi_max_event_size(void* port_buffer)
  76. {
  77. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  78. if (buf && buf->IsValid())
  79. return buf->MaxEventSize();
  80. return 0;
  81. }
  82. EXPORT
  83. jack_midi_data_t* jack_midi_event_reserve(void* port_buffer, jack_nframes_t time, size_t data_size)
  84. {
  85. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  86. if (!buf && !buf->IsValid())
  87. return 0;
  88. if (time < 0 || time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time))
  89. return 0;
  90. return buf->ReserveEvent(time, data_size);
  91. }
  92. EXPORT
  93. int jack_midi_event_write(void* port_buffer,
  94. jack_nframes_t time, const jack_midi_data_t* data, size_t data_size)
  95. {
  96. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  97. if (!buf && !buf->IsValid())
  98. return -EINVAL;
  99. if (time < 0 || time >= buf->nframes || (buf->event_count && buf->events[buf->event_count - 1].time > time))
  100. return -EINVAL;
  101. jack_midi_data_t* dest = buf->ReserveEvent(time, data_size);
  102. if (!dest)
  103. return -ENOBUFS;
  104. memcpy(dest, data, data_size);
  105. return 0;
  106. }
  107. EXPORT
  108. jack_nframes_t jack_midi_get_lost_event_count(void* port_buffer)
  109. {
  110. JackMidiBuffer *buf = (JackMidiBuffer*)port_buffer;
  111. if (buf && buf->IsValid())
  112. return buf->lost_events;
  113. return 0;
  114. }