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.

2662 lines
87KB

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