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.

1225 lines
42KB

  1. /*
  2. * DISTRHO Cardinal Plugin
  3. * Copyright (C) 2021-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 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 <asset.hpp>
  18. #include <library.hpp>
  19. #include <midi.hpp>
  20. #include <patch.hpp>
  21. #include <plugin.hpp>
  22. #include <random.hpp>
  23. #include <settings.hpp>
  24. #include <system.hpp>
  25. #include <app/Browser.hpp>
  26. #include <app/Scene.hpp>
  27. #include <engine/Engine.hpp>
  28. #include <ui/common.hpp>
  29. #include <window/Window.hpp>
  30. #ifdef NDEBUG
  31. # undef DEBUG
  32. #endif
  33. #ifdef HAVE_LIBLO
  34. # ifdef HEADLESS
  35. # include <lo/lo.h>
  36. # include "extra/Thread.hpp"
  37. # endif
  38. # include "CardinalCommon.hpp"
  39. #endif
  40. #include <list>
  41. #include "DistrhoPluginUtils.hpp"
  42. #include "PluginContext.hpp"
  43. #include "extra/Base64.hpp"
  44. #include "extra/SharedResourcePointer.hpp"
  45. static const constexpr uint kCardinalStateBaseCount = 3; // patch, screenshot, comment
  46. #ifndef HEADLESS
  47. # include "WindowParameters.hpp"
  48. static const constexpr uint kCardinalStateCount = kCardinalStateBaseCount + 2; // moduleInfos, windowSize
  49. #else
  50. # define kWindowParameterCount 0
  51. static const constexpr uint kCardinalStateCount = kCardinalStateBaseCount;
  52. #endif
  53. #if CARDINAL_VARIANT_FX
  54. # define CARDINAL_TEMPLATE_NAME "template-fx.vcv"
  55. #elif CARDINAL_VARIANT_SYNTH
  56. # define CARDINAL_TEMPLATE_NAME "template-synth.vcv"
  57. #else
  58. # define CARDINAL_TEMPLATE_NAME "template.vcv"
  59. #endif
  60. namespace rack {
  61. namespace plugin {
  62. void initStaticPlugins();
  63. void destroyStaticPlugins();
  64. }
  65. #ifndef HEADLESS
  66. namespace window {
  67. void WindowInit(Window* window, DISTRHO_NAMESPACE::Plugin* plugin);
  68. }
  69. #endif
  70. }
  71. START_NAMESPACE_DISTRHO
  72. // -----------------------------------------------------------------------------------------------------------
  73. struct Initializer
  74. #if defined(HAVE_LIBLO) && defined(HEADLESS)
  75. : public Thread
  76. #endif
  77. {
  78. #if defined(HAVE_LIBLO) && defined(HEADLESS)
  79. lo_server oscServer = nullptr;
  80. CardinalBasePlugin* oscPlugin = nullptr;
  81. #endif
  82. std::string templatePath;
  83. Initializer(const CardinalBasePlugin* const plugin)
  84. {
  85. using namespace rack;
  86. settings::allowCursorLock = false;
  87. settings::autoCheckUpdates = false;
  88. settings::autosaveInterval = 0;
  89. settings::devMode = true;
  90. settings::discordUpdateActivity = false;
  91. settings::isPlugin = true;
  92. settings::skipLoadOnLaunch = true;
  93. settings::showTipsOnLaunch = false;
  94. settings::windowPos = math::Vec(0, 0);
  95. #ifdef HEADLESS
  96. settings::headless = true;
  97. #endif
  98. // copied from https://community.vcvrack.com/t/16-colour-cable-palette/15951
  99. settings::cableColors = {
  100. color::fromHexString("#ff5252"),
  101. color::fromHexString("#ff9352"),
  102. color::fromHexString("#ffd452"),
  103. color::fromHexString("#e8ff52"),
  104. color::fromHexString("#a8ff52"),
  105. color::fromHexString("#67ff52"),
  106. color::fromHexString("#52ff7d"),
  107. color::fromHexString("#52ffbe"),
  108. color::fromHexString("#52ffff"),
  109. color::fromHexString("#52beff"),
  110. color::fromHexString("#527dff"),
  111. color::fromHexString("#6752ff"),
  112. color::fromHexString("#a852ff"),
  113. color::fromHexString("#e952ff"),
  114. color::fromHexString("#ff52d4"),
  115. color::fromHexString("#ff5293"),
  116. };
  117. system::init();
  118. logger::init();
  119. random::init();
  120. ui::init();
  121. if (asset::systemDir.empty())
  122. {
  123. if (const char* const bundlePath = plugin->getBundlePath())
  124. {
  125. if (const char* const resourcePath = getResourcePath(bundlePath))
  126. {
  127. asset::bundlePath = system::join(resourcePath, "PluginManifests");
  128. asset::systemDir = resourcePath;
  129. templatePath = system::join(asset::systemDir, CARDINAL_TEMPLATE_NAME);
  130. }
  131. }
  132. if (asset::systemDir.empty() || ! system::exists(asset::systemDir))
  133. {
  134. #ifdef CARDINAL_PLUGIN_SOURCE_DIR
  135. // Make system dir point to source code location as fallback
  136. asset::systemDir = CARDINAL_PLUGIN_SOURCE_DIR DISTRHO_OS_SEP_STR "Rack";
  137. if (system::exists(system::join(asset::systemDir, "res")))
  138. {
  139. templatePath = CARDINAL_PLUGIN_SOURCE_DIR DISTRHO_OS_SEP_STR CARDINAL_TEMPLATE_NAME;
  140. }
  141. // If source code dir does not exist use install target prefix as system dir
  142. else
  143. #endif
  144. {
  145. #if defined(ARCH_MAC)
  146. asset::systemDir = "/Library/Application Support/Cardinal";
  147. #elif defined(ARCH_WIN)
  148. const std::string commonprogfiles = getSpecialPath(kSpecialPathCommonProgramFiles);
  149. if (! commonprogfiles.empty())
  150. asset::systemDir = system::join(commonprogfiles, "Cardinal");
  151. #else
  152. asset::systemDir = CARDINAL_PLUGIN_PREFIX "/share/cardinal";
  153. #endif
  154. if (! asset::systemDir.empty())
  155. {
  156. asset::bundlePath = system::join(asset::systemDir, "PluginManifests");
  157. templatePath = system::join(asset::systemDir, CARDINAL_TEMPLATE_NAME);
  158. }
  159. }
  160. }
  161. asset::userDir = asset::systemDir;
  162. }
  163. // Log environment
  164. INFO("%s %s v%s", APP_NAME.c_str(), APP_EDITION.c_str(), APP_VERSION.c_str());
  165. INFO("%s", system::getOperatingSystemInfo().c_str());
  166. INFO("Binary filename: %s", getBinaryFilename());
  167. INFO("Bundle path: %s", plugin->getBundlePath());
  168. INFO("System directory: %s", asset::systemDir.c_str());
  169. INFO("User directory: %s", asset::userDir.c_str());
  170. INFO("Template patch: %s", templatePath.c_str());
  171. // Report to user if something is wrong with the installation
  172. if (asset::systemDir.empty())
  173. {
  174. d_stderr2("Failed to locate Cardinal plugin bundle.\n"
  175. "Install Cardinal with its bundle folder intact and try again.");
  176. }
  177. else if (! system::exists(asset::systemDir))
  178. {
  179. d_stderr2("System directory \"%s\" does not exist.\n"
  180. "Make sure Cardinal was downloaded and installed correctly.", asset::systemDir.c_str());
  181. }
  182. INFO("Initializing plugins");
  183. plugin::initStaticPlugins();
  184. INFO("Initializing plugin browser DB");
  185. app::browserInit();
  186. #if defined(HAVE_LIBLO) && defined(HEADLESS)
  187. INFO("Initializing OSC Remote control");
  188. oscServer = lo_server_new_with_proto(REMOTE_HOST_PORT, LO_UDP, osc_error_handler);
  189. DISTRHO_SAFE_ASSERT_RETURN(oscServer != nullptr,);
  190. lo_server_add_method(oscServer, "/hello", "", osc_hello_handler, this);
  191. lo_server_add_method(oscServer, "/load", "b", osc_load_handler, this);
  192. lo_server_add_method(oscServer, "/screenshot", "b", osc_screenshot_handler, this);
  193. lo_server_add_method(oscServer, nullptr, nullptr, osc_fallback_handler, nullptr);
  194. startThread();
  195. #elif defined(HEADLESS)
  196. INFO("OSC Remote control is not enabled in this build");
  197. #endif
  198. }
  199. ~Initializer()
  200. {
  201. using namespace rack;
  202. #if defined(HAVE_LIBLO) && defined(HEADLESS)
  203. if (oscServer != nullptr)
  204. {
  205. stopThread(5000);
  206. lo_server_del_method(oscServer, nullptr, nullptr);
  207. lo_server_free(oscServer);
  208. oscServer = nullptr;
  209. }
  210. #endif
  211. INFO("Clearing asset paths");
  212. asset::bundlePath.clear();
  213. asset::systemDir.clear();
  214. asset::userDir.clear();
  215. INFO("Destroying plugins");
  216. plugin::destroyStaticPlugins();
  217. INFO("Destroying logger");
  218. logger::destroy();
  219. }
  220. #if defined(HAVE_LIBLO) && defined(HEADLESS)
  221. void run() override
  222. {
  223. INFO("OSC Thread Listening for remote commands");
  224. while (! shouldThreadExit())
  225. {
  226. d_msleep(200);
  227. while (lo_server_recv_noblock(oscServer, 0) != 0) {}
  228. }
  229. INFO("OSC Thread Closed");
  230. }
  231. static void osc_error_handler(int num, const char* msg, const char* path)
  232. {
  233. d_stderr("Cardinal OSC Error: code: %i, msg: \"%s\", path: \"%s\")", num, msg, path);
  234. }
  235. static int osc_fallback_handler(const char* const path, const char* const types, lo_arg**, int, lo_message, void*)
  236. {
  237. d_stderr("Cardinal OSC unhandled message \"%s\" with types \"%s\"", path, types);
  238. return 0;
  239. }
  240. static int osc_hello_handler(const char*, const char*, lo_arg**, int, const lo_message m, void* const self)
  241. {
  242. d_stdout("osc_hello_handler()");
  243. const lo_address source = lo_message_get_source(m);
  244. lo_send_from(source, static_cast<Initializer*>(self)->oscServer, LO_TT_IMMEDIATE, "/resp", "ss", "hello", "ok");
  245. return 0;
  246. }
  247. static int osc_load_handler(const char*, const char* types, lo_arg** argv, int argc, const lo_message m, void* const self)
  248. {
  249. d_stdout("osc_load_handler()");
  250. DISTRHO_SAFE_ASSERT_RETURN(argc == 1, 0);
  251. DISTRHO_SAFE_ASSERT_RETURN(types != nullptr && types[0] == 'b', 0);
  252. const int32_t size = argv[0]->blob.size;
  253. DISTRHO_SAFE_ASSERT_RETURN(size > 4, 0);
  254. const uint8_t* const blob = (uint8_t*)(&argv[0]->blob.data);
  255. DISTRHO_SAFE_ASSERT_RETURN(blob != nullptr, 0);
  256. bool ok = false;
  257. if (CardinalBasePlugin* const plugin = static_cast<Initializer*>(self)->oscPlugin)
  258. {
  259. CardinalPluginContext* const context = plugin->context;
  260. std::vector<uint8_t> data(size);
  261. std::memcpy(data.data(), blob, size);
  262. rack::contextSet(context);
  263. rack::system::removeRecursively(context->patch->autosavePath);
  264. rack::system::createDirectories(context->patch->autosavePath);
  265. try {
  266. rack::system::unarchiveToDirectory(data, context->patch->autosavePath);
  267. context->patch->loadAutosave();
  268. ok = true;
  269. }
  270. catch (rack::Exception& e) {
  271. WARN("%s", e.what());
  272. }
  273. rack::contextSet(nullptr);
  274. }
  275. const lo_address source = lo_message_get_source(m);
  276. lo_send_from(source, static_cast<Initializer*>(self)->oscServer,
  277. LO_TT_IMMEDIATE, "/resp", "ss", "load", ok ? "ok" : "fail");
  278. return 0;
  279. }
  280. static int osc_screenshot_handler(const char*, const char* types, lo_arg** argv, int argc, const lo_message m, void* const self)
  281. {
  282. d_stdout("osc_screenshot_handler()");
  283. DISTRHO_SAFE_ASSERT_RETURN(argc == 1, 0);
  284. DISTRHO_SAFE_ASSERT_RETURN(types != nullptr && types[0] == 'b', 0);
  285. const int32_t size = argv[0]->blob.size;
  286. DISTRHO_SAFE_ASSERT_RETURN(size > 4, 0);
  287. const uint8_t* const blob = (uint8_t*)(&argv[0]->blob.data);
  288. DISTRHO_SAFE_ASSERT_RETURN(blob != nullptr, 0);
  289. bool ok = false;
  290. if (CardinalBasePlugin* const plugin = static_cast<Initializer*>(self)->oscPlugin)
  291. ok = plugin->updateStateValue("screenshot", String::asBase64(blob, size).buffer());
  292. const lo_address source = lo_message_get_source(m);
  293. lo_send_from(source, static_cast<Initializer*>(self)->oscServer,
  294. LO_TT_IMMEDIATE, "/resp", "ss", "screenshot", ok ? "ok" : "fail");
  295. return 0;
  296. }
  297. #endif
  298. };
  299. // -----------------------------------------------------------------------------------------------------------
  300. void CardinalPluginContext::writeMidiMessage(const rack::midi::Message& message, const uint8_t channel)
  301. {
  302. if (bypassed)
  303. return;
  304. const size_t size = message.bytes.size();
  305. DISTRHO_SAFE_ASSERT_RETURN(size > 0,);
  306. DISTRHO_SAFE_ASSERT_RETURN(message.frame >= 0,);
  307. MidiEvent event;
  308. event.frame = message.frame;
  309. switch (message.bytes[0] & 0xF0)
  310. {
  311. case 0x80:
  312. case 0x90:
  313. case 0xA0:
  314. case 0xB0:
  315. case 0xE0:
  316. event.size = 3;
  317. break;
  318. case 0xC0:
  319. case 0xD0:
  320. event.size = 2;
  321. break;
  322. case 0xF0:
  323. switch (message.bytes[0] & 0x0F)
  324. {
  325. case 0x0:
  326. case 0x4:
  327. case 0x5:
  328. case 0x7:
  329. case 0x9:
  330. case 0xD:
  331. // unsupported
  332. return;
  333. case 0x1:
  334. case 0x2:
  335. case 0x3:
  336. case 0xE:
  337. event.size = 3;
  338. break;
  339. case 0x6:
  340. case 0x8:
  341. case 0xA:
  342. case 0xB:
  343. case 0xC:
  344. case 0xF:
  345. event.size = 1;
  346. break;
  347. }
  348. break;
  349. default:
  350. // invalid
  351. return;
  352. }
  353. DISTRHO_SAFE_ASSERT_RETURN(size >= event.size,);
  354. std::memcpy(event.data, message.bytes.data(), event.size);
  355. if (channel != 0 && event.data[0] < 0xF0)
  356. event.data[0] |= channel & 0x0F;
  357. plugin->writeMidiEvent(event);
  358. }
  359. // -----------------------------------------------------------------------------------------------------------
  360. struct ScopedContext {
  361. ScopedContext(const CardinalBasePlugin* const plugin)
  362. {
  363. rack::contextSet(plugin->context);
  364. }
  365. ~ScopedContext()
  366. {
  367. rack::contextSet(nullptr);
  368. }
  369. };
  370. // -----------------------------------------------------------------------------------------------------------
  371. class CardinalPlugin : public CardinalBasePlugin
  372. {
  373. SharedResourcePointer<Initializer> fInitializer;
  374. #if DISTRHO_PLUGIN_NUM_INPUTS != 0
  375. /* If host audio ins == outs we can get issues for inplace processing.
  376. * So allocate a float array that will serve as safe copy for those cases.
  377. */
  378. float** fAudioBufferCopy;
  379. #endif
  380. std::string fAutosavePath;
  381. uint64_t fNextExpectedFrame;
  382. struct {
  383. String comment;
  384. String screenshot;
  385. #ifndef HEADLESS
  386. String windowSize;
  387. #endif
  388. } fState;
  389. // bypass handling
  390. bool fWasBypassed;
  391. MidiEvent bypassMidiEvents[16];
  392. #ifndef HEADLESS
  393. // real values, not VCV interpreted ones
  394. float fWindowParameters[kWindowParameterCount];
  395. #endif
  396. public:
  397. CardinalPlugin()
  398. : CardinalBasePlugin(kModuleParameters + kWindowParameterCount + 1, 0, kCardinalStateCount),
  399. fInitializer(this),
  400. #if DISTRHO_PLUGIN_NUM_INPUTS != 0
  401. fAudioBufferCopy(nullptr),
  402. #endif
  403. fNextExpectedFrame(0),
  404. fWasBypassed(false)
  405. {
  406. #ifndef HEADLESS
  407. fWindowParameters[kWindowParameterShowTooltips] = 1.0f;
  408. fWindowParameters[kWindowParameterCableOpacity] = 50.0f;
  409. fWindowParameters[kWindowParameterCableTension] = 75.0f;
  410. fWindowParameters[kWindowParameterRackBrightness] = 100.0f;
  411. fWindowParameters[kWindowParameterHaloBrightness] = 25.0f;
  412. fWindowParameters[kWindowParameterKnobMode] = 0.0f;
  413. fWindowParameters[kWindowParameterWheelKnobControl] = 0.0f;
  414. fWindowParameters[kWindowParameterWheelSensitivity] = 1.0f;
  415. fWindowParameters[kWindowParameterLockModulePositions] = 0.0f;
  416. fWindowParameters[kWindowParameterUpdateRateLimit] = 0.0f;
  417. fWindowParameters[kWindowParameterBrowserSort] = 3.0f;
  418. fWindowParameters[kWindowParameterBrowserZoom] = 50.0f;
  419. fWindowParameters[kWindowParameterInvertZoom] = 0.0f;
  420. #endif
  421. // create unique temporary path for this instance
  422. try {
  423. char uidBuf[24];
  424. const std::string tmp = rack::system::getTempDirectory();
  425. for (int i=1;; ++i)
  426. {
  427. std::snprintf(uidBuf, sizeof(uidBuf), "Cardinal.%04d", i);
  428. const std::string trypath = rack::system::join(tmp, uidBuf);
  429. if (! rack::system::exists(trypath))
  430. {
  431. if (rack::system::createDirectories(trypath))
  432. fAutosavePath = trypath;
  433. break;
  434. }
  435. }
  436. } DISTRHO_SAFE_EXCEPTION("create unique temporary path");
  437. // initialize midi events used when entering bypassed state
  438. std::memset(bypassMidiEvents, 0, sizeof(bypassMidiEvents));
  439. for (uint8_t i=0; i<16; ++i)
  440. {
  441. bypassMidiEvents[i].size = 3;
  442. bypassMidiEvents[i].data[0] = 0xB0 + i;
  443. bypassMidiEvents[i].data[1] = 0x7B;
  444. }
  445. const float sampleRate = getSampleRate();
  446. rack::settings::sampleRate = sampleRate;
  447. context->bufferSize = getBufferSize();
  448. context->sampleRate = sampleRate;
  449. const ScopedContext sc(this);
  450. context->engine = new rack::engine::Engine;
  451. context->engine->setSampleRate(sampleRate);
  452. context->history = new rack::history::State;
  453. context->patch = new rack::patch::Manager;
  454. context->patch->autosavePath = fAutosavePath;
  455. context->patch->templatePath = fInitializer->templatePath;
  456. context->event = new rack::widget::EventState;
  457. context->scene = new rack::app::Scene;
  458. context->event->rootWidget = context->scene;
  459. if (! isDummyInstance())
  460. context->window = new rack::window::Window;
  461. context->patch->loadTemplate();
  462. context->scene->rackScroll->reset();
  463. #if defined(HAVE_LIBLO) && defined(HEADLESS)
  464. fInitializer->oscPlugin = this;
  465. #endif
  466. }
  467. ~CardinalPlugin() override
  468. {
  469. #if defined(HAVE_LIBLO) && defined(HEADLESS)
  470. fInitializer->oscPlugin = nullptr;
  471. #endif
  472. {
  473. const ScopedContext sc(this);
  474. context->patch->clear();
  475. delete context;
  476. }
  477. if (! fAutosavePath.empty())
  478. rack::system::removeRecursively(fAutosavePath);
  479. }
  480. CardinalPluginContext* getRackContext() const noexcept
  481. {
  482. return context;
  483. }
  484. protected:
  485. /* --------------------------------------------------------------------------------------------------------
  486. * Information */
  487. const char* getLabel() const override
  488. {
  489. return DISTRHO_PLUGIN_LABEL;
  490. }
  491. const char* getDescription() const override
  492. {
  493. return ""
  494. "Cardinal is a free and open-source virtual modular synthesizer plugin.\n"
  495. "It is based on the popular VCV Rack but with a focus on being a fully self-contained plugin version.\n"
  496. "It is not an official VCV project, and it is not affiliated with it in any way.\n"
  497. "\n"
  498. "Cardinal contains Rack, some 3rd-party modules and a few internal utilities.\n"
  499. "It does not load external modules and does not connect to the official Rack library/store.\n";
  500. }
  501. const char* getMaker() const override
  502. {
  503. return "DISTRHO";
  504. }
  505. const char* getHomePage() const override
  506. {
  507. return "https://github.com/DISTRHO/Cardinal";
  508. }
  509. const char* getLicense() const override
  510. {
  511. return "GPLv3+";
  512. }
  513. uint32_t getVersion() const override
  514. {
  515. return d_version(0, 22, 5);
  516. }
  517. int64_t getUniqueId() const override
  518. {
  519. #if CARDINAL_VARIANT_MAIN
  520. return d_cconst('d', 'C', 'd', 'n');
  521. #elif CARDINAL_VARIANT_FX
  522. return d_cconst('d', 'C', 'n', 'F');
  523. #elif CARDINAL_VARIANT_SYNTH
  524. return d_cconst('d', 'C', 'n', 'S');
  525. #else
  526. #error cardinal variant not set
  527. #endif
  528. }
  529. /* --------------------------------------------------------------------------------------------------------
  530. * Init */
  531. void initAudioPort(const bool input, uint32_t index, AudioPort& port) override
  532. {
  533. if (index >= 8)
  534. {
  535. port.hints = kAudioPortIsCV | kCVPortHasPositiveUnipolarRange | kCVPortHasScaledRange;
  536. index -= 8;
  537. }
  538. CardinalBasePlugin::initAudioPort(input, index, port);
  539. }
  540. void initParameter(const uint32_t index, Parameter& parameter) override
  541. {
  542. if (index < kModuleParameters)
  543. {
  544. parameter.name = "Parameter ";
  545. parameter.name += String(index + 1);
  546. parameter.symbol = "param_";
  547. parameter.symbol += String(index + 1);
  548. parameter.unit = "v";
  549. parameter.hints = kParameterIsAutomatable;
  550. parameter.ranges.def = 0.0f;
  551. parameter.ranges.min = 0.0f;
  552. parameter.ranges.max = 10.0f;
  553. return;
  554. }
  555. if (index == kModuleParameters)
  556. {
  557. parameter.initDesignation(kParameterDesignationBypass);
  558. return;
  559. }
  560. #ifndef HEADLESS
  561. switch (index - kModuleParameters - 1)
  562. {
  563. case kWindowParameterShowTooltips:
  564. parameter.name = "Show tooltips";
  565. parameter.symbol = "tooltips";
  566. parameter.hints = kParameterIsAutomatable|kParameterIsInteger|kParameterIsBoolean;
  567. parameter.ranges.def = 1.0f;
  568. parameter.ranges.min = 0.0f;
  569. parameter.ranges.max = 1.0f;
  570. break;
  571. case kWindowParameterCableOpacity:
  572. parameter.name = "Cable opacity";
  573. parameter.symbol = "cableOpacity";
  574. parameter.unit = "%";
  575. parameter.hints = kParameterIsAutomatable;
  576. parameter.ranges.def = 50.0f;
  577. parameter.ranges.min = 0.0f;
  578. parameter.ranges.max = 100.0f;
  579. break;
  580. case kWindowParameterCableTension:
  581. parameter.name = "Cable tension";
  582. parameter.symbol = "cableTension";
  583. parameter.unit = "%";
  584. parameter.hints = kParameterIsAutomatable;
  585. parameter.ranges.def = 75.0f;
  586. parameter.ranges.min = 0.0f;
  587. parameter.ranges.max = 100.0f;
  588. break;
  589. case kWindowParameterRackBrightness:
  590. parameter.name = "Room brightness";
  591. parameter.symbol = "rackBrightness";
  592. parameter.unit = "%";
  593. parameter.hints = kParameterIsAutomatable;
  594. parameter.ranges.def = 100.0f;
  595. parameter.ranges.min = 0.0f;
  596. parameter.ranges.max = 100.0f;
  597. break;
  598. case kWindowParameterHaloBrightness:
  599. parameter.name = "Light Bloom";
  600. parameter.symbol = "haloBrightness";
  601. parameter.unit = "%";
  602. parameter.hints = kParameterIsAutomatable;
  603. parameter.ranges.def = 25.0f;
  604. parameter.ranges.min = 0.0f;
  605. parameter.ranges.max = 100.0f;
  606. break;
  607. case kWindowParameterKnobMode:
  608. parameter.name = "Knob mode";
  609. parameter.symbol = "knobMode";
  610. parameter.hints = kParameterIsAutomatable|kParameterIsInteger;
  611. parameter.ranges.def = 0.0f;
  612. parameter.ranges.min = 0.0f;
  613. parameter.ranges.max = 2.0f;
  614. parameter.enumValues.count = 3;
  615. parameter.enumValues.restrictedMode = true;
  616. parameter.enumValues.values = new ParameterEnumerationValue[3];
  617. parameter.enumValues.values[0].label = "Linear";
  618. parameter.enumValues.values[0].value = 0.0f;
  619. parameter.enumValues.values[1].label = "Absolute rotary";
  620. parameter.enumValues.values[1].value = 1.0f;
  621. parameter.enumValues.values[2].label = "Relative rotary";
  622. parameter.enumValues.values[2].value = 2.0f;
  623. break;
  624. case kWindowParameterWheelKnobControl:
  625. parameter.name = "Scroll wheel knob control";
  626. parameter.symbol = "knobScroll";
  627. parameter.hints = kParameterIsAutomatable|kParameterIsInteger|kParameterIsBoolean;
  628. parameter.ranges.def = 0.0f;
  629. parameter.ranges.min = 0.0f;
  630. parameter.ranges.max = 1.0f;
  631. break;
  632. case kWindowParameterWheelSensitivity:
  633. parameter.name = "Scroll wheel knob sensitivity";
  634. parameter.symbol = "knobScrollSensitivity";
  635. parameter.hints = kParameterIsAutomatable|kParameterIsLogarithmic;
  636. parameter.ranges.def = 1.0f;
  637. parameter.ranges.min = 0.1f;
  638. parameter.ranges.max = 10.0f;
  639. break;
  640. case kWindowParameterLockModulePositions:
  641. parameter.name = "Lock module positions";
  642. parameter.symbol = "lockModules";
  643. parameter.hints = kParameterIsAutomatable|kParameterIsInteger|kParameterIsBoolean;
  644. parameter.ranges.def = 0.0f;
  645. parameter.ranges.min = 0.0f;
  646. parameter.ranges.max = 1.0f;
  647. break;
  648. case kWindowParameterUpdateRateLimit:
  649. parameter.name = "Update rate limit";
  650. parameter.symbol = "rateLimit";
  651. parameter.hints = kParameterIsAutomatable|kParameterIsInteger;
  652. parameter.ranges.def = 0.0f;
  653. parameter.ranges.min = 0.0f;
  654. parameter.ranges.max = 2.0f;
  655. parameter.enumValues.count = 3;
  656. parameter.enumValues.restrictedMode = true;
  657. parameter.enumValues.values = new ParameterEnumerationValue[3];
  658. parameter.enumValues.values[0].label = "None";
  659. parameter.enumValues.values[0].value = 0.0f;
  660. parameter.enumValues.values[1].label = "2x";
  661. parameter.enumValues.values[1].value = 1.0f;
  662. parameter.enumValues.values[2].label = "4x";
  663. parameter.enumValues.values[2].value = 2.0f;
  664. break;
  665. case kWindowParameterBrowserSort:
  666. parameter.name = "Browser sort";
  667. parameter.symbol = "browserSort";
  668. parameter.hints = kParameterIsAutomatable|kParameterIsInteger;
  669. parameter.ranges.def = 3.0f;
  670. parameter.ranges.min = 0.0f;
  671. parameter.ranges.max = 5.0f;
  672. parameter.enumValues.count = 6;
  673. parameter.enumValues.restrictedMode = true;
  674. parameter.enumValues.values = new ParameterEnumerationValue[6];
  675. parameter.enumValues.values[0].label = "Updated";
  676. parameter.enumValues.values[0].value = 0.0f;
  677. parameter.enumValues.values[1].label = "Last used";
  678. parameter.enumValues.values[1].value = 1.0f;
  679. parameter.enumValues.values[2].label = "Most used";
  680. parameter.enumValues.values[2].value = 2.0f;
  681. parameter.enumValues.values[3].label = "Brand";
  682. parameter.enumValues.values[3].value = 3.0f;
  683. parameter.enumValues.values[4].label = "Name";
  684. parameter.enumValues.values[4].value = 4.0f;
  685. parameter.enumValues.values[5].label = "Random";
  686. parameter.enumValues.values[5].value = 5.0f;
  687. break;
  688. case kWindowParameterBrowserZoom:
  689. parameter.name = "Browser zoom";
  690. parameter.symbol = "browserZoom";
  691. parameter.hints = kParameterIsAutomatable;
  692. parameter.unit = "%";
  693. parameter.ranges.def = 50.0f;
  694. parameter.ranges.min = 25.0f;
  695. parameter.ranges.max = 200.0f;
  696. parameter.enumValues.count = 7;
  697. parameter.enumValues.restrictedMode = true;
  698. parameter.enumValues.values = new ParameterEnumerationValue[7];
  699. parameter.enumValues.values[0].label = "25";
  700. parameter.enumValues.values[0].value = 25.0f;
  701. parameter.enumValues.values[1].label = "35";
  702. parameter.enumValues.values[1].value = 35.0f;
  703. parameter.enumValues.values[2].label = "50";
  704. parameter.enumValues.values[2].value = 50.0f;
  705. parameter.enumValues.values[3].label = "71";
  706. parameter.enumValues.values[3].value = 71.0f;
  707. parameter.enumValues.values[4].label = "100";
  708. parameter.enumValues.values[4].value = 100.0f;
  709. parameter.enumValues.values[5].label = "141";
  710. parameter.enumValues.values[5].value = 141.0f;
  711. parameter.enumValues.values[6].label = "200";
  712. parameter.enumValues.values[6].value = 200.0f;
  713. break;
  714. case kWindowParameterInvertZoom:
  715. parameter.name = "Invert zoom";
  716. parameter.symbol = "invertZoom";
  717. parameter.hints = kParameterIsAutomatable|kParameterIsInteger|kParameterIsBoolean;
  718. parameter.ranges.def = 0.0f;
  719. parameter.ranges.min = 0.0f;
  720. parameter.ranges.max = 1.0f;
  721. break;
  722. }
  723. #endif
  724. }
  725. void initState(const uint32_t index, State& state) override
  726. {
  727. switch (index)
  728. {
  729. case 0:
  730. state.hints = kStateIsBase64Blob | kStateIsOnlyForDSP;
  731. state.key = "patch";
  732. state.label = "Patch";
  733. break;
  734. case 1:
  735. state.hints = kStateIsHostReadable | kStateIsBase64Blob;
  736. state.key = "screenshot";
  737. state.label = "Screenshot";
  738. break;
  739. case 2:
  740. state.hints = kStateIsHostWritable;
  741. state.key = "comment";
  742. state.label = "Comment";
  743. break;
  744. case 3:
  745. state.hints = kStateIsOnlyForUI;
  746. state.key = "moduleInfos";
  747. state.label = "moduleInfos";
  748. break;
  749. case 4:
  750. state.hints = kStateIsOnlyForUI;
  751. state.key = "windowSize";
  752. state.label = "Window size";
  753. break;
  754. }
  755. }
  756. /* --------------------------------------------------------------------------------------------------------
  757. * Internal data */
  758. float getParameterValue(uint32_t index) const override
  759. {
  760. // host mapped parameters
  761. if (index < kModuleParameters)
  762. return context->parameters[index];
  763. // bypass
  764. if (index == kModuleParameters)
  765. return context->bypassed ? 1.0f : 0.0f;
  766. #ifndef HEADLESS
  767. // window related parameters
  768. index -= kModuleParameters + 1;
  769. if (index < kWindowParameterCount)
  770. return fWindowParameters[index];
  771. #endif
  772. return 0.0f;
  773. }
  774. void setParameterValue(uint32_t index, float value) override
  775. {
  776. // host mapped parameters
  777. if (index < kModuleParameters)
  778. {
  779. context->parameters[index] = value;
  780. return;
  781. }
  782. // bypass
  783. if (index == kModuleParameters)
  784. {
  785. context->bypassed = value > 0.5f;
  786. return;
  787. }
  788. #ifndef HEADLESS
  789. // window related parameters
  790. index -= kModuleParameters + 1;
  791. if (index < kWindowParameterCount)
  792. {
  793. fWindowParameters[index] = value;
  794. return;
  795. }
  796. #endif
  797. }
  798. String getState(const char* const key) const override
  799. {
  800. #ifndef HEADLESS
  801. if (std::strcmp(key, "moduleInfos") == 0)
  802. {
  803. json_t* const rootJ = json_object();
  804. DISTRHO_SAFE_ASSERT_RETURN(rootJ != nullptr, String());
  805. for (const auto& pluginPair : rack::settings::moduleInfos)
  806. {
  807. json_t* const pluginJ = json_object();
  808. DISTRHO_SAFE_ASSERT_CONTINUE(pluginJ != nullptr);
  809. for (const auto& modulePair : pluginPair.second)
  810. {
  811. json_t* const moduleJ = json_object();
  812. DISTRHO_SAFE_ASSERT_CONTINUE(moduleJ != nullptr);
  813. const rack::settings::ModuleInfo& m(modulePair.second);
  814. // To make setting.json smaller, only set properties if not default values.
  815. if (m.favorite)
  816. json_object_set_new(moduleJ, "favorite", json_boolean(m.favorite));
  817. if (m.added > 0)
  818. json_object_set_new(moduleJ, "added", json_integer(m.added));
  819. if (std::isfinite(m.lastAdded))
  820. json_object_set_new(moduleJ, "lastAdded", json_real(m.lastAdded));
  821. if (json_object_size(moduleJ))
  822. json_object_set_new(pluginJ, modulePair.first.c_str(), moduleJ);
  823. else
  824. json_decref(moduleJ);
  825. }
  826. if (json_object_size(pluginJ))
  827. json_object_set_new(rootJ, pluginPair.first.c_str(), pluginJ);
  828. else
  829. json_decref(pluginJ);
  830. }
  831. const String info(json_dumps(rootJ, JSON_COMPACT), false);
  832. json_decref(rootJ);
  833. return info;
  834. }
  835. if (std::strcmp(key, "windowSize") == 0)
  836. return fState.windowSize;
  837. #endif
  838. if (std::strcmp(key, "comment") == 0)
  839. return fState.comment;
  840. if (std::strcmp(key, "screenshot") == 0)
  841. return fState.screenshot;
  842. if (std::strcmp(key, "patch") != 0)
  843. return String();
  844. if (fAutosavePath.empty())
  845. return String();
  846. std::vector<uint8_t> data;
  847. {
  848. const ScopedContext sc(this);
  849. context->engine->prepareSave();
  850. context->patch->saveAutosave();
  851. context->patch->cleanAutosave();
  852. // context->history->setSaved();
  853. try {
  854. data = rack::system::archiveDirectory(fAutosavePath, 1);
  855. } DISTRHO_SAFE_EXCEPTION_RETURN("getState archiveDirectory", String());
  856. }
  857. return String::asBase64(data.data(), data.size());
  858. }
  859. void setState(const char* const key, const char* const value) override
  860. {
  861. #ifndef HEADLESS
  862. if (std::strcmp(key, "moduleInfos") == 0)
  863. {
  864. json_error_t error;
  865. json_t* const rootJ = json_loads(value, 0, &error);
  866. DISTRHO_SAFE_ASSERT_RETURN(rootJ != nullptr,);
  867. const char* pluginSlug;
  868. json_t* pluginJ;
  869. json_object_foreach(rootJ, pluginSlug, pluginJ)
  870. {
  871. const char* moduleSlug;
  872. json_t* moduleJ;
  873. json_object_foreach(pluginJ, moduleSlug, moduleJ)
  874. {
  875. rack::settings::ModuleInfo m;
  876. if (json_t* const favoriteJ = json_object_get(moduleJ, "favorite"))
  877. m.favorite = json_boolean_value(favoriteJ);
  878. if (json_t* const addedJ = json_object_get(moduleJ, "added"))
  879. m.added = json_integer_value(addedJ);
  880. if (json_t* const lastAddedJ = json_object_get(moduleJ, "lastAdded"))
  881. m.lastAdded = json_number_value(lastAddedJ);
  882. rack::settings::moduleInfos[pluginSlug][moduleSlug] = m;
  883. }
  884. }
  885. json_decref(rootJ);
  886. return;
  887. }
  888. if (std::strcmp(key, "windowSize") == 0)
  889. {
  890. fState.windowSize = value;
  891. return;
  892. }
  893. #endif
  894. if (std::strcmp(key, "comment") == 0)
  895. {
  896. fState.comment = value;
  897. return;
  898. }
  899. if (std::strcmp(key, "screenshot") == 0)
  900. {
  901. fState.screenshot = value;
  902. #if defined(HAVE_LIBLO) && !defined(HEADLESS)
  903. patchUtils::sendScreenshotToRemote(value);
  904. #endif
  905. return;
  906. }
  907. if (std::strcmp(key, "patch") != 0)
  908. return;
  909. if (fAutosavePath.empty())
  910. return;
  911. const std::vector<uint8_t> data(d_getChunkFromBase64String(value));
  912. const ScopedContext sc(this);
  913. rack::system::removeRecursively(fAutosavePath);
  914. rack::system::createDirectories(fAutosavePath);
  915. rack::system::unarchiveToDirectory(data, fAutosavePath);
  916. try {
  917. context->patch->loadAutosave();
  918. } DISTRHO_SAFE_EXCEPTION_RETURN("setState loadAutosave",);
  919. // context->history->setSaved();
  920. }
  921. /* --------------------------------------------------------------------------------------------------------
  922. * Process */
  923. void activate() override
  924. {
  925. #if DISTRHO_PLUGIN_NUM_INPUTS != 0
  926. const uint32_t bufferSize = getBufferSize();
  927. fAudioBufferCopy = new float*[DISTRHO_PLUGIN_NUM_INPUTS];
  928. for (int i=0; i<DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  929. fAudioBufferCopy[i] = new float[bufferSize];
  930. #endif
  931. fNextExpectedFrame = 0;
  932. }
  933. void deactivate() override
  934. {
  935. #if DISTRHO_PLUGIN_NUM_INPUTS != 0
  936. if (fAudioBufferCopy != nullptr)
  937. {
  938. for (int i=0; i<DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  939. delete[] fAudioBufferCopy[i];
  940. delete[] fAudioBufferCopy;
  941. fAudioBufferCopy = nullptr;
  942. }
  943. #endif
  944. }
  945. void run(const float** const inputs, float** const outputs, const uint32_t frames,
  946. const MidiEvent* const midiEvents, const uint32_t midiEventCount) override
  947. {
  948. rack::contextSet(context);
  949. const bool bypassed = context->bypassed;
  950. {
  951. const TimePosition& timePos(getTimePosition());
  952. const bool reset = timePos.playing && (timePos.frame == 0 || fNextExpectedFrame != timePos.frame);
  953. context->playing = timePos.playing;
  954. context->bbtValid = timePos.bbt.valid;
  955. context->frame = timePos.frame;
  956. if (timePos.bbt.valid)
  957. {
  958. const double samplesPerTick = 60.0 * getSampleRate()
  959. / timePos.bbt.beatsPerMinute
  960. / timePos.bbt.ticksPerBeat;
  961. context->bar = timePos.bbt.bar;
  962. context->beat = timePos.bbt.beat;
  963. context->beatsPerBar = timePos.bbt.beatsPerBar;
  964. context->beatType = timePos.bbt.beatType;
  965. context->barStartTick = timePos.bbt.barStartTick;
  966. context->beatsPerMinute = timePos.bbt.beatsPerMinute;
  967. context->tick = timePos.bbt.tick;
  968. context->ticksPerBeat = timePos.bbt.ticksPerBeat;
  969. context->ticksPerClock = timePos.bbt.ticksPerBeat / timePos.bbt.beatType;
  970. context->ticksPerFrame = 1.0 / samplesPerTick;
  971. context->tickClock = std::fmod(timePos.bbt.tick, context->ticksPerClock);
  972. }
  973. context->reset = reset;
  974. fNextExpectedFrame = timePos.playing ? timePos.frame + frames : 0;
  975. }
  976. // separate buffers, use them
  977. if (inputs != outputs && (inputs == nullptr || inputs[0] != outputs[0]))
  978. {
  979. context->dataIns = inputs;
  980. context->dataOuts = outputs;
  981. }
  982. // inline processing, use a safe copy
  983. else
  984. {
  985. #if DISTRHO_PLUGIN_NUM_INPUTS != 0
  986. for (int i=0; i<DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  987. std::memcpy(fAudioBufferCopy[i], inputs[i], sizeof(float)*frames);
  988. context->dataIns = fAudioBufferCopy;
  989. #else
  990. context->dataIns = nullptr;
  991. #endif
  992. context->dataOuts = outputs;
  993. }
  994. for (int i=0; i<DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  995. std::memset(outputs[i], 0, sizeof(float)*frames);
  996. if (bypassed)
  997. {
  998. if (fWasBypassed != bypassed)
  999. {
  1000. context->midiEvents = bypassMidiEvents;
  1001. context->midiEventCount = 16;
  1002. }
  1003. else
  1004. {
  1005. context->midiEvents = nullptr;
  1006. context->midiEventCount = 0;
  1007. }
  1008. }
  1009. else
  1010. {
  1011. context->midiEvents = midiEvents;
  1012. context->midiEventCount = midiEventCount;
  1013. }
  1014. ++context->processCounter;
  1015. context->engine->stepBlock(frames);
  1016. fWasBypassed = bypassed;
  1017. }
  1018. void bufferSizeChanged(const uint32_t newBufferSize) override
  1019. {
  1020. rack::contextSet(context);
  1021. context->bufferSize = newBufferSize;
  1022. }
  1023. void sampleRateChanged(const double newSampleRate) override
  1024. {
  1025. rack::contextSet(context);
  1026. rack::settings::sampleRate = newSampleRate;
  1027. context->sampleRate = newSampleRate;
  1028. context->engine->setSampleRate(newSampleRate);
  1029. }
  1030. // -------------------------------------------------------------------------------------------------------
  1031. private:
  1032. /**
  1033. Set our plugin class as non-copyable and add a leak detector just in case.
  1034. */
  1035. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CardinalPlugin)
  1036. };
  1037. CardinalPluginContext* getRackContextFromPlugin(void* const ptr)
  1038. {
  1039. return static_cast<CardinalPlugin*>(ptr)->getRackContext();
  1040. }
  1041. /* ------------------------------------------------------------------------------------------------------------
  1042. * Plugin entry point, called by DPF to create a new plugin instance. */
  1043. Plugin* createPlugin()
  1044. {
  1045. return new CardinalPlugin();
  1046. }
  1047. // -----------------------------------------------------------------------------------------------------------
  1048. END_NAMESPACE_DISTRHO