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.

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