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.

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