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.

2551 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 && pluginType != PLUGIN_GIG)
  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. index = stateParameter->index;
  594. }
  595. else if (pluginType == PLUGIN_LV2)
  596. {
  597. // Symbol only
  598. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  599. {
  600. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin2(); it2.valid(); it2.next())
  601. {
  602. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  603. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  604. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  605. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  606. {
  607. index = paramSymbol->index;
  608. break;
  609. }
  610. }
  611. if (index == -1)
  612. carla_stderr("Failed to find LV2 parameter symbol '%s')", stateParameter->symbol);
  613. }
  614. else
  615. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  616. }
  617. else
  618. {
  619. // Index only
  620. index = stateParameter->index;
  621. }
  622. // Now set parameter
  623. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  624. {
  625. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  626. if (! stateParameter->dummy)
  627. {
  628. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  629. stateParameter->value *= sampleRate;
  630. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  631. }
  632. #ifndef BUILD_BRIDGE
  633. setParameterMidiCC(static_cast<uint32_t>(index), stateParameter->midiCC, true, true);
  634. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  635. #endif
  636. }
  637. else
  638. carla_stderr("Could not set parameter data for '%s'", stateParameter->name);
  639. }
  640. // ---------------------------------------------------------------
  641. // Part 4c - clear
  642. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin2(); it.valid(); it.next())
  643. {
  644. ParamSymbol* const paramSymbol(it.getValue(nullptr));
  645. delete paramSymbol;
  646. }
  647. paramSymbols.clear();
  648. // ---------------------------------------------------------------
  649. // Part 5 - set custom data
  650. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin2(); it.valid(); it.next())
  651. {
  652. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  653. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  654. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  655. const char* const key(stateCustomData->key);
  656. if (pluginType == PLUGIN_DSSI && (std::strcmp (key, "reloadprograms") == 0 ||
  657. std::strcmp (key, "load" ) == 0 ||
  658. std::strncmp(key, "patches", 7) == 0 ))
  659. continue;
  660. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  661. continue;
  662. setCustomData(stateCustomData->type, key, stateCustomData->value, true);
  663. }
  664. // ---------------------------------------------------------------
  665. // Part 5x - set lv2 state
  666. if (pluginType == PLUGIN_LV2 && pData->custom.count() > 0)
  667. restoreLV2State();
  668. // ---------------------------------------------------------------
  669. // Part 6 - set chunk
  670. if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  671. {
  672. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stateSave.chunk));
  673. #ifdef CARLA_PROPER_CPP11_SUPPORT
  674. setChunkData(chunk.data(), chunk.size());
  675. #else
  676. setChunkData(&chunk.front(), chunk.size());
  677. #endif
  678. }
  679. #ifndef BUILD_BRIDGE
  680. // ---------------------------------------------------------------
  681. // Part 6 - set internal stuff
  682. const uint availOptions(getOptionsAvailable());
  683. for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
  684. {
  685. const uint option(1u << i);
  686. if (availOptions & option)
  687. setOption(option, (stateSave.options & option) != 0, true);
  688. }
  689. setDryWet(stateSave.dryWet, true, true);
  690. setVolume(stateSave.volume, true, true);
  691. setBalanceLeft(stateSave.balanceLeft, true, true);
  692. setBalanceRight(stateSave.balanceRight, true, true);
  693. setPanning(stateSave.panning, true, true);
  694. setCtrlChannel(stateSave.ctrlChannel, true, true);
  695. setActive(stateSave.active, true, true);
  696. #endif
  697. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0f, nullptr);
  698. }
  699. bool CarlaPlugin::saveStateToFile(const char* const filename)
  700. {
  701. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  702. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  703. MemoryOutputStream out, streamState;
  704. getStateSave().dumpToMemoryStream(streamState);
  705. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  706. out << "<!DOCTYPE CARLA-PRESET>\n";
  707. out << "<CARLA-PRESET VERSION='2.0'>\n";
  708. out << streamState;
  709. out << "</CARLA-PRESET>\n";
  710. const String jfilename = String(CharPointer_UTF8(filename));
  711. File file(jfilename);
  712. if (file.replaceWithData(out.getData(), out.getDataSize()))
  713. return true;
  714. pData->engine->setLastError("Failed to write file");
  715. return false;
  716. }
  717. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  718. {
  719. // TODO set errors
  720. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  721. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  722. const String jfilename = String(CharPointer_UTF8(filename));
  723. File file(jfilename);
  724. CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
  725. XmlDocument xml(file);
  726. ScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
  727. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  728. CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
  729. // completely load file
  730. xmlElement = xml.getDocumentElement(false);
  731. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  732. if (pData->stateSave.fillFromXmlElement(xmlElement))
  733. {
  734. loadStateSave(pData->stateSave);
  735. return true;
  736. }
  737. return false;
  738. }
  739. bool CarlaPlugin::exportAsLV2(const char* const lv2path)
  740. {
  741. CARLA_SAFE_ASSERT_RETURN(lv2path != nullptr && lv2path[0] != '\0', false);
  742. carla_debug("CarlaPlugin::exportAsLV2(\"%s\")", lv2path);
  743. CarlaString bundlepath(lv2path);
  744. if (! bundlepath.endsWith(".lv2"))
  745. bundlepath += ".lv2";
  746. const File bundlefolder(bundlepath.buffer());
  747. if (bundlefolder.existsAsFile())
  748. {
  749. pData->engine->setLastError("Requested filename already exists as file, use a folder instead");
  750. return false;
  751. }
  752. if (! bundlefolder.exists())
  753. {
  754. const Result res(bundlefolder.createDirectory());
  755. if (res.failed())
  756. {
  757. pData->engine->setLastError(res.getErrorMessage().toRawUTF8());
  758. return false;
  759. }
  760. }
  761. CarlaString symbol(pData->name);
  762. symbol.toBasic();
  763. {
  764. const CarlaString pluginFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".xml");
  765. if (! saveStateToFile(pluginFilename))
  766. return false;
  767. }
  768. {
  769. MemoryOutputStream manifestStream;
  770. manifestStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  771. manifestStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  772. manifestStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  773. manifestStream << "\n";
  774. manifestStream << "<" << symbol.buffer() << ".ttl>\n";
  775. manifestStream << " a lv2:Plugin ;\n";
  776. manifestStream << " lv2:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  777. manifestStream << " rdfs:seeAlso <" << symbol.buffer() << ".ttl> .\n";
  778. manifestStream << "\n";
  779. manifestStream << "<ext-ui>\n";
  780. manifestStream << " a <http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget> ;\n";
  781. manifestStream << " ui:binary <" << symbol.buffer() << CARLA_LIB_EXT "> ;\n";
  782. manifestStream << " lv2:extensionData <http://lv2plug.in/ns/extensions/ui#idleInterface> ,\n";
  783. manifestStream << " <http://lv2plug.in/ns/extensions/ui#showInterface> ;\n";
  784. manifestStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/instance-access> .\n";
  785. manifestStream << "\n";
  786. const CarlaString manifestFilename(bundlepath + CARLA_OS_SEP_STR "manifest.ttl");
  787. const File manifestFile(manifestFilename.buffer());
  788. if (! manifestFile.replaceWithData(manifestStream.getData(), manifestStream.getDataSize()))
  789. {
  790. pData->engine->setLastError("Failed to write manifest.ttl file");
  791. return false;
  792. }
  793. }
  794. {
  795. MemoryOutputStream mainStream;
  796. mainStream << "@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n";
  797. mainStream << "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  798. mainStream << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n";
  799. mainStream << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  800. mainStream << "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n";
  801. mainStream << "\n";
  802. mainStream << "<>\n";
  803. mainStream << " a lv2:Plugin ;\n";
  804. mainStream << "\n";
  805. mainStream << " lv2:requiredFeature <http://lv2plug.in/ns/ext/buf-size#boundedBlockLength> ,\n";
  806. mainStream << " <http://lv2plug.in/ns/ext/options#options> ,\n";
  807. mainStream << " <http://lv2plug.in/ns/ext/urid#map> ;\n";
  808. mainStream << "\n";
  809. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  810. {
  811. mainStream << " ui:ui <ext-ui> ;\n";
  812. mainStream << "\n";
  813. }
  814. const uint32_t midiIns = getMidiInCount();
  815. const uint32_t midiOuts = getMidiOutCount();
  816. int portIndex = 0;
  817. if (midiIns > 0)
  818. {
  819. mainStream << " lv2:port [\n";
  820. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  821. mainStream << " lv2:index 0 ;\n";
  822. mainStream << " lv2:symbol \"clv2_events_in\" ;\n";
  823. mainStream << " lv2:name \"Events Input\" ;\n";
  824. mainStream << " atom:bufferType atom:Sequence ;\n";
  825. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ,\n";
  826. mainStream << " <http://lv2plug.in/ns/ext/time#Position> ;\n";
  827. mainStream << " ] ;\n";
  828. ++portIndex;
  829. for (uint32_t i=1; i<midiIns; ++i)
  830. {
  831. const String portIndexNum(portIndex++);
  832. const String portIndexLabel(portIndex);
  833. mainStream << " lv2:port [\n";
  834. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  835. mainStream << " lv2:index " << portIndexNum << " ;\n";
  836. mainStream << " lv2:symbol \"clv2_midi_in_" << portIndexLabel << "\" ;\n";
  837. mainStream << " lv2:name \"MIDI Input " << portIndexLabel << "\" ;\n";
  838. mainStream << " ] ;\n";
  839. }
  840. }
  841. else
  842. {
  843. mainStream << " lv2:port [\n";
  844. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  845. mainStream << " lv2:index 0 ;\n";
  846. mainStream << " lv2:symbol \"clv2_time_info\" ;\n";
  847. mainStream << " lv2:name \"Time Info\" ;\n";
  848. mainStream << " atom:bufferType atom:Sequence ;\n";
  849. mainStream << " atom:supports <http://lv2plug.in/ns/ext/time#Position> ;\n";
  850. mainStream << " ] ;\n";
  851. ++portIndex;
  852. }
  853. for (uint32_t i=0; i<midiOuts; ++i)
  854. {
  855. const String portIndexNum(portIndex++);
  856. const String portIndexLabel(portIndex);
  857. mainStream << " lv2:port [\n";
  858. mainStream << " a lv2:InputPort, atom:AtomPort ;\n";
  859. mainStream << " lv2:index " << portIndexNum << " ;\n";
  860. mainStream << " lv2:symbol \"clv2_midi_out_" << portIndexLabel << "\" ;\n";
  861. mainStream << " lv2:name \"MIDI Output " << portIndexLabel << "\" ;\n";
  862. mainStream << " atom:bufferType atom:Sequence ;\n";
  863. mainStream << " atom:supports <http://lv2plug.in/ns/ext/midi#MidiEvent> ;\n";
  864. mainStream << " ] ;\n";
  865. }
  866. mainStream << " lv2:port [\n";
  867. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  868. mainStream << " lv2:index " << String(portIndex++) << " ;\n";
  869. mainStream << " lv2:name \"freewheel\" ;\n";
  870. mainStream << " lv2:symbol \"clv2_freewheel\" ;\n";
  871. mainStream << " lv2:default 0 ;\n";
  872. mainStream << " lv2:minimum 0 ;\n";
  873. mainStream << " lv2:maximum 1 ;\n";
  874. mainStream << " lv2:designation lv2:freeWheeling ;\n";
  875. mainStream << " lv2:portProperty lv2:toggled , lv2:integer ;\n";
  876. mainStream << " lv2:portProperty <http://lv2plug.in/ns/ext/port-props#notOnGUI> ;\n";
  877. mainStream << " ] ;\n";
  878. for (uint32_t i=0; i<pData->audioIn.count; ++i)
  879. {
  880. const String portIndexNum(portIndex++);
  881. const String portIndexLabel(i+1);
  882. mainStream << " lv2:port [\n";
  883. mainStream << " a lv2:InputPort, lv2:AudioPort ;\n";
  884. mainStream << " lv2:index " << portIndexNum << " ;\n";
  885. mainStream << " lv2:symbol \"clv2_audio_in_" << portIndexLabel << "\" ;\n";
  886. mainStream << " lv2:name \"Audio Input " << portIndexLabel << "\" ;\n";
  887. mainStream << " ] ;\n";
  888. }
  889. for (uint32_t i=0; i<pData->audioOut.count; ++i)
  890. {
  891. const String portIndexNum(portIndex++);
  892. const String portIndexLabel(i+1);
  893. mainStream << " lv2:port [\n";
  894. mainStream << " a lv2:OutputPort, lv2:AudioPort ;\n";
  895. mainStream << " lv2:index " << portIndexNum << " ;\n";
  896. mainStream << " lv2:symbol \"clv2_audio_out_" << portIndexLabel << "\" ;\n";
  897. mainStream << " lv2:name \"Audio Output " << portIndexLabel << "\" ;\n";
  898. mainStream << " ] ;\n";
  899. }
  900. CarlaStringList uniqueSymbolNames;
  901. char strBufName[STR_MAX+1];
  902. char strBufSymbol[STR_MAX+1];
  903. strBufName[STR_MAX] = strBufSymbol[STR_MAX] = '\0';
  904. for (uint32_t i=0; i<pData->param.count; ++i)
  905. {
  906. const ParameterData& paramData(pData->param.data[i]);
  907. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  908. const String portIndexNum(portIndex++);
  909. mainStream << " lv2:port [\n";
  910. if (paramData.type == PARAMETER_INPUT)
  911. mainStream << " a lv2:InputPort, lv2:ControlPort ;\n";
  912. else
  913. mainStream << " a lv2:OutputPort, lv2:ControlPort ;\n";
  914. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  915. mainStream << " lv2:portProperty lv2:toggled ;\n";
  916. if (paramData.hints & PARAMETER_IS_INTEGER)
  917. mainStream << " lv2:portProperty lv2:integer ;\n";
  918. // TODO logarithmic, enabled (not on gui), automable, samplerate, scalepoints
  919. strBufName[0] = strBufSymbol[0] = '\0';
  920. getParameterName(i, strBufName);
  921. getParameterSymbol(i, strBufSymbol);
  922. if (strBufSymbol[0] == '\0')
  923. {
  924. CarlaString s(strBufName);
  925. s.toBasic();
  926. std::memcpy(strBufSymbol, s.buffer(), s.length()+1);
  927. if (strBufSymbol[0] >= '0' && strBufSymbol[0] <= '9')
  928. {
  929. const size_t len(std::strlen(strBufSymbol));
  930. std::memmove(strBufSymbol+1, strBufSymbol, len);
  931. strBufSymbol[0] = '_';
  932. strBufSymbol[len+1] = '\0';
  933. }
  934. }
  935. if (uniqueSymbolNames.contains(strBufSymbol))
  936. {
  937. std::snprintf(strBufSymbol, STR_MAX, "clv2_param_%d", i+1);
  938. strBufSymbol[STR_MAX] = '\0';
  939. }
  940. mainStream << " lv2:index " << portIndexNum << " ;\n";
  941. mainStream << " lv2:symbol \"" << strBufSymbol << "\" ;\n";
  942. mainStream << " lv2:name \"\"\"" << strBufName << "\"\"\" ;\n";
  943. mainStream << " lv2:default " << String(paramRanges.def) << " ;\n";
  944. mainStream << " lv2:minimum " << String(paramRanges.min) << " ;\n";
  945. mainStream << " lv2:maximum " << String(paramRanges.max) << " ;\n";
  946. // TODO midiCC, midiChannel
  947. mainStream << " ] ;\n";
  948. }
  949. mainStream << " rdfs:comment \"Plugin generated using Carla LV2 export.\" ;\n";
  950. mainStream << " doap:name \"\"\"" << getName() << "\"\"\" .\n";
  951. mainStream << "\n";
  952. const CarlaString mainFilename(bundlepath + CARLA_OS_SEP_STR + symbol + ".ttl");
  953. const File mainFile(mainFilename.buffer());
  954. if (! mainFile.replaceWithData(mainStream.getData(), mainStream.getDataSize()))
  955. {
  956. pData->engine->setLastError("Failed to write main plugin ttl file");
  957. return false;
  958. }
  959. }
  960. const CarlaString binaryFilename(bundlepath + CARLA_OS_SEP_STR + symbol + CARLA_LIB_EXT);
  961. const File binaryFileSource(File::getSpecialLocation(File::currentExecutableFile).getSiblingFile("carla-bridge-lv2" CARLA_LIB_EXT));
  962. const File binaryFileTarget(binaryFilename.buffer());
  963. if (! binaryFileSource.createSymbolicLink(binaryFileTarget, true))
  964. {
  965. pData->engine->setLastError("Failed to create symbolik link of plugin binary");
  966. return false;
  967. }
  968. const EngineOptions& opts(pData->engine->getOptions());
  969. const CarlaString binFolderTarget(bundlepath + CARLA_OS_SEP_STR + "bin");
  970. const CarlaString resFolderTarget(bundlepath + CARLA_OS_SEP_STR + "res");
  971. File(opts.binaryDir).createSymbolicLink(File(binFolderTarget.buffer()), true);
  972. File(opts.resourceDir).createSymbolicLink(File(resFolderTarget.buffer()), true);
  973. return true;
  974. }
  975. // -------------------------------------------------------------------
  976. // Set data (internal stuff)
  977. void CarlaPlugin::setId(const uint newId) noexcept
  978. {
  979. pData->id = newId;
  980. }
  981. void CarlaPlugin::setName(const char* const newName)
  982. {
  983. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  984. if (pData->name != nullptr)
  985. delete[] pData->name;
  986. pData->name = carla_strdup(newName);
  987. }
  988. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  989. {
  990. CARLA_SAFE_ASSERT_RETURN(getOptionsAvailable() & option,);
  991. if (yesNo)
  992. pData->options |= option;
  993. else
  994. pData->options &= ~option;
  995. #ifndef BUILD_BRIDGE
  996. if (sendCallback)
  997. pData->engine->callback(ENGINE_CALLBACK_OPTION_CHANGED, pData->id, static_cast<int>(option), yesNo ? 1 : 0, 0.0f, nullptr);
  998. #else
  999. // unused
  1000. return; (void)sendCallback;
  1001. #endif
  1002. }
  1003. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  1004. {
  1005. if (pData->enabled == yesNo)
  1006. return;
  1007. pData->masterMutex.lock();
  1008. pData->enabled = yesNo;
  1009. if (yesNo && ! pData->client->isActive())
  1010. pData->client->activate();
  1011. pData->masterMutex.unlock();
  1012. }
  1013. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  1014. {
  1015. #ifndef BUILD_BRIDGE
  1016. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1017. #endif
  1018. if (pData->active == active)
  1019. return;
  1020. {
  1021. const ScopedSingleProcessLocker spl(this, true);
  1022. if (active)
  1023. activate();
  1024. else
  1025. deactivate();
  1026. }
  1027. pData->active = active;
  1028. #ifndef BUILD_BRIDGE
  1029. const float value(active ? 1.0f : 0.0f);
  1030. # ifdef HAVE_LIBLO
  1031. if (sendOsc && pData->engine->isOscControlRegistered())
  1032. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  1033. # endif
  1034. if (sendCallback)
  1035. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  1036. #endif
  1037. // may be unused
  1038. return; (void)sendOsc; (void)sendCallback;
  1039. }
  1040. #ifndef BUILD_BRIDGE
  1041. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1042. {
  1043. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1044. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1045. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1046. return;
  1047. pData->postProc.dryWet = fixedValue;
  1048. #ifdef HAVE_LIBLO
  1049. if (sendOsc && pData->engine->isOscControlRegistered())
  1050. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  1051. #endif
  1052. if (sendCallback)
  1053. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  1054. // may be unused
  1055. return; (void)sendOsc;
  1056. }
  1057. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1058. {
  1059. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1060. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1061. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1062. return;
  1063. pData->postProc.volume = fixedValue;
  1064. #ifdef HAVE_LIBLO
  1065. if (sendOsc && pData->engine->isOscControlRegistered())
  1066. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  1067. #endif
  1068. if (sendCallback)
  1069. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  1070. // may be unused
  1071. return; (void)sendOsc;
  1072. }
  1073. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1074. {
  1075. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1076. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1077. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1078. return;
  1079. pData->postProc.balanceLeft = fixedValue;
  1080. #ifdef HAVE_LIBLO
  1081. if (sendOsc && pData->engine->isOscControlRegistered())
  1082. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  1083. #endif
  1084. if (sendCallback)
  1085. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  1086. // may be unused
  1087. return; (void)sendOsc;
  1088. }
  1089. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1090. {
  1091. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1092. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1093. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1094. return;
  1095. pData->postProc.balanceRight = fixedValue;
  1096. #ifdef HAVE_LIBLO
  1097. if (sendOsc && pData->engine->isOscControlRegistered())
  1098. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  1099. #endif
  1100. if (sendCallback)
  1101. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  1102. // may be unused
  1103. return; (void)sendOsc;
  1104. }
  1105. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  1106. {
  1107. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1108. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1109. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1110. return;
  1111. pData->postProc.panning = fixedValue;
  1112. #ifdef HAVE_LIBLO
  1113. if (sendOsc && pData->engine->isOscControlRegistered())
  1114. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  1115. #endif
  1116. if (sendCallback)
  1117. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  1118. // may be unused
  1119. return; (void)sendOsc;
  1120. }
  1121. void CarlaPlugin::setDryWetRT(const float value) noexcept
  1122. {
  1123. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  1124. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  1125. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  1126. return;
  1127. pData->postProc.dryWet = fixedValue;
  1128. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, fixedValue);
  1129. }
  1130. void CarlaPlugin::setVolumeRT(const float value) noexcept
  1131. {
  1132. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  1133. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  1134. if (carla_isEqual(pData->postProc.volume, fixedValue))
  1135. return;
  1136. pData->postProc.volume = fixedValue;
  1137. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, fixedValue);
  1138. }
  1139. void CarlaPlugin::setBalanceLeftRT(const float value) noexcept
  1140. {
  1141. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1142. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1143. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  1144. return;
  1145. pData->postProc.balanceLeft = fixedValue;
  1146. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, fixedValue);
  1147. }
  1148. void CarlaPlugin::setBalanceRightRT(const float value) noexcept
  1149. {
  1150. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1151. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1152. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  1153. return;
  1154. pData->postProc.balanceRight = fixedValue;
  1155. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, fixedValue);
  1156. }
  1157. void CarlaPlugin::setPanningRT(const float value) noexcept
  1158. {
  1159. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  1160. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  1161. if (carla_isEqual(pData->postProc.panning, fixedValue))
  1162. return;
  1163. pData->postProc.panning = fixedValue;
  1164. }
  1165. #endif // ! BUILD_BRIDGE
  1166. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1167. {
  1168. #ifndef BUILD_BRIDGE
  1169. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1170. #endif
  1171. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  1172. if (pData->ctrlChannel == channel)
  1173. return;
  1174. pData->ctrlChannel = channel;
  1175. #ifndef BUILD_BRIDGE
  1176. const float channelf(channel);
  1177. # ifdef HAVE_LIBLO
  1178. if (sendOsc && pData->engine->isOscControlRegistered())
  1179. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, channelf);
  1180. # endif
  1181. if (sendCallback)
  1182. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, channelf, nullptr);
  1183. #endif
  1184. // may be unused
  1185. return; (void)sendOsc; (void)sendCallback;
  1186. }
  1187. // -------------------------------------------------------------------
  1188. // Set data (plugin-specific stuff)
  1189. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1190. {
  1191. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1192. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1193. uiParameterChange(parameterId, value);
  1194. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1195. if (sendOsc && pData->engine->isOscControlRegistered())
  1196. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  1197. #endif
  1198. if (sendCallback)
  1199. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  1200. // may be unused
  1201. return; (void)sendOsc;
  1202. }
  1203. void CarlaPlugin::setParameterValueRT(const uint32_t parameterId, const float value) noexcept
  1204. {
  1205. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(parameterId), 0, value);
  1206. }
  1207. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1208. {
  1209. #ifndef BUILD_BRIDGE
  1210. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  1211. switch (rindex)
  1212. {
  1213. case PARAMETER_ACTIVE:
  1214. return setActive((value > 0.0f), sendOsc, sendCallback);
  1215. case PARAMETER_CTRL_CHANNEL:
  1216. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  1217. case PARAMETER_DRYWET:
  1218. return setDryWet(value, sendOsc, sendCallback);
  1219. case PARAMETER_VOLUME:
  1220. return setVolume(value, sendOsc, sendCallback);
  1221. case PARAMETER_BALANCE_LEFT:
  1222. return setBalanceLeft(value, sendOsc, sendCallback);
  1223. case PARAMETER_BALANCE_RIGHT:
  1224. return setBalanceRight(value, sendOsc, sendCallback);
  1225. case PARAMETER_PANNING:
  1226. return setPanning(value, sendOsc, sendCallback);
  1227. }
  1228. #endif
  1229. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  1230. for (uint32_t i=0; i < pData->param.count; ++i)
  1231. {
  1232. if (pData->param.data[i].rindex == rindex)
  1233. {
  1234. //if (carla_isNotEqual(getParameterValue(i), value))
  1235. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  1236. break;
  1237. }
  1238. }
  1239. }
  1240. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  1241. {
  1242. #ifndef BUILD_BRIDGE
  1243. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1244. #endif
  1245. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1246. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1247. pData->param.data[parameterId].midiChannel = channel;
  1248. #ifndef BUILD_BRIDGE
  1249. # ifdef HAVE_LIBLO
  1250. if (sendOsc && pData->engine->isOscControlRegistered())
  1251. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  1252. # endif
  1253. if (sendCallback)
  1254. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  1255. #endif
  1256. // may be unused
  1257. return; (void)sendOsc; (void)sendCallback;
  1258. }
  1259. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  1260. {
  1261. #ifndef BUILD_BRIDGE
  1262. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  1263. #endif
  1264. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  1265. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL,);
  1266. pData->param.data[parameterId].midiCC = cc;
  1267. #ifndef BUILD_BRIDGE
  1268. # ifdef HAVE_LIBLO
  1269. if (sendOsc && pData->engine->isOscControlRegistered())
  1270. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  1271. # endif
  1272. if (sendCallback)
  1273. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  1274. #endif
  1275. // may be unused
  1276. return; (void)sendOsc; (void)sendCallback;
  1277. }
  1278. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  1279. {
  1280. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  1281. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  1282. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  1283. // Ignore some keys
  1284. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  1285. {
  1286. const PluginType ptype = getType();
  1287. if ((ptype == PLUGIN_INTERNAL && std::strncmp(key, "CarlaAlternateFile", 18) == 0) ||
  1288. (ptype == PLUGIN_DSSI && std::strcmp (key, "guiVisible") == 0) ||
  1289. (ptype == PLUGIN_LV2 && std::strncmp(key, "OSC:", 4) == 0))
  1290. return;
  1291. }
  1292. // Check if we already have this key
  1293. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  1294. {
  1295. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  1296. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1297. if (std::strcmp(customData.key, key) == 0)
  1298. {
  1299. if (customData.value != nullptr)
  1300. delete[] customData.value;
  1301. customData.value = carla_strdup(value);
  1302. return;
  1303. }
  1304. }
  1305. // Otherwise store it
  1306. CustomData customData;
  1307. customData.type = carla_strdup(type);
  1308. customData.key = carla_strdup(key);
  1309. customData.value = carla_strdup(value);
  1310. pData->custom.append(customData);
  1311. }
  1312. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  1313. {
  1314. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1315. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  1316. CARLA_SAFE_ASSERT(false); // this should never happen
  1317. }
  1318. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1319. {
  1320. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1321. pData->prog.current = index;
  1322. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1323. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1324. if (reallySendOsc && index < 50)
  1325. pData->engine->oscSend_control_set_current_program(pData->id, index);
  1326. #else
  1327. const bool reallySendOsc(false);
  1328. #endif
  1329. if (sendCallback)
  1330. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1331. // Change default parameter values
  1332. if (index >= 0)
  1333. {
  1334. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1335. uiProgramChange(static_cast<uint32_t>(index));
  1336. switch (getType())
  1337. {
  1338. case PLUGIN_GIG:
  1339. case PLUGIN_SF2:
  1340. case PLUGIN_SFZ:
  1341. break;
  1342. default:
  1343. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1344. break;
  1345. }
  1346. }
  1347. // may be unused
  1348. return; (void)sendOsc;
  1349. }
  1350. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool) noexcept
  1351. {
  1352. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1353. pData->midiprog.current = index;
  1354. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1355. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1356. if (reallySendOsc && index < 50)
  1357. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1358. #else
  1359. const bool reallySendOsc(false);
  1360. #endif
  1361. if (sendCallback)
  1362. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1363. // Change default parameter values
  1364. if (index >= 0)
  1365. {
  1366. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1367. uiMidiProgramChange(static_cast<uint32_t>(index));
  1368. switch (getType())
  1369. {
  1370. case PLUGIN_GIG:
  1371. case PLUGIN_SF2:
  1372. case PLUGIN_SFZ:
  1373. break;
  1374. default:
  1375. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1376. break;
  1377. }
  1378. }
  1379. // may be unused
  1380. return; (void)sendOsc;
  1381. }
  1382. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1383. {
  1384. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1385. {
  1386. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1387. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1388. }
  1389. }
  1390. void CarlaPlugin::setProgramRT(const uint32_t uindex) noexcept
  1391. {
  1392. CARLA_SAFE_ASSERT_RETURN(uindex < pData->prog.count,);
  1393. const int32_t index = static_cast<int32_t>(uindex);
  1394. pData->prog.current = index;
  1395. // Change default parameter values
  1396. switch (getType())
  1397. {
  1398. case PLUGIN_GIG:
  1399. case PLUGIN_SF2:
  1400. case PLUGIN_SFZ:
  1401. break;
  1402. default:
  1403. pData->updateDefaultParameterValues(this);
  1404. break;
  1405. }
  1406. pData->postponeRtEvent(kPluginPostRtEventProgramChange, index, 0, 0.0f);
  1407. }
  1408. void CarlaPlugin::setMidiProgramRT(const uint32_t uindex) noexcept
  1409. {
  1410. CARLA_SAFE_ASSERT_RETURN(uindex < pData->midiprog.count,);
  1411. const int32_t index = static_cast<int32_t>(uindex);
  1412. pData->midiprog.current = index;
  1413. // Change default parameter values
  1414. switch (getType())
  1415. {
  1416. case PLUGIN_GIG:
  1417. case PLUGIN_SF2:
  1418. case PLUGIN_SFZ:
  1419. break;
  1420. default:
  1421. pData->updateDefaultParameterValues(this);
  1422. break;
  1423. }
  1424. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  1425. }
  1426. // -------------------------------------------------------------------
  1427. // Plugin state
  1428. void CarlaPlugin::reloadPrograms(const bool)
  1429. {
  1430. }
  1431. // -------------------------------------------------------------------
  1432. // Plugin processing
  1433. void CarlaPlugin::activate() noexcept
  1434. {
  1435. CARLA_SAFE_ASSERT(! pData->active);
  1436. }
  1437. void CarlaPlugin::deactivate() noexcept
  1438. {
  1439. CARLA_SAFE_ASSERT(pData->active);
  1440. }
  1441. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1442. {
  1443. }
  1444. void CarlaPlugin::sampleRateChanged(const double)
  1445. {
  1446. }
  1447. void CarlaPlugin::offlineModeChanged(const bool)
  1448. {
  1449. }
  1450. // -------------------------------------------------------------------
  1451. // Misc
  1452. void CarlaPlugin::idle()
  1453. {
  1454. if (! pData->enabled)
  1455. return;
  1456. const bool hasUI(pData->hints & PLUGIN_HAS_CUSTOM_UI);
  1457. const bool needsUiMainThread(pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD);
  1458. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1459. const bool sendOsc(pData->engine->isOscControlRegistered());
  1460. #endif
  1461. const uint32_t latency(getLatencyInFrames());
  1462. if (pData->latency.frames != latency)
  1463. {
  1464. carla_stdout("latency changed to %i samples", latency);
  1465. const ScopedSingleProcessLocker sspl(this, true);
  1466. pData->client->setLatency(latency);
  1467. #ifndef BUILD_BRIDGE
  1468. pData->latency.recreateBuffers(pData->latency.channels, latency);
  1469. #else
  1470. pData->latency.frames = latency;
  1471. #endif
  1472. }
  1473. const CarlaMutexLocker sl(pData->postRtEvents.getDataMutex());
  1474. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.getDataIterator(); it.valid(); it.next())
  1475. {
  1476. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1477. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1478. switch (event.type)
  1479. {
  1480. case kPluginPostRtEventNull: {
  1481. } break;
  1482. case kPluginPostRtEventDebug: {
  1483. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1484. } break;
  1485. case kPluginPostRtEventParameterChange: {
  1486. // Update UI
  1487. if (event.value1 >= 0 && hasUI)
  1488. {
  1489. if (needsUiMainThread)
  1490. pData->postUiEvents.append(event);
  1491. else
  1492. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1493. }
  1494. if (event.value2 != 1)
  1495. {
  1496. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1497. // Update OSC control client
  1498. if (sendOsc)
  1499. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1500. #endif
  1501. // Update Host
  1502. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1503. }
  1504. } break;
  1505. case kPluginPostRtEventProgramChange: {
  1506. // Update UI
  1507. if (event.value1 >= 0 && hasUI)
  1508. {
  1509. if (needsUiMainThread)
  1510. pData->postUiEvents.append(event);
  1511. else
  1512. uiProgramChange(static_cast<uint32_t>(event.value1));
  1513. }
  1514. // Update param values
  1515. for (uint32_t j=0; j < pData->param.count; ++j)
  1516. {
  1517. const float paramDefault(pData->param.ranges[j].def);
  1518. const float paramValue(getParameterValue(j));
  1519. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1520. if (sendOsc && j < 50)
  1521. {
  1522. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1523. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1524. }
  1525. #endif
  1526. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1527. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1528. }
  1529. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1530. // Update OSC control client
  1531. if (sendOsc)
  1532. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1533. #endif
  1534. // Update Host
  1535. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1536. } break;
  1537. case kPluginPostRtEventMidiProgramChange: {
  1538. // Update UI
  1539. if (event.value1 >= 0 && hasUI)
  1540. {
  1541. if (needsUiMainThread)
  1542. pData->postUiEvents.append(event);
  1543. else
  1544. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1545. }
  1546. // Update param values
  1547. for (uint32_t j=0; j < pData->param.count; ++j)
  1548. {
  1549. const float paramDefault(pData->param.ranges[j].def);
  1550. const float paramValue(getParameterValue(j));
  1551. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1552. if (sendOsc && j < 50)
  1553. {
  1554. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1555. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1556. }
  1557. #endif
  1558. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1559. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1560. }
  1561. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1562. // Update OSC control client
  1563. if (sendOsc)
  1564. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1565. #endif
  1566. // Update Host
  1567. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1568. } break;
  1569. case kPluginPostRtEventNoteOn: {
  1570. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1571. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1572. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1573. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1574. const uint8_t note = static_cast<uint8_t>(event.value2);
  1575. const uint8_t velocity = uint8_t(event.value3);
  1576. // Update UI
  1577. if (hasUI)
  1578. {
  1579. if (needsUiMainThread)
  1580. pData->postUiEvents.append(event);
  1581. else
  1582. uiNoteOn(channel, note, velocity);
  1583. }
  1584. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1585. // Update OSC control client
  1586. if (sendOsc)
  1587. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1588. #endif
  1589. // Update Host
  1590. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1591. } break;
  1592. case kPluginPostRtEventNoteOff: {
  1593. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1594. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1595. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1596. const uint8_t note = static_cast<uint8_t>(event.value2);
  1597. // Update UI
  1598. if (hasUI)
  1599. {
  1600. if (needsUiMainThread)
  1601. pData->postUiEvents.append(event);
  1602. else
  1603. uiNoteOff(channel, note);
  1604. }
  1605. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1606. // Update OSC control client
  1607. if (sendOsc)
  1608. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1609. #endif
  1610. // Update Host
  1611. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1612. } break;
  1613. }
  1614. }
  1615. pData->postRtEvents.clearData();
  1616. }
  1617. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1618. {
  1619. if (forcedOffline)
  1620. {
  1621. #ifndef STOAT_TEST_BUILD
  1622. pData->masterMutex.lock();
  1623. return true;
  1624. #endif
  1625. }
  1626. return pData->masterMutex.tryLock();
  1627. }
  1628. void CarlaPlugin::unlock() noexcept
  1629. {
  1630. pData->masterMutex.unlock();
  1631. }
  1632. // -------------------------------------------------------------------
  1633. // Plugin buffers
  1634. void CarlaPlugin::initBuffers() const noexcept
  1635. {
  1636. pData->audioIn.initBuffers();
  1637. pData->audioOut.initBuffers();
  1638. pData->cvIn.initBuffers();
  1639. pData->cvOut.initBuffers();
  1640. pData->event.initBuffers();
  1641. }
  1642. void CarlaPlugin::clearBuffers() noexcept
  1643. {
  1644. pData->clearBuffers();
  1645. }
  1646. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1647. // -------------------------------------------------------------------
  1648. // OSC stuff
  1649. void CarlaPlugin::registerToOscClient() noexcept
  1650. {
  1651. if (! pData->engine->isOscControlRegistered())
  1652. return;
  1653. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1654. // Base data
  1655. {
  1656. char bufName[STR_MAX+1], bufLabel[STR_MAX+1], bufMaker[STR_MAX+1], bufCopyright[STR_MAX+1];
  1657. carla_zeroChars(bufName, STR_MAX);
  1658. carla_zeroChars(bufLabel, STR_MAX);
  1659. carla_zeroChars(bufMaker, STR_MAX);
  1660. carla_zeroChars(bufCopyright, STR_MAX);
  1661. getRealName(bufName);
  1662. getLabel(bufLabel);
  1663. getMaker(bufMaker);
  1664. getCopyright(bufCopyright);
  1665. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1666. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1667. }
  1668. // Base count
  1669. uint32_t paramIns, paramOuts;
  1670. {
  1671. getParameterCountInfo(paramIns, paramOuts);
  1672. if (paramIns > 49)
  1673. paramIns = 49;
  1674. if (paramOuts > 49)
  1675. paramOuts = 49;
  1676. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1677. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1678. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1679. }
  1680. // Plugin Parameters
  1681. if (const uint32_t count = std::min<uint32_t>(pData->param.count, 98U))
  1682. {
  1683. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1684. for (uint32_t i=0; i<count; ++i)
  1685. {
  1686. const ParameterData& paramData(pData->param.data[i]);
  1687. if (paramData.type == PARAMETER_INPUT)
  1688. {
  1689. if (--paramIns == 0)
  1690. break;
  1691. }
  1692. else if (paramData.type == PARAMETER_INPUT)
  1693. {
  1694. if (--paramOuts == 0)
  1695. break;
  1696. }
  1697. else
  1698. {
  1699. continue;
  1700. }
  1701. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1702. carla_zeroChars(bufName, STR_MAX);
  1703. carla_zeroChars(bufUnit, STR_MAX);
  1704. getParameterName(i, bufName);
  1705. getParameterUnit(i, bufUnit);
  1706. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1707. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1708. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1709. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1710. if (paramData.midiCC >= 0)
  1711. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1712. if (paramData.midiChannel != 0)
  1713. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1714. }
  1715. }
  1716. // Programs
  1717. if (const uint32_t count = std::min<uint32_t>(pData->prog.count, 50U))
  1718. {
  1719. pData->engine->oscSend_control_set_program_count(pData->id, count);
  1720. for (uint32_t i=0; i < count; ++i)
  1721. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1722. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1723. }
  1724. // MIDI Programs
  1725. if (const uint32_t count = std::min<uint32_t>(pData->midiprog.count, 50U))
  1726. {
  1727. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1728. for (uint32_t i=0; i < count; ++i)
  1729. {
  1730. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1731. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1732. }
  1733. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1734. }
  1735. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1736. // Internal Parameters
  1737. {
  1738. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1739. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1740. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1741. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1742. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1743. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1744. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1745. }
  1746. }
  1747. #endif
  1748. // FIXME
  1749. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1750. {
  1751. // do nothing
  1752. }
  1753. //#endif // HAVE_LIBLO && ! BUILD_BRIDGE
  1754. // -------------------------------------------------------------------
  1755. // MIDI events
  1756. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1757. {
  1758. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1759. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1760. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1761. if (! pData->active)
  1762. return;
  1763. ExternalMidiNote extNote;
  1764. extNote.channel = static_cast<int8_t>(channel);
  1765. extNote.note = note;
  1766. extNote.velo = velo;
  1767. pData->extNotes.appendNonRT(extNote);
  1768. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1769. {
  1770. if (velo > 0)
  1771. uiNoteOn(channel, note, velo);
  1772. else
  1773. uiNoteOff(channel, note);
  1774. }
  1775. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1776. if (sendOsc && pData->engine->isOscControlRegistered())
  1777. {
  1778. if (velo > 0)
  1779. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1780. else
  1781. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1782. }
  1783. #endif
  1784. if (sendCallback)
  1785. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1786. // may be unused
  1787. return; (void)sendOsc;
  1788. }
  1789. #ifndef BUILD_BRIDGE
  1790. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1791. {
  1792. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1793. return;
  1794. PluginPostRtEvent postEvent;
  1795. postEvent.type = kPluginPostRtEventNoteOff;
  1796. postEvent.value1 = pData->ctrlChannel;
  1797. postEvent.value2 = 0;
  1798. postEvent.value3 = 0.0f;
  1799. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1800. {
  1801. postEvent.value2 = i;
  1802. pData->postRtEvents.appendRT(postEvent);
  1803. }
  1804. }
  1805. #endif
  1806. // -------------------------------------------------------------------
  1807. // UI Stuff
  1808. void CarlaPlugin::showCustomUI(const bool)
  1809. {
  1810. CARLA_SAFE_ASSERT(false);
  1811. }
  1812. void CarlaPlugin::uiIdle()
  1813. {
  1814. if (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD)
  1815. {
  1816. // Update parameter outputs
  1817. for (uint32_t i=0; i < pData->param.count; ++i)
  1818. {
  1819. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1820. uiParameterChange(i, getParameterValue(i));
  1821. }
  1822. const CarlaMutexLocker sl(pData->postUiEvents.mutex);
  1823. for (LinkedList<PluginPostRtEvent>::Itenerator it = pData->postUiEvents.data.begin2(); it.valid(); it.next())
  1824. {
  1825. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1826. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1827. switch (event.type)
  1828. {
  1829. case kPluginPostRtEventNull:
  1830. case kPluginPostRtEventDebug:
  1831. break;
  1832. case kPluginPostRtEventParameterChange:
  1833. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1834. break;
  1835. case kPluginPostRtEventProgramChange:
  1836. uiProgramChange(static_cast<uint32_t>(event.value1));
  1837. break;
  1838. case kPluginPostRtEventMidiProgramChange:
  1839. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1840. break;
  1841. case kPluginPostRtEventNoteOn:
  1842. uiNoteOn(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2), uint8_t(event.value3));
  1843. break;
  1844. case kPluginPostRtEventNoteOff:
  1845. uiNoteOff(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2));
  1846. break;
  1847. }
  1848. }
  1849. pData->postUiEvents.data.clear();
  1850. }
  1851. #ifndef BUILD_BRIDGE
  1852. if (pData->transientTryCounter == 0)
  1853. return;
  1854. if (++pData->transientTryCounter % 10 != 0)
  1855. return;
  1856. if (pData->transientTryCounter >= 200)
  1857. return;
  1858. carla_stdout("Trying to get window...");
  1859. CarlaString uiTitle(pData->name);
  1860. uiTitle += " (GUI)";
  1861. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle,
  1862. pData->engine->getOptions().frontendWinId, pData->transientFirstTry))
  1863. {
  1864. pData->transientTryCounter = 0;
  1865. pData->transientFirstTry = false;
  1866. }
  1867. #endif
  1868. }
  1869. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1870. {
  1871. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1872. return;
  1873. // unused
  1874. (void)value;
  1875. }
  1876. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1877. {
  1878. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1879. }
  1880. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1881. {
  1882. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1883. }
  1884. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1885. {
  1886. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1887. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1888. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1889. }
  1890. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1891. {
  1892. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1893. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1894. }
  1895. bool CarlaPlugin::canRunInRack() const noexcept
  1896. {
  1897. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1898. }
  1899. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1900. {
  1901. return pData->engine;
  1902. }
  1903. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1904. {
  1905. return pData->client;
  1906. }
  1907. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1908. {
  1909. return pData->audioIn.ports[index].port;
  1910. }
  1911. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1912. {
  1913. return pData->audioOut.ports[index].port;
  1914. }
  1915. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  1916. {
  1917. return pData->cvIn.ports[index].port;
  1918. }
  1919. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  1920. {
  1921. return pData->cvOut.ports[index].port;
  1922. }
  1923. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  1924. {
  1925. return pData->event.portIn;
  1926. }
  1927. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  1928. {
  1929. return pData->event.portOut;
  1930. }
  1931. void* CarlaPlugin::getNativeHandle() const noexcept
  1932. {
  1933. return nullptr;
  1934. }
  1935. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  1936. {
  1937. return nullptr;
  1938. }
  1939. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  1940. {
  1941. return 0;
  1942. }
  1943. // -------------------------------------------------------------------
  1944. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  1945. {
  1946. return pData->nodeId;
  1947. }
  1948. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  1949. {
  1950. pData->nodeId = nodeId;
  1951. }
  1952. // -------------------------------------------------------------------
  1953. void CarlaPlugin::restoreLV2State() noexcept
  1954. {
  1955. }
  1956. void CarlaPlugin::waitForBridgeSaveSignal() noexcept
  1957. {
  1958. }
  1959. // -------------------------------------------------------------------
  1960. // Scoped Disabler
  1961. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1962. : fPlugin(plugin),
  1963. fWasEnabled(false)
  1964. {
  1965. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1966. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1967. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1968. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1969. plugin->pData->masterMutex.lock();
  1970. if (plugin->pData->enabled)
  1971. {
  1972. fWasEnabled = true;
  1973. plugin->pData->enabled = false;
  1974. if (plugin->pData->client->isActive())
  1975. plugin->pData->client->deactivate();
  1976. }
  1977. }
  1978. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1979. {
  1980. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1981. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1982. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1983. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1984. if (fWasEnabled)
  1985. {
  1986. fPlugin->pData->enabled = true;
  1987. fPlugin->pData->client->activate();
  1988. }
  1989. fPlugin->pData->masterMutex.unlock();
  1990. }
  1991. // -------------------------------------------------------------------
  1992. // Scoped Process Locker
  1993. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1994. : fPlugin(plugin),
  1995. fBlock(block)
  1996. {
  1997. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1998. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1999. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  2000. if (! fBlock)
  2001. return;
  2002. plugin->pData->singleMutex.lock();
  2003. }
  2004. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  2005. {
  2006. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  2007. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  2008. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  2009. if (! fBlock)
  2010. return;
  2011. #ifndef BUILD_BRIDGE
  2012. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  2013. fPlugin->pData->needsReset = true;
  2014. #endif
  2015. fPlugin->pData->singleMutex.unlock();
  2016. }
  2017. // -------------------------------------------------------------------
  2018. CARLA_BACKEND_END_NAMESPACE