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.

3201 lines
116KB

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