Audio plugin host https://kx.studio/carla
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.

1942 lines
68KB

  1. /*
  2. * Carla DSSI Plugin
  3. * Copyright (C) 2011-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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 GPL.txt file
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #ifdef WANT_DSSI
  19. #include "CarlaLadspaUtils.hpp"
  20. #include "dssi/dssi.h"
  21. CARLA_BACKEND_START_NAMESPACE
  22. #if 0
  23. }
  24. #endif
  25. class DssiPlugin : public CarlaPlugin
  26. {
  27. public:
  28. DssiPlugin(CarlaEngine* const engine, const unsigned int id)
  29. : CarlaPlugin(engine, id),
  30. fHandle(nullptr),
  31. fHandle2(nullptr),
  32. fDescriptor(nullptr),
  33. fDssiDescriptor(nullptr),
  34. fAudioInBuffers(nullptr),
  35. fAudioOutBuffers(nullptr),
  36. fParamBuffers(nullptr)
  37. {
  38. carla_debug("DssiPlugin::DssiPlugin(%p, %i)", engine, id);
  39. carla_zeroStruct<snd_seq_event_t>(fMidiEvents, MAX_MIDI_EVENTS);
  40. kData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  41. }
  42. ~DssiPlugin()
  43. {
  44. carla_debug("DssiPlugin::~DssiPlugin()");
  45. // close UI
  46. if (fHints & PLUGIN_HAS_GUI)
  47. {
  48. showGui(false);
  49. // Wait a bit first, then force kill
  50. if (kData->osc.thread.isRunning() && ! kData->osc.thread.wait(kData->engine->getOptions().oscUiTimeout))
  51. {
  52. carla_stderr("DSSI GUI thread still running, forcing termination now");
  53. kData->osc.thread.terminate();
  54. }
  55. }
  56. kData->singleMutex.lock();
  57. kData->masterMutex.lock();
  58. if (kData->active)
  59. {
  60. deactivate();
  61. kData->active = false;
  62. }
  63. if (fDescriptor != nullptr)
  64. {
  65. if (fDescriptor->cleanup != nullptr)
  66. {
  67. if (fHandle != nullptr)
  68. fDescriptor->cleanup(fHandle);
  69. if (fHandle2 != nullptr)
  70. fDescriptor->cleanup(fHandle2);
  71. }
  72. fHandle = nullptr;
  73. fHandle2 = nullptr;
  74. fDescriptor = nullptr;
  75. fDssiDescriptor = nullptr;
  76. }
  77. clearBuffers();
  78. }
  79. // -------------------------------------------------------------------
  80. // Information (base)
  81. PluginType type() const
  82. {
  83. return PLUGIN_DSSI;
  84. }
  85. PluginCategory category()
  86. {
  87. if (fHints & PLUGIN_IS_SYNTH)
  88. return PLUGIN_CATEGORY_SYNTH;
  89. return getPluginCategoryFromName(fName);
  90. }
  91. long uniqueId() const
  92. {
  93. CARLA_ASSERT(fDescriptor != nullptr);
  94. return (fDescriptor != nullptr) ? static_cast<long>(fDescriptor->UniqueID) : 0;
  95. }
  96. // -------------------------------------------------------------------
  97. // Information (current data)
  98. int32_t chunkData(void** const dataPtr)
  99. {
  100. CARLA_ASSERT(fOptions & PLUGIN_OPTION_USE_CHUNKS);
  101. CARLA_ASSERT(fDssiDescriptor != nullptr);
  102. CARLA_ASSERT(fDssiDescriptor->get_custom_data != nullptr);
  103. CARLA_ASSERT(fHandle != nullptr);
  104. CARLA_ASSERT(fHandle2 == nullptr);
  105. CARLA_ASSERT(dataPtr != nullptr);
  106. unsigned long dataSize = 0;
  107. if (fDssiDescriptor->get_custom_data != nullptr && fDssiDescriptor->get_custom_data(fHandle, dataPtr, &dataSize) != 0)
  108. return static_cast<int32_t>(dataSize);
  109. return 0;
  110. }
  111. // -------------------------------------------------------------------
  112. // Information (per-plugin data)
  113. unsigned int availableOptions()
  114. {
  115. CARLA_ASSERT(fDescriptor != nullptr);
  116. #ifdef __USE_GNU
  117. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  118. const bool isZASX = fFilename.contains("zynaddsubfx", true);
  119. #else
  120. const bool isDssiVst = fFilename.contains("dssi-vst");
  121. const bool isZASX = fFilename.contains("zynaddsubfx");
  122. #endif
  123. unsigned int options = 0x0;
  124. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  125. if (isDssiVst)
  126. {
  127. if (fDescriptor != nullptr && fDssiDescriptor->get_custom_data != nullptr && fDssiDescriptor->set_custom_data != nullptr)
  128. options |= PLUGIN_OPTION_USE_CHUNKS;
  129. }
  130. else
  131. {
  132. if (kData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK)
  133. {
  134. if (kData->audioIn.count <= 1 && kData->audioOut.count <= 1 && (kData->audioIn.count != 0 || kData->audioOut.count != 0))
  135. options |= PLUGIN_OPTION_FORCE_STEREO;
  136. }
  137. if (! isZASX)
  138. options |= PLUGIN_OPTION_FIXED_BUFFER;
  139. }
  140. if (kData->extraHints & PLUGIN_HINT_HAS_MIDI_IN)
  141. {
  142. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  143. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  144. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  145. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  146. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  147. }
  148. return options;
  149. }
  150. float getParameterValue(const uint32_t parameterId)
  151. {
  152. CARLA_ASSERT(parameterId < kData->param.count);
  153. return fParamBuffers[parameterId];
  154. }
  155. void getLabel(char* const strBuf)
  156. {
  157. CARLA_ASSERT(fDescriptor != nullptr);
  158. if (fDescriptor != nullptr && fDescriptor->Label != nullptr)
  159. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  160. else
  161. CarlaPlugin::getLabel(strBuf);
  162. }
  163. void getMaker(char* const strBuf)
  164. {
  165. CARLA_ASSERT(fDescriptor != nullptr);
  166. if (fDescriptor != nullptr && fDescriptor->Maker != nullptr)
  167. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  168. else
  169. CarlaPlugin::getMaker(strBuf);
  170. }
  171. void getCopyright(char* const strBuf)
  172. {
  173. CARLA_ASSERT(fDescriptor != nullptr);
  174. if (fDescriptor != nullptr && fDescriptor->Copyright != nullptr)
  175. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  176. else
  177. CarlaPlugin::getCopyright(strBuf);
  178. }
  179. void getRealName(char* const strBuf)
  180. {
  181. CARLA_ASSERT(fDescriptor != nullptr);
  182. if (fDescriptor != nullptr && fDescriptor->Name != nullptr)
  183. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  184. else
  185. CarlaPlugin::getRealName(strBuf);
  186. }
  187. void getParameterName(const uint32_t parameterId, char* const strBuf)
  188. {
  189. CARLA_ASSERT(fDescriptor != nullptr);
  190. CARLA_ASSERT(parameterId < kData->param.count);
  191. const int32_t rindex = kData->param.data[parameterId].rindex;
  192. if (fDescriptor != nullptr && rindex < static_cast<int32_t>(fDescriptor->PortCount))
  193. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  194. else
  195. CarlaPlugin::getParameterName(parameterId, strBuf);
  196. }
  197. // -------------------------------------------------------------------
  198. // Set data (plugin-specific stuff)
  199. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  200. {
  201. CARLA_ASSERT(parameterId < kData->param.count);
  202. const float fixedValue = kData->param.fixValue(parameterId, value);
  203. fParamBuffers[parameterId] = fixedValue;
  204. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  205. }
  206. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  207. {
  208. carla_debug("DssiPlugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  209. CARLA_ASSERT(fDescriptor != nullptr);
  210. CARLA_ASSERT(fHandle != nullptr);
  211. CARLA_ASSERT(type != nullptr);
  212. CARLA_ASSERT(key != nullptr);
  213. CARLA_ASSERT(value != nullptr);
  214. if (type == nullptr)
  215. return carla_stderr2("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  216. if (std::strcmp(type, CUSTOM_DATA_STRING) != 0)
  217. return carla_stderr2("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  218. if (key == nullptr)
  219. return carla_stderr2("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  220. if (value == nullptr)
  221. return carla_stderr2("DssiPlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  222. if (fDssiDescriptor->configure != nullptr)
  223. {
  224. fDssiDescriptor->configure(fHandle, key, value);
  225. if (fHandle2)
  226. fDssiDescriptor->configure(fHandle2, key, value);
  227. }
  228. if (sendGui && kData->osc.data.target != nullptr)
  229. osc_send_configure(&kData->osc.data, key, value);
  230. if (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0)
  231. {
  232. const ScopedDisabler sd(this);
  233. reloadPrograms(false);
  234. }
  235. CarlaPlugin::setCustomData(type, key, value, sendGui);
  236. }
  237. void setChunkData(const char* const stringData)
  238. {
  239. CARLA_ASSERT(fOptions & PLUGIN_OPTION_USE_CHUNKS);
  240. CARLA_ASSERT(fDssiDescriptor != nullptr);
  241. CARLA_ASSERT(fDssiDescriptor->set_custom_data != nullptr);
  242. CARLA_ASSERT(fHandle != nullptr);
  243. CARLA_ASSERT(fHandle2 == nullptr);
  244. CARLA_ASSERT(stringData != nullptr);
  245. if (fDssiDescriptor->set_custom_data == nullptr)
  246. return;
  247. // FIXME
  248. fChunk = QByteArray::fromBase64(QByteArray(stringData));
  249. //fChunk.toBase64();
  250. const ScopedSingleProcessLocker spl(this, true);
  251. fDssiDescriptor->set_custom_data(fHandle, fChunk.data(), (unsigned long)fChunk.size());
  252. }
  253. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback)
  254. {
  255. CARLA_ASSERT(fDssiDescriptor != nullptr);
  256. CARLA_ASSERT(fHandle != nullptr);
  257. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->midiprog.count));
  258. if (index < -1)
  259. index = -1;
  260. else if (index > static_cast<int32_t>(kData->midiprog.count))
  261. return;
  262. if (fDssiDescriptor != nullptr && fHandle != nullptr && index >= 0)
  263. {
  264. const uint32_t bank = kData->midiprog.data[index].bank;
  265. const uint32_t program = kData->midiprog.data[index].program;
  266. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  267. fDssiDescriptor->select_program(fHandle, bank, program);
  268. if (fHandle2 != nullptr)
  269. fDssiDescriptor->select_program(fHandle2, bank, program);
  270. }
  271. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  272. }
  273. // -------------------------------------------------------------------
  274. // Set gui stuff
  275. void showGui(const bool yesNo)
  276. {
  277. if (yesNo)
  278. {
  279. kData->osc.thread.start();
  280. }
  281. else
  282. {
  283. if (kData->osc.data.target != nullptr)
  284. {
  285. osc_send_hide(&kData->osc.data);
  286. osc_send_quit(&kData->osc.data);
  287. kData->osc.data.free();
  288. }
  289. if (kData->osc.thread.isRunning() && ! kData->osc.thread.wait(kData->engine->getOptions().oscUiTimeout))
  290. kData->osc.thread.terminate();
  291. }
  292. }
  293. // -------------------------------------------------------------------
  294. // Plugin state
  295. void reload()
  296. {
  297. carla_debug("DssiPlugin::reload() - start");
  298. CARLA_ASSERT(kData->engine != nullptr);
  299. CARLA_ASSERT(fDescriptor != nullptr);
  300. CARLA_ASSERT(fDssiDescriptor != nullptr);
  301. CARLA_ASSERT(fHandle != nullptr);
  302. if (kData->engine == nullptr)
  303. return;
  304. if (fDescriptor == nullptr)
  305. return;
  306. if (fDssiDescriptor == nullptr)
  307. return;
  308. if (fHandle == nullptr)
  309. return;
  310. const ProcessMode processMode(kData->engine->getProccessMode());
  311. // Safely disable plugin for reload
  312. const ScopedDisabler sd(this);
  313. if (kData->active)
  314. deactivate();
  315. clearBuffers();
  316. const float sampleRate = (float)kData->engine->getSampleRate();
  317. const uint32_t portCount = static_cast<uint32_t>(fDescriptor->PortCount);
  318. uint32_t aIns, aOuts, mIns, params, j;
  319. aIns = aOuts = mIns = params = 0;
  320. bool forcedStereoIn, forcedStereoOut;
  321. forcedStereoIn = forcedStereoOut = false;
  322. bool needsCtrlIn, needsCtrlOut;
  323. needsCtrlIn = needsCtrlOut = false;
  324. if (portCount > 0)
  325. {
  326. CARLA_ASSERT(fDescriptor->PortDescriptors != nullptr);
  327. CARLA_ASSERT(fDescriptor->PortRangeHints != nullptr);
  328. CARLA_ASSERT(fDescriptor->PortNames != nullptr);
  329. for (uint32_t i=0; i < portCount; ++i)
  330. {
  331. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  332. if (LADSPA_IS_PORT_AUDIO(portType))
  333. {
  334. if (LADSPA_IS_PORT_INPUT(portType))
  335. aIns += 1;
  336. else if (LADSPA_IS_PORT_OUTPUT(portType))
  337. aOuts += 1;
  338. }
  339. else if (LADSPA_IS_PORT_CONTROL(portType))
  340. params += 1;
  341. }
  342. }
  343. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  344. {
  345. if (fHandle2 == nullptr)
  346. fHandle2 = fDescriptor->instantiate(fDescriptor, (unsigned long)sampleRate);
  347. if (aIns == 1)
  348. {
  349. aIns = 2;
  350. forcedStereoIn = true;
  351. }
  352. if (aOuts == 1)
  353. {
  354. aOuts = 2;
  355. forcedStereoOut = true;
  356. }
  357. }
  358. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  359. {
  360. mIns = 1;
  361. needsCtrlIn = true;
  362. }
  363. if (aIns > 0)
  364. {
  365. kData->audioIn.createNew(aIns);
  366. fAudioInBuffers = new float*[aIns];
  367. for (uint32_t i=0; i < aIns; ++i)
  368. fAudioInBuffers[i] = nullptr;
  369. }
  370. if (aOuts > 0)
  371. {
  372. kData->audioOut.createNew(aOuts);
  373. fAudioOutBuffers = new float*[aOuts];
  374. needsCtrlIn = true;
  375. for (uint32_t i=0; i < aOuts; ++i)
  376. fAudioOutBuffers[i] = nullptr;
  377. }
  378. if (params > 0)
  379. {
  380. kData->param.createNew(params);
  381. fParamBuffers = new float[params];
  382. for (uint32_t i=0; i < params; ++i)
  383. fParamBuffers[i] = 0.0f;
  384. }
  385. const uint portNameSize = kData->engine->maxPortNameSize();
  386. CarlaString portName;
  387. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  388. {
  389. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  390. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  391. CARLA_ASSERT(fDescriptor->PortNames[i] != nullptr);
  392. if (LADSPA_IS_PORT_AUDIO(portType))
  393. {
  394. portName.clear();
  395. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  396. {
  397. portName = fName;
  398. portName += ":";
  399. }
  400. portName += fDescriptor->PortNames[i];
  401. portName.truncate(portNameSize);
  402. if (LADSPA_IS_PORT_INPUT(portType))
  403. {
  404. j = iAudioIn++;
  405. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  406. kData->audioIn.ports[j].rindex = i;
  407. if (forcedStereoIn)
  408. {
  409. portName += "_2";
  410. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  411. kData->audioIn.ports[1].rindex = i;
  412. }
  413. }
  414. else if (LADSPA_IS_PORT_OUTPUT(portType))
  415. {
  416. j = iAudioOut++;
  417. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  418. kData->audioOut.ports[j].rindex = i;
  419. if (forcedStereoOut)
  420. {
  421. portName += "_2";
  422. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  423. kData->audioOut.ports[1].rindex = i;
  424. }
  425. }
  426. else
  427. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  428. }
  429. else if (LADSPA_IS_PORT_CONTROL(portType))
  430. {
  431. j = iCtrl++;
  432. kData->param.data[j].index = j;
  433. kData->param.data[j].rindex = i;
  434. kData->param.data[j].hints = 0x0;
  435. kData->param.data[j].midiChannel = 0;
  436. kData->param.data[j].midiCC = -1;
  437. float min, max, def, step, stepSmall, stepLarge;
  438. // min value
  439. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  440. min = portRangeHints.LowerBound;
  441. else
  442. min = 0.0f;
  443. // max value
  444. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  445. max = portRangeHints.UpperBound;
  446. else
  447. max = 1.0f;
  448. if (min > max)
  449. max = min;
  450. else if (max < min)
  451. min = max;
  452. if (max - min == 0.0f)
  453. {
  454. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fDescriptor->PortNames[i]);
  455. max = min + 0.1f;
  456. }
  457. // default value
  458. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  459. if (def < min)
  460. def = min;
  461. else if (def > max)
  462. def = max;
  463. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  464. {
  465. min *= sampleRate;
  466. max *= sampleRate;
  467. def *= sampleRate;
  468. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  469. }
  470. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  471. {
  472. step = max - min;
  473. stepSmall = step;
  474. stepLarge = step;
  475. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  476. }
  477. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  478. {
  479. step = 1.0f;
  480. stepSmall = 1.0f;
  481. stepLarge = 10.0f;
  482. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  483. }
  484. else
  485. {
  486. float range = max - min;
  487. step = range/100.0f;
  488. stepSmall = range/1000.0f;
  489. stepLarge = range/10.0f;
  490. }
  491. if (LADSPA_IS_PORT_INPUT(portType))
  492. {
  493. kData->param.data[j].type = PARAMETER_INPUT;
  494. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  495. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  496. needsCtrlIn = true;
  497. // MIDI CC value
  498. if (fDssiDescriptor->get_midi_controller_for_port != nullptr)
  499. {
  500. int controller = fDssiDescriptor->get_midi_controller_for_port(fHandle, i);
  501. if (DSSI_CONTROLLER_IS_SET(controller) && DSSI_IS_CC(controller))
  502. {
  503. int16_t cc = DSSI_CC_NUMBER(controller);
  504. if (! MIDI_IS_CONTROL_BANK_SELECT(cc))
  505. kData->param.data[j].midiCC = cc;
  506. }
  507. }
  508. }
  509. else if (LADSPA_IS_PORT_OUTPUT(portType))
  510. {
  511. if (std::strcmp(fDescriptor->PortNames[i], "latency") == 0 || std::strcmp(fDescriptor->PortNames[i], "_latency") == 0)
  512. {
  513. min = 0.0f;
  514. max = sampleRate;
  515. def = 0.0f;
  516. step = 1.0f;
  517. stepSmall = 1.0f;
  518. stepLarge = 1.0f;
  519. kData->param.data[j].type = PARAMETER_LATENCY;
  520. kData->param.data[j].hints = 0;
  521. }
  522. else if (std::strcmp(fDescriptor->PortNames[i], "_sample-rate") == 0)
  523. {
  524. def = sampleRate;
  525. step = 1.0f;
  526. stepSmall = 1.0f;
  527. stepLarge = 1.0f;
  528. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  529. kData->param.data[j].hints = 0;
  530. }
  531. else
  532. {
  533. kData->param.data[j].type = PARAMETER_OUTPUT;
  534. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  535. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  536. needsCtrlOut = true;
  537. }
  538. }
  539. else
  540. {
  541. kData->param.data[j].type = PARAMETER_UNKNOWN;
  542. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  543. }
  544. // extra parameter hints
  545. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  546. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  547. kData->param.ranges[j].min = min;
  548. kData->param.ranges[j].max = max;
  549. kData->param.ranges[j].def = def;
  550. kData->param.ranges[j].step = step;
  551. kData->param.ranges[j].stepSmall = stepSmall;
  552. kData->param.ranges[j].stepLarge = stepLarge;
  553. // Start parameters in their default values
  554. fParamBuffers[j] = def;
  555. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  556. if (fHandle2 != nullptr)
  557. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  558. }
  559. else
  560. {
  561. // Not Audio or Control
  562. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  563. fDescriptor->connect_port(fHandle, i, nullptr);
  564. if (fHandle2 != nullptr)
  565. fDescriptor->connect_port(fHandle2, i, nullptr);
  566. }
  567. }
  568. if (needsCtrlIn)
  569. {
  570. portName.clear();
  571. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  572. {
  573. portName = fName;
  574. portName += ":";
  575. }
  576. portName += "event-in";
  577. portName.truncate(portNameSize);
  578. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  579. }
  580. if (needsCtrlOut)
  581. {
  582. portName.clear();
  583. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  584. {
  585. portName = fName;
  586. portName += ":";
  587. }
  588. portName += "event-out";
  589. portName.truncate(portNameSize);
  590. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  591. }
  592. // plugin hints
  593. const bool hasGUI = (fHints & PLUGIN_HAS_GUI);
  594. #ifdef __USE_GNU
  595. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  596. const bool isZASX = fFilename.contains("zynaddsubfx", true);
  597. #else
  598. const bool isDssiVst = fFilename.contains("dssi-vst");
  599. const bool isZASX = fFilename.contains("zynaddsubfx");
  600. #endif
  601. fHints = 0x0;
  602. if (hasGUI)
  603. fHints |= PLUGIN_HAS_GUI;
  604. if (mIns == 1 && aIns == 0 && aOuts > 0)
  605. fHints |= PLUGIN_IS_SYNTH;
  606. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  607. fHints |= PLUGIN_CAN_DRYWET;
  608. if (aOuts > 0)
  609. fHints |= PLUGIN_CAN_VOLUME;
  610. if (aOuts >= 2 && aOuts % 2 == 0)
  611. fHints |= PLUGIN_CAN_BALANCE;
  612. // extra plugin hints
  613. kData->extraHints = 0x0;
  614. if (mIns > 0)
  615. kData->extraHints |= PLUGIN_HINT_HAS_MIDI_IN;
  616. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  617. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  618. // plugin options
  619. fOptions = 0x0;
  620. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  621. if (forcedStereoIn || forcedStereoOut)
  622. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  623. if (isDssiVst)
  624. {
  625. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  626. if (kData->engine->getOptions().useDssiVstChunks && fDssiDescriptor->get_custom_data != nullptr && fDssiDescriptor->set_custom_data != nullptr)
  627. fOptions |= PLUGIN_OPTION_USE_CHUNKS;
  628. }
  629. else if (isZASX)
  630. {
  631. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  632. }
  633. if (mIns > 0)
  634. {
  635. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  636. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  637. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  638. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  639. }
  640. // check latency
  641. if (fHints & PLUGIN_CAN_DRYWET)
  642. {
  643. for (uint32_t i=0; i < kData->param.count; ++i)
  644. {
  645. if (kData->param.data[i].type != PARAMETER_LATENCY)
  646. continue;
  647. // we need to pre-run the plugin so it can update its latency control-port
  648. float tmpIn[aIns][2];
  649. float tmpOut[aOuts][2];
  650. for (j=0; j < aIns; ++j)
  651. {
  652. tmpIn[j][0] = 0.0f;
  653. tmpIn[j][1] = 0.0f;
  654. fDescriptor->connect_port(fHandle, kData->audioIn.ports[j].rindex, tmpIn[j]);
  655. }
  656. for (j=0; j < aOuts; ++j)
  657. {
  658. tmpOut[j][0] = 0.0f;
  659. tmpOut[j][1] = 0.0f;
  660. fDescriptor->connect_port(fHandle, kData->audioOut.ports[j].rindex, tmpOut[j]);
  661. }
  662. if (fDescriptor->activate != nullptr)
  663. fDescriptor->activate(fHandle);
  664. fDescriptor->run(fHandle, 2);
  665. if (fDescriptor->deactivate != nullptr)
  666. fDescriptor->deactivate(fHandle);
  667. const uint32_t latency = (uint32_t)fParamBuffers[i];
  668. if (kData->latency != latency)
  669. {
  670. kData->latency = latency;
  671. kData->client->setLatency(latency);
  672. kData->recreateLatencyBuffers();
  673. }
  674. break;
  675. }
  676. }
  677. bufferSizeChanged(kData->engine->getBufferSize());
  678. reloadPrograms(true);
  679. carla_debug("DssiPlugin::reload() - end");
  680. }
  681. void reloadPrograms(const bool init)
  682. {
  683. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  684. uint32_t i, oldCount = kData->midiprog.count;
  685. const int32_t current = kData->midiprog.current;
  686. // Delete old programs
  687. kData->midiprog.clear();
  688. // Query new programs
  689. uint32_t count = 0;
  690. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  691. {
  692. while (fDssiDescriptor->get_program(fHandle, count))
  693. count++;
  694. }
  695. if (count > 0)
  696. {
  697. kData->midiprog.createNew(count);
  698. // Update data
  699. for (i=0; i < count; ++i)
  700. {
  701. const DSSI_Program_Descriptor* const pdesc = fDssiDescriptor->get_program(fHandle, i);
  702. CARLA_ASSERT(pdesc != nullptr);
  703. CARLA_ASSERT(pdesc->Name != nullptr);
  704. kData->midiprog.data[i].bank = static_cast<uint32_t>(pdesc->Bank);
  705. kData->midiprog.data[i].program = static_cast<uint32_t>(pdesc->Program);
  706. kData->midiprog.data[i].name = carla_strdup(pdesc->Name);
  707. }
  708. }
  709. #ifndef BUILD_BRIDGE
  710. // Update OSC Names
  711. if (kData->engine->isOscControlRegistered())
  712. {
  713. kData->engine->osc_send_control_set_midi_program_count(fId, count);
  714. for (i=0; i < count; ++i)
  715. kData->engine->osc_send_control_set_midi_program_data(fId, i, kData->midiprog.data[i].bank, kData->midiprog.data[i].program, kData->midiprog.data[i].name);
  716. }
  717. #endif
  718. if (init)
  719. {
  720. if (count > 0)
  721. setMidiProgram(0, false, false, false);
  722. }
  723. else
  724. {
  725. // Check if current program is invalid
  726. bool programChanged = false;
  727. if (count == oldCount+1)
  728. {
  729. // one midi program added, probably created by user
  730. kData->midiprog.current = oldCount;
  731. programChanged = true;
  732. }
  733. else if (current < 0 && count > 0)
  734. {
  735. // programs exist now, but not before
  736. kData->midiprog.current = 0;
  737. programChanged = true;
  738. }
  739. else if (current >= 0 && count == 0)
  740. {
  741. // programs existed before, but not anymore
  742. kData->midiprog.current = -1;
  743. programChanged = true;
  744. }
  745. else if (current >= static_cast<int32_t>(count))
  746. {
  747. // current midi program > count
  748. kData->midiprog.current = 0;
  749. programChanged = true;
  750. }
  751. else
  752. {
  753. // no change
  754. kData->midiprog.current = current;
  755. }
  756. if (programChanged)
  757. setMidiProgram(kData->midiprog.current, true, true, true);
  758. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  759. }
  760. }
  761. // -------------------------------------------------------------------
  762. // Plugin processing
  763. void activate()
  764. {
  765. CARLA_ASSERT(fDescriptor != nullptr);
  766. if (fDescriptor->activate != nullptr)
  767. {
  768. fDescriptor->activate(fHandle);
  769. if (fHandle2 != nullptr)
  770. fDescriptor->activate(fHandle2);
  771. }
  772. }
  773. void deactivate()
  774. {
  775. CARLA_ASSERT(fDescriptor != nullptr);
  776. if (fDescriptor->deactivate != nullptr)
  777. {
  778. fDescriptor->deactivate(fHandle);
  779. if (fHandle2 != nullptr)
  780. fDescriptor->deactivate(fHandle2);
  781. }
  782. }
  783. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  784. {
  785. uint32_t i, k;
  786. // --------------------------------------------------------------------------------------------------------
  787. // Check if active
  788. if (! kData->active)
  789. {
  790. // disable any output sound
  791. for (i=0; i < kData->audioOut.count; ++i)
  792. carla_zeroFloat(outBuffer[i], frames);
  793. return;
  794. }
  795. unsigned long midiEventCount = 0;
  796. // --------------------------------------------------------------------------------------------------------
  797. // Check if not active before
  798. if (kData->needsReset)
  799. {
  800. // TODO!
  801. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  802. {
  803. for (unsigned char j=0, l=MAX_MIDI_CHANNELS; j < MAX_MIDI_CHANNELS; ++j)
  804. {
  805. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[j]);
  806. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[j+l]);
  807. fMidiEvents[j].type = SND_SEQ_EVENT_CONTROLLER;
  808. fMidiEvents[j].data.control.channel = j;
  809. fMidiEvents[j].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  810. fMidiEvents[j+l].type = SND_SEQ_EVENT_CONTROLLER;
  811. fMidiEvents[j+l].data.control.channel = j;
  812. fMidiEvents[j+l].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  813. }
  814. midiEventCount = MAX_MIDI_CHANNELS*2;
  815. }
  816. else
  817. {
  818. }
  819. if (kData->latency > 0)
  820. {
  821. for (i=0; i < kData->audioIn.count; ++i)
  822. carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  823. }
  824. kData->needsReset = false;
  825. }
  826. // --------------------------------------------------------------------------------------------------------
  827. // Event Input and Processing
  828. if (kData->event.portIn != nullptr)
  829. {
  830. // ----------------------------------------------------------------------------------------------------
  831. // MIDI Input (External)
  832. if (kData->extNotes.mutex.tryLock())
  833. {
  834. while (midiEventCount < MAX_MIDI_EVENTS && ! kData->extNotes.data.isEmpty())
  835. {
  836. const ExternalMidiNote& note = kData->extNotes.data.getFirst(true);
  837. CARLA_ASSERT(note.channel >= 0);
  838. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  839. fMidiEvents[midiEventCount].type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  840. fMidiEvents[midiEventCount].data.note.channel = note.channel;
  841. fMidiEvents[midiEventCount].data.note.note = note.note;
  842. fMidiEvents[midiEventCount].data.note.velocity = note.velo;
  843. midiEventCount += 1;
  844. }
  845. kData->extNotes.mutex.unlock();
  846. } // End of MIDI Input (External)
  847. // ----------------------------------------------------------------------------------------------------
  848. // Event Input (System)
  849. bool allNotesOffSent = false;
  850. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  851. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  852. uint32_t startTime = 0;
  853. uint32_t timeOffset = 0;
  854. uint32_t nextBankId = 0;
  855. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  856. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  857. for (i=0; i < nEvents; ++i)
  858. {
  859. const EngineEvent& event = kData->event.portIn->getEvent(i);
  860. time = event.time;
  861. if (time >= frames)
  862. continue;
  863. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  864. if (time > timeOffset && sampleAccurate)
  865. {
  866. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset, midiEventCount))
  867. {
  868. startTime = 0;
  869. timeOffset = time;
  870. midiEventCount = 0;
  871. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  872. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  873. else
  874. nextBankId = 0;
  875. }
  876. else
  877. startTime += timeOffset;
  878. }
  879. // Control change
  880. switch (event.type)
  881. {
  882. case kEngineEventTypeNull:
  883. break;
  884. case kEngineEventTypeControl:
  885. {
  886. const EngineControlEvent& ctrlEvent = event.ctrl;
  887. switch (ctrlEvent.type)
  888. {
  889. case kEngineControlEventTypeNull:
  890. break;
  891. case kEngineControlEventTypeParameter:
  892. {
  893. // Control backend stuff
  894. if (event.channel == kData->ctrlChannel)
  895. {
  896. float value;
  897. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  898. {
  899. value = ctrlEvent.value;
  900. setDryWet(value, false, false);
  901. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  902. continue;
  903. }
  904. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  905. {
  906. value = ctrlEvent.value*127.0f/100.0f;
  907. setVolume(value, false, false);
  908. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  909. continue;
  910. }
  911. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  912. {
  913. float left, right;
  914. value = ctrlEvent.value/0.5f - 1.0f;
  915. if (value < 0.0f)
  916. {
  917. left = -1.0f;
  918. right = (value*2.0f)+1.0f;
  919. }
  920. else if (value > 0.0f)
  921. {
  922. left = (value*2.0f)-1.0f;
  923. right = 1.0f;
  924. }
  925. else
  926. {
  927. left = -1.0f;
  928. right = 1.0f;
  929. }
  930. setBalanceLeft(left, false, false);
  931. setBalanceRight(right, false, false);
  932. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  933. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  934. continue;
  935. }
  936. }
  937. // Control plugin parameters
  938. for (k=0; k < kData->param.count; ++k)
  939. {
  940. if (kData->param.data[k].midiChannel != event.channel)
  941. continue;
  942. if (kData->param.data[k].midiCC != ctrlEvent.param)
  943. continue;
  944. if (kData->param.data[k].type != PARAMETER_INPUT)
  945. continue;
  946. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  947. continue;
  948. float value;
  949. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  950. {
  951. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  952. }
  953. else
  954. {
  955. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  956. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  957. value = std::rint(value);
  958. }
  959. setParameterValue(k, value, false, false, false);
  960. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  961. }
  962. break;
  963. }
  964. case kEngineControlEventTypeMidiBank:
  965. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  966. nextBankId = ctrlEvent.param;
  967. break;
  968. case kEngineControlEventTypeMidiProgram:
  969. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  970. {
  971. const uint32_t nextProgramId = ctrlEvent.param;
  972. for (k=0; k < kData->midiprog.count; ++k)
  973. {
  974. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  975. {
  976. setMidiProgram(k, false, false, false);
  977. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  978. break;
  979. }
  980. }
  981. }
  982. break;
  983. case kEngineControlEventTypeAllSoundOff:
  984. if (event.channel == kData->ctrlChannel)
  985. {
  986. if (! allNotesOffSent)
  987. {
  988. sendMidiAllNotesOff();
  989. allNotesOffSent = true;
  990. }
  991. if (fDescriptor->deactivate != nullptr)
  992. {
  993. fDescriptor->deactivate(fHandle);
  994. if (fHandle2 != nullptr)
  995. fDescriptor->deactivate(fHandle2);
  996. }
  997. if (fDescriptor->activate != nullptr)
  998. {
  999. fDescriptor->activate(fHandle);
  1000. if (fHandle2 != nullptr)
  1001. fDescriptor->activate(fHandle2);
  1002. }
  1003. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0f);
  1004. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0f);
  1005. }
  1006. if (midiEventCount >= MAX_MIDI_EVENTS)
  1007. continue;
  1008. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1009. {
  1010. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  1011. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : time;
  1012. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  1013. fMidiEvents[midiEventCount].data.control.channel = event.channel;
  1014. fMidiEvents[midiEventCount].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  1015. midiEventCount += 1;
  1016. }
  1017. break;
  1018. case kEngineControlEventTypeAllNotesOff:
  1019. if (event.channel == kData->ctrlChannel)
  1020. {
  1021. if (! allNotesOffSent)
  1022. {
  1023. allNotesOffSent = true;
  1024. sendMidiAllNotesOff();
  1025. }
  1026. }
  1027. if (midiEventCount >= MAX_MIDI_EVENTS)
  1028. continue;
  1029. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1030. {
  1031. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  1032. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : time;
  1033. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  1034. fMidiEvents[midiEventCount].data.control.channel = event.channel;
  1035. fMidiEvents[midiEventCount].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1036. midiEventCount += 1;
  1037. }
  1038. break;
  1039. }
  1040. break;
  1041. }
  1042. case kEngineEventTypeMidi:
  1043. {
  1044. if (midiEventCount >= MAX_MIDI_EVENTS)
  1045. continue;
  1046. const EngineMidiEvent& midiEvent = event.midi;
  1047. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1048. uint8_t channel = event.channel;
  1049. // Fix bad note-off (per DSSI spec)
  1050. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  1051. status -= 0x10;
  1052. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  1053. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : time;
  1054. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1055. {
  1056. const uint8_t note = midiEvent.data[1];
  1057. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEOFF;
  1058. fMidiEvents[midiEventCount].data.note.channel = channel;
  1059. fMidiEvents[midiEventCount].data.note.note = note;
  1060. postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0f);
  1061. }
  1062. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1063. {
  1064. const uint8_t note = midiEvent.data[1];
  1065. const uint8_t velo = midiEvent.data[2];
  1066. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEON;
  1067. fMidiEvents[midiEventCount].data.note.channel = channel;
  1068. fMidiEvents[midiEventCount].data.note.note = note;
  1069. fMidiEvents[midiEventCount].data.note.velocity = velo;
  1070. postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  1071. }
  1072. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) != 0)
  1073. {
  1074. const uint8_t note = midiEvent.data[1];
  1075. const uint8_t pressure = midiEvent.data[2];
  1076. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_KEYPRESS;
  1077. fMidiEvents[midiEventCount].data.note.channel = channel;
  1078. fMidiEvents[midiEventCount].data.note.note = note;
  1079. fMidiEvents[midiEventCount].data.note.velocity = pressure;
  1080. }
  1081. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0)
  1082. {
  1083. const uint8_t control = midiEvent.data[1];
  1084. const uint8_t value = midiEvent.data[2];
  1085. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  1086. fMidiEvents[midiEventCount].data.control.channel = channel;
  1087. fMidiEvents[midiEventCount].data.control.param = control;
  1088. fMidiEvents[midiEventCount].data.control.value = value;
  1089. }
  1090. else if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) != 0)
  1091. {
  1092. const uint8_t pressure = midiEvent.data[1];
  1093. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CHANPRESS;
  1094. fMidiEvents[midiEventCount].data.control.channel = channel;
  1095. fMidiEvents[midiEventCount].data.control.value = pressure;
  1096. }
  1097. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) != 0)
  1098. {
  1099. const uint8_t lsb = midiEvent.data[1];
  1100. const uint8_t msb = midiEvent.data[2];
  1101. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_PITCHBEND;
  1102. fMidiEvents[midiEventCount].data.control.channel = channel;
  1103. fMidiEvents[midiEventCount].data.control.value = ((msb << 7) | lsb) - 8192;
  1104. }
  1105. else
  1106. continue;
  1107. midiEventCount += 1;
  1108. break;
  1109. }
  1110. }
  1111. }
  1112. kData->postRtEvents.trySplice();
  1113. if (frames > timeOffset)
  1114. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset, midiEventCount);
  1115. } // End of Event Input and Processing
  1116. // --------------------------------------------------------------------------------------------------------
  1117. // Plugin processing (no events)
  1118. else
  1119. {
  1120. processSingle(inBuffer, outBuffer, frames, 0, midiEventCount);
  1121. } // End of Plugin processing (no events)
  1122. CARLA_PROCESS_CONTINUE_CHECK;
  1123. // --------------------------------------------------------------------------------------------------------
  1124. // Control Output
  1125. if (kData->event.portOut != nullptr)
  1126. {
  1127. uint8_t channel;
  1128. uint16_t param;
  1129. float value;
  1130. for (k=0; k < kData->param.count; ++k)
  1131. {
  1132. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1133. continue;
  1134. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  1135. if (kData->param.data[k].midiCC > 0)
  1136. {
  1137. channel = kData->param.data[k].midiChannel;
  1138. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  1139. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  1140. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  1141. }
  1142. }
  1143. } // End of Control Output
  1144. // --------------------------------------------------------------------------------------------------------
  1145. //kData->activeBefore = kData->active;
  1146. }
  1147. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset, const unsigned long midiEventCount)
  1148. {
  1149. CARLA_ASSERT(frames > 0);
  1150. if (frames == 0)
  1151. return false;
  1152. if (kData->audioIn.count > 0)
  1153. {
  1154. CARLA_ASSERT(inBuffer != nullptr);
  1155. if (inBuffer == nullptr)
  1156. return false;
  1157. }
  1158. if (kData->audioOut.count > 0)
  1159. {
  1160. CARLA_ASSERT(outBuffer != nullptr);
  1161. if (outBuffer == nullptr)
  1162. return false;
  1163. }
  1164. uint32_t i, k;
  1165. // --------------------------------------------------------------------------------------------------------
  1166. // Try lock, silence otherwise
  1167. if (kData->engine->isOffline())
  1168. {
  1169. kData->singleMutex.lock();
  1170. }
  1171. else if (! kData->singleMutex.tryLock())
  1172. {
  1173. for (i=0; i < kData->audioOut.count; ++i)
  1174. {
  1175. for (k=0; k < frames; ++k)
  1176. outBuffer[i][k+timeOffset] = 0.0f;
  1177. }
  1178. return false;
  1179. }
  1180. // --------------------------------------------------------------------------------------------------------
  1181. // Reset audio buffers
  1182. for (i=0; i < kData->audioIn.count; ++i)
  1183. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1184. for (i=0; i < kData->audioOut.count; ++i)
  1185. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1186. // --------------------------------------------------------------------------------------------------------
  1187. // Run plugin
  1188. if (fDssiDescriptor->run_synth != nullptr)
  1189. {
  1190. fDssiDescriptor->run_synth(fHandle, frames, fMidiEvents, midiEventCount);
  1191. if (fHandle2 != nullptr)
  1192. fDssiDescriptor->run_synth(fHandle2, frames, fMidiEvents, midiEventCount);
  1193. }
  1194. else if (fDssiDescriptor->run_multiple_synths != nullptr)
  1195. {
  1196. unsigned long instances = (fHandle2 != nullptr) ? 2 : 1;
  1197. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1198. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1199. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1200. fDssiDescriptor->run_multiple_synths(instances, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1201. }
  1202. else
  1203. {
  1204. fDescriptor->run(fHandle, frames);
  1205. if (fHandle2 != nullptr)
  1206. fDescriptor->run(fHandle2, frames);
  1207. }
  1208. // --------------------------------------------------------------------------------------------------------
  1209. // Post-processing (dry/wet, volume and balance)
  1210. {
  1211. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  1212. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1213. bool isPair;
  1214. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1215. for (i=0; i < kData->audioOut.count; ++i)
  1216. {
  1217. // Dry/Wet
  1218. if (doDryWet)
  1219. {
  1220. for (k=0; k < frames; ++k)
  1221. {
  1222. // TODO
  1223. //if (k < kData->latency && kData->latency < frames)
  1224. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  1225. //else
  1226. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1227. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  1228. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1229. }
  1230. }
  1231. // Balance
  1232. if (doBalance)
  1233. {
  1234. isPair = (i % 2 == 0);
  1235. if (isPair)
  1236. {
  1237. CARLA_ASSERT(i+1 < kData->audioOut.count);
  1238. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  1239. }
  1240. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1241. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1242. for (k=0; k < frames; ++k)
  1243. {
  1244. if (isPair)
  1245. {
  1246. // left
  1247. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1248. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1249. }
  1250. else
  1251. {
  1252. // right
  1253. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1254. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1255. }
  1256. }
  1257. }
  1258. // Volume (and buffer copy)
  1259. {
  1260. for (k=0; k < frames; ++k)
  1261. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  1262. }
  1263. }
  1264. #if 0
  1265. // Latency, save values for next callback, TODO
  1266. if (kData->latency > 0 && kData->latency < frames)
  1267. {
  1268. for (i=0; i < kData->audioIn.count; ++i)
  1269. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  1270. }
  1271. #endif
  1272. } // End of Post-processing
  1273. // --------------------------------------------------------------------------------------------------------
  1274. kData->singleMutex.unlock();
  1275. return true;
  1276. }
  1277. void bufferSizeChanged(const uint32_t newBufferSize)
  1278. {
  1279. carla_debug("DssiPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1280. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1281. {
  1282. if (fAudioInBuffers[i] != nullptr)
  1283. delete[] fAudioInBuffers[i];
  1284. fAudioInBuffers[i] = new float[newBufferSize];
  1285. }
  1286. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1287. {
  1288. if (fAudioOutBuffers[i] != nullptr)
  1289. delete[] fAudioOutBuffers[i];
  1290. fAudioOutBuffers[i] = new float[newBufferSize];
  1291. }
  1292. if (fHandle2 == nullptr)
  1293. {
  1294. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1295. {
  1296. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1297. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1298. }
  1299. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1300. {
  1301. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1302. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1303. }
  1304. }
  1305. else
  1306. {
  1307. if (kData->audioIn.count > 0)
  1308. {
  1309. CARLA_ASSERT(kData->audioIn.count == 2);
  1310. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1311. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1312. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1313. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1314. }
  1315. if (kData->audioOut.count > 0)
  1316. {
  1317. CARLA_ASSERT(kData->audioOut.count == 2);
  1318. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1319. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1320. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1321. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1322. }
  1323. }
  1324. carla_debug("DssiPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1325. }
  1326. // -------------------------------------------------------------------
  1327. // Post-poned events
  1328. void uiParameterChange(const uint32_t index, const float value)
  1329. {
  1330. CARLA_ASSERT(index < kData->param.count);
  1331. if (index >= kData->param.count)
  1332. return;
  1333. if (kData->osc.data.target == nullptr)
  1334. return;
  1335. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  1336. }
  1337. void uiMidiProgramChange(const uint32_t index)
  1338. {
  1339. CARLA_ASSERT(index < kData->midiprog.count);
  1340. if (index >= kData->midiprog.count)
  1341. return;
  1342. if (kData->osc.data.target == nullptr)
  1343. return;
  1344. osc_send_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1345. }
  1346. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1347. {
  1348. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1349. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1350. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1351. if (channel >= MAX_MIDI_CHANNELS)
  1352. return;
  1353. if (note >= MAX_MIDI_NOTE)
  1354. return;
  1355. if (velo >= MAX_MIDI_VALUE)
  1356. return;
  1357. if (kData->osc.data.target == nullptr)
  1358. return;
  1359. uint8_t midiData[4] = { 0 };
  1360. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1361. midiData[2] = note;
  1362. midiData[3] = velo;
  1363. osc_send_midi(&kData->osc.data, midiData);
  1364. }
  1365. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1366. {
  1367. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1368. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1369. if (channel >= MAX_MIDI_CHANNELS)
  1370. return;
  1371. if (note >= MAX_MIDI_NOTE)
  1372. return;
  1373. if (kData->osc.data.target == nullptr)
  1374. return;
  1375. uint8_t midiData[4] = { 0 };
  1376. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1377. midiData[2] = note;
  1378. osc_send_midi(&kData->osc.data, midiData);
  1379. }
  1380. // -------------------------------------------------------------------
  1381. // Cleanup
  1382. void clearBuffers()
  1383. {
  1384. carla_debug("DssiPlugin::clearBuffers() - start");
  1385. if (fAudioInBuffers != nullptr)
  1386. {
  1387. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1388. {
  1389. if (fAudioInBuffers[i] != nullptr)
  1390. {
  1391. delete[] fAudioInBuffers[i];
  1392. fAudioInBuffers[i] = nullptr;
  1393. }
  1394. }
  1395. delete[] fAudioInBuffers;
  1396. fAudioInBuffers = nullptr;
  1397. }
  1398. if (fAudioOutBuffers != nullptr)
  1399. {
  1400. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1401. {
  1402. if (fAudioOutBuffers[i] != nullptr)
  1403. {
  1404. delete[] fAudioOutBuffers[i];
  1405. fAudioOutBuffers[i] = nullptr;
  1406. }
  1407. }
  1408. delete[] fAudioOutBuffers;
  1409. fAudioOutBuffers = nullptr;
  1410. }
  1411. if (fParamBuffers != nullptr)
  1412. {
  1413. delete[] fParamBuffers;
  1414. fParamBuffers = nullptr;
  1415. }
  1416. kData->clearBuffers();
  1417. carla_debug("DssiPlugin::clearBuffers() - end");
  1418. }
  1419. // -------------------------------------------------------------------
  1420. bool init(const char* const filename, const char* const name, const char* const label, const char* const guiFilename)
  1421. {
  1422. CARLA_ASSERT(kData->engine != nullptr);
  1423. CARLA_ASSERT(kData->client == nullptr);
  1424. CARLA_ASSERT(filename != nullptr);
  1425. CARLA_ASSERT(label != nullptr);
  1426. // ---------------------------------------------------------------
  1427. // first checks
  1428. if (kData->engine == nullptr)
  1429. {
  1430. return false;
  1431. }
  1432. if (kData->client != nullptr)
  1433. {
  1434. kData->engine->setLastError("Plugin client is already registered");
  1435. return false;
  1436. }
  1437. if (filename == nullptr)
  1438. {
  1439. kData->engine->setLastError("null filename");
  1440. return false;
  1441. }
  1442. if (label == nullptr)
  1443. {
  1444. kData->engine->setLastError("null label");
  1445. return false;
  1446. }
  1447. // ---------------------------------------------------------------
  1448. // open DLL
  1449. if (! kData->libOpen(filename))
  1450. {
  1451. kData->engine->setLastError(kData->libError(filename));
  1452. return false;
  1453. }
  1454. // ---------------------------------------------------------------
  1455. // get DLL main entry
  1456. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)kData->libSymbol("dssi_descriptor");
  1457. if (descFn == nullptr)
  1458. {
  1459. kData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1460. return false;
  1461. }
  1462. // ---------------------------------------------------------------
  1463. // get descriptor that matches label
  1464. unsigned long i = 0;
  1465. while ((fDssiDescriptor = descFn(i++)) != nullptr)
  1466. {
  1467. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1468. if (fDescriptor != nullptr && fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1469. break;
  1470. }
  1471. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1472. {
  1473. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1474. return false;
  1475. }
  1476. // ---------------------------------------------------------------
  1477. // get info
  1478. if (name != nullptr)
  1479. fName = kData->engine->getNewUniquePluginName(name);
  1480. else if (fDescriptor->Name != nullptr)
  1481. fName = kData->engine->getNewUniquePluginName(fDescriptor->Name);
  1482. else
  1483. fName = kData->engine->getNewUniquePluginName(fDescriptor->Label);
  1484. fFilename = filename;
  1485. // ---------------------------------------------------------------
  1486. // register client
  1487. kData->client = kData->engine->addClient(this);
  1488. if (kData->client == nullptr || ! kData->client->isOk())
  1489. {
  1490. kData->engine->setLastError("Failed to register plugin client");
  1491. return false;
  1492. }
  1493. // ---------------------------------------------------------------
  1494. // initialize plugin
  1495. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)kData->engine->getSampleRate());
  1496. if (fHandle == nullptr)
  1497. {
  1498. kData->engine->setLastError("Plugin failed to initialize");
  1499. return false;
  1500. }
  1501. // ---------------------------------------------------------------
  1502. // gui stuff
  1503. if (guiFilename != nullptr)
  1504. {
  1505. kData->osc.thread.setOscData(guiFilename, fDescriptor->Label);
  1506. fHints |= PLUGIN_HAS_GUI;
  1507. }
  1508. // TODO - load settings for options:
  1509. //fOptions & PLUGIN_OPTION_FORCE_STEREO
  1510. return true;
  1511. }
  1512. private:
  1513. LADSPA_Handle fHandle;
  1514. LADSPA_Handle fHandle2;
  1515. const LADSPA_Descriptor* fDescriptor;
  1516. const DSSI_Descriptor* fDssiDescriptor;
  1517. float** fAudioInBuffers;
  1518. float** fAudioOutBuffers;
  1519. float* fParamBuffers;
  1520. snd_seq_event_t fMidiEvents[MAX_MIDI_EVENTS];
  1521. QByteArray fChunk;
  1522. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(DssiPlugin)
  1523. };
  1524. CARLA_BACKEND_END_NAMESPACE
  1525. #else // WANT_DSSI
  1526. # warning Building without DSSI support
  1527. #endif
  1528. CARLA_BACKEND_START_NAMESPACE
  1529. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init, const char* const guiFilename)
  1530. {
  1531. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\"}, \"%s\")", init.engine, init.filename, init.name, init.label, guiFilename);
  1532. #ifdef WANT_DSSI
  1533. DssiPlugin* const plugin = new DssiPlugin(init.engine, init.id);
  1534. if (! plugin->init(init.filename, init.name, init.label, guiFilename))
  1535. {
  1536. delete plugin;
  1537. return nullptr;
  1538. }
  1539. plugin->reload();
  1540. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1541. {
  1542. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1543. delete plugin;
  1544. return nullptr;
  1545. }
  1546. return plugin;
  1547. #else
  1548. init.engine->setLastError("DSSI support not available");
  1549. return nullptr;
  1550. #endif
  1551. }
  1552. CARLA_BACKEND_END_NAMESPACE