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.

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