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.

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