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.

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