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.

1982 lines
69KB

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