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.

1627 lines
53KB

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