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.

2058 lines
65KB

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