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.

1643 lines
54KB

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