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.

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