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.

3191 lines
115KB

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