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.

2733 lines
90KB

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