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.

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