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.

3210 lines
117KB

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