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.

1781 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(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))
  984. {
  985. if (fHints & PLUGIN_OPTION_SELF_AUTOMATION)
  986. {
  987. const uint8_t control = midiEvent.data[1];
  988. const uint8_t value = midiEvent.data[2];
  989. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  990. fMidiEvents[midiEventCount].data.control.channel = channel;
  991. fMidiEvents[midiEventCount].data.control.param = control;
  992. fMidiEvents[midiEventCount].data.control.value = value;
  993. }
  994. }
  995. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  996. {
  997. const uint8_t pressure = midiEvent.data[1];
  998. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CHANPRESS;
  999. fMidiEvents[midiEventCount].data.control.channel = channel;
  1000. fMidiEvents[midiEventCount].data.control.value = pressure;
  1001. }
  1002. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  1003. {
  1004. const uint8_t lsb = midiEvent.data[1];
  1005. const uint8_t msb = midiEvent.data[2];
  1006. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_PITCHBEND;
  1007. fMidiEvents[midiEventCount].data.control.channel = channel;
  1008. fMidiEvents[midiEventCount].data.control.value = ((msb << 7) | lsb) - 8192;
  1009. }
  1010. else
  1011. continue;
  1012. midiEventCount += 1;
  1013. break;
  1014. }
  1015. }
  1016. }
  1017. kData->postRtEvents.trySplice();
  1018. if (frames > timeOffset)
  1019. {
  1020. if (timeOffset != 0)
  1021. qWarning("FINAL proccessing @ frame %04i/%04i for %04i frames, cur:%04i", timeOffset, frames, frames - timeOffset, time);
  1022. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset, midiEventCount);
  1023. }
  1024. } // End of Event Input and Processing
  1025. // --------------------------------------------------------------------------------------------------------
  1026. // Plugin processing (no events)
  1027. else
  1028. {
  1029. processSingle(inBuffer, outBuffer, frames, 0, 0);
  1030. } // End of Plugin processing (no events)
  1031. // --------------------------------------------------------------------------------------------------------
  1032. // Special Parameters
  1033. #if 0
  1034. CARLA_PROCESS_CONTINUE_CHECK;
  1035. for (k=0; k < param.count; k++)
  1036. {
  1037. if (param.data[k].type == PARAMETER_LATENCY)
  1038. {
  1039. // TODO
  1040. }
  1041. }
  1042. CARLA_PROCESS_CONTINUE_CHECK;
  1043. #endif
  1044. CARLA_PROCESS_CONTINUE_CHECK;
  1045. // --------------------------------------------------------------------------------------------------------
  1046. // Post-processing (dry/wet, volume and balance)
  1047. {
  1048. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) > 0 && kData->postProc.dryWet != 1.0f;
  1049. const bool doVolume = (fHints & PLUGIN_CAN_VOLUME) > 0 && kData->postProc.volume != 1.0f;
  1050. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1051. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1052. for (i=0; i < kData->audioOut.count; i++)
  1053. {
  1054. // Dry/Wet
  1055. if (doDryWet)
  1056. {
  1057. #if 0
  1058. for (k=0; k < frames; k++)
  1059. {
  1060. if (k < m_latency && m_latency < frames)
  1061. bufValue = (aIn.count == 1) ? m_latencyBuffers[0][k] : m_latencyBuffers[i][k];
  1062. else
  1063. bufValue = (aIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1064. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(bufValue*(1.0-x_dryWet));
  1065. }
  1066. #endif
  1067. for (k=0; k < frames; k++)
  1068. {
  1069. // TODO
  1070. //if (k < kData->latency && kData->latency < frames)
  1071. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  1072. //else
  1073. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1074. bufValue = inBuffer[ (kData->audioIn.count == 1) ? 0 : i ][k];
  1075. outBuffer[i][k] = (outBuffer[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1076. }
  1077. }
  1078. // Balance
  1079. if (doBalance)
  1080. {
  1081. if (i % 2 == 0)
  1082. std::memcpy(oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1083. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1084. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1085. for (k=0; k < frames; k++)
  1086. {
  1087. if (i % 2 == 0)
  1088. {
  1089. // left
  1090. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1091. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  1092. }
  1093. else
  1094. {
  1095. // right
  1096. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  1097. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  1098. }
  1099. }
  1100. }
  1101. // Volume
  1102. if (doVolume)
  1103. {
  1104. for (k=0; k < frames; k++)
  1105. outBuffer[i][k] *= kData->postProc.volume;
  1106. }
  1107. }
  1108. #if 0
  1109. // Latency, save values for next callback, TODO
  1110. if (kData->latency > 0 && kData->latency < frames)
  1111. {
  1112. for (i=0; i < kData->audioIn.count; i++)
  1113. std::memcpy(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), sizeof(float)*kData->latency);
  1114. }
  1115. #endif
  1116. } // End of Post-processing
  1117. CARLA_PROCESS_CONTINUE_CHECK;
  1118. // --------------------------------------------------------------------------------------------------------
  1119. // Control Output
  1120. if (kData->event.portOut != nullptr)
  1121. {
  1122. float value;
  1123. for (k=0; k < kData->param.count; k++)
  1124. {
  1125. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1126. continue;
  1127. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  1128. if (kData->param.data[k].midiCC > 0)
  1129. {
  1130. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  1131. kData->event.portOut->writeControlEvent(framesOffset, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  1132. }
  1133. }
  1134. } // End of Control Output
  1135. CARLA_PROCESS_CONTINUE_CHECK;
  1136. // --------------------------------------------------------------------------------------------------------
  1137. kData->activeBefore = kData->active;
  1138. }
  1139. void processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset, const uint32_t midiEventCount)
  1140. {
  1141. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1142. std::memcpy(fAudioInBuffers[i], inBuffer[i]+timeOffset, sizeof(float)*frames);
  1143. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1144. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1145. if (fDssiDescriptor->run_synth != nullptr)
  1146. {
  1147. fDssiDescriptor->run_synth(fHandle, frames, fMidiEvents, midiEventCount);
  1148. if (fHandle2 != nullptr)
  1149. fDssiDescriptor->run_synth(fHandle2, frames, fMidiEvents, midiEventCount);
  1150. }
  1151. else if (fDssiDescriptor->run_multiple_synths != nullptr)
  1152. {
  1153. unsigned long instances = (fHandle2 != nullptr) ? 2 : 1;
  1154. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1155. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1156. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1157. fDssiDescriptor->run_multiple_synths(instances, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1158. }
  1159. else
  1160. {
  1161. fDescriptor->run(fHandle, frames);
  1162. if (fHandle2 != nullptr)
  1163. fDescriptor->run(fHandle2, frames);
  1164. }
  1165. for (uint32_t i=0, k; i < kData->audioOut.count; i++)
  1166. {
  1167. for (k=0; k < frames; k++)
  1168. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1169. }
  1170. }
  1171. void bufferSizeChanged(const uint32_t newBufferSize)
  1172. {
  1173. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1174. {
  1175. if (fAudioInBuffers[i] != nullptr)
  1176. delete[] fAudioInBuffers[i];
  1177. fAudioInBuffers[i] = new float[newBufferSize];
  1178. }
  1179. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1180. {
  1181. if (fAudioOutBuffers[i] != nullptr)
  1182. delete[] fAudioOutBuffers[i];
  1183. fAudioOutBuffers[i] = new float[newBufferSize];
  1184. }
  1185. if (fHandle2 == nullptr)
  1186. {
  1187. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1188. {
  1189. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1190. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1191. }
  1192. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1193. {
  1194. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1195. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1196. }
  1197. }
  1198. else
  1199. {
  1200. if (kData->audioIn.count > 0)
  1201. {
  1202. CARLA_ASSERT(kData->audioIn.count == 2);
  1203. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1204. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1205. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1206. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1207. }
  1208. if (kData->audioOut.count > 0)
  1209. {
  1210. CARLA_ASSERT(kData->audioOut.count == 2);
  1211. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1212. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1213. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1214. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1215. }
  1216. }
  1217. }
  1218. // -------------------------------------------------------------------
  1219. // Post-poned events
  1220. void uiParameterChange(const uint32_t index, const float value)
  1221. {
  1222. CARLA_ASSERT(index < kData->param.count);
  1223. if (index >= kData->param.count)
  1224. return;
  1225. if (kData->osc.data.target == nullptr)
  1226. return;
  1227. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  1228. }
  1229. void uiMidiProgramChange(const uint32_t index)
  1230. {
  1231. CARLA_ASSERT(index < kData->midiprog.count);
  1232. if (index >= kData->midiprog.count)
  1233. return;
  1234. if (kData->osc.data.target == nullptr)
  1235. return;
  1236. osc_send_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1237. }
  1238. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1239. {
  1240. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1241. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1242. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1243. if (channel >= MAX_MIDI_CHANNELS)
  1244. return;
  1245. if (note >= MAX_MIDI_NOTE)
  1246. return;
  1247. if (velo >= MAX_MIDI_VALUE)
  1248. return;
  1249. if (kData->osc.data.target == nullptr)
  1250. return;
  1251. uint8_t midiData[4] = { 0 };
  1252. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1253. midiData[2] = note;
  1254. midiData[3] = velo;
  1255. osc_send_midi(&kData->osc.data, midiData);
  1256. }
  1257. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1258. {
  1259. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1260. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1261. if (channel >= MAX_MIDI_CHANNELS)
  1262. return;
  1263. if (note >= MAX_MIDI_NOTE)
  1264. return;
  1265. if (kData->osc.data.target == nullptr)
  1266. return;
  1267. uint8_t midiData[4] = { 0 };
  1268. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1269. midiData[2] = note;
  1270. osc_send_midi(&kData->osc.data, midiData);
  1271. }
  1272. // -------------------------------------------------------------------
  1273. // Cleanup
  1274. void deleteBuffers()
  1275. {
  1276. qDebug("DssiPlugin::deleteBuffers() - start");
  1277. if (fAudioInBuffers != nullptr)
  1278. {
  1279. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1280. {
  1281. if (fAudioInBuffers[i] != nullptr)
  1282. {
  1283. delete[] fAudioInBuffers[i];
  1284. fAudioInBuffers[i] = nullptr;
  1285. }
  1286. }
  1287. delete[] fAudioInBuffers;
  1288. fAudioInBuffers = nullptr;
  1289. }
  1290. if (fAudioOutBuffers != nullptr)
  1291. {
  1292. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1293. {
  1294. if (fAudioOutBuffers[i] != nullptr)
  1295. {
  1296. delete[] fAudioOutBuffers[i];
  1297. fAudioOutBuffers[i] = nullptr;
  1298. }
  1299. }
  1300. delete[] fAudioOutBuffers;
  1301. fAudioOutBuffers = nullptr;
  1302. }
  1303. if (fParamBuffers != nullptr)
  1304. {
  1305. delete[] fParamBuffers;
  1306. fParamBuffers = nullptr;
  1307. }
  1308. CarlaPlugin::deleteBuffers();
  1309. qDebug("DssiPlugin::deleteBuffers() - end");
  1310. }
  1311. // -------------------------------------------------------------------
  1312. bool init(const char* const filename, const char* const name, const char* const label, const char* const guiFilename)
  1313. {
  1314. CARLA_ASSERT(kData->engine != nullptr);
  1315. CARLA_ASSERT(kData->client == nullptr);
  1316. CARLA_ASSERT(filename != nullptr);
  1317. CARLA_ASSERT(label != nullptr);
  1318. // ---------------------------------------------------------------
  1319. // open DLL
  1320. if (! libOpen(filename))
  1321. {
  1322. kData->engine->setLastError(libError(filename));
  1323. return false;
  1324. }
  1325. // ---------------------------------------------------------------
  1326. // get DLL main entry
  1327. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)libSymbol("dssi_descriptor");
  1328. if (descFn == nullptr)
  1329. {
  1330. kData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1331. return false;
  1332. }
  1333. // ---------------------------------------------------------------
  1334. // get descriptor that matches label
  1335. unsigned long i = 0;
  1336. while ((fDssiDescriptor = descFn(i++)) != nullptr)
  1337. {
  1338. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1339. if (fDescriptor != nullptr && fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1340. break;
  1341. }
  1342. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1343. {
  1344. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1345. return false;
  1346. }
  1347. // ---------------------------------------------------------------
  1348. // get info
  1349. if (name != nullptr)
  1350. fName = kData->engine->getNewUniquePluginName(name);
  1351. else if (fDescriptor->Name != nullptr)
  1352. fName = kData->engine->getNewUniquePluginName(fDescriptor->Name);
  1353. else
  1354. fName = kData->engine->getNewUniquePluginName(fDescriptor->Label);
  1355. fFilename = filename;
  1356. // ---------------------------------------------------------------
  1357. // register client
  1358. kData->client = kData->engine->addClient(this);
  1359. if (kData->client == nullptr || ! kData->client->isOk())
  1360. {
  1361. kData->engine->setLastError("Failed to register plugin client");
  1362. return false;
  1363. }
  1364. // ---------------------------------------------------------------
  1365. // initialize plugin
  1366. fHandle = fDescriptor->instantiate(fDescriptor, kData->engine->getSampleRate());
  1367. if (fHandle == nullptr)
  1368. {
  1369. kData->engine->setLastError("Plugin failed to initialize");
  1370. return false;
  1371. }
  1372. // ---------------------------------------------------------------
  1373. // gui stuff
  1374. if (guiFilename)
  1375. {
  1376. kData->osc.thread = new CarlaPluginThread(kData->engine, this, CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  1377. kData->osc.thread->setOscData(guiFilename, fDescriptor->Label);
  1378. fHints |= PLUGIN_HAS_GUI;
  1379. }
  1380. return true;
  1381. }
  1382. private:
  1383. LADSPA_Handle fHandle;
  1384. LADSPA_Handle fHandle2;
  1385. const LADSPA_Descriptor* fDescriptor;
  1386. const DSSI_Descriptor* fDssiDescriptor;
  1387. float** fAudioInBuffers;
  1388. float** fAudioOutBuffers;
  1389. float* fParamBuffers;
  1390. snd_seq_event_t fMidiEvents[MAX_MIDI_EVENTS];
  1391. QByteArray fChunk;
  1392. };
  1393. CARLA_BACKEND_END_NAMESPACE
  1394. #else // WANT_DSSI
  1395. # warning Building without DSSI support
  1396. #endif
  1397. CARLA_BACKEND_START_NAMESPACE
  1398. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init, const char* const guiFilename)
  1399. {
  1400. qDebug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\"}, \"%s\")", init.engine, init.filename, init.name, init.label, guiFilename);
  1401. #ifdef WANT_DSSI
  1402. DssiPlugin* const plugin = new DssiPlugin(init.engine, init.id);
  1403. if (! plugin->init(init.filename, init.name, init.label, guiFilename))
  1404. {
  1405. delete plugin;
  1406. return nullptr;
  1407. }
  1408. plugin->reload();
  1409. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && (plugin->hints() & PLUGIN_CAN_FORCE_STEREO) == 0)
  1410. {
  1411. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1412. delete plugin;
  1413. return nullptr;
  1414. }
  1415. plugin->registerToOscClient();
  1416. return plugin;
  1417. #else
  1418. init.engine->setLastError("DSSI support not available");
  1419. return nullptr;
  1420. #endif
  1421. }
  1422. CARLA_BACKEND_END_NAMESPACE