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.

1936 lines
61KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2014 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 <ctime>
  24. #include "juce_core.h"
  25. using juce::CharPointer_UTF8;
  26. using juce::File;
  27. using juce::MemoryOutputStream;
  28. using juce::ScopedPointer;
  29. using juce::String;
  30. using juce::XmlDocument;
  31. using juce::XmlElement;
  32. CARLA_BACKEND_START_NAMESPACE
  33. // -------------------------------------------------------------------
  34. // Fallback data
  35. static const ParameterData kParameterDataNull = { PARAMETER_UNKNOWN, 0x0, PARAMETER_NULL, -1, -1, 0 };
  36. static const ParameterRanges kParameterRangesNull = { 0.0f, 0.0f, 1.0f, 0.01f, 0.0001f, 0.1f };
  37. static const MidiProgramData kMidiProgramDataNull = { 0, 0, nullptr };
  38. static const CustomData kCustomDataNull = { nullptr, nullptr, nullptr };
  39. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  40. static /* */ CustomData kCustomDataFallbackNC = { nullptr, nullptr, nullptr };
  41. static const PluginPostRtEvent kPluginPostRtEventFallback = { kPluginPostRtEventNull, 0, 0, 0.0f };
  42. // -------------------------------------------------------------------
  43. // ParamSymbol struct, needed for CarlaPlugin::loadStateSave()
  44. struct ParamSymbol {
  45. int32_t index;
  46. const char* symbol;
  47. ParamSymbol(const uint32_t i, const char* const s)
  48. : index(static_cast<int32_t>(i)),
  49. symbol(carla_strdup(s)) {}
  50. ~ParamSymbol() noexcept
  51. {
  52. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr,)
  53. delete[] symbol;
  54. symbol = nullptr;
  55. }
  56. #ifdef CARLA_PROPER_CPP11_SUPPORT
  57. ParamSymbol() = delete;
  58. CARLA_DECLARE_NON_COPY_STRUCT(ParamSymbol)
  59. #endif
  60. };
  61. // -------------------------------------------------------------------
  62. // Constructor and destructor
  63. CarlaPlugin::CarlaPlugin(CarlaEngine* const engine, const uint id)
  64. : pData(new ProtectedData(engine, id))
  65. {
  66. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  67. CARLA_SAFE_ASSERT(id < engine->getMaxPluginNumber());
  68. carla_debug("CarlaPlugin::CarlaPlugin(%p, %i)", engine, id);
  69. switch (engine->getProccessMode())
  70. {
  71. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  72. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  73. CARLA_SAFE_ASSERT(id < MAX_DEFAULT_PLUGINS);
  74. break;
  75. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  76. CARLA_SAFE_ASSERT(id < MAX_RACK_PLUGINS);
  77. break;
  78. case ENGINE_PROCESS_MODE_PATCHBAY:
  79. CARLA_SAFE_ASSERT(id < MAX_PATCHBAY_PLUGINS);
  80. break;
  81. case ENGINE_PROCESS_MODE_BRIDGE:
  82. CARLA_SAFE_ASSERT(id == 0);
  83. break;
  84. }
  85. }
  86. CarlaPlugin::~CarlaPlugin()
  87. {
  88. carla_debug("CarlaPlugin::~CarlaPlugin()");
  89. delete pData;
  90. }
  91. // -------------------------------------------------------------------
  92. // Information (base)
  93. uint CarlaPlugin::getId() const noexcept
  94. {
  95. return pData->id;
  96. }
  97. uint CarlaPlugin::getHints() const noexcept
  98. {
  99. return pData->hints;
  100. }
  101. uint CarlaPlugin::getOptionsEnabled() const noexcept
  102. {
  103. return pData->options;
  104. }
  105. bool CarlaPlugin::isEnabled() const noexcept
  106. {
  107. return pData->enabled;
  108. }
  109. const char* CarlaPlugin::getName() const noexcept
  110. {
  111. return pData->name;
  112. }
  113. const char* CarlaPlugin::getFilename() const noexcept
  114. {
  115. return pData->filename;
  116. }
  117. const char* CarlaPlugin::getIconName() const noexcept
  118. {
  119. return pData->iconName;
  120. }
  121. PluginCategory CarlaPlugin::getCategory() const noexcept
  122. {
  123. return getPluginCategoryFromName(pData->name);
  124. }
  125. int64_t CarlaPlugin::getUniqueId() const noexcept
  126. {
  127. return 0;
  128. }
  129. uint32_t CarlaPlugin::getLatencyInFrames() const noexcept
  130. {
  131. return pData->latency;
  132. }
  133. // -------------------------------------------------------------------
  134. // Information (count)
  135. uint32_t CarlaPlugin::getAudioInCount() const noexcept
  136. {
  137. return pData->audioIn.count;
  138. }
  139. uint32_t CarlaPlugin::getAudioOutCount() const noexcept
  140. {
  141. return pData->audioOut.count;
  142. }
  143. uint32_t CarlaPlugin::getCVInCount() const noexcept
  144. {
  145. return pData->cvIn.count;
  146. }
  147. uint32_t CarlaPlugin::getCVOutCount() const noexcept
  148. {
  149. return pData->cvOut.count;
  150. }
  151. uint32_t CarlaPlugin::getMidiInCount() const noexcept
  152. {
  153. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_IN) ? 1 : 0;
  154. }
  155. uint32_t CarlaPlugin::getMidiOutCount() const noexcept
  156. {
  157. return (pData->extraHints & PLUGIN_EXTRA_HINT_HAS_MIDI_OUT) ? 1 : 0;
  158. }
  159. uint32_t CarlaPlugin::getParameterCount() const noexcept
  160. {
  161. return pData->param.count;
  162. }
  163. uint32_t CarlaPlugin::getParameterScalePointCount(const uint32_t parameterId) const noexcept
  164. {
  165. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  166. return 0;
  167. }
  168. uint32_t CarlaPlugin::getProgramCount() const noexcept
  169. {
  170. return pData->prog.count;
  171. }
  172. uint32_t CarlaPlugin::getMidiProgramCount() const noexcept
  173. {
  174. return pData->midiprog.count;
  175. }
  176. uint32_t CarlaPlugin::getCustomDataCount() const noexcept
  177. {
  178. return static_cast<uint32_t>(pData->custom.count());
  179. }
  180. // -------------------------------------------------------------------
  181. // Information (current data)
  182. int32_t CarlaPlugin::getCurrentProgram() const noexcept
  183. {
  184. return pData->prog.current;
  185. }
  186. int32_t CarlaPlugin::getCurrentMidiProgram() const noexcept
  187. {
  188. return pData->midiprog.current;
  189. }
  190. const ParameterData& CarlaPlugin::getParameterData(const uint32_t parameterId) const noexcept
  191. {
  192. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterDataNull);
  193. return pData->param.data[parameterId];
  194. }
  195. const ParameterRanges& CarlaPlugin::getParameterRanges(const uint32_t parameterId) const noexcept
  196. {
  197. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, kParameterRangesNull);
  198. return pData->param.ranges[parameterId];
  199. }
  200. bool CarlaPlugin::isParameterOutput(const uint32_t parameterId) const noexcept
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  203. return (pData->param.data[parameterId].type == PARAMETER_OUTPUT);
  204. }
  205. const MidiProgramData& CarlaPlugin::getMidiProgramData(const uint32_t index) const noexcept
  206. {
  207. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count, kMidiProgramDataNull);
  208. return pData->midiprog.data[index];
  209. }
  210. const CustomData& CarlaPlugin::getCustomData(const uint32_t index) const noexcept
  211. {
  212. CARLA_SAFE_ASSERT_RETURN(index < pData->custom.count(), kCustomDataNull);
  213. return pData->custom.getAt(index, kCustomDataFallback);
  214. }
  215. std::size_t CarlaPlugin::getChunkData(void** const dataPtr) noexcept
  216. {
  217. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  218. CARLA_SAFE_ASSERT(false); // this should never happen
  219. return 0;
  220. }
  221. // -------------------------------------------------------------------
  222. // Information (per-plugin data)
  223. uint CarlaPlugin::getOptionsAvailable() const noexcept
  224. {
  225. CARLA_SAFE_ASSERT(false); // this should never happen
  226. return 0x0;
  227. }
  228. float CarlaPlugin::getParameterValue(const uint32_t parameterId) const noexcept
  229. {
  230. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  231. CARLA_SAFE_ASSERT(false); // this should never happen
  232. return 0.0f;
  233. }
  234. float CarlaPlugin::getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept
  235. {
  236. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(), 0.0f);
  237. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  238. CARLA_SAFE_ASSERT(false); // this should never happen
  239. return 0.0f;
  240. }
  241. void CarlaPlugin::getLabel(char* const strBuf) const noexcept
  242. {
  243. strBuf[0] = '\0';
  244. }
  245. void CarlaPlugin::getMaker(char* const strBuf) const noexcept
  246. {
  247. strBuf[0] = '\0';
  248. }
  249. void CarlaPlugin::getCopyright(char* const strBuf) const noexcept
  250. {
  251. strBuf[0] = '\0';
  252. }
  253. void CarlaPlugin::getRealName(char* const strBuf) const noexcept
  254. {
  255. strBuf[0] = '\0';
  256. }
  257. void CarlaPlugin::getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept
  258. {
  259. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  260. CARLA_SAFE_ASSERT(false); // this should never happen
  261. strBuf[0] = '\0';
  262. }
  263. void CarlaPlugin::getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept
  264. {
  265. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  266. strBuf[0] = '\0';
  267. }
  268. void CarlaPlugin::getParameterText(const uint32_t parameterId, char* const strBuf) const noexcept
  269. {
  270. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  271. CARLA_SAFE_ASSERT(false); // this should never happen
  272. strBuf[0] = '\0';
  273. }
  274. void CarlaPlugin::getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept
  275. {
  276. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  277. strBuf[0] = '\0';
  278. }
  279. void CarlaPlugin::getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept
  280. {
  281. CARLA_SAFE_ASSERT_RETURN(parameterId < getParameterCount(),);
  282. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  283. CARLA_SAFE_ASSERT(false); // this should never happen
  284. strBuf[0] = '\0';
  285. }
  286. float CarlaPlugin::getInternalParameterValue(const int32_t parameterId) const noexcept
  287. {
  288. #ifndef BUILD_BRIDGE
  289. CARLA_SAFE_ASSERT_RETURN(parameterId != PARAMETER_NULL && parameterId > PARAMETER_MAX, 0.0f);
  290. switch (parameterId)
  291. {
  292. case PARAMETER_ACTIVE:
  293. return pData->active;
  294. case PARAMETER_CTRL_CHANNEL:
  295. return pData->ctrlChannel;
  296. case PARAMETER_DRYWET:
  297. return pData->postProc.dryWet;
  298. case PARAMETER_VOLUME:
  299. return pData->postProc.volume;
  300. case PARAMETER_BALANCE_LEFT:
  301. return pData->postProc.balanceLeft;
  302. case PARAMETER_BALANCE_RIGHT:
  303. return pData->postProc.balanceRight;
  304. case PARAMETER_PANNING:
  305. return pData->postProc.panning;
  306. };
  307. #endif
  308. CARLA_SAFE_ASSERT_RETURN(parameterId >= 0, 0.0f);
  309. return getParameterValue(static_cast<uint32_t>(parameterId));
  310. }
  311. void CarlaPlugin::getProgramName(const uint32_t index, char* const strBuf) const noexcept
  312. {
  313. CARLA_SAFE_ASSERT_RETURN(index < pData->prog.count,);
  314. CARLA_SAFE_ASSERT_RETURN(pData->prog.names[index] != nullptr,);
  315. std::strncpy(strBuf, pData->prog.names[index], STR_MAX);
  316. }
  317. void CarlaPlugin::getMidiProgramName(const uint32_t index, char* const strBuf) const noexcept
  318. {
  319. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  320. CARLA_SAFE_ASSERT_RETURN(pData->midiprog.data[index].name != nullptr,);
  321. std::strncpy(strBuf, pData->midiprog.data[index].name, STR_MAX);
  322. }
  323. void CarlaPlugin::getParameterCountInfo(uint32_t& ins, uint32_t& outs) const noexcept
  324. {
  325. ins = 0;
  326. outs = 0;
  327. for (uint32_t i=0; i < pData->param.count; ++i)
  328. {
  329. if (pData->param.data[i].type == PARAMETER_INPUT)
  330. ++ins;
  331. else if (pData->param.data[i].type == PARAMETER_OUTPUT)
  332. ++outs;
  333. }
  334. }
  335. // -------------------------------------------------------------------
  336. // Set data (state)
  337. void CarlaPlugin::prepareForSave()
  338. {
  339. }
  340. void CarlaPlugin::resetParameters() noexcept
  341. {
  342. for (uint i=0; i < pData->param.count; ++i)
  343. {
  344. const ParameterData& paramData(pData->param.data[i]);
  345. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  346. if (paramData.type != PARAMETER_INPUT)
  347. continue;
  348. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  349. continue;
  350. setParameterValue(i, paramRanges.def, true, true, true);
  351. }
  352. }
  353. void CarlaPlugin::randomizeParameters() noexcept
  354. {
  355. float value, random;
  356. char strBuf[STR_MAX+1];
  357. strBuf[STR_MAX] = '\0';
  358. std::srand(static_cast<uint>(std::time(nullptr)));
  359. for (uint i=0; i < pData->param.count; ++i)
  360. {
  361. const ParameterData& paramData(pData->param.data[i]);
  362. if (paramData.type != PARAMETER_INPUT)
  363. continue;
  364. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  365. continue;
  366. getParameterName(i, strBuf);
  367. if (std::strstr(strBuf, "olume") != nullptr)
  368. continue;
  369. if (std::strstr(strBuf, "Master") != nullptr)
  370. continue;
  371. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  372. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  373. {
  374. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  375. value = random > 0.5 ? paramRanges.max : paramRanges.min;
  376. }
  377. else
  378. {
  379. random = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
  380. value = random * (paramRanges.max - paramRanges.min) + paramRanges.min;
  381. if (paramData.hints & PARAMETER_IS_INTEGER)
  382. value = std::rint(value);
  383. }
  384. setParameterValue(i, value, true, true, true);
  385. }
  386. }
  387. const CarlaStateSave& CarlaPlugin::getStateSave()
  388. {
  389. pData->stateSave.clear();
  390. prepareForSave();
  391. const PluginType pluginType(getType());
  392. char strBuf[STR_MAX+1];
  393. // ---------------------------------------------------------------
  394. // Basic info
  395. getLabel(strBuf);
  396. pData->stateSave.type = carla_strdup(getPluginTypeAsString(getType()));
  397. pData->stateSave.name = carla_strdup(pData->name);
  398. pData->stateSave.label = carla_strdup(strBuf);
  399. pData->stateSave.uniqueId = getUniqueId();
  400. #ifndef BUILD_BRIDGE
  401. pData->stateSave.options = pData->options;
  402. #endif
  403. if (pData->filename != nullptr)
  404. pData->stateSave.binary = carla_strdup(pData->filename);
  405. #ifndef BUILD_BRIDGE
  406. // ---------------------------------------------------------------
  407. // Internals
  408. pData->stateSave.active = pData->active;
  409. pData->stateSave.dryWet = pData->postProc.dryWet;
  410. pData->stateSave.volume = pData->postProc.volume;
  411. pData->stateSave.balanceLeft = pData->postProc.balanceLeft;
  412. pData->stateSave.balanceRight = pData->postProc.balanceRight;
  413. pData->stateSave.panning = pData->postProc.panning;
  414. pData->stateSave.ctrlChannel = pData->ctrlChannel;
  415. #endif
  416. // ---------------------------------------------------------------
  417. // Chunk
  418. if (pData->options & PLUGIN_OPTION_USE_CHUNKS)
  419. {
  420. void* data = nullptr;
  421. const std::size_t dataSize(getChunkData(&data));
  422. if (data != nullptr && dataSize > 0)
  423. {
  424. pData->stateSave.chunk = CarlaString::asBase64(data, dataSize).dup();
  425. // Don't save anything else if using chunks
  426. return pData->stateSave;
  427. }
  428. }
  429. // ---------------------------------------------------------------
  430. // Current Program
  431. if (pData->prog.current >= 0 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_GIG)
  432. {
  433. pData->stateSave.currentProgramIndex = pData->prog.current;
  434. pData->stateSave.currentProgramName = carla_strdup(pData->prog.names[pData->prog.current]);
  435. }
  436. // ---------------------------------------------------------------
  437. // Current MIDI Program
  438. if (pData->midiprog.current >= 0 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_SF2)
  439. {
  440. const MidiProgramData& mpData(pData->midiprog.getCurrent());
  441. pData->stateSave.currentMidiBank = static_cast<int32_t>(mpData.bank);
  442. pData->stateSave.currentMidiProgram = static_cast<int32_t>(mpData.program);
  443. }
  444. // ---------------------------------------------------------------
  445. // Parameters
  446. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  447. for (uint32_t i=0; i < pData->param.count; ++i)
  448. {
  449. const ParameterData& paramData(pData->param.data[i]);
  450. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  451. continue;
  452. CarlaStateSave::Parameter* const stateParameter(new CarlaStateSave::Parameter());
  453. stateParameter->isInput = (paramData.type == PARAMETER_INPUT);
  454. stateParameter->index = paramData.index;
  455. #ifndef BUILD_BRIDGE
  456. stateParameter->midiCC = paramData.midiCC;
  457. stateParameter->midiChannel = paramData.midiChannel;
  458. #endif
  459. getParameterName(i, strBuf);
  460. stateParameter->name = carla_strdup(strBuf);
  461. getParameterSymbol(i, strBuf);
  462. stateParameter->symbol = carla_strdup(strBuf);;
  463. stateParameter->value = getParameterValue(i);
  464. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  465. stateParameter->value /= sampleRate;
  466. pData->stateSave.parameters.append(stateParameter);
  467. }
  468. // ---------------------------------------------------------------
  469. // Custom Data
  470. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  471. {
  472. const CustomData& cData(it.getValue(kCustomDataFallback));
  473. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  474. CarlaStateSave::CustomData* stateCustomData(new CarlaStateSave::CustomData());
  475. stateCustomData->type = carla_strdup(cData.type);
  476. stateCustomData->key = carla_strdup(cData.key);
  477. stateCustomData->value = carla_strdup(cData.value);
  478. pData->stateSave.customData.append(stateCustomData);
  479. }
  480. return pData->stateSave;
  481. }
  482. void CarlaPlugin::loadStateSave(const CarlaStateSave& stateSave)
  483. {
  484. char strBuf[STR_MAX+1];
  485. const bool usesMultiProgs(pData->extraHints & PLUGIN_EXTRA_HINT_USES_MULTI_PROGS);
  486. // ---------------------------------------------------------------
  487. // Part 1 - PRE-set custom data (only that which reload programs)
  488. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin(); it.valid(); it.next())
  489. {
  490. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  491. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  492. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  493. const char* const key(stateCustomData->key);
  494. bool wantData = false;
  495. if (getType() == PLUGIN_DSSI && (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0))
  496. wantData = true;
  497. else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  498. wantData = true;
  499. if (wantData)
  500. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  501. }
  502. // ---------------------------------------------------------------
  503. // Part 2 - set program
  504. if (stateSave.currentProgramIndex >= 0 && stateSave.currentProgramName != nullptr)
  505. {
  506. int32_t programId = -1;
  507. // index < count
  508. if (stateSave.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
  509. {
  510. programId = stateSave.currentProgramIndex;
  511. }
  512. // index not valid, try to find by name
  513. else
  514. {
  515. for (uint32_t i=0; i < pData->prog.count; ++i)
  516. {
  517. strBuf[0] = '\0';
  518. getProgramName(i, strBuf);
  519. if (strBuf[0] != '\0' && std::strcmp(stateSave.currentProgramName, strBuf) == 0)
  520. {
  521. programId = static_cast<int32_t>(i);
  522. break;
  523. }
  524. }
  525. }
  526. // set program now, if valid
  527. if (programId >= 0)
  528. setProgram(programId, true, true, true);
  529. }
  530. // ---------------------------------------------------------------
  531. // Part 3 - set midi program
  532. if (stateSave.currentMidiBank >= 0 && stateSave.currentMidiProgram >= 0 && ! usesMultiProgs)
  533. setMidiProgramById(static_cast<uint32_t>(stateSave.currentMidiBank), static_cast<uint32_t>(stateSave.currentMidiProgram), true, true, true);
  534. // ---------------------------------------------------------------
  535. // Part 4a - get plugin parameter symbols
  536. LinkedList<ParamSymbol*> paramSymbols;
  537. if (getType() == PLUGIN_LADSPA || getType() == PLUGIN_LV2)
  538. {
  539. for (uint32_t i=0; i < pData->param.count; ++i)
  540. {
  541. strBuf[0] = '\0';
  542. getParameterSymbol(i, strBuf);
  543. if (strBuf[0] != '\0')
  544. {
  545. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  546. paramSymbols.append(paramSymbol);
  547. }
  548. }
  549. }
  550. // ---------------------------------------------------------------
  551. // Part 4b - set parameter values (carefully)
  552. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  553. for (CarlaStateSave::ParameterItenerator it = stateSave.parameters.begin(); it.valid(); it.next())
  554. {
  555. CarlaStateSave::Parameter* const stateParameter(it.getValue(nullptr));
  556. CARLA_SAFE_ASSERT_CONTINUE(stateParameter != nullptr);
  557. int32_t index = -1;
  558. if (getType() == PLUGIN_LADSPA)
  559. {
  560. // Try to set by symbol, otherwise use index
  561. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  562. {
  563. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin(); it2.valid(); it2.next())
  564. {
  565. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  566. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  567. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  568. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  569. {
  570. index = paramSymbol->index;
  571. break;
  572. }
  573. }
  574. if (index == -1)
  575. index = stateParameter->index;
  576. }
  577. else
  578. index = stateParameter->index;
  579. }
  580. else if (getType() == PLUGIN_LV2)
  581. {
  582. // Symbol only
  583. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  584. {
  585. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin(); it2.valid(); it2.next())
  586. {
  587. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  588. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  589. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  590. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  591. {
  592. index = paramSymbol->index;
  593. break;
  594. }
  595. }
  596. if (index == -1)
  597. carla_stderr("Failed to find LV2 parameter symbol '%s')", stateParameter->symbol);
  598. }
  599. else
  600. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  601. }
  602. else
  603. {
  604. // Index only
  605. index = stateParameter->index;
  606. }
  607. // Now set parameter
  608. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  609. {
  610. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  611. if (stateParameter->isInput)
  612. {
  613. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  614. stateParameter->value *= sampleRate;
  615. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  616. }
  617. #ifndef BUILD_BRIDGE
  618. setParameterMidiCC(static_cast<uint32_t>(index), stateParameter->midiCC, true, true);
  619. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  620. #endif
  621. }
  622. else
  623. carla_stderr("Could not set parameter data for '%s'", stateParameter->name);
  624. }
  625. // ---------------------------------------------------------------
  626. // Part 4c - clear
  627. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin(); it.valid(); it.next())
  628. {
  629. ParamSymbol* const paramSymbol(it.getValue(nullptr));
  630. delete paramSymbol;
  631. }
  632. paramSymbols.clear();
  633. // ---------------------------------------------------------------
  634. // Part 5 - set custom data
  635. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin(); it.valid(); it.next())
  636. {
  637. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  638. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  639. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  640. const char* const key(stateCustomData->key);
  641. if (getType() == PLUGIN_DSSI && (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0))
  642. continue;
  643. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  644. continue;
  645. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  646. }
  647. // ---------------------------------------------------------------
  648. // Part 5x - set lv2 state
  649. if (getType() == PLUGIN_LV2 && pData->custom.count() > 0)
  650. setCustomData(CUSTOM_DATA_TYPE_STRING, "CarlaLoadLv2StateNow", "true", true);
  651. // ---------------------------------------------------------------
  652. // Part 6 - set chunk
  653. if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  654. {
  655. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stateSave.chunk));
  656. setChunkData(chunk.data(), chunk.size());
  657. }
  658. #ifndef BUILD_BRIDGE
  659. // ---------------------------------------------------------------
  660. // Part 6 - set internal stuff
  661. const uint availOptions(getOptionsAvailable());
  662. for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
  663. {
  664. const uint option(1u << i);
  665. if (availOptions & option)
  666. setOption(option, (stateSave.options & option) != 0, true);
  667. }
  668. setDryWet(stateSave.dryWet, true, true);
  669. setVolume(stateSave.volume, true, true);
  670. setBalanceLeft(stateSave.balanceLeft, true, true);
  671. setBalanceRight(stateSave.balanceRight, true, true);
  672. setPanning(stateSave.panning, true, true);
  673. setCtrlChannel(stateSave.ctrlChannel, true, true);
  674. setActive(stateSave.active, true, true);
  675. #endif
  676. }
  677. bool CarlaPlugin::saveStateToFile(const char* const filename)
  678. {
  679. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  680. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  681. MemoryOutputStream out;
  682. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  683. out << "<!DOCTYPE CARLA-PRESET>\n";
  684. out << "<CARLA-PRESET VERSION='2.0'>\n";
  685. out << getStateSave().toString();
  686. out << "</CARLA-PRESET>\n";
  687. const String jfilename = String(CharPointer_UTF8(filename));
  688. File file(jfilename);
  689. if (file.replaceWithData(out.getData(), out.getDataSize()))
  690. return true;
  691. pData->engine->setLastError("Failed to write file");
  692. return false;
  693. }
  694. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  695. {
  696. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  697. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  698. const String jfilename = String(CharPointer_UTF8(filename));
  699. File file(jfilename);
  700. CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
  701. XmlDocument xml(file);
  702. ScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
  703. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  704. CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
  705. // completely load file
  706. xmlElement = xml.getDocumentElement(false);
  707. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  708. if (pData->stateSave.fillFromXmlElement(xmlElement->getFirstChildElement()))
  709. loadStateSave(pData->stateSave);
  710. return true;
  711. }
  712. // -------------------------------------------------------------------
  713. // Set data (internal stuff)
  714. void CarlaPlugin::setId(const uint newId) noexcept
  715. {
  716. pData->id = newId;
  717. }
  718. void CarlaPlugin::setName(const char* const newName)
  719. {
  720. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  721. if (pData->name != nullptr)
  722. delete[] pData->name;
  723. pData->name = carla_strdup(newName);
  724. }
  725. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  726. {
  727. CARLA_SAFE_ASSERT_RETURN(getOptionsAvailable() & option,);
  728. if (yesNo)
  729. pData->options |= option;
  730. else
  731. pData->options &= ~option;
  732. #ifndef BUILD_BRIDGE
  733. if (sendCallback)
  734. pData->engine->callback(ENGINE_CALLBACK_OPTION_CHANGED, pData->id, static_cast<int>(option), yesNo ? 1 : 0, 0.0f, nullptr);
  735. #else
  736. // unused
  737. return; (void)sendCallback;
  738. #endif
  739. }
  740. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  741. {
  742. if (pData->enabled == yesNo)
  743. return;
  744. pData->enabled = yesNo;
  745. pData->masterMutex.lock();
  746. pData->masterMutex.unlock();
  747. }
  748. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  749. {
  750. #ifndef BUILD_BRIDGE
  751. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  752. #endif
  753. if (pData->active == active)
  754. return;
  755. {
  756. const ScopedSingleProcessLocker spl(this, true);
  757. if (active)
  758. activate();
  759. else
  760. deactivate();
  761. }
  762. pData->active = active;
  763. #ifndef BUILD_BRIDGE
  764. const float value(active ? 1.0f : 0.0f);
  765. if (sendOsc && pData->engine->isOscControlRegistered())
  766. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  767. if (sendCallback)
  768. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  769. #else
  770. // unused
  771. return; (void)sendOsc; (void)sendCallback;
  772. #endif
  773. }
  774. #ifndef BUILD_BRIDGE
  775. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  776. {
  777. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  778. const float fixedValue(carla_fixValue<float>(0.0f, 1.0f, value));
  779. if (carla_compareFloats(pData->postProc.dryWet, fixedValue))
  780. return;
  781. pData->postProc.dryWet = fixedValue;
  782. if (sendOsc && pData->engine->isOscControlRegistered())
  783. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  784. if (sendCallback)
  785. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  786. }
  787. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  788. {
  789. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  790. const float fixedValue(carla_fixValue<float>(0.0f, 1.27f, value));
  791. if (carla_compareFloats(pData->postProc.volume, fixedValue))
  792. return;
  793. pData->postProc.volume = fixedValue;
  794. if (sendOsc && pData->engine->isOscControlRegistered())
  795. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  796. if (sendCallback)
  797. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  798. }
  799. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  800. {
  801. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  802. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  803. if (carla_compareFloats(pData->postProc.balanceLeft, fixedValue))
  804. return;
  805. pData->postProc.balanceLeft = fixedValue;
  806. if (sendOsc && pData->engine->isOscControlRegistered())
  807. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  808. if (sendCallback)
  809. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  810. }
  811. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  812. {
  813. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  814. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  815. if (carla_compareFloats(pData->postProc.balanceRight, fixedValue))
  816. return;
  817. pData->postProc.balanceRight = fixedValue;
  818. if (sendOsc && pData->engine->isOscControlRegistered())
  819. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  820. if (sendCallback)
  821. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  822. }
  823. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  824. {
  825. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  826. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  827. if (carla_compareFloats(pData->postProc.panning, fixedValue))
  828. return;
  829. pData->postProc.panning = fixedValue;
  830. if (sendOsc && pData->engine->isOscControlRegistered())
  831. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  832. if (sendCallback)
  833. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  834. }
  835. #endif
  836. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  837. {
  838. #ifndef BUILD_BRIDGE
  839. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  840. #endif
  841. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  842. if (pData->ctrlChannel == channel)
  843. return;
  844. pData->ctrlChannel = channel;
  845. #ifndef BUILD_BRIDGE
  846. const float channelf(channel);
  847. if (sendOsc && pData->engine->isOscControlRegistered())
  848. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, channelf);
  849. if (sendCallback)
  850. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, channelf, nullptr);
  851. #else
  852. // unused
  853. return; (void)sendOsc; (void)sendCallback;
  854. #endif
  855. }
  856. // -------------------------------------------------------------------
  857. // Set data (plugin-specific stuff)
  858. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  859. {
  860. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  861. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  862. uiParameterChange(parameterId, value);
  863. #ifndef BUILD_BRIDGE
  864. if (sendOsc && pData->engine->isOscControlRegistered())
  865. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  866. #endif
  867. if (sendCallback)
  868. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  869. #ifdef BUILD_BRIDGE
  870. // unused
  871. return; (void)sendOsc;
  872. #endif
  873. }
  874. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  875. {
  876. #ifndef BUILD_BRIDGE
  877. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  878. switch (rindex)
  879. {
  880. case PARAMETER_ACTIVE:
  881. return setActive((value > 0.0f), sendOsc, sendCallback);
  882. case PARAMETER_CTRL_CHANNEL:
  883. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  884. case PARAMETER_DRYWET:
  885. return setDryWet(value, sendOsc, sendCallback);
  886. case PARAMETER_VOLUME:
  887. return setVolume(value, sendOsc, sendCallback);
  888. case PARAMETER_BALANCE_LEFT:
  889. return setBalanceLeft(value, sendOsc, sendCallback);
  890. case PARAMETER_BALANCE_RIGHT:
  891. return setBalanceRight(value, sendOsc, sendCallback);
  892. case PARAMETER_PANNING:
  893. return setPanning(value, sendOsc, sendCallback);
  894. }
  895. #endif
  896. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  897. for (uint32_t i=0; i < pData->param.count; ++i)
  898. {
  899. if (pData->param.data[i].rindex == rindex)
  900. {
  901. //if (! carla_compareFloats(getParameterValue(i), value))
  902. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  903. break;
  904. }
  905. }
  906. }
  907. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  908. {
  909. #ifndef BUILD_BRIDGE
  910. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  911. #endif
  912. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  913. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  914. pData->param.data[parameterId].midiChannel = channel;
  915. #ifndef BUILD_BRIDGE
  916. if (sendOsc && pData->engine->isOscControlRegistered())
  917. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  918. if (sendCallback)
  919. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  920. #else
  921. // unused
  922. return; (void)sendOsc; (void)sendCallback;
  923. #endif
  924. }
  925. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  926. {
  927. #ifndef BUILD_BRIDGE
  928. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  929. #endif
  930. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  931. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL,);
  932. pData->param.data[parameterId].midiCC = cc;
  933. #ifndef BUILD_BRIDGE
  934. if (sendOsc && pData->engine->isOscControlRegistered())
  935. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  936. if (sendCallback)
  937. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  938. #else
  939. // unused
  940. return; (void)sendOsc; (void)sendCallback;
  941. #endif
  942. }
  943. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  944. {
  945. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  946. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  947. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  948. // Ignore some keys
  949. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  950. {
  951. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  952. return;
  953. }
  954. // Check if we already have this key
  955. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  956. {
  957. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  958. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  959. if (std::strcmp(customData.key, key) == 0)
  960. {
  961. if (customData.value != nullptr)
  962. delete[] customData.value;
  963. customData.value = carla_strdup(value);
  964. return;
  965. }
  966. }
  967. // Otherwise store it
  968. CustomData customData;
  969. customData.type = carla_strdup(type);
  970. customData.key = carla_strdup(key);
  971. customData.value = carla_strdup(value);
  972. pData->custom.append(customData);
  973. }
  974. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  975. {
  976. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  977. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  978. CARLA_SAFE_ASSERT(false); // this should never happen
  979. }
  980. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  981. {
  982. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  983. pData->prog.current = index;
  984. #ifdef BUILD_BRIDGE
  985. const bool reallySendOsc(false);
  986. #else
  987. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  988. if (reallySendOsc)
  989. pData->engine->oscSend_control_set_current_program(pData->id, index);
  990. #endif
  991. if (sendCallback)
  992. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  993. // Change default parameter values
  994. if (index >= 0)
  995. {
  996. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  997. uiProgramChange(static_cast<uint32_t>(index));
  998. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  999. return;
  1000. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1001. }
  1002. #ifdef BUILD_BRIDGE
  1003. // unused
  1004. return; (void)sendGui; (void)sendOsc;
  1005. #endif
  1006. }
  1007. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1008. {
  1009. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1010. pData->midiprog.current = index;
  1011. #ifdef BUILD_BRIDGE
  1012. const bool reallySendOsc(false);
  1013. #else
  1014. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1015. if (reallySendOsc)
  1016. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1017. #endif
  1018. if (sendCallback)
  1019. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1020. if (index >= 0)
  1021. {
  1022. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1023. uiMidiProgramChange(static_cast<uint32_t>(index));
  1024. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1025. return;
  1026. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1027. }
  1028. #ifdef BUILD_BRIDGE
  1029. // unused
  1030. return; (void)sendGui; (void)sendOsc;
  1031. #endif
  1032. }
  1033. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1034. {
  1035. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1036. {
  1037. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1038. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1039. }
  1040. }
  1041. // -------------------------------------------------------------------
  1042. // Set ui stuff
  1043. void CarlaPlugin::idle()
  1044. {
  1045. if (! pData->enabled)
  1046. return;
  1047. if (pData->hints & PLUGIN_NEEDS_SINGLE_THREAD)
  1048. {
  1049. // Process postponed events
  1050. postRtEventsRun();
  1051. // Update parameter outputs
  1052. for (uint32_t i=0; i < pData->param.count; ++i)
  1053. {
  1054. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1055. uiParameterChange(i, getParameterValue(i));
  1056. }
  1057. }
  1058. if (pData->transientTryCounter == 0)
  1059. return;
  1060. if (++pData->transientTryCounter % 10 != 0)
  1061. return;
  1062. if (pData->transientTryCounter >= 200)
  1063. return;
  1064. carla_stdout("Trying to get window...");
  1065. CarlaString uiTitle(pData->name);
  1066. uiTitle += " (GUI)";
  1067. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle, pData->engine->getOptions().frontendWinId, true))
  1068. pData->transientTryCounter = 0;
  1069. }
  1070. void CarlaPlugin::showCustomUI(const bool)
  1071. {
  1072. CARLA_SAFE_ASSERT(false);
  1073. }
  1074. // -------------------------------------------------------------------
  1075. // Plugin state
  1076. void CarlaPlugin::reloadPrograms(const bool)
  1077. {
  1078. }
  1079. // -------------------------------------------------------------------
  1080. // Plugin processing
  1081. void CarlaPlugin::activate() noexcept
  1082. {
  1083. CARLA_SAFE_ASSERT(! pData->active);
  1084. }
  1085. void CarlaPlugin::deactivate() noexcept
  1086. {
  1087. CARLA_SAFE_ASSERT(pData->active);
  1088. }
  1089. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1090. {
  1091. }
  1092. void CarlaPlugin::sampleRateChanged(const double)
  1093. {
  1094. }
  1095. void CarlaPlugin::offlineModeChanged(const bool)
  1096. {
  1097. }
  1098. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1099. {
  1100. if (forcedOffline)
  1101. {
  1102. pData->masterMutex.lock();
  1103. return true;
  1104. }
  1105. return pData->masterMutex.tryLock();
  1106. }
  1107. void CarlaPlugin::unlock() noexcept
  1108. {
  1109. pData->masterMutex.unlock();
  1110. }
  1111. // -------------------------------------------------------------------
  1112. // Plugin buffers
  1113. void CarlaPlugin::initBuffers() const noexcept
  1114. {
  1115. pData->audioIn.initBuffers();
  1116. pData->audioOut.initBuffers();
  1117. pData->cvIn.initBuffers();
  1118. pData->cvOut.initBuffers();
  1119. pData->event.initBuffers();
  1120. }
  1121. void CarlaPlugin::clearBuffers() noexcept
  1122. {
  1123. pData->clearBuffers();
  1124. }
  1125. // -------------------------------------------------------------------
  1126. // OSC stuff
  1127. #ifndef BUILD_BRIDGE
  1128. void CarlaPlugin::registerToOscClient() noexcept
  1129. {
  1130. if (! pData->engine->isOscControlRegistered())
  1131. return;
  1132. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1133. // Base data
  1134. {
  1135. char bufName[STR_MAX+1], bufLabel[STR_MAX+1], bufMaker[STR_MAX+1], bufCopyright[STR_MAX+1];
  1136. carla_zeroChar(bufName, STR_MAX);
  1137. carla_zeroChar(bufLabel, STR_MAX);
  1138. carla_zeroChar(bufMaker, STR_MAX);
  1139. carla_zeroChar(bufCopyright, STR_MAX);
  1140. getRealName(bufName);
  1141. getLabel(bufLabel);
  1142. getMaker(bufMaker);
  1143. getCopyright(bufCopyright);
  1144. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1145. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1146. }
  1147. // Base count
  1148. {
  1149. uint32_t paramIns, paramOuts;
  1150. getParameterCountInfo(paramIns, paramOuts);
  1151. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1152. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1153. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1154. }
  1155. // Plugin Parameters
  1156. if (const uint32_t count = pData->param.count)
  1157. {
  1158. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1159. for (uint32_t i=0, maxParams=pData->engine->getOptions().maxParameters; i<count && i<maxParams; ++i)
  1160. {
  1161. carla_zeroChar(bufName, STR_MAX);
  1162. carla_zeroChar(bufUnit, STR_MAX);
  1163. getParameterName(i, bufName);
  1164. getParameterUnit(i, bufUnit);
  1165. const ParameterData& paramData(pData->param.data[i]);
  1166. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1167. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1168. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1169. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1170. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1171. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1172. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1173. }
  1174. }
  1175. // Programs
  1176. if (const uint32_t count = pData->prog.count)
  1177. {
  1178. pData->engine->oscSend_control_set_program_count(pData->id, count);
  1179. for (uint32_t i=0; i < count; ++i)
  1180. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1181. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1182. }
  1183. // MIDI Programs
  1184. if (const uint32_t count = pData->midiprog.count)
  1185. {
  1186. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1187. for (uint32_t i=0; i < count; ++i)
  1188. {
  1189. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1190. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1191. }
  1192. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1193. }
  1194. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1195. // Internal Parameters
  1196. {
  1197. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1198. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1199. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1200. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1201. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1202. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1203. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1204. }
  1205. }
  1206. #endif // BUILD_BRIDGE
  1207. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1208. {
  1209. // do nothing
  1210. }
  1211. // -------------------------------------------------------------------
  1212. // MIDI events
  1213. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1214. {
  1215. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1216. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1217. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1218. if (! pData->active)
  1219. return;
  1220. ExternalMidiNote extNote;
  1221. extNote.channel = static_cast<int8_t>(channel);
  1222. extNote.note = note;
  1223. extNote.velo = velo;
  1224. pData->extNotes.appendNonRT(extNote);
  1225. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1226. {
  1227. if (velo > 0)
  1228. uiNoteOn(channel, note, velo);
  1229. else
  1230. uiNoteOff(channel, note);
  1231. }
  1232. #ifndef BUILD_BRIDGE
  1233. if (sendOsc && pData->engine->isOscControlRegistered())
  1234. {
  1235. if (velo > 0)
  1236. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1237. else
  1238. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1239. }
  1240. #endif
  1241. if (sendCallback)
  1242. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1243. #ifdef BUILD_BRIDGE
  1244. // unused
  1245. return; (void)sendOsc;
  1246. #endif
  1247. }
  1248. #ifndef BUILD_BRIDGE
  1249. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1250. {
  1251. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1252. return;
  1253. PluginPostRtEvent postEvent;
  1254. postEvent.type = kPluginPostRtEventNoteOff;
  1255. postEvent.value1 = pData->ctrlChannel;
  1256. postEvent.value2 = 0;
  1257. postEvent.value3 = 0.0f;
  1258. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1259. {
  1260. postEvent.value2 = i;
  1261. pData->postRtEvents.appendRT(postEvent);
  1262. }
  1263. }
  1264. #endif
  1265. // -------------------------------------------------------------------
  1266. // Post-poned events
  1267. void CarlaPlugin::postRtEventsRun()
  1268. {
  1269. const CarlaMutexLocker sl(pData->postRtEvents.mutex);
  1270. #ifndef BUILD_BRIDGE
  1271. const bool sendOsc(pData->engine->isOscControlRegistered());
  1272. #endif
  1273. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.data.begin(); it.valid(); it.next())
  1274. {
  1275. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1276. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1277. switch (event.type)
  1278. {
  1279. case kPluginPostRtEventNull: {
  1280. } break;
  1281. case kPluginPostRtEventDebug: {
  1282. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1283. } break;
  1284. case kPluginPostRtEventParameterChange: {
  1285. // Update UI
  1286. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1287. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1288. if (event.value2 != 1)
  1289. {
  1290. #ifndef BUILD_BRIDGE
  1291. // Update OSC control client
  1292. if (sendOsc)
  1293. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1294. #endif
  1295. // Update Host
  1296. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1297. }
  1298. } break;
  1299. case kPluginPostRtEventProgramChange: {
  1300. // Update UI
  1301. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1302. uiProgramChange(static_cast<uint32_t>(event.value1));
  1303. // Update param values
  1304. for (uint32_t j=0; j < pData->param.count; ++j)
  1305. {
  1306. const float paramDefault(pData->param.ranges[j].def);
  1307. const float paramValue(getParameterValue(j));
  1308. #ifndef BUILD_BRIDGE
  1309. if (sendOsc)
  1310. {
  1311. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1312. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1313. }
  1314. #endif
  1315. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1316. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1317. }
  1318. #ifndef BUILD_BRIDGE
  1319. // Update OSC control client
  1320. if (sendOsc)
  1321. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1322. #endif
  1323. // Update Host
  1324. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1325. } break;
  1326. case kPluginPostRtEventMidiProgramChange: {
  1327. // Update UI
  1328. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1329. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1330. // Update param values
  1331. for (uint32_t j=0; j < pData->param.count; ++j)
  1332. {
  1333. const float paramDefault(pData->param.ranges[j].def);
  1334. const float paramValue(getParameterValue(j));
  1335. #ifndef BUILD_BRIDGE
  1336. if (sendOsc)
  1337. {
  1338. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1339. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1340. }
  1341. #endif
  1342. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1343. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1344. }
  1345. #ifndef BUILD_BRIDGE
  1346. // Update OSC control client
  1347. if (sendOsc)
  1348. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1349. #endif
  1350. // Update Host
  1351. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1352. } break;
  1353. case kPluginPostRtEventNoteOn: {
  1354. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1355. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1356. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1357. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1358. const uint8_t note = static_cast<uint8_t>(event.value2);
  1359. const uint8_t velocity = uint8_t(event.value3);
  1360. // Update UI
  1361. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1362. uiNoteOn(channel, note, velocity);
  1363. #ifndef BUILD_BRIDGE
  1364. // Update OSC control client
  1365. if (sendOsc)
  1366. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1367. #endif
  1368. // Update Host
  1369. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1370. } break;
  1371. case kPluginPostRtEventNoteOff: {
  1372. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1373. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1374. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1375. const uint8_t note = static_cast<uint8_t>(event.value2);
  1376. // Update UI
  1377. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1378. uiNoteOff(channel, note);
  1379. #ifndef BUILD_BRIDGE
  1380. // Update OSC control client
  1381. if (sendOsc)
  1382. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1383. #endif
  1384. // Update Host
  1385. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1386. } break;
  1387. }
  1388. }
  1389. pData->postRtEvents.data.clear();
  1390. }
  1391. // -------------------------------------------------------------------
  1392. // Post-poned UI Stuff
  1393. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1394. {
  1395. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1396. return;
  1397. // unused
  1398. (void)value;
  1399. }
  1400. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1401. {
  1402. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1403. }
  1404. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1405. {
  1406. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1407. }
  1408. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1409. {
  1410. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1411. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1412. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1413. }
  1414. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1415. {
  1416. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1417. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1418. }
  1419. bool CarlaPlugin::canRunInRack() const noexcept
  1420. {
  1421. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1422. }
  1423. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1424. {
  1425. return pData->engine;
  1426. }
  1427. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1428. {
  1429. return pData->client;
  1430. }
  1431. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1432. {
  1433. return pData->audioIn.ports[index].port;
  1434. }
  1435. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1436. {
  1437. return pData->audioOut.ports[index].port;
  1438. }
  1439. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  1440. {
  1441. return pData->cvIn.ports[index].port;
  1442. }
  1443. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  1444. {
  1445. return pData->cvOut.ports[index].port;
  1446. }
  1447. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  1448. {
  1449. return pData->event.portIn;
  1450. }
  1451. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  1452. {
  1453. return pData->event.portOut;
  1454. }
  1455. void* CarlaPlugin::getNativeHandle() const noexcept
  1456. {
  1457. return nullptr;
  1458. }
  1459. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  1460. {
  1461. return nullptr;
  1462. }
  1463. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  1464. {
  1465. return 0;
  1466. }
  1467. // -------------------------------------------------------------------
  1468. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  1469. {
  1470. return pData->nodeId;
  1471. }
  1472. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  1473. {
  1474. pData->nodeId = nodeId;
  1475. }
  1476. // -------------------------------------------------------------------
  1477. // Scoped Disabler
  1478. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1479. : fPlugin(plugin)
  1480. {
  1481. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1482. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1483. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1484. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1485. plugin->pData->masterMutex.lock();
  1486. if (plugin->pData->enabled)
  1487. plugin->pData->enabled = false;
  1488. if (plugin->pData->client->isActive())
  1489. plugin->pData->client->deactivate();
  1490. }
  1491. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1492. {
  1493. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1494. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1495. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1496. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1497. fPlugin->pData->enabled = true;
  1498. fPlugin->pData->client->activate();
  1499. fPlugin->pData->masterMutex.unlock();
  1500. }
  1501. // -------------------------------------------------------------------
  1502. // Scoped Process Locker
  1503. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1504. : fPlugin(plugin),
  1505. fBlock(block)
  1506. {
  1507. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1508. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1509. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1510. if (! fBlock)
  1511. return;
  1512. plugin->pData->singleMutex.lock();
  1513. }
  1514. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1515. {
  1516. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1517. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1518. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1519. if (! fBlock)
  1520. return;
  1521. #ifndef BUILD_BRIDGE
  1522. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1523. fPlugin->pData->needsReset = true;
  1524. #endif
  1525. fPlugin->pData->singleMutex.unlock();
  1526. }
  1527. // -------------------------------------------------------------------
  1528. CARLA_BACKEND_END_NAMESPACE