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