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.

1938 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(const bool callPrepareForSave)
  388. {
  389. if (callPrepareForSave)
  390. prepareForSave();
  391. pData->stateSave.clear();
  392. const PluginType pluginType(getType());
  393. char strBuf[STR_MAX+1];
  394. // ---------------------------------------------------------------
  395. // Basic info
  396. getLabel(strBuf);
  397. pData->stateSave.type = carla_strdup(getPluginTypeAsString(getType()));
  398. pData->stateSave.name = carla_strdup(pData->name);
  399. pData->stateSave.label = carla_strdup(strBuf);
  400. pData->stateSave.uniqueId = getUniqueId();
  401. #ifndef BUILD_BRIDGE
  402. pData->stateSave.options = pData->options;
  403. #endif
  404. if (pData->filename != nullptr)
  405. pData->stateSave.binary = carla_strdup(pData->filename);
  406. #ifndef BUILD_BRIDGE
  407. // ---------------------------------------------------------------
  408. // Internals
  409. pData->stateSave.active = pData->active;
  410. pData->stateSave.dryWet = pData->postProc.dryWet;
  411. pData->stateSave.volume = pData->postProc.volume;
  412. pData->stateSave.balanceLeft = pData->postProc.balanceLeft;
  413. pData->stateSave.balanceRight = pData->postProc.balanceRight;
  414. pData->stateSave.panning = pData->postProc.panning;
  415. pData->stateSave.ctrlChannel = pData->ctrlChannel;
  416. #endif
  417. // ---------------------------------------------------------------
  418. // Chunk
  419. if (pData->options & PLUGIN_OPTION_USE_CHUNKS)
  420. {
  421. void* data = nullptr;
  422. const std::size_t dataSize(getChunkData(&data));
  423. if (data != nullptr && dataSize > 0)
  424. {
  425. pData->stateSave.chunk = CarlaString::asBase64(data, dataSize).dup();
  426. // Don't save anything else if using chunks
  427. return pData->stateSave;
  428. }
  429. }
  430. // ---------------------------------------------------------------
  431. // Current Program
  432. if (pData->prog.current >= 0 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_GIG)
  433. {
  434. pData->stateSave.currentProgramIndex = pData->prog.current;
  435. pData->stateSave.currentProgramName = carla_strdup(pData->prog.names[pData->prog.current]);
  436. }
  437. // ---------------------------------------------------------------
  438. // Current MIDI Program
  439. if (pData->midiprog.current >= 0 && pluginType != PLUGIN_LV2 && pluginType != PLUGIN_SF2)
  440. {
  441. const MidiProgramData& mpData(pData->midiprog.getCurrent());
  442. pData->stateSave.currentMidiBank = static_cast<int32_t>(mpData.bank);
  443. pData->stateSave.currentMidiProgram = static_cast<int32_t>(mpData.program);
  444. }
  445. // ---------------------------------------------------------------
  446. // Parameters
  447. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  448. for (uint32_t i=0; i < pData->param.count; ++i)
  449. {
  450. const ParameterData& paramData(pData->param.data[i]);
  451. if ((paramData.hints & PARAMETER_IS_ENABLED) == 0)
  452. continue;
  453. CarlaStateSave::Parameter* const stateParameter(new CarlaStateSave::Parameter());
  454. stateParameter->isInput = (paramData.type == PARAMETER_INPUT);
  455. stateParameter->index = paramData.index;
  456. #ifndef BUILD_BRIDGE
  457. stateParameter->midiCC = paramData.midiCC;
  458. stateParameter->midiChannel = paramData.midiChannel;
  459. #endif
  460. getParameterName(i, strBuf);
  461. stateParameter->name = carla_strdup(strBuf);
  462. getParameterSymbol(i, strBuf);
  463. stateParameter->symbol = carla_strdup(strBuf);;
  464. stateParameter->value = getParameterValue(i);
  465. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  466. stateParameter->value /= sampleRate;
  467. pData->stateSave.parameters.append(stateParameter);
  468. }
  469. // ---------------------------------------------------------------
  470. // Custom Data
  471. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  472. {
  473. const CustomData& cData(it.getValue(kCustomDataFallback));
  474. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  475. CarlaStateSave::CustomData* stateCustomData(new CarlaStateSave::CustomData());
  476. stateCustomData->type = carla_strdup(cData.type);
  477. stateCustomData->key = carla_strdup(cData.key);
  478. stateCustomData->value = carla_strdup(cData.value);
  479. pData->stateSave.customData.append(stateCustomData);
  480. }
  481. return pData->stateSave;
  482. }
  483. void CarlaPlugin::loadStateSave(const CarlaStateSave& stateSave)
  484. {
  485. char strBuf[STR_MAX+1];
  486. const bool usesMultiProgs(pData->extraHints & PLUGIN_EXTRA_HINT_USES_MULTI_PROGS);
  487. // ---------------------------------------------------------------
  488. // Part 1 - PRE-set custom data (only that which reload programs)
  489. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin(); it.valid(); it.next())
  490. {
  491. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  492. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  493. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  494. const char* const key(stateCustomData->key);
  495. bool wantData = false;
  496. if (getType() == PLUGIN_DSSI && (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0))
  497. wantData = true;
  498. else if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  499. wantData = true;
  500. if (wantData)
  501. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  502. }
  503. // ---------------------------------------------------------------
  504. // Part 2 - set program
  505. if (stateSave.currentProgramIndex >= 0 && stateSave.currentProgramName != nullptr)
  506. {
  507. int32_t programId = -1;
  508. // index < count
  509. if (stateSave.currentProgramIndex < static_cast<int32_t>(pData->prog.count))
  510. {
  511. programId = stateSave.currentProgramIndex;
  512. }
  513. // index not valid, try to find by name
  514. else
  515. {
  516. for (uint32_t i=0; i < pData->prog.count; ++i)
  517. {
  518. strBuf[0] = '\0';
  519. getProgramName(i, strBuf);
  520. if (strBuf[0] != '\0' && std::strcmp(stateSave.currentProgramName, strBuf) == 0)
  521. {
  522. programId = static_cast<int32_t>(i);
  523. break;
  524. }
  525. }
  526. }
  527. // set program now, if valid
  528. if (programId >= 0)
  529. setProgram(programId, true, true, true);
  530. }
  531. // ---------------------------------------------------------------
  532. // Part 3 - set midi program
  533. if (stateSave.currentMidiBank >= 0 && stateSave.currentMidiProgram >= 0 && ! usesMultiProgs)
  534. setMidiProgramById(static_cast<uint32_t>(stateSave.currentMidiBank), static_cast<uint32_t>(stateSave.currentMidiProgram), true, true, true);
  535. // ---------------------------------------------------------------
  536. // Part 4a - get plugin parameter symbols
  537. LinkedList<ParamSymbol*> paramSymbols;
  538. if (getType() == PLUGIN_LADSPA || getType() == PLUGIN_LV2)
  539. {
  540. for (uint32_t i=0; i < pData->param.count; ++i)
  541. {
  542. strBuf[0] = '\0';
  543. getParameterSymbol(i, strBuf);
  544. if (strBuf[0] != '\0')
  545. {
  546. ParamSymbol* const paramSymbol(new ParamSymbol(i, strBuf));
  547. paramSymbols.append(paramSymbol);
  548. }
  549. }
  550. }
  551. // ---------------------------------------------------------------
  552. // Part 4b - set parameter values (carefully)
  553. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  554. for (CarlaStateSave::ParameterItenerator it = stateSave.parameters.begin(); it.valid(); it.next())
  555. {
  556. CarlaStateSave::Parameter* const stateParameter(it.getValue(nullptr));
  557. CARLA_SAFE_ASSERT_CONTINUE(stateParameter != nullptr);
  558. int32_t index = -1;
  559. if (getType() == PLUGIN_LADSPA)
  560. {
  561. // Try to set by symbol, otherwise use index
  562. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  563. {
  564. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin(); it2.valid(); it2.next())
  565. {
  566. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  567. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  568. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  569. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  570. {
  571. index = paramSymbol->index;
  572. break;
  573. }
  574. }
  575. if (index == -1)
  576. index = stateParameter->index;
  577. }
  578. else
  579. index = stateParameter->index;
  580. }
  581. else if (getType() == PLUGIN_LV2)
  582. {
  583. // Symbol only
  584. if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0')
  585. {
  586. for (LinkedList<ParamSymbol*>::Itenerator it2 = paramSymbols.begin(); it2.valid(); it2.next())
  587. {
  588. ParamSymbol* const paramSymbol(it2.getValue(nullptr));
  589. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol != nullptr);
  590. CARLA_SAFE_ASSERT_CONTINUE(paramSymbol->symbol != nullptr);
  591. if (std::strcmp(stateParameter->symbol, paramSymbol->symbol) == 0)
  592. {
  593. index = paramSymbol->index;
  594. break;
  595. }
  596. }
  597. if (index == -1)
  598. carla_stderr("Failed to find LV2 parameter symbol '%s')", stateParameter->symbol);
  599. }
  600. else
  601. carla_stderr("LV2 Plugin parameter '%s' has no symbol", stateParameter->name);
  602. }
  603. else
  604. {
  605. // Index only
  606. index = stateParameter->index;
  607. }
  608. // Now set parameter
  609. if (index >= 0 && index < static_cast<int32_t>(pData->param.count))
  610. {
  611. //CARLA_SAFE_ASSERT(stateParameter->isInput == (pData
  612. if (stateParameter->isInput)
  613. {
  614. if (pData->param.data[index].hints & PARAMETER_USES_SAMPLERATE)
  615. stateParameter->value *= sampleRate;
  616. setParameterValue(static_cast<uint32_t>(index), stateParameter->value, true, true, true);
  617. }
  618. #ifndef BUILD_BRIDGE
  619. setParameterMidiCC(static_cast<uint32_t>(index), stateParameter->midiCC, true, true);
  620. setParameterMidiChannel(static_cast<uint32_t>(index), stateParameter->midiChannel, true, true);
  621. #endif
  622. }
  623. else
  624. carla_stderr("Could not set parameter data for '%s'", stateParameter->name);
  625. }
  626. // ---------------------------------------------------------------
  627. // Part 4c - clear
  628. for (LinkedList<ParamSymbol*>::Itenerator it = paramSymbols.begin(); it.valid(); it.next())
  629. {
  630. ParamSymbol* const paramSymbol(it.getValue(nullptr));
  631. delete paramSymbol;
  632. }
  633. paramSymbols.clear();
  634. // ---------------------------------------------------------------
  635. // Part 5 - set custom data
  636. for (CarlaStateSave::CustomDataItenerator it = stateSave.customData.begin(); it.valid(); it.next())
  637. {
  638. const CarlaStateSave::CustomData* const stateCustomData(it.getValue(nullptr));
  639. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr);
  640. CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid());
  641. const char* const key(stateCustomData->key);
  642. if (getType() == PLUGIN_DSSI && (std::strcmp(key, "reloadprograms") == 0 || std::strcmp(key, "load") == 0 || std::strncmp(key, "patches", 7) == 0))
  643. continue;
  644. if (usesMultiProgs && std::strcmp(key, "midiPrograms") == 0)
  645. continue;
  646. setCustomData(stateCustomData->type, stateCustomData->key, stateCustomData->value, true);
  647. }
  648. // ---------------------------------------------------------------
  649. // Part 5x - set lv2 state
  650. if (getType() == PLUGIN_LV2 && pData->custom.count() > 0)
  651. setCustomData(CUSTOM_DATA_TYPE_STRING, "CarlaLoadLv2StateNow", "true", true);
  652. // ---------------------------------------------------------------
  653. // Part 6 - set chunk
  654. if (stateSave.chunk != nullptr && (pData->options & PLUGIN_OPTION_USE_CHUNKS) != 0)
  655. {
  656. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(stateSave.chunk));
  657. setChunkData(chunk.data(), chunk.size());
  658. }
  659. #ifndef BUILD_BRIDGE
  660. // ---------------------------------------------------------------
  661. // Part 6 - set internal stuff
  662. const uint availOptions(getOptionsAvailable());
  663. for (uint i=0; i<10; ++i) // FIXME - get this value somehow...
  664. {
  665. const uint option(1u << i);
  666. if (availOptions & option)
  667. setOption(option, (stateSave.options & option) != 0, true);
  668. }
  669. setDryWet(stateSave.dryWet, true, true);
  670. setVolume(stateSave.volume, true, true);
  671. setBalanceLeft(stateSave.balanceLeft, true, true);
  672. setBalanceRight(stateSave.balanceRight, true, true);
  673. setPanning(stateSave.panning, true, true);
  674. setCtrlChannel(stateSave.ctrlChannel, true, true);
  675. setActive(stateSave.active, true, true);
  676. #endif
  677. }
  678. bool CarlaPlugin::saveStateToFile(const char* const filename)
  679. {
  680. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  681. carla_debug("CarlaPlugin::saveStateToFile(\"%s\")", filename);
  682. MemoryOutputStream out;
  683. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  684. out << "<!DOCTYPE CARLA-PRESET>\n";
  685. out << "<CARLA-PRESET VERSION='2.0'>\n";
  686. out << getStateSave().toString();
  687. out << "</CARLA-PRESET>\n";
  688. const String jfilename = String(CharPointer_UTF8(filename));
  689. File file(jfilename);
  690. if (file.replaceWithData(out.getData(), out.getDataSize()))
  691. return true;
  692. pData->engine->setLastError("Failed to write file");
  693. return false;
  694. }
  695. bool CarlaPlugin::loadStateFromFile(const char* const filename)
  696. {
  697. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', false);
  698. carla_debug("CarlaPlugin::loadStateFromFile(\"%s\")", filename);
  699. const String jfilename = String(CharPointer_UTF8(filename));
  700. File file(jfilename);
  701. CARLA_SAFE_ASSERT_RETURN(file.existsAsFile(), false);
  702. XmlDocument xml(file);
  703. ScopedPointer<XmlElement> xmlElement(xml.getDocumentElement(true));
  704. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  705. CARLA_SAFE_ASSERT_RETURN(xmlElement->getTagName().equalsIgnoreCase("carla-preset"), false);
  706. // completely load file
  707. xmlElement = xml.getDocumentElement(false);
  708. CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false);
  709. if (pData->stateSave.fillFromXmlElement(xmlElement->getFirstChildElement()))
  710. loadStateSave(pData->stateSave);
  711. return true;
  712. }
  713. // -------------------------------------------------------------------
  714. // Set data (internal stuff)
  715. void CarlaPlugin::setId(const uint newId) noexcept
  716. {
  717. pData->id = newId;
  718. }
  719. void CarlaPlugin::setName(const char* const newName)
  720. {
  721. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  722. if (pData->name != nullptr)
  723. delete[] pData->name;
  724. pData->name = carla_strdup(newName);
  725. }
  726. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  727. {
  728. CARLA_SAFE_ASSERT_RETURN(getOptionsAvailable() & option,);
  729. if (yesNo)
  730. pData->options |= option;
  731. else
  732. pData->options &= ~option;
  733. #ifndef BUILD_BRIDGE
  734. if (sendCallback)
  735. pData->engine->callback(ENGINE_CALLBACK_OPTION_CHANGED, pData->id, static_cast<int>(option), yesNo ? 1 : 0, 0.0f, nullptr);
  736. #else
  737. // unused
  738. return; (void)sendCallback;
  739. #endif
  740. }
  741. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  742. {
  743. if (pData->enabled == yesNo)
  744. return;
  745. pData->enabled = yesNo;
  746. pData->masterMutex.lock();
  747. pData->masterMutex.unlock();
  748. }
  749. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  750. {
  751. #ifndef BUILD_BRIDGE
  752. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  753. #endif
  754. if (pData->active == active)
  755. return;
  756. {
  757. const ScopedSingleProcessLocker spl(this, true);
  758. if (active)
  759. activate();
  760. else
  761. deactivate();
  762. }
  763. pData->active = active;
  764. #ifndef BUILD_BRIDGE
  765. const float value(active ? 1.0f : 0.0f);
  766. if (sendOsc && pData->engine->isOscControlRegistered())
  767. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  768. if (sendCallback)
  769. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  770. #else
  771. // unused
  772. return; (void)sendOsc; (void)sendCallback;
  773. #endif
  774. }
  775. #ifndef BUILD_BRIDGE
  776. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  777. {
  778. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  779. const float fixedValue(carla_fixValue<float>(0.0f, 1.0f, value));
  780. if (carla_compareFloats(pData->postProc.dryWet, fixedValue))
  781. return;
  782. pData->postProc.dryWet = fixedValue;
  783. if (sendOsc && pData->engine->isOscControlRegistered())
  784. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  785. if (sendCallback)
  786. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  787. }
  788. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  789. {
  790. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  791. const float fixedValue(carla_fixValue<float>(0.0f, 1.27f, value));
  792. if (carla_compareFloats(pData->postProc.volume, fixedValue))
  793. return;
  794. pData->postProc.volume = fixedValue;
  795. if (sendOsc && pData->engine->isOscControlRegistered())
  796. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  797. if (sendCallback)
  798. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  799. }
  800. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  801. {
  802. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  803. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  804. if (carla_compareFloats(pData->postProc.balanceLeft, fixedValue))
  805. return;
  806. pData->postProc.balanceLeft = fixedValue;
  807. if (sendOsc && pData->engine->isOscControlRegistered())
  808. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  809. if (sendCallback)
  810. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  811. }
  812. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  813. {
  814. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  815. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  816. if (carla_compareFloats(pData->postProc.balanceRight, fixedValue))
  817. return;
  818. pData->postProc.balanceRight = fixedValue;
  819. if (sendOsc && pData->engine->isOscControlRegistered())
  820. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  821. if (sendCallback)
  822. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  823. }
  824. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  825. {
  826. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  827. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  828. if (carla_compareFloats(pData->postProc.panning, fixedValue))
  829. return;
  830. pData->postProc.panning = fixedValue;
  831. if (sendOsc && pData->engine->isOscControlRegistered())
  832. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  833. if (sendCallback)
  834. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  835. }
  836. #endif
  837. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  838. {
  839. #ifndef BUILD_BRIDGE
  840. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  841. #endif
  842. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  843. if (pData->ctrlChannel == channel)
  844. return;
  845. pData->ctrlChannel = channel;
  846. #ifndef BUILD_BRIDGE
  847. const float channelf(channel);
  848. if (sendOsc && pData->engine->isOscControlRegistered())
  849. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, channelf);
  850. if (sendCallback)
  851. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, channelf, nullptr);
  852. #else
  853. // unused
  854. return; (void)sendOsc; (void)sendCallback;
  855. #endif
  856. }
  857. // -------------------------------------------------------------------
  858. // Set data (plugin-specific stuff)
  859. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  860. {
  861. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  862. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  863. uiParameterChange(parameterId, value);
  864. #ifndef BUILD_BRIDGE
  865. if (sendOsc && pData->engine->isOscControlRegistered())
  866. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  867. #endif
  868. if (sendCallback)
  869. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  870. #ifdef BUILD_BRIDGE
  871. // unused
  872. return; (void)sendOsc;
  873. #endif
  874. }
  875. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  876. {
  877. #ifndef BUILD_BRIDGE
  878. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  879. switch (rindex)
  880. {
  881. case PARAMETER_ACTIVE:
  882. return setActive((value > 0.0f), sendOsc, sendCallback);
  883. case PARAMETER_CTRL_CHANNEL:
  884. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  885. case PARAMETER_DRYWET:
  886. return setDryWet(value, sendOsc, sendCallback);
  887. case PARAMETER_VOLUME:
  888. return setVolume(value, sendOsc, sendCallback);
  889. case PARAMETER_BALANCE_LEFT:
  890. return setBalanceLeft(value, sendOsc, sendCallback);
  891. case PARAMETER_BALANCE_RIGHT:
  892. return setBalanceRight(value, sendOsc, sendCallback);
  893. case PARAMETER_PANNING:
  894. return setPanning(value, sendOsc, sendCallback);
  895. }
  896. #endif
  897. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  898. for (uint32_t i=0; i < pData->param.count; ++i)
  899. {
  900. if (pData->param.data[i].rindex == rindex)
  901. {
  902. //if (! carla_compareFloats(getParameterValue(i), value))
  903. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  904. break;
  905. }
  906. }
  907. }
  908. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  909. {
  910. #ifndef BUILD_BRIDGE
  911. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  912. #endif
  913. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  914. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  915. pData->param.data[parameterId].midiChannel = channel;
  916. #ifndef BUILD_BRIDGE
  917. if (sendOsc && pData->engine->isOscControlRegistered())
  918. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  919. if (sendCallback)
  920. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  921. #else
  922. // unused
  923. return; (void)sendOsc; (void)sendCallback;
  924. #endif
  925. }
  926. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  927. {
  928. #ifndef BUILD_BRIDGE
  929. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  930. #endif
  931. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  932. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL,);
  933. pData->param.data[parameterId].midiCC = cc;
  934. #ifndef BUILD_BRIDGE
  935. if (sendOsc && pData->engine->isOscControlRegistered())
  936. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  937. if (sendCallback)
  938. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  939. #else
  940. // unused
  941. return; (void)sendOsc; (void)sendCallback;
  942. #endif
  943. }
  944. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  945. {
  946. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  947. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  948. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  949. // Ignore some keys
  950. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  951. {
  952. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  953. return;
  954. }
  955. // Check if we already have this key
  956. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  957. {
  958. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  959. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  960. if (std::strcmp(customData.key, key) == 0)
  961. {
  962. if (customData.value != nullptr)
  963. delete[] customData.value;
  964. customData.value = carla_strdup(value);
  965. return;
  966. }
  967. }
  968. // Otherwise store it
  969. CustomData customData;
  970. customData.type = carla_strdup(type);
  971. customData.key = carla_strdup(key);
  972. customData.value = carla_strdup(value);
  973. pData->custom.append(customData);
  974. }
  975. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  976. {
  977. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  978. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  979. CARLA_SAFE_ASSERT(false); // this should never happen
  980. }
  981. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  982. {
  983. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  984. pData->prog.current = index;
  985. #ifdef BUILD_BRIDGE
  986. const bool reallySendOsc(false);
  987. #else
  988. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  989. if (reallySendOsc)
  990. pData->engine->oscSend_control_set_current_program(pData->id, index);
  991. #endif
  992. if (sendCallback)
  993. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  994. // Change default parameter values
  995. if (index >= 0)
  996. {
  997. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  998. uiProgramChange(static_cast<uint32_t>(index));
  999. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1000. return;
  1001. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1002. }
  1003. #ifdef BUILD_BRIDGE
  1004. // unused
  1005. return; (void)sendGui; (void)sendOsc;
  1006. #endif
  1007. }
  1008. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1009. {
  1010. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1011. pData->midiprog.current = index;
  1012. #ifdef BUILD_BRIDGE
  1013. const bool reallySendOsc(false);
  1014. #else
  1015. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1016. if (reallySendOsc)
  1017. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1018. #endif
  1019. if (sendCallback)
  1020. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1021. if (index >= 0)
  1022. {
  1023. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1024. uiMidiProgramChange(static_cast<uint32_t>(index));
  1025. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1026. return;
  1027. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1028. }
  1029. #ifdef BUILD_BRIDGE
  1030. // unused
  1031. return; (void)sendGui; (void)sendOsc;
  1032. #endif
  1033. }
  1034. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1035. {
  1036. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1037. {
  1038. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1039. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1040. }
  1041. }
  1042. // -------------------------------------------------------------------
  1043. // Set ui stuff
  1044. void CarlaPlugin::idle()
  1045. {
  1046. if (! pData->enabled)
  1047. return;
  1048. if (pData->hints & PLUGIN_NEEDS_SINGLE_THREAD)
  1049. {
  1050. // Process postponed events
  1051. postRtEventsRun();
  1052. // Update parameter outputs
  1053. for (uint32_t i=0; i < pData->param.count; ++i)
  1054. {
  1055. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1056. uiParameterChange(i, getParameterValue(i));
  1057. }
  1058. }
  1059. if (pData->transientTryCounter == 0)
  1060. return;
  1061. if (++pData->transientTryCounter % 10 != 0)
  1062. return;
  1063. if (pData->transientTryCounter >= 200)
  1064. return;
  1065. carla_stdout("Trying to get window...");
  1066. CarlaString uiTitle(pData->name);
  1067. uiTitle += " (GUI)";
  1068. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle, pData->engine->getOptions().frontendWinId, true))
  1069. pData->transientTryCounter = 0;
  1070. }
  1071. void CarlaPlugin::showCustomUI(const bool)
  1072. {
  1073. CARLA_SAFE_ASSERT(false);
  1074. }
  1075. // -------------------------------------------------------------------
  1076. // Plugin state
  1077. void CarlaPlugin::reloadPrograms(const bool)
  1078. {
  1079. }
  1080. // -------------------------------------------------------------------
  1081. // Plugin processing
  1082. void CarlaPlugin::activate() noexcept
  1083. {
  1084. CARLA_SAFE_ASSERT(! pData->active);
  1085. }
  1086. void CarlaPlugin::deactivate() noexcept
  1087. {
  1088. CARLA_SAFE_ASSERT(pData->active);
  1089. }
  1090. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1091. {
  1092. }
  1093. void CarlaPlugin::sampleRateChanged(const double)
  1094. {
  1095. }
  1096. void CarlaPlugin::offlineModeChanged(const bool)
  1097. {
  1098. }
  1099. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1100. {
  1101. if (forcedOffline)
  1102. {
  1103. pData->masterMutex.lock();
  1104. return true;
  1105. }
  1106. return pData->masterMutex.tryLock();
  1107. }
  1108. void CarlaPlugin::unlock() noexcept
  1109. {
  1110. pData->masterMutex.unlock();
  1111. }
  1112. // -------------------------------------------------------------------
  1113. // Plugin buffers
  1114. void CarlaPlugin::initBuffers() const noexcept
  1115. {
  1116. pData->audioIn.initBuffers();
  1117. pData->audioOut.initBuffers();
  1118. pData->cvIn.initBuffers();
  1119. pData->cvOut.initBuffers();
  1120. pData->event.initBuffers();
  1121. }
  1122. void CarlaPlugin::clearBuffers() noexcept
  1123. {
  1124. pData->clearBuffers();
  1125. }
  1126. // -------------------------------------------------------------------
  1127. // OSC stuff
  1128. #ifndef BUILD_BRIDGE
  1129. void CarlaPlugin::registerToOscClient() noexcept
  1130. {
  1131. if (! pData->engine->isOscControlRegistered())
  1132. return;
  1133. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1134. // Base data
  1135. {
  1136. char bufName[STR_MAX+1], bufLabel[STR_MAX+1], bufMaker[STR_MAX+1], bufCopyright[STR_MAX+1];
  1137. carla_zeroChar(bufName, STR_MAX);
  1138. carla_zeroChar(bufLabel, STR_MAX);
  1139. carla_zeroChar(bufMaker, STR_MAX);
  1140. carla_zeroChar(bufCopyright, STR_MAX);
  1141. getRealName(bufName);
  1142. getLabel(bufLabel);
  1143. getMaker(bufMaker);
  1144. getCopyright(bufCopyright);
  1145. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1146. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1147. }
  1148. // Base count
  1149. {
  1150. uint32_t paramIns, paramOuts;
  1151. getParameterCountInfo(paramIns, paramOuts);
  1152. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1153. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1154. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1155. }
  1156. // Plugin Parameters
  1157. if (const uint32_t count = pData->param.count)
  1158. {
  1159. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1160. for (uint32_t i=0, maxParams=pData->engine->getOptions().maxParameters; i<count && i<maxParams; ++i)
  1161. {
  1162. carla_zeroChar(bufName, STR_MAX);
  1163. carla_zeroChar(bufUnit, STR_MAX);
  1164. getParameterName(i, bufName);
  1165. getParameterUnit(i, bufUnit);
  1166. const ParameterData& paramData(pData->param.data[i]);
  1167. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1168. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1169. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1170. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1171. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1172. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1173. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1174. }
  1175. }
  1176. // Programs
  1177. if (const uint32_t count = pData->prog.count)
  1178. {
  1179. pData->engine->oscSend_control_set_program_count(pData->id, count);
  1180. for (uint32_t i=0; i < count; ++i)
  1181. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1182. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1183. }
  1184. // MIDI Programs
  1185. if (const uint32_t count = pData->midiprog.count)
  1186. {
  1187. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1188. for (uint32_t i=0; i < count; ++i)
  1189. {
  1190. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1191. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1192. }
  1193. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1194. }
  1195. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1196. // Internal Parameters
  1197. {
  1198. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1199. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1200. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1201. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1202. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1203. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1204. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1205. }
  1206. }
  1207. #endif // BUILD_BRIDGE
  1208. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1209. {
  1210. // do nothing
  1211. }
  1212. // -------------------------------------------------------------------
  1213. // MIDI events
  1214. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1215. {
  1216. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1217. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1218. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1219. if (! pData->active)
  1220. return;
  1221. ExternalMidiNote extNote;
  1222. extNote.channel = static_cast<int8_t>(channel);
  1223. extNote.note = note;
  1224. extNote.velo = velo;
  1225. pData->extNotes.appendNonRT(extNote);
  1226. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1227. {
  1228. if (velo > 0)
  1229. uiNoteOn(channel, note, velo);
  1230. else
  1231. uiNoteOff(channel, note);
  1232. }
  1233. #ifndef BUILD_BRIDGE
  1234. if (sendOsc && pData->engine->isOscControlRegistered())
  1235. {
  1236. if (velo > 0)
  1237. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1238. else
  1239. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1240. }
  1241. #endif
  1242. if (sendCallback)
  1243. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1244. #ifdef BUILD_BRIDGE
  1245. // unused
  1246. return; (void)sendOsc;
  1247. #endif
  1248. }
  1249. #ifndef BUILD_BRIDGE
  1250. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1251. {
  1252. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1253. return;
  1254. PluginPostRtEvent postEvent;
  1255. postEvent.type = kPluginPostRtEventNoteOff;
  1256. postEvent.value1 = pData->ctrlChannel;
  1257. postEvent.value2 = 0;
  1258. postEvent.value3 = 0.0f;
  1259. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1260. {
  1261. postEvent.value2 = i;
  1262. pData->postRtEvents.appendRT(postEvent);
  1263. }
  1264. }
  1265. #endif
  1266. // -------------------------------------------------------------------
  1267. // Post-poned events
  1268. void CarlaPlugin::postRtEventsRun()
  1269. {
  1270. const CarlaMutexLocker sl(pData->postRtEvents.mutex);
  1271. #ifndef BUILD_BRIDGE
  1272. const bool sendOsc(pData->engine->isOscControlRegistered());
  1273. #endif
  1274. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.data.begin(); it.valid(); it.next())
  1275. {
  1276. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1277. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1278. switch (event.type)
  1279. {
  1280. case kPluginPostRtEventNull: {
  1281. } break;
  1282. case kPluginPostRtEventDebug: {
  1283. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1284. } break;
  1285. case kPluginPostRtEventParameterChange: {
  1286. // Update UI
  1287. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1288. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1289. if (event.value2 != 1)
  1290. {
  1291. #ifndef BUILD_BRIDGE
  1292. // Update OSC control client
  1293. if (sendOsc)
  1294. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1295. #endif
  1296. // Update Host
  1297. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1298. }
  1299. } break;
  1300. case kPluginPostRtEventProgramChange: {
  1301. // Update UI
  1302. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1303. uiProgramChange(static_cast<uint32_t>(event.value1));
  1304. // Update param values
  1305. for (uint32_t j=0; j < pData->param.count; ++j)
  1306. {
  1307. const float paramDefault(pData->param.ranges[j].def);
  1308. const float paramValue(getParameterValue(j));
  1309. #ifndef BUILD_BRIDGE
  1310. if (sendOsc)
  1311. {
  1312. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1313. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1314. }
  1315. #endif
  1316. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1317. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1318. }
  1319. #ifndef BUILD_BRIDGE
  1320. // Update OSC control client
  1321. if (sendOsc)
  1322. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1323. #endif
  1324. // Update Host
  1325. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1326. } break;
  1327. case kPluginPostRtEventMidiProgramChange: {
  1328. // Update UI
  1329. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1330. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1331. // Update param values
  1332. for (uint32_t j=0; j < pData->param.count; ++j)
  1333. {
  1334. const float paramDefault(pData->param.ranges[j].def);
  1335. const float paramValue(getParameterValue(j));
  1336. #ifndef BUILD_BRIDGE
  1337. if (sendOsc)
  1338. {
  1339. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1340. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1341. }
  1342. #endif
  1343. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1344. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1345. }
  1346. #ifndef BUILD_BRIDGE
  1347. // Update OSC control client
  1348. if (sendOsc)
  1349. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1350. #endif
  1351. // Update Host
  1352. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1353. } break;
  1354. case kPluginPostRtEventNoteOn: {
  1355. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1356. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1357. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1358. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1359. const uint8_t note = static_cast<uint8_t>(event.value2);
  1360. const uint8_t velocity = uint8_t(event.value3);
  1361. // Update UI
  1362. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1363. uiNoteOn(channel, note, velocity);
  1364. #ifndef BUILD_BRIDGE
  1365. // Update OSC control client
  1366. if (sendOsc)
  1367. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1368. #endif
  1369. // Update Host
  1370. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1371. } break;
  1372. case kPluginPostRtEventNoteOff: {
  1373. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1374. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1375. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1376. const uint8_t note = static_cast<uint8_t>(event.value2);
  1377. // Update UI
  1378. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1379. uiNoteOff(channel, note);
  1380. #ifndef BUILD_BRIDGE
  1381. // Update OSC control client
  1382. if (sendOsc)
  1383. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1384. #endif
  1385. // Update Host
  1386. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1387. } break;
  1388. }
  1389. }
  1390. pData->postRtEvents.data.clear();
  1391. }
  1392. // -------------------------------------------------------------------
  1393. // Post-poned UI Stuff
  1394. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1395. {
  1396. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1397. return;
  1398. // unused
  1399. (void)value;
  1400. }
  1401. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1402. {
  1403. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1404. }
  1405. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1406. {
  1407. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1408. }
  1409. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1410. {
  1411. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1412. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1413. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1414. }
  1415. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1416. {
  1417. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1418. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1419. }
  1420. bool CarlaPlugin::canRunInRack() const noexcept
  1421. {
  1422. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1423. }
  1424. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1425. {
  1426. return pData->engine;
  1427. }
  1428. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1429. {
  1430. return pData->client;
  1431. }
  1432. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1433. {
  1434. return pData->audioIn.ports[index].port;
  1435. }
  1436. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1437. {
  1438. return pData->audioOut.ports[index].port;
  1439. }
  1440. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  1441. {
  1442. return pData->cvIn.ports[index].port;
  1443. }
  1444. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  1445. {
  1446. return pData->cvOut.ports[index].port;
  1447. }
  1448. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  1449. {
  1450. return pData->event.portIn;
  1451. }
  1452. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  1453. {
  1454. return pData->event.portOut;
  1455. }
  1456. void* CarlaPlugin::getNativeHandle() const noexcept
  1457. {
  1458. return nullptr;
  1459. }
  1460. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  1461. {
  1462. return nullptr;
  1463. }
  1464. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  1465. {
  1466. return 0;
  1467. }
  1468. // -------------------------------------------------------------------
  1469. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  1470. {
  1471. return pData->nodeId;
  1472. }
  1473. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  1474. {
  1475. pData->nodeId = nodeId;
  1476. }
  1477. // -------------------------------------------------------------------
  1478. // Scoped Disabler
  1479. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1480. : fPlugin(plugin)
  1481. {
  1482. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1483. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1484. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1485. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1486. plugin->pData->masterMutex.lock();
  1487. if (plugin->pData->enabled)
  1488. plugin->pData->enabled = false;
  1489. if (plugin->pData->client->isActive())
  1490. plugin->pData->client->deactivate();
  1491. }
  1492. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1493. {
  1494. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1495. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1496. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1497. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1498. fPlugin->pData->enabled = true;
  1499. fPlugin->pData->client->activate();
  1500. fPlugin->pData->masterMutex.unlock();
  1501. }
  1502. // -------------------------------------------------------------------
  1503. // Scoped Process Locker
  1504. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1505. : fPlugin(plugin),
  1506. fBlock(block)
  1507. {
  1508. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1509. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1510. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1511. if (! fBlock)
  1512. return;
  1513. plugin->pData->singleMutex.lock();
  1514. }
  1515. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1516. {
  1517. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1518. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1519. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1520. if (! fBlock)
  1521. return;
  1522. #ifndef BUILD_BRIDGE
  1523. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1524. fPlugin->pData->needsReset = true;
  1525. #endif
  1526. fPlugin->pData->singleMutex.unlock();
  1527. }
  1528. // -------------------------------------------------------------------
  1529. CARLA_BACKEND_END_NAMESPACE