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.

1583 lines
52KB

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