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.

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