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.

1817 lines
64KB

  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->needsReset || ! 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 (kData->activeBefore)
  754. {
  755. if (fDescriptor->deactivate != nullptr)
  756. {
  757. fDescriptor->deactivate(fHandle);
  758. if (fHandle2 != nullptr)
  759. fDescriptor->deactivate(fHandle2);
  760. }
  761. }
  762. if (fDescriptor->activate != nullptr)
  763. {
  764. fDescriptor->activate(fHandle);
  765. if (fHandle2 != nullptr)
  766. fDescriptor->activate(fHandle2);
  767. }
  768. kData->needsReset = false;
  769. }
  770. // --------------------------------------------------------------------------------------------------------
  771. // Event Input and Processing
  772. if (kData->event.portIn != nullptr && kData->activeBefore)
  773. {
  774. // ----------------------------------------------------------------------------------------------------
  775. // MIDI Input (External)
  776. if (kData->extNotes.mutex.tryLock())
  777. {
  778. while (midiEventCount < MAX_MIDI_EVENTS && ! kData->extNotes.data.isEmpty())
  779. {
  780. const ExternalMidiNote& note = kData->extNotes.data.getFirst(true);
  781. CARLA_ASSERT(note.channel >= 0);
  782. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  783. fMidiEvents[midiEventCount].type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  784. fMidiEvents[midiEventCount].data.note.channel = note.channel;
  785. fMidiEvents[midiEventCount].data.note.note = note.note;
  786. fMidiEvents[midiEventCount].data.note.velocity = note.velo;
  787. midiEventCount += 1;
  788. }
  789. kData->extNotes.mutex.unlock();
  790. } // End of MIDI Input (External)
  791. // ----------------------------------------------------------------------------------------------------
  792. // Event Input (System)
  793. bool allNotesOffSent = false;
  794. bool sampleAccurate = (fHints & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  795. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  796. uint32_t startTime = 0;
  797. uint32_t timeOffset = 0;
  798. uint32_t nextBankId = 0;
  799. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  800. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  801. for (i=0; i < nEvents; i++)
  802. {
  803. const EngineEvent& event = kData->event.portIn->getEvent(i);
  804. time = event.time;
  805. if (time >= frames)
  806. continue;
  807. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  808. if (time > timeOffset && sampleAccurate)
  809. {
  810. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset, midiEventCount))
  811. {
  812. midiEventCount = 0;
  813. timeOffset = time;
  814. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  815. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  816. else
  817. nextBankId = 0;
  818. }
  819. else
  820. startTime += timeOffset;
  821. }
  822. // Control change
  823. switch (event.type)
  824. {
  825. case kEngineEventTypeNull:
  826. break;
  827. case kEngineEventTypeControl:
  828. {
  829. const EngineControlEvent& ctrlEvent = event.ctrl;
  830. switch (ctrlEvent.type)
  831. {
  832. case kEngineControlEventTypeNull:
  833. break;
  834. case kEngineControlEventTypeParameter:
  835. {
  836. // Control backend stuff
  837. if (event.channel == kData->ctrlChannel)
  838. {
  839. double value;
  840. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  841. {
  842. value = ctrlEvent.value;
  843. setDryWet(value, false, false);
  844. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  845. continue;
  846. }
  847. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  848. {
  849. value = ctrlEvent.value*127/100;
  850. setVolume(value, false, false);
  851. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  852. continue;
  853. }
  854. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  855. {
  856. double left, right;
  857. value = ctrlEvent.value/0.5 - 1.0;
  858. if (value < 0.0)
  859. {
  860. left = -1.0;
  861. right = (value*2)+1.0;
  862. }
  863. else if (value > 0.0)
  864. {
  865. left = (value*2)-1.0;
  866. right = 1.0;
  867. }
  868. else
  869. {
  870. left = -1.0;
  871. right = 1.0;
  872. }
  873. setBalanceLeft(left, false, false);
  874. setBalanceRight(right, false, false);
  875. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  876. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  877. continue;
  878. }
  879. }
  880. // Control plugin parameters
  881. for (k=0; k < kData->param.count; k++)
  882. {
  883. if (kData->param.data[k].midiChannel != event.channel)
  884. continue;
  885. if (kData->param.data[k].midiCC != ctrlEvent.param)
  886. continue;
  887. if (kData->param.data[k].type != PARAMETER_INPUT)
  888. continue;
  889. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  890. continue;
  891. double value;
  892. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  893. {
  894. value = (ctrlEvent.value < 0.5) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  895. }
  896. else
  897. {
  898. // FIXME - ranges call for this
  899. value = ctrlEvent.value * (kData->param.ranges[k].max - kData->param.ranges[k].min) + kData->param.ranges[k].min;
  900. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  901. value = std::rint(value);
  902. }
  903. setParameterValue(k, value, false, false, false);
  904. postponeRtEvent(kPluginPostRtEventParameterChange, k, 0, value);
  905. }
  906. break;
  907. }
  908. case kEngineControlEventTypeMidiBank:
  909. if (event.channel == kData->ctrlChannel)
  910. nextBankId = ctrlEvent.param;
  911. break;
  912. case kEngineControlEventTypeMidiProgram:
  913. if (event.channel == kData->ctrlChannel)
  914. {
  915. const uint32_t nextProgramId = ctrlEvent.param;
  916. for (k=0; k < kData->midiprog.count; k++)
  917. {
  918. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  919. {
  920. setMidiProgram(k, false, false, false, false);
  921. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0);
  922. break;
  923. }
  924. }
  925. }
  926. break;
  927. case kEngineControlEventTypeAllSoundOff:
  928. if (event.channel == kData->ctrlChannel)
  929. {
  930. if (! allNotesOffSent)
  931. sendMidiAllNotesOff();
  932. if (fDescriptor->deactivate != nullptr)
  933. {
  934. fDescriptor->deactivate(fHandle);
  935. if (fHandle2 != nullptr)
  936. fDescriptor->deactivate(fHandle2);
  937. }
  938. if (fDescriptor->activate != nullptr)
  939. {
  940. fDescriptor->activate(fHandle);
  941. if (fHandle2 != nullptr)
  942. fDescriptor->activate(fHandle2);
  943. }
  944. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  945. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  946. allNotesOffSent = true;
  947. }
  948. if (midiEventCount >= MAX_MIDI_EVENTS)
  949. continue;
  950. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  951. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : 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_SOUND_OFF;
  955. midiEventCount += 1;
  956. break;
  957. case kEngineControlEventTypeAllNotesOff:
  958. if (event.channel == kData->ctrlChannel)
  959. {
  960. if (! allNotesOffSent)
  961. sendMidiAllNotesOff();
  962. allNotesOffSent = true;
  963. }
  964. if (midiEventCount >= MAX_MIDI_EVENTS)
  965. continue;
  966. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  967. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : time;
  968. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  969. fMidiEvents[midiEventCount].data.control.channel = event.channel;
  970. fMidiEvents[midiEventCount].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  971. midiEventCount += 1;
  972. break;
  973. }
  974. break;
  975. }
  976. case kEngineEventTypeMidi:
  977. {
  978. if (midiEventCount >= MAX_MIDI_EVENTS)
  979. continue;
  980. const EngineMidiEvent& midiEvent = event.midi;
  981. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  982. uint8_t channel = event.channel;
  983. // Fix bad note-off (per DSSI spec)
  984. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  985. status -= 0x10;
  986. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  987. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : time;
  988. if (MIDI_IS_STATUS_NOTE_OFF(status))
  989. {
  990. const uint8_t note = midiEvent.data[1];
  991. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEOFF;
  992. fMidiEvents[midiEventCount].data.note.channel = channel;
  993. fMidiEvents[midiEventCount].data.note.note = note;
  994. postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0);
  995. }
  996. else if (MIDI_IS_STATUS_NOTE_ON(status))
  997. {
  998. const uint8_t note = midiEvent.data[1];
  999. const uint8_t velo = midiEvent.data[2];
  1000. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEON;
  1001. fMidiEvents[midiEventCount].data.note.channel = channel;
  1002. fMidiEvents[midiEventCount].data.note.note = note;
  1003. fMidiEvents[midiEventCount].data.note.velocity = velo;
  1004. postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  1005. }
  1006. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  1007. {
  1008. const uint8_t note = midiEvent.data[1];
  1009. const uint8_t pressure = midiEvent.data[2];
  1010. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_KEYPRESS;
  1011. fMidiEvents[midiEventCount].data.note.channel = channel;
  1012. fMidiEvents[midiEventCount].data.note.note = note;
  1013. fMidiEvents[midiEventCount].data.note.velocity = pressure;
  1014. }
  1015. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fHints & PLUGIN_OPTION_SELF_AUTOMATION) != 0)
  1016. {
  1017. const uint8_t control = midiEvent.data[1];
  1018. const uint8_t value = midiEvent.data[2];
  1019. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  1020. fMidiEvents[midiEventCount].data.control.channel = channel;
  1021. fMidiEvents[midiEventCount].data.control.param = control;
  1022. fMidiEvents[midiEventCount].data.control.value = value;
  1023. }
  1024. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  1025. {
  1026. const uint8_t pressure = midiEvent.data[1];
  1027. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CHANPRESS;
  1028. fMidiEvents[midiEventCount].data.control.channel = channel;
  1029. fMidiEvents[midiEventCount].data.control.value = pressure;
  1030. }
  1031. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  1032. {
  1033. const uint8_t lsb = midiEvent.data[1];
  1034. const uint8_t msb = midiEvent.data[2];
  1035. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_PITCHBEND;
  1036. fMidiEvents[midiEventCount].data.control.channel = channel;
  1037. fMidiEvents[midiEventCount].data.control.value = ((msb << 7) | lsb) - 8192;
  1038. }
  1039. else
  1040. continue;
  1041. midiEventCount += 1;
  1042. break;
  1043. }
  1044. }
  1045. }
  1046. kData->postRtEvents.trySplice();
  1047. if (frames > timeOffset)
  1048. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset, midiEventCount);
  1049. } // End of Event Input and Processing
  1050. // --------------------------------------------------------------------------------------------------------
  1051. // Plugin processing (no events)
  1052. else
  1053. {
  1054. processSingle(inBuffer, outBuffer, frames, 0, midiEventCount);
  1055. } // End of Plugin processing (no events)
  1056. // --------------------------------------------------------------------------------------------------------
  1057. // Special Parameters
  1058. #if 0
  1059. CARLA_PROCESS_CONTINUE_CHECK;
  1060. for (k=0; k < param.count; k++)
  1061. {
  1062. if (param.data[k].type == PARAMETER_LATENCY)
  1063. {
  1064. // TODO
  1065. }
  1066. }
  1067. CARLA_PROCESS_CONTINUE_CHECK;
  1068. #endif
  1069. CARLA_PROCESS_CONTINUE_CHECK;
  1070. // --------------------------------------------------------------------------------------------------------
  1071. // Control Output
  1072. if (kData->event.portOut != nullptr)
  1073. {
  1074. float value;
  1075. for (k=0; k < kData->param.count; k++)
  1076. {
  1077. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1078. continue;
  1079. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  1080. if (kData->param.data[k].midiCC > 0)
  1081. {
  1082. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  1083. kData->event.portOut->writeControlEvent(0, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  1084. }
  1085. }
  1086. } // End of Control Output
  1087. // --------------------------------------------------------------------------------------------------------
  1088. kData->activeBefore = kData->active;
  1089. }
  1090. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset, const uint32_t midiEventCount)
  1091. {
  1092. uint32_t i, k;
  1093. // --------------------------------------------------------------------------------------------------------
  1094. // Try lock, silence otherwise
  1095. if (! kData->mutex.tryLock())
  1096. {
  1097. for (i=0; i < kData->audioOut.count; i++)
  1098. {
  1099. for (k=0; k < frames; k++)
  1100. outBuffer[i][k+timeOffset] = 0.0f;
  1101. }
  1102. return false;
  1103. }
  1104. // --------------------------------------------------------------------------------------------------------
  1105. // Fill plugin buffers
  1106. for (i=0; i < kData->audioIn.count; i++)
  1107. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1108. for (i=0; i < kData->audioOut.count; i++)
  1109. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1110. // --------------------------------------------------------------------------------------------------------
  1111. // Run plugin
  1112. if (fDssiDescriptor->run_synth != nullptr)
  1113. {
  1114. fDssiDescriptor->run_synth(fHandle, frames, fMidiEvents, midiEventCount);
  1115. if (fHandle2 != nullptr)
  1116. fDssiDescriptor->run_synth(fHandle2, frames, fMidiEvents, midiEventCount);
  1117. }
  1118. else if (fDssiDescriptor->run_multiple_synths != nullptr)
  1119. {
  1120. unsigned long instances = (fHandle2 != nullptr) ? 2 : 1;
  1121. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1122. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1123. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1124. fDssiDescriptor->run_multiple_synths(instances, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1125. }
  1126. else
  1127. {
  1128. fDescriptor->run(fHandle, frames);
  1129. if (fHandle2 != nullptr)
  1130. fDescriptor->run(fHandle2, frames);
  1131. }
  1132. // --------------------------------------------------------------------------------------------------------
  1133. // Post-processing (dry/wet, volume and balance)
  1134. {
  1135. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) > 0 && kData->postProc.dryWet != 1.0f;
  1136. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1137. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1138. for (i=0; i < kData->audioOut.count; i++)
  1139. {
  1140. // Dry/Wet
  1141. if (doDryWet)
  1142. {
  1143. for (k=0; k < frames; k++)
  1144. {
  1145. // TODO
  1146. //if (k < kData->latency && kData->latency < frames)
  1147. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  1148. //else
  1149. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1150. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  1151. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1152. }
  1153. }
  1154. // Balance
  1155. if (doBalance)
  1156. {
  1157. if (i % 2 == 0)
  1158. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  1159. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1160. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1161. for (k=0; k < frames; k++)
  1162. {
  1163. if (i % 2 == 0)
  1164. {
  1165. // left
  1166. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1167. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1168. }
  1169. else
  1170. {
  1171. // right
  1172. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1173. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1174. }
  1175. }
  1176. }
  1177. // Volume (and buffer copy)
  1178. {
  1179. for (k=0; k < frames; k++)
  1180. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  1181. }
  1182. }
  1183. #if 0
  1184. // Latency, save values for next callback, TODO
  1185. if (kData->latency > 0 && kData->latency < frames)
  1186. {
  1187. for (i=0; i < kData->audioIn.count; i++)
  1188. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  1189. }
  1190. #endif
  1191. } // End of Post-processing
  1192. // --------------------------------------------------------------------------------------------------------
  1193. kData->mutex.unlock();
  1194. return true;
  1195. }
  1196. void bufferSizeChanged(const uint32_t newBufferSize)
  1197. {
  1198. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1199. {
  1200. if (fAudioInBuffers[i] != nullptr)
  1201. delete[] fAudioInBuffers[i];
  1202. fAudioInBuffers[i] = new float[newBufferSize];
  1203. }
  1204. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1205. {
  1206. if (fAudioOutBuffers[i] != nullptr)
  1207. delete[] fAudioOutBuffers[i];
  1208. fAudioOutBuffers[i] = new float[newBufferSize];
  1209. }
  1210. if (fHandle2 == nullptr)
  1211. {
  1212. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1213. {
  1214. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1215. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1216. }
  1217. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1218. {
  1219. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1220. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1221. }
  1222. }
  1223. else
  1224. {
  1225. if (kData->audioIn.count > 0)
  1226. {
  1227. CARLA_ASSERT(kData->audioIn.count == 2);
  1228. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1229. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1230. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1231. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1232. }
  1233. if (kData->audioOut.count > 0)
  1234. {
  1235. CARLA_ASSERT(kData->audioOut.count == 2);
  1236. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1237. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1238. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1239. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1240. }
  1241. }
  1242. }
  1243. // -------------------------------------------------------------------
  1244. // Post-poned events
  1245. void uiParameterChange(const uint32_t index, const float value)
  1246. {
  1247. CARLA_ASSERT(index < kData->param.count);
  1248. if (index >= kData->param.count)
  1249. return;
  1250. if (kData->osc.data.target == nullptr)
  1251. return;
  1252. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  1253. }
  1254. void uiMidiProgramChange(const uint32_t index)
  1255. {
  1256. CARLA_ASSERT(index < kData->midiprog.count);
  1257. if (index >= kData->midiprog.count)
  1258. return;
  1259. if (kData->osc.data.target == nullptr)
  1260. return;
  1261. osc_send_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1262. }
  1263. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1264. {
  1265. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1266. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1267. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1268. if (channel >= MAX_MIDI_CHANNELS)
  1269. return;
  1270. if (note >= MAX_MIDI_NOTE)
  1271. return;
  1272. if (velo >= MAX_MIDI_VALUE)
  1273. return;
  1274. if (kData->osc.data.target == nullptr)
  1275. return;
  1276. uint8_t midiData[4] = { 0 };
  1277. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1278. midiData[2] = note;
  1279. midiData[3] = velo;
  1280. osc_send_midi(&kData->osc.data, midiData);
  1281. }
  1282. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1283. {
  1284. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1285. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1286. if (channel >= MAX_MIDI_CHANNELS)
  1287. return;
  1288. if (note >= MAX_MIDI_NOTE)
  1289. return;
  1290. if (kData->osc.data.target == nullptr)
  1291. return;
  1292. uint8_t midiData[4] = { 0 };
  1293. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1294. midiData[2] = note;
  1295. osc_send_midi(&kData->osc.data, midiData);
  1296. }
  1297. // -------------------------------------------------------------------
  1298. // Cleanup
  1299. void deleteBuffers()
  1300. {
  1301. carla_debug("DssiPlugin::deleteBuffers() - start");
  1302. if (fAudioInBuffers != nullptr)
  1303. {
  1304. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1305. {
  1306. if (fAudioInBuffers[i] != nullptr)
  1307. {
  1308. delete[] fAudioInBuffers[i];
  1309. fAudioInBuffers[i] = nullptr;
  1310. }
  1311. }
  1312. delete[] fAudioInBuffers;
  1313. fAudioInBuffers = nullptr;
  1314. }
  1315. if (fAudioOutBuffers != nullptr)
  1316. {
  1317. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1318. {
  1319. if (fAudioOutBuffers[i] != nullptr)
  1320. {
  1321. delete[] fAudioOutBuffers[i];
  1322. fAudioOutBuffers[i] = nullptr;
  1323. }
  1324. }
  1325. delete[] fAudioOutBuffers;
  1326. fAudioOutBuffers = nullptr;
  1327. }
  1328. if (fParamBuffers != nullptr)
  1329. {
  1330. delete[] fParamBuffers;
  1331. fParamBuffers = nullptr;
  1332. }
  1333. CarlaPlugin::deleteBuffers();
  1334. carla_debug("DssiPlugin::deleteBuffers() - end");
  1335. }
  1336. // -------------------------------------------------------------------
  1337. bool init(const char* const filename, const char* const name, const char* const label, const char* const guiFilename)
  1338. {
  1339. CARLA_ASSERT(kData->engine != nullptr);
  1340. CARLA_ASSERT(kData->client == nullptr);
  1341. CARLA_ASSERT(filename != nullptr);
  1342. CARLA_ASSERT(label != nullptr);
  1343. // ---------------------------------------------------------------
  1344. // open DLL
  1345. if (! libOpen(filename))
  1346. {
  1347. kData->engine->setLastError(libError(filename));
  1348. return false;
  1349. }
  1350. // ---------------------------------------------------------------
  1351. // get DLL main entry
  1352. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)libSymbol("dssi_descriptor");
  1353. if (descFn == nullptr)
  1354. {
  1355. kData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1356. return false;
  1357. }
  1358. // ---------------------------------------------------------------
  1359. // get descriptor that matches label
  1360. unsigned long i = 0;
  1361. while ((fDssiDescriptor = descFn(i++)) != nullptr)
  1362. {
  1363. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1364. if (fDescriptor != nullptr && fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1365. break;
  1366. }
  1367. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1368. {
  1369. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1370. return false;
  1371. }
  1372. // ---------------------------------------------------------------
  1373. // get info
  1374. if (name != nullptr)
  1375. fName = kData->engine->getNewUniquePluginName(name);
  1376. else if (fDescriptor->Name != nullptr)
  1377. fName = kData->engine->getNewUniquePluginName(fDescriptor->Name);
  1378. else
  1379. fName = kData->engine->getNewUniquePluginName(fDescriptor->Label);
  1380. fFilename = filename;
  1381. // ---------------------------------------------------------------
  1382. // register client
  1383. kData->client = kData->engine->addClient(this);
  1384. if (kData->client == nullptr || ! kData->client->isOk())
  1385. {
  1386. kData->engine->setLastError("Failed to register plugin client");
  1387. return false;
  1388. }
  1389. // ---------------------------------------------------------------
  1390. // initialize plugin
  1391. fHandle = fDescriptor->instantiate(fDescriptor, kData->engine->getSampleRate());
  1392. if (fHandle == nullptr)
  1393. {
  1394. kData->engine->setLastError("Plugin failed to initialize");
  1395. return false;
  1396. }
  1397. // ---------------------------------------------------------------
  1398. // gui stuff
  1399. if (guiFilename != nullptr)
  1400. {
  1401. kData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  1402. kData->osc.thread.setOscData(guiFilename, fDescriptor->Label);
  1403. fHints |= PLUGIN_HAS_GUI;
  1404. }
  1405. return true;
  1406. }
  1407. private:
  1408. LADSPA_Handle fHandle;
  1409. LADSPA_Handle fHandle2;
  1410. const LADSPA_Descriptor* fDescriptor;
  1411. const DSSI_Descriptor* fDssiDescriptor;
  1412. float** fAudioInBuffers;
  1413. float** fAudioOutBuffers;
  1414. float* fParamBuffers;
  1415. snd_seq_event_t fMidiEvents[MAX_MIDI_EVENTS];
  1416. QByteArray fChunk;
  1417. };
  1418. CARLA_BACKEND_END_NAMESPACE
  1419. #else // WANT_DSSI
  1420. # warning Building without DSSI support
  1421. #endif
  1422. CARLA_BACKEND_START_NAMESPACE
  1423. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init, const char* const guiFilename)
  1424. {
  1425. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\"}, \"%s\")", init.engine, init.filename, init.name, init.label, guiFilename);
  1426. #ifdef WANT_DSSI
  1427. DssiPlugin* const plugin = new DssiPlugin(init.engine, init.id);
  1428. if (! plugin->init(init.filename, init.name, init.label, guiFilename))
  1429. {
  1430. delete plugin;
  1431. return nullptr;
  1432. }
  1433. plugin->reload();
  1434. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && (plugin->hints() & PLUGIN_CAN_FORCE_STEREO) == 0)
  1435. {
  1436. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1437. delete plugin;
  1438. return nullptr;
  1439. }
  1440. plugin->registerToOscClient();
  1441. return plugin;
  1442. #else
  1443. init.engine->setLastError("DSSI support not available");
  1444. return nullptr;
  1445. #endif
  1446. }
  1447. CARLA_BACKEND_END_NAMESPACE