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.

146 lines
4.8KB

  1. /*
  2. * Carla JACK API for external applications
  3. * Copyright (C) 2016-2019 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. const JackMidiPortBufferBase* const jbasebuf((const JackMidiPortBufferBase*)buf);
  24. CARLA_SAFE_ASSERT_RETURN(jbasebuf != nullptr, 0);
  25. CARLA_SAFE_ASSERT_RETURN(jbasebuf->isInput, 0);
  26. CARLA_SAFE_ASSERT_RETURN(jbasebuf->isValid, 0);
  27. const JackMidiPortBufferOnStack* const jmidibuf((const JackMidiPortBufferOnStack*)jbasebuf);
  28. return jmidibuf->count;
  29. }
  30. CARLA_EXPORT
  31. int jack_midi_event_get(jack_midi_event_t* ev, void* buf, uint32_t index)
  32. {
  33. const JackMidiPortBufferBase* const jbasebuf((const JackMidiPortBufferBase*)buf);
  34. CARLA_SAFE_ASSERT_RETURN(jbasebuf != nullptr, EFAULT);
  35. CARLA_SAFE_ASSERT_RETURN(jbasebuf->isInput, EFAULT);
  36. CARLA_SAFE_ASSERT_RETURN(jbasebuf->isValid, EFAULT);
  37. const JackMidiPortBufferOnStack* const jmidibuf((const JackMidiPortBufferOnStack*)jbasebuf);
  38. if (index >= jmidibuf->count)
  39. return ENODATA;
  40. std::memcpy(ev, &jmidibuf->events[index], sizeof(jack_midi_event_t));
  41. return 0;
  42. }
  43. CARLA_EXPORT
  44. void jack_midi_clear_buffer(void* buf)
  45. {
  46. JackMidiPortBufferBase* const jbasebuf((JackMidiPortBufferBase*)buf);
  47. CARLA_SAFE_ASSERT_RETURN(jbasebuf != nullptr,);
  48. CARLA_SAFE_ASSERT_RETURN(! jbasebuf->isInput,);
  49. if (! jbasebuf->isValid)
  50. return;
  51. JackMidiPortBufferOnStack* const jmidibuf((JackMidiPortBufferOnStack*)jbasebuf);
  52. jmidibuf->count = 0;
  53. jmidibuf->bufferPoolPos = 0;
  54. std::memset(jmidibuf->bufferPool, 0, JackMidiPortBufferBase::kBufferPoolSize);
  55. }
  56. CARLA_EXPORT
  57. void jack_midi_reset_buffer(void* buf)
  58. {
  59. jack_midi_clear_buffer(buf);
  60. }
  61. CARLA_EXPORT
  62. size_t jack_midi_max_event_size(void*)
  63. {
  64. return JackMidiPortBufferBase::kMaxEventSize;
  65. }
  66. CARLA_EXPORT
  67. jack_midi_data_t* jack_midi_event_reserve(void* buf, jack_nframes_t frame, size_t size)
  68. {
  69. JackMidiPortBufferBase* const jbasebuf((JackMidiPortBufferBase*)buf);
  70. CARLA_SAFE_ASSERT_RETURN(jbasebuf != nullptr, nullptr);
  71. CARLA_SAFE_ASSERT_RETURN(! jbasebuf->isInput, nullptr);
  72. CARLA_SAFE_ASSERT_RETURN(jbasebuf->isValid, nullptr);
  73. CARLA_SAFE_ASSERT_RETURN(size < JackMidiPortBufferBase::kMaxEventSize, nullptr);
  74. // broken jack applications, wow...
  75. if (size == 0)
  76. return nullptr;
  77. JackMidiPortBufferOnStack* const jmidibuf((JackMidiPortBufferOnStack*)jbasebuf);
  78. if (jmidibuf->count >= JackMidiPortBufferBase::kMaxEventCount)
  79. return nullptr;
  80. if (jmidibuf->bufferPoolPos + size >= JackMidiPortBufferBase::kBufferPoolSize)
  81. return nullptr;
  82. jack_midi_data_t* const jmdata = jmidibuf->bufferPool + jmidibuf->bufferPoolPos;
  83. jmidibuf->bufferPoolPos += size;
  84. jmidibuf->events[jmidibuf->count++] = { frame, size, jmdata };
  85. std::memset(jmdata, 0, size);
  86. return jmdata;
  87. }
  88. CARLA_EXPORT
  89. int jack_midi_event_write(void* buf, jack_nframes_t frame, const jack_midi_data_t* data, size_t size)
  90. {
  91. JackMidiPortBufferBase* const jbasebuf((JackMidiPortBufferBase*)buf);
  92. CARLA_SAFE_ASSERT_RETURN(jbasebuf != nullptr, EFAULT);
  93. CARLA_SAFE_ASSERT_RETURN(! jbasebuf->isInput, EINVAL);
  94. CARLA_SAFE_ASSERT_RETURN(jbasebuf->isValid, EINVAL);
  95. CARLA_SAFE_ASSERT_RETURN(size < JackMidiPortBufferBase::kMaxEventSize, ENOBUFS);
  96. // broken jack applications, wow...
  97. if (size == 0)
  98. return EINVAL;
  99. JackMidiPortBufferOnStack* const jmidibuf((JackMidiPortBufferOnStack*)jbasebuf);
  100. if (jmidibuf->count >= JackMidiPortBufferBase::kMaxEventCount)
  101. return ENOBUFS;
  102. if (jmidibuf->bufferPoolPos + size >= JackMidiPortBufferBase::kBufferPoolSize)
  103. return ENOBUFS;
  104. jack_midi_data_t* const jmdata = jmidibuf->bufferPool + jmidibuf->bufferPoolPos;
  105. jmidibuf->bufferPoolPos += size;
  106. jmidibuf->events[jmidibuf->count++] = { frame, size, jmdata };
  107. std::memcpy(jmdata, data, size);
  108. return 0;
  109. }
  110. CARLA_EXPORT
  111. uint32_t jack_midi_get_lost_event_count(void*)
  112. {
  113. return 0;
  114. }
  115. // --------------------------------------------------------------------------------------------------------------------