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.

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