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.

1559 lines
51KB

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