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.

225 lines
7.2KB

  1. /*
  2. * Carla CLAP utils
  3. * Copyright (C) 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. #ifndef CARLA_CLAP_UTILS_HPP_INCLUDED
  18. #define CARLA_CLAP_UTILS_HPP_INCLUDED
  19. #include "CarlaBackend.h"
  20. #include "CarlaUtils.hpp"
  21. #include "clap/entry.h"
  22. #include "clap/plugin-factory.h"
  23. #include "clap/plugin-features.h"
  24. #include "clap/ext/audio-ports.h"
  25. #include "clap/ext/latency.h"
  26. #include "clap/ext/gui.h"
  27. #include "clap/ext/note-ports.h"
  28. #include "clap/ext/params.h"
  29. #include "clap/ext/posix-fd-support.h"
  30. #include "clap/ext/state.h"
  31. #include "clap/ext/timer-support.h"
  32. #if defined(CARLA_OS_WIN)
  33. # define CLAP_WINDOW_API_NATIVE CLAP_WINDOW_API_WIN32
  34. #elif defined(CARLA_OS_MAC)
  35. # define CLAP_WINDOW_API_NATIVE CLAP_WINDOW_API_COCOA
  36. #elif defined(HAVE_X11)
  37. # define CLAP_WINDOW_API_NATIVE CLAP_WINDOW_API_X11
  38. #endif
  39. // --------------------------------------------------------------------------------------------------------------------
  40. extern "C" {
  41. typedef struct clap_audio_buffer_const {
  42. const float* const* data32;
  43. const double* const* data64;
  44. uint32_t channel_count;
  45. uint32_t latency;
  46. uint64_t constant_mask;
  47. } clap_audio_buffer_const_t;
  48. typedef struct clap_audio_buffer_extra_data {
  49. uint16_t offset;
  50. bool isMain;
  51. } clap_audio_buffer_extra_data_t;
  52. }
  53. // --------------------------------------------------------------------------------------------------------------------
  54. CARLA_BACKEND_START_NAMESPACE
  55. // --------------------------------------------------------------------------------------------------------------------
  56. static inline
  57. PluginCategory getPluginCategoryFromClapFeatures(const char* const* const features) noexcept
  58. {
  59. // 1st pass for main categories
  60. for (uint32_t i=0; features[i] != nullptr; ++i)
  61. {
  62. if (std::strcmp(features[i], CLAP_PLUGIN_FEATURE_INSTRUMENT) == 0)
  63. return PLUGIN_CATEGORY_SYNTH;
  64. if (std::strcmp(features[i], CLAP_PLUGIN_FEATURE_NOTE_EFFECT) == 0)
  65. return PLUGIN_CATEGORY_UTILITY;
  66. if (std::strcmp(features[i], CLAP_PLUGIN_FEATURE_ANALYZER) == 0)
  67. return PLUGIN_CATEGORY_UTILITY;
  68. }
  69. // 2nd pass for FX sub categories
  70. /*
  71. #define CLAP_PLUGIN_FEATURE_DEESSER "de-esser"
  72. #define CLAP_PLUGIN_FEATURE_PHASE_VOCODER "phase-vocoder"
  73. #define CLAP_PLUGIN_FEATURE_GRANULAR "granular"
  74. #define CLAP_PLUGIN_FEATURE_FREQUENCY_SHIFTER "frequency-shifter"
  75. #define CLAP_PLUGIN_FEATURE_PITCH_SHIFTER "pitch-shifter"
  76. #define CLAP_PLUGIN_FEATURE_TREMOLO "tremolo"
  77. #define CLAP_PLUGIN_FEATURE_GLITCH "glitch"
  78. */
  79. for (uint32_t i=0; features[i] != nullptr; ++i)
  80. {
  81. if (std::strcmp(features[i], CLAP_PLUGIN_FEATURE_DELAY) == 0 ||
  82. std::strcmp(features[i], CLAP_PLUGIN_FEATURE_REVERB) == 0)
  83. {
  84. return PLUGIN_CATEGORY_DELAY;
  85. }
  86. if (std::strcmp(features[i], CLAP_PLUGIN_FEATURE_EQUALIZER) == 0)
  87. {
  88. return PLUGIN_CATEGORY_EQ;
  89. }
  90. if (std::strcmp(features[i], CLAP_PLUGIN_FEATURE_FILTER) == 0)
  91. {
  92. return PLUGIN_CATEGORY_FILTER;
  93. }
  94. if (std::strcmp(features[i], CLAP_PLUGIN_FEATURE_DISTORTION) == 0)
  95. {
  96. return PLUGIN_CATEGORY_DISTORTION;
  97. }
  98. if (std::strcmp(features[i], CLAP_PLUGIN_FEATURE_COMPRESSOR) == 0 ||
  99. std::strcmp(features[i], CLAP_PLUGIN_FEATURE_LIMITER) == 0 ||
  100. std::strcmp(features[i], CLAP_PLUGIN_FEATURE_MASTERING) == 0 ||
  101. std::strcmp(features[i], CLAP_PLUGIN_FEATURE_MIXING) == 0 ||
  102. std::strcmp(features[i], CLAP_PLUGIN_FEATURE_TRANSIENT_SHAPER) == 0)
  103. {
  104. return PLUGIN_CATEGORY_DYNAMICS;
  105. }
  106. if (std::strcmp(features[i], CLAP_PLUGIN_FEATURE_CHORUS) == 0 ||
  107. std::strcmp(features[i], CLAP_PLUGIN_FEATURE_FLANGER) == 0 ||
  108. std::strcmp(features[i], CLAP_PLUGIN_FEATURE_PHASER) == 0
  109. )
  110. {
  111. return PLUGIN_CATEGORY_MODULATOR;
  112. }
  113. if (std::strcmp(features[i], CLAP_PLUGIN_FEATURE_PITCH_CORRECTION) == 0 ||
  114. std::strcmp(features[i], CLAP_PLUGIN_FEATURE_RESTORATION) == 0 ||
  115. std::strcmp(features[i], CLAP_PLUGIN_FEATURE_UTILITY) == 0
  116. )
  117. {
  118. return PLUGIN_CATEGORY_UTILITY;
  119. }
  120. }
  121. return PLUGIN_CATEGORY_OTHER;
  122. }
  123. static inline
  124. bool clapFeaturesContainInstrument(const char* const* const features) noexcept
  125. {
  126. if (features == nullptr)
  127. return false;
  128. for (uint32_t i=0; features[i] != nullptr; ++i)
  129. {
  130. if (std::strcmp(features[i], CLAP_PLUGIN_FEATURE_INSTRUMENT) == 0)
  131. return true;
  132. }
  133. return false;
  134. }
  135. // --------------------------------------------------------------------------------------------------------------------
  136. struct clap_istream_impl : clap_istream_t {
  137. const void* buffer;
  138. const uint64_t size;
  139. uint64_t readPos;
  140. clap_istream_impl(const void* const buf, const uint64_t bufsize) noexcept
  141. : buffer(buf),
  142. size(bufsize),
  143. readPos(0)
  144. {
  145. ctx = this;
  146. read = read_impl;
  147. }
  148. static CLAP_ABI int64_t read_impl(const clap_istream_t* const stream, void* const buffer, const uint64_t size) noexcept
  149. {
  150. clap_istream_impl* const self = static_cast<clap_istream_impl*>(stream->ctx);
  151. if (const uint64_t bytesRead = std::min(self->size - self->readPos, size))
  152. {
  153. std::memcpy(buffer, static_cast<const uint8_t*>(self->buffer) + self->readPos, bytesRead);
  154. self->readPos += bytesRead;
  155. return bytesRead;
  156. }
  157. return 0;
  158. }
  159. };
  160. // --------------------------------------------------------------------------------------------------------------------
  161. struct clap_ostream_impl : clap_ostream_t {
  162. void* buffer;
  163. uint64_t size;
  164. clap_ostream_impl() noexcept
  165. : buffer(nullptr),
  166. size(0)
  167. {
  168. ctx = this;
  169. write = write_impl;
  170. }
  171. static CLAP_ABI int64_t write_impl(const clap_ostream* const stream, const void* const buffer, const uint64_t size) noexcept
  172. {
  173. CARLA_SAFE_ASSERT_RETURN(size != 0, 0);
  174. clap_ostream_impl* const self = static_cast<clap_ostream_impl*>(stream->ctx);
  175. void* const oldBuffer = self->buffer;
  176. self->buffer = std::realloc(self->buffer, self->size + size);
  177. if (self->buffer == nullptr)
  178. {
  179. std::free(oldBuffer);
  180. return -1;
  181. }
  182. std::memcpy(static_cast<uint8_t*>(self->buffer) + self->size, buffer, size);
  183. self->size += size;
  184. return size;
  185. }
  186. };
  187. // --------------------------------------------------------------------------------------------------------------------
  188. CARLA_BACKEND_END_NAMESPACE
  189. #endif // CARLA_CLAP_UTILS_HPP_INCLUDED