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.

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