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.

3230 lines
117KB

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