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.

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