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.

1937 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. // Plugin state
  1044. void CarlaPlugin::reloadPrograms(const bool)
  1045. {
  1046. }
  1047. // -------------------------------------------------------------------
  1048. // Plugin processing
  1049. void CarlaPlugin::activate() noexcept
  1050. {
  1051. CARLA_SAFE_ASSERT(! pData->active);
  1052. }
  1053. void CarlaPlugin::deactivate() noexcept
  1054. {
  1055. CARLA_SAFE_ASSERT(pData->active);
  1056. }
  1057. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1058. {
  1059. }
  1060. void CarlaPlugin::sampleRateChanged(const double)
  1061. {
  1062. }
  1063. void CarlaPlugin::offlineModeChanged(const bool)
  1064. {
  1065. }
  1066. // -------------------------------------------------------------------
  1067. // Misc
  1068. void CarlaPlugin::idle()
  1069. {
  1070. if (! pData->enabled)
  1071. return;
  1072. postRtEventsRun();
  1073. }
  1074. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1075. {
  1076. if (forcedOffline)
  1077. {
  1078. pData->masterMutex.lock();
  1079. return true;
  1080. }
  1081. return pData->masterMutex.tryLock();
  1082. }
  1083. void CarlaPlugin::unlock() noexcept
  1084. {
  1085. pData->masterMutex.unlock();
  1086. }
  1087. // -------------------------------------------------------------------
  1088. // Plugin buffers
  1089. void CarlaPlugin::initBuffers() const noexcept
  1090. {
  1091. pData->audioIn.initBuffers();
  1092. pData->audioOut.initBuffers();
  1093. pData->cvIn.initBuffers();
  1094. pData->cvOut.initBuffers();
  1095. pData->event.initBuffers();
  1096. }
  1097. void CarlaPlugin::clearBuffers() noexcept
  1098. {
  1099. pData->clearBuffers();
  1100. }
  1101. // -------------------------------------------------------------------
  1102. // OSC stuff
  1103. #ifndef BUILD_BRIDGE
  1104. void CarlaPlugin::registerToOscClient() noexcept
  1105. {
  1106. if (! pData->engine->isOscControlRegistered())
  1107. return;
  1108. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1109. // Base data
  1110. {
  1111. char bufName[STR_MAX+1], bufLabel[STR_MAX+1], bufMaker[STR_MAX+1], bufCopyright[STR_MAX+1];
  1112. carla_zeroChar(bufName, STR_MAX);
  1113. carla_zeroChar(bufLabel, STR_MAX);
  1114. carla_zeroChar(bufMaker, STR_MAX);
  1115. carla_zeroChar(bufCopyright, STR_MAX);
  1116. getRealName(bufName);
  1117. getLabel(bufLabel);
  1118. getMaker(bufMaker);
  1119. getCopyright(bufCopyright);
  1120. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1121. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1122. }
  1123. // Base count
  1124. {
  1125. uint32_t paramIns, paramOuts;
  1126. getParameterCountInfo(paramIns, paramOuts);
  1127. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1128. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1129. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1130. }
  1131. // Plugin Parameters
  1132. if (const uint32_t count = pData->param.count)
  1133. {
  1134. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1135. for (uint32_t i=0, maxParams=pData->engine->getOptions().maxParameters; i<count && i<maxParams; ++i)
  1136. {
  1137. carla_zeroChar(bufName, STR_MAX);
  1138. carla_zeroChar(bufUnit, STR_MAX);
  1139. getParameterName(i, bufName);
  1140. getParameterUnit(i, bufUnit);
  1141. const ParameterData& paramData(pData->param.data[i]);
  1142. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1143. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1144. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1145. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1146. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1147. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1148. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1149. }
  1150. }
  1151. // Programs
  1152. if (const uint32_t count = pData->prog.count)
  1153. {
  1154. pData->engine->oscSend_control_set_program_count(pData->id, count);
  1155. for (uint32_t i=0; i < count; ++i)
  1156. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1157. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1158. }
  1159. // MIDI Programs
  1160. if (const uint32_t count = pData->midiprog.count)
  1161. {
  1162. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1163. for (uint32_t i=0; i < count; ++i)
  1164. {
  1165. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1166. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1167. }
  1168. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1169. }
  1170. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1171. // Internal Parameters
  1172. {
  1173. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1174. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1175. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1176. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1177. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1178. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1179. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1180. }
  1181. }
  1182. #endif // BUILD_BRIDGE
  1183. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1184. {
  1185. // do nothing
  1186. }
  1187. // -------------------------------------------------------------------
  1188. // MIDI events
  1189. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1190. {
  1191. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1192. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1193. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1194. if (! pData->active)
  1195. return;
  1196. ExternalMidiNote extNote;
  1197. extNote.channel = static_cast<int8_t>(channel);
  1198. extNote.note = note;
  1199. extNote.velo = velo;
  1200. pData->extNotes.appendNonRT(extNote);
  1201. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1202. {
  1203. if (velo > 0)
  1204. uiNoteOn(channel, note, velo);
  1205. else
  1206. uiNoteOff(channel, note);
  1207. }
  1208. #ifndef BUILD_BRIDGE
  1209. if (sendOsc && pData->engine->isOscControlRegistered())
  1210. {
  1211. if (velo > 0)
  1212. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1213. else
  1214. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1215. }
  1216. #endif
  1217. if (sendCallback)
  1218. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1219. #ifdef BUILD_BRIDGE
  1220. // unused
  1221. return; (void)sendOsc;
  1222. #endif
  1223. }
  1224. #ifndef BUILD_BRIDGE
  1225. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1226. {
  1227. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1228. return;
  1229. PluginPostRtEvent postEvent;
  1230. postEvent.type = kPluginPostRtEventNoteOff;
  1231. postEvent.value1 = pData->ctrlChannel;
  1232. postEvent.value2 = 0;
  1233. postEvent.value3 = 0.0f;
  1234. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1235. {
  1236. postEvent.value2 = i;
  1237. pData->postRtEvents.appendRT(postEvent);
  1238. }
  1239. }
  1240. #endif
  1241. // -------------------------------------------------------------------
  1242. // Post-poned events
  1243. void CarlaPlugin::postRtEventsRun()
  1244. {
  1245. const CarlaMutexLocker sl(pData->postRtEvents.mutex);
  1246. #ifndef BUILD_BRIDGE
  1247. const bool sendOsc(pData->engine->isOscControlRegistered());
  1248. #endif
  1249. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.data.begin(); it.valid(); it.next())
  1250. {
  1251. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1252. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1253. switch (event.type)
  1254. {
  1255. case kPluginPostRtEventNull: {
  1256. } break;
  1257. case kPluginPostRtEventDebug: {
  1258. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1259. } break;
  1260. case kPluginPostRtEventParameterChange: {
  1261. // Update UI
  1262. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1263. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1264. if (event.value2 != 1)
  1265. {
  1266. #ifndef BUILD_BRIDGE
  1267. // Update OSC control client
  1268. if (sendOsc)
  1269. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1270. #endif
  1271. // Update Host
  1272. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1273. }
  1274. } break;
  1275. case kPluginPostRtEventProgramChange: {
  1276. // Update UI
  1277. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1278. uiProgramChange(static_cast<uint32_t>(event.value1));
  1279. // Update param values
  1280. for (uint32_t j=0; j < pData->param.count; ++j)
  1281. {
  1282. const float paramDefault(pData->param.ranges[j].def);
  1283. const float paramValue(getParameterValue(j));
  1284. #ifndef BUILD_BRIDGE
  1285. if (sendOsc)
  1286. {
  1287. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1288. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1289. }
  1290. #endif
  1291. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1292. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1293. }
  1294. #ifndef BUILD_BRIDGE
  1295. // Update OSC control client
  1296. if (sendOsc)
  1297. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1298. #endif
  1299. // Update Host
  1300. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1301. } break;
  1302. case kPluginPostRtEventMidiProgramChange: {
  1303. // Update UI
  1304. if (event.value1 >= 0 && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1305. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1306. // Update param values
  1307. for (uint32_t j=0; j < pData->param.count; ++j)
  1308. {
  1309. const float paramDefault(pData->param.ranges[j].def);
  1310. const float paramValue(getParameterValue(j));
  1311. #ifndef BUILD_BRIDGE
  1312. if (sendOsc)
  1313. {
  1314. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1315. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1316. }
  1317. #endif
  1318. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1319. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1320. }
  1321. #ifndef BUILD_BRIDGE
  1322. // Update OSC control client
  1323. if (sendOsc)
  1324. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1325. #endif
  1326. // Update Host
  1327. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1328. } break;
  1329. case kPluginPostRtEventNoteOn: {
  1330. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1331. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1332. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1333. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1334. const uint8_t note = static_cast<uint8_t>(event.value2);
  1335. const uint8_t velocity = uint8_t(event.value3);
  1336. // Update UI
  1337. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1338. uiNoteOn(channel, note, velocity);
  1339. #ifndef BUILD_BRIDGE
  1340. // Update OSC control client
  1341. if (sendOsc)
  1342. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1343. #endif
  1344. // Update Host
  1345. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1346. } break;
  1347. case kPluginPostRtEventNoteOff: {
  1348. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1349. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1350. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1351. const uint8_t note = static_cast<uint8_t>(event.value2);
  1352. // Update UI
  1353. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  1354. uiNoteOff(channel, note);
  1355. #ifndef BUILD_BRIDGE
  1356. // Update OSC control client
  1357. if (sendOsc)
  1358. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1359. #endif
  1360. // Update Host
  1361. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1362. } break;
  1363. }
  1364. }
  1365. pData->postRtEvents.data.clear();
  1366. }
  1367. // -------------------------------------------------------------------
  1368. // UI Stuff
  1369. void CarlaPlugin::showCustomUI(const bool)
  1370. {
  1371. CARLA_SAFE_ASSERT(false);
  1372. }
  1373. void CarlaPlugin::uiIdle()
  1374. {
  1375. // Update parameter outputs if needed
  1376. for (uint32_t i=0; i < pData->param.count; ++i)
  1377. {
  1378. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1379. uiParameterChange(i, getParameterValue(i));
  1380. }
  1381. if (pData->transientTryCounter == 0)
  1382. return;
  1383. if (++pData->transientTryCounter % 10 != 0)
  1384. return;
  1385. if (pData->transientTryCounter >= 200)
  1386. return;
  1387. carla_stdout("Trying to get window...");
  1388. CarlaString uiTitle(pData->name);
  1389. uiTitle += " (GUI)";
  1390. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle, pData->engine->getOptions().frontendWinId, true))
  1391. pData->transientTryCounter = 0;
  1392. }
  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