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.

1876 lines
66KB

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