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.

3199 lines
116KB

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