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.

1672 lines
54KB

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