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.

1669 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 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. bool needsCtrlIn = false;
  333. bool needsCtrlOut = false;
  334. const int portNameSize = x_engine->maxPortNameSize();
  335. CarlaString portName;
  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. portName.clear();
  343. #ifndef BUILD_BRIDGE
  344. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  345. {
  346. portName = m_name;
  347. portName += ":";
  348. }
  349. #endif
  350. portName += ldescriptor->PortNames[i];
  351. portName.truncate(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. portName += "_2";
  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. portName += "_2";
  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. portName.clear();
  520. #ifndef BUILD_BRIDGE
  521. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  522. {
  523. portName = m_name;
  524. portName += ":";
  525. }
  526. #endif
  527. portName += "control-in";
  528. portName.truncate(portNameSize);
  529. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  530. }
  531. if (needsCtrlOut)
  532. {
  533. portName.clear();
  534. #ifndef BUILD_BRIDGE
  535. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  536. {
  537. portName = m_name;
  538. portName += ":";
  539. }
  540. #endif
  541. portName += "control-out";
  542. portName.truncate(portNameSize);
  543. param.portCout = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, false);
  544. }
  545. if (mIns == 1)
  546. {
  547. portName.clear();
  548. #ifndef BUILD_BRIDGE
  549. if (x_engine->processMode() == PROCESS_MODE_SINGLE_CLIENT)
  550. {
  551. portName = m_name;
  552. portName += ":";
  553. }
  554. #endif
  555. portName += "midi-in";
  556. portName.truncate(portNameSize);
  557. midi.portMin = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  558. }
  559. aIn.count = aIns;
  560. aOut.count = aOuts;
  561. param.count = params;
  562. // plugin checks
  563. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  564. if (midi.portMin && aOut.count > 0)
  565. m_hints |= PLUGIN_IS_SYNTH;
  566. #ifndef BUILD_BRIDGE
  567. if (x_engine->useDssiVstChunks() && QString(m_filename).endsWith("dssi-vst.so", Qt::CaseInsensitive))
  568. {
  569. if (descriptor->get_custom_data && descriptor->set_custom_data)
  570. m_hints |= PLUGIN_USES_CHUNKS;
  571. }
  572. #endif
  573. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  574. m_hints |= PLUGIN_CAN_DRYWET;
  575. if (aOuts > 0)
  576. m_hints |= PLUGIN_CAN_VOLUME;
  577. if (aOuts >= 2 && aOuts%2 == 0)
  578. m_hints |= PLUGIN_CAN_BALANCE;
  579. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  580. m_hints |= PLUGIN_CAN_FORCE_STEREO;
  581. // check latency
  582. if (m_hints & PLUGIN_CAN_DRYWET)
  583. {
  584. bool hasLatency = false;
  585. m_latency = 0;
  586. for (uint32_t i=0; i < param.count; i++)
  587. {
  588. if (param.data[i].type == PARAMETER_LATENCY)
  589. {
  590. // pre-run so plugin can update latency control-port
  591. float tmpIn[2][aIns];
  592. float tmpOut[2][aOuts];
  593. for (j=0; j < aIn.count; j++)
  594. {
  595. tmpIn[j][0] = 0.0f;
  596. tmpIn[j][1] = 0.0f;
  597. if (j == 0 || ! h2)
  598. ldescriptor->connect_port(handle, aIn.rindexes[j], tmpIn[j]);
  599. }
  600. for (j=0; j < aOut.count; j++)
  601. {
  602. tmpOut[j][0] = 0.0f;
  603. tmpOut[j][1] = 0.0f;
  604. if (j == 0 || ! h2)
  605. ldescriptor->connect_port(handle, aOut.rindexes[j], tmpOut[j]);
  606. }
  607. if (ldescriptor->activate)
  608. ldescriptor->activate(handle);
  609. ldescriptor->run(handle, 2);
  610. if (ldescriptor->deactivate)
  611. ldescriptor->deactivate(handle);
  612. m_latency = rint(paramBuffers[i]);
  613. hasLatency = true;
  614. break;
  615. }
  616. }
  617. if (hasLatency)
  618. {
  619. x_client->setLatency(m_latency);
  620. recreateLatencyBuffers();
  621. }
  622. }
  623. reloadPrograms(true);
  624. x_client->activate();
  625. qDebug("DssiPlugin::reload() - end");
  626. }
  627. void reloadPrograms(const bool init)
  628. {
  629. qDebug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  630. uint32_t i, oldCount = midiprog.count;
  631. // Delete old programs
  632. if (midiprog.count > 0)
  633. {
  634. for (i=0; i < midiprog.count; i++)
  635. {
  636. if (midiprog.data[i].name)
  637. free((void*)midiprog.data[i].name);
  638. }
  639. delete[] midiprog.data;
  640. }
  641. midiprog.count = 0;
  642. midiprog.data = nullptr;
  643. // Query new programs
  644. if (descriptor->get_program && descriptor->select_program)
  645. {
  646. while (descriptor->get_program(handle, midiprog.count))
  647. midiprog.count += 1;
  648. }
  649. if (midiprog.count > 0)
  650. midiprog.data = new MidiProgramData[midiprog.count];
  651. // Update data
  652. for (i=0; i < midiprog.count; i++)
  653. {
  654. const DSSI_Program_Descriptor* const pdesc = descriptor->get_program(handle, i);
  655. CARLA_ASSERT(pdesc);
  656. CARLA_ASSERT(pdesc->Program < 128);
  657. CARLA_ASSERT(pdesc->Name);
  658. midiprog.data[i].bank = pdesc->Bank;
  659. midiprog.data[i].program = pdesc->Program;
  660. midiprog.data[i].name = strdup(pdesc->Name);
  661. }
  662. #ifndef BUILD_BRIDGE
  663. // Update OSC Names
  664. if (x_engine->isOscControlRegisted())
  665. {
  666. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  667. for (i=0; i < midiprog.count; i++)
  668. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  669. }
  670. #endif
  671. if (init)
  672. {
  673. if (midiprog.count > 0)
  674. setMidiProgram(0, false, false, false, true);
  675. }
  676. else
  677. {
  678. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  679. // Check if current program is invalid
  680. bool programChanged = false;
  681. if (midiprog.count == oldCount+1)
  682. {
  683. // one midi program added, probably created by user
  684. midiprog.current = oldCount;
  685. programChanged = true;
  686. }
  687. else if (midiprog.current >= (int32_t)midiprog.count)
  688. {
  689. // current midi program > count
  690. midiprog.current = 0;
  691. programChanged = true;
  692. }
  693. else if (midiprog.current < 0 && midiprog.count > 0)
  694. {
  695. // programs exist now, but not before
  696. midiprog.current = 0;
  697. programChanged = true;
  698. }
  699. else if (midiprog.current >= 0 && midiprog.count == 0)
  700. {
  701. // programs existed before, but not anymore
  702. midiprog.current = -1;
  703. programChanged = true;
  704. }
  705. if (programChanged)
  706. setMidiProgram(midiprog.current, true, true, true, true);
  707. }
  708. }
  709. // -------------------------------------------------------------------
  710. // Plugin processing
  711. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  712. {
  713. uint32_t i, k;
  714. unsigned long midiEventCount = 0;
  715. double aInsPeak[2] = { 0.0 };
  716. double aOutsPeak[2] = { 0.0 };
  717. CARLA_PROCESS_CONTINUE_CHECK;
  718. // --------------------------------------------------------------------------------------------------------
  719. // Input VU
  720. #ifndef BUILD_BRIDGE
  721. if (aIn.count > 0 && x_engine->processMode() != PROCESS_MODE_CONTINUOUS_RACK)
  722. #else
  723. if (aIn.count > 0)
  724. #endif
  725. {
  726. if (aIn.count == 1)
  727. {
  728. for (k=0; k < frames; k++)
  729. {
  730. if (abs(inBuffer[0][k]) > aInsPeak[0])
  731. aInsPeak[0] = abs(inBuffer[0][k]);
  732. }
  733. }
  734. else if (aIn.count > 1)
  735. {
  736. for (k=0; k < frames; k++)
  737. {
  738. if (abs(inBuffer[0][k]) > aInsPeak[0])
  739. aInsPeak[0] = abs(inBuffer[0][k]);
  740. if (abs(inBuffer[1][k]) > aInsPeak[1])
  741. aInsPeak[1] = abs(inBuffer[1][k]);
  742. }
  743. }
  744. }
  745. CARLA_PROCESS_CONTINUE_CHECK;
  746. // --------------------------------------------------------------------------------------------------------
  747. // Parameters Input [Automation]
  748. if (param.portCin && m_active && m_activeBefore)
  749. {
  750. bool allNotesOffSent = false;
  751. const CarlaEngineControlEvent* cinEvent;
  752. uint32_t time, nEvents = param.portCin->getEventCount();
  753. uint32_t nextBankId = 0;
  754. if (midiprog.current >= 0 && midiprog.count > 0)
  755. nextBankId = midiprog.data[midiprog.current].bank;
  756. for (i=0; i < nEvents; i++)
  757. {
  758. cinEvent = param.portCin->getEvent(i);
  759. if (! cinEvent)
  760. continue;
  761. time = cinEvent->time - framesOffset;
  762. if (time >= frames)
  763. continue;
  764. // Control change
  765. switch (cinEvent->type)
  766. {
  767. case CarlaEngineNullEvent:
  768. break;
  769. case CarlaEngineParameterChangeEvent:
  770. {
  771. double value;
  772. // Control backend stuff
  773. if (cinEvent->channel == m_ctrlInChannel)
  774. {
  775. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->parameter) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  776. {
  777. value = cinEvent->value;
  778. setDryWet(value, false, false);
  779. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  780. continue;
  781. }
  782. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->parameter) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  783. {
  784. value = cinEvent->value*127/100;
  785. setVolume(value, false, false);
  786. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  787. continue;
  788. }
  789. if (MIDI_IS_CONTROL_BALANCE(cinEvent->parameter) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  790. {
  791. double left, right;
  792. value = cinEvent->value/0.5 - 1.0;
  793. if (value < 0.0)
  794. {
  795. left = -1.0;
  796. right = (value*2)+1.0;
  797. }
  798. else if (value > 0.0)
  799. {
  800. left = (value*2)-1.0;
  801. right = 1.0;
  802. }
  803. else
  804. {
  805. left = -1.0;
  806. right = 1.0;
  807. }
  808. setBalanceLeft(left, false, false);
  809. setBalanceRight(right, false, false);
  810. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  811. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  812. continue;
  813. }
  814. }
  815. // Control plugin parameters
  816. for (k=0; k < param.count; k++)
  817. {
  818. if (param.data[k].midiChannel != cinEvent->channel)
  819. continue;
  820. if (param.data[k].midiCC != cinEvent->parameter)
  821. continue;
  822. if (param.data[k].type != PARAMETER_INPUT)
  823. continue;
  824. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  825. {
  826. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  827. {
  828. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  829. }
  830. else
  831. {
  832. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  833. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  834. value = rint(value);
  835. }
  836. setParameterValue(k, value, false, false, false);
  837. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  838. }
  839. }
  840. break;
  841. }
  842. case CarlaEngineMidiBankChangeEvent:
  843. if (cinEvent->channel == m_ctrlInChannel)
  844. nextBankId = rint(cinEvent->value);
  845. break;
  846. case CarlaEngineMidiProgramChangeEvent:
  847. if (cinEvent->channel == m_ctrlInChannel)
  848. {
  849. uint32_t nextProgramId = rint(cinEvent->value);
  850. for (k=0; k < midiprog.count; k++)
  851. {
  852. if (midiprog.data[k].bank == nextBankId && midiprog.data[k].program == nextProgramId)
  853. {
  854. setMidiProgram(k, false, false, false, false);
  855. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  856. break;
  857. }
  858. }
  859. }
  860. break;
  861. case CarlaEngineAllSoundOffEvent:
  862. if (cinEvent->channel == m_ctrlInChannel)
  863. {
  864. if (midi.portMin && ! allNotesOffSent)
  865. sendMidiAllNotesOff();
  866. if (ldescriptor->deactivate)
  867. {
  868. ldescriptor->deactivate(handle);
  869. if (h2) ldescriptor->deactivate(h2);
  870. }
  871. if (ldescriptor->activate)
  872. {
  873. ldescriptor->activate(handle);
  874. if (h2) ldescriptor->activate(h2);
  875. }
  876. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  877. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  878. allNotesOffSent = true;
  879. }
  880. break;
  881. case CarlaEngineAllNotesOffEvent:
  882. if (cinEvent->channel == m_ctrlInChannel)
  883. {
  884. if (midi.portMin && ! allNotesOffSent)
  885. sendMidiAllNotesOff();
  886. allNotesOffSent = true;
  887. }
  888. break;
  889. }
  890. }
  891. } // End of Parameters Input
  892. CARLA_PROCESS_CONTINUE_CHECK;
  893. // --------------------------------------------------------------------------------------------------------
  894. // MIDI Input
  895. if (midi.portMin && m_active && m_activeBefore)
  896. {
  897. // ----------------------------------------------------------------------------------------------------
  898. // MIDI Input (External)
  899. {
  900. engineMidiLock();
  901. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  902. {
  903. if (extMidiNotes[i].channel < 0)
  904. break;
  905. snd_seq_event_t* const midiEvent = &midiEvents[midiEventCount];
  906. memset(midiEvent, 0, sizeof(snd_seq_event_t));
  907. midiEvent->type = extMidiNotes[i].velo ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  908. midiEvent->data.note.channel = extMidiNotes[i].channel;
  909. midiEvent->data.note.note = extMidiNotes[i].note;
  910. midiEvent->data.note.velocity = extMidiNotes[i].velo;
  911. extMidiNotes[i].channel = -1; // mark as invalid
  912. midiEventCount += 1;
  913. }
  914. engineMidiUnlock();
  915. } // End of MIDI Input (External)
  916. CARLA_PROCESS_CONTINUE_CHECK;
  917. // ----------------------------------------------------------------------------------------------------
  918. // MIDI Input (System)
  919. {
  920. const CarlaEngineMidiEvent* minEvent;
  921. uint32_t time, nEvents = midi.portMin->getEventCount();
  922. for (i=0; i < nEvents && midiEventCount < MAX_MIDI_EVENTS; i++)
  923. {
  924. minEvent = midi.portMin->getEvent(i);
  925. if (! minEvent)
  926. continue;
  927. time = minEvent->time - framesOffset;
  928. if (time >= frames)
  929. continue;
  930. uint8_t status = minEvent->data[0];
  931. uint8_t channel = status & 0x0F;
  932. // Fix bad note-off
  933. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  934. status -= 0x10;
  935. snd_seq_event_t* const midiEvent = &midiEvents[midiEventCount];
  936. memset(midiEvent, 0, sizeof(snd_seq_event_t));
  937. midiEvent->time.tick = time;
  938. if (MIDI_IS_STATUS_NOTE_OFF(status))
  939. {
  940. uint8_t note = minEvent->data[1];
  941. midiEvent->type = SND_SEQ_EVENT_NOTEOFF;
  942. midiEvent->data.note.channel = channel;
  943. midiEvent->data.note.note = note;
  944. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  945. }
  946. else if (MIDI_IS_STATUS_NOTE_ON(status))
  947. {
  948. uint8_t note = minEvent->data[1];
  949. uint8_t velo = minEvent->data[2];
  950. midiEvent->type = SND_SEQ_EVENT_NOTEON;
  951. midiEvent->data.note.channel = channel;
  952. midiEvent->data.note.note = note;
  953. midiEvent->data.note.velocity = velo;
  954. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  955. }
  956. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  957. {
  958. uint8_t note = minEvent->data[1];
  959. uint8_t pressure = minEvent->data[2];
  960. midiEvent->type = SND_SEQ_EVENT_KEYPRESS;
  961. midiEvent->data.note.channel = channel;
  962. midiEvent->data.note.note = note;
  963. midiEvent->data.note.velocity = pressure;
  964. }
  965. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  966. {
  967. uint8_t pressure = minEvent->data[1];
  968. midiEvent->type = SND_SEQ_EVENT_CHANPRESS;
  969. midiEvent->data.control.channel = channel;
  970. midiEvent->data.control.value = pressure;
  971. }
  972. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  973. {
  974. uint8_t lsb = minEvent->data[1];
  975. uint8_t msb = minEvent->data[2];
  976. midiEvent->type = SND_SEQ_EVENT_PITCHBEND;
  977. midiEvent->data.control.channel = channel;
  978. midiEvent->data.control.value = ((msb << 7) | lsb) - 8192;
  979. }
  980. else
  981. continue;
  982. midiEventCount += 1;
  983. }
  984. } // End of MIDI Input (System)
  985. } // End of MIDI Input
  986. CARLA_PROCESS_CONTINUE_CHECK;
  987. // --------------------------------------------------------------------------------------------------------
  988. // Special Parameters
  989. #if 0
  990. for (k=0; k < param.count; k++)
  991. {
  992. if (param.data[k].type == PARAMETER_LATENCY)
  993. {
  994. // TODO
  995. }
  996. }
  997. CARLA_PROCESS_CONTINUE_CHECK;
  998. #endif
  999. // --------------------------------------------------------------------------------------------------------
  1000. // Plugin processing
  1001. if (m_active)
  1002. {
  1003. if (! m_activeBefore)
  1004. {
  1005. if (midi.portMin)
  1006. {
  1007. for (k=0; k < MAX_MIDI_CHANNELS; k++)
  1008. {
  1009. memset(&midiEvents[k], 0, sizeof(snd_seq_event_t));
  1010. midiEvents[k].type = SND_SEQ_EVENT_CONTROLLER;
  1011. midiEvents[k].data.control.channel = k;
  1012. midiEvents[k].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1013. memset(&midiEvents[k*2], 0, sizeof(snd_seq_event_t));
  1014. midiEvents[k*2].type = SND_SEQ_EVENT_CONTROLLER;
  1015. midiEvents[k*2].data.control.channel = k;
  1016. midiEvents[k*2].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1017. }
  1018. midiEventCount = MAX_MIDI_CHANNELS;
  1019. }
  1020. if (m_latency > 0)
  1021. {
  1022. for (i=0; i < aIn.count; i++)
  1023. memset(m_latencyBuffers[i], 0, sizeof(float)*m_latency);
  1024. }
  1025. if (ldescriptor->activate)
  1026. {
  1027. ldescriptor->activate(handle);
  1028. if (h2) ldescriptor->activate(h2);
  1029. }
  1030. }
  1031. for (i=0; i < aIn.count; i++)
  1032. {
  1033. if (i == 0 || ! h2) ldescriptor->connect_port(handle, aIn.rindexes[i], inBuffer[i]);
  1034. else if (i == 1) ldescriptor->connect_port(h2, aIn.rindexes[i], inBuffer[i]);
  1035. }
  1036. for (i=0; i < aOut.count; i++)
  1037. {
  1038. if (i == 0 || ! h2) ldescriptor->connect_port(handle, aOut.rindexes[i], outBuffer[i]);
  1039. else if (i == 1) ldescriptor->connect_port(h2, aOut.rindexes[i], outBuffer[i]);
  1040. }
  1041. if (descriptor->run_synth)
  1042. {
  1043. descriptor->run_synth(handle, frames, midiEvents, midiEventCount);
  1044. if (h2) descriptor->run_synth(h2, frames, midiEvents, midiEventCount);
  1045. }
  1046. else if (descriptor->run_multiple_synths)
  1047. {
  1048. LADSPA_Handle handlePtr[2] = { handle, h2 };
  1049. snd_seq_event_t* midiEventsPtr[2] = { midiEvents, midiEvents };
  1050. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1051. descriptor->run_multiple_synths(h2 ? 2 : 1, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1052. }
  1053. else
  1054. {
  1055. ldescriptor->run(handle, frames);
  1056. if (h2) ldescriptor->run(h2, frames);
  1057. }
  1058. }
  1059. else
  1060. {
  1061. if (m_activeBefore)
  1062. {
  1063. if (ldescriptor->deactivate)
  1064. {
  1065. ldescriptor->deactivate(handle);
  1066. if (h2) ldescriptor->deactivate(h2);
  1067. }
  1068. }
  1069. }
  1070. CARLA_PROCESS_CONTINUE_CHECK;
  1071. // --------------------------------------------------------------------------------------------------------
  1072. // Post-processing (dry/wet, volume and balance)
  1073. if (m_active)
  1074. {
  1075. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  1076. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  1077. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  1078. double bal_rangeL, bal_rangeR;
  1079. float bufValue, oldBufLeft[do_balance ? frames : 0];
  1080. for (i=0; i < aOut.count; i++)
  1081. {
  1082. // Dry/Wet
  1083. if (do_drywet)
  1084. {
  1085. for (k=0; k < frames; k++)
  1086. {
  1087. if (k < m_latency && m_latency < frames)
  1088. bufValue = (aIn.count == 1) ? m_latencyBuffers[0][k] : m_latencyBuffers[i][k];
  1089. else
  1090. bufValue = (aIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1091. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(bufValue*(1.0-x_dryWet));
  1092. }
  1093. }
  1094. // Balance
  1095. if (do_balance)
  1096. {
  1097. if (i%2 == 0)
  1098. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1099. bal_rangeL = (x_balanceLeft+1.0)/2;
  1100. bal_rangeR = (x_balanceRight+1.0)/2;
  1101. for (k=0; k < frames; k++)
  1102. {
  1103. if (i%2 == 0)
  1104. {
  1105. // left output
  1106. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  1107. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  1108. }
  1109. else
  1110. {
  1111. // right
  1112. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  1113. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  1114. }
  1115. }
  1116. }
  1117. // Volume
  1118. if (do_volume)
  1119. {
  1120. for (k=0; k < frames; k++)
  1121. outBuffer[i][k] *= x_volume;
  1122. }
  1123. // Output VU
  1124. #ifndef BUILD_BRIDGE
  1125. if (x_engine->processMode() != PROCESS_MODE_CONTINUOUS_RACK)
  1126. #endif
  1127. {
  1128. for (k=0; i < 2 && k < frames; k++)
  1129. {
  1130. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  1131. aOutsPeak[i] = abs(outBuffer[i][k]);
  1132. }
  1133. }
  1134. }
  1135. // Latency, save values for next callback
  1136. if (m_latency > 0 && m_latency < frames)
  1137. {
  1138. for (i=0; i < aIn.count; i++)
  1139. memcpy(m_latencyBuffers[i], inBuffer[i] + (frames - m_latency), sizeof(float)*m_latency);
  1140. }
  1141. }
  1142. else
  1143. {
  1144. // disable any output sound if not active
  1145. for (i=0; i < aOut.count; i++)
  1146. carla_zeroF(outBuffer[i], frames);
  1147. aOutsPeak[0] = 0.0;
  1148. aOutsPeak[1] = 0.0;
  1149. } // End of Post-processing
  1150. CARLA_PROCESS_CONTINUE_CHECK;
  1151. // --------------------------------------------------------------------------------------------------------
  1152. // Control Output
  1153. if (param.portCout && m_active)
  1154. {
  1155. double value;
  1156. for (k=0; k < param.count; k++)
  1157. {
  1158. if (param.data[k].type == PARAMETER_OUTPUT)
  1159. {
  1160. fixParameterValue(paramBuffers[k], param.ranges[k]);
  1161. if (param.data[k].midiCC > 0)
  1162. {
  1163. value = (paramBuffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  1164. param.portCout->writeEvent(CarlaEngineParameterChangeEvent, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, value);
  1165. }
  1166. }
  1167. }
  1168. } // End of Control Output
  1169. CARLA_PROCESS_CONTINUE_CHECK;
  1170. // --------------------------------------------------------------------------------------------------------
  1171. // Peak Values
  1172. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  1173. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  1174. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  1175. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  1176. m_activeBefore = m_active;
  1177. }
  1178. // -------------------------------------------------------------------
  1179. // Post-poned events
  1180. #ifndef BUILD_BRIDGE
  1181. void uiParameterChange(const uint32_t index, const double value)
  1182. {
  1183. CARLA_ASSERT(index < param.count);
  1184. if (index >= param.count)
  1185. return;
  1186. if (! osc.data.target)
  1187. return;
  1188. osc_send_control(&osc.data, param.data[index].rindex, value);
  1189. }
  1190. void uiMidiProgramChange(const uint32_t index)
  1191. {
  1192. CARLA_ASSERT(index < midiprog.count);
  1193. if (index >= midiprog.count)
  1194. return;
  1195. if (! osc.data.target)
  1196. return;
  1197. osc_send_program(&osc.data, midiprog.data[index].bank, midiprog.data[index].program);
  1198. }
  1199. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1200. {
  1201. CARLA_ASSERT(channel < 16);
  1202. CARLA_ASSERT(note < 128);
  1203. CARLA_ASSERT(velo > 0 && velo < 128);
  1204. if (! osc.data.target)
  1205. return;
  1206. uint8_t midiData[4] = { 0 };
  1207. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1208. midiData[2] = note;
  1209. midiData[3] = velo;
  1210. osc_send_midi(&osc.data, midiData);
  1211. }
  1212. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1213. {
  1214. CARLA_ASSERT(channel < 16);
  1215. CARLA_ASSERT(note < 128);
  1216. if (! osc.data.target)
  1217. return;
  1218. uint8_t midiData[4] = { 0 };
  1219. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1220. midiData[2] = note;
  1221. osc_send_midi(&osc.data, midiData);
  1222. }
  1223. #endif
  1224. // -------------------------------------------------------------------
  1225. // Cleanup
  1226. void deleteBuffers()
  1227. {
  1228. qDebug("DssiPlugin::deleteBuffers() - start");
  1229. if (param.count > 0)
  1230. delete[] paramBuffers;
  1231. paramBuffers = nullptr;
  1232. CarlaPlugin::deleteBuffers();
  1233. qDebug("DssiPlugin::deleteBuffers() - end");
  1234. }
  1235. // -------------------------------------------------------------------
  1236. bool init(const char* const filename, const char* const name, const char* const label, const char* const guiFilename)
  1237. {
  1238. // ---------------------------------------------------------------
  1239. // open DLL
  1240. if (! libOpen(filename))
  1241. {
  1242. x_engine->setLastError(libError(filename));
  1243. return false;
  1244. }
  1245. // ---------------------------------------------------------------
  1246. // get DLL main entry
  1247. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)libSymbol("dssi_descriptor");
  1248. if (! descFn)
  1249. {
  1250. x_engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1251. return false;
  1252. }
  1253. // ---------------------------------------------------------------
  1254. // get descriptor that matches label
  1255. unsigned long i = 0;
  1256. while ((descriptor = descFn(i++)))
  1257. {
  1258. ldescriptor = descriptor->LADSPA_Plugin;
  1259. if (ldescriptor && strcmp(ldescriptor->Label, label) == 0)
  1260. break;
  1261. }
  1262. if (! descriptor)
  1263. {
  1264. x_engine->setLastError("Could not find the requested plugin Label in the plugin library");
  1265. return false;
  1266. }
  1267. // ---------------------------------------------------------------
  1268. // get info
  1269. m_filename = strdup(filename);
  1270. if (name)
  1271. m_name = x_engine->getUniquePluginName(name);
  1272. else
  1273. m_name = x_engine->getUniquePluginName(ldescriptor->Name);
  1274. // ---------------------------------------------------------------
  1275. // register client
  1276. x_client = x_engine->addClient(this);
  1277. if (! x_client->isOk())
  1278. {
  1279. x_engine->setLastError("Failed to register plugin client");
  1280. return false;
  1281. }
  1282. // ---------------------------------------------------------------
  1283. // initialize plugin
  1284. handle = ldescriptor->instantiate(ldescriptor, x_engine->getSampleRate());
  1285. if (! handle)
  1286. {
  1287. x_engine->setLastError("Plugin failed to initialize");
  1288. return false;
  1289. }
  1290. // ---------------------------------------------------------------
  1291. // gui stuff
  1292. #ifndef BUILD_BRIDGE
  1293. if (guiFilename)
  1294. {
  1295. osc.thread = new CarlaPluginThread(x_engine, this, CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  1296. osc.thread->setOscData(guiFilename, ldescriptor->Label);
  1297. m_hints |= PLUGIN_HAS_GUI;
  1298. }
  1299. #else
  1300. Q_UNUSED(guiFilename);
  1301. #endif
  1302. return true;
  1303. }
  1304. private:
  1305. LADSPA_Handle handle, h2;
  1306. const LADSPA_Descriptor* ldescriptor;
  1307. const DSSI_Descriptor* descriptor;
  1308. snd_seq_event_t midiEvents[MAX_MIDI_EVENTS];
  1309. float* paramBuffers;
  1310. };
  1311. /**@}*/
  1312. CARLA_BACKEND_END_NAMESPACE
  1313. #else // WANT_DSSI
  1314. # warning Building without DSSI support
  1315. #endif
  1316. CARLA_BACKEND_START_NAMESPACE
  1317. CarlaPlugin* CarlaPlugin::newDSSI(const initializer& init, const void* const extra)
  1318. {
  1319. qDebug("CarlaPlugin::newDSSI(%p, \"%s\", \"%s\", \"%s\", %p)", init.engine, init.filename, init.name, init.label, extra);
  1320. #ifdef WANT_DSSI
  1321. short id = init.engine->getNewPluginId();
  1322. if (id < 0 || id > init.engine->maxPluginNumber())
  1323. {
  1324. init.engine->setLastError("Maximum number of plugins reached");
  1325. return nullptr;
  1326. }
  1327. DssiPlugin* const plugin = new DssiPlugin(init.engine, id);
  1328. if (! plugin->init(init.filename, init.name, init.label, (const char*)extra))
  1329. {
  1330. delete plugin;
  1331. return nullptr;
  1332. }
  1333. plugin->reload();
  1334. # ifndef BUILD_BRIDGE
  1335. if (init.engine->processMode() == PROCESS_MODE_CONTINUOUS_RACK)
  1336. {
  1337. if (! (plugin->hints() & PLUGIN_CAN_FORCE_STEREO))
  1338. {
  1339. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1340. delete plugin;
  1341. return nullptr;
  1342. }
  1343. }
  1344. # endif
  1345. plugin->registerToOscControl();
  1346. return plugin;
  1347. #else
  1348. init.engine->setLastError("DSSI support not available");
  1349. return nullptr;
  1350. #endif
  1351. }
  1352. CARLA_BACKEND_END_NAMESPACE