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.

1557 lines
51KB

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