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.

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