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.

1668 lines
55KB

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