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.

1646 lines
57KB

  1. /*
  2. * Carla DSSI Plugin
  3. * Copyright (C) 2011-2013 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 GPL.txt file
  16. */
  17. #include "carla_plugin_internal.hpp"
  18. #ifdef WANT_DSSI
  19. #include "carla_ladspa_utils.hpp"
  20. #include "dssi/dssi.h"
  21. CARLA_BACKEND_START_NAMESPACE
  22. class DssiPlugin : public CarlaPlugin
  23. {
  24. public:
  25. DssiPlugin(CarlaEngine* const engine, const unsigned int id)
  26. : CarlaPlugin(engine, id)
  27. {
  28. qDebug("DssiPlugin::DssiPlugin()");
  29. fHandle = nullptr;
  30. fHandle2 = nullptr;
  31. fDescriptor = nullptr;
  32. fDssiDescriptor = nullptr;
  33. fParamBuffers = nullptr;
  34. carla_zeroMem(fMidiEvents, sizeof(snd_seq_event_t)*MAX_MIDI_EVENTS);
  35. }
  36. ~DssiPlugin()
  37. {
  38. qDebug("DssiPlugin::~DssiPlugin()");
  39. // close UI
  40. if (fHints & PLUGIN_HAS_GUI)
  41. {
  42. showGui(false);
  43. if (kData->osc.thread != nullptr)
  44. {
  45. // Wait a bit first, try safe quit, then force kill
  46. if (kData->osc.thread->isRunning() && ! kData->osc.thread->wait(kData->engine->getOptions().oscUiTimeout))
  47. {
  48. qWarning("Failed to properly stop DSSI GUI thread");
  49. kData->osc.thread->terminate();
  50. }
  51. delete kData->osc.thread;
  52. }
  53. }
  54. if (fDescriptor != nullptr)
  55. {
  56. if (fDescriptor->deactivate != nullptr && kData->activeBefore)
  57. {
  58. if (fHandle != nullptr)
  59. fDescriptor->deactivate(fHandle);
  60. if (fHandle2 != nullptr)
  61. fDescriptor->deactivate(fHandle2);
  62. }
  63. if (fDescriptor->cleanup != nullptr)
  64. {
  65. if (fHandle != nullptr)
  66. fDescriptor->cleanup(fHandle);
  67. if (fHandle2 != nullptr)
  68. fDescriptor->cleanup(fHandle2);
  69. }
  70. fHandle = nullptr;
  71. fHandle2 = nullptr;
  72. fDescriptor = nullptr;
  73. fDssiDescriptor = nullptr;
  74. }
  75. if (fParamBuffers != nullptr)
  76. {
  77. delete[] fParamBuffers;
  78. fParamBuffers = nullptr;
  79. }
  80. }
  81. // -------------------------------------------------------------------
  82. // Information (base)
  83. virtual PluginType type() const
  84. {
  85. return PLUGIN_DSSI;
  86. }
  87. PluginCategory category() const
  88. {
  89. if (fHints & PLUGIN_IS_SYNTH)
  90. return PLUGIN_CATEGORY_SYNTH;
  91. return getPluginCategoryFromName(fName);
  92. }
  93. long uniqueId() const
  94. {
  95. CARLA_ASSERT(fDescriptor != nullptr);
  96. return (fDescriptor != nullptr) ? fDescriptor->UniqueID : 0;
  97. }
  98. // -------------------------------------------------------------------
  99. // Information (current data)
  100. int32_t chunkData(void** const dataPtr)
  101. {
  102. CARLA_ASSERT(fHints & PLUGIN_USES_CHUNKS);
  103. CARLA_ASSERT(fDssiDescriptor != nullptr);
  104. CARLA_ASSERT(fDssiDescriptor->get_custom_data != nullptr);
  105. CARLA_ASSERT(fHandle != nullptr);
  106. CARLA_ASSERT(fHandle2 == nullptr);
  107. CARLA_ASSERT(dataPtr != nullptr);
  108. unsigned long dataSize = 0;
  109. if (fDssiDescriptor->get_custom_data != nullptr && fDssiDescriptor->get_custom_data(fHandle, dataPtr, &dataSize))
  110. return dataSize;
  111. return 0;
  112. }
  113. // -------------------------------------------------------------------
  114. // Information (per-plugin data)
  115. float getParameterValue(const uint32_t parameterId)
  116. {
  117. CARLA_ASSERT(parameterId < kData->param.count);
  118. return fParamBuffers[parameterId];
  119. }
  120. void getLabel(char* const strBuf)
  121. {
  122. CARLA_ASSERT(fDescriptor != nullptr);
  123. if (fDescriptor != nullptr && fDescriptor->Label != nullptr)
  124. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  125. else
  126. CarlaPlugin::getLabel(strBuf);
  127. }
  128. void getMaker(char* const strBuf)
  129. {
  130. CARLA_ASSERT(fDescriptor != nullptr);
  131. if (fDescriptor != nullptr && fDescriptor->Maker != nullptr)
  132. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  133. else
  134. CarlaPlugin::getMaker(strBuf);
  135. }
  136. void getCopyright(char* const strBuf)
  137. {
  138. CARLA_ASSERT(fDescriptor != nullptr);
  139. if (fDescriptor != nullptr && fDescriptor->Copyright != nullptr)
  140. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  141. else
  142. CarlaPlugin::getCopyright(strBuf);
  143. }
  144. void getRealName(char* const strBuf)
  145. {
  146. CARLA_ASSERT(fDescriptor != nullptr);
  147. if (fDescriptor != nullptr && fDescriptor->Name != nullptr)
  148. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  149. else
  150. CarlaPlugin::getRealName(strBuf);
  151. }
  152. void getParameterName(const uint32_t parameterId, char* const strBuf)
  153. {
  154. CARLA_ASSERT(fDescriptor != nullptr);
  155. CARLA_ASSERT(parameterId < kData->param.count);
  156. const int32_t rindex = kData->param.data[parameterId].rindex;
  157. if (fDescriptor != nullptr && rindex < static_cast<int32_t>(fDescriptor->PortCount))
  158. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  159. else
  160. CarlaPlugin::getParameterName(parameterId, strBuf);
  161. }
  162. // -------------------------------------------------------------------
  163. // Set data (plugin-specific stuff)
  164. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  165. {
  166. CARLA_ASSERT(parameterId < kData->param.count);
  167. const float fixedValue = kData->param.fixValue(parameterId, value);
  168. fParamBuffers[parameterId] = fixedValue;
  169. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  170. }
  171. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  172. {
  173. CARLA_ASSERT(fDescriptor != nullptr);
  174. CARLA_ASSERT(fHandle != nullptr);
  175. CARLA_ASSERT(type != nullptr);
  176. CARLA_ASSERT(key != nullptr);
  177. CARLA_ASSERT(value != nullptr);
  178. if (type == nullptr)
  179. return qCritical("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  180. if (std::strcmp(type, CUSTOM_DATA_STRING) != 0)
  181. return qCritical("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  182. if (key == nullptr)
  183. return qCritical("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  184. if (value == nullptr)
  185. return qCritical("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  186. if (fDssiDescriptor->configure != nullptr)
  187. {
  188. fDssiDescriptor->configure(fHandle, key, value);
  189. if (fHandle2)
  190. fDssiDescriptor->configure(fHandle2, key, value);
  191. }
  192. if (sendGui && kData->osc.data.target != nullptr)
  193. osc_send_configure(&kData->osc.data, key, value);
  194. if (strcmp(key, "reloadprograms") == 0 || strcmp(key, "load") == 0 || strncmp(key, "patches", 7) == 0)
  195. {
  196. const ScopedDisabler m(this); // FIXME
  197. reloadPrograms(false);
  198. }
  199. CarlaPlugin::setCustomData(type, key, value, sendGui);
  200. }
  201. void setChunkData(const char* const stringData)
  202. {
  203. CARLA_ASSERT(fHints & PLUGIN_USES_CHUNKS);
  204. CARLA_ASSERT(fDssiDescriptor != nullptr);
  205. CARLA_ASSERT(fDssiDescriptor->set_custom_data != nullptr);
  206. CARLA_ASSERT(fHandle != nullptr);
  207. CARLA_ASSERT(fHandle2 == nullptr);
  208. CARLA_ASSERT(stringData != nullptr);
  209. if (fDssiDescriptor->set_custom_data == nullptr)
  210. return;
  211. // FIXME
  212. fChunk = QByteArray::fromBase64(QByteArray(stringData));
  213. //fChunk.toBase64();
  214. if (kData->engine->isOffline())
  215. {
  216. //const CarlaEngine::ScopedLocker m(kData->engine);
  217. fDssiDescriptor->set_custom_data(fHandle, fChunk.data(), fChunk.size());
  218. }
  219. else
  220. {
  221. const CarlaPlugin::ScopedDisabler m(this);
  222. fDssiDescriptor->set_custom_data(fHandle, fChunk.data(), fChunk.size());
  223. }
  224. }
  225. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  226. {
  227. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->midiprog.count));
  228. if (index < -1)
  229. index = -1;
  230. else if (index > static_cast<int32_t>(kData->midiprog.count))
  231. return;
  232. // FIXME
  233. if (index >= 0)
  234. {
  235. const uint32_t bank = kData->midiprog.data[index].bank;
  236. const uint32_t program = kData->midiprog.data[index].program;
  237. if (kData->engine->isOffline())
  238. {
  239. //const CarlaEngine::ScopedLocker m(x_engine, block);
  240. fDssiDescriptor->select_program(fHandle, bank, program);
  241. if (fHandle)
  242. fDssiDescriptor->select_program(fHandle2, bank, program);
  243. }
  244. else
  245. {
  246. //const ScopedDisabler m(this, block);
  247. fDssiDescriptor->select_program(fHandle, bank, program);
  248. if (fHandle2)
  249. fDssiDescriptor->select_program(fHandle2, bank, program);
  250. }
  251. }
  252. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, block);
  253. }
  254. // -------------------------------------------------------------------
  255. // Set gui stuff
  256. void showGui(const bool yesNo)
  257. {
  258. CARLA_ASSERT(kData->osc.thread != nullptr);
  259. if (kData->osc.thread == nullptr)
  260. {
  261. qCritical("DssiPlugin::showGui(%s) - attempt to show gui, but it does not exist!", bool2str(yesNo));
  262. return;
  263. }
  264. if (yesNo)
  265. {
  266. kData->osc.thread->start();
  267. }
  268. else
  269. {
  270. if (kData->osc.data.target != nullptr)
  271. {
  272. osc_send_hide(&kData->osc.data);
  273. osc_send_quit(&kData->osc.data);
  274. kData->osc.data.free();
  275. }
  276. if (! kData->osc.thread->wait(500))
  277. kData->osc.thread->quit();
  278. }
  279. }
  280. // -------------------------------------------------------------------
  281. // Plugin state
  282. void reload()
  283. {
  284. qDebug("DssiPlugin::reload() - start");
  285. CARLA_ASSERT(kData->engine != nullptr);
  286. CARLA_ASSERT(fDescriptor != nullptr);
  287. CARLA_ASSERT(fHandle != nullptr);
  288. const ProcessMode processMode(kData->engine->getProccessMode());
  289. // Safely disable plugin for reload
  290. const ScopedDisabler sd(this);
  291. if (kData->client->isActive())
  292. kData->client->deactivate();
  293. deleteBuffers();
  294. const double sampleRate = kData->engine->getSampleRate();
  295. const unsigned long portCount = fDescriptor->PortCount;
  296. uint32_t aIns, aOuts, mIns, params, j;
  297. aIns = aOuts = mIns = params = 0;
  298. bool forcedStereoIn, forcedStereoOut;
  299. forcedStereoIn = forcedStereoOut = false;
  300. bool needsCtrlIn, needsCtrlOut;
  301. needsCtrlIn = needsCtrlOut = false;
  302. for (unsigned long i=0; i < portCount; i++)
  303. {
  304. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  305. if (LADSPA_IS_PORT_AUDIO(portType))
  306. {
  307. if (LADSPA_IS_PORT_INPUT(portType))
  308. aIns += 1;
  309. else if (LADSPA_IS_PORT_OUTPUT(portType))
  310. aOuts += 1;
  311. }
  312. else if (LADSPA_IS_PORT_CONTROL(portType))
  313. params += 1;
  314. }
  315. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  316. {
  317. if (fHandle2 == nullptr)
  318. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate);
  319. if (aIns == 1)
  320. {
  321. aIns = 2;
  322. forcedStereoIn = true;
  323. }
  324. if (aOuts == 1)
  325. {
  326. aOuts = 2;
  327. forcedStereoOut = true;
  328. }
  329. }
  330. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  331. {
  332. mIns = 1;
  333. needsCtrlIn = true;
  334. }
  335. if (aIns > 0)
  336. {
  337. kData->audioIn.createNew(aIns);
  338. }
  339. if (aOuts > 0)
  340. {
  341. kData->audioOut.createNew(aOuts);
  342. }
  343. if (params > 0)
  344. {
  345. kData->param.createNew(params);
  346. fParamBuffers = new float[params];
  347. }
  348. const int portNameSize = kData->engine->maxPortNameSize();
  349. CarlaString portName;
  350. for (unsigned long i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; i++)
  351. {
  352. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  353. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  354. if (LADSPA_IS_PORT_AUDIO(portType))
  355. {
  356. portName.clear();
  357. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  358. {
  359. portName = fName;
  360. portName += ":";
  361. }
  362. portName += fDescriptor->PortNames[i];
  363. portName.truncate(portNameSize);
  364. if (LADSPA_IS_PORT_INPUT(portType))
  365. {
  366. j = iAudioIn++;
  367. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  368. kData->audioIn.ports[j].rindex = i;
  369. if (forcedStereoIn)
  370. {
  371. portName += "_2";
  372. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  373. kData->audioIn.ports[1].rindex = i;
  374. }
  375. }
  376. else if (LADSPA_IS_PORT_OUTPUT(portType))
  377. {
  378. j = iAudioOut++;
  379. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  380. kData->audioOut.ports[j].rindex = i;
  381. needsCtrlIn = true;
  382. if (forcedStereoOut)
  383. {
  384. portName += "_2";
  385. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  386. kData->audioOut.ports[1].rindex = i;
  387. }
  388. }
  389. else
  390. qWarning("WARNING - Got a broken Port (Audio, but not input or output)");
  391. }
  392. else if (LADSPA_IS_PORT_CONTROL(portType))
  393. {
  394. j = iCtrl++;
  395. kData->param.data[j].index = j;
  396. kData->param.data[j].rindex = i;
  397. kData->param.data[j].hints = 0x0;
  398. kData->param.data[j].midiChannel = 0;
  399. kData->param.data[j].midiCC = -1;
  400. float min, max, def, step, stepSmall, stepLarge;
  401. // min value
  402. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  403. min = portRangeHints.LowerBound;
  404. else
  405. min = 0.0f;
  406. // max value
  407. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  408. max = portRangeHints.UpperBound;
  409. else
  410. max = 1.0f;
  411. if (min > max)
  412. max = min;
  413. else if (max < min)
  414. min = max;
  415. if (max - min == 0.0f)
  416. {
  417. qWarning("Broken plugin parameter: max - min == 0");
  418. max = min + 0.1f;
  419. }
  420. // default value
  421. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  422. if (def < min)
  423. def = min;
  424. else if (def > max)
  425. def = max;
  426. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  427. {
  428. min *= sampleRate;
  429. max *= sampleRate;
  430. def *= sampleRate;
  431. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  432. }
  433. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  434. {
  435. step = max - min;
  436. stepSmall = step;
  437. stepLarge = step;
  438. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  439. }
  440. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  441. {
  442. step = 1.0f;
  443. stepSmall = 1.0f;
  444. stepLarge = 10.0f;
  445. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  446. }
  447. else
  448. {
  449. float range = max - min;
  450. step = range/100.0f;
  451. stepSmall = range/1000.0f;
  452. stepLarge = range/10.0f;
  453. }
  454. if (LADSPA_IS_PORT_INPUT(portType))
  455. {
  456. kData->param.data[j].type = PARAMETER_INPUT;
  457. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  458. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  459. needsCtrlIn = true;
  460. // MIDI CC value
  461. if (fDssiDescriptor->get_midi_controller_for_port != nullptr)
  462. {
  463. int controller = fDssiDescriptor->get_midi_controller_for_port(fHandle, i);
  464. if (DSSI_CONTROLLER_IS_SET(controller) && DSSI_IS_CC(controller))
  465. {
  466. int16_t cc = DSSI_CC_NUMBER(controller);
  467. if (! MIDI_IS_CONTROL_BANK_SELECT(cc))
  468. kData->param.data[j].midiCC = cc;
  469. }
  470. }
  471. }
  472. else if (LADSPA_IS_PORT_OUTPUT(portType))
  473. {
  474. if (std::strcmp(fDescriptor->PortNames[i], "latency") == 0 || std::strcmp(fDescriptor->PortNames[i], "_latency") == 0)
  475. {
  476. min = 0.0f;
  477. max = sampleRate;
  478. def = 0.0f;
  479. step = 1.0f;
  480. stepSmall = 1.0f;
  481. stepLarge = 1.0f;
  482. kData->param.data[j].type = PARAMETER_LATENCY;
  483. kData->param.data[j].hints = 0;
  484. }
  485. else if (std::strcmp(fDescriptor->PortNames[i], "_sample-rate") == 0)
  486. {
  487. def = sampleRate;
  488. step = 1.0f;
  489. stepSmall = 1.0f;
  490. stepLarge = 1.0f;
  491. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  492. kData->param.data[j].hints = 0;
  493. }
  494. else
  495. {
  496. kData->param.data[j].type = PARAMETER_OUTPUT;
  497. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  498. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  499. needsCtrlOut = true;
  500. }
  501. }
  502. else
  503. {
  504. kData->param.data[j].type = PARAMETER_UNKNOWN;
  505. qWarning("WARNING - Got a broken Port (Control, but not input or output)");
  506. }
  507. // extra parameter hints
  508. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  509. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  510. kData->param.ranges[j].min = min;
  511. kData->param.ranges[j].max = max;
  512. kData->param.ranges[j].def = def;
  513. kData->param.ranges[j].step = step;
  514. kData->param.ranges[j].stepSmall = stepSmall;
  515. kData->param.ranges[j].stepLarge = stepLarge;
  516. // Start parameters in their default values
  517. fParamBuffers[j] = def;
  518. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  519. if (fHandle2 != nullptr)
  520. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  521. }
  522. else
  523. {
  524. // Not Audio or Control
  525. qCritical("ERROR - Got a broken Port (neither Audio or Control)");
  526. fDescriptor->connect_port(fHandle, i, nullptr);
  527. if (fHandle2 != nullptr)
  528. fDescriptor->connect_port(fHandle2, i, nullptr);
  529. }
  530. }
  531. if (needsCtrlIn)
  532. {
  533. portName.clear();
  534. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  535. {
  536. portName = fName;
  537. portName += ":";
  538. }
  539. portName += "event-in";
  540. portName.truncate(portNameSize);
  541. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  542. }
  543. if (needsCtrlOut)
  544. {
  545. portName.clear();
  546. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  547. {
  548. portName = fName;
  549. portName += ":";
  550. }
  551. portName += "event-out";
  552. portName.truncate(portNameSize);
  553. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  554. }
  555. // plugin checks
  556. fHints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  557. if (mIns == 1 && aIns == 0 && aOuts > 0)
  558. fHints |= PLUGIN_IS_SYNTH;
  559. if (kData->engine->getOptions().useDssiVstChunks && QString(fFilename).endsWith("dssi-vst.so", Qt::CaseInsensitive))
  560. {
  561. if (fDssiDescriptor->get_custom_data && fDssiDescriptor->set_custom_data)
  562. fHints |= PLUGIN_USES_CHUNKS;
  563. }
  564. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  565. fHints |= PLUGIN_CAN_DRYWET;
  566. if (aOuts > 0)
  567. fHints |= PLUGIN_CAN_VOLUME;
  568. if (aOuts >= 2 && aOuts % 2 == 0)
  569. fHints |= PLUGIN_CAN_BALANCE;
  570. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  571. fHints |= PLUGIN_CAN_FORCE_STEREO;
  572. // check latency
  573. if (fHints & PLUGIN_CAN_DRYWET)
  574. {
  575. for (uint32_t i=0; i < kData->param.count; i++)
  576. {
  577. if (kData->param.data[i].type != PARAMETER_LATENCY)
  578. continue;
  579. // we need to pre-run the plugin so it can update its latency control-port
  580. float tmpIn[aIns][2];
  581. float tmpOut[aOuts][2];
  582. for (j=0; j < aIns; j++)
  583. {
  584. tmpIn[j][0] = 0.0f;
  585. tmpIn[j][1] = 0.0f;
  586. fDescriptor->connect_port(fHandle, kData->audioIn.ports[j].rindex, tmpIn[j]);
  587. }
  588. for (j=0; j < aOuts; j++)
  589. {
  590. tmpOut[j][0] = 0.0f;
  591. tmpOut[j][1] = 0.0f;
  592. fDescriptor->connect_port(fHandle, kData->audioOut.ports[j].rindex, tmpOut[j]);
  593. }
  594. if (fDescriptor->activate != nullptr)
  595. fDescriptor->activate(fHandle);
  596. fDescriptor->run(fHandle, 2);
  597. if (fDescriptor->deactivate != nullptr)
  598. fDescriptor->deactivate(fHandle);
  599. const uint32_t latency = std::rint(fParamBuffers[i]);
  600. if (kData->latency != latency)
  601. {
  602. kData->latency = latency;
  603. kData->client->setLatency(latency);
  604. recreateLatencyBuffers();
  605. }
  606. break;
  607. }
  608. }
  609. reloadPrograms(true);
  610. kData->client->activate();
  611. qDebug("DssiPlugin::reload() - end");
  612. }
  613. #if 0
  614. void reloadPrograms(const bool init)
  615. {
  616. qDebug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  617. uint32_t i, oldCount = midiprog.count;
  618. // Delete old programs
  619. if (midiprog.count > 0)
  620. {
  621. for (i=0; i < midiprog.count; i++)
  622. {
  623. if (midiprog.data[i].name)
  624. free((void*)midiprog.data[i].name);
  625. }
  626. delete[] midiprog.data;
  627. }
  628. midiprog.count = 0;
  629. midiprog.data = nullptr;
  630. // Query new programs
  631. if (descriptor->get_program && descriptor->select_program)
  632. {
  633. while (descriptor->get_program(handle, midiprog.count))
  634. midiprog.count += 1;
  635. }
  636. if (midiprog.count > 0)
  637. midiprog.data = new MidiProgramData[midiprog.count];
  638. // Update data
  639. for (i=0; i < midiprog.count; i++)
  640. {
  641. const DSSI_Program_Descriptor* const pdesc = descriptor->get_program(handle, i);
  642. CARLA_ASSERT(pdesc);
  643. CARLA_ASSERT(pdesc->Name);
  644. midiprog.data[i].bank = pdesc->Bank;
  645. midiprog.data[i].program = pdesc->Program;
  646. midiprog.data[i].name = strdup(pdesc->Name ? pdesc->Name : "");
  647. }
  648. #ifndef BUILD_BRIDGE
  649. // Update OSC Names
  650. if (x_engine->isOscControlRegistered())
  651. {
  652. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  653. for (i=0; i < midiprog.count; i++)
  654. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  655. }
  656. #endif
  657. if (init)
  658. {
  659. if (midiprog.count > 0)
  660. setMidiProgram(0, false, false, false, true);
  661. }
  662. else
  663. {
  664. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0, nullptr);
  665. // Check if current program is invalid
  666. bool programChanged = false;
  667. if (midiprog.count == oldCount+1)
  668. {
  669. // one midi program added, probably created by user
  670. midiprog.current = oldCount;
  671. programChanged = true;
  672. }
  673. else if (midiprog.current >= (int32_t)midiprog.count)
  674. {
  675. // current midi program > count
  676. midiprog.current = 0;
  677. programChanged = true;
  678. }
  679. else if (midiprog.current < 0 && midiprog.count > 0)
  680. {
  681. // programs exist now, but not before
  682. midiprog.current = 0;
  683. programChanged = true;
  684. }
  685. else if (midiprog.current >= 0 && midiprog.count == 0)
  686. {
  687. // programs existed before, but not anymore
  688. midiprog.current = -1;
  689. programChanged = true;
  690. }
  691. if (programChanged)
  692. setMidiProgram(midiprog.current, true, true, true, true);
  693. }
  694. }
  695. #endif
  696. // -------------------------------------------------------------------
  697. // Plugin processing
  698. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  699. {
  700. uint32_t i, k;
  701. unsigned long midiEventCount = 0;
  702. // --------------------------------------------------------------------------------------------------------
  703. // Check if active
  704. if (! kData->active)
  705. {
  706. // disable any output sound
  707. for (i=0; i < kData->audioOut.count; i++)
  708. carla_zeroFloat(outBuffer[i], frames);
  709. if (kData->activeBefore)
  710. {
  711. if (fDescriptor->deactivate != nullptr)
  712. {
  713. fDescriptor->deactivate(fHandle);
  714. if (fHandle2 != nullptr)
  715. fDescriptor->deactivate(fHandle2);
  716. }
  717. }
  718. kData->activeBefore = kData->active;
  719. return;
  720. }
  721. // --------------------------------------------------------------------------------------------------------
  722. // Event Input
  723. if (kData->event.portIn != nullptr && kData->activeBefore)
  724. {
  725. bool allNotesOffSent = false;
  726. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  727. uint32_t nextBankId = 0;
  728. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  729. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  730. for (i=0; i < nEvents; i++)
  731. {
  732. const EngineEvent& event = kData->event.portIn->getEvent(i);
  733. time = event.time - framesOffset;
  734. if (time >= frames)
  735. continue;
  736. // Control change
  737. switch (event.type)
  738. {
  739. case kEngineEventTypeNull:
  740. break;
  741. case kEngineEventTypeControl:
  742. {
  743. const EngineControlEvent& ctrlEvent = event.ctrl;
  744. switch (ctrlEvent.type)
  745. {
  746. case kEngineControlEventTypeNull:
  747. break;
  748. case kEngineControlEventTypeParameter:
  749. {
  750. // Control backend stuff
  751. if (event.channel == kData->ctrlInChannel)
  752. {
  753. double value;
  754. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  755. {
  756. value = ctrlEvent.value;
  757. setDryWet(value, false, false);
  758. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  759. continue;
  760. }
  761. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  762. {
  763. value = ctrlEvent.value*127/100;
  764. setVolume(value, false, false);
  765. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  766. continue;
  767. }
  768. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  769. {
  770. double left, right;
  771. value = ctrlEvent.value/0.5 - 1.0;
  772. if (value < 0.0)
  773. {
  774. left = -1.0;
  775. right = (value*2)+1.0;
  776. }
  777. else if (value > 0.0)
  778. {
  779. left = (value*2)-1.0;
  780. right = 1.0;
  781. }
  782. else
  783. {
  784. left = -1.0;
  785. right = 1.0;
  786. }
  787. setBalanceLeft(left, false, false);
  788. setBalanceRight(right, false, false);
  789. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  790. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  791. continue;
  792. }
  793. }
  794. // Control plugin parameters
  795. for (k=0; k < kData->param.count; k++)
  796. {
  797. if (kData->param.data[k].midiChannel != event.channel)
  798. continue;
  799. if (kData->param.data[k].midiCC != ctrlEvent.param)
  800. continue;
  801. if (kData->param.data[k].type != PARAMETER_INPUT)
  802. continue;
  803. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  804. continue;
  805. double value;
  806. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  807. {
  808. value = (ctrlEvent.value < 0.5) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  809. }
  810. else
  811. {
  812. // FIXME - ranges call for this
  813. value = ctrlEvent.value * (kData->param.ranges[k].max - kData->param.ranges[k].min) + kData->param.ranges[k].min;
  814. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  815. value = std::rint(value);
  816. }
  817. setParameterValue(k, value, false, false, false);
  818. postponeRtEvent(kPluginPostRtEventParameterChange, k, 0, value);
  819. }
  820. break;
  821. }
  822. case kEngineControlEventTypeMidiBank:
  823. if (event.channel == kData->ctrlInChannel)
  824. nextBankId = ctrlEvent.param;
  825. break;
  826. case kEngineControlEventTypeMidiProgram:
  827. if (event.channel == kData->ctrlInChannel)
  828. {
  829. uint32_t nextProgramId = ctrlEvent.param;
  830. for (k=0; k < kData->midiprog.count; k++)
  831. {
  832. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  833. {
  834. setMidiProgram(k, false, false, false, false);
  835. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0);
  836. break;
  837. }
  838. }
  839. }
  840. break;
  841. case kEngineControlEventTypeAllSoundOff:
  842. if (event.channel == kData->ctrlInChannel)
  843. {
  844. if (/*midi.portMin &&*/ ! allNotesOffSent)
  845. sendMidiAllNotesOff();
  846. if (fDescriptor->deactivate != nullptr)
  847. {
  848. fDescriptor->deactivate(fHandle);
  849. if (fHandle2 != nullptr)
  850. fDescriptor->deactivate(fHandle2);
  851. }
  852. if (fDescriptor->activate != nullptr)
  853. {
  854. fDescriptor->activate(fHandle);
  855. if (fHandle2 != nullptr)
  856. fDescriptor->activate(fHandle2);
  857. }
  858. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  859. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  860. allNotesOffSent = true;
  861. }
  862. break;
  863. case kEngineControlEventTypeAllNotesOff:
  864. if (event.channel == kData->ctrlInChannel)
  865. {
  866. if (/*midi.portMin &&*/ ! allNotesOffSent)
  867. sendMidiAllNotesOff();
  868. allNotesOffSent = true;
  869. }
  870. break;
  871. }
  872. break;
  873. }
  874. case kEngineEventTypeMidi:
  875. {
  876. if (midiEventCount >= MAX_MIDI_EVENTS)
  877. continue;
  878. const EngineMidiEvent& midiEvent = event.midi;
  879. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  880. uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(midiEvent.data);
  881. // Fix bad note-off (per DSSI spec)
  882. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  883. status -= 0x10;
  884. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  885. fMidiEvents[midiEventCount].time.tick = time;
  886. if (MIDI_IS_STATUS_NOTE_OFF(status))
  887. {
  888. const uint8_t note = midiEvent.data[1];
  889. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEOFF;
  890. fMidiEvents[midiEventCount].data.note.channel = channel;
  891. fMidiEvents[midiEventCount].data.note.note = note;
  892. postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0);
  893. }
  894. else if (MIDI_IS_STATUS_NOTE_ON(status))
  895. {
  896. const uint8_t note = midiEvent.data[1];
  897. const uint8_t velo = midiEvent.data[2];
  898. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEON;
  899. fMidiEvents[midiEventCount].data.note.channel = channel;
  900. fMidiEvents[midiEventCount].data.note.note = note;
  901. fMidiEvents[midiEventCount].data.note.velocity = velo;
  902. postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  903. }
  904. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  905. {
  906. const uint8_t note = midiEvent.data[1];
  907. const uint8_t pressure = midiEvent.data[2];
  908. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_KEYPRESS;
  909. fMidiEvents[midiEventCount].data.note.channel = channel;
  910. fMidiEvents[midiEventCount].data.note.note = note;
  911. fMidiEvents[midiEventCount].data.note.velocity = pressure;
  912. }
  913. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  914. {
  915. const uint8_t pressure = midiEvent.data[1];
  916. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CHANPRESS;
  917. fMidiEvents[midiEventCount].data.control.channel = channel;
  918. fMidiEvents[midiEventCount].data.control.value = pressure;
  919. }
  920. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  921. {
  922. const uint8_t lsb = midiEvent.data[1];
  923. const uint8_t msb = midiEvent.data[2];
  924. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_PITCHBEND;
  925. fMidiEvents[midiEventCount].data.control.channel = channel;
  926. fMidiEvents[midiEventCount].data.control.value = ((msb << 7) | lsb) - 8192;
  927. }
  928. else
  929. continue;
  930. midiEventCount += 1;
  931. break;
  932. }
  933. }
  934. }
  935. kData->postRtEvents.trySplice();
  936. // ----------------------------------------------------------------------------------------------------
  937. // MIDI Input (External)
  938. if (kData->extNotes.mutex.tryLock())
  939. {
  940. while (midiEventCount < MAX_MIDI_EVENTS && ! kData->extNotes.data.isEmpty())
  941. {
  942. const ExternalMidiNote& note = kData->extNotes.data.getFirst(true);
  943. CARLA_ASSERT(note.channel >= 0);
  944. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  945. fMidiEvents[midiEventCount].type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  946. fMidiEvents[midiEventCount].data.note.channel = note.channel;
  947. fMidiEvents[midiEventCount].data.note.note = note.note;
  948. fMidiEvents[midiEventCount].data.note.velocity = note.velo;
  949. midiEventCount += 1;
  950. }
  951. kData->extNotes.mutex.unlock();
  952. } // End of MIDI Input (External)
  953. } // End of Event Input
  954. CARLA_PROCESS_CONTINUE_CHECK;
  955. // --------------------------------------------------------------------------------------------------------
  956. // Special Parameters
  957. #if 0
  958. for (k=0; k < param.count; k++)
  959. {
  960. if (param.data[k].type == PARAMETER_LATENCY)
  961. {
  962. // TODO
  963. }
  964. }
  965. CARLA_PROCESS_CONTINUE_CHECK;
  966. #endif
  967. // --------------------------------------------------------------------------------------------------------
  968. // Plugin processing
  969. {
  970. if (! kData->activeBefore)
  971. {
  972. #if 0
  973. if (midi.portMin)
  974. {
  975. for (k=0; k < MAX_MIDI_CHANNELS; k++)
  976. {
  977. memset(&midiEvents[k], 0, sizeof(snd_seq_event_t)); //FIXME
  978. midiEvents[k].type = SND_SEQ_EVENT_CONTROLLER;
  979. midiEvents[k].data.control.channel = k;
  980. midiEvents[k].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  981. memset(&midiEvents[k*2], 0, sizeof(snd_seq_event_t)); //FIXME
  982. midiEvents[k*2].type = SND_SEQ_EVENT_CONTROLLER;
  983. midiEvents[k*2].data.control.channel = k;
  984. midiEvents[k*2].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  985. }
  986. midiEventCount = MAX_MIDI_CHANNELS;
  987. }
  988. #endif
  989. if (kData->latency > 0)
  990. {
  991. for (i=0; i < kData->audioIn.count; i++)
  992. carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  993. }
  994. if (fDescriptor->activate != nullptr)
  995. {
  996. fDescriptor->activate(fHandle);
  997. if (fHandle2 != nullptr)
  998. fDescriptor->activate(fHandle2);
  999. }
  1000. }
  1001. if (fHandle2 == nullptr)
  1002. {
  1003. for (i=0; i < kData->audioIn.count; i++)
  1004. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, inBuffer[i]);
  1005. for (i=0; i < kData->audioOut.count; i++)
  1006. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, outBuffer[i]);
  1007. }
  1008. else
  1009. {
  1010. if (kData->audioIn.count > 0)
  1011. {
  1012. CARLA_ASSERT(kData->audioIn.count == 2);
  1013. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, inBuffer[0]);
  1014. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, inBuffer[1]);
  1015. }
  1016. if (kData->audioOut.count > 0)
  1017. {
  1018. CARLA_ASSERT(kData->audioOut.count == 2);
  1019. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, outBuffer[0]);
  1020. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, outBuffer[1]);
  1021. }
  1022. }
  1023. if (fDssiDescriptor->run_synth != nullptr)
  1024. {
  1025. fDssiDescriptor->run_synth(fHandle, frames, fMidiEvents, midiEventCount);
  1026. if (fHandle2)
  1027. fDssiDescriptor->run_synth(fHandle2, frames, fMidiEvents, midiEventCount);
  1028. }
  1029. else if (fDssiDescriptor->run_multiple_synths != nullptr)
  1030. {
  1031. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1032. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1033. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1034. fDssiDescriptor->run_multiple_synths((fHandle2 != nullptr) ? 2 : 1, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1035. }
  1036. else
  1037. {
  1038. fDescriptor->run(fHandle, frames);
  1039. if (fHandle2)
  1040. fDescriptor->run(fHandle2, frames);
  1041. }
  1042. } // End of Plugin processing
  1043. CARLA_PROCESS_CONTINUE_CHECK;
  1044. // --------------------------------------------------------------------------------------------------------
  1045. // Post-processing (dry/wet, volume and balance)
  1046. {
  1047. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) > 0 && kData->postProc.dryWet != 1.0f;
  1048. const bool doVolume = (fHints & PLUGIN_CAN_VOLUME) > 0 && kData->postProc.volume != 1.0f;
  1049. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1050. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1051. for (i=0; i < kData->audioOut.count; i++)
  1052. {
  1053. // Dry/Wet
  1054. if (doDryWet)
  1055. {
  1056. #if 0
  1057. for (k=0; k < frames; k++)
  1058. {
  1059. if (k < m_latency && m_latency < frames)
  1060. bufValue = (aIn.count == 1) ? m_latencyBuffers[0][k] : m_latencyBuffers[i][k];
  1061. else
  1062. bufValue = (aIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1063. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(bufValue*(1.0-x_dryWet));
  1064. }
  1065. #endif
  1066. for (k=0; k < frames; k++)
  1067. {
  1068. // TODO
  1069. //if (k < kData->latency && kData->latency < frames)
  1070. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  1071. //else
  1072. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1073. bufValue = inBuffer[ (kData->audioIn.count == 1) ? 0 : i ][k];
  1074. outBuffer[i][k] = (outBuffer[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1075. }
  1076. }
  1077. // Balance
  1078. if (doBalance)
  1079. {
  1080. if (i % 2 == 0)
  1081. std::memcpy(oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1082. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1083. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1084. for (k=0; k < frames; k++)
  1085. {
  1086. if (i % 2 == 0)
  1087. {
  1088. // left
  1089. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1090. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  1091. }
  1092. else
  1093. {
  1094. // right
  1095. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  1096. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  1097. }
  1098. }
  1099. }
  1100. // Volume
  1101. if (doVolume)
  1102. {
  1103. for (k=0; k < frames; k++)
  1104. outBuffer[i][k] *= kData->postProc.volume;
  1105. }
  1106. }
  1107. #if 0
  1108. // Latency, save values for next callback, TODO
  1109. if (kData->latency > 0 && kData->latency < frames)
  1110. {
  1111. for (i=0; i < kData->audioIn.count; i++)
  1112. std::memcpy(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), sizeof(float)*kData->latency);
  1113. }
  1114. #endif
  1115. } // End of Post-processing
  1116. CARLA_PROCESS_CONTINUE_CHECK;
  1117. // --------------------------------------------------------------------------------------------------------
  1118. // Control Output
  1119. if (kData->event.portOut != nullptr)
  1120. {
  1121. float value;
  1122. for (k=0; k < kData->param.count; k++)
  1123. {
  1124. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1125. continue;
  1126. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  1127. if (kData->param.data[k].midiCC > 0)
  1128. {
  1129. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  1130. kData->event.portOut->writeControlEvent(framesOffset, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  1131. }
  1132. }
  1133. } // End of Control Output
  1134. CARLA_PROCESS_CONTINUE_CHECK;
  1135. // --------------------------------------------------------------------------------------------------------
  1136. kData->activeBefore = kData->active;
  1137. }
  1138. // -------------------------------------------------------------------
  1139. // Post-poned events
  1140. void uiParameterChange(const uint32_t index, const float value)
  1141. {
  1142. CARLA_ASSERT(index < kData->param.count);
  1143. if (index >= kData->param.count)
  1144. return;
  1145. if (kData->osc.data.target == nullptr)
  1146. return;
  1147. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  1148. }
  1149. void uiMidiProgramChange(const uint32_t index)
  1150. {
  1151. CARLA_ASSERT(index < kData->midiprog.count);
  1152. if (index >= kData->midiprog.count)
  1153. return;
  1154. if (kData->osc.data.target == nullptr)
  1155. return;
  1156. osc_send_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1157. }
  1158. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1159. {
  1160. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1161. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1162. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1163. if (channel >= MAX_MIDI_CHANNELS)
  1164. return;
  1165. if (note >= MAX_MIDI_NOTE)
  1166. return;
  1167. if (velo >= MAX_MIDI_VALUE)
  1168. return;
  1169. if (kData->osc.data.target == nullptr)
  1170. return;
  1171. uint8_t midiData[4] = { 0 };
  1172. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1173. midiData[2] = note;
  1174. midiData[3] = velo;
  1175. osc_send_midi(&kData->osc.data, midiData);
  1176. }
  1177. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1178. {
  1179. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1180. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1181. if (channel >= MAX_MIDI_CHANNELS)
  1182. return;
  1183. if (note >= MAX_MIDI_NOTE)
  1184. return;
  1185. if (kData->osc.data.target == nullptr)
  1186. return;
  1187. uint8_t midiData[4] = { 0 };
  1188. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1189. midiData[2] = note;
  1190. osc_send_midi(&kData->osc.data, midiData);
  1191. }
  1192. // -------------------------------------------------------------------
  1193. // Cleanup
  1194. void deleteBuffers()
  1195. {
  1196. qDebug("DssiPlugin::deleteBuffers() - start");
  1197. if (fParamBuffers != nullptr)
  1198. {
  1199. delete[] fParamBuffers;
  1200. fParamBuffers = nullptr;
  1201. }
  1202. CarlaPlugin::deleteBuffers();
  1203. qDebug("DssiPlugin::deleteBuffers() - end");
  1204. }
  1205. // -------------------------------------------------------------------
  1206. bool init(const char* const filename, const char* const name, const char* const label, const char* const guiFilename)
  1207. {
  1208. CARLA_ASSERT(kData->engine != nullptr);
  1209. CARLA_ASSERT(kData->client == nullptr);
  1210. CARLA_ASSERT(filename != nullptr);
  1211. CARLA_ASSERT(label != nullptr);
  1212. // ---------------------------------------------------------------
  1213. // open DLL
  1214. if (! libOpen(filename))
  1215. {
  1216. kData->engine->setLastError(libError(filename));
  1217. return false;
  1218. }
  1219. // ---------------------------------------------------------------
  1220. // get DLL main entry
  1221. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)libSymbol("dssi_descriptor");
  1222. if (descFn == nullptr)
  1223. {
  1224. kData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1225. return false;
  1226. }
  1227. // ---------------------------------------------------------------
  1228. // get descriptor that matches label
  1229. unsigned long i = 0;
  1230. while ((fDssiDescriptor = descFn(i++)) != nullptr)
  1231. {
  1232. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1233. if (fDescriptor != nullptr && fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1234. break;
  1235. }
  1236. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1237. {
  1238. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1239. return false;
  1240. }
  1241. // ---------------------------------------------------------------
  1242. // get info
  1243. if (name != nullptr)
  1244. fName = kData->engine->getNewUniquePluginName(name);
  1245. else if (fDescriptor->Name != nullptr)
  1246. fName = kData->engine->getNewUniquePluginName(fDescriptor->Name);
  1247. else
  1248. fName = kData->engine->getNewUniquePluginName(fDescriptor->Label);
  1249. fFilename = filename;
  1250. // ---------------------------------------------------------------
  1251. // register client
  1252. kData->client = kData->engine->addClient(this);
  1253. if (kData->client == nullptr || ! kData->client->isOk())
  1254. {
  1255. kData->engine->setLastError("Failed to register plugin client");
  1256. return false;
  1257. }
  1258. // ---------------------------------------------------------------
  1259. // initialize plugin
  1260. fHandle = fDescriptor->instantiate(fDescriptor, kData->engine->getSampleRate());
  1261. if (fHandle == nullptr)
  1262. {
  1263. kData->engine->setLastError("Plugin failed to initialize");
  1264. return false;
  1265. }
  1266. // ---------------------------------------------------------------
  1267. // gui stuff
  1268. if (guiFilename)
  1269. {
  1270. kData->osc.thread = new CarlaPluginThread(kData->engine, this, CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  1271. kData->osc.thread->setOscData(guiFilename, fDescriptor->Label);
  1272. fHints |= PLUGIN_HAS_GUI;
  1273. }
  1274. return true;
  1275. }
  1276. private:
  1277. LADSPA_Handle fHandle;
  1278. LADSPA_Handle fHandle2;
  1279. const LADSPA_Descriptor* fDescriptor;
  1280. const DSSI_Descriptor* fDssiDescriptor;
  1281. snd_seq_event_t fMidiEvents[MAX_MIDI_EVENTS];
  1282. float* fParamBuffers;
  1283. QByteArray fChunk;
  1284. };
  1285. CARLA_BACKEND_END_NAMESPACE
  1286. #else // WANT_DSSI
  1287. # warning Building without DSSI support
  1288. #endif
  1289. CARLA_BACKEND_START_NAMESPACE
  1290. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init, const char* const guiFilename)
  1291. {
  1292. qDebug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\"}, \"%s\")", init.engine, init.filename, init.name, init.label, guiFilename);
  1293. #ifdef WANT_DSSI
  1294. DssiPlugin* const plugin = new DssiPlugin(init.engine, init.id);
  1295. if (! plugin->init(init.filename, init.name, init.label, guiFilename))
  1296. {
  1297. delete plugin;
  1298. return nullptr;
  1299. }
  1300. plugin->reload();
  1301. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && (plugin->hints() & PLUGIN_CAN_FORCE_STEREO) == 0)
  1302. {
  1303. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1304. delete plugin;
  1305. return nullptr;
  1306. }
  1307. plugin->registerToOscClient();
  1308. return plugin;
  1309. #else
  1310. init.engine->setLastError("DSSI support not available");
  1311. return nullptr;
  1312. #endif
  1313. }
  1314. CARLA_BACKEND_END_NAMESPACE