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.

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