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.

2569 lines
82KB

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