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.

2026 lines
64KB

  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. # ifdef HAVE_LIBLO
  767. if (sendOsc && pData->engine->isOscControlRegistered())
  768. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  769. # endif
  770. if (sendCallback)
  771. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  772. #endif
  773. // may be unused
  774. return; (void)sendOsc; (void)sendCallback;
  775. }
  776. #ifndef BUILD_BRIDGE
  777. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  778. {
  779. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  780. const float fixedValue(carla_fixValue<float>(0.0f, 1.0f, value));
  781. if (carla_compareFloats(pData->postProc.dryWet, fixedValue))
  782. return;
  783. pData->postProc.dryWet = fixedValue;
  784. #ifdef HAVE_LIBLO
  785. if (sendOsc && pData->engine->isOscControlRegistered())
  786. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  787. #endif
  788. if (sendCallback)
  789. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  790. // may be unused
  791. return; (void)sendOsc;
  792. }
  793. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  794. {
  795. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  796. const float fixedValue(carla_fixValue<float>(0.0f, 1.27f, value));
  797. if (carla_compareFloats(pData->postProc.volume, fixedValue))
  798. return;
  799. pData->postProc.volume = fixedValue;
  800. #ifdef HAVE_LIBLO
  801. if (sendOsc && pData->engine->isOscControlRegistered())
  802. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  803. #endif
  804. if (sendCallback)
  805. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  806. // may be unused
  807. return; (void)sendOsc;
  808. }
  809. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  810. {
  811. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  812. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  813. if (carla_compareFloats(pData->postProc.balanceLeft, fixedValue))
  814. return;
  815. pData->postProc.balanceLeft = fixedValue;
  816. #ifdef HAVE_LIBLO
  817. if (sendOsc && pData->engine->isOscControlRegistered())
  818. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  819. #endif
  820. if (sendCallback)
  821. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  822. // may be unused
  823. return; (void)sendOsc;
  824. }
  825. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  826. {
  827. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  828. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  829. if (carla_compareFloats(pData->postProc.balanceRight, fixedValue))
  830. return;
  831. pData->postProc.balanceRight = fixedValue;
  832. #ifdef HAVE_LIBLO
  833. if (sendOsc && pData->engine->isOscControlRegistered())
  834. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  835. #endif
  836. if (sendCallback)
  837. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  838. // may be unused
  839. return; (void)sendOsc;
  840. }
  841. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  842. {
  843. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  844. const float fixedValue(carla_fixValue<float>(-1.0f, 1.0f, value));
  845. if (carla_compareFloats(pData->postProc.panning, fixedValue))
  846. return;
  847. pData->postProc.panning = fixedValue;
  848. #ifdef HAVE_LIBLO
  849. if (sendOsc && pData->engine->isOscControlRegistered())
  850. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  851. #endif
  852. if (sendCallback)
  853. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  854. // may be unused
  855. return; (void)sendOsc;
  856. }
  857. #endif // ! BUILD_BRIDGE
  858. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  859. {
  860. #ifndef BUILD_BRIDGE
  861. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  862. #endif
  863. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  864. if (pData->ctrlChannel == channel)
  865. return;
  866. pData->ctrlChannel = channel;
  867. #ifndef BUILD_BRIDGE
  868. const float channelf(channel);
  869. # ifdef HAVE_LIBLO
  870. if (sendOsc && pData->engine->isOscControlRegistered())
  871. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, channelf);
  872. # endif
  873. if (sendCallback)
  874. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, channelf, nullptr);
  875. #endif
  876. // may be unused
  877. return; (void)sendOsc; (void)sendCallback;
  878. }
  879. // -------------------------------------------------------------------
  880. // Set data (plugin-specific stuff)
  881. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  882. {
  883. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  884. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  885. uiParameterChange(parameterId, value);
  886. #ifndef BUILD_BRIDGE
  887. # ifdef HAVE_LIBLO
  888. if (sendOsc && pData->engine->isOscControlRegistered())
  889. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  890. # endif
  891. if (sendCallback)
  892. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  893. #endif
  894. // may be unused
  895. return; (void)sendOsc; (void)sendCallback;
  896. }
  897. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  898. {
  899. #ifndef BUILD_BRIDGE
  900. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  901. switch (rindex)
  902. {
  903. case PARAMETER_ACTIVE:
  904. return setActive((value > 0.0f), sendOsc, sendCallback);
  905. case PARAMETER_CTRL_CHANNEL:
  906. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  907. case PARAMETER_DRYWET:
  908. return setDryWet(value, sendOsc, sendCallback);
  909. case PARAMETER_VOLUME:
  910. return setVolume(value, sendOsc, sendCallback);
  911. case PARAMETER_BALANCE_LEFT:
  912. return setBalanceLeft(value, sendOsc, sendCallback);
  913. case PARAMETER_BALANCE_RIGHT:
  914. return setBalanceRight(value, sendOsc, sendCallback);
  915. case PARAMETER_PANNING:
  916. return setPanning(value, sendOsc, sendCallback);
  917. }
  918. #endif
  919. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  920. for (uint32_t i=0; i < pData->param.count; ++i)
  921. {
  922. if (pData->param.data[i].rindex == rindex)
  923. {
  924. //if (! carla_compareFloats(getParameterValue(i), value))
  925. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  926. break;
  927. }
  928. }
  929. }
  930. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  931. {
  932. #ifndef BUILD_BRIDGE
  933. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  934. #endif
  935. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  936. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  937. pData->param.data[parameterId].midiChannel = channel;
  938. #ifndef BUILD_BRIDGE
  939. # ifdef HAVE_LIBLO
  940. if (sendOsc && pData->engine->isOscControlRegistered())
  941. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  942. # endif
  943. if (sendCallback)
  944. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  945. #endif
  946. // may be unused
  947. return; (void)sendOsc; (void)sendCallback;
  948. }
  949. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  950. {
  951. #ifndef BUILD_BRIDGE
  952. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  953. #endif
  954. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  955. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL,);
  956. pData->param.data[parameterId].midiCC = cc;
  957. #ifndef BUILD_BRIDGE
  958. # ifdef HAVE_LIBLO
  959. if (sendOsc && pData->engine->isOscControlRegistered())
  960. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  961. # endif
  962. if (sendCallback)
  963. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  964. #endif
  965. // may be unused
  966. return; (void)sendOsc; (void)sendCallback;
  967. }
  968. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  969. {
  970. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  971. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  972. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  973. // Ignore some keys
  974. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  975. {
  976. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  977. return;
  978. }
  979. // Check if we already have this key
  980. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  981. {
  982. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  983. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  984. if (std::strcmp(customData.key, key) == 0)
  985. {
  986. if (customData.value != nullptr)
  987. delete[] customData.value;
  988. customData.value = carla_strdup(value);
  989. return;
  990. }
  991. }
  992. // Otherwise store it
  993. CustomData customData;
  994. customData.type = carla_strdup(type);
  995. customData.key = carla_strdup(key);
  996. customData.value = carla_strdup(value);
  997. pData->custom.append(customData);
  998. }
  999. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  1000. {
  1001. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1002. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  1003. CARLA_SAFE_ASSERT(false); // this should never happen
  1004. }
  1005. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1006. {
  1007. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1008. pData->prog.current = index;
  1009. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1010. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1011. if (reallySendOsc)
  1012. pData->engine->oscSend_control_set_current_program(pData->id, index);
  1013. #else
  1014. const bool reallySendOsc(false);
  1015. #endif
  1016. if (sendCallback)
  1017. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1018. // Change default parameter values
  1019. if (index >= 0)
  1020. {
  1021. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1022. uiProgramChange(static_cast<uint32_t>(index));
  1023. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1024. return;
  1025. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1026. }
  1027. // may be unused
  1028. return; (void)sendGui; (void)sendOsc;
  1029. }
  1030. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1031. {
  1032. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1033. pData->midiprog.current = index;
  1034. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1035. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1036. if (reallySendOsc)
  1037. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1038. #else
  1039. const bool reallySendOsc(false);
  1040. #endif
  1041. if (sendCallback)
  1042. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1043. if (index >= 0)
  1044. {
  1045. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1046. uiMidiProgramChange(static_cast<uint32_t>(index));
  1047. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1048. return;
  1049. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1050. }
  1051. // may be unused
  1052. return; (void)sendGui; (void)sendOsc;
  1053. }
  1054. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1055. {
  1056. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1057. {
  1058. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1059. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1060. }
  1061. }
  1062. // -------------------------------------------------------------------
  1063. // Plugin state
  1064. void CarlaPlugin::reloadPrograms(const bool)
  1065. {
  1066. }
  1067. // -------------------------------------------------------------------
  1068. // Plugin processing
  1069. void CarlaPlugin::activate() noexcept
  1070. {
  1071. CARLA_SAFE_ASSERT(! pData->active);
  1072. }
  1073. void CarlaPlugin::deactivate() noexcept
  1074. {
  1075. CARLA_SAFE_ASSERT(pData->active);
  1076. }
  1077. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1078. {
  1079. }
  1080. void CarlaPlugin::sampleRateChanged(const double)
  1081. {
  1082. }
  1083. void CarlaPlugin::offlineModeChanged(const bool)
  1084. {
  1085. }
  1086. // -------------------------------------------------------------------
  1087. // Misc
  1088. void CarlaPlugin::idle()
  1089. {
  1090. if (! pData->enabled)
  1091. return;
  1092. const bool hasUI(pData->hints & PLUGIN_HAS_CUSTOM_UI);
  1093. const bool needsUiMainThread(pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD);
  1094. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1095. const bool sendOsc(pData->engine->isOscControlRegistered());
  1096. #endif
  1097. const CarlaMutexLocker sl(pData->postRtEvents.mutex);
  1098. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.data.begin(); it.valid(); it.next())
  1099. {
  1100. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1101. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1102. switch (event.type)
  1103. {
  1104. case kPluginPostRtEventNull: {
  1105. } break;
  1106. case kPluginPostRtEventDebug: {
  1107. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1108. } break;
  1109. case kPluginPostRtEventParameterChange: {
  1110. // Update UI
  1111. if (event.value1 >= 0 && hasUI)
  1112. {
  1113. if (needsUiMainThread)
  1114. pData->postUiEvents.append(event);
  1115. else
  1116. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1117. }
  1118. if (event.value2 != 1)
  1119. {
  1120. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1121. // Update OSC control client
  1122. if (sendOsc)
  1123. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1124. #endif
  1125. // Update Host
  1126. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1127. }
  1128. } break;
  1129. case kPluginPostRtEventProgramChange: {
  1130. // Update UI
  1131. if (event.value1 >= 0 && hasUI)
  1132. {
  1133. if (needsUiMainThread)
  1134. pData->postUiEvents.append(event);
  1135. else
  1136. uiProgramChange(static_cast<uint32_t>(event.value1));
  1137. }
  1138. // Update param values
  1139. for (uint32_t j=0; j < pData->param.count; ++j)
  1140. {
  1141. const float paramDefault(pData->param.ranges[j].def);
  1142. const float paramValue(getParameterValue(j));
  1143. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1144. if (sendOsc)
  1145. {
  1146. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1147. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1148. }
  1149. #endif
  1150. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1151. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1152. }
  1153. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1154. // Update OSC control client
  1155. if (sendOsc)
  1156. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1157. #endif
  1158. // Update Host
  1159. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1160. } break;
  1161. case kPluginPostRtEventMidiProgramChange: {
  1162. // Update UI
  1163. if (event.value1 >= 0 && hasUI)
  1164. {
  1165. if (needsUiMainThread)
  1166. pData->postUiEvents.append(event);
  1167. else
  1168. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1169. }
  1170. // Update param values
  1171. for (uint32_t j=0; j < pData->param.count; ++j)
  1172. {
  1173. const float paramDefault(pData->param.ranges[j].def);
  1174. const float paramValue(getParameterValue(j));
  1175. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1176. if (sendOsc)
  1177. {
  1178. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1179. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1180. }
  1181. #endif
  1182. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1183. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1184. }
  1185. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1186. // Update OSC control client
  1187. if (sendOsc)
  1188. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1189. #endif
  1190. // Update Host
  1191. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1192. } break;
  1193. case kPluginPostRtEventNoteOn: {
  1194. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1195. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1196. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1197. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1198. const uint8_t note = static_cast<uint8_t>(event.value2);
  1199. const uint8_t velocity = uint8_t(event.value3);
  1200. // Update UI
  1201. if (hasUI)
  1202. {
  1203. if (needsUiMainThread)
  1204. pData->postUiEvents.append(event);
  1205. else
  1206. uiNoteOn(channel, note, velocity);
  1207. }
  1208. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1209. // Update OSC control client
  1210. if (sendOsc)
  1211. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1212. #endif
  1213. // Update Host
  1214. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1215. } break;
  1216. case kPluginPostRtEventNoteOff: {
  1217. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1218. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1219. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1220. const uint8_t note = static_cast<uint8_t>(event.value2);
  1221. // Update UI
  1222. if (hasUI)
  1223. {
  1224. if (needsUiMainThread)
  1225. pData->postUiEvents.append(event);
  1226. else
  1227. uiNoteOff(channel, note);
  1228. }
  1229. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1230. // Update OSC control client
  1231. if (sendOsc)
  1232. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1233. #endif
  1234. // Update Host
  1235. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1236. } break;
  1237. }
  1238. }
  1239. pData->postRtEvents.data.clear();
  1240. }
  1241. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1242. {
  1243. if (forcedOffline)
  1244. {
  1245. pData->masterMutex.lock();
  1246. return true;
  1247. }
  1248. return pData->masterMutex.tryLock();
  1249. }
  1250. void CarlaPlugin::unlock() noexcept
  1251. {
  1252. pData->masterMutex.unlock();
  1253. }
  1254. // -------------------------------------------------------------------
  1255. // Plugin buffers
  1256. void CarlaPlugin::initBuffers() const noexcept
  1257. {
  1258. pData->audioIn.initBuffers();
  1259. pData->audioOut.initBuffers();
  1260. pData->cvIn.initBuffers();
  1261. pData->cvOut.initBuffers();
  1262. pData->event.initBuffers();
  1263. }
  1264. void CarlaPlugin::clearBuffers() noexcept
  1265. {
  1266. pData->clearBuffers();
  1267. }
  1268. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1269. // -------------------------------------------------------------------
  1270. // OSC stuff
  1271. void CarlaPlugin::registerToOscClient() noexcept
  1272. {
  1273. if (! pData->engine->isOscControlRegistered())
  1274. return;
  1275. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1276. // Base data
  1277. {
  1278. char bufName[STR_MAX+1], bufLabel[STR_MAX+1], bufMaker[STR_MAX+1], bufCopyright[STR_MAX+1];
  1279. carla_zeroChar(bufName, STR_MAX);
  1280. carla_zeroChar(bufLabel, STR_MAX);
  1281. carla_zeroChar(bufMaker, STR_MAX);
  1282. carla_zeroChar(bufCopyright, STR_MAX);
  1283. getRealName(bufName);
  1284. getLabel(bufLabel);
  1285. getMaker(bufMaker);
  1286. getCopyright(bufCopyright);
  1287. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1288. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1289. }
  1290. // Base count
  1291. {
  1292. uint32_t paramIns, paramOuts;
  1293. getParameterCountInfo(paramIns, paramOuts);
  1294. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1295. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1296. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1297. }
  1298. // Plugin Parameters
  1299. if (const uint32_t count = pData->param.count)
  1300. {
  1301. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1302. for (uint32_t i=0, maxParams=pData->engine->getOptions().maxParameters; i<count && i<maxParams; ++i)
  1303. {
  1304. carla_zeroChar(bufName, STR_MAX);
  1305. carla_zeroChar(bufUnit, STR_MAX);
  1306. getParameterName(i, bufName);
  1307. getParameterUnit(i, bufUnit);
  1308. const ParameterData& paramData(pData->param.data[i]);
  1309. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1310. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1311. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1312. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1313. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1314. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1315. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1316. }
  1317. }
  1318. // Programs
  1319. if (const uint32_t count = pData->prog.count)
  1320. {
  1321. pData->engine->oscSend_control_set_program_count(pData->id, count);
  1322. for (uint32_t i=0; i < count; ++i)
  1323. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1324. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1325. }
  1326. // MIDI Programs
  1327. if (const uint32_t count = pData->midiprog.count)
  1328. {
  1329. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1330. for (uint32_t i=0; i < count; ++i)
  1331. {
  1332. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1333. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1334. }
  1335. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1336. }
  1337. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1338. // Internal Parameters
  1339. {
  1340. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1341. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1342. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1343. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1344. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1345. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1346. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1347. }
  1348. }
  1349. #endif
  1350. // FIXME
  1351. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1352. {
  1353. // do nothing
  1354. }
  1355. //#endif // HAVE_LIBLO && ! BUILD_BRIDGE
  1356. // -------------------------------------------------------------------
  1357. // MIDI events
  1358. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1359. {
  1360. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1361. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1362. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1363. if (! pData->active)
  1364. return;
  1365. ExternalMidiNote extNote;
  1366. extNote.channel = static_cast<int8_t>(channel);
  1367. extNote.note = note;
  1368. extNote.velo = velo;
  1369. pData->extNotes.appendNonRT(extNote);
  1370. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1371. {
  1372. if (velo > 0)
  1373. uiNoteOn(channel, note, velo);
  1374. else
  1375. uiNoteOff(channel, note);
  1376. }
  1377. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1378. if (sendOsc && pData->engine->isOscControlRegistered())
  1379. {
  1380. if (velo > 0)
  1381. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1382. else
  1383. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1384. }
  1385. #endif
  1386. if (sendCallback)
  1387. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1388. // may be unused
  1389. return; (void)sendOsc;
  1390. }
  1391. #ifndef BUILD_BRIDGE
  1392. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1393. {
  1394. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1395. return;
  1396. PluginPostRtEvent postEvent;
  1397. postEvent.type = kPluginPostRtEventNoteOff;
  1398. postEvent.value1 = pData->ctrlChannel;
  1399. postEvent.value2 = 0;
  1400. postEvent.value3 = 0.0f;
  1401. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1402. {
  1403. postEvent.value2 = i;
  1404. pData->postRtEvents.appendRT(postEvent);
  1405. }
  1406. }
  1407. #endif
  1408. // -------------------------------------------------------------------
  1409. // UI Stuff
  1410. void CarlaPlugin::showCustomUI(const bool)
  1411. {
  1412. CARLA_SAFE_ASSERT(false);
  1413. }
  1414. void CarlaPlugin::uiIdle()
  1415. {
  1416. if (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD)
  1417. {
  1418. // Update parameter outputs
  1419. for (uint32_t i=0; i < pData->param.count; ++i)
  1420. {
  1421. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1422. uiParameterChange(i, getParameterValue(i));
  1423. }
  1424. const CarlaMutexLocker sl(pData->postUiEvents.mutex);
  1425. for (LinkedList<PluginPostRtEvent>::Itenerator it = pData->postUiEvents.data.begin(); it.valid(); it.next())
  1426. {
  1427. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1428. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1429. switch (event.type)
  1430. {
  1431. case kPluginPostRtEventNull:
  1432. case kPluginPostRtEventDebug:
  1433. break;
  1434. case kPluginPostRtEventParameterChange:
  1435. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1436. break;
  1437. case kPluginPostRtEventProgramChange:
  1438. uiProgramChange(static_cast<uint32_t>(event.value1));
  1439. break;
  1440. case kPluginPostRtEventMidiProgramChange:
  1441. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1442. break;
  1443. case kPluginPostRtEventNoteOn:
  1444. uiNoteOn(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2), uint8_t(event.value3));
  1445. break;
  1446. case kPluginPostRtEventNoteOff:
  1447. uiNoteOff(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2));
  1448. break;
  1449. }
  1450. }
  1451. pData->postUiEvents.data.clear();
  1452. }
  1453. if (pData->transientTryCounter == 0)
  1454. return;
  1455. if (++pData->transientTryCounter % 10 != 0)
  1456. return;
  1457. if (pData->transientTryCounter >= 200)
  1458. return;
  1459. carla_stdout("Trying to get window...");
  1460. CarlaString uiTitle(pData->name);
  1461. uiTitle += " (GUI)";
  1462. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle, pData->engine->getOptions().frontendWinId, true))
  1463. pData->transientTryCounter = 0;
  1464. }
  1465. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1466. {
  1467. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1468. return;
  1469. // unused
  1470. (void)value;
  1471. }
  1472. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1473. {
  1474. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1475. }
  1476. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1477. {
  1478. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1479. }
  1480. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1481. {
  1482. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1483. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1484. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1485. }
  1486. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1487. {
  1488. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1489. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1490. }
  1491. bool CarlaPlugin::canRunInRack() const noexcept
  1492. {
  1493. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1494. }
  1495. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1496. {
  1497. return pData->engine;
  1498. }
  1499. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1500. {
  1501. return pData->client;
  1502. }
  1503. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1504. {
  1505. return pData->audioIn.ports[index].port;
  1506. }
  1507. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1508. {
  1509. return pData->audioOut.ports[index].port;
  1510. }
  1511. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  1512. {
  1513. return pData->cvIn.ports[index].port;
  1514. }
  1515. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  1516. {
  1517. return pData->cvOut.ports[index].port;
  1518. }
  1519. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  1520. {
  1521. return pData->event.portIn;
  1522. }
  1523. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  1524. {
  1525. return pData->event.portOut;
  1526. }
  1527. void* CarlaPlugin::getNativeHandle() const noexcept
  1528. {
  1529. return nullptr;
  1530. }
  1531. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  1532. {
  1533. return nullptr;
  1534. }
  1535. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  1536. {
  1537. return 0;
  1538. }
  1539. // -------------------------------------------------------------------
  1540. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  1541. {
  1542. return pData->nodeId;
  1543. }
  1544. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  1545. {
  1546. pData->nodeId = nodeId;
  1547. }
  1548. // -------------------------------------------------------------------
  1549. // Scoped Disabler
  1550. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1551. : fPlugin(plugin)
  1552. {
  1553. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1554. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1555. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1556. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1557. plugin->pData->masterMutex.lock();
  1558. if (plugin->pData->enabled)
  1559. plugin->pData->enabled = false;
  1560. if (plugin->pData->client->isActive())
  1561. plugin->pData->client->deactivate();
  1562. }
  1563. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1564. {
  1565. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1566. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1567. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1568. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1569. fPlugin->pData->enabled = true;
  1570. fPlugin->pData->client->activate();
  1571. fPlugin->pData->masterMutex.unlock();
  1572. }
  1573. // -------------------------------------------------------------------
  1574. // Scoped Process Locker
  1575. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1576. : fPlugin(plugin),
  1577. fBlock(block)
  1578. {
  1579. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1580. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1581. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1582. if (! fBlock)
  1583. return;
  1584. plugin->pData->singleMutex.lock();
  1585. }
  1586. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1587. {
  1588. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1589. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1590. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1591. if (! fBlock)
  1592. return;
  1593. #ifndef BUILD_BRIDGE
  1594. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1595. fPlugin->pData->needsReset = true;
  1596. #endif
  1597. fPlugin->pData->singleMutex.unlock();
  1598. }
  1599. // -------------------------------------------------------------------
  1600. CARLA_BACKEND_END_NAMESPACE