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.

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