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.

3178 lines
115KB

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