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.

232 lines
7.4KB

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