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.

231 lines
7.3KB

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