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.

1647 lines
54KB

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