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.

152 lines
4.8KB

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