Audio plugin host https://kx.studio/carla
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.

1646 lines
54KB

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