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.

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