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.

2378 lines
84KB

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