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.

2839 lines
102KB

  1. /*
  2. * Carla Plugin, DSSI implementation
  3. * Copyright (C) 2011-2018 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. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngineUtils.hpp"
  19. #include "CarlaDssiUtils.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #ifdef HAVE_LIBLO
  22. # include "CarlaOscUtils.hpp"
  23. # include "CarlaPipeUtils.hpp"
  24. # include "CarlaThread.hpp"
  25. #endif
  26. #include "water/threads/ChildProcess.h"
  27. using water::ChildProcess;
  28. using water::String;
  29. using water::StringArray;
  30. #define CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(/* argc, types, */ argcToCompare, typesToCompare) \
  31. /* check argument count */ \
  32. if (argc != argcToCompare) \
  33. { \
  34. carla_stderr("CarlaPluginDSSI::%s() - argument count mismatch: %i != %i", __FUNCTION__, argc, argcToCompare); \
  35. return; \
  36. } \
  37. if (argc > 0) \
  38. { \
  39. /* check for nullness */ \
  40. if (types == nullptr || typesToCompare == nullptr) \
  41. { \
  42. carla_stderr("CarlaPluginDSSI::%s() - argument types are null", __FUNCTION__); \
  43. return; \
  44. } \
  45. /* check argument types */ \
  46. if (std::strcmp(types, typesToCompare) != 0) \
  47. { \
  48. carla_stderr("CarlaPluginDSSI::%s() - argument types mismatch: '%s' != '%s'", __FUNCTION__, types, typesToCompare); \
  49. return; \
  50. } \
  51. }
  52. CARLA_BACKEND_START_NAMESPACE
  53. // -------------------------------------------------------------------
  54. // Fallback data
  55. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  56. #ifdef HAVE_LIBLO
  57. // -------------------------------------------------------------------
  58. class CarlaThreadDSSIUI : public CarlaThread
  59. {
  60. public:
  61. CarlaThreadDSSIUI(CarlaEngine* const engine, CarlaPlugin* const plugin, const CarlaOscData& oscData) noexcept
  62. : CarlaThread("CarlaThreadDSSIUI"),
  63. kEngine(engine),
  64. kPlugin(plugin),
  65. fBinary(),
  66. fLabel(),
  67. fOscData(oscData),
  68. fProcess() {}
  69. void setData(const char* const binary, const char* const label) noexcept
  70. {
  71. CARLA_SAFE_ASSERT_RETURN(binary != nullptr && binary[0] != '\0',);
  72. CARLA_SAFE_ASSERT_RETURN(label != nullptr /*&& label[0] != '\0'*/,);
  73. CARLA_SAFE_ASSERT(! isThreadRunning());
  74. fBinary = binary;
  75. fLabel = label;
  76. if (fLabel.isEmpty())
  77. fLabel = "\"\"";
  78. }
  79. uintptr_t getProcessId() const noexcept
  80. {
  81. CARLA_SAFE_ASSERT_RETURN(fProcess != nullptr, 0);
  82. return (uintptr_t)fProcess->getPID();
  83. }
  84. void run()
  85. {
  86. carla_stdout("DSSI UI thread started");
  87. if (fProcess == nullptr)
  88. {
  89. fProcess = new ChildProcess();
  90. }
  91. else if (fProcess->isRunning())
  92. {
  93. carla_stderr("CarlaThreadDSSI::run() - already running, giving up...");
  94. fProcess->kill();
  95. fProcess = nullptr;
  96. kEngine->callback(CarlaBackend::ENGINE_CALLBACK_UI_STATE_CHANGED, kPlugin->getId(), 0, 0, 0.0f, nullptr);
  97. return;
  98. }
  99. String name(kPlugin->getName());
  100. String filename(kPlugin->getFilename());
  101. if (name.isEmpty())
  102. name = "(none)";
  103. if (filename.isEmpty())
  104. filename = "\"\"";
  105. StringArray arguments;
  106. // binary
  107. arguments.add(fBinary.buffer());
  108. // osc-url
  109. arguments.add(String(kEngine->getOscServerPathUDP()) + String("/") + String(kPlugin->getId()));
  110. // filename
  111. arguments.add(filename);
  112. // label
  113. arguments.add(fLabel.buffer());
  114. // ui-title
  115. arguments.add(name + String(" (GUI)"));
  116. bool started;
  117. {
  118. #ifdef CARLA_OS_LINUX
  119. /*
  120. * If the frontend uses winId parent, set LD_PRELOAD to auto-map the DSSI UI.
  121. * If not, unset LD_PRELOAD.
  122. */
  123. const uintptr_t winId(kEngine->getOptions().frontendWinId);
  124. // for CARLA_ENGINE_OPTION_FRONTEND_WIN_ID
  125. char winIdStr[STR_MAX+1];
  126. winIdStr[STR_MAX] = '\0';
  127. // for LD_PRELOAD
  128. CarlaString ldPreloadValue;
  129. if (winId != 0)
  130. {
  131. std::snprintf(winIdStr, STR_MAX, P_UINTPTR, winId);
  132. ldPreloadValue = (CarlaString(kEngine->getOptions().binaryDir)
  133. + "/libcarla_interposer-x11.so");
  134. }
  135. else
  136. {
  137. winIdStr[0] = '\0';
  138. }
  139. const ScopedEngineEnvironmentLocker _seel(kEngine);
  140. const ScopedEnvVar _sev1("CARLA_ENGINE_OPTION_FRONTEND_WIN_ID", winIdStr[0] != '\0' ? winIdStr : nullptr);
  141. const ScopedEnvVar _sev2("LD_PRELOAD", ldPreloadValue.isNotEmpty() ? ldPreloadValue.buffer() : nullptr);
  142. #endif // CARLA_OS_LINUX
  143. // start the DSSI UI application
  144. carla_stdout("starting DSSI UI...");
  145. started = fProcess->start(arguments);
  146. }
  147. if (! started)
  148. {
  149. carla_stdout("failed!");
  150. fProcess = nullptr;
  151. return;
  152. }
  153. if (waitForOscGuiShow())
  154. {
  155. for (; fProcess->isRunning() && ! shouldThreadExit();)
  156. carla_sleep(1);
  157. // we only get here if UI was closed or thread asked to exit
  158. if (fProcess->isRunning() && shouldThreadExit())
  159. {
  160. fProcess->waitForProcessToFinish(static_cast<int>(kEngine->getOptions().uiBridgesTimeout));
  161. if (fProcess->isRunning())
  162. {
  163. carla_stdout("CarlaThreadDSSIUI::run() - UI refused to close, force kill now");
  164. fProcess->kill();
  165. }
  166. else
  167. {
  168. carla_stdout("CarlaThreadDSSIUI::run() - UI auto-closed successfully");
  169. }
  170. }
  171. else if (fProcess->getExitCode() != 0 /*|| fProcess->exitStatus() == QProcess::CrashExit*/)
  172. carla_stderr("CarlaThreadDSSIUI::run() - UI crashed while running");
  173. else
  174. carla_stdout("CarlaThreadDSSIUI::run() - UI closed cleanly");
  175. }
  176. else
  177. {
  178. fProcess->kill();
  179. carla_stdout("CarlaThreadDSSIUI::run() - GUI timeout");
  180. }
  181. fProcess = nullptr;
  182. kEngine->callback(CarlaBackend::ENGINE_CALLBACK_UI_STATE_CHANGED, kPlugin->getId(), 0, 0, 0.0f, nullptr);
  183. carla_stdout("DSSI UI thread finished");
  184. }
  185. private:
  186. CarlaEngine* const kEngine;
  187. CarlaPlugin* const kPlugin;
  188. CarlaString fBinary;
  189. CarlaString fLabel;
  190. const CarlaOscData& fOscData;
  191. ScopedPointer<ChildProcess> fProcess;
  192. bool waitForOscGuiShow()
  193. {
  194. carla_stdout("CarlaThreadDSSIUI::waitForOscGuiShow()");
  195. const uint uiBridgesTimeout = kEngine->getOptions().uiBridgesTimeout;
  196. // wait for UI 'update' call
  197. for (uint i=0; i < uiBridgesTimeout/100; ++i)
  198. {
  199. if (fOscData.target != nullptr)
  200. {
  201. carla_stdout("CarlaThreadDSSIUI::waitForOscGuiShow() - got response, asking UI to show itself now");
  202. osc_send_show(fOscData);
  203. return true;
  204. }
  205. if (fProcess != nullptr && fProcess->isRunning() && ! shouldThreadExit())
  206. carla_msleep(100);
  207. else
  208. return false;
  209. }
  210. carla_stdout("CarlaThreadDSSIUI::waitForOscGuiShow() - Timeout while waiting for UI to respond"
  211. "(waited %u msecs)", uiBridgesTimeout);
  212. return false;
  213. }
  214. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaThreadDSSIUI)
  215. };
  216. #endif
  217. // -----------------------------------------------------
  218. class CarlaPluginDSSI : public CarlaPlugin
  219. {
  220. public:
  221. CarlaPluginDSSI(CarlaEngine* const engine, const uint id) noexcept
  222. : CarlaPlugin(engine, id),
  223. fHandles(),
  224. fDescriptor(nullptr),
  225. fDssiDescriptor(nullptr),
  226. fAudioInBuffers(nullptr),
  227. fAudioOutBuffers(nullptr),
  228. fExtraStereoBuffer(),
  229. fParamBuffers(nullptr),
  230. fLatencyIndex(-1),
  231. fForcedStereoIn(false),
  232. fForcedStereoOut(false),
  233. fNeedsFixedBuffers(false),
  234. fUsesCustomData(false)
  235. #ifdef HAVE_LIBLO
  236. , fOscData(),
  237. fThreadUI(engine, this, fOscData),
  238. fUiFilename(nullptr)
  239. #endif
  240. {
  241. carla_debug("CarlaPluginDSSI::CarlaPluginDSSI(%p, %i)", engine, id);
  242. carla_zeroPointers(fExtraStereoBuffer, 2);
  243. }
  244. ~CarlaPluginDSSI() noexcept override
  245. {
  246. carla_debug("CarlaPluginDSSI::~CarlaPluginDSSI()");
  247. #ifdef HAVE_LIBLO
  248. // close UI
  249. if (fUiFilename != nullptr)
  250. {
  251. showCustomUI(false);
  252. delete[] fUiFilename;
  253. fUiFilename = nullptr;
  254. }
  255. #endif
  256. pData->singleMutex.lock();
  257. pData->masterMutex.lock();
  258. if (pData->client != nullptr && pData->client->isActive())
  259. pData->client->deactivate();
  260. if (pData->active)
  261. {
  262. deactivate();
  263. pData->active = false;
  264. }
  265. if (fDescriptor != nullptr)
  266. {
  267. if (fDescriptor->cleanup != nullptr)
  268. {
  269. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  270. {
  271. LADSPA_Handle const handle(it.getValue(nullptr));
  272. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  273. try {
  274. fDescriptor->cleanup(handle);
  275. } CARLA_SAFE_EXCEPTION("LADSPA cleanup");
  276. }
  277. }
  278. fHandles.clear();
  279. fDescriptor = nullptr;
  280. fDssiDescriptor = nullptr;
  281. }
  282. clearBuffers();
  283. }
  284. // -------------------------------------------------------------------
  285. // Information (base)
  286. PluginType getType() const noexcept override
  287. {
  288. return PLUGIN_DSSI;
  289. }
  290. PluginCategory getCategory() const noexcept override
  291. {
  292. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  293. if (pData->audioIn.count == 0 && pData->audioOut.count > 0 && fDssiDescriptor->run_synth != nullptr)
  294. return PLUGIN_CATEGORY_SYNTH;
  295. return CarlaPlugin::getCategory();
  296. }
  297. int64_t getUniqueId() const noexcept override
  298. {
  299. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  300. return static_cast<int64_t>(fDescriptor->UniqueID);
  301. }
  302. uint32_t getLatencyInFrames() const noexcept override
  303. {
  304. if (fLatencyIndex < 0 || fParamBuffers == nullptr)
  305. return 0;
  306. const float latency(fParamBuffers[fLatencyIndex]);
  307. CARLA_SAFE_ASSERT_RETURN(latency >= 0.0f, 0);
  308. return static_cast<uint32_t>(latency);
  309. }
  310. // -------------------------------------------------------------------
  311. // Information (count)
  312. // nothing
  313. // -------------------------------------------------------------------
  314. // Information (current data)
  315. std::size_t getChunkData(void** const dataPtr) noexcept override
  316. {
  317. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData, 0);
  318. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  319. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, 0);
  320. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->get_custom_data != nullptr, 0);
  321. CARLA_SAFE_ASSERT_RETURN(fHandles.count() > 0, 0);
  322. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  323. *dataPtr = nullptr;
  324. int ret = 0;
  325. ulong dataSize = 0;
  326. try {
  327. ret = fDssiDescriptor->get_custom_data(fHandles.getFirst(nullptr), dataPtr, &dataSize);
  328. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPluginDSSI::getChunkData", 0);
  329. return (ret != 0) ? dataSize : 0;
  330. }
  331. // -------------------------------------------------------------------
  332. // Information (per-plugin data)
  333. uint getOptionsAvailable() const noexcept override
  334. {
  335. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr, 0x0);
  336. uint options = 0x0;
  337. // can't disable fixed buffers if using latency
  338. if (fLatencyIndex == -1 && ! fNeedsFixedBuffers)
  339. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  340. // can't disable forced stereo if enabled in the engine
  341. if (pData->engine->getOptions().forceStereo)
  342. pass();
  343. // if inputs or outputs are just 1, then yes we can force stereo
  344. else if (pData->audioIn.count == 1 || pData->audioOut.count == 1 || fForcedStereoIn || fForcedStereoOut)
  345. options |= PLUGIN_OPTION_FORCE_STEREO;
  346. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  347. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  348. if (fUsesCustomData)
  349. options |= PLUGIN_OPTION_USE_CHUNKS;
  350. if (fDssiDescriptor->run_synth != nullptr)
  351. {
  352. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  353. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  354. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  355. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  356. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  357. }
  358. return options;
  359. }
  360. float getParameterValue(const uint32_t parameterId) const noexcept override
  361. {
  362. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  363. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  364. // bad plugins might have set output values out of bounds
  365. if (pData->param.data[parameterId].type == PARAMETER_OUTPUT)
  366. return pData->param.ranges[parameterId].getFixedValue(fParamBuffers[parameterId]);
  367. // not output, should be fine
  368. return fParamBuffers[parameterId];
  369. }
  370. void getLabel(char* const strBuf) const noexcept override
  371. {
  372. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  373. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Label != nullptr, nullStrBuf(strBuf));
  374. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  375. }
  376. void getMaker(char* const strBuf) const noexcept override
  377. {
  378. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  379. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Maker != nullptr, nullStrBuf(strBuf));
  380. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  381. }
  382. void getCopyright(char* const strBuf) const noexcept override
  383. {
  384. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  385. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Copyright != nullptr, nullStrBuf(strBuf));
  386. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  387. }
  388. void getRealName(char* const strBuf) const noexcept override
  389. {
  390. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  391. CARLA_SAFE_ASSERT_RETURN(fDescriptor->Name != nullptr, nullStrBuf(strBuf));
  392. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  393. }
  394. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  395. {
  396. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, nullStrBuf(strBuf));
  397. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  398. const int32_t rindex(pData->param.data[parameterId].rindex);
  399. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, nullStrBuf(strBuf));
  400. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fDescriptor->PortCount), nullStrBuf(strBuf));
  401. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames[rindex] != nullptr, nullStrBuf(strBuf));
  402. if (getSeparatedParameterNameOrUnit(fDescriptor->PortNames[rindex], strBuf, true))
  403. return;
  404. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  405. }
  406. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  407. {
  408. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, nullStrBuf(strBuf));
  409. const int32_t rindex(pData->param.data[parameterId].rindex);
  410. CARLA_SAFE_ASSERT_RETURN(rindex >= 0, nullStrBuf(strBuf));
  411. CARLA_SAFE_ASSERT_RETURN(rindex < static_cast<int32_t>(fDescriptor->PortCount), nullStrBuf(strBuf));
  412. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames[rindex] != nullptr, nullStrBuf(strBuf));
  413. if (getSeparatedParameterNameOrUnit(fDescriptor->PortNames[rindex], strBuf, false))
  414. return;
  415. nullStrBuf(strBuf);
  416. }
  417. // -------------------------------------------------------------------
  418. // Set data (state)
  419. // nothing
  420. // -------------------------------------------------------------------
  421. // Set data (internal stuff)
  422. void setId(const uint newId) noexcept override
  423. {
  424. CarlaPlugin::setId(newId);
  425. // UI osc-url uses Id, so we need to close it when it changes
  426. // FIXME - must be RT safe
  427. showCustomUI(false);
  428. }
  429. // -------------------------------------------------------------------
  430. // Set data (plugin-specific stuff)
  431. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  432. {
  433. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  434. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  435. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  436. fParamBuffers[parameterId] = fixedValue;
  437. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  438. }
  439. void setParameterValueRT(const uint32_t parameterId, const float value) noexcept override
  440. {
  441. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  442. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  443. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  444. fParamBuffers[parameterId] = fixedValue;
  445. CarlaPlugin::setParameterValueRT(parameterId, fixedValue);
  446. }
  447. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  448. {
  449. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  450. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  451. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  452. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  453. carla_debug("CarlaPluginDSSI::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  454. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  455. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  456. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0)
  457. return carla_stderr2("CarlaPluginDSSI::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string",
  458. type, key, value, bool2str(sendGui));
  459. if (fDssiDescriptor->configure != nullptr && fHandles.count() > 0)
  460. {
  461. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  462. {
  463. LADSPA_Handle const handle(it.getValue(nullptr));
  464. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  465. try {
  466. fDssiDescriptor->configure(handle, key, value);
  467. } CARLA_SAFE_EXCEPTION("DSSI setCustomData");
  468. }
  469. }
  470. #ifdef HAVE_LIBLO
  471. if (sendGui && fOscData.target != nullptr)
  472. osc_send_configure(fOscData, key, value);
  473. #endif
  474. if (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0)
  475. {
  476. const ScopedSingleProcessLocker spl(this, true);
  477. reloadPrograms(false);
  478. }
  479. CarlaPlugin::setCustomData(type, key, value, sendGui);
  480. }
  481. void setChunkData(const void* const data, const std::size_t dataSize) override
  482. {
  483. CARLA_SAFE_ASSERT_RETURN(fUsesCustomData,);
  484. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  485. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  486. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->set_custom_data != nullptr,);
  487. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  488. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  489. if (fHandles.count() > 0)
  490. {
  491. const ScopedSingleProcessLocker spl(this, true);
  492. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  493. {
  494. LADSPA_Handle const handle(it.getValue(nullptr));
  495. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  496. try {
  497. fDssiDescriptor->set_custom_data(handle, const_cast<void*>(data), static_cast<ulong>(dataSize));
  498. } CARLA_SAFE_EXCEPTION("CarlaPluginDSSI::setChunkData");
  499. }
  500. }
  501. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  502. const bool sendOsc(pData->engine->isOscControlRegistered());
  503. #else
  504. const bool sendOsc(false);
  505. #endif
  506. pData->updateParameterValues(this, sendOsc, true, false);
  507. }
  508. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  509. {
  510. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  511. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->select_program != nullptr,);
  512. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  513. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  514. if (index >= 0 && fHandles.count() > 0)
  515. {
  516. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  517. setMidiProgramInDSSI(static_cast<uint32_t>(index));
  518. }
  519. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  520. }
  521. void setMidiProgramRT(const uint32_t uindex) noexcept override
  522. {
  523. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  524. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor->select_program != nullptr,);
  525. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  526. setMidiProgramInDSSI(uindex);
  527. CarlaPlugin::setMidiProgramRT(uindex);
  528. }
  529. void setMidiProgramInDSSI(const uint32_t uindex) noexcept
  530. {
  531. const uint32_t bank(pData->midiprog.data[uindex].bank);
  532. const uint32_t program(pData->midiprog.data[uindex].program);
  533. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  534. {
  535. LADSPA_Handle const handle(it.getValue(nullptr));
  536. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  537. try {
  538. fDssiDescriptor->select_program(handle, bank, program);
  539. } CARLA_SAFE_EXCEPTION("DSSI setMidiProgram")
  540. }
  541. }
  542. #ifdef HAVE_LIBLO
  543. // -------------------------------------------------------------------
  544. // Set ui stuff
  545. void showCustomUI(const bool yesNo) override
  546. {
  547. if (yesNo)
  548. {
  549. fOscData.clear();
  550. fThreadUI.startThread();
  551. }
  552. else
  553. {
  554. #ifndef BUILD_BRIDGE
  555. pData->transientTryCounter = 0;
  556. #endif
  557. if (fOscData.target != nullptr)
  558. {
  559. osc_send_hide(fOscData);
  560. osc_send_quit(fOscData);
  561. fOscData.clear();
  562. }
  563. fThreadUI.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  564. }
  565. }
  566. #endif
  567. // -------------------------------------------------------------------
  568. // Plugin state
  569. void reload() override
  570. {
  571. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  572. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  573. CARLA_SAFE_ASSERT_RETURN(fDssiDescriptor != nullptr,);
  574. CARLA_SAFE_ASSERT_RETURN(fHandles.count() > 0,);
  575. carla_debug("CarlaPluginDSSI::reload() - start");
  576. const EngineProcessMode processMode(pData->engine->getProccessMode());
  577. // Safely disable plugin for reload
  578. const ScopedDisabler sd(this);
  579. if (pData->active)
  580. deactivate();
  581. clearBuffers();
  582. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  583. const uint32_t portCount(getSafePortCount());
  584. uint32_t aIns, aOuts, mIns, params;
  585. aIns = aOuts = mIns = params = 0;
  586. bool forcedStereoIn, forcedStereoOut;
  587. forcedStereoIn = forcedStereoOut = false;
  588. bool needsCtrlIn, needsCtrlOut;
  589. needsCtrlIn = needsCtrlOut = false;
  590. for (uint32_t i=0; i < portCount; ++i)
  591. {
  592. const LADSPA_PortDescriptor portType(fDescriptor->PortDescriptors[i]);
  593. if (LADSPA_IS_PORT_AUDIO(portType))
  594. {
  595. if (LADSPA_IS_PORT_INPUT(portType))
  596. aIns += 1;
  597. else if (LADSPA_IS_PORT_OUTPUT(portType))
  598. aOuts += 1;
  599. }
  600. else if (LADSPA_IS_PORT_CONTROL(portType))
  601. params += 1;
  602. }
  603. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  604. {
  605. if ((aIns == 1 || aOuts == 1) && fHandles.count() == 1 && addInstance())
  606. {
  607. if (aIns == 1)
  608. {
  609. aIns = 2;
  610. forcedStereoIn = true;
  611. }
  612. if (aOuts == 1)
  613. {
  614. aOuts = 2;
  615. forcedStereoOut = true;
  616. }
  617. }
  618. }
  619. if (fDssiDescriptor->run_synth != nullptr)
  620. {
  621. mIns = 1;
  622. needsCtrlIn = true;
  623. }
  624. if (aIns > 0)
  625. {
  626. pData->audioIn.createNew(aIns);
  627. fAudioInBuffers = new float*[aIns];
  628. for (uint32_t i=0; i < aIns; ++i)
  629. fAudioInBuffers[i] = nullptr;
  630. }
  631. if (aOuts > 0)
  632. {
  633. pData->audioOut.createNew(aOuts);
  634. fAudioOutBuffers = new float*[aOuts];
  635. needsCtrlIn = true;
  636. for (uint32_t i=0; i < aOuts; ++i)
  637. fAudioOutBuffers[i] = nullptr;
  638. }
  639. if (params > 0)
  640. {
  641. pData->param.createNew(params, true);
  642. fParamBuffers = new float[params];
  643. carla_zeroFloats(fParamBuffers, params);
  644. }
  645. const uint portNameSize(pData->engine->getMaxPortNameSize());
  646. CarlaString portName;
  647. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  648. {
  649. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  650. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  651. if (LADSPA_IS_PORT_AUDIO(portType))
  652. {
  653. portName.clear();
  654. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  655. {
  656. portName = pData->name;
  657. portName += ":";
  658. }
  659. if (fDescriptor->PortNames[i] != nullptr && fDescriptor->PortNames[i][0] != '\0')
  660. {
  661. portName += fDescriptor->PortNames[i];
  662. }
  663. else
  664. {
  665. if (LADSPA_IS_PORT_INPUT(portType))
  666. {
  667. if (aIns > 1)
  668. {
  669. portName += "audio-in_";
  670. portName += CarlaString(iAudioIn+1);
  671. }
  672. else
  673. portName += "audio-in";
  674. }
  675. else
  676. {
  677. if (aOuts > 1)
  678. {
  679. portName += "audio-out_";
  680. portName += CarlaString(iAudioOut+1);
  681. }
  682. else
  683. portName += "audio-out";
  684. }
  685. }
  686. portName.truncate(portNameSize);
  687. if (LADSPA_IS_PORT_INPUT(portType))
  688. {
  689. const uint32_t j = iAudioIn++;
  690. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  691. pData->audioIn.ports[j].rindex = i;
  692. if (forcedStereoIn)
  693. {
  694. portName += "_2";
  695. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  696. pData->audioIn.ports[1].rindex = i;
  697. }
  698. }
  699. else if (LADSPA_IS_PORT_OUTPUT(portType))
  700. {
  701. const uint32_t j = iAudioOut++;
  702. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  703. pData->audioOut.ports[j].rindex = i;
  704. if (forcedStereoOut)
  705. {
  706. portName += "_2";
  707. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  708. pData->audioOut.ports[1].rindex = i;
  709. }
  710. }
  711. else
  712. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  713. }
  714. else if (LADSPA_IS_PORT_CONTROL(portType))
  715. {
  716. const uint32_t j = iCtrl++;
  717. pData->param.data[j].index = static_cast<int32_t>(j);
  718. pData->param.data[j].rindex = static_cast<int32_t>(i);
  719. const char* const paramName(fDescriptor->PortNames[i] != nullptr ? fDescriptor->PortNames[i] : "unknown");
  720. float min, max, def, step, stepSmall, stepLarge;
  721. // min value
  722. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  723. min = portRangeHints.LowerBound;
  724. else
  725. min = 0.0f;
  726. // max value
  727. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  728. max = portRangeHints.UpperBound;
  729. else
  730. max = 1.0f;
  731. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  732. {
  733. min *= sampleRate;
  734. max *= sampleRate;
  735. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  736. }
  737. if (min >= max)
  738. {
  739. carla_stderr2("WARNING - Broken plugin parameter '%s': min >= max", paramName);
  740. max = min + 0.1f;
  741. }
  742. // default value
  743. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  744. if (def < min)
  745. def = min;
  746. else if (def > max)
  747. def = max;
  748. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  749. {
  750. step = max - min;
  751. stepSmall = step;
  752. stepLarge = step;
  753. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  754. }
  755. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  756. {
  757. step = 1.0f;
  758. stepSmall = 1.0f;
  759. stepLarge = 10.0f;
  760. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  761. }
  762. else
  763. {
  764. const float range = max - min;
  765. step = range/100.0f;
  766. stepSmall = range/1000.0f;
  767. stepLarge = range/10.0f;
  768. }
  769. if (LADSPA_IS_PORT_INPUT(portType))
  770. {
  771. pData->param.data[j].type = PARAMETER_INPUT;
  772. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  773. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  774. needsCtrlIn = true;
  775. // MIDI CC value
  776. if (fDssiDescriptor->get_midi_controller_for_port != nullptr)
  777. {
  778. const int ctrl = fDssiDescriptor->get_midi_controller_for_port(fHandles.getFirst(nullptr), i);
  779. if (DSSI_CONTROLLER_IS_SET(ctrl) && DSSI_IS_CC(ctrl))
  780. {
  781. const int16_t cc = DSSI_CC_NUMBER(ctrl);
  782. if (! MIDI_IS_CONTROL_BANK_SELECT(cc))
  783. pData->param.data[j].midiCC = cc;
  784. }
  785. }
  786. }
  787. else if (LADSPA_IS_PORT_OUTPUT(portType))
  788. {
  789. pData->param.data[j].type = PARAMETER_OUTPUT;
  790. if (std::strcmp(paramName, "latency") == 0 || std::strcmp(paramName, "_latency") == 0)
  791. {
  792. min = 0.0f;
  793. max = sampleRate;
  794. def = 0.0f;
  795. step = 1.0f;
  796. stepSmall = 1.0f;
  797. stepLarge = 1.0f;
  798. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  799. CARLA_SAFE_ASSERT_INT2(fLatencyIndex == static_cast<int32_t>(j), fLatencyIndex, j);
  800. }
  801. else
  802. {
  803. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  804. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  805. needsCtrlOut = true;
  806. }
  807. }
  808. else
  809. {
  810. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  811. }
  812. // extra parameter hints
  813. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  814. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  815. pData->param.ranges[j].min = min;
  816. pData->param.ranges[j].max = max;
  817. pData->param.ranges[j].def = def;
  818. pData->param.ranges[j].step = step;
  819. pData->param.ranges[j].stepSmall = stepSmall;
  820. pData->param.ranges[j].stepLarge = stepLarge;
  821. // Start parameters in their default values
  822. fParamBuffers[j] = def;
  823. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  824. {
  825. LADSPA_Handle const handle(it.getValue(nullptr));
  826. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  827. try {
  828. fDescriptor->connect_port(handle, i, &fParamBuffers[j]);
  829. } CARLA_SAFE_EXCEPTION("DSSI connect_port (parameter)");
  830. }
  831. }
  832. else
  833. {
  834. // Not Audio or Control
  835. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  836. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  837. {
  838. LADSPA_Handle const handle(it.getValue(nullptr));
  839. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  840. try {
  841. fDescriptor->connect_port(handle, i, nullptr);
  842. } CARLA_SAFE_EXCEPTION("DSSI connect_port (null)");
  843. }
  844. }
  845. }
  846. if (needsCtrlIn)
  847. {
  848. portName.clear();
  849. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  850. {
  851. portName = pData->name;
  852. portName += ":";
  853. }
  854. portName += "events-in";
  855. portName.truncate(portNameSize);
  856. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  857. }
  858. if (needsCtrlOut)
  859. {
  860. portName.clear();
  861. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  862. {
  863. portName = pData->name;
  864. portName += ":";
  865. }
  866. portName += "events-out";
  867. portName.truncate(portNameSize);
  868. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  869. }
  870. if (forcedStereoIn || forcedStereoOut)
  871. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  872. else
  873. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  874. // plugin hints
  875. pData->hints = 0x0;
  876. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  877. pData->hints |= PLUGIN_IS_RTSAFE;
  878. #ifdef HAVE_LIBLO
  879. if (fUiFilename != nullptr)
  880. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  881. #endif
  882. #ifndef BUILD_BRIDGE
  883. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  884. pData->hints |= PLUGIN_CAN_DRYWET;
  885. if (aOuts > 0)
  886. pData->hints |= PLUGIN_CAN_VOLUME;
  887. if (aOuts >= 2 && aOuts % 2 == 0)
  888. pData->hints |= PLUGIN_CAN_BALANCE;
  889. #endif
  890. // extra plugin hints
  891. pData->extraHints = 0x0;
  892. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  893. if (mIns > 0)
  894. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  895. // check initial latency
  896. findInitialLatencyValue(aIns, aOuts);
  897. fForcedStereoIn = forcedStereoIn;
  898. fForcedStereoOut = forcedStereoOut;
  899. bufferSizeChanged(pData->engine->getBufferSize());
  900. reloadPrograms(true);
  901. if (pData->active)
  902. activate();
  903. carla_debug("CarlaPluginDSSI::reload() - end");
  904. }
  905. void findInitialLatencyValue(const uint32_t aIns, const uint32_t aOuts) const
  906. {
  907. if (fLatencyIndex < 0 || fHandles.count() == 0)
  908. return;
  909. // we need to pre-run the plugin so it can update its latency control-port
  910. const LADSPA_Handle handle(fHandles.getFirst(nullptr));
  911. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  912. float tmpIn [(aIns > 0) ? aIns : 1][2];
  913. float tmpOut[(aOuts > 0) ? aOuts : 1][2];
  914. for (uint32_t j=0; j < aIns; ++j)
  915. {
  916. tmpIn[j][0] = 0.0f;
  917. tmpIn[j][1] = 0.0f;
  918. try {
  919. fDescriptor->connect_port(handle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  920. } CARLA_SAFE_EXCEPTION("DSSI connect_port (latency input)");
  921. }
  922. for (uint32_t j=0; j < aOuts; ++j)
  923. {
  924. tmpOut[j][0] = 0.0f;
  925. tmpOut[j][1] = 0.0f;
  926. try {
  927. fDescriptor->connect_port(handle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  928. } CARLA_SAFE_EXCEPTION("DSSI connect_port (latency output)");
  929. }
  930. if (fDescriptor->activate != nullptr)
  931. {
  932. try {
  933. fDescriptor->activate(handle);
  934. } CARLA_SAFE_EXCEPTION("DSSI latency activate");
  935. }
  936. try {
  937. fDescriptor->run(handle, 2);
  938. } CARLA_SAFE_EXCEPTION("DSSI latency run");
  939. if (fDescriptor->deactivate != nullptr)
  940. {
  941. try {
  942. fDescriptor->deactivate(handle);
  943. } CARLA_SAFE_EXCEPTION("DSSI latency deactivate");
  944. }
  945. // done, let's get the value
  946. if (const uint32_t latency = getLatencyInFrames())
  947. {
  948. pData->client->setLatency(latency);
  949. #ifndef BUILD_BRIDGE
  950. pData->latency.recreateBuffers(std::max(aIns, aOuts), latency);
  951. #endif
  952. }
  953. }
  954. void reloadPrograms(const bool doInit) override
  955. {
  956. carla_debug("CarlaPluginDSSI::reloadPrograms(%s)", bool2str(doInit));
  957. const LADSPA_Handle handle(fHandles.getFirst(nullptr));
  958. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  959. const uint32_t oldCount = pData->midiprog.count;
  960. const int32_t current = pData->midiprog.current;
  961. // Delete old programs
  962. pData->midiprog.clear();
  963. // Query new programs
  964. uint32_t newCount = 0;
  965. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  966. {
  967. for (; fDssiDescriptor->get_program(handle, newCount) != nullptr;)
  968. ++newCount;
  969. }
  970. if (newCount > 0)
  971. {
  972. pData->midiprog.createNew(newCount);
  973. // Update data
  974. for (uint32_t i=0; i < newCount; ++i)
  975. {
  976. const DSSI_Program_Descriptor* const pdesc(fDssiDescriptor->get_program(handle, i));
  977. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  978. CARLA_SAFE_ASSERT(pdesc->Name != nullptr);
  979. pData->midiprog.data[i].bank = static_cast<uint32_t>(pdesc->Bank);
  980. pData->midiprog.data[i].program = static_cast<uint32_t>(pdesc->Program);
  981. pData->midiprog.data[i].name = carla_strdup(pdesc->Name);
  982. }
  983. }
  984. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  985. // Update OSC Names
  986. if (pData->engine->isOscControlRegistered() && pData->id < pData->engine->getCurrentPluginCount())
  987. {
  988. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  989. for (uint32_t i=0; i < newCount; ++i)
  990. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  991. }
  992. #endif
  993. if (doInit)
  994. {
  995. if (newCount > 0)
  996. setMidiProgram(0, false, false, false, true);
  997. }
  998. else
  999. {
  1000. // Check if current program is invalid
  1001. bool programChanged = false;
  1002. if (newCount == oldCount+1)
  1003. {
  1004. // one midi program added, probably created by user
  1005. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1006. programChanged = true;
  1007. }
  1008. else if (current < 0 && newCount > 0)
  1009. {
  1010. // programs exist now, but not before
  1011. pData->midiprog.current = 0;
  1012. programChanged = true;
  1013. }
  1014. else if (current >= 0 && newCount == 0)
  1015. {
  1016. // programs existed before, but not anymore
  1017. pData->midiprog.current = -1;
  1018. programChanged = true;
  1019. }
  1020. else if (current >= static_cast<int32_t>(newCount))
  1021. {
  1022. // current midi program > count
  1023. pData->midiprog.current = 0;
  1024. programChanged = true;
  1025. }
  1026. else
  1027. {
  1028. // no change
  1029. pData->midiprog.current = current;
  1030. }
  1031. if (programChanged)
  1032. setMidiProgram(pData->midiprog.current, true, true, true, false);
  1033. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  1034. }
  1035. }
  1036. // -------------------------------------------------------------------
  1037. // Plugin processing
  1038. void activate() noexcept override
  1039. {
  1040. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1041. if (fDescriptor->activate != nullptr)
  1042. {
  1043. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1044. {
  1045. LADSPA_Handle const handle(it.getValue(nullptr));
  1046. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1047. try {
  1048. fDescriptor->activate(handle);
  1049. } CARLA_SAFE_EXCEPTION("DSSI activate");
  1050. }
  1051. }
  1052. }
  1053. void deactivate() noexcept override
  1054. {
  1055. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1056. if (fDescriptor->deactivate != nullptr)
  1057. {
  1058. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1059. {
  1060. LADSPA_Handle const handle(it.getValue(nullptr));
  1061. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1062. try {
  1063. fDescriptor->deactivate(handle);
  1064. } CARLA_SAFE_EXCEPTION("DSSI deactivate");
  1065. }
  1066. }
  1067. }
  1068. void process(const float** const audioIn, float** const audioOut, const float** const, float** const, const uint32_t frames) override
  1069. {
  1070. // --------------------------------------------------------------------------------------------------------
  1071. // Check if active
  1072. if (! pData->active)
  1073. {
  1074. // disable any output sound
  1075. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1076. carla_zeroFloats(audioOut[i], frames);
  1077. return;
  1078. }
  1079. ulong midiEventCount = 0;
  1080. carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents);
  1081. // --------------------------------------------------------------------------------------------------------
  1082. // Check if needs reset
  1083. if (pData->needsReset)
  1084. {
  1085. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1086. {
  1087. midiEventCount = MAX_MIDI_CHANNELS*2;
  1088. for (uchar i=0, k=MAX_MIDI_CHANNELS; i < MAX_MIDI_CHANNELS; ++i)
  1089. {
  1090. fMidiEvents[i].type = SND_SEQ_EVENT_CONTROLLER;
  1091. fMidiEvents[i].data.control.channel = i;
  1092. fMidiEvents[i].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1093. fMidiEvents[k+i].type = SND_SEQ_EVENT_CONTROLLER;
  1094. fMidiEvents[k+i].data.control.channel = i;
  1095. fMidiEvents[k+i].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1096. }
  1097. }
  1098. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1099. {
  1100. midiEventCount = MAX_MIDI_NOTE;
  1101. for (uchar i=0; i < MAX_MIDI_NOTE; ++i)
  1102. {
  1103. fMidiEvents[i].type = SND_SEQ_EVENT_NOTEOFF;
  1104. fMidiEvents[i].data.note.channel = static_cast<uchar>(pData->ctrlChannel);
  1105. fMidiEvents[i].data.note.note = i;
  1106. }
  1107. }
  1108. pData->needsReset = false;
  1109. }
  1110. // --------------------------------------------------------------------------------------------------------
  1111. // Event Input and Processing
  1112. if (pData->event.portIn != nullptr)
  1113. {
  1114. // ----------------------------------------------------------------------------------------------------
  1115. // MIDI Input (External)
  1116. if (pData->extNotes.mutex.tryLock())
  1117. {
  1118. ExternalMidiNote note = { 0, 0, 0 };
  1119. for (; midiEventCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  1120. {
  1121. note = pData->extNotes.data.getFirst(note, true);
  1122. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1123. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1124. seqEvent.type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  1125. seqEvent.data.note.channel = static_cast<uchar>(note.channel);
  1126. seqEvent.data.note.note = note.note;
  1127. seqEvent.data.note.velocity = note.velo;
  1128. }
  1129. pData->extNotes.mutex.unlock();
  1130. } // End of MIDI Input (External)
  1131. // ----------------------------------------------------------------------------------------------------
  1132. // Event Input (System)
  1133. #ifndef BUILD_BRIDGE
  1134. bool allNotesOffSent = false;
  1135. #endif
  1136. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1137. uint32_t startTime = 0;
  1138. uint32_t timeOffset = 0;
  1139. uint32_t nextBankId;
  1140. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1141. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1142. else
  1143. nextBankId = 0;
  1144. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  1145. {
  1146. const EngineEvent& event(pData->event.portIn->getEvent(i));
  1147. if (event.time >= frames)
  1148. continue;
  1149. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  1150. if (isSampleAccurate && event.time > timeOffset)
  1151. {
  1152. if (processSingle(audioIn, audioOut, event.time - timeOffset, timeOffset, midiEventCount))
  1153. {
  1154. startTime = 0;
  1155. timeOffset = event.time;
  1156. midiEventCount = 0;
  1157. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1158. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1159. else
  1160. nextBankId = 0;
  1161. }
  1162. else
  1163. startTime += timeOffset;
  1164. }
  1165. switch (event.type)
  1166. {
  1167. case kEngineEventTypeNull:
  1168. break;
  1169. case kEngineEventTypeControl: {
  1170. const EngineControlEvent& ctrlEvent(event.ctrl);
  1171. switch (ctrlEvent.type)
  1172. {
  1173. case kEngineControlEventTypeNull:
  1174. break;
  1175. case kEngineControlEventTypeParameter: {
  1176. #ifndef BUILD_BRIDGE
  1177. // Control backend stuff
  1178. if (event.channel == pData->ctrlChannel)
  1179. {
  1180. float value;
  1181. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  1182. {
  1183. value = ctrlEvent.value;
  1184. setDryWetRT(value);
  1185. }
  1186. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  1187. {
  1188. value = ctrlEvent.value*127.0f/100.0f;
  1189. setVolumeRT(value);
  1190. }
  1191. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  1192. {
  1193. float left, right;
  1194. value = ctrlEvent.value/0.5f - 1.0f;
  1195. if (value < 0.0f)
  1196. {
  1197. left = -1.0f;
  1198. right = (value*2.0f)+1.0f;
  1199. }
  1200. else if (value > 0.0f)
  1201. {
  1202. left = (value*2.0f)-1.0f;
  1203. right = 1.0f;
  1204. }
  1205. else
  1206. {
  1207. left = -1.0f;
  1208. right = 1.0f;
  1209. }
  1210. setBalanceLeftRT(left);
  1211. setBalanceRightRT(right);
  1212. }
  1213. }
  1214. #endif
  1215. // Control plugin parameters
  1216. for (uint32_t k=0; k < pData->param.count; ++k)
  1217. {
  1218. if (pData->param.data[k].midiChannel != event.channel)
  1219. continue;
  1220. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1221. continue;
  1222. if (pData->param.data[k].type != PARAMETER_INPUT)
  1223. continue;
  1224. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1225. continue;
  1226. float value;
  1227. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1228. {
  1229. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1230. }
  1231. else
  1232. {
  1233. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1234. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1235. else
  1236. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1237. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1238. value = std::rint(value);
  1239. }
  1240. setParameterValueRT(k, value);
  1241. }
  1242. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1243. {
  1244. if (midiEventCount >= kPluginMaxMidiEvents)
  1245. continue;
  1246. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1247. seqEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1248. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1249. seqEvent.data.control.channel = event.channel;
  1250. seqEvent.data.control.param = ctrlEvent.param;
  1251. seqEvent.data.control.value = int8_t(ctrlEvent.value*127.0f);
  1252. }
  1253. break;
  1254. } // case kEngineControlEventTypeParameter
  1255. case kEngineControlEventTypeMidiBank:
  1256. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1257. nextBankId = ctrlEvent.param;
  1258. break;
  1259. case kEngineControlEventTypeMidiProgram:
  1260. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1261. {
  1262. const uint32_t nextProgramId = ctrlEvent.param;
  1263. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1264. {
  1265. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1266. {
  1267. setMidiProgramRT(k);
  1268. break;
  1269. }
  1270. }
  1271. }
  1272. break;
  1273. case kEngineControlEventTypeAllSoundOff:
  1274. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1275. {
  1276. if (midiEventCount >= kPluginMaxMidiEvents)
  1277. continue;
  1278. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1279. seqEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1280. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1281. seqEvent.data.control.channel = event.channel;
  1282. seqEvent.data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1283. }
  1284. break;
  1285. case kEngineControlEventTypeAllNotesOff:
  1286. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1287. {
  1288. #ifndef BUILD_BRIDGE
  1289. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1290. {
  1291. allNotesOffSent = true;
  1292. sendMidiAllNotesOffToCallback();
  1293. }
  1294. #endif
  1295. if (midiEventCount >= kPluginMaxMidiEvents)
  1296. continue;
  1297. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1298. seqEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1299. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1300. seqEvent.data.control.channel = event.channel;
  1301. seqEvent.data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1302. }
  1303. break;
  1304. } // switch (ctrlEvent.type)
  1305. break;
  1306. } // case kEngineEventTypeControl
  1307. case kEngineEventTypeMidi: {
  1308. if (midiEventCount >= kPluginMaxMidiEvents)
  1309. continue;
  1310. const EngineMidiEvent& midiEvent(event.midi);
  1311. if (midiEvent.size > EngineMidiEvent::kDataSize)
  1312. continue;
  1313. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1314. // Fix bad note-off (per DSSI spec)
  1315. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1316. status = MIDI_STATUS_NOTE_OFF;
  1317. snd_seq_event_t& seqEvent(fMidiEvents[midiEventCount++]);
  1318. seqEvent.time.tick = isSampleAccurate ? startTime : event.time;
  1319. switch (status)
  1320. {
  1321. case MIDI_STATUS_NOTE_OFF: {
  1322. const uint8_t note = midiEvent.data[1];
  1323. seqEvent.type = SND_SEQ_EVENT_NOTEOFF;
  1324. seqEvent.data.note.channel = event.channel;
  1325. seqEvent.data.note.note = note;
  1326. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, note, 0.0f);
  1327. break;
  1328. }
  1329. case MIDI_STATUS_NOTE_ON: {
  1330. const uint8_t note = midiEvent.data[1];
  1331. const uint8_t velo = midiEvent.data[2];
  1332. seqEvent.type = SND_SEQ_EVENT_NOTEON;
  1333. seqEvent.data.note.channel = event.channel;
  1334. seqEvent.data.note.note = note;
  1335. seqEvent.data.note.velocity = velo;
  1336. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, note, velo);
  1337. break;
  1338. }
  1339. case MIDI_STATUS_POLYPHONIC_AFTERTOUCH:
  1340. if (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH)
  1341. {
  1342. const uint8_t note = midiEvent.data[1];
  1343. const uint8_t pressure = midiEvent.data[2];
  1344. seqEvent.type = SND_SEQ_EVENT_KEYPRESS;
  1345. seqEvent.data.note.channel = event.channel;
  1346. seqEvent.data.note.note = note;
  1347. seqEvent.data.note.velocity = pressure;
  1348. }
  1349. break;
  1350. case MIDI_STATUS_CONTROL_CHANGE:
  1351. if (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1352. {
  1353. const uint8_t control = midiEvent.data[1];
  1354. const uint8_t value = midiEvent.data[2];
  1355. seqEvent.type = SND_SEQ_EVENT_CONTROLLER;
  1356. seqEvent.data.control.channel = event.channel;
  1357. seqEvent.data.control.param = control;
  1358. seqEvent.data.control.value = value;
  1359. }
  1360. break;
  1361. case MIDI_STATUS_CHANNEL_PRESSURE:
  1362. if (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE)
  1363. {
  1364. const uint8_t pressure = midiEvent.data[1];
  1365. seqEvent.type = SND_SEQ_EVENT_CHANPRESS;
  1366. seqEvent.data.control.channel = event.channel;
  1367. seqEvent.data.control.value = pressure;
  1368. }
  1369. break;
  1370. case MIDI_STATUS_PITCH_WHEEL_CONTROL:
  1371. if (pData->options & PLUGIN_OPTION_SEND_PITCHBEND)
  1372. {
  1373. const uint8_t lsb = midiEvent.data[1];
  1374. const uint8_t msb = midiEvent.data[2];
  1375. seqEvent.type = SND_SEQ_EVENT_PITCHBEND;
  1376. seqEvent.data.control.channel = event.channel;
  1377. seqEvent.data.control.value = ((msb << 7) | lsb) - 8192;
  1378. }
  1379. break;
  1380. default:
  1381. --midiEventCount;
  1382. break;
  1383. } // switch (status)
  1384. } break;
  1385. } // switch (event.type)
  1386. }
  1387. pData->postRtEvents.trySplice();
  1388. if (frames > timeOffset)
  1389. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset, midiEventCount);
  1390. } // End of Event Input and Processing
  1391. // --------------------------------------------------------------------------------------------------------
  1392. // Plugin processing (no events)
  1393. else
  1394. {
  1395. processSingle(audioIn, audioOut, frames, 0, midiEventCount);
  1396. } // End of Plugin processing (no events)
  1397. // --------------------------------------------------------------------------------------------------------
  1398. // Control Output
  1399. if (pData->event.portOut != nullptr)
  1400. {
  1401. uint8_t channel;
  1402. uint16_t param;
  1403. float value;
  1404. for (uint32_t k=0; k < pData->param.count; ++k)
  1405. {
  1406. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1407. continue;
  1408. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  1409. if (pData->param.data[k].midiCC > 0)
  1410. {
  1411. channel = pData->param.data[k].midiChannel;
  1412. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  1413. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  1414. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  1415. }
  1416. }
  1417. } // End of Control Output
  1418. }
  1419. bool processSingle(const float** const audioIn, float** const audioOut, const uint32_t frames,
  1420. const uint32_t timeOffset, const ulong midiEventCount)
  1421. {
  1422. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1423. if (pData->audioIn.count > 0)
  1424. {
  1425. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1426. }
  1427. if (pData->audioOut.count > 0)
  1428. {
  1429. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1430. }
  1431. // --------------------------------------------------------------------------------------------------------
  1432. // Try lock, silence otherwise
  1433. #ifndef STOAT_TEST_BUILD
  1434. if (pData->engine->isOffline())
  1435. {
  1436. pData->singleMutex.lock();
  1437. }
  1438. else
  1439. #endif
  1440. if (! pData->singleMutex.tryLock())
  1441. {
  1442. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1443. {
  1444. for (uint32_t k=0; k < frames; ++k)
  1445. audioOut[i][k+timeOffset] = 0.0f;
  1446. }
  1447. return false;
  1448. }
  1449. // --------------------------------------------------------------------------------------------------------
  1450. // Set audio buffers
  1451. const bool customMonoOut = pData->audioOut.count == 2 && fForcedStereoOut && ! fForcedStereoIn;
  1452. const bool customStereoOut = pData->audioOut.count == 2 && fForcedStereoIn && ! fForcedStereoOut;
  1453. if (! customMonoOut)
  1454. {
  1455. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1456. carla_zeroFloats(fAudioOutBuffers[i], frames);
  1457. }
  1458. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1459. carla_copyFloats(fAudioInBuffers[i], audioIn[i]+timeOffset, frames);
  1460. // --------------------------------------------------------------------------------------------------------
  1461. // Run plugin
  1462. uint instn = 0;
  1463. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next(), ++instn)
  1464. {
  1465. LADSPA_Handle const handle(it.getValue(nullptr));
  1466. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1467. // ----------------------------------------------------------------------------------------------------
  1468. // Mixdown for forced stereo
  1469. if (customMonoOut)
  1470. carla_zeroFloats(fAudioOutBuffers[instn], frames);
  1471. // ----------------------------------------------------------------------------------------------------
  1472. // Run it
  1473. if (fDssiDescriptor->run_synth != nullptr)
  1474. {
  1475. try {
  1476. fDssiDescriptor->run_synth(handle, frames, fMidiEvents, midiEventCount);
  1477. } CARLA_SAFE_EXCEPTION("DSSI run_synth");
  1478. }
  1479. else
  1480. {
  1481. try {
  1482. fDescriptor->run(handle, frames);
  1483. } CARLA_SAFE_EXCEPTION("DSSI run");
  1484. }
  1485. // ----------------------------------------------------------------------------------------------------
  1486. // Mixdown for forced stereo
  1487. if (customMonoOut)
  1488. carla_multiply(fAudioOutBuffers[instn], 0.5f, frames);
  1489. else if (customStereoOut)
  1490. carla_copyFloats(fExtraStereoBuffer[instn], fAudioOutBuffers[instn], frames);
  1491. }
  1492. if (customStereoOut)
  1493. {
  1494. carla_copyFloats(fAudioOutBuffers[0], fExtraStereoBuffer[0], frames);
  1495. carla_copyFloats(fAudioOutBuffers[1], fExtraStereoBuffer[1], frames);
  1496. }
  1497. #ifndef BUILD_BRIDGE
  1498. // --------------------------------------------------------------------------------------------------------
  1499. // Post-processing (dry/wet, volume and balance)
  1500. {
  1501. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1502. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1503. const bool isMono = (pData->audioIn.count == 1);
  1504. bool isPair;
  1505. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1506. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1507. {
  1508. // Dry/Wet
  1509. if (doDryWet)
  1510. {
  1511. const uint32_t c = isMono ? 0 : i;
  1512. for (uint32_t k=0; k < frames; ++k)
  1513. {
  1514. if (k < pData->latency.frames)
  1515. bufValue = pData->latency.buffers[c][k];
  1516. else if (pData->latency.frames < frames)
  1517. bufValue = fAudioInBuffers[c][k-pData->latency.frames];
  1518. else
  1519. bufValue = fAudioInBuffers[c][k];
  1520. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1521. }
  1522. }
  1523. // Balance
  1524. if (doBalance)
  1525. {
  1526. isPair = (i % 2 == 0);
  1527. if (isPair)
  1528. {
  1529. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1530. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  1531. }
  1532. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1533. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1534. for (uint32_t k=0; k < frames; ++k)
  1535. {
  1536. if (isPair)
  1537. {
  1538. // left
  1539. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1540. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1541. }
  1542. else
  1543. {
  1544. // right
  1545. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1546. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1547. }
  1548. }
  1549. }
  1550. // Volume (and buffer copy)
  1551. {
  1552. for (uint32_t k=0; k < frames; ++k)
  1553. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1554. }
  1555. }
  1556. } // End of Post-processing
  1557. // --------------------------------------------------------------------------------------------------------
  1558. // Save latency values for next callback
  1559. if (const uint32_t latframes = pData->latency.frames)
  1560. {
  1561. CARLA_SAFE_ASSERT(timeOffset == 0);
  1562. if (latframes <= frames)
  1563. {
  1564. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1565. carla_copyFloats(pData->latency.buffers[i], audioIn[i]+(frames-latframes), latframes);
  1566. }
  1567. else
  1568. {
  1569. const uint32_t diff = pData->latency.frames-frames;
  1570. for (uint32_t i=0, k; i<pData->audioIn.count; ++i)
  1571. {
  1572. // push back buffer by 'frames'
  1573. for (k=0; k < diff; ++k)
  1574. pData->latency.buffers[i][k] = pData->latency.buffers[i][k+frames];
  1575. // put current input at the end
  1576. for (uint32_t j=0; k < latframes; ++j, ++k)
  1577. pData->latency.buffers[i][k] = audioIn[i][j];
  1578. }
  1579. }
  1580. }
  1581. #else // BUILD_BRIDGE
  1582. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1583. {
  1584. for (uint32_t k=0; k < frames; ++k)
  1585. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1586. }
  1587. #endif
  1588. // --------------------------------------------------------------------------------------------------------
  1589. pData->singleMutex.unlock();
  1590. return true;
  1591. }
  1592. void bufferSizeChanged(const uint32_t newBufferSize) override
  1593. {
  1594. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1595. carla_debug("CarlaPluginDSSI::bufferSizeChanged(%i) - start", newBufferSize);
  1596. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1597. {
  1598. if (fAudioInBuffers[i] != nullptr)
  1599. delete[] fAudioInBuffers[i];
  1600. fAudioInBuffers[i] = new float[newBufferSize];
  1601. carla_zeroFloats(fAudioInBuffers[i], newBufferSize);
  1602. }
  1603. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1604. {
  1605. if (fAudioOutBuffers[i] != nullptr)
  1606. delete[] fAudioOutBuffers[i];
  1607. fAudioOutBuffers[i] = new float[newBufferSize];
  1608. carla_zeroFloats(fAudioOutBuffers[i], newBufferSize);
  1609. }
  1610. if (fExtraStereoBuffer[0] != nullptr)
  1611. {
  1612. delete[] fExtraStereoBuffer[0];
  1613. fExtraStereoBuffer[0] = nullptr;
  1614. }
  1615. if (fExtraStereoBuffer[1] != nullptr)
  1616. {
  1617. delete[] fExtraStereoBuffer[1];
  1618. fExtraStereoBuffer[1] = nullptr;
  1619. }
  1620. if (fForcedStereoIn && pData->audioOut.count == 2)
  1621. {
  1622. fExtraStereoBuffer[0] = new float[newBufferSize];
  1623. fExtraStereoBuffer[1] = new float[newBufferSize];
  1624. carla_zeroFloats(fExtraStereoBuffer[0], newBufferSize);
  1625. carla_zeroFloats(fExtraStereoBuffer[1], newBufferSize);
  1626. }
  1627. reconnectAudioPorts();
  1628. carla_debug("CarlaPluginDSSI::bufferSizeChanged(%i) - end", newBufferSize);
  1629. }
  1630. void sampleRateChanged(const double newSampleRate) override
  1631. {
  1632. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1633. carla_debug("CarlaPluginDSSI::sampleRateChanged(%g) - start", newSampleRate);
  1634. // TODO - handle UI stuff
  1635. if (pData->active)
  1636. deactivate();
  1637. const std::size_t instanceCount(fHandles.count());
  1638. if (fDescriptor->cleanup != nullptr)
  1639. {
  1640. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1641. {
  1642. LADSPA_Handle const handle(it.getValue(nullptr));
  1643. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1644. try {
  1645. fDescriptor->cleanup(handle);
  1646. } CARLA_SAFE_EXCEPTION("LADSPA cleanup");
  1647. }
  1648. }
  1649. fHandles.clear();
  1650. for (std::size_t i=0; i<instanceCount; ++i)
  1651. addInstance();
  1652. reconnectAudioPorts();
  1653. if (pData->active)
  1654. activate();
  1655. carla_debug("CarlaPluginDSSI::sampleRateChanged(%g) - end", newSampleRate);
  1656. }
  1657. void reconnectAudioPorts() const noexcept
  1658. {
  1659. if (fForcedStereoIn)
  1660. {
  1661. if (LADSPA_Handle const handle = fHandles.getFirst(nullptr))
  1662. {
  1663. try {
  1664. fDescriptor->connect_port(handle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1665. } CARLA_SAFE_EXCEPTION("DSSI connect_port (forced stereo input, first)");
  1666. }
  1667. if (LADSPA_Handle const handle = fHandles.getLast(nullptr))
  1668. {
  1669. try {
  1670. fDescriptor->connect_port(handle, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1671. } CARLA_SAFE_EXCEPTION("DSSI connect_port (forced stereo input, last)");
  1672. }
  1673. }
  1674. else
  1675. {
  1676. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1677. {
  1678. LADSPA_Handle const handle(it.getValue(nullptr));
  1679. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1680. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1681. {
  1682. try {
  1683. fDescriptor->connect_port(handle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1684. } CARLA_SAFE_EXCEPTION("DSSI connect_port (audio input)");
  1685. }
  1686. }
  1687. }
  1688. if (fForcedStereoOut)
  1689. {
  1690. if (LADSPA_Handle const handle = fHandles.getFirst(nullptr))
  1691. {
  1692. try {
  1693. fDescriptor->connect_port(handle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1694. } CARLA_SAFE_EXCEPTION("DSSI connect_port (forced stereo output, first)");
  1695. }
  1696. if (LADSPA_Handle const handle = fHandles.getLast(nullptr))
  1697. {
  1698. try {
  1699. fDescriptor->connect_port(handle, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1700. } CARLA_SAFE_EXCEPTION("DSSI connect_port (forced stereo output, last)");
  1701. }
  1702. }
  1703. else
  1704. {
  1705. for (LinkedList<LADSPA_Handle>::Itenerator it = fHandles.begin2(); it.valid(); it.next())
  1706. {
  1707. LADSPA_Handle const handle(it.getValue(nullptr));
  1708. CARLA_SAFE_ASSERT_CONTINUE(handle != nullptr);
  1709. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1710. {
  1711. try {
  1712. fDescriptor->connect_port(handle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1713. } CARLA_SAFE_EXCEPTION("DSSI connect_port (audio output)");
  1714. }
  1715. }
  1716. }
  1717. }
  1718. // -------------------------------------------------------------------
  1719. // Plugin buffers
  1720. void clearBuffers() noexcept override
  1721. {
  1722. carla_debug("CarlaPluginDSSI::clearBuffers() - start");
  1723. if (fAudioInBuffers != nullptr)
  1724. {
  1725. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1726. {
  1727. if (fAudioInBuffers[i] != nullptr)
  1728. {
  1729. delete[] fAudioInBuffers[i];
  1730. fAudioInBuffers[i] = nullptr;
  1731. }
  1732. }
  1733. delete[] fAudioInBuffers;
  1734. fAudioInBuffers = nullptr;
  1735. }
  1736. if (fAudioOutBuffers != nullptr)
  1737. {
  1738. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1739. {
  1740. if (fAudioOutBuffers[i] != nullptr)
  1741. {
  1742. delete[] fAudioOutBuffers[i];
  1743. fAudioOutBuffers[i] = nullptr;
  1744. }
  1745. }
  1746. delete[] fAudioOutBuffers;
  1747. fAudioOutBuffers = nullptr;
  1748. }
  1749. if (fExtraStereoBuffer[0] != nullptr)
  1750. {
  1751. delete[] fExtraStereoBuffer[0];
  1752. fExtraStereoBuffer[0] = nullptr;
  1753. }
  1754. if (fExtraStereoBuffer[1] != nullptr)
  1755. {
  1756. delete[] fExtraStereoBuffer[1];
  1757. fExtraStereoBuffer[1] = nullptr;
  1758. }
  1759. if (fParamBuffers != nullptr)
  1760. {
  1761. delete[] fParamBuffers;
  1762. fParamBuffers = nullptr;
  1763. }
  1764. CarlaPlugin::clearBuffers();
  1765. carla_debug("CarlaPluginDSSI::clearBuffers() - end");
  1766. }
  1767. #ifdef HAVE_LIBLO
  1768. // -------------------------------------------------------------------
  1769. // OSC stuff
  1770. void handleOscMessage(const char* const method, const int argc, const void* const argvx, const char* const types, const lo_message msg) override
  1771. {
  1772. const lo_address source(lo_message_get_source(msg));
  1773. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  1774. // protocol for DSSI UIs *must* be UDP
  1775. CARLA_SAFE_ASSERT_RETURN(lo_address_get_protocol(source) == LO_UDP,);
  1776. if (fOscData.source == nullptr)
  1777. {
  1778. // if no UI is registered yet only "configure" and "update" messages are valid
  1779. CARLA_SAFE_ASSERT_RETURN(std::strcmp(method, "configure") == 0 || std::strcmp(method, "update") == 0,)
  1780. }
  1781. else
  1782. {
  1783. // make sure message source is the DSSI UI
  1784. const char* const msghost = lo_address_get_hostname(source);
  1785. const char* const msgport = lo_address_get_port(source);
  1786. const char* const ourhost = lo_address_get_hostname(fOscData.source);
  1787. const char* const ourport = lo_address_get_port(fOscData.source);
  1788. CARLA_SAFE_ASSERT_RETURN(std::strcmp(msghost, ourhost) == 0,);
  1789. CARLA_SAFE_ASSERT_RETURN(std::strcmp(msgport, ourport) == 0,);
  1790. }
  1791. const lo_arg* const* const argv(static_cast<const lo_arg* const* const>(argvx));
  1792. if (std::strcmp(method, "configure") == 0)
  1793. return handleOscMessageConfigure(argc, argv, types);
  1794. if (std::strcmp(method, "control") == 0)
  1795. return handleOscMessageControl(argc, argv, types);
  1796. if (std::strcmp(method, "program") == 0)
  1797. return handleOscMessageProgram(argc, argv, types);
  1798. if (std::strcmp(method, "midi") == 0)
  1799. return handleOscMessageMIDI(argc, argv, types);
  1800. if (std::strcmp(method, "update") == 0)
  1801. return handleOscMessageUpdate(argc, argv, types, lo_message_get_source(msg));
  1802. if (std::strcmp(method, "exiting") == 0)
  1803. return handleOscMessageExiting();
  1804. carla_stdout("CarlaPluginDSSI::handleOscMessage() - unknown method '%s'", method);
  1805. }
  1806. void handleOscMessageConfigure(const int argc, const lo_arg* const* const argv, const char* const types)
  1807. {
  1808. carla_debug("CarlaPluginDSSI::handleMsgConfigure()");
  1809. CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(2, "ss");
  1810. const char* const key = (const char*)&argv[0]->s;
  1811. const char* const value = (const char*)&argv[1]->s;
  1812. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1813. }
  1814. void handleOscMessageControl(const int argc, const lo_arg* const* const argv, const char* const types)
  1815. {
  1816. carla_debug("CarlaPluginDSSI::handleMsgControl()");
  1817. CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(2, "if");
  1818. const int32_t rindex = argv[0]->i;
  1819. const float value = argv[1]->f;
  1820. setParameterValueByRealIndex(rindex, value, false, true, true);
  1821. }
  1822. void handleOscMessageProgram(const int argc, const lo_arg* const* const argv, const char* const types)
  1823. {
  1824. carla_debug("CarlaPluginDSSI::handleMsgProgram()");
  1825. CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(2, "ii");
  1826. const int32_t bank = argv[0]->i;
  1827. const int32_t program = argv[1]->i;
  1828. CARLA_SAFE_ASSERT_RETURN(bank >= 0,);
  1829. CARLA_SAFE_ASSERT_RETURN(program >= 0,);
  1830. setMidiProgramById(static_cast<uint32_t>(bank), static_cast<uint32_t>(program), false, true, true);
  1831. }
  1832. void handleOscMessageMIDI(const int argc, const lo_arg* const* const argv, const char* const types)
  1833. {
  1834. carla_debug("CarlaPluginDSSI::handleMsgMidi()");
  1835. CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(1, "m");
  1836. if (getMidiInCount() == 0)
  1837. {
  1838. carla_stderr("CarlaPluginDSSI::handleMsgMidi() - received midi when plugin has no midi inputs");
  1839. return;
  1840. }
  1841. const uint8_t* const data = argv[0]->m;
  1842. uint8_t status = data[1];
  1843. uint8_t channel = status & 0x0F;
  1844. // Fix bad note-off
  1845. if (MIDI_IS_STATUS_NOTE_ON(status) && data[3] == 0)
  1846. status = MIDI_STATUS_NOTE_OFF;
  1847. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1848. {
  1849. const uint8_t note = data[2];
  1850. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1851. sendMidiSingleNote(channel, note, 0, false, true, true);
  1852. }
  1853. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1854. {
  1855. const uint8_t note = data[2];
  1856. const uint8_t velo = data[3];
  1857. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1858. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1859. sendMidiSingleNote(channel, note, velo, false, true, true);
  1860. }
  1861. }
  1862. void handleOscMessageUpdate(const int argc, const lo_arg* const* const argv, const char* const types, const lo_address source)
  1863. {
  1864. carla_debug("CarlaPluginDSSI::handleMsgUpdate()");
  1865. CARLA_PLUGIN_DSSI_OSC_CHECK_OSC_TYPES(1, "s");
  1866. const char* const url = (const char*)&argv[0]->s;
  1867. // FIXME - remove debug prints later
  1868. carla_stdout("CarlaPluginDSSI::updateOscData(%p, \"%s\")", source, url);
  1869. fOscData.clear();
  1870. const int proto = lo_address_get_protocol(source);
  1871. {
  1872. const char* host = lo_address_get_hostname(source);
  1873. const char* port = lo_address_get_port(source);
  1874. fOscData.source = lo_address_new_with_proto(proto, host, port);
  1875. carla_stdout("CarlaPlugin::updateOscData() - source: host \"%s\", port \"%s\"", host, port);
  1876. }
  1877. {
  1878. char* host = lo_url_get_hostname(url);
  1879. char* port = lo_url_get_port(url);
  1880. fOscData.path = carla_strdup_free(lo_url_get_path(url));
  1881. fOscData.target = lo_address_new_with_proto(proto, host, port);
  1882. carla_stdout("CarlaPlugin::updateOscData() - target: host \"%s\", port \"%s\", path \"%s\"", host, port, fOscData.path);
  1883. std::free(host);
  1884. std::free(port);
  1885. }
  1886. osc_send_sample_rate(fOscData, static_cast<float>(pData->engine->getSampleRate()));
  1887. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  1888. {
  1889. const CustomData& customData(it.getValue(kCustomDataFallback));
  1890. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1891. if (std::strcmp(customData.type, CUSTOM_DATA_TYPE_STRING) == 0)
  1892. osc_send_configure(fOscData, customData.key, customData.value);
  1893. }
  1894. if (pData->prog.current >= 0)
  1895. osc_send_program(fOscData, static_cast<uint32_t>(pData->prog.current));
  1896. if (pData->midiprog.current >= 0)
  1897. {
  1898. const MidiProgramData& curMidiProg(pData->midiprog.getCurrent());
  1899. osc_send_program(fOscData, curMidiProg.bank, curMidiProg.program);
  1900. }
  1901. for (uint32_t i=0; i < pData->param.count; ++i)
  1902. osc_send_control(fOscData, pData->param.data[i].rindex, getParameterValue(i));
  1903. #ifndef BUILD_BRIDGE
  1904. if (pData->engine->getOptions().frontendWinId != 0)
  1905. pData->transientTryCounter = 1;
  1906. #endif
  1907. carla_stdout("CarlaPluginDSSI::updateOscData() - done");
  1908. }
  1909. void handleOscMessageExiting()
  1910. {
  1911. carla_debug("CarlaPluginDSSI::handleMsgExiting()");
  1912. // hide UI
  1913. showCustomUI(false);
  1914. // tell frontend
  1915. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1916. }
  1917. // -------------------------------------------------------------------
  1918. // Post-poned UI Stuff
  1919. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1920. {
  1921. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1922. if (fOscData.target == nullptr)
  1923. return;
  1924. osc_send_control(fOscData, pData->param.data[index].rindex, value);
  1925. }
  1926. void uiMidiProgramChange(const uint32_t index) noexcept override
  1927. {
  1928. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1929. if (fOscData.target == nullptr)
  1930. return;
  1931. osc_send_program(fOscData, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1932. }
  1933. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1934. {
  1935. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1936. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1937. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1938. if (fOscData.target == nullptr)
  1939. return;
  1940. #if 0
  1941. uint8_t midiData[4];
  1942. midiData[0] = 0;
  1943. midiData[1] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  1944. midiData[2] = note;
  1945. midiData[3] = velo;
  1946. osc_send_midi(fOscData, midiData);
  1947. #endif
  1948. }
  1949. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1950. {
  1951. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1952. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1953. if (fOscData.target == nullptr)
  1954. return;
  1955. #if 0
  1956. uint8_t midiData[4];
  1957. midiData[0] = 0;
  1958. midiData[1] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  1959. midiData[2] = note;
  1960. midiData[3] = 0;
  1961. osc_send_midi(fOscData, midiData);
  1962. #endif
  1963. }
  1964. #endif // HAVE_LIBLO
  1965. // -------------------------------------------------------------------
  1966. const void* getNativeDescriptor() const noexcept override
  1967. {
  1968. return fDssiDescriptor;
  1969. }
  1970. #ifdef HAVE_LIBLO
  1971. uintptr_t getUiBridgeProcessId() const noexcept override
  1972. {
  1973. return fThreadUI.getProcessId();
  1974. }
  1975. const void* getExtraStuff() const noexcept override
  1976. {
  1977. return fUiFilename;
  1978. }
  1979. #endif
  1980. // -------------------------------------------------------------------
  1981. bool init(const char* const filename, const char* name, const char* const label, const uint options)
  1982. {
  1983. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1984. // ---------------------------------------------------------------
  1985. // first checks
  1986. if (pData->client != nullptr)
  1987. {
  1988. pData->engine->setLastError("Plugin client is already registered");
  1989. return false;
  1990. }
  1991. if (filename == nullptr || filename[0] == '\0')
  1992. {
  1993. pData->engine->setLastError("null filename");
  1994. return false;
  1995. }
  1996. // ---------------------------------------------------------------
  1997. // open DLL
  1998. if (! pData->libOpen(filename))
  1999. {
  2000. pData->engine->setLastError(pData->libError(filename));
  2001. return false;
  2002. }
  2003. // ---------------------------------------------------------------
  2004. // get DLL main entry
  2005. const DSSI_Descriptor_Function descFn = pData->libSymbol<DSSI_Descriptor_Function>("dssi_descriptor");
  2006. if (descFn == nullptr)
  2007. {
  2008. pData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  2009. return false;
  2010. }
  2011. // ---------------------------------------------------------------
  2012. // get descriptor that matches label
  2013. // if label is null, get first valid plugin
  2014. const bool nullLabel = (label == nullptr || label[0] == '\0');
  2015. for (ulong d=0;; ++d)
  2016. {
  2017. try {
  2018. fDssiDescriptor = descFn(d);
  2019. }
  2020. catch(...) {
  2021. carla_stderr2("Caught exception when trying to get DSSI descriptor");
  2022. fDescriptor = nullptr;
  2023. fDssiDescriptor = nullptr;
  2024. break;
  2025. }
  2026. if (fDssiDescriptor == nullptr)
  2027. break;
  2028. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  2029. if (fDescriptor == nullptr)
  2030. {
  2031. carla_stderr2("WARNING - Missing LADSPA interface, will not use this plugin");
  2032. fDssiDescriptor = nullptr;
  2033. break;
  2034. }
  2035. if (fDescriptor->Label == nullptr || fDescriptor->Label[0] == '\0')
  2036. {
  2037. carla_stderr2("WARNING - Got an invalid label, will not use this plugin");
  2038. fDescriptor = nullptr;
  2039. fDssiDescriptor = nullptr;
  2040. break;
  2041. }
  2042. if (fDescriptor->run == nullptr)
  2043. {
  2044. carla_stderr2("WARNING - Plugin has no run, cannot use it");
  2045. fDescriptor = nullptr;
  2046. fDssiDescriptor = nullptr;
  2047. break;
  2048. }
  2049. if (nullLabel || std::strcmp(fDescriptor->Label, label) == 0)
  2050. break;
  2051. }
  2052. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  2053. {
  2054. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  2055. return false;
  2056. }
  2057. // ---------------------------------------------------------------
  2058. // check if uses global instance
  2059. if (fDssiDescriptor->run_synth == nullptr && fDssiDescriptor->run_multiple_synths != nullptr)
  2060. {
  2061. pData->engine->setLastError("This plugin requires run_multiple_synths which is not supported");
  2062. return false;
  2063. }
  2064. // ---------------------------------------------------------------
  2065. // check for fixed buffer size requirement
  2066. fNeedsFixedBuffers = CarlaString(filename).contains("dssi-vst", true);
  2067. if (fNeedsFixedBuffers && ! pData->engine->usesConstantBufferSize())
  2068. {
  2069. pData->engine->setLastError("Cannot use this plugin under the current engine.\n"
  2070. "The plugin requires a fixed block size which is not possible right now.");
  2071. return false;
  2072. }
  2073. // ---------------------------------------------------------------
  2074. // get info
  2075. if (name == nullptr || name[0] == '\0')
  2076. {
  2077. if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  2078. name = fDescriptor->Name;
  2079. else
  2080. name = fDescriptor->Label;
  2081. }
  2082. pData->name = pData->engine->getUniquePluginName(name);
  2083. pData->filename = carla_strdup(filename);
  2084. // ---------------------------------------------------------------
  2085. // register client
  2086. pData->client = pData->engine->addClient(this);
  2087. if (pData->client == nullptr || ! pData->client->isOk())
  2088. {
  2089. pData->engine->setLastError("Failed to register plugin client");
  2090. return false;
  2091. }
  2092. // ---------------------------------------------------------------
  2093. // initialize plugin
  2094. if (! addInstance())
  2095. return false;
  2096. // ---------------------------------------------------------------
  2097. // find latency port index
  2098. for (uint32_t i=0, iCtrl=0, count=getSafePortCount(); i<count; ++i)
  2099. {
  2100. const int portType(fDescriptor->PortDescriptors[i]);
  2101. if (! LADSPA_IS_PORT_CONTROL(portType))
  2102. continue;
  2103. const uint32_t index(iCtrl++);
  2104. if (! LADSPA_IS_PORT_OUTPUT(portType))
  2105. continue;
  2106. const char* const portName(fDescriptor->PortNames[i]);
  2107. CARLA_SAFE_ASSERT_BREAK(portName != nullptr);
  2108. if (std::strcmp(portName, "latency") == 0 ||
  2109. std::strcmp(portName, "_latency") == 0)
  2110. {
  2111. fLatencyIndex = static_cast<int32_t>(index);
  2112. break;
  2113. }
  2114. }
  2115. // ---------------------------------------------------------------
  2116. // check for custom data extension
  2117. if (fDssiDescriptor->configure != nullptr)
  2118. {
  2119. if (char* const error = fDssiDescriptor->configure(fHandles.getFirst(nullptr), DSSI_CUSTOMDATA_EXTENSION_KEY, ""))
  2120. {
  2121. if (std::strcmp(error, "true") == 0 && fDssiDescriptor->get_custom_data != nullptr
  2122. && fDssiDescriptor->set_custom_data != nullptr)
  2123. fUsesCustomData = true;
  2124. std::free(error);
  2125. }
  2126. }
  2127. #ifdef HAVE_LIBLO
  2128. // ---------------------------------------------------------------
  2129. // check for gui
  2130. if (const char* const guiFilename = find_dssi_ui(filename, fDescriptor->Label))
  2131. {
  2132. fUiFilename = guiFilename;
  2133. fThreadUI.setData(guiFilename, fDescriptor->Label);
  2134. }
  2135. #endif
  2136. // ---------------------------------------------------------------
  2137. // set default options
  2138. pData->options = 0x0;
  2139. /**/ if (fLatencyIndex >= 0 || fNeedsFixedBuffers)
  2140. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2141. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  2142. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2143. /**/ if (pData->engine->getOptions().forceStereo)
  2144. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2145. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  2146. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2147. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  2148. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  2149. if (fUsesCustomData)
  2150. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  2151. if (fDssiDescriptor->run_synth != nullptr)
  2152. {
  2153. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  2154. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  2155. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  2156. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  2157. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  2158. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  2159. }
  2160. return true;
  2161. }
  2162. // -------------------------------------------------------------------
  2163. private:
  2164. LinkedList<LADSPA_Handle> fHandles;
  2165. const LADSPA_Descriptor* fDescriptor;
  2166. const DSSI_Descriptor* fDssiDescriptor;
  2167. float** fAudioInBuffers;
  2168. float** fAudioOutBuffers;
  2169. float* fExtraStereoBuffer[2]; // used only if forcedStereoIn and audioOut == 2
  2170. float* fParamBuffers;
  2171. snd_seq_event_t fMidiEvents[kPluginMaxMidiEvents];
  2172. int32_t fLatencyIndex; // -1 if invalid
  2173. bool fForcedStereoIn;
  2174. bool fForcedStereoOut;
  2175. bool fNeedsFixedBuffers;
  2176. bool fUsesCustomData;
  2177. #ifdef HAVE_LIBLO
  2178. CarlaOscData fOscData;
  2179. CarlaThreadDSSIUI fThreadUI;
  2180. const char* fUiFilename;
  2181. #endif
  2182. // -------------------------------------------------------------------
  2183. bool addInstance()
  2184. {
  2185. LADSPA_Handle handle;
  2186. try {
  2187. handle = fDescriptor->instantiate(fDescriptor, static_cast<ulong>(pData->engine->getSampleRate()));
  2188. } CARLA_SAFE_EXCEPTION_RETURN_ERR("LADSPA instantiate", "Plugin failed to initialize");
  2189. for (uint32_t i=0, count=pData->param.count; i<count; ++i)
  2190. {
  2191. const int32_t rindex(pData->param.data[i].rindex);
  2192. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0);
  2193. try {
  2194. fDescriptor->connect_port(handle, static_cast<ulong>(rindex), &fParamBuffers[i]);
  2195. } CARLA_SAFE_EXCEPTION("LADSPA connect_port");
  2196. }
  2197. if (fHandles.append(handle))
  2198. return true;
  2199. try {
  2200. fDescriptor->cleanup(handle);
  2201. } CARLA_SAFE_EXCEPTION("LADSPA cleanup");
  2202. pData->engine->setLastError("Out of memory");
  2203. return false;
  2204. }
  2205. uint32_t getSafePortCount() const noexcept
  2206. {
  2207. if (fDescriptor->PortCount == 0)
  2208. return 0;
  2209. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortDescriptors != nullptr, 0);
  2210. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortRangeHints != nullptr, 0);
  2211. CARLA_SAFE_ASSERT_RETURN(fDescriptor->PortNames != nullptr, 0);
  2212. return static_cast<uint32_t>(fDescriptor->PortCount);
  2213. }
  2214. bool getSeparatedParameterNameOrUnit(const char* const paramName, char* const strBuf, const bool wantName) const noexcept
  2215. {
  2216. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, true))
  2217. return true;
  2218. if (_getSeparatedParameterNameOrUnitImpl(paramName, strBuf, wantName, false))
  2219. return true;
  2220. return false;
  2221. }
  2222. static bool _getSeparatedParameterNameOrUnitImpl(const char* const paramName, char* const strBuf,
  2223. const bool wantName, const bool useBracket) noexcept
  2224. {
  2225. const char* const sepBracketStart(std::strstr(paramName, useBracket ? " [" : " ("));
  2226. if (sepBracketStart == nullptr)
  2227. return false;
  2228. const char* const sepBracketEnd(std::strstr(sepBracketStart, useBracket ? "]" : ")"));
  2229. if (sepBracketEnd == nullptr)
  2230. return false;
  2231. const std::size_t unitSize(static_cast<std::size_t>(sepBracketEnd-sepBracketStart-2));
  2232. if (unitSize > 7) // very unlikely to have such big unit
  2233. return false;
  2234. const std::size_t sepIndex(std::strlen(paramName)-unitSize-3);
  2235. // just in case
  2236. if (sepIndex+2 >= STR_MAX)
  2237. return false;
  2238. if (wantName)
  2239. {
  2240. std::strncpy(strBuf, paramName, sepIndex);
  2241. strBuf[sepIndex] = '\0';
  2242. }
  2243. else
  2244. {
  2245. std::strncpy(strBuf, paramName+(sepIndex+2), unitSize);
  2246. strBuf[unitSize] = '\0';
  2247. }
  2248. return true;
  2249. }
  2250. // -------------------------------------------------------------------
  2251. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginDSSI)
  2252. };
  2253. // -------------------------------------------------------------------------------------------------------------------
  2254. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init)
  2255. {
  2256. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\", " P_INT64 ", %x})",
  2257. init.engine, init.filename, init.name, init.label, init.uniqueId, init.options);
  2258. CarlaPluginDSSI* const plugin(new CarlaPluginDSSI(init.engine, init.id));
  2259. if (! plugin->init(init.filename, init.name, init.label, init.options))
  2260. {
  2261. delete plugin;
  2262. return nullptr;
  2263. }
  2264. return plugin;
  2265. }
  2266. // -------------------------------------------------------------------------------------------------------------------
  2267. CARLA_BACKEND_END_NAMESPACE