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.

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