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.

1946 lines
68KB

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