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.

1552 lines
50KB

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