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.

2312 lines
82KB

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