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.

3145 lines
114KB

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