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.

1575 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. Q_ASSERT(ldescriptor);
  91. return ldescriptor->UniqueID;
  92. }
  93. // -------------------------------------------------------------------
  94. // Information (current data)
  95. int32_t chunkData(void** const dataPtr)
  96. {
  97. Q_ASSERT(dataPtr);
  98. Q_ASSERT(descriptor);
  99. Q_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. Q_ASSERT(parameterId < param.count);
  110. return paramBuffers[parameterId];
  111. }
  112. void getLabel(char* const strBuf)
  113. {
  114. Q_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. Q_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. Q_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. Q_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. Q_ASSERT(ldescriptor);
  147. Q_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. Q_ASSERT(type);
  157. Q_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. Q_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. Q_ASSERT(type == CUSTOM_DATA_STRING);
  172. Q_ASSERT(key);
  173. Q_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. Q_ASSERT(m_hints & PLUGIN_USES_CHUNKS);
  196. Q_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. Q_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. Q_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. Q_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);
  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. reloadPrograms(true);
  576. x_client->activate();
  577. qDebug("DssiPlugin::reload() - end");
  578. }
  579. void reloadPrograms(const bool init)
  580. {
  581. qDebug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  582. uint32_t i, oldCount = midiprog.count;
  583. // Delete old programs
  584. if (midiprog.count > 0)
  585. {
  586. for (i=0; i < midiprog.count; i++)
  587. {
  588. if (midiprog.data[i].name)
  589. free((void*)midiprog.data[i].name);
  590. }
  591. delete[] midiprog.data;
  592. }
  593. midiprog.count = 0;
  594. midiprog.data = nullptr;
  595. // Query new programs
  596. if (descriptor->get_program && descriptor->select_program)
  597. {
  598. while (descriptor->get_program(handle, midiprog.count))
  599. midiprog.count += 1;
  600. }
  601. if (midiprog.count > 0)
  602. midiprog.data = new midi_program_t[midiprog.count];
  603. // Update data
  604. for (i=0; i < midiprog.count; i++)
  605. {
  606. const DSSI_Program_Descriptor* const pdesc = descriptor->get_program(handle, i);
  607. Q_ASSERT(pdesc);
  608. Q_ASSERT(pdesc->Program < 128);
  609. Q_ASSERT(pdesc->Name);
  610. midiprog.data[i].bank = pdesc->Bank;
  611. midiprog.data[i].program = pdesc->Program;
  612. midiprog.data[i].name = strdup(pdesc->Name);
  613. }
  614. #ifndef BUILD_BRIDGE
  615. // Update OSC Names
  616. if (x_engine->isOscControllerRegisted())
  617. {
  618. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  619. for (i=0; i < midiprog.count; i++)
  620. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  621. }
  622. #endif
  623. if (init)
  624. {
  625. if (midiprog.count > 0)
  626. setMidiProgram(0, false, false, false, true);
  627. }
  628. else
  629. {
  630. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  631. // Check if current program is invalid
  632. bool programChanged = false;
  633. if (midiprog.count == oldCount+1)
  634. {
  635. // one midi program added, probably created by user
  636. midiprog.current = oldCount;
  637. programChanged = true;
  638. }
  639. else if (midiprog.current >= (int32_t)midiprog.count)
  640. {
  641. // current midi program > count
  642. midiprog.current = 0;
  643. programChanged = true;
  644. }
  645. else if (midiprog.current < 0 && midiprog.count > 0)
  646. {
  647. // programs exist now, but not before
  648. midiprog.current = 0;
  649. programChanged = true;
  650. }
  651. else if (midiprog.current >= 0 && midiprog.count == 0)
  652. {
  653. // programs existed before, but not anymore
  654. midiprog.current = -1;
  655. programChanged = true;
  656. }
  657. if (programChanged)
  658. setMidiProgram(midiprog.current, true, true, true, true);
  659. }
  660. }
  661. // -------------------------------------------------------------------
  662. // Plugin processing
  663. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  664. {
  665. uint32_t i, k;
  666. unsigned long midiEventCount = 0;
  667. double aInsPeak[2] = { 0.0 };
  668. double aOutsPeak[2] = { 0.0 };
  669. CARLA_PROCESS_CONTINUE_CHECK;
  670. // --------------------------------------------------------------------------------------------------------
  671. // Input VU
  672. if (aIn.count > 0)
  673. {
  674. if (aIn.count == 1)
  675. {
  676. for (k=0; k < frames; k++)
  677. {
  678. if (abs(inBuffer[0][k]) > aInsPeak[0])
  679. aInsPeak[0] = abs(inBuffer[0][k]);
  680. }
  681. }
  682. else if (aIn.count > 1)
  683. {
  684. for (k=0; k < frames; k++)
  685. {
  686. if (abs(inBuffer[0][k]) > aInsPeak[0])
  687. aInsPeak[0] = abs(inBuffer[0][k]);
  688. if (abs(inBuffer[1][k]) > aInsPeak[1])
  689. aInsPeak[1] = abs(inBuffer[1][k]);
  690. }
  691. }
  692. }
  693. CARLA_PROCESS_CONTINUE_CHECK;
  694. // --------------------------------------------------------------------------------------------------------
  695. // Parameters Input [Automation]
  696. if (param.portCin && m_active && m_activeBefore)
  697. {
  698. bool allNotesOffSent = false;
  699. const CarlaEngineControlEvent* cinEvent;
  700. uint32_t time, nEvents = param.portCin->getEventCount();
  701. uint32_t nextBankId = 0;
  702. if (midiprog.current >= 0 && midiprog.count > 0)
  703. nextBankId = midiprog.data[midiprog.current].bank;
  704. for (i=0; i < nEvents; i++)
  705. {
  706. cinEvent = param.portCin->getEvent(i);
  707. if (! cinEvent)
  708. continue;
  709. time = cinEvent->time - framesOffset;
  710. if (time >= frames)
  711. continue;
  712. // Control change
  713. switch (cinEvent->type)
  714. {
  715. case CarlaEngineEventNull:
  716. break;
  717. case CarlaEngineEventControlChange:
  718. {
  719. double value;
  720. // Control backend stuff
  721. if (cinEvent->channel == m_ctrlInChannel)
  722. {
  723. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->controller) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  724. {
  725. value = cinEvent->value;
  726. setDryWet(value, false, false);
  727. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  728. continue;
  729. }
  730. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->controller) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  731. {
  732. value = cinEvent->value*127/100;
  733. setVolume(value, false, false);
  734. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  735. continue;
  736. }
  737. if (MIDI_IS_CONTROL_BALANCE(cinEvent->controller) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  738. {
  739. double left, right;
  740. value = cinEvent->value/0.5 - 1.0;
  741. if (value < 0.0)
  742. {
  743. left = -1.0;
  744. right = (value*2)+1.0;
  745. }
  746. else if (value > 0.0)
  747. {
  748. left = (value*2)-1.0;
  749. right = 1.0;
  750. }
  751. else
  752. {
  753. left = -1.0;
  754. right = 1.0;
  755. }
  756. setBalanceLeft(left, false, false);
  757. setBalanceRight(right, false, false);
  758. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  759. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  760. continue;
  761. }
  762. }
  763. // Control plugin parameters
  764. for (k=0; k < param.count; k++)
  765. {
  766. if (param.data[k].midiChannel != cinEvent->channel)
  767. continue;
  768. if (param.data[k].midiCC != cinEvent->controller)
  769. continue;
  770. if (param.data[k].type != PARAMETER_INPUT)
  771. continue;
  772. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  773. {
  774. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  775. {
  776. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  777. }
  778. else
  779. {
  780. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  781. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  782. value = rint(value);
  783. }
  784. setParameterValue(k, value, false, false, false);
  785. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  786. }
  787. }
  788. break;
  789. }
  790. case CarlaEngineEventMidiBankChange:
  791. if (cinEvent->channel == m_ctrlInChannel)
  792. nextBankId = rint(cinEvent->value);
  793. break;
  794. case CarlaEngineEventMidiProgramChange:
  795. if (cinEvent->channel == m_ctrlInChannel)
  796. {
  797. uint32_t nextProgramId = rint(cinEvent->value);
  798. for (k=0; k < midiprog.count; k++)
  799. {
  800. if (midiprog.data[k].bank == nextBankId && midiprog.data[k].program == nextProgramId)
  801. {
  802. setMidiProgram(k, false, false, false, false);
  803. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  804. break;
  805. }
  806. }
  807. }
  808. break;
  809. case CarlaEngineEventAllSoundOff:
  810. if (cinEvent->channel == m_ctrlInChannel)
  811. {
  812. if (midi.portMin && ! allNotesOffSent)
  813. sendMidiAllNotesOff();
  814. if (ldescriptor->deactivate)
  815. {
  816. ldescriptor->deactivate(handle);
  817. if (h2) ldescriptor->deactivate(h2);
  818. }
  819. if (ldescriptor->activate)
  820. {
  821. ldescriptor->activate(handle);
  822. if (h2) ldescriptor->activate(h2);
  823. }
  824. allNotesOffSent = true;
  825. }
  826. break;
  827. case CarlaEngineEventAllNotesOff:
  828. if (cinEvent->channel == m_ctrlInChannel)
  829. {
  830. if (midi.portMin && ! allNotesOffSent)
  831. sendMidiAllNotesOff();
  832. allNotesOffSent = true;
  833. }
  834. break;
  835. }
  836. }
  837. } // End of Parameters Input
  838. CARLA_PROCESS_CONTINUE_CHECK;
  839. // --------------------------------------------------------------------------------------------------------
  840. // MIDI Input
  841. if (midi.portMin && m_active && m_activeBefore)
  842. {
  843. // ----------------------------------------------------------------------------------------------------
  844. // MIDI Input (External)
  845. {
  846. engineMidiLock();
  847. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  848. {
  849. if (extMidiNotes[i].channel < 0)
  850. break;
  851. snd_seq_event_t* const midiEvent = &midiEvents[midiEventCount];
  852. memset(midiEvent, 0, sizeof(snd_seq_event_t));
  853. midiEvent->type = extMidiNotes[i].velo ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  854. midiEvent->data.note.channel = m_ctrlInChannel;
  855. midiEvent->data.note.note = extMidiNotes[i].note;
  856. midiEvent->data.note.velocity = extMidiNotes[i].velo;
  857. extMidiNotes[i].channel = -1; // mark as invalid
  858. midiEventCount += 1;
  859. }
  860. engineMidiUnlock();
  861. } // End of MIDI Input (External)
  862. CARLA_PROCESS_CONTINUE_CHECK;
  863. // ----------------------------------------------------------------------------------------------------
  864. // MIDI Input (System)
  865. {
  866. const CarlaEngineMidiEvent* minEvent;
  867. uint32_t time, nEvents = midi.portMin->getEventCount();
  868. for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
  869. {
  870. minEvent = midi.portMin->getEvent(i);
  871. if (! minEvent)
  872. continue;
  873. time = minEvent->time - framesOffset;
  874. if (time >= frames)
  875. continue;
  876. uint8_t status = minEvent->data[0];
  877. uint8_t channel = status & 0x0F;
  878. // Fix bad note-off
  879. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  880. status -= 0x10;
  881. snd_seq_event_t* const midiEvent = &midiEvents[midiEventCount];
  882. memset(midiEvent, 0, sizeof(snd_seq_event_t));
  883. midiEvent->time.tick = time;
  884. if (MIDI_IS_STATUS_NOTE_OFF(status))
  885. {
  886. uint8_t note = minEvent->data[1];
  887. midiEvent->type = SND_SEQ_EVENT_NOTEOFF;
  888. midiEvent->data.note.channel = channel;
  889. midiEvent->data.note.note = note;
  890. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  891. }
  892. else if (MIDI_IS_STATUS_NOTE_ON(status))
  893. {
  894. uint8_t note = minEvent->data[1];
  895. uint8_t velo = minEvent->data[2];
  896. midiEvent->type = SND_SEQ_EVENT_NOTEON;
  897. midiEvent->data.note.channel = channel;
  898. midiEvent->data.note.note = note;
  899. midiEvent->data.note.velocity = velo;
  900. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  901. }
  902. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  903. {
  904. uint8_t note = minEvent->data[1];
  905. uint8_t pressure = minEvent->data[2];
  906. midiEvent->type = SND_SEQ_EVENT_KEYPRESS;
  907. midiEvent->data.note.channel = channel;
  908. midiEvent->data.note.note = note;
  909. midiEvent->data.note.velocity = pressure;
  910. }
  911. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  912. {
  913. uint8_t pressure = minEvent->data[1];
  914. midiEvent->type = SND_SEQ_EVENT_CHANPRESS;
  915. midiEvent->data.control.channel = channel;
  916. midiEvent->data.control.value = pressure;
  917. }
  918. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  919. {
  920. uint8_t lsb = minEvent->data[1];
  921. uint8_t msb = minEvent->data[2];
  922. midiEvent->type = SND_SEQ_EVENT_PITCHBEND;
  923. midiEvent->data.control.channel = channel;
  924. midiEvent->data.control.value = ((msb << 7) | lsb) - 8192;
  925. }
  926. else
  927. continue;
  928. midiEventCount += 1;
  929. }
  930. } // End of MIDI Input (System)
  931. } // End of MIDI Input
  932. CARLA_PROCESS_CONTINUE_CHECK;
  933. // --------------------------------------------------------------------------------------------------------
  934. // Special Parameters
  935. #if 0
  936. for (k=0; k < param.count; k++)
  937. {
  938. if (param.data[k].type == PARAMETER_LATENCY)
  939. {
  940. // TODO
  941. }
  942. }
  943. CARLA_PROCESS_CONTINUE_CHECK;
  944. #endif
  945. // --------------------------------------------------------------------------------------------------------
  946. // Plugin processing
  947. if (m_active)
  948. {
  949. if (! m_activeBefore)
  950. {
  951. if (midi.portMin && m_ctrlInChannel >= 0 && m_ctrlInChannel < 16)
  952. {
  953. memset(&midiEvents[0], 0, sizeof(snd_seq_event_t));
  954. midiEvents[0].type = SND_SEQ_EVENT_CONTROLLER;
  955. midiEvents[0].data.control.channel = m_ctrlInChannel;
  956. midiEvents[0].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  957. memset(&midiEvents[1], 0, sizeof(snd_seq_event_t));
  958. midiEvents[1].type = SND_SEQ_EVENT_CONTROLLER;
  959. midiEvents[1].data.control.channel = m_ctrlInChannel;
  960. midiEvents[1].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  961. midiEventCount = 2;
  962. }
  963. if (ldescriptor->activate)
  964. {
  965. ldescriptor->activate(handle);
  966. if (h2) ldescriptor->activate(h2);
  967. }
  968. }
  969. for (i=0; i < aIn.count; i++)
  970. {
  971. if (i == 0 || ! h2) ldescriptor->connect_port(handle, aIn.rindexes[i], inBuffer[i]);
  972. if (i == 1 && h2) ldescriptor->connect_port(h2, aIn.rindexes[i], inBuffer[i]);
  973. }
  974. for (i=0; i < aOut.count; i++)
  975. {
  976. if (i == 0 || ! h2) ldescriptor->connect_port(handle, aOut.rindexes[i], outBuffer[i]);
  977. if (i == 1 && h2) ldescriptor->connect_port(h2, aOut.rindexes[i], outBuffer[i]);
  978. }
  979. if (descriptor->run_synth)
  980. {
  981. descriptor->run_synth(handle, frames, midiEvents, midiEventCount);
  982. if (h2) descriptor->run_synth(handle, frames, midiEvents, midiEventCount);
  983. }
  984. else if (descriptor->run_multiple_synths)
  985. {
  986. LADSPA_Handle handlePtr[2] = { handle, h2 };
  987. snd_seq_event_t* midiEventsPtr[2] = { midiEvents, midiEvents };
  988. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  989. descriptor->run_multiple_synths(h2 ? 2 : 1, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  990. }
  991. else
  992. {
  993. ldescriptor->run(handle, frames);
  994. if (h2) ldescriptor->run(h2, frames);
  995. }
  996. }
  997. else
  998. {
  999. if (m_activeBefore)
  1000. {
  1001. if (ldescriptor->deactivate)
  1002. {
  1003. ldescriptor->deactivate(handle);
  1004. if (h2) ldescriptor->deactivate(h2);
  1005. }
  1006. }
  1007. }
  1008. CARLA_PROCESS_CONTINUE_CHECK;
  1009. // --------------------------------------------------------------------------------------------------------
  1010. // Post-processing (dry/wet, volume and balance)
  1011. if (m_active)
  1012. {
  1013. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  1014. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  1015. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  1016. double bal_rangeL, bal_rangeR;
  1017. float oldBufLeft[do_balance ? frames : 0];
  1018. for (i=0; i < aOut.count; i++)
  1019. {
  1020. // Dry/Wet
  1021. if (do_drywet)
  1022. {
  1023. for (k=0; k < frames; k++)
  1024. {
  1025. if (aOut.count == 1)
  1026. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[0][k]*(1.0-x_dryWet));
  1027. else
  1028. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[i][k]*(1.0-x_dryWet));
  1029. }
  1030. }
  1031. // Balance
  1032. if (do_balance)
  1033. {
  1034. if (i%2 == 0)
  1035. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1036. bal_rangeL = (x_balanceLeft+1.0)/2;
  1037. bal_rangeR = (x_balanceRight+1.0)/2;
  1038. for (k=0; k < frames; k++)
  1039. {
  1040. if (i%2 == 0)
  1041. {
  1042. // left output
  1043. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  1044. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  1045. }
  1046. else
  1047. {
  1048. // right
  1049. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  1050. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  1051. }
  1052. }
  1053. }
  1054. // Volume
  1055. if (do_volume)
  1056. {
  1057. for (k=0; k < frames; k++)
  1058. outBuffer[i][k] *= x_volume;
  1059. }
  1060. // Output VU
  1061. for (k=0; i < 2 && k < frames; k++)
  1062. {
  1063. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  1064. aOutsPeak[i] = abs(outBuffer[i][k]);
  1065. }
  1066. }
  1067. }
  1068. else
  1069. {
  1070. // disable any output sound if not active
  1071. for (i=0; i < aOut.count; i++)
  1072. memset(outBuffer[i], 0.0f, sizeof(float)*frames);
  1073. aOutsPeak[0] = 0.0;
  1074. aOutsPeak[1] = 0.0;
  1075. } // End of Post-processing
  1076. CARLA_PROCESS_CONTINUE_CHECK;
  1077. // --------------------------------------------------------------------------------------------------------
  1078. // Control Output
  1079. if (param.portCout && m_active)
  1080. {
  1081. double value;
  1082. for (k=0; k < param.count; k++)
  1083. {
  1084. if (param.data[k].type == PARAMETER_OUTPUT)
  1085. {
  1086. fixParameterValue(paramBuffers[k], param.ranges[k]);
  1087. if (param.data[k].midiCC > 0)
  1088. {
  1089. value = (paramBuffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  1090. param.portCout->writeEvent(CarlaEngineEventControlChange, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, value);
  1091. }
  1092. }
  1093. }
  1094. } // End of Control Output
  1095. CARLA_PROCESS_CONTINUE_CHECK;
  1096. // --------------------------------------------------------------------------------------------------------
  1097. // Peak Values
  1098. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  1099. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  1100. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  1101. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  1102. m_activeBefore = m_active;
  1103. }
  1104. // -------------------------------------------------------------------
  1105. // Post-poned events
  1106. #ifndef BUILD_BRIDGE
  1107. void uiParameterChange(const uint32_t index, const double value)
  1108. {
  1109. Q_ASSERT(index < param.count);
  1110. if (index >= param.count)
  1111. return;
  1112. if (! osc.data.target)
  1113. return;
  1114. osc_send_control(&osc.data, param.data[index].rindex, value);
  1115. }
  1116. void uiMidiProgramChange(const uint32_t index)
  1117. {
  1118. Q_ASSERT(index < midiprog.count);
  1119. if (index >= midiprog.count)
  1120. return;
  1121. if (! osc.data.target)
  1122. return;
  1123. osc_send_program(&osc.data, midiprog.data[index].bank, midiprog.data[index].program);
  1124. }
  1125. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1126. {
  1127. Q_ASSERT(channel < 16);
  1128. Q_ASSERT(note < 128);
  1129. Q_ASSERT(velo > 0 && velo < 128);
  1130. if (! osc.data.target)
  1131. return;
  1132. uint8_t midiData[4] = { 0 };
  1133. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1134. midiData[2] = note;
  1135. midiData[3] = velo;
  1136. osc_send_midi(&osc.data, midiData);
  1137. }
  1138. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1139. {
  1140. Q_ASSERT(channel < 16);
  1141. Q_ASSERT(note < 128);
  1142. if (! osc.data.target)
  1143. return;
  1144. uint8_t midiData[4] = { 0 };
  1145. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1146. midiData[2] = note;
  1147. osc_send_midi(&osc.data, midiData);
  1148. }
  1149. #endif
  1150. // -------------------------------------------------------------------
  1151. // Cleanup
  1152. void deleteBuffers()
  1153. {
  1154. qDebug("DssiPlugin::deleteBuffers() - start");
  1155. if (param.count > 0)
  1156. delete[] paramBuffers;
  1157. paramBuffers = nullptr;
  1158. qDebug("DssiPlugin::deleteBuffers() - end");
  1159. }
  1160. // -------------------------------------------------------------------
  1161. bool init(const char* const filename, const char* const name, const char* const label, const char* const guiFilename)
  1162. {
  1163. // ---------------------------------------------------------------
  1164. // open DLL
  1165. if (! libOpen(filename))
  1166. {
  1167. setLastError(libError(filename));
  1168. return false;
  1169. }
  1170. // ---------------------------------------------------------------
  1171. // get DLL main entry
  1172. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)libSymbol("dssi_descriptor");
  1173. if (! descFn)
  1174. {
  1175. setLastError("Could not find the DSSI Descriptor in the plugin library");
  1176. return false;
  1177. }
  1178. // ---------------------------------------------------------------
  1179. // get descriptor that matches label
  1180. unsigned long i = 0;
  1181. while ((descriptor = descFn(i++)))
  1182. {
  1183. ldescriptor = descriptor->LADSPA_Plugin;
  1184. if (ldescriptor && strcmp(ldescriptor->Label, label) == 0)
  1185. break;
  1186. }
  1187. if (! descriptor)
  1188. {
  1189. setLastError("Could not find the requested plugin Label in the plugin library");
  1190. return false;
  1191. }
  1192. // ---------------------------------------------------------------
  1193. // initialize plugin
  1194. handle = ldescriptor->instantiate(ldescriptor, x_engine->getSampleRate());
  1195. if (! handle)
  1196. {
  1197. setLastError("Plugin failed to initialize");
  1198. return false;
  1199. }
  1200. // ---------------------------------------------------------------
  1201. // get info
  1202. m_filename = strdup(filename);
  1203. if (name)
  1204. m_name = x_engine->getUniqueName(name);
  1205. else
  1206. m_name = x_engine->getUniqueName(ldescriptor->Name);
  1207. // ---------------------------------------------------------------
  1208. // register client
  1209. x_client = x_engine->addClient(this);
  1210. if (! x_client->isOk())
  1211. {
  1212. setLastError("Failed to register plugin client");
  1213. return false;
  1214. }
  1215. // ---------------------------------------------------------------
  1216. // gui stuff
  1217. #ifndef BUILD_BRIDGE
  1218. if (guiFilename)
  1219. {
  1220. osc.thread = new CarlaPluginThread(x_engine, this, CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  1221. osc.thread->setOscData(guiFilename, ldescriptor->Label);
  1222. m_hints |= PLUGIN_HAS_GUI;
  1223. }
  1224. #else
  1225. Q_UNUSED(guiFilename);
  1226. #endif
  1227. return true;
  1228. }
  1229. private:
  1230. LADSPA_Handle handle, h2;
  1231. const LADSPA_Descriptor* ldescriptor;
  1232. const DSSI_Descriptor* descriptor;
  1233. snd_seq_event_t midiEvents[MAX_MIDI_EVENTS];
  1234. float* paramBuffers;
  1235. };
  1236. /**@}*/
  1237. CARLA_BACKEND_END_NAMESPACE
  1238. #else // WANT_DSSI
  1239. # warning Building without DSSI support
  1240. #endif
  1241. CARLA_BACKEND_START_NAMESPACE
  1242. CarlaPlugin* CarlaPlugin::newDSSI(const initializer& init, const void* const extra)
  1243. {
  1244. qDebug("CarlaPlugin::newDSSI(%p, \"%s\", \"%s\", \"%s\", %p)", init.engine, init.filename, init.name, init.label, extra);
  1245. #ifdef WANT_DSSI
  1246. short id = init.engine->getNewPluginId();
  1247. if (id < 0 || id > CarlaEngine::maxPluginNumber())
  1248. {
  1249. setLastError("Maximum number of plugins reached");
  1250. return nullptr;
  1251. }
  1252. DssiPlugin* const plugin = new DssiPlugin(init.engine, id);
  1253. if (! plugin->init(init.filename, init.name, init.label, (const char*)extra))
  1254. {
  1255. delete plugin;
  1256. return nullptr;
  1257. }
  1258. plugin->reload();
  1259. #ifndef BUILD_BRIDGE
  1260. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1261. {
  1262. const uint32_t ins = plugin->audioInCount();
  1263. const uint32_t outs = plugin->audioOutCount();
  1264. if (ins > 2 || outs > 2 || (ins != outs && ins != 0 && outs != 0))
  1265. {
  1266. setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1267. delete plugin;
  1268. return nullptr;
  1269. }
  1270. }
  1271. #endif
  1272. plugin->registerToOsc();
  1273. return plugin;
  1274. #else
  1275. setLastError("DSSI support not available");
  1276. return nullptr;
  1277. #endif
  1278. }
  1279. CARLA_BACKEND_END_NAMESPACE