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.

2384 lines
85KB

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