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.

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