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.

1778 lines
63KB

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