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.

2043 lines
64KB

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