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.

1878 lines
67KB

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