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.

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