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.

2107 lines
66KB

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