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.

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