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.

1664 lines
54KB

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