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.

3208 lines
117KB

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