Collection of tools useful for audio production
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.

1645 lines
54KB

  1. /*
  2. * Carla DSSI Plugin
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #include "carla_plugin.hpp"
  18. #ifdef WANT_DSSI
  19. #include "carla_ladspa_utils.hpp"
  20. #include "dssi/dssi.h"
  21. CARLA_BACKEND_START_NAMESPACE
  22. /*!
  23. * @defgroup CarlaBackendDssiPlugin Carla Backend DSSI Plugin
  24. *
  25. * The Carla Backend DSSI Plugin.\n
  26. * http://dssi.sourceforge.net/
  27. * @{
  28. */
  29. class DssiPlugin : public CarlaPlugin
  30. {
  31. public:
  32. DssiPlugin(CarlaEngine* const engine, const unsigned short id)
  33. : CarlaPlugin(engine, id)
  34. {
  35. qDebug("DssiPlugin::DssiPlugin()");
  36. m_type = PLUGIN_DSSI;
  37. handle = h2 = nullptr;
  38. descriptor = nullptr;
  39. ldescriptor = nullptr;
  40. paramBuffers = nullptr;
  41. memset(midiEvents, 0, sizeof(snd_seq_event_t)*MAX_MIDI_EVENTS);
  42. }
  43. ~DssiPlugin()
  44. {
  45. qDebug("DssiPlugin::~DssiPlugin()");
  46. // close UI
  47. if (m_hints & PLUGIN_HAS_GUI)
  48. {
  49. showGui(false);
  50. if (osc.thread)
  51. {
  52. // Wait a bit first, try safe quit, then force kill
  53. if (osc.thread->isRunning() && ! osc.thread->wait(x_engine->getOptions().oscUiTimeout))
  54. {
  55. qWarning("Failed to properly stop DSSI GUI thread");
  56. osc.thread->terminate();
  57. }
  58. delete osc.thread;
  59. }
  60. }
  61. if (ldescriptor)
  62. {
  63. if (ldescriptor->deactivate && m_activeBefore)
  64. {
  65. if (handle)
  66. ldescriptor->deactivate(handle);
  67. if (h2)
  68. ldescriptor->deactivate(h2);
  69. }
  70. if (ldescriptor->cleanup)
  71. {
  72. if (handle)
  73. ldescriptor->cleanup(handle);
  74. if (h2)
  75. ldescriptor->cleanup(h2);
  76. }
  77. }
  78. }
  79. // -------------------------------------------------------------------
  80. // Information (base)
  81. PluginCategory category()
  82. {
  83. if (m_hints & PLUGIN_IS_SYNTH)
  84. return PLUGIN_CATEGORY_SYNTH;
  85. return getPluginCategoryFromName(m_name);
  86. }
  87. long uniqueId()
  88. {
  89. CARLA_ASSERT(ldescriptor);
  90. return ldescriptor ? ldescriptor->UniqueID : 0;
  91. }
  92. // -------------------------------------------------------------------
  93. // Information (current data)
  94. int32_t chunkData(void** const dataPtr)
  95. {
  96. CARLA_ASSERT(dataPtr);
  97. CARLA_ASSERT(descriptor);
  98. CARLA_ASSERT(descriptor->get_custom_data);
  99. unsigned long dataSize = 0;
  100. if (descriptor->get_custom_data && descriptor->get_custom_data(handle, dataPtr, &dataSize))
  101. return dataSize;
  102. return 0;
  103. }
  104. // -------------------------------------------------------------------
  105. // Information (per-plugin data)
  106. double getParameterValue(const uint32_t parameterId)
  107. {
  108. CARLA_ASSERT(parameterId < param.count);
  109. return paramBuffers[parameterId];
  110. }
  111. void getLabel(char* const strBuf)
  112. {
  113. CARLA_ASSERT(ldescriptor);
  114. if (ldescriptor && ldescriptor->Label)
  115. strncpy(strBuf, ldescriptor->Label, STR_MAX);
  116. else
  117. CarlaPlugin::getLabel(strBuf);
  118. }
  119. void getMaker(char* const strBuf)
  120. {
  121. CARLA_ASSERT(ldescriptor);
  122. if (ldescriptor && ldescriptor->Maker)
  123. strncpy(strBuf, ldescriptor->Maker, STR_MAX);
  124. else
  125. CarlaPlugin::getMaker(strBuf);
  126. }
  127. void getCopyright(char* const strBuf)
  128. {
  129. CARLA_ASSERT(ldescriptor);
  130. if (ldescriptor && ldescriptor->Copyright)
  131. strncpy(strBuf, ldescriptor->Copyright, STR_MAX);
  132. else
  133. CarlaPlugin::getCopyright(strBuf);
  134. }
  135. void getRealName(char* const strBuf)
  136. {
  137. CARLA_ASSERT(ldescriptor);
  138. if (ldescriptor && ldescriptor->Name)
  139. strncpy(strBuf, ldescriptor->Name, STR_MAX);
  140. else
  141. CarlaPlugin::getRealName(strBuf);
  142. }
  143. void getParameterName(const uint32_t parameterId, char* const strBuf)
  144. {
  145. CARLA_ASSERT(ldescriptor);
  146. CARLA_ASSERT(parameterId < param.count);
  147. int32_t rindex = param.data[parameterId].rindex;
  148. if (ldescriptor && rindex < (int32_t)ldescriptor->PortCount)
  149. strncpy(strBuf, ldescriptor->PortNames[rindex], STR_MAX);
  150. else
  151. CarlaPlugin::getParameterName(parameterId, strBuf);
  152. }
  153. void getGuiInfo(GuiType* const type, bool* const resizable)
  154. {
  155. CARLA_ASSERT(type);
  156. CARLA_ASSERT(resizable);
  157. *type = (m_hints & PLUGIN_HAS_GUI) ? GUI_EXTERNAL_OSC : GUI_NONE;
  158. *resizable = false;
  159. }
  160. // -------------------------------------------------------------------
  161. // Set data (plugin-specific stuff)
  162. void setParameterValue(const uint32_t parameterId, double value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  163. {
  164. CARLA_ASSERT(parameterId < param.count);
  165. paramBuffers[parameterId] = fixParameterValue(value, param.ranges[parameterId]);
  166. CarlaPlugin::setParameterValue(parameterId, value, sendGui, sendOsc, sendCallback);
  167. }
  168. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  169. {
  170. CARLA_ASSERT(type);
  171. CARLA_ASSERT(key);
  172. CARLA_ASSERT(value);
  173. if (! type)
  174. return qCritical("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  175. if (strcmp(type, CUSTOM_DATA_STRING) != 0)
  176. return qCritical("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  177. if (! key)
  178. return qCritical("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  179. if (! value)
  180. return qCritical("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  181. descriptor->configure(handle, key, value);
  182. if (h2) descriptor->configure(h2, key, value);
  183. if (sendGui && osc.data.target)
  184. osc_send_configure(&osc.data, key, value);
  185. if (strcmp(key, "reloadprograms") == 0 || strcmp(key, "load") == 0 || strncmp(key, "patches", 7) == 0)
  186. {
  187. const ScopedDisabler m(this);
  188. reloadPrograms(false);
  189. }
  190. CarlaPlugin::setCustomData(type, key, value, sendGui);
  191. }
  192. void setChunkData(const char* const stringData)
  193. {
  194. CARLA_ASSERT(m_hints & PLUGIN_USES_CHUNKS);
  195. CARLA_ASSERT(stringData);
  196. static QByteArray chunk;
  197. chunk = QByteArray::fromBase64(stringData);
  198. if (x_engine->isOffline())
  199. {
  200. const CarlaEngine::ScopedLocker m(x_engine);
  201. descriptor->set_custom_data(handle, chunk.data(), chunk.size());
  202. if (h2) descriptor->set_custom_data(h2, chunk.data(), chunk.size());
  203. }
  204. else
  205. {
  206. const CarlaPlugin::ScopedDisabler m(this);
  207. descriptor->set_custom_data(handle, chunk.data(), chunk.size());
  208. if (h2) descriptor->set_custom_data(h2, chunk.data(), chunk.size());
  209. }
  210. }
  211. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  212. {
  213. CARLA_ASSERT(index >= -1 && index < (int32_t)midiprog.count);
  214. if (index < -1)
  215. index = -1;
  216. else if (index > (int32_t)midiprog.count)
  217. return;
  218. if (index >= 0)
  219. {
  220. if (x_engine->isOffline())
  221. {
  222. const CarlaEngine::ScopedLocker m(x_engine, block);
  223. descriptor->select_program(handle, midiprog.data[index].bank, midiprog.data[index].program);
  224. if (h2) descriptor->select_program(h2, midiprog.data[index].bank, midiprog.data[index].program);
  225. }
  226. else
  227. {
  228. const ScopedDisabler m(this, block);
  229. descriptor->select_program(handle, midiprog.data[index].bank, midiprog.data[index].program);
  230. if (h2) descriptor->select_program(h2, midiprog.data[index].bank, midiprog.data[index].program);
  231. }
  232. }
  233. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, block);
  234. }
  235. // -------------------------------------------------------------------
  236. // Set gui stuff
  237. void showGui(const bool yesNo)
  238. {
  239. CARLA_ASSERT(osc.thread);
  240. if (! osc.thread)
  241. {
  242. qCritical("DssiPlugin::showGui(%s) - attempt to show gui, but it does not exist!", bool2str(yesNo));
  243. return;
  244. }
  245. if (yesNo)
  246. {
  247. osc.thread->start();
  248. }
  249. else
  250. {
  251. if (osc.data.target)
  252. {
  253. osc_send_hide(&osc.data);
  254. osc_send_quit(&osc.data);
  255. osc.data.free();
  256. }
  257. if (! osc.thread->wait(500))
  258. osc.thread->quit();
  259. }
  260. }
  261. // -------------------------------------------------------------------
  262. // Plugin state
  263. void reload()
  264. {
  265. qDebug("DssiPlugin::reload() - start");
  266. CARLA_ASSERT(descriptor && ldescriptor);
  267. const ProcessMode processMode(x_engine->getOptions().processMode);
  268. // Safely disable plugin for reload
  269. const ScopedDisabler m(this);
  270. if (x_client->isActive())
  271. x_client->deactivate();
  272. // Remove client ports
  273. removeClientPorts();
  274. // Delete old data
  275. deleteBuffers();
  276. uint32_t aIns, aOuts, mIns, params, j;
  277. aIns = aOuts = mIns = params = 0;
  278. const double sampleRate = x_engine->getSampleRate();
  279. const unsigned long portCount = ldescriptor->PortCount;
  280. bool forcedStereoIn, forcedStereoOut;
  281. forcedStereoIn = forcedStereoOut = false;
  282. for (unsigned long i=0; i < portCount; i++)
  283. {
  284. const LADSPA_PortDescriptor portType = ldescriptor->PortDescriptors[i];
  285. if (LADSPA_IS_PORT_AUDIO(portType))
  286. {
  287. if (LADSPA_IS_PORT_INPUT(portType))
  288. aIns += 1;
  289. else if (LADSPA_IS_PORT_OUTPUT(portType))
  290. aOuts += 1;
  291. }
  292. else if (LADSPA_IS_PORT_CONTROL(portType))
  293. params += 1;
  294. }
  295. if (x_engine->getOptions().forceStereo && (aIns == 1 || aOuts == 1) && ! h2)
  296. {
  297. h2 = ldescriptor->instantiate(ldescriptor, sampleRate);
  298. if (aIns == 1)
  299. {
  300. aIns = 2;
  301. forcedStereoIn = true;
  302. }
  303. if (aOuts == 1)
  304. {
  305. aOuts = 2;
  306. forcedStereoOut = true;
  307. }
  308. }
  309. if (descriptor->run_synth || descriptor->run_multiple_synths)
  310. mIns = 1;
  311. if (aIns > 0)
  312. {
  313. aIn.ports = new CarlaEngineAudioPort*[aIns];
  314. aIn.rindexes = new uint32_t[aIns];
  315. }
  316. if (aOuts > 0)
  317. {
  318. aOut.ports = new CarlaEngineAudioPort*[aOuts];
  319. aOut.rindexes = new uint32_t[aOuts];
  320. }
  321. if (params > 0)
  322. {
  323. param.data = new ParameterData[params];
  324. param.ranges = new ParameterRanges[params];
  325. paramBuffers = new float[params];
  326. }
  327. bool needsCtrlIn = false;
  328. bool needsCtrlOut = false;
  329. const int portNameSize = x_engine->maxPortNameSize();
  330. CarlaString portName;
  331. for (unsigned long i=0; i < portCount; i++)
  332. {
  333. const LADSPA_PortDescriptor portType = ldescriptor->PortDescriptors[i];
  334. const LADSPA_PortRangeHint portHints = ldescriptor->PortRangeHints[i];
  335. if (LADSPA_IS_PORT_AUDIO(portType))
  336. {
  337. portName.clear();
  338. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  339. {
  340. portName = m_name;
  341. portName += ":";
  342. }
  343. portName += ldescriptor->PortNames[i];
  344. portName.truncate(portNameSize);
  345. if (LADSPA_IS_PORT_INPUT(portType))
  346. {
  347. j = aIn.count++;
  348. aIn.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  349. aIn.rindexes[j] = i;
  350. if (forcedStereoIn)
  351. {
  352. portName += "_2";
  353. aIn.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  354. aIn.rindexes[1] = i;
  355. }
  356. }
  357. else if (LADSPA_IS_PORT_OUTPUT(portType))
  358. {
  359. j = aOut.count++;
  360. aOut.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  361. aOut.rindexes[j] = i;
  362. needsCtrlIn = true;
  363. if (forcedStereoOut)
  364. {
  365. portName += "_2";
  366. aOut.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  367. aOut.rindexes[1] = i;
  368. }
  369. }
  370. else
  371. qWarning("WARNING - Got a broken Port (Audio, but not input or output)");
  372. }
  373. else if (LADSPA_IS_PORT_CONTROL(portType))
  374. {
  375. j = param.count++;
  376. param.data[j].index = j;
  377. param.data[j].rindex = i;
  378. param.data[j].hints = 0;
  379. param.data[j].midiChannel = 0;
  380. param.data[j].midiCC = -1;
  381. double min, max, def, step, stepSmall, stepLarge;
  382. // min value
  383. if (LADSPA_IS_HINT_BOUNDED_BELOW(portHints.HintDescriptor))
  384. min = portHints.LowerBound;
  385. else
  386. min = 0.0;
  387. // max value
  388. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portHints.HintDescriptor))
  389. max = portHints.UpperBound;
  390. else
  391. max = 1.0;
  392. if (min > max)
  393. max = min;
  394. else if (max < min)
  395. min = max;
  396. if (max - min == 0.0)
  397. {
  398. qWarning("Broken plugin parameter: max - min == 0");
  399. max = min + 0.1;
  400. }
  401. // default value
  402. def = get_default_ladspa_port_value(portHints.HintDescriptor, min, max);
  403. if (def < min)
  404. def = min;
  405. else if (def > max)
  406. def = max;
  407. if (LADSPA_IS_HINT_SAMPLE_RATE(portHints.HintDescriptor))
  408. {
  409. min *= sampleRate;
  410. max *= sampleRate;
  411. def *= sampleRate;
  412. param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  413. }
  414. if (LADSPA_IS_HINT_TOGGLED(portHints.HintDescriptor))
  415. {
  416. step = max - min;
  417. stepSmall = step;
  418. stepLarge = step;
  419. param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  420. }
  421. else if (LADSPA_IS_HINT_INTEGER(portHints.HintDescriptor))
  422. {
  423. step = 1.0;
  424. stepSmall = 1.0;
  425. stepLarge = 10.0;
  426. param.data[j].hints |= PARAMETER_IS_INTEGER;
  427. }
  428. else
  429. {
  430. double range = max - min;
  431. step = range/100.0;
  432. stepSmall = range/1000.0;
  433. stepLarge = range/10.0;
  434. }
  435. if (LADSPA_IS_PORT_INPUT(portType))
  436. {
  437. param.data[j].type = PARAMETER_INPUT;
  438. param.data[j].hints |= PARAMETER_IS_ENABLED;
  439. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  440. needsCtrlIn = true;
  441. // MIDI CC value
  442. if (descriptor->get_midi_controller_for_port)
  443. {
  444. int controller = descriptor->get_midi_controller_for_port(handle, i);
  445. if (DSSI_CONTROLLER_IS_SET(controller) && DSSI_IS_CC(controller))
  446. {
  447. int16_t cc = DSSI_CC_NUMBER(controller);
  448. if (! MIDI_IS_CONTROL_BANK_SELECT(cc))
  449. param.data[j].midiCC = cc;
  450. }
  451. }
  452. }
  453. else if (LADSPA_IS_PORT_OUTPUT(portType))
  454. {
  455. if (strcmp(ldescriptor->PortNames[i], "latency") == 0 || strcmp(ldescriptor->PortNames[i], "_latency") == 0)
  456. {
  457. min = 0.0;
  458. max = sampleRate;
  459. def = 0.0;
  460. step = 1.0;
  461. stepSmall = 1.0;
  462. stepLarge = 1.0;
  463. param.data[j].type = PARAMETER_LATENCY;
  464. param.data[j].hints = 0;
  465. }
  466. else if (strcmp(ldescriptor->PortNames[i], "_sample-rate") == 0)
  467. {
  468. def = sampleRate;
  469. step = 1.0;
  470. stepSmall = 1.0;
  471. stepLarge = 1.0;
  472. param.data[j].type = PARAMETER_SAMPLE_RATE;
  473. param.data[j].hints = 0;
  474. }
  475. else
  476. {
  477. param.data[j].type = PARAMETER_OUTPUT;
  478. param.data[j].hints |= PARAMETER_IS_ENABLED;
  479. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  480. needsCtrlOut = true;
  481. }
  482. }
  483. else
  484. {
  485. param.data[j].type = PARAMETER_UNKNOWN;
  486. qWarning("WARNING - Got a broken Port (Control, but not input or output)");
  487. }
  488. // extra parameter hints
  489. if (LADSPA_IS_HINT_LOGARITHMIC(portHints.HintDescriptor))
  490. param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  491. param.ranges[j].min = min;
  492. param.ranges[j].max = max;
  493. param.ranges[j].def = def;
  494. param.ranges[j].step = step;
  495. param.ranges[j].stepSmall = stepSmall;
  496. param.ranges[j].stepLarge = stepLarge;
  497. // Start parameters in their default values
  498. paramBuffers[j] = def;
  499. ldescriptor->connect_port(handle, i, &paramBuffers[j]);
  500. if (h2) ldescriptor->connect_port(h2, i, &paramBuffers[j]);
  501. }
  502. else
  503. {
  504. // Not Audio or Control
  505. qCritical("ERROR - Got a broken Port (neither Audio or Control)");
  506. ldescriptor->connect_port(handle, i, nullptr);
  507. if (h2) ldescriptor->connect_port(h2, i, nullptr);
  508. }
  509. }
  510. if (needsCtrlIn)
  511. {
  512. portName.clear();
  513. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  514. {
  515. portName = m_name;
  516. portName += ":";
  517. }
  518. portName += "control-in";
  519. portName.truncate(portNameSize);
  520. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  521. }
  522. if (needsCtrlOut)
  523. {
  524. portName.clear();
  525. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  526. {
  527. portName = m_name;
  528. portName += ":";
  529. }
  530. portName += "control-out";
  531. portName.truncate(portNameSize);
  532. param.portCout = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, false);
  533. }
  534. if (mIns == 1)
  535. {
  536. portName.clear();
  537. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  538. {
  539. portName = m_name;
  540. portName += ":";
  541. }
  542. portName += "midi-in";
  543. portName.truncate(portNameSize);
  544. midi.portMin = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  545. }
  546. aIn.count = aIns;
  547. aOut.count = aOuts;
  548. param.count = params;
  549. // plugin checks
  550. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  551. if (midi.portMin && aOut.count > 0)
  552. m_hints |= PLUGIN_IS_SYNTH;
  553. if (x_engine->getOptions().useDssiVstChunks && QString(m_filename).endsWith("dssi-vst.so", Qt::CaseInsensitive))
  554. {
  555. if (descriptor->get_custom_data && descriptor->set_custom_data)
  556. m_hints |= PLUGIN_USES_CHUNKS;
  557. }
  558. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  559. m_hints |= PLUGIN_CAN_DRYWET;
  560. if (aOuts > 0)
  561. m_hints |= PLUGIN_CAN_VOLUME;
  562. if (aOuts >= 2 && aOuts%2 == 0)
  563. m_hints |= PLUGIN_CAN_BALANCE;
  564. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  565. m_hints |= PLUGIN_CAN_FORCE_STEREO;
  566. // check latency
  567. if (m_hints & PLUGIN_CAN_DRYWET)
  568. {
  569. bool hasLatency = false;
  570. m_latency = 0;
  571. for (uint32_t i=0; i < param.count; i++)
  572. {
  573. if (param.data[i].type == PARAMETER_LATENCY)
  574. {
  575. // pre-run so plugin can update latency control-port
  576. float tmpIn[2][aIns];
  577. float tmpOut[2][aOuts];
  578. for (j=0; j < aIn.count; j++)
  579. {
  580. tmpIn[j][0] = 0.0f;
  581. tmpIn[j][1] = 0.0f;
  582. if (j == 0 || ! h2)
  583. ldescriptor->connect_port(handle, aIn.rindexes[j], tmpIn[j]);
  584. }
  585. for (j=0; j < aOut.count; j++)
  586. {
  587. tmpOut[j][0] = 0.0f;
  588. tmpOut[j][1] = 0.0f;
  589. if (j == 0 || ! h2)
  590. ldescriptor->connect_port(handle, aOut.rindexes[j], tmpOut[j]);
  591. }
  592. if (ldescriptor->activate)
  593. ldescriptor->activate(handle);
  594. ldescriptor->run(handle, 2);
  595. if (ldescriptor->deactivate)
  596. ldescriptor->deactivate(handle);
  597. m_latency = rint(paramBuffers[i]);
  598. hasLatency = true;
  599. break;
  600. }
  601. }
  602. if (hasLatency)
  603. {
  604. x_client->setLatency(m_latency);
  605. recreateLatencyBuffers();
  606. }
  607. }
  608. reloadPrograms(true);
  609. x_client->activate();
  610. qDebug("DssiPlugin::reload() - end");
  611. }
  612. void reloadPrograms(const bool init)
  613. {
  614. qDebug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  615. uint32_t i, oldCount = midiprog.count;
  616. // Delete old programs
  617. if (midiprog.count > 0)
  618. {
  619. for (i=0; i < midiprog.count; i++)
  620. {
  621. if (midiprog.data[i].name)
  622. free((void*)midiprog.data[i].name);
  623. }
  624. delete[] midiprog.data;
  625. }
  626. midiprog.count = 0;
  627. midiprog.data = nullptr;
  628. // Query new programs
  629. if (descriptor->get_program && descriptor->select_program)
  630. {
  631. while (descriptor->get_program(handle, midiprog.count))
  632. midiprog.count += 1;
  633. }
  634. if (midiprog.count > 0)
  635. midiprog.data = new MidiProgramData[midiprog.count];
  636. // Update data
  637. for (i=0; i < midiprog.count; i++)
  638. {
  639. const DSSI_Program_Descriptor* const pdesc = descriptor->get_program(handle, i);
  640. CARLA_ASSERT(pdesc);
  641. CARLA_ASSERT(pdesc->Name);
  642. midiprog.data[i].bank = pdesc->Bank;
  643. midiprog.data[i].program = pdesc->Program;
  644. midiprog.data[i].name = strdup(pdesc->Name ? pdesc->Name : "");
  645. }
  646. #ifndef BUILD_BRIDGE
  647. // Update OSC Names
  648. if (x_engine->isOscControlRegistered())
  649. {
  650. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  651. for (i=0; i < midiprog.count; i++)
  652. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  653. }
  654. #endif
  655. if (init)
  656. {
  657. if (midiprog.count > 0)
  658. setMidiProgram(0, false, false, false, true);
  659. }
  660. else
  661. {
  662. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0, nullptr);
  663. // Check if current program is invalid
  664. bool programChanged = false;
  665. if (midiprog.count == oldCount+1)
  666. {
  667. // one midi program added, probably created by user
  668. midiprog.current = oldCount;
  669. programChanged = true;
  670. }
  671. else if (midiprog.current >= (int32_t)midiprog.count)
  672. {
  673. // current midi program > count
  674. midiprog.current = 0;
  675. programChanged = true;
  676. }
  677. else if (midiprog.current < 0 && midiprog.count > 0)
  678. {
  679. // programs exist now, but not before
  680. midiprog.current = 0;
  681. programChanged = true;
  682. }
  683. else if (midiprog.current >= 0 && midiprog.count == 0)
  684. {
  685. // programs existed before, but not anymore
  686. midiprog.current = -1;
  687. programChanged = true;
  688. }
  689. if (programChanged)
  690. setMidiProgram(midiprog.current, true, true, true, true);
  691. }
  692. }
  693. // -------------------------------------------------------------------
  694. // Plugin processing
  695. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  696. {
  697. uint32_t i, k;
  698. unsigned long midiEventCount = 0;
  699. double aInsPeak[2] = { 0.0 };
  700. double aOutsPeak[2] = { 0.0 };
  701. CARLA_PROCESS_CONTINUE_CHECK;
  702. // --------------------------------------------------------------------------------------------------------
  703. // Input VU
  704. if (aIn.count > 0 && x_engine->getOptions().processMode != PROCESS_MODE_CONTINUOUS_RACK)
  705. {
  706. if (aIn.count == 1)
  707. {
  708. for (k=0; k < frames; k++)
  709. {
  710. if (std::abs(inBuffer[0][k]) > aInsPeak[0])
  711. aInsPeak[0] = std::abs(inBuffer[0][k]);
  712. }
  713. }
  714. else if (aIn.count > 1)
  715. {
  716. for (k=0; k < frames; k++)
  717. {
  718. if (std::abs(inBuffer[0][k]) > aInsPeak[0])
  719. aInsPeak[0] = std::abs(inBuffer[0][k]);
  720. if (std::abs(inBuffer[1][k]) > aInsPeak[1])
  721. aInsPeak[1] = std::abs(inBuffer[1][k]);
  722. }
  723. }
  724. }
  725. CARLA_PROCESS_CONTINUE_CHECK;
  726. // --------------------------------------------------------------------------------------------------------
  727. // Parameters Input [Automation]
  728. if (param.portCin && m_active && m_activeBefore)
  729. {
  730. bool allNotesOffSent = false;
  731. const CarlaEngineControlEvent* cinEvent;
  732. uint32_t time, nEvents = param.portCin->getEventCount();
  733. uint32_t nextBankId = 0;
  734. if (midiprog.current >= 0 && midiprog.count > 0)
  735. nextBankId = midiprog.data[midiprog.current].bank;
  736. for (i=0; i < nEvents; i++)
  737. {
  738. cinEvent = param.portCin->getEvent(i);
  739. if (! cinEvent)
  740. continue;
  741. time = cinEvent->time - framesOffset;
  742. if (time >= frames)
  743. continue;
  744. // Control change
  745. switch (cinEvent->type)
  746. {
  747. case CarlaEngineNullEvent:
  748. break;
  749. case CarlaEngineParameterChangeEvent:
  750. {
  751. double value;
  752. // Control backend stuff
  753. if (cinEvent->channel == m_ctrlInChannel)
  754. {
  755. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->parameter) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  756. {
  757. value = cinEvent->value;
  758. setDryWet(value, false, false);
  759. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  760. continue;
  761. }
  762. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->parameter) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  763. {
  764. value = cinEvent->value*127/100;
  765. setVolume(value, false, false);
  766. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  767. continue;
  768. }
  769. if (MIDI_IS_CONTROL_BALANCE(cinEvent->parameter) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  770. {
  771. double left, right;
  772. value = cinEvent->value/0.5 - 1.0;
  773. if (value < 0.0)
  774. {
  775. left = -1.0;
  776. right = (value*2)+1.0;
  777. }
  778. else if (value > 0.0)
  779. {
  780. left = (value*2)-1.0;
  781. right = 1.0;
  782. }
  783. else
  784. {
  785. left = -1.0;
  786. right = 1.0;
  787. }
  788. setBalanceLeft(left, false, false);
  789. setBalanceRight(right, false, false);
  790. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  791. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  792. continue;
  793. }
  794. }
  795. // Control plugin parameters
  796. for (k=0; k < param.count; k++)
  797. {
  798. if (param.data[k].midiChannel != cinEvent->channel)
  799. continue;
  800. if (param.data[k].midiCC != cinEvent->parameter)
  801. continue;
  802. if (param.data[k].type != PARAMETER_INPUT)
  803. continue;
  804. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  805. {
  806. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  807. {
  808. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  809. }
  810. else
  811. {
  812. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  813. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  814. value = rint(value);
  815. }
  816. setParameterValue(k, value, false, false, false);
  817. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  818. }
  819. }
  820. break;
  821. }
  822. case CarlaEngineMidiBankChangeEvent:
  823. if (cinEvent->channel == m_ctrlInChannel)
  824. nextBankId = rint(cinEvent->value);
  825. break;
  826. case CarlaEngineMidiProgramChangeEvent:
  827. if (cinEvent->channel == m_ctrlInChannel)
  828. {
  829. uint32_t nextProgramId = rint(cinEvent->value);
  830. for (k=0; k < midiprog.count; k++)
  831. {
  832. if (midiprog.data[k].bank == nextBankId && midiprog.data[k].program == nextProgramId)
  833. {
  834. setMidiProgram(k, false, false, false, false);
  835. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  836. break;
  837. }
  838. }
  839. }
  840. break;
  841. case CarlaEngineAllSoundOffEvent:
  842. if (cinEvent->channel == m_ctrlInChannel)
  843. {
  844. if (midi.portMin && ! allNotesOffSent)
  845. sendMidiAllNotesOff();
  846. if (ldescriptor->deactivate)
  847. {
  848. ldescriptor->deactivate(handle);
  849. if (h2) ldescriptor->deactivate(h2);
  850. }
  851. if (ldescriptor->activate)
  852. {
  853. ldescriptor->activate(handle);
  854. if (h2) ldescriptor->activate(h2);
  855. }
  856. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  857. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  858. allNotesOffSent = true;
  859. }
  860. break;
  861. case CarlaEngineAllNotesOffEvent:
  862. if (cinEvent->channel == m_ctrlInChannel)
  863. {
  864. if (midi.portMin && ! allNotesOffSent)
  865. sendMidiAllNotesOff();
  866. allNotesOffSent = true;
  867. }
  868. break;
  869. }
  870. }
  871. } // End of Parameters Input
  872. CARLA_PROCESS_CONTINUE_CHECK;
  873. // --------------------------------------------------------------------------------------------------------
  874. // MIDI Input
  875. if (midi.portMin && m_active && m_activeBefore)
  876. {
  877. // ----------------------------------------------------------------------------------------------------
  878. // MIDI Input (External)
  879. {
  880. engineMidiLock();
  881. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  882. {
  883. if (extMidiNotes[i].channel < 0)
  884. break;
  885. snd_seq_event_t* const midiEvent = &midiEvents[midiEventCount];
  886. memset(midiEvent, 0, sizeof(snd_seq_event_t));
  887. midiEvent->type = extMidiNotes[i].velo ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  888. midiEvent->data.note.channel = extMidiNotes[i].channel;
  889. midiEvent->data.note.note = extMidiNotes[i].note;
  890. midiEvent->data.note.velocity = extMidiNotes[i].velo;
  891. extMidiNotes[i].channel = -1; // mark as invalid
  892. midiEventCount += 1;
  893. }
  894. engineMidiUnlock();
  895. } // End of MIDI Input (External)
  896. CARLA_PROCESS_CONTINUE_CHECK;
  897. // ----------------------------------------------------------------------------------------------------
  898. // MIDI Input (System)
  899. {
  900. const CarlaEngineMidiEvent* minEvent;
  901. uint32_t time, nEvents = midi.portMin->getEventCount();
  902. for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
  903. {
  904. minEvent = midi.portMin->getEvent(i);
  905. if (! minEvent)
  906. continue;
  907. time = minEvent->time - framesOffset;
  908. if (time >= frames)
  909. continue;
  910. uint8_t status = minEvent->data[0];
  911. uint8_t channel = status & 0x0F;
  912. // Fix bad note-off
  913. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  914. status -= 0x10;
  915. snd_seq_event_t* const midiEvent = &midiEvents[midiEventCount];
  916. memset(midiEvent, 0, sizeof(snd_seq_event_t));
  917. midiEvent->time.tick = time;
  918. if (MIDI_IS_STATUS_NOTE_OFF(status))
  919. {
  920. uint8_t note = minEvent->data[1];
  921. midiEvent->type = SND_SEQ_EVENT_NOTEOFF;
  922. midiEvent->data.note.channel = channel;
  923. midiEvent->data.note.note = note;
  924. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  925. }
  926. else if (MIDI_IS_STATUS_NOTE_ON(status))
  927. {
  928. uint8_t note = minEvent->data[1];
  929. uint8_t velo = minEvent->data[2];
  930. midiEvent->type = SND_SEQ_EVENT_NOTEON;
  931. midiEvent->data.note.channel = channel;
  932. midiEvent->data.note.note = note;
  933. midiEvent->data.note.velocity = velo;
  934. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  935. }
  936. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  937. {
  938. uint8_t note = minEvent->data[1];
  939. uint8_t pressure = minEvent->data[2];
  940. midiEvent->type = SND_SEQ_EVENT_KEYPRESS;
  941. midiEvent->data.note.channel = channel;
  942. midiEvent->data.note.note = note;
  943. midiEvent->data.note.velocity = pressure;
  944. }
  945. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  946. {
  947. uint8_t pressure = minEvent->data[1];
  948. midiEvent->type = SND_SEQ_EVENT_CHANPRESS;
  949. midiEvent->data.control.channel = channel;
  950. midiEvent->data.control.value = pressure;
  951. }
  952. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  953. {
  954. uint8_t lsb = minEvent->data[1];
  955. uint8_t msb = minEvent->data[2];
  956. midiEvent->type = SND_SEQ_EVENT_PITCHBEND;
  957. midiEvent->data.control.channel = channel;
  958. midiEvent->data.control.value = ((msb << 7) | lsb) - 8192;
  959. }
  960. else
  961. continue;
  962. midiEventCount += 1;
  963. }
  964. } // End of MIDI Input (System)
  965. } // End of MIDI Input
  966. CARLA_PROCESS_CONTINUE_CHECK;
  967. // --------------------------------------------------------------------------------------------------------
  968. // Special Parameters
  969. #if 0
  970. for (k=0; k < param.count; k++)
  971. {
  972. if (param.data[k].type == PARAMETER_LATENCY)
  973. {
  974. // TODO
  975. }
  976. }
  977. CARLA_PROCESS_CONTINUE_CHECK;
  978. #endif
  979. // --------------------------------------------------------------------------------------------------------
  980. // Plugin processing
  981. if (m_active)
  982. {
  983. if (! m_activeBefore)
  984. {
  985. if (midi.portMin)
  986. {
  987. for (k=0; k < MAX_MIDI_CHANNELS; k++)
  988. {
  989. memset(&midiEvents[k], 0, sizeof(snd_seq_event_t));
  990. midiEvents[k].type = SND_SEQ_EVENT_CONTROLLER;
  991. midiEvents[k].data.control.channel = k;
  992. midiEvents[k].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  993. memset(&midiEvents[k*2], 0, sizeof(snd_seq_event_t));
  994. midiEvents[k*2].type = SND_SEQ_EVENT_CONTROLLER;
  995. midiEvents[k*2].data.control.channel = k;
  996. midiEvents[k*2].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  997. }
  998. midiEventCount = MAX_MIDI_CHANNELS;
  999. }
  1000. if (m_latency > 0)
  1001. {
  1002. for (i=0; i < aIn.count; i++)
  1003. memset(m_latencyBuffers[i], 0, sizeof(float)*m_latency);
  1004. }
  1005. if (ldescriptor->activate)
  1006. {
  1007. ldescriptor->activate(handle);
  1008. if (h2) ldescriptor->activate(h2);
  1009. }
  1010. }
  1011. for (i=0; i < aIn.count; i++)
  1012. {
  1013. if (i == 0 || ! h2) ldescriptor->connect_port(handle, aIn.rindexes[i], inBuffer[i]);
  1014. else if (i == 1) ldescriptor->connect_port(h2, aIn.rindexes[i], inBuffer[i]);
  1015. }
  1016. for (i=0; i < aOut.count; i++)
  1017. {
  1018. if (i == 0 || ! h2) ldescriptor->connect_port(handle, aOut.rindexes[i], outBuffer[i]);
  1019. else if (i == 1) ldescriptor->connect_port(h2, aOut.rindexes[i], outBuffer[i]);
  1020. }
  1021. if (descriptor->run_synth)
  1022. {
  1023. descriptor->run_synth(handle, frames, midiEvents, midiEventCount);
  1024. if (h2) descriptor->run_synth(h2, frames, midiEvents, midiEventCount);
  1025. }
  1026. else if (descriptor->run_multiple_synths)
  1027. {
  1028. LADSPA_Handle handlePtr[2] = { handle, h2 };
  1029. snd_seq_event_t* midiEventsPtr[2] = { midiEvents, midiEvents };
  1030. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1031. descriptor->run_multiple_synths(h2 ? 2 : 1, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1032. }
  1033. else
  1034. {
  1035. ldescriptor->run(handle, frames);
  1036. if (h2) ldescriptor->run(h2, frames);
  1037. }
  1038. }
  1039. else
  1040. {
  1041. if (m_activeBefore)
  1042. {
  1043. if (ldescriptor->deactivate)
  1044. {
  1045. ldescriptor->deactivate(handle);
  1046. if (h2) ldescriptor->deactivate(h2);
  1047. }
  1048. }
  1049. }
  1050. CARLA_PROCESS_CONTINUE_CHECK;
  1051. // --------------------------------------------------------------------------------------------------------
  1052. // Post-processing (dry/wet, volume and balance)
  1053. if (m_active)
  1054. {
  1055. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  1056. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  1057. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  1058. double bal_rangeL, bal_rangeR;
  1059. float bufValue, oldBufLeft[do_balance ? frames : 1];
  1060. for (i=0; i < aOut.count; i++)
  1061. {
  1062. // Dry/Wet
  1063. if (do_drywet)
  1064. {
  1065. for (k=0; k < frames; k++)
  1066. {
  1067. if (k < m_latency && m_latency < frames)
  1068. bufValue = (aIn.count == 1) ? m_latencyBuffers[0][k] : m_latencyBuffers[i][k];
  1069. else
  1070. bufValue = (aIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1071. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(bufValue*(1.0-x_dryWet));
  1072. }
  1073. }
  1074. // Balance
  1075. if (do_balance)
  1076. {
  1077. if (i%2 == 0)
  1078. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1079. bal_rangeL = (x_balanceLeft+1.0)/2;
  1080. bal_rangeR = (x_balanceRight+1.0)/2;
  1081. for (k=0; k < frames; k++)
  1082. {
  1083. if (i%2 == 0)
  1084. {
  1085. // left output
  1086. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  1087. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  1088. }
  1089. else
  1090. {
  1091. // right
  1092. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  1093. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  1094. }
  1095. }
  1096. }
  1097. // Volume
  1098. if (do_volume)
  1099. {
  1100. for (k=0; k < frames; k++)
  1101. outBuffer[i][k] *= x_volume;
  1102. }
  1103. // Output VU
  1104. if (x_engine->getOptions().processMode != PROCESS_MODE_CONTINUOUS_RACK)
  1105. {
  1106. for (k=0; i < 2 && k < frames; k++)
  1107. {
  1108. if (std::abs(outBuffer[i][k]) > aOutsPeak[i])
  1109. aOutsPeak[i] = std::abs(outBuffer[i][k]);
  1110. }
  1111. }
  1112. }
  1113. // Latency, save values for next callback
  1114. if (m_latency > 0 && m_latency < frames)
  1115. {
  1116. for (i=0; i < aIn.count; i++)
  1117. memcpy(m_latencyBuffers[i], inBuffer[i] + (frames - m_latency), sizeof(float)*m_latency);
  1118. }
  1119. }
  1120. else
  1121. {
  1122. // disable any output sound if not active
  1123. for (i=0; i < aOut.count; i++)
  1124. carla_zeroF(outBuffer[i], frames);
  1125. aOutsPeak[0] = 0.0;
  1126. aOutsPeak[1] = 0.0;
  1127. } // End of Post-processing
  1128. CARLA_PROCESS_CONTINUE_CHECK;
  1129. // --------------------------------------------------------------------------------------------------------
  1130. // Control Output
  1131. if (param.portCout && m_active)
  1132. {
  1133. double value;
  1134. for (k=0; k < param.count; k++)
  1135. {
  1136. if (param.data[k].type == PARAMETER_OUTPUT)
  1137. {
  1138. fixParameterValue(paramBuffers[k], param.ranges[k]);
  1139. if (param.data[k].midiCC > 0)
  1140. {
  1141. value = (paramBuffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  1142. param.portCout->writeEvent(CarlaEngineParameterChangeEvent, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, value);
  1143. }
  1144. }
  1145. }
  1146. } // End of Control Output
  1147. CARLA_PROCESS_CONTINUE_CHECK;
  1148. // --------------------------------------------------------------------------------------------------------
  1149. // Peak Values
  1150. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  1151. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  1152. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  1153. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  1154. m_activeBefore = m_active;
  1155. }
  1156. // -------------------------------------------------------------------
  1157. // Post-poned events
  1158. void uiParameterChange(const uint32_t index, const double value)
  1159. {
  1160. CARLA_ASSERT(index < param.count);
  1161. if (index >= param.count)
  1162. return;
  1163. if (! osc.data.target)
  1164. return;
  1165. osc_send_control(&osc.data, param.data[index].rindex, value);
  1166. }
  1167. void uiMidiProgramChange(const uint32_t index)
  1168. {
  1169. CARLA_ASSERT(index < midiprog.count);
  1170. if (index >= midiprog.count)
  1171. return;
  1172. if (! osc.data.target)
  1173. return;
  1174. osc_send_program(&osc.data, midiprog.data[index].bank, midiprog.data[index].program);
  1175. }
  1176. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1177. {
  1178. CARLA_ASSERT(channel < 16);
  1179. CARLA_ASSERT(note < 128);
  1180. CARLA_ASSERT(velo > 0 && velo < 128);
  1181. if (! osc.data.target)
  1182. return;
  1183. uint8_t midiData[4] = { 0 };
  1184. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1185. midiData[2] = note;
  1186. midiData[3] = velo;
  1187. osc_send_midi(&osc.data, midiData);
  1188. }
  1189. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1190. {
  1191. CARLA_ASSERT(channel < 16);
  1192. CARLA_ASSERT(note < 128);
  1193. if (! osc.data.target)
  1194. return;
  1195. uint8_t midiData[4] = { 0 };
  1196. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1197. midiData[2] = note;
  1198. osc_send_midi(&osc.data, midiData);
  1199. }
  1200. // -------------------------------------------------------------------
  1201. // Cleanup
  1202. void deleteBuffers()
  1203. {
  1204. qDebug("DssiPlugin::deleteBuffers() - start");
  1205. if (param.count > 0)
  1206. delete[] paramBuffers;
  1207. paramBuffers = nullptr;
  1208. CarlaPlugin::deleteBuffers();
  1209. qDebug("DssiPlugin::deleteBuffers() - end");
  1210. }
  1211. // -------------------------------------------------------------------
  1212. bool init(const char* const filename, const char* const name, const char* const label, const char* const guiFilename)
  1213. {
  1214. // ---------------------------------------------------------------
  1215. // open DLL
  1216. if (! libOpen(filename))
  1217. {
  1218. x_engine->setLastError(libError(filename));
  1219. return false;
  1220. }
  1221. // ---------------------------------------------------------------
  1222. // get DLL main entry
  1223. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)libSymbol("dssi_descriptor");
  1224. if (! descFn)
  1225. {
  1226. x_engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1227. return false;
  1228. }
  1229. // ---------------------------------------------------------------
  1230. // get descriptor that matches label
  1231. unsigned long i = 0;
  1232. while ((descriptor = descFn(i++)))
  1233. {
  1234. ldescriptor = descriptor->LADSPA_Plugin;
  1235. if (ldescriptor && strcmp(ldescriptor->Label, label) == 0)
  1236. break;
  1237. }
  1238. if (! descriptor)
  1239. {
  1240. x_engine->setLastError("Could not find the requested plugin Label in the plugin library");
  1241. return false;
  1242. }
  1243. // ---------------------------------------------------------------
  1244. // get info
  1245. m_filename = strdup(filename);
  1246. if (name)
  1247. m_name = x_engine->getUniquePluginName(name);
  1248. else
  1249. m_name = x_engine->getUniquePluginName(ldescriptor->Name);
  1250. // ---------------------------------------------------------------
  1251. // register client
  1252. x_client = x_engine->addClient(this);
  1253. if (! x_client->isOk())
  1254. {
  1255. x_engine->setLastError("Failed to register plugin client");
  1256. return false;
  1257. }
  1258. // ---------------------------------------------------------------
  1259. // initialize plugin
  1260. handle = ldescriptor->instantiate(ldescriptor, x_engine->getSampleRate());
  1261. if (! handle)
  1262. {
  1263. x_engine->setLastError("Plugin failed to initialize");
  1264. return false;
  1265. }
  1266. // ---------------------------------------------------------------
  1267. // gui stuff
  1268. if (guiFilename)
  1269. {
  1270. osc.thread = new CarlaPluginThread(x_engine, this, CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  1271. osc.thread->setOscData(guiFilename, ldescriptor->Label);
  1272. m_hints |= PLUGIN_HAS_GUI;
  1273. }
  1274. return true;
  1275. }
  1276. private:
  1277. LADSPA_Handle handle, h2;
  1278. const LADSPA_Descriptor* ldescriptor;
  1279. const DSSI_Descriptor* descriptor;
  1280. snd_seq_event_t midiEvents[MAX_MIDI_EVENTS];
  1281. float* paramBuffers;
  1282. };
  1283. /**@}*/
  1284. CARLA_BACKEND_END_NAMESPACE
  1285. #else // WANT_DSSI
  1286. # warning Building without DSSI support
  1287. #endif
  1288. CARLA_BACKEND_START_NAMESPACE
  1289. CarlaPlugin* CarlaPlugin::newDSSI(const initializer& init, const void* const extra)
  1290. {
  1291. qDebug("CarlaPlugin::newDSSI(%p, \"%s\", \"%s\", \"%s\", %p)", init.engine, init.filename, init.name, init.label, extra);
  1292. #ifdef WANT_DSSI
  1293. short id = init.engine->getNewPluginId();
  1294. if (id < 0 || id > init.engine->maxPluginNumber())
  1295. {
  1296. init.engine->setLastError("Maximum number of plugins reached");
  1297. return nullptr;
  1298. }
  1299. DssiPlugin* const plugin = new DssiPlugin(init.engine, id);
  1300. if (! plugin->init(init.filename, init.name, init.label, (const char*)extra))
  1301. {
  1302. delete plugin;
  1303. return nullptr;
  1304. }
  1305. plugin->reload();
  1306. if (init.engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1307. {
  1308. if (! (plugin->hints() & PLUGIN_CAN_FORCE_STEREO))
  1309. {
  1310. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1311. delete plugin;
  1312. return nullptr;
  1313. }
  1314. }
  1315. plugin->registerToOscClient();
  1316. return plugin;
  1317. #else
  1318. init.engine->setLastError("DSSI support not available");
  1319. return nullptr;
  1320. #endif
  1321. }
  1322. CARLA_BACKEND_END_NAMESPACE