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.

1577 lines
51KB

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