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.

1765 lines
62KB

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