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.

1759 lines
62KB

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