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.

1970 lines
68KB

  1. /*
  2. * Carla DSSI Plugin
  3. * Copyright (C) 2011-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #ifdef WANT_DSSI
  19. #include "CarlaLadspaUtils.hpp"
  20. #include "dssi/dssi.h"
  21. CARLA_BACKEND_START_NAMESPACE
  22. #if 0
  23. }
  24. #endif
  25. class DssiPlugin : public CarlaPlugin
  26. {
  27. public:
  28. DssiPlugin(CarlaEngine* const engine, const unsigned int id)
  29. : CarlaPlugin(engine, id),
  30. fHandle(nullptr),
  31. fHandle2(nullptr),
  32. fDescriptor(nullptr),
  33. fDssiDescriptor(nullptr),
  34. fAudioInBuffers(nullptr),
  35. fAudioOutBuffers(nullptr),
  36. fParamBuffers(nullptr),
  37. 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 && note.channel < MAX_MIDI_CHANNELS);
  830. fMidiEvents[midiEventCount].type = (note.velo > 0) ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  831. fMidiEvents[midiEventCount].data.note.channel = note.channel;
  832. fMidiEvents[midiEventCount].data.note.note = note.note;
  833. fMidiEvents[midiEventCount].data.note.velocity = note.velo;
  834. midiEventCount += 1;
  835. }
  836. kData->extNotes.mutex.unlock();
  837. } // End of MIDI Input (External)
  838. // ----------------------------------------------------------------------------------------------------
  839. // Event Input (System)
  840. bool allNotesOffSent = false;
  841. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  842. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  843. uint32_t startTime = 0;
  844. uint32_t timeOffset = 0;
  845. uint32_t nextBankId = 0;
  846. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  847. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  848. for (i=0; i < nEvents; ++i)
  849. {
  850. const EngineEvent& event = kData->event.portIn->getEvent(i);
  851. time = event.time;
  852. if (time >= frames)
  853. continue;
  854. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  855. if (time > timeOffset && sampleAccurate)
  856. {
  857. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset, midiEventCount))
  858. {
  859. startTime = 0;
  860. timeOffset = time;
  861. midiEventCount = 0;
  862. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  863. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  864. else
  865. nextBankId = 0;
  866. }
  867. else
  868. startTime += timeOffset;
  869. }
  870. // Control change
  871. switch (event.type)
  872. {
  873. case kEngineEventTypeNull:
  874. break;
  875. case kEngineEventTypeControl:
  876. {
  877. const EngineControlEvent& ctrlEvent = event.ctrl;
  878. switch (ctrlEvent.type)
  879. {
  880. case kEngineControlEventTypeNull:
  881. break;
  882. case kEngineControlEventTypeParameter:
  883. {
  884. // Control backend stuff
  885. if (event.channel == kData->ctrlChannel)
  886. {
  887. float value;
  888. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  889. {
  890. value = ctrlEvent.value;
  891. setDryWet(value, false, false);
  892. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  893. continue;
  894. }
  895. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  896. {
  897. value = ctrlEvent.value*127.0f/100.0f;
  898. setVolume(value, false, false);
  899. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  900. continue;
  901. }
  902. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  903. {
  904. float left, right;
  905. value = ctrlEvent.value/0.5f - 1.0f;
  906. if (value < 0.0f)
  907. {
  908. left = -1.0f;
  909. right = (value*2.0f)+1.0f;
  910. }
  911. else if (value > 0.0f)
  912. {
  913. left = (value*2.0f)-1.0f;
  914. right = 1.0f;
  915. }
  916. else
  917. {
  918. left = -1.0f;
  919. right = 1.0f;
  920. }
  921. setBalanceLeft(left, false, false);
  922. setBalanceRight(right, false, false);
  923. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  924. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  925. continue;
  926. }
  927. }
  928. // Control plugin parameters
  929. for (k=0; k < kData->param.count; ++k)
  930. {
  931. if (kData->param.data[k].midiChannel != event.channel)
  932. continue;
  933. if (kData->param.data[k].midiCC != ctrlEvent.param)
  934. continue;
  935. if (kData->param.data[k].type != PARAMETER_INPUT)
  936. continue;
  937. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  938. continue;
  939. float value;
  940. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  941. {
  942. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  943. }
  944. else
  945. {
  946. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  947. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  948. value = std::rint(value);
  949. }
  950. setParameterValue(k, value, false, false, false);
  951. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  952. }
  953. break;
  954. }
  955. case kEngineControlEventTypeMidiBank:
  956. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  957. nextBankId = ctrlEvent.param;
  958. break;
  959. case kEngineControlEventTypeMidiProgram:
  960. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  961. {
  962. const uint32_t nextProgramId = ctrlEvent.param;
  963. for (k=0; k < kData->midiprog.count; ++k)
  964. {
  965. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  966. {
  967. setMidiProgram(k, false, false, false);
  968. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  969. break;
  970. }
  971. }
  972. }
  973. break;
  974. case kEngineControlEventTypeAllSoundOff:
  975. if (event.channel == kData->ctrlChannel)
  976. {
  977. if (! allNotesOffSent)
  978. {
  979. sendMidiAllNotesOff();
  980. allNotesOffSent = true;
  981. }
  982. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0f);
  983. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0f);
  984. }
  985. if (midiEventCount >= MAX_MIDI_EVENTS)
  986. continue;
  987. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  988. {
  989. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  990. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : time;
  991. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  992. fMidiEvents[midiEventCount].data.control.channel = event.channel;
  993. fMidiEvents[midiEventCount].data.control.param = MIDI_CONTROL_ALL_SOUND_OFF;
  994. midiEventCount += 1;
  995. }
  996. break;
  997. case kEngineControlEventTypeAllNotesOff:
  998. if (event.channel == kData->ctrlChannel)
  999. {
  1000. if (! allNotesOffSent)
  1001. {
  1002. allNotesOffSent = true;
  1003. sendMidiAllNotesOff();
  1004. }
  1005. }
  1006. if (midiEventCount >= MAX_MIDI_EVENTS)
  1007. continue;
  1008. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1009. {
  1010. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  1011. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : time;
  1012. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  1013. fMidiEvents[midiEventCount].data.control.channel = event.channel;
  1014. fMidiEvents[midiEventCount].data.control.param = MIDI_CONTROL_ALL_NOTES_OFF;
  1015. midiEventCount += 1;
  1016. }
  1017. break;
  1018. }
  1019. break;
  1020. }
  1021. case kEngineEventTypeMidi:
  1022. {
  1023. if (midiEventCount >= MAX_MIDI_EVENTS)
  1024. continue;
  1025. const EngineMidiEvent& midiEvent = event.midi;
  1026. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1027. uint8_t channel = event.channel;
  1028. // Fix bad note-off (per DSSI spec)
  1029. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  1030. status -= 0x10;
  1031. carla_zeroStruct<snd_seq_event_t>(fMidiEvents[midiEventCount]);
  1032. fMidiEvents[midiEventCount].time.tick = sampleAccurate ? startTime : time;
  1033. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1034. {
  1035. const uint8_t note = midiEvent.data[1];
  1036. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEOFF;
  1037. fMidiEvents[midiEventCount].data.note.channel = channel;
  1038. fMidiEvents[midiEventCount].data.note.note = note;
  1039. postponeRtEvent(kPluginPostRtEventNoteOff, channel, note, 0.0f);
  1040. }
  1041. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1042. {
  1043. const uint8_t note = midiEvent.data[1];
  1044. const uint8_t velo = midiEvent.data[2];
  1045. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_NOTEON;
  1046. fMidiEvents[midiEventCount].data.note.channel = channel;
  1047. fMidiEvents[midiEventCount].data.note.note = note;
  1048. fMidiEvents[midiEventCount].data.note.velocity = velo;
  1049. postponeRtEvent(kPluginPostRtEventNoteOn, channel, note, velo);
  1050. }
  1051. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) != 0)
  1052. {
  1053. const uint8_t note = midiEvent.data[1];
  1054. const uint8_t pressure = midiEvent.data[2];
  1055. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_KEYPRESS;
  1056. fMidiEvents[midiEventCount].data.note.channel = channel;
  1057. fMidiEvents[midiEventCount].data.note.note = note;
  1058. fMidiEvents[midiEventCount].data.note.velocity = pressure;
  1059. }
  1060. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0)
  1061. {
  1062. const uint8_t control = midiEvent.data[1];
  1063. const uint8_t value = midiEvent.data[2];
  1064. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CONTROLLER;
  1065. fMidiEvents[midiEventCount].data.control.channel = channel;
  1066. fMidiEvents[midiEventCount].data.control.param = control;
  1067. fMidiEvents[midiEventCount].data.control.value = value;
  1068. }
  1069. else if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) != 0)
  1070. {
  1071. const uint8_t pressure = midiEvent.data[1];
  1072. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_CHANPRESS;
  1073. fMidiEvents[midiEventCount].data.control.channel = channel;
  1074. fMidiEvents[midiEventCount].data.control.value = pressure;
  1075. }
  1076. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) != 0)
  1077. {
  1078. const uint8_t lsb = midiEvent.data[1];
  1079. const uint8_t msb = midiEvent.data[2];
  1080. fMidiEvents[midiEventCount].type = SND_SEQ_EVENT_PITCHBEND;
  1081. fMidiEvents[midiEventCount].data.control.channel = channel;
  1082. fMidiEvents[midiEventCount].data.control.value = ((msb << 7) | lsb) - 8192;
  1083. }
  1084. else
  1085. continue;
  1086. midiEventCount += 1;
  1087. break;
  1088. }
  1089. }
  1090. }
  1091. kData->postRtEvents.trySplice();
  1092. if (frames > timeOffset)
  1093. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset, midiEventCount);
  1094. } // End of Event Input and Processing
  1095. // --------------------------------------------------------------------------------------------------------
  1096. // Plugin processing (no events)
  1097. else
  1098. {
  1099. processSingle(inBuffer, outBuffer, frames, 0, midiEventCount);
  1100. } // End of Plugin processing (no events)
  1101. CARLA_PROCESS_CONTINUE_CHECK;
  1102. // --------------------------------------------------------------------------------------------------------
  1103. // Control Output
  1104. if (kData->event.portOut != nullptr)
  1105. {
  1106. uint8_t channel;
  1107. uint16_t param;
  1108. float value;
  1109. for (k=0; k < kData->param.count; ++k)
  1110. {
  1111. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1112. continue;
  1113. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  1114. if (kData->param.data[k].midiCC > 0)
  1115. {
  1116. channel = kData->param.data[k].midiChannel;
  1117. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  1118. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  1119. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  1120. }
  1121. }
  1122. } // End of Control Output
  1123. }
  1124. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset, const unsigned long midiEventCount)
  1125. {
  1126. CARLA_ASSERT(frames > 0);
  1127. if (frames == 0)
  1128. return false;
  1129. if (kData->audioIn.count > 0)
  1130. {
  1131. CARLA_ASSERT(inBuffer != nullptr);
  1132. if (inBuffer == nullptr)
  1133. return false;
  1134. }
  1135. if (kData->audioOut.count > 0)
  1136. {
  1137. CARLA_ASSERT(outBuffer != nullptr);
  1138. if (outBuffer == nullptr)
  1139. return false;
  1140. }
  1141. uint32_t i, k;
  1142. // --------------------------------------------------------------------------------------------------------
  1143. // Try lock, silence otherwise
  1144. if (kData->engine->isOffline())
  1145. {
  1146. kData->singleMutex.lock();
  1147. }
  1148. else if (! kData->singleMutex.tryLock())
  1149. {
  1150. for (i=0; i < kData->audioOut.count; ++i)
  1151. {
  1152. for (k=0; k < frames; ++k)
  1153. outBuffer[i][k+timeOffset] = 0.0f;
  1154. }
  1155. return false;
  1156. }
  1157. // --------------------------------------------------------------------------------------------------------
  1158. // Reset audio buffers
  1159. for (i=0; i < kData->audioIn.count; ++i)
  1160. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1161. for (i=0; i < kData->audioOut.count; ++i)
  1162. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1163. // --------------------------------------------------------------------------------------------------------
  1164. // Run plugin
  1165. if (fDssiDescriptor->run_synth != nullptr)
  1166. {
  1167. fDssiDescriptor->run_synth(fHandle, frames, fMidiEvents, midiEventCount);
  1168. if (fHandle2 != nullptr)
  1169. fDssiDescriptor->run_synth(fHandle2, frames, fMidiEvents, midiEventCount);
  1170. }
  1171. else if (fDssiDescriptor->run_multiple_synths != nullptr)
  1172. {
  1173. unsigned long instances = (fHandle2 != nullptr) ? 2 : 1;
  1174. LADSPA_Handle handlePtr[2] = { fHandle, fHandle2 };
  1175. snd_seq_event_t* midiEventsPtr[2] = { fMidiEvents, fMidiEvents };
  1176. unsigned long midiEventCountPtr[2] = { midiEventCount, midiEventCount };
  1177. fDssiDescriptor->run_multiple_synths(instances, handlePtr, frames, midiEventsPtr, midiEventCountPtr);
  1178. }
  1179. else
  1180. {
  1181. fDescriptor->run(fHandle, frames);
  1182. if (fHandle2 != nullptr)
  1183. fDescriptor->run(fHandle2, frames);
  1184. }
  1185. // --------------------------------------------------------------------------------------------------------
  1186. // Post-processing (dry/wet, volume and balance)
  1187. {
  1188. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  1189. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1190. bool isPair;
  1191. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1192. for (i=0; i < kData->audioOut.count; ++i)
  1193. {
  1194. // Dry/Wet
  1195. if (doDryWet)
  1196. {
  1197. for (k=0; k < frames; ++k)
  1198. {
  1199. // TODO
  1200. //if (k < kData->latency && kData->latency < frames)
  1201. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  1202. //else
  1203. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  1204. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  1205. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1206. }
  1207. }
  1208. // Balance
  1209. if (doBalance)
  1210. {
  1211. isPair = (i % 2 == 0);
  1212. if (isPair)
  1213. {
  1214. CARLA_ASSERT(i+1 < kData->audioOut.count);
  1215. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  1216. }
  1217. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1218. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1219. for (k=0; k < frames; ++k)
  1220. {
  1221. if (isPair)
  1222. {
  1223. // left
  1224. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1225. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1226. }
  1227. else
  1228. {
  1229. // right
  1230. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1231. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1232. }
  1233. }
  1234. }
  1235. // Volume (and buffer copy)
  1236. {
  1237. for (k=0; k < frames; ++k)
  1238. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  1239. }
  1240. }
  1241. #if 0
  1242. // Latency, save values for next callback, TODO
  1243. if (kData->latency > 0 && kData->latency < frames)
  1244. {
  1245. for (i=0; i < kData->audioIn.count; ++i)
  1246. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  1247. }
  1248. #endif
  1249. } // End of Post-processing
  1250. // --------------------------------------------------------------------------------------------------------
  1251. kData->singleMutex.unlock();
  1252. return true;
  1253. }
  1254. void bufferSizeChanged(const uint32_t newBufferSize)
  1255. {
  1256. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1257. carla_debug("DssiPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1258. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1259. {
  1260. if (fAudioInBuffers[i] != nullptr)
  1261. delete[] fAudioInBuffers[i];
  1262. fAudioInBuffers[i] = new float[newBufferSize];
  1263. }
  1264. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1265. {
  1266. if (fAudioOutBuffers[i] != nullptr)
  1267. delete[] fAudioOutBuffers[i];
  1268. fAudioOutBuffers[i] = new float[newBufferSize];
  1269. }
  1270. if (fHandle2 == nullptr)
  1271. {
  1272. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1273. {
  1274. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1275. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1276. }
  1277. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1278. {
  1279. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1280. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1281. }
  1282. }
  1283. else
  1284. {
  1285. if (kData->audioIn.count > 0)
  1286. {
  1287. CARLA_ASSERT(kData->audioIn.count == 2);
  1288. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1289. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1290. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1291. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1292. }
  1293. if (kData->audioOut.count > 0)
  1294. {
  1295. CARLA_ASSERT(kData->audioOut.count == 2);
  1296. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1297. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1298. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1299. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1300. }
  1301. }
  1302. carla_debug("DssiPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  1303. }
  1304. void sampleRateChanged(const double newSampleRate)
  1305. {
  1306. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1307. carla_debug("DssiPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1308. // TODO
  1309. (void)newSampleRate;
  1310. carla_debug("DssiPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1311. }
  1312. // -------------------------------------------------------------------
  1313. // Post-poned events
  1314. void uiParameterChange(const uint32_t index, const float value)
  1315. {
  1316. CARLA_ASSERT(index < kData->param.count);
  1317. if (index >= kData->param.count)
  1318. return;
  1319. if (kData->osc.data.target == nullptr)
  1320. return;
  1321. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  1322. }
  1323. void uiMidiProgramChange(const uint32_t index)
  1324. {
  1325. CARLA_ASSERT(index < kData->midiprog.count);
  1326. if (index >= kData->midiprog.count)
  1327. return;
  1328. if (kData->osc.data.target == nullptr)
  1329. return;
  1330. osc_send_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1331. }
  1332. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1333. {
  1334. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1335. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1336. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1337. if (channel >= MAX_MIDI_CHANNELS)
  1338. return;
  1339. if (note >= MAX_MIDI_NOTE)
  1340. return;
  1341. if (velo >= MAX_MIDI_VALUE)
  1342. return;
  1343. if (kData->osc.data.target == nullptr)
  1344. return;
  1345. uint8_t midiData[4] = { 0 };
  1346. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  1347. midiData[2] = note;
  1348. midiData[3] = velo;
  1349. osc_send_midi(&kData->osc.data, midiData);
  1350. }
  1351. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1352. {
  1353. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1354. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1355. if (channel >= MAX_MIDI_CHANNELS)
  1356. return;
  1357. if (note >= MAX_MIDI_NOTE)
  1358. return;
  1359. if (kData->osc.data.target == nullptr)
  1360. return;
  1361. uint8_t midiData[4] = { 0 };
  1362. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  1363. midiData[2] = note;
  1364. osc_send_midi(&kData->osc.data, midiData);
  1365. }
  1366. // -------------------------------------------------------------------
  1367. // Plugin buffers
  1368. void clearBuffers()
  1369. {
  1370. carla_debug("DssiPlugin::clearBuffers() - start");
  1371. if (fAudioInBuffers != nullptr)
  1372. {
  1373. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1374. {
  1375. if (fAudioInBuffers[i] != nullptr)
  1376. {
  1377. delete[] fAudioInBuffers[i];
  1378. fAudioInBuffers[i] = nullptr;
  1379. }
  1380. }
  1381. delete[] fAudioInBuffers;
  1382. fAudioInBuffers = nullptr;
  1383. }
  1384. if (fAudioOutBuffers != nullptr)
  1385. {
  1386. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1387. {
  1388. if (fAudioOutBuffers[i] != nullptr)
  1389. {
  1390. delete[] fAudioOutBuffers[i];
  1391. fAudioOutBuffers[i] = nullptr;
  1392. }
  1393. }
  1394. delete[] fAudioOutBuffers;
  1395. fAudioOutBuffers = nullptr;
  1396. }
  1397. if (fParamBuffers != nullptr)
  1398. {
  1399. delete[] fParamBuffers;
  1400. fParamBuffers = nullptr;
  1401. }
  1402. CarlaPlugin::clearBuffers();
  1403. carla_debug("DssiPlugin::clearBuffers() - end");
  1404. }
  1405. // -------------------------------------------------------------------
  1406. bool init(const char* const filename, const char* const name, const char* const label, const char* const guiFilename)
  1407. {
  1408. CARLA_ASSERT(kData->engine != nullptr);
  1409. CARLA_ASSERT(kData->client == nullptr);
  1410. CARLA_ASSERT(filename != nullptr);
  1411. CARLA_ASSERT(label != nullptr);
  1412. // ---------------------------------------------------------------
  1413. // first checks
  1414. if (kData->engine == nullptr)
  1415. {
  1416. return false;
  1417. }
  1418. if (kData->client != nullptr)
  1419. {
  1420. kData->engine->setLastError("Plugin client is already registered");
  1421. return false;
  1422. }
  1423. if (filename == nullptr)
  1424. {
  1425. kData->engine->setLastError("null filename");
  1426. return false;
  1427. }
  1428. if (label == nullptr)
  1429. {
  1430. kData->engine->setLastError("null label");
  1431. return false;
  1432. }
  1433. // ---------------------------------------------------------------
  1434. // open DLL
  1435. if (! kData->libOpen(filename))
  1436. {
  1437. kData->engine->setLastError(kData->libError(filename));
  1438. return false;
  1439. }
  1440. // ---------------------------------------------------------------
  1441. // get DLL main entry
  1442. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)kData->libSymbol("dssi_descriptor");
  1443. if (descFn == nullptr)
  1444. {
  1445. kData->engine->setLastError("Could not find the DSSI Descriptor in the plugin library");
  1446. return false;
  1447. }
  1448. // ---------------------------------------------------------------
  1449. // get descriptor that matches label
  1450. unsigned long i = 0;
  1451. while ((fDssiDescriptor = descFn(i++)) != nullptr)
  1452. {
  1453. fDescriptor = fDssiDescriptor->LADSPA_Plugin;
  1454. if (fDescriptor != nullptr && fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1455. break;
  1456. }
  1457. if (fDescriptor == nullptr || fDssiDescriptor == nullptr)
  1458. {
  1459. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1460. return false;
  1461. }
  1462. // ---------------------------------------------------------------
  1463. // get info
  1464. if (name != nullptr)
  1465. fName = kData->engine->getNewUniquePluginName(name);
  1466. else if (fDescriptor->Name != nullptr)
  1467. fName = kData->engine->getNewUniquePluginName(fDescriptor->Name);
  1468. else
  1469. fName = kData->engine->getNewUniquePluginName(fDescriptor->Label);
  1470. fFilename = filename;
  1471. // ---------------------------------------------------------------
  1472. // register client
  1473. kData->client = kData->engine->addClient(this);
  1474. if (kData->client == nullptr || ! kData->client->isOk())
  1475. {
  1476. kData->engine->setLastError("Failed to register plugin client");
  1477. return false;
  1478. }
  1479. // ---------------------------------------------------------------
  1480. // initialize plugin
  1481. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)kData->engine->getSampleRate());
  1482. if (fHandle == nullptr)
  1483. {
  1484. kData->engine->setLastError("Plugin failed to initialize");
  1485. return false;
  1486. }
  1487. // ---------------------------------------------------------------
  1488. // gui stuff
  1489. if (guiFilename != nullptr)
  1490. {
  1491. kData->osc.thread.setOscData(guiFilename, fDescriptor->Label);
  1492. fHints |= PLUGIN_HAS_GUI;
  1493. }
  1494. // ---------------------------------------------------------------
  1495. // load plugin settings
  1496. {
  1497. #ifdef __USE_GNU
  1498. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  1499. #else
  1500. const bool isDssiVst = fFilename.contains("dssi-vst");
  1501. #endif
  1502. // set default options
  1503. fOptions = 0x0;
  1504. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1505. if (kData->engine->getOptions().forceStereo)
  1506. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1507. if (isDssiVst)
  1508. {
  1509. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1510. if (kData->engine->getOptions().useDssiVstChunks && fDssiDescriptor->get_custom_data != nullptr && fDssiDescriptor->set_custom_data != nullptr)
  1511. fOptions |= PLUGIN_OPTION_USE_CHUNKS;
  1512. }
  1513. if (fDssiDescriptor->run_synth != nullptr || fDssiDescriptor->run_multiple_synths != nullptr)
  1514. {
  1515. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1516. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1517. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1518. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1519. if (fDssiDescriptor->run_synth == nullptr)
  1520. carla_stderr2("Plugin can ONLY use run_multiple_synths!");
  1521. }
  1522. // load settings
  1523. kData->idStr = "DSSI/";
  1524. kData->idStr += std::strrchr(filename, OS_SEP)+1;
  1525. kData->idStr += "/";
  1526. kData->idStr += label;
  1527. fOptions = kData->loadSettings(fOptions, availableOptions());
  1528. // ignore settings, we need this anyway
  1529. if (isDssiVst)
  1530. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1531. }
  1532. return true;
  1533. }
  1534. private:
  1535. LADSPA_Handle fHandle;
  1536. LADSPA_Handle fHandle2;
  1537. const LADSPA_Descriptor* fDescriptor;
  1538. const DSSI_Descriptor* fDssiDescriptor;
  1539. float** fAudioInBuffers;
  1540. float** fAudioOutBuffers;
  1541. float* fParamBuffers;
  1542. uint8_t* fLastChunk;
  1543. snd_seq_event_t fMidiEvents[MAX_MIDI_EVENTS];
  1544. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(DssiPlugin)
  1545. };
  1546. CARLA_BACKEND_END_NAMESPACE
  1547. #else // WANT_DSSI
  1548. # warning Building without DSSI support
  1549. #endif
  1550. CARLA_BACKEND_START_NAMESPACE
  1551. CarlaPlugin* CarlaPlugin::newDSSI(const Initializer& init, const char* const guiFilename)
  1552. {
  1553. carla_debug("CarlaPlugin::newDSSI({%p, \"%s\", \"%s\", \"%s\"}, \"%s\")", init.engine, init.filename, init.name, init.label, guiFilename);
  1554. #ifdef WANT_DSSI
  1555. DssiPlugin* const plugin(new DssiPlugin(init.engine, init.id));
  1556. if (! plugin->init(init.filename, init.name, init.label, guiFilename))
  1557. {
  1558. delete plugin;
  1559. return nullptr;
  1560. }
  1561. plugin->reload();
  1562. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1563. {
  1564. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo DSSI plugins, sorry!");
  1565. delete plugin;
  1566. return nullptr;
  1567. }
  1568. return plugin;
  1569. #else
  1570. init.engine->setLastError("DSSI support not available");
  1571. return nullptr;
  1572. #endif
  1573. }
  1574. CARLA_BACKEND_END_NAMESPACE