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.

3249 lines
118KB

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