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.

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