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.

1852 lines
65KB

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