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.

2097 lines
65KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaBase64Utils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include "CarlaPluginUI.hpp"
  23. #include <ctime>
  24. #include "juce_core/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. // -------------------------------------------------------------------
  730. // Set data (internal stuff)
  731. void CarlaPlugin::setId(const uint newId) noexcept
  732. {
  733. pData->id = newId;
  734. }
  735. void CarlaPlugin::setName(const char* const newName)
  736. {
  737. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  738. if (pData->name != nullptr)
  739. delete[] pData->name;
  740. pData->name = carla_strdup(newName);
  741. }
  742. void CarlaPlugin::setOption(const uint option, const bool yesNo, const bool sendCallback)
  743. {
  744. CARLA_SAFE_ASSERT_RETURN(getOptionsAvailable() & option,);
  745. if (yesNo)
  746. pData->options |= option;
  747. else
  748. pData->options &= ~option;
  749. #ifndef BUILD_BRIDGE
  750. if (sendCallback)
  751. pData->engine->callback(ENGINE_CALLBACK_OPTION_CHANGED, pData->id, static_cast<int>(option), yesNo ? 1 : 0, 0.0f, nullptr);
  752. #else
  753. // unused
  754. return; (void)sendCallback;
  755. #endif
  756. }
  757. void CarlaPlugin::setEnabled(const bool yesNo) noexcept
  758. {
  759. if (pData->enabled == yesNo)
  760. return;
  761. pData->masterMutex.lock();
  762. pData->enabled = yesNo;
  763. if (yesNo && ! pData->client->isActive())
  764. pData->client->activate();
  765. pData->masterMutex.unlock();
  766. }
  767. void CarlaPlugin::setActive(const bool active, const bool sendOsc, const bool sendCallback) noexcept
  768. {
  769. #ifndef BUILD_BRIDGE
  770. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  771. #endif
  772. if (pData->active == active)
  773. return;
  774. {
  775. const ScopedSingleProcessLocker spl(this, true);
  776. if (active)
  777. activate();
  778. else
  779. deactivate();
  780. }
  781. pData->active = active;
  782. #ifndef BUILD_BRIDGE
  783. const float value(active ? 1.0f : 0.0f);
  784. # ifdef HAVE_LIBLO
  785. if (sendOsc && pData->engine->isOscControlRegistered())
  786. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, value);
  787. # endif
  788. if (sendCallback)
  789. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_ACTIVE, 0, value, nullptr);
  790. #endif
  791. // may be unused
  792. return; (void)sendOsc; (void)sendCallback;
  793. }
  794. #ifndef BUILD_BRIDGE
  795. void CarlaPlugin::setDryWet(const float value, const bool sendOsc, const bool sendCallback) noexcept
  796. {
  797. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  798. const float fixedValue(carla_fixedValue<float>(0.0f, 1.0f, value));
  799. if (carla_isEqual(pData->postProc.dryWet, fixedValue))
  800. return;
  801. pData->postProc.dryWet = fixedValue;
  802. #ifdef HAVE_LIBLO
  803. if (sendOsc && pData->engine->isOscControlRegistered())
  804. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, fixedValue);
  805. #endif
  806. if (sendCallback)
  807. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_DRYWET, 0, fixedValue, nullptr);
  808. // may be unused
  809. return; (void)sendOsc;
  810. }
  811. void CarlaPlugin::setVolume(const float value, const bool sendOsc, const bool sendCallback) noexcept
  812. {
  813. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.27f);
  814. const float fixedValue(carla_fixedValue<float>(0.0f, 1.27f, value));
  815. if (carla_isEqual(pData->postProc.volume, fixedValue))
  816. return;
  817. pData->postProc.volume = fixedValue;
  818. #ifdef HAVE_LIBLO
  819. if (sendOsc && pData->engine->isOscControlRegistered())
  820. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, fixedValue);
  821. #endif
  822. if (sendCallback)
  823. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_VOLUME, 0, fixedValue, nullptr);
  824. // may be unused
  825. return; (void)sendOsc;
  826. }
  827. void CarlaPlugin::setBalanceLeft(const float value, const bool sendOsc, const bool sendCallback) noexcept
  828. {
  829. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  830. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  831. if (carla_isEqual(pData->postProc.balanceLeft, fixedValue))
  832. return;
  833. pData->postProc.balanceLeft = fixedValue;
  834. #ifdef HAVE_LIBLO
  835. if (sendOsc && pData->engine->isOscControlRegistered())
  836. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, fixedValue);
  837. #endif
  838. if (sendCallback)
  839. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_LEFT, 0, fixedValue, nullptr);
  840. // may be unused
  841. return; (void)sendOsc;
  842. }
  843. void CarlaPlugin::setBalanceRight(const float value, const bool sendOsc, const bool sendCallback) noexcept
  844. {
  845. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  846. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  847. if (carla_isEqual(pData->postProc.balanceRight, fixedValue))
  848. return;
  849. pData->postProc.balanceRight = fixedValue;
  850. #ifdef HAVE_LIBLO
  851. if (sendOsc && pData->engine->isOscControlRegistered())
  852. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, fixedValue);
  853. #endif
  854. if (sendCallback)
  855. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_BALANCE_RIGHT, 0, fixedValue, nullptr);
  856. // may be unused
  857. return; (void)sendOsc;
  858. }
  859. void CarlaPlugin::setPanning(const float value, const bool sendOsc, const bool sendCallback) noexcept
  860. {
  861. CARLA_SAFE_ASSERT(value >= -1.0f && value <= 1.0f);
  862. const float fixedValue(carla_fixedValue<float>(-1.0f, 1.0f, value));
  863. if (carla_isEqual(pData->postProc.panning, fixedValue))
  864. return;
  865. pData->postProc.panning = fixedValue;
  866. #ifdef HAVE_LIBLO
  867. if (sendOsc && pData->engine->isOscControlRegistered())
  868. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, fixedValue);
  869. #endif
  870. if (sendCallback)
  871. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_PANNING, 0, fixedValue, nullptr);
  872. // may be unused
  873. return; (void)sendOsc;
  874. }
  875. #endif // ! BUILD_BRIDGE
  876. void CarlaPlugin::setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  877. {
  878. #ifndef BUILD_BRIDGE
  879. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  880. #endif
  881. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  882. if (pData->ctrlChannel == channel)
  883. return;
  884. pData->ctrlChannel = channel;
  885. #ifndef BUILD_BRIDGE
  886. const float channelf(channel);
  887. # ifdef HAVE_LIBLO
  888. if (sendOsc && pData->engine->isOscControlRegistered())
  889. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, channelf);
  890. # endif
  891. if (sendCallback)
  892. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, PARAMETER_CTRL_CHANNEL, 0, channelf, nullptr);
  893. #endif
  894. // may be unused
  895. return; (void)sendOsc; (void)sendCallback;
  896. }
  897. // -------------------------------------------------------------------
  898. // Set data (plugin-specific stuff)
  899. void CarlaPlugin::setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  900. {
  901. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  902. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  903. uiParameterChange(parameterId, value);
  904. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  905. if (sendOsc && pData->engine->isOscControlRegistered())
  906. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(parameterId), value);
  907. #endif
  908. if (sendCallback)
  909. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(parameterId), 0, value, nullptr);
  910. // may be unused
  911. return; (void)sendOsc;
  912. }
  913. void CarlaPlugin::setParameterValueByRealIndex(const int32_t rindex, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  914. {
  915. #ifndef BUILD_BRIDGE
  916. CARLA_SAFE_ASSERT_RETURN(rindex > PARAMETER_MAX && rindex != PARAMETER_NULL,);
  917. switch (rindex)
  918. {
  919. case PARAMETER_ACTIVE:
  920. return setActive((value > 0.0f), sendOsc, sendCallback);
  921. case PARAMETER_CTRL_CHANNEL:
  922. return setCtrlChannel(int8_t(value), sendOsc, sendCallback);
  923. case PARAMETER_DRYWET:
  924. return setDryWet(value, sendOsc, sendCallback);
  925. case PARAMETER_VOLUME:
  926. return setVolume(value, sendOsc, sendCallback);
  927. case PARAMETER_BALANCE_LEFT:
  928. return setBalanceLeft(value, sendOsc, sendCallback);
  929. case PARAMETER_BALANCE_RIGHT:
  930. return setBalanceRight(value, sendOsc, sendCallback);
  931. case PARAMETER_PANNING:
  932. return setPanning(value, sendOsc, sendCallback);
  933. }
  934. #endif
  935. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  936. for (uint32_t i=0; i < pData->param.count; ++i)
  937. {
  938. if (pData->param.data[i].rindex == rindex)
  939. {
  940. //if (carla_isNotEqual(getParameterValue(i), value))
  941. setParameterValue(i, value, sendGui, sendOsc, sendCallback);
  942. break;
  943. }
  944. }
  945. }
  946. void CarlaPlugin::setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept
  947. {
  948. #ifndef BUILD_BRIDGE
  949. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  950. #endif
  951. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  952. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  953. pData->param.data[parameterId].midiChannel = channel;
  954. #ifndef BUILD_BRIDGE
  955. # ifdef HAVE_LIBLO
  956. if (sendOsc && pData->engine->isOscControlRegistered())
  957. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, parameterId, channel);
  958. # endif
  959. if (sendCallback)
  960. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED, pData->id, static_cast<int>(parameterId), channel, 0.0f, nullptr);
  961. #endif
  962. // may be unused
  963. return; (void)sendOsc; (void)sendCallback;
  964. }
  965. void CarlaPlugin::setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept
  966. {
  967. #ifndef BUILD_BRIDGE
  968. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  969. #endif
  970. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  971. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL,);
  972. pData->param.data[parameterId].midiCC = cc;
  973. #ifndef BUILD_BRIDGE
  974. # ifdef HAVE_LIBLO
  975. if (sendOsc && pData->engine->isOscControlRegistered())
  976. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, parameterId, cc);
  977. # endif
  978. if (sendCallback)
  979. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED, pData->id, static_cast<int>(parameterId), cc, 0.0f, nullptr);
  980. #endif
  981. // may be unused
  982. return; (void)sendOsc; (void)sendCallback;
  983. }
  984. void CarlaPlugin::setCustomData(const char* const type, const char* const key, const char* const value, const bool)
  985. {
  986. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  987. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  988. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  989. // Ignore some keys
  990. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0)
  991. {
  992. if (std::strncmp(key, "OSC:", 4) == 0 || std::strncmp(key, "CarlaAlternateFile", 18) == 0 || std::strcmp(key, "guiVisible") == 0)
  993. return;
  994. }
  995. // Check if we already have this key
  996. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  997. {
  998. CustomData& customData(it.getValue(kCustomDataFallbackNC));
  999. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  1000. if (std::strcmp(customData.key, key) == 0)
  1001. {
  1002. if (customData.value != nullptr)
  1003. delete[] customData.value;
  1004. customData.value = carla_strdup(value);
  1005. return;
  1006. }
  1007. }
  1008. // Otherwise store it
  1009. CustomData customData;
  1010. customData.type = carla_strdup(type);
  1011. customData.key = carla_strdup(key);
  1012. customData.value = carla_strdup(value);
  1013. pData->custom.append(customData);
  1014. }
  1015. void CarlaPlugin::setChunkData(const void* const data, const std::size_t dataSize)
  1016. {
  1017. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1018. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  1019. CARLA_SAFE_ASSERT(false); // this should never happen
  1020. }
  1021. void CarlaPlugin::setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1022. {
  1023. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  1024. pData->prog.current = index;
  1025. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1026. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1027. if (reallySendOsc && index < 50)
  1028. pData->engine->oscSend_control_set_current_program(pData->id, index);
  1029. #else
  1030. const bool reallySendOsc(false);
  1031. #endif
  1032. if (sendCallback)
  1033. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1034. // Change default parameter values
  1035. if (index >= 0)
  1036. {
  1037. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1038. uiProgramChange(static_cast<uint32_t>(index));
  1039. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1040. return;
  1041. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1042. }
  1043. // may be unused
  1044. return; (void)sendOsc;
  1045. }
  1046. void CarlaPlugin::setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1047. {
  1048. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  1049. pData->midiprog.current = index;
  1050. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1051. const bool reallySendOsc(sendOsc && pData->engine->isOscControlRegistered());
  1052. if (reallySendOsc && index < 50)
  1053. pData->engine->oscSend_control_set_current_midi_program(pData->id, index);
  1054. #else
  1055. const bool reallySendOsc(false);
  1056. #endif
  1057. if (sendCallback)
  1058. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  1059. if (index >= 0)
  1060. {
  1061. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1062. uiMidiProgramChange(static_cast<uint32_t>(index));
  1063. if (getType() == PLUGIN_GIG || getType() == PLUGIN_SF2 || getType() == PLUGIN_SFZ)
  1064. return;
  1065. pData->updateParameterValues(this, reallySendOsc, sendCallback, true);
  1066. }
  1067. // may be unused
  1068. return; (void)sendOsc;
  1069. }
  1070. void CarlaPlugin::setMidiProgramById(const uint32_t bank, const uint32_t program, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept
  1071. {
  1072. for (uint32_t i=0; i < pData->midiprog.count; ++i)
  1073. {
  1074. if (pData->midiprog.data[i].bank == bank && pData->midiprog.data[i].program == program)
  1075. return setMidiProgram(static_cast<int32_t>(i), sendGui, sendOsc, sendCallback);
  1076. }
  1077. }
  1078. // -------------------------------------------------------------------
  1079. // Plugin state
  1080. void CarlaPlugin::reloadPrograms(const bool)
  1081. {
  1082. }
  1083. // -------------------------------------------------------------------
  1084. // Plugin processing
  1085. void CarlaPlugin::activate() noexcept
  1086. {
  1087. CARLA_SAFE_ASSERT(! pData->active);
  1088. }
  1089. void CarlaPlugin::deactivate() noexcept
  1090. {
  1091. CARLA_SAFE_ASSERT(pData->active);
  1092. }
  1093. void CarlaPlugin::bufferSizeChanged(const uint32_t)
  1094. {
  1095. }
  1096. void CarlaPlugin::sampleRateChanged(const double)
  1097. {
  1098. }
  1099. void CarlaPlugin::offlineModeChanged(const bool)
  1100. {
  1101. }
  1102. // -------------------------------------------------------------------
  1103. // Misc
  1104. void CarlaPlugin::idle()
  1105. {
  1106. if (! pData->enabled)
  1107. return;
  1108. const bool hasUI(pData->hints & PLUGIN_HAS_CUSTOM_UI);
  1109. const bool needsUiMainThread(pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD);
  1110. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1111. const bool sendOsc(pData->engine->isOscControlRegistered());
  1112. #endif
  1113. const uint32_t latency(getLatencyInFrames());
  1114. if (pData->latency.frames != latency)
  1115. {
  1116. carla_stdout("latency changed to %i samples", latency);
  1117. const ScopedSingleProcessLocker sspl(this, true);
  1118. pData->client->setLatency(latency);
  1119. #ifndef BUILD_BRIDGE
  1120. pData->latency.recreateBuffers(pData->latency.channels, latency);
  1121. #else
  1122. pData->latency.frames = latency;
  1123. #endif
  1124. }
  1125. const CarlaMutexLocker sl(pData->postRtEvents.mutex);
  1126. for (RtLinkedList<PluginPostRtEvent>::Itenerator it = pData->postRtEvents.data.begin2(); it.valid(); it.next())
  1127. {
  1128. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1129. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1130. switch (event.type)
  1131. {
  1132. case kPluginPostRtEventNull: {
  1133. } break;
  1134. case kPluginPostRtEventDebug: {
  1135. pData->engine->callback(ENGINE_CALLBACK_DEBUG, pData->id, event.value1, event.value2, event.value3, nullptr);
  1136. } break;
  1137. case kPluginPostRtEventParameterChange: {
  1138. // Update UI
  1139. if (event.value1 >= 0 && hasUI)
  1140. {
  1141. if (needsUiMainThread)
  1142. pData->postUiEvents.append(event);
  1143. else
  1144. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1145. }
  1146. if (event.value2 != 1)
  1147. {
  1148. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1149. // Update OSC control client
  1150. if (sendOsc)
  1151. pData->engine->oscSend_control_set_parameter_value(pData->id, event.value1, event.value3);
  1152. #endif
  1153. // Update Host
  1154. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, event.value1, 0, event.value3, nullptr);
  1155. }
  1156. } break;
  1157. case kPluginPostRtEventProgramChange: {
  1158. // Update UI
  1159. if (event.value1 >= 0 && hasUI)
  1160. {
  1161. if (needsUiMainThread)
  1162. pData->postUiEvents.append(event);
  1163. else
  1164. uiProgramChange(static_cast<uint32_t>(event.value1));
  1165. }
  1166. // Update param values
  1167. for (uint32_t j=0; j < pData->param.count; ++j)
  1168. {
  1169. const float paramDefault(pData->param.ranges[j].def);
  1170. const float paramValue(getParameterValue(j));
  1171. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1172. if (sendOsc && j < 50)
  1173. {
  1174. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1175. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1176. }
  1177. #endif
  1178. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1179. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1180. }
  1181. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1182. // Update OSC control client
  1183. if (sendOsc)
  1184. pData->engine->oscSend_control_set_current_program(pData->id, event.value1);
  1185. #endif
  1186. // Update Host
  1187. pData->engine->callback(ENGINE_CALLBACK_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1188. } break;
  1189. case kPluginPostRtEventMidiProgramChange: {
  1190. // Update UI
  1191. if (event.value1 >= 0 && hasUI)
  1192. {
  1193. if (needsUiMainThread)
  1194. pData->postUiEvents.append(event);
  1195. else
  1196. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1197. }
  1198. // Update param values
  1199. for (uint32_t j=0; j < pData->param.count; ++j)
  1200. {
  1201. const float paramDefault(pData->param.ranges[j].def);
  1202. const float paramValue(getParameterValue(j));
  1203. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1204. if (sendOsc && j < 50)
  1205. {
  1206. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(j), paramValue);
  1207. pData->engine->oscSend_control_set_default_value(pData->id, j, paramDefault);
  1208. }
  1209. #endif
  1210. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED, pData->id, static_cast<int>(j), 0, paramValue, nullptr);
  1211. pData->engine->callback(ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED, pData->id, static_cast<int>(j), 0, paramDefault, nullptr);
  1212. }
  1213. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1214. // Update OSC control client
  1215. if (sendOsc)
  1216. pData->engine->oscSend_control_set_current_midi_program(pData->id, event.value1);
  1217. #endif
  1218. // Update Host
  1219. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, event.value1, 0, 0.0f, nullptr);
  1220. } break;
  1221. case kPluginPostRtEventNoteOn: {
  1222. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1223. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1224. CARLA_SAFE_ASSERT_BREAK(event.value3 >= 0 && event.value3 < MAX_MIDI_VALUE);
  1225. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1226. const uint8_t note = static_cast<uint8_t>(event.value2);
  1227. const uint8_t velocity = uint8_t(event.value3);
  1228. // Update UI
  1229. if (hasUI)
  1230. {
  1231. if (needsUiMainThread)
  1232. pData->postUiEvents.append(event);
  1233. else
  1234. uiNoteOn(channel, note, velocity);
  1235. }
  1236. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1237. // Update OSC control client
  1238. if (sendOsc)
  1239. pData->engine->oscSend_control_note_on(pData->id, channel, note, velocity);
  1240. #endif
  1241. // Update Host
  1242. pData->engine->callback(ENGINE_CALLBACK_NOTE_ON, pData->id, event.value1, event.value2, event.value3, nullptr);
  1243. } break;
  1244. case kPluginPostRtEventNoteOff: {
  1245. CARLA_SAFE_ASSERT_BREAK(event.value1 >= 0 && event.value1 < MAX_MIDI_CHANNELS);
  1246. CARLA_SAFE_ASSERT_BREAK(event.value2 >= 0 && event.value2 < MAX_MIDI_NOTE);
  1247. const uint8_t channel = static_cast<uint8_t>(event.value1);
  1248. const uint8_t note = static_cast<uint8_t>(event.value2);
  1249. // Update UI
  1250. if (hasUI)
  1251. {
  1252. if (needsUiMainThread)
  1253. pData->postUiEvents.append(event);
  1254. else
  1255. uiNoteOff(channel, note);
  1256. }
  1257. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1258. // Update OSC control client
  1259. if (sendOsc)
  1260. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1261. #endif
  1262. // Update Host
  1263. pData->engine->callback(ENGINE_CALLBACK_NOTE_OFF, pData->id, event.value1, event.value2, 0.0f, nullptr);
  1264. } break;
  1265. }
  1266. }
  1267. pData->postRtEvents.data.clear();
  1268. }
  1269. bool CarlaPlugin::tryLock(const bool forcedOffline) noexcept
  1270. {
  1271. if (forcedOffline)
  1272. {
  1273. pData->masterMutex.lock();
  1274. return true;
  1275. }
  1276. return pData->masterMutex.tryLock();
  1277. }
  1278. void CarlaPlugin::unlock() noexcept
  1279. {
  1280. pData->masterMutex.unlock();
  1281. }
  1282. // -------------------------------------------------------------------
  1283. // Plugin buffers
  1284. void CarlaPlugin::initBuffers() const noexcept
  1285. {
  1286. pData->audioIn.initBuffers();
  1287. pData->audioOut.initBuffers();
  1288. pData->cvIn.initBuffers();
  1289. pData->cvOut.initBuffers();
  1290. pData->event.initBuffers();
  1291. }
  1292. void CarlaPlugin::clearBuffers() noexcept
  1293. {
  1294. pData->clearBuffers();
  1295. }
  1296. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1297. // -------------------------------------------------------------------
  1298. // OSC stuff
  1299. void CarlaPlugin::registerToOscClient() noexcept
  1300. {
  1301. if (! pData->engine->isOscControlRegistered())
  1302. return;
  1303. pData->engine->oscSend_control_add_plugin_start(pData->id, pData->name);
  1304. // Base data
  1305. {
  1306. char bufName[STR_MAX+1], bufLabel[STR_MAX+1], bufMaker[STR_MAX+1], bufCopyright[STR_MAX+1];
  1307. carla_zeroChars(bufName, STR_MAX);
  1308. carla_zeroChars(bufLabel, STR_MAX);
  1309. carla_zeroChars(bufMaker, STR_MAX);
  1310. carla_zeroChars(bufCopyright, STR_MAX);
  1311. getRealName(bufName);
  1312. getLabel(bufLabel);
  1313. getMaker(bufMaker);
  1314. getCopyright(bufCopyright);
  1315. pData->engine->oscSend_control_set_plugin_info1(pData->id, getType(), getCategory(), pData->hints, getUniqueId());
  1316. pData->engine->oscSend_control_set_plugin_info2(pData->id, bufName, bufLabel, bufMaker, bufCopyright);
  1317. }
  1318. // Base count
  1319. uint32_t paramIns, paramOuts;
  1320. {
  1321. getParameterCountInfo(paramIns, paramOuts);
  1322. if (paramIns > 49)
  1323. paramIns = 49;
  1324. if (paramOuts > 49)
  1325. paramOuts = 49;
  1326. pData->engine->oscSend_control_set_audio_count(pData->id, getAudioInCount(), getAudioOutCount());
  1327. pData->engine->oscSend_control_set_midi_count(pData->id, getMidiInCount(), getMidiOutCount());
  1328. pData->engine->oscSend_control_set_parameter_count(pData->id, paramIns, paramOuts);
  1329. }
  1330. // Plugin Parameters
  1331. if (const uint32_t count = std::min<uint32_t>(pData->param.count, 98U))
  1332. {
  1333. char bufName[STR_MAX+1], bufUnit[STR_MAX+1];
  1334. for (uint32_t i=0; i<count; ++i)
  1335. {
  1336. const ParameterData& paramData(pData->param.data[i]);
  1337. if (paramData.type == PARAMETER_INPUT)
  1338. {
  1339. if (--paramIns == 0)
  1340. break;
  1341. }
  1342. else if (paramData.type == PARAMETER_INPUT)
  1343. {
  1344. if (--paramOuts == 0)
  1345. break;
  1346. }
  1347. else
  1348. {
  1349. continue;
  1350. }
  1351. const ParameterRanges& paramRanges(pData->param.ranges[i]);
  1352. carla_zeroChars(bufName, STR_MAX);
  1353. carla_zeroChars(bufUnit, STR_MAX);
  1354. getParameterName(i, bufName);
  1355. getParameterUnit(i, bufUnit);
  1356. pData->engine->oscSend_control_set_parameter_data(pData->id, i, paramData.type, paramData.hints, bufName, bufUnit);
  1357. pData->engine->oscSend_control_set_parameter_ranges1(pData->id, i, paramRanges.def, paramRanges.min, paramRanges.max);
  1358. pData->engine->oscSend_control_set_parameter_ranges2(pData->id, i, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  1359. pData->engine->oscSend_control_set_parameter_value(pData->id, static_cast<int32_t>(i), getParameterValue(i));
  1360. if (paramData.midiCC >= 0)
  1361. pData->engine->oscSend_control_set_parameter_midi_cc(pData->id, i, paramData.midiCC);
  1362. if (paramData.midiChannel != 0)
  1363. pData->engine->oscSend_control_set_parameter_midi_channel(pData->id, i, paramData.midiChannel);
  1364. }
  1365. }
  1366. // Programs
  1367. if (const uint32_t count = std::min<uint32_t>(pData->prog.count, 50U))
  1368. {
  1369. pData->engine->oscSend_control_set_program_count(pData->id, count);
  1370. for (uint32_t i=0; i < count; ++i)
  1371. pData->engine->oscSend_control_set_program_name(pData->id, i, pData->prog.names[i]);
  1372. pData->engine->oscSend_control_set_current_program(pData->id, pData->prog.current);
  1373. }
  1374. // MIDI Programs
  1375. if (const uint32_t count = std::min<uint32_t>(pData->midiprog.count, 50U))
  1376. {
  1377. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1378. for (uint32_t i=0; i < count; ++i)
  1379. {
  1380. const MidiProgramData& mpData(pData->midiprog.data[i]);
  1381. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, mpData.bank, mpData.program, mpData.name);
  1382. }
  1383. pData->engine->oscSend_control_set_current_midi_program(pData->id, pData->midiprog.current);
  1384. }
  1385. pData->engine->oscSend_control_add_plugin_end(pData->id);
  1386. // Internal Parameters
  1387. {
  1388. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_DRYWET, pData->postProc.dryWet);
  1389. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_VOLUME, pData->postProc.volume);
  1390. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_LEFT, pData->postProc.balanceLeft);
  1391. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_BALANCE_RIGHT, pData->postProc.balanceRight);
  1392. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_PANNING, pData->postProc.panning);
  1393. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_CTRL_CHANNEL, pData->ctrlChannel);
  1394. pData->engine->oscSend_control_set_parameter_value(pData->id, PARAMETER_ACTIVE, pData->active ? 1.0f : 0.0f);
  1395. }
  1396. }
  1397. #endif
  1398. // FIXME
  1399. void CarlaPlugin::handleOscMessage(const char* const, const int, const void* const, const char* const, const lo_message)
  1400. {
  1401. // do nothing
  1402. }
  1403. //#endif // HAVE_LIBLO && ! BUILD_BRIDGE
  1404. // -------------------------------------------------------------------
  1405. // MIDI events
  1406. void CarlaPlugin::sendMidiSingleNote(const uint8_t channel, const uint8_t note, const uint8_t velo, const bool sendGui, const bool sendOsc, const bool sendCallback)
  1407. {
  1408. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1409. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1410. CARLA_SAFE_ASSERT_RETURN(velo < MAX_MIDI_VALUE,);
  1411. if (! pData->active)
  1412. return;
  1413. ExternalMidiNote extNote;
  1414. extNote.channel = static_cast<int8_t>(channel);
  1415. extNote.note = note;
  1416. extNote.velo = velo;
  1417. pData->extNotes.appendNonRT(extNote);
  1418. if (sendGui && (pData->hints & PLUGIN_HAS_CUSTOM_UI) != 0)
  1419. {
  1420. if (velo > 0)
  1421. uiNoteOn(channel, note, velo);
  1422. else
  1423. uiNoteOff(channel, note);
  1424. }
  1425. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  1426. if (sendOsc && pData->engine->isOscControlRegistered())
  1427. {
  1428. if (velo > 0)
  1429. pData->engine->oscSend_control_note_on(pData->id, channel, note, velo);
  1430. else
  1431. pData->engine->oscSend_control_note_off(pData->id, channel, note);
  1432. }
  1433. #endif
  1434. if (sendCallback)
  1435. pData->engine->callback((velo > 0) ? ENGINE_CALLBACK_NOTE_ON : ENGINE_CALLBACK_NOTE_OFF, pData->id, channel, note, velo, nullptr);
  1436. // may be unused
  1437. return; (void)sendOsc;
  1438. }
  1439. #ifndef BUILD_BRIDGE
  1440. void CarlaPlugin::sendMidiAllNotesOffToCallback()
  1441. {
  1442. if (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS)
  1443. return;
  1444. PluginPostRtEvent postEvent;
  1445. postEvent.type = kPluginPostRtEventNoteOff;
  1446. postEvent.value1 = pData->ctrlChannel;
  1447. postEvent.value2 = 0;
  1448. postEvent.value3 = 0.0f;
  1449. for (int32_t i=0; i < MAX_MIDI_NOTE; ++i)
  1450. {
  1451. postEvent.value2 = i;
  1452. pData->postRtEvents.appendRT(postEvent);
  1453. }
  1454. }
  1455. #endif
  1456. // -------------------------------------------------------------------
  1457. // UI Stuff
  1458. void CarlaPlugin::showCustomUI(const bool)
  1459. {
  1460. CARLA_SAFE_ASSERT(false);
  1461. }
  1462. void CarlaPlugin::uiIdle()
  1463. {
  1464. if (pData->hints & PLUGIN_NEEDS_UI_MAIN_THREAD)
  1465. {
  1466. // Update parameter outputs
  1467. for (uint32_t i=0; i < pData->param.count; ++i)
  1468. {
  1469. if (pData->param.data[i].type == PARAMETER_OUTPUT)
  1470. uiParameterChange(i, getParameterValue(i));
  1471. }
  1472. const CarlaMutexLocker sl(pData->postUiEvents.mutex);
  1473. for (LinkedList<PluginPostRtEvent>::Itenerator it = pData->postUiEvents.data.begin2(); it.valid(); it.next())
  1474. {
  1475. const PluginPostRtEvent& event(it.getValue(kPluginPostRtEventFallback));
  1476. CARLA_SAFE_ASSERT_CONTINUE(event.type != kPluginPostRtEventNull);
  1477. switch (event.type)
  1478. {
  1479. case kPluginPostRtEventNull:
  1480. case kPluginPostRtEventDebug:
  1481. break;
  1482. case kPluginPostRtEventParameterChange:
  1483. uiParameterChange(static_cast<uint32_t>(event.value1), event.value3);
  1484. break;
  1485. case kPluginPostRtEventProgramChange:
  1486. uiProgramChange(static_cast<uint32_t>(event.value1));
  1487. break;
  1488. case kPluginPostRtEventMidiProgramChange:
  1489. uiMidiProgramChange(static_cast<uint32_t>(event.value1));
  1490. break;
  1491. case kPluginPostRtEventNoteOn:
  1492. uiNoteOn(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2), uint8_t(event.value3));
  1493. break;
  1494. case kPluginPostRtEventNoteOff:
  1495. uiNoteOff(static_cast<uint8_t>(event.value1), static_cast<uint8_t>(event.value2));
  1496. break;
  1497. }
  1498. }
  1499. pData->postUiEvents.data.clear();
  1500. }
  1501. if (pData->transientTryCounter == 0)
  1502. return;
  1503. if (++pData->transientTryCounter % 10 != 0)
  1504. return;
  1505. if (pData->transientTryCounter >= 200)
  1506. return;
  1507. carla_stdout("Trying to get window...");
  1508. CarlaString uiTitle(pData->name);
  1509. uiTitle += " (GUI)";
  1510. if (CarlaPluginUI::tryTransientWinIdMatch(getUiBridgeProcessId(), uiTitle, pData->engine->getOptions().frontendWinId, true))
  1511. pData->transientTryCounter = 0;
  1512. }
  1513. void CarlaPlugin::uiParameterChange(const uint32_t index, const float value) noexcept
  1514. {
  1515. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  1516. return;
  1517. // unused
  1518. (void)value;
  1519. }
  1520. void CarlaPlugin::uiProgramChange(const uint32_t index) noexcept
  1521. {
  1522. CARLA_SAFE_ASSERT_RETURN(index < getProgramCount(),);
  1523. }
  1524. void CarlaPlugin::uiMidiProgramChange(const uint32_t index) noexcept
  1525. {
  1526. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(),);
  1527. }
  1528. void CarlaPlugin::uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept
  1529. {
  1530. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1531. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1532. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1533. }
  1534. void CarlaPlugin::uiNoteOff(const uint8_t channel, const uint8_t note) noexcept
  1535. {
  1536. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1537. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1538. }
  1539. bool CarlaPlugin::canRunInRack() const noexcept
  1540. {
  1541. return (pData->extraHints & PLUGIN_EXTRA_HINT_CAN_RUN_RACK) != 0;
  1542. }
  1543. CarlaEngine* CarlaPlugin::getEngine() const noexcept
  1544. {
  1545. return pData->engine;
  1546. }
  1547. CarlaEngineClient* CarlaPlugin::getEngineClient() const noexcept
  1548. {
  1549. return pData->client;
  1550. }
  1551. CarlaEngineAudioPort* CarlaPlugin::getAudioInPort(const uint32_t index) const noexcept
  1552. {
  1553. return pData->audioIn.ports[index].port;
  1554. }
  1555. CarlaEngineAudioPort* CarlaPlugin::getAudioOutPort(const uint32_t index) const noexcept
  1556. {
  1557. return pData->audioOut.ports[index].port;
  1558. }
  1559. CarlaEngineCVPort* CarlaPlugin::getCVInPort(const uint32_t index) const noexcept
  1560. {
  1561. return pData->cvIn.ports[index].port;
  1562. }
  1563. CarlaEngineCVPort* CarlaPlugin::getCVOutPort(const uint32_t index) const noexcept
  1564. {
  1565. return pData->cvOut.ports[index].port;
  1566. }
  1567. CarlaEngineEventPort* CarlaPlugin::getDefaultEventInPort() const noexcept
  1568. {
  1569. return pData->event.portIn;
  1570. }
  1571. CarlaEngineEventPort* CarlaPlugin::getDefaultEventOutPort() const noexcept
  1572. {
  1573. return pData->event.portOut;
  1574. }
  1575. void* CarlaPlugin::getNativeHandle() const noexcept
  1576. {
  1577. return nullptr;
  1578. }
  1579. const void* CarlaPlugin::getNativeDescriptor() const noexcept
  1580. {
  1581. return nullptr;
  1582. }
  1583. uintptr_t CarlaPlugin::getUiBridgeProcessId() const noexcept
  1584. {
  1585. return 0;
  1586. }
  1587. // -------------------------------------------------------------------
  1588. uint32_t CarlaPlugin::getPatchbayNodeId() const noexcept
  1589. {
  1590. return pData->nodeId;
  1591. }
  1592. void CarlaPlugin::setPatchbayNodeId(const uint32_t nodeId) noexcept
  1593. {
  1594. pData->nodeId = nodeId;
  1595. }
  1596. // -------------------------------------------------------------------
  1597. // Scoped Disabler
  1598. CarlaPlugin::ScopedDisabler::ScopedDisabler(CarlaPlugin* const plugin) noexcept
  1599. : fPlugin(plugin),
  1600. fWasEnabled(false)
  1601. {
  1602. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1603. CARLA_SAFE_ASSERT_RETURN(plugin->pData != nullptr,);
  1604. CARLA_SAFE_ASSERT_RETURN(plugin->pData->client != nullptr,);
  1605. carla_debug("CarlaPlugin::ScopedDisabler(%p)", plugin);
  1606. plugin->pData->masterMutex.lock();
  1607. if (plugin->pData->enabled)
  1608. {
  1609. fWasEnabled = true;
  1610. plugin->pData->enabled = false;
  1611. if (plugin->pData->client->isActive())
  1612. plugin->pData->client->deactivate();
  1613. }
  1614. }
  1615. CarlaPlugin::ScopedDisabler::~ScopedDisabler() noexcept
  1616. {
  1617. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1618. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1619. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData->client != nullptr,);
  1620. carla_debug("CarlaPlugin::~ScopedDisabler()");
  1621. if (fWasEnabled)
  1622. {
  1623. fPlugin->pData->enabled = true;
  1624. fPlugin->pData->client->activate();
  1625. }
  1626. fPlugin->pData->masterMutex.unlock();
  1627. }
  1628. // -------------------------------------------------------------------
  1629. // Scoped Process Locker
  1630. CarlaPlugin::ScopedSingleProcessLocker::ScopedSingleProcessLocker(CarlaPlugin* const plugin, const bool block) noexcept
  1631. : fPlugin(plugin),
  1632. fBlock(block)
  1633. {
  1634. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1635. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1636. carla_debug("CarlaPlugin::ScopedSingleProcessLocker(%p, %s)", plugin, bool2str(block));
  1637. if (! fBlock)
  1638. return;
  1639. plugin->pData->singleMutex.lock();
  1640. }
  1641. CarlaPlugin::ScopedSingleProcessLocker::~ScopedSingleProcessLocker() noexcept
  1642. {
  1643. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1644. CARLA_SAFE_ASSERT_RETURN(fPlugin->pData != nullptr,);
  1645. carla_debug("CarlaPlugin::~ScopedSingleProcessLocker()");
  1646. if (! fBlock)
  1647. return;
  1648. #ifndef BUILD_BRIDGE
  1649. if (fPlugin->pData->singleMutex.wasTryLockCalled())
  1650. fPlugin->pData->needsReset = true;
  1651. #endif
  1652. fPlugin->pData->singleMutex.unlock();
  1653. }
  1654. // -------------------------------------------------------------------
  1655. CARLA_BACKEND_END_NAMESPACE