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.

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