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.

1657 lines
58KB

  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. 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. void reloadPrograms(const bool init)
  614. {
  615. qDebug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  616. uint32_t i, oldCount = kData->midiprog.count;
  617. // Delete old programs
  618. kData->midiprog.clear();
  619. // Query new programs
  620. uint32_t count = 0;
  621. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  622. {
  623. while (fDssiDescriptor->get_program(fHandle, count))
  624. count++;
  625. }
  626. if (count > 0)
  627. kData->midiprog.createNew(count);
  628. // Update data
  629. for (i=0; i < kData->midiprog.count; i++)
  630. {
  631. const DSSI_Program_Descriptor* const pdesc = fDssiDescriptor->get_program(fHandle, i);
  632. CARLA_ASSERT(pdesc != nullptr);
  633. CARLA_ASSERT(pdesc->Name != nullptr);
  634. kData->midiprog.data[i].bank = pdesc->Bank;
  635. kData->midiprog.data[i].program = pdesc->Program;
  636. kData->midiprog.data[i].name = carla_strdup(pdesc->Name);
  637. }
  638. #ifndef BUILD_BRIDGE
  639. // Update OSC Names
  640. if (kData->engine->isOscControlRegistered())
  641. {
  642. kData->engine->osc_send_control_set_midi_program_count(fId, kData->midiprog.count);
  643. for (i=0; i < kData->midiprog.count; i++)
  644. kData->engine->osc_send_control_set_midi_program_data(fId, i, kData->midiprog.data[i].bank, kData->midiprog.data[i].program, kData->midiprog.data[i].name);
  645. }
  646. #endif
  647. if (init)
  648. {
  649. if (kData->midiprog.count > 0)
  650. setMidiProgram(0, false, false, false, true);
  651. }
  652. else
  653. {
  654. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  655. // Check if current program is invalid
  656. bool programChanged = false;
  657. if (kData->midiprog.count == oldCount+1)
  658. {
  659. // one midi program added, probably created by user
  660. kData->midiprog.current = oldCount;
  661. programChanged = true;
  662. }
  663. else if (kData->midiprog.current >= static_cast<int32_t>(kData->midiprog.count))
  664. {
  665. // current midi program > count
  666. kData->midiprog.current = 0;
  667. programChanged = true;
  668. }
  669. else if (kData->midiprog.current < 0 && kData->midiprog.count > 0)
  670. {
  671. // programs exist now, but not before
  672. kData->midiprog.current = 0;
  673. programChanged = true;
  674. }
  675. else if (kData->midiprog.current >= 0 && kData->midiprog.count == 0)
  676. {
  677. // programs existed before, but not anymore
  678. kData->midiprog.current = -1;
  679. programChanged = true;
  680. }
  681. if (programChanged)
  682. setMidiProgram(kData->midiprog.current, true, true, true, true);
  683. }
  684. }
  685. // -------------------------------------------------------------------
  686. // Plugin processing
  687. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  688. {
  689. uint32_t i, k;
  690. unsigned long midiEventCount = 0;
  691. // --------------------------------------------------------------------------------------------------------
  692. // Check if active
  693. if (! kData->active)
  694. {
  695. // disable any output sound
  696. for (i=0; i < kData->audioOut.count; i++)
  697. carla_zeroFloat(outBuffer[i], frames);
  698. if (kData->activeBefore)
  699. {
  700. if (fDescriptor->deactivate != nullptr)
  701. {
  702. fDescriptor->deactivate(fHandle);
  703. if (fHandle2 != nullptr)
  704. fDescriptor->deactivate(fHandle2);
  705. }
  706. }
  707. kData->activeBefore = kData->active;
  708. return;
  709. }
  710. // --------------------------------------------------------------------------------------------------------
  711. // Check if active before
  712. if (! kData->activeBefore)
  713. {
  714. if (kData->event.portIn != nullptr)
  715. {
  716. for (k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; k++)
  717. {
  718. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[k]);
  719. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[k+i]);
  720. fMidiEvents[k].type = SND_SEQ_EVENT_CONTROLLER;
  721. fMidiEvents[k].data.control.channel = k;
  722. fMidiEvents[k].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  723. fMidiEvents[k+i].type = SND_SEQ_EVENT_CONTROLLER;
  724. fMidiEvents[k+i].data.control.channel = k;
  725. fMidiEvents[k+i].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  726. }
  727. midiEventCount = MAX_MIDI_CHANNELS*2;
  728. }
  729. if (kData->latency > 0)
  730. {
  731. for (i=0; i < kData->audioIn.count; i++)
  732. carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  733. }
  734. if (fDescriptor->activate != nullptr)
  735. {
  736. fDescriptor->activate(fHandle);
  737. if (fHandle2 != nullptr)
  738. fDescriptor->activate(fHandle2);
  739. }
  740. }
  741. // --------------------------------------------------------------------------------------------------------
  742. // Event Input and Processing
  743. else if (kData->event.portIn != nullptr)
  744. {
  745. // ----------------------------------------------------------------------------------------------------
  746. // MIDI Input (External)
  747. if (kData->extNotes.mutex.tryLock())
  748. {
  749. while (midiEventCount < MAX_MIDI_EVENTS && ! kData->extNotes.data.isEmpty())
  750. {
  751. const ExternalMidiNote& note = kData->extNotes.data.getFirst(true);
  752. CARLA_ASSERT(note.channel >= 0);
  753. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  754. fMidiEvents[midiEventCount].type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  755. fMidiEvents[midiEventCount].data.note.channel = note.channel;
  756. fMidiEvents[midiEventCount].data.note.note = note.note;
  757. fMidiEvents[midiEventCount].data.note.velocity = note.velo;
  758. midiEventCount += 1;
  759. }
  760. kData->extNotes.mutex.unlock();
  761. } // End of MIDI Input (External)
  762. // ----------------------------------------------------------------------------------------------------
  763. // Event Input (System)
  764. bool allNotesOffSent = false;
  765. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  766. uint32_t timeOffset = 0;
  767. uint32_t nextBankId = 0;
  768. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  769. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  770. for (i=0; i < nEvents; i++)
  771. {
  772. const EngineEvent& event = kData->event.portIn->getEvent(i);
  773. time = event.time - framesOffset;
  774. if (time >= frames)
  775. continue;
  776. CARLA_ASSERT(time >= timeOffset);
  777. if (time > timeOffset)
  778. {
  779. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset, midiEventCount);
  780. midiEventCount = 0;
  781. timeOffset = time;
  782. }
  783. // Control change
  784. switch (event.type)
  785. {
  786. case kEngineEventTypeNull:
  787. break;
  788. case kEngineEventTypeControl:
  789. {
  790. const EngineControlEvent& ctrlEvent = event.ctrl;
  791. switch (ctrlEvent.type)
  792. {
  793. case kEngineControlEventTypeNull:
  794. break;
  795. case kEngineControlEventTypeParameter:
  796. {
  797. // Control backend stuff
  798. if (event.channel == kData->ctrlInChannel)
  799. {
  800. double value;
  801. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  802. {
  803. value = ctrlEvent.value;
  804. setDryWet(value, false, false);
  805. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  806. continue;
  807. }
  808. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  809. {
  810. value = ctrlEvent.value*127/100;
  811. setVolume(value, false, false);
  812. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  813. continue;
  814. }
  815. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  816. {
  817. double left, right;
  818. value = ctrlEvent.value/0.5 - 1.0;
  819. if (value < 0.0)
  820. {
  821. left = -1.0;
  822. right = (value*2)+1.0;
  823. }
  824. else if (value > 0.0)
  825. {
  826. left = (value*2)-1.0;
  827. right = 1.0;
  828. }
  829. else
  830. {
  831. left = -1.0;
  832. right = 1.0;
  833. }
  834. setBalanceLeft(left, false, false);
  835. setBalanceRight(right, false, false);
  836. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  837. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  838. continue;
  839. }
  840. }
  841. // Control plugin parameters
  842. for (k=0; k < kData->param.count; k++)
  843. {
  844. if (kData->param.data[k].midiChannel != event.channel)
  845. continue;
  846. if (kData->param.data[k].midiCC != ctrlEvent.param)
  847. continue;
  848. if (kData->param.data[k].type != PARAMETER_INPUT)
  849. continue;
  850. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  851. continue;
  852. double value;
  853. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  854. {
  855. value = (ctrlEvent.value < 0.5) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  856. }
  857. else
  858. {
  859. // FIXME - ranges call for this
  860. value = ctrlEvent.value * (kData->param.ranges[k].max - kData->param.ranges[k].min) + kData->param.ranges[k].min;
  861. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  862. value = std::rint(value);
  863. }
  864. setParameterValue(k, value, false, false, false);
  865. postponeRtEvent(kPluginPostRtEventParameterChange, k, 0, value);
  866. }
  867. break;
  868. }
  869. case kEngineControlEventTypeMidiBank:
  870. if (event.channel == kData->ctrlInChannel)
  871. nextBankId = ctrlEvent.param;
  872. break;
  873. case kEngineControlEventTypeMidiProgram:
  874. if (event.channel == kData->ctrlInChannel)
  875. {
  876. const uint32_t nextProgramId = ctrlEvent.param;
  877. for (k=0; k < kData->midiprog.count; k++)
  878. {
  879. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  880. {
  881. setMidiProgram(k, false, false, false, false);
  882. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0);
  883. break;
  884. }
  885. }
  886. }
  887. break;
  888. case kEngineControlEventTypeAllSoundOff:
  889. if (event.channel == kData->ctrlInChannel)
  890. {
  891. if (! allNotesOffSent)
  892. sendMidiAllNotesOff();
  893. if (fDescriptor->deactivate != nullptr)
  894. {
  895. fDescriptor->deactivate(fHandle);
  896. if (fHandle2 != nullptr)
  897. fDescriptor->deactivate(fHandle2);
  898. }
  899. if (fDescriptor->activate != nullptr)
  900. {
  901. fDescriptor->activate(fHandle);
  902. if (fHandle2 != nullptr)
  903. fDescriptor->activate(fHandle2);
  904. }
  905. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  906. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  907. allNotesOffSent = true;
  908. }
  909. break;
  910. case kEngineControlEventTypeAllNotesOff:
  911. if (event.channel == kData->ctrlInChannel)
  912. {
  913. if (! allNotesOffSent)
  914. sendMidiAllNotesOff();
  915. allNotesOffSent = true;
  916. }
  917. break;
  918. }
  919. break;
  920. }
  921. case kEngineEventTypeMidi:
  922. {
  923. if (midiEventCount >= MAX_MIDI_EVENTS)
  924. continue;
  925. const EngineMidiEvent& midiEvent = event.midi;
  926. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  927. uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(midiEvent.data);
  928. // Fix bad note-off (per DSSI spec)
  929. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  930. status -= 0x10;
  931. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  932. fMidiEvents[midiEventCount].time.tick = time;
  933. if (MIDI_IS_STATUS_NOTE_OFF(status))
  934. {
  935. const uint8_t note = midiEvent.data[1];
  936. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEOFF;
  937. fMidiEvents[midiEventCount].data.note.channel = channel;
  938. fMidiEvents[midiEventCount].data.note.note = note;
  939. postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0);
  940. }
  941. else if (MIDI_IS_STATUS_NOTE_ON(status))
  942. {
  943. const uint8_t note = midiEvent.data[1];
  944. const uint8_t velo = midiEvent.data[2];
  945. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEON;
  946. fMidiEvents[midiEventCount].data.note.channel = channel;
  947. fMidiEvents[midiEventCount].data.note.note = note;
  948. fMidiEvents[midiEventCount].data.note.velocity = velo;
  949. postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  950. }
  951. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  952. {
  953. const uint8_t note = midiEvent.data[1];
  954. const uint8_t pressure = midiEvent.data[2];
  955. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_KEYPRESS;
  956. fMidiEvents[midiEventCount].data.note.channel = channel;
  957. fMidiEvents[midiEventCount].data.note.note = note;
  958. fMidiEvents[midiEventCount].data.note.velocity = pressure;
  959. }
  960. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  961. {
  962. const uint8_t pressure = midiEvent.data[1];
  963. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CHANPRESS;
  964. fMidiEvents[midiEventCount].data.control.channel = channel;
  965. fMidiEvents[midiEventCount].data.control.value = pressure;
  966. }
  967. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  968. {
  969. const uint8_t lsb = midiEvent.data[1];
  970. const uint8_t msb = midiEvent.data[2];
  971. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_PITCHBEND;
  972. fMidiEvents[midiEventCount].data.control.channel = channel;
  973. fMidiEvents[midiEventCount].data.control.value = ((msb << 7) | lsb) - 8192;
  974. }
  975. else
  976. continue;
  977. midiEventCount += 1;
  978. break;
  979. }
  980. }
  981. }
  982. kData->postRtEvents.trySplice();
  983. if (frames > timeOffset)
  984. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset, midiEventCount);
  985. } // End of Event Input and Processing
  986. // --------------------------------------------------------------------------------------------------------
  987. // Plugin processing (no events)
  988. else
  989. {
  990. processSingle(inBuffer, outBuffer, frames, 0, 0);
  991. } // End of Plugin processing (no events)
  992. // --------------------------------------------------------------------------------------------------------
  993. // Special Parameters
  994. #if 0
  995. CARLA_PROCESS_CONTINUE_CHECK;
  996. for (k=0; k < param.count; k++)
  997. {
  998. if (param.data[k].type == PARAMETER_LATENCY)
  999. {
  1000. // TODO
  1001. }
  1002. }
  1003. CARLA_PROCESS_CONTINUE_CHECK;
  1004. #endif
  1005. CARLA_PROCESS_CONTINUE_CHECK;
  1006. // --------------------------------------------------------------------------------------------------------
  1007. // Post-processing (dry/wet, volume and balance)
  1008. {
  1009. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) > 0 && kData->postProc.dryWet != 1.0f;
  1010. const bool doVolume = (fHints & PLUGIN_CAN_VOLUME) > 0 && kData->postProc.volume != 1.0f;
  1011. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1012. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1013. for (i=0; i < kData->audioOut.count; i++)
  1014. {
  1015. // Dry/Wet
  1016. if (doDryWet)
  1017. {
  1018. #if 0
  1019. for (k=0; k < frames; k++)
  1020. {
  1021. if (k < m_latency && m_latency < frames)
  1022. bufValue = (aIn.count == 1) ? m_latencyBuffers[0][k] : m_latencyBuffers[i][k];
  1023. else
  1024. bufValue = (aIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1025. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(bufValue*(1.0-x_dryWet));
  1026. }
  1027. #endif
  1028. for (k=0; k < frames; k++)
  1029. {
  1030. // TODO
  1031. //if (k < kData->latency && kData->latency < frames)
  1032. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  1033. //else
  1034. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1035. bufValue = inBuffer[ (kData->audioIn.count == 1) ? 0 : i ][k];
  1036. outBuffer[i][k] = (outBuffer[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1037. }
  1038. }
  1039. // Balance
  1040. if (doBalance)
  1041. {
  1042. if (i % 2 == 0)
  1043. std::memcpy(oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1044. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1045. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1046. for (k=0; k < frames; k++)
  1047. {
  1048. if (i % 2 == 0)
  1049. {
  1050. // left
  1051. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1052. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  1053. }
  1054. else
  1055. {
  1056. // right
  1057. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  1058. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  1059. }
  1060. }
  1061. }
  1062. // Volume
  1063. if (doVolume)
  1064. {
  1065. for (k=0; k < frames; k++)
  1066. outBuffer[i][k] *= kData->postProc.volume;
  1067. }
  1068. }
  1069. #if 0
  1070. // Latency, save values for next callback, TODO
  1071. if (kData->latency > 0 && kData->latency < frames)
  1072. {
  1073. for (i=0; i < kData->audioIn.count; i++)
  1074. std::memcpy(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), sizeof(float)*kData->latency);
  1075. }
  1076. #endif
  1077. } // End of Post-processing
  1078. CARLA_PROCESS_CONTINUE_CHECK;
  1079. // --------------------------------------------------------------------------------------------------------
  1080. // Control Output
  1081. if (kData->event.portOut != nullptr)
  1082. {
  1083. float value;
  1084. for (k=0; k < kData->param.count; k++)
  1085. {
  1086. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1087. continue;
  1088. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  1089. if (kData->param.data[k].midiCC > 0)
  1090. {
  1091. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  1092. kData->event.portOut->writeControlEvent(framesOffset, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  1093. }
  1094. }
  1095. } // End of Control Output
  1096. CARLA_PROCESS_CONTINUE_CHECK;
  1097. // --------------------------------------------------------------------------------------------------------
  1098. kData->activeBefore = kData->active;
  1099. }
  1100. void processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset, const uint32_t midiEventCount)
  1101. {
  1102. if (fHandle2 == nullptr)
  1103. {
  1104. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1105. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, inBuffer[i]+timeOffset);
  1106. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1107. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, outBuffer[i]+timeOffset);
  1108. }
  1109. else
  1110. {
  1111. if (kData->audioIn.count > 0)
  1112. {
  1113. CARLA_ASSERT(kData->audioIn.count == 2);
  1114. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, inBuffer[0]+timeOffset);
  1115. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, inBuffer[1]+timeOffset);
  1116. }
  1117. if (kData->audioOut.count > 0)
  1118. {
  1119. CARLA_ASSERT(kData->audioOut.count == 2);
  1120. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, outBuffer[0]+timeOffset);
  1121. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, outBuffer[1]+timeOffset);
  1122. }
  1123. }
  1124. if (fDssiDescriptor->run_synth != nullptr)
  1125. {
  1126. fDssiDescriptor->run_synth(fHandle, frames, fMidiEvents, midiEventCount);
  1127. if (fHandle2)
  1128. fDssiDescriptor->run_synth(fHandle2, frames, fMidiEvents, midiEventCount);
  1129. }
  1130. else if (fDssiDescriptor->run_multiple_synths != nullptr)
  1131. {
  1132. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1133. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1134. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1135. fDssiDescriptor->run_multiple_synths((fHandle2 != nullptr) ? 2 : 1, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1136. }
  1137. else
  1138. {
  1139. fDescriptor->run(fHandle, frames);
  1140. if (fHandle2)
  1141. fDescriptor->run(fHandle2, frames);
  1142. }
  1143. }
  1144. // -------------------------------------------------------------------
  1145. // Post-poned events
  1146. void uiParameterChange(const uint32_t index, const float value)
  1147. {
  1148. CARLA_ASSERT(index < kData->param.count);
  1149. if (index >= kData->param.count)
  1150. return;
  1151. if (kData->osc.data.target == nullptr)
  1152. return;
  1153. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  1154. }
  1155. void uiMidiProgramChange(const uint32_t index)
  1156. {
  1157. CARLA_ASSERT(index < kData->midiprog.count);
  1158. if (index >= kData->midiprog.count)
  1159. return;
  1160. if (kData->osc.data.target == nullptr)
  1161. return;
  1162. osc_send_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1163. }
  1164. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1165. {
  1166. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1167. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1168. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1169. if (channel >= MAX_MIDI_CHANNELS)
  1170. return;
  1171. if (note >= MAX_MIDI_NOTE)
  1172. return;
  1173. if (velo >= MAX_MIDI_VALUE)
  1174. return;
  1175. if (kData->osc.data.target == nullptr)
  1176. return;
  1177. uint8_t midiData[4] = { 0 };
  1178. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1179. midiData[2] = note;
  1180. midiData[3] = velo;
  1181. osc_send_midi(&kData->osc.data, midiData);
  1182. }
  1183. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1184. {
  1185. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1186. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1187. if (channel >= MAX_MIDI_CHANNELS)
  1188. return;
  1189. if (note >= MAX_MIDI_NOTE)
  1190. return;
  1191. if (kData->osc.data.target == nullptr)
  1192. return;
  1193. uint8_t midiData[4] = { 0 };
  1194. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1195. midiData[2] = note;
  1196. osc_send_midi(&kData->osc.data, midiData);
  1197. }
  1198. // -------------------------------------------------------------------
  1199. // Cleanup
  1200. void deleteBuffers()
  1201. {
  1202. qDebug("DssiPlugin::deleteBuffers() - start");
  1203. if (fParamBuffers != nullptr)
  1204. {
  1205. delete[] fParamBuffers;
  1206. fParamBuffers = nullptr;
  1207. }
  1208. CarlaPlugin::deleteBuffers();
  1209. qDebug("DssiPlugin::deleteBuffers() - end");
  1210. }
  1211. // -------------------------------------------------------------------
  1212. bool init(const char* const filename, const char* const name, const char* const label, const char* const guiFilename)
  1213. {
  1214. CARLA_ASSERT(kData->engine != nullptr);
  1215. CARLA_ASSERT(kData->client == nullptr);
  1216. CARLA_ASSERT(filename != nullptr);
  1217. CARLA_ASSERT(label != nullptr);
  1218. // ---------------------------------------------------------------
  1219. // open DLL
  1220. if (! libOpen(filename))
  1221. {
  1222. kData->engine->setLastError(libError(filename));
  1223. return false;
  1224. }
  1225. // ---------------------------------------------------------------
  1226. // get DLL main entry
  1227. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)libSymbol("dssi_descriptor");
  1228. if (descFn == nullptr)
  1229. {
  1230. kData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1231. return false;
  1232. }
  1233. // ---------------------------------------------------------------
  1234. // get descriptor that matches label
  1235. unsigned long i = 0;
  1236. while ((fDssiDescriptor = descFn(i++)) != nullptr)
  1237. {
  1238. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1239. if (fDescriptor != nullptr && fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1240. break;
  1241. }
  1242. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1243. {
  1244. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1245. return false;
  1246. }
  1247. // ---------------------------------------------------------------
  1248. // get info
  1249. if (name != nullptr)
  1250. fName = kData->engine->getNewUniquePluginName(name);
  1251. else if (fDescriptor->Name != nullptr)
  1252. fName = kData->engine->getNewUniquePluginName(fDescriptor->Name);
  1253. else
  1254. fName = kData->engine->getNewUniquePluginName(fDescriptor->Label);
  1255. fFilename = filename;
  1256. // ---------------------------------------------------------------
  1257. // register client
  1258. kData->client = kData->engine->addClient(this);
  1259. if (kData->client == nullptr || ! kData->client->isOk())
  1260. {
  1261. kData->engine->setLastError("Failed to register plugin client");
  1262. return false;
  1263. }
  1264. // ---------------------------------------------------------------
  1265. // initialize plugin
  1266. fHandle = fDescriptor->instantiate(fDescriptor, kData->engine->getSampleRate());
  1267. if (fHandle == nullptr)
  1268. {
  1269. kData->engine->setLastError("Plugin failed to initialize");
  1270. return false;
  1271. }
  1272. // ---------------------------------------------------------------
  1273. // gui stuff
  1274. if (guiFilename)
  1275. {
  1276. kData->osc.thread = new CarlaPluginThread(kData->engine, this, CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  1277. kData->osc.thread->setOscData(guiFilename, fDescriptor->Label);
  1278. fHints |= PLUGIN_HAS_GUI;
  1279. }
  1280. return true;
  1281. }
  1282. private:
  1283. LADSPA_Handle fHandle;
  1284. LADSPA_Handle fHandle2;
  1285. const LADSPA_Descriptor* fDescriptor;
  1286. const DSSI_Descriptor* fDssiDescriptor;
  1287. snd_seq_event_t fMidiEvents[MAX_MIDI_EVENTS];
  1288. float* fParamBuffers;
  1289. QByteArray fChunk;
  1290. };
  1291. CARLA_BACKEND_END_NAMESPACE
  1292. #else // WANT_DSSI
  1293. # warning Building without DSSI support
  1294. #endif
  1295. CARLA_BACKEND_START_NAMESPACE
  1296. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init, const char* const guiFilename)
  1297. {
  1298. qDebug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\"}, \"%s\")", init.engine, init.filename, init.name, init.label, guiFilename);
  1299. #ifdef WANT_DSSI
  1300. DssiPlugin* const plugin = new DssiPlugin(init.engine, init.id);
  1301. if (! plugin->init(init.filename, init.name, init.label, guiFilename))
  1302. {
  1303. delete plugin;
  1304. return nullptr;
  1305. }
  1306. plugin->reload();
  1307. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && (plugin->hints() & PLUGIN_CAN_FORCE_STEREO) == 0)
  1308. {
  1309. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1310. delete plugin;
  1311. return nullptr;
  1312. }
  1313. plugin->registerToOscClient();
  1314. return plugin;
  1315. #else
  1316. init.engine->setLastError("DSSI support not available");
  1317. return nullptr;
  1318. #endif
  1319. }
  1320. CARLA_BACKEND_END_NAMESPACE