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.

2785 lines
100KB

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