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.

1955 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(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)
  152. {
  153. CARLA_ASSERT(parameterId < kData->param.count);
  154. return fParamBuffers[parameterId];
  155. }
  156. void getLabel(char* const strBuf)
  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)
  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)
  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)
  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)
  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)
  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)
  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)
  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)
  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)
  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()
  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. const bool hasGUI = (fHints & PLUGIN_HAS_GUI);
  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. // check latency
  619. if (fHints & PLUGIN_CAN_DRYWET)
  620. {
  621. for (uint32_t i=0; i < kData->param.count; ++i)
  622. {
  623. if (kData->param.data[i].type != PARAMETER_LATENCY)
  624. continue;
  625. // we need to pre-run the plugin so it can update its latency control-port
  626. float tmpIn[aIns][2];
  627. float tmpOut[aOuts][2];
  628. for (j=0; j < aIns; ++j)
  629. {
  630. tmpIn[j][0] = 0.0f;
  631. tmpIn[j][1] = 0.0f;
  632. fDescriptor->connect_port(fHandle, kData->audioIn.ports[j].rindex, tmpIn[j]);
  633. }
  634. for (j=0; j < aOuts; ++j)
  635. {
  636. tmpOut[j][0] = 0.0f;
  637. tmpOut[j][1] = 0.0f;
  638. fDescriptor->connect_port(fHandle, kData->audioOut.ports[j].rindex, tmpOut[j]);
  639. }
  640. if (fDescriptor->activate != nullptr)
  641. fDescriptor->activate(fHandle);
  642. fDescriptor->run(fHandle, 2);
  643. if (fDescriptor->deactivate != nullptr)
  644. fDescriptor->deactivate(fHandle);
  645. const uint32_t latency = (uint32_t)fParamBuffers[i];
  646. if (kData->latency != latency)
  647. {
  648. kData->latency = latency;
  649. kData->client->setLatency(latency);
  650. kData->recreateLatencyBuffers();
  651. }
  652. break;
  653. }
  654. }
  655. bufferSizeChanged(kData->engine->getBufferSize());
  656. reloadPrograms(true);
  657. if (kData->active)
  658. activate();
  659. carla_debug("DssiPlugin::reload() - end");
  660. }
  661. void reloadPrograms(const bool init)
  662. {
  663. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  664. uint32_t i, oldCount = kData->midiprog.count;
  665. const int32_t current = kData->midiprog.current;
  666. // Delete old programs
  667. kData->midiprog.clear();
  668. // Query new programs
  669. uint32_t count = 0;
  670. if (fDssiDescriptor->get_program != nullptr && fDssiDescriptor->select_program != nullptr)
  671. {
  672. while (fDssiDescriptor->get_program(fHandle, count))
  673. count++;
  674. }
  675. if (count > 0)
  676. {
  677. kData->midiprog.createNew(count);
  678. // Update data
  679. for (i=0; i < count; ++i)
  680. {
  681. const DSSI_Program_Descriptor* const pdesc = fDssiDescriptor->get_program(fHandle, i);
  682. CARLA_ASSERT(pdesc != nullptr);
  683. CARLA_ASSERT(pdesc->Name != nullptr);
  684. kData->midiprog.data[i].bank = static_cast<uint32_t>(pdesc->Bank);
  685. kData->midiprog.data[i].program = static_cast<uint32_t>(pdesc->Program);
  686. kData->midiprog.data[i].name = carla_strdup(pdesc->Name);
  687. }
  688. }
  689. #ifndef BUILD_BRIDGE
  690. // Update OSC Names
  691. if (kData->engine->isOscControlRegistered())
  692. {
  693. kData->engine->osc_send_control_set_midi_program_count(fId, count);
  694. for (i=0; i < count; ++i)
  695. 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);
  696. }
  697. #endif
  698. if (init)
  699. {
  700. if (count > 0)
  701. setMidiProgram(0, false, false, false);
  702. }
  703. else
  704. {
  705. // Check if current program is invalid
  706. bool programChanged = false;
  707. if (count == oldCount+1)
  708. {
  709. // one midi program added, probably created by user
  710. kData->midiprog.current = oldCount;
  711. programChanged = true;
  712. }
  713. else if (current < 0 && count > 0)
  714. {
  715. // programs exist now, but not before
  716. kData->midiprog.current = 0;
  717. programChanged = true;
  718. }
  719. else if (current >= 0 && count == 0)
  720. {
  721. // programs existed before, but not anymore
  722. kData->midiprog.current = -1;
  723. programChanged = true;
  724. }
  725. else if (current >= static_cast<int32_t>(count))
  726. {
  727. // current midi program > count
  728. kData->midiprog.current = 0;
  729. programChanged = true;
  730. }
  731. else
  732. {
  733. // no change
  734. kData->midiprog.current = current;
  735. }
  736. if (programChanged)
  737. setMidiProgram(kData->midiprog.current, true, true, true);
  738. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  739. }
  740. }
  741. // -------------------------------------------------------------------
  742. // Plugin processing
  743. void activate()
  744. {
  745. CARLA_ASSERT(fDescriptor != nullptr);
  746. if (fDescriptor->activate != nullptr)
  747. {
  748. fDescriptor->activate(fHandle);
  749. if (fHandle2 != nullptr)
  750. fDescriptor->activate(fHandle2);
  751. }
  752. }
  753. void deactivate()
  754. {
  755. CARLA_ASSERT(fDescriptor != nullptr);
  756. if (fDescriptor->deactivate != nullptr)
  757. {
  758. fDescriptor->deactivate(fHandle);
  759. if (fHandle2 != nullptr)
  760. fDescriptor->deactivate(fHandle2);
  761. }
  762. }
  763. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  764. {
  765. uint32_t i, k;
  766. // --------------------------------------------------------------------------------------------------------
  767. // Check if active
  768. if (! kData->active)
  769. {
  770. // disable any output sound
  771. for (i=0; i < kData->audioOut.count; ++i)
  772. carla_zeroFloat(outBuffer[i], frames);
  773. return;
  774. }
  775. unsigned long midiEventCount = 0;
  776. // --------------------------------------------------------------------------------------------------------
  777. // Check if needs reset
  778. if (kData->needsReset)
  779. {
  780. // TODO!
  781. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  782. {
  783. for (unsigned char j=0, l=MAX_MIDI_CHANNELS; j < MAX_MIDI_CHANNELS; ++j)
  784. {
  785. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[j]);
  786. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[j+l]);
  787. fMidiEvents[j].type = SND_SEQ_EVENT_CONTROLLER;
  788. fMidiEvents[j].data.control.channel = j;
  789. fMidiEvents[j].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  790. fMidiEvents[j+l].type = SND_SEQ_EVENT_CONTROLLER;
  791. fMidiEvents[j+l].data.control.channel = j;
  792. fMidiEvents[j+l].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  793. }
  794. midiEventCount = MAX_MIDI_CHANNELS*2;
  795. }
  796. else
  797. {
  798. }
  799. if (kData->latency > 0)
  800. {
  801. for (i=0; i < kData->audioIn.count; ++i)
  802. carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  803. }
  804. kData->needsReset = false;
  805. }
  806. // --------------------------------------------------------------------------------------------------------
  807. // Event Input and Processing
  808. if (kData->event.portIn != nullptr)
  809. {
  810. // ----------------------------------------------------------------------------------------------------
  811. // MIDI Input (External)
  812. if (kData->extNotes.mutex.tryLock())
  813. {
  814. while (midiEventCount < MAX_MIDI_EVENTS && ! kData->extNotes.data.isEmpty())
  815. {
  816. const ExternalMidiNote& note(kData->extNotes.data.getFirst(true));
  817. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  818. fMidiEvents[midiEventCount].type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  819. fMidiEvents[midiEventCount].data.note.channel = note.channel;
  820. fMidiEvents[midiEventCount].data.note.note = note.note;
  821. fMidiEvents[midiEventCount].data.note.velocity = note.velo;
  822. midiEventCount += 1;
  823. }
  824. kData->extNotes.mutex.unlock();
  825. } // End of MIDI Input (External)
  826. // ----------------------------------------------------------------------------------------------------
  827. // Event Input (System)
  828. bool allNotesOffSent = false;
  829. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  830. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  831. uint32_t startTime = 0;
  832. uint32_t timeOffset = 0;
  833. uint32_t nextBankId = 0;
  834. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  835. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  836. for (i=0; i < nEvents; ++i)
  837. {
  838. const EngineEvent& event = kData->event.portIn->getEvent(i);
  839. time = event.time;
  840. if (time >= frames)
  841. continue;
  842. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  843. if (time > timeOffset && sampleAccurate)
  844. {
  845. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset, midiEventCount))
  846. {
  847. startTime = 0;
  848. timeOffset = time;
  849. midiEventCount = 0;
  850. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  851. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  852. else
  853. nextBankId = 0;
  854. }
  855. else
  856. startTime += timeOffset;
  857. }
  858. // Control change
  859. switch (event.type)
  860. {
  861. case kEngineEventTypeNull:
  862. break;
  863. case kEngineEventTypeControl:
  864. {
  865. const EngineControlEvent& ctrlEvent = event.ctrl;
  866. switch (ctrlEvent.type)
  867. {
  868. case kEngineControlEventTypeNull:
  869. break;
  870. case kEngineControlEventTypeParameter:
  871. {
  872. // Control backend stuff
  873. if (event.channel == kData->ctrlChannel)
  874. {
  875. float value;
  876. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  877. {
  878. value = ctrlEvent.value;
  879. setDryWet(value, false, false);
  880. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  881. continue;
  882. }
  883. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  884. {
  885. value = ctrlEvent.value*127.0f/100.0f;
  886. setVolume(value, false, false);
  887. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  888. continue;
  889. }
  890. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  891. {
  892. float left, right;
  893. value = ctrlEvent.value/0.5f - 1.0f;
  894. if (value < 0.0f)
  895. {
  896. left = -1.0f;
  897. right = (value*2.0f)+1.0f;
  898. }
  899. else if (value > 0.0f)
  900. {
  901. left = (value*2.0f)-1.0f;
  902. right = 1.0f;
  903. }
  904. else
  905. {
  906. left = -1.0f;
  907. right = 1.0f;
  908. }
  909. setBalanceLeft(left, false, false);
  910. setBalanceRight(right, false, false);
  911. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  912. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  913. continue;
  914. }
  915. }
  916. // Control plugin parameters
  917. for (k=0; k < kData->param.count; ++k)
  918. {
  919. if (kData->param.data[k].midiChannel != event.channel)
  920. continue;
  921. if (kData->param.data[k].midiCC != ctrlEvent.param)
  922. continue;
  923. if (kData->param.data[k].type != PARAMETER_INPUT)
  924. continue;
  925. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  926. continue;
  927. float value;
  928. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  929. {
  930. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  931. }
  932. else
  933. {
  934. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  935. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  936. value = std::rint(value);
  937. }
  938. setParameterValue(k, value, false, false, false);
  939. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  940. }
  941. break;
  942. }
  943. case kEngineControlEventTypeMidiBank:
  944. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  945. nextBankId = ctrlEvent.param;
  946. break;
  947. case kEngineControlEventTypeMidiProgram:
  948. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  949. {
  950. const uint32_t nextProgramId = ctrlEvent.param;
  951. for (k=0; k < kData->midiprog.count; ++k)
  952. {
  953. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  954. {
  955. setMidiProgram(k, false, false, false);
  956. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  957. break;
  958. }
  959. }
  960. }
  961. break;
  962. case kEngineControlEventTypeAllSoundOff:
  963. if (event.channel == kData->ctrlChannel)
  964. {
  965. if (! allNotesOffSent)
  966. {
  967. sendMidiAllNotesOff();
  968. allNotesOffSent = true;
  969. }
  970. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0f);
  971. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0f);
  972. }
  973. if (midiEventCount >= MAX_MIDI_EVENTS)
  974. continue;
  975. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  976. {
  977. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  978. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : time;
  979. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  980. fMidiEvents[midiEventCount].data.control.channel = event.channel;
  981. fMidiEvents[midiEventCount].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  982. midiEventCount += 1;
  983. }
  984. break;
  985. case kEngineControlEventTypeAllNotesOff:
  986. if (event.channel == kData->ctrlChannel)
  987. {
  988. if (! allNotesOffSent)
  989. {
  990. allNotesOffSent = true;
  991. sendMidiAllNotesOff();
  992. }
  993. }
  994. if (midiEventCount >= MAX_MIDI_EVENTS)
  995. continue;
  996. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  997. {
  998. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  999. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : time;
  1000. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  1001. fMidiEvents[midiEventCount].data.control.channel = event.channel;
  1002. fMidiEvents[midiEventCount].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1003. midiEventCount += 1;
  1004. }
  1005. break;
  1006. }
  1007. break;
  1008. }
  1009. case kEngineEventTypeMidi:
  1010. {
  1011. if (midiEventCount >= MAX_MIDI_EVENTS)
  1012. continue;
  1013. const EngineMidiEvent& midiEvent = event.midi;
  1014. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1015. uint8_t channel = event.channel;
  1016. // Fix bad note-off (per DSSI spec)
  1017. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  1018. status -= 0x10;
  1019. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  1020. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : time;
  1021. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1022. {
  1023. const uint8_t note = midiEvent.data[1];
  1024. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEOFF;
  1025. fMidiEvents[midiEventCount].data.note.channel = channel;
  1026. fMidiEvents[midiEventCount].data.note.note = note;
  1027. postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0f);
  1028. }
  1029. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1030. {
  1031. const uint8_t note = midiEvent.data[1];
  1032. const uint8_t velo = midiEvent.data[2];
  1033. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEON;
  1034. fMidiEvents[midiEventCount].data.note.channel = channel;
  1035. fMidiEvents[midiEventCount].data.note.note = note;
  1036. fMidiEvents[midiEventCount].data.note.velocity = velo;
  1037. postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  1038. }
  1039. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) != 0)
  1040. {
  1041. const uint8_t note = midiEvent.data[1];
  1042. const uint8_t pressure = midiEvent.data[2];
  1043. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_KEYPRESS;
  1044. fMidiEvents[midiEventCount].data.note.channel = channel;
  1045. fMidiEvents[midiEventCount].data.note.note = note;
  1046. fMidiEvents[midiEventCount].data.note.velocity = pressure;
  1047. }
  1048. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0)
  1049. {
  1050. const uint8_t control = midiEvent.data[1];
  1051. const uint8_t value = midiEvent.data[2];
  1052. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  1053. fMidiEvents[midiEventCount].data.control.channel = channel;
  1054. fMidiEvents[midiEventCount].data.control.param = control;
  1055. fMidiEvents[midiEventCount].data.control.value = value;
  1056. }
  1057. else if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) != 0)
  1058. {
  1059. const uint8_t pressure = midiEvent.data[1];
  1060. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CHANPRESS;
  1061. fMidiEvents[midiEventCount].data.control.channel = channel;
  1062. fMidiEvents[midiEventCount].data.control.value = pressure;
  1063. }
  1064. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) != 0)
  1065. {
  1066. const uint8_t lsb = midiEvent.data[1];
  1067. const uint8_t msb = midiEvent.data[2];
  1068. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_PITCHBEND;
  1069. fMidiEvents[midiEventCount].data.control.channel = channel;
  1070. fMidiEvents[midiEventCount].data.control.value = ((msb << 7) | lsb) - 8192;
  1071. }
  1072. else
  1073. continue;
  1074. midiEventCount += 1;
  1075. break;
  1076. }
  1077. }
  1078. }
  1079. kData->postRtEvents.trySplice();
  1080. if (frames > timeOffset)
  1081. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset, midiEventCount);
  1082. } // End of Event Input and Processing
  1083. // --------------------------------------------------------------------------------------------------------
  1084. // Plugin processing (no events)
  1085. else
  1086. {
  1087. processSingle(inBuffer, outBuffer, frames, 0, midiEventCount);
  1088. } // End of Plugin processing (no events)
  1089. CARLA_PROCESS_CONTINUE_CHECK;
  1090. // --------------------------------------------------------------------------------------------------------
  1091. // Control Output
  1092. if (kData->event.portOut != nullptr)
  1093. {
  1094. uint8_t channel;
  1095. uint16_t param;
  1096. float value;
  1097. for (k=0; k < kData->param.count; ++k)
  1098. {
  1099. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1100. continue;
  1101. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  1102. if (kData->param.data[k].midiCC > 0)
  1103. {
  1104. channel = kData->param.data[k].midiChannel;
  1105. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  1106. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  1107. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  1108. }
  1109. }
  1110. } // End of Control Output
  1111. }
  1112. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset, const unsigned long midiEventCount)
  1113. {
  1114. CARLA_ASSERT(frames > 0);
  1115. if (frames == 0)
  1116. return false;
  1117. if (kData->audioIn.count > 0)
  1118. {
  1119. CARLA_ASSERT(inBuffer != nullptr);
  1120. if (inBuffer == nullptr)
  1121. return false;
  1122. }
  1123. if (kData->audioOut.count > 0)
  1124. {
  1125. CARLA_ASSERT(outBuffer != nullptr);
  1126. if (outBuffer == nullptr)
  1127. return false;
  1128. }
  1129. uint32_t i, k;
  1130. // --------------------------------------------------------------------------------------------------------
  1131. // Try lock, silence otherwise
  1132. if (kData->engine->isOffline())
  1133. {
  1134. kData->singleMutex.lock();
  1135. }
  1136. else if (! kData->singleMutex.tryLock())
  1137. {
  1138. for (i=0; i < kData->audioOut.count; ++i)
  1139. {
  1140. for (k=0; k < frames; ++k)
  1141. outBuffer[i][k+timeOffset] = 0.0f;
  1142. }
  1143. return false;
  1144. }
  1145. // --------------------------------------------------------------------------------------------------------
  1146. // Reset audio buffers
  1147. for (i=0; i < kData->audioIn.count; ++i)
  1148. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1149. for (i=0; i < kData->audioOut.count; ++i)
  1150. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1151. // --------------------------------------------------------------------------------------------------------
  1152. // Run plugin
  1153. if (fDssiDescriptor->run_synth != nullptr)
  1154. {
  1155. fDssiDescriptor->run_synth(fHandle, frames, fMidiEvents, midiEventCount);
  1156. if (fHandle2 != nullptr)
  1157. fDssiDescriptor->run_synth(fHandle2, frames, fMidiEvents, midiEventCount);
  1158. }
  1159. else if (fDssiDescriptor->run_multiple_synths != nullptr)
  1160. {
  1161. unsigned long instances = (fHandle2 != nullptr) ? 2 : 1;
  1162. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1163. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1164. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1165. fDssiDescriptor->run_multiple_synths(instances, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1166. }
  1167. else
  1168. {
  1169. fDescriptor->run(fHandle, frames);
  1170. if (fHandle2 != nullptr)
  1171. fDescriptor->run(fHandle2, frames);
  1172. }
  1173. // --------------------------------------------------------------------------------------------------------
  1174. // Post-processing (dry/wet, volume and balance)
  1175. {
  1176. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  1177. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1178. bool isPair;
  1179. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1180. for (i=0; i < kData->audioOut.count; ++i)
  1181. {
  1182. // Dry/Wet
  1183. if (doDryWet)
  1184. {
  1185. for (k=0; k < frames; ++k)
  1186. {
  1187. // TODO
  1188. //if (k < kData->latency && kData->latency < frames)
  1189. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  1190. //else
  1191. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1192. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  1193. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1194. }
  1195. }
  1196. // Balance
  1197. if (doBalance)
  1198. {
  1199. isPair = (i % 2 == 0);
  1200. if (isPair)
  1201. {
  1202. CARLA_ASSERT(i+1 < kData->audioOut.count);
  1203. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  1204. }
  1205. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1206. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1207. for (k=0; k < frames; ++k)
  1208. {
  1209. if (isPair)
  1210. {
  1211. // left
  1212. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1213. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1214. }
  1215. else
  1216. {
  1217. // right
  1218. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1219. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1220. }
  1221. }
  1222. }
  1223. // Volume (and buffer copy)
  1224. {
  1225. for (k=0; k < frames; ++k)
  1226. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  1227. }
  1228. }
  1229. #if 0
  1230. // Latency, save values for next callback, TODO
  1231. if (kData->latency > 0 && kData->latency < frames)
  1232. {
  1233. for (i=0; i < kData->audioIn.count; ++i)
  1234. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  1235. }
  1236. #endif
  1237. } // End of Post-processing
  1238. // --------------------------------------------------------------------------------------------------------
  1239. kData->singleMutex.unlock();
  1240. return true;
  1241. }
  1242. void bufferSizeChanged(const uint32_t newBufferSize)
  1243. {
  1244. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1245. carla_debug("DssiPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1246. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1247. {
  1248. if (fAudioInBuffers[i] != nullptr)
  1249. delete[] fAudioInBuffers[i];
  1250. fAudioInBuffers[i] = new float[newBufferSize];
  1251. }
  1252. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1253. {
  1254. if (fAudioOutBuffers[i] != nullptr)
  1255. delete[] fAudioOutBuffers[i];
  1256. fAudioOutBuffers[i] = new float[newBufferSize];
  1257. }
  1258. if (fHandle2 == nullptr)
  1259. {
  1260. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1261. {
  1262. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1263. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1264. }
  1265. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1266. {
  1267. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1268. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1269. }
  1270. }
  1271. else
  1272. {
  1273. if (kData->audioIn.count > 0)
  1274. {
  1275. CARLA_ASSERT(kData->audioIn.count == 2);
  1276. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1277. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1278. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1279. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1280. }
  1281. if (kData->audioOut.count > 0)
  1282. {
  1283. CARLA_ASSERT(kData->audioOut.count == 2);
  1284. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1285. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1286. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1287. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1288. }
  1289. }
  1290. carla_debug("DssiPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1291. }
  1292. void sampleRateChanged(const double newSampleRate)
  1293. {
  1294. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1295. carla_debug("DssiPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1296. // TODO
  1297. (void)newSampleRate;
  1298. carla_debug("DssiPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1299. }
  1300. // -------------------------------------------------------------------
  1301. // Post-poned events
  1302. void uiParameterChange(const uint32_t index, const float value)
  1303. {
  1304. CARLA_ASSERT(index < kData->param.count);
  1305. if (index >= kData->param.count)
  1306. return;
  1307. if (kData->osc.data.target == nullptr)
  1308. return;
  1309. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  1310. }
  1311. void uiMidiProgramChange(const uint32_t index)
  1312. {
  1313. CARLA_ASSERT(index < kData->midiprog.count);
  1314. if (index >= kData->midiprog.count)
  1315. return;
  1316. if (kData->osc.data.target == nullptr)
  1317. return;
  1318. osc_send_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1319. }
  1320. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1321. {
  1322. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1323. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1324. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1325. if (channel >= MAX_MIDI_CHANNELS)
  1326. return;
  1327. if (note >= MAX_MIDI_NOTE)
  1328. return;
  1329. if (velo >= MAX_MIDI_VALUE)
  1330. return;
  1331. if (kData->osc.data.target == nullptr)
  1332. return;
  1333. uint8_t midiData[4] = { 0 };
  1334. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1335. midiData[2] = note;
  1336. midiData[3] = velo;
  1337. osc_send_midi(&kData->osc.data, midiData);
  1338. }
  1339. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1340. {
  1341. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1342. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1343. if (channel >= MAX_MIDI_CHANNELS)
  1344. return;
  1345. if (note >= MAX_MIDI_NOTE)
  1346. return;
  1347. if (kData->osc.data.target == nullptr)
  1348. return;
  1349. uint8_t midiData[4] = { 0 };
  1350. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1351. midiData[2] = note;
  1352. osc_send_midi(&kData->osc.data, midiData);
  1353. }
  1354. // -------------------------------------------------------------------
  1355. // Plugin buffers
  1356. void clearBuffers()
  1357. {
  1358. carla_debug("DssiPlugin::clearBuffers() - start");
  1359. if (fAudioInBuffers != nullptr)
  1360. {
  1361. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1362. {
  1363. if (fAudioInBuffers[i] != nullptr)
  1364. {
  1365. delete[] fAudioInBuffers[i];
  1366. fAudioInBuffers[i] = nullptr;
  1367. }
  1368. }
  1369. delete[] fAudioInBuffers;
  1370. fAudioInBuffers = nullptr;
  1371. }
  1372. if (fAudioOutBuffers != nullptr)
  1373. {
  1374. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1375. {
  1376. if (fAudioOutBuffers[i] != nullptr)
  1377. {
  1378. delete[] fAudioOutBuffers[i];
  1379. fAudioOutBuffers[i] = nullptr;
  1380. }
  1381. }
  1382. delete[] fAudioOutBuffers;
  1383. fAudioOutBuffers = nullptr;
  1384. }
  1385. if (fParamBuffers != nullptr)
  1386. {
  1387. delete[] fParamBuffers;
  1388. fParamBuffers = nullptr;
  1389. }
  1390. CarlaPlugin::clearBuffers();
  1391. carla_debug("DssiPlugin::clearBuffers() - end");
  1392. }
  1393. // -------------------------------------------------------------------
  1394. bool init(const char* const filename, const char* const name, const char* const label, const char* const guiFilename)
  1395. {
  1396. CARLA_ASSERT(kData->engine != nullptr);
  1397. CARLA_ASSERT(kData->client == nullptr);
  1398. CARLA_ASSERT(filename != nullptr);
  1399. CARLA_ASSERT(label != nullptr);
  1400. // ---------------------------------------------------------------
  1401. // first checks
  1402. if (kData->engine == nullptr)
  1403. {
  1404. return false;
  1405. }
  1406. if (kData->client != nullptr)
  1407. {
  1408. kData->engine->setLastError("Plugin client is already registered");
  1409. return false;
  1410. }
  1411. if (filename == nullptr)
  1412. {
  1413. kData->engine->setLastError("null filename");
  1414. return false;
  1415. }
  1416. if (label == nullptr)
  1417. {
  1418. kData->engine->setLastError("null label");
  1419. return false;
  1420. }
  1421. // ---------------------------------------------------------------
  1422. // open DLL
  1423. if (! kData->libOpen(filename))
  1424. {
  1425. kData->engine->setLastError(kData->libError(filename));
  1426. return false;
  1427. }
  1428. // ---------------------------------------------------------------
  1429. // get DLL main entry
  1430. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)kData->libSymbol("dssi_descriptor");
  1431. if (descFn == nullptr)
  1432. {
  1433. kData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1434. return false;
  1435. }
  1436. // ---------------------------------------------------------------
  1437. // get descriptor that matches label
  1438. unsigned long i = 0;
  1439. while ((fDssiDescriptor = descFn(i++)) != nullptr)
  1440. {
  1441. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1442. if (fDescriptor != nullptr && fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1443. break;
  1444. }
  1445. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1446. {
  1447. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1448. return false;
  1449. }
  1450. // ---------------------------------------------------------------
  1451. // get info
  1452. if (name != nullptr)
  1453. fName = kData->engine->getNewUniquePluginName(name);
  1454. else if (fDescriptor->Name != nullptr)
  1455. fName = kData->engine->getNewUniquePluginName(fDescriptor->Name);
  1456. else
  1457. fName = kData->engine->getNewUniquePluginName(fDescriptor->Label);
  1458. fFilename = filename;
  1459. // ---------------------------------------------------------------
  1460. // register client
  1461. kData->client = kData->engine->addClient(this);
  1462. if (kData->client == nullptr || ! kData->client->isOk())
  1463. {
  1464. kData->engine->setLastError("Failed to register plugin client");
  1465. return false;
  1466. }
  1467. // ---------------------------------------------------------------
  1468. // initialize plugin
  1469. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)kData->engine->getSampleRate());
  1470. if (fHandle == nullptr)
  1471. {
  1472. kData->engine->setLastError("Plugin failed to initialize");
  1473. return false;
  1474. }
  1475. // ---------------------------------------------------------------
  1476. // gui stuff
  1477. if (guiFilename != nullptr)
  1478. {
  1479. kData->osc.thread.setOscData(guiFilename, fDescriptor->Label);
  1480. fHints |= PLUGIN_HAS_GUI;
  1481. }
  1482. // ---------------------------------------------------------------
  1483. // load plugin settings
  1484. {
  1485. #ifdef __USE_GNU
  1486. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  1487. #else
  1488. const bool isDssiVst = fFilename.contains("dssi-vst");
  1489. #endif
  1490. // set default options
  1491. fOptions = 0x0;
  1492. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1493. if (kData->engine->getOptions().forceStereo)
  1494. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1495. if (isDssiVst)
  1496. {
  1497. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1498. if (kData->engine->getOptions().useDssiVstChunks && fDssiDescriptor->get_custom_data != nullptr && fDssiDescriptor->set_custom_data != nullptr)
  1499. fOptions |= PLUGIN_OPTION_USE_CHUNKS;
  1500. }
  1501. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  1502. {
  1503. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1504. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1505. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1506. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1507. if (fDssiDescriptor->run_synth == nullptr)
  1508. carla_stderr2("Plugin can ONLY use run_multiple_synths!");
  1509. }
  1510. // load settings
  1511. kData->idStr = "DSSI/";
  1512. kData->idStr += std::strrchr(filename, OS_SEP)+1;
  1513. kData->idStr += "/";
  1514. kData->idStr += label;
  1515. fOptions = kData->loadSettings(fOptions, availableOptions());
  1516. // ignore settings, we need this anyway
  1517. if (isDssiVst)
  1518. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1519. }
  1520. return true;
  1521. }
  1522. private:
  1523. LADSPA_Handle fHandle;
  1524. LADSPA_Handle fHandle2;
  1525. const LADSPA_Descriptor* fDescriptor;
  1526. const DSSI_Descriptor* fDssiDescriptor;
  1527. float** fAudioInBuffers;
  1528. float** fAudioOutBuffers;
  1529. float* fParamBuffers;
  1530. snd_seq_event_t fMidiEvents[MAX_MIDI_EVENTS];
  1531. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(DssiPlugin)
  1532. };
  1533. CARLA_BACKEND_END_NAMESPACE
  1534. #else // WANT_DSSI
  1535. # warning Building without DSSI support
  1536. #endif
  1537. CARLA_BACKEND_START_NAMESPACE
  1538. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init, const char* const guiFilename)
  1539. {
  1540. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\"}, \"%s\")", init.engine, init.filename, init.name, init.label, guiFilename);
  1541. #ifdef WANT_DSSI
  1542. DssiPlugin* const plugin(new DssiPlugin(init.engine, init.id));
  1543. if (! plugin->init(init.filename, init.name, init.label, guiFilename))
  1544. {
  1545. delete plugin;
  1546. return nullptr;
  1547. }
  1548. plugin->reload();
  1549. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1550. {
  1551. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1552. delete plugin;
  1553. return nullptr;
  1554. }
  1555. return plugin;
  1556. #else
  1557. init.engine->setLastError("DSSI support not available");
  1558. return nullptr;
  1559. #endif
  1560. }
  1561. CARLA_BACKEND_END_NAMESPACE