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.

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