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.

2747 lines
90KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2022 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 doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaBase64Utils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include "CarlaMIDI.h"
  23. #include "CarlaPluginUI.hpp"
  24. #include "CarlaScopeUtils.hpp"
  25. #include "CarlaStringList.hpp"
  26. #include <ctime>
  27. #include "water/files/File.h"
  28. #include "water/streams/MemoryOutputStream.h"
  29. #include "water/xml/XmlDocument.h"
  30. #include "water/xml/XmlElement.h"
  31. using water::CharPointer_UTF8;
  32. using water::File;
  33. using water::MemoryOutputStream;
  34. using water::Result;
  35. using water::String;
  36. using water::XmlDocument;
  37. using water::XmlElement;
  38. CARLA_BACKEND_START_NAMESPACE
  39. // -------------------------------------------------------------------------------------------------------------------
  40. // Fallback data
  41. static const ParameterData kParameterDataNull = { PARAMETER_UNKNOWN, 0x0, PARAMETER_NULL, -1, 0, CONTROL_INDEX_NONE, 0.0f, 1.0f, 0x0 };
  42. static const ParameterRanges kParameterRangesNull = { 0.0f, 0.0f, 1.0f, 0.01f, 0.0001f, 0.1f };
  43. static const MidiProgramData kMidiProgramDataNull = { 0, 0, nullptr };
  44. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  45. static /* */ CustomData kCustomDataFallbackNC = { nullptr, nullptr, nullptr };
  46. static const PluginPostRtEvent kPluginPostRtEventFallback = { kPluginPostRtEventNull, false, {} };
  47. // -------------------------------------------------------------------------------------------------------------------
  48. // ParamSymbol struct, needed for CarlaPlugin::loadStateSave()
  49. struct ParamSymbol {
  50. int32_t index;
  51. const char* symbol;
  52. ParamSymbol(const uint32_t i, const char* const s)
  53. : index(static_cast<int32_t>(i)),
  54. symbol(carla_strdup(s)) {}
  55. ~ParamSymbol() noexcept
  56. {
  57. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr,)
  58. delete[] symbol;
  59. symbol = nullptr;
  60. }
  61. #ifdef CARLA_PROPER_CPP11_SUPPORT
  62. ParamSymbol() = delete;
  63. CARLA_DECLARE_NON_COPYABLE(ParamSymbol)
  64. #endif
  65. };
  66. // -------------------------------------------------------------------------------------------------------------------
  67. // Constructor and destructor
  68. CarlaPlugin::CarlaPlugin(CarlaEngine* const engine, const uint id)
  69. : pData(new ProtectedData(engine, id))
  70. {
  71. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  72. CARLA_SAFE_ASSERT(id < engine->getMaxPluginNumber());
  73. carla_debug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
  74. switch (engine->getProccessMode())
  75. {
  76. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  77. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  78. CARLA_SAFE_ASSERT(id < MAX_DEFAULT_PLUGINS);
  79. break;
  80. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  81. CARLA_SAFE_ASSERT(id < MAX_RACK_PLUGINS);
  82. break;
  83. case ENGINE_PROCESS_MODE_PATCHBAY:
  84. CARLA_SAFE_ASSERT(id < MAX_PATCHBAY_PLUGINS);
  85. break;
  86. case ENGINE_PROCESS_MODE_BRIDGE:
  87. CARLA_SAFE_ASSERT(id == 0);
  88. break;
  89. }
  90. }
  91. CarlaPlugin::~CarlaPlugin()
  92. {
  93. carla_debug("CarlaPlugin::~CarlaPlugin()");
  94. delete pData;
  95. }
  96. // -------------------------------------------------------------------
  97. // Information (base)
  98. uint CarlaPlugin::getId() const noexcept
  99. {
  100. return pData->id;
  101. }
  102. uint CarlaPlugin::getHints() const noexcept
  103. {
  104. return pData->hints;
  105. }
  106. uint CarlaPlugin::getOptionsEnabled() const noexcept
  107. {
  108. return pData->options;
  109. }
  110. bool CarlaPlugin::isEnabled() const noexcept
  111. {
  112. return pData->enabled;
  113. }
  114. const char* CarlaPlugin::getName() const noexcept
  115. {
  116. return pData->name;
  117. }
  118. const char* CarlaPlugin::getFilename() const noexcept
  119. {
  120. return pData->filename;
  121. }
  122. const char* CarlaPlugin::getIconName() const noexcept
  123. {
  124. return pData->iconName;
  125. }
  126. PluginCategory CarlaPlugin::getCategory() const noexcept
  127. {
  128. return getPluginCategoryFromName(pData->name);
  129. }
  130. int64_t CarlaPlugin::getUniqueId() const noexcept
  131. {
  132. return 0;
  133. }
  134. uint32_t CarlaPlugin::getLatencyInFrames() const noexcept
  135. {
  136. return 0;
  137. }
  138. // -------------------------------------------------------------------
  139. // Information (count)
  140. uint32_t CarlaPlugin::getAudioInCount() const noexcept
  141. {
  142. return pData->audioIn.count;
  143. }
  144. uint32_t CarlaPlugin::getAudioOutCount() const noexcept
  145. {
  146. return pData->audioOut.count;
  147. }
  148. uint32_t CarlaPlugin::getCVInCount() const noexcept
  149. {
  150. return pData->cvIn.count;
  151. }
  152. uint32_t CarlaPlugin::getCVOutCount() const noexcept
  153. {
  154. return pData->cvOut.count;
  155. }
  156. uint32_t CarlaPlugin::getMidiInCount() const noexcept
  157. {
  158. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_IN) ? 1 : 0;
  159. }
  160. uint32_t CarlaPlugin::getMidiOutCount() const noexcept
  161. {
  162. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_OUT) ? 1 : 0;
  163. }
  164. uint32_t CarlaPlugin::getParameterCount() const noexcept
  165. {
  166. return pData->param.count;
  167. }
  168. uint32_t CarlaPlugin::getParameterScalePointCount(const uint32_t parameterId) const noexcept
  169. {
  170. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  171. return 0;
  172. }
  173. uint32_t CarlaPlugin::getProgramCount() const noexcept
  174. {
  175. return pData->prog.count;
  176. }
  177. uint32_t CarlaPlugin::getMidiProgramCount() const noexcept
  178. {
  179. return pData->midiprog.count;
  180. }
  181. uint32_t CarlaPlugin::getCustomDataCount() const noexcept
  182. {
  183. return static_cast<uint32_t>(pData->custom.count());
  184. }
  185. // -------------------------------------------------------------------
  186. // Information (current data)
  187. int32_t CarlaPlugin::getCurrentProgram() const noexcept
  188. {
  189. return pData->prog.current;
  190. }
  191. int32_t CarlaPlugin::getCurrentMidiProgram() const noexcept
  192. {
  193. return pData->midiprog.current;
  194. }
  195. uint CarlaPlugin::getAudioPortHints(bool, uint32_t) const noexcept
  196. {
  197. return 0x0;
  198. }
  199. const ParameterData& CarlaPlugin::getParameterData(const uint32_t parameterId) const noexcept
  200. {
  201. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterDataNull);
  202. return pData->param.data[parameterId];
  203. }
  204. const ParameterRanges& CarlaPlugin::getParameterRanges(const uint32_t parameterId) const noexcept
  205. {
  206. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterRangesNull);
  207. return pData->param.ranges[parameterId];
  208. }
  209. bool CarlaPlugin::isParameterOutput(const uint32_t parameterId) const noexcept
  210. {
  211. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  212. return (pData->param.data[parameterId].type == PARAMETER_OUTPUT);
  213. }
  214. const MidiProgramData& CarlaPlugin::getMidiProgramData(const uint32_t index) const noexcept
  215. {
  216. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, kMidiProgramDataNull);
  217. return pData->midiprog.data[index];
  218. }
  219. const CustomData& CarlaPlugin::getCustomData(const uint32_t index) const noexcept
  220. {
  221. return pData->custom.getAt(index, kCustomDataFallback);
  222. }
  223. std::size_t CarlaPlugin::getChunkData(void** const dataPtr) noexcept
  224. {
  225. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  226. CARLA_SAFE_ASSERT(false); // this should never happen
  227. return 0;
  228. }
  229. // -------------------------------------------------------------------
  230. // Information (per-plugin data)
  231. uint CarlaPlugin::getOptionsAvailable() const noexcept
  232. {
  233. CARLA_SAFE_ASSERT(false); // this should never happen
  234. return 0x0;
  235. }
  236. float CarlaPlugin::getParameterValue(const uint32_t parameterId) const noexcept
  237. {
  238. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  239. CARLA_SAFE_ASSERT(false); // this should never happen
  240. return 0.0f;
  241. }
  242. float CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept
  243. {
  244. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  245. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  246. CARLA_SAFE_ASSERT(false); // this should never happen
  247. return 0.0f;
  248. }
  249. bool CarlaPlugin::getLabel(char* const strBuf) const noexcept
  250. {
  251. strBuf[0] = '\0';
  252. return false;
  253. }
  254. bool CarlaPlugin::getMaker(char* const strBuf) const noexcept
  255. {
  256. strBuf[0] = '\0';
  257. return false;
  258. }
  259. bool CarlaPlugin::getCopyright(char* const strBuf) const noexcept
  260. {
  261. strBuf[0] = '\0';
  262. return false;
  263. }
  264. bool CarlaPlugin::getRealName(char* const strBuf) const noexcept
  265. {
  266. strBuf[0] = '\0';
  267. return false;
  268. }
  269. bool CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept
  270. {
  271. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  272. CARLA_SAFE_ASSERT(false); // this should never happen
  273. strBuf[0] = '\0';
  274. return false;
  275. }
  276. bool CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept
  277. {
  278. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  279. strBuf[0] = '\0';
  280. return false;
  281. }
  282. bool CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf) noexcept
  283. {
  284. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  285. CARLA_SAFE_ASSERT(false); // this should never happen
  286. strBuf[0] = '\0';
  287. return false;
  288. }
  289. bool CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept
  290. {
  291. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  292. strBuf[0] = '\0';
  293. return false;
  294. }
  295. bool CarlaPlugin::getParameterComment(const uint32_t parameterId, char* const strBuf) const noexcept
  296. {
  297. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  298. strBuf[0] = '\0';
  299. return false;
  300. }
  301. bool CarlaPlugin::getParameterGroupName(const uint32_t parameterId, char* const strBuf) const noexcept
  302. {
  303. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  304. strBuf[0] = '\0';
  305. return false;
  306. }
  307. bool CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept
  308. {
  309. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), false);
  310. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), false);
  311. CARLA_SAFE_ASSERT(false); // this should never happen
  312. strBuf[0] = '\0';
  313. return false;
  314. }
  315. float CarlaPlugin::getInternalParameterValue(const int32_t parameterId) const noexcept
  316. {
  317. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  318. CARLA_SAFE_ASSERT_RETURN(parameterId != PARAMETER_NULL && parameterId > PARAMETER_MAX, 0.0f);
  319. switch (parameterId)
  320. {
  321. case PARAMETER_ACTIVE:
  322. return pData->active;
  323. case PARAMETER_CTRL_CHANNEL:
  324. return pData->ctrlChannel;
  325. case PARAMETER_DRYWET:
  326. return pData->postProc.dryWet;
  327. case PARAMETER_VOLUME:
  328. return pData->postProc.volume;
  329. case PARAMETER_BALANCE_LEFT:
  330. return pData->postProc.balanceLeft;
  331. case PARAMETER_BALANCE_RIGHT:
  332. return pData->postProc.balanceRight;
  333. case PARAMETER_PANNING:
  334. return pData->postProc.panning;
  335. };
  336. #endif
  337. CARLA_SAFE_ASSERT_RETURN(parameterId >= 0, 0.0f);
  338. return getParameterValue(static_cast<uint32_t>(parameterId));
  339. }
  340. bool CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf) const noexcept
  341. {
  342. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count, false);
  343. CARLA_SAFE_ASSERT_RETURN(pData->prog.names[index] != nullptr, false);
  344. std::strncpy(strBuf, pData->prog.names[index], STR_MAX);
  345. return true;
  346. }
  347. bool CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf) const noexcept
  348. {
  349. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, false);
  350. CARLA_SAFE_ASSERT_RETURN(pData->midiprog.data[index].name != nullptr, false);
  351. std::strncpy(strBuf, pData->midiprog.data[index].name, STR_MAX);
  352. return true;
  353. }
  354. void CarlaPlugin::getParameterCountInfo(uint32_t& ins, uint32_t& outs) const noexcept
  355. {
  356. ins = 0;
  357. outs = 0;
  358. for (uint32_t i=0; i < pData->param.count; ++i)
  359. {
  360. if (pData->param.data[i].type == PARAMETER_INPUT)
  361. ++ins;
  362. else if (pData->param.data[i].type == PARAMETER_OUTPUT)
  363. ++outs;
  364. }
  365. }
  366. // -------------------------------------------------------------------
  367. // Set data (state)
  368. void CarlaPlugin::prepareForSave(bool)
  369. {
  370. }
  371. void CarlaPlugin::resetParameters() noexcept
  372. {
  373. for (uint i=0; i < pData->param.count; ++i)
  374. {
  375. const ParameterData& paramData(pData->param.data[i]);
  376. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  377. if (paramData.type != PARAMETER_INPUT)
  378. continue;
  379. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  380. continue;
  381. setParameterValue(i, paramRanges.def, true, true, true);
  382. }
  383. }
  384. void CarlaPlugin::randomizeParameters() noexcept
  385. {
  386. float value, random;
  387. char strBuf[STR_MAX+1];
  388. strBuf[STR_MAX] = '\0';
  389. std::srand(static_cast<uint>(std::time(nullptr)));
  390. for (uint i=0; i < pData->param.count; ++i)
  391. {
  392. const ParameterData& paramData(pData->param.data[i]);
  393. if (paramData.type != PARAMETER_INPUT)
  394. continue;
  395. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  396. continue;
  397. if (! getParameterName(i, strBuf))
  398. strBuf[0] = '\0';
  399. if (std::strstr(strBuf, "olume") != nullptr)
  400. continue;
  401. if (std::strstr(strBuf, "Master") != nullptr)
  402. continue;
  403. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  404. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  405. {
  406. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  407. value = random > 0.5f ? paramRanges.max : paramRanges.min;
  408. }
  409. else
  410. {
  411. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  412. value = random * (paramRanges.max - paramRanges.min) + paramRanges.min;
  413. if (paramData.hints & PARAMETER_IS_INTEGER)
  414. value = std::rint(value);
  415. }
  416. setParameterValue(i, value, true, true, true);
  417. }
  418. }
  419. const CarlaStateSave& CarlaPlugin::getStateSave(const bool callPrepareForSave)
  420. {
  421. pData->stateSave.clear();
  422. if (callPrepareForSave)
  423. {
  424. pData->stateSave.temporary = true;
  425. prepareForSave(true);
  426. }
  427. const PluginType pluginType = getType();
  428. char strBuf[STR_MAX+1];
  429. carla_zeroChars(strBuf, STR_MAX+1);
  430. // ---------------------------------------------------------------
  431. // Basic info
  432. if (! getLabel(strBuf))
  433. strBuf[0] = '\0';
  434. pData->stateSave.type = carla_strdup(getPluginTypeAsString(pluginType));
  435. pData->stateSave.name = carla_strdup(pData->name);
  436. pData->stateSave.label = carla_strdup(strBuf);
  437. pData->stateSave.uniqueId = getUniqueId();
  438. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  439. pData->stateSave.options = pData->options;
  440. #endif
  441. if (pData->filename != nullptr)
  442. pData->stateSave.binary = carla_strdup(pData->filename);
  443. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  444. // ---------------------------------------------------------------
  445. // Internals
  446. pData->stateSave.active = pData->active;
  447. pData->stateSave.dryWet = pData->postProc.dryWet;
  448. pData->stateSave.volume = pData->postProc.volume;
  449. pData->stateSave.balanceLeft = pData->postProc.balanceLeft;
  450. pData->stateSave.balanceRight = pData->postProc.balanceRight;
  451. pData->stateSave.panning = pData->postProc.panning;
  452. pData->stateSave.ctrlChannel = pData->ctrlChannel;
  453. #endif
  454. if (pData->hints & PLUGIN_IS_BRIDGE)
  455. waitForBridgeSaveSignal();
  456. // ---------------------------------------------------------------
  457. // Chunk
  458. bool usingChunk = false;
  459. if (pData->options & PLUGIN_OPTION_USE_CHUNKS)
  460. {
  461. void* data = nullptr;
  462. const std::size_t dataSize = getChunkData(&data);
  463. if (data != nullptr && dataSize > 0)
  464. {
  465. pData->stateSave.chunk = CarlaString::asBase64(data, dataSize).dup();
  466. if (pluginType != PLUGIN_INTERNAL && pluginType != PLUGIN_JSFX)
  467. usingChunk = true;
  468. }
  469. }
  470. // ---------------------------------------------------------------
  471. // Current Program
  472. if (pData->prog.current >= 0 && pluginType != PLUGIN_LV2)
  473. {
  474. pData->stateSave.currentProgramIndex = pData->prog.current;
  475. pData->stateSave.currentProgramName = carla_strdup(pData->prog.names[pData->prog.current]);
  476. }
  477. // ---------------------------------------------------------------
  478. // Current MIDI Program
  479. if (pData->midiprog.current >= 0 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_SF2)
  480. {
  481. const MidiProgramData& mpData(pData->midiprog.getCurrent());
  482. pData->stateSave.currentMidiBank = static_cast<int32_t>(mpData.bank);
  483. pData->stateSave.currentMidiProgram = static_cast<int32_t>(mpData.program);
  484. }
  485. // ---------------------------------------------------------------
  486. // Parameters
  487. const float sampleRate = static_cast<float>(pData->engine->getSampleRate());
  488. for (uint32_t i=0; i < pData->param.count; ++i)
  489. {
  490. const ParameterData& paramData(pData->param.data[i]);
  491. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  492. continue;
  493. if (paramData.hints & PARAMETER_IS_NOT_SAVED)
  494. continue;
  495. const bool dummy = paramData.type != PARAMETER_INPUT || usingChunk;
  496. if (dummy && paramData.mappedControlIndex <= CONTROL_INDEX_NONE)
  497. continue;
  498. CarlaStateSave::Parameter* const stateParameter(new CarlaStateSave::Parameter());
  499. stateParameter->dummy = dummy;
  500. stateParameter->index = paramData.index;
  501. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  502. if (paramData.mappedControlIndex != CONTROL_INDEX_MIDI_LEARN)
  503. {
  504. stateParameter->mappedControlIndex = paramData.mappedControlIndex;
  505. stateParameter->midiChannel = paramData.midiChannel;
  506. if (paramData.hints & PARAMETER_MAPPED_RANGES_SET)
  507. {
  508. stateParameter->mappedMinimum = paramData.mappedMinimum;
  509. stateParameter->mappedMaximum = paramData.mappedMaximum;
  510. stateParameter->mappedRangeValid = true;
  511. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  512. {
  513. stateParameter->mappedMinimum /= sampleRate;
  514. stateParameter->mappedMaximum /= sampleRate;
  515. }
  516. }
  517. }
  518. #endif
  519. if (! getParameterName(i, strBuf))
  520. strBuf[0] = '\0';
  521. stateParameter->name = carla_strdup(strBuf);
  522. if (! getParameterSymbol(i, strBuf))
  523. strBuf[0] = '\0';
  524. stateParameter->symbol = carla_strdup(strBuf);;
  525. if (! dummy)
  526. {
  527. stateParameter->value = getParameterValue(i);
  528. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  529. stateParameter->value /= sampleRate;
  530. }
  531. pData->stateSave.parameters.append(stateParameter);
  532. }
  533. // ---------------------------------------------------------------
  534. // Custom Data
  535. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  536. {
  537. const CustomData& cData(it.getValue(kCustomDataFallback));
  538. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  539. CarlaStateSave::CustomData* stateCustomData(new CarlaStateSave::CustomData());
  540. stateCustomData->type = carla_strdup(cData.type);
  541. stateCustomData->key = carla_strdup(cData.key);
  542. stateCustomData->value = carla_strdup(cData.value);
  543. pData->stateSave.customData.append(stateCustomData);
  544. }
  545. return pData->stateSave;
  546. }
  547. void CarlaPlugin::loadStateSave(const CarlaStateSave& stateSave)
  548. {
  549. const bool usesMultiProgs = pData->hints & PLUGIN_USES_MULTI_PROGS;
  550. const PluginType pluginType = getType();
  551. char strBuf[STR_MAX+1];
  552. carla_zeroChars(strBuf, STR_MAX+1);
  553. // ---------------------------------------------------------------
  554. // Part 1 - PRE-set custom data (only those which reload programs)
  555. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  556. {
  557. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  558. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  559. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  560. const char* const key(stateCustomData->key);
  561. /**/ if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  562. std::strcmp (key, "load" ) == 0 ||
  563. std::strncmp(key, "patches", 7) == 0 ))
  564. pass();
  565. else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  566. pass();
  567. else
  568. continue;
  569. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  570. }
  571. // ---------------------------------------------------------------
  572. // Part 2 - set program
  573. if (stateSave.currentProgramIndex >= 0 && stateSave.currentProgramName != nullptr)
  574. {
  575. int32_t programId = -1;
  576. // index < count
  577. if (stateSave.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
  578. {
  579. programId = stateSave.currentProgramIndex;
  580. }
  581. // index not valid, try to find by name
  582. else
  583. {
  584. for (uint32_t i=0; i < pData->prog.count; ++i)
  585. {
  586. if (getProgramName(i, strBuf) && std::strcmp(stateSave.currentProgramName, strBuf) == 0)
  587. {
  588. programId = static_cast<int32_t>(i);
  589. break;
  590. }
  591. }
  592. }
  593. // set program now, if valid
  594. if (programId >= 0)
  595. setProgram(programId, true, true, true);
  596. }
  597. // ---------------------------------------------------------------
  598. // Part 3 - set midi program
  599. if (stateSave.currentMidiBank >= 0 && stateSave.currentMidiProgram >= 0 && ! usesMultiProgs)
  600. setMidiProgramById(static_cast<uint32_t>(stateSave.currentMidiBank), static_cast<uint32_t>(stateSave.currentMidiProgram), true, true, true);
  601. // ---------------------------------------------------------------
  602. // Part 4a - get plugin parameter symbols
  603. LinkedList<ParamSymbol*> paramSymbols;
  604. if (pluginType == PLUGIN_LADSPA || pluginType == PLUGIN_LV2 || pluginType == PLUGIN_CLAP)
  605. {
  606. for (uint32_t i=0; i < pData->param.count; ++i)
  607. {
  608. if (pData->param.data[i].hints & PARAMETER_IS_NOT_SAVED)
  609. continue;
  610. if (getParameterSymbol(i, strBuf))
  611. {
  612. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  613. paramSymbols.append(paramSymbol);
  614. }
  615. }
  616. }
  617. // ---------------------------------------------------------------
  618. // Part 4b - set parameter values (carefully)
  619. const float sampleRate = static_cast<float>(pData->engine->getSampleRate());
  620. for (CarlaStateSave::ParameterItenerator it = stateSave.parameters.begin2(); it.valid(); it.next())
  621. {
  622. CarlaStateSave::Parameter* const stateParameter(it.getValue(nullptr));
  623. CARLA_SAFE_ASSERT_CONTINUE(stateParameter != nullptr);
  624. int32_t index = -1;
  625. if (pluginType == PLUGIN_LADSPA)
  626. {
  627. // Try to set by symbol, otherwise use index
  628. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  629. {
  630. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  631. {
  632. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  633. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  634. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  635. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  636. {
  637. index = paramSymbol->index;
  638. break;
  639. }
  640. }
  641. if (index == -1)
  642. index = stateParameter->index;
  643. }
  644. else
  645. {
  646. index = stateParameter->index;
  647. }
  648. }
  649. else if (pluginType == PLUGIN_LV2 || pluginType == PLUGIN_CLAP)
  650. {
  651. // Symbol only
  652. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  653. {
  654. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  655. {
  656. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  657. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  658. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  659. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  660. {
  661. index = paramSymbol->index;
  662. break;
  663. }
  664. }
  665. if (index == -1)
  666. {
  667. if (pluginType == PLUGIN_LV2)
  668. carla_stderr("Failed to find LV2 parameter symbol '%s' for '%s'",
  669. stateParameter->symbol, pData->name);
  670. else
  671. carla_stderr("Failed to find CLAP parameter id '%s' for '%s'",
  672. stateParameter->symbol, pData->name);
  673. }
  674. }
  675. else
  676. {
  677. if (pluginType == PLUGIN_LV2)
  678. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  679. else
  680. carla_stderr("CLAP Plugin parameter '%s' has no id", stateParameter->name);
  681. }
  682. }
  683. else
  684. {
  685. // Index only
  686. index = stateParameter->index;
  687. }
  688. // Now set parameter
  689. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  690. {
  691. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  692. if (! stateParameter->dummy)
  693. {
  694. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  695. stateParameter->value *= sampleRate;
  696. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  697. }
  698. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  699. if (stateParameter->mappedRangeValid)
  700. {
  701. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  702. {
  703. stateParameter->mappedMinimum *= sampleRate;
  704. stateParameter->mappedMaximum *= sampleRate;
  705. }
  706. setParameterMappedRange(static_cast<uint32_t>(index),
  707. stateParameter->mappedMinimum,
  708. stateParameter->mappedMaximum, true, true);
  709. }
  710. setParameterMappedControlIndex(static_cast<uint32_t>(index),
  711. stateParameter->mappedControlIndex, true, true, false);
  712. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  713. #endif
  714. }
  715. else
  716. carla_stderr("Could not set parameter '%s' value for '%s'",
  717. stateParameter->name, pData->name);
  718. }
  719. // ---------------------------------------------------------------
  720. // Part 4c - clear
  721. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin2(); it.valid(); it.next())
  722. {
  723. ParamSymbol* const paramSymbol(it.getValue(nullptr));
  724. delete paramSymbol;
  725. }
  726. paramSymbols.clear();
  727. // ---------------------------------------------------------------
  728. // Part 5 - set custom data
  729. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  730. {
  731. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  732. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  733. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  734. const char* const key(stateCustomData->key);
  735. if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  736. std::strcmp (key, "load" ) == 0 ||
  737. std::strncmp(key, "patches", 7) == 0 ))
  738. continue;
  739. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  740. continue;
  741. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  742. }
  743. // ---------------------------------------------------------------
  744. // Part 5x - set lv2 state
  745. if (pluginType == PLUGIN_LV2)
  746. {
  747. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  748. {
  749. const CustomData& customData(it.getValue(kCustomDataFallback));
  750. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  751. if (std::strcmp(customData.type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  752. continue;
  753. restoreLV2State(stateSave.temporary);
  754. break;
  755. }
  756. }
  757. // ---------------------------------------------------------------
  758. // Part 6 - set chunk
  759. if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  760. {
  761. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stateSave.chunk));
  762. #ifdef CARLA_PROPER_CPP11_SUPPORT
  763. setChunkData(chunk.data(), chunk.size());
  764. #else
  765. setChunkData(&chunk.front(), chunk.size());
  766. #endif
  767. }
  768. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  769. // ---------------------------------------------------------------
  770. // Part 6 - set internal stuff
  771. const uint availOptions = getOptionsAvailable();
  772. for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
  773. {
  774. const uint option(1u << i);
  775. if (availOptions & option)
  776. setOption(option, (stateSave.options & option) != 0, true);
  777. }
  778. setDryWet(stateSave.dryWet, true, true);
  779. setVolume(stateSave.volume, true, true);
  780. setBalanceLeft(stateSave.balanceLeft, true, true);
  781. setBalanceRight(stateSave.balanceRight, true, true);
  782. setPanning(stateSave.panning, true, true);
  783. setCtrlChannel(stateSave.ctrlChannel, true, true);
  784. setActive(stateSave.active, true, true);
  785. if (! pData->engine->isLoadingProject())
  786. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0, 0.0f, nullptr);
  787. #endif
  788. }
  789. bool CarlaPlugin::saveStateToFile(const char* const filename)
  790. {
  791. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  792. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  793. MemoryOutputStream out, streamState;
  794. getStateSave().dumpToMemoryStream(streamState);
  795. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  796. out << "<!DOCTYPE CARLA-PRESET>\n";
  797. out << "<CARLA-PRESET VERSION='2.0'>\n";
  798. out << streamState;
  799. out << "</CARLA-PRESET>\n";
  800. const String jfilename = String(CharPointer_UTF8(filename));
  801. File file(jfilename);
  802. if (file.replaceWithData(out.getData(), out.getDataSize()))
  803. return true;
  804. pData->engine->setLastError("Failed to write file");
  805. return false;
  806. }
  807. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  808. {
  809. // TODO set errors
  810. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  811. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  812. const String jfilename = String(CharPointer_UTF8(filename));
  813. File file(jfilename);
  814. CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
  815. XmlDocument xml(file);
  816. CarlaScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
  817. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  818. CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
  819. // completely load file
  820. xmlElement = xml.getDocumentElement(false);
  821. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  822. if (pData->stateSave.fillFromXmlElement(xmlElement))
  823. {
  824. loadStateSave(pData->stateSave);
  825. return true;
  826. }
  827. return false;
  828. }
  829. bool CarlaPlugin::exportAsLV2(const char* const lv2path)
  830. {
  831. CARLA_SAFE_ASSERT_RETURN(lv2path != nullptr && lv2path[0] != '\0', false);
  832. carla_debug("CarlaPlugin::exportAsLV2(\"%s\")", lv2path);
  833. CarlaString bundlepath(lv2path);
  834. if (! bundlepath.endsWith(".lv2"))
  835. bundlepath += ".lv2";
  836. const File bundlefolder(bundlepath.buffer());
  837. if (bundlefolder.existsAsFile())
  838. {
  839. pData->engine->setLastError("Requested filename already exists as file, use a folder instead");
  840. return false;
  841. }
  842. if (! bundlefolder.exists())
  843. {
  844. const Result res(bundlefolder.createDirectory());
  845. if (res.failed())
  846. {
  847. pData->engine->setLastError(res.getErrorMessage().c_str());
  848. return false;
  849. }
  850. }
  851. CarlaString symbol(pData->name);
  852. symbol.toBasic();
  853. {
  854. const CarlaString pluginFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".xml");
  855. if (! saveStateToFile(pluginFilename))
  856. return false;
  857. }
  858. {
  859. MemoryOutputStream manifestStream;
  860. manifestStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  861. manifestStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  862. manifestStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  863. manifestStream << "\n";
  864. manifestStream << "<" << symbol.buffer() << ".ttl>\n";
  865. manifestStream << " a lv2:Plugin ;\n";
  866. manifestStream << " lv2:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  867. manifestStream << " rdfs:seeAlso <" << symbol.buffer() << ".ttl> .\n";
  868. manifestStream << "\n";
  869. manifestStream << "<ext-ui>\n";
  870. manifestStream << " a <http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget> ;\n";
  871. manifestStream << " ui:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  872. manifestStream << " lv2:extensionData <http://lv2plug.in/ns/extensions/ui#idleInterface> ,\n";
  873. manifestStream << " <http://lv2plug.in/ns/extensions/ui#showInterface> ;\n";
  874. manifestStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> .\n";
  875. manifestStream << "\n";
  876. const CarlaString manifestFilename(bundlepath + CARLA_OS_SEP_STR "manifest.ttl");
  877. const File manifestFile(manifestFilename.buffer());
  878. if (! manifestFile.replaceWithData(manifestStream.getData(), manifestStream.getDataSize()))
  879. {
  880. pData->engine->setLastError("Failed to write manifest.ttl file");
  881. return false;
  882. }
  883. }
  884. {
  885. MemoryOutputStream mainStream;
  886. mainStream << "@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n";
  887. mainStream << "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  888. mainStream << "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
  889. mainStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  890. mainStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  891. mainStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  892. mainStream << "\n";
  893. mainStream << "<>\n";
  894. mainStream << " a lv2:Plugin ;\n";
  895. mainStream << "\n";
  896. mainStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/buf-size#boundedBlockLength> ,\n";
  897. mainStream << " <http://lv2plug.in/ns/ext/options#options> ,\n";
  898. mainStream << " <http://lv2plug.in/ns/ext/urid#map> ;\n";
  899. mainStream << "\n";
  900. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  901. {
  902. mainStream << " ui:ui <ext-ui> ;\n";
  903. mainStream << "\n";
  904. }
  905. const uint32_t midiIns = getMidiInCount();
  906. const uint32_t midiOuts = getMidiOutCount();
  907. int portIndex = 0;
  908. if (midiIns > 0)
  909. {
  910. mainStream << " lv2:port [\n";
  911. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  912. mainStream << " lv2:index 0 ;\n";
  913. mainStream << " lv2:symbol \"clv2_events_in\" ;\n";
  914. mainStream << " lv2:name \"Events Input\" ;\n";
  915. mainStream << " atom:bufferType atom:Sequence ;\n";
  916. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ,\n";
  917. mainStream << " <http://lv2plug.in/ns/ext/time#Position> ;\n";
  918. mainStream << " ] ;\n";
  919. ++portIndex;
  920. for (uint32_t i=1; i<midiIns; ++i)
  921. {
  922. const String portIndexNum(portIndex++);
  923. const String portIndexLabel(portIndex);
  924. mainStream << " lv2:port [\n";
  925. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  926. mainStream << " lv2:index " << portIndexNum << " ;\n";
  927. mainStream << " lv2:symbol \"clv2_midi_in_" << portIndexLabel << "\" ;\n";
  928. mainStream << " lv2:name \"MIDI Input " << portIndexLabel << "\" ;\n";
  929. mainStream << " ] ;\n";
  930. }
  931. }
  932. else
  933. {
  934. mainStream << " lv2:port [\n";
  935. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  936. mainStream << " lv2:index 0 ;\n";
  937. mainStream << " lv2:symbol \"clv2_time_info\" ;\n";
  938. mainStream << " lv2:name \"Time Info\" ;\n";
  939. mainStream << " atom:bufferType atom:Sequence ;\n";
  940. mainStream << " atom:supports <http://lv2plug.in/ns/ext/time#Position> ;\n";
  941. mainStream << " ] ;\n";
  942. ++portIndex;
  943. }
  944. for (uint32_t i=0; i<midiOuts; ++i)
  945. {
  946. const String portIndexNum(portIndex++);
  947. const String portIndexLabel(portIndex);
  948. mainStream << " lv2:port [\n";
  949. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  950. mainStream << " lv2:index " << portIndexNum << " ;\n";
  951. mainStream << " lv2:symbol \"clv2_midi_out_" << portIndexLabel << "\" ;\n";
  952. mainStream << " lv2:name \"MIDI Output " << portIndexLabel << "\" ;\n";
  953. mainStream << " atom:bufferType atom:Sequence ;\n";
  954. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n";
  955. mainStream << " ] ;\n";
  956. }
  957. mainStream << " lv2:port [\n";
  958. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  959. mainStream << " lv2:index " << String(portIndex++) << " ;\n";
  960. mainStream << " lv2:name \"freewheel\" ;\n";
  961. mainStream << " lv2:symbol \"clv2_freewheel\" ;\n";
  962. mainStream << " lv2:default 0 ;\n";
  963. mainStream << " lv2:minimum 0 ;\n";
  964. mainStream << " lv2:maximum 1 ;\n";
  965. mainStream << " lv2:designation lv2:freeWheeling ;\n";
  966. mainStream << " lv2:portProperty lv2:toggled , lv2:integer ;\n";
  967. mainStream << " lv2:portProperty <http://lv2plug.in/ns/ext/port-props#notOnGUI> ;\n";
  968. mainStream << " ] ;\n";
  969. for (uint32_t i=0; i<pData->audioIn.count; ++i)
  970. {
  971. const String portIndexNum(portIndex++);
  972. const String portIndexLabel(i+1);
  973. mainStream << " lv2:port [\n";
  974. mainStream << " a lv2:InputPort, lv2:AudioPort ;\n";
  975. mainStream << " lv2:index " << portIndexNum << " ;\n";
  976. mainStream << " lv2:symbol \"clv2_audio_in_" << portIndexLabel << "\" ;\n";
  977. mainStream << " lv2:name \"Audio Input " << portIndexLabel << "\" ;\n";
  978. mainStream << " ] ;\n";
  979. }
  980. for (uint32_t i=0; i<pData->audioOut.count; ++i)
  981. {
  982. const String portIndexNum(portIndex++);
  983. const String portIndexLabel(i+1);
  984. mainStream << " lv2:port [\n";
  985. mainStream << " a lv2:OutputPort, lv2:AudioPort ;\n";
  986. mainStream << " lv2:index " << portIndexNum << " ;\n";
  987. mainStream << " lv2:symbol \"clv2_audio_out_" << portIndexLabel << "\" ;\n";
  988. mainStream << " lv2:name \"Audio Output " << portIndexLabel << "\" ;\n";
  989. mainStream << " ] ;\n";
  990. }
  991. CarlaStringList uniqueSymbolNames;
  992. char strBufName[STR_MAX+1];
  993. char strBufSymbol[STR_MAX+1];
  994. carla_zeroChars(strBufName, STR_MAX+1);
  995. carla_zeroChars(strBufSymbol, STR_MAX+1);
  996. for (uint32_t i=0; i<pData->param.count; ++i)
  997. {
  998. const ParameterData& paramData(pData->param.data[i]);
  999. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1000. const String portIndexNum(portIndex++);
  1001. mainStream << " lv2:port [\n";
  1002. if (paramData.type == PARAMETER_INPUT)
  1003. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  1004. else
  1005. mainStream << " a lv2:OutputPort, lv2:ControlPort ;\n";
  1006. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  1007. mainStream << " lv2:portProperty lv2:toggled ;\n";
  1008. if (paramData.hints & PARAMETER_IS_INTEGER)
  1009. mainStream << " lv2:portProperty lv2:integer ;\n";
  1010. // TODO logarithmic, enabled (not on gui), automatable, samplerate, scalepoints
  1011. if (! getParameterName(i, strBufName))
  1012. strBufName[0] = '\0';
  1013. if (! getParameterSymbol(i, strBufSymbol))
  1014. strBufSymbol[0] = '\0';
  1015. if (strBufSymbol[0] == '\0')
  1016. {
  1017. CarlaString s(strBufName);
  1018. s.toBasic();
  1019. std::memcpy(strBufSymbol, s.buffer(), s.length()+1);
  1020. if (strBufSymbol[0] >= '0' && strBufSymbol[0] <= '9')
  1021. {
  1022. const size_t len(std::strlen(strBufSymbol));
  1023. std::memmove(strBufSymbol+1, strBufSymbol, len);
  1024. strBufSymbol[0] = '_';
  1025. strBufSymbol[len+1] = '\0';
  1026. }
  1027. }
  1028. if (uniqueSymbolNames.contains(strBufSymbol))
  1029. std::snprintf(strBufSymbol, STR_MAX, "clv2_param_%d", i+1);
  1030. mainStream << " lv2:index " << portIndexNum << " ;\n";
  1031. mainStream << " lv2:symbol \"" << strBufSymbol << "\" ;\n";
  1032. mainStream << " lv2:name \"\"\"" << strBufName << "\"\"\" ;\n";
  1033. mainStream << " lv2:default " << String(paramRanges.def) << " ;\n";
  1034. mainStream << " lv2:minimum " << String(paramRanges.min) << " ;\n";
  1035. mainStream << " lv2:maximum " << String(paramRanges.max) << " ;\n";
  1036. // TODO midiCC, midiChannel
  1037. mainStream << " ] ;\n";
  1038. }
  1039. char strBuf[STR_MAX+1];
  1040. carla_zeroChars(strBuf, STR_MAX+1);
  1041. if (! getMaker(strBuf))
  1042. strBuf[0] = '\0';
  1043. mainStream << " rdfs:comment \"Plugin generated using Carla LV2 export.\" ;\n";
  1044. mainStream << " doap:name \"\"\"" << getName() << "\"\"\" ;\n";
  1045. mainStream << " doap:maintainer [ foaf:name \"\"\"" << strBuf << "\"\"\" ] .\n";
  1046. mainStream << "\n";
  1047. const CarlaString mainFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".ttl");
  1048. const File mainFile(mainFilename.buffer());
  1049. if (! mainFile.replaceWithData(mainStream.getData(), mainStream.getDataSize()))
  1050. {
  1051. pData->engine->setLastError("Failed to write main plugin ttl file");
  1052. return false;
  1053. }
  1054. }
  1055. const CarlaString binaryFilename(bundlepath + CARLA_OS_SEP_STR + symbol + CARLA_LIB_EXT);
  1056. const File binaryFileSource(File::getSpecialLocation(File::currentExecutableFile).getSiblingFile("carla-bridge-lv2" CARLA_LIB_EXT));
  1057. const File binaryFileTarget(binaryFilename.buffer());
  1058. const EngineOptions& opts(pData->engine->getOptions());
  1059. const CarlaString binFolderTarget(bundlepath + CARLA_OS_SEP_STR + "bin");
  1060. const CarlaString resFolderTarget(bundlepath + CARLA_OS_SEP_STR + "res");
  1061. if (! binaryFileSource.copyFileTo(binaryFileTarget))
  1062. {
  1063. pData->engine->setLastError("Failed to copy plugin binary");
  1064. return false;
  1065. }
  1066. #ifdef CARLA_OS_WIN
  1067. File(opts.resourceDir).copyDirectoryTo(File(resFolderTarget.buffer()));
  1068. // Copying all the binaries is pointless, just go through the expected needed bits
  1069. const File binFolder1(opts.binaryDir);
  1070. const File binFolder2(binFolderTarget.buffer());
  1071. binFolder2.createDirectory();
  1072. static const char* files[] = {
  1073. "carla-bridge-native.exe",
  1074. "carla-bridge-win32.exe",
  1075. "carla-discovery-win32.exe",
  1076. "carla-discovery-win64.exe",
  1077. "libcarla_utils.dll"
  1078. };
  1079. for (int i=0; i<5; ++i)
  1080. binFolder1.getChildFile(files[i]).copyFileTo(binFolder2.getChildFile(files[i]));;
  1081. #else
  1082. File(opts.binaryDir).createSymbolicLink(File(binFolderTarget.buffer()), true);
  1083. File(opts.resourceDir).createSymbolicLink(File(resFolderTarget.buffer()), true);
  1084. #endif
  1085. return true;
  1086. }
  1087. // -------------------------------------------------------------------
  1088. // Set data (internal stuff)
  1089. void CarlaPlugin::setId(const uint newId) noexcept
  1090. {
  1091. pData->id = newId;
  1092. }
  1093. void CarlaPlugin::setName(const char* const newName)
  1094. {
  1095. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  1096. if (pData->name != nullptr)
  1097. delete[] pData->name;
  1098. pData->name = carla_strdup(newName);
  1099. }
  1100. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  1101. {
  1102. CARLA_SAFE_ASSERT_UINT2_RETURN(getOptionsAvailable() & option, getOptionsAvailable(), option,);
  1103. if (yesNo)
  1104. pData->options |= option;
  1105. else
  1106. pData->options &= ~option;
  1107. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1108. if (sendCallback)
  1109. pData->engine->callback(true, true,
  1110. ENGINE_CALLBACK_OPTION_CHANGED,
  1111. pData->id,
  1112. static_cast<int>(option),
  1113. yesNo ? 1 : 0,
  1114. 0, 0.0f, nullptr);
  1115. #else
  1116. // unused
  1117. return; (void)sendCallback;
  1118. #endif
  1119. }
  1120. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  1121. {
  1122. if (pData->enabled == yesNo)
  1123. return;
  1124. pData->masterMutex.lock();
  1125. pData->enabled = yesNo;
  1126. if (yesNo && ! pData->client->isActive())
  1127. pData->client->activate();
  1128. pData->masterMutex.unlock();
  1129. }
  1130. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  1131. {
  1132. if (pData->engineBridged) {
  1133. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1134. } else if (pData->enginePlugin) {
  1135. // nothing here
  1136. } else {
  1137. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1138. }
  1139. if (pData->active == active)
  1140. return;
  1141. {
  1142. const ScopedSingleProcessLocker spl(this, true);
  1143. if (active)
  1144. activate();
  1145. else
  1146. deactivate();
  1147. }
  1148. pData->active = active;
  1149. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1150. const float value = active ? 1.0f : 0.0f;
  1151. pData->engine->callback(sendCallback, sendOsc,
  1152. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1153. pData->id,
  1154. PARAMETER_ACTIVE,
  1155. 0, 0,
  1156. value,
  1157. nullptr);
  1158. #endif
  1159. }
  1160. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1161. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1162. {
  1163. if (pData->engineBridged) {
  1164. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1165. } else {
  1166. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1167. }
  1168. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1169. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1170. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1171. return;
  1172. pData->postProc.dryWet = fixedValue;
  1173. pData->engine->callback(sendCallback, sendOsc,
  1174. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1175. pData->id,
  1176. PARAMETER_DRYWET,
  1177. 0, 0,
  1178. fixedValue,
  1179. nullptr);
  1180. }
  1181. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1182. {
  1183. if (pData->engineBridged) {
  1184. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1185. } else {
  1186. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1187. }
  1188. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1189. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1190. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1191. return;
  1192. pData->postProc.volume = fixedValue;
  1193. pData->engine->callback(sendCallback, sendOsc,
  1194. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1195. pData->id,
  1196. PARAMETER_VOLUME,
  1197. 0, 0,
  1198. fixedValue,
  1199. nullptr);
  1200. }
  1201. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1202. {
  1203. if (pData->engineBridged) {
  1204. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1205. } else {
  1206. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1207. }
  1208. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1209. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1210. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1211. return;
  1212. pData->postProc.balanceLeft = fixedValue;
  1213. pData->engine->callback(sendCallback, sendOsc,
  1214. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1215. pData->id,
  1216. PARAMETER_BALANCE_LEFT,
  1217. 0, 0,
  1218. fixedValue,
  1219. nullptr);
  1220. }
  1221. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1222. {
  1223. if (pData->engineBridged) {
  1224. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1225. } else {
  1226. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1227. }
  1228. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1229. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1230. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1231. return;
  1232. pData->postProc.balanceRight = fixedValue;
  1233. pData->engine->callback(sendCallback, sendOsc,
  1234. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1235. pData->id,
  1236. PARAMETER_BALANCE_RIGHT,
  1237. 0, 0,
  1238. fixedValue,
  1239. nullptr);
  1240. }
  1241. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1242. {
  1243. if (pData->engineBridged) {
  1244. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1245. } else {
  1246. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1247. }
  1248. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1249. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1250. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1251. return;
  1252. pData->postProc.panning = fixedValue;
  1253. pData->engine->callback(sendCallback, sendOsc,
  1254. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1255. pData->id,
  1256. PARAMETER_PANNING,
  1257. 0, 0,
  1258. fixedValue,
  1259. nullptr);
  1260. }
  1261. void CarlaPlugin::setDryWetRT(const float value, const bool sendCallbackLater) noexcept
  1262. {
  1263. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1264. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1265. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1266. return;
  1267. pData->postProc.dryWet = fixedValue;
  1268. pData->postponeParameterChangeRtEvent(sendCallbackLater, PARAMETER_DRYWET, fixedValue);
  1269. }
  1270. void CarlaPlugin::setVolumeRT(const float value, const bool sendCallbackLater) noexcept
  1271. {
  1272. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1273. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1274. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1275. return;
  1276. pData->postProc.volume = fixedValue;
  1277. pData->postponeParameterChangeRtEvent(sendCallbackLater, PARAMETER_VOLUME, fixedValue);
  1278. }
  1279. void CarlaPlugin::setBalanceLeftRT(const float value, const bool sendCallbackLater) noexcept
  1280. {
  1281. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1282. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1283. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1284. return;
  1285. pData->postProc.balanceLeft = fixedValue;
  1286. pData->postponeParameterChangeRtEvent(sendCallbackLater, PARAMETER_BALANCE_LEFT, fixedValue);
  1287. }
  1288. void CarlaPlugin::setBalanceRightRT(const float value, const bool sendCallbackLater) noexcept
  1289. {
  1290. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1291. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1292. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1293. return;
  1294. pData->postProc.balanceRight = fixedValue;
  1295. pData->postponeParameterChangeRtEvent(sendCallbackLater, PARAMETER_BALANCE_RIGHT, fixedValue);
  1296. }
  1297. void CarlaPlugin::setPanningRT(const float value, const bool sendCallbackLater) noexcept
  1298. {
  1299. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1300. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1301. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1302. return;
  1303. pData->postProc.panning = fixedValue;
  1304. pData->postponeParameterChangeRtEvent(sendCallbackLater, PARAMETER_PANNING, fixedValue);
  1305. }
  1306. #endif // ! BUILD_BRIDGE_ALTERNATIVE_ARCH
  1307. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1308. {
  1309. if (pData->engineBridged) {
  1310. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1311. } else {
  1312. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1313. }
  1314. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  1315. if (pData->ctrlChannel == channel)
  1316. return;
  1317. pData->ctrlChannel = channel;
  1318. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1319. const float channelf = static_cast<float>(channel);
  1320. pData->engine->callback(sendCallback, sendOsc,
  1321. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1322. pData->id,
  1323. PARAMETER_CTRL_CHANNEL,
  1324. 0, 0,
  1325. channelf, nullptr);
  1326. #endif
  1327. }
  1328. // -------------------------------------------------------------------
  1329. // Set data (plugin-specific stuff)
  1330. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1331. {
  1332. if (pData->engineBridged) {
  1333. // NOTE: some LV2 plugins feedback messages to UI on purpose
  1334. CARLA_SAFE_ASSERT_RETURN(getType() == PLUGIN_LV2 || !sendGui,);
  1335. } else if (pData->enginePlugin) {
  1336. // nothing here
  1337. } else {
  1338. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,); // never call this from RT
  1339. }
  1340. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1341. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1342. uiParameterChange(parameterId, value);
  1343. pData->engine->callback(sendCallback, sendOsc,
  1344. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1345. pData->id,
  1346. static_cast<int>(parameterId),
  1347. 0, 0,
  1348. value,
  1349. nullptr);
  1350. }
  1351. void CarlaPlugin::setParameterValueRT(const uint32_t parameterId, const float value, uint32_t, const bool sendCallbackLater) noexcept
  1352. {
  1353. pData->postponeParameterChangeRtEvent(sendCallbackLater, static_cast<int32_t>(parameterId), value);
  1354. }
  1355. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1356. {
  1357. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1358. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  1359. switch (rindex)
  1360. {
  1361. case PARAMETER_ACTIVE:
  1362. return setActive((value > 0.0f), sendOsc, sendCallback);
  1363. case PARAMETER_CTRL_CHANNEL:
  1364. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  1365. case PARAMETER_DRYWET:
  1366. return setDryWet(value, sendOsc, sendCallback);
  1367. case PARAMETER_VOLUME:
  1368. return setVolume(value, sendOsc, sendCallback);
  1369. case PARAMETER_BALANCE_LEFT:
  1370. return setBalanceLeft(value, sendOsc, sendCallback);
  1371. case PARAMETER_BALANCE_RIGHT:
  1372. return setBalanceRight(value, sendOsc, sendCallback);
  1373. case PARAMETER_PANNING:
  1374. return setPanning(value, sendOsc, sendCallback);
  1375. }
  1376. #endif
  1377. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  1378. for (uint32_t i=0; i < pData->param.count; ++i)
  1379. {
  1380. if (pData->param.data[i].rindex == rindex)
  1381. {
  1382. //if (carla_isNotEqual(getParameterValue(i), value))
  1383. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  1384. break;
  1385. }
  1386. }
  1387. }
  1388. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1389. {
  1390. if (pData->engineBridged) {
  1391. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1392. } else {
  1393. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1394. }
  1395. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1396. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1397. if (pData->param.data[parameterId].midiChannel == channel)
  1398. return;
  1399. pData->param.data[parameterId].midiChannel = channel;
  1400. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1401. pData->engine->callback(sendCallback, sendOsc,
  1402. ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED,
  1403. pData->id,
  1404. static_cast<int>(parameterId),
  1405. channel,
  1406. 0, 0.0f, nullptr);
  1407. #endif
  1408. }
  1409. void CarlaPlugin::setParameterMappedControlIndex(const uint32_t parameterId, const int16_t index,
  1410. const bool sendOsc, const bool sendCallback,
  1411. const bool reconfigureNow) noexcept
  1412. {
  1413. if (pData->engineBridged) {
  1414. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1415. } else {
  1416. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1417. }
  1418. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1419. CARLA_SAFE_ASSERT_RETURN(index >= CONTROL_INDEX_NONE && index <= CONTROL_INDEX_MAX_ALLOWED,);
  1420. ParameterData& paramData(pData->param.data[parameterId]);
  1421. if (paramData.mappedControlIndex == index)
  1422. return;
  1423. const ParameterRanges& paramRanges(pData->param.ranges[parameterId]);
  1424. if ((paramData.hints & PARAMETER_MAPPED_RANGES_SET) == 0x0)
  1425. setParameterMappedRange(parameterId, paramRanges.min, paramRanges.max, true, true);
  1426. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1427. char strBuf[STR_MAX+1];
  1428. carla_zeroChars(strBuf, STR_MAX+1);
  1429. if (! getParameterName(parameterId, strBuf))
  1430. std::snprintf(strBuf, STR_MAX, "Param %u", parameterId);
  1431. const uint portNameSize = pData->engine->getMaxPortNameSize();
  1432. if (portNameSize < STR_MAX)
  1433. strBuf[portNameSize] = '\0';
  1434. // was learning something else before, stop that first
  1435. if (pData->midiLearnParameterIndex >= 0 && pData->midiLearnParameterIndex != static_cast<int32_t>(parameterId))
  1436. {
  1437. const int32_t oldParameterId = pData->midiLearnParameterIndex;
  1438. pData->midiLearnParameterIndex = -1;
  1439. CARLA_SAFE_ASSERT_RETURN(oldParameterId < static_cast<int32_t>(pData->param.count),);
  1440. pData->param.data[oldParameterId].mappedControlIndex = CONTROL_INDEX_NONE;
  1441. pData->engine->callback(true, true,
  1442. ENGINE_CALLBACK_PARAMETER_MAPPED_CONTROL_INDEX_CHANGED,
  1443. pData->id,
  1444. oldParameterId,
  1445. CONTROL_INDEX_NONE,
  1446. 0, 0.0f, nullptr);
  1447. }
  1448. // mapping new parameter to CV
  1449. if (index == CONTROL_INDEX_CV)
  1450. {
  1451. CARLA_SAFE_ASSERT_RETURN(pData->event.cvSourcePorts != nullptr,);
  1452. CARLA_SAFE_ASSERT_RETURN(paramData.type == PARAMETER_INPUT,);
  1453. CARLA_SAFE_ASSERT_RETURN(paramData.hints & PARAMETER_CAN_BE_CV_CONTROLLED,);
  1454. CarlaEngineCVPort* const cvPort =
  1455. (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, strBuf, true, parameterId);
  1456. cvPort->setRange(paramData.mappedMinimum, paramData.mappedMaximum);
  1457. pData->event.cvSourcePorts->addCVSource(cvPort, parameterId, reconfigureNow);
  1458. }
  1459. // unmapping from CV
  1460. else if (paramData.mappedControlIndex == CONTROL_INDEX_CV)
  1461. {
  1462. CARLA_SAFE_ASSERT_RETURN(pData->event.cvSourcePorts != nullptr,);
  1463. CARLA_SAFE_ASSERT(pData->client->removePort(kEnginePortTypeCV, strBuf, true));
  1464. CARLA_SAFE_ASSERT(pData->event.cvSourcePorts->removeCVSource(parameterId));
  1465. }
  1466. // mapping to something new
  1467. else if (paramData.mappedControlIndex == CONTROL_INDEX_NONE)
  1468. {
  1469. // when doing MIDI CC mapping, ensure ranges are within bounds
  1470. if (paramData.mappedMinimum < paramRanges.min || paramData.mappedMaximum > paramRanges.max)
  1471. setParameterMappedRange(parameterId,
  1472. std::max(paramData.mappedMinimum, paramRanges.min),
  1473. std::min(paramData.mappedMaximum, paramRanges.max),
  1474. true, true);
  1475. }
  1476. #endif
  1477. paramData.mappedControlIndex = index;
  1478. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1479. if (index == CONTROL_INDEX_MIDI_LEARN)
  1480. pData->midiLearnParameterIndex = static_cast<int32_t>(parameterId);
  1481. else
  1482. pData->midiLearnParameterIndex = -1;
  1483. pData->engine->callback(sendCallback, sendOsc,
  1484. ENGINE_CALLBACK_PARAMETER_MAPPED_CONTROL_INDEX_CHANGED,
  1485. pData->id,
  1486. static_cast<int>(parameterId),
  1487. index,
  1488. 0, 0.0f, nullptr);
  1489. #else
  1490. return;
  1491. // unused
  1492. (void)reconfigureNow;
  1493. #endif
  1494. }
  1495. void CarlaPlugin::setParameterMappedRange(const uint32_t parameterId, const float minimum, const float maximum,
  1496. const bool sendOsc, const bool sendCallback) noexcept
  1497. {
  1498. if (pData->engineBridged) {
  1499. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1500. } else {
  1501. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1502. }
  1503. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1504. ParameterData& paramData(pData->param.data[parameterId]);
  1505. if (carla_isEqual(paramData.mappedMinimum, minimum) &&
  1506. carla_isEqual(paramData.mappedMaximum, maximum) &&
  1507. (paramData.hints & PARAMETER_MAPPED_RANGES_SET) != 0x0)
  1508. return;
  1509. if (paramData.mappedControlIndex != CONTROL_INDEX_NONE && paramData.mappedControlIndex != CONTROL_INDEX_CV)
  1510. {
  1511. const ParameterRanges& paramRanges(pData->param.ranges[parameterId]);
  1512. CARLA_SAFE_ASSERT_RETURN(minimum >= paramRanges.min,);
  1513. CARLA_SAFE_ASSERT_RETURN(maximum <= paramRanges.max,);
  1514. }
  1515. paramData.hints |= PARAMETER_MAPPED_RANGES_SET;
  1516. paramData.mappedMinimum = minimum;
  1517. paramData.mappedMaximum = maximum;
  1518. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1519. if (pData->event.cvSourcePorts != nullptr && paramData.mappedControlIndex == CONTROL_INDEX_CV)
  1520. pData->event.cvSourcePorts->setCVSourceRange(parameterId, minimum, maximum);
  1521. char strBuf[STR_MAX+1];
  1522. carla_zeroChars(strBuf, STR_MAX+1);
  1523. std::snprintf(strBuf, STR_MAX, "%.12g:%.12g", static_cast<double>(minimum), static_cast<double>(maximum));
  1524. pData->engine->callback(sendCallback, sendOsc,
  1525. ENGINE_CALLBACK_PARAMETER_MAPPED_RANGE_CHANGED,
  1526. pData->id,
  1527. static_cast<int>(parameterId),
  1528. 0, 0, 0.0f,
  1529. strBuf);
  1530. #endif
  1531. }
  1532. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  1533. {
  1534. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1535. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1536. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1537. // Ignore some keys
  1538. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  1539. {
  1540. const PluginType ptype = getType();
  1541. if ((ptype == PLUGIN_INTERNAL && std::strncmp(key, "CarlaAlternateFile", 18) == 0) ||
  1542. (ptype == PLUGIN_DSSI && std::strcmp (key, "guiVisible") == 0) ||
  1543. (ptype == PLUGIN_LV2 && std::strncmp(key, "OSC:", 4) == 0))
  1544. return;
  1545. }
  1546. // Check if we already have this key
  1547. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  1548. {
  1549. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  1550. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1551. if (std::strcmp(customData.key, key) == 0)
  1552. {
  1553. if (customData.value != nullptr)
  1554. delete[] customData.value;
  1555. customData.value = carla_strdup(value);
  1556. return;
  1557. }
  1558. }
  1559. // Otherwise store it
  1560. CustomData customData;
  1561. customData.type = carla_strdup(type);
  1562. customData.key = carla_strdup(key);
  1563. customData.value = carla_strdup(value);
  1564. pData->custom.append(customData);
  1565. }
  1566. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  1567. {
  1568. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1569. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  1570. CARLA_SAFE_ASSERT(false); // this should never happen
  1571. }
  1572. void CarlaPlugin::setProgram(const int32_t index,
  1573. const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1574. {
  1575. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1576. pData->prog.current = index;
  1577. pData->engine->callback(sendCallback, sendOsc,
  1578. ENGINE_CALLBACK_PROGRAM_CHANGED,
  1579. pData->id,
  1580. index,
  1581. 0, 0, 0.0f, nullptr);
  1582. // Change default parameter values
  1583. if (index >= 0)
  1584. {
  1585. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1586. uiProgramChange(static_cast<uint32_t>(index));
  1587. switch (getType())
  1588. {
  1589. case PLUGIN_SF2:
  1590. case PLUGIN_SFZ:
  1591. break;
  1592. default:
  1593. pData->updateParameterValues(this, sendCallback, sendOsc, true);
  1594. break;
  1595. }
  1596. }
  1597. }
  1598. void CarlaPlugin::setMidiProgram(const int32_t index,
  1599. const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1600. {
  1601. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1602. pData->midiprog.current = index;
  1603. pData->engine->callback(sendCallback, sendOsc,
  1604. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  1605. pData->id,
  1606. index,
  1607. 0, 0, 0.0f, nullptr);
  1608. // Change default parameter values
  1609. if (index >= 0)
  1610. {
  1611. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1612. uiMidiProgramChange(static_cast<uint32_t>(index));
  1613. switch (getType())
  1614. {
  1615. case PLUGIN_SF2:
  1616. case PLUGIN_SFZ:
  1617. break;
  1618. default:
  1619. pData->updateParameterValues(this, sendCallback, sendOsc, true);
  1620. break;
  1621. }
  1622. }
  1623. }
  1624. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1625. {
  1626. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1627. {
  1628. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1629. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1630. }
  1631. }
  1632. void CarlaPlugin::setProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept
  1633. {
  1634. CARLA_SAFE_ASSERT_RETURN(uindex < pData->prog.count,);
  1635. const int32_t index = static_cast<int32_t>(uindex);
  1636. pData->prog.current = index;
  1637. // Change default parameter values
  1638. switch (getType())
  1639. {
  1640. case PLUGIN_SF2:
  1641. case PLUGIN_SFZ:
  1642. break;
  1643. default:
  1644. pData->updateDefaultParameterValues(this);
  1645. break;
  1646. }
  1647. pData->postponeProgramChangeRtEvent(sendCallbackLater, uindex);
  1648. }
  1649. void CarlaPlugin::setMidiProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept
  1650. {
  1651. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  1652. const int32_t index = static_cast<int32_t>(uindex);
  1653. pData->midiprog.current = index;
  1654. // Change default parameter values
  1655. switch (getType())
  1656. {
  1657. case PLUGIN_SF2:
  1658. case PLUGIN_SFZ:
  1659. break;
  1660. default:
  1661. pData->updateDefaultParameterValues(this);
  1662. break;
  1663. }
  1664. pData->postponeMidiProgramChangeRtEvent(sendCallbackLater, uindex);
  1665. }
  1666. // -------------------------------------------------------------------
  1667. // Plugin state
  1668. void CarlaPlugin::reloadPrograms(const bool)
  1669. {
  1670. }
  1671. // -------------------------------------------------------------------
  1672. // Plugin processing
  1673. void CarlaPlugin::activate() noexcept
  1674. {
  1675. CARLA_SAFE_ASSERT(! pData->active);
  1676. }
  1677. void CarlaPlugin::deactivate() noexcept
  1678. {
  1679. CARLA_SAFE_ASSERT(pData->active);
  1680. }
  1681. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1682. {
  1683. }
  1684. void CarlaPlugin::sampleRateChanged(const double)
  1685. {
  1686. }
  1687. void CarlaPlugin::offlineModeChanged(const bool)
  1688. {
  1689. }
  1690. // -------------------------------------------------------------------
  1691. // Misc
  1692. void CarlaPlugin::idle()
  1693. {
  1694. if (! pData->enabled)
  1695. return;
  1696. const bool hasUI = pData->hints & PLUGIN_HAS_CUSTOM_UI;
  1697. const bool needsUiMainThread = pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD;
  1698. const uint32_t latency = getLatencyInFrames();
  1699. if (pData->latency.frames != latency)
  1700. {
  1701. carla_stdout("latency changed to %i samples", latency);
  1702. const ScopedSingleProcessLocker sspl(this, true);
  1703. pData->client->setLatency(latency);
  1704. #ifndef BUILD_BRIDGE
  1705. pData->latency.recreateBuffers(pData->latency.channels, latency);
  1706. #else
  1707. pData->latency.frames = latency;
  1708. #endif
  1709. }
  1710. ProtectedData::PostRtEvents::Access rtEvents(pData->postRtEvents);
  1711. if (rtEvents.isEmpty())
  1712. return;
  1713. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = rtEvents.getDataIterator(); it.valid(); it.next())
  1714. {
  1715. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1716. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1717. switch (event.type)
  1718. {
  1719. case kPluginPostRtEventNull: {
  1720. } break;
  1721. case kPluginPostRtEventParameterChange: {
  1722. // Update UI
  1723. if (event.parameter.index >= 0 && hasUI)
  1724. {
  1725. if (needsUiMainThread)
  1726. pData->postUiEvents.append(event);
  1727. else
  1728. uiParameterChange(static_cast<uint32_t>(event.parameter.index), event.parameter.value);
  1729. }
  1730. if (event.sendCallback)
  1731. {
  1732. // Update Host
  1733. pData->engine->callback(true, true,
  1734. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1735. pData->id,
  1736. event.parameter.index,
  1737. 0, 0,
  1738. event.parameter.value,
  1739. nullptr);
  1740. }
  1741. } break;
  1742. case kPluginPostRtEventProgramChange: {
  1743. // Update UI
  1744. if (hasUI)
  1745. {
  1746. if (needsUiMainThread)
  1747. pData->postUiEvents.append(event);
  1748. else
  1749. uiProgramChange(event.program.index);
  1750. }
  1751. // Update param values
  1752. for (uint32_t j=0; j < pData->param.count; ++j)
  1753. {
  1754. const float paramDefault(pData->param.ranges[j].def);
  1755. const float paramValue(getParameterValue(j));
  1756. pData->engine->callback(true, true,
  1757. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1758. pData->id,
  1759. static_cast<int>(j),
  1760. 0, 0,
  1761. paramValue,
  1762. nullptr);
  1763. pData->engine->callback(true, true,
  1764. ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED,
  1765. pData->id,
  1766. static_cast<int>(j),
  1767. 0, 0,
  1768. paramDefault,
  1769. nullptr);
  1770. }
  1771. if (event.sendCallback)
  1772. {
  1773. // Update Host
  1774. pData->engine->callback(true, true,
  1775. ENGINE_CALLBACK_PROGRAM_CHANGED,
  1776. pData->id,
  1777. static_cast<int>(event.program.index),
  1778. 0, 0, 0.0f, nullptr);
  1779. }
  1780. } break;
  1781. case kPluginPostRtEventMidiProgramChange: {
  1782. // Update UI
  1783. if (hasUI)
  1784. {
  1785. if (needsUiMainThread)
  1786. pData->postUiEvents.append(event);
  1787. else
  1788. uiMidiProgramChange(event.program.index);
  1789. }
  1790. // Update param values
  1791. for (uint32_t j=0; j < pData->param.count; ++j)
  1792. {
  1793. const float paramDefault(pData->param.ranges[j].def);
  1794. const float paramValue(getParameterValue(j));
  1795. pData->engine->callback(true, true,
  1796. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1797. pData->id,
  1798. static_cast<int>(j),
  1799. 0, 0,
  1800. paramValue,
  1801. nullptr);
  1802. pData->engine->callback(true, true,
  1803. ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED,
  1804. pData->id,
  1805. static_cast<int>(j),
  1806. 0, 0,
  1807. paramDefault,
  1808. nullptr);
  1809. }
  1810. if (event.sendCallback)
  1811. {
  1812. // Update Host
  1813. pData->engine->callback(true, true,
  1814. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  1815. pData->id,
  1816. static_cast<int>(event.program.index),
  1817. 0, 0, 0.0f, nullptr);
  1818. }
  1819. } break;
  1820. case kPluginPostRtEventNoteOn: {
  1821. CARLA_SAFE_ASSERT_BREAK(event.note.channel < MAX_MIDI_CHANNELS);
  1822. CARLA_SAFE_ASSERT_BREAK(event.note.note < MAX_MIDI_NOTE);
  1823. CARLA_SAFE_ASSERT_BREAK(event.note.velocity < MAX_MIDI_VALUE);
  1824. // Update UI
  1825. if (hasUI)
  1826. {
  1827. if (needsUiMainThread)
  1828. pData->postUiEvents.append(event);
  1829. else
  1830. uiNoteOn(event.note.channel, event.note.note, event.note.velocity);
  1831. }
  1832. if (event.sendCallback)
  1833. {
  1834. // Update Host
  1835. pData->engine->callback(true, true,
  1836. ENGINE_CALLBACK_NOTE_ON,
  1837. pData->id,
  1838. static_cast<int>(event.note.channel),
  1839. static_cast<int>(event.note.note),
  1840. static_cast<int>(event.note.velocity),
  1841. 0.0f, nullptr);
  1842. }
  1843. } break;
  1844. case kPluginPostRtEventNoteOff: {
  1845. CARLA_SAFE_ASSERT_BREAK(event.note.channel < MAX_MIDI_CHANNELS);
  1846. CARLA_SAFE_ASSERT_BREAK(event.note.note < MAX_MIDI_NOTE);
  1847. // Update UI
  1848. if (hasUI)
  1849. {
  1850. if (needsUiMainThread)
  1851. pData->postUiEvents.append(event);
  1852. else
  1853. uiNoteOff(event.note.channel, event.note.note);
  1854. }
  1855. if (event.sendCallback)
  1856. {
  1857. // Update Host
  1858. pData->engine->callback(true, true,
  1859. ENGINE_CALLBACK_NOTE_OFF,
  1860. pData->id,
  1861. static_cast<int>(event.note.channel),
  1862. static_cast<int>(event.note.note),
  1863. 0, 0.0f, nullptr);
  1864. }
  1865. } break;
  1866. case kPluginPostRtEventMidiLearn: {
  1867. CARLA_SAFE_ASSERT_BREAK(event.midiLearn.cc < MAX_MIDI_VALUE);
  1868. CARLA_SAFE_ASSERT_BREAK(event.midiLearn.channel < MAX_MIDI_CHANNELS);
  1869. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1870. if (event.sendCallback)
  1871. {
  1872. pData->engine->callback(true, true,
  1873. ENGINE_CALLBACK_PARAMETER_MAPPED_CONTROL_INDEX_CHANGED,
  1874. pData->id,
  1875. static_cast<int>(event.midiLearn.parameter),
  1876. static_cast<int>(event.midiLearn.cc),
  1877. 0, 0.0f, nullptr);
  1878. pData->engine->callback(true, true,
  1879. ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED,
  1880. pData->id,
  1881. static_cast<int>(event.midiLearn.parameter),
  1882. static_cast<int>(event.midiLearn.channel),
  1883. 0, 0.0f, nullptr);
  1884. }
  1885. #endif
  1886. } break;
  1887. }
  1888. }
  1889. }
  1890. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1891. {
  1892. if (forcedOffline)
  1893. {
  1894. #ifndef STOAT_TEST_BUILD
  1895. pData->masterMutex.lock();
  1896. return true;
  1897. #endif
  1898. }
  1899. return pData->masterMutex.tryLock();
  1900. }
  1901. void CarlaPlugin::unlock() noexcept
  1902. {
  1903. pData->masterMutex.unlock();
  1904. }
  1905. // -------------------------------------------------------------------
  1906. // Plugin buffers
  1907. void CarlaPlugin::initBuffers() const noexcept
  1908. {
  1909. pData->audioIn.initBuffers();
  1910. pData->audioOut.initBuffers();
  1911. pData->cvIn.initBuffers();
  1912. pData->cvOut.initBuffers();
  1913. pData->event.initBuffers();
  1914. }
  1915. void CarlaPlugin::clearBuffers() noexcept
  1916. {
  1917. pData->clearBuffers();
  1918. }
  1919. // -------------------------------------------------------------------
  1920. // OSC stuff
  1921. // FIXME
  1922. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1923. {
  1924. // do nothing
  1925. }
  1926. // -------------------------------------------------------------------
  1927. // MIDI events
  1928. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1929. {
  1930. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1931. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1932. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1933. if (! pData->active)
  1934. return;
  1935. ExternalMidiNote extNote;
  1936. extNote.channel = static_cast<int8_t>(channel);
  1937. extNote.note = note;
  1938. extNote.velo = velo;
  1939. pData->extNotes.appendNonRT(extNote);
  1940. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1941. {
  1942. if (velo > 0)
  1943. uiNoteOn(channel, note, velo);
  1944. else
  1945. uiNoteOff(channel, note);
  1946. }
  1947. pData->engine->callback(sendCallback, sendOsc,
  1948. (velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF,
  1949. pData->id,
  1950. channel,
  1951. note,
  1952. velo,
  1953. 0.0f, nullptr);
  1954. }
  1955. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1956. void CarlaPlugin::postponeRtAllNotesOff()
  1957. {
  1958. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1959. return;
  1960. PluginPostRtEvent postEvent = { kPluginPostRtEventNoteOff, true, {} };
  1961. postEvent.note.channel = static_cast<uint8_t>(pData->ctrlChannel);
  1962. for (uint8_t i=0; i < MAX_MIDI_NOTE; ++i)
  1963. {
  1964. postEvent.note.note = i;
  1965. pData->postRtEvents.appendRT(postEvent);
  1966. }
  1967. }
  1968. #endif
  1969. // -------------------------------------------------------------------
  1970. // UI Stuff
  1971. void CarlaPlugin::setCustomUITitle(const char* const title) noexcept
  1972. {
  1973. pData->uiTitle = title;
  1974. }
  1975. void CarlaPlugin::showCustomUI(const bool yesNo)
  1976. {
  1977. if (yesNo) {
  1978. CARLA_SAFE_ASSERT(false);
  1979. }
  1980. }
  1981. void* CarlaPlugin::embedCustomUI(void*)
  1982. {
  1983. return nullptr;
  1984. }
  1985. void CarlaPlugin::uiIdle()
  1986. {
  1987. if (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD)
  1988. {
  1989. // Update parameter outputs
  1990. for (uint32_t i=0; i < pData->param.count; ++i)
  1991. {
  1992. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1993. uiParameterChange(i, getParameterValue(i));
  1994. }
  1995. const CarlaMutexLocker sl(pData->postUiEvents.mutex);
  1996. for (LinkedList<PluginPostRtEvent>::Itenerator it = pData->postUiEvents.data.begin2(); it.valid(); it.next())
  1997. {
  1998. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1999. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  2000. switch (event.type)
  2001. {
  2002. case kPluginPostRtEventNull:
  2003. case kPluginPostRtEventMidiLearn:
  2004. break;
  2005. case kPluginPostRtEventParameterChange:
  2006. uiParameterChange(static_cast<uint32_t>(event.parameter.index), event.parameter.value);
  2007. break;
  2008. case kPluginPostRtEventProgramChange:
  2009. uiProgramChange(event.program.index);
  2010. break;
  2011. case kPluginPostRtEventMidiProgramChange:
  2012. uiMidiProgramChange(event.program.index);
  2013. break;
  2014. case kPluginPostRtEventNoteOn:
  2015. uiNoteOn(event.note.channel, event.note.note, event.note.velocity);
  2016. break;
  2017. case kPluginPostRtEventNoteOff:
  2018. uiNoteOff(event.note.channel, event.note.note);
  2019. break;
  2020. }
  2021. }
  2022. pData->postUiEvents.data.clear();
  2023. }
  2024. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2025. if (pData->transientTryCounter == 0)
  2026. return;
  2027. if (++pData->transientTryCounter % 10 != 0)
  2028. return;
  2029. if (pData->transientTryCounter >= 200)
  2030. return;
  2031. carla_stdout("Trying to get window...");
  2032. CarlaString uiTitle;
  2033. if (pData->uiTitle.isNotEmpty())
  2034. {
  2035. uiTitle = pData->uiTitle;
  2036. }
  2037. else
  2038. {
  2039. uiTitle = pData->name;
  2040. uiTitle += " (GUI)";
  2041. }
  2042. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle,
  2043. pData->engine->getOptions().frontendWinId, pData->transientFirstTry))
  2044. {
  2045. pData->transientTryCounter = 0;
  2046. pData->transientFirstTry = false;
  2047. }
  2048. #endif
  2049. }
  2050. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  2051. {
  2052. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  2053. return;
  2054. // unused
  2055. (void)value;
  2056. }
  2057. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  2058. {
  2059. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  2060. }
  2061. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  2062. {
  2063. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  2064. }
  2065. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  2066. {
  2067. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2068. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2069. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2070. }
  2071. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  2072. {
  2073. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2074. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2075. }
  2076. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  2077. {
  2078. return pData->engine;
  2079. }
  2080. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  2081. {
  2082. return pData->client;
  2083. }
  2084. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  2085. {
  2086. return pData->audioIn.ports[index].port;
  2087. }
  2088. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  2089. {
  2090. return pData->audioOut.ports[index].port;
  2091. }
  2092. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  2093. {
  2094. return pData->cvIn.ports[index].port;
  2095. }
  2096. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  2097. {
  2098. return pData->cvOut.ports[index].port;
  2099. }
  2100. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  2101. {
  2102. return pData->event.portIn;
  2103. }
  2104. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  2105. {
  2106. return pData->event.portOut;
  2107. }
  2108. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2109. void CarlaPlugin::checkForMidiLearn(EngineEvent& event) noexcept
  2110. {
  2111. if (pData->midiLearnParameterIndex < 0)
  2112. return;
  2113. if (event.ctrl.param == MIDI_CONTROL_BANK_SELECT || event.ctrl.param == MIDI_CONTROL_BANK_SELECT__LSB)
  2114. return;
  2115. if (event.ctrl.param >= MAX_MIDI_CONTROL)
  2116. return;
  2117. const uint32_t parameterId = static_cast<uint32_t>(pData->midiLearnParameterIndex);
  2118. CARLA_SAFE_ASSERT_UINT2_RETURN(parameterId < pData->param.count, parameterId, pData->param.count,);
  2119. ParameterData& paramData(pData->param.data[parameterId]);
  2120. CARLA_SAFE_ASSERT_INT_RETURN(paramData.mappedControlIndex == CONTROL_INDEX_MIDI_LEARN,
  2121. paramData.mappedControlIndex,);
  2122. event.ctrl.handled = true;
  2123. paramData.mappedControlIndex = static_cast<int16_t>(event.ctrl.param);
  2124. paramData.midiChannel = event.channel;
  2125. pData->postponeMidiLearnRtEvent(true, parameterId, static_cast<uint8_t>(event.ctrl.param), event.channel);
  2126. pData->midiLearnParameterIndex = -1;
  2127. }
  2128. #endif
  2129. void* CarlaPlugin::getNativeHandle() const noexcept
  2130. {
  2131. return nullptr;
  2132. }
  2133. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  2134. {
  2135. return nullptr;
  2136. }
  2137. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  2138. {
  2139. return 0;
  2140. }
  2141. // -------------------------------------------------------------------
  2142. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  2143. {
  2144. return pData->nodeId;
  2145. }
  2146. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  2147. {
  2148. pData->nodeId = nodeId;
  2149. }
  2150. // -------------------------------------------------------------------
  2151. void CarlaPlugin::cloneLV2Files(const CarlaPlugin&)
  2152. {
  2153. carla_stderr2("Warning: cloneLV2Files() called for non-implemented type");
  2154. }
  2155. void CarlaPlugin::restoreLV2State(const bool temporary) noexcept
  2156. {
  2157. carla_stderr2("Warning: restoreLV2State(%s) called for non-implemented type", bool2str(temporary));
  2158. }
  2159. void CarlaPlugin::prepareForDeletion() noexcept
  2160. {
  2161. carla_debug("CarlaPlugin::prepareForDeletion");
  2162. const CarlaMutexLocker cml(pData->masterMutex);
  2163. pData->client->deactivate(true);
  2164. }
  2165. void CarlaPlugin::waitForBridgeSaveSignal() noexcept
  2166. {
  2167. }
  2168. // -------------------------------------------------------------------
  2169. // Scoped Disabler
  2170. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  2171. : fPlugin(plugin),
  2172. fWasEnabled(false)
  2173. {
  2174. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  2175. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  2176. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  2177. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  2178. plugin->pData->masterMutex.lock();
  2179. if (plugin->pData->enabled)
  2180. {
  2181. fWasEnabled = true;
  2182. plugin->pData->enabled = false;
  2183. if (plugin->pData->client->isActive())
  2184. plugin->pData->client->deactivate(false);
  2185. }
  2186. }
  2187. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  2188. {
  2189. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2190. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2191. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  2192. carla_debug("CarlaPlugin::~ScopedDisabler()");
  2193. if (fWasEnabled)
  2194. {
  2195. fPlugin->pData->enabled = true;
  2196. fPlugin->pData->client->activate();
  2197. }
  2198. fPlugin->pData->masterMutex.unlock();
  2199. }
  2200. // -------------------------------------------------------------------
  2201. // Scoped Process Locker
  2202. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  2203. : fPlugin(plugin),
  2204. fBlock(block)
  2205. {
  2206. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2207. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2208. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  2209. if (! fBlock)
  2210. return;
  2211. plugin->pData->singleMutex.lock();
  2212. }
  2213. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  2214. {
  2215. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2216. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2217. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  2218. if (! fBlock)
  2219. return;
  2220. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2221. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  2222. fPlugin->pData->needsReset = true;
  2223. #endif
  2224. fPlugin->pData->singleMutex.unlock();
  2225. }
  2226. // -------------------------------------------------------------------
  2227. CARLA_BACKEND_END_NAMESPACE