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.

222 lines
6.1KB

  1. /*
  2. * DISTRHO Cardinal Plugin
  3. * Copyright (C) 2021 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 3 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 LICENSE file.
  16. */
  17. #include "src/lv2/buf-size.h"
  18. #include "src/lv2/options.h"
  19. #include <rack.hpp>
  20. #include <context.hpp>
  21. #include "DistrhoUtils.hpp"
  22. using namespace rack;
  23. extern Model* modelSpringReverb;
  24. Plugin* pluginInstance__Befaco;
  25. namespace rack {
  26. namespace settings {
  27. bool cpuMeter = false;
  28. }
  29. Context::~Context() {
  30. }
  31. static thread_local Context* threadContext;
  32. Context* contextGet() {
  33. DISTRHO_SAFE_ASSERT(threadContext != nullptr);
  34. return threadContext;
  35. }
  36. // Apple's clang incorrectly compiles this function when -O2 or higher is enabled.
  37. #ifdef ARCH_MAC
  38. __attribute__((optnone))
  39. #endif
  40. void contextSet(Context* const context) {
  41. // DISTRHO_SAFE_ASSERT(threadContext == nullptr);
  42. threadContext = context;
  43. }
  44. Exception::Exception(const char* format, ...)
  45. {
  46. va_list args;
  47. va_start(args, format);
  48. msg = string::fV(format, args);
  49. va_end(args);
  50. }
  51. namespace asset {
  52. std::string plugin(plugin::Plugin* plugin, std::string filename) { return {}; }
  53. std::string system(std::string filename) { return {}; }
  54. }
  55. namespace engine {
  56. float Engine::getParamValue(Module* module, int paramId) { return 0.0f; }
  57. float Engine::getParamSmoothValue(Module* module, int paramId) { return 0.0f; }
  58. void Engine::setParamValue(Module* module, int paramId, float value) {}
  59. void Engine::setParamSmoothValue(Module* module, int paramId, float value) {}
  60. }
  61. namespace plugin {
  62. void Plugin::addModel(Model* model)
  63. {
  64. // Check that the model is not added to a plugin already
  65. DISTRHO_SAFE_ASSERT_RETURN(model != nullptr,);
  66. DISTRHO_SAFE_ASSERT_RETURN(model->plugin == nullptr,);
  67. model->plugin = this;
  68. models.push_back(model);
  69. }
  70. Model* modelFromJson(json_t* moduleJ) {
  71. return nullptr;
  72. }
  73. std::vector<Plugin*> plugins;
  74. } // namespace plugin
  75. } // namespace rack
  76. struct PluginLv2 {
  77. Plugin* plugin;
  78. engine::Module* module;
  79. float sampleRate;
  80. int frameCount = 0;
  81. void* ports[11];
  82. PluginLv2(double sr)
  83. {
  84. sampleRate = sr;
  85. plugin = new Plugin;
  86. pluginInstance__Befaco = plugin;
  87. plugin->addModel(modelSpringReverb);
  88. module = modelSpringReverb->createModule();
  89. // FIXME we need to detect if something is connected
  90. // module->inputs[0].channels = 1;
  91. // module->inputs[1].channels = 1;
  92. module->inputs[2].channels = 1;
  93. module->inputs[3].channels = 1;
  94. module->inputs[4].channels = 1;
  95. module->outputs[0].channels = 1;
  96. module->outputs[1].channels = 1;
  97. }
  98. void lv2_connect_port(const uint32_t port, void* const dataLocation)
  99. {
  100. ports[port] = dataLocation;
  101. }
  102. void lv2_run(const uint32_t sampleCount)
  103. {
  104. if (sampleCount == 0)
  105. return;
  106. Module::ProcessArgs args = {
  107. sampleRate,
  108. 1.0f / sampleRate,
  109. frameCount
  110. };
  111. // const float* CV1_INPUT = (float*)ports[0];
  112. // const float* CV2_INPUT = (float*)ports[1];
  113. const float* IN1_INPUT = (float*)ports[2];
  114. const float* IN2_INPUT = (float*)ports[3];
  115. const float* MIX_CV_INPUT = (float*)ports[4];
  116. float* MIX_OUTPUT = (float*)ports[5];
  117. float* WET_OUTPUT = (float*)ports[6];
  118. const float drywet = *(float*)ports[7] * 0.01f;
  119. const float lvl1 = *(float*)ports[8] * 0.01f;
  120. const float lvl2 = *(float*)ports[9] * 0.01f;
  121. const float hpf = *(float*)ports[10];
  122. module->params[0].setValue(drywet);
  123. module->params[1].setValue(lvl1);
  124. module->params[2].setValue(lvl2);
  125. module->params[3].setValue(hpf);
  126. for (uint32_t i=0; i<sampleCount; ++i)
  127. {
  128. // module->inputs[0].setVoltage(CV1_INPUT[i]);
  129. // module->inputs[1].setVoltage(CV2_INPUT[i]);
  130. module->inputs[2].setVoltage(IN1_INPUT[i] * 10);
  131. module->inputs[3].setVoltage(IN2_INPUT[i] * 10);
  132. module->inputs[4].setVoltage(MIX_CV_INPUT[i]);
  133. module->doProcess(args);
  134. MIX_OUTPUT[i] = module->outputs[0].getVoltage() * 0.1f;
  135. WET_OUTPUT[i] = module->outputs[1].getVoltage() * 0.1f;
  136. ++args.frame;
  137. }
  138. frameCount += sampleCount;
  139. }
  140. };
  141. static LV2_Handle lv2_instantiate(const LV2_Descriptor*, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  142. {
  143. return new PluginLv2(sampleRate);
  144. }
  145. // -----------------------------------------------------------------------
  146. #define instancePtr ((PluginLv2*)instance)
  147. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  148. {
  149. instancePtr->lv2_connect_port(port, dataLocation);
  150. }
  151. static void lv2_activate(LV2_Handle instance)
  152. {
  153. }
  154. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  155. {
  156. instancePtr->lv2_run(sampleCount);
  157. }
  158. static void lv2_deactivate(LV2_Handle instance)
  159. {
  160. }
  161. static void lv2_cleanup(LV2_Handle instance)
  162. {
  163. delete instancePtr;
  164. }
  165. // -----------------------------------------------------------------------
  166. static const void* lv2_extension_data(const char* uri)
  167. {
  168. return nullptr;
  169. }
  170. #undef instancePtr
  171. // -----------------------------------------------------------------------
  172. static const LV2_Descriptor sLv2Descriptor = {
  173. "urn:Cardinal:Befaco",
  174. lv2_instantiate,
  175. lv2_connect_port,
  176. lv2_activate,
  177. lv2_run,
  178. lv2_deactivate,
  179. lv2_cleanup,
  180. lv2_extension_data
  181. };
  182. DISTRHO_PLUGIN_EXPORT
  183. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  184. {
  185. USE_NAMESPACE_DISTRHO
  186. return (index == 0) ? &sLv2Descriptor : nullptr;
  187. }
  188. // -----------------------------------------------------------------------