Audio plugin host https://kx.studio/carla
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.

libjack_midi.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Carla JACK API for external applications
  3. * Copyright (C) 2016-2017 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "libjack.hpp"
  18. CARLA_BACKEND_USE_NAMESPACE
  19. // --------------------------------------------------------------------------------------------------------------------
  20. CARLA_EXPORT
  21. jack_nframes_t jack_midi_get_event_count(void* buf)
  22. {
  23. JackMidiPortBuffer* const jmidibuf((JackMidiPortBuffer*)buf);
  24. CARLA_SAFE_ASSERT_RETURN(jmidibuf != nullptr, 0);
  25. CARLA_SAFE_ASSERT_RETURN(jmidibuf->isInput, 0);
  26. return jmidibuf->count;
  27. }
  28. CARLA_EXPORT
  29. int jack_midi_event_get(jack_midi_event_t* ev, void* buf, uint32_t index)
  30. {
  31. JackMidiPortBuffer* const jmidibuf((JackMidiPortBuffer*)buf);
  32. CARLA_SAFE_ASSERT_RETURN(jmidibuf != nullptr, EFAULT);
  33. CARLA_SAFE_ASSERT_RETURN(jmidibuf->isInput, EFAULT);
  34. if (index >= jmidibuf->count)
  35. return ENODATA;
  36. std::memcpy(ev, &jmidibuf->events[index], sizeof(jack_midi_event_t));
  37. return 0;
  38. }
  39. CARLA_EXPORT
  40. void jack_midi_clear_buffer(void* buf)
  41. {
  42. JackMidiPortBuffer* const jmidibuf((JackMidiPortBuffer*)buf);
  43. CARLA_SAFE_ASSERT_RETURN(jmidibuf != nullptr,);
  44. CARLA_SAFE_ASSERT_RETURN(! jmidibuf->isInput,);
  45. jmidibuf->count = 0;
  46. if (jmidibuf->bufferPool != nullptr)
  47. {
  48. jmidibuf->bufferPoolPos = 0;
  49. std::memset(jmidibuf->bufferPool, 0, JackMidiPortBuffer::kBufferPoolSize);
  50. }
  51. }
  52. CARLA_EXPORT
  53. void jack_midi_reset_buffer(void* buf)
  54. {
  55. jack_midi_clear_buffer(buf);
  56. }
  57. CARLA_EXPORT
  58. size_t jack_midi_max_event_size(void*)
  59. {
  60. return JackMidiPortBuffer::kMaxEventSize;
  61. }
  62. CARLA_EXPORT
  63. jack_midi_data_t* jack_midi_event_reserve(void* buf, jack_nframes_t frame, size_t size)
  64. {
  65. JackMidiPortBuffer* const jmidibuf((JackMidiPortBuffer*)buf);
  66. CARLA_SAFE_ASSERT_RETURN(jmidibuf != nullptr, nullptr);
  67. CARLA_SAFE_ASSERT_RETURN(! jmidibuf->isInput, nullptr);
  68. CARLA_SAFE_ASSERT_RETURN(size < JackMidiPortBuffer::kMaxEventSize, nullptr);
  69. // back jack applicatons, wow...
  70. if (size == 0)
  71. return nullptr;
  72. if (jmidibuf->count >= JackMidiPortBuffer::kMaxEventCount)
  73. return nullptr;
  74. if (jmidibuf->bufferPoolPos + size >= JackMidiPortBuffer::kBufferPoolSize)
  75. return nullptr;
  76. jack_midi_data_t* const jmdata = jmidibuf->bufferPool + jmidibuf->bufferPoolPos;
  77. jmidibuf->bufferPoolPos += size;
  78. jmidibuf->events[jmidibuf->count++] = { frame, size, jmdata };
  79. std::memset(jmdata, 0, size);
  80. return jmdata;
  81. }
  82. CARLA_EXPORT
  83. int jack_midi_event_write(void* buf, jack_nframes_t frame, const jack_midi_data_t* data, size_t size)
  84. {
  85. JackMidiPortBuffer* const jmidibuf((JackMidiPortBuffer*)buf);
  86. CARLA_SAFE_ASSERT_RETURN(jmidibuf != nullptr, EFAULT);
  87. CARLA_SAFE_ASSERT_RETURN(! jmidibuf->isInput, EINVAL);
  88. CARLA_SAFE_ASSERT_RETURN(size < JackMidiPortBuffer::kMaxEventSize, ENOBUFS);
  89. // back jack applicatons, wow...
  90. if (size == 0)
  91. return EINVAL;
  92. if (jmidibuf->count >= JackMidiPortBuffer::kMaxEventCount)
  93. return ENOBUFS;
  94. if (jmidibuf->bufferPoolPos + size >= JackMidiPortBuffer::kBufferPoolSize)
  95. return ENOBUFS;
  96. jack_midi_data_t* const jmdata = jmidibuf->bufferPool + jmidibuf->bufferPoolPos;
  97. jmidibuf->bufferPoolPos += size;
  98. jmidibuf->events[jmidibuf->count++] = { frame, size, jmdata };
  99. std::memcpy(jmdata, data, size);
  100. return 0;
  101. }
  102. CARLA_EXPORT
  103. uint32_t jack_midi_get_lost_event_count(void*)
  104. {
  105. return 0;
  106. }
  107. // --------------------------------------------------------------------------------------------------------------------