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.

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