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.

1853 lines
66KB

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