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.

2171 lines
77KB

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