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.

2326 lines
83KB

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