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.

2738 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)
  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)
  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. carla_stderr("Failed to find LV2 parameter symbol '%s' for '%s'",
  667. stateParameter->symbol, pData->name);
  668. }
  669. else
  670. {
  671. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  672. }
  673. }
  674. else
  675. {
  676. // Index only
  677. index = stateParameter->index;
  678. }
  679. // Now set parameter
  680. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  681. {
  682. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  683. if (! stateParameter->dummy)
  684. {
  685. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  686. stateParameter->value *= sampleRate;
  687. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  688. }
  689. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  690. if (stateParameter->mappedRangeValid)
  691. {
  692. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  693. {
  694. stateParameter->mappedMinimum *= sampleRate;
  695. stateParameter->mappedMaximum *= sampleRate;
  696. }
  697. setParameterMappedRange(static_cast<uint32_t>(index),
  698. stateParameter->mappedMinimum,
  699. stateParameter->mappedMaximum, true, true);
  700. }
  701. setParameterMappedControlIndex(static_cast<uint32_t>(index),
  702. stateParameter->mappedControlIndex, true, true, false);
  703. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  704. #endif
  705. }
  706. else
  707. carla_stderr("Could not set parameter '%s' value for '%s'",
  708. stateParameter->name, pData->name);
  709. }
  710. // ---------------------------------------------------------------
  711. // Part 4c - clear
  712. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin2(); it.valid(); it.next())
  713. {
  714. ParamSymbol* const paramSymbol(it.getValue(nullptr));
  715. delete paramSymbol;
  716. }
  717. paramSymbols.clear();
  718. // ---------------------------------------------------------------
  719. // Part 5 - set custom data
  720. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  721. {
  722. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  723. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  724. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  725. const char* const key(stateCustomData->key);
  726. if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  727. std::strcmp (key, "load" ) == 0 ||
  728. std::strncmp(key, "patches", 7) == 0 ))
  729. continue;
  730. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  731. continue;
  732. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  733. }
  734. // ---------------------------------------------------------------
  735. // Part 5x - set lv2 state
  736. if (pluginType == PLUGIN_LV2)
  737. {
  738. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  739. {
  740. const CustomData& customData(it.getValue(kCustomDataFallback));
  741. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  742. if (std::strcmp(customData.type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  743. continue;
  744. restoreLV2State(stateSave.temporary);
  745. break;
  746. }
  747. }
  748. // ---------------------------------------------------------------
  749. // Part 6 - set chunk
  750. if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  751. {
  752. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stateSave.chunk));
  753. #ifdef CARLA_PROPER_CPP11_SUPPORT
  754. setChunkData(chunk.data(), chunk.size());
  755. #else
  756. setChunkData(&chunk.front(), chunk.size());
  757. #endif
  758. }
  759. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  760. // ---------------------------------------------------------------
  761. // Part 6 - set internal stuff
  762. const uint availOptions(getOptionsAvailable());
  763. for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
  764. {
  765. const uint option(1u << i);
  766. if (availOptions & option)
  767. setOption(option, (stateSave.options & option) != 0, true);
  768. }
  769. setDryWet(stateSave.dryWet, true, true);
  770. setVolume(stateSave.volume, true, true);
  771. setBalanceLeft(stateSave.balanceLeft, true, true);
  772. setBalanceRight(stateSave.balanceRight, true, true);
  773. setPanning(stateSave.panning, true, true);
  774. setCtrlChannel(stateSave.ctrlChannel, true, true);
  775. setActive(stateSave.active, true, true);
  776. if (! pData->engine->isLoadingProject())
  777. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0, 0.0f, nullptr);
  778. #endif
  779. }
  780. bool CarlaPlugin::saveStateToFile(const char* const filename)
  781. {
  782. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  783. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  784. MemoryOutputStream out, streamState;
  785. getStateSave().dumpToMemoryStream(streamState);
  786. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  787. out << "<!DOCTYPE CARLA-PRESET>\n";
  788. out << "<CARLA-PRESET VERSION='2.0'>\n";
  789. out << streamState;
  790. out << "</CARLA-PRESET>\n";
  791. const String jfilename = String(CharPointer_UTF8(filename));
  792. File file(jfilename);
  793. if (file.replaceWithData(out.getData(), out.getDataSize()))
  794. return true;
  795. pData->engine->setLastError("Failed to write file");
  796. return false;
  797. }
  798. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  799. {
  800. // TODO set errors
  801. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  802. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  803. const String jfilename = String(CharPointer_UTF8(filename));
  804. File file(jfilename);
  805. CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
  806. XmlDocument xml(file);
  807. CarlaScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
  808. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  809. CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
  810. // completely load file
  811. xmlElement = xml.getDocumentElement(false);
  812. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  813. if (pData->stateSave.fillFromXmlElement(xmlElement))
  814. {
  815. loadStateSave(pData->stateSave);
  816. return true;
  817. }
  818. return false;
  819. }
  820. bool CarlaPlugin::exportAsLV2(const char* const lv2path)
  821. {
  822. CARLA_SAFE_ASSERT_RETURN(lv2path != nullptr && lv2path[0] != '\0', false);
  823. carla_debug("CarlaPlugin::exportAsLV2(\"%s\")", lv2path);
  824. CarlaString bundlepath(lv2path);
  825. if (! bundlepath.endsWith(".lv2"))
  826. bundlepath += ".lv2";
  827. const File bundlefolder(bundlepath.buffer());
  828. if (bundlefolder.existsAsFile())
  829. {
  830. pData->engine->setLastError("Requested filename already exists as file, use a folder instead");
  831. return false;
  832. }
  833. if (! bundlefolder.exists())
  834. {
  835. const Result res(bundlefolder.createDirectory());
  836. if (res.failed())
  837. {
  838. pData->engine->setLastError(res.getErrorMessage().c_str());
  839. return false;
  840. }
  841. }
  842. CarlaString symbol(pData->name);
  843. symbol.toBasic();
  844. {
  845. const CarlaString pluginFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".xml");
  846. if (! saveStateToFile(pluginFilename))
  847. return false;
  848. }
  849. {
  850. MemoryOutputStream manifestStream;
  851. manifestStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  852. manifestStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  853. manifestStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  854. manifestStream << "\n";
  855. manifestStream << "<" << symbol.buffer() << ".ttl>\n";
  856. manifestStream << " a lv2:Plugin ;\n";
  857. manifestStream << " lv2:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  858. manifestStream << " rdfs:seeAlso <" << symbol.buffer() << ".ttl> .\n";
  859. manifestStream << "\n";
  860. manifestStream << "<ext-ui>\n";
  861. manifestStream << " a <http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget> ;\n";
  862. manifestStream << " ui:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  863. manifestStream << " lv2:extensionData <http://lv2plug.in/ns/extensions/ui#idleInterface> ,\n";
  864. manifestStream << " <http://lv2plug.in/ns/extensions/ui#showInterface> ;\n";
  865. manifestStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> .\n";
  866. manifestStream << "\n";
  867. const CarlaString manifestFilename(bundlepath + CARLA_OS_SEP_STR "manifest.ttl");
  868. const File manifestFile(manifestFilename.buffer());
  869. if (! manifestFile.replaceWithData(manifestStream.getData(), manifestStream.getDataSize()))
  870. {
  871. pData->engine->setLastError("Failed to write manifest.ttl file");
  872. return false;
  873. }
  874. }
  875. {
  876. MemoryOutputStream mainStream;
  877. mainStream << "@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n";
  878. mainStream << "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  879. mainStream << "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
  880. mainStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  881. mainStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  882. mainStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  883. mainStream << "\n";
  884. mainStream << "<>\n";
  885. mainStream << " a lv2:Plugin ;\n";
  886. mainStream << "\n";
  887. mainStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/buf-size#boundedBlockLength> ,\n";
  888. mainStream << " <http://lv2plug.in/ns/ext/options#options> ,\n";
  889. mainStream << " <http://lv2plug.in/ns/ext/urid#map> ;\n";
  890. mainStream << "\n";
  891. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  892. {
  893. mainStream << " ui:ui <ext-ui> ;\n";
  894. mainStream << "\n";
  895. }
  896. const uint32_t midiIns = getMidiInCount();
  897. const uint32_t midiOuts = getMidiOutCount();
  898. int portIndex = 0;
  899. if (midiIns > 0)
  900. {
  901. mainStream << " lv2:port [\n";
  902. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  903. mainStream << " lv2:index 0 ;\n";
  904. mainStream << " lv2:symbol \"clv2_events_in\" ;\n";
  905. mainStream << " lv2:name \"Events Input\" ;\n";
  906. mainStream << " atom:bufferType atom:Sequence ;\n";
  907. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ,\n";
  908. mainStream << " <http://lv2plug.in/ns/ext/time#Position> ;\n";
  909. mainStream << " ] ;\n";
  910. ++portIndex;
  911. for (uint32_t i=1; i<midiIns; ++i)
  912. {
  913. const String portIndexNum(portIndex++);
  914. const String portIndexLabel(portIndex);
  915. mainStream << " lv2:port [\n";
  916. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  917. mainStream << " lv2:index " << portIndexNum << " ;\n";
  918. mainStream << " lv2:symbol \"clv2_midi_in_" << portIndexLabel << "\" ;\n";
  919. mainStream << " lv2:name \"MIDI Input " << portIndexLabel << "\" ;\n";
  920. mainStream << " ] ;\n";
  921. }
  922. }
  923. else
  924. {
  925. mainStream << " lv2:port [\n";
  926. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  927. mainStream << " lv2:index 0 ;\n";
  928. mainStream << " lv2:symbol \"clv2_time_info\" ;\n";
  929. mainStream << " lv2:name \"Time Info\" ;\n";
  930. mainStream << " atom:bufferType atom:Sequence ;\n";
  931. mainStream << " atom:supports <http://lv2plug.in/ns/ext/time#Position> ;\n";
  932. mainStream << " ] ;\n";
  933. ++portIndex;
  934. }
  935. for (uint32_t i=0; i<midiOuts; ++i)
  936. {
  937. const String portIndexNum(portIndex++);
  938. const String portIndexLabel(portIndex);
  939. mainStream << " lv2:port [\n";
  940. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  941. mainStream << " lv2:index " << portIndexNum << " ;\n";
  942. mainStream << " lv2:symbol \"clv2_midi_out_" << portIndexLabel << "\" ;\n";
  943. mainStream << " lv2:name \"MIDI Output " << portIndexLabel << "\" ;\n";
  944. mainStream << " atom:bufferType atom:Sequence ;\n";
  945. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n";
  946. mainStream << " ] ;\n";
  947. }
  948. mainStream << " lv2:port [\n";
  949. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  950. mainStream << " lv2:index " << String(portIndex++) << " ;\n";
  951. mainStream << " lv2:name \"freewheel\" ;\n";
  952. mainStream << " lv2:symbol \"clv2_freewheel\" ;\n";
  953. mainStream << " lv2:default 0 ;\n";
  954. mainStream << " lv2:minimum 0 ;\n";
  955. mainStream << " lv2:maximum 1 ;\n";
  956. mainStream << " lv2:designation lv2:freeWheeling ;\n";
  957. mainStream << " lv2:portProperty lv2:toggled , lv2:integer ;\n";
  958. mainStream << " lv2:portProperty <http://lv2plug.in/ns/ext/port-props#notOnGUI> ;\n";
  959. mainStream << " ] ;\n";
  960. for (uint32_t i=0; i<pData->audioIn.count; ++i)
  961. {
  962. const String portIndexNum(portIndex++);
  963. const String portIndexLabel(i+1);
  964. mainStream << " lv2:port [\n";
  965. mainStream << " a lv2:InputPort, lv2:AudioPort ;\n";
  966. mainStream << " lv2:index " << portIndexNum << " ;\n";
  967. mainStream << " lv2:symbol \"clv2_audio_in_" << portIndexLabel << "\" ;\n";
  968. mainStream << " lv2:name \"Audio Input " << portIndexLabel << "\" ;\n";
  969. mainStream << " ] ;\n";
  970. }
  971. for (uint32_t i=0; i<pData->audioOut.count; ++i)
  972. {
  973. const String portIndexNum(portIndex++);
  974. const String portIndexLabel(i+1);
  975. mainStream << " lv2:port [\n";
  976. mainStream << " a lv2:OutputPort, lv2:AudioPort ;\n";
  977. mainStream << " lv2:index " << portIndexNum << " ;\n";
  978. mainStream << " lv2:symbol \"clv2_audio_out_" << portIndexLabel << "\" ;\n";
  979. mainStream << " lv2:name \"Audio Output " << portIndexLabel << "\" ;\n";
  980. mainStream << " ] ;\n";
  981. }
  982. CarlaStringList uniqueSymbolNames;
  983. char strBufName[STR_MAX+1];
  984. char strBufSymbol[STR_MAX+1];
  985. carla_zeroChars(strBufName, STR_MAX+1);
  986. carla_zeroChars(strBufSymbol, STR_MAX+1);
  987. for (uint32_t i=0; i<pData->param.count; ++i)
  988. {
  989. const ParameterData& paramData(pData->param.data[i]);
  990. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  991. const String portIndexNum(portIndex++);
  992. mainStream << " lv2:port [\n";
  993. if (paramData.type == PARAMETER_INPUT)
  994. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  995. else
  996. mainStream << " a lv2:OutputPort, lv2:ControlPort ;\n";
  997. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  998. mainStream << " lv2:portProperty lv2:toggled ;\n";
  999. if (paramData.hints & PARAMETER_IS_INTEGER)
  1000. mainStream << " lv2:portProperty lv2:integer ;\n";
  1001. // TODO logarithmic, enabled (not on gui), automatable, samplerate, scalepoints
  1002. if (! getParameterName(i, strBufName))
  1003. strBufName[0] = '\0';
  1004. if (! getParameterSymbol(i, strBufSymbol))
  1005. strBufSymbol[0] = '\0';
  1006. if (strBufSymbol[0] == '\0')
  1007. {
  1008. CarlaString s(strBufName);
  1009. s.toBasic();
  1010. std::memcpy(strBufSymbol, s.buffer(), s.length()+1);
  1011. if (strBufSymbol[0] >= '0' && strBufSymbol[0] <= '9')
  1012. {
  1013. const size_t len(std::strlen(strBufSymbol));
  1014. std::memmove(strBufSymbol+1, strBufSymbol, len);
  1015. strBufSymbol[0] = '_';
  1016. strBufSymbol[len+1] = '\0';
  1017. }
  1018. }
  1019. if (uniqueSymbolNames.contains(strBufSymbol))
  1020. std::snprintf(strBufSymbol, STR_MAX, "clv2_param_%d", i+1);
  1021. mainStream << " lv2:index " << portIndexNum << " ;\n";
  1022. mainStream << " lv2:symbol \"" << strBufSymbol << "\" ;\n";
  1023. mainStream << " lv2:name \"\"\"" << strBufName << "\"\"\" ;\n";
  1024. mainStream << " lv2:default " << String(paramRanges.def) << " ;\n";
  1025. mainStream << " lv2:minimum " << String(paramRanges.min) << " ;\n";
  1026. mainStream << " lv2:maximum " << String(paramRanges.max) << " ;\n";
  1027. // TODO midiCC, midiChannel
  1028. mainStream << " ] ;\n";
  1029. }
  1030. char strBuf[STR_MAX+1];
  1031. carla_zeroChars(strBuf, STR_MAX+1);
  1032. if (! getMaker(strBuf))
  1033. strBuf[0] = '\0';
  1034. mainStream << " rdfs:comment \"Plugin generated using Carla LV2 export.\" ;\n";
  1035. mainStream << " doap:name \"\"\"" << getName() << "\"\"\" ;\n";
  1036. mainStream << " doap:maintainer [ foaf:name \"\"\"" << strBuf << "\"\"\" ] .\n";
  1037. mainStream << "\n";
  1038. const CarlaString mainFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".ttl");
  1039. const File mainFile(mainFilename.buffer());
  1040. if (! mainFile.replaceWithData(mainStream.getData(), mainStream.getDataSize()))
  1041. {
  1042. pData->engine->setLastError("Failed to write main plugin ttl file");
  1043. return false;
  1044. }
  1045. }
  1046. const CarlaString binaryFilename(bundlepath + CARLA_OS_SEP_STR + symbol + CARLA_LIB_EXT);
  1047. const File binaryFileSource(File::getSpecialLocation(File::currentExecutableFile).getSiblingFile("carla-bridge-lv2" CARLA_LIB_EXT));
  1048. const File binaryFileTarget(binaryFilename.buffer());
  1049. const EngineOptions& opts(pData->engine->getOptions());
  1050. const CarlaString binFolderTarget(bundlepath + CARLA_OS_SEP_STR + "bin");
  1051. const CarlaString resFolderTarget(bundlepath + CARLA_OS_SEP_STR + "res");
  1052. if (! binaryFileSource.copyFileTo(binaryFileTarget))
  1053. {
  1054. pData->engine->setLastError("Failed to copy plugin binary");
  1055. return false;
  1056. }
  1057. #ifdef CARLA_OS_WIN
  1058. File(opts.resourceDir).copyDirectoryTo(File(resFolderTarget.buffer()));
  1059. // Copying all the binaries is pointless, just go through the expected needed bits
  1060. const File binFolder1(opts.binaryDir);
  1061. const File binFolder2(binFolderTarget.buffer());
  1062. binFolder2.createDirectory();
  1063. static const char* files[] = {
  1064. "carla-bridge-native.exe",
  1065. "carla-bridge-win32.exe",
  1066. "carla-discovery-win32.exe",
  1067. "carla-discovery-win64.exe",
  1068. "libcarla_utils.dll"
  1069. };
  1070. for (int i=0; i<5; ++i)
  1071. binFolder1.getChildFile(files[i]).copyFileTo(binFolder2.getChildFile(files[i]));;
  1072. #else
  1073. File(opts.binaryDir).createSymbolicLink(File(binFolderTarget.buffer()), true);
  1074. File(opts.resourceDir).createSymbolicLink(File(resFolderTarget.buffer()), true);
  1075. #endif
  1076. return true;
  1077. }
  1078. // -------------------------------------------------------------------
  1079. // Set data (internal stuff)
  1080. void CarlaPlugin::setId(const uint newId) noexcept
  1081. {
  1082. pData->id = newId;
  1083. }
  1084. void CarlaPlugin::setName(const char* const newName)
  1085. {
  1086. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  1087. if (pData->name != nullptr)
  1088. delete[] pData->name;
  1089. pData->name = carla_strdup(newName);
  1090. }
  1091. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  1092. {
  1093. CARLA_SAFE_ASSERT_UINT2_RETURN(getOptionsAvailable() & option, getOptionsAvailable(), option,);
  1094. if (yesNo)
  1095. pData->options |= option;
  1096. else
  1097. pData->options &= ~option;
  1098. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1099. if (sendCallback)
  1100. pData->engine->callback(true, true,
  1101. ENGINE_CALLBACK_OPTION_CHANGED,
  1102. pData->id,
  1103. static_cast<int>(option),
  1104. yesNo ? 1 : 0,
  1105. 0, 0.0f, nullptr);
  1106. #else
  1107. // unused
  1108. return; (void)sendCallback;
  1109. #endif
  1110. }
  1111. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  1112. {
  1113. if (pData->enabled == yesNo)
  1114. return;
  1115. pData->masterMutex.lock();
  1116. pData->enabled = yesNo;
  1117. if (yesNo && ! pData->client->isActive())
  1118. pData->client->activate();
  1119. pData->masterMutex.unlock();
  1120. }
  1121. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  1122. {
  1123. if (pData->engineBridged) {
  1124. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1125. } else if (pData->enginePlugin) {
  1126. // nothing here
  1127. } else {
  1128. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1129. }
  1130. if (pData->active == active)
  1131. return;
  1132. {
  1133. const ScopedSingleProcessLocker spl(this, true);
  1134. if (active)
  1135. activate();
  1136. else
  1137. deactivate();
  1138. }
  1139. pData->active = active;
  1140. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1141. const float value = active ? 1.0f : 0.0f;
  1142. pData->engine->callback(sendCallback, sendOsc,
  1143. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1144. pData->id,
  1145. PARAMETER_ACTIVE,
  1146. 0, 0,
  1147. value,
  1148. nullptr);
  1149. #endif
  1150. }
  1151. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1152. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1153. {
  1154. if (pData->engineBridged) {
  1155. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1156. } else {
  1157. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1158. }
  1159. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1160. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1161. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1162. return;
  1163. pData->postProc.dryWet = fixedValue;
  1164. pData->engine->callback(sendCallback, sendOsc,
  1165. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1166. pData->id,
  1167. PARAMETER_DRYWET,
  1168. 0, 0,
  1169. fixedValue,
  1170. nullptr);
  1171. }
  1172. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1173. {
  1174. if (pData->engineBridged) {
  1175. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1176. } else {
  1177. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1178. }
  1179. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1180. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1181. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1182. return;
  1183. pData->postProc.volume = fixedValue;
  1184. pData->engine->callback(sendCallback, sendOsc,
  1185. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1186. pData->id,
  1187. PARAMETER_VOLUME,
  1188. 0, 0,
  1189. fixedValue,
  1190. nullptr);
  1191. }
  1192. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1193. {
  1194. if (pData->engineBridged) {
  1195. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1196. } else {
  1197. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1198. }
  1199. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1200. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1201. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1202. return;
  1203. pData->postProc.balanceLeft = fixedValue;
  1204. pData->engine->callback(sendCallback, sendOsc,
  1205. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1206. pData->id,
  1207. PARAMETER_BALANCE_LEFT,
  1208. 0, 0,
  1209. fixedValue,
  1210. nullptr);
  1211. }
  1212. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1213. {
  1214. if (pData->engineBridged) {
  1215. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1216. } else {
  1217. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1218. }
  1219. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1220. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1221. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1222. return;
  1223. pData->postProc.balanceRight = fixedValue;
  1224. pData->engine->callback(sendCallback, sendOsc,
  1225. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1226. pData->id,
  1227. PARAMETER_BALANCE_RIGHT,
  1228. 0, 0,
  1229. fixedValue,
  1230. nullptr);
  1231. }
  1232. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1233. {
  1234. if (pData->engineBridged) {
  1235. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1236. } else {
  1237. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1238. }
  1239. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1240. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1241. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1242. return;
  1243. pData->postProc.panning = fixedValue;
  1244. pData->engine->callback(sendCallback, sendOsc,
  1245. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1246. pData->id,
  1247. PARAMETER_PANNING,
  1248. 0, 0,
  1249. fixedValue,
  1250. nullptr);
  1251. }
  1252. void CarlaPlugin::setDryWetRT(const float value, const bool sendCallbackLater) noexcept
  1253. {
  1254. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1255. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1256. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1257. return;
  1258. pData->postProc.dryWet = fixedValue;
  1259. pData->postponeParameterChangeRtEvent(sendCallbackLater, PARAMETER_DRYWET, fixedValue);
  1260. }
  1261. void CarlaPlugin::setVolumeRT(const float value, const bool sendCallbackLater) noexcept
  1262. {
  1263. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1264. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1265. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1266. return;
  1267. pData->postProc.volume = fixedValue;
  1268. pData->postponeParameterChangeRtEvent(sendCallbackLater, PARAMETER_VOLUME, fixedValue);
  1269. }
  1270. void CarlaPlugin::setBalanceLeftRT(const float value, const bool sendCallbackLater) noexcept
  1271. {
  1272. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1273. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1274. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1275. return;
  1276. pData->postProc.balanceLeft = fixedValue;
  1277. pData->postponeParameterChangeRtEvent(sendCallbackLater, PARAMETER_BALANCE_LEFT, fixedValue);
  1278. }
  1279. void CarlaPlugin::setBalanceRightRT(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.balanceRight, fixedValue))
  1284. return;
  1285. pData->postProc.balanceRight = fixedValue;
  1286. pData->postponeParameterChangeRtEvent(sendCallbackLater, PARAMETER_BALANCE_RIGHT, fixedValue);
  1287. }
  1288. void CarlaPlugin::setPanningRT(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.panning, fixedValue))
  1293. return;
  1294. pData->postProc.panning = fixedValue;
  1295. pData->postponeParameterChangeRtEvent(sendCallbackLater, PARAMETER_PANNING, fixedValue);
  1296. }
  1297. #endif // ! BUILD_BRIDGE_ALTERNATIVE_ARCH
  1298. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1299. {
  1300. if (pData->engineBridged) {
  1301. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1302. } else {
  1303. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1304. }
  1305. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  1306. if (pData->ctrlChannel == channel)
  1307. return;
  1308. pData->ctrlChannel = channel;
  1309. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1310. const float channelf = static_cast<float>(channel);
  1311. pData->engine->callback(sendCallback, sendOsc,
  1312. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1313. pData->id,
  1314. PARAMETER_CTRL_CHANNEL,
  1315. 0, 0,
  1316. channelf, nullptr);
  1317. #endif
  1318. }
  1319. // -------------------------------------------------------------------
  1320. // Set data (plugin-specific stuff)
  1321. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1322. {
  1323. if (pData->engineBridged) {
  1324. // NOTE: some LV2 plugins feedback messages to UI on purpose
  1325. CARLA_SAFE_ASSERT_RETURN(getType() == PLUGIN_LV2 || !sendGui,);
  1326. } else if (pData->enginePlugin) {
  1327. // nothing here
  1328. } else {
  1329. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback,); // never call this from RT
  1330. }
  1331. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1332. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1333. uiParameterChange(parameterId, value);
  1334. pData->engine->callback(sendCallback, sendOsc,
  1335. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1336. pData->id,
  1337. static_cast<int>(parameterId),
  1338. 0, 0,
  1339. value,
  1340. nullptr);
  1341. }
  1342. void CarlaPlugin::setParameterValueRT(const uint32_t parameterId, const float value, uint32_t, const bool sendCallbackLater) noexcept
  1343. {
  1344. pData->postponeParameterChangeRtEvent(sendCallbackLater, static_cast<int32_t>(parameterId), value);
  1345. }
  1346. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1347. {
  1348. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1349. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  1350. switch (rindex)
  1351. {
  1352. case PARAMETER_ACTIVE:
  1353. return setActive((value > 0.0f), sendOsc, sendCallback);
  1354. case PARAMETER_CTRL_CHANNEL:
  1355. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  1356. case PARAMETER_DRYWET:
  1357. return setDryWet(value, sendOsc, sendCallback);
  1358. case PARAMETER_VOLUME:
  1359. return setVolume(value, sendOsc, sendCallback);
  1360. case PARAMETER_BALANCE_LEFT:
  1361. return setBalanceLeft(value, sendOsc, sendCallback);
  1362. case PARAMETER_BALANCE_RIGHT:
  1363. return setBalanceRight(value, sendOsc, sendCallback);
  1364. case PARAMETER_PANNING:
  1365. return setPanning(value, sendOsc, sendCallback);
  1366. }
  1367. #endif
  1368. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  1369. for (uint32_t i=0; i < pData->param.count; ++i)
  1370. {
  1371. if (pData->param.data[i].rindex == rindex)
  1372. {
  1373. //if (carla_isNotEqual(getParameterValue(i), value))
  1374. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  1375. break;
  1376. }
  1377. }
  1378. }
  1379. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1380. {
  1381. if (pData->engineBridged) {
  1382. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1383. } else {
  1384. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1385. }
  1386. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1387. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1388. if (pData->param.data[parameterId].midiChannel == channel)
  1389. return;
  1390. pData->param.data[parameterId].midiChannel = channel;
  1391. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1392. pData->engine->callback(sendCallback, sendOsc,
  1393. ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED,
  1394. pData->id,
  1395. static_cast<int>(parameterId),
  1396. channel,
  1397. 0, 0.0f, nullptr);
  1398. #endif
  1399. }
  1400. void CarlaPlugin::setParameterMappedControlIndex(const uint32_t parameterId, const int16_t index,
  1401. const bool sendOsc, const bool sendCallback,
  1402. const bool reconfigureNow) noexcept
  1403. {
  1404. if (pData->engineBridged) {
  1405. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1406. } else {
  1407. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1408. }
  1409. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1410. CARLA_SAFE_ASSERT_RETURN(index >= CONTROL_INDEX_NONE && index <= CONTROL_INDEX_MAX_ALLOWED,);
  1411. ParameterData& paramData(pData->param.data[parameterId]);
  1412. if (paramData.mappedControlIndex == index)
  1413. return;
  1414. const ParameterRanges& paramRanges(pData->param.ranges[parameterId]);
  1415. if ((paramData.hints & PARAMETER_MAPPED_RANGES_SET) == 0x0)
  1416. setParameterMappedRange(parameterId, paramRanges.min, paramRanges.max, true, true);
  1417. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1418. char strBuf[STR_MAX+1];
  1419. carla_zeroChars(strBuf, STR_MAX+1);
  1420. if (! getParameterName(parameterId, strBuf))
  1421. std::snprintf(strBuf, STR_MAX, "Param %u", parameterId);
  1422. const uint portNameSize = pData->engine->getMaxPortNameSize();
  1423. if (portNameSize < STR_MAX)
  1424. strBuf[portNameSize] = '\0';
  1425. // was learning something else before, stop that first
  1426. if (pData->midiLearnParameterIndex >= 0 && pData->midiLearnParameterIndex != static_cast<int32_t>(parameterId))
  1427. {
  1428. const int32_t oldParameterId = pData->midiLearnParameterIndex;
  1429. pData->midiLearnParameterIndex = -1;
  1430. CARLA_SAFE_ASSERT_RETURN(oldParameterId < static_cast<int32_t>(pData->param.count),);
  1431. pData->param.data[oldParameterId].mappedControlIndex = CONTROL_INDEX_NONE;
  1432. pData->engine->callback(true, true,
  1433. ENGINE_CALLBACK_PARAMETER_MAPPED_CONTROL_INDEX_CHANGED,
  1434. pData->id,
  1435. oldParameterId,
  1436. CONTROL_INDEX_NONE,
  1437. 0, 0.0f, nullptr);
  1438. }
  1439. // mapping new parameter to CV
  1440. if (index == CONTROL_INDEX_CV)
  1441. {
  1442. CARLA_SAFE_ASSERT_RETURN(pData->event.cvSourcePorts != nullptr,);
  1443. CARLA_SAFE_ASSERT_RETURN(paramData.type == PARAMETER_INPUT,);
  1444. CARLA_SAFE_ASSERT_RETURN(paramData.hints & PARAMETER_CAN_BE_CV_CONTROLLED,);
  1445. CarlaEngineCVPort* const cvPort =
  1446. (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, strBuf, true, parameterId);
  1447. cvPort->setRange(paramData.mappedMinimum, paramData.mappedMaximum);
  1448. pData->event.cvSourcePorts->addCVSource(cvPort, parameterId, reconfigureNow);
  1449. }
  1450. // unmapping from CV
  1451. else if (paramData.mappedControlIndex == CONTROL_INDEX_CV)
  1452. {
  1453. CARLA_SAFE_ASSERT_RETURN(pData->event.cvSourcePorts != nullptr,);
  1454. CARLA_SAFE_ASSERT(pData->client->removePort(kEnginePortTypeCV, strBuf, true));
  1455. CARLA_SAFE_ASSERT(pData->event.cvSourcePorts->removeCVSource(parameterId));
  1456. }
  1457. // mapping to something new
  1458. else if (paramData.mappedControlIndex == CONTROL_INDEX_NONE)
  1459. {
  1460. // when doing MIDI CC mapping, ensure ranges are within bounds
  1461. if (paramData.mappedMinimum < paramRanges.min || paramData.mappedMaximum > paramRanges.max)
  1462. setParameterMappedRange(parameterId,
  1463. std::max(paramData.mappedMinimum, paramRanges.min),
  1464. std::min(paramData.mappedMaximum, paramRanges.max),
  1465. true, true);
  1466. }
  1467. #endif
  1468. paramData.mappedControlIndex = index;
  1469. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1470. if (index == CONTROL_INDEX_MIDI_LEARN)
  1471. pData->midiLearnParameterIndex = static_cast<int32_t>(parameterId);
  1472. else
  1473. pData->midiLearnParameterIndex = -1;
  1474. pData->engine->callback(sendCallback, sendOsc,
  1475. ENGINE_CALLBACK_PARAMETER_MAPPED_CONTROL_INDEX_CHANGED,
  1476. pData->id,
  1477. static_cast<int>(parameterId),
  1478. index,
  1479. 0, 0.0f, nullptr);
  1480. #else
  1481. return;
  1482. // unused
  1483. (void)reconfigureNow;
  1484. #endif
  1485. }
  1486. void CarlaPlugin::setParameterMappedRange(const uint32_t parameterId, const float minimum, const float maximum,
  1487. const bool sendOsc, const bool sendCallback) noexcept
  1488. {
  1489. if (pData->engineBridged) {
  1490. CARLA_SAFE_ASSERT_RETURN(!sendOsc && !sendCallback,);
  1491. } else {
  1492. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1493. }
  1494. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1495. ParameterData& paramData(pData->param.data[parameterId]);
  1496. if (carla_isEqual(paramData.mappedMinimum, minimum) &&
  1497. carla_isEqual(paramData.mappedMaximum, maximum) &&
  1498. (paramData.hints & PARAMETER_MAPPED_RANGES_SET) != 0x0)
  1499. return;
  1500. if (paramData.mappedControlIndex != CONTROL_INDEX_NONE && paramData.mappedControlIndex != CONTROL_INDEX_CV)
  1501. {
  1502. const ParameterRanges& paramRanges(pData->param.ranges[parameterId]);
  1503. CARLA_SAFE_ASSERT_RETURN(minimum >= paramRanges.min,);
  1504. CARLA_SAFE_ASSERT_RETURN(maximum <= paramRanges.max,);
  1505. }
  1506. paramData.hints |= PARAMETER_MAPPED_RANGES_SET;
  1507. paramData.mappedMinimum = minimum;
  1508. paramData.mappedMaximum = maximum;
  1509. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1510. if (pData->event.cvSourcePorts != nullptr && paramData.mappedControlIndex == CONTROL_INDEX_CV)
  1511. pData->event.cvSourcePorts->setCVSourceRange(parameterId, minimum, maximum);
  1512. char strBuf[STR_MAX+1];
  1513. carla_zeroChars(strBuf, STR_MAX+1);
  1514. std::snprintf(strBuf, STR_MAX, "%.12g:%.12g", static_cast<double>(minimum), static_cast<double>(maximum));
  1515. pData->engine->callback(sendCallback, sendOsc,
  1516. ENGINE_CALLBACK_PARAMETER_MAPPED_RANGE_CHANGED,
  1517. pData->id,
  1518. static_cast<int>(parameterId),
  1519. 0, 0, 0.0f,
  1520. strBuf);
  1521. #endif
  1522. }
  1523. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  1524. {
  1525. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1526. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1527. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1528. // Ignore some keys
  1529. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  1530. {
  1531. const PluginType ptype = getType();
  1532. if ((ptype == PLUGIN_INTERNAL && std::strncmp(key, "CarlaAlternateFile", 18) == 0) ||
  1533. (ptype == PLUGIN_DSSI && std::strcmp (key, "guiVisible") == 0) ||
  1534. (ptype == PLUGIN_LV2 && std::strncmp(key, "OSC:", 4) == 0))
  1535. return;
  1536. }
  1537. // Check if we already have this key
  1538. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  1539. {
  1540. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  1541. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1542. if (std::strcmp(customData.key, key) == 0)
  1543. {
  1544. if (customData.value != nullptr)
  1545. delete[] customData.value;
  1546. customData.value = carla_strdup(value);
  1547. return;
  1548. }
  1549. }
  1550. // Otherwise store it
  1551. CustomData customData;
  1552. customData.type = carla_strdup(type);
  1553. customData.key = carla_strdup(key);
  1554. customData.value = carla_strdup(value);
  1555. pData->custom.append(customData);
  1556. }
  1557. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  1558. {
  1559. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1560. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  1561. CARLA_SAFE_ASSERT(false); // this should never happen
  1562. }
  1563. void CarlaPlugin::setProgram(const int32_t index,
  1564. const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1565. {
  1566. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1567. pData->prog.current = index;
  1568. pData->engine->callback(sendCallback, sendOsc,
  1569. ENGINE_CALLBACK_PROGRAM_CHANGED,
  1570. pData->id,
  1571. index,
  1572. 0, 0, 0.0f, nullptr);
  1573. // Change default parameter values
  1574. if (index >= 0)
  1575. {
  1576. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1577. uiProgramChange(static_cast<uint32_t>(index));
  1578. switch (getType())
  1579. {
  1580. case PLUGIN_SF2:
  1581. case PLUGIN_SFZ:
  1582. break;
  1583. default:
  1584. pData->updateParameterValues(this, sendCallback, sendOsc, true);
  1585. break;
  1586. }
  1587. }
  1588. }
  1589. void CarlaPlugin::setMidiProgram(const int32_t index,
  1590. const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1591. {
  1592. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1593. pData->midiprog.current = index;
  1594. pData->engine->callback(sendCallback, sendOsc,
  1595. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  1596. pData->id,
  1597. index,
  1598. 0, 0, 0.0f, nullptr);
  1599. // Change default parameter values
  1600. if (index >= 0)
  1601. {
  1602. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1603. uiMidiProgramChange(static_cast<uint32_t>(index));
  1604. switch (getType())
  1605. {
  1606. case PLUGIN_SF2:
  1607. case PLUGIN_SFZ:
  1608. break;
  1609. default:
  1610. pData->updateParameterValues(this, sendCallback, sendOsc, true);
  1611. break;
  1612. }
  1613. }
  1614. }
  1615. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1616. {
  1617. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1618. {
  1619. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1620. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1621. }
  1622. }
  1623. void CarlaPlugin::setProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept
  1624. {
  1625. CARLA_SAFE_ASSERT_RETURN(uindex < pData->prog.count,);
  1626. const int32_t index = static_cast<int32_t>(uindex);
  1627. pData->prog.current = index;
  1628. // Change default parameter values
  1629. switch (getType())
  1630. {
  1631. case PLUGIN_SF2:
  1632. case PLUGIN_SFZ:
  1633. break;
  1634. default:
  1635. pData->updateDefaultParameterValues(this);
  1636. break;
  1637. }
  1638. pData->postponeProgramChangeRtEvent(sendCallbackLater, uindex);
  1639. }
  1640. void CarlaPlugin::setMidiProgramRT(const uint32_t uindex, const bool sendCallbackLater) noexcept
  1641. {
  1642. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  1643. const int32_t index = static_cast<int32_t>(uindex);
  1644. pData->midiprog.current = index;
  1645. // Change default parameter values
  1646. switch (getType())
  1647. {
  1648. case PLUGIN_SF2:
  1649. case PLUGIN_SFZ:
  1650. break;
  1651. default:
  1652. pData->updateDefaultParameterValues(this);
  1653. break;
  1654. }
  1655. pData->postponeMidiProgramChangeRtEvent(sendCallbackLater, uindex);
  1656. }
  1657. // -------------------------------------------------------------------
  1658. // Plugin state
  1659. void CarlaPlugin::reloadPrograms(const bool)
  1660. {
  1661. }
  1662. // -------------------------------------------------------------------
  1663. // Plugin processing
  1664. void CarlaPlugin::activate() noexcept
  1665. {
  1666. CARLA_SAFE_ASSERT(! pData->active);
  1667. }
  1668. void CarlaPlugin::deactivate() noexcept
  1669. {
  1670. CARLA_SAFE_ASSERT(pData->active);
  1671. }
  1672. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1673. {
  1674. }
  1675. void CarlaPlugin::sampleRateChanged(const double)
  1676. {
  1677. }
  1678. void CarlaPlugin::offlineModeChanged(const bool)
  1679. {
  1680. }
  1681. // -------------------------------------------------------------------
  1682. // Misc
  1683. void CarlaPlugin::idle()
  1684. {
  1685. if (! pData->enabled)
  1686. return;
  1687. const bool hasUI(pData->hints & PLUGIN_HAS_CUSTOM_UI);
  1688. const bool needsUiMainThread(pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD);
  1689. const uint32_t latency(getLatencyInFrames());
  1690. if (pData->latency.frames != latency)
  1691. {
  1692. carla_stdout("latency changed to %i samples", latency);
  1693. const ScopedSingleProcessLocker sspl(this, true);
  1694. pData->client->setLatency(latency);
  1695. #ifndef BUILD_BRIDGE
  1696. pData->latency.recreateBuffers(pData->latency.channels, latency);
  1697. #else
  1698. pData->latency.frames = latency;
  1699. #endif
  1700. }
  1701. ProtectedData::PostRtEvents::Access rtEvents(pData->postRtEvents);
  1702. if (rtEvents.isEmpty())
  1703. return;
  1704. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = rtEvents.getDataIterator(); it.valid(); it.next())
  1705. {
  1706. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1707. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1708. switch (event.type)
  1709. {
  1710. case kPluginPostRtEventNull: {
  1711. } break;
  1712. case kPluginPostRtEventParameterChange: {
  1713. // Update UI
  1714. if (event.parameter.index >= 0 && hasUI)
  1715. {
  1716. if (needsUiMainThread)
  1717. pData->postUiEvents.append(event);
  1718. else
  1719. uiParameterChange(static_cast<uint32_t>(event.parameter.index), event.parameter.value);
  1720. }
  1721. if (event.sendCallback)
  1722. {
  1723. // Update Host
  1724. pData->engine->callback(true, true,
  1725. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1726. pData->id,
  1727. event.parameter.index,
  1728. 0, 0,
  1729. event.parameter.value,
  1730. nullptr);
  1731. }
  1732. } break;
  1733. case kPluginPostRtEventProgramChange: {
  1734. // Update UI
  1735. if (hasUI)
  1736. {
  1737. if (needsUiMainThread)
  1738. pData->postUiEvents.append(event);
  1739. else
  1740. uiProgramChange(event.program.index);
  1741. }
  1742. // Update param values
  1743. for (uint32_t j=0; j < pData->param.count; ++j)
  1744. {
  1745. const float paramDefault(pData->param.ranges[j].def);
  1746. const float paramValue(getParameterValue(j));
  1747. pData->engine->callback(true, true,
  1748. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1749. pData->id,
  1750. static_cast<int>(j),
  1751. 0, 0,
  1752. paramValue,
  1753. nullptr);
  1754. pData->engine->callback(true, true,
  1755. ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED,
  1756. pData->id,
  1757. static_cast<int>(j),
  1758. 0, 0,
  1759. paramDefault,
  1760. nullptr);
  1761. }
  1762. if (event.sendCallback)
  1763. {
  1764. // Update Host
  1765. pData->engine->callback(true, true,
  1766. ENGINE_CALLBACK_PROGRAM_CHANGED,
  1767. pData->id,
  1768. static_cast<int>(event.program.index),
  1769. 0, 0, 0.0f, nullptr);
  1770. }
  1771. } break;
  1772. case kPluginPostRtEventMidiProgramChange: {
  1773. // Update UI
  1774. if (hasUI)
  1775. {
  1776. if (needsUiMainThread)
  1777. pData->postUiEvents.append(event);
  1778. else
  1779. uiMidiProgramChange(event.program.index);
  1780. }
  1781. // Update param values
  1782. for (uint32_t j=0; j < pData->param.count; ++j)
  1783. {
  1784. const float paramDefault(pData->param.ranges[j].def);
  1785. const float paramValue(getParameterValue(j));
  1786. pData->engine->callback(true, true,
  1787. ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  1788. pData->id,
  1789. static_cast<int>(j),
  1790. 0, 0,
  1791. paramValue,
  1792. nullptr);
  1793. pData->engine->callback(true, true,
  1794. ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED,
  1795. pData->id,
  1796. static_cast<int>(j),
  1797. 0, 0,
  1798. paramDefault,
  1799. nullptr);
  1800. }
  1801. if (event.sendCallback)
  1802. {
  1803. // Update Host
  1804. pData->engine->callback(true, true,
  1805. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  1806. pData->id,
  1807. static_cast<int>(event.program.index),
  1808. 0, 0, 0.0f, nullptr);
  1809. }
  1810. } break;
  1811. case kPluginPostRtEventNoteOn: {
  1812. CARLA_SAFE_ASSERT_BREAK(event.note.channel < MAX_MIDI_CHANNELS);
  1813. CARLA_SAFE_ASSERT_BREAK(event.note.note < MAX_MIDI_NOTE);
  1814. CARLA_SAFE_ASSERT_BREAK(event.note.velocity < MAX_MIDI_VALUE);
  1815. // Update UI
  1816. if (hasUI)
  1817. {
  1818. if (needsUiMainThread)
  1819. pData->postUiEvents.append(event);
  1820. else
  1821. uiNoteOn(event.note.channel, event.note.note, event.note.velocity);
  1822. }
  1823. if (event.sendCallback)
  1824. {
  1825. // Update Host
  1826. pData->engine->callback(true, true,
  1827. ENGINE_CALLBACK_NOTE_ON,
  1828. pData->id,
  1829. static_cast<int>(event.note.channel),
  1830. static_cast<int>(event.note.note),
  1831. static_cast<int>(event.note.velocity),
  1832. 0.0f, nullptr);
  1833. }
  1834. } break;
  1835. case kPluginPostRtEventNoteOff: {
  1836. CARLA_SAFE_ASSERT_BREAK(event.note.channel < MAX_MIDI_CHANNELS);
  1837. CARLA_SAFE_ASSERT_BREAK(event.note.note < MAX_MIDI_NOTE);
  1838. // Update UI
  1839. if (hasUI)
  1840. {
  1841. if (needsUiMainThread)
  1842. pData->postUiEvents.append(event);
  1843. else
  1844. uiNoteOff(event.note.channel, event.note.note);
  1845. }
  1846. if (event.sendCallback)
  1847. {
  1848. // Update Host
  1849. pData->engine->callback(true, true,
  1850. ENGINE_CALLBACK_NOTE_OFF,
  1851. pData->id,
  1852. static_cast<int>(event.note.channel),
  1853. static_cast<int>(event.note.note),
  1854. 0, 0.0f, nullptr);
  1855. }
  1856. } break;
  1857. case kPluginPostRtEventMidiLearn: {
  1858. CARLA_SAFE_ASSERT_BREAK(event.midiLearn.cc < MAX_MIDI_VALUE);
  1859. CARLA_SAFE_ASSERT_BREAK(event.midiLearn.channel < MAX_MIDI_CHANNELS);
  1860. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1861. if (event.sendCallback)
  1862. {
  1863. pData->engine->callback(true, true,
  1864. ENGINE_CALLBACK_PARAMETER_MAPPED_CONTROL_INDEX_CHANGED,
  1865. pData->id,
  1866. static_cast<int>(event.midiLearn.parameter),
  1867. static_cast<int>(event.midiLearn.cc),
  1868. 0, 0.0f, nullptr);
  1869. pData->engine->callback(true, true,
  1870. ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED,
  1871. pData->id,
  1872. static_cast<int>(event.midiLearn.parameter),
  1873. static_cast<int>(event.midiLearn.channel),
  1874. 0, 0.0f, nullptr);
  1875. }
  1876. #endif
  1877. } break;
  1878. }
  1879. }
  1880. }
  1881. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1882. {
  1883. if (forcedOffline)
  1884. {
  1885. #ifndef STOAT_TEST_BUILD
  1886. pData->masterMutex.lock();
  1887. return true;
  1888. #endif
  1889. }
  1890. return pData->masterMutex.tryLock();
  1891. }
  1892. void CarlaPlugin::unlock() noexcept
  1893. {
  1894. pData->masterMutex.unlock();
  1895. }
  1896. // -------------------------------------------------------------------
  1897. // Plugin buffers
  1898. void CarlaPlugin::initBuffers() const noexcept
  1899. {
  1900. pData->audioIn.initBuffers();
  1901. pData->audioOut.initBuffers();
  1902. pData->cvIn.initBuffers();
  1903. pData->cvOut.initBuffers();
  1904. pData->event.initBuffers();
  1905. }
  1906. void CarlaPlugin::clearBuffers() noexcept
  1907. {
  1908. pData->clearBuffers();
  1909. }
  1910. // -------------------------------------------------------------------
  1911. // OSC stuff
  1912. // FIXME
  1913. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1914. {
  1915. // do nothing
  1916. }
  1917. // -------------------------------------------------------------------
  1918. // MIDI events
  1919. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1920. {
  1921. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1922. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1923. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1924. if (! pData->active)
  1925. return;
  1926. ExternalMidiNote extNote;
  1927. extNote.channel = static_cast<int8_t>(channel);
  1928. extNote.note = note;
  1929. extNote.velo = velo;
  1930. pData->extNotes.appendNonRT(extNote);
  1931. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1932. {
  1933. if (velo > 0)
  1934. uiNoteOn(channel, note, velo);
  1935. else
  1936. uiNoteOff(channel, note);
  1937. }
  1938. pData->engine->callback(sendCallback, sendOsc,
  1939. (velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF,
  1940. pData->id,
  1941. channel,
  1942. note,
  1943. velo,
  1944. 0.0f, nullptr);
  1945. }
  1946. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1947. void CarlaPlugin::postponeRtAllNotesOff()
  1948. {
  1949. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1950. return;
  1951. PluginPostRtEvent postEvent = { kPluginPostRtEventNoteOff, true, {} };
  1952. postEvent.note.channel = static_cast<uint8_t>(pData->ctrlChannel);
  1953. for (uint8_t i=0; i < MAX_MIDI_NOTE; ++i)
  1954. {
  1955. postEvent.note.note = i;
  1956. pData->postRtEvents.appendRT(postEvent);
  1957. }
  1958. }
  1959. #endif
  1960. // -------------------------------------------------------------------
  1961. // UI Stuff
  1962. void CarlaPlugin::setCustomUITitle(const char* const title) noexcept
  1963. {
  1964. pData->uiTitle = title;
  1965. }
  1966. void CarlaPlugin::showCustomUI(const bool yesNo)
  1967. {
  1968. if (yesNo) {
  1969. CARLA_SAFE_ASSERT(false);
  1970. }
  1971. }
  1972. void* CarlaPlugin::embedCustomUI(void*)
  1973. {
  1974. return nullptr;
  1975. }
  1976. void CarlaPlugin::uiIdle()
  1977. {
  1978. if (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD)
  1979. {
  1980. // Update parameter outputs
  1981. for (uint32_t i=0; i < pData->param.count; ++i)
  1982. {
  1983. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1984. uiParameterChange(i, getParameterValue(i));
  1985. }
  1986. const CarlaMutexLocker sl(pData->postUiEvents.mutex);
  1987. for (LinkedList<PluginPostRtEvent>::Itenerator it = pData->postUiEvents.data.begin2(); it.valid(); it.next())
  1988. {
  1989. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1990. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1991. switch (event.type)
  1992. {
  1993. case kPluginPostRtEventNull:
  1994. case kPluginPostRtEventMidiLearn:
  1995. break;
  1996. case kPluginPostRtEventParameterChange:
  1997. uiParameterChange(static_cast<uint32_t>(event.parameter.index), event.parameter.value);
  1998. break;
  1999. case kPluginPostRtEventProgramChange:
  2000. uiProgramChange(event.program.index);
  2001. break;
  2002. case kPluginPostRtEventMidiProgramChange:
  2003. uiMidiProgramChange(event.program.index);
  2004. break;
  2005. case kPluginPostRtEventNoteOn:
  2006. uiNoteOn(event.note.channel, event.note.note, event.note.velocity);
  2007. break;
  2008. case kPluginPostRtEventNoteOff:
  2009. uiNoteOff(event.note.channel, event.note.note);
  2010. break;
  2011. }
  2012. }
  2013. pData->postUiEvents.data.clear();
  2014. }
  2015. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2016. if (pData->transientTryCounter == 0)
  2017. return;
  2018. if (++pData->transientTryCounter % 10 != 0)
  2019. return;
  2020. if (pData->transientTryCounter >= 200)
  2021. return;
  2022. carla_stdout("Trying to get window...");
  2023. CarlaString uiTitle;
  2024. if (pData->uiTitle.isNotEmpty())
  2025. {
  2026. uiTitle = pData->uiTitle;
  2027. }
  2028. else
  2029. {
  2030. uiTitle = pData->name;
  2031. uiTitle += " (GUI)";
  2032. }
  2033. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle,
  2034. pData->engine->getOptions().frontendWinId, pData->transientFirstTry))
  2035. {
  2036. pData->transientTryCounter = 0;
  2037. pData->transientFirstTry = false;
  2038. }
  2039. #endif
  2040. }
  2041. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  2042. {
  2043. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  2044. return;
  2045. // unused
  2046. (void)value;
  2047. }
  2048. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  2049. {
  2050. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  2051. }
  2052. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  2053. {
  2054. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  2055. }
  2056. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  2057. {
  2058. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2059. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2060. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2061. }
  2062. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  2063. {
  2064. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2065. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2066. }
  2067. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  2068. {
  2069. return pData->engine;
  2070. }
  2071. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  2072. {
  2073. return pData->client;
  2074. }
  2075. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  2076. {
  2077. return pData->audioIn.ports[index].port;
  2078. }
  2079. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  2080. {
  2081. return pData->audioOut.ports[index].port;
  2082. }
  2083. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  2084. {
  2085. return pData->cvIn.ports[index].port;
  2086. }
  2087. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  2088. {
  2089. return pData->cvOut.ports[index].port;
  2090. }
  2091. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  2092. {
  2093. return pData->event.portIn;
  2094. }
  2095. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  2096. {
  2097. return pData->event.portOut;
  2098. }
  2099. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2100. void CarlaPlugin::checkForMidiLearn(EngineEvent& event) noexcept
  2101. {
  2102. if (pData->midiLearnParameterIndex < 0)
  2103. return;
  2104. if (event.ctrl.param == MIDI_CONTROL_BANK_SELECT || event.ctrl.param == MIDI_CONTROL_BANK_SELECT__LSB)
  2105. return;
  2106. if (event.ctrl.param >= MAX_MIDI_CONTROL)
  2107. return;
  2108. const uint32_t parameterId = static_cast<uint32_t>(pData->midiLearnParameterIndex);
  2109. CARLA_SAFE_ASSERT_UINT2_RETURN(parameterId < pData->param.count, parameterId, pData->param.count,);
  2110. ParameterData& paramData(pData->param.data[parameterId]);
  2111. CARLA_SAFE_ASSERT_INT_RETURN(paramData.mappedControlIndex == CONTROL_INDEX_MIDI_LEARN,
  2112. paramData.mappedControlIndex,);
  2113. event.ctrl.handled = true;
  2114. paramData.mappedControlIndex = static_cast<int16_t>(event.ctrl.param);
  2115. paramData.midiChannel = event.channel;
  2116. pData->postponeMidiLearnRtEvent(true, parameterId, static_cast<uint8_t>(event.ctrl.param), event.channel);
  2117. pData->midiLearnParameterIndex = -1;
  2118. }
  2119. #endif
  2120. void* CarlaPlugin::getNativeHandle() const noexcept
  2121. {
  2122. return nullptr;
  2123. }
  2124. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  2125. {
  2126. return nullptr;
  2127. }
  2128. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  2129. {
  2130. return 0;
  2131. }
  2132. // -------------------------------------------------------------------
  2133. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  2134. {
  2135. return pData->nodeId;
  2136. }
  2137. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  2138. {
  2139. pData->nodeId = nodeId;
  2140. }
  2141. // -------------------------------------------------------------------
  2142. void CarlaPlugin::cloneLV2Files(const CarlaPlugin&)
  2143. {
  2144. carla_stderr2("Warning: cloneLV2Files() called for non-implemented type");
  2145. }
  2146. void CarlaPlugin::restoreLV2State(const bool temporary) noexcept
  2147. {
  2148. carla_stderr2("Warning: restoreLV2State(%s) called for non-implemented type", bool2str(temporary));
  2149. }
  2150. void CarlaPlugin::prepareForDeletion() noexcept
  2151. {
  2152. carla_debug("CarlaPlugin::prepareForDeletion");
  2153. const CarlaMutexLocker cml(pData->masterMutex);
  2154. pData->client->deactivate(true);
  2155. }
  2156. void CarlaPlugin::waitForBridgeSaveSignal() noexcept
  2157. {
  2158. }
  2159. // -------------------------------------------------------------------
  2160. // Scoped Disabler
  2161. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  2162. : fPlugin(plugin),
  2163. fWasEnabled(false)
  2164. {
  2165. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  2166. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  2167. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  2168. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  2169. plugin->pData->masterMutex.lock();
  2170. if (plugin->pData->enabled)
  2171. {
  2172. fWasEnabled = true;
  2173. plugin->pData->enabled = false;
  2174. if (plugin->pData->client->isActive())
  2175. plugin->pData->client->deactivate(false);
  2176. }
  2177. }
  2178. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  2179. {
  2180. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2181. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2182. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  2183. carla_debug("CarlaPlugin::~ScopedDisabler()");
  2184. if (fWasEnabled)
  2185. {
  2186. fPlugin->pData->enabled = true;
  2187. fPlugin->pData->client->activate();
  2188. }
  2189. fPlugin->pData->masterMutex.unlock();
  2190. }
  2191. // -------------------------------------------------------------------
  2192. // Scoped Process Locker
  2193. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  2194. : fPlugin(plugin),
  2195. fBlock(block)
  2196. {
  2197. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2198. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2199. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  2200. if (! fBlock)
  2201. return;
  2202. plugin->pData->singleMutex.lock();
  2203. }
  2204. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  2205. {
  2206. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2207. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2208. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  2209. if (! fBlock)
  2210. return;
  2211. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  2212. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  2213. fPlugin->pData->needsReset = true;
  2214. #endif
  2215. fPlugin->pData->singleMutex.unlock();
  2216. }
  2217. // -------------------------------------------------------------------
  2218. CARLA_BACKEND_END_NAMESPACE